diff --git a/lib/bootconfig.c b/lib/bootconfig.c index 819685d6c68d..948c8c10e4b9 100644 --- a/lib/bootconfig.c +++ b/lib/bootconfig.c @@ -428,9 +428,9 @@ int __init xbc_node_compose_key_after(struct xbc_node *root, node = xbc_node_get_parent(node); while (node && node != root) { - keys[depth++] = xbc_node_index(node); if (depth == XBC_DEPTH_MAX) return -ERANGE; + keys[depth++] = xbc_node_index(node); node = xbc_node_get_parent(node); } if (!node && root) @@ -793,16 +793,17 @@ static int __init __xbc_parse_value(char **__v, char **__n) return xbc_parse_error("No value delimiter", p); if (*p) p++; - break; + goto skip_trim; } if (strchr(",;\n#}", c)) { *p++ = '\0'; - v = strim(v); break; } } if (quotes) return xbc_parse_error("No closing quotes", p); + v = strim(v); +skip_trim: if (c == '#') { p = skip_comment(p); c = '\n'; /* A comment must be treated as a newline */ diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index 6f476e3c06b8..d3f840c5adce 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -68,6 +68,23 @@ static int xbc_show_value(struct xbc_node *node, bool semicolon) return i; } +/* Match the conditions under which the compact formatter opens braces. */ +static bool xbc_node_needs_braces(struct xbc_node *node) +{ + struct xbc_node *child = xbc_node_get_child(node); + + if (child && xbc_node_is_value(child)) + child = xbc_node_get_next(child); + if (!child) + return false; + if (child->next) + return true; + + /* A single subkey with both a value and subkeys is not compacted. */ + child = xbc_node_get_child(child); + return child && xbc_node_is_value(child) && child->next; +} + static void xbc_show_compact_tree(void) { struct xbc_node *node, *cnode = NULL, *vnode; @@ -128,14 +145,12 @@ static void xbc_show_compact_tree(void) node = xbc_node_get_parent(node); if (!node) return; - if (!xbc_node_get_child(node)->next) + if (!depth || !xbc_node_needs_braces(node)) continue; - if (depth) { - depth--; - for (i = 0; i < depth; i++) - printf("\t"); - printf("}\n"); - } + depth--; + for (i = 0; i < depth; i++) + printf("\t"); + printf("}\n"); } node = xbc_node_get_next(node); } diff --git a/tools/bootconfig/samples/exp-good-mixed-kv1.bconf b/tools/bootconfig/samples/exp-good-mixed-kv1.bconf index 8346287d9251..0b8b2f59da31 100644 --- a/tools/bootconfig/samples/exp-good-mixed-kv1.bconf +++ b/tools/bootconfig/samples/exp-good-mixed-kv1.bconf @@ -1,2 +1,7 @@ key = "value"; key.subkey = "another-value"; +a { + b = "v"; + b.c = "w"; + z = "q"; +} diff --git a/tools/bootconfig/samples/good-mixed-kv1.bconf b/tools/bootconfig/samples/good-mixed-kv1.bconf index 1761547dd05c..35442ee62b75 100644 --- a/tools/bootconfig/samples/good-mixed-kv1.bconf +++ b/tools/bootconfig/samples/good-mixed-kv1.bconf @@ -1,3 +1,8 @@ # value -> subkey pattern key = value key.subkey = another-value + +# A valued parent with one subkey must not close its enclosing brace. +a.b = v +a.b.c = w +a.z = q diff --git a/tools/bootconfig/test-bootconfig.sh b/tools/bootconfig/test-bootconfig.sh index 530ce7e28d63..f584f7637e12 100755 --- a/tools/bootconfig/test-bootconfig.sh +++ b/tools/bootconfig/test-bootconfig.sh @@ -100,6 +100,16 @@ xpass $BOOTCONF -a $TEMPCONF $INITRD printf "badnode\n" >> $TEMPCONF xfail $BOOTCONF -a $TEMPCONF $INITRD +echo "Max key depth check" + +printf 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p = "value"\n' > $TEMPCONF +xpass $BOOTCONF -a $TEMPCONF $INITRD +$BOOTCONF -l $INITRD > $OUTFILE +xpass cmp $TEMPCONF $OUTFILE + +printf 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q = "value"\n' > $TEMPCONF +xfail $BOOTCONF -a $TEMPCONF $INITRD + echo "Max filesize check" # Max size is 32767 (including terminal byte) @@ -171,6 +181,24 @@ $BOOTCONF $INITRD > $OUTFILE xfail grep -q 'val[[:space:]]' $OUTFILE xpass grep -q 'val2[[:space:]]' $OUTFILE +echo "Remove unquoted trailing whitespace at end of file" +printf 'foo = val \t' > $TEMPCONF +$BOOTCONF -l $TEMPCONF > $OUTFILE +printf 'foo = "val"\n' > $TEMPCONF +xpass cmp $TEMPCONF $OUTFILE + +echo "Remove trailing whitespace from the last array value at end of file" +printf 'foo = first, last \t' > $TEMPCONF +$BOOTCONF -l $TEMPCONF > $OUTFILE +printf 'foo = "first", "last"\n' > $TEMPCONF +xpass cmp $TEMPCONF $OUTFILE + +echo "Keep quoted trailing whitespace at end of file" +printf 'foo = "val "' > $TEMPCONF +$BOOTCONF -l $TEMPCONF > $OUTFILE +printf 'foo = "val "\n' > $TEMPCONF +xpass cmp $TEMPCONF $OUTFILE + echo "Showing correct line:column of no closing brace" cat > $TEMPCONF << EOF foo {