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