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/settings.h>
15 
16 struct net_device;
18 struct menu;
19 struct parameters;
20 
21 /** A command-line 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) */
29  /** Offset of field within options structure */
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 */
77  /** Option descriptors */
79  /** Number of option descriptors */
81  /** Length of option structure */
83  /** Minimum number of non-option arguments */
85  /** Maximum number of non-option arguments */
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 */
122  /** Settings block */
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 */
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:215
unsigned short uint16_t
Definition: stdint.h:11
A parsed named setting.
Definition: parseopt.h:121
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:299
uint8_t min_args
Minimum number of non-option arguments.
Definition: parseopt.h:84
A form parameter list.
Definition: params.h:16
uint8_t has_arg
Argument requirement (as for struct option)
Definition: parseopt.h:28
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:111
const char * longopt
Long option name, if any.
Definition: parseopt.h:24
A command descriptor.
Definition: parseopt.h:76
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:200
int parse_setting(char *text, struct named_setting *setting, get_child_settings_t get_child)
Parse setting name.
Definition: parseopt.c:259
int(* parse)(char *text, void *value)
Parse option.
Definition: parseopt.h:37
uint16_t offset
Offset of field within options structure.
Definition: parseopt.h:30
char shortopt
Short option name.
Definition: parseopt.h:26
int parse_parameters(char *text, struct parameters **params)
Parse form parameter list name.
Definition: parseopt.c:311
const char * usage
Command usage.
Definition: parseopt.h:92
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:80
void print_usage(struct command_descriptor *cmd, char **argv)
Print command usage message.
Definition: parseopt.c:333
static struct net_device * netdev
Definition: gdbudp.c:52
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:70
struct settings * settings
Settings block.
Definition: parseopt.h:123
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static union @423 opts
"cert<xxx>" option list
Configuration settings.
int parse_settings(char *text, struct settings **settings)
Parse settings block name.
Definition: parseopt.c:234
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:364
A network device.
Definition: netdevice.h:348
A settings block.
Definition: settings.h:132
unsigned char uint8_t
Definition: stdint.h:10
A setting.
Definition: settings.h:23
int parse_netdev_configurator(char *text, struct net_device_configurator **configurator)
Parse network device configurator name.
Definition: parseopt.c:154
uint8_t len
Length of option structure.
Definition: parseopt.h:82
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
struct option_descriptor * options
Option descriptors.
Definition: parseopt.h:78
int parse_existing_setting(char *text, struct named_setting *setting)
Parse existing setting name.
Definition: parseopt.c:285
int parse_netdev(char *text, struct net_device **netdev)
Parse network device name.
Definition: parseopt.c:132
A network device configurator.
Definition: netdevice.h:309
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:88
A command-line option descriptor.
Definition: parseopt.h:22
int parse_menu(char *text, struct menu **menu)
Parse menu name.
Definition: parseopt.c:177
A menu.
Definition: menu.h:15
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
union @375 key
Sense key.
Definition: scsi.h:18
uint16_t flag
Flag number.
Definition: hyperv.h:14
uint8_t max_args
Maximum number of non-option arguments.
Definition: parseopt.h:86
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)