opt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* LIBDGL -- a Directed Graph Library implementation
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  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 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* best view tabstop=4
  19. */
  20. /*@*********************************************************************
  21. * @pack: GNO_PACK
  22. * @descr: Command line options utility.
  23. *
  24. * @notes: Support to easily parse command line options. Some concepts
  25. * were taken from the posix getopt() family functions, while
  26. * our specific experience in C programming suggested us a
  27. * proprietary implementation to make source code more readable
  28. * at life easier (I hope).
  29. * Option format:
  30. *
  31. * syntax name
  32. * ----------------------------------------------
  33. * --option=value long-parametric
  34. * --option long-boolean
  35. * -option value short-parametric
  36. * -option short-boolean
  37. *
  38. * C sample:
  39. * ----------------------------------------------
  40. * #include <stdio.h>
  41. * #include <opt.h>
  42. *
  43. * int main( int argc , char ** argv )
  44. * {
  45. * Boolean fHelp;
  46. * Boolean fTcp;
  47. * Boolean fUdp;
  48. * char * pszProtocol;
  49. * char * pszInterface;
  50. * int i;
  51. *
  52. * GNO_BEGIN
  53. * GNO_SWITCH( "h", "help", False , & fHelp , "Print this help." )
  54. * GNO_SWITCH( "t", "tcp", False , & fTcp , NULL )
  55. * GNO_SWITCH( "u", "udp", False , & fUdp , NULL )
  56. * GNO_OPTION( "p", "protocol", "tcp" , & pszProtocol , NULL )
  57. * GNO_OPTION( "i", "interface", "eth0" , & pszInterface , NULL )
  58. * GNO_END
  59. *
  60. *
  61. * if ( GNO_PARSE( argc , argv ) < 0 )
  62. * {
  63. * return 1;
  64. * }
  65. *
  66. * if ( fHelp == True )
  67. * {
  68. * GNO_HELP( "t_opt usage" );
  69. * return 0;
  70. * }
  71. *
  72. * printf ( "t/tcp = %s\n", (fTcp == True) ? "True" : "False" );
  73. * printf ( "u/udp = %s\n", (fUdp == True) ? "True" : "False" );
  74. * printf ( "p/protocol = <%s>\n", pszProtocol );
  75. * printf ( "i/interface = <%s>\n", pszInterface );
  76. *
  77. * GNO_FREE();
  78. *
  79. * printf( "orphan options:\n" );
  80. *
  81. * for ( i = 0 ; i < argc ; i ++ )
  82. * {
  83. * if ( argv[ i ] )
  84. * {
  85. * printf( "arg %d: %s\n", i, argv[i ] );
  86. * }
  87. * else
  88. * {
  89. * printf( "arg %d: --\n", i );
  90. * }
  91. * }
  92. * return 0;
  93. * }
  94. *
  95. **********************************************************************/
  96. #ifndef _GNOPT_H_
  97. #define _GNOPT_H_
  98. #ifdef __cplusplus
  99. extern "C"
  100. {
  101. #endif
  102. /***********************************************************************
  103. * DEFINES
  104. **********************************************************************/
  105. /*@*--------------------------------------------------------------------
  106. * @defs: GNO_FLG
  107. * @descr: flags used to set the nFlg field in the GnoOption_s
  108. * SWITCH = boolean option required
  109. *
  110. * @see: GnoOption_s
  111. *
  112. *--------------------------------------------------------------------*/
  113. #define GNO_FLG_SWITCH 0x01
  114. /*@*--------------------------------------------------------------------
  115. * @defs: True/False
  116. * @descr: one more useful (?) true/false definition
  117. *
  118. *--------------------------------------------------------------------*/
  119. #define True 1
  120. #define False 0
  121. /***********************************************************************
  122. * STRUCTS/UNIONS/TYPES/FUNCDEFS
  123. **********************************************************************/
  124. /*@*--------------------------------------------------------------------
  125. * @type: Boolean
  126. * @descr: wasn't it better to use 'int'?
  127. *--------------------------------------------------------------------*/
  128. typedef int Boolean;
  129. /*@*--------------------------------------------------------------------
  130. * @type: GnoOption_s
  131. * @descr: This structure describes an option. We intend to use an array
  132. * of GnoOption_s, terminated by a 'NULL' entry (one containing
  133. * all binary-zero fields), and to pass it to GnoParse() together
  134. * with the typical argc,argv couple of main() args. The array's
  135. * entries are looked-up and filled with coherent argc,argv
  136. * values. Some fields are statically filled by user while other
  137. * are returned by the GnoParse() function.
  138. * Fields set by the user:
  139. *
  140. * nFlg = So far the only supported flag is GNO_FLG_SWITCH
  141. * to address a boolean option type.
  142. * fDef = The default value for a boolean option.
  143. * pszDef = The default value for a parametric option.
  144. * pszShort = The short name of the option.
  145. * pszLong = The long name of the option.
  146. * pfValue = Pointer to a boolean option return value.
  147. * ppszValue = Pointer to a parametric option return value.
  148. * pszDescr = A brief option description
  149. *
  150. * Fields set by GnoParse():
  151. *
  152. * iArg = argv option index
  153. * *ppszValue = pointer to parametric option value
  154. * *pfValue = True/False
  155. *
  156. * User supplied fields are mandatory only within specific
  157. * conditions:
  158. *
  159. * - at least one of pszShort/pszLong must be specified
  160. * - fDef and pfValue apply to a boolean option only
  161. * - pszDef and ppszValue apply to a parametric option only
  162. * - pszDescr is optional
  163. *
  164. * @see: GnoParse(), GNO_FLG
  165. *
  166. *--------------------------------------------------------------------*/
  167. typedef struct GnoOption
  168. {
  169. int iArg; /* returned argv option index */
  170. int nFlg; /* flags describing the option */
  171. Boolean fDef; /* default returned value for a boolean option */
  172. char *pszDef; /* default returned value for a parametric option */
  173. char *pszShort; /* short-option recogniser */
  174. char *pszLong; /* long-option recogniser */
  175. Boolean *pfValue; /* address to return a boolean option */
  176. char **ppszValue; /* address to return a parametric option */
  177. char *pszDescr; /* a brief option description */
  178. } GnoOption_s;
  179. /***********************************************************************
  180. * MACROS
  181. **********************************************************************/
  182. /*@*--------------------------------------------------------------------
  183. *
  184. * @macro: GNO_BEGIN
  185. * @descr: Begin an option array declaration
  186. *
  187. * @notes: The best use is to start option declaration immediately after
  188. * the automatic-variable declaration in the main() function.
  189. *
  190. *--------------------------------------------------------------------*/
  191. #define GNO_BEGIN GnoOption_s _aopt[] = {
  192. /*@*--------------------------------------------------------------------
  193. *
  194. * @macro: GNO_OPTION
  195. * @descr: Declare a parametric option
  196. *
  197. *
  198. * @args: I: chsopt = short option character
  199. * I: pszlopt -> long option psz
  200. * I: pszdef -> default-returned psz
  201. * I: ppszv -> user-addressed return variable pointer
  202. *
  203. *--------------------------------------------------------------------*/
  204. #define GNO_OPTION(pszsopt,pszlopt,pszdef,ppszv,pszdescr) \
  205. { 0, 0, 0, pszdef, pszsopt, pszlopt, NULL, ppszv, pszdescr },
  206. /*@*--------------------------------------------------------------------
  207. *
  208. * @macro: GNO_SWITCH
  209. * @descr: Declare a boolean option
  210. *
  211. * @args: I: chsopt = short option character
  212. * I: pszlopt -> long option psz
  213. * I: fdef = default-returned Boolean
  214. * I: pfv -> user-addressed return variable pointer
  215. *
  216. *--------------------------------------------------------------------*/
  217. #define GNO_SWITCH(pszsopt,pszlopt,fdef,pfv,pszdescr) \
  218. { \
  219. 0, \
  220. GNO_FLG_SWITCH, \
  221. fdef, NULL, \
  222. pszsopt, pszlopt, \
  223. pfv, NULL, \
  224. pszdescr \
  225. },
  226. /*@*--------------------------------------------------------------------
  227. *
  228. * @macro: GNO_PARSE
  229. * @descr: Parse a GnoOption_s array declaration
  230. *
  231. * @args: I: argc = count of argv entries
  232. * I: argv -> array of sz pointers
  233. *
  234. *--------------------------------------------------------------------*/
  235. #define GNO_PARSE(argc,argv) GnoParse( (argc), (argv), _aopt )
  236. /*@*--------------------------------------------------------------------
  237. *
  238. * @macro: GNO_END
  239. * @descr: Terminate an option array declaration
  240. *
  241. *--------------------------------------------------------------------*/
  242. #define GNO_END { 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL } };
  243. /*@*--------------------------------------------------------------------
  244. *
  245. * @macro: GNO_HELP
  246. *
  247. * @descr: Print a brief option's help on the standard error
  248. *
  249. * @args: I: pszhead -> help header string
  250. *
  251. *--------------------------------------------------------------------*/
  252. #define GNO_HELP(pszhead) GnoHelp( pszhead , _aopt )
  253. /*@*--------------------------------------------------------------------
  254. *
  255. * @macro: GNO_FREE
  256. *
  257. * @descr: Free resources created by a previously parsed array
  258. *
  259. *
  260. *--------------------------------------------------------------------*/
  261. #define GNO_FREE() GnoFree( _aopt )
  262. /***********************************************************************
  263. * FUNCTION PROTOTYPES
  264. **********************************************************************/
  265. extern int GnoParse(int argc, char **argv, GnoOption_s * pOpt);
  266. extern void GnoFree(GnoOption_s * pOpt);
  267. extern void GnoHelp(char *pszHead, GnoOption_s * pOpt);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* top of file */
  272. /***************************** END OF FILE ****************************/