Debian Codegraph @

src/bnetd/cmdline.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998 Mark Baysinger ([email protected])
3 * Copyright (C) 1998,1999,2000 Ross Combs ([email protected])
4 * Copyright (C) 2005 Dizzy
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 #include "common/setup_before.h"
21 #include "cmdline.h"
22
23 #include <cstdio>
24
25 #include "common/xalloc.h"
26 #ifdef WIN32
27 # include "win32/service.h"
28 #endif
29 #ifdef WIN32_GUI
30 # include "common/gui_printf.h"
31 #endif
32 #include "compat/strcasecmp.h"
33 #include "common/eventlog.h"
34 #include "common/conf.h"
35 #include "common/setup_after.h"
36
37 namespace pvpgn
38 {
39
40 namespace bnetd
41 {
42
43 static struct {
44 #ifdef DO_DAEMONIZE
45 unsigned foreground;
46 #endif
47 const char *preffile;
48 const char *hexfile;
49 unsigned debug;
50 #ifdef WIN32_GUI
51 unsigned console;
52 unsigned gui;
53 #endif
54 } cmdline_config;
55
56 static unsigned exitflag;
57 static const char *progname;
58
59 #ifdef DO_DAEMONIZE
60 static int conf_set_foreground(const char *valstr);
61 static int conf_setdef_foreground(void);
62 #endif
63
64 static int conf_set_preffile(const char *valstr);
65 static int conf_setdef_preffile(void);
66
67 static int conf_set_hexfile(const char *valstr);
68 static int conf_setdef_hexfile(void);
69
70 static int conf_set_debug(const char *valstr);
71 static int conf_setdef_debug(void);
72
73 static int conf_set_help(const char *valstr);
74 static int conf_setdef_help(void);
75
76 static int conf_set_version(const char *valstr);
77 static int conf_setdef_version(void);
78
79 #ifdef WIN32
80 static int conf_set_service(const char *valstr);
81 static int conf_setdef_service(void);
82
83 static int conf_set_servaction(const char *valstr);
84 static int conf_setdef_servaction(void);
85 #endif
86
87 #ifdef WIN32_GUI
88 static int conf_set_console(char const * valstr);
89 static int conf_setdef_console(void);
90
91 static int conf_set_gui(char const * valstr);
92 static int conf_setdef_gui(void);
93 #endif
94
95 static t_conf_entry conftab[] = {
96 { "c", conf_set_preffile, NULL, conf_setdef_preffile },
97 { "config", conf_set_preffile, NULL, conf_setdef_preffile },
98 { "d", conf_set_hexfile, NULL, conf_setdef_hexfile },
99 { "hexdump", conf_set_hexfile, NULL, conf_setdef_hexfile },
100 #ifdef DO_DAEMONIZE
101 { "f", conf_set_foreground, NULL, conf_setdef_foreground },
102 { "foreground", conf_set_foreground, NULL, conf_setdef_foreground },
103 #endif
104 { "D", conf_set_debug, NULL, conf_setdef_debug },
105 { "debug", conf_set_debug, NULL, conf_setdef_debug },
106 { "h", conf_set_help, NULL, conf_setdef_help },
107 { "help", conf_set_help, NULL, conf_setdef_help },
108 { "usage", conf_set_help, NULL, conf_setdef_help },
109 { "v", conf_set_version, NULL, conf_setdef_version },
110 { "version", conf_set_version, NULL, conf_setdef_version },
111 #ifdef WIN32
112 { "service", conf_set_service, NULL, conf_setdef_service },
113 { "s", conf_set_servaction, NULL, conf_setdef_servaction },
114 #endif
115 #ifdef WIN32_GUI
116 { "console", conf_set_console, NULL, conf_setdef_console },
117 { "gui", conf_set_gui, NULL, conf_setdef_gui },
118 #endif
119 { NULL, NULL, NULL, NULL }
120 };
121
122 static void usage(void)
123 {
124 std::fprintf(stderr,
125 "usage: %s [<options>]\n"
126 " -c FILE, --config=FILE use FILE as configuration file (default is " BNETD_DEFAULT_CONF_FILE ")\n"
127 " -d FILE, --hexdump=FILE do hex dump of packets into FILE\n"
128 #ifdef DO_DAEMONIZE
129 " -f, --foreground don't daemonize\n"
130 #endif
131 " -D, --debug run in debug mode (run in foreground and log to stdout)\n"
132 " -h, --help, --usage show this information and exit\n"
133 " -v, --version print version number and exit\n"
134 #ifdef WIN32
135 " Running as service functions:\n"
136 " --service run as service\n"
137 " -s install install service\n"
138 " -s uninstall uninstall service\n"
139 #endif
140 #ifdef WIN32GUI
141 " -console run in console mode\n"
142 #endif
143 , progname);
144 }
145
146 extern int cmdline_load(int argc, char** argv)
147 {
148 int res;
149
150 if (argc < 1 || !argv || !argv[0]) {
151 std::fprintf(stderr, "bad arguments\n");
152 return -1;
153 }
154
155 exitflag = 0;
156 progname = argv[0];
157
158 res = conf_load_cmdline(argc, argv, conftab);
159 if (res < 0) return -1;
160 return exitflag ? 0 : 1;
161 }
162
163 extern void cmdline_unload(void)
164 {
165 conf_unload(conftab);
166 }
167
168 #ifdef DO_DAEMONIZE
169 extern int cmdline_get_foreground(void)
170 {
171 return cmdline_config.foreground;
172 }
173
174 static int conf_set_foreground(const char *valstr)
175 {
176 return conf_set_bool(&cmdline_config.foreground, valstr, 0);
177 }
178
179 static int conf_setdef_foreground(void)
180 {
181 return conf_set_bool(&cmdline_config.foreground, NULL, 0);
182 }
183 #endif
184
185 extern const char* cmdline_get_preffile(void)
186 {
187 return cmdline_config.preffile;
188 }
189
190 static int conf_set_preffile(const char *valstr)
191 {
192 return conf_set_str(&cmdline_config.preffile, valstr, NULL);
193 }
194
195 static int conf_setdef_preffile(void)
196 {
197 return conf_set_str(&cmdline_config.preffile, NULL, BNETD_DEFAULT_CONF_FILE);
198 }
199
200
201 extern const char* cmdline_get_hexfile(void)
202 {
203 return cmdline_config.hexfile;
204 }
205
206 static int conf_set_hexfile(const char *valstr)
207 {
208 return conf_set_str(&cmdline_config.hexfile, valstr, NULL);
209 }
210
211 static int conf_setdef_hexfile(void)
212 {
213 return conf_set_str(&cmdline_config.hexfile, NULL, NULL);
214 }
215
216
217 static int conf_set_debug(const char *valstr)
218 {
219 conf_set_bool(&cmdline_config.debug, valstr, 0);
220 if (cmdline_config.debug) eventlog_set_debugmode(1);
221 #ifdef DO_DAEMONIZE
222 cmdline_config.foreground = 1;
223 #endif
224 return 0;
225 }
226
227 static int conf_setdef_debug(void)
228 {
229 return conf_set_bool(&cmdline_config.debug, NULL, 0);
230 }
231
232 static int conf_set_help(const char *valstr)
233 {
234 unsigned tmp = 0;
235
236 conf_set_bool(&tmp, valstr, 0);
237 if (tmp) {
238 usage();
239 exitflag = 1;
240 }
241
242 return 0;
243 }
244
245 static int conf_setdef_help(void)
246 {
247 return 0;
248 }
249
250
251 static int conf_set_version(const char *valstr)
252 {
253 unsigned tmp = 0;
254
255 conf_set_bool(&tmp, valstr, 0);
256 if (tmp) {
257 #ifdef WIN32_GUI
258 gui_lvprintf(eventlog_level_info, PVPGN_SOFTWARE" version " PVPGN_VERSION "\n");
259 #else
260 std::printf(PVPGN_SOFTWARE" version " PVPGN_VERSION "\n");
261 #endif
262 exitflag = 1;
263 }
264
265 return 0;
266 }
267
268 static int conf_setdef_version(void)
269 {
270 return 0;
271 }
272
273 #ifdef WIN32
274 static int conf_set_service(const char *valstr)
275 {
276 unsigned tmp = 0;
277
278 conf_set_bool(&tmp, valstr, 0);
279 if (tmp) {
280 Win32_ServiceRun();
281 exitflag = 1;
282 }
283
284 return 0;
285 }
286
287 static int conf_setdef_service(void)
288 {
289 return 0;
290 }
291
292
293 static int conf_set_servaction(const char *valstr)
294 {
295 const char* tmp = NULL;
296
297 conf_set_str(&tmp, valstr, NULL);
298
299 if (tmp) {
300 if (!strcasecmp(tmp, "install")) {
301 std::fprintf(stderr, "Installing service");
302 Win32_ServiceInstall();
303 }
304 else if (!strcasecmp(tmp, "uninstall")) {
305 std::fprintf(stderr, "Uninstalling service");
306 Win32_ServiceUninstall();
307 }
308 else {
309 std::fprintf(stderr, "Unknown service action '%s'\n", tmp);
310 }
311
312 exitflag = 1;
313 xfree((void *)tmp);
314 }
315
316 return 0;
317 }
318
319 static int conf_setdef_servaction(void)
320 {
321 return 0;
322 }
323 #endif
324
325
326 #ifdef WIN32_GUI
327
328 static int conf_set_console(const char *valstr)
329 {
330 conf_set_bool(&cmdline_config.console, valstr, 0);
331 if (cmdline_config.console){
332 conf_set_bool(&cmdline_config.gui, NULL, 0);
333 }
334 return 0;
335 }
336
337 static int conf_setdef_console(void)
338 {
339 return conf_set_bool(&cmdline_config.console, NULL, 0);
340 }
341
342 extern unsigned cmdline_get_console(void){
343 return cmdline_config.console;
344 }
345
346
347 static int conf_set_gui(const char *valstr)
348 {
349 conf_set_bool(&cmdline_config.gui, valstr, 0);
350 if (cmdline_config.gui){
351 }
352 return 0;
353 }
354
355 static int conf_setdef_gui(void)
356 {
357 return conf_set_bool(&cmdline_config.gui, NULL, 1);
358 }
359
360 extern unsigned cmdline_get_gui(void){
361 return cmdline_config.gui;
362 }
363
364 #endif
365
366 }
367
368 }