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