summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKay Sievers <kay@vrfy.org>2012-10-31 12:53:22 +0100
committerKay Sievers <kay@vrfy.org>2012-10-31 12:53:22 +0100
commitd0d4d7f144bcacbb5aef5dbbae2c8762daa94e1a (patch)
tree1f0b359a55311d1bd14f06ebfa066efd566d36da
parent7f64a76d44c3f5a51cb4297b17f53cd02f6a15a4 (diff)
properly skip empty lines in config files
-rw-r--r--gummiboot.c62
1 files changed, 34 insertions, 28 deletions
diff --git a/gummiboot.c b/gummiboot.c
index 3b91390..41c5faa 100644
--- a/gummiboot.c
+++ b/gummiboot.c
@@ -944,44 +944,48 @@ static CHAR8 *strchra(CHAR8 *s, CHAR8 c) {
return NULL;
}
-static CHAR8 *line_get_key_value(CHAR8 *line, CHAR8 **key_ret, CHAR8 **value_ret) {
- CHAR8 *next;
- CHAR8 *key, *value;
+static CHAR8 *line_get_key_value(CHAR8 *content, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) {
+ CHAR8 *line;
UINTN linelen;
+ CHAR8 *value;
skip:
- /* terminate */
- next = line;
- while (*next && !strchra((CHAR8 *)"\n\r", *next))
- next++;
- *next = '\0';
-
- linelen = next - line;
- if (linelen == 0)
+ line = content + *pos;
+ if (*line == '\0')
return NULL;
- /* next line */
- next++;
- while (*next && strchra((CHAR8 *)"\n\r", *next))
- next++;
+ linelen = 0;
+ while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
+ linelen++;
- /* trailing whitespace */
- while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1]))
- linelen--;
+ /* move pos to next line */
+ *pos += linelen;
+ if (content[*pos])
+ (*pos)++;
+
+ /* empty line */
+ if (linelen == 0)
+ goto skip;
+
+ /* terminate line */
line[linelen] = '\0';
- /* leading whitespace */
- while (strchra((CHAR8 *)" \t", *line))
+ /* remove leading whitespace */
+ while (strchra((CHAR8 *)" \t", *line)) {
line++;
+ linelen--;
+ }
- key = line;
- line = next;
+ /* remove trailing whitespace */
+ while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1]))
+ linelen--;
+ line[linelen] = '\0';
- if (*key == '#')
+ if (*line == '#')
goto skip;
/* split key/value */
- value = key;
+ value = line;
while (*value && !strchra((CHAR8 *)" \t", *value))
value++;
if (*value == '\0')
@@ -991,17 +995,18 @@ skip:
while (*value && strchra((CHAR8 *)" \t", *value))
value++;
- *key_ret = key;
+ *key_ret = line;
*value_ret = value;
- return next;
+ return line;
}
static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
CHAR8 *line;
+ UINTN pos = 0;
CHAR8 *key, *value;
line = content;
- while ((line = line_get_key_value(line, &key, &value))) {
+ while ((line = line_get_key_value(content, &pos, &key, &value))) {
if (strcmpa((CHAR8 *)"timeout", key) == 0) {
CHAR16 *s;
@@ -1022,6 +1027,7 @@ static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) {
ConfigEntry *entry;
CHAR8 *line;
+ UINTN pos = 0;
CHAR8 *key, *value;
UINTN len;
CHAR16 *initrd = NULL;
@@ -1029,7 +1035,7 @@ static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR1
entry = AllocateZeroPool(sizeof(ConfigEntry));
line = content;
- while ((line = line_get_key_value(line, &key, &value))) {
+ while ((line = line_get_key_value(content, &pos, &key, &value))) {
if (strcmpa((CHAR8 *)"title", key) == 0) {
FreePool(entry->title);
entry->title = stra_to_str(value);