argtable3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  5. * <sheitmann@users.sourceforge.net>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #ifndef ARGTABLE3
  31. #define ARGTABLE3
  32. #include <stdio.h> /* FILE */
  33. #include <time.h> /* struct tm */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #define ARG_REX_ICASE 1
  38. /* bit masks for arg_hdr.flag */
  39. enum
  40. {
  41. ARG_TERMINATOR=0x1,
  42. ARG_HASVALUE=0x2,
  43. ARG_HASOPTVALUE=0x4
  44. };
  45. typedef void (arg_resetfn)(void *parent);
  46. typedef int (arg_scanfn)(void *parent, const char *argval);
  47. typedef int (arg_checkfn)(void *parent);
  48. typedef void (arg_errorfn)(void *parent, FILE *fp, int error, const char *argval, const char *progname);
  49. /*
  50. * The arg_hdr struct defines properties that are common to all arg_xxx structs.
  51. * The argtable library requires each arg_xxx struct to have an arg_hdr
  52. * struct as its first data member.
  53. * The argtable library functions then use this data to identify the
  54. * properties of the command line option, such as its option tags,
  55. * datatype string, and glossary strings, and so on.
  56. * Moreover, the arg_hdr struct contains pointers to custom functions that
  57. * are provided by each arg_xxx struct which perform the tasks of parsing
  58. * that particular arg_xxx arguments, performing post-parse checks, and
  59. * reporting errors.
  60. * These functions are private to the individual arg_xxx source code
  61. * and are the pointer to them are initiliased by that arg_xxx struct's
  62. * constructor function. The user could alter them after construction
  63. * if desired, but the original intention is for them to be set by the
  64. * constructor and left unaltered.
  65. */
  66. struct arg_hdr
  67. {
  68. char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
  69. const char *shortopts; /* String defining the short options */
  70. const char *longopts; /* String defiing the long options */
  71. const char *datatype; /* Description of the argument data type */
  72. const char *glossary; /* Description of the option as shown by arg_print_glossary function */
  73. int mincount; /* Minimum number of occurences of this option accepted */
  74. int maxcount; /* Maximum number of occurences if this option accepted */
  75. void *parent; /* Pointer to parent arg_xxx struct */
  76. arg_resetfn *resetfn; /* Pointer to parent arg_xxx reset function */
  77. arg_scanfn *scanfn; /* Pointer to parent arg_xxx scan function */
  78. arg_checkfn *checkfn; /* Pointer to parent arg_xxx check function */
  79. arg_errorfn *errorfn; /* Pointer to parent arg_xxx error function */
  80. void *priv; /* Pointer to private header data for use by arg_xxx functions */
  81. };
  82. struct arg_rem
  83. {
  84. struct arg_hdr hdr; /* The mandatory argtable header struct */
  85. };
  86. struct arg_lit
  87. {
  88. struct arg_hdr hdr; /* The mandatory argtable header struct */
  89. int count; /* Number of matching command line args */
  90. };
  91. struct arg_int
  92. {
  93. struct arg_hdr hdr; /* The mandatory argtable header struct */
  94. int count; /* Number of matching command line args */
  95. int *ival; /* Array of parsed argument values */
  96. };
  97. struct arg_dbl
  98. {
  99. struct arg_hdr hdr; /* The mandatory argtable header struct */
  100. int count; /* Number of matching command line args */
  101. double *dval; /* Array of parsed argument values */
  102. };
  103. struct arg_str
  104. {
  105. struct arg_hdr hdr; /* The mandatory argtable header struct */
  106. int count; /* Number of matching command line args */
  107. const char **sval; /* Array of parsed argument values */
  108. };
  109. struct arg_rex
  110. {
  111. struct arg_hdr hdr; /* The mandatory argtable header struct */
  112. int count; /* Number of matching command line args */
  113. const char **sval; /* Array of parsed argument values */
  114. };
  115. struct arg_file
  116. {
  117. struct arg_hdr hdr; /* The mandatory argtable header struct */
  118. int count; /* Number of matching command line args*/
  119. const char **filename; /* Array of parsed filenames (eg: /home/foo.bar) */
  120. const char **basename; /* Array of parsed basenames (eg: foo.bar) */
  121. const char **extension; /* Array of parsed extensions (eg: .bar) */
  122. };
  123. struct arg_date
  124. {
  125. struct arg_hdr hdr; /* The mandatory argtable header struct */
  126. const char *format; /* strptime format string used to parse the date */
  127. int count; /* Number of matching command line args */
  128. struct tm *tmval; /* Array of parsed time values */
  129. };
  130. enum {ARG_ELIMIT=1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG};
  131. struct arg_end
  132. {
  133. struct arg_hdr hdr; /* The mandatory argtable header struct */
  134. int count; /* Number of errors encountered */
  135. int *error; /* Array of error codes */
  136. void **parent; /* Array of pointers to offending arg_xxx struct */
  137. const char **argval; /* Array of pointers to offending argv[] string */
  138. };
  139. /**** arg_xxx constructor functions *********************************/
  140. struct arg_rem* arg_rem(const char* datatype, const char* glossary);
  141. struct arg_lit* arg_lit0(const char* shortopts,
  142. const char* longopts,
  143. const char* glossary);
  144. struct arg_lit* arg_lit1(const char* shortopts,
  145. const char* longopts,
  146. const char *glossary);
  147. struct arg_lit* arg_litn(const char* shortopts,
  148. const char* longopts,
  149. int mincount,
  150. int maxcount,
  151. const char *glossary);
  152. struct arg_key* arg_key0(const char* keyword,
  153. int flags,
  154. const char* glossary);
  155. struct arg_key* arg_key1(const char* keyword,
  156. int flags,
  157. const char* glossary);
  158. struct arg_key* arg_keyn(const char* keyword,
  159. int flags,
  160. int mincount,
  161. int maxcount,
  162. const char* glossary);
  163. struct arg_int* arg_int0(const char* shortopts,
  164. const char* longopts,
  165. const char* datatype,
  166. const char* glossary);
  167. struct arg_int* arg_int1(const char* shortopts,
  168. const char* longopts,
  169. const char* datatype,
  170. const char *glossary);
  171. struct arg_int* arg_intn(const char* shortopts,
  172. const char* longopts,
  173. const char *datatype,
  174. int mincount,
  175. int maxcount,
  176. const char *glossary);
  177. struct arg_dbl* arg_dbl0(const char* shortopts,
  178. const char* longopts,
  179. const char* datatype,
  180. const char* glossary);
  181. struct arg_dbl* arg_dbl1(const char* shortopts,
  182. const char* longopts,
  183. const char* datatype,
  184. const char *glossary);
  185. struct arg_dbl* arg_dbln(const char* shortopts,
  186. const char* longopts,
  187. const char *datatype,
  188. int mincount,
  189. int maxcount,
  190. const char *glossary);
  191. struct arg_str* arg_str0(const char* shortopts,
  192. const char* longopts,
  193. const char* datatype,
  194. const char* glossary);
  195. struct arg_str* arg_str1(const char* shortopts,
  196. const char* longopts,
  197. const char* datatype,
  198. const char *glossary);
  199. struct arg_str* arg_strn(const char* shortopts,
  200. const char* longopts,
  201. const char* datatype,
  202. int mincount,
  203. int maxcount,
  204. const char *glossary);
  205. struct arg_rex* arg_rex0(const char* shortopts,
  206. const char* longopts,
  207. const char* pattern,
  208. const char* datatype,
  209. int flags,
  210. const char* glossary);
  211. struct arg_rex* arg_rex1(const char* shortopts,
  212. const char* longopts,
  213. const char* pattern,
  214. const char* datatype,
  215. int flags,
  216. const char *glossary);
  217. struct arg_rex* arg_rexn(const char* shortopts,
  218. const char* longopts,
  219. const char* pattern,
  220. const char* datatype,
  221. int mincount,
  222. int maxcount,
  223. int flags,
  224. const char *glossary);
  225. struct arg_file* arg_file0(const char* shortopts,
  226. const char* longopts,
  227. const char* datatype,
  228. const char* glossary);
  229. struct arg_file* arg_file1(const char* shortopts,
  230. const char* longopts,
  231. const char* datatype,
  232. const char *glossary);
  233. struct arg_file* arg_filen(const char* shortopts,
  234. const char* longopts,
  235. const char* datatype,
  236. int mincount,
  237. int maxcount,
  238. const char *glossary);
  239. struct arg_date* arg_date0(const char* shortopts,
  240. const char* longopts,
  241. const char* format,
  242. const char* datatype,
  243. const char* glossary);
  244. struct arg_date* arg_date1(const char* shortopts,
  245. const char* longopts,
  246. const char* format,
  247. const char* datatype,
  248. const char *glossary);
  249. struct arg_date* arg_daten(const char* shortopts,
  250. const char* longopts,
  251. const char* format,
  252. const char* datatype,
  253. int mincount,
  254. int maxcount,
  255. const char *glossary);
  256. struct arg_end* arg_end(int maxerrors);
  257. /**** other functions *******************************************/
  258. int arg_nullcheck(void **argtable);
  259. int arg_parse(int argc, char **argv, void **argtable);
  260. void arg_print_option(FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix);
  261. void arg_print_syntax(FILE *fp, void **argtable, const char *suffix);
  262. void arg_print_syntaxv(FILE *fp, void **argtable, const char *suffix);
  263. void arg_print_glossary(FILE *fp, void **argtable, const char *format);
  264. void arg_print_glossary_gnu(FILE *fp, void **argtable);
  265. void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
  266. void arg_freetable(void **argtable, size_t n);
  267. /**** deprecated functions, for back-compatibility only ********/
  268. void arg_free(void **argtable);
  269. #ifdef __cplusplus
  270. }
  271. #endif
  272. #endif