This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[vlendec/samba-autobuild/.git] / source3 / lib / popt_common.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Common popt routines
4
5    Copyright (C) Tim Potter 2001,2002
6    Copyright (C) Jelmer Vernooij 2002
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /* Handle command line options:
26  *              d,--debuglevel 
27  *              s,--configfile 
28  *              O,--socket-options 
29  *              V,--version
30  *              l,--log-base
31  *              n,--netbios-name
32  */
33
34 extern pstring user_socket_options;
35 extern BOOL AllowDebugChange;
36
37 static void popt_common_callback(poptContext con, 
38                            enum poptCallbackReason reason,
39                            const struct poptOption *opt,
40                            const char *arg, const void *data)
41 {
42         pstring logfile;
43         const char *pname;
44         
45         /* Find out basename of current program */
46         pname = strrchr_m(poptGetInvocationName(con),'/');
47
48         if (!pname)
49                 pname = poptGetInvocationName(con);
50         else 
51                 pname++;
52
53         if (reason == POPT_CALLBACK_REASON_PRE) {
54                 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
55                 lp_set_logfile(logfile);
56                 return;
57         }
58
59         switch(opt->val) {
60         case 'd':
61                 if (arg) {
62                         debug_parse_levels(arg);
63                         AllowDebugChange = False;
64                 }
65                 break;
66
67         case 'V':
68                 printf( "Version %s\n", VERSION );
69                 exit(0);
70                 break;
71
72         case 'O':
73                 if (arg) {
74                         pstrcpy(user_socket_options,arg);
75                 }
76                 break;
77
78         case 's':
79                 if (arg) {
80                         pstrcpy(dyn_CONFIGFILE, arg);
81                 }
82                 break;
83
84         case 'n':
85                 if (arg) {
86                         set_global_myname(arg);
87                 }
88                 break;
89
90         case 'l':
91                 if (arg) {
92                         pstr_sprintf(logfile, "%s/log.%s", arg, pname);
93                         lp_set_logfile(logfile);
94                 }
95                 break;
96         }
97 }
98
99 struct poptOption popt_common_debug[] = {
100         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
101         { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", 
102           "DEBUGLEVEL" },
103         { 0 }
104 };
105
106 struct poptOption popt_common_configfile[] = {
107         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
108         { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file" },
109         { 0 }
110 };
111
112 struct poptOption popt_common_socket_options[] = {
113         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
114         {"socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use" },
115         { 0 }
116 };
117
118 struct poptOption popt_common_version[] = {
119         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
120         {"version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
121         { 0 }
122 };
123
124 struct poptOption popt_common_netbios_name[] = {
125         { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
126         {"netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name"},
127         { 0 }
128 };
129
130 struct poptOption popt_common_log_base[] = {
131         { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
132         { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files"},
133         { 0 }
134 };