{- HLINT ignore "Avoid lambda" -}
module System.Proc.Bindings.Tab.Internal where

import Control.Exception
import Foreign
import GHC.Stack
import System.Proc.Bindings.C
import System.Proc.Bindings.C.Utils
import System.Proc.Bindings.Error
import System.Proc.Bindings.Tab.C
import System.Proc.Bindings.Tab.Config

{- | The proctab (process table) is a persistent handle-like data structure holding the information
the library needs to read process information. While 'System.Proc.Bindings.Proc's are
typically ephemeral, the 'ProcTab' is more long lived.

Typically it will be created and populated by 'System.Proc.Bindings.Tab.openProcTab'
(or 'System.Proc.Bindings.Tab.withProcTab', which handles memory management); the 'TableConfig'
tells the underlying library what information we want to read, but no actual process information
is read yet.

We then query the 'ProcTab' for information about processes, using functions like
'System.Proc.Bindings.readNextProc' or 'System.Proc.Bindings.readNextProcInfo'
-}
data ProcTab
  = UnsafeProcTab
      -- PROCTAB* pointer, guaranteed to be non-null.
      (Ptr ProcTabC)
      -- A reusable proc_t pointer to save time allocating and freeing memory.
      -- will be used as the second argument to the @readproc@ C function.
      (Ptr Proc)

{- | Open a @'Ptr' 'ProcTabC'@ according to a 'TableConfig'. It is the caller's responsibility
to check that the 'Ptr' is not null, and to free it once they're done with it.

It's safer to use 'withProcTabPtr', which will automatically clean up all the pointers.

The @IO ()@ return value is a cleanup function to free any memory that was allocated for
@openproc@, e.g an array of @uid_t@s or @pid_t@s.
-}
openProcTabPtr :: HasCallStack => TableConfig -> IO (Ptr ProcTabC, IO ())
openProcTabPtr :: HasCallStack => TableConfig -> IO (Ptr ProcTabC, IO ())
openProcTabPtr = forall a.
(CInt -> IO a)
-> (CInt -> Ptr CPid -> IO a)
-> (CInt -> Ptr CUid -> CInt -> IO a)
-> TableConfig
-> IO (a, IO ())
openTableConfig CInt -> IO (Ptr ProcTabC)
openProcTabSimple CInt -> Ptr CPid -> IO (Ptr ProcTabC)
openProcTabFromPids CInt -> Ptr CUid -> CInt -> IO (Ptr ProcTabC)
openProcTabFromUids

{- | Bracketed access to a @'Ptr' 'ProcTabC'@. The pointer will be freed after use.
Return a 'ProcError' if the pointer is null.
-}
withProcTabPtr :: HasCallStack => TableConfig -> (Ptr ProcTabC -> IO a) -> IO (Either ProcError a)
withProcTabPtr :: forall a.
HasCallStack =>
TableConfig -> (Ptr ProcTabC -> IO a) -> IO (Either ProcError a)
withProcTabPtr TableConfig
cfg Ptr ProcTabC -> IO a
f =
  forall a.
HasCallStack =>
(CInt -> IO a)
-> (CInt -> Ptr CPid -> IO a)
-> (CInt -> Ptr CUid -> CInt -> IO a)
-> TableConfig
-> IO a
branchTableConfig
    (\CInt
flags -> IO (Ptr ProcTabC) -> IO (Either ProcError a)
bracketProcTab (CInt -> IO (Ptr ProcTabC)
openProcTabSimple CInt
flags))
    (\CInt
flags Ptr CPid
pidPtr -> IO (Ptr ProcTabC) -> IO (Either ProcError a)
bracketProcTab (CInt -> Ptr CPid -> IO (Ptr ProcTabC)
openProcTabFromPids CInt
flags Ptr CPid
pidPtr))
    (\CInt
flags Ptr CUid
uidPtr CInt
nuid -> IO (Ptr ProcTabC) -> IO (Either ProcError a)
bracketProcTab (CInt -> Ptr CUid -> CInt -> IO (Ptr ProcTabC)
openProcTabFromUids CInt
flags Ptr CUid
uidPtr CInt
nuid))
    TableConfig
cfg
  where
    bracketProcTab :: IO (Ptr ProcTabC) -> IO (Either ProcError a)
bracketProcTab IO (Ptr ProcTabC)
open = forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO (Ptr ProcTabC)
open Ptr ProcTabC -> IO ()
closeProcTabC (forall a b.
HasCallStack =>
(Ptr a -> IO b) -> Ptr a -> IO (Either ProcError b)
eitherPeek Ptr ProcTabC -> IO a
f)