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