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