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