3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* slapcommon.c - common routine for the slap tools */
12 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/unistd.h>
18 #include "slapcommon.h"
22 char *progname = NULL;
23 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
26 int noschemacheck = 0;
29 char *ldiffile = NULL;
44 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
45 "\t[-n databasenumber | -b suffix]", progname );
49 options = "\t[-s] [-l ldiffile]\n";
53 options = "\t[-l ldiffile]\n";
61 if( options != NULL ) {
62 fputs( options, stderr );
69 * slap_tool_init - initialize slap utility, handle program options.
73 * argc, argv command line arguments
80 int argc, char **argv )
85 int mode = SLAP_TOOL_MODE;
87 progname = lutil_progname( name, argc, argv );
90 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
91 sprintf( leakfilename, "%s.leak", progname );
92 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
100 options = "b:cd:f:l:n:stv";
104 options = "b:cd:f:n:v";
108 options = "b:cd:f:l:n:v";
112 fprintf( stderr, "%s: unknown tool mode (%d)\n",
114 exit( EXIT_FAILURE );
118 conffile = SLAPD_DEFAULT_CONFIGFILE;
120 while ( (i = getopt( argc, argv, options )) != EOF ) {
123 base = strdup( optarg );
125 case 'c': /* enable continue mode */
129 case 'd': /* turn on debugging */
130 ldap_debug += atoi( optarg );
133 case 'f': /* specify a conf file */
134 conffile = strdup( optarg );
137 case 'l': /* LDIF file */
138 ldiffile = strdup( optarg );
141 case 'n': /* which config file db to index */
142 dbnum = atoi( optarg ) - 1;
145 case 's': /* disable schema checking */
149 case 't': /* turn on truncate */
151 mode |= SLAP_TRUNCATE_MODE;
154 case 'v': /* turn on verbose */
164 if ( ( argc != optind ) || (dbnum >= 0 && base != NULL ) ) {
168 if ( ldiffile == NULL ) {
169 ldiffp = tool == SLAPCAT ? stdout : stdin;
171 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
175 exit( EXIT_FAILURE );
179 * initialize stuff and figure out which backend we're dealing with
182 rc = slap_init( mode, progname );
185 fprintf( stderr, "%s: slap_init failed!\n", progname );
186 exit( EXIT_FAILURE );
192 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
193 exit( EXIT_FAILURE );
196 read_config( conffile );
199 fprintf( stderr, "No databases found in config file\n" );
200 exit( EXIT_FAILURE );
206 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
207 exit( EXIT_FAILURE );
211 char *tbase = ch_strdup( base );
213 if( dn_normalize( tbase ) == NULL ) {
214 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
216 exit( EXIT_FAILURE );
219 be = select_backend( tbase );
223 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
225 exit( EXIT_FAILURE );
228 } else if ( dbnum == -1 ) {
229 be = &backends[dbnum=0];
231 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
233 "Database number selected via -n is out of range\n"
234 "Must be in the range 1 to %d (number of databases in the config file)\n",
236 exit( EXIT_FAILURE );
239 be = &backends[dbnum];
249 void slap_tool_destroy( void )
255 mal_dumpleaktrace( leakfile );