]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
krprint: allow tab and newline in pretty printer
authorPetr Špaček <petr.spacek@nic.cz>
Mon, 26 Oct 2020 18:06:40 +0000 (19:06 +0100)
committerPetr Špaček <petr.spacek@nic.cz>
Mon, 2 Nov 2020 09:28:55 +0000 (10:28 +0100)
Functions like trust_anchors.summary() return strings including tabs and
newlines, so we do not escape those for human-readable output.

daemon/lua/krprint.lua

index 44d67c323db0aa087409e598d6d233ccaac7277e..2bd4dd73fbac7d3ac231d97d7d389787fdf1f640 100644 (file)
@@ -85,15 +85,18 @@ function base_class.number(_, val)
        end
 end
 
-function base_class.string(_, val)
+function base_class.char_is_printable(_, c)
+       -- ASCII (from space to ~) and not ' or \
+       return (c >= 0x20 and c < 0x7f)
+               and c ~= 0x27 and c ~= 0x5C
+end
+
+function base_class.string(self, val)
        assert(type(val) == 'string')
-       val = tostring(val)
        local chars = {'\''}
        for i = 1, #val do
                local c = string.byte(val, i)
-               -- ASCII (from space to ~) and not ' or \
-               if (c >= 0x20 and c < 0x7f)
-                       and c ~= 0x27 and c ~= 0x5C then
+               if self:char_is_printable(c) then
                        table.insert(chars, string.char(c))
                else
                        table.insert(chars, string.format('\\%03d', c))
@@ -245,6 +248,14 @@ function pprinter_class._fallback(self, val)
        return tostring(val)
 end
 
+function pprinter_class.char_is_printable(_, c)
+       -- ASCII (from space to ~) + tab or newline
+       -- and not ' or \
+       return ((c >= 0x20 and c < 0x7f)
+               or c == 0x09 or c == 0x0A)
+               and c ~= 0x27 and c ~= 0x5C
+end
+
 -- "function" is a Lua keyword so assignment below is workaround to create
 -- function pprinter_class.function(self, f)
 pprinter_class['function'] = function(self, f)