| 1 | /* |
| 2 | * Copyright (C) 2004,2005 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 20 | #include "common/conf.h"cxx . . $ `<file>/src/common/conf.h`/ |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <cstring> |
| 24 | #include <string> |
| 25 | |
| 26 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 27 | #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/ |
| 28 | #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/ |
| 29 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 30 | |
| 31 | namespace pvpgncxx . . $ pvpgn/ |
| 32 | { |
| 33 | |
| 34 | extern int conf_set_intcxx . . $ pvpgn/conf_set_int(5db442afce5f848d).(unsigned *pint, const char *valstr, unsigned def) |
| 35 | { |
| 36 | if (!valstr) *pint = def; |
| 37 | else { |
| 38 | unsigned temp; |
| 39 | |
| 40 | if (str_to_uintcxx . . $ pvpgn/str_to_uint(6974107818eeb505).(valstr, &temp) < 0) { |
| 41 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "invalid integer value '{}'", valstr); |
| 42 | return -1; |
| 43 | } |
| 44 | else *pint = temp; |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | extern int conf_set_boolcxx . . $ pvpgn/conf_set_bool(5db442afce5f848d).(unsigned *pbool, const char *valstr, unsigned def) |
| 51 | { |
| 52 | if (!valstr) *pbool = def; |
| 53 | else { |
| 54 | switch (str_get_boolcxx . . $ pvpgn/str_get_bool(5c4306d8553b3696).(valstr)) { |
| 55 | case 1: |
| 56 | *pbool = 1; |
| 57 | break; |
| 58 | case 0: |
| 59 | *pbool = 0; |
| 60 | break; |
| 61 | default: |
| 62 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "invalid boolean value '{}'", valstr); |
| 63 | return -1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | extern int conf_set_strcxx . . $ pvpgn/conf_set_str(ef5efe93f374dbad).(const char **pstr, const char *valstr, const char *def) |
| 71 | { |
| 72 | if (*pstr) xfreecxx . . $ `src/common/xalloc.h:51:9`!((void*)*pstr); |
| 73 | if (!valstr && !def) *pstr = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 74 | else *pstr = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(valstr ? valstr : def); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | extern int conf_set_timestrcxx . . $ pvpgn/conf_set_timestr(e90df6d2f7bece25).(stdcxx . . $ std/::time_tcxx . . $ std/time_t#* ptime, const char *valstr, stdcxx . . $ std/::time_tcxx . . $ std/time_t# def) |
| 80 | { |
| 81 | if (!valstr) *ptime = def; |
| 82 | else if (timestr_to_timecxx . . $ pvpgn/timestr_to_time(ef3eecfa0254c586).(valstr, ptime) < 0) { |
| 83 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "invalid timestr value '{}'", valstr); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | extern const char* conf_get_intcxx . . $ pvpgn/conf_get_int(81a3ae90da26a711).(unsigned ival) |
| 91 | { |
| 92 | static char buffer[128] = {}; |
| 93 | stdcxx . . $ std/::snprintfcxx . . $ std/snprintf(416d90c494a2998e).(buffer, sizeof buffer, "%u", ival); |
| 94 | return buffer; |
| 95 | } |
| 96 | |
| 97 | extern const char* conf_get_boolcxx . . $ pvpgn/conf_get_bool(81a3ae90da26a711).(unsigned ival) |
| 98 | { |
| 99 | return ival ? "true" : "false"; |
| 100 | } |
| 101 | |
| 102 | static void _conf_reset_defaultscxx . . $ pvpgn/_conf_reset_defaults(ffbf16c7007c705c).(t_conf_entrycxx . . $ pvpgn/t_conf_entry# *conftab) |
| 103 | { |
| 104 | t_conf_entrycxx . . $ pvpgn/t_conf_entry# *curr; |
| 105 | |
| 106 | assertcxx . . $ `/usr/include/assert.h:102:11`!(conftab); |
| 107 | for (curr = conftab; curr->namecxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#name.; curr++) |
| 108 | curr->setdefcxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#setdef.(); |
| 109 | } |
| 110 | |
| 111 | static char * _get_valuecxx . . $ pvpgn/_get_value(f08beea2d405899a).(char *str, unsigned lineno) |
| 112 | { |
| 113 | char *cp, prev; |
| 114 | |
| 115 | if (*str == '"') { |
| 116 | for (cp = ++str, prev = '\0'; *cp; cp++) { |
| 117 | switch (*cp) { |
| 118 | case '"': |
| 119 | if (prev != '\\') break; |
| 120 | prev = '"'; |
| 121 | continue; |
| 122 | case '\\': |
| 123 | if (prev == '\\') prev = '\0'; |
| 124 | else prev = '\\'; |
| 125 | continue; |
| 126 | default: |
| 127 | prev = *cp; |
| 128 | continue; |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (*cp != '"') { |
| 134 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "missing end quota at line {}", lineno); |
| 135 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 136 | } |
| 137 | |
| 138 | *cp = '\0'; |
| 139 | cp = str_skip_spacecxx . . $ pvpgn/str_skip_space(7a9722d75b371431).(cp + 1); |
| 140 | if (*cp) { |
| 141 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "extra characters in value after ending quote at line {}", lineno); |
| 142 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 143 | } |
| 144 | } |
| 145 | else { |
| 146 | cp = str_skip_wordcxx . . $ pvpgn/str_skip_word(7a9722d75b371431).(str); |
| 147 | if (*cp) { |
| 148 | *cp = '\0'; |
| 149 | cp = str_skip_spacecxx . . $ pvpgn/str_skip_space(7a9722d75b371431).(cp + 1); |
| 150 | if (*cp) { |
| 151 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "extra characters after the value at line {}", lineno); |
| 152 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return str; |
| 158 | } |
| 159 | |
| 160 | static void _process_optioncxx . . $ pvpgn/_process_option(3f450ea3dc5d1394).(const char *key, const char *val, t_conf_entrycxx . . $ pvpgn/t_conf_entry# *conftab) |
| 161 | { |
| 162 | t_conf_entrycxx . . $ pvpgn/t_conf_entry# *curr; |
| 163 | |
| 164 | for (curr = conftab; curr->namecxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#name.; curr++) |
| 165 | if (!stdcxx . . $ std/::strcmp(key, curr->namecxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#name.)) { |
| 166 | curr->setcxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#set.(val); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "option '{}' unknown", key); |
| 171 | } |
| 172 | |
| 173 | extern int conf_load_filecxx . . $ pvpgn/conf_load_file(d06f02f90cf2e5b0).(stdcxx . . $ std/::FILEcxx . . $ std/FILE# *fd, t_conf_entrycxx . . $ pvpgn/t_conf_entry# *conftab) |
| 174 | { |
| 175 | char *buff, *directive, *value, *cp; |
| 176 | unsigned cflag, lineno; |
| 177 | |
| 178 | if (!fd) { |
| 179 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL file"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | if (!conftab) { |
| 184 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL conftab"); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | /* restore defaults */ |
| 189 | _conf_reset_defaultscxx . . $ pvpgn/_conf_reset_defaults(ffbf16c7007c705c).(conftab); |
| 190 | |
| 191 | for (lineno = 1; (buff = file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(fd)); lineno++) { |
| 192 | cflag = 1; |
| 193 | for (cp = buff; *cp; cp++) { |
| 194 | switch (*cp) { |
| 195 | case '\\': |
| 196 | if (!*(++cp)) cflag = 0; |
| 197 | break; |
| 198 | case '"': |
| 199 | switch (cflag) { |
| 200 | case 1: cflag = 2; break; |
| 201 | case 2: cflag = 1; break; |
| 202 | } |
| 203 | break; |
| 204 | case '#': |
| 205 | if (cflag == 1) cflag = 0; |
| 206 | break; |
| 207 | } |
| 208 | if (!cflag) break; |
| 209 | } |
| 210 | |
| 211 | if (*cp == '#') *cp = '\0'; |
| 212 | cp = str_skip_spacecxx . . $ pvpgn/str_skip_space(7a9722d75b371431).(buff); |
| 213 | if (!*cp) continue; |
| 214 | |
| 215 | directive = cp; |
| 216 | cp = str_skip_wordcxx . . $ pvpgn/str_skip_word(7a9722d75b371431).(cp + 1); |
| 217 | if (*cp) *(cp++) = '\0'; |
| 218 | |
| 219 | cp = str_skip_spacecxx . . $ pvpgn/str_skip_space(7a9722d75b371431).(cp); |
| 220 | if (*cp != '=') { |
| 221 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "missing = on line {}", lineno); |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | cp = str_skip_spacecxx . . $ pvpgn/str_skip_space(7a9722d75b371431).(cp + 1); |
| 226 | if (!*cp) { |
| 227 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "missing value at line {}", lineno); |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | value = _get_valuecxx . . $ pvpgn/_get_value(f08beea2d405899a).(cp, lineno); |
| 232 | if (!value) continue; |
| 233 | |
| 234 | _process_optioncxx . . $ pvpgn/_process_option(3f450ea3dc5d1394).(directive, value, conftab); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | extern void conf_unloadcxx . . $ pvpgn/conf_unload(ffbf16c7007c705c).(t_conf_entrycxx . . $ pvpgn/t_conf_entry# *conftab) |
| 241 | { |
| 242 | t_conf_entrycxx . . $ pvpgn/t_conf_entry# *curr; |
| 243 | |
| 244 | if (!conftab) { |
| 245 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL conftab"); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | for (curr = conftab; curr->namecxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#name.; curr++) |
| 250 | curr->setcxx . . $ pvpgn/$anonymous_type_29e473bcbc8c3c72_0#set.(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!); |
| 251 | } |
| 252 | |
| 253 | extern int conf_load_cmdlinecxx . . $ pvpgn/conf_load_cmdline(8a18c527fa86306c).(int argc, char **argv, t_conf_entrycxx . . $ pvpgn/t_conf_entry# *conftab) |
| 254 | { |
| 255 | int i; |
| 256 | char *key, *val, *newkey; |
| 257 | |
| 258 | if (!conftab) { |
| 259 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL conftab"); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | /* restore defaults */ |
| 264 | _conf_reset_defaultscxx . . $ pvpgn/_conf_reset_defaults(ffbf16c7007c705c).(conftab); |
| 265 | |
| 266 | for (i = 1; i < argc; ++i) { |
| 267 | newkey = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 268 | key = argv[i]; |
| 269 | |
| 270 | if (*(key++) != '-') /* skip non options */ |
| 271 | continue; |
| 272 | |
| 273 | if (*key == '-') key++; /* allow both - and -- options */ |
| 274 | |
| 275 | if ((val = stdcxx . . $ std/::strchr(key, '='))) { /* we got option=value format */ |
| 276 | newkey = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(key); |
| 277 | key = newkey; |
| 278 | val = stdcxx . . $ std/::strchr(key, '='); |
| 279 | *(val++) = '\0'; |
| 280 | } |
| 281 | else if (i + 1 < argc && argv[i + 1][0] != '-') { |
| 282 | val = argv[++i]; |
| 283 | } |
| 284 | |
| 285 | if (!val) /* option without argument, so it's like boolean */ |
| 286 | val = (char *)("true"); |
| 287 | |
| 288 | _process_optioncxx . . $ pvpgn/_process_option(3f450ea3dc5d1394).(key, val, conftab); |
| 289 | |
| 290 | if (newkey) xfreecxx . . $ `src/common/xalloc.h:51:9`!(newkey); |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | } |