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