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