1 /***********************************************************************
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 *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * David Korn <dgk@research.att.com> *
19 ***********************************************************************/
25 * Shell interface private definitions
28 #ifndef defs_h_defined
29 #define defs_h_defined
34 #include "FEATURE/externs"
35 #include "FEATURE/options"
47 #define pointerof(x) ((void*)((char*)0+(x)))
50 #define Empty ((char*)(e_sptbnl+3))
52 #define env_change() (++ast.env_serial)
57 # define sh_envput(e,p) env_change()
58 # define env_delete(e,p) env_change()
61 extern char* sh_getenv(const char*);
62 extern char* sh_setenviron(const char*);
65 * note that the first few fields have to be the same as for
66 * Shscoped_t in <shell.h>
72 struct sh_scoped
*prevst
; /* pointer to previous state */
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 */
90 int32_t tmout
; /* value for TMOUT */
94 unsigned short trapmax
;
95 char *trap
[SH_DEBUGTRAP
+1];
99 struct Ufunction
*real_fun
; /* current 'function name' function */
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 */
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 */ \
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 */ \
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 */ \
156 pid_t spid; /* subshell process id */ \
158 int32_t ppid; /* parent process id of shell */ \
160 int sigmax; /* maximum number of signals */ \
162 unsigned char *sigflag; /* pointer to signal states */ \
170 char used_pos; /* used postional parameter */\
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 */ \
185 struct argnod *envlist; \
186 struct dolnod *arglist; \
198 void *init_context; \
209 struct sh_scoped global; \
210 struct checkpt checkbase; \
212 Shbltin_f bltinfun; \
213 Shbltin_t bltindata; \
214 Shwait_f waitevent; \
217 char **login_files; \
220 unsigned char *fdstatus; \
222 History_t *hist_ptr; \
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; \
239 struct Regress_s*regress;
245 /* error exits from various parts of shell */
246 #define NIL(type) ((type)0)
248 #define new_of(type,x) ((type*)malloc((unsigned)sizeof(type)+(x)))
250 #define exitset() (sh.savexit=sh.exitval)
253 #define SH_DICT (void*)e_dict
256 #ifndef SH_CMDLIB_DIR
257 #define SH_CMDLIB_DIR "/opt/ast/bin"
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 */
278 #define SH_BRACEEXPAND 42
280 #define SH_MULTILINE 47
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
289 #define SH_ID "ksh" /* ksh id */
290 #define SH_STD "sh" /* standard sh id */
292 /* defines for sh_type() */
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
302 # ifndef SHOPT_HISTEXPAND
303 # define SHOPT_HISTEXPAND 1
306 * define for all the bash options
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
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
343 # define PIPE_BUF 512
346 #if SHOPT_PFSH && ( !_lib_getexecuser || !_lib_free_execattr )
352 #define SH_READEVAL 0x4000 /* for sh_eval */
353 #define SH_FUNEVAL 0x10000 /* for sh_eval for function load */
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);
372 extern void sh_envput(Env_t
*, Namval_t
*);
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);
406 #ifndef ERROR_dictionary
407 # define ERROR_dictionary(s) (s)
409 #define sh_translate(s) _sh_translate(ERROR_dictionary(s))
411 #define WBITS (sizeof(long)*8)
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)
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))
429 #define sh_sigcheck() do{if(sh.trapnote&SH_SIGSET)sh_exit(SH_EXITSIG);} while(0)
431 extern int32_t sh_mailchk
;
432 extern const char e_dict
[];
434 /* sh_printopts() mode flags -- set --[no]option by default */
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 */
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)]++)
462 #endif /* SHOPT_STATS */