]> git.sur5r.net Git - openldap/blob - libraries/libldap/dsparse.c
Add ftest (filter test) to the mix, needs work.
[openldap] / libraries / libldap / dsparse.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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/string.h>
29 #include <ac/time.h>
30
31 #ifdef HAVE_SYS_FILE_H
32 #include <sys/file.h>
33 #endif
34
35 #include "ldap-int.h"
36
37 static int next_line LDAP_P(( char **bufp, ber_len_t *blenp, char **linep ));
38 static char *next_token LDAP_P(( char ** sp ));
39
40
41 int
42 ldap_int_next_line_tokens( char **bufp, ber_len_t *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 **)LDAP_CALLOC( 1, sizeof( char * ))) == NULL ) {
54         LBER_FREE( line );
55         return( -1 );
56     }
57     tokcnt = 0;
58
59     p = line;
60     while (( token = next_token( &p )) != NULL ) {
61         if (( toks = (char **)LDAP_REALLOC( toks, ( tokcnt + 2 ) *
62                 sizeof( char * ))) == NULL ) {
63             LBER_FREE( (char *)toks );
64             LBER_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         LDAP_VFREE( toks );
74         toks = NULL;
75     }
76
77     LBER_FREE( line );
78
79     if ( tokcnt == 0 ) {
80         if ( toks != NULL ) {
81             LBER_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, ber_len_t *blenp, char **linep )
93 {
94     char        *linestart, *line, *p;
95     ber_slen_t  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 = LDAP_MALLOC( p - linestart )) == NULL ) {
134         *linep = NULL;
135         return( -1 );   /* fatal error */
136     }
137
138     AC_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 ( LDAP_SPACE( (unsigned char) *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' || ( LDAP_SPACE( (unsigned char) *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( LDAP_STRDUP( tokstart ));
195 }