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