@@ -20,12 +20,17 @@ import Control.Applicative
2020import Control.Monad (forM)
2121import System.Exit (exitSuccess, exitFailure)
2222import qualified System.IO.UTF8 as U
23+import Text.Parsec (ParseError)
2324
24−compile :: [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
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
30+
31+compile :: Maybe [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
2532compile input output externs = do
26− asts <- fmap (fmap concat . sequence) $ forM input $ \inputFile -> do
27− text <- U.readFile inputFile
28− return $ P.runIndentParser P.parseDeclarations text
33+ asts <- readInput input
2934 case asts of
3035 Left err -> do
3136 U.print err
@@ -44,8 +49,12 @@ compile input output externs = do
4449 Just filePath -> U.writeFile filePath exts
4550 exitSuccess
4651
52+useStdIn :: Term Bool
53+useStdIn = value . flag $ (optInfo [ "s", "stdin" ])
54+ { optDoc = "Read from standard input" }
55+
4756inputFiles :: Term [FilePath]
48−inputFiles = nonEmpty $ posAny [] $ posInfo
57+inputFiles = value $ posAny [] $ posInfo
4958 { posDoc = "The input .ps files" }
5059
5160outputFile :: Term (Maybe FilePath)
@@ -56,8 +65,14 @@ externsFile :: Term (Maybe FilePath)
5665externsFile = value $ opt Nothing $ (optInfo [ "e", "externs" ])
5766 { optDoc = "The output .e.ps file" }
5867
68+stdInOrInputFiles :: Term (Maybe [FilePath])
69+stdInOrInputFiles = combine <$> useStdIn <*> inputFiles
70+ where
71+ combine False input = Just input
72+ combine True _ = Nothing
73+
5974term :: Term (IO ())
60−term = compile <$> inputFiles <*> outputFile <*> externsFile
75+term = compile <$> stdInOrInputFiles <*> outputFile <*> externsFile
6176
6277termInfo :: TermInfo
6378termInfo = defTI