Freeciv-3.2
Loading...
Searching...
No Matches
civserver.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include "fc_prehdrs.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27
28#ifdef HAVE_SIGNAL_H
29#include <signal.h>
30#endif
31
32#ifdef GENERATING_MAC /* mac header(s) */
33#include <Controls.h>
34#include <Dialogs.h>
35#endif
36
37#ifdef FREECIV_MSWINDOWS
38#include <windows.h>
39#endif
40
41/* utility */
42#include "deprecations.h"
43#include "fc_cmdline.h"
44#include "fciconv.h"
45#include "fcintl.h"
46#include "log.h"
47#include "support.h"
48
49/* common */
50#include "capstr.h"
51#include "fc_cmdhelp.h"
52#include "game.h"
53#include "version.h"
54
55/* server */
56#include "aiiface.h"
57#include "console.h"
58#include "meta.h"
59#include "sernet.h"
60#include "srv_main.h"
61#include "srv_signal.h"
62
63#ifdef GENERATING_MAC
64static void Mac_options(int argc); /* don't need argv */
65#endif
66
67/**********************************************************************/
72int main(int argc, char *argv[])
73{
74 int inx;
75 bool showhelp = FALSE;
76 bool showvers = FALSE;
77 char *option = NULL;
78
79 /* Load Windows post-crash debugger */
80#ifdef FREECIV_MSWINDOWS
81# ifndef FREECIV_NDEBUG
82 if (LoadLibrary("exchndl.dll") == NULL) {
83# ifdef FREECIV_DEBUG
84 fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
85# endif /* FREECIV_DEBUG */
86 }
87# endif /* FREECIV_NDEBUG */
88#endif /* FREECIV_MSWINDOWS */
89
91
92 /* initialize server */
93 srv_init();
94
95 /* parse command-line arguments... */
96
97#ifdef GENERATING_MAC
99#endif
101
102 game.server.meta_info.type[0] = '\0';
103
104 /* no we don't use GNU's getopt or even the "standard" getopt */
105 /* yes we do have reasons ;) */
106 /* FIXME: and that are? */
107 inx = 1;
108 while (inx < argc) {
109 if ((option = get_option_malloc("--file", argv, &inx, argc,
110 FALSE))) {
112 free(option);
113 } else if (is_option("--help", argv[inx])) {
114 showhelp = TRUE;
115 break;
116 } else if ((option = get_option_malloc("--log", argv, &inx, argc, TRUE))) {
118#ifndef FREECIV_NDEBUG
119 } else if (is_option("--Fatal", argv[inx])) {
120 if (inx + 1 >= argc || '-' == argv[inx + 1][0]) {
122 } else if (str_to_int(argv[inx + 1], &srvarg.fatal_assertions)) {
123 inx++;
124 } else {
125 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
126 argv[inx + 1]);
127 inx++;
128 showhelp = TRUE;
129 }
130#endif /* FREECIV_NDEBUG */
131 } else if ((option = get_option_malloc("--Ranklog", argv, &inx, argc, TRUE))) {
133 } else if (is_option("--keep", argv[inx])) {
135 /* Implies --meta */
137 } else if (is_option("--nometa", argv[inx])) {
138 fc_fprintf(stderr, _("Warning: the %s option is obsolete. "
139 "Use -m to enable the metaserver.\n"), argv[inx]);
140 showhelp = TRUE;
141 } else if (is_option("--meta", argv[inx])) {
143 } else if ((option = get_option_malloc("--Metaserver",
144 argv, &inx, argc, FALSE))) {
146 free(option);
147 srvarg.metaserver_no_send = FALSE; /* --Metaserver implies --meta */
148 } else if ((option = get_option_malloc("--identity",
149 argv, &inx, argc, FALSE))) {
151 free(option);
152 } else if ((option = get_option_malloc("--port", argv, &inx, argc, FALSE))) {
153 if (!str_to_int(option, &srvarg.port)) {
154 showhelp = TRUE;
155 break;
156 }
157 free(option);
158 } else if ((option = get_option_malloc("--bind", argv, &inx, argc, TRUE))) {
160 } else if ((option = get_option_malloc("--Bind-meta", argv, &inx, argc, TRUE))) {
162#ifdef FREECIV_WEB
163 } else if ((option = get_option_malloc("--type", argv, &inx, argc, FALSE))) {
165 free(option);
166#endif /* FREECIV_WEB */
167 } else if ((option = get_option_malloc("--read", argv, &inx, argc, TRUE)))
169 else if ((option = get_option_malloc("--quitidle", argv, &inx, argc, FALSE))) {
171 showhelp = TRUE;
172 break;
173 }
174 free(option);
175 } else if (is_option("--exit-on-end", argv[inx])) {
177 } else if ((option = get_option_malloc("--debug", argv, &inx, argc, FALSE))) {
179 showhelp = TRUE;
180 break;
181 }
182 free(option);
183#ifdef HAVE_FCDB
184 } else if ((option = get_option_malloc("--Database", argv, &inx, argc, FALSE))) {
185 /* Freed after file has been loaded - not here nor in server quit */
188 } else if (is_option("--auth", argv[inx])) {
190 } else if (is_option("--Guests", argv[inx])) {
192 } else if (is_option("--Newusers", argv[inx])) {
194#endif /* HAVE_FCDB */
195 } else if ((option = get_option_malloc("--Serverid", argv, &inx, argc, FALSE))) {
197 free(option);
198 } else if ((option = get_option_malloc("--saves", argv, &inx, argc, TRUE))) {
200 } else if ((option = get_option_malloc("--scenarios", argv, &inx, argc, TRUE))) {
202 } else if ((option = get_option_malloc("--ruleset", argv, &inx, argc, TRUE))) {
204 } else if (is_option("--version", argv[inx])) {
205 showvers = TRUE;
206 } else if ((option = get_option_malloc("--Announce", argv, &inx, argc, FALSE))) {
207 if (!fc_strcasecmp(option, "ipv4")) {
209 } else if (!fc_strcasecmp(option, "none")) {
211#ifdef FREECIV_IPV6_SUPPORT
212 } else if (!fc_strcasecmp(option, "ipv6")) {
214#endif /* IPv6 support */
215 } else {
216 log_error(_("Illegal value \"%s\" for --Announce"), option);
217 }
218 free(option);
219 } else if (is_option("--warnings", argv[inx])) {
221#ifdef AI_MODULES
222 } else if ((option = get_option_malloc("--LoadAI", argv, &inx, argc, FALSE))) {
223 if (!load_ai_module(option)) {
224 fc_fprintf(stderr, _("Failed to load AI module \"%s\"\n"), option);
226 }
227 free(option);
228#endif /* AI_MODULES */
229 } else {
230 fc_fprintf(stderr, _("Error: unknown option '%s'\n"), argv[inx]);
231 showhelp = TRUE;
232 break;
233 }
234 inx++;
235 }
236
237 if (showvers && !showhelp) {
240 }
241 con_write(C_VERSION, _("This is the server for %s"), freeciv_name_version());
242 /* TRANS: No full stop after the URL, could cause confusion. */
243 con_write(C_COMMENT, _("You can learn a lot about Freeciv at %s"),
245
246 if (showhelp) {
247 struct cmdhelp *help = cmdhelp_new(argv[0]);
248
249 cmdhelp_add(help, "A",
250 /* TRANS: "Announce" is exactly what user must type, do not translate. */
251 _("Announce PROTO"),
252 _("Announce game in LAN using protocol PROTO "
253 "(IPv4/IPv6/none)"));
254#ifdef HAVE_FCDB
255 cmdhelp_add(help, "D",
256 /* TRANS: "Database" is exactly what user must type, do not translate. */
257 _("Database FILE"),
258 _("Enable database connection with configuration from "
259 "FILE."));
260 cmdhelp_add(help, "a", "auth",
261 _("Enable server authentication (requires --Database)."));
262 cmdhelp_add(help, "G", "Guests",
263 _("Allow guests to login if auth is enabled."));
264 cmdhelp_add(help, "N", "Newusers",
265 _("Allow new users to login if auth is enabled."));
266#endif /* HAVE_FCDB */
267 cmdhelp_add(help, "b",
268 /* TRANS: "bind" is exactly what user must type, do not translate. */
269 _("bind ADDR"),
270 _("Listen for clients on ADDR"));
271 cmdhelp_add(help, "B", "Bind-meta ADDR",
272 _("Connect to metaserver from this address"));
273#ifdef FREECIV_DEBUG
274 cmdhelp_add(help, "d",
275 /* TRANS: "debug" is exactly what user must type, do not translate. */
276 _("debug LEVEL"),
277 _("Set debug log level (one of f,e,w,n,v,d, or "
278 "d:file1,min,max:...)"));
279#else /* FREECIV_DEBUG */
280 cmdhelp_add(help, "d",
281 /* TRANS: "debug" is exactly what user must type, do not translate. */
282 _("debug LEVEL"),
283 _("Set debug log level (one of f,e,w,n,v)"));
284#endif /* FREECIV_DEBUG */
285#ifndef FREECIV_NDEBUG
286 cmdhelp_add(help, "F",
287 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
288 _("Fatal [SIGNAL]"),
289 _("Raise a signal on failed assertion"));
290#endif /* FREECIV_NDEBUG */
291 cmdhelp_add(help, "f",
292 /* TRANS: "file" is exactly what user must type, do not translate. */
293 _("file FILE"),
294 _("Load saved game FILE"));
295 cmdhelp_add(help, "h", "help",
296 _("Print a summary of the options"));
297 cmdhelp_add(help, "i",
298 /* TRANS: "identity" is exactly what user must type, do not translate. */
299 _("identity ADDR"),
300 _("Be known as ADDR at metaserver or LAN client"));
301 cmdhelp_add(help, "l",
302 /* TRANS: "log" is exactly what user must type, do not translate. */
303 _("log FILE"),
304 _("Use FILE as logfile"));
305 cmdhelp_add(help, "m", "meta",
306 _("Notify metaserver and send server's info"));
307 cmdhelp_add(help, "M",
308 /* TRANS: "Metaserver" is exactly what user must type, do not translate. */
309 _("Metaserver ADDR"),
310 _("Set ADDR as metaserver address"));
311#ifdef FREECIV_WEB
312 cmdhelp_add(help, "t",
313 /* TRANS: "type" is exactly what user must type, do not translate. */
314 _("type TYPE"),
315 _("Set TYPE as server type in metaserver"));
316#endif /* FREECIV_WEB */
317 cmdhelp_add(help, "k", "keep",
318 _("Keep updating game information on metaserver even after "
319 "failure")),
320 cmdhelp_add(help, "p",
321 /* TRANS: "port" is exactly what user must type, do not translate. */
322 _("port PORT"),
323 _("Listen for clients on port PORT"));
324 cmdhelp_add(help, "q",
325 /* TRANS: "quitidle" is exactly what user must type, do not translate. */
326 _("quitidle TIME"),
327 _("Quit if no players for TIME seconds"));
328 cmdhelp_add(help, "e", "exit-on-end",
329 _("When a game ends, exit instead of restarting"));
330 cmdhelp_add(help, "s",
331 /* TRANS: "saves" is exactly what user must type, do not translate. */
332 _("saves DIR"),
333 _("Save games to directory DIR"));
334 cmdhelp_add(help, NULL,
335 /* TRANS: "scenarios" is exactly what user must type, do not translate. */
336 _("scenarios DIR"),
337 _("Save scenarios to directory DIR"));
338 cmdhelp_add(help, "S",
339 /* TRANS: "Serverid" is exactly what user must type, do not translate. */
340 _("Serverid ID"),
341 _("Sets the server id to ID"));
342 cmdhelp_add(help, "r",
343 /* TRANS: "read" is exactly what user must type, do not translate. */
344 _("read FILE"),
345 _("Read startup script FILE"));
346 cmdhelp_add(help, "R",
347 /* TRANS: "Ranklog" is exactly what user must type, do not translate. */
348 _("Ranklog FILE"),
349 _("Use FILE as ranking logfile"));
350 cmdhelp_add(help, NULL,
351 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
352 _("ruleset RULESET"),
353 _("Load ruleset RULESET"));
354#ifdef AI_MODULES
355 cmdhelp_add(help, "L",
356 /* TRANS: "LoadAI" is exactly what user must type, do not translate. */
357 _("LoadAI MODULE"),
358 _("Load ai module MODULE. Can appear multiple times"));
359#endif /* AI_MODULES */
360 cmdhelp_add(help, "v", "version",
361 _("Print the version number"));
362 cmdhelp_add(help, "w", "warnings",
363 _("Warn about deprecated modpack constructs"));
364
365 /* The function below prints a header and footer for the options.
366 * Furthermore, the options are sorted. */
368 cmdhelp_destroy(help);
369
371 }
372
373#ifdef HAVE_FCDB
376 _("Requested authentication with --auth, "
377 "but no --Database given\n"));
379 }
380#endif /* HAVE_FCDB */
381
382 /* disallow running as root -- too dangerous */
383 dont_run_as_root(argv[0], "freeciv_server");
384
386
387 /* have arguments, call the main server loop... */
388 srv_main();
389
390 /* Technically, we won't ever get here. We exit via server_quit. */
391
392 /* done */
394}
395
396#ifdef GENERATING_MAC
397/**********************************************************************/
400static void Mac_options(int argc)
401{
402#define HARDCODED_OPT
403 /*temporary hack since GetNewDialog() doesn't want to work*/
404#ifdef HARDCODED_OPT
405 srvarg.log_filename = "log.out";
407#else /* HARDCODED_OPT */
408 if (argc == 0) {
409 OSErr err;
411 Ptr storage;
412 Handle ditl;
413 Handle dlog;
414 short the_type;
417 short the_item, old_item = 16;
418 int done = false;
420
421 /* load/init the stuff for the dialog */
422 storage = NewPtr(sizeof(DialogRecord));
423 if (storage == 0) {
425 }
426 ditl = Get1Resource('DITL',200);
427 if ((ditl == 0) || (ResError())) {
429 }
430 dlog = Get1Resource('DLOG',200);
431 if ((dlog == 0) || (ResError())) {
433 }
434 /* make the dialog */
435 optptr = GetNewDialog(200, storage, (WindowPtr)-1L);
436 /* setup the dialog */
438 if (err != 0) {
440 }
441 /* insert default highlight draw code? */
443 if (err != 0) {
445 }
447 if (err != 0) {
449 }
450 GetDItem(optptr, 16/*normal radio button*/, &the_type, &the_handle, &the_rect);
452
453 while (!done) /* loop */
454 {
455 ModalDialog(0L, &the_item);/* don't feed 0 where a upp is expected? */
456 /* old book suggests using OL(NIL) as the first argument, but
457 It doesn't include anything about UPPs either, so... */
458 switch (the_item) {
459 case 1:
460 done = true;
461 break;
462 case 2:
464 break;
465 case 13:
469 break;
470 case 15:
471 case 16:
472 case 17:
478 break;
479 }
480 }
481 /* now, load the dialog items into the correct variables interpritation */
483 GetIText( the_handle, (unsigned char *)srvarg.load_filename);
485 GetIText( the_handle, (unsigned char *)srvarg.log_filename);
490 GetIText( the_handle, (unsigned char *)srvarg.script_filename);
494 }
498 }
502 }
503 DisposeDialog(optptr);/*get rid of the dialog after sorting out the options*/
504 DisposePtr(storage);/*clean up the allocated memory*/
505 }
506#endif /* HARDCODED_OPT */
507#undef HARDCODED_OPT
508}
509#endif /* GENERATING_MAC */
bool load_ai_module(const char *modname)
void init_our_capability(void)
Definition capstr.c:87
int main(int argc, char *argv[])
Definition civserver.c:72
char * incite_cost
Definition comments.c:75
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
@ C_COMMENT
Definition console.h:37
@ C_VERSION
Definition console.h:38
void deprecation_warnings_enable(void)
void cmdhelp_destroy(struct cmdhelp *pcmdhelp)
Definition fc_cmdhelp.c:70
void cmdhelp_display(struct cmdhelp *pcmdhelp, bool sort, bool gui_options, bool report_bugs)
Definition fc_cmdhelp.c:104
struct cmdhelp * cmdhelp_new(const char *cmdname)
Definition fc_cmdhelp.c:57
void cmdhelp_add(struct cmdhelp *pcmdhelp, const char *shortarg, const char *longarg, const char *helpstr,...)
Definition fc_cmdhelp.c:86
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
bool log_parse_level_str(const char *level_str, enum log_level *ret_level)
Definition log.c:86
@ LOG_DEBUG
Definition log.h:34
@ LOG_NORMAL
Definition log.h:32
@ LOG_FATAL
Definition log.h:29
@ LOG_VERBOSE
Definition log.h:33
#define log_error(message,...)
Definition log.h:103
@ ANNOUNCE_IPV6
Definition net_types.h:56
@ ANNOUNCE_IPV4
Definition net_types.h:55
@ ANNOUNCE_NONE
Definition net_types.h:54
#define ANNOUNCE_DEFAULT
Definition net_types.h:59
void dont_run_as_root(const char *argv0, const char *fallback)
Definition shared.c:1549
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
struct server_arguments srvarg
Definition srv_main.c:176
void fc__noreturn srv_main(void)
Definition srv_main.c:3532
void srv_init(void)
Definition srv_main.c:227
void setup_interrupt_handlers(void)
Definition srv_signal.c:111
char type[20]
Definition game.h:270
struct civ_game::@31::@35 server
struct civ_game::@31::@35::@38 meta_info
bool metaconnection_persistent
Definition srv_main.h:30
char * bind_addr
Definition srv_main.h:34
enum log_level loglevel
Definition srv_main.h:40
char metaserver_addr[256]
Definition srv_main.h:29
char * scenarios_pathname
Definition srv_main.h:47
enum announce_type announce
Definition srv_main.h:60
char load_filename[512]
Definition srv_main.h:44
char * fcdb_conf
Definition srv_main.h:56
bool auth_allow_guests
Definition srv_main.h:58
char * saves_pathname
Definition srv_main.h:46
char * log_filename
Definition srv_main.h:42
char * ranklog_filename
Definition srv_main.h:43
bool metaserver_no_send
Definition srv_main.h:28
char * bind_meta_addr
Definition srv_main.h:38
bool auth_allow_newusers
Definition srv_main.h:59
char * script_filename
Definition srv_main.h:45
char serverid[256]
Definition srv_main.h:49
char identity_name[256]
Definition srv_main.h:31
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define sz_strlcpy(dest, src)
Definition support.h:195
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const char * freeciv_name_version(void)
Definition version.c:35