]> git.sur5r.net Git - openldap/blob - libraries/libldap/dsparse.c
More files that didn't get merged properly.
[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 "lber.h"
32 #include "ldap.h"
33
34 int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
35 void free_strarray LDAP_P(( char **sap ));
36 static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
37 static char *next_token LDAP_P(( char ** sp ));
38
39
40
41 int
42 next_line_tokens( char **bufp, long *blenp, char ***toksp )
43 {
44     char        *p, *line, *token, **toks;
45     int         rc, tokcnt;
46
47     *toksp = NULL;
48
49     if (( rc = next_line( bufp, blenp, &line )) <= 0 ) {
50         return( rc );
51     }
52
53     if (( toks = (char **)calloc( 1, sizeof( char * ))) == NULL ) {
54         free( line );
55         return( -1 );
56     }
57     tokcnt = 0;
58
59     p = line;
60     while (( token = next_token( &p )) != NULL ) {
61         if (( toks = (char **)realloc( toks, ( tokcnt + 2 ) *
62                 sizeof( char * ))) == NULL ) {
63             free( (char *)toks );
64             free( line );
65             return( -1 );
66         }
67         toks[ tokcnt ] = token;
68         toks[ ++tokcnt ] = NULL;
69     }
70
71     if ( tokcnt == 1 && strcasecmp( toks[ 0 ], "END" ) == 0 ) {
72         tokcnt = 0;
73         free_strarray( toks );
74         toks = NULL;
75     }
76
77     free( line );
78
79     if ( tokcnt == 0 ) {
80         if ( toks != NULL ) {
81             free( (char *)toks );
82         }
83     } else {
84         *toksp = toks;
85     }
86
87     return( tokcnt );
88 }
89
90
91 static int
92 next_line( char **bufp, long *blenp, char **linep )
93 {
94     char        *linestart, *line, *p;
95     long        plen;
96
97     linestart = *bufp;
98     p = *bufp;
99     plen = *blenp;
100
101     do {
102         for ( linestart = p; plen > 0; ++p, --plen ) {
103             if ( *p == '\r' ) {
104                 if ( plen > 1 && *(p+1) == '\n' ) {
105                     ++p;
106                     --plen;
107                 }
108                 break;
109             }
110
111             if ( *p == '\n' ) {
112                 if ( plen > 1 && *(p+1) == '\r' ) {
113                     ++p;
114                     --plen;
115                 }
116                 break;
117             }
118         }
119         ++p;
120         --plen;
121     } while ( plen > 0 && ( *linestart == '#' || linestart + 1 == p ));
122
123
124     *bufp = p;
125     *blenp = plen;
126
127
128     if ( plen <= 0 ) {
129         *linep = NULL;
130         return( 0 );    /* end of file */
131     }
132
133     if (( line = malloc( p - linestart )) == NULL ) {
134         *linep = NULL;
135         return( -1 );   /* fatal error */
136     }
137
138     (void) memcpy( line, linestart, p - linestart );
139     line[ p - linestart - 1 ] = '\0';
140     *linep = line;
141     return( strlen( line ));
142 }
143
144
145 static char *
146 next_token( char **sp )
147 {
148     int         in_quote = 0;
149     char        *p, *tokstart, *t;
150
151     if ( **sp == '\0' ) {
152         return( NULL );
153     }
154
155     p = *sp;
156
157     while ( isspace( *p )) {            /* skip leading white space */
158         ++p;
159     }
160
161     if ( *p == '\0' ) {
162         return( NULL );
163     }
164
165     if ( *p == '\"' ) {
166         in_quote = 1;
167         ++p;
168     }
169     t = tokstart = p;
170
171     for ( ;; ) {
172         if ( *p == '\0' || ( isspace( *p ) && !in_quote )) {
173             if ( *p != '\0' ) {
174                 ++p;
175             }
176             *t++ = '\0';                /* end of token */
177             break;
178         }
179
180         if ( *p == '\"' ) {
181             in_quote = !in_quote;
182             ++p;
183         } else {
184             *t++ = *p++;
185         }
186     }
187
188     *sp = p;
189
190     if ( t == tokstart ) {
191         return( NULL );
192     }
193
194     return( strdup( tokstart ));
195 }
196
197
198 void
199 free_strarray( char **sap )
200 {
201     int         i;
202
203     if ( sap != NULL ) {
204         for ( i = 0; sap[ i ] != NULL; ++i ) {
205             free( sap[ i ] );
206         }
207         free( (char *)sap );
208     }
209 }