1 /* src/client/command_db.c */
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()
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 */
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);
25 static uint8_t copy_tokenized_string(char * line, char ** ch_ptr, char * delim)
27 char * f_name = "copy_tokenized_string()";
28 char * dsc_ptr = strtok(line, delim);
33 * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
34 memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
40 static char * init_command_db_err(char * line_copy, uint8_t line_number)
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,
49 sprintf(err, "%s%s%s%d: %s", err_start, world.path_commands, err_middle,
50 line_number, line_copy);
56 extern struct Command * get_command(char * dsc_short)
58 struct Command * cmd_ptr = world.commandDB.cmds;
60 while (i < world.commandDB.n)
62 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
66 cmd_ptr = &cmd_ptr[1];
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);
78 extern void init_command_db()
80 char * f_name = "init_command_db()";
81 FILE * file = try_fopen(world.path_commands, "r", f_name);
83 uint32_t linemax = textfile_sizes(file, &lines);
84 char line[linemax + 1];
87 while (try_fgets(line, linemax + 1, file, f_name))
89 if ('\n' == line[0] || 0 == line[0])
93 char line_copy[strlen(line) + 1];
94 sprintf(line_copy, "%s", line);
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);
113 try_fclose(file, f_name);
114 world.commandDB.n = i;
115 set_cleanup_flag(CLEANUP_COMMANDS);
120 extern void free_command_db()
123 while (i < world.commandDB.n)
125 free(world.commandDB.cmds[i].dsc_short);
126 free(world.commandDB.cmds[i].dsc_long);
127 free(world.commandDB.cmds[i].server_msg);
130 free(world.commandDB.cmds);