WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / lib / popt_common.c
blob11db080c82d7453b4d0cd1c4d9dbefd875a44000
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 = NULL;
46 const char *pname;
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
51 if (!pname)
52 pname = poptGetInvocationName(con);
53 else
54 pname++;
56 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
57 return;
59 lp_set_logfile(lfile);
60 SAFE_FREE(lfile);
63 static bool PrintSambaVersionString;
65 static void popt_s3_talloc_log_fn(const char *message)
67 DEBUG(0,("%s", message));
70 static void popt_common_callback(poptContext con,
71 enum poptCallbackReason reason,
72 const struct poptOption *opt,
73 const char *arg, const void *data)
76 if (reason == POPT_CALLBACK_REASON_PRE) {
77 set_logfile(con, get_dyn_LOGFILEBASE());
78 talloc_set_log_fn(popt_s3_talloc_log_fn);
79 talloc_set_abort_fn(smb_panic);
80 return;
83 if (reason == POPT_CALLBACK_REASON_POST) {
85 if (PrintSambaVersionString) {
86 printf( "Version %s\n", samba_version_string());
87 exit(0);
90 if (is_default_dyn_CONFIGFILE()) {
91 if(getenv("SMB_CONF_PATH")) {
92 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
96 if (override_logfile) {
97 setup_logging(lp_logfile(talloc_tos()), DEBUG_FILE );
100 /* Further 'every Samba program must do this' hooks here. */
101 return;
104 switch(opt->val) {
105 case OPT_OPTION:
107 struct loadparm_context *lp_ctx;
109 lp_ctx = loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
110 if (lp_ctx == NULL) {
111 fprintf(stderr, "loadparm_init_s3() failed!\n");
112 exit(1);
115 if (!lpcfg_set_option(lp_ctx, arg)) {
116 fprintf(stderr, "Error setting option '%s'\n", arg);
117 exit(1);
119 TALLOC_FREE(lp_ctx);
120 break;
122 case 'd':
123 if (arg) {
124 lp_set_cmdline("log level", arg);
126 break;
128 case 'V':
129 PrintSambaVersionString = True;
130 break;
132 case 'O':
133 if (arg) {
134 lp_set_cmdline("socket options", arg);
136 break;
138 case 's':
139 if (arg) {
140 set_dyn_CONFIGFILE(arg);
142 break;
144 case 'n':
145 if (arg) {
146 lp_set_cmdline("netbios name", arg);
148 break;
150 case 'l':
151 if (arg) {
152 set_logfile(con, arg);
153 override_logfile = True;
154 set_dyn_LOGFILEBASE(arg);
156 break;
158 case 'i':
159 if (arg) {
160 lp_set_cmdline("netbios scope", arg);
162 break;
164 case 'W':
165 if (arg) {
166 lp_set_cmdline("workgroup", arg);
168 break;
172 struct poptOption popt_common_connection[] = {
173 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
174 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
175 "SOCKETOPTIONS" },
176 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
177 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
178 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
180 POPT_TABLEEND
183 struct poptOption popt_common_samba[] = {
184 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
185 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
186 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
187 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
188 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
189 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
190 POPT_TABLEEND
193 struct poptOption popt_common_configfile[] = {
194 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
195 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
196 POPT_TABLEEND
199 struct poptOption popt_common_version[] = {
200 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
201 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
202 POPT_TABLEEND
205 struct poptOption popt_common_debuglevel[] = {
206 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
207 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
208 POPT_TABLEEND
211 struct poptOption popt_common_option[] = {
212 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
213 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
214 POPT_TABLEEND