xref: /onnv-gate/usr/src/lib/libldap5/sources/ldap/common/dsparse.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*0Sstevel@tonic-gate 
3*0Sstevel@tonic-gate 
4*0Sstevel@tonic-gate /*
5*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
6*0Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
7*0Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
8*0Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
9*0Sstevel@tonic-gate  *
10*0Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
11*0Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
12*0Sstevel@tonic-gate  * implied. See the License for the specific language governing
13*0Sstevel@tonic-gate  * rights and limitations under the License.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
16*0Sstevel@tonic-gate  * March 31, 1998.
17*0Sstevel@tonic-gate  *
18*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
19*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
20*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
21*0Sstevel@tonic-gate  * Rights Reserved.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Contributor(s):
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
27*0Sstevel@tonic-gate  * All rights reserved.
28*0Sstevel@tonic-gate  *
29*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
30*0Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
31*0Sstevel@tonic-gate  * to the University of Michigan at Ann Arbor. The name of the University
32*0Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
33*0Sstevel@tonic-gate  * software without specific prior written permission. This software
34*0Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * dsparse.c:  parsing routines used by display template and search
38*0Sstevel@tonic-gate  * preference file library routines for LDAP clients.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "ldap-int.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static int next_line( char **bufp, long *blenp, char **linep );
45*0Sstevel@tonic-gate static char *next_token( char ** sp );
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate int
ldap_next_line_tokens(char ** bufp,long * blenp,char *** toksp)48*0Sstevel@tonic-gate ldap_next_line_tokens( char **bufp, long *blenp, char ***toksp )
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate     char	*p, *line, *token, **toks;
51*0Sstevel@tonic-gate     int		rc, tokcnt;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate     *toksp = NULL;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate     if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
56*0Sstevel@tonic-gate 	return( rc );
57*0Sstevel@tonic-gate     }
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate     if (( toks = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
60*0Sstevel@tonic-gate 	NSLDAPI_FREE( line );
61*0Sstevel@tonic-gate 	return( -1 );
62*0Sstevel@tonic-gate     }
63*0Sstevel@tonic-gate     tokcnt = 0;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate     p = line;
66*0Sstevel@tonic-gate     while (( token = next_token( &p )) != NULL ) {
67*0Sstevel@tonic-gate 	if (( toks = (char **)NSLDAPI_REALLOC( toks, ( tokcnt + 2 ) *
68*0Sstevel@tonic-gate 		sizeof( char * ))) == NULL ) {
69*0Sstevel@tonic-gate 	    NSLDAPI_FREE( (char *)toks );
70*0Sstevel@tonic-gate 	    NSLDAPI_FREE( line );
71*0Sstevel@tonic-gate 	    return( -1 );
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 	toks[ tokcnt ] = token;
74*0Sstevel@tonic-gate 	toks[ ++tokcnt ] = NULL;
75*0Sstevel@tonic-gate     }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate     if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
78*0Sstevel@tonic-gate 	tokcnt = 0;
79*0Sstevel@tonic-gate 	ldap_free_strarray( toks );
80*0Sstevel@tonic-gate 	toks = NULL;
81*0Sstevel@tonic-gate     }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate     NSLDAPI_FREE( line );
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate     if ( tokcnt == 0 ) {
86*0Sstevel@tonic-gate 	if ( toks != NULL ) {
87*0Sstevel@tonic-gate 	    NSLDAPI_FREE( (char *)toks );
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate     } else {
90*0Sstevel@tonic-gate 	*toksp = toks;
91*0Sstevel@tonic-gate     }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate     return( tokcnt );
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static int
next_line(char ** bufp,long * blenp,char ** linep)98*0Sstevel@tonic-gate next_line( char **bufp, long *blenp, char **linep )
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate     char	*linestart, *line, *p;
101*0Sstevel@tonic-gate     long	plen;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate     linestart = *bufp;
104*0Sstevel@tonic-gate     p = *bufp;
105*0Sstevel@tonic-gate     plen = *blenp;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate     do {
108*0Sstevel@tonic-gate 	for ( linestart = p; plen > 0; ++p, --plen ) {
109*0Sstevel@tonic-gate 	    if ( *p == '\r' ) {
110*0Sstevel@tonic-gate 		if ( plen > 1 && *(p+1) == '\n' ) {
111*0Sstevel@tonic-gate 		    ++p;
112*0Sstevel@tonic-gate 		    --plen;
113*0Sstevel@tonic-gate 		}
114*0Sstevel@tonic-gate 		break;
115*0Sstevel@tonic-gate 	    }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	    if ( *p == '\n' ) {
118*0Sstevel@tonic-gate 		if ( plen > 1 && *(p+1) == '\r' ) {
119*0Sstevel@tonic-gate 		    ++p;
120*0Sstevel@tonic-gate 		    --plen;
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 		break;
123*0Sstevel@tonic-gate 	    }
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 	++p;
126*0Sstevel@tonic-gate 	--plen;
127*0Sstevel@tonic-gate     } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate     *bufp = p;
131*0Sstevel@tonic-gate     *blenp = plen;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate     if ( plen <= 0 ) {
135*0Sstevel@tonic-gate 	*linep = NULL;
136*0Sstevel@tonic-gate 	return( 0 );	/* end of file */
137*0Sstevel@tonic-gate     }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate     if (( line = NSLDAPI_MALLOC( p - linestart )) == NULL ) {
140*0Sstevel@tonic-gate 	*linep = NULL;
141*0Sstevel@tonic-gate 	return( -1 );	/* fatal error */
142*0Sstevel@tonic-gate     }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate     SAFEMEMCPY( line, linestart, p - linestart );
145*0Sstevel@tonic-gate     line[ p - linestart - 1 ] = '\0';
146*0Sstevel@tonic-gate     *linep = line;
147*0Sstevel@tonic-gate     return( strlen( line ));
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static char *
next_token(char ** sp)152*0Sstevel@tonic-gate next_token( char **sp )
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate     int		in_quote = 0;
155*0Sstevel@tonic-gate     char	*p, *tokstart, *t;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate     if ( **sp == '\0' ) {
158*0Sstevel@tonic-gate 	return( NULL );
159*0Sstevel@tonic-gate     }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate     p = *sp;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate     while ( ldap_utf8isspace( p )) {		/* skip leading white space */
164*0Sstevel@tonic-gate 	++p;
165*0Sstevel@tonic-gate     }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate     if ( *p == '\0' ) {
168*0Sstevel@tonic-gate 	return( NULL );
169*0Sstevel@tonic-gate     }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate     if ( *p == '\"' ) {
172*0Sstevel@tonic-gate 	in_quote = 1;
173*0Sstevel@tonic-gate 	++p;
174*0Sstevel@tonic-gate     }
175*0Sstevel@tonic-gate     t = tokstart = p;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate     for ( ;; ) {
178*0Sstevel@tonic-gate 	if ( *p == '\0' || ( ldap_utf8isspace( p ) && !in_quote )) {
179*0Sstevel@tonic-gate 	    if ( *p != '\0' ) {
180*0Sstevel@tonic-gate 		++p;
181*0Sstevel@tonic-gate 	    }
182*0Sstevel@tonic-gate 	    *t++ = '\0';		/* end of token */
183*0Sstevel@tonic-gate 	    break;
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if ( *p == '\"' ) {
187*0Sstevel@tonic-gate 	    in_quote = !in_quote;
188*0Sstevel@tonic-gate 	    ++p;
189*0Sstevel@tonic-gate 	} else {
190*0Sstevel@tonic-gate 	    *t++ = *p++;
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate     }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate     *sp = p;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate     if ( t == tokstart ) {
197*0Sstevel@tonic-gate 	return( NULL );
198*0Sstevel@tonic-gate     }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate     return( nsldapi_strdup( tokstart ));
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate void
ldap_free_strarray(char ** sap)205*0Sstevel@tonic-gate ldap_free_strarray( char **sap )
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate     int		i;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate     if ( sap != NULL ) {
210*0Sstevel@tonic-gate 	for ( i = 0; sap[ i ] != NULL; ++i ) {
211*0Sstevel@tonic-gate 	    NSLDAPI_FREE( sap[ i ] );
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	NSLDAPI_FREE( (char *)sap );
214*0Sstevel@tonic-gate     }
215*0Sstevel@tonic-gate }
216