home · contact · privacy
b9d10a6234c92560bbeb1994d4ec0de5ccb52f10
[plomrogue] / src / client / command_db.c
1 /* src/client/command_db.c */
2
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(),
13                                    * parse_flagval()
14                                    */
15 #include "array_append.h" /* array_append() */
16 #include "world.h" /* global world */
17 #include "cleanup.h" /* set_cleanup_flag() */
18
19
20
21 /* Flags for defining state of command DB config file entries. */
22 enum cmd_flag
23 {
24     DESC_SET      = 0x02,
25     SERVERCMD_SET = 0x04,
26     SERVERARG_SET = 0x08,
27     READY_CMD = DESC_SET
28 };
29
30
31
32 /* Interpret "token0" and "token1" as data to write into the CommandDB.
33  *
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.
38  */
39 static void tokens_into_entries(char * token0, char * token1);
40
41
42
43 static void tokens_into_entries(char * token0, char * token1)
44 {
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))
49     {
50         parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
51         if (cmd)
52         {
53             array_append(world.commandDB.n, sizeof(struct Command),
54                          (void *) cmd, (void **) &world.commandDB.cmds);
55             world.commandDB.n++;
56             free(cmd);
57             cmd = NULL;
58         }
59     }
60     if (token0)
61     {
62         parsetest_too_many_values();
63         if      (!strcmp(token0, str_cmd))
64         {
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));
69         }
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)))
76         {
77             parse_unknown_arg();
78         }
79     }
80 }
81
82
83
84 extern struct Command * get_command(char * dsc_short)
85 {
86     struct Command * cmd_ptr = world.commandDB.cmds;
87     uint8_t i = 0;
88     while (i < world.commandDB.n)
89     {
90         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
91         {
92             return cmd_ptr;
93         }
94         cmd_ptr = &cmd_ptr[1];
95         i++;
96     }
97     return NULL;
98 }
99
100
101
102 extern void init_command_db()
103 {
104     parse_file(world.path_commands, tokens_into_entries);
105     set_cleanup_flag(CLEANUP_COMMANDS);
106 }
107
108
109
110 extern void free_command_db()
111 {
112     uint8_t i = 0;
113     while (i < world.commandDB.n)
114     {
115         free(world.commandDB.cmds[i].dsc_short);
116         free(world.commandDB.cmds[i].dsc_long);
117         free(world.commandDB.cmds[i].server_msg);
118         i++;
119     }
120     free(world.commandDB.cmds);
121 }