1 /* str2filter.c - parse an rfc 1588 string filter */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
13 #include <ac/socket.h>
17 static char *find_matching_paren(char *s);
18 static Filter *str2list(char *str, long unsigned int ftype);
19 static Filter *str2simple(char *str);
20 static int str2subvals(char *val, Filter *f);
23 str2filter( char *str )
28 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
30 if ( str == NULL || *str == '\0' ) {
36 if ( (end = find_matching_paren( str )) == NULL ) {
45 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
49 f = str2list( str, LDAP_FILTER_AND );
53 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
57 f = str2list( str, LDAP_FILTER_OR );
61 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
65 f = str2list( str, LDAP_FILTER_NOT );
69 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
72 f = str2simple( str );
78 default: /* assume it's a simple type=value filter */
79 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
82 f = str2simple( str );
90 * Put a list of filters like this "(filter1)(filter2)..."
94 str2list( char *str, unsigned long ftype )
101 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
103 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
108 while ( *str && isspace( (unsigned char) *str ) )
113 if ( (next = find_matching_paren( str )) == NULL ) {
120 /* now we have "(filter)" with str pointing to it */
121 if ( (*fp = str2filter( str )) == NULL ) {
137 str2simple( char *str )
141 char *value, savechar;
143 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
145 if ( (s = strchr( str, '=' )) == NULL ) {
152 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
156 f->f_choice = LDAP_FILTER_LE;
160 f->f_choice = LDAP_FILTER_GE;
164 f->f_choice = LDAP_FILTER_APPROX;
168 if ( strchr( value, '*' ) == NULL ) {
169 f->f_choice = LDAP_FILTER_EQUALITY;
170 } else if ( strcmp( value, "*" ) == 0 ) {
171 f->f_choice = LDAP_FILTER_PRESENT;
173 f->f_choice = LDAP_FILTER_SUBSTRINGS;
174 f->f_sub_type = ch_strdup( str );
175 if ( str2subvals( value, f ) != 0 ) {
186 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
187 f->f_type = ch_strdup( str );
189 f->f_avtype = ch_strdup( str );
190 f->f_avvalue.bv_val = ch_strdup( value );
191 f->f_avvalue.bv_len = strlen( value );
200 str2subvals( char *val, Filter *f )
205 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
208 while ( val != NULL && *val ) {
209 if ( (nextstar = strchr( val, '*' )) != NULL )
212 if ( gotstar == 0 ) {
213 f->f_sub_initial = ch_strdup( val );
214 } else if ( nextstar == NULL ) {
215 f->f_sub_final = ch_strdup( val );
217 charray_add( &f->f_sub_any, ch_strdup( val ) );
221 if ( nextstar != NULL )
230 * find_matching_paren - return a pointer to the right paren in s matching
231 * the left paren to which *s currently points
235 find_matching_paren( char *s )
245 else if ( *s == ')' )
248 if ( balance == 0 ) {
251 if ( *s == '\\' && ! escape )