argmatch.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* argmatch.h -- definitions and prototypes for argmatch.c
  2. Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2020 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@ai.mit.edu>
  15. Modified by Akim Demaille <demaille@inf.enst.fr> */
  16. #ifndef ARGMATCH_H_
  17. # define ARGMATCH_H_ 1
  18. # include <limits.h>
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdio.h>
  22. # include <string.h> /* memcmp */
  23. # include "gettext.h"
  24. # include "quote.h"
  25. # include "verify.h"
  26. # ifdef __cplusplus
  27. extern "C" {
  28. # endif
  29. # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
  30. /* Assert there are as many real arguments as there are values
  31. (argument list ends with a NULL guard). */
  32. # define ARGMATCH_VERIFY(Arglist, Vallist) \
  33. verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
  34. /* Return the index of the element of ARGLIST (NULL terminated) that
  35. matches with ARG. If VALLIST is not NULL, then use it to resolve
  36. false ambiguities (i.e., different matches of ARG but corresponding
  37. to the same values in VALLIST). */
  38. ptrdiff_t argmatch (char const *arg, char const *const *arglist,
  39. void const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
  40. # define ARGMATCH(Arg, Arglist, Vallist) \
  41. argmatch (Arg, Arglist, (void const *) (Vallist), sizeof *(Vallist))
  42. /* xargmatch calls this function when it fails. This function should not
  43. return. By default, this is a function that calls ARGMATCH_DIE which
  44. in turn defaults to 'exit (exit_failure)'. */
  45. typedef void (*argmatch_exit_fn) (void);
  46. extern argmatch_exit_fn argmatch_die;
  47. /* Report on stderr why argmatch failed. Report correct values. */
  48. void argmatch_invalid (char const *context, char const *value,
  49. ptrdiff_t problem);
  50. /* Left for compatibility with the old name invalid_arg */
  51. # define invalid_arg(Context, Value, Problem) \
  52. argmatch_invalid (Context, Value, Problem)
  53. /* Report on stderr the list of possible arguments. */
  54. void argmatch_valid (char const *const *arglist,
  55. void const *vallist, size_t valsize);
  56. # define ARGMATCH_VALID(Arglist, Vallist) \
  57. argmatch_valid (Arglist, (void const *) (Vallist), sizeof *(Vallist))
  58. /* Same as argmatch, but upon failure, report an explanation of the
  59. failure, and exit using the function EXIT_FN. */
  60. ptrdiff_t __xargmatch_internal (char const *context,
  61. char const *arg, char const *const *arglist,
  62. void const *vallist, size_t valsize,
  63. argmatch_exit_fn exit_fn);
  64. /* Programmer friendly interface to __xargmatch_internal. */
  65. # define XARGMATCH(Context, Arg, Arglist, Vallist) \
  66. ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
  67. (void const *) (Vallist), \
  68. sizeof *(Vallist), \
  69. argmatch_die)])
  70. /* Convert a value into a corresponding argument. */
  71. char const *argmatch_to_argument (void const *value,
  72. char const *const *arglist,
  73. void const *vallist, size_t valsize)
  74. _GL_ATTRIBUTE_PURE;
  75. # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
  76. argmatch_to_argument (Value, Arglist, \
  77. (void const *) (Vallist), sizeof *(Vallist))
  78. # define ARGMATCH_DEFINE_GROUP(Name, Type) \
  79. /* The type of the values of this group. */ \
  80. typedef Type argmatch_##Name##_type; \
  81. \
  82. /* The size of the type of the values of this group. */ \
  83. enum argmatch_##Name##_size_enum \
  84. { \
  85. argmatch_##Name##_size = sizeof (argmatch_##Name##_type) \
  86. }; \
  87. \
  88. /* Argument mapping of this group. */ \
  89. typedef struct \
  90. { \
  91. /* Argument (e.g., "simple"). */ \
  92. const char *arg; \
  93. /* Value (e.g., simple_backups). */ \
  94. const argmatch_##Name##_type val; \
  95. } argmatch_##Name##_arg; \
  96. \
  97. /* Documentation of this group. */ \
  98. typedef struct \
  99. { \
  100. /* Argument (e.g., "simple"). */ \
  101. const char *arg; \
  102. /* Documentation (e.g., N_("always make simple backups")). */ \
  103. const char *doc; \
  104. } argmatch_##Name##_doc; \
  105. \
  106. /* All the features of an argmatch group. */ \
  107. typedef struct \
  108. { \
  109. const argmatch_##Name##_arg* args; \
  110. const argmatch_##Name##_doc* docs; \
  111. \
  112. /* Printed before the usage message. */ \
  113. const char *doc_pre; \
  114. /* Printed after the usage message. */ \
  115. const char *doc_post; \
  116. } argmatch_##Name##_group_type; \
  117. \
  118. /* The structure the user must build. */ \
  119. extern const argmatch_##Name##_group_type argmatch_##Name##_group; \
  120. \
  121. /* Print the documentation of this group. */ \
  122. void argmatch_##Name##_usage (FILE *out); \
  123. \
  124. /* If nonnegative, the index I of ARG in ARGS, i.e, \
  125. ARGS[I] == ARG. \
  126. Return -1 for invalid argument, -2 for ambiguous argument. */ \
  127. ptrdiff_t argmatch_##Name##_choice (const char *arg); \
  128. \
  129. /* A pointer to the corresponding value if it exists, or \
  130. report an error and exit with failure if the argument was \
  131. not recognized. */ \
  132. const argmatch_##Name##_type* \
  133. argmatch_##Name##_value (const char *context, const char *arg); \
  134. \
  135. /* The first argument in ARGS that matches this value, or NULL. */ \
  136. const char * \
  137. argmatch_##Name##_argument (const argmatch_##Name##_type *val); \
  138. \
  139. ptrdiff_t \
  140. argmatch_##Name##_choice (const char *arg) \
  141. { \
  142. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  143. size_t size = argmatch_##Name##_size; \
  144. ptrdiff_t res = -1; /* Index of first nonexact match. */ \
  145. bool ambiguous = false; /* Whether multiple nonexact match(es). */ \
  146. size_t arglen = strlen (arg); \
  147. \
  148. /* Test all elements for either exact match or abbreviated \
  149. matches. */ \
  150. for (size_t i = 0; g->args[i].arg; i++) \
  151. if (!strncmp (g->args[i].arg, arg, arglen)) \
  152. { \
  153. if (strlen (g->args[i].arg) == arglen) \
  154. /* Exact match found. */ \
  155. return i; \
  156. else if (res == -1) \
  157. /* First nonexact match found. */ \
  158. res = i; \
  159. else if (memcmp (&g->args[res].val, &g->args[i].val, size)) \
  160. /* Second nonexact match found. */ \
  161. /* There is a real ambiguity, or we could not \
  162. disambiguate. */ \
  163. ambiguous = true; \
  164. } \
  165. return ambiguous ? -2 : res; \
  166. } \
  167. \
  168. const char * \
  169. argmatch_##Name##_argument (const argmatch_##Name##_type *val) \
  170. { \
  171. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  172. size_t size = argmatch_##Name##_size; \
  173. for (size_t i = 0; g->args[i].arg; i++) \
  174. if (!memcmp (val, &g->args[i].val, size)) \
  175. return g->args[i].arg; \
  176. return NULL; \
  177. } \
  178. \
  179. /* List the valid values of this group. */ \
  180. static void \
  181. argmatch_##Name##_valid (FILE *out) \
  182. { \
  183. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  184. size_t size = argmatch_##Name##_size; \
  185. \
  186. /* Try to put synonyms on the same line. Synonyms are expected \
  187. to follow each other. */ \
  188. fputs (gettext ("Valid arguments are:"), out); \
  189. for (int i = 0; g->args[i].arg; i++) \
  190. if (i == 0 \
  191. || memcmp (&g->args[i-1].val, &g->args[i].val, size)) \
  192. fprintf (out, "\n - %s", quote (g->args[i].arg)); \
  193. else \
  194. fprintf (out, ", %s", quote (g->args[i].arg)); \
  195. putc ('\n', out); \
  196. } \
  197. \
  198. const argmatch_##Name##_type* \
  199. argmatch_##Name##_value (const char *context, const char *arg) \
  200. { \
  201. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  202. ptrdiff_t res = argmatch_##Name##_choice (arg); \
  203. if (res < 0) \
  204. { \
  205. argmatch_invalid (context, arg, res); \
  206. argmatch_##Name##_valid (stderr); \
  207. argmatch_die (); \
  208. } \
  209. return &g->args[res].val; \
  210. } \
  211. \
  212. /* The column in which the documentation is displayed. \
  213. The leftmost possible, but no more than 20. */ \
  214. static int \
  215. argmatch_##Name##_doc_col (void) \
  216. { \
  217. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  218. size_t size = argmatch_##Name##_size; \
  219. int res = 0; \
  220. for (int i = 0; g->docs[i].arg; ++i) \
  221. { \
  222. int col = 4; \
  223. int ival = argmatch_##Name##_choice (g->docs[i].arg); \
  224. if (ival < 0) \
  225. /* Pseudo argument, display it. */ \
  226. col += strlen (g->docs[i].arg); \
  227. else \
  228. /* Genuine argument, display it with its synonyms. */ \
  229. for (int j = 0; g->args[j].arg; ++j) \
  230. if (! memcmp (&g->args[ival].val, &g->args[j].val, size)) \
  231. col += (col == 4 ? 0 : 2) + strlen (g->args[j].arg); \
  232. if (res <= col) \
  233. res = col <= 20 ? col : 20; \
  234. } \
  235. return res ? res : 20; \
  236. } \
  237. \
  238. void \
  239. argmatch_##Name##_usage (FILE *out) \
  240. { \
  241. const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
  242. size_t size = argmatch_##Name##_size; \
  243. /* Width of the screen. Help2man does not seem to support \
  244. arguments on several lines, so in that case pretend a very \
  245. large width. */ \
  246. const int screen_width = getenv ("HELP2MAN") ? INT_MAX : 80; \
  247. if (g->doc_pre) \
  248. fprintf (out, "%s\n", gettext (g->doc_pre)); \
  249. int doc_col = argmatch_##Name##_doc_col (); \
  250. for (int i = 0; g->docs[i].arg; ++i) \
  251. { \
  252. int col = 0; \
  253. bool first = true; \
  254. int ival = argmatch_##Name##_choice (g->docs[i].arg); \
  255. if (ival < 0) \
  256. /* Pseudo argument, display it. */ \
  257. col += fprintf (out, " %s", g->docs[i].arg); \
  258. else \
  259. /* Genuine argument, display it with its synonyms. */ \
  260. for (int j = 0; g->args[j].arg; ++j) \
  261. if (! memcmp (&g->args[ival].val, &g->args[j].val, size)) \
  262. { \
  263. if (!first \
  264. && screen_width < col + 2 + strlen (g->args[j].arg)) \
  265. { \
  266. fprintf (out, ",\n"); \
  267. col = 0; \
  268. first = true; \
  269. } \
  270. if (first) \
  271. { \
  272. col += fprintf (out, " "); \
  273. first = false; \
  274. } \
  275. else \
  276. col += fprintf (out, ","); \
  277. col += fprintf (out, " %s", g->args[j].arg); \
  278. } \
  279. /* The doc. Separated by at least two spaces. */ \
  280. if (doc_col < col + 2) \
  281. { \
  282. fprintf (out, "\n"); \
  283. col = 0; \
  284. } \
  285. fprintf (out, "%*s%s\n", \
  286. doc_col - col, "", gettext (g->docs[i].doc)); \
  287. } \
  288. if (g->doc_post) \
  289. fprintf (out, "%s\n", gettext (g->doc_post)); \
  290. }
  291. # ifdef __cplusplus
  292. }
  293. # endif
  294. #endif /* ARGMATCH_H_ */