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