]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/attr.c
merged with autoconf branch
[openldap] / servers / slapd / back-ldbm / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12
13 extern char     **str2charray();
14
15 static int
16 ainfo_type_cmp(
17     char                *type,
18     struct attrinfo     *a
19 )
20 {
21         return( strcasecmp( type, a->ai_type ) );
22 }
23
24 static int
25 ainfo_cmp(
26     struct attrinfo     *a,
27     struct attrinfo     *b
28 )
29 {
30         return( strcasecmp( a->ai_type, b->ai_type ) );
31 }
32
33 /*
34  * Called when a duplicate "index" line is encountered.
35  *
36  * returns 1 => original from init code, indexmask updated
37  *         2 => original not from init code, warn the user
38  */
39
40 static int
41 ainfo_dup(
42     struct attrinfo     *a,
43     struct attrinfo     *b
44 )
45 {
46         /*
47          * if the duplicate definition is because we initialized the attr,
48          * just add what came from the config file. otherwise, complain.
49          */
50         if ( a->ai_indexmask & INDEX_FROMINIT ) {
51                 a->ai_indexmask |= b->ai_indexmask;
52
53                 return( 1 );
54         }
55
56         return( 2 );
57 }
58
59 void
60 attr_masks(
61     struct ldbminfo     *li,
62     char                *type,
63     int                 *indexmask,
64     int                 *syntaxmask
65 )
66 {
67         struct attrinfo *a;
68
69         *indexmask = 0;
70         *syntaxmask = 0;
71         if ( (a = (struct attrinfo *) avl_find( li->li_attrs, type,
72             ainfo_type_cmp )) == NULL ) {
73                 if ( (a = (struct attrinfo *) avl_find( li->li_attrs, "default",
74                     ainfo_type_cmp )) == NULL ) {
75                         return;
76                 }
77         }
78         *indexmask = a->ai_indexmask;
79         if ( strcasecmp( a->ai_type, "default" ) == 0 ) {
80                 *syntaxmask = attr_syntax( type );
81         } else {
82                 *syntaxmask = a->ai_syntaxmask;
83         }
84 }
85
86 void
87 attr_index_config(
88     struct ldbminfo     *li,
89     char                *fname,
90     int                 lineno,
91     int                 argc,
92     char                **argv,
93     int                 init
94 )
95 {
96         int             i, j;
97         char            **attrs, **indexes;
98         struct attrinfo *a;
99
100         attrs = str2charray( argv[0], "," );
101         if ( argc > 1 ) {
102                 indexes = str2charray( argv[1], "," );
103         }
104         for ( i = 0; attrs[i] != NULL; i++ ) {
105                 a = (struct attrinfo *) ch_malloc( sizeof(struct attrinfo) );
106                 a->ai_type = strdup( attrs[i] );
107                 a->ai_syntaxmask = attr_syntax( a->ai_type );
108                 if ( argc == 1 ) {
109                         a->ai_indexmask = (INDEX_PRESENCE | INDEX_EQUALITY |
110                             INDEX_APPROX | INDEX_SUB);
111                 } else {
112                         a->ai_indexmask = 0;
113                         for ( j = 0; indexes[j] != NULL; j++ ) {
114                                 if ( strncasecmp( indexes[j], "pres", 4 )
115                                     == 0 ) {
116                                         a->ai_indexmask |= INDEX_PRESENCE;
117                                 } else if ( strncasecmp( indexes[j], "eq", 2 )
118                                     == 0 ) {
119                                         a->ai_indexmask |= INDEX_EQUALITY;
120                                 } else if ( strncasecmp( indexes[j], "approx",
121                                     6 ) == 0 ) {
122                                         a->ai_indexmask |= INDEX_APPROX;
123                                 } else if ( strncasecmp( indexes[j], "sub", 3 )
124                                     == 0 ) {
125                                         a->ai_indexmask |= INDEX_SUB;
126                                 } else if ( strncasecmp( indexes[j], "none", 4 )
127                                     == 0 ) {
128                                         if ( a->ai_indexmask != 0 ) {
129                                                 fprintf( stderr,
130 "%s: line %d: index type \"none\" cannot be combined with other types\n",
131                                                     fname, lineno );
132                                         }
133                                         a->ai_indexmask = 0;
134                                 } else {
135                                         fprintf( stderr,
136                         "%s: line %d: unknown index type \"%s\" (ignored)\n",
137                                             fname, lineno, indexes[j] );
138                                         fprintf( stderr,
139         "valid index types are \"pres\", \"eq\", \"approx\", or \"sub\"\n" );
140                                 }
141                         }
142                 }
143                 if ( init ) {
144                         a->ai_indexmask |= INDEX_FROMINIT;
145                 }
146
147                 switch (avl_insert( &li->li_attrs, (caddr_t) a, ainfo_cmp, ainfo_dup )) {
148                 case 1:         /* duplicate - updating init version */
149                         free( a->ai_type );
150                         free( (char *) a );
151                         break;
152
153                 case 2:         /* user duplicate - ignore and warn */
154                         fprintf( stderr,
155     "%s: line %d: duplicate index definition for attr \"%s\" (ignored)\n",
156                             fname, lineno, a->ai_type );
157                         free( a->ai_type );
158                         free( (char *) a );
159                         break;
160
161                 default:;       /* inserted ok */
162                         /* FALL */
163                 }
164         }
165         charray_free( attrs );
166         if ( argc > 1 )
167                 charray_free( indexes );
168 }