1 #ifndef _IPXE_PARSEOPT_H
2 #define _IPXE_PARSEOPT_H
6 * Command line option parsing
10 FILE_LICENCE ( GPL2_OR_LATER );
14 #include <ipxe/settings.h>
17 struct net_device_configurator;
21 /** A command-line option descriptor */
22 struct option_descriptor {
23 /** Long option name, if any */
25 /** Short option name */
27 /** Argument requirement (as for @c struct @c option) */
29 /** Offset of field within options structure */
34 * @v value Option value to fill in
35 * @ret rc Return status code
37 int ( * parse ) ( char *text, void *value );
41 * Construct option parser
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
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 ) )
56 * Construct option descriptor
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
66 #define OPTION_DESC( _longopt, _shortopt, _has_arg, _struct, _field, _parse ) \
68 .longopt = _longopt, \
69 .shortopt = _shortopt, \
70 .has_arg = _has_arg, \
71 .offset = offsetof ( _struct, _field ), \
72 .parse = OPTION_PARSER ( _struct, _field, _parse ), \
75 /** A command descriptor */
76 struct command_descriptor {
77 /** Option descriptors */
78 struct option_descriptor *options;
79 /** Number of option descriptors */
81 /** Length of option structure */
83 /** Minimum number of non-option arguments */
85 /** Maximum number of non-option arguments */
89 * This excludes the literal "Usage:" and the command name,
90 * which will be prepended automatically.
95 /** No maximum number of arguments */
96 #define MAX_ARGUMENTS 0xff
99 * Construct command descriptor
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
107 #define COMMAND_DESC( _struct, _options, _min_args, _max_args, _usage ) \
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, \
120 /** A parsed named setting */
121 struct named_setting {
122 /** Settings block */
123 struct settings *settings;
125 struct setting setting;
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 );
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 );
151 #endif /* _IPXE_PARSEOPT_H */