on master help sign in
PParser.hs: filename: Verify it is a non-empty filename
3ccdc4bJeremy W. Sherman
src/Pkg/PParser.hs+20−10
@@ -14,6 +14,7 @@ import Idris.CmdOptions
14
14
15import Control.Monad.State.Strict
15import Control.Monad.State.Strict
16import Control.Applicative
16import Control.Applicative
17+import System.FilePath (takeFileName)
17
18
18import Util.System
19import Util.System
19
20
@@ -67,17 +68,26 @@ pPkg = do
67-- |
68-- |
68-- | Treated for now as an identifier or a double-quoted string.
69-- | Treated for now as an identifier or a double-quoted string.
69filename :: (MonadicParsing m, HasLastTokenSpan m) => m String
70filename :: (MonadicParsing m, HasLastTokenSpan m) => m String
70−filename = token $
71+filename = (do
72+ filename <- token $
71− -- Treat a double-quoted string as a filename to support spaces.
73+ -- Treat a double-quoted string as a filename to support spaces.
72− -- This also moves away from tying filenames to identifiers, so
74+ -- This also moves away from tying filenames to identifiers, so
73− -- it will also accept hyphens
75+ -- it will also accept hyphens
74− -- (https://github.com/idris-lang/Idris-dev/issues/2721)
76+ -- (https://github.com/idris-lang/Idris-dev/issues/2721)
75− stringLiteral
77+ stringLiteral
76− <|>
78+ <|>
77− -- Through at least version 0.9.19.1, IPKG executable values were
79+ -- Through at least version 0.9.19.1, IPKG executable values were
78− -- possibly namespaced identifiers, like foo.bar.baz.
80+ -- possibly namespaced identifiers, like foo.bar.baz.
79− show <$> fst <$> iName []
81+ show <$> fst <$> iName []
82+ guard $ isValidFilename filename
83+ return filename)
80 <?> "filename"
84 <?> "filename"
85+ where
86+ isValidFilename :: FilePath -> Bool
87+ isValidFilename path = isNonEmpty && hasNoDirectoryComponent
88+ where
89+ isNonEmpty = path /= ""
90+ hasNoDirectoryComponent = path == takeFileName path
81
91
82
92
83pClause :: PParser ()
93pClause :: PParser ()