]> git.sur5r.net Git - openldap/blob - libraries/libldap/dsparse.c
Merge in all devel changes since 2.0-alpha2.
[openldap] / libraries / libldap / dsparse.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  * Copyright (c) 1993, 1994 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  *
17  * dsparse.c:  parsing routines used by display template and search 
18  * preference file library routines for LDAP clients.
19  *
20  * 7 March 1994 by Mark C Smith
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <ac/stdlib.h>
27
28 #include <ac/ctype.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #ifdef HAVE_SYS_FILE_H
33 #include <sys/file.h>
34 #endif
35
36 #include "ldap-int.h"
37
38 static int next_line LDAP_P(( char **bufp, ber_len_t *blenp, char **linep ));
39 static char *next_token LDAP_P(( char ** sp ));
40
41
42
43 int
44 next_line_tokens( char **bufp, ber_len_t *blenp, char ***toksp )
45 {
46     char        *p, *line, *token, **toks;
47     int         rc, tokcnt;
48
49     *toksp = NULL;
50
51     if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
52         return( rc );
53     }
54
55     if (( toks = (char **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
56         LBER_FREE( line );
57         return( -1 );
58     }
59     tokcnt = 0;
60
61     p = line;
62     while (( token = next_token( &p )) != NULL ) {
63         if (( toks = (char **)LDAP_REALLOC( toks, ( tokcnt + 2 ) *
64                 sizeof( char * ))) == NULL ) {
65             LBER_FREE( (char *)toks );
66             LBER_FREE( line );
67             return( -1 );
68         }
69         toks[ tokcnt ] = token;
70         toks[ ++tokcnt ] = NULL;
71     }
72
73     if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
74         tokcnt = 0;
75         free_strarray( toks );
76         toks = NULL;
77     }
78
79     LBER_FREE( line );
80
81     if ( tokcnt == 0 ) {
82         if ( toks != NULL ) {
83             LBER_FREE( (char *)toks );
84         }
85     } else {
86         *toksp = toks;
87     }
88
89     return( tokcnt );
90 }
91
92
93 static int
94 next_line( char **bufp, ber_len_t *blenp, char **linep )
95 {
96     char        *linestart, *line, *p;
97     ber_slen_t  plen;
98
99     linestart = *bufp;
100     p = *bufp;
101     plen = *blenp;
102
103     do {
104         for ( linestart = p; plen > 0; ++p, --plen ) {
105             if ( *p == '\r' ) {
106                 if ( plen > 1 && *(p+1) == '\n' ) {
107                     ++p;
108                     --plen;
109                 }
110                 break;
111             }
112
113             if ( *p == '\n' ) {
114                 if ( plen > 1 && *(p+1) == '\r' ) {
115                     ++p;
116                     --plen;
117                 }
118                 break;
119             }
120         }
121         ++p;
122         --plen;
123     } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
124
125
126     *bufp = p;
127     *blenp = plen;
128
129
130     if ( plen <= 0 ) {
131         *linep = NULL;
132         return( 0 );    /* end of file */
133     }
134
135     if (( line = LDAP_MALLOC( p - linestart )) == NULL ) {
136         *linep = NULL;
137         return( -1 );   /* fatal error */
138     }
139
140     (void) memcpy( line, linestart, p - linestart );
141     line[ p - linestart - 1 ] = '\0';
142     *linep = line;
143     return( strlen( line ));
144 }
145
146
147 static char *
148 next_token( char **sp )
149 {
150     int         in_quote = 0;
151     char        *p, *tokstart, *t;
152
153     if ( **sp == '\0' ) {
154         return( NULL );
155     }
156
157     p = *sp;
158
159     while ( isspace( (unsigned char) *p )) {    /* skip leading white space */
160         ++p;
161     }
162
163     if ( *p == '\0' ) {
164         return( NULL );
165     }
166
167     if ( *p == '\"' ) {
168         in_quote = 1;
169         ++p;
170     }
171     t = tokstart = p;
172
173     for ( ;; ) {
174         if ( *p == '\0' || ( isspace( (unsigned char) *p ) && !in_quote )) {
175             if ( *p != '\0' ) {
176                 ++p;
177             }
178             *t++ = '\0';                /* end of token */
179             break;
180         }
181
182         if ( *p == '\"' ) {
183             in_quote = !in_quote;
184             ++p;
185         } else {
186             *t++ = *p++;
187         }
188     }
189
190     *sp = p;
191
192     if ( t == tokstart ) {
193         return( NULL );
194     }
195
196     return( LDAP_STRDUP( tokstart ));
197 }
198
199
200 void
201 free_strarray( char **sap )
202 {
203     int         i;
204
205     if ( sap != NULL ) {
206         for ( i = 0; sap[ i ] != NULL; ++i ) {
207             LBER_FREE( sap[ i ] );
208         }
209         LBER_FREE( (char *)sap );
210     }
211 }