xref: /titanic_51/usr/src/cmd/csh/sh.h (revision 779fc935796a940997d18b31d64a9fec9c6b40f6)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved.  The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <stdlib.h>	 /* MB_xxx, mbxxx(), wcxxx() etc. */
18 #include <limits.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/siginfo.h>
22 #include <sys/ucontext.h>
23 #include <sys/param.h>
24 #include <sys/stat.h>
25 #include <sys/termios.h>
26 #include <sys/ttold.h>
27 #include <errno.h>
28 #include <signal.h>	/* std sysV signal.h */
29 #include <setjmp.h>
30 #include <sys/resource.h>
31 #include "signal.h"	/* mainly BSD related signal.h */
32 #include "sh.local.h"
33 #include "sh.char.h"
34 
35 /*
36  * MAXHOSTNAMELEN is defined in param.h under SunOS
37  */
38 #define	MAXHOSTNAMELEN	64
39 
40 #ifdef MBCHAR
41 # if !defined(MB_LEN_MAX) || !defined(MB_CUR_MAX)
42 	Error: I need both ANSI macros!
43 # endif
44 #else
45 # if !defined(MB_LEN_MAX)
46 #  define MB_LEN_MAX	1
47 # endif
48 # if !defined(MB_CUR_MAX)
49 #  define MB_CUR_MAX	1
50 # endif
51 #endif
52 
53 #ifndef MBCHAR /* Let's replace the ANSI functions with our own macro
54 		* for efficiency!
55 		*/
56 #define	mbtowc(pwc, pmb, n_is_ignored)	((*(pwc)=*(pmb)), 1)
57 #define	wctomb(pmb, wc)			((*(pmb)=((char)wc)), 1)
58 #endif/*!MBCHAR*/
59 
60 /*
61  * C shell
62  *
63  * Bill Joy, UC Berkeley
64  * October, 1978; May 1980
65  *
66  * Jim Kulp, IIASA, Laxenburg Austria
67  * April, 1980
68  */
69 
70 /*If we are setting the $cwd variable becuz we did a
71   cd, chdir, pushd, popd command, then set didchdir to
72   1.  This prevents globbing down when setting $cwd.
73   However, if the user set $cwd, we want the globbing
74   done; so, didchdir would be equal to 0 in that case.
75  */
76 int didchdir;
77 
78 #define	isdir(d)	((d.st_mode & S_IFMT) == S_IFDIR)
79 
80 typedef	char	bool;
81 
82 /* tchar (Tagged CHARacter) is a place holder to keep a QUOTE bit and
83  * a character.
84  * For European language handling, lower 8 bits of tchar is used
85  * to store a character.  For other languages, especially Asian, 16 bits
86  * are used to store a character.
87  * Following typedef's assume short int is a 16-bit entity and long int is
88  * a 32-bit entity.
89  * The QUOTE bit tells whether the character is subject to further
90  * interpretation such as history substitution, file mathing, command
91  * subsitution.  TRIM is a mask to strip off the QUOTE bit.
92  */
93 #ifdef MBCHAR		/* For multibyte character handling. */
94 typedef long int	tchar;
95 #define QUOTE	0x80000000
96 #define	TRIM	0x7fffffff
97 #else/*!MBCHAR*/	/* European language requires only 8 bits. */
98 typedef unsigned short int	tchar;
99 #define	QUOTE	0x8000
100 #define	TRIM	0x00ff
101 #endif/*!MBCHAR*/
102 #define	eq(a, b)	(strcmp_(a, b) == 0)
103 
104 
105 /*
106  * Global flags
107  */
108 bool	chkstop;		/* Warned of stopped jobs... allow exit */
109 bool	didfds;			/* Have setup i/o fd's for child */
110 bool	doneinp;		/* EOF indicator after reset from readc */
111 bool	exiterr;		/* Exit if error or non-zero exit status */
112 bool	child;			/* Child shell ... errors cause exit */
113 bool	haderr;			/* Reset was because of an error */
114 bool	intty;			/* Input is a tty */
115 bool	cflg;			/* invoked with -c option */
116 bool	intact;			/* We are interactive... therefore prompt */
117 bool	justpr;			/* Just print because of :p hist mod */
118 bool	loginsh;		/* We are a loginsh -> .login/.logout */
119 bool	neednote;		/* Need to pnotify() */
120 bool	noexec;			/* Don't execute, just syntax check */
121 bool	pjobs;			/* want to print jobs if interrupted */
122 bool	pfcshflag;		/* set to 0 for pfcsh */
123 bool	setintr;		/* Set interrupts on/off -> Wait intr... */
124 bool	timflg;			/* Time the next waited for command */
125 bool	havhash;		/* path hashing is available */
126 bool	havhash2;		/* cdpath hashing is available */
127 #ifdef FILEC
128 bool	filec;			/* doing filename expansion */
129 #endif
130 
131 /*
132  * Global i/o info
133  */
134 tchar	*arginp;		/* Argument input for sh -c and internal `xx` */
135 int	onelflg;		/* 2 -> need line for -t, 1 -> exit on read */
136 tchar	*file;			/* Name of shell file for $0 */
137 
138 char	*err;			/* Error message from scanner/parser */
139 struct	timeval time0;		/* Time at which the shell started */
140 struct	rusage ru0;
141 
142 /*
143  * Miscellany
144  */
145 tchar	*doldol;		/* Character pid for $$ */
146 int	uid;			/* Invokers uid */
147 time_t	chktim;			/* Time mail last checked */
148 int	shpgrp;			/* Pgrp of shell */
149 int	tpgrp;			/* Terminal process group */
150 /* If tpgrp is -1, leave tty alone! */
151 int	opgrp;			/* Initial pgrp and tty pgrp */
152 int	oldisc;			/* Initial line discipline or -1 */
153 
154 /*
155  * These are declared here because they want to be
156  * initialized in sh.init.c (to allow them to be made readonly)
157  */
158 
159 extern struct	biltins {
160 	tchar	*bname;
161 	int	(*bfunct)();
162 	short	minargs, maxargs;
163 } bfunc[];
164 extern int nbfunc;
165 
166 extern struct srch {
167 	tchar	*s_name;
168 	short	s_value;
169 } srchn[];
170 extern int nsrchn;
171 
172 /*
173  * To be able to redirect i/o for builtins easily, the shell moves the i/o
174  * descriptors it uses away from 0,1,2.
175  * Ideally these should be in units which are closed across exec's
176  * (this saves work) but for version 6, this is not usually possible.
177  * The desired initial values for these descriptors are defined in
178  * sh.local.h.
179  */
180 short	SHIN;			/* Current shell input (script) */
181 short	SHOUT;			/* Shell output */
182 short	SHDIAG;			/* Diagnostic output... shell errs go here */
183 short	OLDSTD;			/* Old standard input (def for cmds) */
184 
185 /*
186  * Error control
187  *
188  * Errors in scanning and parsing set up an error message to be printed
189  * at the end and complete.  Other errors always cause a reset.
190  * Because of source commands and .cshrc we need nested error catches.
191  */
192 
193 jmp_buf	reslab;
194 
195 #define	setexit()	((void) setjmp(reslab))
196 #define	reset()		longjmp(reslab, 0)
197 	/* Should use structure assignment here */
198 #define	getexit(a)	copy((void *)(a), (void *)reslab, sizeof reslab)
199 #define	resexit(a)	copy((void *)reslab, ((void *)(a)), sizeof reslab)
200 
201 tchar	*gointr;		/* Label for an onintr transfer */
202 void	(*parintr)();		/* Parents interrupt catch */
203 void	(*parterm)();		/* Parents terminate catch */
204 
205 
206 /*
207  * Each level of input has a buffered input structure.
208  * There are one or more blocks of buffered input for each level,
209  * exactly one if the input is seekable and tell is available.
210  * In other cases, the shell buffers enough blocks to keep all loops
211  * in the buffer.
212  */
213 struct	Bin {
214 	off_t	Bfseekp;		/* Seek pointer */
215 	off_t	Bfbobp;			/* Seekp of beginning of buffers */
216 	off_t	Bfeobp;			/* Seekp of end of buffers */
217 	short	Bfblocks;		/* Number of buffer blocks */
218 	tchar	**Bfbuf;		/* The array of buffer blocks */
219 } B;
220 
221 #define	fseekp	B.Bfseekp
222 #define	fbobp	B.Bfbobp
223 #define	feobp	B.Bfeobp
224 #define	fblocks	B.Bfblocks
225 #define	fbuf	B.Bfbuf
226 
227 #define btell()	fseekp
228 
229 #ifndef btell
230 off_t	btell(void);
231 #endif
232 
233 /*
234  * The shell finds commands in loops by reseeking the input
235  * For whiles, in particular, it reseeks to the beginning of the
236  * line the while was on; hence the while placement restrictions.
237  */
238 off_t	lineloc;
239 
240 #ifdef	TELL
241 bool	cantell;			/* Is current source tellable ? */
242 #endif
243 
244 /*
245  * Input lines are parsed into doubly linked circular
246  * lists of words of the following form.
247  */
248 struct	wordent {
249 	tchar	*word;
250 	struct	wordent *prev;
251 	struct	wordent *next;
252 };
253 
254 /*
255  * During word building, both in the initial lexical phase and
256  * when expanding $ variable substitutions, expansion by `!' and `$'
257  * must be inhibited when reading ahead in routines which are themselves
258  * processing `!' and `$' expansion or after characters such as `\' or in
259  * quotations.  The following flags are passed to the getC routines
260  * telling them which of these substitutions are appropriate for the
261  * next character to be returned.
262  */
263 #define	DODOL	1
264 #define	DOEXCL	2
265 #define	DOALL	DODOL|DOEXCL
266 
267 /*
268  * Labuf implements a general buffer for lookahead during lexical operations.
269  * Text which is to be placed in the input stream can be stuck here.
270  * We stick parsed ahead $ constructs during initial input,
271  * process id's from `$$', and modified variable values (from qualifiers
272  * during expansion in sh.dol.c) here.
273  */
274 tchar	*labuf;
275 
276 tchar	*lap;
277 
278 /*
279  * Parser structure
280  *
281  * Each command is parsed to a tree of command structures and
282  * flags are set bottom up during this process, to be propagated down
283  * as needed during the semantics/exeuction pass (sh.sem.c).
284  */
285 struct	command {
286 	short	t_dtyp;				/* Type of node */
287 	short	t_dflg;				/* Flags, e.g. FAND|... */
288 	union {
289 		tchar	*T_dlef;		/* Input redirect word */
290 		struct	command *T_dcar;	/* Left part of list/pipe */
291 	} L;
292 	union {
293 		tchar	*T_drit;		/* Output redirect word */
294 		struct	command *T_dcdr;	/* Right part of list/pipe */
295 	} R;
296 #define	t_dlef	L.T_dlef
297 #define	t_dcar	L.T_dcar
298 #define	t_drit	R.T_drit
299 #define	t_dcdr	R.T_dcdr
300 	tchar	**t_dcom;			/* Command/argument vector */
301 	char	*cfname;			/* char pathname for execv */
302 	char	**cargs;			/* char arg vec  for execv */
303 	struct	command *t_dspr;		/* Pointer to ()'d subtree */
304 	short	t_nice;
305 };
306 
307 #define	TCOM	1		/* t_dcom <t_dlef >t_drit	*/
308 #define	TPAR	2		/* ( t_dspr ) <t_dlef >t_drit	*/
309 #define	TFIL	3		/* t_dlef | t_drit		*/
310 #define	TLST	4		/* t_dlef ; t_drit		*/
311 #define	TOR	5		/* t_dlef || t_drit		*/
312 #define	TAND	6		/* t_dlef && t_drit		*/
313 
314 #define	FSAVE	(FNICE|FTIME|FNOHUP)	/* save these when re-doing */
315 
316 #define	FAND	(1<<0)		/* executes in background	*/
317 #define	FCAT	(1<<1)		/* output is redirected >>	*/
318 #define	FPIN	(1<<2)		/* input is a pipe		*/
319 #define	FPOU	(1<<3)		/* output is a pipe		*/
320 #define	FPAR	(1<<4)		/* don't fork, last ()ized cmd	*/
321 #define	FINT	(1<<5)		/* should be immune from intr's */
322 /* spare */
323 #define	FDIAG	(1<<7)		/* redirect unit 2 with unit 1	*/
324 #define	FANY	(1<<8)		/* output was !			*/
325 #define	FHERE	(1<<9)		/* input redirection is <<	*/
326 #define	FREDO	(1<<10)		/* reexec aft if, repeat,...	*/
327 #define	FNICE	(1<<11)		/* t_nice is meaningful */
328 #define	FNOHUP	(1<<12)		/* nohup this command */
329 #define	FTIME	(1<<13)		/* time this command */
330 
331 /*
332  * The keywords for the parser
333  */
334 #define	ZBREAK		0
335 #define	ZBRKSW		1
336 #define	ZCASE		2
337 #define	ZDEFAULT 	3
338 #define	ZELSE		4
339 #define	ZEND		5
340 #define	ZENDIF		6
341 #define	ZENDSW		7
342 #define	ZEXIT		8
343 #define	ZFOREACH	9
344 #define	ZGOTO		10
345 #define	ZIF		11
346 #define	ZLABEL		12
347 #define	ZLET		13
348 #define	ZSET		14
349 #define	ZSWITCH		15
350 #define	ZTEST		16
351 #define	ZTHEN		17
352 #define	ZWHILE		18
353 
354 /*
355  * Structure defining the existing while/foreach loops at this
356  * source level.  Loops are implemented by seeking back in the
357  * input.  For foreach (fe), the word list is attached here.
358  */
359 struct	whyle {
360 	off_t	w_start;		/* Point to restart loop */
361 	off_t	w_end;			/* End of loop (0 if unknown) */
362 	tchar	**w_fe, **w_fe0;	/* Current/initial wordlist for fe */
363 	tchar	*w_fename;		/* Name for fe */
364 	struct	whyle *w_next;		/* Next (more outer) loop */
365 } *whyles;
366 
367 /*
368  * Variable structure
369  *
370  * Aliases and variables are stored in AVL balanced binary trees.
371  */
372 struct	varent {
373 	tchar	**vec;		/* Array of words which is the value */
374 	tchar	*v_name;	/* Name of variable/alias */
375 	struct	varent *v_link[3];	/* The links, see below */
376 	int	v_bal;		/* Balance factor */
377 } shvhed, aliases;
378 #define v_left		v_link[0]
379 #define v_right		v_link[1]
380 #define v_parent	v_link[2]
381 
382 struct varent *adrof1();
383 #define adrof(v)	adrof1(v, &shvhed)
384 #define value(v)	value1(v, &shvhed)
385 
386 /*
387  * MAX_VAR_LEN - maximum variable name defined by csh man page to be 20
388  */
389 #define MAX_VAR_LEN           20
390 
391 /*
392  * MAX_VREF_LEN - maximum variable reference $name[...]
393  * it can be as big as a csh word, which is 1024
394  */
395 #define MAX_VREF_LEN  1024
396 
397 
398 /*
399  * The following are for interfacing redo substitution in
400  * aliases to the lexical routines.
401  */
402 struct	wordent *alhistp;		/* Argument list (first) */
403 struct	wordent *alhistt;		/* Node after last in arg list */
404 tchar	**alvec;			/* The (remnants of) alias vector */
405 
406 /*
407  * Filename/command name expansion variables
408  */
409 short	gflag;				/* After tglob -> is globbing needed? */
410 
411 /*
412  * A reasonable limit on number of arguments would seem to be
413  * the maximum number of characters in an arg list / 6.
414  *
415  * XXX:	With the new VM system, NCARGS has become enormous, making
416  *	it impractical to allocate arrays with NCARGS / 6 entries on
417  *	the stack.  The proper fix is to revamp code elsewhere (in
418  *	sh.dol.c and sh.glob.c) to use a different technique for handling
419  *	command line arguments.  In the meantime, we simply fall back
420  *	on using the old value of NCARGS.
421  */
422 #ifdef	notyet
423 #define	GAVSIZ	(NCARGS / 6)
424 #else	/* notyet */
425 #define	GAVSIZ	(10240 / 6)
426 #endif	/* notyet */
427 
428 /*
429  * Variables for filename expansion
430  */
431 tchar	**gargv;			/* Pointer to the (stack) arglist */
432 long	gargc;				/* Number args in gargv */
433 long	gnleft;
434 
435 /*
436  * Variables for command expansion.
437  */
438 tchar	**pargv;			/* Pointer to the argv list space */
439 tchar	*pargs;				/* Pointer to start current word */
440 long	pargc;				/* Count of arguments in pargv */
441 long	pnleft;				/* Number of chars left in pargs */
442 tchar	*pargcp;			/* Current index into pargs */
443 
444 /*
445  * History list
446  *
447  * Each history list entry contains an embedded wordlist
448  * from the scanner, a number for the event, and a reference count
449  * to aid in discarding old entries.
450  *
451  * Essentially "invisible" entries are put on the history list
452  * when history substitution includes modifiers, and thrown away
453  * at the next discarding since their event numbers are very negative.
454  */
455 struct	Hist {
456 	struct	wordent Hlex;
457 	int	Hnum;
458 	int	Href;
459 	struct	Hist *Hnext;
460 } Histlist;
461 
462 struct	wordent	paraml;			/* Current lexical word list */
463 int	eventno;			/* Next events number */
464 int	lastev;				/* Last event reference (default) */
465 
466 tchar	HIST;				/* history invocation character */
467 tchar	HISTSUB;			/* auto-substitute character */
468 
469 /*
470  * In lines for frequently called functions
471  *
472  * WARNING: changes here also need to occur in the xfree() function in
473  *	    sh.misc.c.
474  */
475 #if defined(sparc)
476 #define XFREE(cp) { \
477 	extern char end[]; \
478 	char stack; \
479 /*??*/	if (((char *)(cp)) >= end && ((char *)(cp)) < &stack) \
480 /*??*/		free((void *)(cp)); \
481 }
482 #elif defined(i386)
483 #define XFREE(cp) { \
484 	extern char end[]; \
485 	if (((char *)(cp)) >= end) \
486 		free((void *)(cp)); \
487 }
488 #else
489 #error XFREE macro is machine dependant and no machine type is recognized
490 #endif
491 void	*alloctmp;
492 #define xalloc(i) ((alloctmp = (void *)malloc(i)) ? alloctmp : (void *)nomem(i))/*??*/
493 #define	xrealloc(buf, i) ((alloctmp = (void *)realloc(buf, i)) ? alloctmp : \
494 				(void *)nomem(i))
495 extern void	Putchar(tchar);
496 extern void	bferr(char *)	__NORETURN;
497 extern void	error()	__NORETURN;
498 extern void	exitstat(void)	__NORETURN;
499 extern tchar	*Dfix1(tchar *);
500 extern tchar	**blkcpy(tchar **, tchar **);
501 extern tchar	**blkspl(tchar **, tchar **);
502 extern char	**blkspl_(char **, char **);
503 extern tchar	**copyblk(tchar **);
504 extern tchar	**dobackp(tchar *, bool);
505 extern tchar	*domod(tchar *, int);
506 extern struct	Hist *enthist(int, struct wordent *, bool);
507 extern tchar	*getenv_(tchar *);
508 extern tchar	*getenvs_(char *);
509 extern tchar	*getwd_(tchar *);
510 extern tchar	**glob(tchar **);
511 extern tchar	*globone(tchar *);
512 extern tchar	*index_(tchar *, tchar);
513 extern struct biltins	*isbfunc(struct command *);
514 extern void	pintr(void);
515 extern void	pchild(void);
516 extern tchar	*putn(int);
517 extern tchar	*rindex_(tchar *, tchar);
518 extern tchar	**saveblk(tchar **);
519 extern tchar	*savestr(tchar *);
520 extern tchar	*strcat_(tchar *, tchar *);
521 extern int	strlen_(tchar *);
522 extern tchar	*strcpy_(tchar *, tchar *);
523 extern tchar	*strend(tchar *);
524 extern tchar	*strip(tchar *);
525 extern tchar	*strspl(tchar *, tchar *);
526 extern struct command	*syntax(struct wordent *, struct wordent *, int);
527 extern tchar	*value1(tchar *, struct varent *);
528 
529 #define	NOSTR	((tchar *) 0)
530 
531 /*
532  * setname is a macro to copy the path in bname. (see sh.err.c)
533  * Here we are dynamically reallocating the bname to the new length
534  * to store the new path
535  */
536 tchar	*bname;
537 #define	setname(a)	 { \
538 	bname = xrealloc(bname, (strlen_(a)+1) * sizeof (tchar)); \
539 	strcpy_(bname, a); \
540 	bname[strlen_(a)] = '\0'; \
541 }
542 
543 #ifdef VFORK
544 tchar	*Vsav;
545 tchar	**Vav;
546 tchar	*Vdp;
547 #endif
548 
549 tchar	**evalvec;
550 tchar	*evalp;
551 
552 /* Conversion functions between char and tchar strings. */
553 tchar	*strtots(/* tchar * , char * */);
554 char	*tstostr(/* char *  , tchar * */);
555 
556 #ifndef NULL
557 #define	NULL	0
558 #endif
559 
560 
561 /*
562  * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
563  * to hash execs.  If it is allocated (havhash true), then to tell
564  * whether ``name'' is (possibly) present in the i'th component
565  * of the variable path, you look at the bit in xhash indexed by
566  * hash(hashname("name"), i).  This is setup automatically
567  * after .login is executed, and recomputed whenever ``path'' is
568  * changed.
569  * The two part hash function is designed to let texec() call the
570  * more expensive hashname() only once and the simple hash() several
571  * times (once for each path component checked).
572  * Byte size is assumed to be 8.
573  */
574 #define HSHSIZ          (32*1024)       /* 4k bytes */
575 #define HSHMASK         (HSHSIZ - 1)
576 #define HSHMUL          243
577 
578 /*
579  * The following two arrays are used for caching.  xhash
580  * is for caching path variable and xhash2 is for cdpath
581  * variable.
582  */
583 
584 char xhash[HSHSIZ / 8];
585 char xhash2[HSHSIZ / 8];
586 #define hash(a, b)      ((a) * HSHMUL + (b) & HSHMASK)
587 #define bit(h, b)       ((h)[(b) >> 3] & 1 << ((b) & 7))        /* bit test */
588 #define bis(h, b)       ((h)[(b) >> 3] |= 1 << ((b) & 7))       /* bit set */
589 #ifdef VFORK
590 int     hits, misses;
591 #endif
592 
593 
594