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