home · contact · privacy
License everything (GPL).
[plomrogue] / src / client / command_db.c
index 6e43ff6c3da11cd7a89e8c9a0dfbacc3c962f430..68d4ce2dfd7f8908dd188d85b39195e7337fef59 100644 (file)
@@ -1,18 +1,22 @@
-/* src/client/command_db.c */
+/* src/client/command_db.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "command_db.h"
 #include <stddef.h> /* NULL */
 #include <stdint.h> /* uint8_t */
 #include <stdlib.h> /* free() */
-#include <string.h> /* memset(), strcmp(), strdup() */
-#include "../common/err_try_fgets.h" /* err_line() */
-#include "../common/parse_file.h" /* Context, EDIT_STARTED, parse_file(),
-                                   * set_val()
-                                   */
-#include "../common/rexit.h" /* exit_trouble() */
-#include "../common/try_malloc.h" /* try_malloc() */
+#include <string.h> /* strcmp(), strdup() */
 #include "array_append.h" /* array_append() */
+#include "parse.h" /* EDIT_STARTED, parse_init_entry(), parse_id_uniq(),
+                    * parse_unknown_arg(), parsetest_too_many_values(),
+                    * parse_file(), parse_and_reduce_to_readyflag(),
+                    * parse_flagval()
+                    */
 #include "world.h" /* global world */
 #include "cleanup.h" /* set_cleanup_flag() */
 
@@ -29,59 +33,53 @@ enum cmd_flag
 
 
 
-/* Get tokens from "context" and, by their order (in the individual context and
- * in subsequent calls of this function), interpret them as data to write into
- * the CommandDB.
+/* Interpret "token0" and "token1" as data to write into the CommandDB.
  *
  * Individual CommandDB entries are put together line by line before being
  * written. Writing happens after all necessary members of an entry have been
- * assembled, and when additionally a) a new entry is started by a
- * context->token0 of "COMMAND"; or b) a NULL context->token0 is passed. This is
- * interpreted as the CommandDB read's end: appropriate cleanup flags are set.
+ * assembled, and when additionally a) a new entry is started by a "token0" of
+ * "COMMAND"; or b) of "token0" of NULL is passed.
  */
-static void tokens_into_entries(struct Context * context);
+static void tokens_into_entries(char * token0, char * token1);
 
 
 
-static void tokens_into_entries(struct Context * context)
+static void tokens_into_entries(char * token0, char * token1)
 {
-    char * f_name = "tokens_into_entries()";
     char * str_cmd = "COMMAND";
     static uint8_t cmd_flags = READY_CMD;
     static struct Command * cmd = NULL;
-    if (!context->token0 || !strcmp(context->token0, str_cmd))
+    if (!token0 || !strcmp(token0, str_cmd))
     {
-        err_line((cmd_flags & READY_CMD) ^ READY_CMD, context->line,
-                  context->err_pre, "Last definition block not finished yet.");
+        parse_and_reduce_to_readyflag(&cmd_flags, READY_CMD);
         if (cmd)
         {
             array_append(world.commandDB.n, sizeof(struct Command),
                          (void *) cmd, (void **) &world.commandDB.cmds);
             world.commandDB.n++;
+            free(cmd);
             cmd = NULL;
         }
     }
-    if (!context->token0)
-    {
-        set_cleanup_flag(CLEANUP_COMMANDS);
-    }
-    else if (!strcmp(context->token0, str_cmd))
-    {
-        cmd_flags = EDIT_STARTED;
-        cmd = try_malloc(sizeof(struct Command), f_name);
-        memset(cmd, 0, sizeof(struct Command));
-        cmd->dsc_short = strdup(context->token1);
-        err_line(NULL != get_command(cmd->dsc_short), context->line,
-                 context->err_pre, "Declaration of ID already used.");
-    }
-    else if (!(   set_val(context, "DESCRIPTION", &cmd_flags,
-                          DESC_SET, 's', (char *) &cmd->dsc_long)
-               || set_val(context, "SERVER_COMMAND", &cmd_flags,
-                          SERVERCMD_SET, 's', (char *) &cmd->server_msg)
-               || set_val(context, "SERVER_ARGUMENT", &cmd_flags,
-                          SERVERARG_SET, 'c', (char *) &cmd->arg)))
+    if (token0)
     {
-        err_line(1, context->line, context->err_pre, "Unknown argument");
+        parsetest_too_many_values();
+        if      (!strcmp(token0, str_cmd))
+        {
+            cmd = (struct Command *) parse_init_entry(&cmd_flags,
+                                                      sizeof(struct Command));
+            cmd->dsc_short = strdup(token1);
+            parse_id_uniq(!(!get_command(cmd->dsc_short)));
+        }
+        else if (!(   parse_flagval(token0, token1, "DESCRIPTION", &cmd_flags,
+                                    DESC_SET, 's', (char *) &cmd->dsc_long)
+                   || parse_flagval(token0, token1,"SERVER_COMMAND", &cmd_flags,
+                                    SERVERCMD_SET, 's',(char *)&cmd->server_msg)
+                   || parse_flagval(token0, token1,"SERVER_ARGUMENT",&cmd_flags,
+                                    SERVERARG_SET, 'c', (char *) &cmd->arg)))
+        {
+            parse_unknown_arg();
+        }
     }
 }
 
@@ -108,6 +106,7 @@ extern struct Command * get_command(char * dsc_short)
 extern void init_command_db()
 {
     parse_file(world.path_commands, tokens_into_entries);
+    set_cleanup_flag(CLEANUP_COMMANDS);
 }