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