1
0

argtable3.h 14 KB

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