xref: /titanic_50/usr/src/lib/libshell/common/include/defs.h (revision b88e3eef9e58fa5cfbf3ff1075b84a2b87683634)
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 /*
22  * David Korn
23  * AT&T Labs
24  *
25  * Shell interface private definitions
26  *
27  */
28 
29 #include	<ast.h>
30 #include	<sfio.h>
31 #include	<error.h>
32 #include	"FEATURE/options"
33 #include	<cdt.h>
34 #include	<history.h>
35 #include	"fault.h"
36 #include	"argnod.h"
37 
38 #ifndef pointerof
39 #define pointerof(x)		((void*)((char*)0+(x)))
40 #endif
41 
42 #define	env_change()		(++ast.env_serial)
43 #if SHOPT_ENV
44 #   include	<env.h>
45 #else
46 #   define Env_t		void
47 #   define sh_envput(e,p)	env_change()
48 #   define env_delete(e,p)	env_change()
49 #endif
50 
51 /*
52  * note that the first few fields have to be the same as for
53  * Shscoped_t in <shell.h>
54  */
55 struct sh_scoped
56 {
57 	struct sh_scoped *prevst;	/* pointer to previous state */
58 	int		dolc;
59 	char		**dolv;
60 	char		*cmdname;
61 	char		*filename;
62 	int		lineno;
63 	Dt_t		*save_tree;	/* var_tree for calling function */
64 	struct sh_scoped *self;		/* pointer to copy of this scope*/
65 	Dt_t		*var_local;	/* local level variables for name() */
66 	struct slnod	*staklist;	/* link list of function stacks */
67 	int		states;
68 	int		breakcnt;
69 	int		execbrk;
70 	int		loopcnt;
71 	int		firstline;
72 	int32_t		optindex;
73 	int32_t		optnum;
74 	int32_t		tmout;		/* value for TMOUT */
75 	short		optchar;
76 	short		opterror;
77 	int		ioset;
78 	unsigned short	trapmax;
79 	char		*trap[SH_DEBUGTRAP+1];
80 	char		**trapcom;
81 	char		**otrapcom;
82 	void		*timetrap;
83 };
84 
85 struct limits
86 {
87 	long		arg_max;	/* max arg+env exec() size */
88 	int		open_max;	/* maximum number of file descriptors */
89 	int		clk_tck;	/* number of ticks per second */
90 	int		child_max;	/* maxumum number of children */
91 	int		ngroups_max;	/* maximum number of process groups */
92 	unsigned char	posix_version;	/* posix version number */
93 	unsigned char	posix_jobcontrol;/* non-zero for job control systems */
94 	unsigned char	fs3d;		/* non-zero for 3-d file system */
95 };
96 
97 #define _SH_PRIVATE \
98 	struct sh_scoped st;		/* scoped information */ \
99 	struct limits	lim;		/* run time limits */ \
100 	Sfio_t		*heredocs;	/* current here-doc temp file */ \
101 	Sfio_t		*funlog;	/* for logging function definitions */ \
102 	int		**fdptrs;	/* pointer to file numbers */ \
103 	int		savexit; \
104 	char		*lastarg; \
105 	char		*lastpath;	/* last alsolute path found */ \
106 	int		path_err;	/* last error on path search */ \
107 	Dt_t		*track_tree;	/* for tracked aliases*/ \
108 	Namval_t	*bltin_nodes;	/* pointer to built-in variables */ \
109 	Dt_t		*var_base;	/* global level variables */ \
110 	Namval_t	*namespace;	/* current active namespace*/ \
111 	Namval_t	*last_table;	/* last table used in last nv_open  */ \
112 	Sfio_t		*outpool;	/* ouput stream pool */ \
113 	long		timeout;	/* read timeout */ \
114 	short		curenv;		/* current subshell number */ \
115 	short		jobenv;		/* subshell number for jobs */ \
116 	int		nextprompt;	/* next prompt is PS<nextprompt> */ \
117 	Namval_t	*bltin_cmds;	/* pointer to built-in commands */ \
118 	Namval_t	*posix_fun;	/* points to last name() function */ \
119 	int		infd;		/* input file descriptor */ \
120 	char		*outbuff;	/* pointer to output buffer */ \
121 	char		*errbuff;	/* pointer to stderr buffer */ \
122 	char		*prompt;	/* pointer to prompt string */ \
123 	char		*shname;	/* shell name */ \
124 	char		*shpath;	/* path name of shell */ \
125 	char		*user;		/* name of real user for pfsh */ \
126 	char		*comdiv;	/* points to sh -c argument */ \
127 	char		*prefix;	/* prefix for compound assignment */ \
128 	sigjmp_buf	*jmplist;	/* longjmp return stack */ \
129 	char		**sigmsg;	/* points to signal messages */ \
130 	int		oldexit; \
131 	uid_t 		userid,euserid;	/* real and effective user id */ \
132 	gid_t 		groupid,egroupid;/* real and effective group id */ \
133 	pid_t		pid;		/* process id of shell */ \
134 	pid_t		bckpid;		/* background process id */ \
135 	pid_t		cpid; \
136 	int32_t		ppid;		/* parent process id of shell */ \
137 	int		topfd; \
138 	int		sigmax;		/* maximum number of signals */ \
139 	int		savesig; \
140 	unsigned char	*sigflag;	/* pointer to signal states */ \
141 	char		intrap; \
142 	char		login_sh; \
143 	char		lastbase; \
144 	char		forked;	\
145 	char		binscript; \
146 	char		deftype; \
147 	char		used_pos;	/* used postional parameter */\
148 	unsigned char	lastsig;	/* last signal received */ \
149 	char		*readscript;	/* set before reading a script */ \
150 	int		*inpipe;	/* input pipe pointer */ \
151 	int		*outpipe;	/* output pipe pointer */ \
152 	int		cpipe[2]; \
153 	int		coutpipe; \
154 	int		inuse_bits; \
155 	struct argnod	*envlist; \
156 	struct dolnod	*arglist; \
157 	int		fn_depth; \
158 	int		dot_depth; \
159 	int		hist_depth; \
160 	int		xargmin; \
161 	int		xargmax; \
162 	int		xargexit; \
163 	mode_t		mask; \
164 	long		nforks; \
165 	Env_t		*env; \
166 	void		*init_context; \
167 	void		*mac_context; \
168 	void		*lex_context; \
169 	void		*arg_context; \
170 	void		*ed_context; \
171 	void		*job_context; \
172 	void		*pathlist; \
173 	void		*defpathlist; \
174 	void		*cdpathlist; \
175 	char		**argaddr; \
176 	void		*optlist; \
177 	int		optcount ; \
178 	struct sh_scoped global; \
179 	struct checkpt	checkbase; \
180 	Shinit_f	userinit; \
181 	Shbltin_f	bltinfun; \
182 	Shwait_f	waitevent; \
183 	char		*cur_line; \
184 	char		*rcfile; \
185 	char		**login_files; \
186 	short		offsets[10]; \
187 	Sfio_t		**sftable; \
188 	unsigned char	*fdstatus; \
189 	const char	*pwd; \
190 	History_t	*hist_ptr; \
191 	char		universe; \
192 	void		*jmpbuffer; \
193 	void		*mktype; \
194 	Sfio_t		*strbuf; \
195 	Dt_t		*last_root; \
196 	char		ifstable[256]; \
197 	Shopt_t		offoptions;
198 
199 #include	<shell.h>
200 
201 
202 /* error exits from various parts of shell */
203 #define	NIL(type)	((type)0)
204 
205 #define new_of(type,x)	((type*)malloc((unsigned)sizeof(type)+(x)))
206 
207 #define exitset()	(sh.savexit=sh.exitval)
208 
209 #ifndef SH_DICT
210 #define SH_DICT		(void*)e_dict
211 #endif
212 
213 #ifndef SH_CMDLIB_DIR
214 #define SH_CMDLIB_DIR	"/opt/ast/bin"
215 #endif
216 
217 /* states */
218 /* low numbered states are same as options */
219 #define SH_NOFORK	0	/* set when fork not necessary, not a state */
220 #define SH_COMPLETE	0	/* set for command completion */
221 #define	SH_FORKED	7	/* set when process has been forked */
222 #define	SH_PROFILE	8	/* set when processing profiles */
223 #define SH_NOALIAS	9	/* do not expand non-exported aliases */
224 #define SH_NOTRACK	10	/* set to disable sftrack() function */
225 #define SH_STOPOK	11	/* set for stopable builtins */
226 #define SH_GRACE	12	/* set for timeout grace period */
227 #define SH_TIMING	13	/* set while timing pipelines */
228 #define SH_DEFPATH	14	/* set when using default path */
229 #define SH_INIT		15	/* set when initializing the shell */
230 #define SH_TTYWAIT	16	/* waiting for keyboard input */
231 #define	SH_FCOMPLETE	17	/* set for filename completion */
232 #define	SH_PREINIT	18	/* set with SH_INIT before parsing options */
233 
234 #define SH_BASH			41
235 #define SH_BRACEEXPAND		42
236 #define SH_POSIX		46
237 #define SH_MULTILINE    	47
238 
239 #define SH_NOPROFILE		78
240 #define SH_NOUSRPROFILE		79
241 #define SH_LOGIN_SHELL		67
242 #define SH_COMMANDLINE		0x100
243 #define SH_BASHEXTRA		0x200
244 #define SH_BASHOPT		0x400
245 
246 #define SH_ID			"ksh"	/* ksh id */
247 #define SH_STD			"sh"	/* standard sh id */
248 
249 /* defines for sh_type() */
250 
251 #define SH_TYPE_SH		001
252 #define SH_TYPE_KSH		002
253 #define SH_TYPE_BASH		004
254 #define SH_TYPE_LOGIN		010
255 #define SH_TYPE_PROFILE		020
256 #define SH_TYPE_RESTRICTED	040
257 
258 #if SHOPT_BASH
259 #   ifndef SHOPT_HISTEXPAND
260 #	define SHOPT_HISTEXPAND	1
261 #   endif
262 /*
263  *  define for all the bash options
264  */
265 #   define SH_CDABLE_VARS	51
266 #   define SH_CDSPELL		52
267 #   define SH_CHECKHASH		53
268 #   define SH_CHECKWINSIZE	54
269 #   define SH_CMDHIST		55
270 #   define SH_DOTGLOB		56
271 #   define SH_EXECFAIL		57
272 #   define SH_EXPAND_ALIASES	58
273 #   define SH_EXTGLOB		59
274 #   define SH_HOSTCOMPLETE	63
275 #   define SH_HUPONEXIT		64
276 #   define SH_INTERACTIVE_COMM	65
277 #   define SH_LITHIST		66
278 #   define SH_MAILWARN		68
279 #   define SH_NOEMPTYCMDCOMPL	69
280 #   define SH_NOCASEGLOB	70
281 #   define SH_NULLGLOB		71
282 #   define SH_PHYSICAL		45
283 #   define SH_PROGCOMP		72
284 #   define SH_PROMPTVARS	73
285 #   define SH_RESTRICTED2	74
286 #   define SH_SHIFT_VERBOSE	75
287 #   define SH_SOURCEPATH	76
288 #   define SH_XPG_ECHO		77
289 #endif
290 
291 #if SHOPT_HISTEXPAND
292 #   define SH_HISTAPPEND	60
293 #   define SH_HISTEXPAND	43
294 #   define SH_HISTORY2		44
295 #   define SH_HISTREEDIT	61
296 #   define SH_HISTVERIFY	62
297 #endif
298 
299 #ifndef PIPE_BUF
300 #   define PIPE_BUF		512
301 #endif
302 
303 #define MATCH_MAX		64
304 
305 extern int		sh_addlib(void*);
306 extern void 		*sh_argopen(Shell_t*);
307 extern Namval_t		*sh_assignok(Namval_t*,int);
308 extern char		*sh_checkid(char*,char*);
309 extern int		sh_debug(const char*,const char*,const char*,char *const[],int);
310 extern int 		sh_echolist(Sfio_t*, int, char**);
311 extern struct argnod	*sh_endword(int);
312 extern char 		**sh_envgen(void);
313 #if SHOPT_ENV
314 extern void 		sh_envput(Env_t*, Namval_t*);
315 #endif
316 extern void 		sh_envnolocal(Namval_t*,void*);
317 extern Sfdouble_t	sh_arith(const char*);
318 extern void		*sh_arithcomp(char*);
319 extern pid_t 		sh_fork(int,int*);
320 extern pid_t		_sh_fork(pid_t, int ,int*);
321 extern char 		*sh_mactrim(char*,int);
322 extern int 		sh_macexpand(struct argnod*,struct argnod**,int);
323 extern void 		sh_machere(Sfio_t*, Sfio_t*, char*);
324 extern void 		*sh_macopen(Shell_t*);
325 extern char 		*sh_macpat(struct argnod*,int);
326 extern char 		*sh_mactry(char*);
327 extern void		sh_printopts(Shopt_t,int,Shopt_t*);
328 extern int 		sh_readline(Shell_t*,char**,int,int,long);
329 extern Sfio_t		*sh_sfeval(char*[]);
330 extern void		sh_setmatch(const char*,int,int,int[]);
331 extern Dt_t		*sh_subaliastree(int);
332 extern Dt_t		*sh_subfuntree(int);
333 extern int		sh_subsavefd(int);
334 extern void		sh_subtmpfile(void);
335 extern char 		*sh_substitute(const char*,const char*,char*);
336 extern const char	*_sh_translate(const char*);
337 extern int		sh_trace(char*[],int);
338 extern void		sh_trim(char*);
339 extern int		sh_type(const char*);
340 extern void		sh_utol(const char*, char*);
341 extern int 		sh_whence(char**,int);
342 
343 #ifndef ERROR_dictionary
344 #   define ERROR_dictionary(s)	(s)
345 #endif
346 #define sh_translate(s)	_sh_translate(ERROR_dictionary(s))
347 
348 #define WBITS		(sizeof(long)*8)
349 #define WMASK		(0xff)
350 
351 #define is_option(s,x)	((s)->v[((x)&WMASK)/WBITS] & (1L << ((x) % WBITS)))
352 #define on_option(s,x)	((s)->v[((x)&WMASK)/WBITS] |= (1L << ((x) % WBITS)))
353 #define off_option(s,x)	((s)->v[((x)&WMASK)/WBITS] &= ~(1L << ((x) % WBITS)))
354 #define sh_isoption(x)	is_option(&sh.options,x)
355 #define sh_onoption(x)	on_option(&sh.options,x)
356 #define sh_offoption(x)	off_option(&sh.options,x)
357 
358 
359 #define sh_state(x)	( 1<<(x))
360 #define	sh_isstate(x)	(sh.st.states&sh_state(x))
361 #define	sh_onstate(x)	(sh.st.states |= sh_state(x))
362 #define	sh_offstate(x)	(sh.st.states &= ~sh_state(x))
363 #define	sh_getstate()	(sh.st.states)
364 #define	sh_setstate(x)	(sh.st.states = (x))
365 
366 #define sh_sigcheck() do{if(sh.trapnote&SH_SIGSET)sh_exit(SH_EXITSIG);} while(0)
367 
368 extern int32_t		sh_mailchk;
369 extern const char	e_dict[];
370 
371 /* sh_printopts() mode flags -- set --[no]option by default */
372 
373 #define PRINT_VERBOSE	0x01	/* option on|off list		*/
374 #define PRINT_ALL	0x02	/* list unset iptions too	*/
375 #define PRINT_NO_HEADER	0x04	/* omit listing header		*/
376 #define PRINT_SHOPT	0x08	/* shopt -s|-u			*/
377 #define PRINT_TABLE	0x10	/* table of all options		*/
378