1 /* src/client/command_db.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "command_db.h"
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* memset(), strcmp(), strdup() */
9 #include "../common/parse_file.h" /* EDIT_STARTED, parse_file(), set_val(),
10 * token_from_line(), err_line()
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "array_append.h" /* array_append() */
14 #include "world.h" /* global world */
15 #include "cleanup.h" /* set_cleanup_flag() */
19 /* Flags for defining state of command DB config file entries. */
30 /* Get tokens from "context" and, by their order (in the individual context and
31 * in subsequent calls of this function), interpret them as data to write into
34 * Individual CommandDB entries are put together line by line before being
35 * written. Writing happens after all necessary members of an entry have been
36 * assembled, and when additionally a) a new entry is started by a
37 * context->token0 of "COMMAND"; or b) a NULL context->token0 is passed.
39 static void tokens_into_entries(char * token0, char * token1);
43 static void tokens_into_entries(char * token0, char * token1)
45 char * f_name = "tokens_into_entries()";
46 char * str_cmd = "COMMAND";
47 static uint8_t cmd_flags = READY_CMD;
48 static struct Command * cmd = NULL;
49 if (!token0 || !strcmp(token0, str_cmd))
51 char * err_fin = "Last definition block not finished yet.";
52 err_line((cmd_flags & READY_CMD) ^ READY_CMD, err_fin);
55 array_append(world.commandDB.n, sizeof(struct Command),
56 (void *) cmd, (void **) &world.commandDB.cmds);
62 err_line(token0 && NULL != token_from_line(NULL), "Too many values.");
63 if (token0 && !strcmp(token0, str_cmd))
65 char * err_uniq = "Declaration of ID already used.";
66 cmd_flags = EDIT_STARTED;
67 cmd = try_malloc(sizeof(struct Command), f_name);
68 memset(cmd, 0, sizeof(struct Command));
69 cmd->dsc_short = strdup(token1);
70 err_line(NULL != get_command(cmd->dsc_short), err_uniq);
73 && !( set_val(token0, token1, "DESCRIPTION", &cmd_flags,
74 DESC_SET, 's', (char *) &cmd->dsc_long)
75 || set_val(token0, token1, "SERVER_COMMAND", &cmd_flags,
76 SERVERCMD_SET, 's', (char *) &cmd->server_msg)
77 || set_val(token0, token1, "SERVER_ARGUMENT", &cmd_flags,
78 SERVERARG_SET, 'c', (char *) &cmd->arg)))
80 err_line(1, "Unknown arguemnt.");
86 extern struct Command * get_command(char * dsc_short)
88 struct Command * cmd_ptr = world.commandDB.cmds;
90 while (i < world.commandDB.n)
92 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
96 cmd_ptr = &cmd_ptr[1];
104 extern void init_command_db()
106 parse_file(world.path_commands, tokens_into_entries);
107 set_cleanup_flag(CLEANUP_COMMANDS);
112 extern void free_command_db()
115 while (i < world.commandDB.n)
117 free(world.commandDB.cmds[i].dsc_short);
118 free(world.commandDB.cmds[i].dsc_long);
119 free(world.commandDB.cmds[i].server_msg);
122 free(world.commandDB.cmds);