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