xref: /onnv-gate/usr/src/lib/udapl/libdat/common/udat_sr_parser.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 /*
23*0Sstevel@tonic-gate  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * MODULE: dat_sr_parser.c
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * PURPOSE: static registry parser
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * $Id: udat_sr_parser.c,v 1.1 2003/07/31 14:04:19 jlentini Exp $
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "udat_sr_parser.h"
45*0Sstevel@tonic-gate #include "dat_sr.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * Constants
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	DAT_SR_CONF_ENV 		"DAT_OVERRIDE"
55*0Sstevel@tonic-gate #define	DAT_SR_CONF_DEFAULT 		"/etc/dat/dat.conf"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #define	DAT_SR_TOKEN_THREADSAFE 	"threadsafe"
58*0Sstevel@tonic-gate #define	DAT_SR_TOKEN_NONTHREADSAFE 	"nonthreadsafe"
59*0Sstevel@tonic-gate #define	DAT_SR_TOKEN_DEFAULT 		"default"
60*0Sstevel@tonic-gate #define	DAT_SR_TOKEN_NONDEFAULT 	"nondefault"
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	DAT_SR_CHAR_NEWLINE 		'\n'
63*0Sstevel@tonic-gate #define	DAT_SR_CHAR_COMMENT 		'#'
64*0Sstevel@tonic-gate #define	DAT_SR_CHAR_QUOTE 		'"'
65*0Sstevel@tonic-gate #define	DAT_SR_CHAR_BACKSLASH 		'\\'
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * Enumerations
71*0Sstevel@tonic-gate  *
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate typedef enum
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	DAT_SR_TOKEN_STRING,	/* text field (both quoted or unquoted)	*/
77*0Sstevel@tonic-gate 	DAT_SR_TOKEN_EOR,	/* end of record (newline)		*/
78*0Sstevel@tonic-gate 	DAT_SR_TOKEN_EOF 	/* end of file				*/
79*0Sstevel@tonic-gate } DAT_SR_TOKEN_TYPE;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate typedef enum
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	DAT_SR_API_UDAT,
84*0Sstevel@tonic-gate 	DAT_SR_API_KDAT
85*0Sstevel@tonic-gate } DAT_SR_API_TYPE;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  * Structures
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate typedef struct
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate     DAT_SR_TOKEN_TYPE 	type;
97*0Sstevel@tonic-gate     char		*value; /* valid if type is DAT_SR_TOKEN_STRING */
98*0Sstevel@tonic-gate     DAT_OS_SIZE 	value_len;
99*0Sstevel@tonic-gate } DAT_SR_TOKEN;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate typedef struct DAT_SR_STACK_NODE
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate     DAT_SR_TOKEN 		token;
104*0Sstevel@tonic-gate     struct DAT_SR_STACK_NODE    *next;
105*0Sstevel@tonic-gate } DAT_SR_STACK_NODE;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate typedef struct
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate     DAT_UINT32 			major;
110*0Sstevel@tonic-gate     DAT_UINT32 			minor;
111*0Sstevel@tonic-gate } DAT_SR_VERSION;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate typedef struct
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate     char			*id;
116*0Sstevel@tonic-gate     DAT_SR_VERSION 		version;
117*0Sstevel@tonic-gate } DAT_SR_PROVIDER_VERSION;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate typedef struct
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate     DAT_SR_API_TYPE 		type;
122*0Sstevel@tonic-gate     DAT_SR_VERSION 		version;
123*0Sstevel@tonic-gate } DAT_SR_API_VERSION;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate typedef struct
126*0Sstevel@tonic-gate {
127*0Sstevel@tonic-gate     char			*ia_name;
128*0Sstevel@tonic-gate     DAT_SR_API_VERSION 		api_version;
129*0Sstevel@tonic-gate     DAT_BOOLEAN 		is_thread_safe;
130*0Sstevel@tonic-gate     DAT_BOOLEAN 		is_default;
131*0Sstevel@tonic-gate     char 			*lib_path;
132*0Sstevel@tonic-gate     DAT_SR_PROVIDER_VERSION 	provider_version;
133*0Sstevel@tonic-gate     char 			*ia_params;
134*0Sstevel@tonic-gate     char  			*platform_params;
135*0Sstevel@tonic-gate } DAT_SR_CONF_ENTRY;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /*
139*0Sstevel@tonic-gate  *
140*0Sstevel@tonic-gate  * Internal Function Declarations
141*0Sstevel@tonic-gate  *
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate static DAT_RETURN
145*0Sstevel@tonic-gate dat_sr_load_entry(
146*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate static DAT_BOOLEAN
149*0Sstevel@tonic-gate dat_sr_is_valid_entry(
150*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate static char *
153*0Sstevel@tonic-gate dat_sr_type_to_str(
154*0Sstevel@tonic-gate     DAT_SR_TOKEN_TYPE type);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static DAT_RETURN
157*0Sstevel@tonic-gate dat_sr_parse_eof(
158*0Sstevel@tonic-gate     DAT_OS_FILE *file);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate static DAT_RETURN
161*0Sstevel@tonic-gate dat_sr_parse_entry(
162*0Sstevel@tonic-gate     DAT_OS_FILE *file);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static DAT_RETURN
165*0Sstevel@tonic-gate dat_sr_parse_ia_name(
166*0Sstevel@tonic-gate     DAT_OS_FILE *file,
167*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static DAT_RETURN
170*0Sstevel@tonic-gate dat_sr_parse_api(
171*0Sstevel@tonic-gate     DAT_OS_FILE *file,
172*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate static DAT_RETURN
175*0Sstevel@tonic-gate dat_sr_parse_thread_safety(
176*0Sstevel@tonic-gate     DAT_OS_FILE *file,
177*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate static DAT_RETURN
180*0Sstevel@tonic-gate dat_sr_parse_default(
181*0Sstevel@tonic-gate     DAT_OS_FILE *file,
182*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static DAT_RETURN
185*0Sstevel@tonic-gate dat_sr_parse_lib_path(
186*0Sstevel@tonic-gate     DAT_OS_FILE *file,
187*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static DAT_RETURN
190*0Sstevel@tonic-gate dat_sr_parse_provider_version(
191*0Sstevel@tonic-gate     DAT_OS_FILE *file,
192*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static DAT_RETURN
195*0Sstevel@tonic-gate dat_sr_parse_ia_params(
196*0Sstevel@tonic-gate     DAT_OS_FILE *file,
197*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate static DAT_RETURN
200*0Sstevel@tonic-gate dat_sr_parse_platform_params(
201*0Sstevel@tonic-gate     DAT_OS_FILE *file,
202*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate static DAT_RETURN
205*0Sstevel@tonic-gate dat_sr_parse_eoe(
206*0Sstevel@tonic-gate     DAT_OS_FILE *file,
207*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate static DAT_RETURN
210*0Sstevel@tonic-gate dat_sr_convert_api(
211*0Sstevel@tonic-gate     char *str,
212*0Sstevel@tonic-gate     DAT_SR_API_VERSION *api_version);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate static DAT_RETURN
215*0Sstevel@tonic-gate dat_sr_convert_thread_safety(
216*0Sstevel@tonic-gate     char *str,
217*0Sstevel@tonic-gate     DAT_BOOLEAN *is_thread_safe);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static DAT_RETURN
220*0Sstevel@tonic-gate dat_sr_convert_default(
221*0Sstevel@tonic-gate     char *str,
222*0Sstevel@tonic-gate     DAT_BOOLEAN *is_default);
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate static DAT_RETURN
225*0Sstevel@tonic-gate dat_sr_convert_provider_version(
226*0Sstevel@tonic-gate     char *str,
227*0Sstevel@tonic-gate     DAT_SR_PROVIDER_VERSION *provider_version);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate static DAT_RETURN
230*0Sstevel@tonic-gate dat_sr_get_token(
231*0Sstevel@tonic-gate     DAT_OS_FILE *file,
232*0Sstevel@tonic-gate     DAT_SR_TOKEN *token);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate static DAT_RETURN
235*0Sstevel@tonic-gate dat_sr_put_token(
236*0Sstevel@tonic-gate     DAT_OS_FILE *file,
237*0Sstevel@tonic-gate     DAT_SR_TOKEN *token);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static DAT_RETURN
240*0Sstevel@tonic-gate dat_sr_read_token(
241*0Sstevel@tonic-gate     DAT_OS_FILE *file,
242*0Sstevel@tonic-gate     DAT_SR_TOKEN *token);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate static DAT_RETURN
245*0Sstevel@tonic-gate dat_sr_read_str(
246*0Sstevel@tonic-gate     DAT_OS_FILE *file,
247*0Sstevel@tonic-gate     DAT_SR_TOKEN *token,
248*0Sstevel@tonic-gate     DAT_OS_SIZE token_len);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate static DAT_RETURN
251*0Sstevel@tonic-gate dat_sr_read_quoted_str(
252*0Sstevel@tonic-gate     DAT_OS_FILE *file,
253*0Sstevel@tonic-gate     DAT_SR_TOKEN *token,
254*0Sstevel@tonic-gate     DAT_OS_SIZE token_len,
255*0Sstevel@tonic-gate     DAT_COUNT num_escape_seq);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate static void
258*0Sstevel@tonic-gate dat_sr_read_comment(
259*0Sstevel@tonic-gate     DAT_OS_FILE *file);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate  *
264*0Sstevel@tonic-gate  * Global Variables
265*0Sstevel@tonic-gate  *
266*0Sstevel@tonic-gate  */
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate static DAT_SR_STACK_NODE 	*g_token_stack = NULL;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate /*
272*0Sstevel@tonic-gate  *
273*0Sstevel@tonic-gate  * External Function Definitions
274*0Sstevel@tonic-gate  *
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /*
278*0Sstevel@tonic-gate  * Function: dat_sr_load
279*0Sstevel@tonic-gate  */
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate DAT_RETURN
dat_sr_load(void)282*0Sstevel@tonic-gate dat_sr_load(void)
283*0Sstevel@tonic-gate {
284*0Sstevel@tonic-gate 	char 			*sr_path;
285*0Sstevel@tonic-gate 	DAT_OS_FILE 		*sr_file;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	sr_path = dat_os_getenv(DAT_SR_CONF_ENV);
288*0Sstevel@tonic-gate 	if (sr_path == NULL) {
289*0Sstevel@tonic-gate 		sr_path = DAT_SR_CONF_DEFAULT;
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
293*0Sstevel@tonic-gate 	    "DAT Registry: static registry file <%s> \n", sr_path);
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	sr_file = dat_os_fopen(sr_path);
296*0Sstevel@tonic-gate 	if (sr_file == NULL) {
297*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	for (;;) {
301*0Sstevel@tonic-gate 		if (DAT_SUCCESS == dat_sr_parse_eof(sr_file)) {
302*0Sstevel@tonic-gate 			break;
303*0Sstevel@tonic-gate 		} else if (DAT_SUCCESS == dat_sr_parse_entry(sr_file)) {
304*0Sstevel@tonic-gate 			continue;
305*0Sstevel@tonic-gate 		} else {
306*0Sstevel@tonic-gate 			dat_os_assert(!"unable to parse static registry file");
307*0Sstevel@tonic-gate 			break;
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	if (0 != dat_os_fclose(sr_file)) {
312*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate /*
320*0Sstevel@tonic-gate  *
321*0Sstevel@tonic-gate  * Internal Function Definitions
322*0Sstevel@tonic-gate  *
323*0Sstevel@tonic-gate  */
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * Function: dat_sr_is_valid_entry
327*0Sstevel@tonic-gate  */
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate DAT_BOOLEAN
dat_sr_is_valid_entry(DAT_SR_CONF_ENTRY * entry)330*0Sstevel@tonic-gate dat_sr_is_valid_entry(
331*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate 	if ((DAT_SR_API_UDAT == entry->api_version.type) &&
334*0Sstevel@tonic-gate 	    (entry->is_default)) {
335*0Sstevel@tonic-gate 		return (DAT_TRUE);
336*0Sstevel@tonic-gate 	} else {
337*0Sstevel@tonic-gate 		return (DAT_FALSE);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate /*
343*0Sstevel@tonic-gate  * Function: dat_sr_load_entry
344*0Sstevel@tonic-gate  */
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate DAT_RETURN
dat_sr_load_entry(DAT_SR_CONF_ENTRY * conf_entry)347*0Sstevel@tonic-gate dat_sr_load_entry(
348*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *conf_entry)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	DAT_SR_ENTRY entry;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if (DAT_NAME_MAX_LENGTH < (strlen(conf_entry->ia_name) + 1)) {
353*0Sstevel@tonic-gate 		dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
354*0Sstevel@tonic-gate 		    "DAT Registry: ia name %s is longer than "
355*0Sstevel@tonic-gate 		    "DAT_NAME_MAX_LENGTH (%i)\n",
356*0Sstevel@tonic-gate 		    conf_entry->ia_name, DAT_NAME_MAX_LENGTH);
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 		return (DAT_INSUFFICIENT_RESOURCES);
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	(void) dat_os_strncpy(entry.info.ia_name, conf_entry->ia_name,
362*0Sstevel@tonic-gate 	    DAT_NAME_MAX_LENGTH);
363*0Sstevel@tonic-gate 	entry.info.dapl_version_major = conf_entry->api_version.version.major;
364*0Sstevel@tonic-gate 	entry.info.dapl_version_minor = conf_entry->api_version.version.minor;
365*0Sstevel@tonic-gate 	entry.info.is_thread_safe = conf_entry->is_thread_safe;
366*0Sstevel@tonic-gate 	entry.lib_path = conf_entry->lib_path;
367*0Sstevel@tonic-gate 	entry.ia_params = conf_entry->ia_params;
368*0Sstevel@tonic-gate 	entry.lib_handle = NULL;
369*0Sstevel@tonic-gate 	entry.ref_count = 0;
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
372*0Sstevel@tonic-gate 	    "DAT Registry: loading provider for %s\n",
373*0Sstevel@tonic-gate 	    conf_entry->ia_name);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	return (dat_sr_insert(&entry.info, &entry));
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate /*
380*0Sstevel@tonic-gate  * Function: dat_sr_type_to_str
381*0Sstevel@tonic-gate  */
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate char *
dat_sr_type_to_str(DAT_SR_TOKEN_TYPE type)384*0Sstevel@tonic-gate dat_sr_type_to_str(
385*0Sstevel@tonic-gate     DAT_SR_TOKEN_TYPE type)
386*0Sstevel@tonic-gate {
387*0Sstevel@tonic-gate 	static char *str_array[] = { "string", "eor", "eof" };
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if ((type < 0) || (2 < type)) {
390*0Sstevel@tonic-gate 		return ("error: invalid token type");
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	return (str_array[type]);
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate /*
398*0Sstevel@tonic-gate  * Function: dat_sr_parse_eof
399*0Sstevel@tonic-gate  */
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_eof(DAT_OS_FILE * file)402*0Sstevel@tonic-gate dat_sr_parse_eof(
403*0Sstevel@tonic-gate 	DAT_OS_FILE 		*file)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
408*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_EOF == token.type) {
412*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
413*0Sstevel@tonic-gate 	} else {
414*0Sstevel@tonic-gate 		(void) dat_sr_put_token(file, &token);
415*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate /*
421*0Sstevel@tonic-gate  * Function: dat_sr_parse_ia_name
422*0Sstevel@tonic-gate  */
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_entry(DAT_OS_FILE * file)425*0Sstevel@tonic-gate dat_sr_parse_entry(
426*0Sstevel@tonic-gate     DAT_OS_FILE 		*file)
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	DAT_SR_CONF_ENTRY		entry;
429*0Sstevel@tonic-gate 	DAT_RETURN 			status;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	(void) dat_os_memset(&entry, 0, sizeof (DAT_SR_CONF_ENTRY));
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if ((DAT_SUCCESS == dat_sr_parse_ia_name(file, &entry)) &&
434*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_api(file, &entry)) &&
435*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_thread_safety(file, &entry)) &&
436*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_default(file, &entry)) &&
437*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_lib_path(file, &entry)) &&
438*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_provider_version(file, &entry)) &&
439*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_ia_params(file, &entry)) &&
440*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_platform_params(file, &entry)) &&
441*0Sstevel@tonic-gate 	    (DAT_SUCCESS == dat_sr_parse_eoe(file, &entry))) {
442*0Sstevel@tonic-gate 		dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
443*0Sstevel@tonic-gate 		    "\n"
444*0Sstevel@tonic-gate 		    "DAT Registry: entry \n"
445*0Sstevel@tonic-gate 		    " ia_name %s\n"
446*0Sstevel@tonic-gate 		    " api_version\n"
447*0Sstevel@tonic-gate 		    "     type 0x%X\n"
448*0Sstevel@tonic-gate 		    "     major.minor %d.%d\n"
449*0Sstevel@tonic-gate 		    " is_thread_safe %d\n"
450*0Sstevel@tonic-gate 		    " is_default %d\n"
451*0Sstevel@tonic-gate 		    " lib_path %s\n"
452*0Sstevel@tonic-gate 		    " provider_version\n"
453*0Sstevel@tonic-gate 		    "     id %s\n"
454*0Sstevel@tonic-gate 		    "     major.minor %d.%d\n"
455*0Sstevel@tonic-gate 		    " ia_params %s\n"
456*0Sstevel@tonic-gate 		    "\n",
457*0Sstevel@tonic-gate 		    entry.ia_name,
458*0Sstevel@tonic-gate 		    entry.api_version.type,
459*0Sstevel@tonic-gate 		    entry.api_version.version.major,
460*0Sstevel@tonic-gate 		    entry.api_version.version.minor,
461*0Sstevel@tonic-gate 		    entry.is_thread_safe,
462*0Sstevel@tonic-gate 		    entry.is_default,
463*0Sstevel@tonic-gate 		    entry.lib_path,
464*0Sstevel@tonic-gate 		    entry.provider_version.id,
465*0Sstevel@tonic-gate 		    entry.provider_version.version.major,
466*0Sstevel@tonic-gate 		    entry.provider_version.version.minor,
467*0Sstevel@tonic-gate 		    entry.ia_params);
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 		if (DAT_TRUE == dat_sr_is_valid_entry(&entry)) {
470*0Sstevel@tonic-gate 			/*
471*0Sstevel@tonic-gate 			 * The static registry configuration file may have
472*0Sstevel@tonic-gate 			 * multiple entries with the same IA name. The first
473*0Sstevel@tonic-gate 			 * entry will be installed in the static registry
474*0Sstevel@tonic-gate 			 * causing subsequent attempts to register the same IA
475*0Sstevel@tonic-gate 			 * name to fail. Therefore the return code from
476*0Sstevel@tonic-gate 			 * dat_sr_load_entry() is ignored.
477*0Sstevel@tonic-gate 			 */
478*0Sstevel@tonic-gate 			(void) dat_sr_load_entry(&entry);
479*0Sstevel@tonic-gate 		}
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
482*0Sstevel@tonic-gate 	} else { /* resync */
483*0Sstevel@tonic-gate 		DAT_SR_TOKEN 		token;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 		/*
486*0Sstevel@tonic-gate 		 * The static registry format is specified in the DAT
487*0Sstevel@tonic-gate 		 * specification. While the registry file's contents may change
488*0Sstevel@tonic-gate 		 * between revisions of the specification, there is no way to
489*0Sstevel@tonic-gate 		 * determine the specification version to which the
490*0Sstevel@tonic-gate 		 * configuration file conforms. If an entry is found that does
491*0Sstevel@tonic-gate 		 * not match the expected format, the entry is discarded
492*0Sstevel@tonic-gate 		 * and the parsing of the file continues. There is no way to
493*0Sstevel@tonic-gate 		 * determine if the entry was an error or an entry confirming
494*0Sstevel@tonic-gate 		 * to an alternate version of specification.
495*0Sstevel@tonic-gate 		 */
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 		for (;;) {
498*0Sstevel@tonic-gate 			if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
499*0Sstevel@tonic-gate 				status = DAT_INTERNAL_ERROR;
500*0Sstevel@tonic-gate 				break;
501*0Sstevel@tonic-gate 			}
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 			if (DAT_SR_TOKEN_STRING != token.type) {
504*0Sstevel@tonic-gate 				status = DAT_SUCCESS;
505*0Sstevel@tonic-gate 				break;
506*0Sstevel@tonic-gate 			} else {
507*0Sstevel@tonic-gate 				dat_os_free(token.value,
508*0Sstevel@tonic-gate 				    (sizeof (char) *
509*0Sstevel@tonic-gate 					dat_os_strlen(token.value)) + 1);
510*0Sstevel@tonic-gate 				continue;
511*0Sstevel@tonic-gate 			}
512*0Sstevel@tonic-gate 		}
513*0Sstevel@tonic-gate 	}
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	/* free resources */
516*0Sstevel@tonic-gate 	if (NULL != entry.ia_name) {
517*0Sstevel@tonic-gate 		dat_os_free(entry.ia_name,
518*0Sstevel@tonic-gate 		    sizeof (char) * (dat_os_strlen(entry.ia_name) + 1));
519*0Sstevel@tonic-gate 	}
520*0Sstevel@tonic-gate 	if (NULL != entry.lib_path) {
521*0Sstevel@tonic-gate 		dat_os_free(entry.lib_path,
522*0Sstevel@tonic-gate 		    sizeof (char) * (dat_os_strlen(entry.lib_path) + 1));
523*0Sstevel@tonic-gate 	}
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	if (NULL != entry.provider_version.id) {
526*0Sstevel@tonic-gate 		dat_os_free(entry.provider_version.id,
527*0Sstevel@tonic-gate 		    sizeof (char) *
528*0Sstevel@tonic-gate 		    (dat_os_strlen(entry.provider_version.id) + 1));
529*0Sstevel@tonic-gate 	}
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	if (NULL != entry.ia_params) {
532*0Sstevel@tonic-gate 		dat_os_free(entry.ia_params,
533*0Sstevel@tonic-gate 		    sizeof (char) * (dat_os_strlen(entry.ia_params) + 1));
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	return (status);
537*0Sstevel@tonic-gate }
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * Function: dat_sr_parse_ia_name
542*0Sstevel@tonic-gate  */
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_ia_name(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)545*0Sstevel@tonic-gate dat_sr_parse_ia_name(
546*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
547*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
550*0Sstevel@tonic-gate 	DAT_RETURN 	status;
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
553*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
554*0Sstevel@tonic-gate 	}
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
557*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
558*0Sstevel@tonic-gate 	} else {
559*0Sstevel@tonic-gate 		entry->ia_name = token.value;
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
562*0Sstevel@tonic-gate 	}
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
565*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
568*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	return (status);
572*0Sstevel@tonic-gate }
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate /*
576*0Sstevel@tonic-gate  * Function: dat_sr_parse_ia_name
577*0Sstevel@tonic-gate  */
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_api(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)580*0Sstevel@tonic-gate dat_sr_parse_api(
581*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
582*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
583*0Sstevel@tonic-gate {
584*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
585*0Sstevel@tonic-gate 	DAT_RETURN 	status;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
588*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
589*0Sstevel@tonic-gate 	}
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
592*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
593*0Sstevel@tonic-gate 	} else if (DAT_SUCCESS != dat_sr_convert_api(
594*0Sstevel@tonic-gate 		token.value, &entry->api_version)) {
595*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
596*0Sstevel@tonic-gate 	} else {
597*0Sstevel@tonic-gate 		dat_os_free(token.value,
598*0Sstevel@tonic-gate 		    (sizeof (char) * dat_os_strlen(token.value)) + 1);
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
604*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
607*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
608*0Sstevel@tonic-gate 	}
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	return (status);
611*0Sstevel@tonic-gate }
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate /*
615*0Sstevel@tonic-gate  * Function: dat_sr_parse_thread_safety
616*0Sstevel@tonic-gate  */
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate static DAT_RETURN
dat_sr_parse_thread_safety(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)619*0Sstevel@tonic-gate dat_sr_parse_thread_safety(
620*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
621*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
622*0Sstevel@tonic-gate {
623*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
624*0Sstevel@tonic-gate 	DAT_RETURN 	status;
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
627*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
628*0Sstevel@tonic-gate 	}
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
631*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
632*0Sstevel@tonic-gate 	} else if (DAT_SUCCESS != dat_sr_convert_thread_safety(
633*0Sstevel@tonic-gate 		token.value, &entry->is_thread_safe)) {
634*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
635*0Sstevel@tonic-gate 	} else {
636*0Sstevel@tonic-gate 		dat_os_free(token.value,
637*0Sstevel@tonic-gate 		    (sizeof (char) * dat_os_strlen(token.value)) + 1);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
640*0Sstevel@tonic-gate 	}
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
643*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
646*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	return (status);
650*0Sstevel@tonic-gate }
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate /*
654*0Sstevel@tonic-gate  * Function: dat_sr_parse_default
655*0Sstevel@tonic-gate  */
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_default(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)658*0Sstevel@tonic-gate dat_sr_parse_default(
659*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
660*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
661*0Sstevel@tonic-gate {
662*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
663*0Sstevel@tonic-gate 	DAT_RETURN 	status;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
666*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
667*0Sstevel@tonic-gate 	}
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
670*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
671*0Sstevel@tonic-gate 	} else if (DAT_SUCCESS != dat_sr_convert_default(
672*0Sstevel@tonic-gate 		token.value, &entry->is_default)) {
673*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
674*0Sstevel@tonic-gate 	} else {
675*0Sstevel@tonic-gate 		dat_os_free(token.value,
676*0Sstevel@tonic-gate 		    (sizeof (char) * dat_os_strlen(token.value)) + 1);
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
679*0Sstevel@tonic-gate 	}
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
682*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
685*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	return (status);
689*0Sstevel@tonic-gate }
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate /*
693*0Sstevel@tonic-gate  * Function: dat_sr_parse_lib_path
694*0Sstevel@tonic-gate  */
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_lib_path(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)697*0Sstevel@tonic-gate dat_sr_parse_lib_path(
698*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
699*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
700*0Sstevel@tonic-gate {
701*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
702*0Sstevel@tonic-gate 	DAT_RETURN 	status;
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
705*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
706*0Sstevel@tonic-gate 	}
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
709*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
710*0Sstevel@tonic-gate 	} else {
711*0Sstevel@tonic-gate 		entry->lib_path = token.value;
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
714*0Sstevel@tonic-gate 	}
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
717*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
720*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
721*0Sstevel@tonic-gate 	}
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	return (status);
724*0Sstevel@tonic-gate }
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate /*
727*0Sstevel@tonic-gate  * Function: dat_sr_parse_provider_version
728*0Sstevel@tonic-gate  */
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_provider_version(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)731*0Sstevel@tonic-gate dat_sr_parse_provider_version(
732*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
733*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY 	*entry)
734*0Sstevel@tonic-gate {
735*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
736*0Sstevel@tonic-gate 	DAT_RETURN 	status;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
739*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
743*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
744*0Sstevel@tonic-gate 	} else if (DAT_SUCCESS != dat_sr_convert_provider_version(
745*0Sstevel@tonic-gate 		token.value, &entry->provider_version)) {
746*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
747*0Sstevel@tonic-gate 	} else {
748*0Sstevel@tonic-gate 		dat_os_free(token.value,
749*0Sstevel@tonic-gate 		    (sizeof (char) * dat_os_strlen(token.value)) + 1);
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
755*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
758*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
759*0Sstevel@tonic-gate 	}
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	return (status);
762*0Sstevel@tonic-gate }
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate /*
766*0Sstevel@tonic-gate  * Function: dat_sr_parse_ia_params
767*0Sstevel@tonic-gate  */
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_ia_params(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)770*0Sstevel@tonic-gate dat_sr_parse_ia_params(
771*0Sstevel@tonic-gate     DAT_OS_FILE *file,
772*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry)
773*0Sstevel@tonic-gate {
774*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
775*0Sstevel@tonic-gate 	DAT_RETURN 	status;
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
778*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
779*0Sstevel@tonic-gate 	}
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
782*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
783*0Sstevel@tonic-gate 	} else {
784*0Sstevel@tonic-gate 		entry->ia_params = token.value;
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
787*0Sstevel@tonic-gate 	}
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
790*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
793*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 
796*0Sstevel@tonic-gate 	return (status);
797*0Sstevel@tonic-gate }
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate /*
801*0Sstevel@tonic-gate  * Function: dat_sr_parse_platform_params
802*0Sstevel@tonic-gate  */
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_platform_params(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)805*0Sstevel@tonic-gate dat_sr_parse_platform_params(
806*0Sstevel@tonic-gate     DAT_OS_FILE *file,
807*0Sstevel@tonic-gate     DAT_SR_CONF_ENTRY *entry)
808*0Sstevel@tonic-gate {
809*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
810*0Sstevel@tonic-gate 	DAT_RETURN 	status;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
813*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
814*0Sstevel@tonic-gate 	}
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 	if (DAT_SR_TOKEN_STRING != token.type) {
817*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
818*0Sstevel@tonic-gate 	} else {
819*0Sstevel@tonic-gate 		entry->platform_params = token.value;
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
822*0Sstevel@tonic-gate 	}
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
825*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
828*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
829*0Sstevel@tonic-gate 	}
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 	return (status);
832*0Sstevel@tonic-gate }
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate /*
836*0Sstevel@tonic-gate  * Function: dat_sr_parse_eoe
837*0Sstevel@tonic-gate  */
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate DAT_RETURN
dat_sr_parse_eoe(DAT_OS_FILE * file,DAT_SR_CONF_ENTRY * entry)840*0Sstevel@tonic-gate dat_sr_parse_eoe(
841*0Sstevel@tonic-gate     DAT_OS_FILE *file,
842*0Sstevel@tonic-gate 	DAT_SR_CONF_ENTRY *entry) /*ARGSUSED*/
843*0Sstevel@tonic-gate {
844*0Sstevel@tonic-gate 	DAT_SR_TOKEN 	token;
845*0Sstevel@tonic-gate 	DAT_RETURN 	status;
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_sr_get_token(file, &token)) {
848*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	if ((DAT_SR_TOKEN_EOF != token.type) &&
852*0Sstevel@tonic-gate 	    (DAT_SR_TOKEN_EOR != token.type)) {
853*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
854*0Sstevel@tonic-gate 	} else {
855*0Sstevel@tonic-gate 		status = DAT_SUCCESS;
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
859*0Sstevel@tonic-gate 		DAT_RETURN 	status_success;
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 		status_success = dat_sr_put_token(file, &token);
862*0Sstevel@tonic-gate 		dat_os_assert(DAT_SUCCESS == status_success);
863*0Sstevel@tonic-gate 	}
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	return (status);
866*0Sstevel@tonic-gate }
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate /*
870*0Sstevel@tonic-gate  * Function: dat_sr_convert_api
871*0Sstevel@tonic-gate  */
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate DAT_RETURN
dat_sr_convert_api(char * str,DAT_SR_API_VERSION * api_version)874*0Sstevel@tonic-gate dat_sr_convert_api(
875*0Sstevel@tonic-gate     char *str,
876*0Sstevel@tonic-gate     DAT_SR_API_VERSION *api_version)
877*0Sstevel@tonic-gate {
878*0Sstevel@tonic-gate 	int i;
879*0Sstevel@tonic-gate 	int minor_i;
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 	dat_os_assert(0 < dat_os_strlen(str));
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	if ('u' == str[0]) {
884*0Sstevel@tonic-gate 		api_version->type = DAT_SR_API_UDAT;
885*0Sstevel@tonic-gate 	} else if ('k' == str[0]) {
886*0Sstevel@tonic-gate 		api_version->type = DAT_SR_API_KDAT;
887*0Sstevel@tonic-gate 	} else {
888*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
889*0Sstevel@tonic-gate 	}
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	for (i = 1 /* move past initial [u|k] */; '\0' != str[i]; i++) {
892*0Sstevel@tonic-gate 		if ('.' == str[i]) {
893*0Sstevel@tonic-gate 			break;
894*0Sstevel@tonic-gate 		} else if (DAT_TRUE != dat_os_isdigit(str[i])) {
895*0Sstevel@tonic-gate 			return (DAT_INTERNAL_ERROR);
896*0Sstevel@tonic-gate 		}
897*0Sstevel@tonic-gate 	}
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	api_version->version.major = (DAT_UINT32)dat_os_strtol(str + 1, NULL,
900*0Sstevel@tonic-gate 	    10);
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	/* move past '.' */
903*0Sstevel@tonic-gate 	minor_i = ++i;
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	for (; '\0' != str[i]; i++) {
906*0Sstevel@tonic-gate 		if (DAT_TRUE != dat_os_isdigit(str[i])) {
907*0Sstevel@tonic-gate 			return (DAT_INTERNAL_ERROR);
908*0Sstevel@tonic-gate 		}
909*0Sstevel@tonic-gate 	}
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 	api_version->version.minor = (DAT_UINT32)dat_os_strtol(str + minor_i,
912*0Sstevel@tonic-gate 	    NULL, 10);
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate 	if ('\0' != str[i]) {
915*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
916*0Sstevel@tonic-gate 	}
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
919*0Sstevel@tonic-gate }
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate /*
923*0Sstevel@tonic-gate  * Function: dat_sr_convert_thread_safety
924*0Sstevel@tonic-gate  */
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate static DAT_RETURN
dat_sr_convert_thread_safety(char * str,DAT_BOOLEAN * is_thread_safe)927*0Sstevel@tonic-gate dat_sr_convert_thread_safety(
928*0Sstevel@tonic-gate     char *str,
929*0Sstevel@tonic-gate     DAT_BOOLEAN *is_thread_safe)
930*0Sstevel@tonic-gate {
931*0Sstevel@tonic-gate 	if (!dat_os_strncmp(str,
932*0Sstevel@tonic-gate 	    DAT_SR_TOKEN_THREADSAFE,
933*0Sstevel@tonic-gate 	    dat_os_strlen(DAT_SR_TOKEN_THREADSAFE))) {
934*0Sstevel@tonic-gate 		*is_thread_safe = DAT_TRUE;
935*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
936*0Sstevel@tonic-gate 	} else if (!dat_os_strncmp(str,
937*0Sstevel@tonic-gate 	    DAT_SR_TOKEN_NONTHREADSAFE,
938*0Sstevel@tonic-gate 	    dat_os_strlen(DAT_SR_TOKEN_NONTHREADSAFE))) {
939*0Sstevel@tonic-gate 		*is_thread_safe = DAT_FALSE;
940*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
941*0Sstevel@tonic-gate 	} else {
942*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
943*0Sstevel@tonic-gate 	}
944*0Sstevel@tonic-gate }
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate /*
948*0Sstevel@tonic-gate  * Function: dat_sr_convert_default
949*0Sstevel@tonic-gate  */
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate static DAT_RETURN
dat_sr_convert_default(char * str,DAT_BOOLEAN * is_default)952*0Sstevel@tonic-gate dat_sr_convert_default(
953*0Sstevel@tonic-gate     char *str,
954*0Sstevel@tonic-gate     DAT_BOOLEAN *is_default)
955*0Sstevel@tonic-gate {
956*0Sstevel@tonic-gate 	if (!dat_os_strncmp(str,
957*0Sstevel@tonic-gate 	    DAT_SR_TOKEN_DEFAULT,
958*0Sstevel@tonic-gate 	    dat_os_strlen(DAT_SR_TOKEN_DEFAULT))) {
959*0Sstevel@tonic-gate 		*is_default = DAT_TRUE;
960*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
961*0Sstevel@tonic-gate 	} else if (!dat_os_strncmp(str,
962*0Sstevel@tonic-gate 	    DAT_SR_TOKEN_NONDEFAULT,
963*0Sstevel@tonic-gate 	    dat_os_strlen(DAT_SR_TOKEN_NONDEFAULT))) {
964*0Sstevel@tonic-gate 		*is_default = DAT_FALSE;
965*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
966*0Sstevel@tonic-gate 	} else {
967*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
968*0Sstevel@tonic-gate 	}
969*0Sstevel@tonic-gate }
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate /*
973*0Sstevel@tonic-gate  * Function: dat_sr_convert_provider_version
974*0Sstevel@tonic-gate  */
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate DAT_RETURN
dat_sr_convert_provider_version(char * str,DAT_SR_PROVIDER_VERSION * provider_version)977*0Sstevel@tonic-gate dat_sr_convert_provider_version(
978*0Sstevel@tonic-gate     char *str,
979*0Sstevel@tonic-gate     DAT_SR_PROVIDER_VERSION *provider_version)
980*0Sstevel@tonic-gate {
981*0Sstevel@tonic-gate 	DAT_RETURN 	status;
982*0Sstevel@tonic-gate 	int 		i;
983*0Sstevel@tonic-gate 	int 		decimal_i;
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 	dat_os_assert(0 < dat_os_strlen(str));
986*0Sstevel@tonic-gate 	dat_os_assert(NULL == provider_version->id);
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 	status = DAT_SUCCESS;
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 	for (i = 0; '\0' != str[i]; i++) {
991*0Sstevel@tonic-gate 		if ('.' == str[i]) {
992*0Sstevel@tonic-gate 			break;
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 	}
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	/* if no id value was found */
997*0Sstevel@tonic-gate 	if (0 == i) {
998*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
999*0Sstevel@tonic-gate 		goto exit;
1000*0Sstevel@tonic-gate 	}
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	if (NULL == (provider_version->id = dat_os_alloc(sizeof (char) *
1003*0Sstevel@tonic-gate 	    (i + 1)))) {
1004*0Sstevel@tonic-gate 		status = DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY;
1005*0Sstevel@tonic-gate 		goto exit;
1006*0Sstevel@tonic-gate 	}
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	(void) dat_os_strncpy(provider_version->id, str, i);
1009*0Sstevel@tonic-gate 	provider_version->id[i] = '\0';
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate 	/* move past '.' */
1012*0Sstevel@tonic-gate 	decimal_i = ++i;
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	for (; '\0' != str[i]; i++) {
1015*0Sstevel@tonic-gate 		if ('.' == str[i]) {
1016*0Sstevel@tonic-gate 			break;
1017*0Sstevel@tonic-gate 		} else if (DAT_TRUE != dat_os_isdigit(str[i])) {
1018*0Sstevel@tonic-gate 			status = DAT_INTERNAL_ERROR;
1019*0Sstevel@tonic-gate 			goto exit;
1020*0Sstevel@tonic-gate 		}
1021*0Sstevel@tonic-gate 	}
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	/* if no version value was found */
1024*0Sstevel@tonic-gate 	if (decimal_i == i) {
1025*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
1026*0Sstevel@tonic-gate 		goto exit;
1027*0Sstevel@tonic-gate 	}
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 	provider_version->version.major = (DAT_UINT32)
1030*0Sstevel@tonic-gate 	    dat_os_strtol(str + decimal_i, NULL, 10);
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	/* move past '.' */
1033*0Sstevel@tonic-gate 	decimal_i = ++i;
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 	for (; '\0' != str[i]; i++) {
1036*0Sstevel@tonic-gate 		if (DAT_TRUE != dat_os_isdigit(str[i])) {
1037*0Sstevel@tonic-gate 			status = DAT_INTERNAL_ERROR;
1038*0Sstevel@tonic-gate 			goto exit;
1039*0Sstevel@tonic-gate 		}
1040*0Sstevel@tonic-gate 	}
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 	/* if no version value was found */
1043*0Sstevel@tonic-gate 	if (decimal_i == i) {
1044*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
1045*0Sstevel@tonic-gate 		goto exit;
1046*0Sstevel@tonic-gate 	}
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 	provider_version->version.minor = (DAT_UINT32)
1049*0Sstevel@tonic-gate 	    dat_os_strtol(str + decimal_i, NULL, 10);
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 	if ('\0' != str[i]) {
1052*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
1053*0Sstevel@tonic-gate 		goto exit;
1054*0Sstevel@tonic-gate 	}
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate exit:
1057*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
1058*0Sstevel@tonic-gate 		if (NULL != provider_version->id) {
1059*0Sstevel@tonic-gate 			dat_os_free(provider_version->id,
1060*0Sstevel@tonic-gate 			    sizeof (char) *
1061*0Sstevel@tonic-gate 			    (dat_os_strlen(provider_version->id) + 1));
1062*0Sstevel@tonic-gate 			provider_version->id = NULL;
1063*0Sstevel@tonic-gate 		}
1064*0Sstevel@tonic-gate 	}
1065*0Sstevel@tonic-gate 
1066*0Sstevel@tonic-gate 	return (status);
1067*0Sstevel@tonic-gate }
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate /*
1071*0Sstevel@tonic-gate  * Function: dat_sr_get_token
1072*0Sstevel@tonic-gate  */
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate DAT_RETURN
dat_sr_get_token(DAT_OS_FILE * file,DAT_SR_TOKEN * token)1075*0Sstevel@tonic-gate dat_sr_get_token(
1076*0Sstevel@tonic-gate     DAT_OS_FILE *file,
1077*0Sstevel@tonic-gate     DAT_SR_TOKEN *token)
1078*0Sstevel@tonic-gate {
1079*0Sstevel@tonic-gate 	if (NULL == g_token_stack) {
1080*0Sstevel@tonic-gate 		return (dat_sr_read_token(file, token));
1081*0Sstevel@tonic-gate 	} else {
1082*0Sstevel@tonic-gate 		DAT_SR_STACK_NODE *top;
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 		top = g_token_stack;
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 		*token = top->token;
1087*0Sstevel@tonic-gate 		g_token_stack = top->next;
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 		dat_os_free(top, sizeof (DAT_SR_STACK_NODE));
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 		return (DAT_SUCCESS);
1092*0Sstevel@tonic-gate 	}
1093*0Sstevel@tonic-gate }
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate /*
1097*0Sstevel@tonic-gate  * Function: dat_sr_put_token
1098*0Sstevel@tonic-gate  */
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate DAT_RETURN
dat_sr_put_token(DAT_OS_FILE * file,DAT_SR_TOKEN * token)1101*0Sstevel@tonic-gate dat_sr_put_token(
1102*0Sstevel@tonic-gate     DAT_OS_FILE *file,
1103*0Sstevel@tonic-gate 	DAT_SR_TOKEN *token) /*ARGSUSED*/
1104*0Sstevel@tonic-gate {
1105*0Sstevel@tonic-gate 	DAT_SR_STACK_NODE *top;
1106*0Sstevel@tonic-gate 
1107*0Sstevel@tonic-gate 	if (NULL == (top = dat_os_alloc(sizeof (DAT_SR_STACK_NODE)))) {
1108*0Sstevel@tonic-gate 		return (DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY);
1109*0Sstevel@tonic-gate 	}
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	top->token = *token;
1112*0Sstevel@tonic-gate 	top->next = g_token_stack;
1113*0Sstevel@tonic-gate 	g_token_stack = top;
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
1116*0Sstevel@tonic-gate }
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate /*
1120*0Sstevel@tonic-gate  * Function: dat_sr_read_token
1121*0Sstevel@tonic-gate  */
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate DAT_RETURN
dat_sr_read_token(DAT_OS_FILE * file,DAT_SR_TOKEN * token)1124*0Sstevel@tonic-gate dat_sr_read_token(
1125*0Sstevel@tonic-gate     DAT_OS_FILE 		*file,
1126*0Sstevel@tonic-gate     DAT_SR_TOKEN 		*token)
1127*0Sstevel@tonic-gate {
1128*0Sstevel@tonic-gate 	DAT_OS_FILE_POS 	pos;
1129*0Sstevel@tonic-gate 	DAT_OS_SIZE 		token_len;
1130*0Sstevel@tonic-gate 	DAT_COUNT 		num_escape_seq;
1131*0Sstevel@tonic-gate 	DAT_BOOLEAN 		is_quoted_str;
1132*0Sstevel@tonic-gate 	DAT_BOOLEAN 		is_prev_char_backslash;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 	/*
1135*0Sstevel@tonic-gate 	 * The DAT standard does not specify a maximum size for quoted strings.
1136*0Sstevel@tonic-gate 	 * Therefore the tokenizer must be able to read in a token of arbitrary
1137*0Sstevel@tonic-gate 	 * size. Instead of allocating a fixed length buffer, the tokenizer
1138*0Sstevel@tonic-gate 	 * first scans the input a single character at a time looking for the
1139*0Sstevel@tonic-gate 	 * begining and end of the token. Once the these positions are found,
1140*0Sstevel@tonic-gate 	 * the entire token is read into memory. By using this algorithm, the
1141*0Sstevel@tonic-gate 	 * implementation does not place an arbitrary maximum on the token size.
1142*0Sstevel@tonic-gate 	 */
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 	token_len = 0;
1145*0Sstevel@tonic-gate 	num_escape_seq = 0;
1146*0Sstevel@tonic-gate 	is_quoted_str = DAT_FALSE;
1147*0Sstevel@tonic-gate 	is_prev_char_backslash = DAT_FALSE;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	for (;;) {
1150*0Sstevel@tonic-gate 		DAT_OS_FILE_POS cur_pos;
1151*0Sstevel@tonic-gate 		int c;
1152*0Sstevel@tonic-gate 
1153*0Sstevel@tonic-gate 		/* if looking for start of the token */
1154*0Sstevel@tonic-gate 		if (0 == token_len) {
1155*0Sstevel@tonic-gate 			if (DAT_SUCCESS != dat_os_fgetpos(file, &cur_pos)) {
1156*0Sstevel@tonic-gate 				return (DAT_INTERNAL_ERROR);
1157*0Sstevel@tonic-gate 			}
1158*0Sstevel@tonic-gate 		}
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 		c = dat_os_fgetc(file);
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 		/* if looking for start of the token */
1163*0Sstevel@tonic-gate 		if (0 == token_len) {
1164*0Sstevel@tonic-gate 			if (EOF == c) {
1165*0Sstevel@tonic-gate 				token->type = DAT_SR_TOKEN_EOF;
1166*0Sstevel@tonic-gate 				token->value = NULL;
1167*0Sstevel@tonic-gate 				token->value_len = 0;
1168*0Sstevel@tonic-gate 				goto success;
1169*0Sstevel@tonic-gate 			} else if (DAT_SR_CHAR_NEWLINE == c) {
1170*0Sstevel@tonic-gate 				token->type = DAT_SR_TOKEN_EOR;
1171*0Sstevel@tonic-gate 				token->value = NULL;
1172*0Sstevel@tonic-gate 				token->value_len = 0;
1173*0Sstevel@tonic-gate 				goto success;
1174*0Sstevel@tonic-gate 			} else if (dat_os_isblank(c)) {
1175*0Sstevel@tonic-gate 				continue;
1176*0Sstevel@tonic-gate 			} else if (DAT_SR_CHAR_COMMENT == c) {
1177*0Sstevel@tonic-gate 				dat_sr_read_comment(file);
1178*0Sstevel@tonic-gate 				continue;
1179*0Sstevel@tonic-gate 			} else {
1180*0Sstevel@tonic-gate 				if (DAT_SR_CHAR_QUOTE == c) {
1181*0Sstevel@tonic-gate 					is_quoted_str = DAT_TRUE;
1182*0Sstevel@tonic-gate 				}
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 				pos = cur_pos;
1185*0Sstevel@tonic-gate 				token_len++;
1186*0Sstevel@tonic-gate 			}
1187*0Sstevel@tonic-gate 		} else { /* looking for the end of the token */
1188*0Sstevel@tonic-gate 			if (EOF == c) {
1189*0Sstevel@tonic-gate 				break;
1190*0Sstevel@tonic-gate 			} else if (DAT_SR_CHAR_NEWLINE == c) {
1191*0Sstevel@tonic-gate 				/* put back the newline */
1192*0Sstevel@tonic-gate 				(void) dat_os_fungetc(file);
1193*0Sstevel@tonic-gate 				break;
1194*0Sstevel@tonic-gate 			} else if (!is_quoted_str && dat_os_isblank(c)) {
1195*0Sstevel@tonic-gate 				break;
1196*0Sstevel@tonic-gate 			} else {
1197*0Sstevel@tonic-gate 				token_len++;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 				if ((DAT_SR_CHAR_QUOTE == c) &&
1200*0Sstevel@tonic-gate 				    !is_prev_char_backslash) {
1201*0Sstevel@tonic-gate 					break;
1202*0Sstevel@tonic-gate 				} else if ((DAT_SR_CHAR_BACKSLASH == c) &&
1203*0Sstevel@tonic-gate 				    !is_prev_char_backslash) {
1204*0Sstevel@tonic-gate 					is_prev_char_backslash = DAT_TRUE;
1205*0Sstevel@tonic-gate 					num_escape_seq++;
1206*0Sstevel@tonic-gate 				} else {
1207*0Sstevel@tonic-gate 					is_prev_char_backslash = DAT_FALSE;
1208*0Sstevel@tonic-gate 				}
1209*0Sstevel@tonic-gate 			}
1210*0Sstevel@tonic-gate 		}
1211*0Sstevel@tonic-gate 	}
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 	/* the token was a string */
1214*0Sstevel@tonic-gate 	if (DAT_SUCCESS != dat_os_fsetpos(file, &pos)) {
1215*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
1216*0Sstevel@tonic-gate 	}
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate 	if (is_quoted_str) {
1219*0Sstevel@tonic-gate 		if (DAT_SUCCESS != dat_sr_read_quoted_str(file,
1220*0Sstevel@tonic-gate 		    token,
1221*0Sstevel@tonic-gate 		    token_len,
1222*0Sstevel@tonic-gate 		    num_escape_seq)) {
1223*0Sstevel@tonic-gate 			return (DAT_INTERNAL_ERROR);
1224*0Sstevel@tonic-gate 		}
1225*0Sstevel@tonic-gate 	} else {
1226*0Sstevel@tonic-gate 		if (DAT_SUCCESS != dat_sr_read_str(file,
1227*0Sstevel@tonic-gate 		    token,
1228*0Sstevel@tonic-gate 		    token_len)) {
1229*0Sstevel@tonic-gate 			return (DAT_INTERNAL_ERROR);
1230*0Sstevel@tonic-gate 		}
1231*0Sstevel@tonic-gate 	}
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate success:
1234*0Sstevel@tonic-gate 	dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
1235*0Sstevel@tonic-gate 	    "\n"
1236*0Sstevel@tonic-gate 	    "DAT Registry: token\n"
1237*0Sstevel@tonic-gate 	    " type  %s\n"
1238*0Sstevel@tonic-gate 	    " value <%s>\n"
1239*0Sstevel@tonic-gate 	    "\n",
1240*0Sstevel@tonic-gate 	    dat_sr_type_to_str(token->type),
1241*0Sstevel@tonic-gate 	    ((DAT_SR_TOKEN_STRING == token->type) ? token->value : ""));
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
1244*0Sstevel@tonic-gate }
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 
1247*0Sstevel@tonic-gate /*
1248*0Sstevel@tonic-gate  * Function: dat_sr_read_str
1249*0Sstevel@tonic-gate  */
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate DAT_RETURN
dat_sr_read_str(DAT_OS_FILE * file,DAT_SR_TOKEN * token,DAT_OS_SIZE token_len)1252*0Sstevel@tonic-gate dat_sr_read_str(
1253*0Sstevel@tonic-gate     DAT_OS_FILE *file,
1254*0Sstevel@tonic-gate     DAT_SR_TOKEN *token,
1255*0Sstevel@tonic-gate     DAT_OS_SIZE token_len)
1256*0Sstevel@tonic-gate {
1257*0Sstevel@tonic-gate 	token->type = DAT_SR_TOKEN_STRING;
1258*0Sstevel@tonic-gate 	/* +1 for null termination */
1259*0Sstevel@tonic-gate 	token->value_len = sizeof (char) * (token_len + 1);
1260*0Sstevel@tonic-gate 	if (NULL == (token->value = dat_os_alloc(token->value_len))) {
1261*0Sstevel@tonic-gate 		return (DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY);
1262*0Sstevel@tonic-gate 	}
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 	if (token_len != dat_os_fread(file, token->value, token_len)) {
1265*0Sstevel@tonic-gate 		dat_os_free(token->value, token->value_len);
1266*0Sstevel@tonic-gate 		token->value = NULL;
1267*0Sstevel@tonic-gate 
1268*0Sstevel@tonic-gate 		return (DAT_INTERNAL_ERROR);
1269*0Sstevel@tonic-gate 	}
1270*0Sstevel@tonic-gate 
1271*0Sstevel@tonic-gate 	token->value[token->value_len - 1] = '\0';
1272*0Sstevel@tonic-gate 
1273*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
1274*0Sstevel@tonic-gate }
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate /*
1278*0Sstevel@tonic-gate  * Function: dat_sr_read_quoted_str
1279*0Sstevel@tonic-gate  */
1280*0Sstevel@tonic-gate 
1281*0Sstevel@tonic-gate DAT_RETURN
dat_sr_read_quoted_str(DAT_OS_FILE * file,DAT_SR_TOKEN * token,DAT_OS_SIZE token_len,DAT_COUNT num_escape_seq)1282*0Sstevel@tonic-gate dat_sr_read_quoted_str(
1283*0Sstevel@tonic-gate     DAT_OS_FILE 	*file,
1284*0Sstevel@tonic-gate     DAT_SR_TOKEN 	*token,
1285*0Sstevel@tonic-gate     DAT_OS_SIZE 	token_len,
1286*0Sstevel@tonic-gate     DAT_COUNT 		num_escape_seq)
1287*0Sstevel@tonic-gate {
1288*0Sstevel@tonic-gate 	DAT_OS_SIZE 	str_len;
1289*0Sstevel@tonic-gate 	DAT_OS_SIZE 	i;
1290*0Sstevel@tonic-gate 	DAT_OS_SIZE 	j;
1291*0Sstevel@tonic-gate 	int 		c;
1292*0Sstevel@tonic-gate 	DAT_RETURN 	status;
1293*0Sstevel@tonic-gate 	DAT_BOOLEAN 	is_prev_char_backslash;
1294*0Sstevel@tonic-gate 
1295*0Sstevel@tonic-gate 	str_len = token_len - 2; /* minus 2 " characters */
1296*0Sstevel@tonic-gate 	is_prev_char_backslash = DAT_FALSE;
1297*0Sstevel@tonic-gate 	status = DAT_SUCCESS;
1298*0Sstevel@tonic-gate 
1299*0Sstevel@tonic-gate 	token->type = DAT_SR_TOKEN_STRING;
1300*0Sstevel@tonic-gate 	/* +1 for null termination */
1301*0Sstevel@tonic-gate 	token->value_len = sizeof (char) * (str_len - num_escape_seq + 1);
1302*0Sstevel@tonic-gate 
1303*0Sstevel@tonic-gate 	if (NULL == (token->value = dat_os_alloc(token->value_len))) {
1304*0Sstevel@tonic-gate 		status = DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY;
1305*0Sstevel@tonic-gate 		goto exit;
1306*0Sstevel@tonic-gate 	}
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	/* throw away " */
1309*0Sstevel@tonic-gate 	if (DAT_SR_CHAR_QUOTE != dat_os_fgetc(file)) {
1310*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
1311*0Sstevel@tonic-gate 		goto exit;
1312*0Sstevel@tonic-gate 	}
1313*0Sstevel@tonic-gate 
1314*0Sstevel@tonic-gate 	for (i = 0, j = 0; i < str_len; i++) {
1315*0Sstevel@tonic-gate 		c = dat_os_fgetc(file);
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate 		if (EOF == c) {
1318*0Sstevel@tonic-gate 			status = DAT_INTERNAL_ERROR;
1319*0Sstevel@tonic-gate 			goto exit;
1320*0Sstevel@tonic-gate 		} else if ((DAT_SR_CHAR_BACKSLASH == c) &&
1321*0Sstevel@tonic-gate 		    !is_prev_char_backslash) {
1322*0Sstevel@tonic-gate 			is_prev_char_backslash = DAT_TRUE;
1323*0Sstevel@tonic-gate 		} else {
1324*0Sstevel@tonic-gate 			token->value[j] = c;
1325*0Sstevel@tonic-gate 			j++;
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate 			is_prev_char_backslash = DAT_FALSE;
1328*0Sstevel@tonic-gate 		}
1329*0Sstevel@tonic-gate 	}
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 	/* throw away " */
1332*0Sstevel@tonic-gate 	if (DAT_SR_CHAR_QUOTE != dat_os_fgetc(file)) {
1333*0Sstevel@tonic-gate 		status = DAT_INTERNAL_ERROR;
1334*0Sstevel@tonic-gate 		goto exit;
1335*0Sstevel@tonic-gate 	}
1336*0Sstevel@tonic-gate 
1337*0Sstevel@tonic-gate 	token->value[token->value_len - 1] = '\0';
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate exit:
1340*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
1341*0Sstevel@tonic-gate 		if (NULL != token->value) {
1342*0Sstevel@tonic-gate 			dat_os_free(token->value, token->value_len);
1343*0Sstevel@tonic-gate 			token->value = NULL;
1344*0Sstevel@tonic-gate 		}
1345*0Sstevel@tonic-gate 	}
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 	return (status);
1348*0Sstevel@tonic-gate }
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate /*
1352*0Sstevel@tonic-gate  * Function: dat_sr_read_comment
1353*0Sstevel@tonic-gate  */
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate void
dat_sr_read_comment(DAT_OS_FILE * file)1356*0Sstevel@tonic-gate dat_sr_read_comment(
1357*0Sstevel@tonic-gate     DAT_OS_FILE *file)
1358*0Sstevel@tonic-gate {
1359*0Sstevel@tonic-gate 	int c;
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 	/* read up to an EOR or EOF to move past the comment */
1362*0Sstevel@tonic-gate 	do {
1363*0Sstevel@tonic-gate 		c = dat_os_fgetc(file);
1364*0Sstevel@tonic-gate 	} while ((DAT_SR_CHAR_NEWLINE != c) && (EOF != c));
1365*0Sstevel@tonic-gate 
1366*0Sstevel@tonic-gate 	/* put back the newline */
1367*0Sstevel@tonic-gate 	(void) dat_os_fungetc(file);
1368*0Sstevel@tonic-gate }
1369