xref: /titanic_44/usr/src/lib/libshell/common/include/name.h (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
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_PRIVATE
22 /*
23  * This is the implementation header file for name-value pairs
24  */
25 
26 #define _NV_PRIVATE	\
27 	Namfun_t	*nvfun;		/* pointer to trap functions */ \
28 	union Value	nvalue; 	/* value field */ \
29 	char		*nvenv;		/* pointer to environment name */
30 
31 #include	<ast.h>
32 #include	<cdt.h>
33 #include	"shtable.h"
34 
35 /* Nodes can have all kinds of values */
36 union Value
37 {
38 	const char		*cp;
39 	int			*ip;
40 	char			c;
41 	int			i;
42 	unsigned int		u;
43 	int32_t			*lp;
44 	Sflong_t		*llp;	/* for long long arithmetic */
45 	int16_t			s;
46 	double			*dp;	/* for floating point arithmetic */
47 	Sfdouble_t		*ldp;	/* for long floating point arithmetic */
48 	struct Namarray		*array;	/* for array node */
49 	struct Namval		*np;	/* for Namval_t node */
50 	union Value		*up;	/* for indirect node */
51 	struct Ufunction 	*rp;	/* shell user defined functions */
52 	struct Namfun		*funp;	/* discipline pointer */
53 	struct Namref		*nrp;	/* name reference */
54 	int			(*bfp)(int,char*[],void*);/* builtin entry point function pointer */
55 };
56 
57 #include	"nval.h"
58 
59 /* used for arrays */
60 
61 #define ARRAY_MAX 	(1L<<ARRAY_BITS) /* maximum number of elements in an array */
62 #define ARRAY_MASK	(ARRAY_MAX-1)	/* For index values */
63 
64 #define ARRAY_INCR	32	/* number of elements to grow when array
65 				   bound exceeded.  Must be a power of 2 */
66 #define ARRAY_FILL	(8L<<ARRAY_BITS)	/* used with nv_putsub() */
67 #define ARRAY_NOCLONE	(16L<<ARRAY_BITS)	/* do not clone array disc */
68 #define ARRAY_NOCHILD   (32L<<ARRAY_BITS)	/* skip compound arrays */
69 #define ARRAY_SETSUB	(64L<<ARRAY_BITS)	/* set subscript */
70 #define NV_ASETSUB	8			/* set subscript */
71 
72 /* These flags are used as options to array_get() */
73 #define ARRAY_ASSIGN	0
74 #define ARRAY_LOOKUP	1
75 #define ARRAY_DELETE	2
76 
77 
78 struct Namref
79 {
80 	Namval_t	*np;
81 	Namval_t	*table;
82 	Dt_t		*root;
83 	char		*sub;
84 };
85 
86 /* This describes a user shell function node */
87 struct Ufunction
88 {
89 	int	*ptree;			/* address of parse tree */
90 	int	lineno;			/* line number of function start */
91 	off_t	hoffset;		/* offset into source or history file */
92 	Namval_t *nspace;		/* pointer to name space */
93 	char	*fname;			/* file name where function defined */
94 };
95 
96 #ifndef ARG_RAW
97     struct argnod;
98 #endif /* !ARG_RAW */
99 
100 /* attributes of Namval_t items */
101 
102 /* The following attributes are for internal use */
103 #define NV_NOCHANGE	(NV_EXPORT|NV_IMPORT|NV_RDONLY|NV_TAGGED|NV_NOFREE)
104 #define NV_ATTRIBUTES	(~(NV_NOSCOPE|NV_ARRAY|NV_NOARRAY|NV_IDENT|NV_ASSIGN|NV_REF|NV_VARNAME))
105 #define NV_PARAM	NV_NODISC	/* expansion use positional params */
106 
107 /* This following are for use with nodes which are not name-values */
108 #define NV_TYPE		0x1000000
109 #define NV_FUNCTION	(NV_RJUST|NV_FUNCT)	/* value is shell function */
110 #define NV_FPOSIX	NV_LJUST		/* posix function semantics */
111 #define NV_FTMP		NV_ZFILL		/* function source in tmpfile */
112 
113 #define NV_NOPRINT	(NV_LTOU|NV_UTOL)	/* do not print */
114 #define NV_NOALIAS	(NV_NOPRINT|NV_IMPORT)
115 #define NV_NOEXPAND	NV_RJUST		/* do not expand alias */
116 #define NV_BLTIN	(NV_NOPRINT|NV_EXPORT)
117 #define BLT_ENV		(NV_RDONLY)		/* non-stoppable,
118 						 * can modify enviornment */
119 #define BLT_SPC		(NV_LJUST)		/* special built-ins */
120 #define BLT_EXIT	(NV_RJUST)		/* exit value can be > 255 */
121 #define BLT_DCL		(NV_TAGGED)		/* declaration command */
122 #define nv_isref(n)	(nv_isattr((n),NV_REF)==NV_REF)
123 #define nv_istable(n)	(nv_isattr((n),NV_TABLE|NV_LJUST|NV_RJUST)==NV_TABLE)
124 #define is_abuiltin(n)	(nv_isattr(n,NV_BLTIN)==NV_BLTIN)
125 #define is_afunction(n)	(nv_isattr(n,NV_FUNCTION)==NV_FUNCTION)
126 #define	nv_funtree(n)	((n)->nvalue.rp->ptree)
127 #define	funptr(n)	((n)->nvalue.bfp)
128 
129 #define NV_SUBQUOTE	(NV_ADD<<1)	/* used with nv_endsubscript */
130 
131 /* NAMNOD MACROS */
132 /* ... for attributes */
133 
134 #define nv_setattr(n,f)	((n)->nvflag = (f))
135 #define nv_context(n)	((void*)(n)->nvfun)		/* for builtins */
136 /* The following are for name references */
137 #define nv_refnode(n)	((n)->nvalue.nrp->np)
138 #define nv_reftree(n)	((n)->nvalue.nrp->root)
139 #define nv_reftable(n)	((n)->nvalue.nrp->table)
140 #define nv_refsub(n)	((n)->nvalue.nrp->sub)
141 #if SHOPT_OO
142 #   define nv_class(np)		(nv_isattr(np,NV_REF|NV_IMPORT)?0:(Namval_t*)((np)->nvenv))
143 #endif /* SHOPT_OO */
144 
145 /* ... etc */
146 
147 #define nv_setsize(n,s)	((n)->nvsize = (s))
148 #undef nv_size
149 #define nv_size(np)	((np)->nvsize)
150 #define nv_isnull(np)	(!(np)->nvalue.cp && !(np)->nvfun && !nv_isattr(np,NV_SHORT))
151 
152 /* ...	for arrays */
153 
154 #define array_elem(ap)	((ap)->nelem&ARRAY_MASK)
155 #define array_assoc(ap)	((ap)->fun)
156 
157 extern int		array_maxindex(Namval_t*);
158 extern char 		*nv_endsubscript(Namval_t*, char*, int);
159 extern Namfun_t 	*nv_cover(Namval_t*);
160 extern Namarr_t 	*nv_arrayptr(Namval_t*);
161 extern int		nv_setnotify(Namval_t*,char **);
162 extern int		nv_unsetnotify(Namval_t*,char **);
163 extern void		nv_setlist(struct argnod*, int);
164 extern void 		nv_optimize(Namval_t*);
165 extern void		nv_outname(Sfio_t*,char*, int);
166 extern void 		nv_scope(struct argnod*);
167 extern void 		nv_unref(Namval_t*);
168 extern void		_nv_unset(Namval_t*,int);
169 extern int		nv_clone(Namval_t*, Namval_t*, int);
170 extern void		*nv_diropen(const char*);
171 extern char		*nv_dirnext(void*);
172 extern void		nv_dirclose(void*);
173 extern char		*nv_getvtree(Namval_t*, Namfun_t*);
174 extern void		nv_attribute(Namval_t*, Sfio_t*, char*, int);
175 extern Namval_t		*nv_bfsearch(const char*, Dt_t*, Namval_t**, char**);
176 extern Namval_t		*nv_mkclone(Namval_t*);
177 extern Namval_t		*nv_mktype(Namval_t**, int);
178 extern void		nv_addnode(Namval_t*, int);
179 extern Namval_t		*nv_parent(Namval_t*);
180 extern char		*nv_getbuf(size_t);
181 extern Namval_t		*nv_mount(Namval_t*, const char *name, Dt_t*);
182 extern Namval_t		*nv_arraychild(Namval_t*, Namval_t*, int);
183 extern int		nv_compare(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
184 
185 extern const Namdisc_t	RESTRICTED_disc;
186 extern char		nv_local;
187 extern Dtdisc_t		_Nvdisc;
188 extern const char	e_subscript[];
189 extern const char	e_nullset[];
190 extern const char	e_notset[];
191 extern const char	e_noparent[];
192 extern const char	e_readonly[];
193 extern const char	e_badfield[];
194 extern const char	e_restricted[];
195 extern const char	e_ident[];
196 extern const char	e_varname[];
197 extern const char	e_noalias[];
198 extern const char	e_noarray[];
199 extern const char	e_aliname[];
200 extern const char	e_badexport[];
201 extern const char	e_badref[];
202 extern const char	e_noref[];
203 extern const char	e_selfref[];
204 extern const char	e_envmarker[];
205 extern const char	e_badlocale[];
206 extern const char	e_loop[];
207 extern const char	e_redef[];
208 #endif /* _NV_PRIVATE */
209