xref: /onnv-gate/usr/src/lib/libshell/amd64/include/ast/nval.h (revision 12068:08a39a083754)
14887Schin 
24887Schin /* : : generated by proto : : */
34887Schin /***********************************************************************
44887Schin *                                                                      *
54887Schin *               This software is part of the ast package               *
6*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1982-2010 AT&T Intellectual Property          *
74887Schin *                      and is licensed under the                       *
84887Schin *                  Common Public License, Version 1.0                  *
98462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
104887Schin *                                                                      *
114887Schin *                A copy of the License is available at                 *
124887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
134887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
144887Schin *                                                                      *
154887Schin *              Information and Software Systems Research               *
164887Schin *                            AT&T Research                             *
174887Schin *                           Florham Park NJ                            *
184887Schin *                                                                      *
194887Schin *                  David Korn <dgk@research.att.com>                   *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin 
234887Schin #ifndef NV_DEFAULT
244887Schin #if !defined(__PROTO__)
254887Schin #include <prototyped.h>
264887Schin #endif
274887Schin #if !defined(__LINKAGE__)
284887Schin #define __LINKAGE__		/* 2004-08-11 transition */
294887Schin #endif
304887Schin 
314887Schin /*
324887Schin  * David Korn
334887Schin  * AT&T Labs
344887Schin  *
354887Schin  * Interface definitions of structures for name-value pairs
364887Schin  * These structures are used for named variables, functions and aliases
374887Schin  *
384887Schin  */
394887Schin 
404887Schin 
414887Schin #include	<ast.h>
424887Schin #include	<cdt.h>
438462SApril.Chin@Sun.COM #include	<option.h>
444887Schin 
454887Schin /* for compatibility with old hash library */
464887Schin #define Hashtab_t	Dt_t
474887Schin #define HASH_BUCKET	1
484887Schin #define HASH_NOSCOPE	2
494887Schin #define HASH_SCOPE	4
504887Schin #define hashscope(x)	dtvnext(x)
514887Schin 
524887Schin typedef struct Namval Namval_t;
534887Schin typedef struct Namfun Namfun_t;
544887Schin typedef struct Namdisc Namdisc_t;
554887Schin typedef struct Nambfun Nambfun_t;
564887Schin typedef struct Namarray Namarr_t;
578462SApril.Chin@Sun.COM typedef struct Namdecl Namdecl_t;
584887Schin 
594887Schin /*
604887Schin  * This defines the template for nodes that have their own assignment
614887Schin  * and or lookup functions
624887Schin  */
634887Schin struct Namdisc
644887Schin {
654887Schin 	size_t	dsize;
664887Schin 	void	(*putval) __PROTO__((Namval_t*, const char*, int, Namfun_t*));
674887Schin 	char	*(*getval) __PROTO__((Namval_t*, Namfun_t*));
684887Schin 	Sfdouble_t	(*getnum) __PROTO__((Namval_t*, Namfun_t*));
694887Schin 	char	*(*setdisc) __PROTO__((Namval_t*, const char*, Namval_t*, Namfun_t*));
704887Schin 	Namval_t *(*createf) __PROTO__((Namval_t*, const char*, int, Namfun_t*));
714887Schin 	Namfun_t *(*clonef) __PROTO__((Namval_t*, Namval_t*, int, Namfun_t*));
724887Schin 	char	*(*namef) __PROTO__((Namval_t*, Namfun_t*));
734887Schin 	Namval_t *(*nextf) __PROTO__((Namval_t*, Dt_t*, Namfun_t*));
744887Schin 	Namval_t *(*typef) __PROTO__((Namval_t*, Namfun_t*));
754887Schin 	int	(*readf) __PROTO__((Namval_t*, Sfio_t*, int, Namfun_t*));
768462SApril.Chin@Sun.COM 	int	(*writef) __PROTO__((Namval_t*, Sfio_t*, int, Namfun_t*));
774887Schin };
784887Schin 
794887Schin struct Namfun
804887Schin {
814887Schin 	const Namdisc_t	*disc;
824887Schin 	char		nofree;
838462SApril.Chin@Sun.COM 	unsigned char	subshell;
844887Schin 	unsigned short	dsize;
854887Schin 	Namfun_t	*next;
864887Schin 	char		*last;
874887Schin 	Namval_t	*type;
884887Schin };
894887Schin 
904887Schin struct Nambfun
914887Schin {
924887Schin 	Namfun_t        fun;
934887Schin 	int		num;
944887Schin 	const char	**bnames;
954887Schin 	Namval_t	*bltins[1];
964887Schin };
974887Schin 
984887Schin /* This is an array template header */
994887Schin struct Namarray
1004887Schin {
1014887Schin 	Namfun_t	hdr;
1024887Schin 	long		nelem;				/* number of elements */
1034887Schin 	__V_	*(*fun) __PROTO__((Namval_t*,const char*,int));	/* associative arrays */
1044887Schin 	Namval_t	*parent;		/* for multi-dimensional */
1058462SApril.Chin@Sun.COM 	Dt_t		*table;			/* for subscripts */
1068462SApril.Chin@Sun.COM 	__V_		*scope;			/* non-zerp when scoped */
1074887Schin };
1084887Schin 
1098462SApril.Chin@Sun.COM /* The context pointer for declaration command */
1108462SApril.Chin@Sun.COM struct Namdecl
1114887Schin {
1128462SApril.Chin@Sun.COM 	Namval_t	*tp;			/* point to type */
1134887Schin 	const char	*optstring;
1144887Schin 	__V_		*optinfof;
1154887Schin };
1164887Schin 
1174887Schin /* attributes of name-value node attribute flags */
1184887Schin 
1194887Schin #define NV_DEFAULT 0
1204887Schin /* This defines the attributes for an attributed name-value pair node */
1214887Schin struct Namval
1224887Schin {
1234887Schin 	Dtlink_t	nvlink;		/* space for cdt links */
1244887Schin 	char		*nvname;	/* pointer to name of the node */
1254887Schin 	unsigned short	nvflag; 	/* attributes */
1264887Schin 	unsigned short 	nvsize;		/* size or base */
1274887Schin #ifdef _NV_PRIVATE
1284887Schin 	_NV_PRIVATE
1294887Schin #else
1304887Schin 	Namfun_t	*nvfun;
1314887Schin 	char		*nvalue;
1324887Schin 	char		*nvprivate;
1334887Schin #endif /* _NV_PRIVATE */
1344887Schin };
1354887Schin 
1364887Schin #define NV_CLASS	".sh.type"
1378462SApril.Chin@Sun.COM #define NV_DATA		"_"	/* special class or instance variable */
1384887Schin #define NV_MINSZ	(sizeof(struct Namval)-sizeof(Dtlink_t)-sizeof(char*))
1394887Schin #define nv_namptr(p,n)	((Namval_t*)((char*)(p)+(n)*NV_MINSZ-sizeof(Dtlink_t)))
1404887Schin 
1414887Schin /* The following attributes are for internal use */
1424887Schin #define NV_NOFREE	0x200	/* don't free the space when releasing value */
1434887Schin #define NV_ARRAY	0x400	/* node is an array */
1444887Schin #define NV_REF		0x4000	/* reference bit */
1454887Schin #define NV_TABLE	0x800	/* node is a dictionary table */
1464887Schin #define NV_IMPORT	0x1000	/* value imported from environment */
1474887Schin #define NV_MINIMAL	NV_IMPORT	/* node does not contain all fields */
1484887Schin 
1494887Schin #define NV_INTEGER	0x2	/* integer attribute */
1504887Schin /* The following attributes are valid only when NV_INTEGER is off */
1514887Schin #define NV_LTOU		0x4	/* convert to uppercase */
1524887Schin #define NV_UTOL		0x8	/* convert to lowercase */
1534887Schin #define NV_ZFILL	0x10	/* right justify and fill with leading zeros */
1544887Schin #define NV_RJUST	0x20	/* right justify and blank fill */
1554887Schin #define NV_LJUST	0x40	/* left justify and blank fill */
1564887Schin #define NV_BINARY	0x100	/* fixed size data buffer */
1574887Schin #define NV_RAW		NV_LJUST	/* used only with NV_BINARY */
1584887Schin #define NV_HOST		(NV_RJUST|NV_LJUST)	/* map to host filename */
1594887Schin 
1604887Schin /* The following attributes do not effect the value */
1614887Schin #define NV_RDONLY	0x1	/* readonly bit */
1624887Schin #define NV_EXPORT	0x2000	/* export bit */
1634887Schin #define NV_TAGGED	0x8000	/* user define tag bit */
1644887Schin 
1654887Schin /* The following are used with NV_INTEGER */
1664887Schin #define NV_SHORT	(NV_RJUST)	/* when integers are not long */
1674887Schin #define NV_LONG		(NV_UTOL)	/* for long long and long double */
1684887Schin #define NV_UNSIGN	(NV_LTOU)	/* for unsigned quantities */
1698462SApril.Chin@Sun.COM #define NV_DOUBLE	(NV_INTEGER|NV_ZFILL)	/* for floating point */
1704887Schin #define NV_EXPNOTE	(NV_LJUST)	/* for scientific notation */
1718462SApril.Chin@Sun.COM #define NV_HEXFLOAT	(NV_LTOU)	/* for C99 base16 float notation */
1724887Schin 
1734887Schin /*  options for nv_open */
1744887Schin 
1754887Schin #define NV_APPEND	0x10000		/* append value */
1768462SApril.Chin@Sun.COM #define NV_MOVE		0x8000000	/* for use with nv_clone */
1774887Schin #define NV_ADD		8
1784887Schin 					/* add node if not found */
1794887Schin #define NV_ASSIGN	NV_NOFREE	/* assignment is possible */
1804887Schin #define NV_NOASSIGN	0		/* backward compatibility */
1814887Schin #define NV_NOARRAY	0x200000	/* array name not possible */
1824887Schin #define NV_IARRAY	0x400000	/* for indexed array */
1834887Schin #define NV_NOREF	NV_REF		/* don't follow reference */
1844887Schin #define NV_IDENT	0x80		/* name must be identifier */
1854887Schin #define NV_VARNAME	0x20000		/* name must be ?(.)id*(.id) */
1864887Schin #define NV_NOADD	0x40000		/* do not add node */
1874887Schin #define NV_NOSCOPE	0x80000		/* look only in current scope */
1884887Schin #define NV_NOFAIL	0x100000	/* return 0 on failure, no msg */
1894887Schin #define NV_NODISC	NV_IDENT	/* ignore disciplines */
1904887Schin 
1914887Schin #define NV_FUNCT	NV_IDENT	/* option for nv_create */
1928462SApril.Chin@Sun.COM #define NV_BLTINOPT	NV_ZFILL	/* mark builtins in libcmd */
1934887Schin 
1944887Schin #define NV_PUBLIC	(~(NV_NOSCOPE|NV_ASSIGN|NV_IDENT|NV_VARNAME|NV_NOADD))
1954887Schin 
1964887Schin /* numeric types */
1978462SApril.Chin@Sun.COM #define NV_INT16P	(NV_LJUST|NV_SHORT|NV_INTEGER)
1984887Schin #define NV_INT16	(NV_SHORT|NV_INTEGER)
1994887Schin #define NV_UINT16	(NV_UNSIGN|NV_SHORT|NV_INTEGER)
2008462SApril.Chin@Sun.COM #define NV_UINT16P	(NV_LJUSTNV_UNSIGN|NV_SHORT|NV_INTEGER)
2014887Schin #define NV_INT32	(NV_INTEGER)
2024887Schin #define NV_UNT32	(NV_UNSIGN|NV_INTEGER)
2034887Schin #define NV_INT64	(NV_LONG|NV_INTEGER)
2044887Schin #define NV_UINT64	(NV_UNSIGN|NV_LONG|NV_INTEGER)
2058462SApril.Chin@Sun.COM #define NV_FLOAT	(NV_SHORT|NV_DOUBLE)
2068462SApril.Chin@Sun.COM #define NV_LDOUBLE	(NV_LONG|NV_DOUBLE)
2074887Schin 
2084887Schin /* name-value pair macros */
2094887Schin #define nv_isattr(np,f)		((np)->nvflag & (f))
2104887Schin #define nv_onattr(n,f)		((n)->nvflag |= (f))
2114887Schin #define nv_offattr(n,f)		((n)->nvflag &= ~(f))
2124887Schin #define nv_isarray(np)		(nv_isattr((np),NV_ARRAY))
2134887Schin 
2144887Schin /* The following are operations for associative arrays */
2154887Schin #define NV_AINIT	1	/* initialize */
2164887Schin #define NV_AFREE	2	/* free array */
2174887Schin #define NV_ANEXT	3	/* advance to next subscript */
2184887Schin #define NV_ANAME	4	/* return subscript name */
2194887Schin #define NV_ADELETE	5	/* delete current subscript */
2204887Schin #define NV_AADD		6	/* add subscript if not found */
2214887Schin #define NV_ACURRENT	7	/* return current subscript Namval_t* */
2228462SApril.Chin@Sun.COM #define NV_ASETSUB	8	/* set current subscript */
2234887Schin 
2244887Schin /* The following are for nv_disc */
2254887Schin #define NV_FIRST	1
2264887Schin #define NV_LAST		2
2274887Schin #define NV_POP		3
2284887Schin #define NV_CLONE	4
2294887Schin 
2304887Schin /* The following are operations for nv_putsub() */
2318462SApril.Chin@Sun.COM #define ARRAY_BITS	22
2324887Schin #define ARRAY_ADD	(1L<<ARRAY_BITS)	/* add subscript if not found */
2334887Schin #define	ARRAY_SCAN	(2L<<ARRAY_BITS)	/* For ${array[@]} */
2344887Schin #define ARRAY_UNDEF	(4L<<ARRAY_BITS)	/* For ${array} */
2354887Schin 
2364887Schin 
2374887Schin /* These  are disciplines provided by the library for use with nv_discfun */
2384887Schin #define NV_DCADD	0	/* used to add named disciplines */
2394887Schin #define NV_DCRESTRICT	1	/* variable that are restricted in rsh */
2404887Schin 
2414887Schin #if defined(__EXPORT__) && defined(_DLL)
2424887Schin #   ifdef _BLD_shell
2434887Schin #undef __MANGLE__
2444887Schin #define __MANGLE__ __LINKAGE__ __EXPORT__
2454887Schin #   else
2464887Schin #undef __MANGLE__
2474887Schin #define __MANGLE__ __LINKAGE__ __IMPORT__
2484887Schin #   endif /* _BLD_shell */
2494887Schin #endif /* _DLL */
2504887Schin /* prototype for array interface*/
2518462SApril.Chin@Sun.COM extern __MANGLE__ Namarr_t	*nv_arrayptr __PROTO__((Namval_t*));
2524887Schin extern __MANGLE__ Namarr_t	*nv_setarray __PROTO__((Namval_t*,__V_*(*)(Namval_t*,const char*,int)));
2538462SApril.Chin@Sun.COM extern __MANGLE__ int	nv_arraynsub __PROTO__((Namarr_t*));
2544887Schin extern __MANGLE__ __V_	*nv_associative __PROTO__((Namval_t*,const char*,int));
2554887Schin extern __MANGLE__ int	nv_aindex __PROTO__((Namval_t*));
2564887Schin extern __MANGLE__ int	nv_nextsub __PROTO__((Namval_t*));
2574887Schin extern __MANGLE__ char	*nv_getsub __PROTO__((Namval_t*));
2584887Schin extern __MANGLE__ Namval_t	*nv_putsub __PROTO__((Namval_t*, char*, long));
2594887Schin extern __MANGLE__ Namval_t	*nv_opensub __PROTO__((Namval_t*));
2604887Schin 
2614887Schin /* name-value pair function prototypes */
2624887Schin extern __MANGLE__ int		nv_adddisc __PROTO__((Namval_t*, const char**, Namval_t**));
2634887Schin extern __MANGLE__ int		nv_clone __PROTO__((Namval_t*, Namval_t*, int));
2644887Schin extern __MANGLE__ void 		nv_close __PROTO__((Namval_t*));
2654887Schin extern __MANGLE__ __V_		*nv_context __PROTO__((Namval_t*));
2664887Schin extern __MANGLE__ Namval_t		*nv_create __PROTO__((const char*, Dt_t*, int,Namfun_t*));
2678462SApril.Chin@Sun.COM extern __MANGLE__ void		nv_delete __PROTO__((Namval_t*, Dt_t*, int));
2684887Schin extern __MANGLE__ Dt_t		*nv_dict __PROTO__((Namval_t*));
2694887Schin extern __MANGLE__ Sfdouble_t	nv_getn __PROTO__((Namval_t*, Namfun_t*));
2704887Schin extern __MANGLE__ Sfdouble_t	nv_getnum __PROTO__((Namval_t*));
2714887Schin extern __MANGLE__ char 		*nv_getv __PROTO__((Namval_t*, Namfun_t*));
2724887Schin extern __MANGLE__ char 		*nv_getval __PROTO__((Namval_t*));
2734887Schin extern __MANGLE__ Namfun_t		*nv_hasdisc __PROTO__((Namval_t*, const Namdisc_t*));
2744887Schin extern __MANGLE__ int		nv_isnull __PROTO__((Namval_t*));
2758462SApril.Chin@Sun.COM extern __MANGLE__ Namfun_t		*nv_isvtree __PROTO__((Namval_t*));
2764887Schin extern __MANGLE__ Namval_t		*nv_lastdict __PROTO__((void));
2778462SApril.Chin@Sun.COM extern __MANGLE__ Namval_t		*nv_mkinttype __PROTO__((char*, size_t, int, const char*, Namdisc_t*));
2784887Schin extern __MANGLE__ void 		nv_newattr __PROTO__((Namval_t*,unsigned,int));
27910898Sroland.mainz@nrubsig.org extern __MANGLE__ void 		nv_newtype __PROTO__((Namval_t*));
2804887Schin extern __MANGLE__ Namval_t		*nv_open __PROTO__((const char*,Dt_t*,int));
2814887Schin extern __MANGLE__ void 		nv_putval __PROTO__((Namval_t*,const char*,int));
2824887Schin extern __MANGLE__ void 		nv_putv __PROTO__((Namval_t*,const char*,int,Namfun_t*));
2838462SApril.Chin@Sun.COM extern __MANGLE__ int		nv_rename __PROTO__((Namval_t*,int));
2844887Schin extern __MANGLE__ int		nv_scan __PROTO__((Dt_t*,void(*)(Namval_t*,__V_*),__V_*,int,int));
2854887Schin extern __MANGLE__ char 		*nv_setdisc __PROTO__((Namval_t*,const char*,Namval_t*,Namfun_t*));
2864887Schin extern __MANGLE__ void		nv_setref __PROTO__((Namval_t*, Dt_t*,int));
2874887Schin extern __MANGLE__ int		nv_settype __PROTO__((Namval_t*, Namval_t*, int));
2884887Schin extern __MANGLE__ void 		nv_setvec __PROTO__((Namval_t*,int,int,char*[]));
2894887Schin extern __MANGLE__ void		nv_setvtree __PROTO__((Namval_t*));
2904887Schin extern __MANGLE__ int 		nv_setsize __PROTO__((Namval_t*,int));
2914887Schin extern __MANGLE__ Namfun_t		*nv_disc __PROTO__((Namval_t*,Namfun_t*,int));
2928462SApril.Chin@Sun.COM extern __MANGLE__ void 		nv_unset __PROTO__((Namval_t*));	 /*obsolete */
2938462SApril.Chin@Sun.COM extern __MANGLE__ void 		_nv_unset __PROTO__((Namval_t*,int));
2944887Schin extern __MANGLE__ Namval_t		*nv_search __PROTO__((const char *, Dt_t*, int));
2954887Schin extern __MANGLE__ char		*nv_name __PROTO__((Namval_t*));
2964887Schin extern __MANGLE__ Namval_t		*nv_type __PROTO__((Namval_t*));
2978462SApril.Chin@Sun.COM extern __MANGLE__ void		nv_addtype __PROTO__((Namval_t*,const char*, Optdisc_t*, size_t));
2984887Schin extern __MANGLE__ const Namdisc_t	*nv_discfun __PROTO__((int));
2994887Schin 
3004887Schin #ifdef _DLL
3014887Schin #undef __MANGLE__
3024887Schin #define __MANGLE__ __LINKAGE__
3034887Schin #endif /* _DLL */
3044887Schin 
3058462SApril.Chin@Sun.COM #define nv_unset(np)		_nv_unset(np,0)
3064887Schin #define nv_size(np)		nv_setsize((np),-1)
3074887Schin #define nv_stack(np,nf)		nv_disc(np,nf,0)
3084887Schin 
3094887Schin #if 0
3104887Schin /*
3114887Schin  * The names of many functions were changed in early '95
3124887Schin  * Here is a mapping to the old names
3134887Schin  */
3144887Schin #   define nv_istype(np)	nv_isattr(np)
3154887Schin #   define nv_newtype(np)	nv_newattr(np)
3164887Schin #   define nv_namset(np,a,b)	nv_open(np,a,b)
3178462SApril.Chin@Sun.COM #   define nv_free(np)		nv_unset(np,0)
3184887Schin #   define nv_settype(np,a,b,c)	nv_setdisc(np,a,b,c)
3194887Schin #   define nv_search(np,a,b)	nv_open(np,a,((b)?0:NV_NOADD))
3204887Schin #   define settype	setdisc
3214887Schin #endif
3224887Schin 
3234887Schin #endif /* NV_DEFAULT */
324