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 /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include "utility.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  *	TYPE_ENUM standard type
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  *	usage:
41*7c478bd9Sstevel@tonic-gate  *		set_field_type(f, TYPE_ENUM, list, checkcase, checkuniq);
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *		char ** list;	list of acceptable strings
44*7c478bd9Sstevel@tonic-gate  *		int checkcase;	TRUE - upper/lower case is significant
45*7c478bd9Sstevel@tonic-gate  *		int checkuniq;	TRUE - unique match required
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate typedef struct {
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	char **	list;
51*7c478bd9Sstevel@tonic-gate 	int	checkcase;
52*7c478bd9Sstevel@tonic-gate 	int	checkuniq;
53*7c478bd9Sstevel@tonic-gate 	int	count;
54*7c478bd9Sstevel@tonic-gate } ENUM;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static char * make_enum(va_list *);
57*7c478bd9Sstevel@tonic-gate static char * copy_enum(char *);
58*7c478bd9Sstevel@tonic-gate static void free_enum(char *);
59*7c478bd9Sstevel@tonic-gate static int fcheck_enum(FIELD *, char *);
60*7c478bd9Sstevel@tonic-gate static int next_enum(FIELD *, char *);
61*7c478bd9Sstevel@tonic-gate static int prev_enum(FIELD *, char *);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static FIELDTYPE typeENUM =
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 				ARGS | CHOICE,		/* status	*/
66*7c478bd9Sstevel@tonic-gate 				1,			/* ref		*/
67*7c478bd9Sstevel@tonic-gate 				(FIELDTYPE *) 0,	/* left		*/
68*7c478bd9Sstevel@tonic-gate 				(FIELDTYPE *) 0,	/* right	*/
69*7c478bd9Sstevel@tonic-gate 				make_enum,		/* makearg	*/
70*7c478bd9Sstevel@tonic-gate 				copy_enum,		/* copyarg	*/
71*7c478bd9Sstevel@tonic-gate 				free_enum,		/* freearg	*/
72*7c478bd9Sstevel@tonic-gate 				fcheck_enum,		/* fcheck	*/
73*7c478bd9Sstevel@tonic-gate 				(PTF_int) 0,		/* ccheck	*/
74*7c478bd9Sstevel@tonic-gate 				next_enum,		/* next		*/
75*7c478bd9Sstevel@tonic-gate 				prev_enum,		/* prev		*/
76*7c478bd9Sstevel@tonic-gate };
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate FIELDTYPE * TYPE_ENUM = &typeENUM;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static char *
make_enum(va_list * ap)81*7c478bd9Sstevel@tonic-gate make_enum(va_list *ap)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	ENUM * n;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (Alloc(n, ENUM)) {
86*7c478bd9Sstevel@tonic-gate 		char **		v;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 		n -> list	= va_arg(*ap, char **);
89*7c478bd9Sstevel@tonic-gate 		n -> checkcase	= va_arg(*ap, int);
90*7c478bd9Sstevel@tonic-gate 		n -> checkuniq	= va_arg(*ap, int);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		for (v = n -> list; *v; ++v)
93*7c478bd9Sstevel@tonic-gate 			;
94*7c478bd9Sstevel@tonic-gate 		n -> count = (int) (v - n -> list);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 	return ((char *) n);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static char *
copy_enum(char * arg)100*7c478bd9Sstevel@tonic-gate copy_enum(char *arg)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	ENUM * n;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (Alloc(n, ENUM))
105*7c478bd9Sstevel@tonic-gate 		*n = *((ENUM *) arg);
106*7c478bd9Sstevel@tonic-gate 	return ((char *) n);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate static void
free_enum(char * arg)110*7c478bd9Sstevel@tonic-gate free_enum(char *arg)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	Free(arg);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate #define	NO_MATCH		0
116*7c478bd9Sstevel@tonic-gate #define	PARTIAL_MATCH		1
117*7c478bd9Sstevel@tonic-gate #define	EXACT_MATCH		2
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static int
cmp(char * x,char * v,int checkcase)120*7c478bd9Sstevel@tonic-gate cmp(char *x, char *v, int checkcase)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	while (*v && *v == ' ')			/* remove leading blanks */
123*7c478bd9Sstevel@tonic-gate 		++v;
124*7c478bd9Sstevel@tonic-gate 	while (*x && *x == ' ')			/* remove leading blanks */
125*7c478bd9Sstevel@tonic-gate 		++x;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (*v == '\0')
128*7c478bd9Sstevel@tonic-gate 		return (*x == '\0' ? EXACT_MATCH : NO_MATCH);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (checkcase) {			/* case is significant */
131*7c478bd9Sstevel@tonic-gate 		while (*x++ == *v)
132*7c478bd9Sstevel@tonic-gate 			if (*v++ == '\0')
133*7c478bd9Sstevel@tonic-gate 				return (EXACT_MATCH);
134*7c478bd9Sstevel@tonic-gate 	} else {				/* ignore case */
135*7c478bd9Sstevel@tonic-gate 		while (toupper (*x++) == toupper (*v))
136*7c478bd9Sstevel@tonic-gate 			if (*v++ == '\0')
137*7c478bd9Sstevel@tonic-gate 				return (EXACT_MATCH);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	while (*v && *v == ' ')			/* remove trailing blanks */
140*7c478bd9Sstevel@tonic-gate 		++v;
141*7c478bd9Sstevel@tonic-gate 	if (*v)
142*7c478bd9Sstevel@tonic-gate 		return (NO_MATCH);
143*7c478bd9Sstevel@tonic-gate 	else
144*7c478bd9Sstevel@tonic-gate 		return (*--x ? PARTIAL_MATCH : EXACT_MATCH);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate static int
fcheck_enum(FIELD * f,char * arg)148*7c478bd9Sstevel@tonic-gate fcheck_enum(FIELD *f, char *arg)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
151*7c478bd9Sstevel@tonic-gate 	char **		list		= n -> list;
152*7c478bd9Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
153*7c478bd9Sstevel@tonic-gate 	int		checkuniq	= n -> checkuniq;
154*7c478bd9Sstevel@tonic-gate 	int		m;
155*7c478bd9Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
156*7c478bd9Sstevel@tonic-gate 	char *		x;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	while (x = *list++)
159*7c478bd9Sstevel@tonic-gate 		if (m = cmp(x, v, checkcase)) {
160*7c478bd9Sstevel@tonic-gate 			char * value = x;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 			if (checkuniq && m != EXACT_MATCH)
163*7c478bd9Sstevel@tonic-gate 				while (x = *list++)
164*7c478bd9Sstevel@tonic-gate 					if (m = cmp(x, v, checkcase)) {
165*7c478bd9Sstevel@tonic-gate 						if (m == EXACT_MATCH) {
166*7c478bd9Sstevel@tonic-gate 							value = x;
167*7c478bd9Sstevel@tonic-gate 							break;
168*7c478bd9Sstevel@tonic-gate 						}
169*7c478bd9Sstevel@tonic-gate 						else
170*7c478bd9Sstevel@tonic-gate 							value = (char *) 0;
171*7c478bd9Sstevel@tonic-gate 					}
172*7c478bd9Sstevel@tonic-gate 			if (! value)
173*7c478bd9Sstevel@tonic-gate 				return (FALSE);
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 			(void) set_field_buffer(f, 0, value);
176*7c478bd9Sstevel@tonic-gate 			return (TRUE);
177*7c478bd9Sstevel@tonic-gate 		}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	return (FALSE);
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static int
next_enum(FIELD * f,char * arg)183*7c478bd9Sstevel@tonic-gate next_enum(FIELD *f, char *arg)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
186*7c478bd9Sstevel@tonic-gate 	char **		list		= n -> list;
187*7c478bd9Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
188*7c478bd9Sstevel@tonic-gate 	int		count		= n -> count;
189*7c478bd9Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	while (count--)
192*7c478bd9Sstevel@tonic-gate 		if (cmp(*list++, v, checkcase) == EXACT_MATCH)
193*7c478bd9Sstevel@tonic-gate 			break;
194*7c478bd9Sstevel@tonic-gate 	if (count <= 0)
195*7c478bd9Sstevel@tonic-gate 		list = n -> list;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (count >= 0 || cmp("", v, checkcase) == EXACT_MATCH) {
198*7c478bd9Sstevel@tonic-gate 		(void) set_field_buffer(f, 0, *list);
199*7c478bd9Sstevel@tonic-gate 		return (TRUE);
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 	return (FALSE);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate static int
prev_enum(FIELD * f,char * arg)205*7c478bd9Sstevel@tonic-gate prev_enum(FIELD *f, char *arg)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
208*7c478bd9Sstevel@tonic-gate 	char **		list		= n -> list + n -> count - 1;
209*7c478bd9Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
210*7c478bd9Sstevel@tonic-gate 	int		count		= n -> count;
211*7c478bd9Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	while (count--)
214*7c478bd9Sstevel@tonic-gate 		if (cmp(*list--, v, checkcase) == EXACT_MATCH)
215*7c478bd9Sstevel@tonic-gate 			break;
216*7c478bd9Sstevel@tonic-gate 	if (count <= 0)
217*7c478bd9Sstevel@tonic-gate 		list = n -> list + n -> count - 1;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (count >= 0 || cmp("", v, checkcase) == EXACT_MATCH) {
220*7c478bd9Sstevel@tonic-gate 		(void) set_field_buffer(f, 0, *list);
221*7c478bd9Sstevel@tonic-gate 		return (TRUE);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	return (FALSE);
224*7c478bd9Sstevel@tonic-gate }
225