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