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