diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-31 13:14:18 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-31 13:14:18 -0800 |
| commit | e27a69b258f4ff3d0fefa214e8507a93e4c5c133 (patch) | |
| tree | cff6f98b16c24c208ee845acf6134d5c30da960f /src | |
| parent | b86bd75f5bf15961d8c07bfe589a0087c71379b2 (diff) | |
| download | tinyrad-e27a69b258f4ff3d0fefa214e8507a93e4c5c133.tar.gz tinyrad-e27a69b258f4ff3d0fefa214e8507a93e4c5c133.tar.bz2 tinyrad-e27a69b258f4ff3d0fefa214e8507a93e4c5c133.tar.xz tinyrad-e27a69b258f4ff3d0fefa214e8507a93e4c5c133.zip | |
tinyrad: adding error functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/tru/tinyradutil.c | 77 | ||||
| -rw-r--r-- | src/tru/tinyradutil.h | 20 |
2 files changed, 87 insertions, 10 deletions
diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c index fafd309..b07492a 100644 --- a/src/tru/tinyradutil.c +++ b/src/tru/tinyradutil.c | |||
| @@ -218,6 +218,65 @@ int main(int argc, char * argv[]) | |||
| 218 | } | 218 | } |
| 219 | 219 | ||
| 220 | 220 | ||
| 221 | //-------------------// | ||
| 222 | // logging functions // | ||
| 223 | //-------------------// | ||
| 224 | #pragma mark logging functions | ||
| 225 | |||
| 226 | int | ||
| 227 | tru_error( | ||
| 228 | TinyRadUtilConf * cnf, | ||
| 229 | int rc, | ||
| 230 | const char * fmt, | ||
| 231 | ... ) | ||
| 232 | { | ||
| 233 | va_list args; | ||
| 234 | const char * prog_name; | ||
| 235 | |||
| 236 | if ((cnf->opts & TRUTILS_OPT_QUIET)) | ||
| 237 | return(rc); | ||
| 238 | |||
| 239 | prog_name = ((cnf->prog_name)) ? cnf->prog_name : PROGRAM_NAME; | ||
| 240 | |||
| 241 | fprintf(stderr, "%s: ", prog_name); | ||
| 242 | |||
| 243 | if (!(fmt)) | ||
| 244 | { | ||
| 245 | fprintf(stderr, "%s\n", tinyrad_strerror(rc)); | ||
| 246 | return(rc); | ||
| 247 | } | ||
| 248 | |||
| 249 | va_start(args, fmt); | ||
| 250 | vfprintf(stderr, fmt, args); | ||
| 251 | va_end(args); | ||
| 252 | fprintf(stderr, "\n"); | ||
| 253 | |||
| 254 | return(rc); | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | int | ||
| 259 | tru_errors( | ||
| 260 | TinyRadUtilConf * cnf, | ||
| 261 | int rc, | ||
| 262 | char ** errs ) | ||
| 263 | { | ||
| 264 | int pos; | ||
| 265 | const char * prog_name; | ||
| 266 | |||
| 267 | if ((cnf->opts & TRUTILS_OPT_QUIET)) | ||
| 268 | return(rc); | ||
| 269 | |||
| 270 | prog_name = ((cnf->prog_name)) ? cnf->prog_name : PROGRAM_NAME; | ||
| 271 | |||
| 272 | if ((errs)) | ||
| 273 | for(pos = 0; ((errs[pos])); pos++) | ||
| 274 | fprintf(stderr, "%s: %s\n", prog_name, errs[pos]); | ||
| 275 | |||
| 276 | return(rc); | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 221 | //-------------------------// | 280 | //-------------------------// |
| 222 | // miscellaneous functions // | 281 | // miscellaneous functions // |
| 223 | //-------------------------// | 282 | //-------------------------// |
| @@ -262,17 +321,14 @@ tru_load_tinyrad( | |||
| 262 | { | 321 | { |
| 263 | // initialize dictionary | 322 | // initialize dictionary |
| 264 | if ((rc = tinyrad_dict_initialize(&dict, cnf->tr_opts)) != TRAD_SUCCESS) | 323 | if ((rc = tinyrad_dict_initialize(&dict, cnf->tr_opts)) != TRAD_SUCCESS) |
| 265 | { | 324 | return(tru_error(cnf, rc, "out of virtual memory")); |
| 266 | trutils_error(cnf->opts, NULL, "out of virtual memory"); | ||
| 267 | return(rc); | ||
| 268 | }; | ||
| 269 | 325 | ||
| 270 | // set paths | 326 | // set paths |
| 271 | if ((cnf->dict_paths)) | 327 | if ((cnf->dict_paths)) |
| 272 | { | 328 | { |
| 273 | if ((rc = tinyrad_dict_set_option(dict, TRAD_DICT_OPT_PATHS, cnf->dict_paths)) != TRAD_SUCCESS) | 329 | if ((rc = tinyrad_dict_set_option(dict, TRAD_DICT_OPT_PATHS, cnf->dict_paths)) != TRAD_SUCCESS) |
| 274 | { | 330 | { |
| 275 | trutils_error(cnf->opts, NULL, "tinyrad_dict_set_option(TRAD_DICT_OPT_PATHS): %s", tinyrad_strerror(rc)); | 331 | tru_error(cnf, rc, "tinyrad_dict_set_option(TRAD_DICT_OPT_PATHS): %s", tinyrad_strerror(rc)); |
| 276 | tinyrad_free(dict); | 332 | tinyrad_free(dict); |
| 277 | return(rc); | 333 | return(rc); |
| 278 | }; | 334 | }; |
| @@ -283,7 +339,7 @@ tru_load_tinyrad( | |||
| 283 | { | 339 | { |
| 284 | if ((rc = tinyrad_dict_parse(dict, cnf->dict_files[pos], &errs, 0)) != TRAD_SUCCESS) | 340 | if ((rc = tinyrad_dict_parse(dict, cnf->dict_files[pos], &errs, 0)) != TRAD_SUCCESS) |
| 285 | { | 341 | { |
| 286 | trutils_error(cnf->opts, errs, NULL); | 342 | tru_errors(cnf, rc, errs); |
| 287 | tinyrad_strsfree(errs); | 343 | tinyrad_strsfree(errs); |
| 288 | tinyrad_free(dict); | 344 | tinyrad_free(dict); |
| 289 | return(rc); | 345 | return(rc); |
| @@ -361,6 +417,7 @@ tru_getopt( | |||
| 361 | const struct option * long_opt, | 417 | const struct option * long_opt, |
| 362 | int * opt_index ) | 418 | int * opt_index ) |
| 363 | { | 419 | { |
| 420 | int rc; | ||
| 364 | int c; | 421 | int c; |
| 365 | int opt; | 422 | int opt; |
| 366 | 423 | ||
| @@ -379,9 +436,9 @@ tru_getopt( | |||
| 379 | return(TRU_GETOPT_MATCHED); | 436 | return(TRU_GETOPT_MATCHED); |
| 380 | 437 | ||
| 381 | case 'D': | 438 | case 'D': |
| 382 | if (tinyrad_strsadd(&cnf->dict_files, optarg) != TRAD_SUCCESS) | 439 | if ((rc = tinyrad_strsadd(&cnf->dict_files, optarg)) != TRAD_SUCCESS) |
| 383 | { | 440 | { |
| 384 | trutils_error(cnf->opts, NULL, "out of virtual memory"); | 441 | tru_error(cnf, rc, "out of virtual memory"); |
| 385 | return(TRU_GETOPT_ERROR); | 442 | return(TRU_GETOPT_ERROR); |
| 386 | }; | 443 | }; |
| 387 | return(TRU_GETOPT_MATCHED); | 444 | return(TRU_GETOPT_MATCHED); |
| @@ -396,9 +453,9 @@ tru_getopt( | |||
| 396 | return(TRU_GETOPT_MATCHED); | 453 | return(TRU_GETOPT_MATCHED); |
| 397 | 454 | ||
| 398 | case 'I': | 455 | case 'I': |
| 399 | if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) | 456 | if ((rc = tinyrad_strsadd(&cnf->dict_paths, optarg)) != TRAD_SUCCESS) |
| 400 | { | 457 | { |
| 401 | trutils_error(cnf->opts, NULL, "out of virtual memory"); | 458 | tru_error(cnf, rc, "out of virtual memory"); |
| 402 | return(TRU_GETOPT_ERROR); | 459 | return(TRU_GETOPT_ERROR); |
| 403 | }; | 460 | }; |
| 404 | return(TRU_GETOPT_MATCHED); | 461 | return(TRU_GETOPT_MATCHED); |
diff --git a/src/tru/tinyradutil.h b/src/tru/tinyradutil.h index 1b259a7..156a07e 100644 --- a/src/tru/tinyradutil.h +++ b/src/tru/tinyradutil.h | |||
| @@ -170,6 +170,26 @@ extern const TinyRadUtilWidget tru_widget_map[]; | |||
| 170 | ////////////////// | 170 | ////////////////// |
| 171 | #pragma mark - Prototypes | 171 | #pragma mark - Prototypes |
| 172 | 172 | ||
| 173 | //--------------------// | ||
| 174 | // logging prototypes // | ||
| 175 | //--------------------// | ||
| 176 | #pragma mark logging prototypes | ||
| 177 | |||
| 178 | int | ||
| 179 | tru_error( | ||
| 180 | TinyRadUtilConf * cnf, | ||
| 181 | int rc, | ||
| 182 | const char * fmt, | ||
| 183 | ... ); | ||
| 184 | |||
| 185 | |||
| 186 | int | ||
| 187 | tru_errors( | ||
| 188 | TinyRadUtilConf * cnf, | ||
| 189 | int rc, | ||
| 190 | char ** errs ); | ||
| 191 | |||
| 192 | |||
| 173 | //--------------------------// | 193 | //--------------------------// |
| 174 | // miscellaneous prototypes // | 194 | // miscellaneous prototypes // |
| 175 | //--------------------------// | 195 | //--------------------------// |
