diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-09-12 12:14:14 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-09-12 12:50:52 -0700 |
| commit | 5f73996bf368e7f5d7249679c738a87873cb340d (patch) | |
| tree | d73413a4f8c0f4a07fd6a6a5a08a8d6bfae41851 | |
| parent | 74b067f6c381567e3da89e8a95ca13b4ffe81302 (diff) | |
| download | nng-5f73996bf368e7f5d7249679c738a87873cb340d.tar.gz nng-5f73996bf368e7f5d7249679c738a87873cb340d.tar.bz2 nng-5f73996bf368e7f5d7249679c738a87873cb340d.zip | |
fixes #720 fix for #715 still not right
The fix for #715 to address const is still not quite right.
In ISO C, the argv array is just char **. getopt() in POSIX uses
char *const[]. That part is right, but we should then not pass
const char ** in calls. Furthermore, really the optarg should also
not be constified.
This aligns us closer to getopt() and leads to the fewest problems.
This does represent a slight breaking change, but as old code should
still compile and run, we don't think we should bump the API version
for this change. Furthermore, we don't think anyone else is actually
using this API yet.
| -rw-r--r-- | docs/man/nng_opts_parse.3supp.adoc | 2 | ||||
| -rw-r--r-- | src/supplemental/util/options.c | 4 | ||||
| -rw-r--r-- | src/supplemental/util/options.h | 2 | ||||
| -rw-r--r-- | tests/options.c | 119 | ||||
| -rw-r--r-- | tools/nngcat/nngcat.c | 8 |
5 files changed, 64 insertions, 71 deletions
diff --git a/docs/man/nng_opts_parse.3supp.adoc b/docs/man/nng_opts_parse.3supp.adoc index 20a52109..2fbbe443 100644 --- a/docs/man/nng_opts_parse.3supp.adoc +++ b/docs/man/nng_opts_parse.3supp.adoc @@ -27,7 +27,7 @@ typedef struct nng_optspec { bool o_arg; // Option takes an argument if true } nng_optspec; -int nng_opts_parse(int argc, char *const *argv, const nng_optspec *spec, int *val, const char **arg, int *idx); +int nng_opts_parse(int argc, char *const *argv, const nng_optspec *spec, int *val, char **arg, int *idx); ---- == DESCRIPTION diff --git a/src/supplemental/util/options.c b/src/supplemental/util/options.c index c1881185..826a3692 100644 --- a/src/supplemental/util/options.c +++ b/src/supplemental/util/options.c @@ -17,13 +17,13 @@ // Call with optidx set to 1 to start parsing. int nng_opts_parse(int argc, char *const *argv, const nng_optspec *opts, int *val, - const char **optarg, int *optidx) + char **optarg, int *optidx) { const nng_optspec *opt; int matches; bool shortopt; size_t l; - const char * arg = argv[*optidx]; + char * arg = argv[*optidx]; int i; if ((i = *optidx) >= argc) { diff --git a/src/supplemental/util/options.h b/src/supplemental/util/options.h index 392c4a53..83969a90 100644 --- a/src/supplemental/util/options.h +++ b/src/supplemental/util/options.h @@ -39,7 +39,7 @@ typedef struct nng_optspec nng_optspec; // Returns -1 when the end of options is reached, 0 on success, or // NNG_EINVAL if the option parse is invalid for any reason. NNG_DECL int nng_opts_parse(int argc, char *const *argv, - const nng_optspec *opts, int *val, const char **optarg, int *optidx); + const nng_optspec *opts, int *val, char **optarg, int *optidx); #ifdef __cplusplus } diff --git a/tests/options.c b/tests/options.c index db04eea6..2d81bd83 100644 --- a/tests/options.c +++ b/tests/options.c @@ -27,14 +27,12 @@ static nng_optspec case1[] = { }; TestMain("Option Parsing", { - Convey("Simple works", { - - int opti = 1; - const char *av[6]; - int ac = 5; - int v; - const char *a = NULL; + int opti = 1; + char *av[6]; + int ac = 5; + int v; + char *a = NULL; av[0] = "program"; av[1] = "-f"; @@ -55,12 +53,11 @@ TestMain("Option Parsing", { }); Convey("Long works", { - - int opti = 1; - const char *av[6]; - int ac = 5; - int v; - const char *a = NULL; + int opti = 1; + char *av[6]; + int ac = 5; + int v; + char *a = NULL; av[0] = "program"; av[1] = "--flag"; @@ -81,12 +78,11 @@ TestMain("Option Parsing", { }); Convey("Attached short works", { - - int opti = 1; - const char *av[3]; - int ac = 3; - int v; - const char *a = NULL; + int opti = 1; + char *av[3]; + int ac = 3; + int v; + char *a = NULL; av[0] = "program"; av[1] = "-v123"; @@ -101,12 +97,11 @@ TestMain("Option Parsing", { }); Convey("Attached long (=) works", { - - int opti = 1; - const char *av[3]; - int ac = 3; - int v; - const char *a = NULL; + int opti = 1; + char *av[3]; + int ac = 3; + int v; + char *a = NULL; av[0] = "program"; av[1] = "--value=123"; @@ -121,12 +116,11 @@ TestMain("Option Parsing", { }); Convey("Attached long (:) works", { - - int opti = 1; - const char *av[3]; - int ac = 3; - int v; - const char *a = NULL; + int opti = 1; + char *av[3]; + int ac = 3; + int v; + char *a = NULL; av[0] = "program"; av[1] = "--value:123"; @@ -141,12 +135,11 @@ TestMain("Option Parsing", { }); Convey("Negative bad short works", { - - int opti = 1; - const char *av[3]; - int ac = 3; - int v; - const char *a = NULL; + int opti = 1; + char *av[3]; + int ac = 3; + int v; + char *a = NULL; av[0] = "program"; av[1] = "-Z"; @@ -156,12 +149,11 @@ TestMain("Option Parsing", { }); Convey("Negative bad long works", { - - int opti = 1; - const char *av[3]; - int ac = 3; - int v; - const char *a = NULL; + int opti = 1; + char *av[3]; + int ac = 3; + int v; + char *a = NULL; av[0] = "program"; av[1] = "--something"; @@ -171,11 +163,11 @@ TestMain("Option Parsing", { }); Convey("Separator flag works", { - int opti = 1; - const char *av[5]; - int ac = 5; - int v; - const char *a = NULL; + int opti = 1; + char *av[5]; + int ac = 5; + int v; + char *a = NULL; av[0] = "program"; av[1] = "-f"; @@ -190,22 +182,22 @@ TestMain("Option Parsing", { }); Convey("No options works", { - int opti = 1; - const char *av[1]; - int ac = 1; - int v; - const char *a = NULL; + int opti = 1; + char *av[1]; + int ac = 1; + int v; + char *a = NULL; av[0] = "program"; So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); }); Convey("No options (but arguments) works", { - int opti = 1; - const char *av[2]; - int ac = 2; - int v; - const char *a = NULL; + int opti = 1; + char *av[2]; + int ac = 2; + int v; + char *a = NULL; av[0] = "program"; av[1] = "123"; @@ -213,12 +205,11 @@ TestMain("Option Parsing", { So(opti == 1); }); Convey("Mixed long and short works", { - - int opti = 1; - const char *av[7]; - int ac = 7; - int v; - const char *a = NULL; + int opti = 1; + char *av[7]; + int ac = 7; + int v; + char *a = NULL; av[0] = "program"; av[1] = "--value=123"; diff --git a/tools/nngcat/nngcat.c b/tools/nngcat/nngcat.c index 132599e4..e81f59c0 100644 --- a/tools/nngcat/nngcat.c +++ b/tools/nngcat/nngcat.c @@ -200,7 +200,9 @@ static nng_optspec opts[] = { .o_arg = true, }, { - .o_name = "zt-home", .o_val = OPT_ZTHOME, .o_arg = true, + .o_name = "zt-home", + .o_val = OPT_ZTHOME, + .o_arg = true, }, { .o_name = "version", .o_short = 'V', .o_val = OPT_VERSION }, @@ -671,10 +673,10 @@ sendrecv(nng_socket sock) } int -main(int ac, const char **av) +main(int ac, char **av) { int idx; - const char * arg; + char * arg; int val; int rv; char scratch[512]; |
