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 #ifndef _NV_PRIVATE
22 /*
23  * This is the implementation header file for name-value pairs
24  */
25 
26 #define _NV_PRIVATE	\
27 	Namfun_t	*nvfun;		/* pointer to trap functions */ \
28 	union Value	nvalue; 	/* value field */ \
29 	char		*nvenv;		/* pointer to environment name */
30 
31 #include	<ast.h>
32 #include	<cdt.h>
33 
34 typedef int (*Nambfp_f)(int, char**, void*);
35 
36 /* Nodes can have all kinds of values */
37 union Value
38 {
39 	const char		*cp;
40 	int			*ip;
41 	char			c;
42 	int			i;
43 	unsigned int		u;
44 	int32_t			*lp;
45 	Sflong_t		*llp;	/* for long long arithmetic */
46 	int16_t			s;
47 	int16_t			*sp;
48 	double			*dp;	/* for floating point arithmetic */
49 	Sfdouble_t		*ldp;	/* for long floating point arithmetic */
50 	struct Namarray		*array;	/* for array node */
51 	struct Namval		*np;	/* for Namval_t node */
52 	union Value		*up;	/* for indirect node */
53 	struct Ufunction 	*rp;	/* shell user defined functions */
54 	struct Namfun		*funp;	/* discipline pointer */
55 	struct Namref		*nrp;	/* name reference */
56 	Nambfp_f		bfp;	/* builtin entry point function pointer */
57 };
58 
59 #include	"nval.h"
60 
61 /* used for arrays */
62 
63 #define ARRAY_MAX 	(1L<<ARRAY_BITS) /* maximum number of elements in an array */
64 #define ARRAY_MASK	(ARRAY_MAX-1)	/* For index values */
65 
66 #define ARRAY_INCR	32	/* number of elements to grow when array
67 				   bound exceeded.  Must be a power of 2 */
68 #define ARRAY_FILL	(8L<<ARRAY_BITS)	/* used with nv_putsub() */
69 #define ARRAY_NOCLONE	(16L<<ARRAY_BITS)	/* do not clone array disc */
70 #define ARRAY_NOCHILD   (32L<<ARRAY_BITS)	/* skip compound arrays */
71 #define ARRAY_SETSUB	(64L<<ARRAY_BITS)	/* set subscript */
72 #define ARRAY_NOSCOPE	(128L<<ARRAY_BITS)	/* top level scope only */
73 #define ARRAY_TREE	(256L<<ARRAY_BITS)	/* arrays of compound vars */
74 #if SHOPT_FIXEDARRAY
75 #   define ARRAY_FIXED	ARRAY_NOCLONE		/* For index values */
76 #endif /* SHOPT_FIXEDARRAY */
77 #define NV_FARRAY	0x10000000		/* fixed sized arrays */
78 #define NV_ASETSUB	8			/* set subscript */
79 
80 /* These flags are used as options to array_get() */
81 #define ARRAY_ASSIGN	0
82 #define ARRAY_LOOKUP	1
83 #define ARRAY_DELETE	2
84 
85 
86 struct Namref
87 {
88 	Namval_t	*np;
89 	Namval_t	*table;
90 	Dt_t		*root;
91 	char		*sub;
92 #if SHOPT_FIXEDARRAY
93 	int		curi;
94 	char		dim;
95 #endif /* SHOPT_FIXEDARRAY */
96 };
97 
98 /* This describes a user shell function node */
99 struct Ufunction
100 {
101 	int		*ptree;		/* address of parse tree */
102 	int		lineno;		/* line number of function start */
103 	short		argc;		/* number of references */
104 	short		running;	/* function is running */
105 	char		**argv;		/* reference argument list */
106 	off_t		hoffset;	/* offset into source or history file */
107 	Namval_t	*nspace;	/* pointer to name space */
108 	char		*fname;		/* file name where function defined */
109 	char		*help;		/* help string */
110 	Dt_t		*sdict;		/* dictionary for statics */
111 	Dt_t		*fdict;		/* dictionary node belongs to */
112 	Namval_t	*np;		/* function node pointer */
113 };
114 
115 #ifndef ARG_RAW
116     struct argnod;
117 #endif /* !ARG_RAW */
118 
119 /* attributes of Namval_t items */
120 
121 /* The following attributes are for internal use */
122 #define NV_NOCHANGE	(NV_EXPORT|NV_IMPORT|NV_RDONLY|NV_TAGGED|NV_NOFREE|NV_ARRAY)
123 #define NV_ATTRIBUTES	(~(NV_NOSCOPE|NV_ARRAY|NV_NOARRAY|NV_IDENT|NV_ASSIGN|NV_REF|NV_VARNAME|NV_STATIC))
124 #define NV_PARAM	NV_NODISC	/* expansion use positional params */
125 
126 /* This following are for use with nodes which are not name-values */
127 #define NV_TYPE		0x1000000
128 #define NV_STATIC	0x2000000
129 #define NV_COMVAR	0x4000000
130 #define NV_UNJUST	0x800000		/* clear justify attributes */
131 #define NV_FUNCTION	(NV_RJUST|NV_FUNCT)	/* value is shell function */
132 #define NV_FPOSIX	NV_LJUST		/* posix function semantics */
133 #define NV_FTMP		NV_ZFILL		/* function source in tmpfile */
134 #define NV_STATICF	NV_INTEGER		/* static class function */
135 
136 #define NV_NOPRINT	(NV_LTOU|NV_UTOL)	/* do not print */
137 #define NV_NOALIAS	(NV_NOPRINT|NV_IMPORT)
138 #define NV_NOEXPAND	NV_RJUST		/* do not expand alias */
139 #define NV_BLTIN	(NV_NOPRINT|NV_EXPORT)
140 #define BLT_ENV		(NV_RDONLY)		/* non-stoppable,
141 						 * can modify enviornment */
142 #define BLT_SPC		(NV_LJUST)		/* special built-ins */
143 #define BLT_EXIT	(NV_RJUST)		/* exit value can be > 255 */
144 #define BLT_DCL		(NV_TAGGED)		/* declaration command */
145 #define BLT_NOSFIO	(NV_IMPORT)		/* doesn't use sfio */
146 #define NV_OPTGET	(NV_BINARY)		/* function calls getopts */
147 #define nv_isref(n)	(nv_isattr((n),NV_REF|NV_TAGGED|NV_FUNCT)==NV_REF)
148 #define is_abuiltin(n)	(nv_isattr(n,NV_BLTIN|NV_INTEGER)==NV_BLTIN)
149 #define is_afunction(n)	(nv_isattr(n,NV_FUNCTION|NV_REF)==NV_FUNCTION)
150 #define	nv_funtree(n)	((n)->nvalue.rp->ptree)
151 #define	funptr(n)	((n)->nvalue.bfp)
152 
153 #define NV_SUBQUOTE	(NV_ADD<<1)	/* used with nv_endsubscript */
154 
155 /* NAMNOD MACROS */
156 /* ... for attributes */
157 
158 #define nv_setattr(n,f)	((n)->nvflag = (f))
159 #define nv_context(n)	((void*)(n)->nvfun)		/* for builtins */
160 /* The following are for name references */
161 #define nv_refnode(n)	((n)->nvalue.nrp->np)
162 #define nv_reftree(n)	((n)->nvalue.nrp->root)
163 #define nv_reftable(n)	((n)->nvalue.nrp->table)
164 #define nv_refsub(n)	((n)->nvalue.nrp->sub)
165 #if SHOPT_FIXEDARRAY
166 #   define nv_refindex(n)	((n)->nvalue.nrp->curi)
167 #   define nv_refdimen(n)	((n)->nvalue.nrp->dim)
168 #endif /* SHOPT_FIXEDARRAY */
169 
170 /* ... etc */
171 
172 #define nv_setsize(n,s)	((n)->nvsize = (s))
173 #undef nv_size
174 #define nv_size(np)	((np)->nvsize)
175 #define _nv_hasget(np)  ((np)->nvfun && (np)->nvfun->disc && nv_hasget(np))
176 #define nv_isnull(np)	(!(np)->nvalue.cp && (nv_isattr(np,NV_SHORT|NV_INTEGER)!=(NV_SHORT|NV_INTEGER)) && !_nv_hasget(np))
177 
178 /* ...	for arrays */
179 
180 #define array_elem(ap)	((ap)->nelem&ARRAY_MASK)
181 #define array_assoc(ap)	((ap)->fun)
182 
183 extern int		array_maxindex(Namval_t*);
184 extern char 		*nv_endsubscript(Namval_t*, char*, int);
185 extern Namfun_t 	*nv_cover(Namval_t*);
186 extern Namarr_t 	*nv_arrayptr(Namval_t*);
187 extern int		nv_arrayisset(Namval_t*, Namarr_t*);
188 extern int		nv_arraysettype(Namval_t*, Namval_t*,const char*,int);
189 extern int		nv_aimax(Namval_t*);
190 extern int		nv_atypeindex(Namval_t*, const char*);
191 extern int		nv_setnotify(Namval_t*,char **);
192 extern int		nv_unsetnotify(Namval_t*,char **);
193 extern void		nv_setlist(struct argnod*, int, Namval_t*);
194 extern struct argnod*	nv_onlist(struct argnod*, const char*);
195 extern void 		nv_optimize(Namval_t*);
196 extern void		nv_outname(Sfio_t*,char*, int);
197 extern void 		nv_unref(Namval_t*);
198 extern void		_nv_unset(Namval_t*,int);
199 extern int		nv_hasget(Namval_t*);
200 extern int		nv_clone(Namval_t*, Namval_t*, int);
201 void			clone_all_disc(Namval_t*, Namval_t*, int);
202 extern Namfun_t		*nv_clone_disc(Namfun_t*, int);
203 extern void		*nv_diropen(Namval_t*, const char*);
204 extern char		*nv_dirnext(void*);
205 extern void		nv_dirclose(void*);
206 extern char		*nv_getvtree(Namval_t*, Namfun_t*);
207 extern void		nv_attribute(Namval_t*, Sfio_t*, char*, int);
208 extern Namval_t		*nv_bfsearch(const char*, Dt_t*, Namval_t**, char**);
209 extern Namval_t		*nv_mkclone(Namval_t*);
210 extern Namval_t		*nv_mktype(Namval_t**, int);
211 extern Namval_t		*nv_addnode(Namval_t*, int);
212 extern Namval_t		*nv_parent(Namval_t*);
213 extern char		*nv_getbuf(size_t);
214 extern Namval_t		*nv_mount(Namval_t*, const char *name, Dt_t*);
215 extern Namval_t		*nv_arraychild(Namval_t*, Namval_t*, int);
216 extern int		nv_compare(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
217 extern void		nv_outnode(Namval_t*,Sfio_t*, int, int);
218 extern int		nv_subsaved(Namval_t*);
219 extern void		nv_typename(Namval_t*, Sfio_t*);
220 extern void		nv_newtype(Namval_t*);
221 extern int		nv_istable(Namval_t*);
222 extern size_t		nv_datasize(Namval_t*, size_t*);
223 extern Namfun_t		*nv_mapchar(Namval_t*, const char*);
224 #if SHOPT_FIXEDARRAY
225    extern int		nv_arrfixed(Namval_t*, Sfio_t*, int, char*);
226 #endif /* SHOPT_FIXEDARRAY */
227 
228 extern const Namdisc_t	RESTRICTED_disc;
229 extern const Namdisc_t	ENUM_disc;
230 extern char		nv_local;
231 extern Dtdisc_t		_Nvdisc;
232 extern const char	*nv_discnames[];
233 extern const char	e_subscript[];
234 extern const char	e_nullset[];
235 extern const char	e_notset[];
236 extern const char	e_noparent[];
237 extern const char	e_notelem[];
238 extern const char	e_readonly[];
239 extern const char	e_badfield[];
240 extern const char	e_restricted[];
241 extern const char	e_ident[];
242 extern const char	e_varname[];
243 extern const char	e_noalias[];
244 extern const char	e_noarray[];
245 extern const char	e_notenum[];
246 extern const char	e_nounattr[];
247 extern const char	e_aliname[];
248 extern const char	e_badexport[];
249 extern const char	e_badref[];
250 extern const char	e_badsubscript[];
251 extern const char	e_noref[];
252 extern const char	e_selfref[];
253 extern const char	e_staticfun[];
254 extern const char	e_envmarker[];
255 extern const char	e_badlocale[];
256 extern const char	e_loop[];
257 extern const char	e_redef[];
258 extern const char	e_required[];
259 extern const char	e_badappend[];
260 extern const char	e_unknowntype[];
261 extern const char	e_unknownmap[];
262 extern const char	e_mapchararg[];
263 extern const char	e_subcomvar[];
264 extern const char	e_badtypedef[];
265 extern const char	e_typecompat[];
266 extern const char	e_globalref[];
267 extern const char	e_tolower[];
268 extern const char	e_toupper[];
269 #endif /* _NV_PRIVATE */
270