1 /* src/client/command_db.c */
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(),
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() */
21 /* Flags for defining state of command DB config file entries. */
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
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.
41 static void tokens_into_entries(struct Context * context);
45 static void tokens_into_entries(struct Context * context)
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))
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);
58 array_append(world.commandDB.n, sizeof(struct Command),
59 (void *) cmd, (void **) &world.commandDB.cmds);
64 if (context->token0 && !strcmp(context->token0, str_cmd))
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);
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)))
82 char * err_unknown = "Unknown argument.";
83 err_line(1, context->line, context->err_pre, err_unknown);
89 extern struct Command * get_command(char * dsc_short)
91 struct Command * cmd_ptr = world.commandDB.cmds;
93 while (i < world.commandDB.n)
95 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
99 cmd_ptr = &cmd_ptr[1];
107 extern void init_command_db()
109 parse_file(world.path_commands, tokens_into_entries);
110 set_cleanup_flag(CLEANUP_COMMANDS);
115 extern void free_command_db()
118 while (i < world.commandDB.n)
120 free(world.commandDB.cmds[i].dsc_short);
121 free(world.commandDB.cmds[i].dsc_long);
122 free(world.commandDB.cmds[i].server_msg);
125 free(world.commandDB.cmds);