summaryrefslogtreecommitdiffhomepage
path: root/debug-me.hs
blob: 69870f1ac87dab61bd648d42cefc0d5d7af67e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Main where

import Types
import Hash
import Pty

import Control.Concurrent
import Control.Concurrent.Async
import Control.Concurrent.STM
import System.IO
import System.Process
import System.Exit
import qualified Data.ByteString as B

main :: IO ()
main = do
	exitstatus <- go
	putStrLn ""
	putStrLn ">>> debug-me is exiting..."
	exitWith exitstatus
  where
	go = withChildPty $ \p -> do
		(Nothing, Nothing, Nothing, ph) <- createProcess $
			onPty p (proc "bash" [])
		let Master h = ptyMaster p
		hSetBuffering stdin NoBuffering
		hSetBuffering h NoBuffering
		ithread <- async (forward stdin h ph 200000)
		othread <- async (forward h stdout ph 0)
		exitstatus <- waitForProcess ph
		cancel ithread
		cancel othread
		return exitstatus

forward :: Handle -> Handle -> ProcessHandle -> Int -> IO ()
forward from to ph delay = do
	b <- B.hGetSome from 1024
	if b == B.empty
		then do
			terminateProcess ph
			return ()
		else do
			threadDelay delay
			B.hPut to b
			hFlush to
			forward from to ph delay