@@ -20,12 +20,17 @@ import Control.Applicative
20import Control.Monad (forM)
20import Control.Monad (forM)
21import System.Exit (exitSuccess, exitFailure)
21import System.Exit (exitSuccess, exitFailure)
22import qualified System.IO.UTF8 as U
22import qualified System.IO.UTF8 as U
23+import Text.Parsec (ParseError)
25+readInput :: Maybe [FilePath] -> IO (Either ParseError [P.Declaration])
26+readInput Nothing = getContents >>= return . P.runIndentParser P.parseDeclarations
27+readInput (Just input) = fmap (fmap concat . sequence) $ forM input $ \inputFile -> do
28+ text <- U.readFile inputFile
29+ return $ P.runIndentParser P.parseDeclarations text
24−compile :: [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
31+compile :: Maybe [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
25compile input output externs = do
32compile input output externs = do
26− asts <- fmap (fmap concat . sequence) $ forM input $ \inputFile -> do
33+ asts <- readInput input
27− text <- U.readFile inputFile
28− return $ P.runIndentParser P.parseDeclarations text
29 case asts of
34 case asts of
30 Left err -> do
35 Left err -> do
31 U.print err
36 U.print err
@@ -44,8 +49,12 @@ compile input output externs = do
44 Just filePath -> U.writeFile filePath exts
49 Just filePath -> U.writeFile filePath exts
45 exitSuccess
50 exitSuccess
53+useStdIn = value . flag $ (optInfo [ "s", "stdin" ])
54+ { optDoc = "Read from standard input" }
47inputFiles :: Term [FilePath]
56inputFiles :: Term [FilePath]
48−inputFiles = nonEmpty $ posAny [] $ posInfo
57+inputFiles = value $ posAny [] $ posInfo
49 { posDoc = "The input .ps files" }
58 { posDoc = "The input .ps files" }
51outputFile :: Term (Maybe FilePath)
60outputFile :: Term (Maybe FilePath)
@@ -56,8 +65,14 @@ externsFile :: Term (Maybe FilePath)
56externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
65externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
57 { optDoc = "The output .e.ps file" }
66 { optDoc = "The output .e.ps file" }
68+stdInOrInputFiles :: Term (Maybe [FilePath])
69+stdInOrInputFiles = combine <$> useStdIn <*> inputFiles
71+ combine False input = Just input
72+ combine True _ = Nothing
59term :: Term (IO ())
74term :: Term (IO ())
60−term = compile <$> inputFiles <*> outputFile <*> externsFile
75+term = compile <$> stdInOrInputFiles <*> outputFile <*> externsFile
62termInfo :: TermInfo
77termInfo :: TermInfo
63termInfo = defTI
78termInfo = defTI