From 01209d714d3720cd4aa18edac10a549177c81b1e Mon Sep 17 00:00:00 2001 From: katherine Date: Tue, 4 Jun 2019 17:52:21 -0700 Subject: add name-suffix and uthash-header options --- src/opt.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'src/opt.c') diff --git a/src/opt.c b/src/opt.c index a856c74..a50f4ca 100644 --- a/src/opt.c +++ b/src/opt.c @@ -1,11 +1,14 @@ #include "version.h" #include "opt.h" +#include "tok.h" #include "../reqs/simple-opt/simple-opt.h" #include #include #include +#include +#include static struct simple_opt options[] = { { SIMPLE_OPT_FLAG, 'h', "help", false, @@ -16,12 +19,18 @@ static struct simple_opt options[] = { "specify file to read from (default is stdin)", "" }, { SIMPLE_OPT_STRING, 'o', "output", true, "specify file to write to (default is stdout)", "" }, + { SIMPLE_OPT_STRING, 'u', "uthash-header", true, + "specify location header for uthash in the output (default is )", "" }, + { SIMPLE_OPT_STRING, 'n', "name-suffix", true, + "specify unique suffix to append to generated functions and types", "" }, { SIMPLE_OPT_END } }; void opt_parse(int argc, char **argv) { struct simple_opt_result result; + size_t i; + char c, cend; result = simple_opt_parse(argc, argv, options); @@ -33,7 +42,7 @@ void opt_parse(int argc, char **argv) /* help */ if (options[0].was_seen) { - simple_opt_print_usage(stdout, 70, argv[0], + simple_opt_print_usage(stdout, 80, argv[0], "[-i input.confconf] [-o output.h]", "confconf is a config file parser generator for C", options); @@ -45,6 +54,53 @@ void opt_parse(int argc, char **argv) puts(VERSION); exit(EXIT_SUCCESS); } + + /* check header */ + if (options[4].was_seen) { + cend = options[4].val.v_string[0]; + + if (cend != '"' && cend != '<') { + fprintf(stderr, "%s: err: badly-formatted uthash header `%s`\n", + argv[0], options[4].val.v_string); + exit(EXIT_FAILURE); + } + + if (cend == '<') + cend = '>'; + + for (i = 1, c = options[4].val.v_string[1]; + c && c != cend && c != '\n'; + c = options[4].val.v_string[++i] + ); + + if (c != cend + || options[4].val.v_string[i+1] != '\0' + || strlen(options[4].val.v_string) > TOK_MAX_LEN + || options[4].val.v_string[2] == '\0' + ) { + fprintf(stderr, "%s: err: badly-formatted uthash header `%s`\n", + argv[0], options[4].val.v_string); + exit(EXIT_FAILURE); + } + } + + /* check suffix */ + if (options[5].was_seen) { + if (options[5].val.v_string[i] == '\0') { + fprintf(stderr, "%s: err: badly-formatted name suffix ``\n", + argv[0]); + exit(EXIT_FAILURE); + } + + for (i = 0; options[5].val.v_string[i] != '\0'; i++) { + if (!isalnum(options[5].val.v_string[i]) ) { + fprintf(stderr, "%s: err: badly-formatted name suffix `%s`\n", + argv[0], options[5].val.v_string); + exit(EXIT_FAILURE); + } + } + } + } const char* opt_infile_str(void) @@ -60,3 +116,17 @@ const char* opt_outfile_str(void) ? options[3].val.v_string : NULL); } + +const char* opt_header_str(void) +{ + return (options[4].was_seen + ? options[4].val.v_string + : NULL); +} + +const char* opt_suffix_str(void) +{ + return (options[5].was_seen + ? options[5].val.v_string + : NULL); +} -- cgit v1.2.3