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