Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/HASKELL/Tac1.hs.html
--   http://angg.twu.net/HASKELL/Tac1.hs
--           (find-angg "HASKELL/Tac1.hs")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- Pipe a string through "tac".
-- "main0" was based on this,
--   https://lotz84.github.io/haskellbyexample/ex/spawning-processes
-- but with this fix:
--  (find-fline "~/LOGS/2022oct08.haskell" "<probie> after `hPutStr`")
--
-- See: (find-es "haskell" "pipeThrough")
--      (find-es "haskell" "System.Process")
--      (find-fline "~/LOGS/2022oct08.haskell" "that I should close its stdout too")
--
-- (defun e () (interactive) (find-angg "HASKELL/Tac1.hs"))

import System.IO
import System.Process

main0 = do
    (_, Just hout, _, _) <-
      createProcess (proc "date" []) { std_out = CreatePipe }
    dateOut <- hGetContents hout
    putStrLn "> date"
    putStrLn dateOut

    (Just hin, Just hout, _, _) <-
      createProcess (proc "grep" ["hello"]) { std_in = CreatePipe, std_out = CreatePipe }
    hPutStr hin "hello grep\ngoodbye grep"
    hClose hin
    grepBytes <- hGetContents hout
    putStrLn "> grep hello"
    putStrLn grepBytes

    (_, Just hout, _, _) <-
      createProcess (proc "bash" ["-c", "ls -a -l -h"]){ std_out = CreatePipe }
    lsOut <- hGetContents hout
    putStrLn "> ls -a -l -h"
    putStrLn lsOut

main1 = do
    (Just hin, Just hout, _, _) <-
      createProcess (proc "tac" []) { std_in = CreatePipe, std_out = CreatePipe }
    hPutStr hin "a\nbb\nccc\ndddd\n"
    hClose hin
    strout <- hGetContents hout
    putStrLn strout

pipeThrough p str_in = do
    (Just hin, Just hout, _, _) <-
      createProcess p { std_in = CreatePipe, std_out = CreatePipe }
    hPutStr hin str_in
    hClose hin
    hGetContents hout

main2 = do
  strout <- pipeThrough (proc "tac" []) "a\nbb\nccc\ndddd\n"
  putStr strout

-- main = main0
-- main = main1
main = main2


{-
* (eepitch-ghci)
* (eepitch-kill)
* (eepitch-ghci)
:load Tac1.hs
:main
:t (proc "tac" [])
pipeThrough (proc "tac" []) "a\nbb\nccc\ndddd\n"

* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
runghc Tac1.hs

-}





-- Local Variables:
-- coding:  utf-8-unix
-- End: