2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
17 #include <stdlib.h> /* MB_xxx, mbxxx(), wcxxx() etc. */
20 #include <sys/types.h>
21 #include <sys/siginfo.h>
22 #include <sys/ucontext.h>
23 #include <sys/param.h>
25 #include <sys/termios.h>
26 #include <sys/ttold.h>
28 #include <signal.h> /* std sysV signal.h */
30 #include <sys/resource.h>
31 #include <netdb.h> /* for MAXHOSTNAMELEN */
32 #include "signal.h" /* mainly BSD related signal.h */
38 #if !defined(MB_LEN_MAX) || !defined(MB_CUR_MAX)
39 Error
: I need both ANSI macros
!
42 #if !defined(MB_LEN_MAX)
45 #if !defined(MB_CUR_MAX)
50 #ifndef MBCHAR /* Let's replace the ANSI functions with our own macro
53 #define mbtowc(pwc, pmb, n_is_ignored) ((*(pwc) = *(pmb)), 1)
54 #define wctomb(pmb, wc) ((*(pmb) = ((char)wc)), 1)
60 * Bill Joy, UC Berkeley
61 * October, 1978; May 1980
63 * Jim Kulp, IIASA, Laxenburg Austria
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.
76 #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
81 * tchar (Tagged CHARacter) is a place holder to keep a QUOTE bit and
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
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.
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
;
101 #define eq(a, b) (strcmp_(a, b) == 0)
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 */
127 bool filec
; /* doing filename expansion */
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 */
137 char *err
; /* Error message from scanner/parser */
138 struct timeval time0
; /* Time at which the shell started */
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 */
154 * These are declared here because they want to be
155 * initialized in sh.init.c (to allow them to be made readonly)
158 extern struct biltins
{
161 short minargs
, maxargs
;
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
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) */
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.
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)
200 tchar
*gointr
; /* Label for an onintr transfer */
201 void (*parintr
)(); /* Parents interrupt catch */
202 void (*parterm
)(); /* Parents terminate catch */
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
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 */
220 #define fseekp B.Bfseekp
221 #define fbobp B.Bfbobp
222 #define feobp B.Bfeobp
223 #define fblocks B.Bfblocks
226 #define btell() fseekp
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.
240 bool cantell
; /* Is current source tellable ? */
244 * Input lines are parsed into doubly linked circular
245 * lists of words of the following form.
249 struct wordent
*prev
;
250 struct wordent
*next
;
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.
264 #define DOALL DODOL|DOEXCL
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.
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).
285 short t_dtyp
; /* Type of node */
286 short t_dflg
; /* Flags, e.g. FAND|... */
288 tchar
*T_dlef
; /* Input redirect word */
289 struct command
*T_dcar
; /* Left part of list/pipe */
292 tchar
*T_drit
; /* Output redirect word */
293 struct command
*T_dcdr
; /* Right part of list/pipe */
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 */
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 */
313 #define FSAVE (FNICE|FTIME|FNOHUP) /* save these when re-doing */
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 */
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 */
331 * The keywords for the parser
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.
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 */
369 * Aliases and variables are stored in AVL balanced binary trees.
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 */
377 #define v_left v_link[0]
378 #define v_right v_link[1]
379 #define v_parent v_link[2]
381 struct varent
*adrof1();
382 #define adrof(v) adrof1(v, &shvhed)
383 #define value(v) value1(v, &shvhed)
386 * MAX_VAR_LEN - maximum variable name defined by csh man page to be 128
388 #define MAX_VAR_LEN 128
391 * MAX_VREF_LEN - maximum variable reference $name[...]
392 * it can be as big as a csh word, which is 1024
394 #define MAX_VREF_LEN 1024
398 * The following are for interfacing redo substitution in
399 * aliases to the lexical routines.
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 */
406 * Filename/command name expansion variables
408 short gflag
; /* After tglob -> is globbing needed? */
411 * A reasonable limit on number of arguments would seem to be
412 * the maximum number of characters in an arg list / 6.
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.
422 #define GAVSIZ (NCARGS / 6)
424 #define GAVSIZ (10240 / 6)
428 * Variables for filename expansion
430 tchar
**gargv
; /* Pointer to the (stack) arglist */
431 long gargc
; /* Number args in gargv */
435 * Variables for command expansion.
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 */
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.
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.
461 struct wordent paraml
; /* Current lexical word list */
462 int eventno
; /* Next events number */
463 int lastev
; /* Last event reference (default) */
465 tchar HIST
; /* history invocation character */
466 tchar HISTSUB
; /* auto-substitute character */
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 *);
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
*);
507 #define NOSTR ((tchar *) 0)
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
515 #define setname(a) { \
516 bname = xrealloc(bname, (strlen_(a)+1) * sizeof (tchar)); \
518 bname[strlen_(a)] = '\0'; \
530 /* Conversion functions between char and tchar strings. */
531 tchar
*strtots(/* tchar * , char * */);
532 char *tstostr(/* char * , tchar * */);
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
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.
552 #define HSHSIZ (32*1024) /* 4k bytes */
553 #define HSHMASK (HSHSIZ - 1)
557 * The following two arrays are used for caching. xhash
558 * is for caching path variable and xhash2 is for cdpath
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 */