Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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