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