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