Debian Codegraph @

src/d2dbs/cmdline.cpp

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