summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-07-06 10:10:05 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-09-08 16:12:34 +0200
commit78bde1b41e5cb692ea9ffbfff4d35edf3162dcdf (patch)
tree28e73d3dc2899c80abaafe2af66367fce91989cb
parent87ce8402c1c3c62dadce1537bd255c27c94959eb (diff)
downloadcaldr-78bde1b41e5cb692ea9ffbfff4d35edf3162dcdf.tar.gz
rem, common: handle string sanitization
-rw-r--r--common.lua39
-rw-r--r--rem.lua49
2 files changed, 63 insertions, 25 deletions
diff --git a/common.lua b/common.lua
index c009207..6afde62 100644
--- a/common.lua
+++ b/common.lua
@@ -1,3 +1,5 @@
+local lpeg = require "lpeg"
+
local string = require "string"
local stringformat = string.format
@@ -20,11 +22,38 @@ end
local set_verbosity = function (n) verboselvl = n end
+local trim_whitespace
+local unescape_string
+do
+ local P = lpeg.P
+ local S = lpeg.S
+ local Cs = lpeg.Cs
+ local p_ws_char = S" \n\r\t\v"
+ local p_ws = p_ws_char^1
+ local p_ws_trailing = p_ws * P (-1)
+ local p_ws_drop = Cs ( (p_ws^-1 / "")
+ * (1 - p_ws_trailing)^0
+ * (p_ws^-1 * P (-1) / ""))
+
+ local p_escape_char = (P"\\n" / "\n")
+ + (P"\\r" / "\r")
+ + (P"\\t" / "\t")
+ + (P"\\v" / "\v")
+ local p_unescape = Cs ((p_escape_char + 1)^0)
+
+ local lpegmatch = lpeg.match
+
+ trim_whitespace = function (s) return lpegmatch (p_ws_drop , s) end
+ unescape_string = function (s) return lpegmatch (p_unescape, s) end
+end
+
return
- { println = mk_out ("stdout", 0, true)
- , errorln = mk_out ("stderr", 0, true)
- , noiseln = mk_out ("stderr", 1, true)
- , debugln = mk_out ("stderr", 2, true)
- , set_verbosity = set_verbosity
+ { println = mk_out ("stdout", 0, true)
+ , errorln = mk_out ("stderr", 0, true)
+ , noiseln = mk_out ("stderr", 1, true)
+ , debugln = mk_out ("stderr", 2, true)
+ , set_verbosity = set_verbosity
+ , trim_whitespace = trim_whitespace
+ , unescape_string = unescape_string
}
diff --git a/rem.lua b/rem.lua
index e1fdc1f..ed3a355 100644
--- a/rem.lua
+++ b/rem.lua
@@ -6,30 +6,33 @@
local inspect = require "ext.inspect"
-local pcall = pcall
+local pcall = pcall
-local io = require "io"
-local iostdout = io.stdout
+local io = require "io"
+local iostdout = io.stdout
-local math = require "math"
-local mathfloor = math.floor
+local math = require "math"
+local mathfloor = math.floor
-local os = os
-local osdate = os.date
-local ostime = os.time
+local os = os
+local osdate = os.date
+local ostime = os.time
-local string = require "string"
-local stringformat = string.format
-local stringsub = string.sub
+local string = require "string"
+local stringformat = string.format
+local stringsub = string.sub
+local stringgsub = string.gsub
-local table = require "table"
-local tableconcat = table.concat
+local table = require "table"
+local tableconcat = table.concat
-local common = require "common"
-local println = common.println
-local errorln = common.errorln
-local noiseln = common.noiseln
-local debugln = common.debugln
+local common = require "common"
+local println = common.println
+local errorln = common.errorln
+local noiseln = common.noiseln
+local debugln = common.debugln
+local trim_whitespace = common.trim_whitespace
+local unescape_string = common.unescape_string
local seconds_of_time = function (t)
return t % 60
@@ -245,7 +248,13 @@ local rem_of_vcalendars do
end
local msg_of_description = function (val)
- return val.value
+ local sane = unescape_string (val.value)
+
+ return trim_whitespace (sane)
+ end
+
+ local msg_of_string = function (s)
+ return stringgsub (s, "\n", "^M")
end
local rem_of_vevent = function (ev)
@@ -280,7 +289,7 @@ local rem_of_vcalendars do
error ("could not derive message text from event values")
end
- return stringformat ("REM %s MSG %s", date, msg)
+ return stringformat ("REM %s MSG %s", date, msg_of_string (msg))
end
local rem_of_vcalendar = function (vcal)