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