dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libldap5 / sources / ldap / common / dsparse.c
blob69aed4ae65d06b37a0c0b0d95a18e906288db61d
1 #pragma ident "%Z%%M% %I% %E% SMI"
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/
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.
15 * The Original Code is Mozilla Communicator client code, released
16 * March 31, 1998.
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.
23 * Contributor(s):
26 * Copyright (c) 1993, 1994 Regents of the University of Michigan.
27 * All rights reserved.
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.
37 * dsparse.c: parsing routines used by display template and search
38 * preference file library routines for LDAP clients.
42 #include "ldap-int.h"
44 static int next_line( char **bufp, long *blenp, char **linep );
45 static char *next_token( char ** sp );
47 int
48 ldap_next_line_tokens( char **bufp, long *blenp, char ***toksp )
50 char *p, *line, *token, **toks;
51 int rc, tokcnt;
53 *toksp = NULL;
55 if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
56 return( rc );
59 if (( toks = (char **)NSLDAPI_CALLOC( 1, sizeof( char * ))) == NULL ) {
60 NSLDAPI_FREE( line );
61 return( -1 );
63 tokcnt = 0;
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 );
73 toks[ tokcnt ] = token;
74 toks[ ++tokcnt ] = NULL;
77 if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
78 tokcnt = 0;
79 ldap_free_strarray( toks );
80 toks = NULL;
83 NSLDAPI_FREE( line );
85 if ( tokcnt == 0 ) {
86 if ( toks != NULL ) {
87 NSLDAPI_FREE( (char *)toks );
89 } else {
90 *toksp = toks;
93 return( tokcnt );
97 static int
98 next_line( char **bufp, long *blenp, char **linep )
100 char *linestart, *line, *p;
101 long plen;
103 linestart = *bufp;
104 p = *bufp;
105 plen = *blenp;
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;
114 break;
117 if ( *p == '\n' ) {
118 if ( plen > 1 && *(p+1) == '\r' ) {
119 ++p;
120 --plen;
122 break;
125 ++p;
126 --plen;
127 } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
130 *bufp = p;
131 *blenp = plen;
134 if ( plen <= 0 ) {
135 *linep = NULL;
136 return( 0 ); /* end of file */
139 if (( line = NSLDAPI_MALLOC( p - linestart )) == NULL ) {
140 *linep = NULL;
141 return( -1 ); /* fatal error */
144 SAFEMEMCPY( line, linestart, p - linestart );
145 line[ p - linestart - 1 ] = '\0';
146 *linep = line;
147 return( strlen( line ));
151 static char *
152 next_token( char **sp )
154 int in_quote = 0;
155 char *p, *tokstart, *t;
157 if ( **sp == '\0' ) {
158 return( NULL );
161 p = *sp;
163 while ( ldap_utf8isspace( p )) { /* skip leading white space */
164 ++p;
167 if ( *p == '\0' ) {
168 return( NULL );
171 if ( *p == '\"' ) {
172 in_quote = 1;
173 ++p;
175 t = tokstart = p;
177 for ( ;; ) {
178 if ( *p == '\0' || ( ldap_utf8isspace( p ) && !in_quote )) {
179 if ( *p != '\0' ) {
180 ++p;
182 *t++ = '\0'; /* end of token */
183 break;
186 if ( *p == '\"' ) {
187 in_quote = !in_quote;
188 ++p;
189 } else {
190 *t++ = *p++;
194 *sp = p;
196 if ( t == tokstart ) {
197 return( NULL );
200 return( nsldapi_strdup( tokstart ));
204 void
205 ldap_free_strarray( char **sap )
207 int i;
209 if ( sap != NULL ) {
210 for ( i = 0; sap[ i ] != NULL; ++i ) {
211 NSLDAPI_FREE( sap[ i ] );
213 NSLDAPI_FREE( (char *)sap );