1 /* str2filter.c - parse an rfc 1588 string filter */
12 static char *find_matching_paren();
13 static Filter *str2list();
14 static Filter *str2simple();
15 static int str2subvals();
18 str2filter( char *str )
23 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
25 if ( str == NULL || *str == '\0' ) {
31 if ( (end = find_matching_paren( str )) == NULL ) {
40 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
44 f = str2list( str, LDAP_FILTER_AND );
48 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
52 f = str2list( str, LDAP_FILTER_OR );
56 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
60 f = str2list( str, LDAP_FILTER_NOT );
64 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
67 f = str2simple( str );
73 default: /* assume it's a simple type=value filter */
74 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
77 f = str2simple( str );
85 * Put a list of filters like this "(filter1)(filter2)..."
89 str2list( char *str, unsigned long ftype )
96 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
98 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
103 while ( *str && isspace( *str ) )
108 if ( (next = find_matching_paren( str )) == NULL ) {
115 /* now we have "(filter)" with str pointing to it */
116 if ( (*fp = str2filter( str )) == NULL ) {
132 str2simple( char *str )
136 char *value, savechar;
138 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
140 if ( (s = strchr( str, '=' )) == NULL ) {
147 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
151 f->f_choice = LDAP_FILTER_LE;
155 f->f_choice = LDAP_FILTER_GE;
159 f->f_choice = LDAP_FILTER_APPROX;
163 if ( strchr( value, '*' ) == NULL ) {
164 f->f_choice = LDAP_FILTER_EQUALITY;
165 } else if ( strcmp( value, "*" ) == 0 ) {
166 f->f_choice = LDAP_FILTER_PRESENT;
168 f->f_choice = LDAP_FILTER_SUBSTRINGS;
169 f->f_sub_type = strdup( str );
170 if ( str2subvals( value, f ) != 0 ) {
181 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
182 f->f_type = strdup( str );
184 f->f_avtype = strdup( str );
185 f->f_avvalue.bv_val = strdup( value );
186 f->f_avvalue.bv_len = strlen( value );
195 str2subvals( char *val, Filter *f )
200 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
203 while ( val != NULL && *val ) {
204 if ( (nextstar = strchr( val, '*' )) != NULL )
207 if ( gotstar == 0 ) {
208 f->f_sub_initial = strdup( val );
209 } else if ( nextstar == NULL ) {
210 f->f_sub_final = strdup( val );
212 charray_add( &f->f_sub_any, strdup( val ) );
216 if ( nextstar != NULL )
225 * find_matching_paren - return a pointer to the right paren in s matching
226 * the left paren to which *s currently points
230 find_matching_paren( char *s )
240 else if ( *s == ')' )
243 if ( balance == 0 ) {
246 if ( *s == '\\' && ! escape )