# lib9p/protogen/__init__.py - Generate C marshalers/unmarshalers for # .9p files defining 9P protocol variants # # Copyright (C) 2024-2025 Luke T. Shumaker # SPDX-License-Identifier: AGPL-3.0-or-later import os.path import sys import typing import idl from . import c, h # pylint: disable=unused-variable __all__ = ["main"] def main() -> None: if typing.TYPE_CHECKING: class ANSIColors: MAGENTA = "\x1b[35m" RED = "\x1b[31m" RESET = "\x1b[0m" else: from _colorize import ANSIColors # Present in Python 3.13+ if len(sys.argv) < 2: raise ValueError("requires at least 1 .9p filename") parser = idl.Parser() for txtname in sys.argv[1:]: try: parser.parse_file(txtname) except SyntaxError as e: print( f"{ANSIColors.RED}{e.filename}{ANSIColors.RESET}:{ANSIColors.MAGENTA}{e.lineno}{ANSIColors.RESET}: {e.msg}", file=sys.stderr, ) assert e.text print(f"\t{e.text}", file=sys.stderr) text_suffix = e.text.lstrip() text_prefix = e.text[: -len(text_suffix)] print( f"\t{text_prefix}{ANSIColors.RED}{'~'*len(text_suffix)}{ANSIColors.RESET}", file=sys.stderr, ) sys.exit(2) versions, typs = parser.all() outdir = os.path.normpath(os.path.join(sys.argv[0], "..")) with open( os.path.join(outdir, "include/lib9p/9p.generated.h"), "w", encoding="utf-8" ) as fh: fh.write(h.gen_h(versions, typs)) with open(os.path.join(outdir, "9p.generated.c"), "w", encoding="utf-8") as fh: fh.write(c.gen_c(versions, typs))