xref: /titanic_44/usr/src/lib/libshell/common/include/defs.h (revision 8475e04352e630e4bd0f59a283286ee2475a14ce)
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		subshare;	/* set when in ${..} comsub */ \
170 	char		*readscript;	/* set before reading a script */ \
171 	int		*inpipe;	/* input pipe pointer */ \
172 	int		*outpipe;	/* output pipe pointer */ \
173 	int		cpipe[2]; \
174 	int		coutpipe; \
175 	int		inuse_bits; \
176 	struct argnod	*envlist; \
177 	struct dolnod	*arglist; \
178 	int		fn_depth; \
179 	int		fn_reset; \
180 	int		dot_depth; \
181 	int		hist_depth; \
182 	int		xargmin; \
183 	int		xargmax; \
184 	int		xargexit; \
185 	int		nenv; \
186 	mode_t		mask; \
187 	long		nforks; \
188 	Env_t		*env; \
189 	void		*init_context; \
190 	void		*mac_context; \
191 	void		*lex_context; \
192 	void		*arg_context; \
193 	void		*ed_context; \
194 	void		*job_context; \
195 	void		*pathlist; \
196 	void		*defpathlist; \
197 	void		*cdpathlist; \
198 	char		**argaddr; \
199 	void		*optlist; \
200 	struct sh_scoped global; \
201 	struct checkpt	checkbase; \
202 	Shinit_f	userinit; \
203 	Shbltin_f	bltinfun; \
204 	Shbltin_t	bltindata; \
205 	Shwait_f	waitevent; \
206 	char		*cur_line; \
207 	char		*rcfile; \
208 	char		**login_files; \
209 	int		offsets[10]; \
210 	Sfio_t		**sftable; \
211 	unsigned char	*fdstatus; \
212 	const char	*pwd; \
213 	History_t	*hist_ptr; \
214 	void		*jmpbuffer; \
215 	void		*mktype; \
216 	Sfio_t		*strbuf; \
217 	Sfio_t		*strbuf2; \
218 	Dt_t		*last_root; \
219 	Dt_t		*prev_root; \
220 	Dt_t		*fpathdict; \
221 	Dt_t		*typedict; \
222 	char		ifstable[256]; \
223 	unsigned char	sigruntime[2]; \
224 	unsigned long	test; \
225 	Shopt_t		offoptions; \
226 	Shopt_t		glob_options; \
227 	Namval_t	*typeinit; \
228 	int		*stats; \
229 	Namfun_t	nvfun;
230 
231 #include	<shell.h>
232 
233 
234 /* error exits from various parts of shell */
235 #define	NIL(type)	((type)0)
236 
237 #define new_of(type,x)	((type*)malloc((unsigned)sizeof(type)+(x)))
238 
239 #define exitset()	(sh.savexit=sh.exitval)
240 
241 #ifndef SH_DICT
242 #define SH_DICT		(void*)e_dict
243 #endif
244 
245 #ifndef SH_CMDLIB_DIR
246 #define SH_CMDLIB_DIR	"/opt/ast/bin"
247 #endif
248 
249 /* states */
250 /* low numbered states are same as options */
251 #define SH_NOFORK	0	/* set when fork not necessary, not a state */
252 #define SH_COMPLETE	0	/* set for command completion */
253 #define	SH_FORKED	7	/* set when process has been forked */
254 #define	SH_PROFILE	8	/* set when processing profiles */
255 #define SH_NOALIAS	9	/* do not expand non-exported aliases */
256 #define SH_NOTRACK	10	/* set to disable sftrack() function */
257 #define SH_STOPOK	11	/* set for stopable builtins */
258 #define SH_GRACE	12	/* set for timeout grace period */
259 #define SH_TIMING	13	/* set while timing pipelines */
260 #define SH_DEFPATH	14	/* set when using default path */
261 #define SH_INIT		15	/* set when initializing the shell */
262 #define SH_TTYWAIT	16	/* waiting for keyboard input */
263 #define	SH_FCOMPLETE	17	/* set for filename completion */
264 #define	SH_PREINIT	18	/* set with SH_INIT before parsing options */
265 
266 #define SH_BASH			41
267 #define SH_BRACEEXPAND		42
268 #define SH_POSIX		46
269 #define SH_MULTILINE    	47
270 
271 #define SH_NOPROFILE		78
272 #define SH_NOUSRPROFILE		79
273 #define SH_LOGIN_SHELL		67
274 #define SH_COMMANDLINE		0x100
275 #define SH_BASHEXTRA		0x200
276 #define SH_BASHOPT		0x400
277 
278 #define SH_ID			"ksh"	/* ksh id */
279 #define SH_STD			"sh"	/* standard sh id */
280 
281 /* defines for sh_type() */
282 
283 #define SH_TYPE_SH		001
284 #define SH_TYPE_KSH		002
285 #define SH_TYPE_BASH		004
286 #define SH_TYPE_LOGIN		010
287 #define SH_TYPE_PROFILE		020
288 #define SH_TYPE_RESTRICTED	040
289 
290 #if SHOPT_BASH
291 #   ifndef SHOPT_HISTEXPAND
292 #	define SHOPT_HISTEXPAND	1
293 #   endif
294 /*
295  *  define for all the bash options
296  */
297 #   define SH_CDABLE_VARS	51
298 #   define SH_CDSPELL		52
299 #   define SH_CHECKHASH		53
300 #   define SH_CHECKWINSIZE	54
301 #   define SH_CMDHIST		55
302 #   define SH_DOTGLOB		56
303 #   define SH_EXECFAIL		57
304 #   define SH_EXPAND_ALIASES	58
305 #   define SH_EXTGLOB		59
306 #   define SH_HOSTCOMPLETE	63
307 #   define SH_HUPONEXIT		64
308 #   define SH_INTERACTIVE_COMM	65
309 #   define SH_LITHIST		66
310 #   define SH_MAILWARN		68
311 #   define SH_NOEMPTYCMDCOMPL	69
312 #   define SH_NOCASEGLOB	70
313 #   define SH_NULLGLOB		71
314 #   define SH_PHYSICAL		45
315 #   define SH_PROGCOMP		72
316 #   define SH_PROMPTVARS	73
317 #   define SH_RESTRICTED2	74
318 #   define SH_SHIFT_VERBOSE	75
319 #   define SH_SOURCEPATH	76
320 #   define SH_XPG_ECHO		77
321 #endif
322 
323 #if SHOPT_HISTEXPAND
324 #   define SH_HISTAPPEND	60
325 #   define SH_HISTEXPAND	43
326 #   define SH_HISTORY2		44
327 #   define SH_HISTREEDIT	61
328 #   define SH_HISTVERIFY	62
329 #endif
330 
331 #ifndef PIPE_BUF
332 #   define PIPE_BUF		512
333 #endif
334 
335 #define MATCH_MAX		64
336 
337 #define SH_READEVAL		0x4000	/* for sh_eval */
338 
339 extern Shell_t		*nv_shell(Namval_t*);
340 extern int		sh_addlib(void*);
341 extern void		sh_applyopts(Shell_t*,Shopt_t);
342 extern char 		**sh_argbuild(Shell_t*,int*,const struct comnod*,int);
343 extern struct dolnod	*sh_argfree(Shell_t *, struct dolnod*,int);
344 extern struct dolnod	*sh_argnew(Shell_t*,char*[],struct dolnod**);
345 extern void 		*sh_argopen(Shell_t*);
346 extern void 		sh_argreset(Shell_t*,struct dolnod*,struct dolnod*);
347 extern Namval_t		*sh_assignok(Namval_t*,int);
348 extern struct dolnod	*sh_arguse(Shell_t*);
349 extern char		*sh_checkid(char*,char*);
350 extern int		sh_debug(Shell_t *shp,const char*,const char*,const char*,char *const[],int);
351 extern int 		sh_echolist(Sfio_t*, int, char**);
352 extern struct argnod	*sh_endword(Shell_t*,int);
353 extern char 		**sh_envgen(void);
354 #if SHOPT_ENV
355 extern void 		sh_envput(Env_t*, Namval_t*);
356 #endif
357 extern void 		sh_envnolocal(Namval_t*,void*);
358 extern Sfdouble_t	sh_arith(const char*);
359 extern void		*sh_arithcomp(char*);
360 extern pid_t 		sh_fork(int,int*);
361 extern pid_t		_sh_fork(pid_t, int ,int*);
362 extern char 		*sh_mactrim(Shell_t*,char*,int);
363 extern int 		sh_macexpand(Shell_t*,struct argnod*,struct argnod**,int);
364 extern int		sh_macfun(Shell_t*,const char*,int);
365 extern void 		sh_machere(Shell_t*,Sfio_t*, Sfio_t*, char*);
366 extern void 		*sh_macopen(Shell_t*);
367 extern char 		*sh_macpat(Shell_t*,struct argnod*,int);
368 extern char 		*sh_mactry(Shell_t*,char*);
369 extern void		sh_printopts(Shopt_t,int,Shopt_t*);
370 extern int 		sh_readline(Shell_t*,char**,int,int,long);
371 extern Sfio_t		*sh_sfeval(char*[]);
372 extern void		sh_setmatch(const char*,int,int,int[]);
373 extern Dt_t		*sh_subaliastree(int);
374 extern void             sh_scope(Shell_t*, struct argnod*, int);
375 extern Namval_t		*sh_scoped(Shell_t*, Namval_t*);
376 extern Dt_t		*sh_subfuntree(int);
377 extern void		sh_subjobcheck(pid_t);
378 extern int		sh_subsavefd(int);
379 extern void		sh_subtmpfile(int);
380 extern char 		*sh_substitute(const char*,const char*,char*);
381 extern const char	*_sh_translate(const char*);
382 extern int		sh_trace(char*[],int);
383 extern void		sh_trim(char*);
384 extern int		sh_type(const char*);
385 extern void             sh_unscope(Shell_t*);
386 extern void		sh_utol(const char*, char*);
387 extern int 		sh_whence(char**,int);
388 
389 #ifndef ERROR_dictionary
390 #   define ERROR_dictionary(s)	(s)
391 #endif
392 #define sh_translate(s)	_sh_translate(ERROR_dictionary(s))
393 
394 #define WBITS		(sizeof(long)*8)
395 #define WMASK		(0xff)
396 
397 #define is_option(s,x)	((s)->v[((x)&WMASK)/WBITS] & (1L << ((x) % WBITS)))
398 #define on_option(s,x)	((s)->v[((x)&WMASK)/WBITS] |= (1L << ((x) % WBITS)))
399 #define off_option(s,x)	((s)->v[((x)&WMASK)/WBITS] &= ~(1L << ((x) % WBITS)))
400 #define sh_isoption(x)	is_option(&sh.options,x)
401 #define sh_onoption(x)	on_option(&sh.options,x)
402 #define sh_offoption(x)	off_option(&sh.options,x)
403 
404 
405 #define sh_state(x)	( 1<<(x))
406 #define	sh_isstate(x)	(sh.st.states&sh_state(x))
407 #define	sh_onstate(x)	(sh.st.states |= sh_state(x))
408 #define	sh_offstate(x)	(sh.st.states &= ~sh_state(x))
409 #define	sh_getstate()	(sh.st.states)
410 #define	sh_setstate(x)	(sh.st.states = (x))
411 
412 #define sh_sigcheck() do{if(sh.trapnote&SH_SIGSET)sh_exit(SH_EXITSIG);} while(0)
413 
414 extern int32_t		sh_mailchk;
415 extern const char	e_dict[];
416 
417 /* sh_printopts() mode flags -- set --[no]option by default */
418 
419 #define PRINT_VERBOSE	0x01	/* option on|off list		*/
420 #define PRINT_ALL	0x02	/* list unset options too	*/
421 #define PRINT_NO_HEADER	0x04	/* omit listing header		*/
422 #define PRINT_SHOPT	0x08	/* shopt -s|-u			*/
423 #define PRINT_TABLE	0x10	/* table of all options		*/
424 
425 #ifdef SHOPT_STATS
426     /* performance statistics */
427 #   define	STAT_ARGHITS	0
428 #   define	STAT_ARGEXPAND	1
429 #   define	STAT_COMSUB	2
430 #   define	STAT_FORKS	3
431 #   define	STAT_FUNCT	4
432 #   define	STAT_GLOBS	5
433 #   define	STAT_READS	6
434 #   define	STAT_NVHITS	7
435 #   define	STAT_NVOPEN	8
436 #   define	STAT_PATHS	9
437 #   define	STAT_SVFUNCT	10
438 #   define	STAT_SCMDS	11
439 #   define	STAT_SPAWN	12
440 #   define	STAT_SUBSHELL	13
441     extern const Shtable_t shtab_stats[];
442 #   define sh_stats(x)	(sh.stats[(x)]++)
443 #else
444 #   define sh_stats(x)
445 #endif /* SHOPT_STATS */
446 
447 
448 #endif
449