xref: /titanic_50/usr/src/lib/libshell/common/include/name.h (revision ae6aa22afeb444ae208c287e7227a4a7c877f17a)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2008 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_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 	int16_t			*sp;
47 	double			*dp;	/* for floating point arithmetic */
48 	Sfdouble_t		*ldp;	/* for long floating point arithmetic */
49 	struct Namarray		*array;	/* for array node */
50 	struct Namval		*np;	/* for Namval_t node */
51 	union Value		*up;	/* for indirect node */
52 	struct Ufunction 	*rp;	/* shell user defined functions */
53 	struct Namfun		*funp;	/* discipline pointer */
54 	struct Namref		*nrp;	/* name reference */
55 	int			(*bfp)(int,char*[],void*);/* builtin entry point function pointer */
56 };
57 
58 #include	"nval.h"
59 
60 /* used for arrays */
61 
62 #define ARRAY_MAX 	(1L<<ARRAY_BITS) /* maximum number of elements in an array */
63 #define ARRAY_MASK	(ARRAY_MAX-1)	/* For index values */
64 
65 #define ARRAY_INCR	32	/* number of elements to grow when array
66 				   bound exceeded.  Must be a power of 2 */
67 #define ARRAY_FILL	(8L<<ARRAY_BITS)	/* used with nv_putsub() */
68 #define ARRAY_NOCLONE	(16L<<ARRAY_BITS)	/* do not clone array disc */
69 #define ARRAY_NOCHILD   (32L<<ARRAY_BITS)	/* skip compound arrays */
70 #define ARRAY_SETSUB	(64L<<ARRAY_BITS)	/* set subscript */
71 #define ARRAY_NOSCOPE	(128L<<ARRAY_BITS)	/* top level scope only */
72 #define ARRAY_TREE	(256L<<ARRAY_BITS)	/* arrays of compound vars */
73 #define NV_ASETSUB	8			/* set subscript */
74 
75 /* These flags are used as options to array_get() */
76 #define ARRAY_ASSIGN	0
77 #define ARRAY_LOOKUP	1
78 #define ARRAY_DELETE	2
79 
80 
81 struct Namref
82 {
83 	Namval_t	*np;
84 	Namval_t	*table;
85 	Dt_t		*root;
86 	char		*sub;
87 };
88 
89 /* This describes a user shell function node */
90 struct Ufunction
91 {
92 	int		*ptree;		/* address of parse tree */
93 	int		lineno;		/* line number of function start */
94 	off_t		hoffset;	/* offset into source or history file */
95 	Namval_t	*nspace;	/* pointer to name space */
96 	char		*fname;		/* file name where function defined */
97 	char		*help;		/* help string */
98 	Dt_t		*sdict;		/* dictionary for statics */
99 	Dt_t		*fdict;		/* dictionary node belongs to */
100 	Namval_t	*np;		/* function node pointer */
101 };
102 
103 #ifndef ARG_RAW
104     struct argnod;
105 #endif /* !ARG_RAW */
106 
107 /* attributes of Namval_t items */
108 
109 /* The following attributes are for internal use */
110 #define NV_NOCHANGE	(NV_EXPORT|NV_IMPORT|NV_RDONLY|NV_TAGGED|NV_NOFREE)
111 #define NV_ATTRIBUTES	(~(NV_NOSCOPE|NV_ARRAY|NV_NOARRAY|NV_IDENT|NV_ASSIGN|NV_REF|NV_VARNAME|NV_STATIC))
112 #define NV_PARAM	NV_NODISC	/* expansion use positional params */
113 
114 /* This following are for use with nodes which are not name-values */
115 #define NV_TYPE		0x1000000
116 #define NV_STATIC	0x2000000
117 #define NV_COMVAR	0x4000000
118 #define NV_FUNCTION	(NV_RJUST|NV_FUNCT)	/* value is shell function */
119 #define NV_FPOSIX	NV_LJUST		/* posix function semantics */
120 #define NV_FTMP		NV_ZFILL		/* function source in tmpfile */
121 
122 #define NV_NOPRINT	(NV_LTOU|NV_UTOL)	/* do not print */
123 #define NV_NOALIAS	(NV_NOPRINT|NV_IMPORT)
124 #define NV_NOEXPAND	NV_RJUST		/* do not expand alias */
125 #define NV_BLTIN	(NV_NOPRINT|NV_EXPORT)
126 #define BLT_ENV		(NV_RDONLY)		/* non-stoppable,
127 						 * can modify enviornment */
128 #define BLT_SPC		(NV_LJUST)		/* special built-ins */
129 #define BLT_EXIT	(NV_RJUST)		/* exit value can be > 255 */
130 #define BLT_DCL		(NV_TAGGED)		/* declaration command */
131 #define BLT_NOSFIO	(NV_IMPORT)		/* doesn't use sfio */
132 #define NV_OPTGET	(NV_BINARY)		/* function calls getopts */
133 #define nv_isref(n)	(nv_isattr((n),NV_REF|NV_TAGGED|NV_FUNCT)==NV_REF)
134 #define nv_istable(n)	(nv_isattr((n),NV_TABLE|NV_LJUST|NV_RJUST|NV_INTEGER)==NV_TABLE)
135 #define is_abuiltin(n)	(nv_isattr(n,NV_BLTIN|NV_INTEGER)==NV_BLTIN)
136 #define is_afunction(n)	(nv_isattr(n,NV_FUNCTION|NV_REF)==NV_FUNCTION)
137 #define	nv_funtree(n)	((n)->nvalue.rp->ptree)
138 #define	funptr(n)	((n)->nvalue.bfp)
139 
140 #define NV_SUBQUOTE	(NV_ADD<<1)	/* used with nv_endsubscript */
141 
142 /* NAMNOD MACROS */
143 /* ... for attributes */
144 
145 #define nv_setattr(n,f)	((n)->nvflag = (f))
146 #define nv_context(n)	((void*)(n)->nvfun)		/* for builtins */
147 /* The following are for name references */
148 #define nv_refnode(n)	((n)->nvalue.nrp->np)
149 #define nv_reftree(n)	((n)->nvalue.nrp->root)
150 #define nv_reftable(n)	((n)->nvalue.nrp->table)
151 #define nv_refsub(n)	((n)->nvalue.nrp->sub)
152 
153 /* ... etc */
154 
155 #define nv_setsize(n,s)	((n)->nvsize = (s))
156 #undef nv_size
157 #define nv_size(np)	((np)->nvsize)
158 #define nv_isnull(np)	(!(np)->nvalue.cp && !((np)->nvfun && (np)->nvfun->disc) && nv_isattr(np,NV_SHORT|NV_INTEGER)!=(NV_SHORT|NV_INTEGER))
159 
160 /* ...	for arrays */
161 
162 #define array_elem(ap)	((ap)->nelem&ARRAY_MASK)
163 #define array_assoc(ap)	((ap)->fun)
164 
165 extern int		array_maxindex(Namval_t*);
166 extern char 		*nv_endsubscript(Namval_t*, char*, int);
167 extern Namfun_t 	*nv_cover(Namval_t*);
168 extern Namarr_t 	*nv_arrayptr(Namval_t*);
169 extern int		nv_arraysettype(Namval_t*, Namval_t*,const char*,int);
170 extern int		nv_aimax(Namval_t*);
171 extern int		nv_atypeindex(Namval_t*, const char*);
172 extern int		nv_setnotify(Namval_t*,char **);
173 extern int		nv_unsetnotify(Namval_t*,char **);
174 extern void		nv_setlist(struct argnod*, int);
175 extern struct argnod*	nv_onlist(struct argnod*, const char*);
176 extern void 		nv_optimize(Namval_t*);
177 extern void		nv_outname(Sfio_t*,char*, int);
178 extern void 		nv_unref(Namval_t*);
179 extern void		_nv_unset(Namval_t*,int);
180 extern int		nv_clone(Namval_t*, Namval_t*, int);
181 void			clone_all_disc(Namval_t*, Namval_t*, int);
182 extern Namfun_t		*nv_clone_disc(Namfun_t*, int);
183 extern void		*nv_diropen(Namval_t*, const char*);
184 extern char		*nv_dirnext(void*);
185 extern void		nv_dirclose(void*);
186 extern char		*nv_getvtree(Namval_t*, Namfun_t*);
187 extern void		nv_attribute(Namval_t*, Sfio_t*, char*, int);
188 extern Namval_t		*nv_bfsearch(const char*, Dt_t*, Namval_t**, char**);
189 extern Namval_t		*nv_mkclone(Namval_t*);
190 extern Namval_t		*nv_mktype(Namval_t**, int);
191 extern Namval_t		*nv_addnode(Namval_t*, int);
192 extern Namval_t		*nv_parent(Namval_t*);
193 extern char		*nv_getbuf(size_t);
194 extern Namval_t		*nv_mount(Namval_t*, const char *name, Dt_t*);
195 extern Namval_t		*nv_arraychild(Namval_t*, Namval_t*, int);
196 extern int		nv_compare(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
197 extern void		nv_outnode(Namval_t*,Sfio_t*, int, int);
198 extern int		nv_subsaved(Namval_t*);
199 extern void		nv_typename(Namval_t*, Sfio_t*);
200 
201 extern const Namdisc_t	RESTRICTED_disc;
202 extern const Namdisc_t	ENUM_disc;
203 extern char		nv_local;
204 extern Dtdisc_t		_Nvdisc;
205 extern const char	*nv_discnames[];
206 extern const char	e_subscript[];
207 extern const char	e_nullset[];
208 extern const char	e_notset[];
209 extern const char	e_noparent[];
210 extern const char	e_notelem[];
211 extern const char	e_readonly[];
212 extern const char	e_badfield[];
213 extern const char	e_restricted[];
214 extern const char	e_ident[];
215 extern const char	e_varname[];
216 extern const char	e_noalias[];
217 extern const char	e_noarray[];
218 extern const char	e_notenum[];
219 extern const char	e_aliname[];
220 extern const char	e_badexport[];
221 extern const char	e_badref[];
222 extern const char	e_noref[];
223 extern const char	e_selfref[];
224 extern const char	e_envmarker[];
225 extern const char	e_badlocale[];
226 extern const char	e_loop[];
227 extern const char	e_redef[];
228 extern const char	e_required[];
229 extern const char	e_badappend[];
230 extern const char	e_unknowntype[];
231 #endif /* _NV_PRIVATE */
232