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