17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*80ab886dSwesolows * Common Development and Distribution License (the "License").
6*80ab886dSwesolows * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*80ab886dSwesolows
227c478bd9Sstevel@tonic-gate /*
237aec1d6eScindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Events, FMRIs and authorities must be declared before they can be used.
317c478bd9Sstevel@tonic-gate * Routines in this file, driven by the parser, create the data structures
327c478bd9Sstevel@tonic-gate * associated with the declarations.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <assert.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include <inj_event.h>
397c478bd9Sstevel@tonic-gate #include <inj_err.h>
407c478bd9Sstevel@tonic-gate #include <inj_lex.h>
417c478bd9Sstevel@tonic-gate #include <inj_list.h>
427c478bd9Sstevel@tonic-gate #include <inj.h>
437c478bd9Sstevel@tonic-gate
447aec1d6eScindi static inj_hash_t inj_decls[ITEMTYPE_NITEMS];
457c478bd9Sstevel@tonic-gate static int inj_decls_initialized;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate static inj_hash_t *
item2hash(inj_itemtype_t item)487c478bd9Sstevel@tonic-gate item2hash(inj_itemtype_t item)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate int i;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate assert(item >= 0 && item < sizeof (inj_decls) / sizeof (inj_hash_t));
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if (!inj_decls_initialized) {
557c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (inj_decls) / sizeof (inj_hash_t); i++)
567c478bd9Sstevel@tonic-gate inj_strhash_create(&inj_decls[i]);
577c478bd9Sstevel@tonic-gate inj_decls_initialized = 1;
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate return (&inj_decls[item]);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate inj_decl_t *
inj_decl_lookup(const char * name,inj_itemtype_t type)647c478bd9Sstevel@tonic-gate inj_decl_lookup(const char *name, inj_itemtype_t type)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate inj_hash_t *hash = item2hash(type);
677c478bd9Sstevel@tonic-gate inj_var_t *v;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate if ((v = inj_strhash_lookup(hash, name)) == NULL)
707c478bd9Sstevel@tonic-gate return (NULL);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate return (inj_hash_get_cookie(v));
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate void
inj_decl_mem_destroy(inj_declmem_t * dlm)767c478bd9Sstevel@tonic-gate inj_decl_mem_destroy(inj_declmem_t *dlm)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate inj_strfree(dlm->dlm_name);
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate if (dlm->dlm_type == MEMTYPE_ENUM)
817c478bd9Sstevel@tonic-gate inj_strhash_destroy(dlm->dlm_enumvals);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create(const char * name,inj_memtype_t type)857c478bd9Sstevel@tonic-gate inj_decl_mem_create(const char *name, inj_memtype_t type)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate dlm->dlm_name = name;
907c478bd9Sstevel@tonic-gate dlm->dlm_type = type;
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate return (dlm);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /* An embedded event, authority, or FMRI */
967c478bd9Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create_defined(const char * name,const char * declnm,inj_itemtype_t type)977c478bd9Sstevel@tonic-gate inj_decl_mem_create_defined(const char *name, const char *declnm,
987c478bd9Sstevel@tonic-gate inj_itemtype_t type)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate dlm->dlm_name = name;
1037c478bd9Sstevel@tonic-gate dlm->dlm_type = inj_item2mem(type);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if ((dlm->dlm_decl = inj_decl_lookup(declnm, type)) == NULL) {
1067c478bd9Sstevel@tonic-gate yyerror("unknown %s %s", inj_item2str(type), declnm);
1077c478bd9Sstevel@tonic-gate return (NULL);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate return (dlm);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate inj_declmem_t *
inj_decl_mem_create_enum(const char * name,inj_hash_t * vals)1147c478bd9Sstevel@tonic-gate inj_decl_mem_create_enum(const char *name, inj_hash_t *vals)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate inj_declmem_t *dlm = inj_zalloc(sizeof (inj_declmem_t));
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate dlm->dlm_name = name;
1197c478bd9Sstevel@tonic-gate dlm->dlm_type = MEMTYPE_ENUM;
1207c478bd9Sstevel@tonic-gate dlm->dlm_enumvals = vals;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate return (dlm);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /* Turn a previously-declared member into an array */
1267c478bd9Sstevel@tonic-gate void
inj_decl_mem_make_array(inj_declmem_t * dlm,uint_t dim)1277c478bd9Sstevel@tonic-gate inj_decl_mem_make_array(inj_declmem_t *dlm, uint_t dim)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate dlm->dlm_flags |= DECLMEM_F_ARRAY;
1307c478bd9Sstevel@tonic-gate dlm->dlm_arrdim = dim;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate void
inj_decl_destroy(inj_decl_t * decl)1347c478bd9Sstevel@tonic-gate inj_decl_destroy(inj_decl_t *decl)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate inj_declmem_t *m, *n;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate inj_strfree(decl->decl_name);
1397c478bd9Sstevel@tonic-gate inj_strhash_destroy(&decl->decl_memhash);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate for (m = inj_list_next(&decl->decl_members); m != NULL; m = n) {
1427c478bd9Sstevel@tonic-gate n = inj_list_next(m);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate inj_decl_mem_destroy(m);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate inj_free(decl, sizeof (inj_declmem_t));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate inj_decl_t *
inj_decl_create(inj_declmem_t * dlm)1517c478bd9Sstevel@tonic-gate inj_decl_create(inj_declmem_t *dlm)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate inj_decl_t *decl = inj_zalloc(sizeof (inj_decl_t));
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate decl->decl_lineno = yylineno;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate inj_strhash_create(&decl->decl_memhash);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate inj_list_append(&decl->decl_members, dlm);
1607c478bd9Sstevel@tonic-gate (void) inj_strhash_insert(&decl->decl_memhash, dlm->dlm_name,
161*80ab886dSwesolows (uintptr_t)dlm);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate return (decl);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate void
inj_decl_addmem(inj_decl_t * decl,inj_declmem_t * dlm)1677c478bd9Sstevel@tonic-gate inj_decl_addmem(inj_decl_t *decl, inj_declmem_t *dlm)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate inj_var_t *v;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if ((v = inj_strhash_lookup(&decl->decl_memhash, dlm->dlm_name)) !=
1727c478bd9Sstevel@tonic-gate NULL) {
1737c478bd9Sstevel@tonic-gate inj_decl_t *other = inj_hash_get_cookie(v);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate yyerror("duplicate member name %s (other on line %d)\n",
1767c478bd9Sstevel@tonic-gate dlm->dlm_name, other->decl_lineno);
1777c478bd9Sstevel@tonic-gate inj_decl_destroy(decl);
1787c478bd9Sstevel@tonic-gate return;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate inj_list_append(&decl->decl_members, dlm);
1827c478bd9Sstevel@tonic-gate (void) inj_strhash_insert(&decl->decl_memhash, dlm->dlm_name,
183*80ab886dSwesolows (uintptr_t)dlm);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate * The various declaration types - events, FMRIs, and authorities - each have
1887c478bd9Sstevel@tonic-gate * their own semantic validation requirements.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* No user-defined class member. If ena isn't present, we'll generate it */
1927c478bd9Sstevel@tonic-gate static int
inj_decl_validate_event(inj_decl_t * decl)1937c478bd9Sstevel@tonic-gate inj_decl_validate_event(inj_decl_t *decl)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate if (inj_strhash_lookup(&decl->decl_memhash, "class") != NULL) {
1967c478bd9Sstevel@tonic-gate yyerror("class may not be explicitly declared\n");
1977c478bd9Sstevel@tonic-gate return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate if (inj_strhash_lookup(&decl->decl_memhash, "ena") == NULL)
2017c478bd9Sstevel@tonic-gate decl->decl_flags |= DECL_F_AUTOENA;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* FMRIs must have a string scheme member */
2077c478bd9Sstevel@tonic-gate static int
inj_decl_validate_fmri(inj_decl_t * decl)2087c478bd9Sstevel@tonic-gate inj_decl_validate_fmri(inj_decl_t *decl)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate inj_declmem_t *dlm;
2117c478bd9Sstevel@tonic-gate inj_var_t *v;
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if ((v = inj_strhash_lookup(&decl->decl_memhash, "scheme")) == NULL) {
2147c478bd9Sstevel@tonic-gate yyerror("fmri declared without scheme member\n");
2157c478bd9Sstevel@tonic-gate return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate dlm = inj_hash_get_cookie(v);
2197c478bd9Sstevel@tonic-gate if (dlm->dlm_type != MEMTYPE_STRING) {
2207c478bd9Sstevel@tonic-gate yyerror("scheme member must be a string\n");
2217c478bd9Sstevel@tonic-gate return (0);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate return (1);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2287c478bd9Sstevel@tonic-gate static int
inj_decl_validate_nop(inj_decl_t * decl)2297aec1d6eScindi inj_decl_validate_nop(inj_decl_t *decl)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate return (1);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate void
inj_decl_finish(inj_decl_t * decl,const char * name,inj_itemtype_t type)2357c478bd9Sstevel@tonic-gate inj_decl_finish(inj_decl_t *decl, const char *name, inj_itemtype_t type)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate static int (*const validators[])(inj_decl_t *) = {
2387c478bd9Sstevel@tonic-gate inj_decl_validate_event,
2397c478bd9Sstevel@tonic-gate inj_decl_validate_fmri,
2407aec1d6eScindi inj_decl_validate_nop, /* no validation for auth */
2417aec1d6eScindi inj_decl_validate_nop /* no validation for lists */
2427c478bd9Sstevel@tonic-gate };
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate inj_hash_t *hash = item2hash(type);
2457c478bd9Sstevel@tonic-gate inj_var_t *v;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate decl->decl_name = name;
2487c478bd9Sstevel@tonic-gate decl->decl_type = type;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate if (!validators[type](decl)) {
2517c478bd9Sstevel@tonic-gate inj_decl_destroy(decl);
2527c478bd9Sstevel@tonic-gate return;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if ((v = inj_strhash_lookup(hash, name)) != NULL) {
2567c478bd9Sstevel@tonic-gate inj_decl_t *other = inj_hash_get_cookie(v);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate yyerror("duplicate %s name %s (other on line %d)\n",
2597c478bd9Sstevel@tonic-gate inj_item2str(type), name, other->decl_lineno);
2607c478bd9Sstevel@tonic-gate inj_decl_destroy(decl);
2617c478bd9Sstevel@tonic-gate return;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
264*80ab886dSwesolows (void) inj_strhash_insert(hash, name, (uintptr_t)decl);
2657c478bd9Sstevel@tonic-gate }
266