1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1982-2009 AT&T Intellectual Property * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Intellectual Property * 8 * * 9 * A copy of the License is available at * 10 * http://www.opensource.org/licenses/cpl1.0.txt * 11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * David Korn <dgk@research.att.com> * 18 * * 19 ***********************************************************************/ 20 #pragma prototyped 21 /* 22 * David Korn 23 * AT&T Labs 24 * 25 * Shell interface private definitions 26 * 27 */ 28 #ifndef defs_h_defined 29 #define defs_h_defined 30 31 #include <ast.h> 32 #include <sfio.h> 33 #include <error.h> 34 #include "FEATURE/options" 35 #include <cdt.h> 36 #include <history.h> 37 #include "fault.h" 38 #include "argnod.h" 39 #include "name.h" 40 #include <ctype.h> 41 #define _SH_PRIVATE 42 #include <shcmd.h> 43 #undef _SH_PRIVATE 44 45 #ifndef pointerof 46 #define pointerof(x) ((void*)((char*)0+(x))) 47 #endif 48 49 #define Empty ((char*)(e_sptbnl+3)) 50 51 #define env_change() (++ast.env_serial) 52 #if SHOPT_ENV 53 # include <env.h> 54 #else 55 # define Env_t void 56 # define sh_envput(e,p) env_change() 57 # define env_delete(e,p) env_change() 58 #endif 59 60 /* 61 * note that the first few fields have to be the same as for 62 * Shscoped_t in <shell.h> 63 */ 64 65 66 struct sh_scoped 67 { 68 struct sh_scoped *prevst; /* pointer to previous state */ 69 int dolc; 70 char **dolv; 71 char *cmdname; 72 char *filename; 73 char *funname; 74 int lineno; 75 Dt_t *save_tree; /* var_tree for calling function */ 76 struct sh_scoped *self; /* pointer to copy of this scope*/ 77 Dt_t *var_local; /* local level variables for name() */ 78 struct slnod *staklist; /* link list of function stacks */ 79 int states; 80 int breakcnt; 81 int execbrk; 82 int loopcnt; 83 int firstline; 84 int32_t optindex; 85 int32_t optnum; 86 int32_t tmout; /* value for TMOUT */ 87 short optchar; 88 short opterror; 89 int ioset; 90 unsigned short trapmax; 91 char *trap[SH_DEBUGTRAP+1]; 92 char **trapcom; 93 char **otrapcom; 94 void *timetrap; 95 struct Ufunction *real_fun; /* current 'function name' function */ 96 }; 97 98 struct limits 99 { 100 long arg_max; /* max arg+env exec() size */ 101 int open_max; /* maximum number of file descriptors */ 102 int clk_tck; /* number of ticks per second */ 103 int child_max; /* maxumum number of children */ 104 int ngroups_max; /* maximum number of process groups */ 105 unsigned char posix_version; /* posix version number */ 106 unsigned char posix_jobcontrol;/* non-zero for job control systems */ 107 unsigned char fs3d; /* non-zero for 3-d file system */ 108 }; 109 110 #define _SH_PRIVATE \ 111 struct sh_scoped st; /* scoped information */ \ 112 struct limits lim; /* run time limits */ \ 113 Stk_t *stk; /* stack poiter */ \ 114 Sfio_t *heredocs; /* current here-doc temp file */ \ 115 Sfio_t *funlog; /* for logging function definitions */ \ 116 int **fdptrs; /* pointer to file numbers */ \ 117 int savexit; \ 118 char *lastarg; \ 119 char *lastpath; /* last alsolute path found */ \ 120 int path_err; /* last error on path search */ \ 121 Dt_t *track_tree; /* for tracked aliases*/ \ 122 Dt_t *var_base; /* global level variables */ \ 123 Namval_t *namespace; /* current active namespace*/ \ 124 Namval_t *last_table; /* last table used in last nv_open */ \ 125 Namval_t *prev_table; /* previous table used in nv_open */ \ 126 Sfio_t *outpool; /* ouput stream pool */ \ 127 long timeout; /* read timeout */ \ 128 short curenv; /* current subshell number */ \ 129 short jobenv; /* subshell number for jobs */ \ 130 int infd; /* input file descriptor */ \ 131 int nextprompt; /* next prompt is PS<nextprompt> */ \ 132 int bltin_nnodes; /* number of bltins nodes */ \ 133 Namval_t *bltin_nodes; /* pointer to built-in variables */ \ 134 Namval_t *bltin_cmds; /* pointer to built-in commands */ \ 135 Namval_t *posix_fun; /* points to last name() function */ \ 136 char *outbuff; /* pointer to output buffer */ \ 137 char *errbuff; /* pointer to stderr buffer */ \ 138 char *prompt; /* pointer to prompt string */ \ 139 char *shname; /* shell name */ \ 140 char *shpath; /* path name of shell */ \ 141 char *user; /* name of real user for pfsh */ \ 142 char *comdiv; /* points to sh -c argument */ \ 143 char *prefix; /* prefix for compound assignment */ \ 144 sigjmp_buf *jmplist; /* longjmp return stack */ \ 145 char **sigmsg; /* points to signal messages */ \ 146 int oldexit; \ 147 uid_t userid,euserid; /* real and effective user id */ \ 148 gid_t groupid,egroupid;/* real and effective group id */ \ 149 pid_t pid; /* process id of shell */ \ 150 pid_t bckpid; /* background process id */ \ 151 pid_t cpid; \ 152 pid_t spid; /* subshell process id */ \ 153 pid_t pipepid; \ 154 int32_t ppid; /* parent process id of shell */ \ 155 int topfd; \ 156 int sigmax; /* maximum number of signals */ \ 157 int savesig; \ 158 unsigned char *sigflag; /* pointer to signal states */ \ 159 char intrap; \ 160 char login_sh; \ 161 char lastbase; \ 162 char forked; \ 163 char binscript; \ 164 char deftype; \ 165 char funload; \ 166 char used_pos; /* used postional parameter */\ 167 char universe; \ 168 char winch; \ 169 char indebug; /* set when in debug trap */ \ 170 unsigned char lastsig; /* last signal received */ \ 171 char subshare; /* set when in ${..} comsub */ \ 172 char toomany; /* set when out of fd's */ \ 173 char *readscript; /* set before reading a script */ \ 174 int *inpipe; /* input pipe pointer */ \ 175 int *outpipe; /* output pipe pointer */ \ 176 int cpipe[2]; \ 177 int coutpipe; \ 178 int inuse_bits; \ 179 struct argnod *envlist; \ 180 struct dolnod *arglist; \ 181 int fn_depth; \ 182 int fn_reset; \ 183 int dot_depth; \ 184 int hist_depth; \ 185 int xargmin; \ 186 int xargmax; \ 187 int xargexit; \ 188 int nenv; \ 189 mode_t mask; \ 190 long nforks; \ 191 Env_t *env; \ 192 void *init_context; \ 193 void *mac_context; \ 194 void *lex_context; \ 195 void *arg_context; \ 196 void *ed_context; \ 197 void *job_context; \ 198 void *pathlist; \ 199 void *defpathlist; \ 200 void *cdpathlist; \ 201 char **argaddr; \ 202 void *optlist; \ 203 struct sh_scoped global; \ 204 struct checkpt checkbase; \ 205 Shinit_f userinit; \ 206 Shbltin_f bltinfun; \ 207 Shbltin_t bltindata; \ 208 Shwait_f waitevent; \ 209 char *cur_line; \ 210 char *rcfile; \ 211 char **login_files; \ 212 int offsets[10]; \ 213 Sfio_t **sftable; \ 214 unsigned char *fdstatus; \ 215 const char *pwd; \ 216 History_t *hist_ptr; \ 217 void *jmpbuffer; \ 218 void *mktype; \ 219 Sfio_t *strbuf; \ 220 Sfio_t *strbuf2; \ 221 Dt_t *last_root; \ 222 Dt_t *prev_root; \ 223 Dt_t *fpathdict; \ 224 Dt_t *typedict; \ 225 char ifstable[256]; \ 226 unsigned char sigruntime[2]; \ 227 unsigned long test; \ 228 Shopt_t offoptions; \ 229 Shopt_t glob_options; \ 230 Namval_t *typeinit; \ 231 int *stats; \ 232 Namfun_t nvfun; \ 233 struct Regress_s*regress; 234 235 #include <shell.h> 236 237 #include "regress.h" 238 239 /* error exits from various parts of shell */ 240 #define NIL(type) ((type)0) 241 242 #define new_of(type,x) ((type*)malloc((unsigned)sizeof(type)+(x))) 243 244 #define exitset() (sh.savexit=sh.exitval) 245 246 #ifndef SH_DICT 247 #define SH_DICT (void*)e_dict 248 #endif 249 250 #ifndef SH_CMDLIB_DIR 251 #define SH_CMDLIB_DIR "/opt/ast/bin" 252 #endif 253 254 /* states */ 255 /* low numbered states are same as options */ 256 #define SH_NOFORK 0 /* set when fork not necessary */ 257 #define SH_FORKED 7 /* set when process has been forked */ 258 #define SH_PROFILE 8 /* set when processing profiles */ 259 #define SH_NOALIAS 9 /* do not expand non-exported aliases */ 260 #define SH_NOTRACK 10 /* set to disable sftrack() function */ 261 #define SH_STOPOK 11 /* set for stopable builtins */ 262 #define SH_GRACE 12 /* set for timeout grace period */ 263 #define SH_TIMING 13 /* set while timing pipelines */ 264 #define SH_DEFPATH 14 /* set when using default path */ 265 #define SH_INIT 15 /* set when initializing the shell */ 266 #define SH_TTYWAIT 16 /* waiting for keyboard input */ 267 #define SH_FCOMPLETE 17 /* set for filename completion */ 268 #define SH_PREINIT 18 /* set with SH_INIT before parsing options */ 269 #define SH_COMPLETE 19 /* set for command completion */ 270 271 #define SH_BASH 41 272 #define SH_BRACEEXPAND 42 273 #define SH_POSIX 46 274 #define SH_MULTILINE 47 275 276 #define SH_NOPROFILE 78 277 #define SH_NOUSRPROFILE 79 278 #define SH_LOGIN_SHELL 67 279 #define SH_COMMANDLINE 0x100 280 #define SH_BASHEXTRA 0x200 281 #define SH_BASHOPT 0x400 282 283 #define SH_ID "ksh" /* ksh id */ 284 #define SH_STD "sh" /* standard sh id */ 285 286 /* defines for sh_type() */ 287 288 #define SH_TYPE_SH 001 289 #define SH_TYPE_KSH 002 290 #define SH_TYPE_BASH 004 291 #define SH_TYPE_LOGIN 010 292 #define SH_TYPE_PROFILE 020 293 #define SH_TYPE_RESTRICTED 040 294 295 #if SHOPT_BASH 296 # ifndef SHOPT_HISTEXPAND 297 # define SHOPT_HISTEXPAND 1 298 # endif 299 /* 300 * define for all the bash options 301 */ 302 # define SH_CDABLE_VARS 51 303 # define SH_CDSPELL 52 304 # define SH_CHECKHASH 53 305 # define SH_CHECKWINSIZE 54 306 # define SH_CMDHIST 55 307 # define SH_DOTGLOB 56 308 # define SH_EXECFAIL 57 309 # define SH_EXPAND_ALIASES 58 310 # define SH_EXTGLOB 59 311 # define SH_HOSTCOMPLETE 63 312 # define SH_HUPONEXIT 64 313 # define SH_INTERACTIVE_COMM 65 314 # define SH_LITHIST 66 315 # define SH_MAILWARN 68 316 # define SH_NOEMPTYCMDCOMPL 69 317 # define SH_NOCASEGLOB 70 318 # define SH_NULLGLOB 71 319 # define SH_PHYSICAL 45 320 # define SH_PROGCOMP 72 321 # define SH_PROMPTVARS 73 322 # define SH_RESTRICTED2 74 323 # define SH_SHIFT_VERBOSE 75 324 # define SH_SOURCEPATH 76 325 # define SH_XPG_ECHO 77 326 #endif 327 328 #if SHOPT_HISTEXPAND 329 # define SH_HISTAPPEND 60 330 # define SH_HISTEXPAND 43 331 # define SH_HISTORY2 44 332 # define SH_HISTREEDIT 61 333 # define SH_HISTVERIFY 62 334 #endif 335 336 #ifndef PIPE_BUF 337 # define PIPE_BUF 512 338 #endif 339 340 #define MATCH_MAX 64 341 342 #define SH_READEVAL 0x4000 /* for sh_eval */ 343 #define SH_FUNEVAL 0x10000 /* for sh_eval for function load */ 344 345 extern Shell_t *nv_shell(Namval_t*); 346 extern int sh_addlib(void*); 347 extern void sh_applyopts(Shell_t*,Shopt_t); 348 extern char **sh_argbuild(Shell_t*,int*,const struct comnod*,int); 349 extern struct dolnod *sh_argfree(Shell_t *, struct dolnod*,int); 350 extern struct dolnod *sh_argnew(Shell_t*,char*[],struct dolnod**); 351 extern void *sh_argopen(Shell_t*); 352 extern struct argnod *sh_argprocsub(Shell_t*,struct argnod*); 353 extern void sh_argreset(Shell_t*,struct dolnod*,struct dolnod*); 354 extern Namval_t *sh_assignok(Namval_t*,int); 355 extern struct dolnod *sh_arguse(Shell_t*); 356 extern char *sh_checkid(char*,char*); 357 extern int sh_debug(Shell_t *shp,const char*,const char*,const char*,char *const[],int); 358 extern int sh_echolist(Sfio_t*, int, char**); 359 extern struct argnod *sh_endword(Shell_t*,int); 360 extern char **sh_envgen(void); 361 #if SHOPT_ENV 362 extern void sh_envput(Env_t*, Namval_t*); 363 #endif 364 extern void sh_envnolocal(Namval_t*,void*); 365 extern Sfdouble_t sh_arith(const char*); 366 extern void *sh_arithcomp(char*); 367 extern pid_t sh_fork(int,int*); 368 extern pid_t _sh_fork(pid_t, int ,int*); 369 extern char *sh_mactrim(Shell_t*,char*,int); 370 extern int sh_macexpand(Shell_t*,struct argnod*,struct argnod**,int); 371 extern int sh_macfun(Shell_t*,const char*,int); 372 extern void sh_machere(Shell_t*,Sfio_t*, Sfio_t*, char*); 373 extern void *sh_macopen(Shell_t*); 374 extern char *sh_macpat(Shell_t*,struct argnod*,int); 375 extern char *sh_mactry(Shell_t*,char*); 376 extern void sh_printopts(Shopt_t,int,Shopt_t*); 377 extern int sh_readline(Shell_t*,char**,int,int,long); 378 extern Sfio_t *sh_sfeval(char*[]); 379 extern void sh_setmatch(const char*,int,int,int[]); 380 extern Dt_t *sh_subaliastree(int); 381 extern void sh_scope(Shell_t*, struct argnod*, int); 382 extern Namval_t *sh_scoped(Shell_t*, Namval_t*); 383 extern Dt_t *sh_subfuntree(int); 384 extern void sh_subjobcheck(pid_t); 385 extern int sh_subsavefd(int); 386 extern void sh_subtmpfile(int); 387 extern char *sh_substitute(const char*,const char*,char*); 388 extern const char *_sh_translate(const char*); 389 extern int sh_trace(char*[],int); 390 extern void sh_trim(char*); 391 extern int sh_type(const char*); 392 extern void sh_unscope(Shell_t*); 393 extern void sh_utol(const char*, char*); 394 extern int sh_whence(char**,int); 395 396 #ifndef ERROR_dictionary 397 # define ERROR_dictionary(s) (s) 398 #endif 399 #define sh_translate(s) _sh_translate(ERROR_dictionary(s)) 400 401 #define WBITS (sizeof(long)*8) 402 #define WMASK (0xff) 403 404 #define is_option(s,x) ((s)->v[((x)&WMASK)/WBITS] & (1L << ((x) % WBITS))) 405 #define on_option(s,x) ((s)->v[((x)&WMASK)/WBITS] |= (1L << ((x) % WBITS))) 406 #define off_option(s,x) ((s)->v[((x)&WMASK)/WBITS] &= ~(1L << ((x) % WBITS))) 407 #define sh_isoption(x) is_option(&sh.options,x) 408 #define sh_onoption(x) on_option(&sh.options,x) 409 #define sh_offoption(x) off_option(&sh.options,x) 410 411 412 #define sh_state(x) ( 1<<(x)) 413 #define sh_isstate(x) (sh.st.states&sh_state(x)) 414 #define sh_onstate(x) (sh.st.states |= sh_state(x)) 415 #define sh_offstate(x) (sh.st.states &= ~sh_state(x)) 416 #define sh_getstate() (sh.st.states) 417 #define sh_setstate(x) (sh.st.states = (x)) 418 419 #define sh_sigcheck() do{if(sh.trapnote&SH_SIGSET)sh_exit(SH_EXITSIG);} while(0) 420 421 extern int32_t sh_mailchk; 422 extern const char e_dict[]; 423 424 /* sh_printopts() mode flags -- set --[no]option by default */ 425 426 #define PRINT_VERBOSE 0x01 /* option on|off list */ 427 #define PRINT_ALL 0x02 /* list unset options too */ 428 #define PRINT_NO_HEADER 0x04 /* omit listing header */ 429 #define PRINT_SHOPT 0x08 /* shopt -s|-u */ 430 #define PRINT_TABLE 0x10 /* table of all options */ 431 432 #ifdef SHOPT_STATS 433 /* performance statistics */ 434 # define STAT_ARGHITS 0 435 # define STAT_ARGEXPAND 1 436 # define STAT_COMSUB 2 437 # define STAT_FORKS 3 438 # define STAT_FUNCT 4 439 # define STAT_GLOBS 5 440 # define STAT_READS 6 441 # define STAT_NVHITS 7 442 # define STAT_NVOPEN 8 443 # define STAT_PATHS 9 444 # define STAT_SVFUNCT 10 445 # define STAT_SCMDS 11 446 # define STAT_SPAWN 12 447 # define STAT_SUBSHELL 13 448 extern const Shtable_t shtab_stats[]; 449 # define sh_stats(x) (sh.stats[(x)]++) 450 #else 451 # define sh_stats(x) 452 #endif /* SHOPT_STATS */ 453 454 455 #endif 456