{-# LINE 1 "Monky/CUtil.hsc" #-}
{-
{-# LINE 2 "Monky/CUtil.hsc" #-}
    Copyright 2017 Markus Ongyerth

    This file is part of Monky.

    Monky is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Monky is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Monky.  If not, see <http://www.gnu.org/licenses/>.
-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-|
Module      : Monky.CUtil
Description : Provides C utility functions
Maintainer  : ongy
Stability   : testing
Portability : Linux

This provides low-level utility wrappers used by Monky.Util
-}
module Monky.CUtil
    ( UName(..)
    , uname
    )
where

import Data.Word (Word)
import Foreign.Ptr
import Foreign.C.Types

import Data.Text (Text)
import qualified Data.Text.Foreign as T

import Foreign.Storable
import Foreign.Marshal.Alloc

import Control.Applicative ((<$>), (<*>))


{-# LINE 49 "Monky/CUtil.hsc" #-}

foreign import ccall unsafe "strlen" c_strlen :: Ptr CChar -> IO Word
foreign import ccall unsafe "uname" c_uname :: Ptr UName -> IO ()

-- | The haskell type for Cs utsname (man uname)
data UName = UName
    { _uSysName  :: Text
    , _uNodeName :: Text
    , _uRelease  :: Text
    , _uVersion  :: Text
    , _uMachine  :: Text
    } deriving (Eq, Show)

peekCString :: Ptr CChar -> IO Text
peekCString ptr = do
    len <- c_strlen ptr
    T.peekCStringLen (ptr, fromIntegral len)

instance Storable UName where
    sizeOf _ = (390)
{-# LINE 69 "Monky/CUtil.hsc" #-}
    alignment _ = alignment (undefined :: CLong)
    peek p = UName
        <$> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 0) p)
{-# LINE 72 "Monky/CUtil.hsc" #-}
        <*> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 65) p)
{-# LINE 73 "Monky/CUtil.hsc" #-}
        <*> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 130) p)
{-# LINE 74 "Monky/CUtil.hsc" #-}
        <*> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 195) p)
{-# LINE 75 "Monky/CUtil.hsc" #-}
        <*> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 260) p)
{-# LINE 76 "Monky/CUtil.hsc" #-}
    poke _ _ = error "There is no reason we should EVER poke a struct utsname. Please check your code."

-- | Get a UName structure for the current kernel
uname :: IO UName
uname = alloca $ \ptr -> do
    c_uname ptr
    peek ptr