home · contact · privacy
a1c3551fc9567ebe919267876cba64a403597d9b
[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> /* memset(), strcmp(), strdup() */
9 #include "../common/err_try_fgets.h" /* err_line() */
10 #include "../common/parse_file.h" /* Context, EDIT_STARTED, parse_file(),
11                                    * set_val()
12                                    */
13 #include "../common/rexit.h" /* exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
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 /* Get tokens from "context" and, by their order (in the individual context and
33  * in subsequent calls of this function), interpret them as data to write into
34  * the CommandDB.
35  *
36  * Individual CommandDB entries are put together line by line before being
37  * written. Writing happens after all necessary members of an entry have been
38  * assembled, and when additionally a) a new entry is started by a
39  * context->token0 of "COMMAND"; or b) a NULL context->token0 is passed.
40  */
41 static void tokens_into_entries(struct Context * context);
42
43
44
45 static void tokens_into_entries(struct Context * context)
46 {
47     char * f_name = "tokens_into_entries()";
48     char * str_cmd = "COMMAND";
49     static uint8_t cmd_flags = READY_CMD;
50     static struct Command * cmd = NULL;
51     if (!context->token0 || !strcmp(context->token0, str_cmd))
52     {
53         char * err_fin = "Last definition block not finished yet.";
54         err_line((cmd_flags & READY_CMD) ^ READY_CMD,
55                   context->line, context->err_pre, err_fin);
56         if (cmd)
57         {
58             array_append(world.commandDB.n, sizeof(struct Command),
59                          (void *) cmd, (void **) &world.commandDB.cmds);
60             world.commandDB.n++;
61             cmd = NULL;
62         }
63     }
64     if (context->token0 && !strcmp(context->token0, str_cmd))
65     {
66         char * err_uniq = "Declaration of ID already used.";
67         cmd_flags = EDIT_STARTED;
68         cmd = try_malloc(sizeof(struct Command), f_name);
69         memset(cmd, 0, sizeof(struct Command));
70         cmd->dsc_short = strdup(context->token1);
71         err_line(NULL != get_command(cmd->dsc_short),
72                  context->line, context->err_pre, err_uniq);
73     }
74     else if (   context->token0
75              && !(   set_val(context, "DESCRIPTION", &cmd_flags,
76                              DESC_SET, 's', (char *) &cmd->dsc_long)
77                   || set_val(context, "SERVER_COMMAND", &cmd_flags,
78                              SERVERCMD_SET, 's', (char *) &cmd->server_msg)
79                   || set_val(context, "SERVER_ARGUMENT", &cmd_flags,
80                              SERVERARG_SET, 'c', (char *) &cmd->arg)))
81     {
82         char * err_unknown = "Unknown argument.";
83         err_line(1, context->line, context->err_pre, err_unknown);
84     }
85 }
86
87
88
89 extern struct Command * get_command(char * dsc_short)
90 {
91     struct Command * cmd_ptr = world.commandDB.cmds;
92     uint8_t i = 0;
93     while (i < world.commandDB.n)
94     {
95         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
96         {
97             return cmd_ptr;
98         }
99         cmd_ptr = &cmd_ptr[1];
100         i++;
101     }
102     return NULL;
103 }
104
105
106
107 extern void init_command_db()
108 {
109     parse_file(world.path_commands, tokens_into_entries);
110     set_cleanup_flag(CLEANUP_COMMANDS);
111 }
112
113
114
115 extern void free_command_db()
116 {
117     uint8_t i = 0;
118     while (i < world.commandDB.n)
119     {
120         free(world.commandDB.cmds[i].dsc_short);
121         free(world.commandDB.cmds[i].dsc_long);
122         free(world.commandDB.cmds[i].server_msg);
123         i++;
124     }
125     free(world.commandDB.cmds);
126 }