2021-08-30 17:31:37 +03:00
|
|
|
module System.File.Process
|
|
|
|
|
2021-10-29 18:38:32 +03:00
|
|
|
import public System.Escape
|
2021-08-30 17:31:37 +03:00
|
|
|
import public System.File.Error
|
|
|
|
import public System.File.Mode
|
|
|
|
import System.File.Support
|
|
|
|
import public System.File.Types
|
|
|
|
|
|
|
|
%foreign "C:fflush,libc 6"
|
|
|
|
prim__flush : FilePtr -> PrimIO Int
|
|
|
|
%foreign support "idris2_popen"
|
|
|
|
prim__popen : String -> String -> PrimIO FilePtr
|
|
|
|
%foreign support "idris2_pclose"
|
2021-11-03 18:10:43 +03:00
|
|
|
prim__pclose : FilePtr -> PrimIO Int
|
2021-08-30 17:31:37 +03:00
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| Force a write of all user-space buffered data for the given `File`.
|
|
|
|
|||
|
|
|
|
||| @ h the file handle to flush
|
2021-08-30 17:31:37 +03:00
|
|
|
export
|
|
|
|
fflush : HasIO io => (h : File) -> io ()
|
|
|
|
fflush (FHandle f)
|
|
|
|
= ignore $ primIO (prim__flush f)
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| Create a new unidirectional pipe by invoking the shell, which is passed the
|
|
|
|
||| given command-string using the '-c' flag, in a new process. The pipe is
|
|
|
|
||| opened with the given mode.
|
|
|
|
|||
|
|
|
|
||| @ cmd the command to pass to the shell
|
|
|
|
||| @ m the mode the pipe should have
|
2021-08-30 17:31:37 +03:00
|
|
|
export
|
2021-10-29 19:58:29 +03:00
|
|
|
popen : HasIO io => (cmd : String) -> (m : Mode) -> io (Either FileError File)
|
|
|
|
popen cmd m = do
|
|
|
|
ptr <- primIO (prim__popen cmd (modeStr m))
|
2021-08-30 17:31:37 +03:00
|
|
|
if prim__nullAnyPtr ptr /= 0
|
|
|
|
then returnError
|
|
|
|
else pure (Right (FHandle ptr))
|
|
|
|
|
2021-10-29 18:38:32 +03:00
|
|
|
namespace Escaped
|
|
|
|
export
|
|
|
|
popen : HasIO io => (cmd : List String) -> (m : Mode) -> io (Either FileError File)
|
|
|
|
popen = popen . escapeCmd
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| Wait for the process associated with the pipe to terminate.
|
|
|
|
|||
|
|
|
|
||| @ fh the file handle to the stream to close/wait on
|
2021-08-30 17:31:37 +03:00
|
|
|
export
|
2021-11-03 18:10:43 +03:00
|
|
|
pclose : HasIO io => (fh : File) -> io Int
|
2021-08-30 17:31:37 +03:00
|
|
|
pclose (FHandle h) = primIO (prim__pclose h)
|