4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / bin / csh / csh.h
CommitLineData
ecc449eb
KB
1/*-
2 * Copyright (c) 1980, 1991 The Regents of the University of California.
3 * All rights reserved.
b79f4fa9 4 *
ecc449eb
KB
5 * %sccs.include.redist.c%
6 *
635843dc 7 * @(#)csh.h 5.26 (Berkeley) %G%
b79f4fa9 8 */
a72f8ea1 9
0aec749d
CZ
10/*
11 * Fundamental definitions which may vary from system to system.
12 *
13 * BUFSIZ The i/o buffering size; also limits word size
14 * MAILINTVL How often to mailcheck; more often is more expensive
15 */
16#ifndef BUFSIZ
17#define BUFSIZ 1024 /* default buffer size */
18#endif /* BUFSIZ */
19
20#define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
21#define MAILINTVL 600 /* 10 minutes */
22
23/*
24 * The shell moves std in/out/diag and the old std input away from units
25 * 0, 1, and 2 so that it is easy to set up these standards for invoked
26 * commands.
27 */
28#define FSHTTY 15 /* /dev/tty when manip pgrps */
29#define FSHIN 16 /* Preferred desc for shell input */
30#define FSHOUT 17 /* ... shell output */
454c2aa3 31#define FSHERR 18 /* ... shell diagnostics */
0aec749d
CZ
32#define FOLDSTD 19 /* ... old std input */
33
34#ifdef PROF
35#define xexit(n) done(n)
36#endif
37
4d7b2685
KB
38#ifdef SHORT_STRINGS
39typedef short Char;
40
41#define SAVE(a) (Strsave(str2short(a)))
42#else
43typedef char Char;
44
45#define SAVE(a) (strsave(a))
46#endif
47
37999c01
CZ
48/*
49 * Make sure a variable is not stored in a register by taking its address
50 * This is used where variables might be clobbered by longjmp.
51 */
52#define UNREGISTER(a) (void) &a
53
4d7b2685 54typedef void *ioctl_t; /* Third arg of ioctl */
6e37afca
KB
55
56typedef void *ptr_t;
57
b9c4f741 58#include "const.h"
4d7b2685
KB
59#include "char.h"
60#include "err.h"
61
4d7b2685 62#define xmalloc(i) Malloc(i)
6e37afca
KB
63#define xrealloc(p, i) Realloc(p, i)
64#define xcalloc(n, s) Calloc(n, s)
4d7b2685 65#define xfree(p) Free(p)
6e37afca 66
454c2aa3
CZ
67#include <stdio.h>
68FILE *cshin, *cshout, *csherr;
69
6e37afca
KB
70#define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
71
6e37afca
KB
72typedef int bool;
73
74#define eq(a, b) (Strcmp(a, b) == 0)
75
76/* globone() flags */
77#define G_ERROR 0 /* default action: error if multiple words */
4d7b2685
KB
78#define G_IGNORE 1 /* ignore the rest of the words */
79#define G_APPEND 2 /* make a sentence by cat'ing the words */
2f2a0649 80
8c95f417
BJ
81/*
82 * Global flags
83 */
6e37afca
KB
84bool chkstop; /* Warned of stopped jobs... allow exit */
85bool didfds; /* Have setup i/o fd's for child */
86bool doneinp; /* EOF indicator after reset from readc */
87bool exiterr; /* Exit if error or non-zero exit status */
88bool child; /* Child shell ... errors cause exit */
89bool haderr; /* Reset was because of an error */
90bool intty; /* Input is a tty */
91bool intact; /* We are interactive... therefore prompt */
92bool justpr; /* Just print because of :p hist mod */
93bool loginsh; /* We are a loginsh -> .login/.logout */
94bool neednote; /* Need to pnotify() */
95bool noexec; /* Don't execute, just syntax check */
96bool pjobs; /* want to print jobs if interrupted */
97bool setintr; /* Set interrupts on/off -> Wait intr... */
98bool timflg; /* Time the next waited for command */
99bool havhash; /* path hashing is available */
100
35371dec 101#ifdef FILEC
6e37afca 102bool filec; /* doing filename expansion */
35371dec 103#endif
8c95f417
BJ
104
105/*
106 * Global i/o info
107 */
6e37afca
KB
108Char *arginp; /* Argument input for sh -c and internal `xx` */
109int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
110Char *ffile; /* Name of shell file for $0 */
8c95f417 111
6e37afca
KB
112char *seterr; /* Error message from scanner/parser */
113Char *shtemp; /* Temp name for << shell files in /tmp */
114
d08af90b 115#include <sys/types.h>
b9c4f741
KB
116#include <sys/time.h>
117#include <sys/resource.h>
118
6e37afca
KB
119struct timeval time0; /* Time at which the shell started */
120struct rusage ru0;
8c95f417
BJ
121
122/*
123 * Miscellany
124 */
6e37afca 125Char *doldol; /* Character pid for $$ */
5498b8cd 126int backpid; /* Pid of the last background process */
d21ac901
CZ
127int uid, euid; /* Invokers uid */
128int gid, egid; /* Invokers gid */
6e37afca
KB
129time_t chktim; /* Time mail last checked */
130int shpgrp; /* Pgrp of shell */
131int tpgrp; /* Terminal process group */
132
8c95f417 133/* If tpgrp is -1, leave tty alone! */
6e37afca 134int opgrp; /* Initial pgrp and tty pgrp */
8c95f417 135
8c95f417
BJ
136
137/*
138 * To be able to redirect i/o for builtins easily, the shell moves the i/o
139 * descriptors it uses away from 0,1,2.
140 * Ideally these should be in units which are closed across exec's
141 * (this saves work) but for version 6, this is not usually possible.
635843dc 142 * The desired initial values for these descriptors are F{SHIN,...}.
8c95f417 143 */
454c2aa3
CZ
144int SHIN; /* Current shell input (script) */
145int SHOUT; /* Shell output */
146int SHERR; /* Diagnostic output... shell errs go here */
147int OLDSTD; /* Old standard input (def for cmds) */
8c95f417
BJ
148
149/*
150 * Error control
151 *
152 * Errors in scanning and parsing set up an error message to be printed
153 * at the end and complete. Other errors always cause a reset.
154 * Because of source commands and .cshrc we need nested error catches.
155 */
156
b9c4f741 157#include <setjmp.h>
6e37afca
KB
158jmp_buf reslab;
159
160#define setexit() (setjmp(reslab))
161#define reset() longjmp(reslab, 1)
162 /* Should use structure assignment here */
163#define getexit(a) bcopy((char *)reslab, ((char *)(a)), sizeof reslab)
164#define resexit(a) bcopy((char *)(a), (char *)reslab, sizeof reslab)
8c95f417 165
6e37afca 166Char *gointr; /* Label for an onintr transfer */
8c95f417 167
b9c4f741 168#include <signal.h>
6e37afca
KB
169sig_t parintr; /* Parents interrupt catch */
170sig_t parterm; /* Parents terminate catch */
8c95f417
BJ
171
172/*
173 * Lexical definitions.
174 *
175 * All lexical space is allocated dynamically.
1bf25f10 176 * The eighth/sixteenth bit of characters is used to prevent recognition,
8c95f417
BJ
177 * and eventually stripped.
178 */
6e37afca
KB
179#define META 0200
180#define ASCII 0177
181#ifdef SHORT_STRINGS
182#define CHAR 0377
183#define QUOTE 0100000 /* 16nth char bit used for 'ing */
184#define TRIM 0077777 /* Mask to strip quote bit */
185#else
186#define CHAR 0177
187#define QUOTE 0200 /* Eighth char bit used for 'ing */
188#define TRIM 0177 /* Mask to strip quote bit */
189#endif
190
191int AsciiOnly; /* If set only 7 bits is expected in characters */
8c95f417
BJ
192
193/*
194 * Each level of input has a buffered input structure.
195 * There are one or more blocks of buffered input for each level,
196 * exactly one if the input is seekable and tell is available.
197 * In other cases, the shell buffers enough blocks to keep all loops
198 * in the buffer.
199 */
289ec3f8 200struct Bin {
6e37afca
KB
201 off_t Bfseekp; /* Seek pointer */
202 off_t Bfbobp; /* Seekp of beginning of buffers */
203 off_t Bfeobp; /* Seekp of end of buffers */
454c2aa3 204 int Bfblocks; /* Number of buffer blocks */
6e37afca
KB
205 Char **Bfbuf; /* The array of buffer blocks */
206} B;
8c95f417 207
c4a58397
CZ
208/*
209 * This structure allows us to seek inside aliases
210 */
211struct Ain {
212 int type;
213#define I_SEEK -1 /* Invalid seek */
214#define A_SEEK 0 /* Alias seek */
215#define F_SEEK 1 /* File seek */
216#define E_SEEK 2 /* Eval seek */
555d3867
CZ
217 union {
218 off_t _f_seek;
219 Char* _c_seek;
220 } fc;
221#define f_seek fc._f_seek
222#define c_seek fc._c_seek
c4a58397
CZ
223 Char **a_seek;
224} ;
225extern int aret; /* What was the last character returned */
226#define SEEKEQ(a, b) ((a)->type == (b)->type && \
227 (a)->f_seek == (b)->f_seek && \
228 (a)->a_seek == (b)->a_seek)
229
8c95f417
BJ
230#define fseekp B.Bfseekp
231#define fbobp B.Bfbobp
232#define feobp B.Bfeobp
233#define fblocks B.Bfblocks
234#define fbuf B.Bfbuf
235
8c95f417
BJ
236/*
237 * The shell finds commands in loops by reseeking the input
238 * For whiles, in particular, it reseeks to the beginning of the
239 * line the while was on; hence the while placement restrictions.
240 */
c4a58397 241struct Ain lineloc;
8c95f417 242
6e37afca
KB
243bool cantell; /* Is current source tellable ? */
244
8c95f417
BJ
245/*
246 * Input lines are parsed into doubly linked circular
247 * lists of words of the following form.
248 */
289ec3f8 249struct wordent {
6e37afca
KB
250 Char *word;
251 struct wordent *prev;
252 struct wordent *next;
8c95f417
BJ
253};
254
255/*
256 * During word building, both in the initial lexical phase and
257 * when expanding $ variable substitutions, expansion by `!' and `$'
258 * must be inhibited when reading ahead in routines which are themselves
259 * processing `!' and `$' expansion or after characters such as `\' or in
260 * quotations. The following flags are passed to the getC routines
261 * telling them which of these substitutions are appropriate for the
262 * next character to be returned.
263 */
264#define DODOL 1
265#define DOEXCL 2
266#define DOALL DODOL|DOEXCL
267
268/*
269 * Labuf implements a general buffer for lookahead during lexical operations.
6e37afca
KB
270 * Text which is to be placed in the input stream can be stuck here.
271 * We stick parsed ahead $ constructs during initial input,
272 * process id's from `$$', and modified variable values (from qualifiers
273 * during expansion in sh.dol.c) here.
8c95f417 274 */
6e37afca 275Char *lap;
8c95f417
BJ
276
277/*
278 * Parser structure
279 *
6e37afca
KB
280 * Each command is parsed to a tree of command structures and
281 * flags are set bottom up during this process, to be propagated down
282 * as needed during the semantics/exeuction pass (sh.sem.c).
8c95f417 283 */
6e37afca
KB
284struct command {
285 short t_dtyp; /* Type of node */
286#define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
287#define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
288#define NODE_PIPE 3 /* t_dlef | t_drit */
289#define NODE_LIST 4 /* t_dlef ; t_drit */
290#define NODE_OR 5 /* t_dlef || t_drit */
291#define NODE_AND 6 /* t_dlef && t_drit */
292 short t_dflg; /* Flags, e.g. F_AMPERSAND|... */
293#define F_SAVE (F_NICE|F_TIME|F_NOHUP) /* save these when re-doing */
294
295#define F_AMPERSAND (1<<0) /* executes in background */
296#define F_APPEND (1<<1) /* output is redirected >> */
297#define F_PIPEIN (1<<2) /* input is a pipe */
298#define F_PIPEOUT (1<<3) /* output is a pipe */
299#define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
300#define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
301/* spare */
302#define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
303#define F_OVERWRITE (1<<8) /* output was ! */
304#define F_READ (1<<9) /* input redirection is << */
305#define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
306#define F_NICE (1<<11) /* t_nice is meaningful */
307#define F_NOHUP (1<<12) /* nohup this command */
308#define F_TIME (1<<13) /* time this command */
309 union {
310 Char *T_dlef; /* Input redirect word */
311 struct command *T_dcar; /* Left part of list/pipe */
312 } L;
313 union {
314 Char *T_drit; /* Output redirect word */
315 struct command *T_dcdr; /* Right part of list/pipe */
316 } R;
8c95f417
BJ
317#define t_dlef L.T_dlef
318#define t_dcar L.T_dcar
319#define t_drit R.T_drit
320#define t_dcdr R.T_dcdr
6e37afca
KB
321 Char **t_dcom; /* Command/argument vector */
322 struct command *t_dspr; /* Pointer to ()'d subtree */
454c2aa3 323 int t_nice;
8c95f417
BJ
324};
325
6e37afca 326
454c2aa3
CZ
327/*
328 * These are declared here because they want to be
329 * initialized in sh.init.c (to allow them to be made readonly)
330 */
331
332extern struct biltins {
333 char *bname;
334 void (*bfunct) __P((Char **, struct command *));
335 short minargs, maxargs;
336} bfunc[];
337extern int nbfunc;
338
339extern struct srch {
340 char *s_name;
341 short s_value;
342} srchn[];
343extern int nsrchn;
344
6e37afca
KB
345/*
346 * The keywords for the parser
347 */
289ec3f8
KB
348#define T_BREAK 0
349#define T_BRKSW 1
350#define T_CASE 2
351#define T_DEFAULT 3
352#define T_ELSE 4
353#define T_END 5
354#define T_ENDIF 6
355#define T_ENDSW 7
356#define T_EXIT 8
357#define T_FOREACH 9
358#define T_GOTO 10
359#define T_IF 11
360#define T_LABEL 12
361#define T_LET 13
362#define T_SET 14
363#define T_SWITCH 15
364#define T_TEST 16
365#define T_THEN 17
366#define T_WHILE 18
8c95f417
BJ
367
368/*
369 * Structure defining the existing while/foreach loops at this
370 * source level. Loops are implemented by seeking back in the
371 * input. For foreach (fe), the word list is attached here.
372 */
6e37afca 373struct whyle {
c4a58397
CZ
374 struct Ain w_start; /* Point to restart loop */
375 struct Ain w_end; /* End of loop (0 if unknown) */
6e37afca
KB
376 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
377 Char *w_fename; /* Name for fe */
378 struct whyle *w_next; /* Next (more outer) loop */
379} *whyles;
8c95f417
BJ
380
381/*
382 * Variable structure
383 *
35371dec 384 * Aliases and variables are stored in AVL balanced binary trees.
8c95f417 385 */
6e37afca
KB
386struct varent {
387 Char **vec; /* Array of words which is the value */
388 Char *v_name; /* Name of variable/alias */
389 struct varent *v_link[3]; /* The links, see below */
390 int v_bal; /* Balance factor */
391} shvhed, aliases;
392
35371dec
EW
393#define v_left v_link[0]
394#define v_right v_link[1]
395#define v_parent v_link[2]
396
397struct varent *adrof1();
6e37afca 398
35371dec
EW
399#define adrof(v) adrof1(v, &shvhed)
400#define value(v) value1(v, &shvhed)
8c95f417
BJ
401
402/*
403 * The following are for interfacing redo substitution in
404 * aliases to the lexical routines.
405 */
6e37afca
KB
406struct wordent *alhistp; /* Argument list (first) */
407struct wordent *alhistt; /* Node after last in arg list */
5498b8cd 408Char **alvec, *alvecp; /* The (remnants of) alias vector */
8c95f417
BJ
409
410/*
411 * Filename/command name expansion variables
412 */
454c2aa3 413int gflag; /* After tglob -> is globbing needed? */
8c95f417 414
6e37afca 415#define MAXVARLEN 30 /* Maximum number of char in a variable name */
8c95f417
BJ
416
417/*
418 * Variables for filename expansion
419 */
6e37afca
KB
420extern Char **gargv; /* Pointer to the (stack) arglist */
421extern long gargc; /* Number args in gargv */
8c95f417
BJ
422
423/*
424 * Variables for command expansion.
425 */
6e37afca
KB
426extern Char **pargv; /* Pointer to the argv list space */
427extern long pargc; /* Count of arguments in pargv */
428Char *pargs; /* Pointer to start current word */
429long pnleft; /* Number of chars left in pargs */
430Char *pargcp; /* Current index into pargs */
8c95f417
BJ
431
432/*
433 * History list
434 *
435 * Each history list entry contains an embedded wordlist
436 * from the scanner, a number for the event, and a reference count
437 * to aid in discarding old entries.
438 *
439 * Essentially "invisible" entries are put on the history list
440 * when history substitution includes modifiers, and thrown away
441 * at the next discarding since their event numbers are very negative.
442 */
6e37afca
KB
443struct Hist {
444 struct wordent Hlex;
445 int Hnum;
446 int Href;
6e37afca
KB
447 struct Hist *Hnext;
448} Histlist;
8c95f417 449
6e37afca
KB
450struct wordent paraml; /* Current lexical word list */
451int eventno; /* Next events number */
452int lastev; /* Last event reference (default) */
8c95f417 453
6e37afca
KB
454Char HIST; /* history invocation character */
455Char HISTSUB; /* auto-substitute character */
8c95f417 456
35371dec 457/*
6e37afca 458 * strings.h:
35371dec 459 */
6e37afca 460#ifndef SHORT_STRINGS
4d7b2685
KB
461#define Strchr(a, b) strchr(a, b)
462#define Strrchr(a, b) strrchr(a, b)
463#define Strcat(a, b) strcat(a, b)
6e37afca 464#define Strncat(a, b, c) strncat(a, b, c)
4d7b2685 465#define Strcpy(a, b) strcpy(a, b)
6e37afca
KB
466#define Strncpy(a, b, c) strncpy(a, b, c)
467#define Strlen(a) strlen(a)
468#define Strcmp(a, b) strcmp(a, b)
469#define Strncmp(a, b, c) strncmp(a, b, c)
470
471#define Strspl(a, b) strspl(a, b)
472#define Strsave(a) strsave(a)
473#define Strend(a) strend(a)
474#define Strstr(a, b) strstr(a, b)
475
476#define str2short(a) (a)
477#define blk2short(a) saveblk(a)
478#define short2blk(a) saveblk(a)
1bf25f10 479#define short2str(a) strip(a)
6e37afca 480#else
4d7b2685 481#define Strchr(a, b) s_strchr(a, b)
6e37afca 482#define Strrchr(a, b) s_strrchr(a, b)
4d7b2685 483#define Strcat(a, b) s_strcat(a, b)
6e37afca 484#define Strncat(a, b, c) s_strncat(a, b, c)
4d7b2685 485#define Strcpy(a, b) s_strcpy(a, b)
6e37afca
KB
486#define Strncpy(a, b, c) s_strncpy(a, b, c)
487#define Strlen(a) s_strlen(a)
488#define Strcmp(a, b) s_strcmp(a, b)
489#define Strncmp(a, b, c) s_strncmp(a, b, c)
490
491#define Strspl(a, b) s_strspl(a, b)
492#define Strsave(a) s_strsave(a)
493#define Strend(a) s_strend(a)
494#define Strstr(a, b) s_strstr(a, b)
495#endif
8c95f417
BJ
496
497/*
498 * setname is a macro to save space (see sh.err.c)
499 */
6e37afca
KB
500char *bname;
501
35371dec 502#define setname(a) (bname = (a))
8c95f417 503
6e37afca
KB
504Char *Vsav;
505Char *Vdp;
506Char *Vexpath;
507char **Vt;
508
6e37afca
KB
509Char **evalvec;
510Char *evalp;
511
6e37afca
KB
512/* word_chars is set by default to WORD_CHARS but can be overridden by
513 the worchars variable--if unset, reverts to WORD_CHARS */
514
515Char *word_chars;
516
517#define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
518
519Char *STR_SHELLPATH;
520
b9c4f741 521#include <paths.h>
6e37afca
KB
522#ifdef _PATH_BSHELL
523Char *STR_BSHELL;
8c95f417 524#endif
6e37afca
KB
525Char *STR_WORD_CHARS;
526Char **STR_environ;