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 #ifndef SH_INTERACTIVE 22 /* 23 * David Korn 24 * AT&T Labs 25 * 26 * Interface definitions for shell command language 27 * 28 */ 29 30 #include <ast.h> 31 #include <cdt.h> 32 #ifdef _SH_PRIVATE 33 # include "name.h" 34 #else 35 # include <nval.h> 36 #endif /* _SH_PRIVATE */ 37 38 #define SH_VERSION 20071012 39 40 #undef NOT_USED 41 #define NOT_USED(x) (&x,1) 42 43 /* options */ 44 typedef struct 45 { 46 unsigned long v[4]; 47 } 48 Shopt_t; 49 50 typedef struct Shell_s Shell_t; 51 52 typedef void (*Shinit_f)(Shell_t*, int); 53 typedef int (*Shwait_f)(int, long, int); 54 55 union Shnode_u; 56 typedef union Shnode_u Shnode_t; 57 58 #define SH_CFLAG 0 59 #define SH_HISTORY 1 /* used also as a state */ 60 #define SH_ERREXIT 2 /* used also as a state */ 61 #define SH_VERBOSE 3 /* used also as a state */ 62 #define SH_MONITOR 4 /* used also as a state */ 63 #define SH_INTERACTIVE 5 /* used also as a state */ 64 #define SH_RESTRICTED 6 65 #define SH_XTRACE 7 66 #define SH_KEYWORD 8 67 #define SH_NOUNSET 9 68 #define SH_NOGLOB 10 69 #define SH_ALLEXPORT 11 70 #define SH_PFSH 12 71 #define SH_IGNOREEOF 13 72 #define SH_NOCLOBBER 14 73 #define SH_MARKDIRS 15 74 #define SH_BGNICE 16 75 #define SH_VI 17 76 #define SH_VIRAW 18 77 #define SH_TFLAG 19 78 #define SH_TRACKALL 20 79 #define SH_SFLAG 21 80 #define SH_NOEXEC 22 81 #define SH_GMACS 24 82 #define SH_EMACS 25 83 #define SH_PRIVILEGED 26 84 #define SH_SUBSHARE 27 /* subshell shares state with parent */ 85 #define SH_NOLOG 28 86 #define SH_NOTIFY 29 87 #define SH_DICTIONARY 30 88 #define SH_PIPEFAIL 32 89 #define SH_GLOBSTARS 33 90 #define SH_XARGS 34 91 #define SH_RC 35 92 #define SH_SHOWME 36 93 94 /* 95 * passed as flags to builtins in Nambltin_t struct when BLT_OPTIM is on 96 */ 97 #define SH_BEGIN_OPTIM 0x1 98 #define SH_END_OPTIM 0x2 99 100 /* The following type is used for error messages */ 101 102 /* error messages */ 103 extern const char e_defpath[]; 104 extern const char e_found[]; 105 extern const char e_nospace[]; 106 extern const char e_format[]; 107 extern const char e_number[]; 108 extern const char e_restricted[]; 109 extern const char e_recursive[]; 110 extern char e_version[]; 111 112 typedef struct sh_scope 113 { 114 struct sh_scope *par_scope; 115 int argc; 116 char **argv; 117 char *cmdname; 118 char *filename; 119 char *funname; 120 int lineno; 121 Dt_t *var_tree; 122 struct sh_scope *self; 123 } Shscope_t; 124 125 /* 126 * Saves the state of the shell 127 */ 128 129 struct Shell_s 130 { 131 Shopt_t options; /* set -o options */ 132 Dt_t *var_tree; /* for shell variables */ 133 Dt_t *fun_tree; /* for shell functions */ 134 Dt_t *alias_tree; /* for alias names */ 135 Dt_t *bltin_tree; /* for builtin commands */ 136 Shscope_t *topscope; /* pointer to top-level scope */ 137 int inlineno; /* line number of current input file */ 138 int exitval; /* most recent exit value */ 139 unsigned char trapnote; /* set when trap/signal is pending */ 140 char shcomp; /* set when runing shcomp */ 141 short subshell; /* set for virtual subshell */ 142 #ifdef _SH_PRIVATE 143 _SH_PRIVATE 144 #endif /* _SH_PRIVATE */ 145 }; 146 147 /* flags for sh_parse */ 148 #define SH_NL 1 /* Treat new-lines as ; */ 149 #define SH_EOF 2 /* EOF causes syntax error */ 150 151 /* symbolic values for sh_iogetiop */ 152 #define SH_IOCOPROCESS (-2) 153 #define SH_IOHISTFILE (-3) 154 155 #include <cmd.h> 156 157 /* symbolic value for sh_fdnotify */ 158 #define SH_FDCLOSE (-1) 159 160 #undef getenv /* -lshell provides its own */ 161 162 #if defined(__EXPORT__) && defined(_DLL) 163 # ifdef _BLD_shell 164 # define extern __EXPORT__ 165 # endif /* _BLD_shell */ 166 #endif /* _DLL */ 167 168 extern Dt_t *sh_bltin_tree(void); 169 extern void sh_subfork(void); 170 extern Shell_t *sh_init(int,char*[],Shinit_f); 171 extern int sh_reinit(char*[]); 172 extern int sh_eval(Sfio_t*,int); 173 extern void sh_delay(double); 174 extern void *sh_parse(Shell_t*, Sfio_t*,int); 175 extern int sh_trap(const char*,int); 176 extern int sh_fun(Namval_t*,Namval_t*, char*[]); 177 extern int sh_funscope(int,char*[],int(*)(void*),void*,int); 178 extern Sfio_t *sh_iogetiop(int,int); 179 extern int sh_main(int, char*[], Shinit_f); 180 extern int sh_run(int, char*[]); 181 extern void sh_menu(Sfio_t*, int, char*[]); 182 extern Namval_t *sh_addbuiltin(const char*, int(*)(int, char*[],void*), void*); 183 extern char *sh_fmtq(const char*); 184 extern char *sh_fmtqf(const char*, int, int); 185 extern Sfdouble_t sh_strnum(const char*, char**, int); 186 extern int sh_access(const char*,int); 187 extern int sh_close(int); 188 extern int sh_dup(int); 189 extern void sh_exit(int); 190 extern int sh_fcntl(int, int, ...); 191 extern Sfio_t *sh_fd2sfio(int); 192 extern int (*sh_fdnotify(int(*)(int,int)))(int,int); 193 extern Shell_t *sh_getinterp(void); 194 extern int sh_open(const char*, int, ...); 195 extern int sh_openmax(void); 196 extern Sfio_t *sh_pathopen(const char*); 197 extern ssize_t sh_read(int, void*, size_t); 198 extern ssize_t sh_write(int, const void*, size_t); 199 extern off_t sh_seek(int, off_t, int); 200 extern int sh_pipe(int[]); 201 extern mode_t sh_umask(mode_t); 202 extern void *sh_waitnotify(Shwait_f); 203 extern Shscope_t *sh_getscope(int,int); 204 extern Shscope_t *sh_setscope(Shscope_t*); 205 extern void sh_sigcheck(void); 206 extern unsigned long sh_isoption(int); 207 extern unsigned long sh_onoption(int); 208 extern unsigned long sh_offoption(int); 209 extern int sh_waitsafe(void); 210 extern int sh_exec(const Shnode_t*,int); 211 212 #if SHOPT_DYNAMIC 213 extern void **sh_getliblist(void); 214 #endif /* SHOPT_DYNAMIC */ 215 216 /* 217 * direct access to sh is obsolete, use sh_getinterp() instead 218 */ 219 #if !defined(_SH_PRIVATE) && defined(__IMPORT__) && !defined(_BLD_shell) 220 extern __IMPORT__ Shell_t sh; 221 #else 222 extern Shell_t sh; 223 #endif 224 225 #ifdef _DLL 226 # undef extern 227 #endif /* _DLL */ 228 229 #ifndef _SH_PRIVATE 230 # define access(a,b) sh_access(a,b) 231 # define close(a) sh_close(a) 232 # define exit(a) sh_exit(a) 233 # define fcntl(a,b,c) sh_fcntl(a,b,c) 234 # define pipe(a) sh_pipe(a) 235 # define read(a,b,c) sh_read(a,b,c) 236 # define write(a,b,c) sh_write(a,b,c) 237 # define umask(a) sh_umask(a) 238 # define dup sh_dup 239 # if _lib_lseek64 240 # define open64 sh_open 241 # define lseek64 sh_seek 242 # else 243 # define open sh_open 244 # define lseek sh_seek 245 # endif 246 #endif /* !_SH_PRIVATE */ 247 248 #define SH_SIGSET 4 249 #define SH_EXITSIG 0400 /* signal exit bit */ 250 #define SH_EXITMASK (SH_EXITSIG-1) /* normal exit status bits */ 251 #define SH_RUNPROG -1022 /* needs to be negative and < 256 */ 252 253 #endif /* SH_INTERACTIVE */ 254