2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 /* slapcommon.c - common routine for the slap tools */
11 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
17 #include "slapcommon.h"
21 char *progname = NULL;
22 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
26 char *ldiffile = NULL;
36 "usage: %s [-v] [-d debuglevel] [-f configfile]\n"
37 "\t[-n databasenumber | -b suffix]", progname );
41 options = "\t[-l ldiffile]\n";
45 options = "\t[-l ldiffile]\n";
49 options = "\tattributetype\n";
53 if( options != NULL ) {
54 fputs( options, stderr );
61 * slap_tool_init - initialize slap utility, handle program options.
65 * argc, argv command line arguments
72 int argc, char **argv )
77 int mode = SLAP_TOOL_MODE;
79 progname = lutil_progname( name, argc, argv );
83 options = "b:d:f:l:n:tv";
87 options = "b:d:f:n:v";
91 options = "b:d:f:l:n:v";
95 fprintf( stderr, "%s: unknown tool mode (%d)\n", tool );
100 conffile = SLAPD_DEFAULT_CONFIGFILE;
102 while ( (i = getopt( argc, argv, options )) != EOF ) {
105 base = strdup( optarg );
107 case 'd': /* turn on debugging */
108 ldap_debug += atoi( optarg );
111 case 'f': /* specify a conf file */
112 conffile = strdup( optarg );
115 case 'l': /* LDIF file */
116 ldiffile = strdup( optarg );
119 case 'n': /* which config file db to index */
120 dbnum = atoi( optarg ) - 1;
123 case 't': /* turn on truncate */
125 mode |= SLAP_TRUNCATE_MODE;
128 case 'v': /* turn on verbose */
138 if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
139 || (dbnum >= 0 && base != NULL ) )
144 if ( ldiffile == NULL ) {
145 ldiffp = tool == SLAPCAT ? stdout : stdin;
147 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
151 exit( EXIT_FAILURE );
155 * initialize stuff and figure out which backend we're dealing with
158 rc = slap_init( mode, progname );
161 fprintf( stderr, "%s: slap_init failed!\n", progname );
162 exit( EXIT_FAILURE );
165 read_config( conffile );
168 fprintf( stderr, "No databases found in config file\n" );
169 exit( EXIT_FAILURE );
173 char *tbase = ch_strdup( base );
175 if( dn_normalize_case( tbase ) == NULL ) {
176 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
178 exit( EXIT_FAILURE );
181 be = select_backend( tbase );
185 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
187 exit( EXIT_FAILURE );
190 } else if ( dbnum == -1 ) {
191 be = &backends[dbnum=0];
193 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
195 "Database number selected via -n is out of range\n"
196 "Must be in the range 1 to %d (number of databases in the config file)\n",
198 exit( EXIT_FAILURE );
201 be = &backends[dbnum];