diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-10 15:39:03 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-10 15:39:03 -0900 |
| commit | 2590625e87ece15972e8912e9c358598d5ba376a (patch) | |
| tree | c46399c9fffd24da1cb9603123bd1ac652ad4118 /src | |
| parent | d5c8f81b9041310200108a4c5ccd3dd2ba13fed1 (diff) | |
| download | tinyrad-2590625e87ece15972e8912e9c358598d5ba376a.tar.gz tinyrad-2590625e87ece15972e8912e9c358598d5ba376a.tar.bz2 tinyrad-2590625e87ece15972e8912e9c358598d5ba376a.tar.xz tinyrad-2590625e87ece15972e8912e9c358598d5ba376a.zip | |
tinyrad: moving dictionary function to my_dict_load()
Diffstat (limited to 'src')
| -rw-r--r-- | src/tinyrad.c | 130 |
1 files changed, 79 insertions, 51 deletions
diff --git a/src/tinyrad.c b/src/tinyrad.c index d4c37ad..65d6988 100644 --- a/src/tinyrad.c +++ b/src/tinyrad.c | |||
| @@ -98,6 +98,14 @@ main( | |||
| 98 | char * argv[] ); | 98 | char * argv[] ); |
| 99 | 99 | ||
| 100 | 100 | ||
| 101 | int | ||
| 102 | my_dict_load( | ||
| 103 | int opts, | ||
| 104 | TinyRadDict ** dictp, | ||
| 105 | char ** dict_files, | ||
| 106 | char ** dict_paths ); | ||
| 107 | |||
| 108 | |||
| 101 | void | 109 | void |
| 102 | my_usage( void ); | 110 | my_usage( void ); |
| 103 | 111 | ||
| @@ -115,11 +123,9 @@ int main(int argc, char * argv[]) | |||
| 115 | int c; | 123 | int c; |
| 116 | int opt_index; | 124 | int opt_index; |
| 117 | int rc; | 125 | int rc; |
| 118 | size_t pos; | ||
| 119 | TinyRadDict * dict; | 126 | TinyRadDict * dict; |
| 120 | TinyRad * tr; | 127 | TinyRad * tr; |
| 121 | const char * url; | 128 | const char * url; |
| 122 | char ** errs; | ||
| 123 | char ** dict_files; | 129 | char ** dict_files; |
| 124 | char ** dict_paths; | 130 | char ** dict_paths; |
| 125 | unsigned opts; | 131 | unsigned opts; |
| @@ -228,56 +234,11 @@ int main(int argc, char * argv[]) | |||
| 228 | optind++; | 234 | optind++; |
| 229 | 235 | ||
| 230 | // load RADIUS dictionary | 236 | // load RADIUS dictionary |
| 231 | if ((dict_files)) | 237 | if ((rc = my_dict_load(opts, &dict, dict_files, dict_paths)) != TRAD_SUCCESS) |
| 232 | { | 238 | { |
| 233 | // initialize dictionary | 239 | tinyrad_strsfree(dict_files); |
| 234 | if (tinyrad_dict_initialize(&dict, 0) != TRAD_SUCCESS) | 240 | tinyrad_strsfree(dict_paths); |
| 235 | { | 241 | return(trutils_exit_code(rc)); |
| 236 | trutils_error(opts, NULL, "out of virtual memory"); | ||
| 237 | tinyrad_strsfree(dict_files); | ||
| 238 | tinyrad_strsfree(dict_paths); | ||
| 239 | return(trutils_exit_code(TRAD_ENOMEM)); | ||
| 240 | }; | ||
| 241 | |||
| 242 | // load defaults | ||
| 243 | if ((opts & MY_OPT_DICT_DEFAULTS)) | ||
| 244 | { | ||
| 245 | if (tinyrad_dict_defaults(dict, &errs, 0) != TRAD_SUCCESS) | ||
| 246 | { | ||
| 247 | trutils_error(opts, errs, NULL); | ||
| 248 | tinyrad_free(dict); | ||
| 249 | tinyrad_strsfree(dict_files); | ||
| 250 | tinyrad_strsfree(dict_paths); | ||
| 251 | return(1); | ||
| 252 | }; | ||
| 253 | }; | ||
| 254 | |||
| 255 | // set paths | ||
| 256 | for(pos = 0; ( ((dict_paths)) && ((dict_paths[pos])) ); pos++) | ||
| 257 | { | ||
| 258 | if ((rc = tinyrad_dict_add_path(dict, dict_paths[pos])) != TRAD_SUCCESS) | ||
| 259 | { | ||
| 260 | tinyrad_strsfree(dict_paths); | ||
| 261 | tinyrad_strsfree(dict_files); | ||
| 262 | tinyrad_free(dict); | ||
| 263 | trutils_error(opts, NULL, "%s: %s", dict_paths[pos], tinyrad_strerror(rc)); | ||
| 264 | return(trutils_exit_code(rc)); | ||
| 265 | }; | ||
| 266 | }; | ||
| 267 | |||
| 268 | // parse dictionary files | ||
| 269 | for(pos = 0; ((dict_files[pos])); pos++) | ||
| 270 | { | ||
| 271 | if ((rc = tinyrad_dict_parse(dict, dict_files[pos], &errs, 0)) != TRAD_SUCCESS) | ||
| 272 | { | ||
| 273 | trutils_error(opts, errs, NULL); | ||
| 274 | tinyrad_strsfree(errs); | ||
| 275 | tinyrad_strsfree(dict_paths); | ||
| 276 | tinyrad_strsfree(dict_files); | ||
| 277 | tinyrad_free(dict); | ||
| 278 | return(trutils_exit_code(rc)); | ||
| 279 | }; | ||
| 280 | }; | ||
| 281 | }; | 242 | }; |
| 282 | tinyrad_strsfree(dict_paths); | 243 | tinyrad_strsfree(dict_paths); |
| 283 | tinyrad_strsfree(dict_files); | 244 | tinyrad_strsfree(dict_files); |
| @@ -306,6 +267,73 @@ int main(int argc, char * argv[]) | |||
| 306 | } | 267 | } |
| 307 | 268 | ||
| 308 | 269 | ||
| 270 | int | ||
| 271 | my_dict_load( | ||
| 272 | int opts, | ||
| 273 | TinyRadDict ** dictp, | ||
| 274 | char ** dict_files, | ||
| 275 | char ** dict_paths ) | ||
| 276 | { | ||
| 277 | int rc; | ||
| 278 | char ** errs; | ||
| 279 | size_t pos; | ||
| 280 | TinyRadDict * dict; | ||
| 281 | |||
| 282 | assert(dictp != NULL); | ||
| 283 | |||
| 284 | errs = NULL; | ||
| 285 | |||
| 286 | if (!(dict_files)) | ||
| 287 | return(TRAD_SUCCESS); | ||
| 288 | |||
| 289 | // initialize dictionary | ||
| 290 | if ((rc = tinyrad_dict_initialize(&dict, 0)) != TRAD_SUCCESS) | ||
| 291 | { | ||
| 292 | trutils_error(opts, NULL, "out of virtual memory"); | ||
| 293 | return(rc); | ||
| 294 | }; | ||
| 295 | |||
| 296 | // load defaults | ||
| 297 | if ((opts & MY_OPT_DICT_DEFAULTS)) | ||
| 298 | { | ||
| 299 | if ((rc = tinyrad_dict_defaults(dict, &errs, 0)) != TRAD_SUCCESS) | ||
| 300 | { | ||
| 301 | trutils_error(opts, errs, NULL); | ||
| 302 | tinyrad_strsfree(errs); | ||
| 303 | tinyrad_free(dict); | ||
| 304 | return(rc); | ||
| 305 | }; | ||
| 306 | }; | ||
| 307 | |||
| 308 | // set paths | ||
| 309 | for(pos = 0; ( ((dict_paths)) && ((dict_paths[pos])) ); pos++) | ||
| 310 | { | ||
| 311 | if ((rc = tinyrad_dict_add_path(dict, dict_paths[pos])) != TRAD_SUCCESS) | ||
| 312 | { | ||
| 313 | trutils_error(opts, NULL, "%s: %s", dict_paths[pos], tinyrad_strerror(rc)); | ||
| 314 | tinyrad_free(dict); | ||
| 315 | return(rc); | ||
| 316 | }; | ||
| 317 | }; | ||
| 318 | |||
| 319 | // parse dictionary files | ||
| 320 | for(pos = 0; ( ((dict_files)) && ((dict_files[pos])) ); pos++) | ||
| 321 | { | ||
| 322 | if ((rc = tinyrad_dict_parse(dict, dict_files[pos], &errs, 0)) != TRAD_SUCCESS) | ||
| 323 | { | ||
| 324 | trutils_error(opts, errs, NULL); | ||
| 325 | tinyrad_strsfree(errs); | ||
| 326 | tinyrad_free(dict); | ||
| 327 | return(rc); | ||
| 328 | }; | ||
| 329 | }; | ||
| 330 | |||
| 331 | *dictp = dict; | ||
| 332 | |||
| 333 | return(TRAD_SUCCESS); | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 309 | void my_usage(void) | 337 | void my_usage(void) |
| 310 | { | 338 | { |
| 311 | printf("Usage: %s [OPTIONS] url\n", PROGRAM_NAME); | 339 | printf("Usage: %s [OPTIONS] url\n", PROGRAM_NAME); |
