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