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