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