home · contact · privacy
Added client commands config error check condition: third token longer than one char.
[plomrogue] / src / client / command_db.c
1 /* src/client/command_db.c */
2
3 #include "command_db.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* memcpy(), strlen(), strtok(), strcmp() */
9 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets()
10                                   * textfile_sizes()
11                                   */
12 #include "../common/rexit.h" /* for exit_err() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "misc.h" /* array_append() */
16 #include "world.h" /* global world */
17
18
19
20 /* Helpers to init_command_db(). */
21 static uint8_t copy_tokenized_string(char * line, char ** ch_ptr, char * delim);
22 static char * init_command_db_err(char * line_copy, uint8_t line_number);
23
24
25 static uint8_t copy_tokenized_string(char * line, char ** ch_ptr, char * delim)
26 {
27     char * f_name = "copy_tokenized_string()";
28     char * dsc_ptr = strtok(line, delim);
29     if (!dsc_ptr)
30     {
31         return 1;
32     }
33     * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
34     memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
35     return 0;
36 }
37
38
39
40 static char * init_command_db_err(char * line_copy, uint8_t line_number)
41 {
42     char * f_name = "init_command_db_err";
43     char * err_start = "Failed reading command config file at ";
44     char * err_middle = " due to malformed line ";
45     line_copy[strlen(line_copy) - 1] = '\0';
46     char * err = try_malloc(strlen(err_start) + strlen(world.path_commands) +
47                             strlen(err_middle) + 3 + 2 + strlen(line_copy) + 1,
48                             f_name);
49     sprintf(err, "%s%s%s%d: %s", err_start, world.path_commands, err_middle,
50             line_number, line_copy);
51     return err;
52 }
53
54
55
56 extern struct Command * get_command(char * dsc_short)
57 {
58     struct Command * cmd_ptr = world.commandDB.cmds;
59     uint8_t i = 0;
60     while (i < world.commandDB.n)
61     {
62         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
63         {
64             break;
65         }
66         cmd_ptr = &cmd_ptr[1];
67         i++;
68     }
69     char * err_start = "get_command_data() failed on request for: ";
70     char err[strlen(err_start) + strlen(dsc_short) + 1];
71     sprintf(err, "%s%s", err_start, dsc_short);
72     exit_err(i == world.commandDB.n, err);
73     return cmd_ptr;
74 }
75
76
77
78 extern void init_command_db()
79 {
80     char * f_name = "init_command_db()";
81     FILE * file = try_fopen(world.path_commands, "r", f_name);
82     uint32_t lines;
83     uint32_t linemax = textfile_sizes(file, &lines);
84     char line[linemax + 1];
85     uint8_t i = 0;
86     char * delim = " ";
87     while (try_fgets(line, linemax + 1, file, f_name))
88     {
89         if ('\n' == line[0] || 0 == line[0])
90         {
91             break;
92         }
93         char line_copy[strlen(line) + 1];
94         sprintf(line_copy, "%s", line);
95         struct Command cmd;
96         char * arg_string;
97         exit_err((  copy_tokenized_string(line, &cmd.dsc_short, delim)
98                  || copy_tokenized_string(NULL, &cmd.server_msg, delim)
99                  || NULL == (arg_string = strtok(NULL, delim))
100                  || strlen(arg_string) > 1
101                  || copy_tokenized_string(NULL, &cmd.dsc_long, "\n")),
102                  init_command_db_err(line_copy, i + 1));
103         cmd.arg = arg_string[0];
104         if (!strcmp("0", cmd.server_msg))
105         {                          /* A .server_msg == NULL helps control.c's */
106             free(cmd.server_msg);  /* try_key() and try_server_command() to   */
107             cmd.server_msg = NULL; /* differentiate server commands from      */
108         }                          /* non-server commands.                    */
109         array_append(i, sizeof(struct Command), (void *) &cmd,
110                      (void **) &world.commandDB.cmds);
111         i++;
112     }
113     try_fclose(file, f_name);
114     world.commandDB.n = i;
115     set_cleanup_flag(CLEANUP_COMMANDS);
116 }
117
118
119
120 extern void free_command_db()
121 {
122     uint8_t i = 0;
123     while (i < world.commandDB.n)
124     {
125         free(world.commandDB.cmds[i].dsc_short);
126         free(world.commandDB.cmds[i].dsc_long);
127         free(world.commandDB.cmds[i].server_msg);
128         i++;
129     }
130     free(world.commandDB.cmds);
131 }