1 /* src/server/configfile.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include <stddef.h> /* size_t, NULL */
5 #include <stdio.h> /* FILE, sprintf() */
6 #include <stdint.h> /* uint8_t, uint32_t */
7 #include <stdlib.h> /* atoi(), free() */
8 #include <string.h> /* strchr(), strcmp(), strdup(), strlen() */
9 #include <unistd.h> /* access(), F_OK */
10 #include "../common/err_try_fgets.h" /* err_line(), err_try_fgets(),
11 * reset_err_try_fgets_counter()
13 #include "../common/readwrite.h" /* try_fopen(),try_fclose(),textfile_width() */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS,
17 * CLEANUP_MAP_OBJ_ACTS
19 #include "map_object_actions.h" /* struct MapObjAct */
20 #include "map_objects.h" /* struct MapObj, struct MapObjDef */
21 #include "world.h" /* world global */
25 /* Flags defining state of object and action entry reading ((un-)finished, ready
26 * for starting the reading of a new definition etc.
35 LIFEPOINTS_SET = 0x10,
36 CONSUMABLE_SET = 0x20,
37 READY_ACT = NAME_SET | EFFORT_SET,
38 READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET
44 /* What MapObjDef and MapObjAct structs have in common at their top. Used to
45 * have common functions run over structs of both types.
50 struct EntryHead * next;
55 /* Many functions working on config file lines / tokens work with these elements
56 * that only change on line change. Makes sense to pass them over together.
67 /* Return next token from "line" or NULL if none is found. Tokens either a)
68 * start at the first non-whitespace character and end before the next
69 * whitespace character after that; or b) if the first non-whitespace character
70 * is a single quote followed by at least one other single quote some time later
71 * on the line, the token starts after that first single quote and ends before
72 * the second, with the next token_from_line() call starting its token search
73 * after that second quote. The only way to return an empty string (instead of
74 * NULL) as a token is to delimit the token by two succeeding single quotes.
76 static char * token_from_line(char * line);
78 /* Determines the end of the token_from_line() token. */
79 static void set_token_end(char ** start, char ** limit_char);
81 /* Get tokens from "context" and, by their order (in the individual context and
82 * in subsequent calls of this function), interpret them as data to write into
83 * the MapObjAct / MapObjDef DB.
85 * Individual MapObjDef / MapObjAct DB entries are put together line by line
86 * before being written. Writing only happens after all necessary members of an
87 * entry have been assembled, and when additionally a) a new entry is started by
88 * a context->token0 of "ACTION" or "OBJECT"; or b) a NULL context->token0 is
89 * passed. This is interpreted as the end of the MapObjDef / MapObjAct DB read,
90 * so the appropriate cleanup flags are set and test_corpse_ids() is called.
92 static void tokens_into_entries(struct Context * context);
94 /* Start reading a new DB entry of "size" from tokens in "context" if ->token0
95 * matches "comparand". Set EDIT_STARTED in "flags" to mark beginning of new
96 * entry reading. Check that id of new entry in ->token1 has not already been
97 * used in DB starting at "entry_cmp".
99 static uint8_t new_entry(struct Context * context, char * comparand,
100 enum flag * flags, size_t size,
101 struct EntryHead ** entry,
102 struct EntryHead * entry_cmp);
104 /* Write DB entry pointed to by "entry" to its appropriate location. */
105 static void write_if_entry(struct EntryHead ** entry,
106 struct EntryHead *** entry_p_p_p);
108 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
109 * MapObjDef DB entries.
111 static void test_corpse_ids();
113 /* Try to read tokens in "context" as members for the entry currently edited,
114 * which must be either "mod" or "moa". What member of which of the two is set
115 * depends on which of "object_flags" and "action_flags" has EDIT_STARTED set
116 * and on the key name of ->token0. Return 1 if interpretation succeeds, else 0.
118 * Note that MapObjAct entries' .name also determines their .func.
120 static uint8_t set_members(struct Context * context, enum flag * object_flags,
121 enum flag * action_flags, struct MapObjDef * mod,
122 struct MapObjAct * moa);
124 /* If "context"->token0 fits "comparand", set "element" to value read from
125 * ->token1 as either string (type: "s"), char ("c") or uint8 ("8"), set
126 * that element's flag to "flags" and return 1; else return 0.
128 static uint8_t set_val(struct Context * context, char * comparand,
129 enum flag * flags, enum flag set_flag, char type,
132 /* Writes "context"->token1 to "target" only if it describes a proper uint8. */
133 static void set_uint8(struct Context * context, uint8_t * target);
135 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
136 * .func from .name for set_members().
138 static uint8_t try_func_name(struct MapObjAct * moa,
139 char * name, void (* func) (struct MapObj *));
143 static char * token_from_line(char * line)
145 static char * final_char = NULL;
146 static char * limit_char = NULL;
147 char * start = limit_char + 1;
152 final_char = &(line[strlen(line)]);
153 if ('\n' == *(final_char - 1))
155 *(--final_char) = '\0';
160 for (i = 0; '\0' != start[i]; i++)
162 if (' ' != start[i] && '\t' != start[i])
173 set_token_end(&start, &limit_char);
179 static void set_token_end(char ** start, char ** limit_char)
181 char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL;
182 *start = (end_quote) ? *start + 1 : *start;
186 *limit_char = end_quote;
189 char * space = strchr(*start, ' ');
190 char * tab = strchr(*start, '\t');
191 space = (!space || (tab && tab < space)) ? tab : space;
196 *limit_char = strchr(*start, '\0');
201 static void tokens_into_entries(struct Context * context)
203 char * str_act = "ACTION";
204 char * str_obj = "OBJECT";
205 static struct MapObjAct ** moa_p_p = &world.map_obj_acts;
206 static struct MapObjDef ** mod_p_p = &world.map_obj_defs;
207 static enum flag action_flags = READY_ACT;
208 static enum flag object_flags = READY_OBJ;
209 static struct EntryHead * moa = NULL;
210 static struct EntryHead * mod = NULL;
211 if ( !context->token0
212 || !strcmp(context->token0,str_act) || !strcmp(context->token0,str_obj))
214 char * err_fin = "Last definition block not finished yet.";
215 err_line((action_flags & READY_ACT) ^ READY_ACT,
216 context->line, context->err_pre, err_fin);
217 err_line((object_flags & READY_OBJ) ^ READY_OBJ,
218 context->line, context->err_pre, err_fin);
219 write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
220 write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
221 object_flags = action_flags = READY_OBJ;
223 if (!context->token0)
225 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);
229 if (!( new_entry(context, str_act, &action_flags,
230 sizeof(struct MapObjAct), (struct EntryHead**) &moa,
231 (struct EntryHead *) world.map_obj_acts)
232 || new_entry(context, str_obj, &object_flags,
233 sizeof(struct MapObjDef), (struct EntryHead**) &mod,
234 (struct EntryHead *) world.map_obj_defs)
235 || set_members(context, &object_flags, &action_flags,
236 (struct MapObjDef *) mod, (struct MapObjAct *) moa)))
238 err_line(1, context->line, context->err_pre, "Unknown argument");
244 static uint8_t new_entry(struct Context * context, char * comparand,
245 enum flag * flags, size_t size,
246 struct EntryHead ** entry,struct EntryHead * entry_cmp)
248 char * f_name = "new_entry()";
249 char * err_uni = "Declaration of ID already used.";
250 if (!strcmp(context->token0, comparand))
252 * flags = EDIT_STARTED;
253 * entry = try_malloc(size, f_name);
254 set_uint8(context, &((*entry)->id));
255 for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
257 err_line((*entry)->id == entry_cmp->id,
258 context->line, context->err_pre, err_uni);
267 static void write_if_entry(struct EntryHead ** entry,
268 struct EntryHead *** entry_p_p_p)
272 (* entry)->next = NULL;
273 ** entry_p_p_p = *entry;
274 * entry_p_p_p = &((*entry)->next);
275 * entry = NULL; /* So later runs of this don't re-append same entry. */
281 static void test_corpse_ids()
283 char * f_name = "test_corpse_ids()";
284 char * err_corpse_prefix = "In the object definition DB, one object corpse "
285 "ID does not reference any known object in the "
286 "DB. ID of responsible object: ";
287 char * err_corpse = try_malloc(strlen(err_corpse_prefix) + 3 + 1, f_name);
288 struct MapObjDef * test_entry_0 = world.map_obj_defs;
289 for (; test_entry_0; test_entry_0 = test_entry_0->next)
291 uint8_t corpse_id_found = 0;
292 struct MapObjDef * test_entry_1 = world.map_obj_defs;
293 for (; test_entry_1; test_entry_1 = test_entry_1->next)
295 if (test_entry_0->corpse_id == test_entry_1->id)
300 sprintf(err_corpse, "%s%d", err_corpse_prefix, test_entry_0->id);
301 exit_err(!corpse_id_found, err_corpse);
308 static uint8_t set_members(struct Context * context, enum flag * object_flags,
309 enum flag * action_flags, struct MapObjDef * mod,
310 struct MapObjAct * moa)
312 if ( * action_flags & EDIT_STARTED
313 && set_val(context, "NAME", action_flags,
314 NAME_SET, 's', (char *) &moa->name))
316 if (!( try_func_name(moa, "move", actor_move)
317 || try_func_name(moa, "pick_up", actor_pick)
318 || try_func_name(moa, "drop", actor_drop)
319 || try_func_name(moa, "use", actor_use)))
321 moa->func = actor_wait;
323 *action_flags = *action_flags | NAME_SET;
326 else if ( set_val(context, "NAME", object_flags,
327 NAME_SET, 's', (char *) &mod->name)
328 || set_val(context, "SYMBOL", object_flags,
329 SYMBOL_SET, 'c', (char *) &mod->char_on_map)
330 || set_val(context, "EFFORT", action_flags,
331 EFFORT_SET, '8', (char *) &moa->effort)
332 || set_val(context, "LIFEPOINTS", object_flags,
333 LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
334 || set_val(context, "CONSUMABLE", object_flags,
335 CONSUMABLE_SET, '8', (char *) &mod->consumable)
336 || set_val(context, "CORPSE_ID", object_flags,
337 CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
346 static uint8_t set_val(struct Context * context, char * comparand,
347 enum flag * flags, enum flag set_flag, char type,
350 char * err_out = "Outside appropriate definition's context.";
351 char * err_singlechar = "Value must be single ASCII character.";
352 if (!strcmp(context->token0, comparand))
354 err_line(!(* flags & EDIT_STARTED),
355 context->line, context->err_pre, err_out);
356 * flags = * flags | set_flag;
359 * (char **) element = strdup(context->token1);
361 else if ('c' == type)
363 err_line(1 != strlen(context->token1),
364 context->line, context->err_pre, err_singlechar);
365 * element = (context->token1)[0];
367 else if ('8' == type)
369 set_uint8(context, (uint8_t *) element);
378 static void set_uint8(struct Context * context, uint8_t * target)
380 char * err_uint8 = "Value not unsigned decimal number between 0 and 255.";
382 uint8_t is_uint8 = 1;
383 for (i = 0; '\0' != context->token1[i]; i++)
385 if (i > 2 || '0' > context->token1[i] || '9' < context->token1[i])
390 if (is_uint8 && atoi(context->token1) > UINT8_MAX)
394 err_line(!(is_uint8), context->line, context->err_pre, err_uint8);
395 *target = atoi(context->token1);
400 static uint8_t try_func_name(struct MapObjAct * moa,
401 char * name, void (* func) (struct MapObj *))
403 if (0 == strcmp(moa->name, name))
413 extern void read_config_file()
415 char * f_name = "read_new_config_file()";
416 char * path = world.path_config;
417 struct Context context;
418 char * err_pre_prefix = "Failed reading config file: \"";
419 char * err_pre_affix = "\". ";
420 context.err_pre = try_malloc(strlen(err_pre_prefix) + strlen(path)
421 + strlen(err_pre_affix) + 1, f_name);
422 sprintf(context.err_pre, "%s%s%s", err_pre_prefix, path, err_pre_affix);
423 exit_err(access(path, F_OK), context.err_pre);
424 FILE * file = try_fopen(path, "r", f_name);
425 uint32_t linemax = textfile_width(file);
426 context.line = try_malloc(linemax + 1, f_name);
427 reset_err_try_fgets_counter();
428 err_line(0 == linemax, context.line, context.err_pre, "File is empty.");
429 context.token0 = NULL; /* For tokens_into_entries() if while() stagnates. */
430 char * err_val = "No value given.";
431 char * err_many = "Too many values.";
432 while (err_try_fgets(context.line, linemax + 1, file, context.err_pre, ""))
434 char * line_copy = strdup(context.line);
435 context.token0 = token_from_line(line_copy);
438 err_line(0 == (context.token1 = token_from_line(NULL)),
439 context.line, context.err_pre, err_val);
440 err_line(NULL != token_from_line(NULL),
441 context.line, context.err_pre, err_many);
442 tokens_into_entries(&context);
443 context.token0 = NULL;
447 tokens_into_entries(&context);
448 try_fclose(file, f_name);
450 free(context.err_pre);