xref: /titanic_41/usr/src/lib/libshell/common/include/name.h (revision bd9f6899328e19cbb74e3ad02f5c32002285887e)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2009 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|NV_ARRAY)
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_UNJUST	0x8000000		/* clear justify attributes */
119 #define NV_FUNCTION	(NV_RJUST|NV_FUNCT)	/* value is shell function */
120 #define NV_FPOSIX	NV_LJUST		/* posix function semantics */
121 #define NV_FTMP		NV_ZFILL		/* function source in tmpfile */
122 
123 #define NV_NOPRINT	(NV_LTOU|NV_UTOL)	/* do not print */
124 #define NV_NOALIAS	(NV_NOPRINT|NV_IMPORT)
125 #define NV_NOEXPAND	NV_RJUST		/* do not expand alias */
126 #define NV_BLTIN	(NV_NOPRINT|NV_EXPORT)
127 #define BLT_ENV		(NV_RDONLY)		/* non-stoppable,
128 						 * can modify enviornment */
129 #define BLT_SPC		(NV_LJUST)		/* special built-ins */
130 #define BLT_EXIT	(NV_RJUST)		/* exit value can be > 255 */
131 #define BLT_DCL		(NV_TAGGED)		/* declaration command */
132 #define BLT_NOSFIO	(NV_IMPORT)		/* doesn't use sfio */
133 #define NV_OPTGET	(NV_BINARY)		/* function calls getopts */
134 #define nv_isref(n)	(nv_isattr((n),NV_REF|NV_TAGGED|NV_FUNCT)==NV_REF)
135 #define nv_istable(n)	(nv_isattr((n),NV_TABLE|NV_LJUST|NV_RJUST|NV_INTEGER)==NV_TABLE)
136 #define is_abuiltin(n)	(nv_isattr(n,NV_BLTIN|NV_INTEGER)==NV_BLTIN)
137 #define is_afunction(n)	(nv_isattr(n,NV_FUNCTION|NV_REF)==NV_FUNCTION)
138 #define	nv_funtree(n)	((n)->nvalue.rp->ptree)
139 #define	funptr(n)	((n)->nvalue.bfp)
140 
141 #define NV_SUBQUOTE	(NV_ADD<<1)	/* used with nv_endsubscript */
142 
143 /* NAMNOD MACROS */
144 /* ... for attributes */
145 
146 #define nv_setattr(n,f)	((n)->nvflag = (f))
147 #define nv_context(n)	((void*)(n)->nvfun)		/* for builtins */
148 /* The following are for name references */
149 #define nv_refnode(n)	((n)->nvalue.nrp->np)
150 #define nv_reftree(n)	((n)->nvalue.nrp->root)
151 #define nv_reftable(n)	((n)->nvalue.nrp->table)
152 #define nv_refsub(n)	((n)->nvalue.nrp->sub)
153 
154 /* ... etc */
155 
156 #define nv_setsize(n,s)	((n)->nvsize = (s))
157 #undef nv_size
158 #define nv_size(np)	((np)->nvsize)
159 #define nv_isnull(np)	(!(np)->nvalue.cp && !((np)->nvfun && (np)->nvfun->disc) && nv_isattr(np,NV_SHORT|NV_INTEGER)!=(NV_SHORT|NV_INTEGER))
160 
161 /* ...	for arrays */
162 
163 #define array_elem(ap)	((ap)->nelem&ARRAY_MASK)
164 #define array_assoc(ap)	((ap)->fun)
165 
166 extern int		array_maxindex(Namval_t*);
167 extern char 		*nv_endsubscript(Namval_t*, char*, int);
168 extern Namfun_t 	*nv_cover(Namval_t*);
169 extern Namarr_t 	*nv_arrayptr(Namval_t*);
170 extern int		nv_arrayisset(Namval_t*, Namarr_t*);
171 extern int		nv_arraysettype(Namval_t*, Namval_t*,const char*,int);
172 extern int		nv_aimax(Namval_t*);
173 extern int		nv_atypeindex(Namval_t*, const char*);
174 extern int		nv_setnotify(Namval_t*,char **);
175 extern int		nv_unsetnotify(Namval_t*,char **);
176 extern void		nv_setlist(struct argnod*, int, Namval_t*);
177 extern struct argnod*	nv_onlist(struct argnod*, const char*);
178 extern void 		nv_optimize(Namval_t*);
179 extern void		nv_outname(Sfio_t*,char*, int);
180 extern void 		nv_unref(Namval_t*);
181 extern void		_nv_unset(Namval_t*,int);
182 extern int		nv_clone(Namval_t*, Namval_t*, int);
183 void			clone_all_disc(Namval_t*, Namval_t*, int);
184 extern Namfun_t		*nv_clone_disc(Namfun_t*, int);
185 extern void		*nv_diropen(Namval_t*, const char*);
186 extern char		*nv_dirnext(void*);
187 extern void		nv_dirclose(void*);
188 extern char		*nv_getvtree(Namval_t*, Namfun_t*);
189 extern void		nv_attribute(Namval_t*, Sfio_t*, char*, int);
190 extern Namval_t		*nv_bfsearch(const char*, Dt_t*, Namval_t**, char**);
191 extern Namval_t		*nv_mkclone(Namval_t*);
192 extern Namval_t		*nv_mktype(Namval_t**, int);
193 extern Namval_t		*nv_addnode(Namval_t*, int);
194 extern Namval_t		*nv_parent(Namval_t*);
195 extern char		*nv_getbuf(size_t);
196 extern Namval_t		*nv_mount(Namval_t*, const char *name, Dt_t*);
197 extern Namval_t		*nv_arraychild(Namval_t*, Namval_t*, int);
198 extern int		nv_compare(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
199 extern void		nv_outnode(Namval_t*,Sfio_t*, int, int);
200 extern int		nv_subsaved(Namval_t*);
201 extern void		nv_typename(Namval_t*, Sfio_t*);
202 extern void		nv_newtype(Namval_t*);
203 
204 extern const Namdisc_t	RESTRICTED_disc;
205 extern const Namdisc_t	ENUM_disc;
206 extern char		nv_local;
207 extern Dtdisc_t		_Nvdisc;
208 extern const char	*nv_discnames[];
209 extern const char	e_subscript[];
210 extern const char	e_nullset[];
211 extern const char	e_notset[];
212 extern const char	e_noparent[];
213 extern const char	e_notelem[];
214 extern const char	e_readonly[];
215 extern const char	e_badfield[];
216 extern const char	e_restricted[];
217 extern const char	e_ident[];
218 extern const char	e_varname[];
219 extern const char	e_noalias[];
220 extern const char	e_noarray[];
221 extern const char	e_notenum[];
222 extern const char	e_aliname[];
223 extern const char	e_badexport[];
224 extern const char	e_badref[];
225 extern const char	e_badsubscript[];
226 extern const char	e_noref[];
227 extern const char	e_selfref[];
228 extern const char	e_envmarker[];
229 extern const char	e_badlocale[];
230 extern const char	e_loop[];
231 extern const char	e_redef[];
232 extern const char	e_required[];
233 extern const char	e_badappend[];
234 extern const char	e_unknowntype[];
235 extern const char	e_globalref[];
236 #endif /* _NV_PRIVATE */
237