1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                  David Korn <dgk@research.att.com>                   *
18*4887Schin *                                                                      *
19*4887Schin ***********************************************************************/
20*4887Schin #pragma prototyped
21*4887Schin #ifndef _NV_PRIVATE
22*4887Schin /*
23*4887Schin  * This is the implementation header file for name-value pairs
24*4887Schin  */
25*4887Schin 
26*4887Schin #define _NV_PRIVATE	\
27*4887Schin 	Namfun_t	*nvfun;		/* pointer to trap functions */ \
28*4887Schin 	union Value	nvalue; 	/* value field */ \
29*4887Schin 	char		*nvenv;		/* pointer to environment name */
30*4887Schin 
31*4887Schin #include	<ast.h>
32*4887Schin #include	<cdt.h>
33*4887Schin #include	"shtable.h"
34*4887Schin 
35*4887Schin /* Nodes can have all kinds of values */
36*4887Schin union Value
37*4887Schin {
38*4887Schin 	const char		*cp;
39*4887Schin 	int			*ip;
40*4887Schin 	char			c;
41*4887Schin 	int			i;
42*4887Schin 	unsigned int		u;
43*4887Schin 	int32_t			*lp;
44*4887Schin 	Sflong_t		*llp;	/* for long long arithmetic */
45*4887Schin 	int16_t			s;
46*4887Schin 	double			*dp;	/* for floating point arithmetic */
47*4887Schin 	Sfdouble_t		*ldp;	/* for long floating point arithmetic */
48*4887Schin 	struct Namarray		*array;	/* for array node */
49*4887Schin 	struct Namval		*np;	/* for Namval_t node */
50*4887Schin 	union Value		*up;	/* for indirect node */
51*4887Schin 	struct Ufunction 	*rp;	/* shell user defined functions */
52*4887Schin 	struct Namfun		*funp;	/* discipline pointer */
53*4887Schin 	struct Namref		*nrp;	/* name reference */
54*4887Schin 	int			(*bfp)(int,char*[],void*);/* builtin entry point function pointer */
55*4887Schin };
56*4887Schin 
57*4887Schin #include	"nval.h"
58*4887Schin 
59*4887Schin /* used for arrays */
60*4887Schin 
61*4887Schin #define ARRAY_MAX 	(1L<<ARRAY_BITS) /* maximum number of elements in an array */
62*4887Schin #define ARRAY_MASK	(ARRAY_MAX-1)	/* For index values */
63*4887Schin 
64*4887Schin #define ARRAY_INCR	32	/* number of elements to grow when array
65*4887Schin 				   bound exceeded.  Must be a power of 2 */
66*4887Schin #define ARRAY_FILL	(8L<<ARRAY_BITS)	/* used with nv_putsub() */
67*4887Schin #define ARRAY_NOCLONE	(16L<<ARRAY_BITS)	/* do not clone array disc */
68*4887Schin #define ARRAY_NOCHILD   (32L<<ARRAY_BITS)	/* skip compound arrays */
69*4887Schin #define ARRAY_SETSUB	(64L<<ARRAY_BITS)	/* set subscript */
70*4887Schin #define NV_ASETSUB	8			/* set subscript */
71*4887Schin 
72*4887Schin /* These flags are used as options to array_get() */
73*4887Schin #define ARRAY_ASSIGN	0
74*4887Schin #define ARRAY_LOOKUP	1
75*4887Schin #define ARRAY_DELETE	2
76*4887Schin 
77*4887Schin 
78*4887Schin struct Namref
79*4887Schin {
80*4887Schin 	Namval_t	*np;
81*4887Schin 	Namval_t	*table;
82*4887Schin 	Dt_t		*root;
83*4887Schin 	char		*sub;
84*4887Schin };
85*4887Schin 
86*4887Schin /* This describes a user shell function node */
87*4887Schin struct Ufunction
88*4887Schin {
89*4887Schin 	int	*ptree;			/* address of parse tree */
90*4887Schin 	int	lineno;			/* line number of function start */
91*4887Schin 	off_t	hoffset;		/* offset into source or history file */
92*4887Schin 	Namval_t *nspace;		/* pointer to name space */
93*4887Schin 	char	*fname;			/* file name where function defined */
94*4887Schin };
95*4887Schin 
96*4887Schin #ifndef ARG_RAW
97*4887Schin     struct argnod;
98*4887Schin #endif /* !ARG_RAW */
99*4887Schin 
100*4887Schin /* attributes of Namval_t items */
101*4887Schin 
102*4887Schin /* The following attributes are for internal use */
103*4887Schin #define NV_NOCHANGE	(NV_EXPORT|NV_IMPORT|NV_RDONLY|NV_TAGGED|NV_NOFREE)
104*4887Schin #define NV_ATTRIBUTES	(~(NV_NOSCOPE|NV_ARRAY|NV_NOARRAY|NV_IDENT|NV_ASSIGN|NV_REF|NV_VARNAME))
105*4887Schin #define NV_PARAM	NV_NODISC	/* expansion use positional params */
106*4887Schin 
107*4887Schin /* This following are for use with nodes which are not name-values */
108*4887Schin #define NV_TYPE		0x1000000
109*4887Schin #define NV_FUNCTION	(NV_RJUST|NV_FUNCT)	/* value is shell function */
110*4887Schin #define NV_FPOSIX	NV_LJUST		/* posix function semantics */
111*4887Schin #define NV_FTMP		NV_ZFILL		/* function source in tmpfile */
112*4887Schin 
113*4887Schin #define NV_NOPRINT	(NV_LTOU|NV_UTOL)	/* do not print */
114*4887Schin #define NV_NOALIAS	(NV_NOPRINT|NV_IMPORT)
115*4887Schin #define NV_NOEXPAND	NV_RJUST		/* do not expand alias */
116*4887Schin #define NV_BLTIN	(NV_NOPRINT|NV_EXPORT)
117*4887Schin #define BLT_ENV		(NV_RDONLY)		/* non-stoppable,
118*4887Schin 						 * can modify enviornment */
119*4887Schin #define BLT_SPC		(NV_LJUST)		/* special built-ins */
120*4887Schin #define BLT_EXIT	(NV_RJUST)		/* exit value can be > 255 */
121*4887Schin #define BLT_DCL		(NV_TAGGED)		/* declaration command */
122*4887Schin #define nv_isref(n)	(nv_isattr((n),NV_REF)==NV_REF)
123*4887Schin #define nv_istable(n)	(nv_isattr((n),NV_TABLE|NV_LJUST|NV_RJUST)==NV_TABLE)
124*4887Schin #define is_abuiltin(n)	(nv_isattr(n,NV_BLTIN)==NV_BLTIN)
125*4887Schin #define is_afunction(n)	(nv_isattr(n,NV_FUNCTION)==NV_FUNCTION)
126*4887Schin #define	nv_funtree(n)	((n)->nvalue.rp->ptree)
127*4887Schin #define	funptr(n)	((n)->nvalue.bfp)
128*4887Schin 
129*4887Schin #define NV_SUBQUOTE	(NV_ADD<<1)	/* used with nv_endsubscript */
130*4887Schin 
131*4887Schin /* NAMNOD MACROS */
132*4887Schin /* ... for attributes */
133*4887Schin 
134*4887Schin #define nv_setattr(n,f)	((n)->nvflag = (f))
135*4887Schin #define nv_context(n)	((void*)(n)->nvfun)		/* for builtins */
136*4887Schin /* The following are for name references */
137*4887Schin #define nv_refnode(n)	((n)->nvalue.nrp->np)
138*4887Schin #define nv_reftree(n)	((n)->nvalue.nrp->root)
139*4887Schin #define nv_reftable(n)	((n)->nvalue.nrp->table)
140*4887Schin #define nv_refsub(n)	((n)->nvalue.nrp->sub)
141*4887Schin #if SHOPT_OO
142*4887Schin #   define nv_class(np)		(nv_isattr(np,NV_REF|NV_IMPORT)?0:(Namval_t*)((np)->nvenv))
143*4887Schin #endif /* SHOPT_OO */
144*4887Schin 
145*4887Schin /* ... etc */
146*4887Schin 
147*4887Schin #define nv_setsize(n,s)	((n)->nvsize = (s))
148*4887Schin #undef nv_size
149*4887Schin #define nv_size(np)	((np)->nvsize)
150*4887Schin #define nv_isnull(np)	(!(np)->nvalue.cp && !(np)->nvfun && !nv_isattr(np,NV_SHORT))
151*4887Schin 
152*4887Schin /* ...	for arrays */
153*4887Schin 
154*4887Schin #define array_elem(ap)	((ap)->nelem&ARRAY_MASK)
155*4887Schin #define array_assoc(ap)	((ap)->fun)
156*4887Schin 
157*4887Schin extern int		array_maxindex(Namval_t*);
158*4887Schin extern char 		*nv_endsubscript(Namval_t*, char*, int);
159*4887Schin extern Namfun_t 	*nv_cover(Namval_t*);
160*4887Schin extern Namarr_t 	*nv_arrayptr(Namval_t*);
161*4887Schin extern int		nv_setnotify(Namval_t*,char **);
162*4887Schin extern int		nv_unsetnotify(Namval_t*,char **);
163*4887Schin extern void		nv_setlist(struct argnod*, int);
164*4887Schin extern void 		nv_optimize(Namval_t*);
165*4887Schin extern void		nv_outname(Sfio_t*,char*, int);
166*4887Schin extern void 		nv_scope(struct argnod*);
167*4887Schin extern void 		nv_unref(Namval_t*);
168*4887Schin extern void		_nv_unset(Namval_t*,int);
169*4887Schin extern int		nv_clone(Namval_t*, Namval_t*, int);
170*4887Schin extern void		*nv_diropen(const char*);
171*4887Schin extern char		*nv_dirnext(void*);
172*4887Schin extern void		nv_dirclose(void*);
173*4887Schin extern char		*nv_getvtree(Namval_t*, Namfun_t*);
174*4887Schin extern void		nv_attribute(Namval_t*, Sfio_t*, char*, int);
175*4887Schin extern Namval_t		*nv_bfsearch(const char*, Dt_t*, Namval_t**, char**);
176*4887Schin extern Namval_t		*nv_mkclone(Namval_t*);
177*4887Schin extern Namval_t		*nv_mktype(Namval_t**, int);
178*4887Schin extern void		nv_addnode(Namval_t*, int);
179*4887Schin extern Namval_t		*nv_parent(Namval_t*);
180*4887Schin extern char		*nv_getbuf(size_t);
181*4887Schin extern Namval_t		*nv_mount(Namval_t*, const char *name, Dt_t*);
182*4887Schin extern Namval_t		*nv_arraychild(Namval_t*, Namval_t*, int);
183*4887Schin extern int		nv_compare(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
184*4887Schin 
185*4887Schin extern const Namdisc_t	RESTRICTED_disc;
186*4887Schin extern char		nv_local;
187*4887Schin extern Dtdisc_t		_Nvdisc;
188*4887Schin extern const char	e_subscript[];
189*4887Schin extern const char	e_nullset[];
190*4887Schin extern const char	e_notset[];
191*4887Schin extern const char	e_noparent[];
192*4887Schin extern const char	e_readonly[];
193*4887Schin extern const char	e_badfield[];
194*4887Schin extern const char	e_restricted[];
195*4887Schin extern const char	e_ident[];
196*4887Schin extern const char	e_varname[];
197*4887Schin extern const char	e_noalias[];
198*4887Schin extern const char	e_noarray[];
199*4887Schin extern const char	e_aliname[];
200*4887Schin extern const char	e_badexport[];
201*4887Schin extern const char	e_badref[];
202*4887Schin extern const char	e_noref[];
203*4887Schin extern const char	e_selfref[];
204*4887Schin extern const char	e_envmarker[];
205*4887Schin extern const char	e_badlocale[];
206*4887Schin extern const char	e_loop[];
207*4887Schin extern const char	e_redef[];
208*4887Schin #endif /* _NV_PRIVATE */
209