Skip to content

Commit

Permalink
Take line endings into account in IOEncodingUTF8 mode
Browse files Browse the repository at this point in the history
When collecting the output from programs.
  • Loading branch information
dcoutts committed Dec 2, 2009
1 parent d876c40 commit 4983da9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
10 changes: 2 additions & 8 deletions Distribution/ParseUtils.hs
Expand Up @@ -76,7 +76,8 @@ import Distribution.Compat.ReadP as ReadP hiding (get)
import Distribution.ReadE
import Distribution.Text
( Text(..) )
import Distribution.Simple.Utils (intercalate, lowercase)
import Distribution.Simple.Utils
( intercalate, lowercase, normaliseLineEndings )
import Language.Haskell.Extension (Extension)

import Text.PrettyPrint.HughesPJ hiding (braces)
Expand Down Expand Up @@ -428,13 +429,6 @@ trimLeading = dropWhile isSpace
trimTrailing = reverse . dropWhile isSpace . reverse


-- | Fix different systems silly line ending conventions
normaliseLineEndings :: String -> String
normaliseLineEndings [] = []
normaliseLineEndings ('\r':'\n':s) = '\n' : normaliseLineEndings s -- windows
normaliseLineEndings ('\r':s) = '\n' : normaliseLineEndings s -- old osx
normaliseLineEndings ( c :s) = c : normaliseLineEndings s

type SyntaxTree = Tree (LineNo, HasTabs, String)

-- | Parse the stream of tokens into a tree of them, based on indent \/ layout
Expand Down
2 changes: 2 additions & 0 deletions Distribution/Simple/Program/HcPkg.hs
Expand Up @@ -132,6 +132,8 @@ dump verbosity hcPkg packagedb = do
[] -> Left [ pkg | ParseOk _ pkg <- parsed ]
msgs -> Right msgs

--TODO: this could be a lot faster. We're doing normaliseLineEndings twice
-- and converting back and forth with lines/unlines.
splitPkgs :: String -> [String]
splitPkgs = map unlines . splitWith ("---" ==) . lines
where
Expand Down
6 changes: 4 additions & 2 deletions Distribution/Simple/Program/Run.hs
Expand Up @@ -26,7 +26,7 @@ import Distribution.Simple.Program.Types
( ConfiguredProgram(..), programPath )
import Distribution.Simple.Utils
( die, rawSystemExit, rawSystemStdInOut
, toUTF8, fromUTF8 )
, toUTF8, fromUTF8, normaliseLineEndings )
import Distribution.Verbosity
( Verbosity )

Expand Down Expand Up @@ -129,12 +129,14 @@ getProgramInvocationOutput verbosity
progInvokeOutputEncoding = encoding
} = do
let utf8 = case encoding of IOEncodingUTF8 -> True; _ -> False
decode | utf8 = fromUTF8 . normaliseLineEndings
| otherwise = id
(output, errors, exitCode) <- rawSystemStdInOut verbosity
path args
Nothing utf8
when (exitCode /= ExitSuccess) $
die errors
return (if utf8 then fromUTF8 output else output)
return (decode output)


getProgramInvocationOutput _ _ =
Expand Down
8 changes: 8 additions & 0 deletions Distribution/Simple/Utils.hs
Expand Up @@ -117,6 +117,7 @@ module Distribution.Simple.Utils (
readUTF8File,
withUTF8FileContents,
writeUTF8File,
normaliseLineEndings,

-- * generic utils
equating,
Expand Down Expand Up @@ -1033,6 +1034,13 @@ withUTF8FileContents name action =
writeUTF8File :: FilePath -> String -> IO ()
writeUTF8File path = writeFileAtomic path . toUTF8

-- | Fix different systems silly line ending conventions
normaliseLineEndings :: String -> String
normaliseLineEndings [] = []
normaliseLineEndings ('\r':'\n':s) = '\n' : normaliseLineEndings s -- windows
normaliseLineEndings ('\r':s) = '\n' : normaliseLineEndings s -- old osx
normaliseLineEndings ( c :s) = c : normaliseLineEndings s

-- ------------------------------------------------------------
-- * Common utils
-- ------------------------------------------------------------
Expand Down

0 comments on commit 4983da9

Please sign in to comment.