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