xref: /openbsd/bin/csh/csh.h (revision 914fbe0c)
1 /*	$OpenBSD: csh.h,v 1.34 2020/08/30 22:23:47 mortimer Exp $	*/
2 /*	$NetBSD: csh.h,v 1.9 1995/03/21 09:02:40 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1980, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)csh.h	8.1 (Berkeley) 5/31/93
33  */
34 
35 /*
36  * Fundamental definitions which may vary from system to system.
37  *
38  *	BUFSIZ		The i/o buffering size; also limits word size
39  *	MAILINTVL	How often to mailcheck; more often is more expensive
40  */
41 
42 #define FORKSLEEP	10	/* delay loop on non-interactive fork failure */
43 #define	MAILINTVL	600	/* 10 minutes */
44 
45 /*
46  * The shell moves std in/out/diag and the old std input away from units
47  * 0, 1, and 2 so that it is easy to set up these standards for invoked
48  * commands.
49  */
50 #define	FSHTTY	15		/* /dev/tty when manip pgrps */
51 #define	FSHIN	16		/* Preferred desc for shell input */
52 #define	FSHOUT	17		/* ... shell output */
53 #define	FSHERR	18		/* ... shell diagnostics */
54 #define	FOLDSTD	19		/* ... old std input */
55 
56 typedef short Char;
57 
58 #define SAVE(a) (Strsave(str2short(a)))
59 
60 /*
61  * Make sure a variable is not stored in register a by taking its address
62  * This is used where variables might be clobbered by longjmp.
63  */
64 #define UNREGISTER(a)	(void) &a
65 
66 typedef void *ioctl_t;		/* Third arg of ioctl */
67 
68 #include "const.h"
69 #include "char.h"
70 #include "error.h"
71 
72 #include <stdio.h>
73 extern FILE *cshin, *cshout, *csherr;
74 
75 #define	isdir(d)	(S_ISDIR(d.st_mode))
76 
77 typedef int bool;
78 
79 #define	eq(a, b)	(Strcmp(a, b) == 0)
80 
81 /* globone() flags */
82 #define G_ERROR		0	/* default action: error if multiple words */
83 #define G_IGNORE	1	/* ignore the rest of the words */
84 #define G_APPEND	2	/* make a sentence by cat'ing the words */
85 
86 /*
87  * Global flags
88  */
89 extern bool    chkstop;		/* Warned of stopped jobs... allow exit */
90 extern bool    didfds;		/* Have setup i/o fd's for child */
91 extern bool    doneinp;		/* EOF indicator after reset from readc */
92 extern bool    exiterr;		/* Exit if error or non-zero exit status */
93 extern bool    child;		/* Child shell ... errors cause exit */
94 extern bool    haderr;		/* Reset was because of an error */
95 extern bool    intty;		/* Input is a tty */
96 extern bool    intact;		/* We are interactive... therefore prompt */
97 extern bool    justpr;		/* Just print because of :p hist mod */
98 extern bool    loginsh;		/* We are a loginsh -> .login/.logout */
99 extern bool    neednote;	/* Need to pnotify() */
100 extern bool    noexec;		/* Don't execute, just syntax check */
101 extern bool    pjobs;		/* want to print jobs if interrupted */
102 extern bool    setintr;		/* Set interrupts on/off -> Wait intr... */
103 extern bool    timflg;		/* Time the next waited for command */
104 extern bool    havhash;		/* path hashing is available */
105 
106 extern bool    filec;		/* doing filename expansion */
107 extern bool    needprompt;	/* print prompt, used by filec */
108 
109 /*
110  * Global i/o info
111  */
112 extern Char   *arginp;		/* Argument input for sh -c and internal `xx` */
113 extern int     onelflg;		/* 2 -> need line for -t, 1 -> exit on read */
114 extern Char   *ffile;		/* Name of shell file for $0 */
115 
116 extern char   *seterr;		/* Error message from scanner/parser */
117 
118 #include <sys/types.h>
119 #include <sys/time.h>
120 #include <sys/resource.h>
121 
122 extern struct timespec time0;	/* Time at which the shell started */
123 extern struct rusage ru0;
124 
125 /*
126  * Miscellany
127  */
128 extern Char   *doldol;		/* Character pid for $$ */
129 extern int	backpid;	/* Pid of the last background process */
130 extern uid_t	uid, euid;	/* Invokers uid */
131 extern gid_t	gid, egid;	/* Invokers gid */
132 extern time_t  chktim;		/* Time mail last checked */
133 extern pid_t	shpgrp;		/* Pgrp of shell */
134 extern pid_t	tpgrp;		/* Terminal process group */
135 
136 /* If tpgrp is -1, leave tty alone! */
137 extern pid_t	opgrp;		/* Initial pgrp and tty pgrp */
138 
139 
140 /*
141  * To be able to redirect i/o for builtins easily, the shell moves the i/o
142  * descriptors it uses away from 0,1,2.
143  * Ideally these should be in units which are closed across exec's
144  * (this saves work) but for version 6, this is not usually possible.
145  * The desired initial values for these descriptors are F{SHIN,...}.
146  */
147 extern int   SHIN;		/* Current shell input (script) */
148 extern int   SHOUT;		/* Shell output */
149 extern int   SHERR;		/* Diagnostic output... shell errs go here */
150 extern int   OLDSTD;		/* Old standard input (def for cmds) */
151 
152 /*
153  * Error control
154  *
155  * Errors in scanning and parsing set up an error message to be printed
156  * at the end and complete.  Other errors always cause a reset.
157  * Because of source commands and .cshrc we need nested error catches.
158  */
159 
160 #include <setjmp.h>
161 extern jmp_buf reslab;
162 extern int exitset;
163 
164 #define	setexit()	(setjmp(reslab))
165 #define	reset()		longjmp(reslab, 1)
166  /* Should use structure assignment here */
167 #define	getexit(a)	memcpy((a), reslab, sizeof reslab)
168 #define	resexit(a)	memcpy(reslab, (a), sizeof reslab)
169 
170 extern Char   *gointr;		/* Label for an onintr transfer */
171 
172 #include <signal.h>
173 extern sig_t parintr;		/* Parents interrupt catch */
174 extern sig_t parterm;		/* Parents terminate catch */
175 
176 /*
177  * Lexical definitions.
178  *
179  * All lexical space is allocated dynamically.
180  * The eighth/sixteenth bit of characters is used to prevent recognition,
181  * and eventually stripped.
182  */
183 #define	META		0200
184 #define	ASCII		0177
185 #define	QUOTE 		0100000U /* 16nth char bit used for 'ing */
186 #define	TRIM		0077777	/* Mask to strip quote bit */
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 extern 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 re-seeking the input
233  * For whiles, in particular, it re-seeks to the beginning of the
234  * line the while was on; hence the while placement restrictions.
235  */
236 extern struct Ain lineloc;
237 
238 extern 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 extern 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/execution 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)(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 extern 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 extern 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(Char *, struct varent *);
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 extern struct wordent *alhistp;	/* Argument list (first) */
402 extern struct wordent *alhistt;	/* Node after last in arg list */
403 extern Char  **alvec, *alvecp;	/* The (remnants of) alias vector */
404 
405 /*
406  * Filename/command name expansion variables
407  */
408 extern 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 extern Char   *pargs;		/* Pointer to start current word */
424 extern long    pnleft;		/* Number of chars left in pargs */
425 extern 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 extern struct Hist {
439     struct wordent Hlex;
440     int     Hnum;
441     int     Href;
442     struct Hist *Hnext;
443 }       Histlist;
444 
445 extern struct wordent paraml;	/* Current lexical word list */
446 extern int     eventno;		/* Next events number */
447 extern int     lastev;		/* Last event reference (default) */
448 
449 extern Char    HIST;		/* history invocation character */
450 extern Char    HISTSUB;		/* auto-substitute character */
451 
452 /*
453  * setname is a macro to save space (see sh.err.c)
454  */
455 extern char   *bname;
456 
457 #define	setname(a)	(bname = (a))
458 
459 extern Char   *Vsav;
460 extern Char   *Vdp;
461 extern Char   *Vexpath;
462 extern char  **Vt;
463 
464 extern Char  **evalvec;
465 extern Char   *evalp;
466 
467 /* word_chars is set by default to WORD_CHARS but can be overridden by
468    the worchars variable--if unset, reverts to WORD_CHARS */
469 
470 extern Char   *word_chars;
471 
472 #define WORD_CHARS "*?_-.[]~="	/* default chars besides alnums in words */
473 
474 extern Char   *STR_SHELLPATH;
475 
476 #include <paths.h>
477 extern Char   *STR_BSHELL;
478 extern Char   *STR_WORD_CHARS;
479 extern Char  **STR_environ;
480