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