2010-08-08 10:06:34 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <util/log.h>
|
|
|
|
|
|
|
|
#include <kernel/config.h>
|
2013-12-27 01:02:41 +01:00
|
|
|
#include <kernel/types.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/save.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
#include <kernel/version.h>
|
2012-06-07 21:44:25 +02:00
|
|
|
#include <bindings/bindings.h>
|
2012-06-05 16:36:33 +02:00
|
|
|
#include <eressea.h>
|
|
|
|
#include <gmtool.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2010-08-08 09:40:42 +02:00
|
|
|
#include "races/races.h"
|
2012-06-05 16:36:33 +02:00
|
|
|
#include "spells/spells.h"
|
|
|
|
#include "curses.h"
|
2012-06-07 21:44:25 +02:00
|
|
|
|
2012-06-10 22:33:05 +02:00
|
|
|
#include <lua.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <wctype.h>
|
2012-06-05 06:45:25 +02:00
|
|
|
#include <iniparser.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2013-12-27 01:02:41 +01:00
|
|
|
static const char *logfile= "eressea.log";
|
2011-03-07 08:03:10 +01:00
|
|
|
static const char *luafile = "setup.lua";
|
2011-03-07 08:02:35 +01:00
|
|
|
static const char *entry_point = NULL;
|
|
|
|
static const char *inifile = "eressea.ini";
|
2010-08-08 10:06:34 +02:00
|
|
|
static int memdebug = 0;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void parse_config(const char *filename)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
dictionary *d = iniparser_new(filename);
|
2010-08-08 10:06:34 +02:00
|
|
|
if (d) {
|
|
|
|
load_inifile(d);
|
2012-06-07 21:44:25 +02:00
|
|
|
log_debug("reading from configuration file %s\n", filename);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
memdebug = iniparser_getint(d, "eressea:memcheck", memdebug);
|
|
|
|
entry_point = iniparser_getstring(d, "eressea:run", entry_point);
|
|
|
|
luafile = iniparser_getstring(d, "eressea:load", luafile);
|
|
|
|
|
|
|
|
/* only one value in the [editor] section */
|
|
|
|
force_color = iniparser_getint(d, "editor:color", force_color);
|
2010-08-08 09:40:42 +02:00
|
|
|
|
|
|
|
/* excerpt from [config] (the rest is used in bindings.c) */
|
|
|
|
game_name = iniparser_getstring(d, "config:game", game_name);
|
2012-06-07 21:44:25 +02:00
|
|
|
} else {
|
|
|
|
log_error("could not open configuration file %s\n", filename);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
global.inifile = d;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int usage(const char *prog, const char *arg)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
if (arg) {
|
|
|
|
fprintf(stderr, "unknown argument: %s\n\n", arg);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Usage: %s [options]\n"
|
|
|
|
"-t <turn> : read this datafile, not the most current one\n"
|
|
|
|
"-q : be quite (same as -v 0)\n"
|
|
|
|
"-v <level> : verbosity level\n"
|
|
|
|
"-C : run in interactive mode\n"
|
2012-06-07 21:44:25 +02:00
|
|
|
"--color : force curses to use colors even when not detected\n", prog);
|
2010-08-08 10:06:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:56:59 +01:00
|
|
|
static int get_arg(int argc, char **argv, size_t len, int index, const char **result, const char *def) {
|
|
|
|
if (argv[index][len]) {
|
|
|
|
*result = argv[index]+len;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
if (index+1 < argc) {
|
2013-12-27 01:54:02 +01:00
|
|
|
*result = argv[index+1];
|
2013-12-26 22:56:59 +01:00
|
|
|
return index+1;
|
|
|
|
}
|
|
|
|
*result = def;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int parse_args(int argc, char **argv, int *exitcode)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2012-06-07 21:44:25 +02:00
|
|
|
int i;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
for (i = 1; i != argc; ++i) {
|
|
|
|
if (argv[i][0] != '-') {
|
2010-08-08 10:06:34 +02:00
|
|
|
return usage(argv[0], argv[i]);
|
2011-03-07 08:02:35 +01:00
|
|
|
} else if (argv[i][1] == '-') { /* long format */
|
|
|
|
if (strcmp(argv[i] + 2, "version") == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
printf("\n%s PBEM host\n"
|
|
|
|
"Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n"
|
2013-12-31 10:06:28 +01:00
|
|
|
"Compilation: " __DATE__ " at " __TIME__ "\nVersion: %d\n\n",
|
|
|
|
global.gamename, RELEASE_VERSION);
|
2011-03-07 08:02:35 +01:00
|
|
|
} else if (strcmp(argv[i] + 2, "color") == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
/* force the editor to have colors */
|
|
|
|
force_color = 1;
|
2011-03-07 08:02:35 +01:00
|
|
|
} else if (strcmp(argv[i] + 2, "help") == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
return usage(argv[0], NULL);
|
2011-03-07 08:02:35 +01:00
|
|
|
} else {
|
2010-08-08 10:06:34 +02:00
|
|
|
return usage(argv[0], argv[i]);
|
|
|
|
}
|
2012-05-22 23:53:46 +02:00
|
|
|
} else {
|
2013-12-26 22:56:59 +01:00
|
|
|
const char *arg;
|
2011-03-07 08:02:35 +01:00
|
|
|
switch (argv[i][1]) {
|
2013-12-26 22:56:59 +01:00
|
|
|
case 'C':
|
|
|
|
entry_point = NULL;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
i = get_arg(argc, argv, 2, i, &logfile, 0);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
i = get_arg(argc, argv, 2, i, &entry_point, 0);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
turn = atoi(arg);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
verbosity = 0;
|
|
|
|
break;
|
2013-12-27 01:02:41 +01:00
|
|
|
case 'r':
|
|
|
|
entry_point = "run_turn";
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
turn = atoi(arg);
|
|
|
|
break;
|
2013-12-26 22:56:59 +01:00
|
|
|
case 'v':
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
verbosity = arg ? atoi(arg) : 0xff;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0], NULL);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
*exitcode = -1;
|
|
|
|
usage(argv[0], argv[i]);
|
|
|
|
return 1;
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2013-12-26 22:56:59 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2013-12-26 22:56:59 +01:00
|
|
|
|
2012-05-23 03:44:54 +02:00
|
|
|
switch (verbosity) {
|
|
|
|
case 0:
|
|
|
|
log_stderr = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
log_stderr = LOG_CPERROR;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
log_stderr = LOG_CPERROR|LOG_CPWARNING;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
log_stderr = LOG_CPERROR|LOG_CPWARNING|LOG_CPDEBUG;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_stderr = LOG_CPERROR|LOG_CPWARNING|LOG_CPDEBUG|LOG_CPINFO;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-08 09:40:42 +02:00
|
|
|
#if defined(HAVE_SIGACTION) && defined(HAVE_EXECINFO)
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2011-03-07 08:03:10 +01:00
|
|
|
static void report_segfault(int signo, siginfo_t * sinf, void *arg)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
2011-03-07 08:03:10 +01:00
|
|
|
void *btrace[50];
|
2010-08-08 09:40:42 +02:00
|
|
|
size_t size;
|
|
|
|
int fd = fileno(stderr);
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fputs("\n\nProgram received SIGSEGV, backtrace follows.\n", stderr);
|
|
|
|
size = backtrace(btrace, 50);
|
|
|
|
backtrace_symbols_fd(btrace, size, fd);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:03:10 +01:00
|
|
|
static int setup_signal_handler(void)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
act.sa_flags = SA_ONESHOT | SA_SIGINFO;
|
|
|
|
act.sa_sigaction = report_segfault;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
return sigaction(SIGSEGV, &act, NULL);
|
|
|
|
}
|
|
|
|
#else
|
2011-03-07 08:03:10 +01:00
|
|
|
static int setup_signal_handler(void)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef CRTDBG
|
|
|
|
#ifdef CRTDBG
|
|
|
|
#include <crtdbg.h>
|
2011-03-07 08:03:10 +01:00
|
|
|
void init_crtdbg(void)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
|
|
|
#if (defined(_MSC_VER))
|
|
|
|
int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
|
2011-03-07 08:03:10 +01:00
|
|
|
if (memdebug == 1) {
|
|
|
|
flags |= _CRTDBG_CHECK_ALWAYS_DF; /* expensive */
|
|
|
|
} else if (memdebug == 2) {
|
|
|
|
flags = (flags & 0x0000FFFF) | _CRTDBG_CHECK_EVERY_16_DF;
|
|
|
|
} else if (memdebug == 3) {
|
|
|
|
flags = (flags & 0x0000FFFF) | _CRTDBG_CHECK_EVERY_128_DF;
|
|
|
|
} else if (memdebug == 4) {
|
|
|
|
flags = (flags & 0x0000FFFF) | _CRTDBG_CHECK_EVERY_1024_DF;
|
2010-08-08 09:40:42 +02:00
|
|
|
}
|
|
|
|
_CrtSetDbgFlag(flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
void locale_init(void)
|
|
|
|
{
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
2011-03-07 08:03:10 +01:00
|
|
|
if (towlower(0xC4) != 0xE4) { /* Ä => ä */
|
2012-05-17 01:52:12 +02:00
|
|
|
log_error("Umlaut conversion is not working properly. Wrong locale? LANG=%s\n",
|
|
|
|
getenv("LANG"));
|
2010-08-08 09:40:42 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:03:10 +01:00
|
|
|
extern void bind_eressea(struct lua_State *L);
|
2010-08-08 09:40:42 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int main(int argc, char **argv)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
int err, result = 0;
|
2012-06-10 22:33:05 +02:00
|
|
|
lua_State *L;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2010-08-08 09:40:42 +02:00
|
|
|
setup_signal_handler();
|
2010-08-08 10:06:34 +02:00
|
|
|
parse_config(inifile);
|
2013-12-27 01:02:41 +01:00
|
|
|
log_open(logfile);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
err = parse_args(argc, argv, &result);
|
|
|
|
if (err) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-02 02:59:01 +02:00
|
|
|
locale_init();
|
|
|
|
|
2010-08-08 09:40:42 +02:00
|
|
|
#ifdef CRTDBG
|
|
|
|
init_crtdbg();
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 21:44:25 +02:00
|
|
|
L = lua_init();
|
|
|
|
game_init();
|
2010-08-08 09:40:42 +02:00
|
|
|
register_races();
|
|
|
|
register_curses();
|
|
|
|
register_spells();
|
2012-06-10 22:33:05 +02:00
|
|
|
bind_eressea(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2012-06-07 21:44:25 +02:00
|
|
|
err = eressea_run(L, luafile, entry_point);
|
2010-08-08 10:06:34 +02:00
|
|
|
if (err) {
|
2012-05-17 01:52:01 +02:00
|
|
|
log_error("server execution failed with code %d\n", err);
|
2010-08-08 10:06:34 +02:00
|
|
|
return err;
|
|
|
|
}
|
2010-08-08 09:40:42 +02:00
|
|
|
#ifdef MSPACES
|
|
|
|
malloc_stats();
|
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2012-06-07 21:44:25 +02:00
|
|
|
game_done();
|
|
|
|
lua_done(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
log_close();
|
2013-12-27 01:02:41 +01:00
|
|
|
if (global.inifile) {
|
2011-03-07 08:03:10 +01:00
|
|
|
iniparser_free(global.inifile);
|
2013-12-27 01:02:41 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|