Guest User

Untitled

a guest
Jun 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. {-# LANGUAGE QuasiQuotes, StandaloneDeriving, DeriveDataTypeable #-}
  2.  
  3. import Atomo.Environment
  4. import Atomo.Haskell
  5.  
  6. import Data.Typeable
  7. import System.Exit
  8. import System.Process
  9.  
  10. deriving instance Typeable ProcessHandle
  11.  
  12.  
  13. load :: VM ()
  14. load = do
  15. ([$p|Command|] =::) =<< eval [$e|Object clone|]
  16.  
  17. [$p|Command run: (cmd: String)|] =: do
  18. cmd <- getString [$e|cmd|]
  19. ph <- liftIO (runCommand cmd)
  20. newCommand ph
  21.  
  22. [$p|Command run-interactively: (cmd: String)|] =: do
  23. cmd <- getString [$e|cmd|]
  24. (sin, sout, serr, h) <- liftIO (runInteractiveCommand cmd)
  25. newInteractiveCommand (sin, sout, serr) h
  26.  
  27. [$p|(c: Command) wait|] =:
  28. getPH [$e|c handle|]
  29. >>= liftIO . waitForProcess
  30. >>= return . toExit
  31.  
  32. [$p|(c: Command) exit-status|] =:
  33. getPH [$e|c handle|]
  34. >>= liftIO . getProcessExitCode
  35. >>= return . maybe
  36. (particle "running")
  37. (\ec -> keyParticleN ["done"] [toExit ec])
  38.  
  39. [$p|(c: Command) terminate|] =:
  40. getPH [$e|c handle|]
  41. >>= liftIO . terminateProcess
  42. >> return (particle "ok")
  43. where
  44. getPH e = eval e >>= fromHaskell "ProcessHandle"
  45.  
  46. toExit ExitSuccess = particle "success"
  47. toExit (ExitFailure i) =
  48. keyParticleN ["failure"] [Integer (fromIntegral i)]
  49.  
  50. newCommand h = do
  51. proc <- eval [$e|Command clone|]
  52. [$p|p|] =:: proc
  53. [$p|p handle|] =:: haskell h
  54. here "p"
  55.  
  56. newInteractiveCommand (sin, sout, serr) h = do
  57. proc <- eval [$e|Command clone|]
  58. [$p|p|] =:: proc
  59. [$p|p handle|] =:: haskell h
  60. ([$p|p standard-input|] =::) =<< portObj sin
  61. ([$p|p standard-output|] =::) =<< portObj sout
  62. ([$p|p standard-error|] =::) =<< portObj serr
  63. here "p"
  64.  
  65. portObj hdl = newScope $ do
  66. port <- eval [$e|Port clone|]
  67. [$p|p|] =:: port
  68. [$p|p handle|] =:: haskell hdl
  69. here "p"
Add Comment
Please, Sign In to add comment