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 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * util.c -- low-level utilities used by map*.c
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include "xlator.h"
35*7c478bd9Sstevel@tonic-gate #include "util.h"
36*7c478bd9Sstevel@tonic-gate #include "errlog.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * String tables -- WARNING!  This uses realloc to recreate tables,
40*7c478bd9Sstevel@tonic-gate  *	so always assign table_t * return value to the current
41*7c478bd9Sstevel@tonic-gate  *	table pointer, lest the table address change in the
42*7c478bd9Sstevel@tonic-gate  *	called function.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate static char *strset(char *, char *);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate table_t *
create_stringtable(int size)47*7c478bd9Sstevel@tonic-gate create_stringtable(int size)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	table_t *t;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	/* Solaris idiom: malloc && memset. TBD. */
52*7c478bd9Sstevel@tonic-gate 	if ((t = calloc((size_t)1, (size_t)(sizeof (table_t) +
53*7c478bd9Sstevel@tonic-gate 	    ((sizeof (char *)) * size)))) == NULL) {
54*7c478bd9Sstevel@tonic-gate 		errlog(FATAL,
55*7c478bd9Sstevel@tonic-gate 		    "\nOut of memory.\n"
56*7c478bd9Sstevel@tonic-gate 		    "We wish to hold the whole sky,\n"
57*7c478bd9Sstevel@tonic-gate 		    "But we never will.\n");
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	t->nelem = size;
60*7c478bd9Sstevel@tonic-gate 	t->used = -1;
61*7c478bd9Sstevel@tonic-gate 	return (t);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate table_t *
add_to_stringtable(table_t * t,char * value)66*7c478bd9Sstevel@tonic-gate add_to_stringtable(table_t *t, char *value)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	table_t *t2;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	int i;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
73*7c478bd9Sstevel@tonic-gate 		seterrline(__LINE__, __FILE__, NULL, NULL);
74*7c478bd9Sstevel@tonic-gate 		errlog(FATAL|PROGRAM, "programmer error: tried to add to "
75*7c478bd9Sstevel@tonic-gate 			"a NULL table");
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	if (in_stringtable(t, value)) {
78*7c478bd9Sstevel@tonic-gate 		return (t);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	++t->used;
81*7c478bd9Sstevel@tonic-gate 	if (t->used >= t->nelem) {
82*7c478bd9Sstevel@tonic-gate 		if ((t2 = realloc(t, (size_t)(sizeof (table_t) +
83*7c478bd9Sstevel@tonic-gate 		    ((sizeof (char *)) * (t->nelem + TABLE_INCREMENT)))))
84*7c478bd9Sstevel@tonic-gate 		    == NULL) {
85*7c478bd9Sstevel@tonic-gate 			print_stringtable(t);
86*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
87*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory extending a "
88*7c478bd9Sstevel@tonic-gate 				"string table");
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		t = t2;
91*7c478bd9Sstevel@tonic-gate 		t->nelem += TABLE_INCREMENT;
92*7c478bd9Sstevel@tonic-gate 		for (i = t->used; i < t->nelem; ++i) {
93*7c478bd9Sstevel@tonic-gate 			t->elements[i] = NULL;
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 	t->elements[t->used] = strset(t->elements[t->used], value);
97*7c478bd9Sstevel@tonic-gate 	return (t);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * free_stringtable -- really only mark it empty for reuse.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate table_t *
free_stringtable(table_t * t)104*7c478bd9Sstevel@tonic-gate free_stringtable(table_t *t)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (t != NULL) {
108*7c478bd9Sstevel@tonic-gate 		t->used = -1;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	return (t);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate char *
get_stringtable(table_t * t,int index)115*7c478bd9Sstevel@tonic-gate get_stringtable(table_t *t, int index)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
119*7c478bd9Sstevel@tonic-gate 		return (NULL);
120*7c478bd9Sstevel@tonic-gate 	} else if (index > t->used) {
121*7c478bd9Sstevel@tonic-gate 		return (NULL);
122*7c478bd9Sstevel@tonic-gate 	} else {
123*7c478bd9Sstevel@tonic-gate 		return (t->elements[index]);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate int
in_stringtable(table_t * t,const char * value)128*7c478bd9Sstevel@tonic-gate in_stringtable(table_t *t, const char *value)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	int i;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (t == NULL) {
133*7c478bd9Sstevel@tonic-gate 		return (0);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= t->used; ++i) {
136*7c478bd9Sstevel@tonic-gate 		if (strcmp(value, t->elements[i]) == 0)
137*7c478bd9Sstevel@tonic-gate 			return (1);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	return (0);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate void
print_stringtable(table_t * t)144*7c478bd9Sstevel@tonic-gate print_stringtable(table_t *t)
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate 	int i;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (t == NULL)
149*7c478bd9Sstevel@tonic-gate 		return;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	errlog(VERBOSE,
152*7c478bd9Sstevel@tonic-gate 		"table size = %d elements out of %d elements/%d bytes\n",
153*7c478bd9Sstevel@tonic-gate 		t->used + 1, t->nelem,
154*7c478bd9Sstevel@tonic-gate 		sizeof (table_t) + (sizeof (char *) * t->nelem));
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= t->used; ++i) {
157*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s\n",
158*7c478bd9Sstevel@tonic-gate 			get_stringtable(t, i));
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static int
compare(const void * p,const void * q)163*7c478bd9Sstevel@tonic-gate compare(const void *p, const void *q)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	return (strcmp((char *)p, (char *)q));
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate void
sort_stringtable(table_t * t)169*7c478bd9Sstevel@tonic-gate sort_stringtable(table_t *t)
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (t && t->used > 0) {
173*7c478bd9Sstevel@tonic-gate 		qsort((char *)t->elements, (size_t)t->used,
174*7c478bd9Sstevel@tonic-gate 			sizeof (char *), compare);
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  * strset -- update a dynamically-allocated string or die trying.
181*7c478bd9Sstevel@tonic-gate  */
182*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
183*7c478bd9Sstevel@tonic-gate static char *
strset(char * string,char * value)184*7c478bd9Sstevel@tonic-gate strset(char *string, char *value)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 	size_t vlen;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	assert(value != NULL, "passed a null value to strset");
189*7c478bd9Sstevel@tonic-gate 	vlen = strlen(value);
190*7c478bd9Sstevel@tonic-gate 	if (string == NULL) {
191*7c478bd9Sstevel@tonic-gate 		/* It was never allocated, so allocate it. */
192*7c478bd9Sstevel@tonic-gate 		if ((string = malloc(vlen + 1)) == NULL) {
193*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
194*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory allocating a "
195*7c478bd9Sstevel@tonic-gate 			    "string");
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 	} else if (strlen(string) < vlen) {
198*7c478bd9Sstevel@tonic-gate 		/* Reallocate bigger. */
199*7c478bd9Sstevel@tonic-gate 		if ((string = realloc(string, vlen + 1)) == NULL) {
200*7c478bd9Sstevel@tonic-gate 			seterrline(__LINE__, __FILE__, NULL, NULL);
201*7c478bd9Sstevel@tonic-gate 			errlog(FATAL|PROGRAM, "out of memory reallocating"
202*7c478bd9Sstevel@tonic-gate 			    "a string");
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 	(void) strcpy(string, value);
206*7c478bd9Sstevel@tonic-gate 	return (string);
207*7c478bd9Sstevel@tonic-gate }
208