xref: /onnv-gate/usr/src/lib/libeti/form/common/ty_enum.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  *      All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*LINTLIBRARY*/
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include "utility.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  *	TYPE_ENUM standard type
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  *	usage:
43*0Sstevel@tonic-gate  *		set_field_type(f, TYPE_ENUM, list, checkcase, checkuniq);
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  *		char ** list;	list of acceptable strings
46*0Sstevel@tonic-gate  *		int checkcase;	TRUE - upper/lower case is significant
47*0Sstevel@tonic-gate  *		int checkuniq;	TRUE - unique match required
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate typedef struct {
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	char **	list;
53*0Sstevel@tonic-gate 	int	checkcase;
54*0Sstevel@tonic-gate 	int	checkuniq;
55*0Sstevel@tonic-gate 	int	count;
56*0Sstevel@tonic-gate } ENUM;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static char * make_enum(va_list *);
59*0Sstevel@tonic-gate static char * copy_enum(char *);
60*0Sstevel@tonic-gate static void free_enum(char *);
61*0Sstevel@tonic-gate static int fcheck_enum(FIELD *, char *);
62*0Sstevel@tonic-gate static int next_enum(FIELD *, char *);
63*0Sstevel@tonic-gate static int prev_enum(FIELD *, char *);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static FIELDTYPE typeENUM =
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 				ARGS | CHOICE,		/* status	*/
68*0Sstevel@tonic-gate 				1,			/* ref		*/
69*0Sstevel@tonic-gate 				(FIELDTYPE *) 0,	/* left		*/
70*0Sstevel@tonic-gate 				(FIELDTYPE *) 0,	/* right	*/
71*0Sstevel@tonic-gate 				make_enum,		/* makearg	*/
72*0Sstevel@tonic-gate 				copy_enum,		/* copyarg	*/
73*0Sstevel@tonic-gate 				free_enum,		/* freearg	*/
74*0Sstevel@tonic-gate 				fcheck_enum,		/* fcheck	*/
75*0Sstevel@tonic-gate 				(PTF_int) 0,		/* ccheck	*/
76*0Sstevel@tonic-gate 				next_enum,		/* next		*/
77*0Sstevel@tonic-gate 				prev_enum,		/* prev		*/
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate FIELDTYPE * TYPE_ENUM = &typeENUM;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static char *
make_enum(va_list * ap)83*0Sstevel@tonic-gate make_enum(va_list *ap)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	ENUM * n;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (Alloc(n, ENUM)) {
88*0Sstevel@tonic-gate 		char **		v;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		n -> list	= va_arg(*ap, char **);
91*0Sstevel@tonic-gate 		n -> checkcase	= va_arg(*ap, int);
92*0Sstevel@tonic-gate 		n -> checkuniq	= va_arg(*ap, int);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 		for (v = n -> list; *v; ++v)
95*0Sstevel@tonic-gate 			;
96*0Sstevel@tonic-gate 		n -> count = (int) (v - n -> list);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 	return ((char *) n);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate static char *
copy_enum(char * arg)102*0Sstevel@tonic-gate copy_enum(char *arg)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	ENUM * n;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	if (Alloc(n, ENUM))
107*0Sstevel@tonic-gate 		*n = *((ENUM *) arg);
108*0Sstevel@tonic-gate 	return ((char *) n);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static void
free_enum(char * arg)112*0Sstevel@tonic-gate free_enum(char *arg)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	Free(arg);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate #define	NO_MATCH		0
118*0Sstevel@tonic-gate #define	PARTIAL_MATCH		1
119*0Sstevel@tonic-gate #define	EXACT_MATCH		2
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static int
cmp(char * x,char * v,int checkcase)122*0Sstevel@tonic-gate cmp(char *x, char *v, int checkcase)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	while (*v && *v == ' ')			/* remove leading blanks */
125*0Sstevel@tonic-gate 		++v;
126*0Sstevel@tonic-gate 	while (*x && *x == ' ')			/* remove leading blanks */
127*0Sstevel@tonic-gate 		++x;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (*v == '\0')
130*0Sstevel@tonic-gate 		return (*x == '\0' ? EXACT_MATCH : NO_MATCH);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (checkcase) {			/* case is significant */
133*0Sstevel@tonic-gate 		while (*x++ == *v)
134*0Sstevel@tonic-gate 			if (*v++ == '\0')
135*0Sstevel@tonic-gate 				return (EXACT_MATCH);
136*0Sstevel@tonic-gate 	} else {				/* ignore case */
137*0Sstevel@tonic-gate 		while (toupper (*x++) == toupper (*v))
138*0Sstevel@tonic-gate 			if (*v++ == '\0')
139*0Sstevel@tonic-gate 				return (EXACT_MATCH);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	while (*v && *v == ' ')			/* remove trailing blanks */
142*0Sstevel@tonic-gate 		++v;
143*0Sstevel@tonic-gate 	if (*v)
144*0Sstevel@tonic-gate 		return (NO_MATCH);
145*0Sstevel@tonic-gate 	else
146*0Sstevel@tonic-gate 		return (*--x ? PARTIAL_MATCH : EXACT_MATCH);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static int
fcheck_enum(FIELD * f,char * arg)150*0Sstevel@tonic-gate fcheck_enum(FIELD *f, char *arg)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
153*0Sstevel@tonic-gate 	char **		list		= n -> list;
154*0Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
155*0Sstevel@tonic-gate 	int		checkuniq	= n -> checkuniq;
156*0Sstevel@tonic-gate 	int		m;
157*0Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
158*0Sstevel@tonic-gate 	char *		x;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	while (x = *list++)
161*0Sstevel@tonic-gate 		if (m = cmp(x, v, checkcase)) {
162*0Sstevel@tonic-gate 			char * value = x;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 			if (checkuniq && m != EXACT_MATCH)
165*0Sstevel@tonic-gate 				while (x = *list++)
166*0Sstevel@tonic-gate 					if (m = cmp(x, v, checkcase)) {
167*0Sstevel@tonic-gate 						if (m == EXACT_MATCH) {
168*0Sstevel@tonic-gate 							value = x;
169*0Sstevel@tonic-gate 							break;
170*0Sstevel@tonic-gate 						}
171*0Sstevel@tonic-gate 						else
172*0Sstevel@tonic-gate 							value = (char *) 0;
173*0Sstevel@tonic-gate 					}
174*0Sstevel@tonic-gate 			if (! value)
175*0Sstevel@tonic-gate 				return (FALSE);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 			(void) set_field_buffer(f, 0, value);
178*0Sstevel@tonic-gate 			return (TRUE);
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	return (FALSE);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static int
next_enum(FIELD * f,char * arg)185*0Sstevel@tonic-gate next_enum(FIELD *f, char *arg)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
188*0Sstevel@tonic-gate 	char **		list		= n -> list;
189*0Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
190*0Sstevel@tonic-gate 	int		count		= n -> count;
191*0Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	while (count--)
194*0Sstevel@tonic-gate 		if (cmp(*list++, v, checkcase) == EXACT_MATCH)
195*0Sstevel@tonic-gate 			break;
196*0Sstevel@tonic-gate 	if (count <= 0)
197*0Sstevel@tonic-gate 		list = n -> list;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (count >= 0 || cmp("", v, checkcase) == EXACT_MATCH) {
200*0Sstevel@tonic-gate 		(void) set_field_buffer(f, 0, *list);
201*0Sstevel@tonic-gate 		return (TRUE);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 	return (FALSE);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate static int
prev_enum(FIELD * f,char * arg)207*0Sstevel@tonic-gate prev_enum(FIELD *f, char *arg)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	ENUM *		n		= (ENUM *) arg;
210*0Sstevel@tonic-gate 	char **		list		= n -> list + n -> count - 1;
211*0Sstevel@tonic-gate 	int		checkcase	= n -> checkcase;
212*0Sstevel@tonic-gate 	int		count		= n -> count;
213*0Sstevel@tonic-gate 	char *		v		= field_buffer(f, 0);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	while (count--)
216*0Sstevel@tonic-gate 		if (cmp(*list--, v, checkcase) == EXACT_MATCH)
217*0Sstevel@tonic-gate 			break;
218*0Sstevel@tonic-gate 	if (count <= 0)
219*0Sstevel@tonic-gate 		list = n -> list + n -> count - 1;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if (count >= 0 || cmp("", v, checkcase) == EXACT_MATCH) {
222*0Sstevel@tonic-gate 		(void) set_field_buffer(f, 0, *list);
223*0Sstevel@tonic-gate 		return (TRUE);
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	return (FALSE);
226*0Sstevel@tonic-gate }
227