<commit_before>{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}

module Main where

import Data.Functor ((<$>))
import qualified Data.Map as M
import qualified Data.List as L
import Network.IPTables.Ruleset
import Network.IPTables.Parser
import Network.IPTables.IpassmtParser
import System.Environment (getArgs, getProgName)
import System.IO

import qualified Network.IPTables.Generated as Isabelle

putErrStrLn = hPutStrLn stderr

preprocessForSpoofingProtection = Isabelle.upper_closure . Isabelle.ctstate_assume_new

exampleCertSpoof ipassmt fuc = map (\ifce -> (ifce, Isabelle.no_spoofing_iface ifce ipassmtMap fuc)) interfaces
    where interfaces = map fst ipassmt
          ipassmtMap = Isabelle.map_of_ipassmt ipassmt

readIpAssmt filename = do
    src <- readFile filename
    case parseIpAssmt filename src of
        Left err -> do print err
                       error $ "could not parse " ++ filename
        Right res -> do putStrLn "Parsed IpAssmt"
                        putStrLn (show res)
                        return $ ipAssmtToIsabelle res


getIpAssmt = do
    args <- getArgs
    progName <- getProgName
    case length args of
      0 -> do putStrLn "no argument supplied"
              putStrLn "for the sake of example, I'm loading ipassmt_generic"
              putStrLn "Supply the ipassmt to get better results!"
              return Isabelle.ipassmt_generic
              --return Isabelle.example_TUM_i8_spoofing_ipassmt
      1 -> do putStrLn $ "loading ipassmt from `" ++ filename ++ "'"
              readIpAssmt filename
                  where filename = head args
      _ -> do putStrLn $ "Usage: " ++ progName ++ " ipassmt_file"
              error "too many command line parameters"


-- TODO: select table and chain

readArgs = getArgs >>= \case
    "-h" : _ ->
        return Nothing
    "-a" : assignmentFile : rest -> do
        assmt <- readIpAssmt assignmentFile
        input <- readInput rest
        return $ Just (assmt, input)
    ('-' : _) : _ -> error "ERROR: unknown argument, try `-h`"
    rest -> do
        putErrStrLn "WARNING: no IP assignment specified, loading a generic file"
        input <- readInput rest
        return $ Just (Isabelle.ipassmt_generic, input)
    where readInput [] = ("<stdin>",) <$> getContents
          readInput [file] = (file,) <$> readFile file
          readInput rest = error "ERROR: to many files"

usage :: IO ()
usage = do
    name <- getProgName
    putErrStrLn $ "Usage: " ++ name ++ " [-a FILE | -h] [FILE]"
    putErrStrLn "Parse `iptables-save` output from FILE or standard input"
    putErrStrLn ""
    putErrStrLn $ "  -a FILE   optional IP assignment file; if unspecified, a generic file is loaded"
    putErrStrLn $ "  -h        print this help text"

main :: IO ()
main = readArgs >>= \case
    Nothing ->
        usage
    Just (ipassmt, (srcname, src)) ->
        case parseIptablesSave srcname src of
            Left err -> print err<commit_msg>warn on windows line endings in haskell tool<commit_after>{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}

module Main where

import Data.Functor ((<$>))
import qualified Data.Map as M
import qualified Data.List as L
import Network.IPTables.Ruleset
import Network.IPTables.Parser
import Network.IPTables.IpassmtParser
import System.Environment (getArgs, getProgName)
import System.IO

import qualified Text.Parsec.Error --Windows line ending debug

import qualified Network.IPTables.Generated as Isabelle

putErrStrLn = hPutStrLn stderr

preprocessForSpoofingProtection = Isabelle.upper_closure . Isabelle.ctstate_assume_new

exampleCertSpoof ipassmt fuc = map (\ifce -> (ifce, Isabelle.no_spoofing_iface ifce ipassmtMap fuc)) interfaces
    where interfaces = map fst ipassmt
          ipassmtMap = Isabelle.map_of_ipassmt ipassmt

readIpAssmt filename = do
    src <- readFile filename
    case parseIpAssmt filename src of
        Left err -> do print err
                       error $ "could not parse " ++ filename
        Right res -> do putStrLn "Parsed IpAssmt"
                        putStrLn (show res)
                        return $ ipAssmtToIsabelle res


getIpAssmt = do
    args <- getArgs
    progName <- getProgName
    case length args of
      0 -> do putStrLn "no argument supplied"
              putStrLn "for the sake of example, I'm loading ipassmt_generic"
              putStrLn "Supply the ipassmt to get better results!"
              return Isabelle.ipassmt_generic
              --return Isabelle.example_TUM_i8_spoofing_ipassmt
      1 -> do putStrLn $ "loading ipassmt from `" ++ filename ++ "'"
              readIpAssmt filename
                  where filename = head args
      _ -> do putStrLn $ "Usage: " ++ progName ++ " ipassmt_file"
              error "too many command line parameters"


-- TODO: select table and chain

readArgs = getArgs >>= \case
    "-h" : _ ->
        return Nothing
    "-a" : assignmentFile : rest -> do
        assmt <- readIpAssmt assignmentFile
        input <- readInput rest
        return $ Just (assmt, input)
    ('-' : _) : _ -> error "ERROR: unknown argument, try `-h`"
    rest -> do
        putErrStrLn "WARNING: no IP assignment specified, loading a generic file"
        input <- readInput rest
        return $ Just (Isabelle.ipassmt_generic, input)
    where readInput [] = ("<stdin>",) <$> getContents
          readInput [file] = (file,) <$> readFile file
          readInput rest = error "ERROR: to many files"

usage :: IO ()
usage = do
    name <- getProgName
    putErrStrLn $ "Usage: " ++ name ++ " [-a FILE | -h] [FILE]"
    putErrStrLn "Parse `iptables-save` output from FILE or standard input"
    putErrStrLn ""
    putErrStrLn $ "  -a FILE   optional IP assignment file; if unspecified, a generic file is loaded"
    putErrStrLn $ "  -h        print this help text"

main :: IO ()
main = readArgs >>= \case
    Nothing ->
        usage
    Just (ipassmt, (srcname, src)) ->
        case parseIptablesSave srcname src of
            Left err -> do
                let errorMsgs = Text.Parsec.Error.errorMessages err
                if L.length errorMsgs >= 2
                then case (L.last (L.init errorMsgs), L.last errorMsgs) of
                    (Text.Parsec.Error.SysUnExpect "\"\\r\"", Text.Parsec.Error.Expect "\"\\n\"") -> putStrLn "Warning: Windows newlines not supported"
                    _ -> return ()
                else return ()
                print err
{'fraction_non_alphanumeric': 0.09131782945736434, 'fraction_numerical': 0.0010852713178294573, 'mean_word_length': 2.3845750262329486, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4185818', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1915, 'n_words': 613}