procconf.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. * This header gives the interface types and prototypes for the procconf
  18. * module, which processes command line options.
  19. */
  20. #ifndef PROCCONF_H
  21. #define PROCCONF_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <limits.h> /* for UINT_MAX */
  26. /*
  27. * These are used to specify the option type expected
  28. */
  29. typedef enum {
  30. CF_CHAR, /* A single character */
  31. CF_STRING, /* A string */
  32. CF_NE_STRING, /* A non-empty string */
  33. CF_INT, /* An integer */
  34. CF_NON_NEG_INT, /* A non-negative integer */
  35. CF_POS_INT, /* A positive integer */
  36. CF_FLOAT, /* A floating-point value */
  37. CF_NON_NEG_FLOAT, /* A non-negative floating point value */
  38. CF_POS_FLOAT, /* A positive floating point value */
  39. CF_SWITCH, /* An option that does not take a value */
  40. CF_ENDLIST /* End of option list */
  41. } cf_type;
  42. /*
  43. * This is to be OR'd into a confvar_t outind to indicate that an index
  44. * number that is given is not to be interpreted as a command line option
  45. * character.
  46. */
  47. #define CF_NOTFLAG (UINT_MAX & ~(UINT_MAX >> 1))
  48. /*
  49. * Structure for passing varname/value pairs.
  50. * This gives the variable names to search for, and allows variable names to
  51. * mapped to characters so that the characters may be used as indexes into the
  52. * results array.
  53. */
  54. typedef struct {
  55. unsigned outind; /* Single-character option, or option output index */
  56. char* varname; /* Long name, for config file and long option */
  57. cf_type type; /* Option type */
  58. const void* addr; /* Address of variable associated with this option */
  59. int value; /* Value to assign to switch options */
  60. } confvar_t;
  61. /*
  62. * Structure for returning assigned values.
  63. */
  64. typedef struct confval_struct {
  65. const char* strval; /* String form of value */
  66. unsigned index; /* Relative position of this instance */
  67. union {
  68. int intval;
  69. unsigned int nnint;
  70. double floatval;
  71. const char* string;
  72. int switchval;
  73. char charval;
  74. } value;
  75. struct confval_struct* next;
  76. } confval;
  77. /* Information about the values assigned to a particular option */
  78. typedef struct {
  79. int num; /* number of instances of this option */
  80. confvar_t* confvar; /* which option descriptor this corresponds to */
  81. confval** values; /* Start of pointers to values for this option */
  82. } cf_option;
  83. typedef struct {
  84. cf_option* optVals; /* All option values */
  85. cf_option** map; /* Option values indexed by option-char/option-index */
  86. } confdata_t;
  87. /*
  88. * Input variables:
  89. * argc, argv: Command line data.
  90. * optConf[]: Option description structures. The array should end with an
  91. * element with a type of CF_ENDLIST.
  92. * name: Name of program, for messages.
  93. * usage: Usage message. If non-null, on error a message is printed to stderr
  94. * and the program exits.
  95. *
  96. * Output variables:
  97. * The processed option values are stored in confdata.
  98. * A pointer to the start of the values for each option is stored in
  99. * confdata->optVals[].values at the same offset as the option appears in
  100. * confdata[].
  101. * For any option for option characters/indexes have been used,
  102. * confdata->map[index] is set to the same data.
  103. * After processing, argc will have been adjusted to be the number of
  104. * non-option arguments and argv will have been adjusted to start with the
  105. * first non-option argument.
  106. *
  107. * Return value:
  108. * On success, NULL.
  109. * On error, a message describing the problem.
  110. */
  111. const char*
  112. procOpts(int* argc, const char** argv[], const confvar_t optConf[],
  113. confdata_t* confdata, const char name[],
  114. const char usage[]);
  115. /*
  116. * Free the malloced data stored in confdata elements by ProcOpts()
  117. */
  118. void confdataFree(confdata_t *confdata);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif