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