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