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