Skip to content

Instantly share code, notes, and snippets.

@victoredwardocallaghan
Last active December 26, 2015 09:49
Show Gist options
  • Save victoredwardocallaghan/7132010 to your computer and use it in GitHub Desktop.
Save victoredwardocallaghan/7132010 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module POSIX where
-- This attoparsec module is intended for parsing text that is
-- -- represented using an 8-bit character set, e.g. ASCII or ISO-8859-15.
--import Data.Attoparsec.Char8
import Data.Text
import Data.Attoparsec.Text
import Control.Applicative
--import qualified Data.ByteString as B
-- special
passwdFile :: FilePath
passwdFile = "/etc/passwd"
-------------------------
---------- TYPES --------
-------------------------
-- replace me with FilePath?
type Path = Text
-- | Type for /etc/passwd file
type Passwd = [PasswdEntry]
-- | Type for /etc/passwd file entries
data PasswdEntry = PasswdEntry {
user :: Text,
shadow :: Text,
uId :: Int,
gId :: Int,
comment :: Text,
home :: Text,
shell :: Text
} deriving Show
-------------------------
--------- PARSERS -------
-------------------------
passwdParser :: Parser Passwd
passwdParser = many $ passwdEntryParser <* endOfLine
passwdEntryParser :: Parser PasswdEntry
passwdEntryParser = do
u <- takeTill (inClass ":")
s <- takeTill (inClass ":")
uid <- decimal
char ':'
gid <- decimal
char ':'
c <- takeTill (inClass ":")
h <- takeTill (inClass ":")
sh <- takeTill (inClass ":")
return $ PasswdEntry u s uid gid c h sh
--
-- test.
main :: IO ()
main = print $ parseOnly passwdEntryParser "rpc:x:32:32:Rpcbind Daemon:/dev/null:/bin/false"
--main = B.readFile passwdFile >>= print . parseOnly passwdParser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment