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