Debian Codegraph @

src/bnetd/command_groups.cpp

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16 #include "common/setup_before.h"
17 #define COMMAND_GROUPS_INTERNAL_ACCESS
18 #include "command_groups.h"
19
20 #include <cstdio>
21 #include <cerrno>
22 #include <cstring>
23
24 #include "common/eventlog.h"
25 #include "common/list.h"
26 #include "common/util.h"
27 #include "common/xalloc.h"
28
29 #include "common/setup_after.h"
30
31 //#define COMMANDGROUPSDEBUG 1
32
33 namespace pvpgn
34 {
35
36 namespace bnetd
37 {
38
39 static t_list * command_groups_head = NULL;
40 static std::FILE * fp = NULL;
41
42 extern int command_groups_load(char const * filename)
43 {
44 unsigned int line;
45 unsigned int pos;
46 char * buff;
47 char * temp;
48 char const * command;
49 unsigned int group;
50 t_command_groups * entry;
51
52 if (!filename) {
53 eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename");
54 return -1;
55 }
56 if (!(fp = std::fopen(filename, "r"))) {
57 eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno));
58 return -1;
59 }
60
61 command_groups_head = list_create();
62
63 for (line = 1; (buff = file_get_line(fp)); line++) {
64 for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++);
65 if (buff[pos] == '\0' || buff[pos] == '#') {
66 continue;
67 }
68 if ((temp = std::strrchr(buff, '#'))) {
69 unsigned int len;
70 unsigned int endpos;
71
72 *temp = '\0';
73 len = std::strlen(buff) + 1;
74 for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--);
75 buff[endpos + 1] = '\0';
76 }
77 if (!(temp = std::strtok(buff, " \t"))) { /* std::strtok modifies the string it is passed */
78 eventlog(eventlog_level_error, __FUNCTION__, "missing group on line {} of file \"{}\"", line, filename);
79 continue;
80 }
81 if (str_to_uint(temp, &group)<0) {
82 eventlog(eventlog_level_error, __FUNCTION__, "group '{}' not a valid group (1-8)", temp);
83 continue;
84 }
85 if (group == 0 || group > 8) {
86 eventlog(eventlog_level_error, __FUNCTION__, "group '{}' not within groups limits (1-8)", group);
87 continue;
88 }
89 while ((command = std::strtok(NULL, " \t"))) {
90 entry = (t_command_groups*)xmalloc(sizeof(t_command_groups));
91 entry->group = 1 << (group - 1);
92 entry->command = xstrdup(command);
93 list_append_data(command_groups_head, entry);
94 #ifdef COMMANDGROUPSDEBUG
95 eventlog(eventlog_level_info, __FUNCTION__, "Added command: {} - with group {}", entry->command, entry->group);
96 #endif
97 }
98 }
99 file_get_line(NULL); // clear file_get_line buffer
100 std::fclose(fp);
101 return 0;
102 }
103
104 extern int command_groups_unload(void)
105 {
106 t_elem * curr;
107 t_command_groups * entry;
108
109 if (command_groups_head) {
110 LIST_TRAVERSE(command_groups_head, curr) {
111 if (!(entry = (t_command_groups*)elem_get_data(curr)))
112 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
113 else {
114 xfree(entry->command);
115 xfree(entry);
116 }
117 list_remove_elem(command_groups_head, &curr);
118 }
119 list_destroy(command_groups_head);
120 command_groups_head = NULL;
121 }
122 return 0;
123 }
124
125 extern unsigned int command_get_group(char const * command)
126 {
127 t_elem const * curr;
128 t_command_groups * entry;
129
130 if (command_groups_head) {
131 LIST_TRAVERSE(command_groups_head, curr) {
132 if (!(entry = (t_command_groups*)elem_get_data(curr)))
133 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
134 else if (!(std::strcmp(entry->command, command)))
135 return entry->group;
136 }
137 }
138 return 0;
139 }
140
141 extern int command_groups_reload(char const * filename)
142 {
143 command_groups_unload();
144 return command_groups_load(filename);
145 }
146
147 }
148
149 }