allow reading the configuration from a different directory (-c dir).

This commit is contained in:
Enno Rehling 2014-07-23 08:10:14 +02:00
parent af73c6dcdf
commit 51207167c8
3 changed files with 3869 additions and 3741 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -41,7 +41,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <wctype.h> #include <wctype.h>
#include <iniparser.h> #include <iniparser.h>
static const char *logfile= "eressea.log"; extern const char *confpath;
static const char *logfile = "eressea.log";
static const char *luafile = 0; static const char *luafile = 0;
static const char *inifile = "eressea.ini"; static const char *inifile = "eressea.ini";
static int memdebug = 0; static int memdebug = 0;
@ -60,8 +61,9 @@ static void parse_config(const char *filename)
#endif #endif
/* excerpt from [config] (the rest is used in bindings.c) */ /* excerpt from [config] (the rest is used in bindings.c) */
game_name = iniparser_getstring(d, "config:game", game_name); game_name = iniparser_getstring(d, "config:game", game_name);
} else { }
log_warning("could not open configuration file %s\n", filename); else {
log_error("could not open configuration file %s\n", filename);
} }
global.inifile = d; global.inifile = d;
} }
@ -83,12 +85,12 @@ static int usage(const char *prog, const char *arg)
static int get_arg(int argc, char **argv, size_t len, int index, const char **result, const char *def) { static int get_arg(int argc, char **argv, size_t len, int index, const char **result, const char *def) {
if (argv[index][len]) { if (argv[index][len]) {
*result = argv[index]+len; *result = argv[index] + len;
return index; return index;
} }
if (index+1 < argc) { if (index + 1 < argc) {
*result = argv[index+1]; *result = argv[index + 1];
return index+1; return index + 1;
} }
*result = def; *result = def;
return index; return index;
@ -101,25 +103,33 @@ static int parse_args(int argc, char **argv, int *exitcode)
for (i = 1; i != argc; ++i) { for (i = 1; i != argc; ++i) {
if (argv[i][0] != '-') { if (argv[i][0] != '-') {
luafile = argv[i]; luafile = argv[i];
} else if (argv[i][1] == '-') { /* long format */ }
else if (argv[i][1] == '-') { /* long format */
if (strcmp(argv[i] + 2, "version") == 0) { if (strcmp(argv[i] + 2, "version") == 0) {
printf("\n%s PBEM host\n" printf("\n%s PBEM host\n"
"Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n" "Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n"
"Compilation: " __DATE__ " at " __TIME__ "\nVersion: %d.%d.%d\n\n", "Compilation: " __DATE__ " at " __TIME__ "\nVersion: %d.%d.%d\n\n",
global.gamename, VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD); global.gamename, VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD);
#ifdef USE_CURSES #ifdef USE_CURSES
} else if (strcmp(argv[i] + 2, "color") == 0) { }
else if (strcmp(argv[i] + 2, "color") == 0) {
/* force the editor to have colors */ /* force the editor to have colors */
force_color = 1; force_color = 1;
#endif #endif
} else if (strcmp(argv[i] + 2, "help") == 0) { }
else if (strcmp(argv[i] + 2, "help") == 0) {
return usage(argv[0], NULL); return usage(argv[0], NULL);
} else { }
else {
return usage(argv[0], argv[i]); return usage(argv[0], argv[i]);
} }
} else { }
else {
const char *arg; const char *arg;
switch (argv[i][1]) { switch (argv[i][1]) {
case 'c':
i = get_arg(argc, argv, 2, i, &confpath, 0);
break;
case 'f': case 'f':
i = get_arg(argc, argv, 2, i, &luafile, 0); i = get_arg(argc, argv, 2, i, &luafile, 0);
break; break;
@ -160,13 +170,13 @@ static int parse_args(int argc, char **argv, int *exitcode)
log_stderr = LOG_CPERROR; log_stderr = LOG_CPERROR;
break; break;
case 2: case 2:
log_stderr = LOG_CPERROR|LOG_CPWARNING; log_stderr = LOG_CPERROR | LOG_CPWARNING;
break; break;
case 3: case 3:
log_stderr = LOG_CPERROR|LOG_CPWARNING|LOG_CPDEBUG; log_stderr = LOG_CPERROR | LOG_CPWARNING | LOG_CPDEBUG;
break; break;
default: default:
log_stderr = LOG_CPERROR|LOG_CPWARNING|LOG_CPDEBUG|LOG_CPINFO; log_stderr = LOG_CPERROR | LOG_CPWARNING | LOG_CPDEBUG | LOG_CPINFO;
break; break;
} }
@ -241,17 +251,27 @@ extern void bind_monsters(struct lua_State *L);
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int err, result = 0; char inipath[MAX_PATH];
int err = 0;
lua_State *L; lua_State *L;
setup_signal_handler(); setup_signal_handler();
parse_config(inifile);
log_open(logfile);
err = parse_args(argc, argv, &result); /* parse args once to read config file location */
if (err) { if (parse_args(argc, argv, &err) != 0) {
return result; return err;
} }
/* ini file sets defaults for arguments*/
if (confpath) {
_snprintf(inipath, sizeof(inipath), "%s/%s", confpath, inifile);
parse_config(inipath);
}
else {
parse_config(inifile);
}
/* parse arguments again, to override ini file */
parse_args(argc, argv, &err);
log_open(logfile);
locale_init(); locale_init();
#ifdef CRTDBG #ifdef CRTDBG