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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include "parser.h"
327c478bd9Sstevel@tonic-gate #include "trace.h"
337c478bd9Sstevel@tonic-gate #include "util.h"
347c478bd9Sstevel@tonic-gate #include "errlog.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	TABLE_INITIAL	50
377c478bd9Sstevel@tonic-gate #define	TABLE_INCREMENT	50
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * String processing
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * strnormalize -- combined tab-to-space and strtrim, plus removal
457c478bd9Sstevel@tonic-gate  *	of leading and trailing *$%$^@!!! semicolons.
467c478bd9Sstevel@tonic-gate  *  Not internationalized: TBD.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate char *
strnormalize(char * str)497c478bd9Sstevel@tonic-gate strnormalize(char *str)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	char	*p;
527c478bd9Sstevel@tonic-gate 
53*07c94cbfSToomas Soome 	if (str == NULL || *str == '\0')
547c478bd9Sstevel@tonic-gate 		return (str);
55*07c94cbfSToomas Soome 	for (p = str; *p != '\0'; p++) {
567c478bd9Sstevel@tonic-gate 		if (isspace(*p)) {
577c478bd9Sstevel@tonic-gate 			*p = ' ';
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 	p--;
617c478bd9Sstevel@tonic-gate 	while (p >= str && (isspace(*p) || *p == ';'))
62*07c94cbfSToomas Soome 		*p-- = '\0';
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* ERA - remove leading spaces */
657c478bd9Sstevel@tonic-gate 	while (isspace(*str))
667c478bd9Sstevel@tonic-gate 		str++;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	return (str);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate char *
strtrim(char * str)727c478bd9Sstevel@tonic-gate strtrim(char *str)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	char	*p;
757c478bd9Sstevel@tonic-gate 
76*07c94cbfSToomas Soome 	for (p = str; *p != '\0'; p++)
777c478bd9Sstevel@tonic-gate 		continue;
787c478bd9Sstevel@tonic-gate 	p--;
797c478bd9Sstevel@tonic-gate 	while (p >= str && isspace(*p))
80*07c94cbfSToomas Soome 		*p-- = '\0';
817c478bd9Sstevel@tonic-gate 	return (str);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * strlower -- make a string lower-case, destructively.
867c478bd9Sstevel@tonic-gate  *	Not internationalized: TBD.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate char *
strlower(char * str)897c478bd9Sstevel@tonic-gate strlower(char *str)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	char	*p;
927c478bd9Sstevel@tonic-gate 
93*07c94cbfSToomas Soome 	for (p = str; *p != '\0'; p++) {
947c478bd9Sstevel@tonic-gate 		*p = tolower(*p);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	return (str);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * strset -- update a dynamically-allocated string or die trying.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate char *
strset(char * string,char * value)1037c478bd9Sstevel@tonic-gate strset(char *string, char *value)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	size_t	vlen;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	assert(value != NULL, "passed a NULL value to strset");
1087c478bd9Sstevel@tonic-gate 	vlen = strlen(value);
1097c478bd9Sstevel@tonic-gate 	if (string == NULL) {
1107c478bd9Sstevel@tonic-gate 		/* It was never allocated, so allocate it. */
1117c478bd9Sstevel@tonic-gate 		if ((string = malloc(vlen+1)) == NULL) {
1127c478bd9Sstevel@tonic-gate 			errlog(FATAL, "malloc ran out of space");
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 	} else if (strlen(string) < vlen) {
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		/* Reallocate bigger. */
1177c478bd9Sstevel@tonic-gate 		if ((string = realloc(string, vlen+1)) == NULL) {
1187c478bd9Sstevel@tonic-gate 			errlog(FATAL, "realloc ran out of space", "", 0);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	(void) strcpy(string, value);
1227c478bd9Sstevel@tonic-gate 	return (string);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * in_string_set --see if string matches any member of a space-separated
1277c478bd9Sstevel@tonic-gate  *	set of strings.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate int
in_string_set(char * p,char * set)1307c478bd9Sstevel@tonic-gate in_string_set(char *p, char *set)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char	*q;
1337c478bd9Sstevel@tonic-gate 	char save;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "in_string_set( p = \"%s\", set = \"%s\") {", p, set);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	for (;;) {
1387c478bd9Sstevel@tonic-gate 		set = skipb(set);
1397c478bd9Sstevel@tonic-gate 		q = nextsep(set);
1407c478bd9Sstevel@tonic-gate 		if (q == set) {
1417c478bd9Sstevel@tonic-gate 			/* We've hit the end */
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		save = *q;
145*07c94cbfSToomas Soome 		*q = '\0';
1467c478bd9Sstevel@tonic-gate 		if (strcmp(p, set) == 0) {
1477c478bd9Sstevel@tonic-gate 			*q = save;
1487c478bd9Sstevel@tonic-gate 			errlog(VERBOSE, "return YES");
1497c478bd9Sstevel@tonic-gate 			errlog(END, "}");
1507c478bd9Sstevel@tonic-gate 			return (YES);
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		*q = save;
1537c478bd9Sstevel@tonic-gate 		set = q;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "return NO");
1567c478bd9Sstevel@tonic-gate 	errlog(END, "}");
1577c478bd9Sstevel@tonic-gate 	return (NO);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate char *
strend(char * p)1627c478bd9Sstevel@tonic-gate strend(char *p)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	while (*p)
1667c478bd9Sstevel@tonic-gate 		p++;
1677c478bd9Sstevel@tonic-gate 	return (p);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate char *
lastspace(char * p)1717c478bd9Sstevel@tonic-gate lastspace(char *p)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	char	*q;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	q = strend(p);
1767c478bd9Sstevel@tonic-gate 	q--;
1777c478bd9Sstevel@tonic-gate 	while (q >= p && isspace(*q))
1787c478bd9Sstevel@tonic-gate 		q--;
1797c478bd9Sstevel@tonic-gate 	return (++q);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * skipb -- skip over blanks (whitespace, actually), stopping
1847c478bd9Sstevel@tonic-gate  *	on first non-blank.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate char *
skipb(char * p)1877c478bd9Sstevel@tonic-gate skipb(char *p)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	while (*p && isspace(*p))
1907c478bd9Sstevel@tonic-gate 		p++;
1917c478bd9Sstevel@tonic-gate 	return (p);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * nextb -- skip over non-blanks (including operators!)
1967c478bd9Sstevel@tonic-gate  *	stopping on first blank.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate char *
nextb(char * p)1997c478bd9Sstevel@tonic-gate nextb(char *p)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	while (*p && !isspace(*p))
2027c478bd9Sstevel@tonic-gate 		p++;
2037c478bd9Sstevel@tonic-gate 	return (p);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * skipsep -- skip over separators (all but alnum and _),
2087c478bd9Sstevel@tonic-gate  *	stopping on first non-separator.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate char *
skipsep(char * p)2117c478bd9Sstevel@tonic-gate skipsep(char *p)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "skipsep() {");
2147c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (in) = %s", p);
2157c478bd9Sstevel@tonic-gate 	while (*p && !(isalnum(*p) || *p == '_' || *p == '$'))
2167c478bd9Sstevel@tonic-gate 		p++;
2177c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (out) = %s", p);
2187c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2197c478bd9Sstevel@tonic-gate 	return (p);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * nextsep -- skip over non-separators (alnum and _, actually),
2247c478bd9Sstevel@tonic-gate  *	stopping on first separator.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate char *
nextsep(char * p)2277c478bd9Sstevel@tonic-gate nextsep(char *p)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "nextsep() {");
2307c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (in) = %s", p);
2317c478bd9Sstevel@tonic-gate 	while (*p && isalnum(*p) || *p == '_' || *p == '$')
2327c478bd9Sstevel@tonic-gate 		p++;
2337c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (out) = %s", p);
2347c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2357c478bd9Sstevel@tonic-gate 	return (p);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * nextsep2 -- same as nextsep but also skips '.'
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate char *
nextsep2(char * p)2427c478bd9Sstevel@tonic-gate nextsep2(char *p)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "nextsep() {");
2457c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (in) = %s", p);
2467c478bd9Sstevel@tonic-gate 	while (*p && isalnum(*p) || *p == '_' || *p == '$' || *p == '.')
2477c478bd9Sstevel@tonic-gate 		p++;
2487c478bd9Sstevel@tonic-gate 	errlog(VERBOSE, "p (out) = %s", p);
2497c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2507c478bd9Sstevel@tonic-gate 	return (p);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * objectname -- basename was taken (in man3c), so...
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate char *
objectname(char * name)2577c478bd9Sstevel@tonic-gate objectname(char *name)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	char    *p;
2607c478bd9Sstevel@tonic-gate 	static char basename[MAXLINE];
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	p = strrchr(name, '/');
263*07c94cbfSToomas Soome 	while (p != NULL && *(p+1) == '\0') {
2647c478bd9Sstevel@tonic-gate 		/* The / was at the end of the name. */
265*07c94cbfSToomas Soome 		*p = '\0';
2667c478bd9Sstevel@tonic-gate 		p = strrchr(name, '/');
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	(void) strlcpy(basename, p? p+1: name, MAXLINE);
2697c478bd9Sstevel@tonic-gate 	if ((p = strstr(basename, ".c")) != NULL) {
270*07c94cbfSToomas Soome 		*p = '\0';
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	return (strcat(basename, ".o"));
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * String tables
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate table_t *
create_string_table(int size)2807c478bd9Sstevel@tonic-gate create_string_table(int size)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	table_t	*t;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "create_string_table() {");
2857c478bd9Sstevel@tonic-gate 	if ((t = (table_t *)calloc((size_t)1,
2867c478bd9Sstevel@tonic-gate 	    (size_t)(sizeof (table_t) + (sizeof (char *)*size)))) == NULL) {
2877c478bd9Sstevel@tonic-gate 		errlog(FATAL, "out of memory creating a string table");
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	t->nelem = size;
2907c478bd9Sstevel@tonic-gate 	t->used = -1;
2917c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2927c478bd9Sstevel@tonic-gate 	return (t);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate table_t *
add_string_table(table_t * t,char * value)2967c478bd9Sstevel@tonic-gate add_string_table(table_t *t, char *value)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	table_t *t2;
2997c478bd9Sstevel@tonic-gate 	int	i;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (t == NULL) {
3027c478bd9Sstevel@tonic-gate 		errlog(FATAL, "programmer error: tried to add to "
3037c478bd9Sstevel@tonic-gate 			"a NULL table");
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	if (in_string_table(t, value)) {
3067c478bd9Sstevel@tonic-gate 		return (t);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	t->used++;
3097c478bd9Sstevel@tonic-gate 	if (t->used >= t->nelem) {
3107c478bd9Sstevel@tonic-gate 		if ((t2 = realloc(t, (size_t)(sizeof (table_t)+(sizeof
3117c478bd9Sstevel@tonic-gate 				(char *)*(t->nelem+TABLE_INCREMENT)))))
3127c478bd9Sstevel@tonic-gate 								== NULL) {
3137c478bd9Sstevel@tonic-gate 			errlog(FATAL, "out of memory extending string table");
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		t = t2;
3167c478bd9Sstevel@tonic-gate 		t->nelem += TABLE_INCREMENT;
3177c478bd9Sstevel@tonic-gate 		for (i = t->used; i < t->nelem; i++) {
3187c478bd9Sstevel@tonic-gate 			t->elements[i] = NULL;
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	t->elements[t->used] = strset(t->elements[t->used], value);
3237c478bd9Sstevel@tonic-gate 	return (t);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * free_string_table -- really only mark it empty for reuse.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate table_t *
free_string_table(table_t * t)3307c478bd9Sstevel@tonic-gate free_string_table(table_t *t)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "free_string_table() {");
3337c478bd9Sstevel@tonic-gate 	if (t != NULL) {
3347c478bd9Sstevel@tonic-gate 		t->used = -1;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	errlog(END, "}");
3377c478bd9Sstevel@tonic-gate 	return (t);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate char *
get_string_table(table_t * t,int index)3417c478bd9Sstevel@tonic-gate get_string_table(table_t *t, int index)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	if (t == NULL) {
3447c478bd9Sstevel@tonic-gate 		return (NULL);
3457c478bd9Sstevel@tonic-gate 	} else if (index > t->used) {
3467c478bd9Sstevel@tonic-gate 		return (NULL);
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		return (t->elements[index]);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate int
in_string_table(table_t * t,char * value)3537c478bd9Sstevel@tonic-gate in_string_table(table_t *t, char *value)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	int	i;
3567c478bd9Sstevel@tonic-gate 	size_t	len = strlen(value);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (t == NULL) {
3597c478bd9Sstevel@tonic-gate 		return (0);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	for (i = 0; i <= t->used; i++) {
3627c478bd9Sstevel@tonic-gate 		if (strncmp(value, t->elements[i], len) == 0 &&
363*07c94cbfSToomas Soome 		    (t->elements[i][len] == '\0' ||
3647c478bd9Sstevel@tonic-gate 			t->elements[i][len] == ','))
3657c478bd9Sstevel@tonic-gate 			return (1);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	return (0);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static int
compare(const void * p,const void * q)3717c478bd9Sstevel@tonic-gate compare(const void *p, const void *q)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	return (strcmp((char *)(*(char **)p), (char *)(*(char **)q)));
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate void
sort_string_table(table_t * t)3777c478bd9Sstevel@tonic-gate sort_string_table(table_t *t)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	if (t) {
3807c478bd9Sstevel@tonic-gate 		qsort((char *)t->elements, (size_t)t->used,
3817c478bd9Sstevel@tonic-gate 			sizeof (char *), compare);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate }
384