X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Fopt.h;h=539a0a664de155962f3d2efdf116eca52d25fcee;hp=8796752a133f9f8d70854af0d99ba52af4d8b51c;hb=e34192d580958aaffff3754a4e2bf1eccbb489f8;hpb=1b2efd5679452d03ff7a0dacb496eb3830b37434 diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h index 8796752a..539a0a66 100644 --- a/ccan/opt/opt.h +++ b/ccan/opt/opt.h @@ -1,7 +1,9 @@ #ifndef CCAN_OPT_H #define CCAN_OPT_H +#include #include #include +#include struct opt_table; @@ -28,7 +30,7 @@ struct opt_table; * OPT_WITH_ARG() */ #define OPT_WITHOUT_ARG(names, cb, arg, desc) \ - { (names), OPT_CB_NOARG((cb), (arg)), (desc) } + { (names), OPT_CB_NOARG((cb), (arg)), { (arg) }, (desc) } /** * OPT_WITH_ARG() - macro for initializing long and short option (with arg) @@ -63,7 +65,7 @@ struct opt_table; * OPT_WITHOUT_ARG() */ #define OPT_WITH_ARG(name, cb, show, arg, desc) \ - { (name), OPT_CB_ARG((cb), (show), (arg)), (desc) } + { (name), OPT_CB_ARG((cb), (show), (arg)), { (arg) }, (desc) } /** * OPT_SUBTABLE() - macro for including another table inside a table. @@ -72,14 +74,15 @@ struct opt_table; */ #define OPT_SUBTABLE(table, desc) \ { (const char *)(table), OPT_SUBTABLE, \ - sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, NULL, (desc) } + sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, \ + { NULL }, (desc) } /** * OPT_ENDTABLE - macro to create final entry in table. * * This must be the final element in the opt_table array. */ -#define OPT_ENDTABLE { NULL, OPT_END } +#define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, { NULL }, NULL } /** * opt_register_table - register a table of options @@ -125,7 +128,7 @@ void opt_register_table(const struct opt_table *table, const char *desc); * string and return false. */ #define opt_register_noarg(names, cb, arg, desc) \ - _opt_register((names), OPT_CB_NOARG((cb), (arg)), (desc)) + _opt_register((names), OPT_CB_NOARG((cb), (arg)), (arg), (desc)) /** * opt_register_arg - register an option with an arguments @@ -155,7 +158,7 @@ void opt_register_table(const struct opt_table *table, const char *desc); * opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden); */ #define opt_register_arg(names, cb, show, arg, desc) \ - _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc)) + _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (arg), (desc)) /** * opt_parse - parse arguments. @@ -182,6 +185,14 @@ void opt_register_table(const struct opt_table *table, const char *desc); */ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)); +/** + * opt_free_table - free the table. + * + * This frees the internal memory. Call this as the last + * opt function. + */ +void opt_free_table(void); + /** * opt_log_stderr - print message to stderr. * @fmt: printf-style format. @@ -293,7 +304,11 @@ struct opt_table { char *(*cb)(void *arg); /* OPT_NOARG */ char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */ void (*show)(char buf[OPT_SHOW_LEN], const void *arg); - void *arg; + union { + const void *carg; + void *arg; + size_t tlen; + } u; const char *desc; }; @@ -304,7 +319,7 @@ struct opt_table { char *(*)(typeof(*(arg))*), \ char *(*)(const typeof(*(arg))*), \ char *(*)(const void *)), \ - NULL, NULL, (arg) + NULL, NULL /* Resolves to the four parameters for arg callbacks. */ #define OPT_CB_ARG(cb, show, arg) \ @@ -314,17 +329,16 @@ struct opt_table { char *(*)(const char *, const typeof(*(arg))*), \ char *(*)(const char *, const void *)), \ cast_if_type(void (*)(char buf[], const void *), (show), (show)+0, \ - void (*)(char buf[], const typeof(*(arg))*)), \ - (arg) + void (*)(char buf[], const typeof(*(arg))*)) /* Non-typesafe register function. */ void _opt_register(const char *names, enum opt_type type, char *(*cb)(void *arg), char *(*cb_arg)(const char *optarg, void *arg), void (*show)(char buf[OPT_SHOW_LEN], const void *arg), - void *arg, const char *desc); + const void *arg, const char *desc); /* We use this to get typechecking for OPT_SUBTABLE */ -static inline int _check_is_entry(struct opt_table *e) { return 0; } +static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; } #endif /* CCAN_OPT_H */