module Main where import System.Environment (getArgs) import System.Exit (exitFailure, exitSuccess) import System.IO (hPutStrLn, stderr) import FWL.Parser (parseFile) import FWL.Pretty (prettyProgram) import FWL.Check (checkProgram) import FWL.Compile (compileToJson, compileProgram) 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 do 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 putStrLn (compileToJson prog) else do 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)