don't bother resolving onbld python module deps
[unleashed.git] / bin / ksh / sh.h
blobcf2c6c3a59598587b649b16e33d668a4ab19b9a5
1 /* $OpenBSD: sh.h,v 1.73 2018/05/18 13:25:20 benno 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 <limits.h>
14 #include <setjmp.h>
15 #include <stdarg.h>
16 #include <stddef.h>
17 #include <signal.h>
18 #include <stdbool.h>
20 /* end of common headers */
22 #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
23 #define BIT(i) (1<<(i)) /* define bit in flag */
25 #define NUFILE 32 /* Number of user-accessible files */
26 #define FDBASE 10 /* First file usable by Shell */
28 #define BITS(t) (CHAR_BIT * sizeof(t))
30 /* Make MAGIC a char that might be printed to make bugs more obvious, but
31 * not a char that is used often. Also, can't use the high bit as it causes
32 * portability problems (calling strchr(x, 0x80|'x') is error prone).
34 #define MAGIC (7) /* prefix for *?[!{,} during expand */
35 #define ISMAGIC(c) ((unsigned char)(c) == MAGIC)
37 #define LINE 4096 /* input line size */
39 extern const char *kshname; /* $0 */
40 extern pid_t kshpid; /* $$, shell pid */
41 extern pid_t procpid; /* pid of executing process */
42 extern uid_t ksheuid; /* effective uid of shell */
43 extern int exstat; /* exit status */
44 extern int subst_exstat; /* exit status of last $(..)/`..` */
45 extern const char *safe_prompt; /* safe prompt if PS1 substitution fails */
46 extern char username[]; /* username for \u prompt expansion */
49 * Area-based allocation built on malloc/free
51 typedef struct Area {
52 struct link *freelist; /* free list */
53 } Area;
55 extern Area aperm; /* permanent object space */
56 #define APERM &aperm
57 #define ATEMP &genv->area
59 #ifdef KSH_DEBUG
60 # define kshdebug_init() kshdebug_init_()
61 # define kshdebug_printf(a) kshdebug_printf_ a
62 # define kshdebug_dump(a) kshdebug_dump_ a
63 #else /* KSH_DEBUG */
64 # define kshdebug_init()
65 # define kshdebug_printf(a)
66 # define kshdebug_dump(a)
67 #endif /* KSH_DEBUG */
70 * parsing & execution environment
72 struct env {
73 short type; /* environment type - see below */
74 short flags; /* EF_* */
75 Area area; /* temporary allocation area */
76 struct block *loc; /* local variables and functions */
77 short *savefd; /* original redirected fd's */
78 struct env *oenv; /* link to previous environment */
79 sigjmp_buf jbuf; /* long jump back to env creator */
80 struct temp *temps; /* temp files */
82 extern struct env *genv;
84 /* struct env.type values */
85 #define E_NONE 0 /* dummy environment */
86 #define E_PARSE 1 /* parsing command # */
87 #define E_FUNC 2 /* executing function # */
88 #define E_INCL 3 /* including a file via . # */
89 #define E_EXEC 4 /* executing command tree */
90 #define E_LOOP 5 /* executing for/while # */
91 #define E_ERRH 6 /* general error handler # */
92 /* # indicates env has valid jbuf (see unwind()) */
94 /* struct env.flag values */
95 #define EF_FUNC_PARSE BIT(0) /* function being parsed */
96 #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
97 #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
99 /* Do breaks/continues stop at env type e? */
100 #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE \
101 || (t) == E_FUNC || (t) == E_INCL)
102 /* Do returns stop at env type e? */
103 #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
105 /* values for siglongjmp(e->jbuf, 0) */
106 #define LRETURN 1 /* return statement */
107 #define LEXIT 2 /* exit statement */
108 #define LERROR 3 /* errorf() called */
109 #define LLEAVE 4 /* untrappable exit/error */
110 #define LINTR 5 /* ^C noticed */
111 #define LBREAK 6 /* break statement */
112 #define LCONTIN 7 /* continue statement */
113 #define LSHELL 8 /* return to interactive shell() */
114 #define LAEXPR 9 /* error in arithmetic expression */
116 /* option processing */
117 #define OF_CMDLINE 0x01 /* command line */
118 #define OF_SET 0x02 /* set builtin */
119 #define OF_SPECIAL 0x04 /* a special variable changing */
120 #define OF_INTERNAL 0x08 /* set internally by shell */
121 #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
123 struct option {
124 const char *name; /* long name of option */
125 char c; /* character flag (if any) */
126 short flags; /* OF_* */
128 extern const struct option sh_options[];
131 * flags (the order of these enums MUST match the order in misc.c(options[]))
133 enum sh_flag {
134 FEXPORT = 0, /* -a: export all */
135 FBRACEEXPAND, /* enable {} globbing */
136 FBGNICE, /* bgnice */
137 FCOMMAND, /* -c: (invocation) execute specified command */
138 FCSHHISTORY, /* csh-style history enabled */
139 #ifdef EMACS
140 FEMACS, /* emacs command editing */
141 #endif
142 FERREXIT, /* -e: quit on error */
143 #ifdef EMACS
144 FGMACS, /* gmacs command editing */
145 #endif
146 FIGNOREEOF, /* eof does not exit */
147 FTALKING, /* -i: interactive */
148 FKEYWORD, /* -k: name=value anywhere */
149 FLOGIN, /* -l: a login shell */
150 FMARKDIRS, /* mark dirs with / in file name completion */
151 FMONITOR, /* -m: job control monitoring */
152 FNOCLOBBER, /* -C: don't overwrite existing files */
153 FNOEXEC, /* -n: don't execute any commands */
154 FNOGLOB, /* -f: don't do file globbing */
155 FNOHUP, /* -H: don't kill running jobs when login shell exits */
156 FNOLOG, /* don't save functions in history (ignored) */
157 FNOTIFY, /* -b: asynchronous job completion notification */
158 FNOUNSET, /* -u: using an unset var is an error */
159 FPHYSICAL, /* -o physical: don't do logical cd's/pwd's */
160 FPOSIX, /* -o posix: be posixly correct */
161 FPRIVILEGED, /* -p: use suid_profile */
162 FRESTRICTED, /* -r: restricted shell */
163 FSH, /* -o sh: favor sh behaviour */
164 FSTDIN, /* -s: (invocation) parse stdin */
165 FTRACKALL, /* -h: create tracked aliases for all commands */
166 FVERBOSE, /* -v: echo input */
167 #ifdef VI
168 FVI, /* vi command editing */
169 FVIRAW, /* always read in raw mode (ignored) */
170 FVISHOW8, /* display chars with 8th bit set as is (versus M-) */
171 FVITABCOMPLETE, /* enable tab as file name completion char */
172 FVIESCCOMPLETE, /* enable ESC as file name completion in command mode */
173 #endif
174 FXTRACE, /* -x: execution trace */
175 FTALKING_I, /* (internal): initial shell was interactive */
176 FNFLAGS /* (place holder: how many flags are there) */
179 #define Flag(f) (shell_flags[(int) (f)])
181 extern char shell_flags[FNFLAGS];
183 extern char null[]; /* null value for variable */
185 enum temp_type {
186 TT_HEREDOC_EXP, /* expanded heredoc */
187 TT_HIST_EDIT /* temp file used for history editing (fc -e) */
189 typedef enum temp_type Temp_type;
190 /* temp/heredoc files. The file is removed when the struct is freed. */
191 struct temp {
192 struct temp *next;
193 struct shf *shf;
194 int pid; /* pid of process parsed here-doc */
195 Temp_type type;
196 char *name;
200 * stdio and our IO routines
203 #define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */
204 #define shl_stdout (&shf_iob[1])
205 #define shl_out (&shf_iob[2])
206 extern int shl_stdout_ok;
209 * trap handlers
211 typedef struct trap {
212 int signal; /* signal number */
213 const char *name; /* short name */
214 const char *mess; /* descriptive name */
215 char *trap; /* trap command */
216 volatile sig_atomic_t set; /* trap pending */
217 int flags; /* TF_* */
218 sig_t cursig; /* current handler (valid if TF_ORIG_* set) */
219 sig_t shtrap; /* shell signal handler */
220 } Trap;
222 /* values for Trap.flags */
223 #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
224 #define TF_USER_SET BIT(1) /* user has (tried to) set trap */
225 #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
226 #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
227 #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
228 #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
229 #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
230 #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
231 #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
232 #define TF_FATAL BIT(9) /* causes termination if not trapped */
234 /* values for setsig()/setexecsig() flags argument */
235 #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
236 #define SS_RESTORE_CURR 0 /* leave current handler in place */
237 #define SS_RESTORE_ORIG 1 /* restore original handler */
238 #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
239 #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
240 #define SS_FORCE BIT(3) /* set signal even if original signal ignored */
241 #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
242 #define SS_SHTRAP BIT(5) /* trap for internal use (CHLD,ALRM,WINCH) */
244 #define SIGEXIT_ 0 /* for trap EXIT */
245 #define SIGERR_ NSIG /* for trap ERR */
247 extern volatile sig_atomic_t trap; /* traps pending? */
248 extern volatile sig_atomic_t intrsig; /* pending trap interrupts command */
249 extern volatile sig_atomic_t fatal_trap; /* received a fatal signal */
250 extern volatile sig_atomic_t got_sigwinch;
251 extern Trap sigtraps[NSIG+1];
254 * TMOUT support
256 /* values for ksh_tmout_state */
257 enum tmout_enum {
258 TMOUT_EXECUTING = 0, /* executing commands */
259 TMOUT_READING, /* waiting for input */
260 TMOUT_LEAVING /* have timed out */
262 extern unsigned int ksh_tmout;
263 extern enum tmout_enum ksh_tmout_state;
265 /* For "You have stopped jobs" message */
266 extern int really_exit;
269 * fast character classes
271 #define C_ALPHA BIT(0) /* a-z_A-Z */
272 /* was C_DIGIT */
273 #define C_LEX1 BIT(2) /* \0 \t\n|&;<>() */
274 #define C_VAR1 BIT(3) /* *@#!$-? */
275 #define C_IFSWS BIT(4) /* \t \n (IFS white space) */
276 #define C_SUBOP1 BIT(5) /* "=-+?" */
277 #define C_SUBOP2 BIT(6) /* "#%" */
278 #define C_IFS BIT(7) /* $IFS */
279 #define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */
281 extern short ctypes [];
283 #define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t))
284 #define letter(c) ctype(c, C_ALPHA)
285 #define digit(c) isdigit((unsigned char)(c))
286 #define letnum(c) (ctype(c, C_ALPHA) || isdigit((unsigned char)(c)))
288 extern int ifs0; /* for "$*" */
290 /* Argument parsing for built-in commands and getopts command */
292 /* Values for Getopt.flags */
293 #define GF_ERROR BIT(0) /* call errorf() if there is an error */
294 #define GF_PLUSOPT BIT(1) /* allow +c as an option */
295 #define GF_NONAME BIT(2) /* don't print argv[0] in errors */
297 /* Values for Getopt.info */
298 #define GI_MINUS BIT(0) /* an option started with -... */
299 #define GI_PLUS BIT(1) /* an option started with +... */
300 #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
302 typedef struct {
303 int optind;
304 int uoptind;/* what user sees in $OPTIND */
305 char *optarg;
306 int flags; /* see GF_* */
307 int info; /* see GI_* */
308 unsigned int p; /* 0 or index into argv[optind - 1] */
309 char buf[2]; /* for bad option OPTARG value */
310 } Getopt;
312 extern Getopt builtin_opt; /* for shell builtin commands */
313 extern Getopt user_opt; /* parsing state for getopts builtin command */
315 /* This for co-processes */
317 typedef int Coproc_id; /* something that won't (realistically) wrap */
318 struct coproc {
319 int read; /* pipe from co-process's stdout */
320 int readw; /* other side of read (saved temporarily) */
321 int write; /* pipe to co-process's stdin */
322 Coproc_id id; /* id of current output pipe */
323 int njobs; /* number of live jobs using output pipe */
324 void *job; /* 0 or job of co-process using input pipe */
326 extern struct coproc coproc;
328 /* Used in jobs.c and by coprocess stuff in exec.c */
329 extern sigset_t sm_default, sm_sigchld;
331 extern const char ksh_version[];
333 /* name of called builtin function (used by error functions) */
334 extern char *builtin_argv0;
335 extern int builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */
337 /* current working directory, and size of memory allocated for same */
338 extern char *current_wd;
339 extern int current_wd_size;
341 /* Minimum required space to work with on a line - if the prompt leaves less
342 * space than this on a line, the prompt is truncated.
344 #define MIN_EDIT_SPACE 7
345 /* Minimum allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
347 #define MIN_COLS (2 + MIN_EDIT_SPACE + 3)
348 extern int x_cols; /* tty columns */
350 /* These to avoid bracket matching problems */
351 #define OPAREN '('
352 #define CPAREN ')'
353 #define OBRACK '['
354 #define CBRACK ']'
355 #define OBRACE '{'
356 #define CBRACE '}'
358 /* Determine the location of the system (common) profile */
359 #define KSH_SYSTEM_PROFILE "/etc/profile"
361 /* Used by v_evaluate() and setstr() to control action when error occurs */
362 #define KSH_UNWIND_ERROR 0x0 /* unwind the stack (longjmp) */
363 #define KSH_RETURN_ERROR 0x1 /* return 1/0 for success/failure */
364 #define KSH_IGNORE_RDONLY 0x4 /* ignore the read-only flag */
366 #include "shf.h"
367 #include "table.h"
368 #include "tree.h"
369 #include "expand.h"
370 #include "lex.h"
372 /* alloc.c */
373 Area * ainit(Area *);
374 void afreeall(Area *);
375 void * alloc(size_t, Area *);
376 void * areallocarray(void *, size_t, size_t, Area *);
377 void * aresize(void *, size_t, Area *);
378 void afree(void *, Area *);
379 /* c_ksh.c */
380 int c_cd(char **);
381 int c_pwd(char **);
382 int c_print(char **);
383 int c_whence(char **);
384 int c_command(char **);
385 int c_type(char **);
386 int c_typeset(char **);
387 int c_alias(char **);
388 int c_unalias(char **);
389 int c_let(char **);
390 int c_jobs(char **);
391 int c_fgbg(char **);
392 int c_kill(char **);
393 void getopts_reset(int);
394 int c_getopts(char **);
395 int c_bind(char **);
396 /* c_sh.c */
397 int c_label(char **);
398 int c_shift(char **);
399 int c_umask(char **);
400 int c_dot(char **);
401 int c_wait(char **);
402 int c_read(char **);
403 int c_eval(char **);
404 int c_trap(char **);
405 int c_brkcont(char **);
406 int c_exitreturn(char **);
407 int c_set(char **);
408 int c_unset(char **);
409 int c_ulimit(char **);
410 int c_times(char **);
411 int timex(struct op *, int, volatile int *);
412 void timex_hook(struct op *, char ** volatile *);
413 int c_exec(char **);
414 int c_builtin(char **);
415 /* c_test.c */
416 int c_test(char **);
417 /* edit.c: most prototypes in edit.h */
418 void x_init(void);
419 int x_read(char *, size_t);
420 void set_editmode(const char *);
421 /* emacs.c: most prototypes in edit.h */
422 int x_bind(const char *, const char *, int, int);
423 /* eval.c */
424 char * substitute(const char *, int);
425 char ** eval(char **, int);
426 char * evalstr(char *cp, int);
427 char * evalonestr(char *cp, int);
428 char *debunk(char *, const char *, size_t);
429 void expand(char *, XPtrV *, int);
430 int glob_str(char *, XPtrV *, int);
431 /* exec.c */
432 int execute(struct op * volatile, volatile int, volatile int *);
433 int shcomexec(char **);
434 struct tbl * findfunc(const char *, unsigned int, int);
435 int define(const char *, struct op *);
436 void builtin(const char *, int (*)(char **));
437 struct tbl * findcom(const char *, int);
438 void flushcom(int);
439 char * search(const char *, const char *, int, int *);
440 int search_access(const char *, int, int *);
441 int pr_menu(char *const *);
442 int pr_list(char *const *);
443 /* expr.c */
444 int evaluate(const char *, int64_t *, int, bool);
445 int v_evaluate(struct tbl *, const char *, volatile int, bool);
446 /* history.c */
447 void init_histvec(void);
448 void hist_init(Source *);
449 void hist_finish(void);
450 void histsave(int, const char *, int);
451 int c_fc(char **);
452 void sethistcontrol(const char *);
453 void sethistsize(int);
454 void sethistfile(const char *);
455 char ** histpos(void);
456 int histnum(int);
457 int findhist(int, int, const char *, int);
458 int findhistrel(const char *);
459 char **hist_get_newest(int);
461 /* io.c */
462 void errorf(const char *, ...)
463 __attribute__((__noreturn__, __format__ (printf, 1, 2)));
464 void warningf(bool, const char *, ...)
465 __attribute__((__format__ (printf, 2, 3)));
466 void bi_errorf(const char *, ...)
467 __attribute__((__format__ (printf, 1, 2)));
468 void internal_errorf(const char *, ...)
469 __attribute__((__noreturn__, __format__ (printf, 1, 2)));
470 void internal_warningf(const char *, ...)
471 __attribute__((__format__ (printf, 1, 2)));
472 void error_prefix(int);
473 void shellf(const char *, ...)
474 __attribute__((__format__ (printf, 1, 2)));
475 void shprintf(const char *, ...)
476 __attribute__((__format__ (printf, 1, 2)));
477 #ifdef KSH_DEBUG
478 void kshdebug_init_(void);
479 void kshdebug_printf_(const char *, ...)
480 __attribute__((__format__ (printf, 1, 2)));
481 void kshdebug_dump_(const char *, const void *, int);
482 #endif /* KSH_DEBUG */
483 int can_seek(int);
484 void initio(void);
485 int ksh_dup2(int, int, int);
486 int savefd(int);
487 void restfd(int, int);
488 void openpipe(int *);
489 void closepipe(int *);
490 int check_fd(char *, int, const char **);
491 void coproc_init(void);
492 void coproc_read_close(int);
493 void coproc_readw_close(int);
494 void coproc_write_close(int);
495 int coproc_getfd(int, const char **);
496 void coproc_cleanup(int);
497 struct temp *maketemp(Area *, Temp_type, struct temp **);
498 /* jobs.c */
499 void j_init(int);
500 void j_suspend(void);
501 void j_exit(void);
502 void j_change(void);
503 int exchild(struct op *, int, volatile int *, int);
504 void startlast(void);
505 int waitlast(void);
506 int waitfor(const char *, int *);
507 int j_kill(const char *, int);
508 int j_resume(const char *, int);
509 int j_jobs(const char *, int, int);
510 int j_njobs(void);
511 void j_notify(void);
512 pid_t j_async(void);
513 int j_stopped_running(void);
514 /* mail.c */
515 void mcheck(void);
516 void mcset(int64_t);
517 void mbset(char *);
518 void mpset(char *);
519 /* main.c */
520 int include(const char *, int, char **, int);
521 int command(const char *, int);
522 int shell(Source *volatile, int volatile);
523 void unwind(int) __attribute__((__noreturn__));
524 void newenv(int);
525 void quitenv(struct shf *);
526 void cleanup_parents_env(void);
527 void cleanup_proc_env(void);
528 /* misc.c */
529 void setctypes(const char *, int);
530 void initctypes(void);
531 char * u64ton(uint64_t, int);
532 char * str_save(const char *, Area *);
533 char * str_nsave(const char *, int, Area *);
534 int option(const char *);
535 char * getoptions(void);
536 void change_flag(enum sh_flag, int, int);
537 int parse_args(char **, int, int *);
538 int getn(const char *, int *);
539 int bi_getn(const char *, int *);
540 int gmatch(const char *, const char *, int);
541 int has_globbing(const char *, const char *);
542 const unsigned char *pat_scan(const unsigned char *, const unsigned char *,
543 int);
544 void qsortp(void **, size_t, int (*)(const void *, const void *));
545 int xstrcmp(const void *, const void *);
546 void ksh_getopt_reset(Getopt *, int);
547 int ksh_getopt(char **, Getopt *, const char *);
548 void print_value_quoted(const char *);
549 void print_columns(struct shf *, int, char *(*)(void *, int, char *, int),
550 void *, int, int prefcol);
551 int strip_nuls(char *, int);
552 int blocking_read(int, char *, int);
553 int reset_nonblock(int);
554 char *ksh_get_wd(char *, int);
555 /* path.c */
556 int make_path(const char *, const char *, char **, XString *, int *);
557 void simplify_path(char *);
558 char *get_phys_path(const char *);
559 void set_current_wd(char *);
560 /* syn.c */
561 void initkeywords(void);
562 struct op * compile(Source *);
563 /* trap.c */
564 void inittraps(void);
565 void alarm_init(void);
566 Trap * gettrap(const char *, int);
567 void trapsig(int);
568 void intrcheck(void);
569 int fatal_trap_check(void);
570 int trap_pending(void);
571 void runtraps(int intr);
572 void runtrap(Trap *);
573 void cleartraps(void);
574 void restoresigs(void);
575 void settrap(Trap *, char *);
576 int block_pipe(void);
577 void restore_pipe(int);
578 int setsig(Trap *, sig_t, int);
579 void setexecsig(Trap *, int);
580 /* var.c */
581 void newblock(void);
582 void popblock(void);
583 void initvar(void);
584 struct tbl * global(const char *);
585 struct tbl * local(const char *, bool);
586 char * str_val(struct tbl *);
587 int64_t intval(struct tbl *);
588 int setstr(struct tbl *, const char *, int);
589 struct tbl *setint_v(struct tbl *, struct tbl *, bool);
590 void setint(struct tbl *, int64_t);
591 int getint(struct tbl *, int64_t *, bool);
592 struct tbl *typeset(const char *, int, int, int, int);
593 void unset(struct tbl *, int);
594 char * skip_varname(const char *, int);
595 char *skip_wdvarname(const char *, int);
596 int is_wdvarname(const char *, int);
597 int is_wdvarassign(const char *);
598 char ** makenv(void);
599 void change_random(void);
600 int array_ref_len(const char *);
601 char * arrayname(const char *);
602 void set_array(const char *, int, char **);
603 /* vi.c: see edit.h */