lib:util: Move discard_const(_p) to own header for libndr.h
[Samba.git] / source3 / lib / popt_common.c
blobfa21668000e6816750c2457b9cfca26fa0ff430e
1 /*
2 Unix SMB/CIFS implementation.
3 Common popt routines
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
7 Copyright (C) James Peach 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "lib/param/param.h"
27 /* Handle command line options:
28 * -d,--debuglevel
29 * -s,--configfile
30 * -O,--socket-options
31 * -V,--version
32 * -l,--log-base
33 * -n,--netbios-name
34 * -W,--workgroup
35 * -i,--scope
38 enum {OPT_OPTION=1};
40 extern bool override_logfile;
42 static void set_logfile(poptContext con, const char * arg)
45 char lfile[PATH_MAX];
46 const char *pname;
47 int ret;
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con), '/');
51 if (pname == NULL) {
52 pname = poptGetInvocationName(con);
53 } else {
54 pname++;
57 ret = snprintf(lfile, sizeof(lfile), "%s/log.%s", arg, pname);
58 if (ret >= sizeof(lfile)) {
59 return;
61 lp_set_logfile(lfile);
64 static bool PrintSambaVersionString;
66 static void popt_s3_talloc_log_fn(const char *message)
68 DEBUG(0,("%s", message));
71 static void popt_common_callback(poptContext con,
72 enum poptCallbackReason reason,
73 const struct poptOption *opt,
74 const char *arg, const void *data)
76 TALLOC_CTX *mem_ctx = talloc_new(NULL);
77 if (mem_ctx == NULL) {
78 exit(1);
81 if (reason == POPT_CALLBACK_REASON_PRE) {
82 set_logfile(con, get_dyn_LOGFILEBASE());
83 talloc_set_log_fn(popt_s3_talloc_log_fn);
84 talloc_set_abort_fn(smb_panic);
85 talloc_free(mem_ctx);
86 return;
89 if (reason == POPT_CALLBACK_REASON_POST) {
91 if (PrintSambaVersionString) {
92 printf( "Version %s\n", samba_version_string());
93 talloc_free(mem_ctx);
94 exit(0);
97 if (is_default_dyn_CONFIGFILE()) {
98 if (getenv("SMB_CONF_PATH")) {
99 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
103 if (override_logfile) {
104 char *logfile = lp_logfile(mem_ctx);
105 if (logfile == NULL) {
106 talloc_free(mem_ctx);
107 exit(1);
109 setup_logging(logfile, DEBUG_FILE);
112 /* Further 'every Samba program must do this' hooks here. */
113 talloc_free(mem_ctx);
114 return;
117 switch(opt->val) {
118 case OPT_OPTION:
120 struct loadparm_context *lp_ctx;
121 bool ok;
123 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
124 if (lp_ctx == NULL) {
125 fprintf(stderr, "loadparm_init_s3() failed!\n");
126 talloc_free(mem_ctx);
127 exit(1);
130 ok = lpcfg_set_option(lp_ctx, arg);
131 if (!ok) {
132 fprintf(stderr, "Error setting option '%s'\n", arg);
133 talloc_free(mem_ctx);
134 exit(1);
136 break;
138 case 'd':
139 if (arg) {
140 lp_set_cmdline("log level", arg);
142 break;
144 case 'V':
145 PrintSambaVersionString = True;
146 break;
148 case 'O':
149 if (arg) {
150 lp_set_cmdline("socket options", arg);
152 break;
154 case 's':
155 if (arg) {
156 set_dyn_CONFIGFILE(arg);
158 break;
160 case 'n':
161 if (arg) {
162 lp_set_cmdline("netbios name", arg);
164 break;
166 case 'l':
167 if (arg) {
168 set_logfile(con, arg);
169 override_logfile = True;
170 set_dyn_LOGFILEBASE(arg);
172 break;
174 case 'i':
175 if (arg) {
176 lp_set_cmdline("netbios scope", arg);
178 break;
180 case 'W':
181 if (arg) {
182 lp_set_cmdline("workgroup", arg);
184 break;
187 talloc_free(mem_ctx);
190 struct poptOption popt_common_connection[] = {
192 .argInfo = POPT_ARG_CALLBACK,
193 .arg = (void *)popt_common_callback,
196 .longName = "socket-options",
197 .shortName = 'O',
198 .argInfo = POPT_ARG_STRING,
199 .val = 'O',
200 .descrip = "socket options to use",
201 .argDescrip = "SOCKETOPTIONS",
204 .longName = "netbiosname",
205 .shortName = 'n',
206 .argInfo = POPT_ARG_STRING,
207 .val = 'n',
208 .descrip = "Primary netbios name",
209 .argDescrip = "NETBIOSNAME"
212 .longName = "workgroup",
213 .shortName = 'W',
214 .argInfo = POPT_ARG_STRING,
215 .val = 'W',
216 .descrip = "Set the workgroup name",
217 .argDescrip = "WORKGROUP"
220 .longName = "scope",
221 .shortName = 'i',
222 .argInfo = POPT_ARG_STRING,
223 .val = 'i',
224 .descrip = "Use this Netbios scope",
225 .argDescrip = "SCOPE"
227 POPT_TABLEEND
230 struct poptOption popt_common_samba[] = {
232 .argInfo = POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
233 .arg = (void *)popt_common_callback,
236 .longName = "debuglevel",
237 .shortName = 'd',
238 .argInfo = POPT_ARG_STRING,
239 .val = 'd',
240 .descrip = "Set debug level",
241 .argDescrip = "DEBUGLEVEL",
244 .longName = "configfile",
245 .shortName = 's',
246 .argInfo = POPT_ARG_STRING,
247 .val = 's',
248 .descrip = "Use alternate configuration file",
249 .argDescrip = "CONFIGFILE",
252 .longName = "log-basename",
253 .shortName = 'l',
254 .argInfo = POPT_ARG_STRING,
255 .val = 'l',
256 .descrip = "Base name for log files",
257 .argDescrip = "LOGFILEBASE",
260 .longName = "version",
261 .shortName = 'V',
262 .argInfo = POPT_ARG_NONE,
263 .val = 'V',
264 .descrip = "Print version",
267 .longName = "option",
268 .argInfo = POPT_ARG_STRING,
269 .val = OPT_OPTION,
270 .descrip = "Set smb.conf option from command line",
271 .argDescrip = "name=value",
273 POPT_TABLEEND
276 struct poptOption popt_common_configfile[] = {
278 .argInfo = POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST,
279 .arg = (void *)popt_common_callback,
282 .longName = "configfile",
283 .argInfo = POPT_ARG_STRING,
284 .val = 's',
285 .descrip = "Use alternate configuration file",
286 .argDescrip = "CONFIGFILE",
288 POPT_TABLEEND
291 struct poptOption popt_common_version[] = {
293 .argInfo = POPT_ARG_CALLBACK|POPT_CBFLAG_POST,
294 .arg = (void *)popt_common_callback
297 .longName = "version",
298 .shortName = 'V',
299 .argInfo = POPT_ARG_NONE,
300 .val = 'V',
301 .descrip = "Print version",
303 POPT_TABLEEND
306 struct poptOption popt_common_debuglevel[] = {
308 .argInfo = POPT_ARG_CALLBACK,
309 .arg = (void *)popt_common_callback,
312 .longName = "debuglevel",
313 .shortName = 'd',
314 .argInfo = POPT_ARG_STRING,
315 .val = 'd',
316 .descrip = "Set debug level",
317 .argDescrip = "DEBUGLEVEL",
319 POPT_TABLEEND
322 struct poptOption popt_common_option[] = {
324 .argInfo = POPT_ARG_CALLBACK|POPT_CBFLAG_POST,
325 .arg = (void *)popt_common_callback,
328 .longName = "option",
329 .argInfo = POPT_ARG_STRING,
330 .val = OPT_OPTION,
331 .descrip = "Set smb.conf option from command line",
332 .argDescrip = "name=value",
334 POPT_TABLEEND