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