opt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #ifndef CCAN_OPT_H
  2. #define CCAN_OPT_H
  3. #include <ccan/compiler/compiler.h>
  4. #include <ccan/typesafe_cb/typesafe_cb.h>
  5. #include <stdbool.h>
  6. #include <stdlib.h>
  7. struct opt_table;
  8. /**
  9. * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
  10. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  11. * @cb: the callback when the option is found.
  12. * @arg: the argument to hand to @cb.
  13. * @desc: the description for opt_usage(), or opt_hidden.
  14. *
  15. * This is a typesafe wrapper for initializing a struct opt_table. The callback
  16. * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
  17. * where "type" is the type of the @arg argument.
  18. *
  19. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  20. * returned string to form an error message for errlog(), free() the
  21. * string and return false.
  22. *
  23. * Any number of equivalent short or long options can be listed in @names,
  24. * separated by '|'. Short options are a single hyphen followed by a single
  25. * character, long options are two hyphens followed by one or more characters.
  26. *
  27. * See Also:
  28. * OPT_WITH_ARG()
  29. */
  30. #define OPT_WITHOUT_ARG(names, cb, arg, desc) \
  31. { (names), OPT_CB_NOARG((cb), (arg)), { (arg) }, (desc) }
  32. /**
  33. * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
  34. * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
  35. * @cb: the callback when the option is found (along with <arg>).
  36. * @show: the callback to print the value in get_usage (or NULL)
  37. * @arg: the argument to hand to @cb and @show
  38. * @desc: the description for opt_usage(), or opt_hidden.
  39. *
  40. * This is a typesafe wrapper for initializing a struct opt_table. The callback
  41. * is of type "char *cb(const char *, type *)",
  42. * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
  43. * where "type" is the type of the @arg argument. The first argument to the
  44. * @cb is the argument found on the commandline.
  45. *
  46. * Similarly, if @show is not NULL, it should be of type "void *show(char *,
  47. * const type *)". It should write up to OPT_SHOW_LEN bytes into the first
  48. * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
  49. * nul-terminate that buffer.
  50. *
  51. * Any number of equivalent short or long options can be listed in @names,
  52. * separated by '|'. Short options are a single hyphen followed by a single
  53. * character, long options are two hyphens followed by one or more characters.
  54. * A space or equals in @names is ignored for parsing, and only used
  55. * for printing the usage.
  56. *
  57. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  58. * returned string to form an error message for errlog(), free() the
  59. * string and return false.
  60. *
  61. * See Also:
  62. * OPT_WITHOUT_ARG()
  63. */
  64. #define OPT_WITH_ARG(name, cb, show, arg, desc) \
  65. { (name), OPT_CB_ARG((cb), (show), (arg)), { (arg) }, (desc) }
  66. /**
  67. * OPT_WITH_CBARG() - variant of OPT_WITH_ARG which assigns arguments to arg
  68. * and then performs the callback function on the args as well.
  69. */
  70. #define OPT_WITH_CBARG(name, cb, show, arg, desc) \
  71. { (name), OPT_CB_WITHARG((cb), (show), (arg)), { (arg) }, (desc) }
  72. /**
  73. * OPT_SUBTABLE() - macro for including another table inside a table.
  74. * @table: the table to include in this table.
  75. * @desc: description of this subtable (for opt_usage()) or NULL.
  76. */
  77. #define OPT_SUBTABLE(table, desc) \
  78. { (const char *)(table), OPT_SUBTABLE, \
  79. sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, \
  80. { NULL }, (desc) }
  81. /**
  82. * OPT_ENDTABLE - macro to create final entry in table.
  83. *
  84. * This must be the final element in the opt_table array.
  85. */
  86. #define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, { NULL }, NULL }
  87. /**
  88. * opt_register_table - register a table of options
  89. * @table: the table of options
  90. * @desc: description of this subtable (for opt_usage()) or NULL.
  91. *
  92. * The table must be terminated by OPT_ENDTABLE.
  93. *
  94. * Example:
  95. * static int verbose = 0;
  96. * static struct opt_table opts[] = {
  97. * OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose,
  98. * "Verbose mode (can be specified more than once)"),
  99. * OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose,
  100. * "Verbose mode (can be specified more than once)"),
  101. * OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
  102. * "args...\nA silly test program.",
  103. * "Print this message."),
  104. * OPT_ENDTABLE
  105. * };
  106. *
  107. * ...
  108. * opt_register_table(opts, NULL);
  109. */
  110. void opt_register_table(const struct opt_table *table, const char *desc);
  111. /**
  112. * opt_register_noarg - register an option with no arguments
  113. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  114. * @cb: the callback when the option is found.
  115. * @arg: the argument to hand to @cb.
  116. * @desc: the verbose description of the option (for opt_usage()), or NULL.
  117. *
  118. * This is used for registering a single commandline option which takes
  119. * no argument.
  120. *
  121. * The callback is of type "char *cb(type *)", "char *cb(const type *)"
  122. * or "char *cb(void *)", where "type" is the type of the @arg
  123. * argument.
  124. *
  125. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  126. * returned string to form an error message for errlog(), free() the
  127. * string and return false.
  128. */
  129. #define opt_register_noarg(names, cb, arg, desc) \
  130. _opt_register((names), OPT_CB_NOARG((cb), (arg)), (arg), (desc))
  131. /**
  132. * opt_register_arg - register an option with an arguments
  133. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  134. * @cb: the callback when the option is found.
  135. * @show: the callback to print the value in get_usage (or NULL)
  136. * @arg: the argument to hand to @cb.
  137. * @desc: the verbose description of the option (for opt_usage()), or NULL.
  138. *
  139. * This is used for registering a single commandline option which takes
  140. * an argument.
  141. *
  142. * The callback is of type "char *cb(const char *, type *)",
  143. * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
  144. * where "type" is the type of the @arg argument. The first argument to the
  145. * @cb is the argument found on the commandline.
  146. *
  147. * At least one of @longopt and @shortopt must be non-zero. If the
  148. * @cb returns false, opt_parse() will stop parsing and return false.
  149. *
  150. * Example:
  151. * static char *explode(const char *optarg, void *unused)
  152. * {
  153. * errx(1, "BOOM! %s", optarg);
  154. * }
  155. * ...
  156. * opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
  157. */
  158. #define opt_register_arg(names, cb, show, arg, desc) \
  159. _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (arg), (desc))
  160. /**
  161. * opt_parse - parse arguments.
  162. * @argc: pointer to argc
  163. * @argv: argv array.
  164. * @errlog: the function to print errors
  165. *
  166. * This iterates through the command line and calls callbacks registered with
  167. * opt_register_table()/opt_register_arg()/opt_register_noarg(). If there
  168. * are unknown options, missing arguments or a callback returns false, then
  169. * an error message is printed and false is returned.
  170. *
  171. * On success, argc and argv are adjusted so only the non-option elements
  172. * remain, and true is returned.
  173. *
  174. * Example:
  175. * if (!opt_parse(&argc, argv, opt_log_stderr)) {
  176. * printf("You screwed up, aborting!\n");
  177. * exit(1);
  178. * }
  179. *
  180. * See Also:
  181. * opt_log_stderr, opt_log_stderr_exit
  182. */
  183. bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
  184. /**
  185. * opt_free_table - free the table.
  186. *
  187. * This frees the internal memory. Call this as the last
  188. * opt function.
  189. */
  190. void opt_free_table(void);
  191. /**
  192. * opt_log_stderr - print message to stderr.
  193. * @fmt: printf-style format.
  194. *
  195. * This is a helper for opt_parse, to print errors to stderr.
  196. *
  197. * See Also:
  198. * opt_log_stderr_exit
  199. */
  200. void opt_log_stderr(const char *fmt, ...);
  201. /**
  202. * opt_log_stderr_exit - print message to stderr, then exit(1)
  203. * @fmt: printf-style format.
  204. *
  205. * Just like opt_log_stderr, only then does exit(1). This means that
  206. * when handed to opt_parse, opt_parse will never return false.
  207. *
  208. * Example:
  209. * // This never returns false; just exits if there's an erorr.
  210. * opt_parse(&argc, argv, opt_log_stderr_exit);
  211. */
  212. void opt_log_stderr_exit(const char *fmt, ...);
  213. /**
  214. * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
  215. * @arg: the argument which was invalid.
  216. *
  217. * This is a helper for callbacks to return a simple error string.
  218. */
  219. char *opt_invalid_argument(const char *arg);
  220. /**
  221. * opt_usage - create usage message
  222. * @argv0: the program name
  223. * @extra: extra details to print after the initial command, or NULL.
  224. *
  225. * Creates a usage message, with the program name, arguments, some extra details
  226. * and a table of all the options with their descriptions. If an option has
  227. * description opt_hidden, it is not shown here.
  228. *
  229. * If "extra" is NULL, then the extra information is taken from any
  230. * registered option which calls opt_usage_and_exit(). This avoids duplicating
  231. * that string in the common case.
  232. *
  233. * The result should be passed to free().
  234. */
  235. char *opt_usage(const char *argv0, const char *extra);
  236. /**
  237. * opt_hidden - string for undocumented options.
  238. *
  239. * This can be used as the desc parameter if you want an option not to be
  240. * shown by opt_usage().
  241. */
  242. extern const char opt_hidden[];
  243. /* Maximum length of arg to show in opt_usage */
  244. #define OPT_SHOW_LEN 80
  245. /* Standard helpers. You can write your own: */
  246. /* Sets the @b to true. */
  247. char *opt_set_bool(bool *b);
  248. /* Sets the @b to false. */
  249. char *opt_set_false(bool *b);
  250. /* Sets @b based on arg: (yes/no/true/false). */
  251. char *opt_set_bool_arg(const char *arg, bool *b);
  252. void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
  253. /* The inverse */
  254. char *opt_set_invbool(bool *b);
  255. void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
  256. /* Sets @b based on !arg: (yes/no/true/false). */
  257. char *opt_set_invbool_arg(const char *arg, bool *b);
  258. /* Set a char *. */
  259. char *opt_set_charp(const char *arg, char **p);
  260. void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
  261. /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
  262. char *opt_set_intval(const char *arg, int *i);
  263. void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
  264. char *opt_set_floatval(const char *arg, float *f);
  265. void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f);
  266. char *opt_set_uintval(const char *arg, unsigned int *ui);
  267. void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
  268. char *opt_set_longval(const char *arg, long *l);
  269. void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
  270. char *opt_set_ulongval(const char *arg, unsigned long *ul);
  271. void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
  272. /* Increment. */
  273. char *opt_inc_intval(int *i);
  274. /* Display version string to stdout, exit(0). */
  275. char *opt_version_and_exit(const char *version);
  276. /* Display usage string to stdout, exit(0). */
  277. char *opt_usage_and_exit(const char *extra);
  278. /* Below here are private declarations. */
  279. /* You can use this directly to build tables, but the macros will ensure
  280. * consistency and type safety. */
  281. enum opt_type {
  282. OPT_NOARG = 1, /* -f|--foo */
  283. OPT_HASARG = 2, /* -f arg|--foo=arg|--foo arg */
  284. OPT_PROCESSARG = 4,
  285. OPT_SUBTABLE = 8, /* Actually, longopt points to a subtable... */
  286. OPT_END = 16, /* End of the table. */
  287. };
  288. struct opt_table {
  289. const char *names; /* pipe-separated names, --longopt or -s */
  290. enum opt_type type;
  291. char *(*cb)(void *arg); /* OPT_NOARG */
  292. char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
  293. void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
  294. union {
  295. const void *carg;
  296. void *arg;
  297. size_t tlen;
  298. } u;
  299. const char *desc;
  300. };
  301. /* Resolves to the four parameters for non-arg callbacks. */
  302. #define OPT_CB_NOARG(cb, arg) \
  303. OPT_NOARG, \
  304. typesafe_cb_cast3(char *(*)(void *), \
  305. char *(*)(typeof(*(arg))*), \
  306. char *(*)(const typeof(*(arg))*), \
  307. char *(*)(const void *), (cb)), \
  308. NULL, NULL
  309. /* Resolves to the four parameters for arg callbacks. */
  310. #define OPT_CB_ARG(cb, show, arg) \
  311. OPT_HASARG, NULL, \
  312. typesafe_cb_cast3(char *(*)(const char *,void *), \
  313. char *(*)(const char *, typeof(*(arg))*), \
  314. char *(*)(const char *, const typeof(*(arg))*), \
  315. char *(*)(const char *, const void *), \
  316. (cb)), \
  317. typesafe_cb_cast(void (*)(char buf[], const void *), \
  318. void (*)(char buf[], const typeof(*(arg))*), (show))
  319. #define OPT_CB_WITHARG(cb, show, arg) \
  320. OPT_PROCESSARG, NULL, \
  321. typesafe_cb_cast3(char *(*)(const char *,void *), \
  322. char *(*)(const char *, typeof(*(arg))*), \
  323. char *(*)(const char *, const typeof(*(arg))*), \
  324. char *(*)(const char *, const void *), \
  325. (cb)), \
  326. typesafe_cb_cast(void (*)(char buf[], const void *), \
  327. void (*)(char buf[], const typeof(*(arg))*), (show))
  328. /* Non-typesafe register function. */
  329. void _opt_register(const char *names, enum opt_type type,
  330. char *(*cb)(void *arg),
  331. char *(*cb_arg)(const char *optarg, void *arg),
  332. void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
  333. const void *arg, const char *desc);
  334. /* We use this to get typechecking for OPT_SUBTABLE */
  335. static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; }
  336. #endif /* CCAN_OPT_H */