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