X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fconfigfile.c;h=14464396dc7e93ff6af93396d8a0faee0e2f78bf;hb=639a12151eeb7ce0d18de330b4bb8d9d8094c4d3;hp=7428500b14ab3021ad62c1cedd0c79e65a421ca6;hpb=895ac8aaa7694f3e1759abe4de934eaafdb8d009;p=plomrogue diff --git a/src/server/configfile.c b/src/server/configfile.c index 7428500..1446439 100644 --- a/src/server/configfile.c +++ b/src/server/configfile.c @@ -1,48 +1,50 @@ /* src/server/configfile.c */ -#define _POSIX_C_SOURCE 200809L /* strdup() */ #include /* size_t, NULL */ -#include /* FILE, sprintf() */ -#include /* uint8_t, uint32_t */ +#include /* snprintf() */ +#include /* uint8_t */ #include /* atoi(), free() */ -#include /* strchr(), strcmp(), strdup(), strlen() */ -#include /* access(), F_OK */ -#include "../common/err_try_fgets.h" /* err_line(), err_try_fgets(), - * reset_err_try_fgets_counter() - */ -#include "../common/readwrite.h" /* try_fopen(),try_fclose(),textfile_width() */ -#include "../common/rexit.h" /* exit_err() */ +#include /* strcmp() */ +#include "../common/parse_file.h" /* EDIT_STARTED, parsetest_int(),parse_file(), + * parsetest_too_many_values(),parse_id_uniq() + * parse_unknown_arg(), parse_init_entry(), + * parse_and_reduce_to_readyflag(),parse_val() + */ +#include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS, * CLEANUP_MAP_OBJ_ACTS */ -#include "map_object_actions.h" /* struct MapObjAct */ -#include "map_objects.h" /* struct MapObj, struct MapObjDef */ +#include "map_object_actions.h" /* MapObjAct */ +#include "map_objects.h" /* MapObj, MapObjDef, struct MapObjDef */ #include "world.h" /* world global */ -/* Flags defining state of object and action entry reading ((un-)finished, ready - * for starting the reading of a new definition etc. +/* Flags defining state of object and action entry reading ((un-)finished / + * ready for starting the reading of a new definition etc.) */ enum flag { - EDIT_STARTED = 0x01, + HEIGHT_SET = 0x02, + WIDTH_SET = 0x04, NAME_SET = 0x02, EFFORT_SET = 0x04, CORPSE_ID_SET = 0x04, SYMBOL_SET = 0x08, LIFEPOINTS_SET = 0x10, CONSUMABLE_SET = 0x20, + START_N_SET = 0x40, READY_ACT = NAME_SET | EFFORT_SET, READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET - | CONSUMABLE_SET + | CONSUMABLE_SET | START_N_SET, + READY_MAP = HEIGHT_SET | WIDTH_SET }; -/* What MapObjDef and MapObjAct structs have in common at their top. Used to - * have common functions run over structs of both types. +/* What MapObjDef and MapObjAct structs have in common at their top. Use this to + * allow same functions run over structs of both types. */ struct EntryHead { @@ -52,55 +54,33 @@ struct EntryHead -/* Many functions working on config file lines / tokens work with these elements - * that only change on line change. Makes sense to pass them over together. - */ -struct Context { - char * line; - char * token0; - char * token1; - char * err_pre; -}; - - - -/* Return next token from "line" or NULL if none is found. Tokens either a) - * start at the first non-whitespace character and end before the next - * whitespace character after that; or b) if the first non-whitespace character - * is a single quote followed by at least one other single quote some time later - * on the line, the token starts after that first single quote and ends before - * the second, with the next token_from_line() call starting its token search - * after that second quote. The only way to return an empty string (instead of - * NULL) as a token is to delimit the token by two succeeding single quotes. - * */ -static char * token_from_line(char * line); - -/* Determines the end of the token_from_line() token. */ -static void set_token_end(char ** start, char ** limit_char); - -/* Get tokens from "context" and, by their order (in the individual context and - * in subsequent calls of this function), interpret them as data to write into - * the MapObjAct / MapObjDef DB. +/* Interpret "token0" and "token1" as data to write into the MapObjAct / + * MapObjDef DB. * * Individual MapObjDef / MapObjAct DB entries are put together line by line * before being written. Writing only happens after all necessary members of an * entry have been assembled, and when additionally a) a new entry is started by - * a context->token0 of "ACTION" or "OBJECT"; or b) a NULL context->token0 is - * passed. This is interpreted as the end of the MapObjDef / MapObjAct DB read, - * so the appropriate cleanup flags are set and test_corpse_ids() is called. + * a "token0" of "ACTION" or "OBJECT"; or b) "token0" is NULL. + * + * Also check against the line parse_file() read tokens from having more tokens. */ -static void tokens_into_entries(struct Context * context); +static void tokens_into_entries(char * token0, char * token1); -/* Start reading a new DB entry of "size" from tokens in "context" if ->token0 - * matches "comparand". Set EDIT_STARTED in "flags" to mark beginning of new - * entry reading. Check that id of new entry in ->token1 has not already been - * used in DB starting at "entry_cmp". +/* Start reading a new DB entry of "size" from tokens if "token0" matches + * "comparand". Set EDIT_STARTED in "flags" to mark beginning of new entry + * reading. Check that "token1" id of new entry has not already been used in DB + * starting at "entry_cmp". */ -static uint8_t new_entry(struct Context * context, char * comparand, - enum flag * flags, size_t size, +static uint8_t start_entry(char * token0, char * token1, char * comparand, + uint8_t * flags, size_t size, struct EntryHead ** entry, struct EntryHead * entry_cmp); +/* Start reading map definition if "token0" matches "comparand". Set "map_flags" + * to EDIT_STARTED to mark beginning of map definition reading. + */ +static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags); + /* Write DB entry pointed to by "entry" to its appropriate location. */ static void write_if_entry(struct EntryHead ** entry, struct EntryHead *** entry_p_p_p); @@ -110,27 +90,25 @@ static void write_if_entry(struct EntryHead ** entry, */ static void test_corpse_ids(); -/* Try to read tokens in "context" as members for the entry currently edited, - * which must be either "mod" or "moa". What member of which of the two is set - * depends on which of "object_flags" and "action_flags" has EDIT_STARTED set - * and on the key name of ->token0. Return 1 if interpretation succeeds, else 0. +/* set_members() helper specifically for editing world.map members. */ +static uint8_t set_map_members(char * token0,char * token1,uint8_t * map_flags); + +/* If "token0" matches "comparand", set world.enemy_fov to "token1". */ +static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1); + +/* If "token0" matches "comparand", set world.player_type to int in "token1". */ +static uint8_t set_player_type(char * token0, char * comparand, char * token1); + +/* Try to read tokens as members for the definition currently edited, which may + * be "mod" or "moa" or that of world.map. What member of which of the three is + * set depends on which of the flags has EDIT_STARTED set and on the key name in + * "token0". Return 1 if interpretation succeeds, else 0. * * Note that MapObjAct entries' .name also determines their .func. */ -static uint8_t set_members(struct Context * context, enum flag * object_flags, - enum flag * action_flags, struct MapObjDef * mod, - struct MapObjAct * moa); - -/* If "context"->token0 fits "comparand", set "element" to value read from - * ->token1 as either string (type: "s"), char ("c") or uint8 ("8"), set - * that element's flag to "flags" and return 1; else return 0. - */ -static uint8_t set_val(struct Context * context, char * comparand, - enum flag * flags, enum flag set_flag, char type, - char * element); - -/* Writes "context"->token1 to "target" only if it describes a proper uint8. */ -static void set_uint8(struct Context * context, uint8_t * target); +static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, + uint8_t * action_flags, uint8_t * map_flags, + struct MapObjDef * mod, struct MapObjAct * moa); /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct * .func from .name for set_members(). @@ -140,126 +118,86 @@ static uint8_t try_func_name(struct MapObjAct * moa, -static char * token_from_line(char * line) +static void tokens_into_entries(char * token0, char * token1) { - static char * final_char = NULL; - static char * limit_char = NULL; - char * start = limit_char + 1; - if (line) + char * str_act = "ACTION"; + char * str_obj = "OBJECT"; + char * str_map = "MAP_TYPE"; + char * str_player = "PLAYER_TYPE"; + char * str_enemyfov = "ENEMY_FOV"; + static struct MapObjAct ** moa_p_p = &world.map_obj_acts; + static struct MapObjDef ** mod_p_p = &world.map_obj_defs; + static uint8_t action_flags = READY_ACT; + static uint8_t object_flags = READY_OBJ; + static uint8_t map_flags = READY_MAP; + static struct EntryHead * moa = NULL; + static struct EntryHead * mod = NULL; + if (!token0 || !strcmp(token0, str_act) || !strcmp(token0, str_obj) + || !strcmp(token0, str_map) || !strcmp(token0, str_player) + || !strcmp(token0, str_enemyfov)) { - start = line; - limit_char = start; - final_char = &(line[strlen(line)]); - if ('\n' == *(final_char - 1)) - { - *(--final_char) = '\0'; - } + parse_and_reduce_to_readyflag(&action_flags, READY_ACT); + parse_and_reduce_to_readyflag(&object_flags, READY_OBJ); + parse_and_reduce_to_readyflag(&map_flags, READY_MAP); + write_if_entry(&moa, (struct EntryHead ***) &moa_p_p); + write_if_entry(&mod, (struct EntryHead ***) &mod_p_p); } - uint8_t empty = 1; - uint32_t i; - for (i = 0; '\0' != start[i]; i++) + if (token0) { - if (' ' != start[i] && '\t' != start[i]) + parsetest_too_many_values(); + if (start_entry(token0, token1, str_act, &action_flags, + sizeof(struct MapObjAct), (struct EntryHead**) &moa, + (struct EntryHead *) world.map_obj_acts)) { - start = &start[i]; - empty = 0; - break; + err_line(0 == atoi(token1), "Value must not be 0."); + } + else if (!( start_entry(token0, token1, str_obj, &object_flags, + sizeof(struct MapObjDef), + (struct EntryHead**) &mod, + (struct EntryHead *) world.map_obj_defs) + || start_map(token0, str_map, &map_flags) + || set_player_type(token0, str_player, token1) + || set_enemy_fov(token0, str_enemyfov, token1) + || set_members(token0, token1, &object_flags, &action_flags, + &map_flags, (struct MapObjDef *)mod, + (struct MapObjAct *) moa))) + { + parse_unknown_arg(); } } - if (empty) - { - return start = NULL; - } - set_token_end(&start, &limit_char); - return start; } -static void set_token_end(char ** start, char ** limit_char) +static uint8_t start_entry(char * token0, char * token1, char * comparand, + uint8_t * flags, size_t size, + struct EntryHead ** entry, + struct EntryHead * entry_cmp) { - char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL; - *start = (end_quote) ? *start + 1 : *start; - if (end_quote) + if (strcmp(token0, comparand)) { - *end_quote = '\0'; - *limit_char = end_quote; - return; + return 0; } - char * space = strchr(*start, ' '); - char * tab = strchr(*start, '\t'); - space = (!space || (tab && tab < space)) ? tab : space; - if (space) + *entry = (struct EntryHead *) parse_init_entry(flags, size); + parsetest_int(token1, '8'); + (*entry)-> id = atoi(token1); + for (; NULL != entry_cmp; entry_cmp = entry_cmp->next) { - * space = '\0'; + parse_id_uniq((*entry)->id == entry_cmp->id); } - *limit_char = strchr(*start, '\0'); + return 1; } -static void tokens_into_entries(struct Context * context) +static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags) { - char * str_act = "ACTION"; - char * str_obj = "OBJECT"; - static struct MapObjAct ** moa_p_p = &world.map_obj_acts; - static struct MapObjDef ** mod_p_p = &world.map_obj_defs; - static enum flag action_flags = READY_ACT; - static enum flag object_flags = READY_OBJ; - static struct EntryHead * moa = NULL; - static struct EntryHead * mod = NULL; - if ( !context->token0 - || !strcmp(context->token0,str_act) || !strcmp(context->token0,str_obj)) + if (strcmp(token0, comparand)) { - char * err_fin = "Last definition block not finished yet."; - err_line((action_flags & READY_ACT) ^ READY_ACT, - context->line, context->err_pre, err_fin); - err_line((object_flags & READY_OBJ) ^ READY_OBJ, - context->line, context->err_pre, err_fin); - write_if_entry(&moa, (struct EntryHead ***) &moa_p_p); - write_if_entry(&mod, (struct EntryHead ***) &mod_p_p); - object_flags = action_flags = READY_OBJ; - } - if (!context->token0) - { - set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS); - test_corpse_ids(); - return; - } - if (!( new_entry(context, str_act, &action_flags, - sizeof(struct MapObjAct), (struct EntryHead**) &moa, - (struct EntryHead *) world.map_obj_acts) - || new_entry(context, str_obj, &object_flags, - sizeof(struct MapObjDef), (struct EntryHead**) &mod, - (struct EntryHead *) world.map_obj_defs) - || set_members(context, &object_flags, &action_flags, - (struct MapObjDef *) mod, (struct MapObjAct *) moa))) - { - err_line(1, context->line, context->err_pre, "Unknown argument"); + return 0; } -} - - - -static uint8_t new_entry(struct Context * context, char * comparand, - enum flag * flags, size_t size, - struct EntryHead ** entry,struct EntryHead * entry_cmp) -{ - char * f_name = "new_entry()"; - char * err_uni = "Declaration of ID already used."; - if (!strcmp(context->token0, comparand)) - { - * flags = EDIT_STARTED; - * entry = try_malloc(size, f_name); - set_uint8(context, &((*entry)->id)); - for (; NULL != entry_cmp; entry_cmp = entry_cmp->next) - { - err_line((*entry)->id == entry_cmp->id, - context->line, context->err_pre, err_uni); - } - return 1; - } - return 0; + *map_flags = EDIT_STARTED; + return 1; } @@ -269,10 +207,10 @@ static void write_if_entry(struct EntryHead ** entry, { if (*entry) { - (* entry)->next = NULL; - ** entry_p_p_p = *entry; - * entry_p_p_p = &((*entry)->next); - * entry = NULL; /* So later runs of this don't re-append same entry. */ + (*entry)->next = NULL; + **entry_p_p_p = *entry; + *entry_p_p_p = &((*entry)->next); + *entry = NULL; /* So later runs of this don't re-append same entry. */ } } @@ -281,10 +219,11 @@ static void write_if_entry(struct EntryHead ** entry, static void test_corpse_ids() { char * f_name = "test_corpse_ids()"; - char * err_corpse_prefix = "In the object definition DB, one object corpse " - "ID does not reference any known object in the " - "DB. ID of responsible object: "; - char * err_corpse = try_malloc(strlen(err_corpse_prefix) + 3 + 1, f_name); + char * prefix = "In the object definitions DB, one object corpse ID does " + "not reference any known object in the DB. ID of " + "responsible object: "; + size_t size = strlen(prefix) + 3 + 1; /* 3: uint8_t representation strlen */ + char * err_corpse = try_malloc(size, f_name); struct MapObjDef * test_entry_0 = world.map_obj_defs; for (; test_entry_0; test_entry_0 = test_entry_0->next) { @@ -297,7 +236,8 @@ static void test_corpse_ids() corpse_id_found = 1; } } - sprintf(err_corpse, "%s%d", err_corpse_prefix, test_entry_0->id); + int test = snprintf(err_corpse, size, "%s%d", prefix, test_entry_0->id); + exit_trouble(test < 0, f_name, "snprintf()"); exit_err(!corpse_id_found, err_corpse); } free(err_corpse); @@ -305,100 +245,93 @@ static void test_corpse_ids() -static uint8_t set_members(struct Context * context, enum flag * object_flags, - enum flag * action_flags, struct MapObjDef * mod, - struct MapObjAct * moa) +static uint8_t set_map_members(char * token0, char * token1,uint8_t * map_flags) { - if ( * action_flags & EDIT_STARTED - && set_val(context, "NAME", action_flags, - NAME_SET, 's', (char *) &moa->name)) + if ( parse_val(token0, token1, "HEIGHT", map_flags, + HEIGHT_SET, 'i', (char *) &world.map.size.y) + || parse_val(token0, token1, "WIDTH", map_flags, + WIDTH_SET, 'i', (char *) &world.map.size.x)) { - if (!( try_func_name(moa, "move", actor_move) - || try_func_name(moa, "pick_up", actor_pick) - || try_func_name(moa, "drop", actor_drop) - || try_func_name(moa, "use", actor_use))) - { - moa->func = actor_wait; - } - *action_flags = *action_flags | NAME_SET; + int test = atoi(token1) > 256 || atoi(token1) < 1; + err_line(test, "Value must be >= 1 and <= 256."); return 1; } - else if ( set_val(context, "NAME", object_flags, - NAME_SET, 's', (char *) &mod->name) - || set_val(context, "SYMBOL", object_flags, - SYMBOL_SET, 'c', (char *) &mod->char_on_map) - || set_val(context, "EFFORT", action_flags, - EFFORT_SET, '8', (char *) &moa->effort) - || set_val(context, "LIFEPOINTS", object_flags, - LIFEPOINTS_SET, '8', (char *) &mod->lifepoints) - || set_val(context, "CONSUMABLE", object_flags, - CONSUMABLE_SET, '8', (char *) &mod->consumable) - || set_val(context, "CORPSE_ID", object_flags, - CORPSE_ID_SET, '8', (char *) &mod->corpse_id)) + return 0; +} + + + +static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1) +{ + if (strcmp(token0, comparand)) { - return 1; + return 0; } - return 0; + parsetest_int(token1, '8'); + int test = atoi(token1) > 1; + err_line(test, "Value must be 0 or 1."); + world.enemy_fov = atoi(token1); + return 1; } -static uint8_t set_val(struct Context * context, char * comparand, - enum flag * flags, enum flag set_flag, char type, - char * element) +static uint8_t set_player_type(char * token0, char * comparand, char * token1) { - char * err_out = "Outside appropriate definition's context."; - char * err_singlechar = "Value must be single ASCII character."; - if (!strcmp(context->token0, comparand)) + if (strcmp(token0, comparand)) { - err_line(!(* flags & EDIT_STARTED), - context->line, context->err_pre, err_out); - * flags = * flags | set_flag; - if ('s' == type) - { - * (char **) element = strdup(context->token1); - } - else if ('c' == type) - { - err_line(1 != strlen(context->token1), - context->line, context->err_pre, err_singlechar); - * element = (context->token1)[0]; - } - else if ('8' == type) - { - set_uint8(context, (uint8_t *) element); - } - return 1; + return 0; } - return 0; + parsetest_int(token1, '8'); + world.player_type = atoi(token1); + return 1; } -static void set_uint8(struct Context * context, uint8_t * target) +static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, + uint8_t * action_flags, uint8_t * map_flags, + struct MapObjDef * mod, struct MapObjAct * moa) { - char * err_uint8 = "Value not unsigned decimal number between 0 and 255."; - uint8_t i; - uint8_t is_uint8 = 1; - for (i = 0; '\0' != context->token1[i]; i++) + if ( *action_flags & EDIT_STARTED + && parse_val(token0, token1, "NAME", action_flags, + NAME_SET, 's', (char *) &moa->name)) { - if (i > 2 || '0' > context->token1[i] || '9' < context->token1[i]) + if (!( try_func_name(moa, "move", actor_move) + || try_func_name(moa, "pick_up", actor_pick) + || try_func_name(moa, "drop", actor_drop) + || try_func_name(moa, "use", actor_use))) { - is_uint8 = 0; + moa->func = actor_wait; } + *action_flags = *action_flags | NAME_SET; + return 1; } - if (is_uint8 && atoi(context->token1) > UINT8_MAX) + else if ( set_map_members(token0, token1, map_flags) + || parse_val(token0, token1, "NAME", object_flags, + NAME_SET, 's', (char *) &mod->name) + || parse_val(token0, token1, "SYMBOL", object_flags, + SYMBOL_SET, 'c', (char *) &mod->char_on_map) + || parse_val(token0, token1, "EFFORT", action_flags, + EFFORT_SET, '8', (char *) &moa->effort) + || parse_val(token0, token1, "START_NUMBER", object_flags, + START_N_SET, '8', (char *) &mod->start_n) + || parse_val(token0, token1, "LIFEPOINTS", object_flags, + LIFEPOINTS_SET, '8', (char *) &mod->lifepoints) + || parse_val(token0, token1, "CONSUMABLE", object_flags, + CONSUMABLE_SET, '8', (char *) &mod->consumable) + || parse_val(token0, token1, "CORPSE_ID", object_flags, + CORPSE_ID_SET, '8', (char *) &mod->corpse_id)) { - is_uint8 = 0; + return 1; } - err_line(!(is_uint8), context->line, context->err_pre, err_uint8); - *target = atoi(context->token1); + return 0; } -static uint8_t try_func_name(struct MapObjAct * moa, - char * name, void (* func) (struct MapObj *)) +static uint8_t try_func_name(struct MapObjAct * moa, char * name, + void (* func) (struct MapObj *)) { if (0 == strcmp(moa->name, name)) { @@ -412,40 +345,19 @@ static uint8_t try_func_name(struct MapObjAct * moa, extern void read_config_file() { - char * f_name = "read_new_config_file()"; - char * path = world.path_config; - struct Context context; - char * err_pre_prefix = "Failed reading config file: \""; - char * err_pre_affix = "\". "; - context.err_pre = try_malloc(strlen(err_pre_prefix) + strlen(path) - + strlen(err_pre_affix) + 1, f_name); - sprintf(context.err_pre, "%s%s%s", err_pre_prefix, path, err_pre_affix); - exit_err(access(path, F_OK), context.err_pre); - FILE * file = try_fopen(path, "r", f_name); - uint32_t linemax = textfile_width(file); - context.line = try_malloc(linemax + 1, f_name); - reset_err_try_fgets_counter(); - err_line(0 == linemax, context.line, context.err_pre, "File is empty."); - context.token0 = NULL; /* For tokens_into_entries() if while() stagnates. */ - char * err_val = "No value given."; - char * err_many = "Too many values."; - while (err_try_fgets(context.line, linemax + 1, file, context.err_pre, "")) + parse_file(world.path_config, tokens_into_entries); + exit_err(!world.map.size.y, "Map not defined in config file."); + uint8_t player_type_is_valid = 0; + struct MapObjDef * mod; + for (mod = world.map_obj_defs; NULL != mod; mod = mod->next) { - char * line_copy = strdup(context.line); - context.token0 = token_from_line(line_copy); - if (context.token0) + if (world.player_type == mod->id) { - err_line(0 == (context.token1 = token_from_line(NULL)), - context.line, context.err_pre, err_val); - err_line(NULL != token_from_line(NULL), - context.line, context.err_pre, err_many); - tokens_into_entries(&context); - context.token0 = NULL; + player_type_is_valid = 1; + break; } - free(line_copy); } - tokens_into_entries(&context); - try_fclose(file, f_name); - free(context.line); - free(context.err_pre); + exit_err(!player_type_is_valid, "No valid map object type set for player."); + set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS); + test_corpse_ids(); }