]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
Remove old build system.
[openldap] / servers / slapd / tools / ldif2index.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include "../slap.h"
6
7 #include "ldapconfig.h"
8
9 #define MAXARGS                 100
10
11 extern void     attr_index_config();
12 extern char     *str_getline();
13 extern char     *attr_normalize();
14 extern int      nbackends;
15 extern Backend  *backends;
16 extern int      ldap_debug;
17
18 int             ldap_debug;
19 int             ldap_syslog;
20 int             ldap_syslog_level;
21 int             global_schemacheck;
22 int             num_entries_sent;
23 int             num_bytes_sent;
24 int             active_threads;
25 char            *default_referral;
26 struct objclass *global_oc;
27 time_t          currenttime;
28 pthread_t       listener_tid;
29 pthread_mutex_t num_sent_mutex;
30 pthread_mutex_t entry2str_mutex;
31 pthread_mutex_t active_threads_mutex;
32 pthread_mutex_t new_conn_mutex;
33 pthread_mutex_t currenttime_mutex;
34 pthread_mutex_t replog_mutex;
35 pthread_mutex_t ops_mutex;
36 pthread_mutex_t regex_mutex;
37
38 static void
39 usage( char *name )
40 {
41         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
42         exit( 1 );
43 }
44
45 main( int argc, char **argv )
46 {
47         int             i, cargc, indb, stop;
48         char            *cargv[MAXARGS];
49         char            *defargv[MAXARGS];
50         char            *tailorfile, *inputfile;
51         char            *linep, *buf, *attr;
52         char            line[BUFSIZ];
53         int             lineno, elineno;
54         int             lmax, lcur, indexmask, syntaxmask;
55         int             dbnum;
56         unsigned long   id;
57         Backend         *be = NULL;
58         struct berval   bv;
59         struct berval   *vals[2];
60         extern char     *optarg;
61
62         inputfile = NULL;
63         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
64         dbnum = -1;
65         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
66                 switch ( i ) {
67                 case 'd':       /* turn on debugging */
68                         ldap_debug = atoi( optarg );
69                         break;
70
71                 case 'f':       /* specify a tailor file */
72                         tailorfile = strdup( optarg );
73                         break;
74
75                 case 'i':       /* input file */
76                         inputfile = strdup( optarg );
77                         break;
78
79                 case 'n':       /* which config file db to index */
80                         dbnum = atoi( optarg ) - 1;
81                         break;
82
83                 default:
84                         usage( argv[0] );
85                         break;
86                 }
87         }
88         attr = attr_normalize( argv[argc - 1] );
89         if ( inputfile == NULL ) {
90                 usage( argv[0] );
91         } else {
92                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
93                         perror( inputfile );
94                         exit( 1 );
95                 }
96         }
97
98         init();
99         read_config( tailorfile, &be, NULL );
100
101         if ( dbnum == -1 ) {
102                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
103                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
104                             == 0 ) {
105                                 break;
106                         }
107                 }
108                 if ( dbnum == nbackends ) {
109                         fprintf( stderr, "No ldbm database found in config file\n" );
110                         exit( 1 );
111                 }
112         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
113                 fprintf( stderr, "Database number selected via -n is out of range\n" );
114                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
115                 exit( 1 );
116         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
117                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
118                 exit( 1 );
119         }
120         be = &backends[dbnum];
121
122         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
123         if ( indexmask == 0 ) {
124                 exit( 0 );
125         }
126
127         id = 0;
128         stop = 0;
129         lineno = 0;
130         buf = NULL;
131         lcur = lmax = 0;
132         vals[0] = &bv;
133         vals[1] = NULL;
134         while ( ! stop ) {
135                 char            *type, *val, *s;
136                 int             vlen;
137
138                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
139                         int     len;
140
141                         lineno++;
142                         len = strlen( line );
143                         while ( lcur + len + 1 > lmax ) {
144                                 lmax += BUFSIZ;
145                                 buf = (char *) ch_realloc( buf, lmax );
146                         }
147                         strcpy( buf + lcur, line );
148                         lcur += len;
149                 } else {
150                         stop = 1;
151                 }
152                 if ( line[0] == '\n' || stop && buf && *buf ) {
153                         if ( *buf != '\n' ) {
154                                 id++;
155                                 s = buf;
156                                 elineno = 0;
157                                 while ( (linep = str_getline( &s )) != NULL ) {
158                                         elineno++;
159                                         if ( str_parse_line( linep, &type, &val,
160                                             &vlen ) != 0 ) {
161                                                 Debug( LDAP_DEBUG_PARSE,
162                             "bad line %d in entry ending at line %d ignored\n",
163                                                     elineno, elineno, 0 );
164                                                 continue;
165                                         }
166
167                                         if ( strcasecmp( type, attr ) == 0 ) {
168                                                 bv.bv_val = val;
169                                                 bv.bv_len = vlen;
170                                                 index_add_values( be, attr,
171                                                     vals, id );
172                                         }
173                                 }
174                         }
175                         *buf = '\0';
176                         lcur = 0;
177                 }
178         }
179         (*be->be_close)( be );
180
181         exit( 0 );
182 }