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