diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-08 23:45:20 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-08 23:45:20 -0900 |
| commit | da00da010e80f5ec4b838ef12a03a4a82c2c9bcb (patch) | |
| tree | 5e6f720b92855237690914b817046de5842a69cb /src | |
| parent | e35f3778ef091f46bc1727a5df3da63edbc22de1 (diff) | |
| download | tinyrad-da00da010e80f5ec4b838ef12a03a4a82c2c9bcb.tar.gz tinyrad-da00da010e80f5ec4b838ef12a03a4a82c2c9bcb.tar.bz2 tinyrad-da00da010e80f5ec4b838ef12a03a4a82c2c9bcb.tar.xz tinyrad-da00da010e80f5ec4b838ef12a03a4a82c2c9bcb.zip | |
tinyrad: refactoring dictionary management
Diffstat (limited to 'src')
| -rw-r--r-- | src/tinyrad.c | 121 |
1 files changed, 83 insertions, 38 deletions
diff --git a/src/tinyrad.c b/src/tinyrad.c index 5a9db3d..d4c37ad 100644 --- a/src/tinyrad.c +++ b/src/tinyrad.c | |||
| @@ -66,8 +66,9 @@ | |||
| 66 | #undef PROGRAM_NAME | 66 | #undef PROGRAM_NAME |
| 67 | #define PROGRAM_NAME "tinyrad" | 67 | #define PROGRAM_NAME "tinyrad" |
| 68 | 68 | ||
| 69 | #define MY_OPT_DICT_DUMP 0x0001UL | 69 | #define MY_OPT_DICT_DEFAULTS 0x0001UL |
| 70 | #define MY_OPT_DICT_LOADED 0x0002UL | 70 | #define MY_OPT_DICT_DUMP 0x0002UL |
| 71 | #define MY_OPT_DICT_LOADED 0x0004UL | ||
| 71 | 72 | ||
| 72 | 73 | ||
| 73 | ////////////////// | 74 | ////////////////// |
| @@ -114,10 +115,13 @@ int main(int argc, char * argv[]) | |||
| 114 | int c; | 115 | int c; |
| 115 | int opt_index; | 116 | int opt_index; |
| 116 | int rc; | 117 | int rc; |
| 118 | size_t pos; | ||
| 117 | TinyRadDict * dict; | 119 | TinyRadDict * dict; |
| 118 | TinyRad * tr; | 120 | TinyRad * tr; |
| 119 | const char * url; | 121 | const char * url; |
| 120 | char ** errs; | 122 | char ** errs; |
| 123 | char ** dict_files; | ||
| 124 | char ** dict_paths; | ||
| 121 | unsigned opts; | 125 | unsigned opts; |
| 122 | 126 | ||
| 123 | // getopt options | 127 | // getopt options |
| @@ -138,13 +142,10 @@ int main(int argc, char * argv[]) | |||
| 138 | 142 | ||
| 139 | trutils_initialize(PROGRAM_NAME); | 143 | trutils_initialize(PROGRAM_NAME); |
| 140 | 144 | ||
| 141 | opts = 0; | 145 | opts = 0; |
| 142 | 146 | dict = NULL; | |
| 143 | if (tinyrad_dict_initialize(&dict, 0) != TRAD_SUCCESS) | 147 | dict_files = NULL; |
| 144 | { | 148 | dict_paths = NULL; |
| 145 | fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); | ||
| 146 | return(1); | ||
| 147 | }; | ||
| 148 | 149 | ||
| 149 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | 150 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) |
| 150 | { | 151 | { |
| @@ -159,24 +160,17 @@ int main(int argc, char * argv[]) | |||
| 159 | break; | 160 | break; |
| 160 | 161 | ||
| 161 | case 2: | 162 | case 2: |
| 162 | if (tinyrad_dict_defaults(dict, &errs, 0) != TRAD_SUCCESS) | 163 | opts |= MY_OPT_DICT_DEFAULTS; |
| 163 | { | ||
| 164 | trutils_error(opts, errs, NULL); | ||
| 165 | tinyrad_free(dict); | ||
| 166 | return(1); | ||
| 167 | }; | ||
| 168 | opts |= MY_OPT_DICT_LOADED; | ||
| 169 | break; | 164 | break; |
| 170 | 165 | ||
| 171 | case 'D': | 166 | case 'D': |
| 172 | if (tinyrad_dict_parse(dict, optarg, &errs, 0) != TRAD_SUCCESS) | 167 | if (tinyrad_strsadd(&dict_files, optarg) != TRAD_SUCCESS) |
| 173 | { | 168 | { |
| 174 | tinyrad_free(dict); | 169 | trutils_error(opts, NULL, "out of virtual memory"); |
| 175 | trutils_error(opts, errs, NULL); | 170 | tinyrad_strsfree(dict_files); |
| 176 | tinyrad_strsfree(errs); | 171 | tinyrad_strsfree(dict_paths); |
| 177 | return(1); | 172 | return(trutils_exit_code(TRAD_ENOMEM)); |
| 178 | }; | 173 | }; |
| 179 | opts |= MY_OPT_DICT_LOADED; | ||
| 180 | break; | 174 | break; |
| 181 | 175 | ||
| 182 | case 'd': | 176 | case 'd': |
| @@ -189,11 +183,12 @@ int main(int argc, char * argv[]) | |||
| 189 | return(0); | 183 | return(0); |
| 190 | 184 | ||
| 191 | case 'I': | 185 | case 'I': |
| 192 | if ((rc = tinyrad_dict_add_path(dict, optarg)) != TRAD_SUCCESS) | 186 | if (tinyrad_strsadd(&dict_paths, optarg) != TRAD_SUCCESS) |
| 193 | { | 187 | { |
| 194 | tinyrad_free(dict); | 188 | trutils_error(opts, NULL, "out of virtual memory"); |
| 195 | trutils_error(opts, NULL, "%s: %s", optarg, tinyrad_strerror(rc)); | 189 | tinyrad_strsfree(dict_files); |
| 196 | return(1); | 190 | tinyrad_strsfree(dict_paths); |
| 191 | return(trutils_exit_code(TRAD_ENOMEM)); | ||
| 197 | }; | 192 | }; |
| 198 | break; | 193 | break; |
| 199 | 194 | ||
| @@ -221,41 +216,91 @@ int main(int argc, char * argv[]) | |||
| 221 | return(1); | 216 | return(1); |
| 222 | }; | 217 | }; |
| 223 | }; | 218 | }; |
| 224 | |||
| 225 | if (optind >= argc) | 219 | if (optind >= argc) |
| 226 | { | 220 | { |
| 227 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | 221 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); |
| 228 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 222 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); |
| 223 | tinyrad_strsfree(dict_files); | ||
| 224 | tinyrad_strsfree(dict_paths); | ||
| 229 | return(1); | 225 | return(1); |
| 230 | }; | 226 | }; |
| 231 | url = argv[optind]; | 227 | url = argv[optind]; |
| 232 | optind++; | 228 | optind++; |
| 233 | 229 | ||
| 234 | if (!(opts & MY_OPT_DICT_LOADED)) | 230 | // load RADIUS dictionary |
| 231 | if ((dict_files)) | ||
| 235 | { | 232 | { |
| 236 | if (tinyrad_dict_defaults(dict, &errs, 0) != TRAD_SUCCESS) | 233 | // initialize dictionary |
| 234 | if (tinyrad_dict_initialize(&dict, 0) != TRAD_SUCCESS) | ||
| 237 | { | 235 | { |
| 238 | trutils_error(opts, errs, NULL); | 236 | trutils_error(opts, NULL, "out of virtual memory"); |
| 239 | tinyrad_free(dict); | 237 | tinyrad_strsfree(dict_files); |
| 240 | return(1); | 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 | }; | ||
| 241 | }; | 280 | }; |
| 242 | }; | 281 | }; |
| 282 | tinyrad_strsfree(dict_paths); | ||
| 283 | tinyrad_strsfree(dict_files); | ||
| 243 | 284 | ||
| 244 | if ((opts & MY_OPT_DICT_DUMP)) | 285 | // initialize tinyrad handle |
| 286 | if ((tinyrad_initialize(&tr, dict, url, 0)) != TRAD_SUCCESS) | ||
| 245 | { | 287 | { |
| 246 | tinyrad_dict_print(dict, 0xffff); | ||
| 247 | tinyrad_free(dict); | 288 | tinyrad_free(dict); |
| 248 | return(0); | 289 | return(1); |
| 249 | }; | 290 | }; |
| 291 | tinyrad_free(dict); | ||
| 250 | 292 | ||
| 251 | if ((tinyrad_initialize(&tr, dict, url, 0)) != TRAD_SUCCESS) | 293 | // display dictionary |
| 294 | if ((opts & MY_OPT_DICT_DUMP)) | ||
| 252 | { | 295 | { |
| 296 | tinyrad_get_option(tr, TRAD_OPT_DICTIONARY, &dict); | ||
| 297 | tinyrad_dict_print(dict, 0xffff); | ||
| 298 | tinyrad_free(tr); | ||
| 253 | tinyrad_free(dict); | 299 | tinyrad_free(dict); |
| 254 | return(1); | 300 | return(0); |
| 255 | }; | 301 | }; |
| 256 | 302 | ||
| 257 | tinyrad_free(tr); | 303 | tinyrad_free(tr); |
| 258 | tinyrad_free(dict); | ||
| 259 | 304 | ||
| 260 | return(0); | 305 | return(0); |
| 261 | } | 306 | } |
