home · contact · privacy
Server: Slightly simplify command parsing.
[plomrogue] / src / server / run.c
index 8251d06118e7ddb071bfd21841c7fab9c1c68379..6cbbd58ee85b587ce2d6597ea364d0daede2b88e 100644 (file)
@@ -33,19 +33,14 @@ static uint8_t set_char_by_string_comparison(char * string, char * comparand,
 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
 static uint8_t player_commands_allowed();
 
-/* Parse player command "tok0" with no argument to player action, comment on
- * invalidity of non-zero "tok1" (but do not abort in that case).
- */
-static uint8_t parse_player_command_0arg(char * tok0, char * tok1);
+/* Parse player command "tok0" with no argument to player action. */
+static uint8_t parse_player_command_0arg(char * tok0);
 
 /* Parse player command "tok0" with one argument "tok1" to player action. */
 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
 
-/* Parse/apply command "tok0" with argument "tok1" and test the line for further
- * tokens, commenting on their invalidity (but don't abort on finding them).
- */
-static uint8_t parse_command_1arg(char * tok0, char * tok1);
-
+/* Parse/apply command "tok0". */
+static uint8_t parse_command(char * tok0);
 
 /* Compares first line of server out file to world.server_test, aborts if they
  * don't match, but not before unsetting the flags deleting files in the server
@@ -87,7 +82,7 @@ static uint8_t player_commands_allowed()
 
 
 
-static uint8_t parse_player_command_0arg(char * tok0, char * tok1)
+static uint8_t parse_player_command_0arg(char * tok0)
 {
     struct Thing * player = get_player();
     if (!strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP]))
@@ -97,7 +92,6 @@ static uint8_t parse_player_command_0arg(char * tok0, char * tok1)
             player->command = get_thing_action_id_by_name(tok0);
             player->arg = 0;
             turn_over();
-            err_line (NULL != tok1, "No arguments expected, ignoring them.");
         }
         return 1;
     }
@@ -141,29 +135,32 @@ static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
 
 
 
-static uint8_t parse_command_1arg(char * tok0, char * tok1)
+static uint8_t parse_command(char * tok0)
 {
-    char * tok2 = token_from_line(NULL);
-    if (   parse_player_command_1arg(tok0, tok1)
-        || parse_god_command_1arg(tok0, tok1));
+    if (parse_player_command_0arg(tok0))
+    {
+        return 1;
+    }
     else
     {
-        return 0;
+        char * tok1 = token_from_line(NULL);
+        if (tok1 && (   parse_player_command_1arg(tok0, tok1)
+                     || parse_god_command_1arg(tok0, tok1)))
+        {
+            return 1;
+        }
     }
-    char * err = "But one argument expected, ignoring further arguments.";
-    err_line (NULL != tok2, err);
-    return 1;
+    return 0;
 }
 
 
 
 static void server_test()
 {
-    char * f_name = "server_test()";
     char test[10 + 1 + 10 + 1 + 1];
-    FILE * file = try_fopen(s[S_PATH_OUT], "r", f_name);
-    try_fgets(test, 10 + 10 + 1 + 1, file, f_name);
-    try_fclose(file, f_name);
+    FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
+    try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
+    try_fclose(file, __func__);
     if (strcmp(test, world.server_test))
     {
         unset_cleanup_flag(CLEANUP_WORLDSTATE);
@@ -219,23 +216,22 @@ static void turn_over()
 
 static void record_msg(char * msg)
 {
-    char * f_name = "record_msg()";
     char * path_tmp;
     FILE * file_tmp = atomic_write_start(s[S_PATH_RECORD], &path_tmp);
     if (!access(s[S_PATH_RECORD], F_OK))
     {
-        FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", f_name);
+        FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
         uint32_t linemax = textfile_width(file_read);
-        char * line = try_malloc(linemax + 1, f_name);
-        while (try_fgets(line, linemax + 1, file_read, f_name))
+        char * line = try_malloc(linemax + 1, __func__);
+        while (try_fgets(line, linemax + 1, file_read, __func__))
         {
-            try_fwrite(line, strlen(line), 1, file_tmp, f_name);
+            try_fwrite(line, strlen(line), 1, file_tmp, __func__);
         }
         free(line);
-        try_fclose(file_read, f_name);
+        try_fclose(file_read, __func__);
     }
-    try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
-    try_fputc('\n', file_tmp, f_name);
+    try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
+    try_fputc('\n', file_tmp, __func__);
     atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
 }
 
@@ -243,24 +239,17 @@ static void record_msg(char * msg)
 
 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
 {
-    char * f_name = "obey_msg()";
     if (world.is_verbose && do_verbose)
     {
-        exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
+        exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
     }
     set_err_line_options("Trouble with message: ", msg, 0);
     char * msg_copy = strdup(msg);
-    if (msg[0] == 'm')
-    {
-        int a = 5;
-        a = a;
-    }
     char * tok0 = token_from_line(msg_copy);
     if (NULL != tok0)
     {
-        char * tok1 = token_from_line(NULL);
-        if (    parse_player_command_0arg(tok0, tok1)
-            || (tok1 && parse_command_1arg(tok0, tok1)))
+
+        if (parse_command(tok0))
         {
             if (world.exists)
             {
@@ -271,6 +260,8 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
                 save_world();
                 record_msg(msg);
             }
+            char * tokplus = token_from_line(NULL);
+            err_line(NULL != tokplus, "Too many arguments, ignoring overflow.");
             free(msg_copy);
             return;
         }
@@ -283,7 +274,6 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
 
 extern uint8_t io_loop()
 {
-    char * f_name = "io_loop()";
     while (1)
     {
         server_test();
@@ -294,7 +284,7 @@ extern uint8_t io_loop()
         }
         if (world.is_verbose)
         {
-            exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
+            exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
         }
         if (!strcmp("QUIT", msg))
         {
@@ -305,7 +295,7 @@ extern uint8_t io_loop()
         {
             free(msg);
             char * pong = "PONG\n";
-            try_fwrite(pong, strlen(pong), 1, world.file_out, f_name);
+            try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
             fflush(world.file_out);
             continue;
         }