2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
29 #include <ipxe/netdevice.h>
30 #include <ipxe/menu.h>
31 #include <ipxe/settings.h>
32 #include <ipxe/params.h>
33 #include <ipxe/timer.h>
34 #include <ipxe/parseopt.h>
38 * Command line option parsing
42 /** Return status code for "--help" option */
43 #define ECANCELED_NO_OP __einfo_error ( EINFO_ECANCELED_NO_OP )
44 #define EINFO_ECANCELED_NO_OP \
45 __einfo_uniqify ( EINFO_ECANCELED, 0x01, "Nothing to do" )
47 /* Disambiguate the various error codes */
48 #define EINVAL_INTEGER __einfo_error ( EINFO_EINVAL_INTEGER )
49 #define EINFO_EINVAL_INTEGER \
50 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid integer value" )
51 #define EINVAL_UNKNOWN_OPTION __einfo_error ( EINFO_EINVAL_UNKNOWN_OPTION )
52 #define EINFO_EINVAL_UNKNOWN_OPTION \
53 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Unrecognised option" )
54 #define EINVAL_MISSING_ARGUMENT __einfo_error ( EINFO_EINVAL_MISSING_ARGUMENT )
55 #define EINFO_EINVAL_MISSING_ARGUMENT \
56 __einfo_uniqify ( EINFO_EINVAL, 0x03, "Missing argument" )
62 * @ret value String value
63 * @ret rc Return status code
65 int parse_string ( char *text, char **value ) {
68 assert ( text != NULL );
80 * @ret value Integer value
81 * @ret rc Return status code
83 int parse_integer ( char *text, unsigned int *value ) {
87 assert ( text != NULL );
90 *value = strtoul ( text, &endp, 0 );
92 printf ( "\"%s\": invalid integer value\n", text );
93 return -EINVAL_INTEGER;
100 * Parse timeout value (in ms)
103 * @ret value Integer value
104 * @ret rc Return status code
106 int parse_timeout ( char *text, unsigned long *value ) {
107 unsigned int value_ms;
110 /* Parse raw integer value */
111 if ( ( rc = parse_integer ( text, &value_ms ) ) != 0 )
114 /* Convert to a number of timer ticks */
115 *value = ( ( value_ms * TICKS_PER_SEC ) / 1000 );
121 * Parse network device name
124 * @ret netdev Network device
125 * @ret rc Return status code
127 int parse_netdev ( char *text, struct net_device **netdev ) {
130 assert ( text != NULL );
132 /* Find network device */
133 *netdev = find_netdev ( text );
135 printf ( "\"%s\": no such network device\n", text );
143 * Parse network device configurator name
146 * @ret configurator Network device configurator
147 * @ret rc Return status code
149 int parse_netdev_configurator ( char *text,
150 struct net_device_configurator **configurator ){
153 assert ( text != NULL );
155 /* Find network device configurator */
156 *configurator = find_netdev_configurator ( text );
157 if ( ! *configurator ) {
158 printf ( "\"%s\": no such configurator\n", text );
170 * @ret rc Return status code
172 int parse_menu ( char *text, struct menu **menu ) {
175 *menu = find_menu ( text );
178 printf ( "\"%s\": no such menu\n", text );
180 printf ( "No default menu\n" );
191 * @v text Text (ignored)
192 * @ret flag Flag to set
193 * @ret rc Return status code
195 int parse_flag ( char *text __unused, int *flag ) {
208 * @ret rc Return status code
210 int parse_key ( char *text, unsigned int *key ) {
212 /* Interpret single characters as being a literal key character */
213 if ( text[0] && ! text[1] ) {
218 /* Otherwise, interpret as an integer */
219 return parse_integer ( text, key );
223 * Parse settings block name
226 * @ret value Integer value
227 * @ret rc Return status code
229 int parse_settings ( char *text, struct settings **value ) {
232 assert ( text != NULL );
234 /* Parse scope name */
235 *value = find_settings ( text );
237 printf ( "\"%s\": no such scope\n", text );
248 * @v setting Named setting to fill in
249 * @v get_child Function to find or create child settings block
250 * @ret rc Return status code
252 * Note that this function modifies the original @c text.
254 int parse_setting ( char *text, struct named_setting *setting,
255 get_child_settings_t get_child ) {
259 assert ( text != NULL );
261 /* Parse setting name */
262 if ( ( rc = parse_setting_name ( text, get_child, &setting->settings,
263 &setting->setting ) ) != 0 ) {
264 printf ( "\"%s\": invalid setting\n", text );
272 * Parse existing setting name
275 * @v setting Named setting to fill in
276 * @ret rc Return status code
278 * Note that this function modifies the original @c text.
280 int parse_existing_setting ( char *text, struct named_setting *setting ) {
282 return parse_setting ( text, setting, find_child_settings );
286 * Parse and autovivify setting name
289 * @v setting Named setting to fill in
290 * @ret rc Return status code
292 * Note that this function modifies the original @c text.
294 int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
296 return parse_setting ( text, setting, autovivify_child_settings );
300 * Parse form parameter list name
303 * @ret params Parameter list
304 * @ret rc Return status code
306 int parse_parameters ( char *text, struct parameters **params ) {
308 /* Find parameter list */
309 *params = find_parameters ( text );
312 printf ( "\"%s\": no such parameter list\n", text );
314 printf ( "No default parameter list\n" );
323 * Print command usage message
325 * @v cmd Command descriptor
326 * @v argv Argument list
328 void print_usage ( struct command_descriptor *cmd, char **argv ) {
329 struct option_descriptor *option;
333 printf ( "Usage:\n\n %s", argv[0] );
334 for ( i = 0 ; i < cmd->num_options ; i++ ) {
335 option = &cmd->options[i];
336 printf ( " [-%c|--%s", option->shortopt, option->longopt );
337 if ( option->has_arg ) {
338 is_optional = ( option->has_arg == optional_argument );
339 printf ( " %s<%s>%s", ( is_optional ? "[" : "" ),
340 option->longopt, ( is_optional ? "]" : "" ) );
345 printf ( " %s", cmd->usage );
346 printf ( "\n\nSee http://ipxe.org/cmd/%s for further information\n",
351 * Reparse command-line options
353 * @v argc Argument count
354 * @v argv Argument list
355 * @v cmd Command descriptor
356 * @v opts Options (already initialised with default values)
357 * @ret rc Return status code
359 int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
361 struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
362 char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
364 unsigned int shortopt_idx = 0;
365 int ( * parse ) ( char *text, void *value );
369 unsigned int num_args;
373 /* Construct long and short option lists for getopt_long() */
374 memset ( longopts, 0, sizeof ( longopts ) );
375 for ( i = 0 ; i < cmd->num_options ; i++ ) {
376 longopts[i].name = cmd->options[i].longopt;
377 longopts[i].has_arg = cmd->options[i].has_arg;
378 longopts[i].val = cmd->options[i].shortopt;
379 shortopts[shortopt_idx++] = cmd->options[i].shortopt;
380 assert ( cmd->options[i].has_arg <= optional_argument );
381 for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
382 shortopts[shortopt_idx++] = ':';
384 longopts[i].name = "help";
385 longopts[i].val = 'h';
386 shortopts[shortopt_idx++] = 'h';
387 shortopts[shortopt_idx++] = '\0';
388 assert ( shortopt_idx <= sizeof ( shortopts ) );
389 DBGC ( cmd, "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
390 argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
393 while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
398 print_usage ( cmd, argv );
399 return -ECANCELED_NO_OP;
401 /* Print usage message */
402 print_usage ( cmd, argv );
403 return -EINVAL_UNKNOWN_OPTION;
405 /* Print usage message */
406 print_usage ( cmd, argv );
407 return -EINVAL_MISSING_ARGUMENT;
409 /* Search for an option to parse */
410 for ( i = 0 ; i < cmd->num_options ; i++ ) {
411 if ( c != cmd->options[i].shortopt )
413 parse = cmd->options[i].parse;
414 value = ( opts + cmd->options[i].offset );
415 if ( ( rc = parse ( optarg, value ) ) != 0 )
419 assert ( i < cmd->num_options );
423 /* Check remaining arguments */
424 num_args = ( argc - optind );
425 if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
426 print_usage ( cmd, argv );
434 * Parse command-line options
436 * @v argc Argument count
437 * @v argv Argument list
438 * @v cmd Command descriptor
439 * @v opts Options (may be uninitialised)
440 * @ret rc Return status code
442 int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
446 memset ( opts, 0, cmd->len );
448 return reparse_options ( argc, argv, cmd, opts );