home · contact · privacy
Client: Apply new commands DB file format, fix wrong path to it.
[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. This is
40  * interpreted as the CommandDB read's end: appropriate cleanup flags are set.
41  */
42 static void tokens_into_entries(struct Context * context);
43
44
45
46 static void tokens_into_entries(struct Context * context)
47 {
48     char * f_name = "tokens_into_entries()";
49     char * str_cmd = "COMMAND";
50     static uint8_t cmd_flags = READY_CMD;
51     static struct Command * cmd = NULL;
52     if (!context->token0 || !strcmp(context->token0, str_cmd))
53     {
54         err_line((cmd_flags & READY_CMD) ^ READY_CMD, context->line,
55                   context->err_pre, "Last definition block not finished yet.");
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)
65     {
66         set_cleanup_flag(CLEANUP_COMMANDS);
67     }
68     else if (!strcmp(context->token0, str_cmd))
69     {
70         cmd_flags = EDIT_STARTED;
71         cmd = try_malloc(sizeof(struct Command), f_name);
72         memset(cmd, 0, sizeof(struct Command));
73         cmd->dsc_short = strdup(context->token1);
74         err_line(NULL != get_command(cmd->dsc_short), context->line,
75                  context->err_pre, "Declaration of ID already used.");
76     }
77     else if (!(   set_val(context, "DESCRIPTION", &cmd_flags,
78                           DESC_SET, 's', (char *) &cmd->dsc_long)
79                || set_val(context, "SERVER_COMMAND", &cmd_flags,
80                           SERVERCMD_SET, 's', (char *) &cmd->server_msg)
81                || set_val(context, "SERVER_ARGUMENT", &cmd_flags,
82                           SERVERARG_SET, 'c', (char *) &cmd->arg)))
83     {
84         err_line(1, context->line, context->err_pre, "Unknown argument");
85     }
86 }
87
88
89
90 extern struct Command * get_command(char * dsc_short)
91 {
92     struct Command * cmd_ptr = world.commandDB.cmds;
93     uint8_t i = 0;
94     while (i < world.commandDB.n)
95     {
96         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
97         {
98             return cmd_ptr;
99         }
100         cmd_ptr = &cmd_ptr[1];
101         i++;
102     }
103     return NULL;
104 }
105
106
107
108 extern void init_command_db()
109 {
110     parse_file(world.path_commands, tokens_into_entries);
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 }