I have a program that depending on the arguments given works in different ways:
- If there are 2 arguments - it takes 2nd argument as a filename, reads from it and then simply prints it out.
- If there is 1 argument - it reads from stdin and also prints it out.
Here is the code:
main :: IO ()
main = do
-- Read given arguments
args <- getArgs
-- If file containing gramma was given
if length args == 2 then do
    hfile <- openFile (last args) ReadMode
    content <- hGetContents hfile
    let inGramma = getGramma content
    doJob (head args) inGramma
    hClose hfile
    return ()
-- If no file was given - reads from stdin
else if length args == 1 then do
    content <- getContents
    let inGramma = getGramma content
    doJob (head args) inGramma
    return ()
else do putStrLn "Invalid count of arguments!"
The problem is, when it reads from stdin, after every new line (enter pressed), it prints that line and than reads next. I need it to wait for the whole input and than print it out (after Ctrl+D).
Here are the functions used in that code:
-- | Structure of gramma
data GrammaStruct = Gramma
    { nonTerminals :: NonTerminals
    , terminals :: Terminals
    , start :: Start
    , rules :: Rules
    } deriving (Eq)
-- | Representation of gramma
instance Show GrammaStruct where
    show (Gramma n t s r) = 
                init (showSplit n) ++ 
        "\n" ++ init (showSplit t) ++ 
        "\n" ++ showStart s ++ 
        "\n" ++ init (showRules r)
-- | Print gramma
showGramma :: GrammaStruct -> IO ()
showGramma gr = do
    putStrLn $ show gr
-- | Transforms string given from file of stdin into gramma representation in app
getGramma :: String -> GrammaStruct
getGramma hIn = procLns (lines hIn)
-- | Depending on option given, provides required functionality
doJob :: String -> GrammaStruct -> IO ()
doJob op gramma
        | op == "-i" = showGramma gramma
Thank you.
