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.4KB

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