1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1982-2013 AT&T Intellectual Property * 5 * and is licensed under the * 6 * Eclipse Public License, Version 1.0 * 7 * by AT&T Intellectual Property * 8 * * 9 * A copy of the License is available at * 10 * http://www.eclipse.org/org/documents/epl-v10.html * 11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * David Korn <dgkorn@gmail.com> * 18 * * 19 ***********************************************************************/ 20 // 21 // David Korn 22 // AT&T Labs 23 // 24 // Interface definitions for shell command language. 25 // 26 #ifndef _SHELL_H 27 #define _SHELL_H 1 28 29 #include <stdbool.h> 30 #include <stdint.h> 31 #include <sys/stat.h> 32 33 #define SH_VERSION 20120720 34 35 #include "ast.h" 36 #include "cdt.h" 37 #include "defs.h" 38 #include "fault.h" 39 #include "name.h" 40 #include "shcmd.h" 41 #include "stk.h" 42 43 #if USE_SPAWN 44 #include "spawnvex.h" 45 #endif // USE_SPAWN 46 47 typedef void (*Shinit_f)(Shell_t *, int); 48 49 #define SH_CFLAG 0 50 #define SH_HISTORY 1 // used also as a state 51 #define SH_ERREXIT 2 // used also as a state 52 #define SH_VERBOSE 3 // used also as a state 53 #define SH_MONITOR 4 // used also as a state 54 #define SH_INTERACTIVE 5 // used also as a state 55 #define SH_RESTRICTED 6 56 #define SH_XTRACE 7 57 #define SH_KEYWORD 8 58 #define SH_NOUNSET 9 59 #define SH_NOGLOB 10 60 #define SH_ALLEXPORT 11 61 #define SH_IGNOREEOF 13 62 #define SH_NOCLOBBER 14 63 #define SH_MARKDIRS 15 64 #define SH_BGNICE 16 65 #define SH_VI 17 66 // The "viraw" option no longer has any effect as its behavior is now always eanbled. 67 #define SH_VIRAW 18 68 #define SH_TFLAG 19 69 #define SH_TRACKALL 20 70 #define SH_SFLAG 21 71 #define SH_NOEXEC 22 72 #define SH_GMACS 24 73 #define SH_EMACS 25 74 #define SH_PRIVILEGED 26 75 #define SH_SUBSHARE 27 // subshell shares state with parent 76 #define SH_NOLOG 28 77 #define SH_NOTIFY 29 78 #define SH_DICTIONARY 30 79 #define SH_PIPEFAIL 32 80 #define SH_GLOBSTARS 33 81 // #define SH_XARGS 34 82 #define SH_RC 35 83 #define SH_SHOWME 36 84 #define SH_LETOCTAL 37 85 86 // Error messages. 87 extern const char e_defpath[]; 88 extern const char e_found[]; 89 extern const char e_nospace[]; 90 extern const char e_format[]; 91 extern const char e_number[]; 92 extern const char e_restricted[]; 93 extern const char e_recursive[]; 94 extern const char *e_version; 95 extern const char *ksh_version; 96 97 typedef struct sh_scope { 98 struct sh_scope *par_scope; 99 int argc; 100 char **argv; 101 char *cmdname; 102 char *filename; 103 char *funname; 104 int64_t lineno; 105 Dt_t *var_tree; 106 struct sh_scope *self; 107 } Shscope_t; 108 109 // 110 // Saves the state of the shell. 111 // 112 struct Shell_s { 113 Shopt_t options; // set -o options 114 Dt_t *var_tree; // for shell variables 115 Dt_t *fun_tree; // for shell functions 116 Dt_t *alias_tree; // for alias names 117 Dt_t *bltin_tree; // for builtin commands 118 Shscope_t *topscope; // pointer to top-level scope 119 int inlineno; // line number of current input file 120 int exitval; // most recent exit value 121 unsigned char trapnote; // set when trap/signal is pending 122 char shcomp; // set when running shcomp 123 short subshell; // set for virtual subshell 124 Stk_t *stk; // stack pointer 125 int pwdfd; // file descriptor for pwd 126 // Everything below this line is private and should not be touched by a plugin. 127 struct shared *gd; // global data 128 struct sh_scoped st; // scoped information 129 Sfio_t *heredocs; // current here-doc temp file 130 Sfio_t *funlog; // for logging function definitions 131 int **fdptrs; // pointer to file numbers 132 int savexit; 133 char *lastarg; 134 char *lastpath; // last alsolute path found 135 int path_err; // last error on path search 136 Dt_t *track_tree; // for tracked aliases*/ 137 Dt_t *var_base; // global level variables 138 Dt_t *openmatch; 139 Dt_t *namref_root; 140 Namval_t *namespace; // current active namespace*/ 141 Namval_t *last_table; // last table used in last nv_open 142 Namval_t *prev_table; // previous table used in nv_open 143 Namval_t *oldnp; // last valid parent node 144 Namval_t **nodelist; // for decl commands 145 Sfio_t *outpool; // output stream pool 146 long timeout; // read timeout 147 long curenv; // current subshell number 148 long jobenv; // subshell number for jobs 149 int infd; // input file descriptor 150 short nextprompt; // next prompt is PS<nextprompt> 151 short poolfiles; 152 Namval_t *posix_fun; // points to last name() function 153 char *outbuff; // pointer to output buffer 154 char *errbuff; // pointer to stderr buffer 155 char *prompt; // pointer to prompt string 156 char *shname; // shell name 157 char *comdiv; // points to sh -c argument 158 char *prefix; // prefix for compound assignment 159 checkpt_t *jmplist; // longjmp return stack plus other data 160 char *fifo; // fifo name for process sub 161 int oldexit; 162 pid_t bckpid; // background process id 163 pid_t cpid; 164 pid_t spid; // subshell process id 165 pid_t pipepid; 166 pid_t outpipepid; 167 pid_t *procsub; // pids for >() argument 168 int nprocsub; // number of pids in procsub 169 int topfd; 170 int errorfd; 171 int savesig; 172 unsigned char *sigflag; // pointer to signal states 173 char intrap; 174 char login_sh; 175 char lastbase; 176 char forked; 177 char binscript; 178 char deftype; 179 char funload; 180 char used_pos; // used postional parameter 181 bool echo_universe_valid; 182 char winch; 183 char inarith; // set when in ((...)) 184 char indebug; // set when in debug trap 185 unsigned char ignsig; // ignored signal in subshell 186 unsigned char lastsig; // last signal received 187 char pathinit; // pathinit called from subshell 188 char comsub; // set when in $() comsub 189 char subshare; // set when in ${..} comsub 190 char toomany; // set when out of fd's 191 char instance; // in set_instance 192 char decomma; // decimal_point=',' 193 char redir0; // redirect of 0 194 char intrace; // set when trace expands PS4 195 char *readscript; // set before reading a script 196 int subdup; // bitmask for dups of 1 197 int *inpipe; // input pipe pointer 198 int *outpipe; // output pipe pointer 199 int cpipe[3]; 200 int coutpipe; 201 int inuse_bits; 202 struct argnod *envlist; 203 struct dolnod *arglist; 204 int fn_depth; 205 int fn_reset; 206 int dot_depth; 207 int hist_depth; 208 int xargmin; 209 int xargmax; 210 int xargexit; 211 int nenv; 212 int lexsize; 213 Sflong_t sigval; 214 mode_t mask; 215 Env_t *env; 216 void *init_context; 217 void *mac_context; 218 Lex_t *lex_context; 219 struct Shell_arg *arg_context; 220 void *job_context; 221 Pathcomp_t *pathlist; 222 Pathcomp_t *defpathlist; 223 Pathcomp_t *cdpathlist; 224 char **argaddr; 225 void *optlist; 226 siginfo_ll_t **siginfo; 227 #if USE_SPAWN 228 Spawnvex_t *vex; 229 Spawnvex_t *vexp; 230 #endif // USE_SPAWN 231 struct sh_scoped global; 232 checkpt_t checkbase; 233 Shinit_f userinit; 234 Shbltin_f bltinfun; 235 Shbltin_t bltindata; 236 char *cur_line; 237 int offsets[10]; 238 Sfio_t **sftable; 239 unsigned int *fdstatus; 240 const char *pwd; 241 checkpt_t *jmpbuffer; 242 void *mktype; 243 Sfio_t *strbuf; 244 Sfio_t *strbuf2; 245 Dt_t *first_root; 246 Dt_t *prefix_root; 247 Dt_t *last_root; 248 Dt_t *prev_root; 249 Dt_t *fpathdict; 250 Dt_t *typedict; 251 Dt_t *inpool; 252 Dt_t *transdict; 253 char ifstable[256]; 254 unsigned long test; 255 Shopt_t offoptions; 256 Shopt_t glob_options; 257 Namval_t *typeinit; 258 Namfun_t nvfun; 259 char *mathnodes; 260 void *coshell; 261 char *bltin_dir; 262 char exittrap; 263 char errtrap; 264 char end_fn; 265 }; 266 267 // Flags for sh_parse. 268 #define SH_NL 1 // treat new-lines as ; 269 #define SH_EOF 2 // EOF causes syntax error 270 271 // Symbolic values for sh_iogetiop. 272 #define SH_IOCOPROCESS (-2) 273 #define SH_IOHISTFILE (-3) 274 275 // Symbolic value for sh_fdnotify. 276 #define SH_FDCLOSE (-1) 277 278 extern int sh_access(const char *, int); 279 extern int sh_chdir(const char *); 280 extern int sh_close(int); 281 extern void sh_delay(double); 282 extern int sh_dup(int); 283 extern int sh_exec(Shell_t *, const Shnode_t *, int); 284 extern int sh_fchdir(int); 285 extern int sh_fcntl(int, int, ...); 286 extern int (*sh_fdnotify(int (*)(int, int)))(int, int); 287 extern char *sh_fmtq(const char *); 288 extern char *sh_fmtqf(const char *, int, int); 289 extern Shell_t *sh_getinterp(void); 290 extern Shell_t *sh_init(int, char *[], Shinit_f); 291 extern Sfio_t *sh_iogetiop(int, int); 292 extern int sh_main(int, char *[], Shinit_f); 293 extern int sh_open(const char *, int, ...); 294 extern Shnode_t *sh_parse(Shell_t *, Sfio_t *, int); 295 extern int sh_pipe(int[]); 296 extern ssize_t sh_read(int, void *, size_t); 297 extern off_t sh_seek(int, off_t, int); 298 extern void sh_sigcheck(Shell_t *); 299 extern int sh_stat(const char *, struct stat *); 300 extern void sh_subfork(void); 301 extern mode_t sh_umask(mode_t); 302 extern int sh_waitsafe(void); 303 extern ssize_t sh_write(int, const void *, size_t); 304 305 extern Namval_t *sh_addbuiltin(Shell_t *, const char *, int (*)(int, char *[], Shbltin_t *), 306 void *); 307 extern int sh_eval(Shell_t *, Sfio_t *, int); 308 extern void sh_exit(Shell_t *, int); 309 extern Sfio_t *sh_fd2sfio(Shell_t *, int); 310 extern int sh_fun(Shell_t *, Namval_t *, Namval_t *, char *[]); 311 extern int sh_funscope(Shell_t *, int, char *[], int (*)(void *), void *, int); 312 extern Shscope_t *sh_getscope(Shell_t *, int, int); 313 extern void sh_menu(Shell_t *, Sfio_t *, int, char *[]); 314 extern Sfio_t *sh_pathopen(Shell_t *, const char *); 315 extern int sh_reinit(Shell_t *, char *[]); 316 extern int sh_run(Shell_t *, int, char *[]); 317 extern Shscope_t *sh_setscope(Shell_t *, Shscope_t *); 318 extern Sfdouble_t sh_strnum(Shell_t *, const char *, char **, int); 319 extern int sh_trap(Shell_t *, const char *, int); 320 321 #define sh_ptr(np) ((np)->nvshell) 322 323 // 324 // Direct access to sh is obsolete, use sh_getinterp() instead. 325 // 326 extern Shell_t sh; 327 328 #define SH_EXITSIG 0400 // signal exit bit 329 #define SH_EXITMASK (SH_EXITSIG - 1) // normal exit status bits 330 331 #endif // _SHELL_H 332