module Main where import System.Environment (getArgs) import System.Exit (exitFailure, exitSuccess) import System.IO (hPutStrLn, stderr) import qualified Data.ByteString.Lazy.Char8 as BL import FWL.Parser (parseFile) import FWL.Pretty (prettyProgram) import FWL.Check (checkProgram) import FWL.Compile (compileToJson) main :: IO () main = do args <- getArgs case args of ["check", fp] -> runCheck fp ["compile", fp] -> runCompile fp ["pretty", fp] -> runPretty fp _ -> do putStrLn "Usage: fwlc " putStrLn " check -- parse and static-check" putStrLn " compile -- emit nftables JSON to stdout" putStrLn " pretty -- parse and re-print" exitFailure runCheck :: FilePath -> IO () runCheck fp = do result <- parseFile fp case result of Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure Right prog -> do let errs = checkProgram prog if null errs then putStrLn "OK" >> exitSuccess else mapM_ (hPutStrLn stderr . show) errs >> exitFailure runCompile :: FilePath -> IO () runCompile fp = do result <- parseFile fp case result of Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure Right prog -> do let errs = checkProgram prog if null errs then BL.putStrLn (compileToJson prog) else mapM_ (hPutStrLn stderr . ("Check error: " ++) . show) errs >> exitFailure runPretty :: FilePath -> IO () runPretty fp = do result <- parseFile fp case result of Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure Right prog -> putStr (prettyProgram prog)