home · contact · privacy
Server: Remove log_help(), this should be serverd by the client.
[plomrogue] / src / client / command_db.c
1 /* src/client/command_db.c
2  *
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.
6  */
7
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(),
18                     * parse_flagval()
19                     */
20 #include "world.h" /* global world */
21 #include "cleanup.h" /* set_cleanup_flag() */
22
23
24
25 /* Flags for defining state of command DB config file entries. */
26 enum cmd_flag
27 {
28     DESC_SET      = 0x02,
29     SERVERCMD_SET = 0x04,
30     SERVERARG_SET = 0x08,
31     READY_CMD = DESC_SET
32 };
33
34
35
36 /* Interpret "token0" and "token1" as data to write into the CommandDB.
37  *
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.
42  */
43 static void tokens_into_entries(char * token0, char * token1);
44
45
46
47 static void tokens_into_entries(char * token0, char * token1)
48 {
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))
53     {
54         parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
55         if (cmd)
56         {
57             array_append(world.commandDB.n, sizeof(struct Command),
58                          (void *) cmd, (void **) &world.commandDB.cmds);
59             world.commandDB.n++;
60             free(cmd);
61             cmd = NULL;
62         }
63     }
64     if (token0)
65     {
66         parsetest_too_many_values();
67         if      (!strcmp(token0, str_cmd))
68         {
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)));
73         }
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)))
80         {
81             parse_unknown_arg();
82         }
83     }
84 }
85
86
87
88 extern struct Command * get_command(char * dsc_short)
89 {
90     struct Command * cmd_ptr = world.commandDB.cmds;
91     uint8_t i = 0;
92     while (i < world.commandDB.n)
93     {
94         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
95         {
96             return cmd_ptr;
97         }
98         cmd_ptr = &cmd_ptr[1];
99         i++;
100     }
101     return NULL;
102 }
103
104
105
106 extern void init_command_db()
107 {
108     parse_file(world.path_commands, tokens_into_entries);
109     set_cleanup_flag(CLEANUP_COMMANDS);
110 }
111
112
113
114 extern void free_command_db()
115 {
116     uint8_t i = 0;
117     while (i < world.commandDB.n)
118     {
119         free(world.commandDB.cmds[i].dsc_short);
120         free(world.commandDB.cmds[i].dsc_long);
121         free(world.commandDB.cmds[i].server_msg);
122         i++;
123     }
124     free(world.commandDB.cmds);
125 }