1 /* str2filter.c - parse an rfc 1588 string filter */
8 #include <sys/socket.h>
11 static char *find_matching_paren();
12 static Filter *str2list();
13 static Filter *str2simple();
14 static int str2subvals();
17 str2filter( char *str )
22 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
24 if ( str == NULL || *str == '\0' ) {
30 if ( (end = find_matching_paren( str )) == NULL ) {
39 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
43 f = str2list( str, LDAP_FILTER_AND );
47 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
51 f = str2list( str, LDAP_FILTER_OR );
55 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
59 f = str2list( str, LDAP_FILTER_NOT );
63 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
66 f = str2simple( str );
72 default: /* assume it's a simple type=value filter */
73 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
76 f = str2simple( str );
84 * Put a list of filters like this "(filter1)(filter2)..."
88 str2list( char *str, unsigned long ftype )
95 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
97 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
102 while ( *str && isspace( *str ) )
107 if ( (next = find_matching_paren( str )) == NULL ) {
114 /* now we have "(filter)" with str pointing to it */
115 if ( (*fp = str2filter( str )) == NULL ) {
131 str2simple( char *str )
135 char *value, savechar;
137 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
139 if ( (s = strchr( str, '=' )) == NULL ) {
146 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
150 f->f_choice = LDAP_FILTER_LE;
154 f->f_choice = LDAP_FILTER_GE;
158 f->f_choice = LDAP_FILTER_APPROX;
162 if ( strchr( value, '*' ) == NULL ) {
163 f->f_choice = LDAP_FILTER_EQUALITY;
164 } else if ( strcmp( value, "*" ) == 0 ) {
165 f->f_choice = LDAP_FILTER_PRESENT;
167 f->f_choice = LDAP_FILTER_SUBSTRINGS;
168 f->f_sub_type = strdup( str );
169 if ( str2subvals( value, f ) != 0 ) {
180 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
181 f->f_type = strdup( str );
183 f->f_avtype = strdup( str );
184 f->f_avvalue.bv_val = strdup( value );
185 f->f_avvalue.bv_len = strlen( value );
194 str2subvals( char *val, Filter *f )
199 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
202 while ( val != NULL && *val ) {
203 if ( (nextstar = strchr( val, '*' )) != NULL )
206 if ( gotstar == 0 ) {
207 f->f_sub_initial = strdup( val );
208 } else if ( nextstar == NULL ) {
209 f->f_sub_final = strdup( val );
211 charray_add( &f->f_sub_any, strdup( val ) );
215 if ( nextstar != NULL )
224 * find_matching_paren - return a pointer to the right paren in s matching
225 * the left paren to which *s currently points
229 find_matching_paren( char *s )
239 else if ( *s == ')' )
242 if ( balance == 0 ) {
245 if ( *s == '\\' && ! escape )