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 typedef struct {
38*7c478bd9Sstevel@tonic-gate 	char *leftarg;
39*7c478bd9Sstevel@tonic-gate 	char *rightarg;
40*7c478bd9Sstevel@tonic-gate }
41*7c478bd9Sstevel@tonic-gate 	LINK;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	ArgL(n)		(((LINK *)(n))->leftarg)
44*7c478bd9Sstevel@tonic-gate #define	ArgR(n)		(((LINK *)(n))->rightarg)
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	Ref(t)		((t)->ref)
47*7c478bd9Sstevel@tonic-gate #define	TypeL(t)	((t)->left)
48*7c478bd9Sstevel@tonic-gate #define	TypeR(t)	((t)->right)
49*7c478bd9Sstevel@tonic-gate #define	MakeA(t)	((t)->makearg)
50*7c478bd9Sstevel@tonic-gate #define	CopyA(t)	((t)->copyarg)
51*7c478bd9Sstevel@tonic-gate #define	FreeA(t)	((t)->freearg)
52*7c478bd9Sstevel@tonic-gate #define	Fcheck(t)	((t)->fcheck)
53*7c478bd9Sstevel@tonic-gate #define	Ccheck(t)	((t)->ccheck)
54*7c478bd9Sstevel@tonic-gate #define	Next(t)		((t)->next)
55*7c478bd9Sstevel@tonic-gate #define	Prev(t)		((t)->prev)
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	/*
58*7c478bd9Sstevel@tonic-gate 	 *  default fieldtype
59*7c478bd9Sstevel@tonic-gate 	 */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static FIELDTYPE default_fieldtype =
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 			0,			/* status	*/
64*7c478bd9Sstevel@tonic-gate 			0,			/* ref		*/
65*7c478bd9Sstevel@tonic-gate 			(FIELDTYPE *) 0,	/* left		*/
66*7c478bd9Sstevel@tonic-gate 			(FIELDTYPE *) 0,	/* right	*/
67*7c478bd9Sstevel@tonic-gate 			(PTF_charP) 0,		/* makearg	*/
68*7c478bd9Sstevel@tonic-gate 			(PTF_charP) 0,		/* copyarg	*/
69*7c478bd9Sstevel@tonic-gate 			(PTF_void) 0,		/* freearg	*/
70*7c478bd9Sstevel@tonic-gate 			(PTF_int) 0,		/* fcheck	*/
71*7c478bd9Sstevel@tonic-gate 			(PTF_int) 0,		/* ccheck	*/
72*7c478bd9Sstevel@tonic-gate 			(PTF_int) 0,		/* next		*/
73*7c478bd9Sstevel@tonic-gate 			(PTF_int) 0,		/* prev		*/
74*7c478bd9Sstevel@tonic-gate };
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate FIELDTYPE * _DEFAULT_FIELDTYPE	= &default_fieldtype;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /* new_fieldtype - field & character validation function */
79*7c478bd9Sstevel@tonic-gate FIELDTYPE *
new_fieldtype(PTF_int fcheck,PTF_int ccheck)80*7c478bd9Sstevel@tonic-gate new_fieldtype(PTF_int fcheck, PTF_int ccheck)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	FIELDTYPE *t = (FIELDTYPE *) 0;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if ((fcheck || ccheck) && Alloc(t, FIELDTYPE)) {
85*7c478bd9Sstevel@tonic-gate 		*t = *_DEFAULT_FIELDTYPE;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		Fcheck(t) = fcheck;
88*7c478bd9Sstevel@tonic-gate 		Ccheck(t) = ccheck;
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	return (t);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate FIELDTYPE *
link_fieldtype(FIELDTYPE * left,FIELDTYPE * right)94*7c478bd9Sstevel@tonic-gate link_fieldtype(FIELDTYPE *left, FIELDTYPE *right)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	FIELDTYPE *t = (FIELDTYPE *) 0;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if ((left || right) && Alloc(t, FIELDTYPE)) {
99*7c478bd9Sstevel@tonic-gate 		*t = *_DEFAULT_FIELDTYPE;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		Set(t, LINKED);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 		if (Status(left, ARGS) || Status(right, ARGS))
104*7c478bd9Sstevel@tonic-gate 			Set(t, ARGS);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		if (Status(left, CHOICE) || Status(right, CHOICE))
107*7c478bd9Sstevel@tonic-gate 			Set(t, CHOICE);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		TypeL(t) = left;
110*7c478bd9Sstevel@tonic-gate 		TypeR(t) = right;
111*7c478bd9Sstevel@tonic-gate 		IncrType(left);	/* increment reference count */
112*7c478bd9Sstevel@tonic-gate 		IncrType(right);	/* increment reference count */
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 	return (t);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate int
free_fieldtype(FIELDTYPE * t)118*7c478bd9Sstevel@tonic-gate free_fieldtype(FIELDTYPE *t)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	if (!t)
121*7c478bd9Sstevel@tonic-gate 		return (E_BAD_ARGUMENT);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (Ref(t))
124*7c478bd9Sstevel@tonic-gate 		return (E_CONNECTED);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED)) {
127*7c478bd9Sstevel@tonic-gate 		DecrType(TypeL(t));	/* decrement reference count */
128*7c478bd9Sstevel@tonic-gate 		DecrType(TypeR(t));	/* decrement reference count */
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	Free(t);
131*7c478bd9Sstevel@tonic-gate 	return (E_OK);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate int
set_fieldtype_arg(FIELDTYPE * t,PTF_charP makearg,PTF_charP copyarg,PTF_void freearg)135*7c478bd9Sstevel@tonic-gate set_fieldtype_arg(FIELDTYPE *t, PTF_charP makearg,
136*7c478bd9Sstevel@tonic-gate 	PTF_charP copyarg, PTF_void freearg)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	if (t && makearg && copyarg && freearg) {
139*7c478bd9Sstevel@tonic-gate 		Set(t, ARGS);
140*7c478bd9Sstevel@tonic-gate 		MakeA(t) = makearg;
141*7c478bd9Sstevel@tonic-gate 		CopyA(t) = copyarg;
142*7c478bd9Sstevel@tonic-gate 		FreeA(t) = freearg;
143*7c478bd9Sstevel@tonic-gate 		return (E_OK);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	return (E_BAD_ARGUMENT);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /* set_fieldtype_choice next & prev choice function */
149*7c478bd9Sstevel@tonic-gate int
set_fieldtype_choice(FIELDTYPE * t,PTF_int next,PTF_int prev)150*7c478bd9Sstevel@tonic-gate set_fieldtype_choice(FIELDTYPE *t, PTF_int next, PTF_int prev)
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 	if (t && next && prev) {
153*7c478bd9Sstevel@tonic-gate 		Set(t, CHOICE);
154*7c478bd9Sstevel@tonic-gate 		Next(t) = next;
155*7c478bd9Sstevel@tonic-gate 		Prev(t) = prev;
156*7c478bd9Sstevel@tonic-gate 		return (E_OK);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 	return (E_BAD_ARGUMENT);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate char *
_makearg(FIELDTYPE * t,va_list * ap,int * err)162*7c478bd9Sstevel@tonic-gate _makearg(FIELDTYPE *t, va_list *ap, int *err)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate /*
165*7c478bd9Sstevel@tonic-gate  * invoke make_arg function associated with field type t.
166*7c478bd9Sstevel@tonic-gate  * return pointer to argument information or null if none.
167*7c478bd9Sstevel@tonic-gate  * increment err if an error is encountered.
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate 	char *p = (char *)0;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	if (! t || ! Status(t, ARGS))
172*7c478bd9Sstevel@tonic-gate 		return (p);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED)) {
175*7c478bd9Sstevel@tonic-gate 		LINK *n = (LINK *) 0;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 		if (Alloc(n, LINK)) {
178*7c478bd9Sstevel@tonic-gate 			ArgL(n) = _makearg(TypeL(t), ap, err);
179*7c478bd9Sstevel@tonic-gate 			ArgR(n) = _makearg(TypeR(t), ap, err);
180*7c478bd9Sstevel@tonic-gate 			p = (char *)n;
181*7c478bd9Sstevel@tonic-gate 		} else
182*7c478bd9Sstevel@tonic-gate 			++(*err);		/* out of space */
183*7c478bd9Sstevel@tonic-gate 	} else
184*7c478bd9Sstevel@tonic-gate 		if (!(p = (*MakeA(t)) (ap)))
185*7c478bd9Sstevel@tonic-gate 			++(*err);		/* make_arg had problem */
186*7c478bd9Sstevel@tonic-gate 	return (p);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate char *
_copyarg(FIELDTYPE * t,char * arg,int * err)190*7c478bd9Sstevel@tonic-gate _copyarg(FIELDTYPE *t, char *arg, int *err)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate /*
193*7c478bd9Sstevel@tonic-gate  * invoke copy_arg function associated with field type t.
194*7c478bd9Sstevel@tonic-gate  * return pointer to argument information or null if none.
195*7c478bd9Sstevel@tonic-gate  * increment err if an error is encountered.
196*7c478bd9Sstevel@tonic-gate  */
197*7c478bd9Sstevel@tonic-gate 	char *p = (char *)0;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (!t || !Status(t, ARGS))
200*7c478bd9Sstevel@tonic-gate 		return (p);
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED)) {
203*7c478bd9Sstevel@tonic-gate 		LINK *n = (LINK *) 0;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (Alloc(n, LINK)) {
206*7c478bd9Sstevel@tonic-gate 			ArgL(n) = _copyarg(TypeL(t), ArgL(arg), err);
207*7c478bd9Sstevel@tonic-gate 			ArgR(n) = _copyarg(TypeR(t), ArgR(arg), err);
208*7c478bd9Sstevel@tonic-gate 			p = (char *)n;
209*7c478bd9Sstevel@tonic-gate 		} else
210*7c478bd9Sstevel@tonic-gate 			++(*err);		/* out of space */
211*7c478bd9Sstevel@tonic-gate 	} else
212*7c478bd9Sstevel@tonic-gate 		if (!(p = (*CopyA(t)) (arg)))
213*7c478bd9Sstevel@tonic-gate 			++(*err);		/* copy_arg had problem */
214*7c478bd9Sstevel@tonic-gate 	return (p);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate /* _freearg - invoke free_arg function associated with field type t.  */
218*7c478bd9Sstevel@tonic-gate void
_freearg(FIELDTYPE * t,char * arg)219*7c478bd9Sstevel@tonic-gate _freearg(FIELDTYPE *t, char *arg)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	if (!t || !Status(t, ARGS))
222*7c478bd9Sstevel@tonic-gate 		return;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED)) {
225*7c478bd9Sstevel@tonic-gate 		_freearg(TypeL(t), ArgL(arg));
226*7c478bd9Sstevel@tonic-gate 		_freearg(TypeR(t), ArgR(arg));
227*7c478bd9Sstevel@tonic-gate 		Free(arg);
228*7c478bd9Sstevel@tonic-gate 	} else
229*7c478bd9Sstevel@tonic-gate 		(*FreeA(t)) (arg);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate /* _checkfield - invoke check_field function associated with field type t.  */
233*7c478bd9Sstevel@tonic-gate int
_checkfield(FIELDTYPE * t,FIELD * f,char * arg)234*7c478bd9Sstevel@tonic-gate _checkfield(FIELDTYPE *t, FIELD *f, char *arg)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	if (!t)
237*7c478bd9Sstevel@tonic-gate 		return (TRUE);
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	if (Opt(f, O_NULLOK)) {
240*7c478bd9Sstevel@tonic-gate 		char *v = Buf(f);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		while (*v && *v == ' ')
243*7c478bd9Sstevel@tonic-gate 			++v;
244*7c478bd9Sstevel@tonic-gate 		if (!*v)
245*7c478bd9Sstevel@tonic-gate 			return (TRUE);	/* empty field */
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED))
248*7c478bd9Sstevel@tonic-gate 		return	(_checkfield(TypeL(t), f, ArgL(arg)) ||
249*7c478bd9Sstevel@tonic-gate 		    _checkfield(TypeR(t), f, ArgR(arg)));
250*7c478bd9Sstevel@tonic-gate 	else
251*7c478bd9Sstevel@tonic-gate 		if (Fcheck(t))
252*7c478bd9Sstevel@tonic-gate 			return ((*Fcheck(t)) (f, arg));
253*7c478bd9Sstevel@tonic-gate 	return (TRUE);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate /* _checkchar - invoke check_char function associated with field type t.  */
257*7c478bd9Sstevel@tonic-gate int
_checkchar(FIELDTYPE * t,int c,char * arg)258*7c478bd9Sstevel@tonic-gate _checkchar(FIELDTYPE *t, int c, char *arg)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	if (!t)
261*7c478bd9Sstevel@tonic-gate 		return (TRUE);
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED))
264*7c478bd9Sstevel@tonic-gate 		return	(_checkchar(TypeL(t), c, ArgL(arg)) ||
265*7c478bd9Sstevel@tonic-gate 		    _checkchar(TypeR(t), c, ArgR(arg)));
266*7c478bd9Sstevel@tonic-gate 	else
267*7c478bd9Sstevel@tonic-gate 		if (Ccheck(t))
268*7c478bd9Sstevel@tonic-gate 			return ((*Ccheck(t)) (c, arg));
269*7c478bd9Sstevel@tonic-gate 	return (TRUE);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /* _nextchoice - invoke next_choice function associated with field type t.  */
273*7c478bd9Sstevel@tonic-gate int
_nextchoice(FIELDTYPE * t,FIELD * f,char * arg)274*7c478bd9Sstevel@tonic-gate _nextchoice(FIELDTYPE *t, FIELD *f, char *arg)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	if (!t || !Status(t, CHOICE))
277*7c478bd9Sstevel@tonic-gate 		return (FALSE);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED))
280*7c478bd9Sstevel@tonic-gate 		return	(_nextchoice(TypeL(t), f, ArgL(arg)) ||
281*7c478bd9Sstevel@tonic-gate 		    _nextchoice(TypeR(t), f, ArgR(arg)));
282*7c478bd9Sstevel@tonic-gate 	else
283*7c478bd9Sstevel@tonic-gate 		return ((*Next(t)) (f, arg));
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate /* _prevchoice - invoke prev_choice function associated with field type t. */
287*7c478bd9Sstevel@tonic-gate int
_prevchoice(FIELDTYPE * t,FIELD * f,char * arg)288*7c478bd9Sstevel@tonic-gate _prevchoice(FIELDTYPE *t, FIELD *f, char *arg)
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	if (!t || !Status(t, CHOICE))
291*7c478bd9Sstevel@tonic-gate 		return (FALSE);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	if (Status(t, LINKED))
294*7c478bd9Sstevel@tonic-gate 		return	(_prevchoice(TypeL(t), f, ArgL(arg)) ||
295*7c478bd9Sstevel@tonic-gate 		    _prevchoice(TypeR(t), f, ArgR(arg)));
296*7c478bd9Sstevel@tonic-gate 	else
297*7c478bd9Sstevel@tonic-gate 		return ((*Prev(t)) (f, arg));
298*7c478bd9Sstevel@tonic-gate }
299