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