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