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