aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tinyrad.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tinyrad.c')
-rw-r--r--src/tinyrad.c390
1 files changed, 273 insertions, 117 deletions
diff --git a/src/tinyrad.c b/src/tinyrad.c
index 80008e6..ecad466 100644
--- a/src/tinyrad.c
+++ b/src/tinyrad.c
@@ -72,21 +72,32 @@
72#define MY_OPT_CONFIG_PRINT 0x0008UL 72#define MY_OPT_CONFIG_PRINT 0x0008UL
73 73
74 74
75#define TINYRAD_GETOPT_SHORT "h" 75#define MY_GETOPT_MATCHED -2
76#define MY_GETOPT_ERROR -3
77#define MY_GETOPT_EXIT -4
78
79
80#define TINYRAD_GETOPT_SHORT "d:f:hqVv"
76#define TINYRAD_GETOPT_LONG \ 81#define TINYRAD_GETOPT_LONG \
77 { "debug", optional_argument, NULL, 'd' }, \ 82 { "debug", optional_argument, NULL, 'd' }, \
78 { "file", optional_argument, NULL, 'f' }, \
79 { "help", no_argument, NULL, 'h' }, \ 83 { "help", no_argument, NULL, 'h' }, \
80 { "quiet", no_argument, NULL, 'q' }, \ 84 { "quiet", no_argument, NULL, 'q' }, \
81 { "silent", no_argument, NULL, 'q' }, \ 85 { "silent", no_argument, NULL, 'q' }, \
82 { "version", no_argument, NULL, 'V' }, \ 86 { "version", no_argument, NULL, 'V' }, \
83 { "verbose", no_argument, NULL, 'v' }, \ 87 { "verbose", no_argument, NULL, 'v' }, \
84 { "configuration", no_argument, NULL, 3 }, \
85 { "defaults", no_argument, NULL, 2 }, \
86 { "dictionary-dump", no_argument, NULL, 1 }, \
87 { NULL, 0, NULL, 0 } 88 { NULL, 0, NULL, 0 }
88 89
89 90
91#define TINYRAD_DICT_GETOPT_SHORT "bD:I:"
92#define TINYRAD_DICT_GETOPT_LONG \
93 { "builtin-dict", no_argument, NULL, 'b' }, \
94
95
96#define TINYRAD_REQ_GETOPT_SHORT "D:I:"
97#define TINYRAD_REQ_GETOPT_LONG \
98 { "file", optional_argument, NULL, 'f' }, \
99
100
90////////////////// 101//////////////////
91// // 102// //
92// Data Types // 103// Data Types //
@@ -109,7 +120,13 @@ struct tinyrad_config
109{ 120{
110 unsigned opts; 121 unsigned opts;
111 unsigned tr_opts; 122 unsigned tr_opts;
112 TinyRadCommand * cmd; 123 int padint;
124 int cmd_argc;
125 char ** cmd_argv;
126 const char * cmd_name;
127 size_t cmd_len;
128 void * padptr;
129 const TinyRadCommand * cmd;
113 TinyRad * tr; 130 TinyRad * tr;
114 const char * url; 131 const char * url;
115 char ** dict_files; 132 char ** dict_files;
@@ -149,6 +166,26 @@ tinyrad_cleanup(
149 166
150 167
151int 168int
169tinyrad_cmd_config(
170 TinyRadConf * cnf );
171
172
173int
174tinyrad_cmd_dict(
175 TinyRadConf * cnf );
176
177
178int
179tinyrad_getopt(
180 TinyRadConf * cnf,
181 int argc,
182 char * const * argv,
183 const char * short_opt,
184 const struct option * long_opt,
185 int * opt_index );
186
187
188int
152tinyrad_load_dict( 189tinyrad_load_dict(
153 TinyRadConf * cnf ); 190 TinyRadConf * cnf );
154 191
@@ -165,9 +202,36 @@ tinyrad_usage(
165///////////////// 202/////////////////
166#pragma mark - Variables 203#pragma mark - Variables
167 204
205#pragma mark tinyrad_cmdmap[]
168static const TinyRadCommand tinyrad_cmdmap[] = 206static const TinyRadCommand tinyrad_cmdmap[] =
169{ 207{
170 { 208 {
209 "configuration", // command name
210 &tinyrad_cmd_config, // entry function
211 TINYRAD_DICT_GETOPT_SHORT
212 TINYRAD_GETOPT_SHORT, // getopt short options
213 (struct option [])
214 { TINYRAD_DICT_GETOPT_LONG
215 TINYRAD_GETOPT_LONG
216 }, // getopt long options
217 0, 0, // min/max arguments
218 NULL, // extra cli usage
219 "print configuration" // command description
220 },
221 {
222 "dictionary", // command name
223 &tinyrad_cmd_dict, // entry function
224 TINYRAD_DICT_GETOPT_SHORT
225 TINYRAD_GETOPT_SHORT, // getopt short options
226 (struct option [])
227 { TINYRAD_DICT_GETOPT_LONG
228 TINYRAD_GETOPT_LONG
229 }, // getopt long options
230 0, 0, // min/max arguments
231 NULL, // extra cli usage
232 "print processed dictionary" // command description
233 },
234 {
171 "help", // command name 235 "help", // command name
172 &tinyrad_usage, // entry function 236 &tinyrad_usage, // entry function
173 TINYRAD_GETOPT_SHORT, // getopt short options 237 TINYRAD_GETOPT_SHORT, // getopt short options
@@ -179,6 +243,7 @@ static const TinyRadCommand tinyrad_cmdmap[] =
179 { NULL, NULL, NULL, NULL, -1, -1, NULL, NULL } 243 { NULL, NULL, NULL, NULL, -1, -1, NULL, NULL }
180}; 244};
181 245
246
182///////////////// 247/////////////////
183// // 248// //
184// Functions // 249// Functions //
@@ -188,30 +253,17 @@ static const TinyRadCommand tinyrad_cmdmap[] =
188 253
189int main(int argc, char * argv[]) 254int main(int argc, char * argv[])
190{ 255{
191 int opt;
192 int c; 256 int c;
193 int opt_index; 257 int opt_index;
194 int rc; 258 int rc;
259 size_t pos;
195 TinyRadDict * dict; 260 TinyRadDict * dict;
196 TinyRadConf cnfdata; 261 TinyRadConf cnfdata;
197 TinyRadConf * cnf; 262 TinyRadConf * cnf;
198 263
199 // getopt options 264 // getopt options
200 static char short_opt[] = "D:d:f:hI:qVv"; 265 static char short_opt[] = "+" TINYRAD_GETOPT_SHORT;
201 static struct option long_opt[] = 266 static struct option long_opt[] = { TINYRAD_GETOPT_LONG };
202 {
203 {"debug", optional_argument, NULL, 'd' },
204 {"file", optional_argument, NULL, 'f' },
205 {"help", no_argument, NULL, 'h' },
206 {"quiet", no_argument, NULL, 'q' },
207 {"silent", no_argument, NULL, 'q' },
208 {"version", no_argument, NULL, 'V' },
209 {"verbose", no_argument, NULL, 'v' },
210 {"configuration", no_argument, NULL, 3 },
211 {"defaults", no_argument, NULL, 2 },
212 {"dictionary-dump", no_argument, NULL, 1 },
213 { NULL, 0, NULL, 0 }
214 };
215 267
216 trutils_initialize(PROGRAM_NAME); 268 trutils_initialize(PROGRAM_NAME);
217 269
@@ -219,66 +271,22 @@ int main(int argc, char * argv[])
219 cnf = &cnfdata; 271 cnf = &cnfdata;
220 dict = NULL; 272 dict = NULL;
221 273
222 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) 274 while((c = tinyrad_getopt(cnf, argc, argv, short_opt, long_opt, &opt_index)) != -1)
223 { 275 {
224 switch(c) 276 switch(c)
225 { 277 {
226 case -1: /* no more arguments */ 278 case MY_GETOPT_MATCHED: /* captured by common options */
227 case 0: /* long options toggles */ 279 case -1: /* no more arguments */
280 case 0: /* long options toggles */
228 break; 281 break;
229 282
230 case 1: 283 case MY_GETOPT_EXIT:
231 cnf->opts |= MY_OPT_DICT_DUMP; 284 tinyrad_cleanup(cnf);
232 break;
233
234 case 2:
235 cnf->tr_opts |= TRAD_BUILTIN_DICT;
236 break;
237
238 case 3:
239 cnf->opts |= MY_OPT_CONFIG_PRINT;
240 break;
241
242 case 'D':
243 if (tinyrad_strsadd(&cnf->dict_files, optarg) != TRAD_SUCCESS)
244 {
245 trutils_error(cnf->opts, NULL, "out of virtual memory");
246 tinyrad_cleanup(cnf);
247 return(trutils_exit_code(TRAD_ENOMEM));
248 };
249 break;
250
251 case 'd':
252 opt = ((optarg)) ? (int)strtol(optarg, NULL, 0) : TRAD_DEBUG_ANY;
253 tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt);
254 break;
255
256 case 'h':
257 tinyrad_usage(cnf);
258 return(0); 285 return(0);
259 286
260 case 'I': 287 case MY_GETOPT_ERROR:
261 if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) 288 tinyrad_cleanup(cnf);
262 { 289 return(1);
263 trutils_error(cnf->opts, NULL, "out of virtual memory");
264 tinyrad_cleanup(cnf);
265 return(trutils_exit_code(TRAD_ENOMEM));
266 };
267 break;
268
269 case 'q':
270 cnf->opts |= TRUTILS_OPT_QUIET;
271 cnf->opts &= ~TRUTILS_OPT_VERBOSE;
272 break;
273
274 case 'V':
275 trutils_version();
276 return(0);
277
278 case 'v':
279 cnf->opts |= TRUTILS_OPT_VERBOSE;
280 cnf->opts &= ~TRUTILS_OPT_QUIET;
281 break;
282 290
283 case '?': 291 case '?':
284 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); 292 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
@@ -290,50 +298,90 @@ int main(int argc, char * argv[])
290 return(1); 298 return(1);
291 }; 299 };
292 }; 300 };
293 if (optind < argc) 301 if ((argc - optind) < 1)
302 {
303 fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME);
304 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
305 return(1);
306 };
307 cnf->cmd_argc = (argc - optind);
308 cnf->cmd_argv = &argv[optind];
309 cnf->cmd_name = cnf->cmd_argv[0];
310 cnf->cmd_len = strlen(cnf->cmd_name);
311
312 // looks up widget
313 for(pos = 0; (tinyrad_cmdmap[pos].cmd_name != NULL); pos++)
294 { 314 {
295 cnf->url = argv[optind]; 315 if ((strncmp(cnf->cmd_name, tinyrad_cmdmap[pos].cmd_name, cnf->cmd_len)))
296 optind++; 316 continue;
317 if ((cnf->cmd))
318 {
319 fprintf(stderr, "%s: ambiguous command -- \"%s\"\n", PROGRAM_NAME, cnf->cmd_name);
320 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
321 return(1);
322 };
323 cnf->cmd = &tinyrad_cmdmap[pos];
297 }; 324 };
298 if (optind < argc) 325 if (!(cnf->cmd))
299 { 326 {
300 fprintf(stderr, "%s: unrecognized argument -- %s\n", PROGRAM_NAME, argv[optind]); 327 fprintf(stderr, "%s: unknown command -- \"%s\"\n", PROGRAM_NAME, cnf->cmd_name);
301 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); 328 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
302 tinyrad_cleanup(cnf);
303 return(1); 329 return(1);
304 }; 330 };
305 331 if (!(cnf->cmd->cmd_func))
306 // load TinyRad handle
307 rc = tinyrad_load_dict(cnf);
308 if (rc != TRAD_SUCCESS)
309 { 332 {
310 tinyrad_cleanup(cnf); 333 fprintf(stderr, "%s: command not implemented -- \"%s\"\n", PROGRAM_NAME, cnf->cmd->cmd_name);
311 return(trutils_exit_code(rc)); 334 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
335 return(1);
312 }; 336 };
313 337
314 // display dictionary 338 // process widget cli options
315 if ((cnf->opts & MY_OPT_DICT_DUMP)) 339 optind = 1;
340 opt_index = 0;
341 while((c = tinyrad_getopt(cnf, cnf->cmd_argc, cnf->cmd_argv, cnf->cmd->cmd_shortopts, cnf->cmd->cmd_longopts, &opt_index)) != -1)
316 { 342 {
317 tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict); 343 switch(c)
318 tinyrad_dict_print(dict, 0xffff); 344 {
319 tinyrad_cleanup(cnf); 345 case -2: /* captured by common options */
320 tinyrad_free(dict); 346 case -1: /* no more arguments */
321 return(0); 347 case 0: /* long options toggles */
322 }; 348 break;
349
350 case MY_GETOPT_EXIT:
351 tinyrad_cleanup(cnf);
352 return(0);
323 353
324 // display configuration 354 case MY_GETOPT_ERROR:
325 if ((cnf->opts & MY_OPT_CONFIG_PRINT)) 355 tinyrad_cleanup(cnf);
356 return(1);
357
358 case '?':
359 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->cmd_name);
360 return(1);
361
362 default:
363 fprintf(stderr, "%s: %s: unrecognized option `--%c'\n", PROGRAM_NAME, cnf->cmd_name, c);
364 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->cmd_name);
365 return(1);
366 };
367 };
368 if ((cnf->cmd_argc - optind) < cnf->cmd->cmd_min_arg)
326 { 369 {
327 tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict); 370 fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME);
328 tinyrad_conf_print(cnf->tr, dict); 371 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->cmd_name);
329 tinyrad_cleanup(cnf); 372 return(1);
330 tinyrad_free(dict); 373 };
331 return(0); 374 if ((optind+cnf->cmd->cmd_max_arg) > cnf->cmd_argc)
375 {
376 fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->cmd_argv[optind+1]);
377 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->cmd_name);
378 return(1);
332 }; 379 };
333 380
381 // call widget
382 rc = cnf->cmd->cmd_func(cnf);
334 tinyrad_cleanup(cnf); 383 tinyrad_cleanup(cnf);
335 384 return(trutils_exit_code(rc));
336 return(0);
337} 385}
338 386
339 387
@@ -349,6 +397,101 @@ tinyrad_cleanup(
349 397
350 398
351int 399int
400tinyrad_cmd_config(
401 TinyRadConf * cnf )
402{
403 int rc;
404 TinyRadDict * dict;
405 if ((rc = tinyrad_load_dict(cnf)) != TRAD_SUCCESS)
406 return(rc);
407 tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict);
408 tinyrad_conf_print(cnf->tr, dict);
409 tinyrad_free(dict);
410 return(TRAD_SUCCESS);
411}
412
413
414int
415tinyrad_cmd_dict(
416 TinyRadConf * cnf )
417{
418 int rc;
419 TinyRadDict * dict;
420 if ((rc = tinyrad_load_dict(cnf)) != TRAD_SUCCESS)
421 return(rc);
422 tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict);
423 tinyrad_dict_print(dict, 0xffff);
424 tinyrad_free(dict);
425 return(TRAD_SUCCESS);
426}
427
428
429int
430tinyrad_getopt(
431 TinyRadConf * cnf,
432 int argc,
433 char * const * argv,
434 const char * short_opt,
435 const struct option * long_opt,
436 int * opt_index )
437{
438 int c;
439 int opt;
440
441 switch(c = getopt_long(argc, argv, short_opt, long_opt, opt_index))
442 {
443 case 'b':
444 cnf->tr_opts |= TRAD_BUILTIN_DICT;
445 return(MY_GETOPT_MATCHED);
446
447 case 'D':
448 if (tinyrad_strsadd(&cnf->dict_files, optarg) != TRAD_SUCCESS)
449 {
450 trutils_error(cnf->opts, NULL, "out of virtual memory");
451 return(MY_GETOPT_ERROR);
452 };
453 return(MY_GETOPT_MATCHED);
454
455 case 'd':
456 opt = ((optarg)) ? (int)strtol(optarg, NULL, 0) : TRAD_DEBUG_ANY;
457 tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt);
458 return(MY_GETOPT_MATCHED);
459
460 case 'h':
461 tinyrad_usage(cnf);
462 return(MY_GETOPT_EXIT);
463
464 case 'I':
465 if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS)
466 {
467 trutils_error(cnf->opts, NULL, "out of virtual memory");
468 return(MY_GETOPT_ERROR);
469 };
470 return(MY_GETOPT_MATCHED);
471
472 case 'q':
473 cnf->opts |= TRUTILS_OPT_QUIET;
474 cnf->opts &= ~TRUTILS_OPT_VERBOSE;
475 return(MY_GETOPT_MATCHED);
476
477 case 'V':
478 trutils_version();
479 return(MY_GETOPT_EXIT);
480
481 case 'v':
482 cnf->opts |= TRUTILS_OPT_VERBOSE;
483 cnf->opts &= ~TRUTILS_OPT_QUIET;
484 return(MY_GETOPT_MATCHED);
485
486 default:
487 break;
488 };
489
490 return(c);
491}
492
493
494int
352tinyrad_load_dict( 495tinyrad_load_dict(
353 TinyRadConf * cnf ) 496 TinyRadConf * cnf )
354{ 497{
@@ -404,20 +547,33 @@ int
404tinyrad_usage( 547tinyrad_usage(
405 TinyRadConf * cnf ) 548 TinyRadConf * cnf )
406{ 549{
407 int i; 550 int i;
408 printf("Usage: %s [OPTIONS] [url]\n", PROGRAM_NAME); 551 const char * name;
552 const char * help;
553 const char * s;
554
555 name = "COMMAND";
556 help = " ...";
557 s = TINYRAD_GETOPT_SHORT;
558
559 if ((cnf->cmd))
560 {
561 name = cnf->cmd->cmd_name;
562 help = ((cnf->cmd->cmd_help)) ? cnf->cmd->cmd_help : "";
563 s = ((cnf->cmd->cmd_shortopts)) ? cnf->cmd->cmd_shortopts : TINYRAD_GETOPT_SHORT;
564 };
565
566 printf("Usage: %s %s [OPTIONS]%s\n", PROGRAM_NAME, name, help);
409 printf("OPTIONS:\n"); 567 printf("OPTIONS:\n");
410 printf(" -D dictionary include dictionary\n"); 568 if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n");
411 printf(" -d level, --debug=level print debug messages\n"); 569 if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n");
412 printf(" -f file, --file=file attribute list\n"); 570 if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n");
413 printf(" -h, --help print this help and exit\n"); 571 if ((strchr(s, 'f'))) printf(" -f file, --file=file attribute list\n");
414 printf(" -I path add path to dictionary search paths\n"); 572 if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n");
415 printf(" -q, --quiet, --silent do not print messages\n"); 573 if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n");
416 printf(" -V, --version print version number and exit\n"); 574 if ((strchr(s, 'q'))) printf(" -q, --quiet, --silent do not print messages\n");
417 printf(" -v, --verbose print verbose messages\n"); 575 if ((strchr(s, 'V'))) printf(" -V, --version print version number and exit\n");
418 printf(" --defaults load default dictionaries\n"); 576 if ((strchr(s, 'v'))) printf(" -v, --verbose print verbose messages\n");
419 printf(" --dictionary-dump print imported dictionaries\n");
420 printf(" --configuration print configuration\n");
421 if (!(cnf->cmd)) 577 if (!(cnf->cmd))
422 { 578 {
423 printf("COMMANDS:\n"); 579 printf("COMMANDS:\n");