You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parseopt.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef _IPXE_PARSEOPT_H
  2. #define _IPXE_PARSEOPT_H
  3. /** @file
  4. *
  5. * Command line option parsing
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. #include <stddef.h>
  11. struct net_device;
  12. /** A command-line option descriptor */
  13. struct option_descriptor {
  14. /** Long option name, if any */
  15. const char *longopt;
  16. /** Short option name */
  17. char shortopt;
  18. /** Argument requirement (as for @c struct @c option) */
  19. uint8_t has_arg;
  20. /** Offset of field within options structure */
  21. uint16_t offset;
  22. /** Parse option
  23. *
  24. * @v text Option text
  25. * @v value Option value to fill in
  26. * @ret rc Return status code
  27. */
  28. int ( * parse ) ( const char *text, void *value );
  29. };
  30. /**
  31. * Construct option parser
  32. *
  33. * @v _struct Options structure type
  34. * @v _field Field within options structure
  35. * @v _parse Field type-specific option parser
  36. * @ret _parse Generic option parser
  37. */
  38. #define OPTION_PARSER( _struct, _field, _parse ) \
  39. ( ( int ( * ) ( const char *text, void *value ) ) \
  40. ( ( ( ( typeof ( _parse ) * ) NULL ) == \
  41. ( ( int ( * ) ( const char *text, \
  42. typeof ( ( ( _struct * ) NULL )->_field ) * ) ) \
  43. NULL ) ) ? _parse : _parse ) )
  44. /**
  45. * Construct option descriptor
  46. *
  47. * @v _longopt Long option name, if any
  48. * @v _shortopt Short option name, if any
  49. * @v _has_arg Argument requirement
  50. * @v _struct Options structure type
  51. * @v _field Field within options structure
  52. * @v _parse Field type-specific option parser
  53. * @ret _option Option descriptor
  54. */
  55. #define OPTION_DESC( _longopt, _shortopt, _has_arg, _struct, _field, _parse ) \
  56. { \
  57. .longopt = _longopt, \
  58. .shortopt = _shortopt, \
  59. .has_arg = _has_arg, \
  60. .offset = offsetof ( _struct, _field ), \
  61. .parse = OPTION_PARSER ( _struct, _field, _parse ), \
  62. }
  63. /** A command descriptor */
  64. struct command_descriptor {
  65. /** Option descriptors */
  66. struct option_descriptor *options;
  67. /** Number of option descriptors */
  68. uint8_t num_options;
  69. /** Length of option structure */
  70. uint8_t len;
  71. /** Minimum number of non-option arguments */
  72. uint8_t min_args;
  73. /** Maximum number of non-option arguments */
  74. uint8_t max_args;
  75. /** Command usage
  76. *
  77. * This excludes the literal "Usage:" and the command name,
  78. * which will be prepended automatically.
  79. */
  80. const char *usage;
  81. };
  82. /** No maximum number of arguments */
  83. #define MAX_ARGUMENTS 0xff
  84. /**
  85. * Construct command descriptor
  86. *
  87. * @v _struct Options structure type
  88. * @v _options Option descriptor array
  89. * @v _check_args Remaining argument checker
  90. * @v _usage Command usage
  91. * @ret _command Command descriptor
  92. */
  93. #define COMMAND_DESC( _struct, _options, _min_args, _max_args, _usage ) \
  94. { \
  95. .options = ( ( ( ( typeof ( _options[0] ) * ) NULL ) == \
  96. ( ( struct option_descriptor * ) NULL ) ) ? \
  97. _options : _options ), \
  98. .num_options = ( sizeof ( _options ) / \
  99. sizeof ( _options[0] ) ), \
  100. .len = sizeof ( _struct ), \
  101. .min_args = _min_args, \
  102. .max_args = _max_args, \
  103. .usage = _usage, \
  104. }
  105. extern int parse_string ( const char *text, const char **value );
  106. extern int parse_integer ( const char *text, unsigned int *value );
  107. extern int parse_netdev ( const char *text, struct net_device **netdev );
  108. extern int parse_flag ( const char *text __unused, int *flag );
  109. extern void print_usage ( struct command_descriptor *cmd, char **argv );
  110. extern int reparse_options ( int argc, char **argv,
  111. struct command_descriptor *cmd, void *opts );
  112. extern int parse_options ( int argc, char **argv,
  113. struct command_descriptor *cmd, void *opts );
  114. #endif /* _IPXE_PARSEOPT_H */