UEFIStarter
a simple UEFI framework
Data Structures | Functions | Variables
cmdline.c File Reference

Command line parameter parser. More...

#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <UEFIStarter/core/cmdline.h>
#include <UEFIStarter/core/memory.h>
#include <UEFIStarter/core/logger.h>
#include <UEFIStarter/core/string.h>

Data Structures

struct  logger_args_mapping_t
 data type to map a log level to a command line argument More...
 

Functions

static BOOLEAN _wctype_number (CHAR16 *string, BOOLEAN allow_decimal)
 (internal) performs check whether string is numeric More...
 
BOOLEAN wctype_int (CHAR16 *string)
 Checks whether a string looks like an integer. More...
 
BOOLEAN wctype_float (CHAR16 *string)
 Checks whether a string looks like a number. More...
 
double _wcstof (CHAR16 *str)
 Converts a numeric string into a double value. More...
 
BOOLEAN validate_double_range (cmdline_value_t v, CHAR16 *field, double min, double max)
 Checks whether a double command line parameter is within boundaries. More...
 
BOOLEAN validate_uint64_range (cmdline_value_t v, CHAR16 *field, UINT64 min, UINT64 max)
 Checks whether a uint64 command line parameter is within boundaries. More...
 
static void _print_argument_group_help (cmdline_argument_group_t *arguments)
 (internal) prints the help text for a command line argument group More...
 
void print_help_text (UINTN argument_group_count, VA_LIST args)
 Outputs the help text for all argument groups, including the built-in ones. More...
 
BOOLEAN check_no_arguments_remaining (INTN argc, CHAR16 **argv)
 Checks if all command line arguments have been parsed successfully. More...
 
static BOOLEAN _parse_logger_args (INTN argc, CHAR16 **argv)
 (internal) parses logger/help parameters from command line More...
 
static BOOLEAN _parse_parameter_group (INTN argc, CHAR16 **argv, cmdline_argument_group_t *arguments)
 (internal) fills argument group structure with all matching command line arguments More...
 
EFI_STATUS parse_parameters (INTN argc, CHAR16 **argv, UINTN group_count, VA_LIST args)
 Parses command-line parameters. More...
 
CHAR16 ** argv_from_ascii (int argc, char **argv_ascii)
 Converts ASCII command line parameters to UTF-16. More...
 
void free_argv ()
 Frees the memory pages for the internal _argv storage.
 

Variables

static logger_args_mapping_t logger_args []
 log level -> command line argument mappings More...
 
static CHAR16 ** _argv =NULL
 internal storage for command line arguments converted to UTF-16
 
static UINTN _argv_pages =0
 number of memory pages allocated for _argv
 

Detailed Description

Command line parameter parser.

Author
Richard Nusser
License
GPLv3 (see http://www.gnu.org/licenses/)
See also
https://github.com/rinusser/UEFIStarter

Function Documentation

◆ _wctype_number()

static BOOLEAN _wctype_number ( CHAR16 *  string,
BOOLEAN  allow_decimal 
)
static

(internal) performs check whether string is numeric

Parameters
stringthe string to check, as UTF-16
allow_decimalwhether decimal numbers are OK
Returns
whether the string looks like a number
Todo:
the wctype* functions should be replaced by a built-in, or moved to a separate lib

◆ wctype_int()

BOOLEAN wctype_int ( CHAR16 *  string)

Checks whether a string looks like an integer.

Parameters
stringthe string to check, as UTF-16
Returns
whether it's an integer

◆ wctype_float()

BOOLEAN wctype_float ( CHAR16 *  string)

Checks whether a string looks like a number.

Parameters
stringthe string to check, as UTF-16
Returns
whether it's a decimal number

◆ _wcstof()

double _wcstof ( CHAR16 *  str)

Converts a numeric string into a double value.

Errors will produce an ERROR level log message.

Parameters
strthe string to convert
Returns
the parsed number; -1.0 on error
Todo:
replace _wcstof() with built-in, or move to separate file

◆ validate_double_range()

BOOLEAN validate_double_range ( cmdline_value_t  v,
CHAR16 *  field,
double  min,
double  max 
)

Checks whether a double command line parameter is within boundaries.

Parameters
vthe command line parameter
fieldthe field name to use in error messages, as UTF-16
minthe lowest allowed value (inclusive)
maxthe highest allowed value (inclusive)
Returns
whether the parameter is within the boundaries

◆ validate_uint64_range()

BOOLEAN validate_uint64_range ( cmdline_value_t  v,
CHAR16 *  field,
UINT64  min,
UINT64  max 
)

Checks whether a uint64 command line parameter is within boundaries.

Parameters
vthe command line parameter
fieldthe field name to use in error messages, as UTF-16
minthe lowest allowed value (inclusive)
maxthe highest allowed value (inclusive)
Returns
whether the parameter is within the boundaries

◆ _print_argument_group_help()

static void _print_argument_group_help ( cmdline_argument_group_t arguments)
static

(internal) prints the help text for a command line argument group

Parameters
argumentsthe argument group
Todo:
could mind required parameters (currently numbers for double/int args) for pad length, then remove typetext padding in Print() below
Todo:
combine pad length for different groups that were combined, e.g. builtin and custom audio options

◆ print_help_text()

void print_help_text ( UINTN  argument_group_count,
VA_LIST  args 
)

Outputs the help text for all argument groups, including the built-in ones.

Parameters
argument_group_countthe number of argument groups passed
argsthe vararg list of argument groups (cmdline_argument_group_t *)

◆ check_no_arguments_remaining()

BOOLEAN check_no_arguments_remaining ( INTN  argc,
CHAR16 **  argv 
)

Checks if all command line arguments have been parsed successfully.

The parser actually shortens the processed strings to 0 length, this checks for that.

Parameters
argcthe number of command-line arguments
argvthe list of command-line arguments, as UTF-16
Returns
whether all parameters have been handled

◆ _parse_logger_args()

static BOOLEAN _parse_logger_args ( INTN  argc,
CHAR16 **  argv 
)
static

(internal) parses logger/help parameters from command line

Parameters
argcthe number of command-line arguments
argvthe list of command-line arguments, as UTF-16
Returns
whether the help screen was requested

◆ _parse_parameter_group()

static BOOLEAN _parse_parameter_group ( INTN  argc,
CHAR16 **  argv,
cmdline_argument_group_t arguments 
)
static

(internal) fills argument group structure with all matching command line arguments

Parameters
argcthe number of command-line arguments
argvthe list of command-line arguments, as UTF-16
argumentsthe argument group to parse values into
Returns
whether all matching parameters have been parsed successfully

◆ parse_parameters()

EFI_STATUS parse_parameters ( INTN  argc,
CHAR16 **  argv,
UINTN  group_count,
VA_LIST  args 
)

Parses command-line parameters.

Parameters
argcthe number of command-line arguments
argvthe list of command-line arguments, as UTF-16
group_countthe number of command line argument groups passed
argsthe vararg list of command line groups (as cmdline_argument_group_t *)
Returns
an EFI status code

◆ argv_from_ascii()

CHAR16** argv_from_ascii ( int  argc,
char **  argv_ascii 
)

Converts ASCII command line parameters to UTF-16.

The StdLib's entry function passes arguments as ASCII, you can use this function to convert the arguments list.

Parameters
argcthe number of command-line arguments
argv_asciithe list of command-line arguments, as ASCII
Returns
the same command-line arguments, as UTF-16

Variable Documentation

◆ logger_args

logger_args_mapping_t logger_args[]
static
Initial value:
=
{
{ TRACE, L"-trace" },
{ DEBUG, L"-debug" },
{ INFO, L"-info" },
{ WARN, L"-warn" },
{ ERROR, L"-error" },
{ OFF, L"-no-log" }
}
warnings: something may have broken
Definition: logger.h:46
debug messages: something breaks and I&#39;m trying to fix it
Definition: logger.h:48
trace messages: something breaks and I don&#39;t know what or where
Definition: logger.h:49
error messages: something definitely broke
Definition: logger.h:45
disable logging: la-la-la, I can&#39;t hear you
Definition: logger.h:44
informative messages: just in case something breaks
Definition: logger.h:47

log level -> command line argument mappings