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