2010-08-08 10:06:34 +02:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2014-07-23 08:10:14 +02:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
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>
|
2016-09-10 17:41:44 +02:00
|
|
|
#include <kernel/version.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/save.h>
|
2014-12-09 06:01:10 +01:00
|
|
|
#include <util/filereader.h>
|
|
|
|
#include <util/language.h>
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "eressea.h"
|
2014-06-12 05:30:34 +02:00
|
|
|
#ifdef USE_CURSES
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "gmtool.h"
|
2014-06-12 05:30:34 +02:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-02-22 09:30:20 +01:00
|
|
|
#include "bindings.h"
|
2010-08-08 09:40:42 +02:00
|
|
|
#include "races/races.h"
|
2014-08-27 21:09:39 +02:00
|
|
|
#include "spells.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
|
|
|
|
2014-07-23 08:10:14 +02:00
|
|
|
static const char *logfile = "eressea.log";
|
2014-02-17 20:51:57 +01:00
|
|
|
static const char *luafile = 0;
|
2011-03-07 08:02:35 +01:00
|
|
|
static const char *inifile = "eressea.ini";
|
2010-08-08 10:06:34 +02:00
|
|
|
static int memdebug = 0;
|
2015-09-12 20:58:09 +02:00
|
|
|
static int verbosity = 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-03-12 21:03:38 +01:00
|
|
|
static void load_inifile(void)
|
2014-12-09 06:01:10 +01:00
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2017-03-12 21:03:38 +01:00
|
|
|
str = config_get("game.base");
|
|
|
|
if (str) {
|
2014-12-09 06:01:10 +01:00
|
|
|
set_basepath(str);
|
|
|
|
}
|
2017-03-12 21:03:38 +01:00
|
|
|
str = config_get("game.report");
|
|
|
|
if (str) {
|
2014-12-09 06:01:10 +01:00
|
|
|
set_reportpath(str);
|
|
|
|
}
|
2017-03-12 21:03:38 +01:00
|
|
|
str = config_get("game.data");
|
|
|
|
if (str) {
|
2014-12-09 06:01:10 +01:00
|
|
|
set_datapath(str);
|
|
|
|
}
|
|
|
|
|
2017-03-12 21:03:38 +01:00
|
|
|
lomem = config_get_int("game.lomem", lomem) ? 1 : 0;
|
|
|
|
verbosity = config_get_int("game.verbose", 2);
|
|
|
|
memdebug = config_get_int("game.memcheck", memdebug);
|
2017-03-12 19:48:44 +01:00
|
|
|
#ifdef USE_CURSES
|
|
|
|
/* only one value in the [editor] section */
|
2017-03-12 21:03:38 +01:00
|
|
|
force_color = config_get_int("editor.color", force_color);
|
|
|
|
gm_codepage = config_get_int("editor.codepage", gm_codepage);
|
2017-03-12 19:48:44 +01:00
|
|
|
#endif
|
2014-12-09 06:01:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-12 21:03:38 +01:00
|
|
|
static const char * valid_keys[] = {
|
|
|
|
"game.id",
|
|
|
|
"game.name",
|
|
|
|
"game.locale",
|
|
|
|
"game.verbose",
|
|
|
|
"game.report",
|
|
|
|
"game.lomem",
|
|
|
|
"game.memcheck",
|
|
|
|
"game.email",
|
|
|
|
"game.mailcmd",
|
2017-03-25 14:15:01 +01:00
|
|
|
"game.era",
|
2017-03-12 21:03:38 +01:00
|
|
|
"game.sender",
|
|
|
|
"editor.color",
|
|
|
|
"editor.codepage",
|
2017-03-25 14:15:01 +01:00
|
|
|
"editor.population.",
|
2017-03-12 21:03:38 +01:00
|
|
|
"lua.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
static dictionary *parse_config(const char *filename)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-02-02 10:10:21 +01:00
|
|
|
char path[MAX_PATH];
|
|
|
|
dictionary *d;
|
2017-02-12 05:52:46 +01:00
|
|
|
const char *str, *cfgpath = config_get("config.path");
|
2017-02-02 10:10:21 +01:00
|
|
|
|
|
|
|
if (cfgpath) {
|
|
|
|
join_path(cfgpath, filename, path, sizeof(path));
|
|
|
|
log_debug("reading from configuration file %s\n", path);
|
|
|
|
d = iniparser_load(path);
|
|
|
|
} else {
|
|
|
|
log_debug("reading from configuration file %s\n", filename);
|
|
|
|
d = iniparser_load(filename);
|
|
|
|
}
|
2014-07-23 08:10:14 +02:00
|
|
|
if (d) {
|
2017-03-12 21:03:38 +01:00
|
|
|
config_set_from(d, valid_keys);
|
|
|
|
load_inifile();
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
2017-02-12 05:52:46 +01:00
|
|
|
str = config_get("game.locales");
|
|
|
|
make_locales(str ? str : "de,en");
|
2017-01-21 19:53:47 +01:00
|
|
|
return d;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-23 08:10:14 +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"
|
|
|
|
"-f <script.lua> : execute a lua script\n"
|
|
|
|
"-q : be quite (same as -v 0)\n"
|
|
|
|
"-v <level> : verbosity level\n"
|
|
|
|
"-C : run in interactive mode\n"
|
|
|
|
"--color : force curses to use colors even when not detected\n", prog);
|
|
|
|
return -1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2014-07-23 08:10:14 +02:00
|
|
|
if (argv[index][len]) {
|
|
|
|
*result = argv[index] + len;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
if (index + 1 < argc) {
|
|
|
|
*result = argv[index + 1];
|
|
|
|
return index + 1;
|
|
|
|
}
|
|
|
|
*result = def;
|
2013-12-26 22:56:59 +01:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2016-05-20 20:55:27 +02:00
|
|
|
static int verbosity_to_flags(int verbosity) {
|
|
|
|
int flags = 0;
|
|
|
|
switch (verbosity) {
|
|
|
|
case 0:
|
|
|
|
flags = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
flags = LOG_CPERROR;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
flags = LOG_CPERROR | LOG_CPWARNING;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
flags = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
flags = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO | LOG_CPDEBUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-08-05 22:15:41 +02:00
|
|
|
int i;
|
|
|
|
int log_stderr = LOG_CPERROR;
|
|
|
|
int log_flags = LOG_CPERROR | LOG_CPWARNING | LOG_CPINFO;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 08:10:14 +02:00
|
|
|
for (i = 1; i != argc; ++i) {
|
2015-07-12 10:35:09 +02:00
|
|
|
char *argi = argv[i];
|
|
|
|
if (argi[0] != '-') {
|
|
|
|
luafile = argi;
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
2015-07-12 10:35:09 +02:00
|
|
|
else if (argi[1] == '-') { /* long format */
|
|
|
|
if (strcmp(argi + 2, "version") == 0) {
|
2014-07-23 08:10:14 +02:00
|
|
|
printf("\n%s PBEM host\n"
|
|
|
|
"Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n"
|
2016-09-10 17:41:44 +02:00
|
|
|
"Compilation: " __DATE__ " at " __TIME__ "\nVersion: %s\n\n",
|
2016-09-15 20:11:38 +02:00
|
|
|
game_name(), eressea_version());
|
2014-06-12 05:30:34 +02:00
|
|
|
#ifdef USE_CURSES
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
2015-07-12 10:35:09 +02:00
|
|
|
else if (strcmp(argi + 2, "color") == 0) {
|
2014-07-23 08:10:14 +02:00
|
|
|
/* force the editor to have colors */
|
|
|
|
force_color = 1;
|
2014-06-12 05:30:34 +02:00
|
|
|
#endif
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
2015-07-12 10:35:09 +02:00
|
|
|
else if (strcmp(argi + 2, "help") == 0) {
|
2014-07-23 08:10:14 +02:00
|
|
|
return usage(argv[0], NULL);
|
|
|
|
}
|
|
|
|
else {
|
2015-07-12 10:35:09 +02:00
|
|
|
return usage(argv[0], argi);
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *arg;
|
2015-07-12 10:35:09 +02:00
|
|
|
switch (argi[1]) {
|
2017-02-02 10:10:21 +01:00
|
|
|
case 'c':
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
config_set("config.path", arg);
|
|
|
|
break;
|
2017-02-02 19:30:29 +01:00
|
|
|
case 'r':
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
config_set("config.rules", arg);
|
|
|
|
break;
|
2014-07-23 08:10:14 +02:00
|
|
|
case 'f':
|
|
|
|
i = get_arg(argc, argv, 2, i, &luafile, 0);
|
|
|
|
break;
|
|
|
|
case 'l':
|
2016-05-20 20:55:27 +02:00
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
log_flags = arg ? atoi(arg) : 0xff;
|
2014-07-23 08:10:14 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
i = get_arg(argc, argv, 2, i, &arg, 0);
|
|
|
|
turn = atoi(arg);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
verbosity = 0;
|
|
|
|
break;
|
|
|
|
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;
|
2015-07-12 10:35:09 +02:00
|
|
|
usage(argv[0], argi);
|
2014-07-23 08:10:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 21:15:14 +01:00
|
|
|
/* open logfile on disk: */
|
2016-05-20 20:55:27 +02:00
|
|
|
log_flags = verbosity_to_flags(log_flags);
|
|
|
|
log_open(logfile, log_flags);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-02-18 21:15:14 +01:00
|
|
|
/* also log to stderr: */
|
2016-05-20 20:55:27 +02:00
|
|
|
log_stderr = verbosity_to_flags(verbosity);
|
2016-01-31 19:54:49 +01:00
|
|
|
if (log_stderr) {
|
2016-02-01 09:26:24 +01:00
|
|
|
log_to_file(log_stderr | LOG_FLUSH | LOG_BRIEF, stderr);
|
2016-01-31 19:54:49 +01:00
|
|
|
}
|
2014-07-23 08:10:14 +02:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-23 08:10:14 +02:00
|
|
|
void *btrace[50];
|
|
|
|
size_t size;
|
|
|
|
int fd = fileno(stderr);
|
2010-08-08 09:40:42 +02:00
|
|
|
|
2014-07-23 08:10:14 +02:00
|
|
|
fflush(stdout);
|
|
|
|
fputs("\n\nProgram received SIGSEGV, backtrace follows.\n", stderr);
|
|
|
|
size = backtrace(btrace, 50);
|
|
|
|
backtrace_symbols_fd(btrace, size, fd);
|
|
|
|
abort();
|
2010-08-08 09:40:42 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:03:10 +01:00
|
|
|
static int setup_signal_handler(void)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
2014-07-23 08:10:14 +02:00
|
|
|
struct sigaction act;
|
2010-08-08 09:40:42 +02:00
|
|
|
|
2014-07-23 08:10:14 +02:00
|
|
|
act.sa_flags = SA_ONESHOT | SA_SIGINFO;
|
|
|
|
act.sa_sigaction = report_segfault;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
return sigaction(SIGSEGV, &act, NULL);
|
2010-08-08 09:40:42 +02:00
|
|
|
}
|
|
|
|
#else
|
2011-03-07 08:03:10 +01:00
|
|
|
static int setup_signal_handler(void)
|
2010-08-08 09:40:42 +02:00
|
|
|
{
|
2014-07-23 08:10:14 +02:00
|
|
|
return 0;
|
2010-08-08 09:40:42 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
void locale_init(void)
|
|
|
|
{
|
2014-07-23 08:10:14 +02:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
if (towlower(0xC4) != 0xE4) { /* Ä => ä */
|
|
|
|
log_error("Umlaut conversion is not working properly. Wrong locale? LANG=%s\n",
|
|
|
|
getenv("LANG"));
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-02-18 05:45:00 +01:00
|
|
|
extern void bind_monsters(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
|
|
|
{
|
2014-07-23 08:10:14 +02:00
|
|
|
int err = 0;
|
|
|
|
lua_State *L;
|
2017-02-02 10:10:21 +01:00
|
|
|
dictionary *d = 0;
|
2014-07-23 08:10:14 +02:00
|
|
|
setup_signal_handler();
|
2017-02-02 10:10:21 +01:00
|
|
|
/* parse arguments again, to override ini file */
|
|
|
|
parse_args(argc, argv, &err);
|
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
d = parse_config(inifile);
|
|
|
|
if (!d) {
|
2014-07-23 11:54:51 +02:00
|
|
|
log_error("could not open ini configuration %s\n", inifile);
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 08:10:14 +02:00
|
|
|
locale_init();
|
2012-06-02 02:59:01 +02:00
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
L = lua_init(d);
|
2014-07-23 08:10:14 +02:00
|
|
|
game_init();
|
|
|
|
bind_monsters(L);
|
|
|
|
err = eressea_run(L, luafile);
|
|
|
|
if (err) {
|
2014-11-06 07:53:15 +01:00
|
|
|
log_error("script %s failed with code %d\n", luafile, err);
|
2014-07-23 08:10:14 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
game_done();
|
|
|
|
lua_done(L);
|
|
|
|
log_close();
|
2017-01-21 19:53:47 +01:00
|
|
|
if (d) {
|
|
|
|
iniparser_freedict(d);
|
2014-07-23 08:10:14 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|