1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Routines for manipulating iidesc_t structures
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <strings.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "ctftools.h"
38*0Sstevel@tonic-gate #include "memory.h"
39*0Sstevel@tonic-gate #include "list.h"
40*0Sstevel@tonic-gate #include "hash.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate typedef struct iidesc_find {
43*0Sstevel@tonic-gate 	iidesc_t *iif_tgt;
44*0Sstevel@tonic-gate 	iidesc_t *iif_ret;
45*0Sstevel@tonic-gate } iidesc_find_t;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate iidesc_t *
48*0Sstevel@tonic-gate iidesc_new(char *name)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	iidesc_t *ii;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	ii = xcalloc(sizeof (iidesc_t));
53*0Sstevel@tonic-gate 	if (name)
54*0Sstevel@tonic-gate 		ii->ii_name = xstrdup(name);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	return (ii);
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate int
60*0Sstevel@tonic-gate iidesc_hash(int nbuckets, void *arg)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	iidesc_t *ii = arg;
63*0Sstevel@tonic-gate 	int h = 0;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (ii->ii_name)
66*0Sstevel@tonic-gate 		return (hash_name(nbuckets, ii->ii_name));
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	return (h);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static int
72*0Sstevel@tonic-gate iidesc_cmp(iidesc_t *src, iidesc_find_t *find)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	iidesc_t *tgt = find->iif_tgt;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (src->ii_type != tgt->ii_type ||
77*0Sstevel@tonic-gate 	    !streq(src->ii_name, tgt->ii_name))
78*0Sstevel@tonic-gate 		return (0);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	find->iif_ret = src;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	return (-1);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate void
86*0Sstevel@tonic-gate iidesc_add(hash_t *hash, iidesc_t *new)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	iidesc_find_t find;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	find.iif_tgt = new;
91*0Sstevel@tonic-gate 	find.iif_ret = NULL;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	(void) hash_match(hash, new, (int (*)())iidesc_cmp, &find);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if (find.iif_ret != NULL) {
96*0Sstevel@tonic-gate 		iidesc_t *old = find.iif_ret;
97*0Sstevel@tonic-gate 		iidesc_t tmp;
98*0Sstevel@tonic-gate 		/* replacing existing one */
99*0Sstevel@tonic-gate 		bcopy(old, &tmp, sizeof (tmp));
100*0Sstevel@tonic-gate 		bcopy(new, old, sizeof (*old));
101*0Sstevel@tonic-gate 		bcopy(&tmp, new, sizeof (*new));
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		iidesc_free(new, NULL);
104*0Sstevel@tonic-gate 		return;
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	hash_add(hash, new);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate iidesc_t *
111*0Sstevel@tonic-gate iidesc_dup(iidesc_t *src)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	iidesc_t *tgt;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	tgt = xmalloc(sizeof (iidesc_t));
116*0Sstevel@tonic-gate 	bcopy(src, tgt, sizeof (iidesc_t));
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
119*0Sstevel@tonic-gate 	tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (tgt->ii_nargs) {
122*0Sstevel@tonic-gate 		tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
123*0Sstevel@tonic-gate 		bcopy(src->ii_args, tgt->ii_args,
124*0Sstevel@tonic-gate 		    sizeof (tdesc_t *) * tgt->ii_nargs);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	return (tgt);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate iidesc_t *
131*0Sstevel@tonic-gate iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	iidesc_t *tgt = iidesc_dup(src);
134*0Sstevel@tonic-gate 	free(tgt->ii_name);
135*0Sstevel@tonic-gate 	free(tgt->ii_owner);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	tgt->ii_name = name ? xstrdup(name) : NULL;
138*0Sstevel@tonic-gate 	tgt->ii_owner = owner ? xstrdup(owner) : NULL;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	return (tgt);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate /*ARGSUSED*/
144*0Sstevel@tonic-gate void
145*0Sstevel@tonic-gate iidesc_free(iidesc_t *idp, void *private)
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate 	if (idp->ii_name)
148*0Sstevel@tonic-gate 		free(idp->ii_name);
149*0Sstevel@tonic-gate 	if (idp->ii_nargs)
150*0Sstevel@tonic-gate 		free(idp->ii_args);
151*0Sstevel@tonic-gate 	if (idp->ii_owner)
152*0Sstevel@tonic-gate 		free(idp->ii_owner);
153*0Sstevel@tonic-gate 	free(idp);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate int
157*0Sstevel@tonic-gate iidesc_dump(iidesc_t *ii)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	printf("type: %d  name %s\n", ii->ii_type,
160*0Sstevel@tonic-gate 	    (ii->ii_name ? ii->ii_name : "(anon)"));
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	return (0);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate int
166*0Sstevel@tonic-gate iidesc_count_type(void *data, void *private)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	iidesc_t *ii = data;
169*0Sstevel@tonic-gate 	iitype_t match = (iitype_t)private;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	return (ii->ii_type == match);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate void
175*0Sstevel@tonic-gate iidesc_stats(hash_t *ii)
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate 	printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
178*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
179*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
180*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
181*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
182*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
183*0Sstevel@tonic-gate 	    hash_iter(ii, iidesc_count_type, (void *)II_SOU));
184*0Sstevel@tonic-gate }
185