iPXE
parseopt.h
Go to the documentation of this file.
00001 #ifndef _IPXE_PARSEOPT_H
00002 #define _IPXE_PARSEOPT_H
00003 
00004 /** @file
00005  *
00006  * Command line option parsing
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <stddef.h>
00014 #include <ipxe/settings.h>
00015 
00016 struct net_device;
00017 struct net_device_configurator;
00018 struct menu;
00019 struct parameters;
00020 
00021 /** A command-line option descriptor */
00022 struct option_descriptor {
00023         /** Long option name, if any */
00024         const char *longopt;
00025         /** Short option name */
00026         char shortopt;
00027         /** Argument requirement (as for @c struct @c option) */
00028         uint8_t has_arg;
00029         /** Offset of field within options structure */
00030         uint16_t offset;
00031         /** Parse option
00032          *
00033          * @v text              Option text
00034          * @v value             Option value to fill in
00035          * @ret rc              Return status code
00036          */
00037         int ( * parse ) ( char *text, void *value );
00038 };
00039 
00040 /**
00041  * Construct option parser
00042  *
00043  * @v _struct           Options structure type
00044  * @v _field            Field within options structure
00045  * @v _parse            Field type-specific option parser
00046  * @ret _parse          Generic option parser
00047  */
00048 #define OPTION_PARSER( _struct, _field, _parse )                              \
00049         ( ( int ( * ) ( char *text, void *value ) )                           \
00050           ( ( ( ( typeof ( _parse ) * ) NULL ) ==                             \
00051               ( ( int ( * ) ( char *text,                                     \
00052                               typeof ( ( ( _struct * ) NULL )->_field ) * ) ) \
00053                 NULL ) ) ? _parse : _parse ) )
00054 
00055 /**
00056  * Construct option descriptor
00057  *
00058  * @v _longopt          Long option name, if any
00059  * @v _shortopt         Short option name, if any
00060  * @v _has_arg          Argument requirement
00061  * @v _struct           Options structure type
00062  * @v _field            Field within options structure
00063  * @v _parse            Field type-specific option parser
00064  * @ret _option         Option descriptor
00065  */
00066 #define OPTION_DESC( _longopt, _shortopt, _has_arg, _struct, _field, _parse ) \
00067         {                                                                     \
00068                 .longopt = _longopt,                                          \
00069                 .shortopt = _shortopt,                                        \
00070                 .has_arg = _has_arg,                                          \
00071                 .offset = offsetof ( _struct, _field ),                       \
00072                 .parse = OPTION_PARSER ( _struct, _field, _parse ),           \
00073         }
00074 
00075 /** A command descriptor */
00076 struct command_descriptor {
00077         /** Option descriptors */
00078         struct option_descriptor *options;
00079         /** Number of option descriptors */
00080         uint8_t num_options;
00081         /** Length of option structure */
00082         uint8_t len;
00083         /** Minimum number of non-option arguments */
00084         uint8_t min_args;
00085         /** Maximum number of non-option arguments */
00086         uint8_t max_args;
00087         /** Command usage
00088          *
00089          * This excludes the literal "Usage:" and the command name,
00090          * which will be prepended automatically.
00091          */
00092         const char *usage;
00093 };
00094 
00095 /** No maximum number of arguments */
00096 #define MAX_ARGUMENTS 0xff
00097 
00098 /**
00099  * Construct command descriptor
00100  *
00101  * @v _struct           Options structure type
00102  * @v _options          Option descriptor array
00103  * @v _check_args       Remaining argument checker
00104  * @v _usage            Command usage
00105  * @ret _command        Command descriptor
00106  */
00107 #define COMMAND_DESC( _struct, _options, _min_args, _max_args, _usage )       \
00108         {                                                                     \
00109                 .options = ( ( ( ( typeof ( _options[0] ) * ) NULL ) ==       \
00110                                ( ( struct option_descriptor * ) NULL ) ) ?    \
00111                              _options : _options ),                           \
00112                 .num_options = ( sizeof ( _options ) /                        \
00113                                  sizeof ( _options[0] ) ),                    \
00114                 .len = sizeof ( _struct ),                                    \
00115                 .min_args = _min_args,                                        \
00116                 .max_args = _max_args,                                        \
00117                 .usage = _usage,                                              \
00118          }
00119 
00120 /** A parsed named setting */
00121 struct named_setting {
00122         /** Settings block */
00123         struct settings *settings;
00124         /** Setting */
00125         struct setting setting;
00126 };
00127 
00128 extern int parse_string ( char *text, char **value );
00129 extern int parse_integer ( char *text, unsigned int *value );
00130 extern int parse_timeout ( char *text, unsigned long *value );
00131 extern int parse_netdev ( char *text, struct net_device **netdev );
00132 extern int
00133 parse_netdev_configurator ( char *text,
00134                             struct net_device_configurator **configurator );
00135 extern int parse_menu ( char *text, struct menu **menu );
00136 extern int parse_flag ( char *text __unused, int *flag );
00137 extern int parse_key ( char *text, unsigned int *key );
00138 extern int parse_settings ( char *text, struct settings **settings );
00139 extern int parse_setting ( char *text, struct named_setting *setting,
00140                            get_child_settings_t get_child );
00141 extern int parse_existing_setting ( char *text, struct named_setting *setting );
00142 extern int parse_autovivified_setting ( char *text,
00143                                         struct named_setting *setting );
00144 extern int parse_parameters ( char *text, struct parameters **params );
00145 extern void print_usage ( struct command_descriptor *cmd, char **argv );
00146 extern int reparse_options ( int argc, char **argv,
00147                              struct command_descriptor *cmd, void *opts );
00148 extern int parse_options ( int argc, char **argv,
00149                            struct command_descriptor *cmd, void *opts );
00150 
00151 #endif /* _IPXE_PARSEOPT_H */