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(),
15 #include "array_append.h" /* array_append() */
16 #include "world.h" /* global world */
17 #include "cleanup.h" /* set_cleanup_flag() */
21 /* Flags for defining state of command DB config file entries. */
32 /* Interpret "token0" and "token1" as data to write into the CommandDB.
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 "token0" of
37 * "COMMAND"; or b) of "token0" of NULL is passed.
39 static void tokens_into_entries(char * token0, char * token1);
43 static void tokens_into_entries(char * token0, char * token1)
45 char * str_cmd = "COMMAND";
46 static uint8_t cmd_flags = READY_CMD;
47 static struct Command * cmd = NULL;
48 if (!token0 || !strcmp(token0, str_cmd))
50 parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
53 array_append(world.commandDB.n, sizeof(struct Command),
54 (void *) cmd, (void **) &world.commandDB.cmds);
62 parsetest_too_many_values();
63 if (!strcmp(token0, str_cmd))
65 cmd = (struct Command *) parse_init_entry(&cmd_flags,
66 sizeof(struct Command));
67 cmd->dsc_short = strdup(token1);
68 parse_id_uniq(NULL != get_command(cmd->dsc_short));
70 else if (!( parse_flagval(token0, token1, "DESCRIPTION", &cmd_flags,
71 DESC_SET, 's', (char *) &cmd->dsc_long)
72 || parse_flagval(token0, token1,"SERVER_COMMAND", &cmd_flags,
73 SERVERCMD_SET, 's',(char *)&cmd->server_msg)
74 || parse_flagval(token0, token1,"SERVER_ARGUMENT",&cmd_flags,
75 SERVERARG_SET, 'c', (char *) &cmd->arg)))
84 extern struct Command * get_command(char * dsc_short)
86 struct Command * cmd_ptr = world.commandDB.cmds;
88 while (i < world.commandDB.n)
90 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
94 cmd_ptr = &cmd_ptr[1];
102 extern void init_command_db()
104 parse_file(world.path_commands, tokens_into_entries);
105 set_cleanup_flag(CLEANUP_COMMANDS);
110 extern void free_command_db()
113 while (i < world.commandDB.n)
115 free(world.commandDB.cmds[i].dsc_short);
116 free(world.commandDB.cmds[i].dsc_long);
117 free(world.commandDB.cmds[i].server_msg);
120 free(world.commandDB.cmds);