sync
[bitrig.git] / bin / ksh / sh.h
blob17462049618780a4353fa1d0cc63ad76731db087
1 /* $OpenBSD: sh.h,v 1.31 2012/09/10 01:25:30 tedu Exp $ */
3 /*
4 * Public Domain Bourne/Korn shell
5 */
7 /* $From: sh.h,v 1.2 1994/05/19 18:32:40 michael Exp michael $ */
9 #include "config.h" /* system and option configuration info */
11 /* Start of common headers */
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <setjmp.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdarg.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
27 #include <signal.h>
29 #include <paths.h>
31 /* Find a integer type that is at least 32 bits (or die) - SIZEOF_* defined
32 * by autoconf (assumes an 8 bit byte, but I'm not concerned).
33 * NOTE: INT32 may end up being more than 32 bits.
35 # define INT32 int
37 /* end of common headers */
39 /* some useful #defines */
40 #ifdef EXTERN
41 # define I__(i) = i
42 #else
43 # define I__(i)
44 # define EXTERN extern
45 # define EXTERN_DEFINED
46 #endif
48 #define EXECSHELL _PATH_BSHELL
49 #define EXECSHELL_STR "EXECSHELL"
51 #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
52 #define sizeofN(type, n) (sizeof(type) * (n))
53 #define BIT(i) (1<<(i)) /* define bit in flag */
55 /* Table flag type - needs > 16 and < 32 bits */
56 typedef INT32 Tflag;
58 #define NUFILE 32 /* Number of user-accessible files */
59 #define FDBASE 10 /* First file usable by Shell */
61 /* Make MAGIC a char that might be printed to make bugs more obvious, but
62 * not a char that is used often. Also, can't use the high bit as it causes
63 * portability problems (calling strchr(x, 0x80|'x') is error prone).
65 #define MAGIC (7) /* prefix for *?[!{,} during expand */
66 #define ISMAGIC(c) ((unsigned char)(c) == MAGIC)
67 #define NOT '!' /* might use ^ (ie, [!...] vs [^..]) */
69 #define LINE 2048 /* input line size */
70 #define PATH 1024 /* pathname size (todo: PATH_MAX/pathconf()) */
71 #define ARRAYMAX (10*1024-1) /* max array index */
73 EXTERN const char *kshname; /* $0 */
74 EXTERN pid_t kshpid; /* $$, shell pid */
75 EXTERN pid_t procpid; /* pid of executing process */
76 EXTERN uid_t ksheuid; /* effective uid of shell */
77 EXTERN int exstat; /* exit status */
78 EXTERN int subst_exstat; /* exit status of last $(..)/`..` */
79 EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */
80 EXTERN char username[]; /* username for \u prompt expansion */
83 * Area-based allocation built on malloc/free
85 typedef struct Area {
86 struct link *freelist; /* free list */
87 } Area;
89 EXTERN Area aperm; /* permanent object space */
90 #define APERM &aperm
91 #define ATEMP &e->area
93 #ifdef KSH_DEBUG
94 # define kshdebug_init() kshdebug_init_()
95 # define kshdebug_printf(a) kshdebug_printf_ a
96 # define kshdebug_dump(a) kshdebug_dump_ a
97 #else /* KSH_DEBUG */
98 # define kshdebug_init()
99 # define kshdebug_printf(a)
100 # define kshdebug_dump(a)
101 #endif /* KSH_DEBUG */
104 * parsing & execution environment
106 EXTERN struct env {
107 short type; /* environment type - see below */
108 short flags; /* EF_* */
109 Area area; /* temporary allocation area */
110 struct block *loc; /* local variables and functions */
111 short *savefd; /* original redirected fd's */
112 struct env *oenv; /* link to previous environment */
113 sigjmp_buf jbuf; /* long jump back to env creator */
114 struct temp *temps; /* temp files */
115 } *e;
117 /* struct env.type values */
118 #define E_NONE 0 /* dummy environment */
119 #define E_PARSE 1 /* parsing command # */
120 #define E_FUNC 2 /* executing function # */
121 #define E_INCL 3 /* including a file via . # */
122 #define E_EXEC 4 /* executing command tree */
123 #define E_LOOP 5 /* executing for/while # */
124 #define E_ERRH 6 /* general error handler # */
125 /* # indicates env has valid jbuf (see unwind()) */
127 /* struct env.flag values */
128 #define EF_FUNC_PARSE BIT(0) /* function being parsed */
129 #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
130 #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
132 /* Do breaks/continues stop at env type e? */
133 #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE \
134 || (t) == E_FUNC || (t) == E_INCL)
135 /* Do returns stop at env type e? */
136 #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
138 /* values for siglongjmp(e->jbuf, 0) */
139 #define LRETURN 1 /* return statement */
140 #define LEXIT 2 /* exit statement */
141 #define LERROR 3 /* errorf() called */
142 #define LLEAVE 4 /* untrappable exit/error */
143 #define LINTR 5 /* ^C noticed */
144 #define LBREAK 6 /* break statement */
145 #define LCONTIN 7 /* continue statement */
146 #define LSHELL 8 /* return to interactive shell() */
147 #define LAEXPR 9 /* error in arithmetic expression */
149 /* option processing */
150 #define OF_CMDLINE 0x01 /* command line */
151 #define OF_SET 0x02 /* set builtin */
152 #define OF_SPECIAL 0x04 /* a special variable changing */
153 #define OF_INTERNAL 0x08 /* set internally by shell */
154 #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
156 struct option {
157 const char *name; /* long name of option */
158 char c; /* character flag (if any) */
159 short flags; /* OF_* */
161 extern const struct option options[];
164 * flags (the order of these enums MUST match the order in misc.c(options[]))
166 enum sh_flag {
167 FEXPORT = 0, /* -a: export all */
168 #ifdef BRACE_EXPAND
169 FBRACEEXPAND, /* enable {} globbing */
170 #endif
171 FBGNICE, /* bgnice */
172 FCOMMAND, /* -c: (invocation) execute specified command */
173 FCSHHISTORY, /* csh-style history enabled */
174 #ifdef EMACS
175 FEMACS, /* emacs command editing */
176 FEMACSUSEMETA, /* use 8th bit as meta */
177 #endif
178 FERREXIT, /* -e: quit on error */
179 #ifdef EMACS
180 FGMACS, /* gmacs command editing */
181 #endif
182 FIGNOREEOF, /* eof does not exit */
183 FTALKING, /* -i: interactive */
184 FKEYWORD, /* -k: name=value anywhere */
185 FLOGIN, /* -l: a login shell */
186 FMARKDIRS, /* mark dirs with / in file name completion */
187 FMONITOR, /* -m: job control monitoring */
188 FNOCLOBBER, /* -C: don't overwrite existing files */
189 FNOEXEC, /* -n: don't execute any commands */
190 FNOGLOB, /* -f: don't do file globbing */
191 FNOHUP, /* -H: don't kill running jobs when login shell exits */
192 FNOLOG, /* don't save functions in history (ignored) */
193 #ifdef JOBS
194 FNOTIFY, /* -b: asynchronous job completion notification */
195 #endif
196 FNOUNSET, /* -u: using an unset var is an error */
197 FPHYSICAL, /* -o physical: don't do logical cd's/pwd's */
198 FPOSIX, /* -o posix: be posixly correct */
199 FPRIVILEGED, /* -p: use suid_profile */
200 FRESTRICTED, /* -r: restricted shell */
201 FSH, /* -o sh: favor sh behaviour */
202 FSTDIN, /* -s: (invocation) parse stdin */
203 FTRACKALL, /* -h: create tracked aliases for all commands */
204 FVERBOSE, /* -v: echo input */
205 #ifdef VI
206 FVI, /* vi command editing */
207 FVIRAW, /* always read in raw mode (ignored) */
208 FVISHOW8, /* display chars with 8th bit set as is (versus M-) */
209 FVITABCOMPLETE, /* enable tab as file name completion char */
210 FVIESCCOMPLETE, /* enable ESC as file name completion in command mode */
211 #endif
212 FXTRACE, /* -x: execution trace */
213 FTALKING_I, /* (internal): initial shell was interactive */
214 FNFLAGS /* (place holder: how many flags are there) */
217 #define Flag(f) (shell_flags[(int) (f)])
219 EXTERN char shell_flags [FNFLAGS];
221 EXTERN char null [] I__(""); /* null value for variable */
222 EXTERN char space [] I__(" ");
223 EXTERN char newline [] I__("\n");
225 enum temp_type {
226 TT_HEREDOC_EXP, /* expanded heredoc */
227 TT_HIST_EDIT /* temp file used for history editing (fc -e) */
229 typedef enum temp_type Temp_type;
230 /* temp/heredoc files. The file is removed when the struct is freed. */
231 struct temp {
232 struct temp *next;
233 struct shf *shf;
234 int pid; /* pid of process parsed here-doc */
235 Temp_type type;
236 char *name;
240 * stdio and our IO routines
243 #define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */
244 #define shl_stdout (&shf_iob[1])
245 #define shl_out (&shf_iob[2])
246 EXTERN int shl_stdout_ok;
249 * trap handlers
251 typedef struct trap {
252 int signal; /* signal number */
253 const char *name; /* short name */
254 const char *mess; /* descriptive name */
255 char *trap; /* trap command */
256 volatile sig_atomic_t set; /* trap pending */
257 int flags; /* TF_* */
258 sig_t cursig; /* current handler (valid if TF_ORIG_* set) */
259 sig_t shtrap; /* shell signal handler */
260 } Trap;
262 /* values for Trap.flags */
263 #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
264 #define TF_USER_SET BIT(1) /* user has (tried to) set trap */
265 #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
266 #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
267 #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
268 #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
269 #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
270 #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
271 #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
272 #define TF_FATAL BIT(9) /* causes termination if not trapped */
274 /* values for setsig()/setexecsig() flags argument */
275 #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
276 #define SS_RESTORE_CURR 0 /* leave current handler in place */
277 #define SS_RESTORE_ORIG 1 /* restore original handler */
278 #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
279 #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
280 #define SS_FORCE BIT(3) /* set signal even if original signal ignored */
281 #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
282 #define SS_SHTRAP BIT(5) /* trap for internal use (CHLD,ALRM,WINCH) */
284 #define SIGEXIT_ 0 /* for trap EXIT */
285 #define SIGERR_ NSIG /* for trap ERR */
287 EXTERN volatile sig_atomic_t trap; /* traps pending? */
288 EXTERN volatile sig_atomic_t intrsig; /* pending trap interrupts command */
289 EXTERN volatile sig_atomic_t fatal_trap;/* received a fatal signal */
290 extern volatile sig_atomic_t got_sigwinch;
291 extern Trap sigtraps[NSIG+1];
294 * TMOUT support
296 /* values for ksh_tmout_state */
297 enum tmout_enum {
298 TMOUT_EXECUTING = 0, /* executing commands */
299 TMOUT_READING, /* waiting for input */
300 TMOUT_LEAVING /* have timed out */
302 EXTERN unsigned int ksh_tmout;
303 EXTERN enum tmout_enum ksh_tmout_state I__(TMOUT_EXECUTING);
305 /* For "You have stopped jobs" message */
306 EXTERN int really_exit;
309 * fast character classes
311 #define C_ALPHA BIT(0) /* a-z_A-Z */
312 #define C_DIGIT BIT(1) /* 0-9 */
313 #define C_LEX1 BIT(2) /* \0 \t\n|&;<>() */
314 #define C_VAR1 BIT(3) /* *@#!$-? */
315 #define C_IFSWS BIT(4) /* \t \n (IFS white space) */
316 #define C_SUBOP1 BIT(5) /* "=-+?" */
317 #define C_SUBOP2 BIT(6) /* "#%" */
318 #define C_IFS BIT(7) /* $IFS */
319 #define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */
321 extern short ctypes [];
323 #define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t))
324 #define letter(c) ctype(c, C_ALPHA)
325 #define digit(c) ctype(c, C_DIGIT)
326 #define letnum(c) ctype(c, C_ALPHA|C_DIGIT)
328 EXTERN int ifs0 I__(' '); /* for "$*" */
330 /* Argument parsing for built-in commands and getopts command */
332 /* Values for Getopt.flags */
333 #define GF_ERROR BIT(0) /* call errorf() if there is an error */
334 #define GF_PLUSOPT BIT(1) /* allow +c as an option */
335 #define GF_NONAME BIT(2) /* don't print argv[0] in errors */
337 /* Values for Getopt.info */
338 #define GI_MINUS BIT(0) /* an option started with -... */
339 #define GI_PLUS BIT(1) /* an option started with +... */
340 #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
342 typedef struct {
343 int optind;
344 int uoptind;/* what user sees in $OPTIND */
345 char *optarg;
346 int flags; /* see GF_* */
347 int info; /* see GI_* */
348 unsigned int p; /* 0 or index into argv[optind - 1] */
349 char buf[2]; /* for bad option OPTARG value */
350 } Getopt;
352 EXTERN Getopt builtin_opt; /* for shell builtin commands */
353 EXTERN Getopt user_opt; /* parsing state for getopts builtin command */
355 /* This for co-processes */
357 typedef INT32 Coproc_id; /* something that won't (realisticly) wrap */
358 struct coproc {
359 int read; /* pipe from co-process's stdout */
360 int readw; /* other side of read (saved temporarily) */
361 int write; /* pipe to co-process's stdin */
362 Coproc_id id; /* id of current output pipe */
363 int njobs; /* number of live jobs using output pipe */
364 void *job; /* 0 or job of co-process using input pipe */
366 EXTERN struct coproc coproc;
368 /* Used in jobs.c and by coprocess stuff in exec.c */
369 EXTERN sigset_t sm_default, sm_sigchld;
371 extern const char ksh_version[];
373 /* name of called builtin function (used by error functions) */
374 EXTERN char *builtin_argv0;
375 EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */
377 /* current working directory, and size of memory allocated for same */
378 EXTERN char *current_wd;
379 EXTERN int current_wd_size;
381 #ifdef EDIT
382 /* Minimum required space to work with on a line - if the prompt leaves less
383 * space than this on a line, the prompt is truncated.
385 # define MIN_EDIT_SPACE 7
386 /* Minimum allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
388 # define MIN_COLS (2 + MIN_EDIT_SPACE + 3)
389 EXTERN int x_cols I__(80); /* tty columns */
390 #else
391 # define x_cols 80 /* for pr_menu(exec.c) */
392 #endif
394 /* These to avoid bracket matching problems */
395 #define OPAREN '('
396 #define CPAREN ')'
397 #define OBRACK '['
398 #define CBRACK ']'
399 #define OBRACE '{'
400 #define CBRACE '}'
402 /* Determine the location of the system (common) profile */
403 #define KSH_SYSTEM_PROFILE "/etc/profile"
405 /* Used by v_evaluate() and setstr() to control action when error occurs */
406 #define KSH_UNWIND_ERROR 0 /* unwind the stack (longjmp) */
407 #define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */
409 #include "shf.h"
410 #include "table.h"
411 #include "tree.h"
412 #include "expand.h"
413 #include "lex.h"
414 #include "proto.h"
416 /* be sure not to interfere with anyone else's idea about EXTERN */
417 #ifdef EXTERN_DEFINED
418 # undef EXTERN_DEFINED
419 # undef EXTERN
420 #endif
421 #undef I__