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_DEFAULT
22*4887Schin /*
23*4887Schin  * David Korn
24*4887Schin  * AT&T Labs
25*4887Schin  *
26*4887Schin  * Interface definitions of structures for name-value pairs
27*4887Schin  * These structures are used for named variables, functions and aliases
28*4887Schin  *
29*4887Schin  */
30*4887Schin 
31*4887Schin 
32*4887Schin #include	<ast.h>
33*4887Schin #include	<cdt.h>
34*4887Schin 
35*4887Schin /* for compatibility with old hash library */
36*4887Schin #define Hashtab_t	Dt_t
37*4887Schin #define HASH_BUCKET	1
38*4887Schin #define HASH_NOSCOPE	2
39*4887Schin #define HASH_SCOPE	4
40*4887Schin #define hashscope(x)	dtvnext(x)
41*4887Schin 
42*4887Schin typedef struct Namval Namval_t;
43*4887Schin typedef struct Namfun Namfun_t;
44*4887Schin typedef struct Namdisc Namdisc_t;
45*4887Schin typedef struct Nambfun Nambfun_t;
46*4887Schin typedef struct Namarray Namarr_t;
47*4887Schin typedef struct Nambltin Nambltin_t;
48*4887Schin typedef struct Namtype Namtype_t;
49*4887Schin 
50*4887Schin /*
51*4887Schin  * This defines the template for nodes that have their own assignment
52*4887Schin  * and or lookup functions
53*4887Schin  */
54*4887Schin struct Namdisc
55*4887Schin {
56*4887Schin 	size_t	dsize;
57*4887Schin 	void	(*putval)(Namval_t*, const char*, int, Namfun_t*);
58*4887Schin 	char	*(*getval)(Namval_t*, Namfun_t*);
59*4887Schin 	Sfdouble_t	(*getnum)(Namval_t*, Namfun_t*);
60*4887Schin 	char	*(*setdisc)(Namval_t*, const char*, Namval_t*, Namfun_t*);
61*4887Schin 	Namval_t *(*createf)(Namval_t*, const char*, int, Namfun_t*);
62*4887Schin 	Namfun_t *(*clonef)(Namval_t*, Namval_t*, int, Namfun_t*);
63*4887Schin 	char	*(*namef)(Namval_t*, Namfun_t*);
64*4887Schin 	Namval_t *(*nextf)(Namval_t*, Dt_t*, Namfun_t*);
65*4887Schin 	Namval_t *(*typef)(Namval_t*, Namfun_t*);
66*4887Schin 	int	(*readf)(Namval_t*, Sfio_t*, int, Namfun_t*);
67*4887Schin };
68*4887Schin 
69*4887Schin struct Namfun
70*4887Schin {
71*4887Schin 	const Namdisc_t	*disc;
72*4887Schin 	char		nofree;
73*4887Schin 	char		funs;
74*4887Schin 	unsigned short	dsize;
75*4887Schin 	Namfun_t	*next;
76*4887Schin 	char		*last;
77*4887Schin 	Namval_t	*type;
78*4887Schin };
79*4887Schin 
80*4887Schin struct Nambfun
81*4887Schin {
82*4887Schin 	Namfun_t        fun;
83*4887Schin 	int		num;
84*4887Schin 	const char	**bnames;
85*4887Schin 	Namval_t	*bltins[1];
86*4887Schin };
87*4887Schin 
88*4887Schin /* This is an array template header */
89*4887Schin struct Namarray
90*4887Schin {
91*4887Schin 	Namfun_t	hdr;
92*4887Schin 	long		nelem;				/* number of elements */
93*4887Schin 	void	*(*fun)(Namval_t*,const char*,int);	/* associative arrays */
94*4887Schin 	Namval_t	*parent;		/* for multi-dimensional */
95*4887Schin };
96*4887Schin 
97*4887Schin /* Passed as third argument to a builtin when  NV_BLTINOPT is set on node */
98*4887Schin struct Nambltin
99*4887Schin {
100*4887Schin         void		*shp;
101*4887Schin 	Namval_t	*np;
102*4887Schin         void	 	*ptr;
103*4887Schin         void		*data;
104*4887Schin         int		flags;
105*4887Schin };
106*4887Schin 
107*4887Schin struct Namtype
108*4887Schin {
109*4887Schin         void		*shp;
110*4887Schin 	Namval_t	*np;
111*4887Schin 	const char	*optstring;
112*4887Schin 	void		*optinfof;
113*4887Schin };
114*4887Schin 
115*4887Schin /* attributes of name-value node attribute flags */
116*4887Schin 
117*4887Schin #define NV_DEFAULT 0
118*4887Schin /* This defines the attributes for an attributed name-value pair node */
119*4887Schin struct Namval
120*4887Schin {
121*4887Schin 	Dtlink_t	nvlink;		/* space for cdt links */
122*4887Schin 	char		*nvname;	/* pointer to name of the node */
123*4887Schin 	unsigned short	nvflag; 	/* attributes */
124*4887Schin 	unsigned short 	nvsize;		/* size or base */
125*4887Schin #ifdef _NV_PRIVATE
126*4887Schin 	_NV_PRIVATE
127*4887Schin #else
128*4887Schin 	Namfun_t	*nvfun;
129*4887Schin 	char		*nvalue;
130*4887Schin 	char		*nvprivate;
131*4887Schin #endif /* _NV_PRIVATE */
132*4887Schin };
133*4887Schin 
134*4887Schin #define NV_CLASS	".sh.type"
135*4887Schin #define NV_MINSZ	(sizeof(struct Namval)-sizeof(Dtlink_t)-sizeof(char*))
136*4887Schin #define nv_namptr(p,n)	((Namval_t*)((char*)(p)+(n)*NV_MINSZ-sizeof(Dtlink_t)))
137*4887Schin 
138*4887Schin /* The following attributes are for internal use */
139*4887Schin #define NV_NOFREE	0x200	/* don't free the space when releasing value */
140*4887Schin #define NV_ARRAY	0x400	/* node is an array */
141*4887Schin #define NV_REF		0x4000	/* reference bit */
142*4887Schin #define NV_TABLE	0x800	/* node is a dictionary table */
143*4887Schin #define NV_IMPORT	0x1000	/* value imported from environment */
144*4887Schin #define NV_MINIMAL	NV_IMPORT	/* node does not contain all fields */
145*4887Schin 
146*4887Schin #define NV_INTEGER	0x2	/* integer attribute */
147*4887Schin /* The following attributes are valid only when NV_INTEGER is off */
148*4887Schin #define NV_LTOU		0x4	/* convert to uppercase */
149*4887Schin #define NV_UTOL		0x8	/* convert to lowercase */
150*4887Schin #define NV_ZFILL	0x10	/* right justify and fill with leading zeros */
151*4887Schin #define NV_RJUST	0x20	/* right justify and blank fill */
152*4887Schin #define NV_LJUST	0x40	/* left justify and blank fill */
153*4887Schin #define NV_BINARY	0x100	/* fixed size data buffer */
154*4887Schin #define NV_RAW		NV_LJUST	/* used only with NV_BINARY */
155*4887Schin #define NV_HOST		(NV_RJUST|NV_LJUST)	/* map to host filename */
156*4887Schin 
157*4887Schin /* The following attributes do not effect the value */
158*4887Schin #define NV_RDONLY	0x1	/* readonly bit */
159*4887Schin #define NV_EXPORT	0x2000	/* export bit */
160*4887Schin #define NV_TAGGED	0x8000	/* user define tag bit */
161*4887Schin 
162*4887Schin /* The following are used with NV_INTEGER */
163*4887Schin #define NV_SHORT	(NV_RJUST)	/* when integers are not long */
164*4887Schin #define NV_LONG		(NV_UTOL)	/* for long long and long double */
165*4887Schin #define NV_UNSIGN	(NV_LTOU)	/* for unsigned quantities */
166*4887Schin #define NV_DOUBLE	(NV_ZFILL)	/* for floating point */
167*4887Schin #define NV_EXPNOTE	(NV_LJUST)	/* for scientific notation */
168*4887Schin 
169*4887Schin /*  options for nv_open */
170*4887Schin 
171*4887Schin #define NV_APPEND	0x10000		/* append value */
172*4887Schin #define NV_MOVE		0x20000		/* for use with nv_clone */
173*4887Schin #define NV_ADD		8
174*4887Schin 					/* add node if not found */
175*4887Schin #define NV_ASSIGN	NV_NOFREE	/* assignment is possible */
176*4887Schin #define NV_NOASSIGN	0		/* backward compatibility */
177*4887Schin #define NV_NOARRAY	0x200000	/* array name not possible */
178*4887Schin #define NV_IARRAY	0x400000	/* for indexed array */
179*4887Schin #define NV_NOREF	NV_REF		/* don't follow reference */
180*4887Schin #define NV_IDENT	0x80		/* name must be identifier */
181*4887Schin #define NV_VARNAME	0x20000		/* name must be ?(.)id*(.id) */
182*4887Schin #define NV_NOADD	0x40000		/* do not add node */
183*4887Schin #define NV_NOSCOPE	0x80000		/* look only in current scope */
184*4887Schin #define NV_NOFAIL	0x100000	/* return 0 on failure, no msg */
185*4887Schin #define NV_NODISC	NV_IDENT	/* ignore disciplines */
186*4887Schin 
187*4887Schin #define NV_FUNCT	NV_IDENT	/* option for nv_create */
188*4887Schin #define NV_BLTINOPT	NV_ZFILL	/* save state for optimization*/
189*4887Schin 
190*4887Schin #define NV_PUBLIC	(~(NV_NOSCOPE|NV_ASSIGN|NV_IDENT|NV_VARNAME|NV_NOADD))
191*4887Schin 
192*4887Schin /* numeric types */
193*4887Schin #define NV_INT16	(NV_SHORT|NV_INTEGER)
194*4887Schin #define NV_UINT16	(NV_UNSIGN|NV_SHORT|NV_INTEGER)
195*4887Schin #define NV_INT32	(NV_INTEGER)
196*4887Schin #define NV_UNT32	(NV_UNSIGN|NV_INTEGER)
197*4887Schin #define NV_INT64	(NV_LONG|NV_INTEGER)
198*4887Schin #define NV_UINT64	(NV_UNSIGN|NV_LONG|NV_INTEGER)
199*4887Schin #define NV_FLOAT	(NV_SHORT|NV_DOUBLE|NV_INTEGER)
200*4887Schin #define NV_LDOUBLE	(NV_LONG|NV_DOUBLE|NV_INTEGER)
201*4887Schin 
202*4887Schin /* name-value pair macros */
203*4887Schin #define nv_isattr(np,f)		((np)->nvflag & (f))
204*4887Schin #define nv_onattr(n,f)		((n)->nvflag |= (f))
205*4887Schin #define nv_offattr(n,f)		((n)->nvflag &= ~(f))
206*4887Schin #define nv_isarray(np)		(nv_isattr((np),NV_ARRAY))
207*4887Schin 
208*4887Schin /* The following are operations for associative arrays */
209*4887Schin #define NV_AINIT	1	/* initialize */
210*4887Schin #define NV_AFREE	2	/* free array */
211*4887Schin #define NV_ANEXT	3	/* advance to next subscript */
212*4887Schin #define NV_ANAME	4	/* return subscript name */
213*4887Schin #define NV_ADELETE	5	/* delete current subscript */
214*4887Schin #define NV_AADD		6	/* add subscript if not found */
215*4887Schin #define NV_ACURRENT	7	/* return current subscript Namval_t* */
216*4887Schin 
217*4887Schin /* The following are for nv_disc */
218*4887Schin #define NV_FIRST	1
219*4887Schin #define NV_LAST		2
220*4887Schin #define NV_POP		3
221*4887Schin #define NV_CLONE	4
222*4887Schin 
223*4887Schin /* The following are operations for nv_putsub() */
224*4887Schin #define ARRAY_BITS	24
225*4887Schin #define ARRAY_ADD	(1L<<ARRAY_BITS)	/* add subscript if not found */
226*4887Schin #define	ARRAY_SCAN	(2L<<ARRAY_BITS)	/* For ${array[@]} */
227*4887Schin #define ARRAY_UNDEF	(4L<<ARRAY_BITS)	/* For ${array} */
228*4887Schin 
229*4887Schin 
230*4887Schin /* These  are disciplines provided by the library for use with nv_discfun */
231*4887Schin #define NV_DCADD	0	/* used to add named disciplines */
232*4887Schin #define NV_DCRESTRICT	1	/* variable that are restricted in rsh */
233*4887Schin 
234*4887Schin #if defined(__EXPORT__) && defined(_DLL)
235*4887Schin #   ifdef _BLD_shell
236*4887Schin #	define extern __EXPORT__
237*4887Schin #   else
238*4887Schin #	define extern __IMPORT__
239*4887Schin #   endif /* _BLD_shell */
240*4887Schin #endif /* _DLL */
241*4887Schin /* prototype for array interface*/
242*4887Schin extern Namarr_t	*nv_setarray(Namval_t*,void*(*)(Namval_t*,const char*,int));
243*4887Schin extern void	*nv_associative(Namval_t*,const char*,int);
244*4887Schin extern int	nv_aindex(Namval_t*);
245*4887Schin extern int	nv_nextsub(Namval_t*);
246*4887Schin extern char	*nv_getsub(Namval_t*);
247*4887Schin extern Namval_t	*nv_putsub(Namval_t*, char*, long);
248*4887Schin extern Namval_t	*nv_opensub(Namval_t*);
249*4887Schin 
250*4887Schin /* name-value pair function prototypes */
251*4887Schin extern int		nv_adddisc(Namval_t*, const char**, Namval_t**);
252*4887Schin extern int		nv_clone(Namval_t*, Namval_t*, int);
253*4887Schin extern void 		nv_close(Namval_t*);
254*4887Schin extern void		*nv_context(Namval_t*);
255*4887Schin extern Namval_t		*nv_create(const char*, Dt_t*, int,Namfun_t*);
256*4887Schin extern Dt_t		*nv_dict(Namval_t*);
257*4887Schin extern Sfdouble_t	nv_getn(Namval_t*, Namfun_t*);
258*4887Schin extern Sfdouble_t	nv_getnum(Namval_t*);
259*4887Schin extern char 		*nv_getv(Namval_t*, Namfun_t*);
260*4887Schin extern char 		*nv_getval(Namval_t*);
261*4887Schin extern Namfun_t		*nv_hasdisc(Namval_t*, const Namdisc_t*);
262*4887Schin extern int		nv_isnull(Namval_t*);
263*4887Schin extern Namval_t		*nv_lastdict(void);
264*4887Schin extern void 		nv_newattr(Namval_t*,unsigned,int);
265*4887Schin extern Namval_t		*nv_open(const char*,Dt_t*,int);
266*4887Schin extern void 		nv_putval(Namval_t*,const char*,int);
267*4887Schin extern void 		nv_putv(Namval_t*,const char*,int,Namfun_t*);
268*4887Schin extern int		nv_scan(Dt_t*,void(*)(Namval_t*,void*),void*,int,int);
269*4887Schin extern Namval_t		*nv_scoped(Namval_t*);
270*4887Schin extern char 		*nv_setdisc(Namval_t*,const char*,Namval_t*,Namfun_t*);
271*4887Schin extern void		nv_setref(Namval_t*, Dt_t*,int);
272*4887Schin extern int		nv_settype(Namval_t*, Namval_t*, int);
273*4887Schin extern void 		nv_setvec(Namval_t*,int,int,char*[]);
274*4887Schin extern void		nv_setvtree(Namval_t*);
275*4887Schin extern int 		nv_setsize(Namval_t*,int);
276*4887Schin extern Namfun_t		*nv_disc(Namval_t*,Namfun_t*,int);
277*4887Schin extern void 		nv_unset(Namval_t*);
278*4887Schin extern Namval_t		*nv_search(const char *, Dt_t*, int);
279*4887Schin extern void		nv_unscope(void);
280*4887Schin extern char		*nv_name(Namval_t*);
281*4887Schin extern Namval_t		*nv_type(Namval_t*);
282*4887Schin extern const Namdisc_t	*nv_discfun(int);
283*4887Schin 
284*4887Schin #ifdef _DLL
285*4887Schin #   undef extern
286*4887Schin #endif /* _DLL */
287*4887Schin 
288*4887Schin #define nv_size(np)		nv_setsize((np),-1)
289*4887Schin #define nv_stack(np,nf)		nv_disc(np,nf,0)
290*4887Schin 
291*4887Schin #if 0
292*4887Schin /*
293*4887Schin  * The names of many functions were changed in early '95
294*4887Schin  * Here is a mapping to the old names
295*4887Schin  */
296*4887Schin #   define nv_istype(np)	nv_isattr(np)
297*4887Schin #   define nv_newtype(np)	nv_newattr(np)
298*4887Schin #   define nv_namset(np,a,b)	nv_open(np,a,b)
299*4887Schin #   define nv_free(np)		nv_unset(np)
300*4887Schin #   define nv_settype(np,a,b,c)	nv_setdisc(np,a,b,c)
301*4887Schin #   define nv_search(np,a,b)	nv_open(np,a,((b)?0:NV_NOADD))
302*4887Schin #   define settype	setdisc
303*4887Schin #endif
304*4887Schin 
305*4887Schin #endif /* NV_DEFAULT */
306