sh.h revision 1.23
1/* $OpenBSD: sh.h,v 1.23 2004/12/18 22:11:43 millert 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 <stdio.h> 14#include <sys/types.h> 15#include <setjmp.h> 16#include <stddef.h> 17#include <stdlib.h> 18#include <unistd.h> 19#include <string.h> 20#include <stdarg.h> 21 22#include <errno.h> 23#include <fcntl.h> 24#include <limits.h> 25 26#include <signal.h> 27 28#include <paths.h> 29 30/* Find a integer type that is at least 32 bits (or die) - SIZEOF_* defined 31 * by autoconf (assumes an 8 bit byte, but I'm not concerned). 32 * NOTE: INT32 may end up being more than 32 bits. 33 */ 34# define INT32 int 35 36/* end of common headers */ 37 38/* some useful #defines */ 39#ifdef EXTERN 40# define I__(i) = i 41#else 42# define I__(i) 43# define EXTERN extern 44# define EXTERN_DEFINED 45#endif 46 47#define EXECSHELL "/bin/sh" 48#define EXECSHELL_STR "EXECSHELL" 49 50typedef int bool_t; 51#define FALSE 0 52#define TRUE 1 53 54#define NELEM(a) (sizeof(a) / sizeof((a)[0])) 55#define sizeofN(type, n) (sizeof(type) * (n)) 56#define BIT(i) (1<<(i)) /* define bit in flag */ 57 58/* Table flag type - needs > 16 and < 32 bits */ 59typedef INT32 Tflag; 60 61#define NUFILE 32 /* Number of user-accessible files */ 62#define FDBASE 10 /* First file usable by Shell */ 63 64/* Make MAGIC a char that might be printed to make bugs more obvious, but 65 * not a char that is used often. Also, can't use the high bit as it causes 66 * portability problems (calling strchr(x, 0x80|'x') is error prone). 67 */ 68#define MAGIC (7) /* prefix for *?[!{,} during expand */ 69#define ISMAGIC(c) ((unsigned char)(c) == MAGIC) 70#define NOT '!' /* might use ^ (ie, [!...] vs [^..]) */ 71 72#define LINE 2048 /* input line size */ 73#define PATH 1024 /* pathname size (todo: PATH_MAX/pathconf()) */ 74#define ARRAYMAX 1023 /* max array index */ 75 76EXTERN const char *kshname; /* $0 */ 77EXTERN pid_t kshpid; /* $$, shell pid */ 78EXTERN pid_t procpid; /* pid of executing process */ 79EXTERN uid_t ksheuid; /* effective uid of shell */ 80EXTERN int exstat; /* exit status */ 81EXTERN int subst_exstat; /* exit status of last $(..)/`..` */ 82EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */ 83 84/* 85 * Area-based allocation built on malloc/free 86 */ 87typedef struct Area { 88 struct link *freelist; /* free list */ 89} Area; 90 91EXTERN Area aperm; /* permanent object space */ 92#define APERM &aperm 93#define ATEMP &e->area 94 95#ifdef KSH_DEBUG 96# define kshdebug_init() kshdebug_init_() 97# define kshdebug_printf(a) kshdebug_printf_ a 98# define kshdebug_dump(a) kshdebug_dump_ a 99#else /* KSH_DEBUG */ 100# define kshdebug_init() 101# define kshdebug_printf(a) 102# define kshdebug_dump(a) 103#endif /* KSH_DEBUG */ 104 105/* 106 * parsing & execution environment 107 */ 108EXTERN struct env { 109 short type; /* environment type - see below */ 110 short flags; /* EF_* */ 111 Area area; /* temporary allocation area */ 112 struct block *loc; /* local variables and functions */ 113 short *savefd; /* original redirected fd's */ 114 struct env *oenv; /* link to previous environment */ 115 sigjmp_buf jbuf; /* long jump back to env creator */ 116 struct temp *temps; /* temp files */ 117} *e; 118 119/* struct env.type values */ 120#define E_NONE 0 /* dummy environment */ 121#define E_PARSE 1 /* parsing command # */ 122#define E_FUNC 2 /* executing function # */ 123#define E_INCL 3 /* including a file via . # */ 124#define E_EXEC 4 /* executing command tree */ 125#define E_LOOP 5 /* executing for/while # */ 126#define E_ERRH 6 /* general error handler # */ 127/* # indicates env has valid jbuf (see unwind()) */ 128 129/* struct env.flag values */ 130#define EF_FUNC_PARSE BIT(0) /* function being parsed */ 131#define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */ 132#define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */ 133 134/* Do breaks/continues stop at env type e? */ 135#define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE \ 136 || (t) == E_FUNC || (t) == E_INCL) 137/* Do returns stop at env type e? */ 138#define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL) 139 140/* values for siglongjmp(e->jbuf, 0) */ 141#define LRETURN 1 /* return statement */ 142#define LEXIT 2 /* exit statement */ 143#define LERROR 3 /* errorf() called */ 144#define LLEAVE 4 /* untrappable exit/error */ 145#define LINTR 5 /* ^C noticed */ 146#define LBREAK 6 /* break statement */ 147#define LCONTIN 7 /* continue statement */ 148#define LSHELL 8 /* return to interactive shell() */ 149#define LAEXPR 9 /* error in arithmetic expression */ 150 151/* option processing */ 152#define OF_CMDLINE 0x01 /* command line */ 153#define OF_SET 0x02 /* set builtin */ 154#define OF_SPECIAL 0x04 /* a special variable changing */ 155#define OF_INTERNAL 0x08 /* set internally by shell */ 156#define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL) 157 158struct option { 159 const char *name; /* long name of option */ 160 char c; /* character flag (if any) */ 161 short flags; /* OF_* */ 162}; 163extern const struct option options[]; 164 165/* 166 * flags (the order of these enums MUST match the order in misc.c(options[])) 167 */ 168enum sh_flag { 169 FEXPORT = 0, /* -a: export all */ 170#ifdef BRACE_EXPAND 171 FBRACEEXPAND, /* enable {} globbing */ 172#endif 173 FBGNICE, /* bgnice */ 174 FCOMMAND, /* -c: (invocation) execute specified command */ 175 FCSHHISTORY, /* csh-style history enabled */ 176#ifdef EMACS 177 FEMACS, /* emacs command editing */ 178 FEMACSUSEMETA, /* use 8th bit as meta */ 179#endif 180 FERREXIT, /* -e: quit on error */ 181#ifdef EMACS 182 FGMACS, /* gmacs command editing */ 183#endif 184 FIGNOREEOF, /* eof does not exit */ 185 FTALKING, /* -i: interactive */ 186 FKEYWORD, /* -k: name=value anywhere */ 187 FLOGIN, /* -l: a login shell */ 188 FMARKDIRS, /* mark dirs with / in file name completion */ 189 FMONITOR, /* -m: job control monitoring */ 190 FNOCLOBBER, /* -C: don't overwrite existing files */ 191 FNOEXEC, /* -n: don't execute any commands */ 192 FNOGLOB, /* -f: don't do file globbing */ 193 FNOHUP, /* -H: don't kill running jobs when login shell exits */ 194 FNOLOG, /* don't save functions in history (ignored) */ 195#ifdef JOBS 196 FNOTIFY, /* -b: asynchronous job completion notification */ 197#endif 198 FNOUNSET, /* -u: using an unset var is an error */ 199 FPHYSICAL, /* -o physical: don't do logical cd's/pwd's */ 200 FPOSIX, /* -o posix: be posixly correct */ 201 FPRIVILEGED, /* -p: use suid_profile */ 202 FRESTRICTED, /* -r: restricted shell */ 203 FSH, /* -o sh: favor sh behaviour */ 204 FSTDIN, /* -s: (invocation) parse stdin */ 205 FTRACKALL, /* -h: create tracked aliases for all commands */ 206 FVERBOSE, /* -v: echo input */ 207#ifdef VI 208 FVI, /* vi command editing */ 209 FVIRAW, /* always read in raw mode (ignored) */ 210 FVISHOW8, /* display chars with 8th bit set as is (versus M-) */ 211 FVITABCOMPLETE, /* enable tab as file name completion char */ 212 FVIESCCOMPLETE, /* enable ESC as file name completion in command mode */ 213#endif 214 FXTRACE, /* -x: execution trace */ 215 FTALKING_I, /* (internal): initial shell was interactive */ 216 FNFLAGS /* (place holder: how many flags are there) */ 217}; 218 219#define Flag(f) (shell_flags[(int) (f)]) 220 221EXTERN char shell_flags [FNFLAGS]; 222 223EXTERN char null [] I__(""); /* null value for variable */ 224EXTERN char space [] I__(" "); 225EXTERN char newline [] I__("\n"); 226EXTERN char slash [] I__("/"); 227 228enum temp_type { 229 TT_HEREDOC_EXP, /* expanded heredoc */ 230 TT_HIST_EDIT /* temp file used for history editing (fc -e) */ 231}; 232typedef enum temp_type Temp_type; 233/* temp/heredoc files. The file is removed when the struct is freed. */ 234struct temp { 235 struct temp *next; 236 struct shf *shf; 237 int pid; /* pid of process parsed here-doc */ 238 Temp_type type; 239 char *name; 240}; 241 242/* 243 * stdio and our IO routines 244 */ 245 246#define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */ 247#define shl_stdout (&shf_iob[1]) 248#define shl_out (&shf_iob[2]) 249EXTERN int shl_stdout_ok; 250 251/* 252 * trap handlers 253 */ 254typedef struct trap { 255 int signal; /* signal number */ 256 const char *name; /* short name */ 257 const char *mess; /* descriptive name */ 258 char *trap; /* trap command */ 259 volatile sig_atomic_t set; /* trap pending */ 260 int flags; /* TF_* */ 261 sig_t cursig; /* current handler (valid if TF_ORIG_* set) */ 262 sig_t shtrap; /* shell signal handler */ 263} Trap; 264 265/* values for Trap.flags */ 266#define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */ 267#define TF_USER_SET BIT(1) /* user has (tried to) set trap */ 268#define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */ 269#define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */ 270#define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */ 271#define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */ 272#define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */ 273#define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */ 274#define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */ 275#define TF_FATAL BIT(9) /* causes termination if not trapped */ 276 277/* values for setsig()/setexecsig() flags argument */ 278#define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */ 279#define SS_RESTORE_CURR 0 /* leave current handler in place */ 280#define SS_RESTORE_ORIG 1 /* restore original handler */ 281#define SS_RESTORE_DFL 2 /* restore to SIG_DFL */ 282#define SS_RESTORE_IGN 3 /* restore to SIG_IGN */ 283#define SS_FORCE BIT(3) /* set signal even if original signal ignored */ 284#define SS_USER BIT(4) /* user is doing the set (ie, trap command) */ 285#define SS_SHTRAP BIT(5) /* trap for internal use (CHLD,ALRM,WINCH) */ 286 287#define SIGEXIT_ 0 /* for trap EXIT */ 288#define SIGERR_ NSIG /* for trap ERR */ 289 290EXTERN volatile sig_atomic_t trap; /* traps pending? */ 291EXTERN volatile sig_atomic_t intrsig; /* pending trap interrupts command */ 292EXTERN volatile sig_atomic_t fatal_trap;/* received a fatal signal */ 293extern Trap sigtraps[NSIG+1]; 294 295/* 296 * TMOUT support 297 */ 298/* values for ksh_tmout_state */ 299enum tmout_enum { 300 TMOUT_EXECUTING = 0, /* executing commands */ 301 TMOUT_READING, /* waiting for input */ 302 TMOUT_LEAVING /* have timed out */ 303 }; 304EXTERN unsigned int ksh_tmout; 305EXTERN enum tmout_enum ksh_tmout_state I__(TMOUT_EXECUTING); 306 307/* For "You have stopped jobs" message */ 308EXTERN int really_exit; 309 310/* 311 * fast character classes 312 */ 313#define C_ALPHA BIT(0) /* a-z_A-Z */ 314#define C_DIGIT BIT(1) /* 0-9 */ 315#define C_LEX1 BIT(2) /* \0 \t\n|&;<>() */ 316#define C_VAR1 BIT(3) /* *@#!$-? */ 317#define C_IFSWS BIT(4) /* \t \n (IFS white space) */ 318#define C_SUBOP1 BIT(5) /* "=-+?" */ 319#define C_SUBOP2 BIT(6) /* "#%" */ 320#define C_IFS BIT(7) /* $IFS */ 321#define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */ 322 323extern short ctypes []; 324 325#define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t)) 326#define letter(c) ctype(c, C_ALPHA) 327#define digit(c) ctype(c, C_DIGIT) 328#define letnum(c) ctype(c, C_ALPHA|C_DIGIT) 329 330EXTERN int ifs0 I__(' '); /* for "$*" */ 331 332/* Argument parsing for built-in commands and getopts command */ 333 334/* Values for Getopt.flags */ 335#define GF_ERROR BIT(0) /* call errorf() if there is an error */ 336#define GF_PLUSOPT BIT(1) /* allow +c as an option */ 337#define GF_NONAME BIT(2) /* don't print argv[0] in errors */ 338 339/* Values for Getopt.info */ 340#define GI_MINUS BIT(0) /* an option started with -... */ 341#define GI_PLUS BIT(1) /* an option started with +... */ 342#define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */ 343 344typedef struct { 345 int optind; 346 int uoptind;/* what user sees in $OPTIND */ 347 char *optarg; 348 int flags; /* see GF_* */ 349 int info; /* see GI_* */ 350 unsigned int p; /* 0 or index into argv[optind - 1] */ 351 char buf[2]; /* for bad option OPTARG value */ 352} Getopt; 353 354EXTERN Getopt builtin_opt; /* for shell builtin commands */ 355EXTERN Getopt user_opt; /* parsing state for getopts builtin command */ 356 357/* This for co-processes */ 358 359typedef INT32 Coproc_id; /* something that won't (realisticly) wrap */ 360struct coproc { 361 int read; /* pipe from co-process's stdout */ 362 int readw; /* other side of read (saved temporarily) */ 363 int write; /* pipe to co-process's stdin */ 364 Coproc_id id; /* id of current output pipe */ 365 int njobs; /* number of live jobs using output pipe */ 366 void *job; /* 0 or job of co-process using input pipe */ 367}; 368EXTERN struct coproc coproc; 369 370/* Used in jobs.c and by coprocess stuff in exec.c */ 371EXTERN sigset_t sm_default, sm_sigchld; 372 373extern const char ksh_version[]; 374 375/* name of called builtin function (used by error functions) */ 376EXTERN char *builtin_argv0; 377EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */ 378 379/* current working directory, and size of memory allocated for same */ 380EXTERN char *current_wd; 381EXTERN int current_wd_size; 382 383#ifdef EDIT 384/* Minimum required space to work with on a line - if the prompt leaves less 385 * space than this on a line, the prompt is truncated. 386 */ 387# define MIN_EDIT_SPACE 7 388/* Minimum allowed value for x_cols: 2 for prompt, 3 for " < " at end of line 389 */ 390# define MIN_COLS (2 + MIN_EDIT_SPACE + 3) 391EXTERN int x_cols I__(80); /* tty columns */ 392#else 393# define x_cols 80 /* for pr_menu(exec.c) */ 394#endif 395 396/* These to avoid bracket matching problems */ 397#define OPAREN '(' 398#define CPAREN ')' 399#define OBRACK '[' 400#define CBRACK ']' 401#define OBRACE '{' 402#define CBRACE '}' 403 404/* Determine the location of the system (common) profile */ 405#define KSH_SYSTEM_PROFILE "/etc/profile" 406 407/* Used by v_evaluate() and setstr() to control action when error occurs */ 408#define KSH_UNWIND_ERROR 0 /* unwind the stack (longjmp) */ 409#define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */ 410 411#include "shf.h" 412#include "table.h" 413#include "tree.h" 414#include "expand.h" 415#include "lex.h" 416#include "proto.h" 417 418/* be sure not to interfere with anyone else's idea about EXTERN */ 419#ifdef EXTERN_DEFINED 420# undef EXTERN_DEFINED 421# undef EXTERN 422#endif 423#undef I__ 424