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> /* strcmp(), strdup() */
9 #include "../common/parse_file.h" /* EDIT_STARTED,parse_init_entry(),
10 * parse_id_uniq(), parse_unknown_arg(),
11 * parsetest_too_many_values(), parse_file(),
12 * parse_and_reduce_to_readyflag(),parse_val()
14 #include "array_append.h" /* array_append() */
15 #include "world.h" /* global world */
16 #include "cleanup.h" /* set_cleanup_flag() */
20 /* Flags for defining state of command DB config file entries. */
31 /* Interpret "token0" and "token1" as data to write into the CommandDB.
33 * Individual CommandDB entries are put together line by line before being
34 * written. Writing happens after all necessary members of an entry have been
35 * assembled, and when additionally a) a new entry is started by a "token0" of
36 * "COMMAND"; or b) of "token0" of NULL is passed.
38 static void tokens_into_entries(char * token0, char * token1);
42 static void tokens_into_entries(char * token0, char * token1)
44 char * str_cmd = "COMMAND";
45 static uint8_t cmd_flags = READY_CMD;
46 static struct Command * cmd = NULL;
47 if (!token0 || !strcmp(token0, str_cmd))
49 parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
52 array_append(world.commandDB.n, sizeof(struct Command),
53 (void *) cmd, (void **) &world.commandDB.cmds);
61 parsetest_too_many_values();
62 if (!strcmp(token0, str_cmd))
64 cmd = (struct Command *) parse_init_entry(&cmd_flags,
65 sizeof(struct Command));
66 cmd->dsc_short = strdup(token1);
67 parse_id_uniq(NULL != get_command(cmd->dsc_short));
69 else if (!( parse_val(token0, token1, "DESCRIPTION", &cmd_flags,
70 DESC_SET, 's', (char *) &cmd->dsc_long)
71 || parse_val(token0, token1, "SERVER_COMMAND", &cmd_flags,
72 SERVERCMD_SET, 's', (char *) &cmd->server_msg)
73 || parse_val(token0, token1, "SERVER_ARGUMENT", &cmd_flags,
74 SERVERARG_SET, 'c', (char *) &cmd->arg)))
83 extern struct Command * get_command(char * dsc_short)
85 struct Command * cmd_ptr = world.commandDB.cmds;
87 while (i < world.commandDB.n)
89 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
93 cmd_ptr = &cmd_ptr[1];
101 extern void init_command_db()
103 parse_file(world.path_commands, tokens_into_entries);
104 set_cleanup_flag(CLEANUP_COMMANDS);
109 extern void free_command_db()
112 while (i < world.commandDB.n)
114 free(world.commandDB.cmds[i].dsc_short);
115 free(world.commandDB.cmds[i].dsc_long);
116 free(world.commandDB.cmds[i].server_msg);
119 free(world.commandDB.cmds);