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