From 7d78867f271a3b53487aba6e4498b18905186a62 Mon Sep 17 00:00:00 2001 From: Benjamin Lipp Date: Mon, 2 Feb 2026 16:20:04 +0100 Subject: [PATCH] feat: use m4 when parsing pcv files --- marzipan/src/__init__.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/marzipan/src/__init__.py b/marzipan/src/__init__.py index 220561a0..60ba1009 100644 --- a/marzipan/src/__init__.py +++ b/marzipan/src/__init__.py @@ -108,6 +108,14 @@ def run_proverif(file, log_file, extra_args=[]): exit(return_code) +def m4(input_file, output_file): + logger.debug(f"m4: {input_file}, {output_file}") + input_file_path = pkgs.pathlib.Path(input_file) + + params = ["m4", "-D", "ProVerif", pkgs.os.path.basename(input_file)] + return exc(params, stderr=pkgs.sys.stderr, cwd=input_file_path.parent, stdout=output_file) + + def cpp(file, cpp_prep): logger.debug(f"_cpp: {file}, {cpp_prep}") file_path = pkgs.pathlib.Path(file) @@ -302,11 +310,28 @@ def metaverif(repo_path, tmpdir, file): @main.command() @click.argument("file_path") def parse(file_path): - try: - parse_main(file_path) - except pkgs.lark.exceptions.UnexpectedCharacters as e: - logger.error(f"Error {type(e).__name__} parsing {file_path}: {e}") + if not pkgs.os.path.isfile(file_path): + logger.error(f"{file_path} is not a file or does not exist.") + exit(1) + + if file_path.lower().endswith(".pv"): + logger.info(f"Parsing ProVerif .pv file {file_path}") + try: + parse_main(file_path) + except pkgs.lark.exceptions.UnexpectedCharacters as e: + logger.error(f"Error {type(e).__name__} parsing {file_path}: {e}") + elif file_path.lower().endswith(".pcv"): + with pkgs.tempfile.NamedTemporaryFile(suffix=".pv", delete_on_close=False) as f: + m4(file_path, f) + try: + parse_main(f.name) + except pkgs.lark.exceptions.UnexpectedCharacters as e: + logger.error(f"Error {type(e).__name__} parsing {file_path}: {e}") + + else: + logger.error(f"Unsupported file extension for file {file_path}") + exit(2) if __name__ == "__main__": main()