10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1717Swesolows * Common Development and Distribution License (the "License").
6*1717Swesolows * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*1717Swesolows
220Sstevel@tonic-gate /*
231414Scindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Events, FMRIs and authorities must be declared before they can be used.
310Sstevel@tonic-gate * Routines in this file, driven by the parser, create the data structures
320Sstevel@tonic-gate * associated with the declarations.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <assert.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <inj_event.h>
390Sstevel@tonic-gate #include <inj_err.h>
400Sstevel@tonic-gate #include <inj_lex.h>
410Sstevel@tonic-gate #include <inj_list.h>
420Sstevel@tonic-gate #include <inj.h>
430Sstevel@tonic-gate
441414Scindi static inj_hash_t inj_decls[ITEMTYPE_NITEMS];
450Sstevel@tonic-gate static int inj_decls_initialized;
460Sstevel@tonic-gate
470Sstevel@tonic-gate static inj_hash_t *
item2hash(inj_itemtype_t item)480Sstevel@tonic-gate item2hash(inj_itemtype_t item)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate int i;
510Sstevel@tonic-gate
520Sstevel@tonic-gate assert(item >= 0 && item < sizeof (inj_decls) / sizeof (inj_hash_t));
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (!inj_decls_initialized) {
550Sstevel@tonic-gate for (i = 0; i < sizeof (inj_decls) / sizeof (inj_hash_t); i++)
560Sstevel@tonic-gate inj_strhash_create(&inj_decls[i]);
570Sstevel@tonic-gate inj_decls_initialized = 1;
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate return (&inj_decls[item]);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate inj_decl_t *
inj_decl_lookup(const char * name,inj_itemtype_t type)640Sstevel@tonic-gate inj_decl_lookup(const char *name, inj_itemtype_t type)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate inj_hash_t *hash = item2hash(type);
670Sstevel@tonic-gate inj_var_t *v;
680Sstevel@tonic-gate
690Sstevel@tonic-gate if ((v = inj_strhash_lookup(hash, name)) == NULL)
700Sstevel@tonic-gate return (NULL);
710Sstevel@tonic-gate
720Sstevel@tonic-gate return (inj_hash_get_cookie(v));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate void
inj_decl_mem_destroy(inj_declmem_t * dlm)760Sstevel@tonic-gate inj_decl_mem_destroy(inj_declmem_t *dlm)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate inj_strfree(dlm->dlm_name);
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (dlm->dlm_type == MEMTYPE_ENUM)
810Sstevel@tonic-gate inj_strhash_destroy(dlm->dlm_enumvals);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create(const char * name,inj_memtype_t type)850Sstevel@tonic-gate inj_decl_mem_create(const char *name, inj_memtype_t type)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
880Sstevel@tonic-gate
890Sstevel@tonic-gate dlm->dlm_name = name;
900Sstevel@tonic-gate dlm->dlm_type = type;
910Sstevel@tonic-gate
920Sstevel@tonic-gate return (dlm);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate /* An embedded event, authority, or FMRI */
960Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create_defined(const char * name,const char * declnm,inj_itemtype_t type)970Sstevel@tonic-gate inj_decl_mem_create_defined(const char *name, const char *declnm,
980Sstevel@tonic-gate inj_itemtype_t type)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate dlm->dlm_name = name;
1030Sstevel@tonic-gate dlm->dlm_type = inj_item2mem(type);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if ((dlm->dlm_decl = inj_decl_lookup(declnm, type)) == NULL) {
1060Sstevel@tonic-gate yyerror("unknown %s %s", inj_item2str(type), declnm);
1070Sstevel@tonic-gate return (NULL);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate return (dlm);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create_enum(const char * name,inj_hash_t * vals)1140Sstevel@tonic-gate inj_decl_mem_create_enum(const char *name, inj_hash_t *vals)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate dlm->dlm_name = name;
1190Sstevel@tonic-gate dlm->dlm_type = MEMTYPE_ENUM;
1200Sstevel@tonic-gate dlm->dlm_enumvals = vals;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate return (dlm);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* Turn a previously-declared member into an array */
1260Sstevel@tonic-gate void
inj_decl_mem_make_array(inj_declmem_t * dlm,uint_t dim)1270Sstevel@tonic-gate inj_decl_mem_make_array(inj_declmem_t *dlm, uint_t dim)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate dlm->dlm_flags |= DECLMEM_F_ARRAY;
1300Sstevel@tonic-gate dlm->dlm_arrdim = dim;
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate void
inj_decl_destroy(inj_decl_t * decl)1340Sstevel@tonic-gate inj_decl_destroy(inj_decl_t *decl)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate inj_declmem_t *m, *n;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate inj_strfree(decl->decl_name);
1390Sstevel@tonic-gate inj_strhash_destroy(&decl->decl_memhash);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate for (m = inj_list_next(&decl->decl_members); m != NULL; m = n) {
1420Sstevel@tonic-gate n = inj_list_next(m);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate inj_decl_mem_destroy(m);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate inj_free(decl, sizeof (inj_declmem_t));
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate inj_decl_t *
inj_decl_create(inj_declmem_t * dlm)1510Sstevel@tonic-gate inj_decl_create(inj_declmem_t *dlm)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate inj_decl_t *decl = inj_zalloc(sizeof (inj_decl_t));
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate decl->decl_lineno = yylineno;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate inj_strhash_create(&decl->decl_memhash);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate inj_list_append(&decl->decl_members, dlm);
1600Sstevel@tonic-gate (void) inj_strhash_insert(&decl->decl_memhash, dlm->dlm_name,
161*1717Swesolows (uintptr_t)dlm);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate return (decl);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate void
inj_decl_addmem(inj_decl_t * decl,inj_declmem_t * dlm)1670Sstevel@tonic-gate inj_decl_addmem(inj_decl_t *decl, inj_declmem_t *dlm)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate inj_var_t *v;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if ((v = inj_strhash_lookup(&decl->decl_memhash, dlm->dlm_name)) !=
1720Sstevel@tonic-gate NULL) {
1730Sstevel@tonic-gate inj_decl_t *other = inj_hash_get_cookie(v);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate yyerror("duplicate member name %s (other on line %d)\n",
1760Sstevel@tonic-gate dlm->dlm_name, other->decl_lineno);
1770Sstevel@tonic-gate inj_decl_destroy(decl);
1780Sstevel@tonic-gate return;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate inj_list_append(&decl->decl_members, dlm);
1820Sstevel@tonic-gate (void) inj_strhash_insert(&decl->decl_memhash, dlm->dlm_name,
183*1717Swesolows (uintptr_t)dlm);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate * The various declaration types - events, FMRIs, and authorities - each have
1880Sstevel@tonic-gate * their own semantic validation requirements.
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate /* No user-defined class member. If ena isn't present, we'll generate it */
1920Sstevel@tonic-gate static int
inj_decl_validate_event(inj_decl_t * decl)1930Sstevel@tonic-gate inj_decl_validate_event(inj_decl_t *decl)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate if (inj_strhash_lookup(&decl->decl_memhash, "class") != NULL) {
1960Sstevel@tonic-gate yyerror("class may not be explicitly declared\n");
1970Sstevel@tonic-gate return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (inj_strhash_lookup(&decl->decl_memhash, "ena") == NULL)
2010Sstevel@tonic-gate decl->decl_flags |= DECL_F_AUTOENA;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate return (1);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /* FMRIs must have a string scheme member */
2070Sstevel@tonic-gate static int
inj_decl_validate_fmri(inj_decl_t * decl)2080Sstevel@tonic-gate inj_decl_validate_fmri(inj_decl_t *decl)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate inj_declmem_t *dlm;
2110Sstevel@tonic-gate inj_var_t *v;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if ((v = inj_strhash_lookup(&decl->decl_memhash, "scheme")) == NULL) {
2140Sstevel@tonic-gate yyerror("fmri declared without scheme member\n");
2150Sstevel@tonic-gate return (0);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate dlm = inj_hash_get_cookie(v);
2190Sstevel@tonic-gate if (dlm->dlm_type != MEMTYPE_STRING) {
2200Sstevel@tonic-gate yyerror("scheme member must be a string\n");
2210Sstevel@tonic-gate return (0);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate return (1);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /*ARGSUSED*/
2280Sstevel@tonic-gate static int
inj_decl_validate_nop(inj_decl_t * decl)2291414Scindi inj_decl_validate_nop(inj_decl_t *decl)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate return (1);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate void
inj_decl_finish(inj_decl_t * decl,const char * name,inj_itemtype_t type)2350Sstevel@tonic-gate inj_decl_finish(inj_decl_t *decl, const char *name, inj_itemtype_t type)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate static int (*const validators[])(inj_decl_t *) = {
2380Sstevel@tonic-gate inj_decl_validate_event,
2390Sstevel@tonic-gate inj_decl_validate_fmri,
2401414Scindi inj_decl_validate_nop, /* no validation for auth */
2411414Scindi inj_decl_validate_nop /* no validation for lists */
2420Sstevel@tonic-gate };
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate inj_hash_t *hash = item2hash(type);
2450Sstevel@tonic-gate inj_var_t *v;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate decl->decl_name = name;
2480Sstevel@tonic-gate decl->decl_type = type;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate if (!validators[type](decl)) {
2510Sstevel@tonic-gate inj_decl_destroy(decl);
2520Sstevel@tonic-gate return;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if ((v = inj_strhash_lookup(hash, name)) != NULL) {
2560Sstevel@tonic-gate inj_decl_t *other = inj_hash_get_cookie(v);
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate yyerror("duplicate %s name %s (other on line %d)\n",
2590Sstevel@tonic-gate inj_item2str(type), name, other->decl_lineno);
2600Sstevel@tonic-gate inj_decl_destroy(decl);
2610Sstevel@tonic-gate return;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
264*1717Swesolows (void) inj_strhash_insert(hash, name, (uintptr_t)decl);
2650Sstevel@tonic-gate }
266