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
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <stdint.h>
14#include <stddef.h>
15#include <ipxe/uuid.h>
16#include <ipxe/settings.h>
17
18struct net_device;
20struct dynamic_ui;
21struct parameters;
22
23/** A command-line option descriptor */
25 /** Long option name, if any */
26 const char *longopt;
27 /** Short option name */
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 */
128};
129
130/** A UUID command-line option */
132 /** UUID */
133 union uuid *value;
134 /** Storage buffer */
135 union uuid buf;
136};
137
138extern int parse_string ( char *text, char **value );
139extern int parse_integer ( char *text, unsigned int *value );
140extern int parse_timeout ( char *text, unsigned long *value );
141extern int parse_uuid ( char *text, struct uuid_option *uuid );
142extern int parse_netdev ( char *text, struct net_device **netdev );
143extern int
144parse_netdev_configurator ( char *text,
145 struct net_device_configurator **configurator );
146extern int parse_dynui ( char *text, struct dynamic_ui **dynui );
147extern int parse_flag ( char *text __unused, int *flag );
148extern int parse_key ( char *text, unsigned int *key );
149extern int parse_settings ( char *text, struct settings **settings );
150extern int parse_setting ( char *text, struct named_setting *setting,
151 get_child_settings_t get_child );
152extern int parse_existing_setting ( char *text, struct named_setting *setting );
153extern int parse_autovivified_setting ( char *text,
154 struct named_setting *setting );
155extern int parse_parameters ( char *text, struct parameters **params );
156extern void print_usage ( struct command_descriptor *cmd, char **argv );
157extern int reparse_options ( int argc, char **argv,
158 struct command_descriptor *cmd, void *opts );
159extern int parse_options ( int argc, char **argv,
160 struct command_descriptor *cmd, void *opts );
161
162#endif /* _IPXE_PARSEOPT_H */
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eqe_cmd cmd
Definition CIB_PRM.h:1
uint32_t flag
Flag number.
Definition aqc1xx.h:2
pseudo_bit_t value[0x00020]
Definition arbel.h:2
unsigned short uint16_t
Definition stdint.h:11
unsigned char uint8_t
Definition stdint.h:10
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
static struct net_device * netdev
Definition gdbudp.c:53
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Configuration settings.
struct settings *(* get_child_settings_t)(struct settings *settings, const char *name)
A child settings block locator function.
Definition settings.h:307
int parse_netdev(char *text, struct net_device **netdev)
Parse network device name.
Definition parseopt.c:159
int parse_key(char *text, unsigned int *key)
Parse key.
Definition parseopt.c:242
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition parseopt.c:227
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition parseopt.c:402
int parse_existing_setting(char *text, struct named_setting *setting)
Parse existing setting name.
Definition parseopt.c:323
int parse_netdev_configurator(char *text, struct net_device_configurator **configurator)
Parse network device configurator name.
Definition parseopt.c:181
int parse_dynui(char *text, struct dynamic_ui **dynui)
Parse dynamic user interface name.
Definition parseopt.c:204
int parse_string(char *text, char **value)
Parse string value.
Definition parseopt.c:74
int parse_settings(char *text, struct settings **settings)
Parse settings block name.
Definition parseopt.c:272
int parse_uuid(char *text, struct uuid_option *uuid)
Parse UUID.
Definition parseopt.c:136
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition parseopt.c:337
int parse_setting(char *text, struct named_setting *setting, get_child_settings_t get_child)
Parse setting name.
Definition parseopt.c:297
void print_usage(struct command_descriptor *cmd, char **argv)
Print command usage message.
Definition parseopt.c:371
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition parseopt.c:92
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition parseopt.c:115
int parse_parameters(char *text, struct parameters **params)
Parse request parameter list name.
Definition parseopt.c:349
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
A command descriptor.
Definition parseopt.h:78
struct option_descriptor * options
Option descriptors.
Definition parseopt.h:80
uint8_t num_options
Number of option descriptors.
Definition parseopt.h:82
uint8_t max_args
Maximum number of non-option arguments.
Definition parseopt.h:88
uint8_t len
Length of option structure.
Definition parseopt.h:84
uint8_t min_args
Minimum number of non-option arguments.
Definition parseopt.h:86
const char * usage
Command usage.
Definition parseopt.h:94
A dynamic user interface.
Definition dynui.h:16
A parsed named setting.
Definition parseopt.h:123
struct setting setting
Setting.
Definition parseopt.h:127
struct settings * settings
Settings block.
Definition parseopt.h:125
A network device configurator.
Definition netdevice.h:314
A network device.
Definition netdevice.h:353
A command-line option descriptor.
Definition parseopt.h:24
uint8_t has_arg
Argument requirement (as for struct option)
Definition parseopt.h:30
int(* parse)(char *text, void *value)
Parse option.
Definition parseopt.h:39
char shortopt
Short option name.
Definition parseopt.h:28
uint16_t offset
Offset of field within options structure.
Definition parseopt.h:32
const char * longopt
Long option name, if any.
Definition parseopt.h:26
A request parameter list.
Definition params.h:17
A setting.
Definition settings.h:24
A settings block.
Definition settings.h:133
A UUID command-line option.
Definition parseopt.h:131
union uuid buf
Storage buffer.
Definition parseopt.h:135
union uuid * value
UUID.
Definition parseopt.h:133
A universally unique ID.
Definition uuid.h:16
Universally unique IDs.