iPXE
parseopt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdint.h>
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 #include <getopt.h>
00033 #include <ipxe/netdevice.h>
00034 #include <ipxe/menu.h>
00035 #include <ipxe/settings.h>
00036 #include <ipxe/params.h>
00037 #include <ipxe/timer.h>
00038 #include <ipxe/parseopt.h>
00039 #include <config/branding.h>
00040 
00041 /** @file
00042  *
00043  * Command line option parsing
00044  *
00045  */
00046 
00047 /** Return status code for "--help" option */
00048 #define ECANCELED_NO_OP __einfo_error ( EINFO_ECANCELED_NO_OP )
00049 #define EINFO_ECANCELED_NO_OP \
00050         __einfo_uniqify ( EINFO_ECANCELED, 0x01, "Nothing to do" )
00051 
00052 /* Disambiguate the various error codes */
00053 #define EINVAL_INTEGER __einfo_error ( EINFO_EINVAL_INTEGER )
00054 #define EINFO_EINVAL_INTEGER \
00055         __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid integer value" )
00056 #define EINVAL_UNKNOWN_OPTION __einfo_error ( EINFO_EINVAL_UNKNOWN_OPTION )
00057 #define EINFO_EINVAL_UNKNOWN_OPTION \
00058         __einfo_uniqify ( EINFO_EINVAL, 0x02, "Unrecognised option" )
00059 #define EINVAL_MISSING_ARGUMENT __einfo_error ( EINFO_EINVAL_MISSING_ARGUMENT )
00060 #define EINFO_EINVAL_MISSING_ARGUMENT \
00061         __einfo_uniqify ( EINFO_EINVAL, 0x03, "Missing argument" )
00062 
00063 /**
00064 * Parse string value
00065  *
00066  * @v text              Text
00067  * @ret value           String value
00068  * @ret rc              Return status code
00069  */
00070 int parse_string ( char *text, char **value ) {
00071 
00072         /* Sanity check */
00073         assert ( text != NULL );
00074 
00075         /* Parse string */
00076         *value = text;
00077 
00078         return 0;
00079 }
00080 
00081 /**
00082  * Parse integer value
00083  *
00084  * @v text              Text
00085  * @ret value           Integer value
00086  * @ret rc              Return status code
00087  */
00088 int parse_integer ( char *text, unsigned int *value ) {
00089         char *endp;
00090 
00091         /* Sanity check */
00092         assert ( text != NULL );
00093 
00094         /* Parse integer */
00095         *value = strtoul ( text, &endp, 0 );
00096         if ( *endp ) {
00097                 printf ( "\"%s\": invalid integer value\n", text );
00098                 return -EINVAL_INTEGER;
00099         }
00100 
00101         return 0;
00102 }
00103 
00104 /**
00105  * Parse timeout value (in ms)
00106  *
00107  * @v text              Text
00108  * @ret value           Integer value
00109  * @ret rc              Return status code
00110  */
00111 int parse_timeout ( char *text, unsigned long *value ) {
00112         unsigned int value_ms;
00113         int rc;
00114 
00115         /* Parse raw integer value */
00116         if ( ( rc = parse_integer ( text, &value_ms ) ) != 0 )
00117                 return rc;
00118 
00119         /* Convert to a number of timer ticks */
00120         *value = ( value_ms * TICKS_PER_MS );
00121 
00122         return 0;
00123 }
00124 
00125 /**
00126  * Parse network device name
00127  *
00128  * @v text              Text
00129  * @ret netdev          Network device
00130  * @ret rc              Return status code
00131  */
00132 int parse_netdev ( char *text, struct net_device **netdev ) {
00133 
00134         /* Sanity check */
00135         assert ( text != NULL );
00136 
00137         /* Find network device */
00138         *netdev = find_netdev ( text );
00139         if ( ! *netdev ) {
00140                 printf ( "\"%s\": no such network device\n", text );
00141                 return -ENODEV;
00142         }
00143 
00144         return 0;
00145 }
00146 
00147 /**
00148  * Parse network device configurator name
00149  *
00150  * @v text              Text
00151  * @ret configurator    Network device configurator
00152  * @ret rc              Return status code
00153  */
00154 int parse_netdev_configurator ( char *text,
00155                                 struct net_device_configurator **configurator ){
00156 
00157         /* Sanity check */
00158         assert ( text != NULL );
00159 
00160         /* Find network device configurator */
00161         *configurator = find_netdev_configurator ( text );
00162         if ( ! *configurator ) {
00163                 printf ( "\"%s\": no such configurator\n", text );
00164                 return -ENOTSUP;
00165         }
00166 
00167         return 0;
00168 }
00169 
00170 /**
00171  * Parse menu name
00172  *
00173  * @v text              Text
00174  * @ret menu            Menu
00175  * @ret rc              Return status code
00176  */
00177 int parse_menu ( char *text, struct menu **menu ) {
00178 
00179         /* Find menu */
00180         *menu = find_menu ( text );
00181         if ( ! *menu ) {
00182                 if ( text ) {
00183                         printf ( "\"%s\": no such menu\n", text );
00184                 } else {
00185                         printf ( "No default menu\n" );
00186                 }
00187                 return -ENOENT;
00188         }
00189 
00190         return 0;
00191 }
00192 
00193 /**
00194  * Parse flag
00195  *
00196  * @v text              Text (ignored)
00197  * @ret flag            Flag to set
00198  * @ret rc              Return status code
00199  */
00200 int parse_flag ( char *text __unused, int *flag ) {
00201 
00202         /* Set flag */
00203         *flag = 1;
00204 
00205         return 0;
00206 }
00207 
00208 /**
00209  * Parse key
00210  *
00211  * @v text              Text
00212  * @ret key             Key
00213  * @ret rc              Return status code
00214  */
00215 int parse_key ( char *text, unsigned int *key ) {
00216 
00217         /* Interpret single characters as being a literal key character */
00218         if ( text[0] && ! text[1] ) {
00219                 *key = text[0];
00220                 return 0;
00221         }
00222 
00223         /* Otherwise, interpret as an integer */
00224         return parse_integer ( text, key );
00225 }
00226 
00227 /**
00228  * Parse settings block name
00229  *
00230  * @v text              Text
00231  * @ret value           Integer value
00232  * @ret rc              Return status code
00233  */
00234 int parse_settings ( char *text, struct settings **value ) {
00235 
00236         /* Sanity check */
00237         assert ( text != NULL );
00238 
00239         /* Parse scope name */
00240         *value = find_settings ( text );
00241         if ( ! *value ) {
00242                 printf ( "\"%s\": no such scope\n", text );
00243                 return -EINVAL;
00244         }
00245 
00246         return 0;
00247 }
00248 
00249 /**
00250  * Parse setting name
00251  *
00252  * @v text              Text
00253  * @v setting           Named setting to fill in
00254  * @v get_child         Function to find or create child settings block
00255  * @ret rc              Return status code
00256  *
00257  * Note that this function modifies the original @c text.
00258  */
00259 int parse_setting ( char *text, struct named_setting *setting,
00260                     get_child_settings_t get_child ) {
00261         int rc;
00262 
00263         /* Sanity check */
00264         assert ( text != NULL );
00265 
00266         /* Parse setting name */
00267         if ( ( rc = parse_setting_name ( text, get_child, &setting->settings,
00268                                          &setting->setting ) ) != 0 ) {
00269                 printf ( "\"%s\": invalid setting\n", text );
00270                 return rc;
00271         }
00272 
00273         return 0;
00274 }
00275 
00276 /**
00277  * Parse existing setting name
00278  *
00279  * @v text              Text
00280  * @v setting           Named setting to fill in
00281  * @ret rc              Return status code
00282  *
00283  * Note that this function modifies the original @c text.
00284  */
00285 int parse_existing_setting ( char *text, struct named_setting *setting ) {
00286 
00287         return parse_setting ( text, setting, find_child_settings );
00288 }
00289 
00290 /**
00291  * Parse and autovivify setting name
00292  *
00293  * @v text              Text
00294  * @v setting           Named setting to fill in
00295  * @ret rc              Return status code
00296  *
00297  * Note that this function modifies the original @c text.
00298  */
00299 int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
00300 
00301         return parse_setting ( text, setting, autovivify_child_settings );
00302 }
00303 
00304 /**
00305  * Parse form parameter list name
00306  *
00307  * @v text              Text
00308  * @ret params          Parameter list
00309  * @ret rc              Return status code
00310  */
00311 int parse_parameters ( char *text, struct parameters **params ) {
00312 
00313         /* Find parameter list */
00314         *params = find_parameters ( text );
00315         if ( ! *params ) {
00316                 if ( text ) {
00317                         printf ( "\"%s\": no such parameter list\n", text );
00318                 } else {
00319                         printf ( "No default parameter list\n" );
00320                 }
00321                 return -ENOENT;
00322         }
00323 
00324         return 0;
00325 }
00326 
00327 /**
00328  * Print command usage message
00329  *
00330  * @v cmd               Command descriptor
00331  * @v argv              Argument list
00332  */
00333 void print_usage ( struct command_descriptor *cmd, char **argv ) {
00334         struct option_descriptor *option;
00335         unsigned int i;
00336         int is_optional;
00337 
00338         printf ( "Usage:\n\n  %s", argv[0] );
00339         for ( i = 0 ; i < cmd->num_options ; i++ ) {
00340                 option = &cmd->options[i];
00341                 printf ( " [-%c|--%s", option->shortopt, option->longopt );
00342                 if ( option->has_arg ) {
00343                         is_optional = ( option->has_arg == optional_argument );
00344                         printf ( " %s<%s>%s", ( is_optional ? "[" : "" ),
00345                                  option->longopt, ( is_optional ? "]" : "" ) );
00346                 }
00347                 printf ( "]" );
00348         }
00349         if ( cmd->usage )
00350                 printf ( " %s", cmd->usage );
00351         printf ( "\n\nSee " PRODUCT_COMMAND_URI " for further information\n",
00352                  argv[0] );
00353 }
00354 
00355 /**
00356  * Reparse command-line options
00357  *
00358  * @v argc              Argument count
00359  * @v argv              Argument list
00360  * @v cmd               Command descriptor
00361  * @v opts              Options (already initialised with default values)
00362  * @ret rc              Return status code
00363  */
00364 int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
00365                       void *opts ) {
00366         struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
00367         char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
00368                         + 1 /* NUL */ ];
00369         unsigned int shortopt_idx = 0;
00370         int ( * parse ) ( char *text, void *value );
00371         void *value;
00372         unsigned int i;
00373         unsigned int j;
00374         unsigned int num_args;
00375         int c;
00376         int rc;
00377 
00378         /* Construct long and short option lists for getopt_long() */
00379         memset ( longopts, 0, sizeof ( longopts ) );
00380         for ( i = 0 ; i < cmd->num_options ; i++ ) {
00381                 longopts[i].name = cmd->options[i].longopt;
00382                 longopts[i].has_arg = cmd->options[i].has_arg;
00383                 longopts[i].val = cmd->options[i].shortopt;
00384                 shortopts[shortopt_idx++] = cmd->options[i].shortopt;
00385                 assert ( cmd->options[i].has_arg <= optional_argument );
00386                 for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
00387                         shortopts[shortopt_idx++] = ':';
00388         }
00389         longopts[i].name = "help";
00390         longopts[i].val = 'h';
00391         shortopts[shortopt_idx++] = 'h';
00392         shortopts[shortopt_idx++] = '\0';
00393         assert ( shortopt_idx <= sizeof ( shortopts ) );
00394         DBGC ( cmd,  "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
00395                argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
00396 
00397         /* Parse options */
00398         while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
00399                                     NULL ) ) >= 0 ) {
00400                 switch ( c ) {
00401                 case 'h' :
00402                         /* Print help */
00403                         print_usage ( cmd, argv );
00404                         return -ECANCELED_NO_OP;
00405                 case '?' :
00406                         /* Print usage message */
00407                         print_usage ( cmd, argv );
00408                         return -EINVAL_UNKNOWN_OPTION;
00409                 case ':' :
00410                         /* Print usage message */
00411                         print_usage ( cmd, argv );
00412                         return -EINVAL_MISSING_ARGUMENT;
00413                 default:
00414                         /* Search for an option to parse */
00415                         for ( i = 0 ; i < cmd->num_options ; i++ ) {
00416                                 if ( c != cmd->options[i].shortopt )
00417                                         continue;
00418                                 parse = cmd->options[i].parse;
00419                                 value = ( opts + cmd->options[i].offset );
00420                                 if ( ( rc = parse ( optarg, value ) ) != 0 )
00421                                         return rc;
00422                                 break;
00423                         }
00424                         assert ( i < cmd->num_options );
00425                 }
00426         }
00427 
00428         /* Check remaining arguments */
00429         num_args = ( argc - optind );
00430         if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
00431                 print_usage ( cmd, argv );
00432                 return -ERANGE;
00433         }
00434 
00435         return 0;
00436 }
00437 
00438 /**
00439  * Parse command-line options
00440  *
00441  * @v argc              Argument count
00442  * @v argv              Argument list
00443  * @v cmd               Command descriptor
00444  * @v opts              Options (may be uninitialised)
00445  * @ret rc              Return status code
00446  */
00447 int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
00448                     void *opts ) {
00449 
00450         /* Clear options */
00451         memset ( opts, 0, cmd->len );
00452 
00453         return reparse_options ( argc, argv, cmd, opts );
00454 }