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