parseopt.c revision a734c64bff58bda2fa48c2795453e092167b0ff7
/*
* Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
*
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/netdevice.h>
#include <ipxe/parseopt.h>
/** @file
*
* Command line option parsing
*
*/
/** Return status code for "--help" option */
#define EINFO_ECANCELED_NO_OP \
/* Disambiguate the various error codes */
#define EINFO_EINVAL_INTEGER \
#define EINFO_EINVAL_UNKNOWN_OPTION \
#define EINFO_EINVAL_MISSING_ARGUMENT \
/**
* Parse string value
*
* @v text Text
* @ret value String value
* @ret rc Return status code
*/
/* Sanity check */
/* Parse string */
return 0;
}
/**
* Parse integer value
*
* @v text Text
* @ret value Integer value
* @ret rc Return status code
*/
char *endp;
/* Sanity check */
/* Parse integer */
if ( *endp ) {
return -EINVAL_INTEGER;
}
return 0;
}
/**
* Parse network device name
*
* @v text Text
* @ret netdev Network device
* @ret rc Return status code
*/
/* Sanity check */
/* Find network device */
if ( ! *netdev ) {
return -ENODEV;
}
return 0;
}
/**
* Parse menu name
*
* @v text Text
* @ret menu Menu
* @ret rc Return status code
*/
/* Find menu */
if ( ! *menu ) {
if ( text ) {
} else {
printf ( "No default menu\n" );
}
return -ENOENT;
}
return 0;
}
/**
* Parse flag
*
* @v text Text (ignored)
* @ret flag Flag to set
* @ret rc Return status code
*/
/* Set flag */
*flag = 1;
return 0;
}
/**
* Parse key
*
* @v text Text
* @ret key Key
* @ret rc Return status code
*/
/* Interpret single characters as being a literal key character */
return 0;
}
/* Otherwise, interpret as an integer */
}
/**
* Print command usage message
*
* @v cmd Command descriptor
* @v argv Argument list
*/
printf ( "Usage:\n\n %s %s\n\nSee http://ipxe.org/cmd/%s for further "
}
/**
* Reparse command-line options
*
* @v argc Argument count
* @v argv Argument list
* @v cmd Command descriptor
* @v opts Options (already initialised with default values)
* @ret rc Return status code
*/
void *opts ) {
+ 1 /* NUL */ ];
unsigned int shortopt_idx = 0;
void *value;
unsigned int i;
unsigned int j;
unsigned int num_args;
int c;
int rc;
/* Construct long and short option lists for getopt_long() */
for ( i = 0 ; i < cmd->num_options ; i++ ) {
}
/* Parse options */
NULL ) ) >= 0 ) {
switch ( c ) {
case 'h' :
/* Print help */
return -ECANCELED_NO_OP;
case '?' :
/* Print usage message */
return -EINVAL_UNKNOWN_OPTION;
case ':' :
/* Print usage message */
return -EINVAL_MISSING_ARGUMENT;
default:
/* Search for an option to parse */
for ( i = 0 ; i < cmd->num_options ; i++ ) {
continue;
return rc;
break;
}
}
}
/* Check remaining arguments */
return -ERANGE;
}
return 0;
}
/**
* Parse command-line options
*
* @v argc Argument count
* @v argv Argument list
* @v cmd Command descriptor
* @v opts Options (may be uninitialised)
* @ret rc Return status code
*/
void *opts ) {
/* Clear options */
}