1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * util.c -- low-level utilities used by map*.c
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <errno.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include "xlator.h"
36*7c478bd9Sstevel@tonic-gate #include "util.h"
37*7c478bd9Sstevel@tonic-gate #include "errlog.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * String tables -- WARNING!  This uses realloc to recreate tables,
41*7c478bd9Sstevel@tonic-gate  *	so always assign table_t * return value to the current
42*7c478bd9Sstevel@tonic-gate  *	table pointer, lest the table address change in the
43*7c478bd9Sstevel@tonic-gate  *	called function.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate static char *strset(char *, char *);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate table_t *
create_stringtable(int size)48*7c478bd9Sstevel@tonic-gate create_stringtable(int size)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	table_t *t;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	/* Solaris idiom: malloc && memset. TBD. */
53*7c478bd9Sstevel@tonic-gate 	if ((t = calloc((size_t)1, (size_t)(sizeof (table_t) +
54*7c478bd9Sstevel@tonic-gate 	    ((sizeof (char *)) * size)))) == NULL) {
55*7c478bd9Sstevel@tonic-gate 		errlog(FATAL,
56*7c478bd9Sstevel@tonic-gate 		    "\nOut of memory.\n"
57*7c478bd9Sstevel@tonic-gate 		    "We wish to hold the whole sky,\n"
58*7c478bd9Sstevel@tonic-gate 		    "But we never will.\n");
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 	t->nelem = size;
61*7c478bd9Sstevel@tonic-gate 	t->used = -1;
62*7c478bd9Sstevel@tonic-gate 	return (t);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate table_t *
add_to_stringtable(table_t * t,char * value)67*7c478bd9Sstevel@tonic-gate add_to_stringtable(table_t *t, char *value)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	table_t *t2;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	int i;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
74*7c478bd9Sstevel@tonic-gate 		seterrline(__LINE__, __FILE__, NULL, NULL);
75*7c478bd9Sstevel@tonic-gate 		errlog(FATAL|PROGRAM, "programmer error: tried to add to "
76*7c478bd9Sstevel@tonic-gate 			"a NULL table");
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 	if (in_stringtable(t, value)) {
79*7c478bd9Sstevel@tonic-gate 		return (t);
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	++t->used;
82*7c478bd9Sstevel@tonic-gate 	if (t->used >= t->nelem) {
83*7c478bd9Sstevel@tonic-gate 		if ((t2 = realloc(t, (size_t)(sizeof (table_t) +
84*7c478bd9Sstevel@tonic-gate 		    ((sizeof (char *)) * (t->nelem + TABLE_INCREMENT)))))
85*7c478bd9Sstevel@tonic-gate 		    == NULL) {
86*7c478bd9Sstevel@tonic-gate 			print_stringtable(t);
87*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
88*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory extending a "
89*7c478bd9Sstevel@tonic-gate 				"string table");
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 		t = t2;
92*7c478bd9Sstevel@tonic-gate 		t->nelem += TABLE_INCREMENT;
93*7c478bd9Sstevel@tonic-gate 		for (i = t->used; i < t->nelem; ++i) {
94*7c478bd9Sstevel@tonic-gate 			t->elements[i] = NULL;
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	t->elements[t->used] = strset(t->elements[t->used], value);
98*7c478bd9Sstevel@tonic-gate 	return (t);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * free_stringtable -- really only mark it empty for reuse.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate table_t *
free_stringtable(table_t * t)105*7c478bd9Sstevel@tonic-gate free_stringtable(table_t *t)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if (t != NULL) {
109*7c478bd9Sstevel@tonic-gate 		t->used = -1;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	return (t);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate char *
get_stringtable(table_t * t,int index)116*7c478bd9Sstevel@tonic-gate get_stringtable(table_t *t, int index)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
120*7c478bd9Sstevel@tonic-gate 		return (NULL);
121*7c478bd9Sstevel@tonic-gate 	} else if (index > t->used) {
122*7c478bd9Sstevel@tonic-gate 		return (NULL);
123*7c478bd9Sstevel@tonic-gate 	} else {
124*7c478bd9Sstevel@tonic-gate 		return (t->elements[index]);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate int
in_stringtable(table_t * t,const char * value)129*7c478bd9Sstevel@tonic-gate in_stringtable(table_t *t, const char *value)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	int i;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
134*7c478bd9Sstevel@tonic-gate 		return (0);
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= t->used; ++i) {
137*7c478bd9Sstevel@tonic-gate 		if (strcmp(value, t->elements[i]) == 0)
138*7c478bd9Sstevel@tonic-gate 			return (1);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	return (0);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate void
print_stringtable(table_t * t)145*7c478bd9Sstevel@tonic-gate print_stringtable(table_t *t)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	int i;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (t == NULL)
150*7c478bd9Sstevel@tonic-gate 		return;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	errlog(VERBOSE,
153*7c478bd9Sstevel@tonic-gate 		"table size = %d elements out of %d elements/%d bytes\n",
154*7c478bd9Sstevel@tonic-gate 		t->used + 1, t->nelem,
155*7c478bd9Sstevel@tonic-gate 		sizeof (table_t) + (sizeof (char *) * t->nelem));
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= t->used; ++i) {
158*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s\n",
159*7c478bd9Sstevel@tonic-gate 			get_stringtable(t, i));
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate static int
compare(const void * p,const void * q)164*7c478bd9Sstevel@tonic-gate compare(const void *p, const void *q)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	return (strcmp((char *)p, (char *)q));
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate void
sort_stringtable(table_t * t)170*7c478bd9Sstevel@tonic-gate sort_stringtable(table_t *t)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (t && t->used > 0) {
174*7c478bd9Sstevel@tonic-gate 		qsort((char *)t->elements, (size_t)t->used,
175*7c478bd9Sstevel@tonic-gate 			sizeof (char *), compare);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate  * strset -- update a dynamically-allocated string or die trying.
182*7c478bd9Sstevel@tonic-gate  */
183*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
184*7c478bd9Sstevel@tonic-gate static char *
strset(char * string,char * value)185*7c478bd9Sstevel@tonic-gate strset(char *string, char *value)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	size_t vlen;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	assert(value != NULL, "passed a null value to strset");
190*7c478bd9Sstevel@tonic-gate 	vlen = strlen(value);
191*7c478bd9Sstevel@tonic-gate 	if (string == NULL) {
192*7c478bd9Sstevel@tonic-gate 		/* It was never allocated, so allocate it. */
193*7c478bd9Sstevel@tonic-gate 		if ((string = malloc(vlen + 1)) == NULL) {
194*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
195*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory allocating a "
196*7c478bd9Sstevel@tonic-gate 			    "string");
197*7c478bd9Sstevel@tonic-gate 		}
198*7c478bd9Sstevel@tonic-gate 	} else if (strlen(string) < vlen) {
199*7c478bd9Sstevel@tonic-gate 		/* Reallocate bigger. */
200*7c478bd9Sstevel@tonic-gate 		if ((string = realloc(string, vlen + 1)) == NULL) {
201*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
202*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory reallocating"
203*7c478bd9Sstevel@tonic-gate 			    "a string");
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	(void) strcpy(string, value);
207*7c478bd9Sstevel@tonic-gate 	return (string);
208*7c478bd9Sstevel@tonic-gate }
209