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