david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[settings] Add "hexhyp" setting type

Provide a "hexhyp" setting type, which functions identically to the
"hex" setting type except that it uses a hyphen instead of a colon as
the byte delimiter.

For example, if ${mac} expands to "52:54:00:12:34:56", then
${mac:hexhyp} will expand to "52-54-00-12-34-56".

Originally-implemented-by: Jarrod Johnson <jarrod.b.johnson@gmail.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2010-11-13 00:22:23 +00:00
parent 4526f431d7
commit 312ae024d8
1 changed files with 43 additions and 4 deletions

View File

@ -1312,6 +1312,7 @@ static int storef_hex ( struct settings *settings, struct setting *setting,
case '\0' :
return store_setting ( settings, setting, bytes, len );
case ':' :
case '-' :
ptr++;
break;
default :
@ -1327,10 +1328,11 @@ static int storef_hex ( struct settings *settings, struct setting *setting,
* @v setting Setting to fetch
* @v buf Buffer to contain formatted value
* @v len Length of buffer
* @v delimiter Byte delimiter
* @ret len Length of formatted value, or negative error
*/
static int fetchf_hex ( struct settings *settings, struct setting *setting,
char *buf, size_t len ) {
char *buf, size_t len, const char *delimiter ) {
int raw_len;
int check_len;
int used = 0;
@ -1353,18 +1355,55 @@ static int fetchf_hex ( struct settings *settings, struct setting *setting,
buf[0] = 0; /* Ensure that a terminating NUL exists */
for ( i = 0 ; i < raw_len ; i++ ) {
used += ssnprintf ( ( buf + used ), ( len - used ),
"%s%02x", ( used ? ":" : "" ),
"%s%02x", ( used ? delimiter : "" ),
raw[i] );
}
return used;
}
}
/** A hex-string setting */
/**
* Fetch and format value of hex string setting (using colon delimiter)
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
* @v buf Buffer to contain formatted value
* @v len Length of buffer
* @ret len Length of formatted value, or negative error
*/
static int fetchf_hex_colon ( struct settings *settings,
struct setting *setting,
char *buf, size_t len ) {
return fetchf_hex ( settings, setting, buf, len, ":" );
}
/**
* Fetch and format value of hex string setting (using hyphen delimiter)
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
* @v buf Buffer to contain formatted value
* @v len Length of buffer
* @ret len Length of formatted value, or negative error
*/
static int fetchf_hex_hyphen ( struct settings *settings,
struct setting *setting,
char *buf, size_t len ) {
return fetchf_hex ( settings, setting, buf, len, "-" );
}
/** A hex-string setting (colon-delimited) */
struct setting_type setting_type_hex __setting_type = {
.name = "hex",
.storef = storef_hex,
.fetchf = fetchf_hex,
.fetchf = fetchf_hex_colon,
};
/** A hex-string setting (hyphen-delimited) */
struct setting_type setting_type_hexhyp __setting_type = {
.name = "hexhyp",
.storef = storef_hex,
.fetchf = fetchf_hex_hyphen,
};
/**