[core] Add a new isin command

This commit aim at providing a new solution to write iPXE script when
working with string that you know will contain a certain keyword, but
don't know, or don't want, to write or compare the full string.

This also help with some string especially the one that are wrongfully
set in the SMBIOS values (with trailing whitespace...).

With the `isin` command you will be able to check if a string correctly
contains a certain substring, without having to know the full string,
allowing for easier scripting.

```
iPXE> isin --help
Usage:

  isin <string> <substring>

See https://ipxe.org/cmd/isin for further information
iPXE> isin ${product} Capri2 && echo is_in || echo is_not_in
is_in
iPXE> isin ${product} test && echo is_in || echo is_not_in
is_not_in
iPXE> echo ${product}
Capri2
```
This commit is contained in:
Renaud Duret 2024-10-30 19:13:25 +01:00
parent 8fc11d8a4a
commit ccd0d0210d
No known key found for this signature in database
GPG Key ID: F56C44F9BF88D8CC

View File

@ -549,6 +549,42 @@ struct command iseq_command __command = {
.exec = iseq_exec,
};
/** "isin" options */
struct isin_options {};
/** "isin" option list */
static struct option_descriptor isin_opts[] = {};
/** "isin" command descriptor */
static struct command_descriptor isin_cmd =
COMMAND_DESC ( struct isin_options, isin_opts, 2, 2,
"<string> <substring>");
/**
* "isin" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int isin_exec (int argc, char **argv ) {
struct isin_options opts;
int rc;
/* Parse options */
if ( ( rc = parse_options ( argc, argv, &isin_cmd, &opts ) ) != 0 )
return rc;
/* Return success if first string contains second string */
return ( ( strstr ( argv[optind], argv[ optind +1 ] ) != NULL ) ?
0: -ERANGE );
}
struct command isin_command __command = {
.name = "isin",
.exec = isin_exec,
};
/** "sleep" options */
struct sleep_options {};