From e9a173120fc95c669d40fddeb7425bc8ada18495 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Thu, 22 Aug 2019 09:21:05 -0500 Subject: [PATCH] scripts/dts/gen_defines.py: generalize string escape Add str2str to make the conversion of a string into a C literal with all necessary escapes and enclosing double quotes available outside a function that emits a define. Signed-off-by: Peter A. Bigot --- scripts/dts/gen_defines.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index ef881ec375d..1e3a406d6a7 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -597,18 +597,16 @@ def out_dev(dev, ident, val, name_alias=None): def out_dev_s(dev, ident, s): - # Like out_dev(), but puts quotes around 's' and escapes any double quotes - # and backslashes within it + # Like out_dev(), but emits 's' as a string literal - # \ must be escaped before " to avoid double escaping - out_dev(dev, ident, '"{}"'.format(escape(s))) + out_dev(dev, ident, quote_str(s)) def out_s(ident, val): - # Like out(), but puts quotes around 's' and escapes any double quotes and - # backslashes within it + # Like out(), but puts quotes around 'val' and escapes any double + # quotes and backslashes within it - out(ident, '"{}"'.format(escape(val))) + out(ident, quote_str(val)) def out(ident, val, aliases=()): @@ -652,6 +650,13 @@ def escape(s): return s.replace("\\", "\\\\").replace('"', '\\"') +def quote_str(s): + # Puts quotes around 's' and escapes any double quotes and + # backslashes within it + + return '"{}"'.format(escape(s)) + + def err(s): raise Exception(s)