0 | module System.File.Process
  1 |
  2 | import public System.Escape
  3 | import public System.File.Error
  4 | import public System.File.Mode
  5 | import System.FFI
  6 | import System.File.Support
  7 | import public System.File.Types
  8 |
  9 | %foreign "C:fflush,libc 6"
 10 |          "node:lambda:()=>0"
 11 | prim__flush : FilePtr -> PrimIO Int
 12 | %foreign supportC "idris2_popen"
 13 |          supportNode "popen"
 14 | prim__popen : String -> String -> PrimIO FilePtr
 15 | -- Waiting for a child process can take arbitrarily long, so these use
 16 | -- `supportCSafe` to keep other threads running on Chez.
 17 | %foreign supportCSafe "idris2_pclose"
 18 |          supportC "idris2_pclose"
 19 |          supportNode "pclose"
 20 | prim__pclose : FilePtr -> PrimIO Int
 21 |
 22 | data Popen2Result : Type where [external]
 23 |
 24 | %foreign supportC "idris2_popen2"
 25 | prim__popen2 : String -> PrimIO (Ptr Popen2Result)
 26 |
 27 | %foreign supportCSafe "idris2_popen2WaitByPid"
 28 |          supportC "idris2_popen2WaitByPid"
 29 | covering -- significantly non-total
 30 | prim__popen2WaitByPid : Int -> PrimIO Int
 31 | %foreign supportCSafe "idris2_popen2WaitByHandler"
 32 |          supportC "idris2_popen2WaitByHandler"
 33 | covering -- significantly non-total
 34 | prim__popen2WaitByHandler : AnyPtr -> PrimIO Int
 35 |
 36 | %foreign supportC "idris2_popen2ChildPid"
 37 | prim__popen2ChildPid : Ptr Popen2Result -> PrimIO Int
 38 |
 39 | %foreign supportC "idris2_popen2ChildHandler"
 40 | prim__popen2ChildHandler : Ptr Popen2Result -> PrimIO AnyPtr
 41 |
 42 | %foreign supportC "idris2_popen2FileIn"
 43 | prim__popen2FileIn : Ptr Popen2Result -> PrimIO FilePtr
 44 |
 45 | %foreign supportC "idris2_popen2FileOut"
 46 | prim__popen2FileOut : Ptr Popen2Result -> PrimIO FilePtr
 47 |
 48 | ||| Force a write of all user-space buffered data for the given `File`.
 49 | |||
 50 | ||| @ h the file handle to flush
 51 | export
 52 | fflush : HasIO io => (h : File) -> io ()
 53 | fflush (FHandle f)
 54 |     = ignore $ primIO (prim__flush f)
 55 |
 56 | ||| Create a new unidirectional pipe by invoking the shell, which is passed the
 57 | ||| given command-string using the '-c' flag, in a new process. The pipe is
 58 | ||| opened with the given mode.
 59 | |||
 60 | ||| IMPORTANT: The NodeJS backend only currently supports the Read mode. Also with
 61 | |||            the NodeJS backend, the opened process will finish execution before
 62 | |||            popen returns (it blocks on open) which is different than other
 63 | |||            backends which will block on close.
 64 | |||
 65 | ||| @ cmd the command to pass to the shell
 66 | ||| @ m   the mode the pipe should have
 67 | export
 68 | popen : HasIO io => (cmd : String) -> (m : Mode) -> io (Either FileError File)
 69 | popen cmd m = do
 70 |     ptr <- primIO (prim__popen cmd (modeStr m))
 71 |     if prim__nullAnyPtr ptr /= 0
 72 |         then returnError
 73 |         else pure (Right (FHandle ptr))
 74 |
 75 |
 76 | ||| Wait for the process associated with the pipe to terminate.
 77 | |||
 78 | ||| @ fh the file handle to the stream to close/wait on
 79 | export
 80 | pclose : HasIO io => (fh : File) -> io Int
 81 | pclose (FHandle h) = primIO (prim__pclose h)
 82 |
 83 | ||| Result of a popen2 command, containing the
 84 | public export
 85 | record SubProcess where
 86 |   constructor MkSubProcess
 87 |   ||| Process id of the spawned process
 88 |   pid : Int
 89 |   ||| The way to manipulate the spawned process, for systems where pid is not enough for this
 90 |   handler : AnyPtr
 91 |   ||| The input stream of the spawned process
 92 |   input : File
 93 |   ||| The output stream of the spawned process
 94 |   output : File
 95 |
 96 | ||| Create a new bidirectional pipe by invoking the shell, which is passed the
 97 | ||| given command-string using the '-c' flag, in a new process. On success
 98 | ||| a SubProcess is returned which holds the process id and file handles
 99 | ||| for input and output.
100 | ||| You should call `popen2Wait` after you've done with the child process
101 | ||| in order to clean up all system resources.
102 | |||
103 | ||| IMPORTANT: You may deadlock if write to a process which is waiting to flush
104 | |||            its output.  It is recommended to read and write from separate threads.
105 | |||
106 | ||| This function is not supported on node at this time.
107 | export
108 | popen2 : HasIO io => (cmd : String) -> io (Either FileError SubProcess)
109 | popen2 cmd = do
110 |   ptr <- primIO (prim__popen2 cmd)
111 |   if prim__nullPtr ptr /= 0
112 |     then returnError
113 |     else do
114 |       pid <- primIO (prim__popen2ChildPid ptr)
115 |       handle <- primIO (prim__popen2ChildHandler ptr)
116 |       input <- primIO (prim__popen2FileIn ptr)
117 |       output <- primIO (prim__popen2FileOut ptr)
118 |       free (prim__forgetPtr ptr)
119 |       pure $ Right (MkSubProcess pid handle (FHandle input) (FHandle output))
120 |
121 | ||| Blocks and waits until the process created by `popen2` finished
122 | |||
123 | ||| This function relates to `popen2` like `pclose` relates to `popen`.
124 | ||| Returns exit code of the process being waited.
125 | ||| IMPORTANT: this function mustn't be called twice for the same argument.
126 | |||
127 | ||| Support of this function in the backends must be the same as for `popen2`.
128 | export
129 | covering -- significantly non-total
130 | popen2Wait : HasIO io => SubProcess -> io Int
131 | popen2Wait sp = primIO $
132 |   if prim__nullAnyPtr sp.handler /= 0
133 |     then prim__popen2WaitByPid sp.pid
134 |     else prim__popen2WaitByHandler sp.handler
135 |
136 | namespace Escaped
137 |   export
138 |   popen : HasIO io => (cmd : List String) -> (m : Mode) -> io (Either FileError File)
139 |   popen = popen . escapeCmd
140 |
141 |   export
142 |   popen2 : HasIO io => (cmd : List String) -> io (Either FileError SubProcess)
143 |   popen2 = popen2 . escapeCmd
144 |