Home | History | Annotate | Line # | Download | only in ksh
sh.h revision 1.9
      1 /*	$NetBSD: sh.h,v 1.9 2017/06/22 13:33:39 kamil Exp $	*/
      2 
      3 /*
      4  * Public Domain Bourne/Korn shell
      5  */
      6 
      7 /* $Id: sh.h,v 1.9 2017/06/22 13:33:39 kamil Exp $ */
      8 
      9 #include "config.h"	/* system and option configuration info */
     10 
     11 #ifdef HAVE_PROTOTYPES
     12 # define	ARGS(args)	args	/* prototype declaration */
     13 #else
     14 # define	ARGS(args)	()	/* K&R declaration */
     15 #endif
     16 
     17 /* Start of common headers */
     18 
     19 #include <stdio.h>
     20 #include <sys/types.h>
     21 #include <setjmp.h>
     22 #ifdef HAVE_STDDEF_H
     23 # include <stddef.h>
     24 #endif
     25 
     26 #ifdef HAVE_STDLIB_H
     27 # include <stdlib.h>
     28 #else
     29 /* just a useful subset of what stdlib.h would have */
     30 extern char * getenv  ARGS((const char *));
     31 extern void * malloc  ARGS((size_t));
     32 extern void * realloc ARGS((void *, size_t));
     33 extern int    free    ARGS((void *));
     34 extern int    exit    ARGS((int));
     35 extern int    rand    ARGS((void));
     36 extern void   srand   ARGS((unsigned int));
     37 extern int    atoi    ARGS((const char *));
     38 #endif /* HAVE_STDLIB_H */
     39 
     40 #ifdef HAVE_UNISTD_H
     41 # include <unistd.h>
     42 #else
     43 /* just a useful subset of what unistd.h would have */
     44 extern int access ARGS((const char *, int));
     45 extern int open ARGS((const char *, int, ...));
     46 extern int creat ARGS((const char *, mode_t));
     47 extern int read ARGS((int, char *, unsigned));
     48 extern int write ARGS((int, const char *, unsigned));
     49 extern off_t lseek ARGS((int, off_t, int));
     50 extern int close ARGS((int));
     51 extern int pipe ARGS((int []));
     52 extern int dup2 ARGS((int, int));
     53 extern int unlink ARGS((const char *));
     54 extern int fork ARGS((void));
     55 extern int execve ARGS((const char *, char * const[], char * const[]));
     56 extern int chdir ARGS((const char *));
     57 extern int kill ARGS((pid_t, int));
     58 extern char *getcwd();	/* no ARGS here - differs on different machines */
     59 extern int geteuid ARGS((void));
     60 extern int readlink ARGS((const char *, char *, int));
     61 extern int getegid ARGS((void));
     62 extern int getpid ARGS((void));
     63 extern int getppid ARGS((void));
     64 extern unsigned int sleep ARGS((unsigned int));
     65 extern int isatty ARGS((int));
     66 # ifdef POSIX_PGRP
     67 extern int getpgrp ARGS((void));
     68 extern int setpgid ARGS((pid_t, pid_t));
     69 # endif /* POSIX_PGRP */
     70 # ifdef BSD_PGRP
     71 extern int getpgrp ARGS((pid_t));
     72 extern int setpgrp ARGS((pid_t, pid_t));
     73 # endif /* BSD_PGRP */
     74 # ifdef SYSV_PGRP
     75 extern int getpgrp ARGS((void));
     76 extern int setpgrp ARGS((void));
     77 # endif /* SYSV_PGRP */
     78 #endif /* HAVE_UNISTD_H */
     79 
     80 #ifdef HAVE_STRING_H
     81 # include <string.h>
     82 #else
     83 # include <strings.h>
     84 # define strchr index
     85 # define strrchr rindex
     86 #endif /* HAVE_STRING_H */
     87 #ifndef HAVE_STRSTR
     88 char *strstr ARGS((const char *s, const char *p));
     89 #endif /* HAVE_STRSTR */
     90 #ifndef HAVE_STRCASECMP
     91 int strcasecmp ARGS((const char *s1, const char *s2));
     92 int strncasecmp ARGS((const char *s1, const char *s2, int n));
     93 #endif /* HAVE_STRCASECMP */
     94 
     95 #ifdef HAVE_MEMORY_H
     96 # include <memory.h>
     97 #endif
     98 #ifndef HAVE_MEMSET
     99 # define memcpy(d, s, n)	bcopy(s, d, n)
    100 # define memcmp(s1, s2, n)	bcmp(s1, s2, n)
    101 void *memset ARGS((void *d, int c, size_t n));
    102 #endif /* HAVE_MEMSET */
    103 #ifndef HAVE_MEMMOVE
    104 # ifdef HAVE_BCOPY
    105 #  define memmove(d, s, n)	bcopy(s, d, n)
    106 # else
    107 void *memmove ARGS((void *d, const void *s, size_t n));
    108 # endif
    109 #endif /* HAVE_MEMMOVE */
    110 
    111 #ifdef HAVE_PROTOTYPES
    112 # include <stdarg.h>
    113 # define SH_VA_START(va, argn) va_start(va, argn)
    114 #else
    115 # include <varargs.h>
    116 # define SH_VA_START(va, argn) va_start(va)
    117 #endif /* HAVE_PROTOTYPES */
    118 
    119 #include <errno.h>
    120 
    121 #ifdef HAVE_FCNTL_H
    122 # include <fcntl.h>
    123 #else
    124 # include <sys/file.h>
    125 #endif /* HAVE_FCNTL_H */
    126 #ifndef O_ACCMODE
    127 # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
    128 #endif /* !O_ACCMODE */
    129 
    130 #ifndef F_OK 	/* access() arguments */
    131 # define F_OK 0
    132 # define X_OK 1
    133 # define W_OK 2
    134 # define R_OK 4
    135 #endif /* !F_OK */
    136 
    137 #ifndef SEEK_SET
    138 # ifdef L_SET
    139 #  define SEEK_SET L_SET
    140 #  define SEEK_CUR L_INCR
    141 #  define SEEK_END L_XTND
    142 # else /* L_SET */
    143 #  define SEEK_SET 0
    144 #  define SEEK_CUR 1
    145 #  define SEEK_END 2
    146 # endif /* L_SET */
    147 #endif /* !SEEK_SET */
    148 
    149 /* Some machines (eg, FreeBSD 1.1.5) define CLK_TCK in limits.h
    150  * (ksh_limval.h assumes limits has been included, if available)
    151  */
    152 #ifdef HAVE_LIMITS_H
    153 # include <limits.h>
    154 #endif /* HAVE_LIMITS_H */
    155 
    156 #include <signal.h>
    157 #ifdef	NSIG
    158 # define SIGNALS	NSIG
    159 #else
    160 # ifdef	_MINIX
    161 #  define SIGNALS	(_NSIG+1) /* _NSIG is # of signals used, excluding 0. */
    162 # else
    163 #  ifdef _SIGMAX	/* QNX */
    164 #   define SIGNALS	_SIGMAX
    165 #  else /* _SIGMAX */
    166 #   define SIGNALS	32
    167 #  endif /* _SIGMAX */
    168 # endif	/* _MINIX */
    169 #endif	/* NSIG */
    170 #ifndef SIGCHLD
    171 # define SIGCHLD SIGCLD
    172 #endif
    173 /* struct sigaction.sa_flags is set to KSH_SA_FLAGS.  Used to ensure
    174  * system calls are interrupted
    175  */
    176 #ifdef SA_INTERRUPT
    177 # define KSH_SA_FLAGS	SA_INTERRUPT
    178 #else /* SA_INTERRUPT */
    179 # define KSH_SA_FLAGS	0
    180 #endif /* SA_INTERRUPT */
    181 
    182 typedef	RETSIGTYPE (*handler_t) ARGS((int));	/* signal handler */
    183 
    184 #ifdef USE_FAKE_SIGACT
    185 # include "sigact.h"			/* use sjg's fake sigaction() */
    186 #endif
    187 
    188 #ifdef HAVE_PATHS_H
    189 # include <paths.h>
    190 #endif /* HAVE_PATHS_H */
    191 #ifdef _PATH_DEFPATH
    192 # define DEFAULT__PATH _PATH_DEFPATH
    193 #else /* _PATH_DEFPATH */
    194 # define DEFAULT__PATH DEFAULT_PATH
    195 #endif /* _PATH_DEFPATH */
    196 
    197 #ifndef offsetof
    198 # define offsetof(type,id) ((size_t)&((type*)NULL)->id)
    199 #endif
    200 
    201 #ifndef HAVE_KILLPG
    202 # define killpg(p, s)	kill(-(p), (s))
    203 #endif /* !HAVE_KILLPG */
    204 
    205 /* Special cases for execve(2) */
    206 # if defined(OS_ISC) && defined(_POSIX_SOURCE)
    207 /* Kludge for ISC 3.2 (and other versions?) so programs will run correctly.  */
    208 #  define ksh_execve(p, av, ev, flags) \
    209 				do { \
    210 					__setostype(0); \
    211 					execve(p, av, ev); \
    212 					__setostype(1); \
    213 				} while (0)
    214 # else /* OS_ISC && _POSIX */
    215 #  define ksh_execve(p, av, ev, flags)	execve(p, av, ev)
    216 # endif /* OS_ISC && _POSIX */
    217 
    218 /* this is a hang-over from older versions of the os2 port */
    219 #define ksh_dupbase(fd, base) fcntl(fd, F_DUPFD, base)
    220 
    221 #ifdef HAVE_SIGSETJMP
    222 # define ksh_sigsetjmp(env,sm)	sigsetjmp((env), (sm))
    223 # define ksh_siglongjmp(env,v)	siglongjmp((env), (v))
    224 # define ksh_jmp_buf		sigjmp_buf
    225 #else /* HAVE_SIGSETJMP */
    226 # ifdef HAVE__SETJMP
    227 #  define ksh_sigsetjmp(env,sm)	_setjmp(env)
    228 #  define ksh_siglongjmp(env,v)	_longjmp((env), (v))
    229 # else /* HAVE__SETJMP */
    230 #  define ksh_sigsetjmp(env,sm)	setjmp(env)
    231 #  define ksh_siglongjmp(env,v)	longjmp((env), (v))
    232 # endif /* HAVE__SETJMP */
    233 # define ksh_jmp_buf		jmp_buf
    234 #endif /* HAVE_SIGSETJMP */
    235 
    236 #ifndef HAVE_DUP2
    237 extern int dup2 ARGS((int, int));
    238 #endif /* !HAVE_DUP2 */
    239 
    240 /* Find a integer type that is at least 32 bits (or die) - SIZEOF_* defined
    241  * by autoconf (assumes an 8 bit byte, but I'm not concerned).
    242  * NOTE: INT32 may end up being more than 32 bits.
    243  */
    244 #if SIZEOF_INT >= 4
    245 # define INT32	int
    246 #else /* SIZEOF_INT */
    247 # if SIZEOF_LONG >= 4
    248 #  define INT32	long
    249 # else /* SIZEOF_LONG */
    250    #error cannot find 32 bit type...
    251 # endif /* SIZEOF_LONG */
    252 #endif /* SIZEOF_INT */
    253 
    254 /* end of common headers */
    255 
    256 /* Stop gcc and lint from complaining about possibly uninitialized variables */
    257 #if defined(__GNUC__) || defined(lint)
    258 # define UNINITIALIZED(var)	var = 0
    259 #else
    260 # define UNINITIALIZED(var)	var
    261 #endif /* GNUC || lint */
    262 
    263 /* some useful #defines */
    264 #ifdef EXTERN
    265 # define I__(i) = i
    266 #else
    267 # define I__(i)
    268 # define EXTERN extern
    269 # define EXTERN_DEFINED
    270 #endif
    271 
    272 #ifndef EXECSHELL
    273 /* shell to exec scripts (see also $SHELL initialization in main.c) */
    274 #  define EXECSHELL	"/bin/sh"
    275 #  define EXECSHELL_STR	"EXECSHELL"
    276 #endif
    277 
    278 /* ISABSPATH() means path is fully and completely specified,
    279  * ISROOTEDPATH() means a .. as the first component is a no-op,
    280  * ISRELPATH() means $PWD can be tacked on to get an absolute path.
    281  *
    282  * OS		Path		ISABSPATH	ISROOTEDPATH	ISRELPATH
    283  * unix		/foo		yes		yes		no
    284  * unix		foo		no		no		yes
    285  * unix		../foo		no		no		yes
    286  */
    287 # define PATHSEP        ':'
    288 # define DIRSEP         '/'
    289 # define DIRSEPSTR      "/"
    290 # define ISDIRSEP(c)    ((c) == '/')
    291 # define ISABSPATH(s)	ISDIRSEP((s)[0])
    292 # define ISRELPATH(s)	(!ISABSPATH(s))
    293 # define ISROOTEDPATH(s) ISABSPATH(s)
    294 # define FILECHCONV(c)	c
    295 # define FILECMP(s1, s2) strcmp(s1, s2)
    296 # define FILENCMP(s1, s2, n) strncmp(s1, s2, n)
    297 # define ksh_strchr_dirsep(p)   strchr(p, DIRSEP)
    298 # define ksh_strrchr_dirsep(p)  strrchr(p, DIRSEP)
    299 
    300 typedef int bool_t;
    301 #define	FALSE	0
    302 #define	TRUE	1
    303 
    304 #define	NELEM(a) (sizeof(a) / sizeof((a)[0]))
    305 #define	sizeofN(type, n) (sizeof(type) * (n))
    306 #define	BIT(i)	(1<<(i))	/* define bit in flag */
    307 
    308 /* Table flag type - needs > 16 and < 32 bits */
    309 typedef INT32 Tflag;
    310 
    311 #define	NUFILE	32		/* Number of user-accessible files */
    312 #define	FDBASE	10		/* First file usable by Shell */
    313 
    314 /* you're not going to run setuid shell scripts, are you? */
    315 #define	eaccess(path, mode)	access(path, mode)
    316 
    317 /* Make MAGIC a char that might be printed to make bugs more obvious, but
    318  * not a char that is used often.  Also, can't use the high bit as it causes
    319  * portability problems (calling strchr(x, 0x80|'x') is error prone).
    320  */
    321 #define	MAGIC		(7)	/* prefix for *?[!{,} during expand */
    322 #define ISMAGIC(c)	((unsigned char)(c) == MAGIC)
    323 #define	NOT		'!'	/* might use ^ (ie, [!...] vs [^..]) */
    324 
    325 #define	LINE	1024		/* input line size */
    326 #define	PATH	1024		/* pathname size (todo: PATH_MAX/pathconf()) */
    327 #define ARRAYMAX 1023		/* max array index */
    328 
    329 EXTERN	const char *kshname;	/* $0 */
    330 EXTERN	pid_t	kshpid;		/* $$, shell pid */
    331 EXTERN	pid_t	procpid;	/* pid of executing process */
    332 EXTERN	uid_t	ksheuid;	/* effective uid of shell */
    333 EXTERN	int	exstat;		/* exit status */
    334 EXTERN	int	subst_exstat;	/* exit status of last $(..)/`..` */
    335 EXTERN	const char *safe_prompt; /* safe prompt if PS1 substitution fails */
    336 
    337 /*
    338  * Area-based allocation built on malloc/free
    339  */
    340 typedef struct Area {
    341 	struct link *freelist;	/* free list */
    342 } Area;
    343 
    344 EXTERN	Area	aperm;		/* permanent object space */
    345 #define	APERM	&aperm
    346 #define	ATEMP	&e->area
    347 
    348 #ifdef MEM_DEBUG
    349 # include "chmem.h" /* a debugging front end for malloc et. al. */
    350 #endif /* MEM_DEBUG */
    351 
    352 #ifdef KSH_DEBUG
    353 # define kshdebug_init()	kshdebug_init_()
    354 # define kshdebug_printf(a)	kshdebug_printf_ a
    355 # define kshdebug_dump(a)	kshdebug_dump_ a
    356 #else /* KSH_DEBUG */
    357 # define kshdebug_init()
    358 # define kshdebug_printf(a)
    359 # define kshdebug_dump(a)
    360 #endif /* KSH_DEBUG */
    361 
    362 /*
    363  * parsing & execution environment
    364  */
    365 EXTERN	struct env {
    366 	short	type;			/* environment type - see below */
    367 	short	flags;			/* EF_* */
    368 	Area	area;			/* temporary allocation area */
    369 	struct	block *loc;		/* local variables and functions */
    370 	short  *savefd;			/* original redirected fd's */
    371 	struct	env *oenv;		/* link to previous environment */
    372 	ksh_jmp_buf jbuf;		/* long jump back to env creator */
    373 	struct temp *temps;		/* temp files */
    374 } *e;
    375 
    376 /* struct env.type values */
    377 #define	E_NONE	0		/* dummy environment */
    378 #define	E_PARSE	1		/* parsing command # */
    379 #define	E_FUNC	2		/* executing function # */
    380 #define	E_INCL	3		/* including a file via . # */
    381 #define	E_EXEC	4		/* executing command tree */
    382 #define	E_LOOP	5		/* executing for/while # */
    383 #define	E_ERRH	6		/* general error handler # */
    384 /* # indicates env has valid jbuf (see unwind()) */
    385 
    386 /* struct env.flag values */
    387 #define EF_FUNC_PARSE	BIT(0)	/* function being parsed */
    388 #define EF_BRKCONT_PASS	BIT(1)	/* set if E_LOOP must pass break/continue on */
    389 #define EF_FAKE_SIGDIE	BIT(2)	/* hack to get info from unwind to quitenv */
    390 
    391 /* Do breaks/continues stop at env type e? */
    392 #define STOP_BRKCONT(t)	((t) == E_NONE || (t) == E_PARSE \
    393 			 || (t) == E_FUNC || (t) == E_INCL)
    394 /* Do returns stop at env type e? */
    395 #define STOP_RETURN(t)	((t) == E_FUNC || (t) == E_INCL)
    396 
    397 /* values for ksh_siglongjmp(e->jbuf, 0) */
    398 #define LRETURN	1		/* return statement */
    399 #define	LEXIT	2		/* exit statement */
    400 #define LERROR	3		/* errorf() called */
    401 #define LLEAVE	4		/* untrappable exit/error */
    402 #define LINTR	5		/* ^C noticed */
    403 #define	LBREAK	6		/* break statement */
    404 #define	LCONTIN	7		/* continue statement */
    405 #define LSHELL	8		/* return to interactive shell() */
    406 #define LAEXPR	9		/* error in arithmetic expression */
    407 
    408 /* option processing */
    409 #define OF_CMDLINE	0x01	/* command line */
    410 #define OF_SET		0x02	/* set builtin */
    411 #define OF_SPECIAL	0x04	/* a special variable changing */
    412 #define OF_INTERNAL	0x08	/* set internally by shell */
    413 #define OF_ANY		(OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
    414 
    415 struct option {
    416     const char	*name;	/* long name of option */
    417     char	c;	/* character flag (if any) */
    418     short	flags;	/* OF_* */
    419 };
    420 extern const struct option goptions[];
    421 
    422 /*
    423  * flags (the order of these enums MUST match the order in misc.c(options[]))
    424  */
    425 enum sh_flag {
    426 	FEXPORT = 0,	/* -a: export all */
    427 #ifdef BRACE_EXPAND
    428 	FBRACEEXPAND,	/* enable {} globbing */
    429 #endif
    430 	FBGNICE,	/* bgnice */
    431 	FCOMMAND,	/* -c: (invocation) execute specified command */
    432 #ifdef EMACS
    433 	FEMACS,		/* emacs command editing */
    434 	FEMACSUSEMETA,	/* use 8th bit as meta */
    435 #endif
    436 	FERREXIT,	/* -e: quit on error */
    437 #ifdef EMACS
    438 	FGMACS,		/* gmacs command editing */
    439 #endif
    440 	FIGNOREEOF,	/* eof does not exit */
    441 	FTALKING,	/* -i: interactive */
    442 	FKEYWORD,	/* -k: name=value anywhere */
    443 	FLOGIN,		/* -l: a login shell */
    444 	FMARKDIRS,	/* mark dirs with / in file name completion */
    445 	FMONITOR,	/* -m: job control monitoring */
    446 	FNOCLOBBER,	/* -C: don't overwrite existing files */
    447 	FNOEXEC,	/* -n: don't execute any commands */
    448 	FNOGLOB,	/* -f: don't do file globbing */
    449 	FNOHUP,		/* -H: don't kill running jobs when login shell exits */
    450 	FNOLOG,		/* don't save functions in history (ignored) */
    451 #ifdef	JOBS
    452 	FNOTIFY,	/* -b: asynchronous job completion notification */
    453 #endif
    454 	FNOUNSET,	/* -u: using an unset var is an error */
    455 	FPHYSICAL,	/* -o physical: don't do logical cd's/pwd's */
    456 	FPOSIX,		/* -o posix: be posixly correct */
    457 	FPRIVILEGED,	/* -p: use suid_profile */
    458 	FRESTRICTED,	/* -r: restricted shell */
    459 	FSTDIN,		/* -s: (invocation) parse stdin */
    460 	FTRACKALL,	/* -h: create tracked aliases for all commands */
    461 	FVERBOSE,	/* -v: echo input */
    462 #ifdef VI
    463 	FVI,		/* vi command editing */
    464 	FVIRAW,		/* always read in raw mode (ignored) */
    465 	FVISHOW8,	/* display chars with 8th bit set as is (versus M-) */
    466 	FVITABCOMPLETE,	/* enable tab as file name completion char */
    467 	FVIESCCOMPLETE,	/* enable ESC as file name completion in command mode */
    468 #endif
    469 	FXTRACE,	/* -x: execution trace */
    470 	FTALKING_I,	/* (internal): initial shell was interactive */
    471 	FNFLAGS /* (place holder: how many flags are there) */
    472 };
    473 
    474 #define Flag(f)	(shell_flags[(int) (f)])
    475 
    476 EXTERN	char shell_flags [FNFLAGS];
    477 
    478 EXTERN	char	null [] I__("");	/* null value for variable */
    479 EXTERN	char	space [] I__(" ");
    480 EXTERN	char	newline [] I__("\n");
    481 EXTERN	char	slash [] I__("/");
    482 
    483 enum temp_type {
    484     TT_HEREDOC_EXP,	/* expanded heredoc */
    485     TT_HIST_EDIT	/* temp file used for history editing (fc -e) */
    486 };
    487 typedef enum temp_type Temp_type;
    488 /* temp/heredoc files.  The file is removed when the struct is freed. */
    489 struct temp {
    490 	struct temp	*next;
    491 	struct shf	*shf;
    492 	int		pid;		/* pid of process parsed here-doc */
    493 	Temp_type	type;
    494 	char		*name;
    495 };
    496 
    497 /*
    498  * stdio and our IO routines
    499  */
    500 
    501 #define shl_spare	(&shf_iob[0])	/* for c_read()/c_print() */
    502 #define shl_stdout	(&shf_iob[1])
    503 #define shl_out		(&shf_iob[2])
    504 EXTERN int shl_stdout_ok;
    505 
    506 /*
    507  * trap handlers
    508  */
    509 typedef struct trap {
    510 	int	signal;		/* signal number */
    511 	const char *name;	/* short name */
    512 	const char *mess;	/* descriptive name */
    513 	char   *trap;		/* trap command */
    514 	int	volatile set;	/* trap pending */
    515 	int	flags;		/* TF_* */
    516 	handler_t cursig;	/* current handler (valid if TF_ORIG_* set) */
    517 	handler_t shtrap;	/* shell signal handler */
    518 } Trap;
    519 
    520 /* values for Trap.flags */
    521 #define TF_SHELL_USES	BIT(0)	/* shell uses signal, user can't change */
    522 #define TF_USER_SET	BIT(1)	/* user has (tried to) set trap */
    523 #define TF_ORIG_IGN	BIT(2)	/* original action was SIG_IGN */
    524 #define TF_ORIG_DFL	BIT(3)	/* original action was SIG_DFL */
    525 #define TF_EXEC_IGN	BIT(4)	/* restore SIG_IGN just before exec */
    526 #define TF_EXEC_DFL	BIT(5)	/* restore SIG_DFL just before exec */
    527 #define TF_DFL_INTR	BIT(6)	/* when received, default action is LINTR */
    528 #define TF_TTY_INTR	BIT(7)	/* tty generated signal (see j_waitj) */
    529 #define TF_CHANGED	BIT(8)	/* used by runtrap() to detect trap changes */
    530 #define TF_FATAL	BIT(9)	/* causes termination if not trapped */
    531 
    532 /* values for setsig()/setexecsig() flags argument */
    533 #define SS_RESTORE_MASK	0x3	/* how to restore a signal before an exec() */
    534 #define SS_RESTORE_CURR	0	/* leave current handler in place */
    535 #define SS_RESTORE_ORIG	1	/* restore original handler */
    536 #define SS_RESTORE_DFL	2	/* restore to SIG_DFL */
    537 #define SS_RESTORE_IGN	3	/* restore to SIG_IGN */
    538 #define SS_FORCE	BIT(3)	/* set signal even if original signal ignored */
    539 #define SS_USER		BIT(4)	/* user is doing the set (ie, trap command) */
    540 #define SS_SHTRAP	BIT(5)	/* trap for internal use (CHLD,ALRM,WINCH) */
    541 
    542 #define SIGEXIT_	0	/* for trap EXIT */
    543 #define SIGERR_		SIGNALS	/* for trap ERR */
    544 
    545 EXTERN	int volatile trap;	/* traps pending? */
    546 EXTERN	int volatile intrsig;	/* pending trap interrupts executing command */
    547 EXTERN	int volatile fatal_trap;/* received a fatal signal */
    548 #ifndef FROM_TRAP_C
    549 /* Kludge to avoid bogus re-declaration of sigtraps[] error on AIX 3.2.5 */
    550 extern	Trap	sigtraps[SIGNALS+1];
    551 #endif /* !FROM_TRAP_C */
    552 
    553 #ifdef KSH
    554 /*
    555  * TMOUT support
    556  */
    557 /* values for ksh_tmout_state */
    558 enum tmout_enum {
    559 		TMOUT_EXECUTING	= 0,	/* executing commands */
    560 		TMOUT_READING,		/* waiting for input */
    561 		TMOUT_LEAVING		/* have timed out */
    562 	};
    563 EXTERN unsigned int ksh_tmout;
    564 EXTERN enum tmout_enum ksh_tmout_state I__(TMOUT_EXECUTING);
    565 #endif /* KSH */
    566 
    567 /* For "You have stopped jobs" message */
    568 EXTERN int really_exit;
    569 
    570 /*
    571  * fast character classes
    572  */
    573 #define	C_ALPHA	 BIT(0)		/* a-z_A-Z */
    574 #define	C_DIGIT	 BIT(1)		/* 0-9 */
    575 #define	C_LEX1	 BIT(2)		/* \0 \t\n|&;<>() */
    576 #define	C_VAR1	 BIT(3)		/* *@#!$-? */
    577 #define	C_IFSWS	 BIT(4)		/* \t \n (IFS white space) */
    578 #define	C_SUBOP1 BIT(5)		/* "=-+?" */
    579 #define	C_SUBOP2 BIT(6)		/* "#%" */
    580 #define	C_IFS	 BIT(7)		/* $IFS */
    581 #define	C_QUOTE	 BIT(8)		/*  \n\t"#$&'()*;<>?[\`| (needing quoting) */
    582 
    583 extern	short ctypes [];
    584 
    585 #define	ctype(c, t)	!!(ctypes[(unsigned char)(c)]&(t))
    586 #define	letter(c)	ctype(c, C_ALPHA)
    587 #define	digit(c)	ctype(c, C_DIGIT)
    588 #define	letnum(c)	ctype(c, C_ALPHA|C_DIGIT)
    589 
    590 EXTERN int ifs0 I__(' ');	/* for "$*" */
    591 
    592 /* Argument parsing for built-in commands and getopts command */
    593 
    594 /* Values for Getopt.flags */
    595 #define GF_ERROR	BIT(0)	/* call errorf() if there is an error */
    596 #define GF_PLUSOPT	BIT(1)	/* allow +c as an option */
    597 #define GF_NONAME	BIT(2)	/* don't print argv[0] in errors */
    598 
    599 /* Values for Getopt.info */
    600 #define GI_MINUS	BIT(0)	/* an option started with -... */
    601 #define GI_PLUS		BIT(1)	/* an option started with +... */
    602 #define GI_MINUSMINUS	BIT(2)	/* arguments were ended with -- */
    603 
    604 typedef struct {
    605 	int		optind;
    606 	int		uoptind;/* what user sees in $OPTIND */
    607 	char		*optarg;
    608 	int		flags;	/* see GF_* */
    609 	int		info;	/* see GI_* */
    610 	unsigned int	p;	/* 0 or index into argv[optind - 1] */
    611 	char		buf[2];	/* for bad option OPTARG value */
    612 } Getopt;
    613 
    614 EXTERN Getopt builtin_opt;	/* for shell builtin commands */
    615 EXTERN Getopt user_opt;		/* parsing state for getopts builtin command */
    616 
    617 #ifdef KSH
    618 /* This for co-processes */
    619 
    620 typedef INT32 Coproc_id; /* something that won't (realisticly) wrap */
    621 struct coproc {
    622 	int	read;		/* pipe from co-process's stdout */
    623 	int	readw;		/* other side of read (saved temporarily) */
    624 	int	write;		/* pipe to co-process's stdin */
    625 	Coproc_id id;		/* id of current output pipe */
    626 	int	njobs;		/* number of live jobs using output pipe */
    627 	void    *job;           /* 0 or job of co-process using input pipe */
    628 };
    629 EXTERN struct coproc coproc;
    630 #endif /* KSH */
    631 
    632 /* Used in jobs.c and by coprocess stuff in exec.c */
    633 #ifdef JOB_SIGS
    634 EXTERN sigset_t		sm_default, sm_sigchld;
    635 #endif /* JOB_SIGS */
    636 
    637 extern char ksh_version[];
    638 
    639 /* name of called builtin function (used by error functions) */
    640 EXTERN char	*builtin_argv0;
    641 EXTERN Tflag	builtin_flag;	/* flags of called builtin (SPEC_BI, etc.) */
    642 
    643 /* current working directory, and size of memory allocated for same */
    644 EXTERN char	*current_wd;
    645 EXTERN int	current_wd_size;
    646 
    647 #ifdef EDIT
    648 /* Minimum required space to work with on a line - if the prompt leaves less
    649  * space than this on a line, the prompt is truncated.
    650  */
    651 # define MIN_EDIT_SPACE	7
    652 /* Minimum allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
    653  */
    654 # define MIN_COLS	(2 + MIN_EDIT_SPACE + 3)
    655 EXTERN	int	x_cols I__(80);	/* tty columns */
    656 #else
    657 # define x_cols 80		/* for pr_menu(exec.c) */
    658 #endif
    659 
    660 /* These to avoid bracket matching problems */
    661 #define OPAREN	'('
    662 #define CPAREN	')'
    663 #define OBRACK	'['
    664 #define CBRACK	']'
    665 #define OBRACE	'{'
    666 #define CBRACE	'}'
    667 
    668 /* Determine the location of the system (common) profile */
    669 #ifndef KSH_SYSTEM_PROFILE
    670 # ifdef __NeXT
    671 #  define KSH_SYSTEM_PROFILE "/etc/profile.std"
    672 # else /* __NeXT */
    673 #  define KSH_SYSTEM_PROFILE "/etc/profile"
    674 # endif /* __NeXT */
    675 #endif /* KSH_SYSTEM_PROFILE */
    676 
    677 /* Used by v_evaluate() and setstr() to control action when error occurs */
    678 #define KSH_UNWIND_ERROR	0	/* unwind the stack (longjmp) */
    679 #define KSH_RETURN_ERROR	1	/* return 1/0 for success/failure */
    680 
    681 #include "shf.h"
    682 #include "table.h"
    683 #include "tree.h"
    684 #include "expand.h"
    685 #include "lex.h"
    686 #include "proto.h"
    687 
    688 /* be sure not to interfere with anyone else's idea about EXTERN */
    689 #ifdef EXTERN_DEFINED
    690 # undef EXTERN_DEFINED
    691 # undef EXTERN
    692 #endif
    693 #undef I__
    694