argument parsing

fix crash when ct_flyingship is missing
This commit is contained in:
Enno Rehling 2010-02-21 21:54:33 +00:00
parent e25a2e3884
commit e81a47cb31
5 changed files with 84 additions and 12 deletions

View file

@ -4,13 +4,14 @@
#include <eressea.h>
#include <gmtool.h>
#include <kernel/config.h>
#include <kernel/save.h>
#include <iniparser/iniparser.h>
static const char * luafile = "init.lua";
static const char * entry_point = NULL;
static int memdebug = 0;
static void load_config(const char * filename)
static void parse_config(const char * filename)
{
dictionary * d = iniparser_new(filename);
if (d) {
@ -26,12 +27,84 @@ static void load_config(const char * filename)
global.inifile = d;
}
static int
usage(const char * prog, const char * arg)
{
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"
"--color : force curses to use colors even when not detected\n"
"--help : help\n", prog);
return -1;
}
static int
parse_args(int argc, char **argv)
{
int i;
for (i=1;i!=argc;++i) {
if (argv[i][0]!='-') {
return usage(argv[0], argv[i]);
} else if (argv[i][1]=='-') { /* long format */
if (strcmp(argv[i]+2, "version")==0) {
printf("\n%s PBEM host\n"
"Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n"
"Compilation: " __DATE__ " at " __TIME__ "\nVersion: %f\n\n", global.gamename, version());
}
else if (strcmp(argv[i]+2, "color")==0) {
/* force the editor to have colors */
force_color = 1;
}
else if (strcmp(argv[i]+2, "help")==0) {
return usage(argv[0], NULL);
}
else {
return usage(argv[0], argv[i]);
}
} else switch(argv[i][1]) {
case 'C':
entry_point = NULL;
break;
case 'e':
entry_point = argv[++i];
break;
case 't':
turn = atoi(argv[++i]);
break;
case 'q':
verbosity = 0;
break;
case 'v':
verbosity = atoi(argv[++i]);
break;
case 'h':
return usage(argv[0], NULL);
default:
return usage(argv[0], argv[i]);
}
}
return 0;
}
int main(int argc, char ** argv)
{
int err;
log_open("eressea.log");
load_config("eressea.ini");
parse_config("eressea.ini");
err = parse_args(argc, argv);
if (err) {
return err;
}
err = eressea_init();
if (err) {
@ -46,5 +119,6 @@ int main(int argc, char ** argv)
}
eressea_done();
log_close();
return 0;
}

View file

@ -187,7 +187,6 @@ void eressea_done(void)
game_done();
kernel_done();
lua_done((lua_State *)global.vm_state);
log_close();
}
static int

View file

@ -217,7 +217,7 @@ curse_read(attrib * a, struct storage * store)
if (result!=0) {
log_error(("missing curse %s, no compatibility code either.\n", cursename));
}
assert(result!=0);
assert(result==0);
return AT_READ_FAIL;
}
if (store->version < CURSEFLAGS_VERSION) {

View file

@ -687,12 +687,15 @@ ship_allowed(const struct ship * sh, const region * r)
static boolean
flying_ship(const ship * sh)
{
static int init = 0;
static const curse_type * ct_flyingship;
if (!ct_flyingship) {
ct_flyingship = ct_find("flyingship");
assert(ct_flyingship);
}
if (sh->type->flags & SFL_FLY) return true;
if (!init) {
ct_flyingship = ct_find("flyingship");
init = 1;
}
if (!ct_flyingship) return false;
if (curse_active(get_curse(sh->attribs, ct_flyingship))) return true;
return false;
}

View file

@ -154,7 +154,6 @@ static boolean g_ignore_errors = false;
static const char * luafile = "init.lua";
static const char * entry_point = NULL;
static int memdebug = 0;
static int g_console = 1;
#if defined(HAVE_SIGACTION) && defined(HAVE_EXECINFO)
#include <execinfo.h>
#include <signal.h>
@ -302,14 +301,12 @@ read_args(int argc, char **argv, lua_State * luaState)
return usage(argv[0], argv[i]);
} else switch(argv[i][1]) {
case 'C':
g_console = 1;
entry_point = NULL;
break;
case 'R':
g_reportdir = argv[++i];
break;
case 'e':
g_console = 0;
entry_point = argv[++i];
break;
case 'd':
@ -322,7 +319,6 @@ read_args(int argc, char **argv, lua_State * luaState)
game_name = argv[++i];
break;
case 't':
g_console = 0;
turn = atoi(argv[++i]);
break;
case 'q':