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;
28 char *ldiffile = NULL;
43 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
44 "\t[-n databasenumber | -b suffix]", progname );
48 options = "\t[-l ldiffile]\n";
52 options = "\t[-l ldiffile]\n";
60 if( options != NULL ) {
61 fputs( options, stderr );
68 * slap_tool_init - initialize slap utility, handle program options.
72 * argc, argv command line arguments
79 int argc, char **argv )
84 int mode = SLAP_TOOL_MODE;
86 progname = lutil_progname( name, argc, argv );
89 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
90 sprintf( leakfilename, "%s.leak", progname );
91 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
99 options = "b:cd:f:l:n:tv";
103 options = "b:cd:f:n:v";
107 options = "b:cd:f:l:n:v";
111 fprintf( stderr, "%s: unknown tool mode (%d)\n",
113 exit( EXIT_FAILURE );
117 conffile = SLAPD_DEFAULT_CONFIGFILE;
119 while ( (i = getopt( argc, argv, options )) != EOF ) {
122 base = strdup( optarg );
124 case 'c': /* enable continue mode */
128 case 'd': /* turn on debugging */
129 ldap_debug += atoi( optarg );
132 case 'f': /* specify a conf file */
133 conffile = strdup( optarg );
136 case 'l': /* LDIF file */
137 ldiffile = strdup( optarg );
140 case 'n': /* which config file db to index */
141 dbnum = atoi( optarg ) - 1;
144 case 't': /* turn on truncate */
146 mode |= SLAP_TRUNCATE_MODE;
149 case 'v': /* turn on verbose */
159 if ( ( argc != optind ) || (dbnum >= 0 && base != NULL ) ) {
163 if ( ldiffile == NULL ) {
164 ldiffp = tool == SLAPCAT ? stdout : stdin;
166 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
170 exit( EXIT_FAILURE );
174 * initialize stuff and figure out which backend we're dealing with
177 rc = slap_init( mode, progname );
180 fprintf( stderr, "%s: slap_init failed!\n", progname );
181 exit( EXIT_FAILURE );
187 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
188 exit( EXIT_FAILURE );
191 read_config( conffile );
194 fprintf( stderr, "No databases found in config file\n" );
195 exit( EXIT_FAILURE );
201 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
202 exit( EXIT_FAILURE );
206 char *tbase = ch_strdup( base );
208 if( dn_normalize( tbase ) == NULL ) {
209 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
211 exit( EXIT_FAILURE );
214 be = select_backend( tbase, 0 );
218 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
220 exit( EXIT_FAILURE );
223 } else if ( dbnum == -1 ) {
224 be = &backends[dbnum=0];
226 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
228 "Database number selected via -n is out of range\n"
229 "Must be in the range 1 to %d (number of databases in the config file)\n",
231 exit( EXIT_FAILURE );
234 be = &backends[dbnum];
244 void slap_tool_destroy( void )
250 mal_dumpleaktrace( leakfile );