From d49d4cf2ea9a227e3f310105a8d7d2cb31426d95 Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Wed, 30 Dec 2015 20:46:06 +0300 Subject: [PATCH] [Shell-escape] Escape toggle with \ prefix E.g. check file.hs will treat quote characters as literal characters, while \check file.hs will assume quoting behavior Backslash will be dropped, naturally. --- src/GHCMod/Options/ShellParse.hs | 4 +++- test/ShellParseSpec.hs | 14 ++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/GHCMod/Options/ShellParse.hs b/src/GHCMod/Options/ShellParse.hs index 4ad4efe..2bcba0c 100644 --- a/src/GHCMod/Options/ShellParse.hs +++ b/src/GHCMod/Options/ShellParse.hs @@ -36,4 +36,6 @@ go (c:cl) curarg accargs quotes | otherwise = go cl (c:curarg) accargs quotes parseCmdLine :: String -> [String] -parseCmdLine comline = go comline [] [] False +parseCmdLine ('\\':comline) = go comline [] [] False +parseCmdLine [] = [""] +parseCmdLine comline = words comline diff --git a/test/ShellParseSpec.hs b/test/ShellParseSpec.hs index 8817ec7..30274f9 100644 --- a/test/ShellParseSpec.hs +++ b/test/ShellParseSpec.hs @@ -8,11 +8,17 @@ import Test.Hspec spec :: Spec spec = describe "parseCmdLine" $ do - it "splits arguments" $ + it "splits arguments" $ do parseCmdLine "test command line" `shouldBe` ["test", "command", "line"] - it "honors quoted segments" $ - parseCmdLine "test command line \STXwith quoted segment\ETX" + parseCmdLine "\\test command line" `shouldBe` ["test", "command", "line"] + it "honors quoted segments if turned on" $ + parseCmdLine "\\test command line \STXwith quoted segment\ETX" `shouldBe` ["test", "command", "line", "with quoted segment"] - it "squashes multiple spaces" $ + it "doesn't honor quoted segments if turned off" $ + parseCmdLine "test command line \STXwith quoted segment\ETX" + `shouldBe` words "test command line \STXwith quoted segment\ETX" + it "squashes multiple spaces" $ do parseCmdLine "test command" `shouldBe` ["test", "command"] + parseCmdLine "\\test command" + `shouldBe` ["test", "command"]