1 /* src/client/command_db.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
9 #include "command_db.h"
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* strcmp(), strdup() */
14 #include "array_append.h" /* array_append() */
15 #include "parse.h" /* EDIT_STARTED, parse_init_entry(), parse_id_uniq(),
16 * parse_unknown_arg(), parsetest_too_many_values(),
17 * parse_file(), parse_and_reduce_to_readyflag(),
20 #include "world.h" /* global world */
21 #include "cleanup.h" /* set_cleanup_flag() */
25 /* Flags for defining state of command DB config file entries. */
36 /* Interpret "token0" and "token1" as data to write into the CommandDB.
38 * Individual CommandDB entries are put together line by line before being
39 * written. Writing happens after all necessary members of an entry have been
40 * assembled, and when additionally a) a new entry is started by a "token0" of
41 * "COMMAND"; or b) of "token0" of NULL is passed.
43 static void tokens_into_entries(char * token0, char * token1);
47 static void tokens_into_entries(char * token0, char * token1)
49 char * str_cmd = "COMMAND";
50 static uint8_t cmd_flags = READY_CMD;
51 static struct Command * cmd = NULL;
52 if (!token0 || !strcmp(token0, str_cmd))
54 parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
57 array_append(world.commandDB.n, sizeof(struct Command),
58 (void *) cmd, (void **) &world.commandDB.cmds);
66 parsetest_too_many_values();
67 if (!strcmp(token0, str_cmd))
69 cmd = (struct Command *) parse_init_entry(&cmd_flags,
70 sizeof(struct Command));
71 cmd->dsc_short = strdup(token1);
72 parse_id_uniq(!(!get_command(cmd->dsc_short)));
74 else if (!( parse_flagval(token0, token1, "DESCRIPTION", &cmd_flags,
75 DESC_SET, 's', (char *) &cmd->dsc_long)
76 || parse_flagval(token0, token1,"SERVER_COMMAND", &cmd_flags,
77 SERVERCMD_SET, 's',(char *)&cmd->server_msg)
78 || parse_flagval(token0, token1,"SERVER_ARGUMENT",&cmd_flags,
79 SERVERARG_SET, 'c', (char *) &cmd->arg)))
88 extern struct Command * get_command(char * dsc_short)
90 struct Command * cmd_ptr = world.commandDB.cmds;
92 while (i < world.commandDB.n)
94 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
98 cmd_ptr = &cmd_ptr[1];
106 extern void init_command_db()
108 parse_file(world.path_commands, tokens_into_entries);
109 set_cleanup_flag(CLEANUP_COMMANDS);
114 extern void free_command_db()
117 while (i < world.commandDB.n)
119 free(world.commandDB.cmds[i].dsc_short);
120 free(world.commandDB.cmds[i].dsc_long);
121 free(world.commandDB.cmds[i].server_msg);
124 free(world.commandDB.cmds);