Add some missing includes to kill warnings when building with the default
[oweals/busybox.git] / ash.h
1 /*-
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 /*      $NetBSD: alias.h,v 1.4 1995/05/11 21:28:42 christos Exp $       */
38
39 #define ALIASINUSE      1
40 #define ALIASDEAD       2
41
42 struct alias {
43         struct alias *next;
44         char *name;
45         char *val;
46         int flag;
47 };
48
49 struct alias *lookupalias __P((const char *, int));
50 static int aliascmd __P((int, char **));
51 static int unaliascmd __P((int, char **));
52 static void rmaliases __P((void));
53 static int unalias __P((char *));
54 static void printalias __P((const struct alias *));
55 #define ARITH_NUM 257
56 #define ARITH_LPAREN 258
57 #define ARITH_RPAREN 259
58 #define ARITH_OR 260
59 #define ARITH_AND 261
60 #define ARITH_BOR 262
61 #define ARITH_BXOR 263
62 #define ARITH_BAND 264
63 #define ARITH_EQ 265
64 #define ARITH_NE 266
65 #define ARITH_LT 267
66 #define ARITH_GT 268
67 #define ARITH_GE 269
68 #define ARITH_LE 270
69 #define ARITH_LSHIFT 271
70 #define ARITH_RSHIFT 272
71 #define ARITH_ADD 273
72 #define ARITH_SUB 274
73 #define ARITH_MUL 275
74 #define ARITH_DIV 276
75 #define ARITH_REM 277
76 #define ARITH_UNARYMINUS 278
77 #define ARITH_UNARYPLUS 279
78 #define ARITH_NOT 280
79 #define ARITH_BNOT 281
80
81 /*
82  * This file was generated by the mkbuiltins program.
83  */
84
85
86 #define BUILTIN_SPECIAL 0x1
87 #define BUILTIN_REGULAR 0x2
88 #define BUILTIN_ASSIGN 0x4
89
90 struct builtincmd {
91         const char *name;
92         int (*const builtinfunc) __P((int, char **));
93         unsigned flags;
94 };
95
96 extern const struct builtincmd builtincmds[];
97
98
99
100 /*      $NetBSD: cd.h,v 1.2 1997/07/04 21:01:52 christos Exp $  */
101 static int      cdcmd __P((int, char **));
102 static int      pwdcmd __P((int, char **));
103 static void     setpwd __P((const char *, int));
104
105
106 /*      $NetBSD: error.h,v 1.14 2001/02/04 19:52:06 christos Exp $      */
107
108 /*
109  * Types of operations (passed to the errmsg routine).
110  */
111
112 #define E_OPEN 01       /* opening a file */
113 #define E_CREAT 02      /* creating a file */
114 #define E_EXEC 04       /* executing a program */
115
116
117 /*
118  * We enclose jmp_buf in a structure so that we can declare pointers to
119  * jump locations.  The global variable handler contains the location to
120  * jump to when an exception occurs, and the global variable exception
121  * contains a code identifying the exeception.  To implement nested
122  * exception handlers, the user should save the value of handler on entry
123  * to an inner scope, set handler to point to a jmploc structure for the
124  * inner scope, and restore handler on exit from the scope.
125  */
126
127 struct jmploc {
128         jmp_buf loc;
129 };
130
131 extern struct jmploc *handler;
132 extern int exception;
133
134 /* exceptions */
135 #define EXINT 0         /* SIGINT received */
136 #define EXERROR 1       /* a generic error */
137 #define EXSHELLPROC 2   /* execute a shell procedure */
138 #define EXEXEC 3        /* command execution failed */
139
140
141 /*
142  * These macros allow the user to suspend the handling of interrupt signals
143  * over a period of time.  This is similar to SIGHOLD to or sigblock, but
144  * much more efficient and portable.  (But hacking the kernel is so much
145  * more fun than worrying about efficiency and portability. :-))
146  */
147
148 extern int suppressint;
149 extern volatile int intpending;
150
151 #define INTOFF suppressint++
152 #ifdef REALLY_SMALL
153 static void __inton __P((void));
154 #define INTON __inton()
155 #else
156 #define INTON { if (--suppressint == 0 && intpending) onint(); }
157 #endif
158 #define FORCEINTON {suppressint = 0; if (intpending) onint();}
159 #define CLEAR_PENDING_INT intpending = 0
160 #define int_pending() intpending
161
162 static void exraise __P((int)) __attribute__((__noreturn__));
163 static void onint __P((void));
164 static void error __P((const char *, ...)) __attribute__((__noreturn__));
165 static void exerror __P((int, const char *, ...)) __attribute__((__noreturn__));
166 static const char *errmsg __P((int, int));
167
168
169 /*
170  * BSD setjmp saves the signal mask, which violates ANSI C and takes time,
171  * so we use _setjmp instead.
172  */
173
174 #if defined(BSD) && !defined(__SVR4) && !defined(__GLIBC__)
175 #define setjmp(jmploc)  _setjmp(jmploc)
176 #define longjmp(jmploc, val)    _longjmp(jmploc, val)
177 #endif
178
179
180
181 /*      $NetBSD: shell.h,v 1.13 2000/05/22 10:18:47 elric Exp $ */
182
183 /*
184  * The follow should be set to reflect the type of system you have:
185  *      JOBS -> 1 if you have Berkeley job control, 0 otherwise.
186  *      SHORTNAMES -> 1 if your linker cannot handle long names.
187  *      define BSD if you are running 4.2 BSD or later.
188  *      define SYSV if you are running under System V.
189  *      define DEBUG=1 to compile in debugging (set global "debug" to turn on)
190  *      define DEBUG=2 to compile in and turn on debugging.
191  *
192  * When debugging is on, debugging info will be written to $HOME/trace and
193  * a quit signal will generate a core dump.
194  */
195
196
197 #define JOBS 1
198 #ifndef BSD
199 #define BSD 1
200 #endif
201
202 #ifdef __STDC__
203 typedef void *pointer;
204 #ifndef NULL
205 #define NULL (void *)0
206 #endif
207 #else /* not __STDC__ */
208 typedef char *pointer;
209 #ifndef NULL
210 #define NULL 0
211 #endif
212 #endif /*  not __STDC__ */
213
214 extern char nullstr[1];         /* null string */
215
216
217 #ifdef DEBUG
218 #define TRACE(param)    trace param
219 #else
220 #define TRACE(param)
221 #endif
222
223
224
225
226 /*
227  * This file was generated by the mknodes program.
228  */
229
230 #define NSEMI 0
231 #define NCMD 1
232 #define NPIPE 2
233 #define NREDIR 3
234 #define NBACKGND 4
235 #define NSUBSHELL 5
236 #define NAND 6
237 #define NOR 7
238 #define NIF 8
239 #define NWHILE 9
240 #define NUNTIL 10
241 #define NFOR 11
242 #define NCASE 12
243 #define NCLIST 13
244 #define NDEFUN 14
245 #define NARG 15
246 #define NTO 16
247 #define NFROM 17
248 #define NFROMTO 18
249 #define NAPPEND 19
250 #define NTOOV 20
251 #define NTOFD 21
252 #define NFROMFD 22
253 #define NHERE 23
254 #define NXHERE 24
255 #define NNOT 25
256
257
258
259 struct nbinary {
260       int type;
261       union node *ch1;
262       union node *ch2;
263 };
264
265
266 struct ncmd {
267       int type;
268       int backgnd;
269       union node *assign;
270       union node *args;
271       union node *redirect;
272 };
273
274
275 struct npipe {
276       int type;
277       int backgnd;
278       struct nodelist *cmdlist;
279 };
280
281
282 struct nredir {
283       int type;
284       union node *n;
285       union node *redirect;
286 };
287
288
289 struct nif {
290       int type;
291       union node *test;
292       union node *ifpart;
293       union node *elsepart;
294 };
295
296
297 struct nfor {
298       int type;
299       union node *args;
300       union node *body;
301       char *var;
302 };
303
304
305 struct ncase {
306       int type;
307       union node *expr;
308       union node *cases;
309 };
310
311
312 struct nclist {
313       int type;
314       union node *next;
315       union node *pattern;
316       union node *body;
317 };
318
319
320 struct narg {
321       int type;
322       union node *next;
323       char *text;
324       struct nodelist *backquote;
325 };
326
327
328 struct nfile {
329       int type;
330       union node *next;
331       int fd;
332       union node *fname;
333       char *expfname;
334 };
335
336
337 struct ndup {
338       int type;
339       union node *next;
340       int fd;
341       int dupfd;
342       union node *vname;
343 };
344
345
346 struct nhere {
347       int type;
348       union node *next;
349       int fd;
350       union node *doc;
351 };
352
353
354 struct nnot {
355       int type;
356       union node *com;
357 };
358
359
360 union node {
361       int type;
362       struct nbinary nbinary;
363       struct ncmd ncmd;
364       struct npipe npipe;
365       struct nredir nredir;
366       struct nif nif;
367       struct nfor nfor;
368       struct ncase ncase;
369       struct nclist nclist;
370       struct narg narg;
371       struct nfile nfile;
372       struct ndup ndup;
373       struct nhere nhere;
374       struct nnot nnot;
375 };
376
377
378 struct nodelist {
379         struct nodelist *next;
380         union node *n;
381 };
382
383
384 #ifdef __STDC__
385 union node *copyfunc(union node *);
386 static void freefunc(union node *);
387 #else
388 union node *copyfunc();
389 static void freefunc();
390 #endif
391
392
393
394 /*      $NetBSD: eval.h,v 1.10 2000/01/27 23:39:40 christos Exp $       */
395 extern char *commandname;       /* currently executing command */
396 extern int exitstatus;          /* exit status of last command */
397 extern struct strlist *cmdenviron;  /* environment for builtin command */
398
399
400 struct backcmd {                /* result of evalbackcmd */
401         int fd;                 /* file descriptor to read from */
402         char *buf;              /* buffer */
403         int nleft;              /* number of chars in buffer */
404         struct job *jp;         /* job structure for command */
405 };
406
407 static int evalcmd __P((int, char **));
408 static void evalstring __P((char *, int));
409 static void evaltree __P((union node *, int));
410 static void evalbackcmd __P((union node *, struct backcmd *));
411 static int bltincmd __P((int, char **));
412 static int breakcmd __P((int, char **));
413 static int returncmd __P((int, char **));
414 static int execcmd __P((int, char **));
415
416 /* in_function returns nonzero if we are currently evaluating a function */
417 #define in_function()   funcnest
418 extern int funcnest;
419 extern int evalskip;
420
421 /* reasons for skipping commands (see comment on breakcmd routine) */
422 #define SKIPBREAK       1
423 #define SKIPCONT        2
424 #define SKIPFUNC        3
425 #define SKIPFILE        4
426
427
428
429
430 /*      $NetBSD: exec.h,v 1.17 2000/05/22 10:18:47 elric Exp $  */
431
432 /* values of cmdtype */
433 #define CMDUNKNOWN -1           /* no entry in table for command */
434 #define CMDNORMAL 0             /* command is an executable program */
435 #define CMDBUILTIN 1            /* command is a shell builtin */
436 #define CMDFUNCTION 2           /* command is a shell function */
437
438
439 struct cmdentry {
440         int cmdtype;
441         union param {
442                 int index;
443                 union node *func;
444                 const struct builtincmd *cmd;
445         } u;
446 };
447
448
449 #define DO_ERR  1               /* find_command prints errors */
450 #define DO_ABS  2               /* find_command checks absolute paths */
451 #define DO_NOFUN        4       /* find_command ignores functions */
452 #define DO_BRUTE        8       /* find_command ignores hash table */
453
454 extern const char *pathopt;     /* set by padvance */
455 extern int exerrno;             /* last exec error */
456
457 static void shellexec __P((char **, char **, const char *, int))
458     __attribute__((noreturn));
459 static char *padvance __P((const char **, const char *));
460 static int hashcmd __P((int, char **));
461 static void find_command __P((char *, struct cmdentry *, int, const char *));
462 struct builtincmd *find_builtin __P((char *));
463 static void hashcd __P((void));
464 static void changepath __P((const char *));
465 static void deletefuncs __P((void));
466 #ifdef notdef
467 static void getcmdentry __P((char *, struct cmdentry *));
468 #endif
469 static void addcmdentry __P((char *, struct cmdentry *));
470 static void defun __P((char *, union node *));
471 static void unsetfunc __P((char *));
472 #ifdef ASH_TYPE
473 static int typecmd __P((int, char **));
474 #endif
475 static int commandcmd __P((int, char **));
476
477
478
479 /*      $NetBSD: expand.h,v 1.12 1999/07/09 03:05:50 christos Exp $     */
480 struct strlist {
481         struct strlist *next;
482         char *text;
483 };
484
485
486 struct arglist {
487         struct strlist *list;
488         struct strlist **lastp;
489 };
490
491 /*
492  * expandarg() flags
493  */
494 #define EXP_FULL        0x1     /* perform word splitting & file globbing */
495 #define EXP_TILDE       0x2     /* do normal tilde expansion */
496 #define EXP_VARTILDE    0x4     /* expand tildes in an assignment */
497 #define EXP_REDIR       0x8     /* file glob for a redirection (1 match only) */
498 #define EXP_CASE        0x10    /* keeps quotes around for CASE pattern */
499 #define EXP_RECORD      0x20    /* need to record arguments for ifs breakup */
500
501
502 static void expandhere __P((union node *, int));
503 static void expandarg __P((union node *, struct arglist *, int));
504 #ifdef ASH_MATH_SUPPORT
505 static void expari __P((int));
506 #endif
507 #if !(defined(__GLIBC__) && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN))
508 static int patmatch __P((char *, char *, int));
509 #endif
510 #if defined(__GLIBC__) && !defined(FNMATCH_BROKEN)
511 #define rmescapes(p) _rmescapes((p), 0)
512 static char *_rmescapes __P((char *, int));
513 #else
514 static void rmescapes __P((char *));
515 #endif
516 static int casematch __P((union node *, char *));
517
518
519 #ifdef ASH_MATH_SUPPORT
520 /* From arith.y */
521 static int arith __P((const char *));
522 static int expcmd __P((int , char **));
523 static void arith_lex_reset __P((void));
524 static int yylex __P((void));
525 #endif  
526
527
528
529
530 /*      $NetBSD: init.h,v 1.8 1995/05/11 21:29:14 christos Exp $        */
531 static void init __P((void));
532 static void reset __P((void));
533 static void initshellproc __P((void));
534
535
536
537 /*      $NetBSD: input.h,v 1.12 2000/05/22 10:18:47 elric Exp $ */
538
539 /* PEOF (the end of file marker) is defined in syntax.h */
540 /*
541  * The input line number.  Input.c just defines this variable, and saves
542  * and restores it when files are pushed and popped.  The user of this
543  * package must set its value.
544  */
545 extern int plinno;
546 extern int parsenleft;          /* number of characters left in input buffer */
547 extern char *parsenextc;        /* next character in input buffer */
548
549 static char *pfgets __P((char *, int));
550 static int pgetc __P((void));
551 static int pgetc2 __P((void));
552 static int preadbuffer __P((void));
553 static void pungetc __P((void));
554 static void pushstring __P((char *, int, void *));
555 static void popstring __P((void));
556 static void setinputfile __P((const char *, int));
557 static void setinputfd __P((int, int));
558 static void setinputstring __P((char *));
559 static void popfile __P((void));
560 static void popallfiles __P((void));
561 static void closescript __P((void));
562
563 #define pgetc_macro()   (--parsenleft >= 0? *parsenextc++ : preadbuffer())
564
565
566
567 /*      $NetBSD: jobs.h,v 1.12 2000/05/22 10:18:47 elric Exp $  */
568
569 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
570 #define FORK_FG 0
571 #define FORK_BG 1
572 #define FORK_NOJOB 2
573
574
575 /*
576  * A job structure contains information about a job.  A job is either a
577  * single process or a set of processes contained in a pipeline.  In the
578  * latter case, pidlist will be non-NULL, and will point to a -1 terminated
579  * array of pids.
580  */
581
582 struct procstat {
583         pid_t pid;              /* process id */
584         int status;             /* status flags (defined above) */
585         char *cmd;              /* text of command being run */
586 };
587
588
589 /* states */
590 #define JOBSTOPPED 1            /* all procs are stopped */
591 #define JOBDONE 2               /* all procs are completed */
592
593
594 struct job {
595         struct procstat ps0;    /* status of process */
596         struct procstat *ps;    /* status or processes when more than one */
597         short nprocs;           /* number of processes */
598         short pgrp;             /* process group of this job */
599         char state;             /* true if job is finished */
600         char used;              /* true if this entry is in used */
601         char changed;           /* true if status has changed */
602 #if JOBS
603         char jobctl;            /* job running under job control */
604 #endif
605 };
606
607 extern short backgndpid;        /* pid of last background process */
608 extern int job_warning;         /* user was warned about stopped jobs */
609
610 static void setjobctl __P((int));
611 static int killcmd __P((int, char **));
612 static int fgcmd __P((int, char **));
613 static int bgcmd __P((int, char **));
614 static int jobscmd __P((int, char **));
615 static void showjobs __P((int));
616 static int waitcmd __P((int, char **));
617 struct job *makejob __P((union node *, int));
618 static int forkshell __P((struct job *, union node *, int));
619 static int waitforjob __P((struct job *));
620 static int stoppedjobs __P((void));
621 static char *commandtext __P((union node *));
622
623 #if ! JOBS
624 #define setjobctl(on)   /* do nothing */
625 #endif
626
627
628
629 /*      $NetBSD: machdep.h,v 1.8 1995/05/11 21:29:21 christos Exp $     */
630
631 /*
632  * Most machines require the value returned from malloc to be aligned
633  * in some way.  The following macro will get this right on many machines.
634  */
635
636 #ifndef ALIGN
637 union align {
638         int i;
639         char *cp;
640 };
641
642 #define ALIGN(nbytes)   (((nbytes) + sizeof(union align) - 1) & ~(sizeof(union align) - 1))
643 #endif
644
645
646
647 /*      $NetBSD: mail.h,v 1.8 1995/05/11 21:29:23 christos Exp $        */
648
649 static void chkmail __P((int));
650
651
652
653 /*      $NetBSD: main.h,v 1.8 1995/05/11 21:29:27 christos Exp $        */
654 extern int rootpid;     /* pid of main shell */
655 extern int rootshell;   /* true if we aren't a child of the main shell */
656
657 static void readcmdfile __P((char *));
658 static void cmdloop __P((int));
659 static int dotcmd __P((int, char **));
660 static int exitcmd __P((int, char **));
661
662
663
664 /*      $NetBSD: memalloc.h,v 1.11 2000/11/01 19:56:01 christos Exp $   */
665 struct stackmark {
666         struct stack_block *stackp;
667         char *stacknxt;
668         int stacknleft;
669         struct stackmark *marknext;
670 };
671
672
673 extern char *stacknxt;
674 extern int stacknleft;
675 extern int sstrnleft;
676 extern int herefd;
677
678 static inline pointer  ckmalloc (int sz)          { return xmalloc(sz);     }
679 static inline pointer  ckrealloc(void *p, int sz) { return xrealloc(p, sz); }
680 static inline char *   savestr  (const char *s)   { return xstrdup(s);      }
681
682 pointer stalloc __P((int));
683 static void stunalloc __P((pointer));
684 static void setstackmark __P((struct stackmark *));
685 static void popstackmark __P((struct stackmark *));
686 static void growstackblock __P((void));
687 static void grabstackblock __P((int));
688 static char *growstackstr __P((void));
689 static char *makestrspace __P((size_t));
690 static void ungrabstackstr __P((char *, char *));
691
692
693
694 #define stackblock() stacknxt
695 #define stackblocksize() stacknleft
696 #define STARTSTACKSTR(p)        p = stackblock(), sstrnleft = stackblocksize()
697 #define STPUTC(c, p)    (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c)))
698 #define CHECKSTRSPACE(n, p)     { if (sstrnleft < n) p = makestrspace(n); }
699 #define USTPUTC(c, p)   (--sstrnleft, *p++ = (c))
700 #define STACKSTRNUL(p)  (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0'))
701 #define STUNPUTC(p)     (++sstrnleft, --p)
702 #define STTOPC(p)       p[-1]
703 #define STADJUST(amount, p)     (p += (amount), sstrnleft -= (amount))
704 #define grabstackstr(p) stalloc(stackblocksize() - sstrnleft)
705
706 #define ckfree(p)       free((pointer)(p))
707
708
709
710 /*      $NetBSD: miscbltin.h,v 1.1 1997/07/04 21:02:10 christos Exp $   */
711 static int readcmd __P((int, char **));
712 static int umaskcmd __P((int, char **));
713 static int ulimitcmd __P((int, char **));
714
715
716
717 /*      $NetBSD: mystring.h,v 1.9 1995/05/11 21:29:42 christos Exp $    */
718
719 extern const char snlfmt[];
720 extern const char spcstr[];
721
722 #if 0
723 static void scopyn __P((const char *, char *, int));
724 #endif
725 static int prefix __P((const char *, const char *));
726 static int number __P((const char *));
727 static int is_number __P((const char *));
728 static char *single_quote __P((const char *));
729 static char *sstrdup __P((const char *));
730 static int pstrcmp __P((const void *, const void *));
731 static const char *const *findstring __P((const char *, const char *const *, size_t));
732
733 #define equal(s1, s2)   (strcmp(s1, s2) == 0)
734 #define scopy(s1, s2)   ((void)strcpy(s2, s1))
735
736
737 /*      $NetBSD: options.h,v 1.14 2001/02/04 19:52:06 christos Exp $    */
738
739 struct shparam {
740         int nparam;             /* # of positional parameters (without $0) */
741         unsigned char malloc;   /* if parameter list dynamically allocated */
742         char **p;               /* parameter list */
743         int optind;             /* next parameter to be processed by getopts */
744         int optoff;             /* used by getopts */
745 };
746
747
748
749 #define eflag optlist[0].val
750 #define fflag optlist[1].val
751 #define Iflag optlist[2].val
752 #define iflag optlist[3].val
753 #define mflag optlist[4].val
754 #define nflag optlist[5].val
755 #define sflag optlist[6].val
756 #define xflag optlist[7].val
757 #define vflag optlist[8].val
758 #define Vflag optlist[9].val
759 #define Eflag optlist[10].val
760 #define Cflag optlist[11].val
761 #define aflag optlist[12].val
762 #define bflag optlist[13].val
763 #define uflag optlist[14].val
764 #define qflag optlist[15].val
765
766 #define NOPTS   16
767
768 struct optent {
769         const char *name;
770         const char letter;
771         char val;
772 };
773
774 extern struct optent optlist[NOPTS];
775
776
777 extern char *minusc;            /* argument to -c option */
778 extern char *arg0;              /* $0 */
779 extern struct shparam shellparam;  /* $@ */
780 extern char **argptr;           /* argument list for builtin commands */
781 extern char *optionarg;         /* set by nextopt */
782 extern char *optptr;            /* used by nextopt */
783
784 static void procargs __P((int, char **));
785 static void optschanged __P((void));
786 static void setparam __P((char **));
787 static void freeparam __P((volatile struct shparam *));
788 static int shiftcmd __P((int, char **));
789 static int setcmd __P((int, char **));
790 #ifdef ASH_GETOPTS
791 static int getoptscmd __P((int, char **));
792 static int setvarsafe __P((const char *, const char *, int));
793 #endif
794 static int nextopt __P((const char *));
795 static void getoptsreset __P((const char *));
796
797
798
799 /*      $NetBSD: output.h,v 1.14 1998/01/31 12:37:55 christos Exp $     */
800 struct output {
801 #ifdef USE_GLIBC_STDIO
802         FILE *stream;
803 #endif
804         char *nextc;
805         int nleft;
806         char *buf;
807         int bufsize;
808         int fd;
809         short flags;
810 };
811
812 extern struct output output;
813 extern struct output errout;
814 extern struct output memout;
815 extern struct output *out1;
816 extern struct output *out2;
817
818 static void outstr __P((const char *, struct output *));
819 #ifndef USE_GLIBC_STDIO
820 static void outcslow __P((char, struct output *));
821 #endif
822 static void flushall __P((void));
823 static void flushout __P((struct output *));
824 static void freestdout __P((void));
825 static void outfmt __P((struct output *, const char *, ...))
826     __attribute__((__format__(__printf__,2,3)));
827 static void out1fmt __P((const char *, ...))
828     __attribute__((__format__(__printf__,1,2)));
829 static void fmtstr __P((char *, size_t, const char *, ...))
830     __attribute__((__format__(__printf__,3,4)));
831 #ifndef USE_GLIBC_STDIO
832 static void doformat __P((struct output *, const char *, va_list));
833 #endif
834 static int xwrite __P((int, const char *, int));
835 #ifdef USE_GLIBC_STDIO
836 static void initstreams __P((void));
837 static void openmemout __P((void));
838 static int __closememout __P((void));
839 #endif
840
841 #define OUTPUT_ERR 01           /* error occurred on output */
842
843 #ifdef USE_GLIBC_STDIO
844 #define outc(c, o)      putc((c), (o)->stream)
845 #define doformat(d, f, a)       vfprintf((d)->stream, (f), (a))
846 #else
847 #define outc(c, file)   (--(file)->nleft < 0? outcslow((c), (file)) : (*(file)->nextc = (c), (file)->nextc++))
848 #endif
849 #define out1c(c)        outc((c), out1)
850 #define out2c(c)        outc((c), out2)
851 #define out1str(s)      outstr((s), out1)
852 #define out2str(s)      outstr((s), out2)
853 #define outerr(f)       ((f)->flags & OUTPUT_ERR)
854
855
856
857 /*      $NetBSD: parser.h,v 1.14 2000/07/27 04:09:28 cgd Exp $  */
858 /* control characters in argument strings */
859 #define CTLESC '\201'
860 #define CTLVAR '\202'
861 #define CTLENDVAR '\203'
862 #define CTLBACKQ '\204'
863 #define CTLQUOTE 01             /* ored with CTLBACKQ code if in quotes */
864 /*      CTLBACKQ | CTLQUOTE == '\205' */
865 #define CTLARI  '\206'
866 #define CTLENDARI '\207'
867 #define CTLQUOTEMARK '\210'
868
869 /* variable substitution byte (follows CTLVAR) */
870 #define VSTYPE  0x0f            /* type of variable substitution */
871 #define VSNUL   0x10            /* colon--treat the empty string as unset */
872 #define VSQUOTE 0x80            /* inside double quotes--suppress splitting */
873
874 /* values of VSTYPE field */
875 #define VSNORMAL        0x1             /* normal variable:  $var or ${var} */
876 #define VSMINUS         0x2             /* ${var-text} */
877 #define VSPLUS          0x3             /* ${var+text} */
878 #define VSQUESTION      0x4             /* ${var?message} */
879 #define VSASSIGN        0x5             /* ${var=text} */
880 #define VSTRIMLEFT      0x6             /* ${var#pattern} */
881 #define VSTRIMLEFTMAX   0x7             /* ${var##pattern} */
882 #define VSTRIMRIGHT     0x8             /* ${var%pattern} */
883 #define VSTRIMRIGHTMAX  0x9             /* ${var%%pattern} */
884 #define VSLENGTH        0xa             /* ${#var} */
885
886
887 /*
888  * NEOF is returned by parsecmd when it encounters an end of file.  It
889  * must be distinct from NULL, so we use the address of a variable that
890  * happens to be handy.
891  */
892 extern int tokpushback;
893 #define NEOF ((union node *)&tokpushback)
894 extern int whichprompt;         /* 1 == PS1, 2 == PS2 */
895 extern int checkalias;
896
897
898 union node *parsecmd(int);
899 static void fixredir(union node *, const char *, int);
900 static int goodname(char *);
901 static const char *getprompt(void *);
902 static int isassignment __P((const char *));
903 static const char *const *findkwd __P((const char *));
904
905
906 /*      $NetBSD: redir.h,v 1.12 2000/05/22 10:18:47 elric Exp $ */
907 /* flags passed to redirect */
908 #define REDIR_PUSH 01           /* save previous values of file descriptors */
909 #define REDIR_BACKQ 02          /* save the command output in memory */
910
911 extern int fileno2;
912
913 static void redirect __P((union node *, int));
914 static void popredir __P((void));
915 static int fd0_redirected_p __P((void));
916 static void clearredir __P((void));
917 static int dup_as_newfd __P((int, int));
918
919
920
921
922 /*      $NetBSD: show.h,v 1.4 1999/10/08 21:10:44 pk Exp $      */
923 #ifdef DEBUG
924 static void trace __P((const char *, ...));
925 static void trargs __P((char **));
926 static void showtree __P((union node *));
927 static void trputc __P((int));
928 static void trputs __P((const char *));
929 static void opentrace __P((void));
930 #endif
931 /*
932  * This file was generated by the mksyntax program.
933  */
934
935 #ifdef CEOF
936 #undef CEOF
937 #endif
938
939 /* Syntax classes */
940 #define CWORD 0                 /* character is nothing special */
941 #define CNL 1                   /* newline character */
942 #define CBACK 2                 /* a backslash character */
943 #define CSQUOTE 3               /* single quote */
944 #define CDQUOTE 4               /* double quote */
945 #define CENDQUOTE 5             /* a terminating quote */
946 #define CBQUOTE 6               /* backwards single quote */
947 #define CVAR 7                  /* a dollar sign */
948 #define CENDVAR 8               /* a '}' character */
949 #define CLP 9                   /* a left paren in arithmetic */
950 #define CRP 10                  /* a right paren in arithmetic */
951 #define CEOF 11                 /* end of file */
952 #define CCTL 12                 /* like CWORD, except it must be escaped */
953 #define CSPCL 13                /* these terminate a word */
954 #define CIGN 14                 /* character should be ignored */
955
956 /* Syntax classes for is_ functions */
957 #define ISDIGIT 01              /* a digit */
958 #define ISUPPER 02              /* an upper case letter */
959 #define ISLOWER 04              /* a lower case letter */
960 #define ISUNDER 010             /* an underscore */
961 #define ISSPECL 020             /* the name of a special parameter */
962
963 #define SYNBASE 130
964 #define PEOF -130
965
966 #define PEOA -129
967
968
969 #define BASESYNTAX (basesyntax + SYNBASE)
970 #define DQSYNTAX (dqsyntax + SYNBASE)
971 #define SQSYNTAX (sqsyntax + SYNBASE)
972 #define ARISYNTAX (arisyntax + SYNBASE)
973
974 #define is_digit(c)     ((unsigned)((c) - '0') <= 9)
975 #define is_alpha(c)     (((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))
976 #define is_name(c)      (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))
977 #define is_in_name(c)   (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))
978 #define is_special(c)   ((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))
979 #define digit_val(c)    ((c) - '0')
980
981 extern const char basesyntax[];
982 extern const char dqsyntax[];
983 extern const char sqsyntax[];
984 extern const char arisyntax[];
985 extern const char is_type[];
986 #define TEOF 0
987 #define TNL 1
988 #define TSEMI 2
989 #define TBACKGND 3
990 #define TAND 4
991 #define TOR 5
992 #define TPIPE 6
993 #define TLP 7
994 #define TRP 8
995 #define TENDCASE 9
996 #define TENDBQUOTE 10
997 #define TREDIR 11
998 #define TWORD 12
999 #define TASSIGN 13
1000 #define TNOT 14
1001 #define TCASE 15
1002 #define TDO 16
1003 #define TDONE 17
1004 #define TELIF 18
1005 #define TELSE 19
1006 #define TESAC 20
1007 #define TFI 21
1008 #define TFOR 22
1009 #define TIF 23
1010 #define TIN 24
1011 #define TTHEN 25
1012 #define TUNTIL 26
1013 #define TWHILE 27
1014 #define TBEGIN 28
1015 #define TEND 29
1016
1017 /* Array indicating which tokens mark the end of a list */
1018 static const char tokendlist[] = {
1019         1,
1020         0,
1021         0,
1022         0,
1023         0,
1024         0,
1025         0,
1026         0,
1027         1,
1028         1,
1029         1,
1030         0,
1031         0,
1032         0,
1033         0,
1034         0,
1035         1,
1036         1,
1037         1,
1038         1,
1039         1,
1040         1,
1041         0,
1042         0,
1043         0,
1044         1,
1045         0,
1046         0,
1047         0,
1048         1,
1049 };
1050
1051 static const char *const tokname[] = {
1052         "end of file",
1053         "newline",
1054         "\";\"",
1055         "\"&\"",
1056         "\"&&\"",
1057         "\"||\"",
1058         "\"|\"",
1059         "\"(\"",
1060         "\")\"",
1061         "\";;\"",
1062         "\"`\"",
1063         "redirection",
1064         "word",
1065         "assignment",
1066         "\"!\"",
1067         "\"case\"",
1068         "\"do\"",
1069         "\"done\"",
1070         "\"elif\"",
1071         "\"else\"",
1072         "\"esac\"",
1073         "\"fi\"",
1074         "\"for\"",
1075         "\"if\"",
1076         "\"in\"",
1077         "\"then\"",
1078         "\"until\"",
1079         "\"while\"",
1080         "\"{\"",
1081         "\"}\"",
1082 };
1083
1084 #define KWDOFFSET 14
1085
1086 static const char *const parsekwd[] = {
1087         "!",
1088         "case",
1089         "do",
1090         "done",
1091         "elif",
1092         "else",
1093         "esac",
1094         "fi",
1095         "for",
1096         "if",
1097         "in",
1098         "then",
1099         "until",
1100         "while",
1101         "{",
1102         "}"
1103 };
1104
1105
1106
1107
1108 /*      $NetBSD: trap.h,v 1.14 2000/05/22 10:18:47 elric Exp $  */
1109 extern int pendingsigs;
1110
1111 static int trapcmd __P((int, char **));
1112 static void clear_traps __P((void));
1113 static void setsignal __P((int));
1114 static void ignoresig __P((int));
1115 static void onsig __P((int));
1116 static void dotrap __P((void));
1117 static void setinteractive __P((int));
1118 static void exitshell __P((int)) __attribute__((noreturn));
1119 static int decode_signal __P((const char *, int));
1120
1121
1122
1123 /*      $NetBSD: var.h,v 1.18 2000/05/22 10:18:47 elric Exp $   */
1124
1125 /*
1126  * Shell variables.
1127  */
1128
1129 /* flags */
1130 #define VEXPORT         0x01    /* variable is exported */
1131 #define VREADONLY       0x02    /* variable cannot be modified */
1132 #define VSTRFIXED       0x04    /* variable struct is staticly allocated */
1133 #define VTEXTFIXED      0x08    /* text is staticly allocated */
1134 #define VSTACK          0x10    /* text is allocated on the stack */
1135 #define VUNSET          0x20    /* the variable is not set */
1136 #define VNOFUNC         0x40    /* don't call the callback function */
1137
1138
1139 struct var {
1140         struct var *next;               /* next entry in hash list */
1141         int flags;                      /* flags are defined above */
1142         char *text;                     /* name=value */
1143         void (*func) __P((const char *));
1144                                         /* function to be called when  */
1145                                         /* the variable gets set/unset */
1146 };
1147
1148
1149 struct localvar {
1150         struct localvar *next;          /* next local variable in list */
1151         struct var *vp;                 /* the variable that was made local */
1152         int flags;                      /* saved flags */
1153         char *text;                     /* saved text */
1154 };
1155
1156
1157 extern struct localvar *localvars;
1158
1159 #if ATTY
1160 extern struct var vatty;
1161 #endif
1162 extern struct var vifs;
1163 extern struct var vmail;
1164 extern struct var vmpath;
1165 extern struct var vpath;
1166 extern struct var vps1;
1167 extern struct var vps2;
1168 #ifndef SMALL
1169 extern struct var vterm;
1170 extern struct var vtermcap;
1171 extern struct var vhistsize;
1172 #endif
1173
1174 #ifdef IFS_BROKEN
1175 extern const char defifsvar[];
1176 #define defifs (defifsvar + 4)
1177 #else
1178 extern const char defifs[];
1179 #endif
1180 extern const char defpathvar[];
1181 #define defpath (defpathvar + 5)
1182
1183 /*
1184  * The following macros access the values of the above variables.
1185  * They have to skip over the name.  They return the null string
1186  * for unset variables.
1187  */
1188
1189 #define ifsval()        (vifs.text + 4)
1190 #define ifsset()        ((vifs.flags & VUNSET) == 0)
1191 #define mailval()       (vmail.text + 5)
1192 #define mpathval()      (vmpath.text + 9)
1193 #define pathval()       (vpath.text + 5)
1194 #define ps1val()        (vps1.text + 4)
1195 #define ps2val()        (vps2.text + 4)
1196 #define optindval()     (voptind.text + 7)
1197 #ifndef SMALL
1198 #define histsizeval()   (vhistsize.text + 9)
1199 #define termval()       (vterm.text + 5)
1200 #endif
1201
1202 #if ATTY
1203 #define attyset()       ((vatty.flags & VUNSET) == 0)
1204 #endif
1205 #define mpathset()      ((vmpath.flags & VUNSET) == 0)
1206
1207 static void initvar __P((void));
1208 static void setvar __P((const char *, const char *, int));
1209 static void setvareq __P((char *, int));
1210 struct strlist;
1211 static void listsetvar __P((struct strlist *));
1212 static char *lookupvar __P((const char *));
1213 static char *bltinlookup __P((const char *));
1214 static char **environment __P((void));
1215 static void shprocvar __P((void));
1216 static int showvarscmd __P((int, char **));
1217 static int exportcmd __P((int, char **));
1218 static int localcmd __P((int, char **));
1219 static void mklocal __P((char *));
1220 static void poplocalvars __P((void));
1221 static int setvarcmd __P((int, char **));
1222 static int unsetcmd __P((int, char **));
1223 static int unsetvar __P((const char *));
1224 static int varequal __P((const char *, const char *));
1225