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",
101 conffile = SLAPD_DEFAULT_CONFIGFILE;
103 while ( (i = getopt( argc, argv, options )) != EOF ) {
106 base = strdup( optarg );
108 case 'd': /* turn on debugging */
109 ldap_debug += atoi( optarg );
112 case 'f': /* specify a conf file */
113 conffile = strdup( optarg );
116 case 'l': /* LDIF file */
117 ldiffile = strdup( optarg );
120 case 'n': /* which config file db to index */
121 dbnum = atoi( optarg ) - 1;
124 case 't': /* turn on truncate */
126 mode |= SLAP_TRUNCATE_MODE;
129 case 'v': /* turn on verbose */
139 if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
140 || (dbnum >= 0 && base != NULL ) )
145 if ( ldiffile == NULL ) {
146 ldiffp = tool == SLAPCAT ? stdout : stdin;
148 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
152 exit( EXIT_FAILURE );
156 * initialize stuff and figure out which backend we're dealing with
159 rc = slap_init( mode, progname );
162 fprintf( stderr, "%s: slap_init failed!\n", progname );
163 exit( EXIT_FAILURE );
166 read_config( conffile );
169 fprintf( stderr, "No databases found in config file\n" );
170 exit( EXIT_FAILURE );
174 char *tbase = ch_strdup( base );
176 if( dn_normalize_case( tbase ) == NULL ) {
177 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
179 exit( EXIT_FAILURE );
182 be = select_backend( tbase );
186 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
188 exit( EXIT_FAILURE );
191 } else if ( dbnum == -1 ) {
192 be = &backends[dbnum=0];
194 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
196 "Database number selected via -n is out of range\n"
197 "Must be in the range 1 to %d (number of databases in the config file)\n",
199 exit( EXIT_FAILURE );
202 be = &backends[dbnum];