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