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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_OR_UBDL );
  9. #include <stdint.h>
  10. #include <stddef.h>
  11. #include <ipxe/settings.h>
  12. struct net_device;
  13. struct net_device_configurator;
  14. struct menu;
  15. struct parameters;
  16. /** A command-line option descriptor */
  17. struct option_descriptor {
  18. /** Long option name, if any */
  19. const char *longopt;
  20. /** Short option name */
  21. char shortopt;
  22. /** Argument requirement (as for @c struct @c option) */
  23. uint8_t has_arg;
  24. /** Offset of field within options structure */
  25. uint16_t offset;
  26. /** Parse option
  27. *
  28. * @v text Option text
  29. * @v value Option value to fill in
  30. * @ret rc Return status code
  31. */
  32. int ( * parse ) ( char *text, void *value );
  33. };
  34. /**
  35. * Construct option parser
  36. *
  37. * @v _struct Options structure type
  38. * @v _field Field within options structure
  39. * @v _parse Field type-specific option parser
  40. * @ret _parse Generic option parser
  41. */
  42. #define OPTION_PARSER( _struct, _field, _parse ) \
  43. ( ( int ( * ) ( char *text, void *value ) ) \
  44. ( ( ( ( typeof ( _parse ) * ) NULL ) == \
  45. ( ( int ( * ) ( char *text, \
  46. typeof ( ( ( _struct * ) NULL )->_field ) * ) ) \
  47. NULL ) ) ? _parse : _parse ) )
  48. /**
  49. * Construct option descriptor
  50. *
  51. * @v _longopt Long option name, if any
  52. * @v _shortopt Short option name, if any
  53. * @v _has_arg Argument requirement
  54. * @v _struct Options structure type
  55. * @v _field Field within options structure
  56. * @v _parse Field type-specific option parser
  57. * @ret _option Option descriptor
  58. */
  59. #define OPTION_DESC( _longopt, _shortopt, _has_arg, _struct, _field, _parse ) \
  60. { \
  61. .longopt = _longopt, \
  62. .shortopt = _shortopt, \
  63. .has_arg = _has_arg, \
  64. .offset = offsetof ( _struct, _field ), \
  65. .parse = OPTION_PARSER ( _struct, _field, _parse ), \
  66. }
  67. /** A command descriptor */
  68. struct command_descriptor {
  69. /** Option descriptors */
  70. struct option_descriptor *options;
  71. /** Number of option descriptors */
  72. uint8_t num_options;
  73. /** Length of option structure */
  74. uint8_t len;
  75. /** Minimum number of non-option arguments */
  76. uint8_t min_args;
  77. /** Maximum number of non-option arguments */
  78. uint8_t max_args;
  79. /** Command usage
  80. *
  81. * This excludes the literal "Usage:" and the command name,
  82. * which will be prepended automatically.
  83. */
  84. const char *usage;
  85. };
  86. /** No maximum number of arguments */
  87. #define MAX_ARGUMENTS 0xff
  88. /**
  89. * Construct command descriptor
  90. *
  91. * @v _struct Options structure type
  92. * @v _options Option descriptor array
  93. * @v _check_args Remaining argument checker
  94. * @v _usage Command usage
  95. * @ret _command Command descriptor
  96. */
  97. #define COMMAND_DESC( _struct, _options, _min_args, _max_args, _usage ) \
  98. { \
  99. .options = ( ( ( ( typeof ( _options[0] ) * ) NULL ) == \
  100. ( ( struct option_descriptor * ) NULL ) ) ? \
  101. _options : _options ), \
  102. .num_options = ( sizeof ( _options ) / \
  103. sizeof ( _options[0] ) ), \
  104. .len = sizeof ( _struct ), \
  105. .min_args = _min_args, \
  106. .max_args = _max_args, \
  107. .usage = _usage, \
  108. }
  109. /** A parsed named setting */
  110. struct named_setting {
  111. /** Settings block */
  112. struct settings *settings;
  113. /** Setting */
  114. struct setting setting;
  115. };
  116. extern int parse_string ( char *text, char **value );
  117. extern int parse_integer ( char *text, unsigned int *value );
  118. extern int parse_timeout ( char *text, unsigned long *value );
  119. extern int parse_netdev ( char *text, struct net_device **netdev );
  120. extern int
  121. parse_netdev_configurator ( char *text,
  122. struct net_device_configurator **configurator );
  123. extern int parse_menu ( char *text, struct menu **menu );
  124. extern int parse_flag ( char *text __unused, int *flag );
  125. extern int parse_key ( char *text, unsigned int *key );
  126. extern int parse_settings ( char *text, struct settings **settings );
  127. extern int parse_setting ( char *text, struct named_setting *setting,
  128. get_child_settings_t get_child );
  129. extern int parse_existing_setting ( char *text, struct named_setting *setting );
  130. extern int parse_autovivified_setting ( char *text,
  131. struct named_setting *setting );
  132. extern int parse_parameters ( char *text, struct parameters **params );
  133. extern void print_usage ( struct command_descriptor *cmd, char **argv );
  134. extern int reparse_options ( int argc, char **argv,
  135. struct command_descriptor *cmd, void *opts );
  136. extern int parse_options ( int argc, char **argv,
  137. struct command_descriptor *cmd, void *opts );
  138. #endif /* _IPXE_PARSEOPT_H */