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