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. This is
40 * interpreted as the CommandDB read's end: appropriate cleanup flags are set.
42 static void tokens_into_entries(struct Context * context);
46 static void tokens_into_entries(struct Context * context)
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))
54 err_line((cmd_flags & READY_CMD) ^ READY_CMD, context->line,
55 context->err_pre, "Last definition block not finished yet.");
58 array_append(world.commandDB.n, sizeof(struct Command),
59 (void *) cmd, (void **) &world.commandDB.cmds);
66 set_cleanup_flag(CLEANUP_COMMANDS);
68 else if (!strcmp(context->token0, str_cmd))
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.");
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)))
84 err_line(1, context->line, context->err_pre, "Unknown argument");
90 extern struct Command * get_command(char * dsc_short)
92 struct Command * cmd_ptr = world.commandDB.cmds;
94 while (i < world.commandDB.n)
96 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
100 cmd_ptr = &cmd_ptr[1];
108 extern void init_command_db()
110 parse_file("confclient/new_commands", tokens_into_entries);
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);