From: Christian Heller Date: Thu, 3 Apr 2014 02:50:36 +0000 (+0200) Subject: Make client's commandDB reading use new parsing / config file format. X-Git-Tag: tce~784 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/pick_tasks?a=commitdiff_plain;h=6245b251886853269262bc77d0f0378c28e241bc;p=plomrogue Make client's commandDB reading use new parsing / config file format. Those new parsing methods are outsourced from src/server/configfile.c to src/common/parse_file.c. CommandDB file parsing now also checks against doubly declared commands. --- diff --git a/src/client/command_db.c b/src/client/command_db.c index 06329fc..a37d0d5 100644 --- a/src/client/command_db.c +++ b/src/client/command_db.c @@ -1,13 +1,15 @@ /* src/client/command_db.c */ +#define _POSIX_C_SOURCE 200809L /* strdup() */ #include "command_db.h" #include /* NULL */ -#include /* uint8_t, uint32_t */ -#include /* FILE, sprintf() */ +#include /* uint8_t */ #include /* free() */ -#include /* memset(), strlen(), strcmp() */ -#include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */ -#include "../common/readwrite.h" /* try_fopen(),try_fclose(),textfile_width() */ +#include /* memset(), strcmp(), strdup() */ +#include "../common/err_try_fgets.h" /* err_line() */ +#include "../common/parse_file.h" /* Context, EDIT_STARTED, parse_file(), + * set_val() + */ #include "../common/rexit.h" /* exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "array_append.h" /* array_append() */ @@ -16,13 +18,71 @@ -/* Helper to init_command_db(). */ -static void write_line_to_target(char ** target, char * line) +/* Flags for defining state of command DB config file entries. */ +enum cmd_flag { - char * f_name = "write_line_to_target()"; - *target = try_malloc(strlen(line), f_name); - line[strlen(line) - 1] = '\0'; - sprintf(*target, "%s", line); + DESC_SET = 0x02, + SERVERCMD_SET = 0x04, + SERVERARG_SET = 0x08, + READY_CMD = DESC_SET +}; + + + +/* 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 CommandDB. + * + * Individual CommandDB entries are put together line by line before being + * written. Writing 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 "COMMAND"; or b) a NULL context->token0 is passed. This is + * interpreted as the CommandDB read's end: appropriate cleanup flags are set. + */ +static void tokens_into_entries(struct Context * context); + + + +static void tokens_into_entries(struct Context * context) +{ + char * f_name = "tokens_into_entries()"; + char * str_cmd = "COMMAND"; + static uint8_t cmd_flags = READY_CMD; + static struct Command * cmd = NULL; + if (!context->token0 || !strcmp(context->token0, str_cmd)) + { + err_line((cmd_flags & READY_CMD) ^ READY_CMD, context->line, + context->err_pre, "Last definition block not finished yet."); + if (cmd) + { + array_append(world.commandDB.n, sizeof(struct Command), + (void *) cmd, (void **) &world.commandDB.cmds); + world.commandDB.n++; + cmd = NULL; + } + } + if (!context->token0) + { + set_cleanup_flag(CLEANUP_COMMANDS); + } + else if (!strcmp(context->token0, str_cmd)) + { + cmd_flags = EDIT_STARTED; + cmd = try_malloc(sizeof(struct Command), f_name); + memset(cmd, 0, sizeof(struct Command)); + cmd->dsc_short = strdup(context->token1); + err_line(NULL != get_command(cmd->dsc_short), context->line, + context->err_pre, "Declaration of ID already used."); + } + else if (!( set_val(context, "DESCRIPTION", &cmd_flags, + DESC_SET, 's', (char *) &cmd->dsc_long) + || set_val(context, "SERVER_COMMAND", &cmd_flags, + SERVERCMD_SET, 's', (char *) &cmd->server_msg) + || set_val(context, "SERVER_ARGUMENT", &cmd_flags, + SERVERARG_SET, 'c', (char *) &cmd->arg))) + { + err_line(1, context->line, context->err_pre, "Unknown argument"); + } } @@ -47,42 +107,7 @@ extern struct Command * get_command(char * dsc_short) extern void init_command_db() { - char * f_name = "init_command_db()"; - char * context = "Failed reading command DB file. "; - FILE * file = try_fopen(world.path_commands, "r", f_name); - uint32_t linemax = textfile_width(file); - char line[linemax + 1]; - reset_err_try_fgets_counter(); - uint8_t i = 0; - while (1) - { - int test_for_end = try_fgetc(file, f_name); - if (EOF == test_for_end || '\n' == test_for_end) - { - break; - } - exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()"); - struct Command cmd; - memset(&cmd, 0, sizeof(struct Command)); - err_try_fgets(line, linemax, file, context, "nf"); - write_line_to_target(&cmd.dsc_short, line); - err_try_fgets(line, linemax, file, context, "0nf"); - write_line_to_target(&cmd.dsc_long, line); - err_try_fgets(line, linemax, file, context, "0nf"); - if (strcmp(world.delim, line)) - { - write_line_to_target(&cmd.server_msg, line); - err_try_fgets(line, linemax, file, context, "0nfs"); - cmd.arg = line[0]; - err_try_fgets(line, linemax, file, context, "d"); - } - array_append(i, sizeof(struct Command), (void *) &cmd, - (void **) &world.commandDB.cmds); - i++; - } - try_fclose(file, f_name); - world.commandDB.n = i; - set_cleanup_flag(CLEANUP_COMMANDS); + parse_file("confclient/new_commands", tokens_into_entries); } diff --git a/src/client/command_db.h b/src/client/command_db.h index e20d16d..bed89c4 100644 --- a/src/client/command_db.h +++ b/src/client/command_db.h @@ -33,7 +33,7 @@ struct CommandDB */ extern struct Command * get_command(char * dsc_short); -/* Reads in CommandDB config file line by line until end or first empty line. */ +/* Reads in CommandDB config file. */ extern void init_command_db(); /* Free all memory allocated with init_command_db. */ diff --git a/src/common/parse_file.c b/src/common/parse_file.c new file mode 100644 index 0000000..d6c6949 --- /dev/null +++ b/src/common/parse_file.c @@ -0,0 +1,185 @@ +/* src/common/parse_file.c */ + +#define _POSIX_C_SOURCE 200809L /* strdup() */ +#include "parse_file.h" +#include /* NULL */ // size_t +#include /* FILE, sprintf() */ +#include /* uint8_t, uint32_t */ +#include /* free() */ // atoi() +#include /* strchr, strdup(), strlen() */ // strcmp() +#include /* access(), F_OK */ +#include "err_try_fgets.h" /* err_line(), err_try_fgets(), + * reset_err_try_fgets_counter() + */ +#include "readwrite.h" /* try_fopen(),try_fclose(), textfile_width() */ +#include "rexit.h" /* exit_err() */ +#include "try_malloc.h" /* try_malloc() */ + + + +/* 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); + + + +static char * token_from_line(char * line) +{ + static char * final_char = NULL; + static char * limit_char = NULL; + char * start = limit_char + 1; + if (line) + { + start = line; + limit_char = start; + final_char = &(line[strlen(line)]); + if ('\n' == *(final_char - 1)) + { + *(--final_char) = '\0'; + } + } + uint8_t empty = 1; + uint32_t i; + for (i = 0; '\0' != start[i]; i++) + { + if (' ' != start[i] && '\t' != start[i]) + { + start = &start[i]; + empty = 0; + break; + } + } + if (empty) + { + return start = NULL; + } + set_token_end(&start, &limit_char); + return start; +} + + + +static void set_token_end(char ** start, char ** limit_char) +{ + char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL; + *start = (end_quote) ? *start + 1 : *start; + if (end_quote) + { + *end_quote = '\0'; + *limit_char = end_quote; + return; + } + char * space = strchr(*start, ' '); + char * tab = strchr(*start, '\t'); + space = (!space || (tab && tab < space)) ? tab : space; + if (space) + { + * space = '\0'; + } + *limit_char = strchr(*start, '\0'); +} + + + +extern void set_uint8(struct Context * context, uint8_t * target) +{ + 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 (i > 2 || '0' > context->token1[i] || '9' < context->token1[i]) + { + is_uint8 = 0; + } + } + if (is_uint8 && atoi(context->token1) > UINT8_MAX) + { + is_uint8 = 0; + } + err_line(!(is_uint8), context->line, context->err_pre, err_uint8); + *target = atoi(context->token1); +} + + + +extern uint8_t set_val(struct Context * context, char * comparand, + uint8_t * flags, uint8_t set_flag, char type, + char * element) +{ + char * err_out = "Outside appropriate definition's context."; + char * err_singlechar = "Value must be single ASCII character."; + if (!strcmp(context->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; +} + + + +extern void parse_file(char * path, void (* token_to_entry) (struct Context *)) +{ + char * f_name = "read_new_config_file()"; + 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 token_to_entry() 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, "")) + { + char * line_copy = strdup(context.line); + context.token0 = token_from_line(line_copy); + if (context.token0) + { + 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); + token_to_entry(&context); + context.token0 = NULL; + } + free(line_copy); + } + token_to_entry(&context); + try_fclose(file, f_name); + free(context.line); + free(context.err_pre); +} diff --git a/src/common/parse_file.h b/src/common/parse_file.h new file mode 100644 index 0000000..28865c4 --- /dev/null +++ b/src/common/parse_file.h @@ -0,0 +1,53 @@ +/* src/common/parse_file.h + * + * Tools for parsing config files. + */ + +#ifndef PARSE_FILE_H +#define PARSE_FILE_H + +#include /* uint8_t */ + + + +/* 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; +}; + + + +enum parse_flags +{ + EDIT_STARTED = 0x01 +}; + + + +/* Writes "context"->token1 to "target" only if it describes a proper uint8. */ +extern void set_uint8(struct Context * context, uint8_t * target); + +/* 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. + */ +extern uint8_t set_val(struct Context * context, char * comparand, + uint8_t * flags, uint8_t set_flag, char type, + char * element); + +/* Parse file at "path" by passing each line's tokens to "token_to_entry" + * encapsulated into "Context". Empty lines are ignored. Non-empty lines have to + * feature exactly two tokens as delimited either be whitespace or single quotes + * (to allow for tokens featuring whitespaces). When EOF is reached, + * token_to_entry() is called a last time with an empty Context.token0. + */ +extern void parse_file(char * path, void ( *token_to_entry) (struct Context *)); + + + +#endif diff --git a/src/server/configfile.c b/src/server/configfile.c index 7428500..f3d831e 100644 --- a/src/server/configfile.c +++ b/src/server/configfile.c @@ -1,16 +1,14 @@ /* src/server/configfile.c */ -#define _POSIX_C_SOURCE 200809L /* strdup() */ #include /* size_t, NULL */ -#include /* FILE, sprintf() */ -#include /* uint8_t, uint32_t */ +#include /* sprintf() */ +#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 /* strcmp() */ +#include "../common/err_try_fgets.h" /* err_line() */ +#include "../common/parse_file.h" /* Context, EDIT_STARTED, set_val(), + * set_uint8(), parse_file() + */ #include "../common/rexit.h" /* exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS, @@ -22,12 +20,11 @@ -/* 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, NAME_SET = 0x02, EFFORT_SET = 0x04, CORPSE_ID_SET = 0x04, @@ -52,32 +49,6 @@ 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. @@ -97,7 +68,7 @@ static void tokens_into_entries(struct Context * context); * used in DB starting at "entry_cmp". */ static uint8_t new_entry(struct Context * context, char * comparand, - enum flag * flags, size_t size, + uint8_t * flags, size_t size, struct EntryHead ** entry, struct EntryHead * entry_cmp); @@ -117,21 +88,10 @@ static void test_corpse_ids(); * * 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, +static uint8_t set_members(struct Context * context, uint8_t * object_flags, + uint8_t * 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); - /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct * .func from .name for set_members(). */ @@ -140,72 +100,14 @@ static uint8_t try_func_name(struct MapObjAct * moa, -static char * token_from_line(char * line) -{ - static char * final_char = NULL; - static char * limit_char = NULL; - char * start = limit_char + 1; - if (line) - { - start = line; - limit_char = start; - final_char = &(line[strlen(line)]); - if ('\n' == *(final_char - 1)) - { - *(--final_char) = '\0'; - } - } - uint8_t empty = 1; - uint32_t i; - for (i = 0; '\0' != start[i]; i++) - { - if (' ' != start[i] && '\t' != start[i]) - { - start = &start[i]; - empty = 0; - break; - } - } - if (empty) - { - return start = NULL; - } - set_token_end(&start, &limit_char); - return start; -} - - - -static void set_token_end(char ** start, char ** limit_char) -{ - char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL; - *start = (end_quote) ? *start + 1 : *start; - if (end_quote) - { - *end_quote = '\0'; - *limit_char = end_quote; - return; - } - char * space = strchr(*start, ' '); - char * tab = strchr(*start, '\t'); - space = (!space || (tab && tab < space)) ? tab : space; - if (space) - { - * space = '\0'; - } - *limit_char = strchr(*start, '\0'); -} - - - static void tokens_into_entries(struct Context * context) { 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 uint8_t action_flags = READY_ACT; + static uint8_t object_flags = READY_OBJ; static struct EntryHead * moa = NULL; static struct EntryHead * mod = NULL; if ( !context->token0 @@ -242,7 +144,7 @@ static void tokens_into_entries(struct Context * context) static uint8_t new_entry(struct Context * context, char * comparand, - enum flag * flags, size_t size, + uint8_t * flags, size_t size, struct EntryHead ** entry,struct EntryHead * entry_cmp) { char * f_name = "new_entry()"; @@ -305,8 +207,8 @@ static void test_corpse_ids() -static uint8_t set_members(struct Context * context, enum flag * object_flags, - enum flag * action_flags, struct MapObjDef * mod, +static uint8_t set_members(struct Context * context, uint8_t * object_flags, + uint8_t * action_flags, struct MapObjDef * mod, struct MapObjAct * moa) { if ( * action_flags & EDIT_STARTED @@ -343,60 +245,6 @@ static uint8_t set_members(struct Context * context, enum flag * object_flags, -static uint8_t set_val(struct Context * context, char * comparand, - enum flag * flags, enum flag set_flag, char type, - char * element) -{ - char * err_out = "Outside appropriate definition's context."; - char * err_singlechar = "Value must be single ASCII character."; - if (!strcmp(context->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; -} - - - -static void set_uint8(struct Context * context, uint8_t * target) -{ - 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 (i > 2 || '0' > context->token1[i] || '9' < context->token1[i]) - { - is_uint8 = 0; - } - } - if (is_uint8 && atoi(context->token1) > UINT8_MAX) - { - is_uint8 = 0; - } - err_line(!(is_uint8), context->line, context->err_pre, err_uint8); - *target = atoi(context->token1); -} - - - static uint8_t try_func_name(struct MapObjAct * moa, char * name, void (* func) (struct MapObj *)) { @@ -412,40 +260,5 @@ 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, "")) - { - char * line_copy = strdup(context.line); - context.token0 = token_from_line(line_copy); - if (context.token0) - { - 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; - } - free(line_copy); - } - tokens_into_entries(&context); - try_fclose(file, f_name); - free(context.line); - free(context.err_pre); + parse_file(world.path_config, tokens_into_entries); }