Skip to contentSkip to conversation

dtc: Improve support for string escapes

PATCH Applied David Gibson 2 participants 2 messages Oct 16, 2007

lore
Share & tools
lore
#0 lore raw

dtc supports the use of C-style escapes (\n, \t and so forth) in string property definitions via the data_copy_escape_string() function. However, while it supports the most common escape characters, it doesn't support the full set that C does, which is a potential gotcha.

Worse, a bug in the lexer means that while data_copy_escape_string() can handle the \" escape, a string with such an escape won't lex correctly.

This patch fixes both problems, extending data_copy_escape_string() to support the missing escapes, and fixing the regex for strings in the lexer to handle internal escaped quotes.

This also adds a testcase for string escape functionality.

data.c +12 -0
Index: dtc/data.c
===================================================================
@@ -150,12 +150,24 @@ struct data data_copy_escape_string(char
 		c = s[i++];
 		assert(c);
 		switch (c) {
+		case 'a':
+			q[d.len++] = '\a';
+			break;
+		case 'b':
+			q[d.len++] = '\b';
+			break;
 		case 't':
 			q[d.len++] = '\t';
 			break;
 		case 'n':
 			q[d.len++] = '\n';
 			break;
+		case 'v':
+			q[d.len++] = '\v';
+			break;
+		case 'f':
+			q[d.len++] = '\f';
+			break;
 		case 'r':
 			q[d.len++] = '\r';
 			break;
dtc-lexer.l +1 -1
Index: dtc/dtc-lexer.l
===================================================================
@@ -69,7 +69,7 @@ REFCHAR		({PROPCHAR}|{UNITCHAR}|[/@])
 			}
 		}
 
-\"[^"]*\"	{
+\"([^\\"]|\\.)*\"	{
 			yylloc.filenum = srcpos_filenum;
 			yylloc.first_line = yylineno;
 			DPRINT("String: %s\n", yytext);
tests/testdata.h +1 -0
Index: dtc/tests/testdata.h
===================================================================
@@ -24,6 +24,7 @@
 #define TEST_VALUE_2	cell_to_fdt(0xabcd1234)
 
 #define TEST_STRING_1	"hello world"
+#define TEST_STRING_2	"nastystring: \a\b\t\n\v\f\r\\\"\xff"
 
 #ifndef __ASSEMBLY__
 extern struct fdt_header _test_tree1;
tests/escapes.dts new file +4 -0
Index: dtc/tests/escapes.dts
===================================================================
@@ -0,0 +1,4 @@
+/ {
+	compatible = "test_string_escapes";
+	escape-str = "nastystring: \a\b\t\n\v\f\r\\\"\xff";
+};
tests/run_tests.sh +3 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -104,6 +104,9 @@ dtc_tests () {
     run_test dtc.sh -f -I dts -O dtb -o dtc_tree1.test.dtb test_tree1.dts
     tree1_tests dtc_tree1.test.dtb
     tree1_tests_rw dtc_tree1.test.dtb
+
+    run_test dtc.sh -f -I dts -O dtb -o dtc_escapes.test.dtb escapes.dts
+    run_test string_escapes dtc_escapes.test.dtb
 }
 
 while getopts "vdt:" ARG ; do
tests/string_escapes.c new file +42 -0

42 lines not on this page · raw message

tests/Makefile.tests +2 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -7,7 +7,8 @@ LIB_TESTS_L = get_mem_rsv \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
 	move_and_save \
-	open_pack rw_tree1 setprop del_property del_node
+	open_pack rw_tree1 setprop del_property del_node \
+	string_escapes
 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)
 
 LIBTREE_TESTS_L = truncated_property
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Applied
#1 lore raw

So, like, the other day David Gibson mumbled:

Quoted 10 lines

dtc supports the use of C-style escapes (\n, \t and so forth) in string property definitions via the data_copy_escape_string() function. However, while it supports the most common escape characters, it doesn't support the full set that C does, which is a potential gotcha.

Worse, a bug in the lexer means that while data_copy_escape_string() can handle the \" escape, a string with such an escape won't lex correctly.

Quoted 7 lines

This patch fixes both problems, extending data_copy_escape_string() to support the missing escapes, and fixing the regex for strings in the lexer to handle internal escaped quotes.

This also adds a testcase for string escape functionality.

Excellent. Thanks.

tests/string_escapes.c line 3

Quoted 6 lines
tests/string_escapes.c new file +3 -0
@@ -0,0 +1,42 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ *	Testcase for strinc escapes in dtc

Applied, modulo fixing that typo.

jdl