diff options
author | bptato <nincsnevem662@gmail.com> | 2024-08-13 22:36:34 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-08-13 22:44:41 +0200 |
commit | 968de41082280dde47bac7c2bb59522284b4c672 (patch) | |
tree | 78a119176fb6ef7a668c91ab1a417db1eaf4f1ab /src/config | |
parent | a766983bff89b834017c08b252c2de1d3a2c5b4d (diff) | |
download | chawan-968de41082280dde47bac7c2bb59522284b4c672.tar.gz |
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
Diffstat (limited to 'src/config')
-rw-r--r-- | src/config/mailcap.nim | 32 |
1 files changed, 13 insertions, 19 deletions
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] = |