14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1982-2010 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 78462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * David Korn <dgk@research.att.com> * 184887Schin * * 194887Schin ***********************************************************************/ 204887Schin #pragma prototyped 214887Schin #ifndef SH_INTERACTIVE 224887Schin /* 234887Schin * David Korn 244887Schin * AT&T Labs 254887Schin * 264887Schin * Interface definitions for shell command language 274887Schin * 284887Schin */ 294887Schin 308462SApril.Chin@Sun.COM #include <ast.h> 314887Schin #include <cdt.h> 324887Schin #ifdef _SH_PRIVATE 334887Schin # include "name.h" 344887Schin #else 354887Schin # include <nval.h> 364887Schin #endif /* _SH_PRIVATE */ 374887Schin 388462SApril.Chin@Sun.COM #define SH_VERSION 20071012 394887Schin 404887Schin #undef NOT_USED 414887Schin #define NOT_USED(x) (&x,1) 424887Schin 434887Schin /* options */ 444887Schin typedef struct 454887Schin { 464887Schin unsigned long v[4]; 474887Schin } 484887Schin Shopt_t; 494887Schin 508462SApril.Chin@Sun.COM typedef struct Shell_s Shell_t; 518462SApril.Chin@Sun.COM 528462SApril.Chin@Sun.COM typedef void (*Shinit_f)(Shell_t*, int); 534887Schin typedef int (*Shwait_f)(int, long, int); 544887Schin 554887Schin union Shnode_u; 564887Schin typedef union Shnode_u Shnode_t; 574887Schin 584887Schin #define SH_CFLAG 0 594887Schin #define SH_HISTORY 1 /* used also as a state */ 604887Schin #define SH_ERREXIT 2 /* used also as a state */ 614887Schin #define SH_VERBOSE 3 /* used also as a state */ 624887Schin #define SH_MONITOR 4 /* used also as a state */ 634887Schin #define SH_INTERACTIVE 5 /* used also as a state */ 644887Schin #define SH_RESTRICTED 6 654887Schin #define SH_XTRACE 7 664887Schin #define SH_KEYWORD 8 674887Schin #define SH_NOUNSET 9 684887Schin #define SH_NOGLOB 10 694887Schin #define SH_ALLEXPORT 11 704887Schin #define SH_PFSH 12 714887Schin #define SH_IGNOREEOF 13 724887Schin #define SH_NOCLOBBER 14 734887Schin #define SH_MARKDIRS 15 744887Schin #define SH_BGNICE 16 754887Schin #define SH_VI 17 764887Schin #define SH_VIRAW 18 774887Schin #define SH_TFLAG 19 784887Schin #define SH_TRACKALL 20 794887Schin #define SH_SFLAG 21 804887Schin #define SH_NOEXEC 22 814887Schin #define SH_GMACS 24 824887Schin #define SH_EMACS 25 834887Schin #define SH_PRIVILEGED 26 844887Schin #define SH_SUBSHARE 27 /* subshell shares state with parent */ 854887Schin #define SH_NOLOG 28 864887Schin #define SH_NOTIFY 29 874887Schin #define SH_DICTIONARY 30 884887Schin #define SH_PIPEFAIL 32 894887Schin #define SH_GLOBSTARS 33 904887Schin #define SH_XARGS 34 914887Schin #define SH_RC 35 924887Schin #define SH_SHOWME 36 934887Schin 944887Schin /* 954887Schin * passed as flags to builtins in Nambltin_t struct when BLT_OPTIM is on 964887Schin */ 974887Schin #define SH_BEGIN_OPTIM 0x1 984887Schin #define SH_END_OPTIM 0x2 994887Schin 1004887Schin /* The following type is used for error messages */ 1014887Schin 1024887Schin /* error messages */ 1034887Schin extern const char e_defpath[]; 1044887Schin extern const char e_found[]; 1054887Schin extern const char e_nospace[]; 1064887Schin extern const char e_format[]; 1074887Schin extern const char e_number[]; 1084887Schin extern const char e_restricted[]; 1094887Schin extern const char e_recursive[]; 1104887Schin extern char e_version[]; 1114887Schin 1124887Schin typedef struct sh_scope 1134887Schin { 1144887Schin struct sh_scope *par_scope; 1154887Schin int argc; 1164887Schin char **argv; 1174887Schin char *cmdname; 1184887Schin char *filename; 1198462SApril.Chin@Sun.COM char *funname; 1204887Schin int lineno; 1214887Schin Dt_t *var_tree; 1224887Schin struct sh_scope *self; 1234887Schin } Shscope_t; 1244887Schin 1254887Schin /* 1264887Schin * Saves the state of the shell 1274887Schin */ 1284887Schin 1298462SApril.Chin@Sun.COM struct Shell_s 1304887Schin { 1314887Schin Shopt_t options; /* set -o options */ 1324887Schin Dt_t *var_tree; /* for shell variables */ 1334887Schin Dt_t *fun_tree; /* for shell functions */ 1344887Schin Dt_t *alias_tree; /* for alias names */ 1354887Schin Dt_t *bltin_tree; /* for builtin commands */ 1364887Schin Shscope_t *topscope; /* pointer to top-level scope */ 1374887Schin int inlineno; /* line number of current input file */ 1384887Schin int exitval; /* most recent exit value */ 1394887Schin unsigned char trapnote; /* set when trap/signal is pending */ 1408462SApril.Chin@Sun.COM char shcomp; /* set when runing shcomp */ 14110898Sroland.mainz@nrubsig.org short subshell; /* set for virtual subshell */ 1424887Schin #ifdef _SH_PRIVATE 1434887Schin _SH_PRIVATE 1444887Schin #endif /* _SH_PRIVATE */ 1458462SApril.Chin@Sun.COM }; 1464887Schin 1474887Schin /* flags for sh_parse */ 1484887Schin #define SH_NL 1 /* Treat new-lines as ; */ 1494887Schin #define SH_EOF 2 /* EOF causes syntax error */ 1504887Schin 1514887Schin /* symbolic values for sh_iogetiop */ 1524887Schin #define SH_IOCOPROCESS (-2) 1534887Schin #define SH_IOHISTFILE (-3) 1544887Schin 1558462SApril.Chin@Sun.COM #include <cmd.h> 1568462SApril.Chin@Sun.COM 1574887Schin /* symbolic value for sh_fdnotify */ 1584887Schin #define SH_FDCLOSE (-1) 1594887Schin 1608462SApril.Chin@Sun.COM #undef getenv /* -lshell provides its own */ 1618462SApril.Chin@Sun.COM 1624887Schin #if defined(__EXPORT__) && defined(_DLL) 1634887Schin # ifdef _BLD_shell 1644887Schin # define extern __EXPORT__ 1654887Schin # endif /* _BLD_shell */ 1664887Schin #endif /* _DLL */ 1674887Schin 1684887Schin extern Dt_t *sh_bltin_tree(void); 1694887Schin extern void sh_subfork(void); 1704887Schin extern Shell_t *sh_init(int,char*[],Shinit_f); 1714887Schin extern int sh_reinit(char*[]); 1724887Schin extern int sh_eval(Sfio_t*,int); 1734887Schin extern void sh_delay(double); 1744887Schin extern void *sh_parse(Shell_t*, Sfio_t*,int); 1754887Schin extern int sh_trap(const char*,int); 1764887Schin extern int sh_fun(Namval_t*,Namval_t*, char*[]); 1774887Schin extern int sh_funscope(int,char*[],int(*)(void*),void*,int); 1784887Schin extern Sfio_t *sh_iogetiop(int,int); 1798462SApril.Chin@Sun.COM extern int sh_main(int, char*[], Shinit_f); 1808462SApril.Chin@Sun.COM extern int sh_run(int, char*[]); 1814887Schin extern void sh_menu(Sfio_t*, int, char*[]); 1824887Schin extern Namval_t *sh_addbuiltin(const char*, int(*)(int, char*[],void*), void*); 1834887Schin extern char *sh_fmtq(const char*); 1844887Schin extern char *sh_fmtqf(const char*, int, int); 1854887Schin extern Sfdouble_t sh_strnum(const char*, char**, int); 1864887Schin extern int sh_access(const char*,int); 1874887Schin extern int sh_close(int); 1884887Schin extern int sh_dup(int); 1894887Schin extern void sh_exit(int); 1904887Schin extern int sh_fcntl(int, int, ...); 1914887Schin extern Sfio_t *sh_fd2sfio(int); 1924887Schin extern int (*sh_fdnotify(int(*)(int,int)))(int,int); 1934887Schin extern Shell_t *sh_getinterp(void); 1944887Schin extern int sh_open(const char*, int, ...); 1954887Schin extern int sh_openmax(void); 1964887Schin extern Sfio_t *sh_pathopen(const char*); 1974887Schin extern ssize_t sh_read(int, void*, size_t); 1984887Schin extern ssize_t sh_write(int, const void*, size_t); 1994887Schin extern off_t sh_seek(int, off_t, int); 2004887Schin extern int sh_pipe(int[]); 2014887Schin extern mode_t sh_umask(mode_t); 2024887Schin extern void *sh_waitnotify(Shwait_f); 2034887Schin extern Shscope_t *sh_getscope(int,int); 2044887Schin extern Shscope_t *sh_setscope(Shscope_t*); 2054887Schin extern void sh_sigcheck(void); 2064887Schin extern unsigned long sh_isoption(int); 2074887Schin extern unsigned long sh_onoption(int); 2084887Schin extern unsigned long sh_offoption(int); 2094887Schin extern int sh_waitsafe(void); 2104887Schin extern int sh_exec(const Shnode_t*,int); 2114887Schin 2124887Schin #if SHOPT_DYNAMIC 2134887Schin extern void **sh_getliblist(void); 2144887Schin #endif /* SHOPT_DYNAMIC */ 2154887Schin 2164887Schin /* 2174887Schin * direct access to sh is obsolete, use sh_getinterp() instead 2184887Schin */ 2194887Schin #if !defined(_SH_PRIVATE) && defined(__IMPORT__) && !defined(_BLD_shell) 2204887Schin extern __IMPORT__ Shell_t sh; 2214887Schin #else 2224887Schin extern Shell_t sh; 2234887Schin #endif 2244887Schin 2254887Schin #ifdef _DLL 2264887Schin # undef extern 2274887Schin #endif /* _DLL */ 2284887Schin 2294887Schin #ifndef _SH_PRIVATE 2304887Schin # define access(a,b) sh_access(a,b) 2314887Schin # define close(a) sh_close(a) 2324887Schin # define exit(a) sh_exit(a) 2334887Schin # define fcntl(a,b,c) sh_fcntl(a,b,c) 2344887Schin # define pipe(a) sh_pipe(a) 2354887Schin # define read(a,b,c) sh_read(a,b,c) 2364887Schin # define write(a,b,c) sh_write(a,b,c) 2374887Schin # define umask(a) sh_umask(a) 2384887Schin # define dup sh_dup 2394887Schin # if _lib_lseek64 2404887Schin # define open64 sh_open 2414887Schin # define lseek64 sh_seek 2424887Schin # else 2434887Schin # define open sh_open 2444887Schin # define lseek sh_seek 2454887Schin # endif 2464887Schin #endif /* !_SH_PRIVATE */ 2474887Schin 2484887Schin #define SH_SIGSET 4 2494887Schin #define SH_EXITSIG 0400 /* signal exit bit */ 2504887Schin #define SH_EXITMASK (SH_EXITSIG-1) /* normal exit status bits */ 2514887Schin #define SH_RUNPROG -1022 /* needs to be negative and < 256 */ 2524887Schin 2534887Schin #endif /* SH_INTERACTIVE */ 254