BSD 3 development
[unix-history] / usr / src / cmd / csh / sh.h
CommitLineData
5d9efaf5
BJ
1/* Copyright (c) 1979 Regents of the University of California */
2#include "sh.local.h"
3/*
4 * C shell
5 *
6 * Bill Joy, UC Berkeley
7 * October, 1978
8 */
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/dir.h>
12
13#define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
14
15#include <errno.h>
16#include <setjmp.h>
17#include <signal.h>
18
19typedef char bool;
20
21#define eq(a, b) (strcmp(a, b) == 0)
22
23/*
24 * Global flags
25 */
26bool didcch; /* Have closed unused fd's for child */
27bool didfds; /* Have setup i/o fd's for child */
28bool doneinp; /* EOF indicator after reset from readc */
29bool exiterr; /* Exit if error or non-zero exit status */
30bool child; /* Child shell ... errors cause exit */
31bool haderr; /* Reset was because of an error */
32bool intty; /* Input is a tty */
33bool intact; /* We are interactive... therefore prompt */
34bool justpr; /* Just print because of :p hist mod */
35bool loginsh; /* We are a loginsh -> .login/.logout */
36bool noexec; /* Don't execute, just syntax check */
37bool setintr; /* Set interrupts on/off -> Wait intr... */
38bool timflg; /* Time the next waited for command */
39
40/*
41 * Global i/o info
42 */
43char *arginp; /* Argument input for sh -c and internal `xx` */
44int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
45char *file; /* Name of shell file for $0 */
46
47char *err; /* Error message from scanner/parser */
48int errno; /* Error from C library routines */
49char *shtemp; /* Temp name for << shell files in /tmp */
50time_t time0; /* Time at which the shell started */
51
52/*
53 * Miscellany
54 */
55char *doldol; /* Character pid for $$ */
56int uid; /* Invokers uid */
57time_t chktim; /* Time mail last checked */
58
59/*
60 * These are declared here because they want to be
61 * initialized in sh.init.c (to allow them to be made readonly)
62 */
63char *mesg[];
64
65struct biltins {
66 char *bname;
67 int (*bfunct)();
68 short minargs, maxargs;
69} bfunc[];
70
71struct srch {
72 char *s_name;
73 short s_value;
74} srchn[];
75
76/*
77 * To be able to redirect i/o for builtins easily, the shell moves the i/o
78 * descriptors it uses away from 0,1,2.
79 * Ideally these should be in units which are closed across exec's
80 * (this saves work) but for version 6, this is not usually possible.
81 * The desired initial values for these descriptors are defined in
82 * sh.local.h.
83 */
84short SHIN; /* Current shell input (script) */
85short SHOUT; /* Shell output */
86short SHDIAG; /* Diagnostic output... shell errs go here */
87short OLDSTD; /* Old standard input (def for cmds) */
88
89/*
90 * Error control
91 *
92 * Errors in scanning and parsing set up an error message to be printed
93 * at the end and complete. Other errors always cause a reset.
94 * Because of source commands and .cshrc we need nested error catches.
95 */
96
97jmp_buf reslab;
98
99#define setexit() setjmp(reslab)
100#define reset() longjmp(reslab)
101 /* Should use structure assignment here */
102#define getexit(a) copy(a, reslab, sizeof reslab)
103#define resexit(a) copy(reslab, a, sizeof reslab)
104
105char *gointr; /* Label for an onintr transfer */
106int (*parintr)(); /* Parents interrupt catch */
107int (*parterm)(); /* Parents terminate catch */
108
109/*
110 * Lexical definitions.
111 *
112 * All lexical space is allocated dynamically.
113 * The eighth bit of characters is used to prevent recognition,
114 * and eventually stripped.
115 */
116#define QUOTE 0200 /* Eighth char bit used internally for 'ing */
117#define TRIM 0177 /* Mask to strip quote bit */
118
119/*
120 * Each level of input has a buffered input structure.
121 * There are one or more blocks of buffered input for each level,
122 * exactly one if the input is seekable and tell is available.
123 * In other cases, the shell buffers enough blocks to keep all loops
124 * in the buffer.
125 */
126struct Bin {
127 off_t Bfseekp; /* Seek pointer */
128 off_t Bfbobp; /* Seekp of beginning of buffers */
129 off_t Bfeobp; /* Seekp of end of buffers */
130 short Bfblocks; /* Number of buffer blocks */
131 char **Bfbuf; /* The array of buffer blocks */
132} B;
133
134#define fseekp B.Bfseekp
135#define fbobp B.Bfbobp
136#define feobp B.Bfeobp
137#define fblocks B.Bfblocks
138#define fbuf B.Bfbuf
139
140off_t btell();
141
142/*
143 * The shell finds commands in loops by reseeking the input
144 * For whiles, in particular, it reseeks to the beginning of the
145 * line the while was on; hence the while placement restrictions.
146 */
147off_t lineloc;
148
149#ifdef TELL
150off_t tell();
151bool cantell; /* Is current source tellable ? */
152#endif
153
154/*
155 * Input lines are parsed into doubly linked circular
156 * lists of words of the following form.
157 */
158struct wordent {
159 char *word;
160 struct wordent *prev;
161 struct wordent *next;
162};
163
164/*
165 * During word building, both in the initial lexical phase and
166 * when expanding $ variable substitutions, expansion by `!' and `$'
167 * must be inhibited when reading ahead in routines which are themselves
168 * processing `!' and `$' expansion or after characters such as `\' or in
169 * quotations. The following flags are passed to the getC routines
170 * telling them which of these substitutions are appropriate for the
171 * next character to be returned.
172 */
173#define DODOL 1
174#define DOEXCL 2
175#define DOALL DODOL|DOEXCL
176
177/*
178 * Labuf implements a general buffer for lookahead during lexical operations.
179 * Text which is to be placed in the input stream can be stuck here.
180 * We stick parsed ahead $ constructs during initial input,
181 * process id's from `$$', and modified variable values (from qualifiers
182 * during expansion in sh.dol.c) here.
183 */
184char labuf[256];
185char *lap;
186
187/*
188 * Parser structure
189 *
190 * Each command is parsed to a tree of command structures and
191 * flags are set bottom up during this process, to be propagated down
192 * as needed during the semantics/exeuction pass (sh.sem.c).
193 */
194struct command {
195 short t_dtyp; /* Type of node */
196 short t_dflg; /* Flags, e.g. FAND|... */
197 union {
198 char *T_dlef; /* Input redirect word */
199 struct command *T_dcar; /* Left part of list/pipe */
200 } L;
201 union {
202 char *T_drit; /* Output redirect word */
203 struct command *T_dcdr; /* Right part of list/pipe */
204 } R;
205#define t_dlef L.T_dlef
206#define t_dcar L.T_dcar
207#define t_drit R.T_drit
208#define t_dcdr R.T_dcdr
209 char **t_dcom; /* Command/argument vector */
210 struct command *t_dspr; /* Pointer to ()'d subtree */
211};
212
213#define TCOM 1 /* t_dcom <t_dlef >t_drit */
214#define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */
215#define TFIL 3 /* t_dlef | t_drit */
216#define TLST 4 /* t_dlef ; t_drit */
217#define TOR 5 /* t_dlef || t_drit */
218#define TAND 6 /* t_dlef && t_drit */
219
220#define FAND (1<<0) /* executes in background */
221#define FCAT (1<<1) /* output is redirected >> */
222#define FPIN (1<<2) /* input is a pipe */
223#define FPOU (1<<3) /* output is a pipe */
224#define FPAR (1<<4) /* don't fork, last ()ized cmd */
225#define FINT (1<<5) /* don't make interruptible */
226#define FPRS (1<<6) /* print number when forked */
227#define FDIAG (1<<7) /* redirect unit 2 with unit 1 */
228#define FANY (1<<8) /* output was ! */
229#define FHERE (1<<9) /* input redirection is << */
230#define FREDO (1<<10) /* reexec aft if, repeat,... */
231
232/*
233 * The keywords for the parser
234 */
235#define ZBREAK 0
236#define ZBRKSW 1
237#define ZCASE 2
238#define ZDEFAULT 3
239#define ZELSE 4
240#define ZEND 5
241#define ZENDIF 6
242#define ZENDSW 7
243#define ZEXIT 8
244#define ZFOREACH 9
245#define ZGOTO 10
246#define ZIF 11
247#define ZLABEL 12
248#define ZLET 13
249#define ZSET 14
250#define ZSWITCH 15
251#define ZTEST 16
252#define ZTHEN 17
253#define ZWHILE 18
254
255/*
256 * Structure defining the existing while/foreach loops at this
257 * source level. Loops are implemented by seeking back in the
258 * input. For foreach (fe), the word list is attached here.
259 */
260struct whyle {
261 off_t w_start; /* Point to restart loop */
262 off_t w_end; /* End of loop (0 if unknown) */
263 char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
264 char *w_fename; /* Name for fe */
265 struct whyle *w_next; /* Next (more outer) loop */
266} *whyles;
267
268/*
269 * Variable structure
270 *
271 * Lists of aliases and variables are sorted alphabetically by name
272 */
273struct varent {
274 char **vec; /* Array of words which is the value */
275 char *name; /* Name of variable/alias */
276 struct varent *link;
277} shvhed, aliases;
278
279/*
280 * The following are for interfacing redo substitution in
281 * aliases to the lexical routines.
282 */
283struct wordent *alhistp; /* Argument list (first) */
284struct wordent *alhistt; /* Node after last in arg list */
285char **alvec; /* The (remnants of) alias vector */
286
287/*
288 * Filename/command name expansion variables
289 */
290short gflag; /* After tglob -> is globbing needed? */
291
292/*
293 * A reasonable limit on number of arguments would seem to be
294 * the maximum number of characters in an arg list / 6.
295 */
296#define GAVSIZ NCARGS / 6
297
298/*
299 * Variables for filename expansion
300 */
301char **gargv; /* Pointer to the (stack) arglist */
302short gargc; /* Number args in gargv */
303short gnleft;
304
305/*
306 * Variables for command expansion.
307 */
308char **pargv; /* Pointer to the argv list space */
309char *pargs; /* Pointer to start current word */
310short pargc; /* Count of arguments in pargv */
311short pnleft; /* Number of chars left in pargs */
312char *pargcp; /* Current index into pargs */
313
314/*
315 * History list
316 *
317 * Each history list entry contains an embedded wordlist
318 * from the scanner, a number for the event, and a reference count
319 * to aid in discarding old entries.
320 *
321 * Essentially "invisible" entries are put on the history list
322 * when history substitution includes modifiers, and thrown away
323 * at the next discarding since their event numbers are very negative.
324 */
325struct Hist {
326 struct wordent Hlex;
327 int Hnum;
328 int Href;
329 struct Hist *Hnext;
330} Histlist;
331
332int eventno; /* Next events number */
333int lastev; /* Last event reference (default) */
334
335char *Dfix1();
336struct varent *adrof(), *adrof1();
337char **blkcat();
338char **blkcpy();
339char **blkend();
340char **blkspl();
341char *calloc();
342char *cname();
343char **copyblk();
344char **dobackp();
345char *domod();
346struct wordent *dosub();
347char *exp3();
348char *exp3a();
349char *exp4();
350char *exp5();
351char *exp6();
352struct Hist *enthist();
353struct Hist *findev();
354struct wordent *freenod();
355char *getenv();
356char *getinx();
357struct varent *getvx();
358struct passwd *getpwnam();
359struct wordent *gethent();
360struct wordent *getsub();
361char *globone();
362struct biltins *isbfunc();
363char **glob();
364char *operate();
365int pintr();
366char *putn();
367char **saveblk();
368char *savestr();
369char *strcat();
370char *strcpy();
371char *strend();
372char *strings();
373char *strip();
374char *strspl();
375char *subword();
376struct command *syntax();
377struct command *syn0();
378struct command *syn1();
379struct command *syn1a();
380struct command *syn1b();
381struct command *syn2();
382struct command *syn3();
383int tglob();
384int trim();
385char *value(), *value1();
386char *xhome();
387char *xname();
388char *xset();
389
390#define NOSTR ((char *) 0)
391
392/*
393 * setname is a macro to save space (see sh.err.c)
394 */
395char *bname;
396#define setname(a) bname = (a);
397
398#ifdef VFORK
399char *Vsav;
400char **Vav;
401char *Vdp;
402#endif