iPXE
parseopt.h
Go to the documentation of this file.
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/uuid.h>
15 #include <ipxe/settings.h>
16 
17 struct net_device;
19 struct dynamic_ui;
20 struct parameters;
21 
22 /** A command-line option descriptor */
24  /** Long option name, if any */
25  const char *longopt;
26  /** Short option name */
27  char shortopt;
28  /** Argument requirement (as for @c struct @c option) */
30  /** Offset of field within options structure */
32  /** Parse option
33  *
34  * @v text Option text
35  * @v value Option value to fill in
36  * @ret rc Return status code
37  */
38  int ( * parse ) ( char *text, void *value );
39 };
40 
41 /**
42  * Construct option parser
43  *
44  * @v _struct Options structure type
45  * @v _field Field within options structure
46  * @v _parse Field type-specific option parser
47  * @ret _parse Generic option parser
48  */
49 #define OPTION_PARSER( _struct, _field, _parse ) \
50  ( ( int ( * ) ( char *text, void *value ) ) \
51  ( ( ( ( typeof ( _parse ) * ) NULL ) == \
52  ( ( int ( * ) ( char *text, \
53  typeof ( ( ( _struct * ) NULL )->_field ) * ) ) \
54  NULL ) ) ? _parse : _parse ) )
55 
56 /**
57  * Construct option descriptor
58  *
59  * @v _longopt Long option name, if any
60  * @v _shortopt Short option name, if any
61  * @v _has_arg Argument requirement
62  * @v _struct Options structure type
63  * @v _field Field within options structure
64  * @v _parse Field type-specific option parser
65  * @ret _option Option descriptor
66  */
67 #define OPTION_DESC( _longopt, _shortopt, _has_arg, _struct, _field, _parse ) \
68  { \
69  .longopt = _longopt, \
70  .shortopt = _shortopt, \
71  .has_arg = _has_arg, \
72  .offset = offsetof ( _struct, _field ), \
73  .parse = OPTION_PARSER ( _struct, _field, _parse ), \
74  }
75 
76 /** A command descriptor */
78  /** Option descriptors */
80  /** Number of option descriptors */
82  /** Length of option structure */
84  /** Minimum number of non-option arguments */
86  /** Maximum number of non-option arguments */
88  /** Command usage
89  *
90  * This excludes the literal "Usage:" and the command name,
91  * which will be prepended automatically.
92  */
93  const char *usage;
94 };
95 
96 /** No maximum number of arguments */
97 #define MAX_ARGUMENTS 0xff
98 
99 /**
100  * Construct command descriptor
101  *
102  * @v _struct Options structure type
103  * @v _options Option descriptor array
104  * @v _check_args Remaining argument checker
105  * @v _usage Command usage
106  * @ret _command Command descriptor
107  */
108 #define COMMAND_DESC( _struct, _options, _min_args, _max_args, _usage ) \
109  { \
110  .options = ( ( ( ( typeof ( _options[0] ) * ) NULL ) == \
111  ( ( struct option_descriptor * ) NULL ) ) ? \
112  _options : _options ), \
113  .num_options = ( sizeof ( _options ) / \
114  sizeof ( _options[0] ) ), \
115  .len = sizeof ( _struct ), \
116  .min_args = _min_args, \
117  .max_args = _max_args, \
118  .usage = _usage, \
119  }
120 
121 /** A parsed named setting */
123  /** Settings block */
125  /** Setting */
126  struct setting setting;
127 };
128 
129 /** A UUID command-line option */
130 struct uuid_option {
131  /** UUID */
132  union uuid *value;
133  /** Storage buffer */
134  union uuid buf;
135 };
136 
137 extern int parse_string ( char *text, char **value );
138 extern int parse_integer ( char *text, unsigned int *value );
139 extern int parse_timeout ( char *text, unsigned long *value );
140 extern int parse_uuid ( char *text, struct uuid_option *uuid );
141 extern int parse_netdev ( char *text, struct net_device **netdev );
142 extern int
143 parse_netdev_configurator ( char *text,
144  struct net_device_configurator **configurator );
145 extern int parse_dynui ( char *text, struct dynamic_ui **dynui );
146 extern int parse_flag ( char *text __unused, int *flag );
147 extern int parse_key ( char *text, unsigned int *key );
148 extern int parse_settings ( char *text, struct settings **settings );
149 extern int parse_setting ( char *text, struct named_setting *setting,
150  get_child_settings_t get_child );
151 extern int parse_existing_setting ( char *text, struct named_setting *setting );
152 extern int parse_autovivified_setting ( char *text,
153  struct named_setting *setting );
154 extern int parse_parameters ( char *text, struct parameters **params );
155 extern void print_usage ( struct command_descriptor *cmd, char **argv );
156 extern int reparse_options ( int argc, char **argv,
157  struct command_descriptor *cmd, void *opts );
158 extern int parse_options ( int argc, char **argv,
159  struct command_descriptor *cmd, void *opts );
160 
161 #endif /* _IPXE_PARSEOPT_H */
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:241
unsigned short uint16_t
Definition: stdint.h:11
A parsed named setting.
Definition: parseopt.h:122
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
A UUID command-line option.
Definition: parseopt.h:130
A universally unique ID.
Definition: uuid.h:15
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:336
uint8_t min_args
Minimum number of non-option arguments.
Definition: parseopt.h:85
A request parameter list.
Definition: params.h:16
uint8_t has_arg
Argument requirement (as for struct option)
Definition: parseopt.h:29
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
Universally unique IDs.
const char * longopt
Long option name, if any.
Definition: parseopt.h:25
A command descriptor.
Definition: parseopt.h:77
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
int parse_setting(char *text, struct named_setting *setting, get_child_settings_t get_child)
Parse setting name.
Definition: parseopt.c:296
int(* parse)(char *text, void *value)
Parse option.
Definition: parseopt.h:38
uint16_t offset
Offset of field within options structure.
Definition: parseopt.h:31
char shortopt
Short option name.
Definition: parseopt.h:27
int parse_parameters(char *text, struct parameters **params)
Parse request parameter list name.
Definition: parseopt.c:348
const char * usage
Command usage.
Definition: parseopt.h:93
struct settings *(* get_child_settings_t)(struct settings *settings, const char *name)
A child settings block locator function.
Definition: settings.h:306
uint8_t num_options
Number of option descriptors.
Definition: parseopt.h:81
void print_usage(struct command_descriptor *cmd, char **argv)
Print command usage message.
Definition: parseopt.c:370
static struct net_device * netdev
Definition: gdbudp.c:52
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
struct settings * settings
Settings block.
Definition: parseopt.h:124
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
Configuration settings.
int parse_settings(char *text, struct settings **settings)
Parse settings block name.
Definition: parseopt.c:271
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:401
A network device.
Definition: netdevice.h:352
A settings block.
Definition: settings.h:132
unsigned char uint8_t
Definition: stdint.h:10
static union @437 opts
"cert<xxx>" option list
A setting.
Definition: settings.h:23
union uuid * value
UUID.
Definition: parseopt.h:132
int parse_netdev_configurator(char *text, struct net_device_configurator **configurator)
Parse network device configurator name.
Definition: parseopt.c:180
uint8_t len
Length of option structure.
Definition: parseopt.h:83
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
A dynamic user interface.
Definition: dynui.h:15
struct option_descriptor * options
Option descriptors.
Definition: parseopt.h:79
int parse_existing_setting(char *text, struct named_setting *setting)
Parse existing setting name.
Definition: parseopt.c:322
int parse_netdev(char *text, struct net_device **netdev)
Parse network device name.
Definition: parseopt.c:158
A network device configurator.
Definition: netdevice.h:313
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:91
A command-line option descriptor.
Definition: parseopt.h:23
union uuid buf
Storage buffer.
Definition: parseopt.h:134
int parse_uuid(char *text, struct uuid_option *uuid)
Parse UUID.
Definition: parseopt.c:135
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
union @382 key
Sense key.
Definition: crypto.h:284
uint16_t flag
Flag number.
Definition: hyperv.h:14
int parse_dynui(char *text, struct dynamic_ui **dynui)
Parse dynamic user interface name.
Definition: parseopt.c:203
uint8_t max_args
Maximum number of non-option arguments.
Definition: parseopt.h:87
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)