xref: /illumos-gate/usr/src/cmd/fm/eversholt/common/tree.h (revision cbab2b2687744cbfdc12fae90f8088127a0b266c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * tree.h -- public definitions for tree module
27  *
28  * the parse tree is made up of struct node's.  the struct is
29  * a "variant record" with a type, the filename and line number
30  * related to the node, and then type-specific node data.
31  */
32 
33 #ifndef	_ESC_COMMON_TREE_H
34 #define	_ESC_COMMON_TREE_H
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 struct node {
43 	enum nodetype {
44 		T_NOTHING = 1000,	/* used to keep going on error cases */
45 		T_NAME,			/* identifiers, sometimes chained */
46 		T_GLOBID,		/* globals (e.g. $a) */
47 		T_EVENT,		/* class@path{expr} */
48 		T_ENGINE,		/* upset threshold engine (e.g. SERD) */
49 		T_ASRU,			/* ASRU declaration */
50 		T_FRU,			/* FRU declaration */
51 		T_TIMEVAL,		/* num w/time suffix (ns internally) */
52 		T_NUM,			/* num (ull internally) */
53 		T_QUOTE,		/* quoted string */
54 		T_FUNC,			/* func(arglist) */
55 		T_NVPAIR,		/* name=value pair in decl */
56 		T_ASSIGN,		/* assignment statement */
57 		T_CONDIF,		/* a and T_CONDELSE in (a ? b : c ) */
58 		T_CONDELSE,		/* lists b and c in (a ? b : c ) */
59 		T_NOT,			/* boolean ! operator */
60 		T_AND,			/* boolean && operator */
61 		T_OR,			/* boolean || operator */
62 		T_EQ,			/* boolean == operator */
63 		T_NE,			/* boolean != operator */
64 		T_SUB,			/* integer - operator */
65 		T_ADD,			/* integer + operator */
66 		T_MUL,			/* integer * operator */
67 		T_DIV,			/* integer / operator */
68 		T_MOD,			/* integer % operator */
69 		T_LT,			/* boolean < operator */
70 		T_LE,			/* boolean <= operator */
71 		T_GT,			/* boolean > operator */
72 		T_GE,			/* boolean >= operator */
73 		T_BITAND,		/* bitwise & operator */
74 		T_BITOR,		/* bitwise | operator */
75 		T_BITXOR,		/* bitwise ^ operator */
76 		T_BITNOT,		/* bitwise ~ operator */
77 		T_LSHIFT,		/* bitwise << operator */
78 		T_RSHIFT,		/* bitwise >> operator */
79 		T_ARROW,		/* lhs (N)->(K) rhs */
80 		T_LIST,			/* comma-separated list */
81 		T_FAULT,		/* fault declaration */
82 		T_UPSET,		/* upset declaration */
83 		T_DEFECT,		/* defect declaration */
84 		T_ERROR,		/* error declaration */
85 		T_EREPORT,		/* ereport declaration */
86 		T_SERD,			/* SERD engine declaration */
87 		T_STAT,			/* STAT engine declaration */
88 		T_PROP,			/* prop statement */
89 		T_MASK,			/* mask statement */
90 		T_CONFIG		/* config statement */
91 	} t;
92 
93 	/*
94 	 * regardless of the type of node, filename and line number
95 	 * information from the original .esc file is tracked here.
96 	 */
97 	const char *file;
98 	int line;
99 
100 	/*
101 	 * the variant part of a struct node...
102 	 */
103 	union {
104 		struct {
105 			/*
106 			 * info kept for T_NAME, used in several ways:
107 			 *
108 			 *	1 for simple variable names.
109 			 *		example: j
110 			 *
111 			 *	2 for event class names, with component
112 			 *	  names chained together via the "next"
113 			 *	  pointers.
114 			 *		example: fault.fan.broken
115 			 *
116 			 *	3 for component pathnames, with component
117 			 *	  names chained together via the "next"
118 			 *	  pointers and iterators or instance numbers
119 			 *	  attached via the "child" pointers.
120 			 *		example: sysboard[0]/cpu[n]
121 			 *
122 			 * case 3 is the most interesting.
123 			 *	- if child is set, there's an iterator
124 			 *	- if child is a T_NAME, it is x[j] or x<j> and
125 			 *	  iterator type tells you vertical or horizontal
126 			 *	- if child is a T_NUM, it is x[0] or x<0> or
127 			 *	  x0 and iterator type tells you which one
128 			 *	- if cp pointer is set, then we recently
129 			 *	  matched it to a config cache entry and one
130 			 *	  can ignore child for now because it still
131 			 *	  represents the *pattern* you're matching.
132 			 *	  cp represents what you matched.  ptree()
133 			 *	  knows that if cp is set, to print that number
134 			 *	  instead of following child.
135 			 *
136 			 * when T_NAME nodes are chained:
137 			 * the "last" pointer takes you to the end of the
138 			 * chain, but only the first component's last pointer
139 			 * is kept up to date.  it is used to determine
140 			 * where to append newly-created T_NAME nodes (see
141 			 * tree_name_append()).
142 			 */
143 			const char *s;		/* the name itself */
144 
145 			struct node *child;
146 			struct node *next;
147 			struct node *last;
148 
149 			/* opaque pointer used during config matching */
150 			struct config *cp;
151 
152 			enum nametype {
153 				N_UNSPEC,
154 				N_FAULT,
155 				N_UPSET,
156 				N_DEFECT,
157 				N_ERROR,
158 				N_EREPORT,
159 				N_SERD,
160 				N_STAT
161 			} t:3;
162 			enum itertype {
163 				IT_NONE,
164 				IT_VERTICAL,
165 				IT_HORIZONTAL,
166 				IT_ENAME
167 			} it:2;
168 			unsigned childgen:1;	/* child was auto-generated */
169 		} name;
170 
171 		struct {
172 			/*
173 			 * info kept for T_GLOBID
174 			 */
175 			const char *s;		/* the name itself */
176 		} globid;
177 
178 		/*
179 		 * info kept for T_TIMEVAL and T_NUM
180 		 *
181 		 * timevals are kept in nanoseconds.
182 		 */
183 		unsigned long long ull;
184 
185 		struct {
186 			/*
187 			 * info kept for T_QUOTE
188 			 */
189 			const char *s;		/* the quoted string */
190 		} quote;
191 
192 		struct {
193 			/*
194 			 * info kept for T_FUNC
195 			 */
196 			const char *s;		/* name of function */
197 			struct node *arglist;
198 		} func;
199 
200 		struct {
201 			/*
202 			 * info kept for T_PROP and T_MASK statements
203 			 * as well as declarations for:
204 			 *	T_FAULT
205 			 *	T_UPSET
206 			 *	T_DEFECT
207 			 *	T_ERROR
208 			 *	T_EREPORT
209 			 *	T_ASRU
210 			 *	T_FRU
211 			 *	T_CONFIG
212 			 */
213 			struct node *np;
214 			struct node *nvpairs;	/* for declarations */
215 			struct lut *lutp;	/* for declarations */
216 			struct node *next;	/* for Props & Masks lists */
217 			struct node *expr;	/* for if statements */
218 			unsigned char flags;	/* see STMT_ flags below */
219 		} stmt;			/* used for stmt */
220 
221 		struct {
222 			/*
223 			 * info kept for T_EVENT
224 			 */
225 			struct node *ename;	/* event class name */
226 			struct node *epname;	/* component path name */
227 			struct node *eexprlist;	/* constraint expression */
228 			struct node *declp;	/* event declaration */
229 		} event;
230 
231 		struct {
232 			/*
233 			 * info kept for T_ARROW
234 			 */
235 			struct node *lhs;	/* left side of arrow */
236 			struct node *rhs;	/* right side of arrow */
237 			struct node *nnp;	/* N value */
238 			struct node *knp;	/* K value */
239 			struct node *prop;	/* arrow is part of this prop */
240 		} arrow;
241 
242 		struct {
243 			/*
244 			 * info kept for everything else (T_ADD, T_LIST, etc.)
245 			 */
246 			struct node *left;
247 			struct node *right;
248 		} expr;
249 	} u;
250 };
251 
252 /* flags we keep with stmts */
253 #define	STMT_REF	0x01	/* declared item is referenced */
254 #define	STMT_CYMARK	0x02	/* declared item is marked for cycle check */
255 #define	STMT_CYCLE	0x04	/* cycle detected and already reported */
256 
257 #define	TIMEVAL_EVENTUALLY (1000000000ULL*60*60*24*365*100)	/* 100 years */
258 
259 void tree_init(void);
260 void tree_fini(void);
261 struct node *newnode(enum nodetype t, const char *file, int line);
262 void tree_free(struct node *root);
263 struct node *tree_root(struct node *np);
264 struct node *tree_nothing(void);
265 struct node *tree_expr(enum nodetype t, struct node *left, struct node *right);
266 struct node *tree_event(struct node *ename, struct node *epname,
267     struct node *eexprlist);
268 struct node *tree_if(struct node *expr, struct node *stmts,
269     const char *file, int line);
270 struct node *tree_name(const char *s, enum itertype it,
271     const char *file, int line);
272 struct node *tree_iname(const char *s, const char *file, int line);
273 struct node *tree_globid(const char *s, const char *file, int line);
274 struct node *tree_name_append(struct node *np1, struct node *np2);
275 struct node *tree_name_repairdash(struct node *np1, const char *s);
276 struct node *tree_name_iterator(struct node *np1, struct node *np2);
277 struct node *tree_timeval(const char *s, const char *suffix,
278     const char *file, int line);
279 struct node *tree_num(const char *s, const char *file, int line);
280 struct node *tree_quote(const char *s, const char *file, int line);
281 struct node *tree_func(const char *s, struct node *np,
282     const char *file, int line);
283 struct node *tree_pname(struct node *np);
284 struct node *tree_arrow(struct node *lhs, struct node *nnp, struct node *knp,
285     struct node *rhs);
286 struct lut *tree_s2np_lut_add(struct lut *root, const char *s, struct node *np);
287 struct node *tree_s2np_lut_lookup(struct lut *root, const char *s);
288 struct lut *tree_name2np_lut_add(struct lut *root,
289     struct node *namep, struct node *np);
290 struct node *tree_name2np_lut_lookup(struct lut *root, struct node *namep);
291 struct node *tree_name2np_lut_lookup_name(struct lut *root, struct node *namep);
292 struct lut *tree_event2np_lut_add(struct lut *root,
293     struct node *enp, struct node *np);
294 struct node *tree_event2np_lut_lookup(struct lut *root, struct node *enp);
295 struct node *tree_event2np_lut_lookup_event(struct lut *root,
296     struct node *enp);
297 struct node *tree_decl(enum nodetype t, struct node *enp, struct node *nvpairs,
298     const char *file, int line);
299 struct node *tree_stmt(enum nodetype t, struct node *np,
300     const char *file, int line);
301 void tree_report();
302 int tree_namecmp(struct node *np1, struct node *np2);
303 int tree_eventcmp(struct node *np1, struct node *np2);
304 
305 struct lut *Faults;
306 struct lut *Upsets;
307 struct lut *Defects;
308 struct lut *Errors;
309 struct lut *Ereports;
310 struct lut *Ereportenames;
311 struct lut *SERDs;
312 struct lut *STATs;
313 struct lut *ASRUs;
314 struct lut *FRUs;
315 struct lut *Configs;
316 struct node *Props;
317 struct node *Lastprops;
318 struct node *Masks;
319 struct node *Lastmasks;
320 struct node *Problems;
321 struct node *Lastproblems;
322 
323 #ifdef	__cplusplus
324 }
325 #endif
326 
327 #endif	/* _ESC_COMMON_TREE_H */
328