r7926: poptGetNextOpt() returns int, not char
[sfrench/samba-autobuild/.git] / source4 / lib / ldb / tools / cmdline.c
1 /* 
2    ldb database library - command line handling for ldb tools
3
4    Copyright (C) Andrew Tridgell  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/include/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "ldb/tools/cmdline.h"
29 #ifdef _SAMBA_BUILD_
30 #include "lib/cmdline/popt_common.h"
31 #endif
32
33 /*
34   process command line options
35 */
36 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
37                                         void (*usage)(void))
38 {
39         struct ldb_cmdline options, *ret=NULL;
40         poptContext pc;
41 #ifdef _SAMBA_BUILD_
42         int r;
43 #endif
44         int num_options = 0;
45         int opt;
46         struct poptOption popt_options[] = {
47                 POPT_AUTOHELP
48                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
49                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
50                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
51                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
52                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
53                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
54                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
55                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
56                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
57                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "dn=*", NULL },
58                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
59                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
60                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
61 #ifdef _SAMBA_BUILD_
62                 POPT_COMMON_SAMBA
63                 POPT_COMMON_CREDENTIALS
64                 POPT_COMMON_VERSION
65 #endif
66                 POPT_TABLEEND
67         };
68
69 #ifdef _SAMBA_BUILD_
70         ldbsearch_init_subsystems;
71         r = ldb_register_samba_handlers(ldb);
72         if (r != 0) {
73                 goto failed;
74         }
75 #endif
76
77         ret = talloc_zero(ldb, struct ldb_cmdline);
78         if (ret == NULL) {
79                 ldb_oom(ldb);
80                 goto failed;
81         }
82
83         options = *ret;
84         
85         /* pull in URL */
86         options.url = getenv("LDB_URL");
87
88         /* and editor (used by ldbedit) */
89         options.editor = getenv("VISUAL");
90         if (!options.editor) {
91                 options.editor = getenv("EDITOR");
92         }
93         if (!options.editor) {
94                 options.editor = "vi";
95         }
96
97         options.scope = LDB_SCOPE_DEFAULT;
98
99         pc = poptGetContext(argv[0], argc, argv, popt_options, 
100                             POPT_CONTEXT_KEEP_FIRST);
101
102         while((opt = poptGetNextOpt(pc)) != -1) {
103                 switch (opt) {
104                 case 's': {
105                         const char *arg = poptGetOptArg(pc);
106                         if (strcmp(arg, "base") == 0) {
107                                 options.scope = LDB_SCOPE_BASE;
108                         } else if (strcmp(arg, "sub") == 0) {
109                                 options.scope = LDB_SCOPE_SUBTREE;
110                         } else if (strcmp(arg, "one") == 0) {
111                                 options.scope = LDB_SCOPE_ONELEVEL;
112                         } else {
113                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
114                                 goto failed;
115                         }
116                         break;
117                 }
118
119                 case 'v':
120                         options.verbose++;
121                         break;
122
123                 case 'o':
124                         options.options = talloc_realloc(ret, options.options, 
125                                                          const char *, num_options+2);
126                         if (options.options == NULL) {
127                                 ldb_oom(ldb);
128                                 goto failed;
129                         }
130                         options.options[num_options++] = poptGetOptArg(pc);
131                         options.options[num_options+1] = NULL;
132                         break;
133                         
134                 default:
135                         fprintf(stderr, "Invalid option %s: %s\n", 
136                                 poptBadOption(pc, 0), poptStrerror(opt));
137                         if (usage) usage();
138                         goto failed;
139                 }
140         }
141
142         /* setup the remaining options for the main program to use */
143         options.argv = poptGetArgs(pc);
144         if (options.argv) {
145                 options.argv++;
146                 while (options.argv[options.argc]) options.argc++;
147         }
148
149         *ret = options;
150
151         /* all utils need some option */
152         if (ret->url == NULL) {
153                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
154                 if (usage) usage();
155                 goto failed;
156         }
157
158         if (ldb_connect(ldb, ret->url, 0, ret->options) != 0) {
159                 fprintf(stderr, "Failed to connect to %s - %s\n", 
160                         ret->url, ldb_errstring(ldb));
161                 goto failed;
162         }
163
164         return ret;
165
166 failed:
167         talloc_free(ret);
168         exit(1);
169         return NULL;
170 }