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