xref: /titanic_41/usr/src/lib/libshell/common/include/nval.h (revision 9ea11b96f9f71eef4f1bd2062ea054451a32a216)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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 	unsigned short	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 	Namval_t	*parent;		/* for multi-dimensional */
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 	unsigned short	nvflag; 	/* attributes */
117 	unsigned short 	nvsize;		/* size or base */
118 #ifdef _NV_PRIVATE
119 	_NV_PRIVATE
120 #else
121 	Namfun_t	*nvfun;
122 	char		*nvalue;
123 	char		*nvprivate;
124 #endif /* _NV_PRIVATE */
125 };
126 
127 #define NV_CLASS	".sh.type"
128 #define NV_DATA		"_"	/* special class or instance variable */
129 #define NV_MINSZ	(sizeof(struct Namval)-sizeof(Dtlink_t)-sizeof(char*))
130 #define nv_namptr(p,n)	((Namval_t*)((char*)(p)+(n)*NV_MINSZ-sizeof(Dtlink_t)))
131 
132 /* The following attributes are for internal use */
133 #define NV_NOFREE	0x200	/* don't free the space when releasing value */
134 #define NV_ARRAY	0x400	/* node is an array */
135 #define NV_REF		0x4000	/* reference bit */
136 #define NV_TABLE	0x800	/* node is a dictionary table */
137 #define NV_IMPORT	0x1000	/* value imported from environment */
138 #define NV_MINIMAL	NV_IMPORT	/* node does not contain all fields */
139 
140 #define NV_INTEGER	0x2	/* integer attribute */
141 /* The following attributes are valid only when NV_INTEGER is off */
142 #define NV_LTOU		0x4	/* convert to uppercase */
143 #define NV_UTOL		0x8	/* convert to lowercase */
144 #define NV_ZFILL	0x10	/* right justify and fill with leading zeros */
145 #define NV_RJUST	0x20	/* right justify and blank fill */
146 #define NV_LJUST	0x40	/* left justify and blank fill */
147 #define NV_BINARY	0x100	/* fixed size data buffer */
148 #define NV_RAW		NV_LJUST	/* used only with NV_BINARY */
149 #define NV_HOST		(NV_RJUST|NV_LJUST)	/* map to host filename */
150 
151 /* The following attributes do not effect the value */
152 #define NV_RDONLY	0x1	/* readonly bit */
153 #define NV_EXPORT	0x2000	/* export bit */
154 #define NV_TAGGED	0x8000	/* user define tag bit */
155 
156 /* The following are used with NV_INTEGER */
157 #define NV_SHORT	(NV_RJUST)	/* when integers are not long */
158 #define NV_LONG		(NV_UTOL)	/* for long long and long double */
159 #define NV_UNSIGN	(NV_LTOU)	/* for unsigned quantities */
160 #define NV_DOUBLE	(NV_INTEGER|NV_ZFILL)	/* for floating point */
161 #define NV_EXPNOTE	(NV_LJUST)	/* for scientific notation */
162 #define NV_HEXFLOAT	(NV_LTOU)	/* for C99 base16 float notation */
163 
164 /*  options for nv_open */
165 
166 #define NV_APPEND	0x10000		/* append value */
167 #define NV_MOVE		0x8000000	/* for use with nv_clone */
168 #define NV_ADD		8
169 					/* add node if not found */
170 #define NV_ASSIGN	NV_NOFREE	/* assignment is possible */
171 #define NV_NOASSIGN	0		/* backward compatibility */
172 #define NV_NOARRAY	0x200000	/* array name not possible */
173 #define NV_IARRAY	0x400000	/* for indexed array */
174 #define NV_NOREF	NV_REF		/* don't follow reference */
175 #define NV_IDENT	0x80		/* name must be identifier */
176 #define NV_VARNAME	0x20000		/* name must be ?(.)id*(.id) */
177 #define NV_NOADD	0x40000		/* do not add node */
178 #define NV_NOSCOPE	0x80000		/* look only in current scope */
179 #define NV_NOFAIL	0x100000	/* return 0 on failure, no msg */
180 #define NV_NODISC	NV_IDENT	/* ignore disciplines */
181 
182 #define NV_FUNCT	NV_IDENT	/* option for nv_create */
183 #define NV_BLTINOPT	NV_ZFILL	/* mark builtins in libcmd */
184 
185 #define NV_PUBLIC	(~(NV_NOSCOPE|NV_ASSIGN|NV_IDENT|NV_VARNAME|NV_NOADD))
186 
187 /* numeric types */
188 #define NV_INT16P	(NV_LJUST|NV_SHORT|NV_INTEGER)
189 #define NV_INT16	(NV_SHORT|NV_INTEGER)
190 #define NV_UINT16	(NV_UNSIGN|NV_SHORT|NV_INTEGER)
191 #define NV_UINT16P	(NV_LJUSTNV_UNSIGN|NV_SHORT|NV_INTEGER)
192 #define NV_INT32	(NV_INTEGER)
193 #define NV_UNT32	(NV_UNSIGN|NV_INTEGER)
194 #define NV_INT64	(NV_LONG|NV_INTEGER)
195 #define NV_UINT64	(NV_UNSIGN|NV_LONG|NV_INTEGER)
196 #define NV_FLOAT	(NV_SHORT|NV_DOUBLE)
197 #define NV_LDOUBLE	(NV_LONG|NV_DOUBLE)
198 
199 /* name-value pair macros */
200 #define nv_isattr(np,f)		((np)->nvflag & (f))
201 #define nv_onattr(n,f)		((n)->nvflag |= (f))
202 #define nv_offattr(n,f)		((n)->nvflag &= ~(f))
203 #define nv_isarray(np)		(nv_isattr((np),NV_ARRAY))
204 
205 /* The following are operations for associative arrays */
206 #define NV_AINIT	1	/* initialize */
207 #define NV_AFREE	2	/* free array */
208 #define NV_ANEXT	3	/* advance to next subscript */
209 #define NV_ANAME	4	/* return subscript name */
210 #define NV_ADELETE	5	/* delete current subscript */
211 #define NV_AADD		6	/* add subscript if not found */
212 #define NV_ACURRENT	7	/* return current subscript Namval_t* */
213 #define NV_ASETSUB	8	/* set current subscript */
214 
215 /* The following are for nv_disc */
216 #define NV_FIRST	1
217 #define NV_LAST		2
218 #define NV_POP		3
219 #define NV_CLONE	4
220 
221 /* The following are operations for nv_putsub() */
222 #define ARRAY_BITS	22
223 #define ARRAY_ADD	(1L<<ARRAY_BITS)	/* add subscript if not found */
224 #define	ARRAY_SCAN	(2L<<ARRAY_BITS)	/* For ${array[@]} */
225 #define ARRAY_UNDEF	(4L<<ARRAY_BITS)	/* For ${array} */
226 
227 
228 /* These  are disciplines provided by the library for use with nv_discfun */
229 #define NV_DCADD	0	/* used to add named disciplines */
230 #define NV_DCRESTRICT	1	/* variable that are restricted in rsh */
231 
232 #if defined(__EXPORT__) && defined(_DLL)
233 #   ifdef _BLD_shell
234 #	define extern __EXPORT__
235 #   else
236 #	define extern __IMPORT__
237 #   endif /* _BLD_shell */
238 #endif /* _DLL */
239 /* prototype for array interface*/
240 extern Namarr_t	*nv_arrayptr(Namval_t*);
241 extern Namarr_t	*nv_setarray(Namval_t*,void*(*)(Namval_t*,const char*,int));
242 extern int	nv_arraynsub(Namarr_t*);
243 extern void	*nv_associative(Namval_t*,const char*,int);
244 extern int	nv_aindex(Namval_t*);
245 extern int	nv_nextsub(Namval_t*);
246 extern char	*nv_getsub(Namval_t*);
247 extern Namval_t	*nv_putsub(Namval_t*, char*, long);
248 extern Namval_t	*nv_opensub(Namval_t*);
249 
250 /* name-value pair function prototypes */
251 extern int		nv_adddisc(Namval_t*, const char**, Namval_t**);
252 extern int		nv_clone(Namval_t*, Namval_t*, int);
253 extern void 		nv_close(Namval_t*);
254 extern void		*nv_context(Namval_t*);
255 extern Namval_t		*nv_create(const char*, Dt_t*, int,Namfun_t*);
256 extern void		nv_delete(Namval_t*, Dt_t*, int);
257 extern Dt_t		*nv_dict(Namval_t*);
258 extern Sfdouble_t	nv_getn(Namval_t*, Namfun_t*);
259 extern Sfdouble_t	nv_getnum(Namval_t*);
260 extern char 		*nv_getv(Namval_t*, Namfun_t*);
261 extern char 		*nv_getval(Namval_t*);
262 extern Namfun_t		*nv_hasdisc(Namval_t*, const Namdisc_t*);
263 extern int		nv_isnull(Namval_t*);
264 extern Namfun_t		*nv_isvtree(Namval_t*);
265 extern Namval_t		*nv_lastdict(void);
266 extern Namval_t		*nv_mkinttype(char*, size_t, int, const char*, Namdisc_t*);
267 extern void 		nv_newattr(Namval_t*,unsigned,int);
268 extern void 		nv_newtype(Namval_t*);
269 extern Namval_t		*nv_open(const char*,Dt_t*,int);
270 extern void 		nv_putval(Namval_t*,const char*,int);
271 extern void 		nv_putv(Namval_t*,const char*,int,Namfun_t*);
272 extern int		nv_rename(Namval_t*,int);
273 extern int		nv_scan(Dt_t*,void(*)(Namval_t*,void*),void*,int,int);
274 extern char 		*nv_setdisc(Namval_t*,const char*,Namval_t*,Namfun_t*);
275 extern void		nv_setref(Namval_t*, Dt_t*,int);
276 extern int		nv_settype(Namval_t*, Namval_t*, int);
277 extern void 		nv_setvec(Namval_t*,int,int,char*[]);
278 extern void		nv_setvtree(Namval_t*);
279 extern int 		nv_setsize(Namval_t*,int);
280 extern Namfun_t		*nv_disc(Namval_t*,Namfun_t*,int);
281 extern void 		nv_unset(Namval_t*);	 /*obsolete */
282 extern void 		_nv_unset(Namval_t*,int);
283 extern Namval_t		*nv_search(const char *, Dt_t*, int);
284 extern char		*nv_name(Namval_t*);
285 extern Namval_t		*nv_type(Namval_t*);
286 extern void		nv_addtype(Namval_t*,const char*, Optdisc_t*, size_t);
287 extern const Namdisc_t	*nv_discfun(int);
288 
289 #ifdef _DLL
290 #   undef extern
291 #endif /* _DLL */
292 
293 #define nv_unset(np)		_nv_unset(np,0)
294 #define nv_size(np)		nv_setsize((np),-1)
295 #define nv_stack(np,nf)		nv_disc(np,nf,0)
296 
297 #if 0
298 /*
299  * The names of many functions were changed in early '95
300  * Here is a mapping to the old names
301  */
302 #   define nv_istype(np)	nv_isattr(np)
303 #   define nv_newtype(np)	nv_newattr(np)
304 #   define nv_namset(np,a,b)	nv_open(np,a,b)
305 #   define nv_free(np)		nv_unset(np,0)
306 #   define nv_settype(np,a,b,c)	nv_setdisc(np,a,b,c)
307 #   define nv_search(np,a,b)	nv_open(np,a,((b)?0:NV_NOADD))
308 #   define settype	setdisc
309 #endif
310 
311 #endif /* NV_DEFAULT */
312