1#!/usr/bin/env python3 2# ex: set filetype=python: 3 4"""Translate an XDR specification into executable code that 5can be compiled for the Linux kernel.""" 6 7__author__ = "Chuck Lever" 8__copyright__ = "Copyright (c) 2024 Oracle and/or its affiliates." 9__license__ = "GPL-2.0 only" 10__version__ = "0.2" 11 12import sys 13import argparse 14 15from subcmds import definitions 16from subcmds import declarations 17from subcmds import lint 18from subcmds import source 19 20 21sys.path.insert(1, "@pythondir@") 22 23 24def main() -> int: 25 """Parse command-line options""" 26 parser = argparse.ArgumentParser( 27 formatter_class=argparse.RawDescriptionHelpFormatter, 28 description="Convert an XDR specification to Linux kernel source code", 29 epilog="""\ 30Copyright (c) 2024 Oracle and/or its affiliates. 31 32License GPLv2: <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt> 33This is free software. You are free to change and redistribute it. 34There is NO WARRANTY, to the extent permitted by law.""", 35 ) 36 parser.add_argument( 37 "--version", 38 help="Display the version of this tool", 39 action="version", 40 version=__version__, 41 ) 42 43 subcommands = parser.add_subparsers(title="Subcommands", required=True) 44 45 definitions_parser = subcommands.add_parser( 46 "definitions", help="Generate XDR definitions" 47 ) 48 definitions_parser.add_argument( 49 "--annotate", 50 action="store_true", 51 default=False, 52 help="Add annotation comments", 53 ) 54 definitions_parser.add_argument( 55 "--language", 56 action="store_true", 57 default="C", 58 help="Output language", 59 ) 60 definitions_parser.add_argument( 61 "--peer", 62 choices=["server", "client",], 63 default="server", 64 help="Generate header code for client or server side", 65 type=str, 66 ) 67 definitions_parser.add_argument("filename", help="File containing an XDR specification") 68 definitions_parser.set_defaults(func=definitions.subcmd) 69 70 declarations_parser = subcommands.add_parser( 71 "declarations", help="Generate function declarations" 72 ) 73 declarations_parser.add_argument( 74 "--annotate", 75 action="store_true", 76 default=False, 77 help="Add annotation comments", 78 ) 79 declarations_parser.add_argument( 80 "--language", 81 action="store_true", 82 default="C", 83 help="Output language", 84 ) 85 declarations_parser.add_argument( 86 "--peer", 87 choices=["server", "client",], 88 default="server", 89 help="Generate code for client or server side", 90 type=str, 91 ) 92 declarations_parser.add_argument("filename", help="File containing an XDR specification") 93 declarations_parser.set_defaults(func=declarations.subcmd) 94 95 linter_parser = subcommands.add_parser("lint", help="Check an XDR specification") 96 linter_parser.add_argument("filename", help="File containing an XDR specification") 97 linter_parser.set_defaults(func=lint.subcmd) 98 99 source_parser = subcommands.add_parser( 100 "source", help="Generate XDR encoder and decoder source code" 101 ) 102 source_parser.add_argument( 103 "--annotate", 104 action="store_true", 105 default=False, 106 help="Add annotation comments", 107 ) 108 source_parser.add_argument( 109 "--language", 110 action="store_true", 111 default="C", 112 help="Output language", 113 ) 114 source_parser.add_argument( 115 "--peer", 116 choices=["server", "client",], 117 default="server", 118 help="Generate code for client or server side", 119 type=str, 120 ) 121 source_parser.add_argument("filename", help="File containing an XDR specification") 122 source_parser.set_defaults(func=source.subcmd) 123 124 args = parser.parse_args() 125 return args.func(args) 126 127 128try: 129 if __name__ == "__main__": 130 sys.exit(main()) 131except (SystemExit, KeyboardInterrupt, BrokenPipeError): 132 sys.exit(1) 133