From 968de41082280dde47bac7c2bb59522284b4c672 Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 13 Aug 2024 22:36:34 +0200 Subject: mailcap: fix weird error handling * don't accept flags specified as named fields * don't error out on named field specified as flag * allow trailing whitespace on named fields & flags --- src/config/mailcap.nim | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'src/config/mailcap.nim') diff --git a/src/config/mailcap.nim b/src/config/mailcap.nim index 1d81b20a..d1c103b5 100644 --- a/src/config/mailcap.nim +++ b/src/config/mailcap.nim @@ -123,19 +123,10 @@ proc consumeCommand(state: var MailcapParser): Result[string, string] = return ok(s) type NamedField = enum - nmNone = "none" nmTest = "test" nmNametemplate = "nametemplate" nmEdit = "edit" -proc parseFieldKey(entry: var MailcapEntry; k: string): NamedField = - if (let x = parseEnumNoCase[MailcapFlag](k); x.isSome): - entry.flags.incl(x.get) - return nmNone - if (let x = parseEnumNoCase[NamedField](k); x.isSome): - return x.get - return nmNone - proc consumeField(state: var MailcapParser; entry: var MailcapEntry): Result[bool, string] = state.skipBlanks() @@ -149,20 +140,23 @@ proc consumeField(state: var MailcapParser; entry: var MailcapEntry): of '\r': continue of '=': - let f = parseFieldKey(entry, buf) let cmd = ?state.consumeCommand() - case f - of nmNone: discard - of nmTest: entry.test = cmd - of nmNametemplate: entry.nametemplate = cmd - of nmEdit: entry.edit = cmd + while buf.len > 0 and buf[^1] in AsciiWhitespace: + buf.setLen(buf.len - 1) + if (let x = parseEnumNoCase[NamedField](buf); x.isSome): + case x.get + of nmTest: entry.test = cmd + of nmNametemplate: entry.nametemplate = cmd + of nmEdit: entry.edit = cmd return ok(state.consume() == ';') + elif c in Controls: + return err("line " & $state.line & ": invalid character in field: " & c) else: - if c in Controls: - return err("line " & $state.line & ": invalid character in field: " & c) buf &= c - if parseFieldKey(entry, buf) != nmNone: - return err("Expected command") + while buf.len > 0 and buf[^1] in AsciiWhitespace: + buf.setLen(buf.len - 1) + if (let x = parseEnumNoCase[MailcapFlag](buf); x.isSome): + entry.flags.incl(x.get) return ok(res) proc parseMailcap*(stream: Stream): Result[Mailcap, string] = -- cgit 1.4.1-2-gfad0