xmlwf_helpgen.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #! /usr/bin/env python3
  2. # __ __ _
  3. # ___\ \/ /_ __ __ _| |_
  4. # / _ \\ /| '_ \ / _` | __|
  5. # | __// \| |_) | (_| | |_
  6. # \___/_/\_\ .__/ \__,_|\__|
  7. # |_| XML parser
  8. #
  9. # Copyright (c) 2019-2026 Sebastian Pipping <[email protected]>
  10. # Copyright (c) 2021 Tim Bray <[email protected]>
  11. # Licensed under the MIT license:
  12. #
  13. # Permission is hereby granted, free of charge, to any person obtaining
  14. # a copy of this software and associated documentation files (the
  15. # "Software"), to deal in the Software without restriction, including
  16. # without limitation the rights to use, copy, modify, merge, publish,
  17. # distribute, sublicense, and/or sell copies of the Software, and to permit
  18. # persons to whom the Software is furnished to do so, subject to the
  19. # following conditions:
  20. #
  21. # The above copyright notice and this permission notice shall be included
  22. # in all copies or substantial portions of the Software.
  23. #
  24. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  27. # NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  28. # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  30. # USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. import argparse
  32. from textwrap import dedent
  33. epilog = dedent(
  34. """
  35. environment variables:
  36. EXPAT_ACCOUNTING_DEBUG=(0|1|2|3)
  37. Control verbosity of accounting debugging (default: 0)
  38. EXPAT_ENTITY_DEBUG=(0|1)
  39. Control verbosity of entity debugging (default: 0)
  40. EXPAT_ENTROPY_DEBUG=(0|1)
  41. Control verbosity of entropy debugging (default: 0)
  42. EXPAT_MALLOC_DEBUG=(0|1|2)
  43. Control verbosity of allocation tracker (default: 0)
  44. exit status:
  45. 0 the input files are well-formed and the output (if requested) was written successfully
  46. 1 could not allocate data structures, signals a serious problem with execution environment
  47. 2 one or more input files were not well-formed
  48. 3 could not create an output file
  49. 4 command-line argument error
  50. xmlwf of libexpat is software libre, licensed under the MIT license.
  51. Please report bugs at https://github.com/libexpat/libexpat/issues -- thank you!
  52. """
  53. )
  54. usage = """
  55. %(prog)s [OPTIONS] [FILE ...]
  56. %(prog)s -h|--help
  57. %(prog)s -v|--version
  58. """
  59. parser = argparse.ArgumentParser(
  60. prog="xmlwf",
  61. add_help=False,
  62. usage=usage,
  63. description="xmlwf - Determines if an XML document is well-formed",
  64. formatter_class=argparse.RawTextHelpFormatter,
  65. epilog=epilog,
  66. )
  67. input_related = parser.add_argument_group("input control arguments")
  68. input_related.add_argument(
  69. "-s", action="store_true", help="print an error if the document is not [s]tandalone"
  70. )
  71. input_related.add_argument(
  72. "-n", action="store_true", help="enable [n]amespace processing"
  73. )
  74. input_related.add_argument(
  75. "-p",
  76. action="store_true",
  77. help="enable processing of external DTDs and [p]arameter entities",
  78. )
  79. input_related.add_argument(
  80. "-x",
  81. action="store_true",
  82. help=(
  83. "enable processing of e[x]ternal entities"
  84. "\n"
  85. "(CAREFUL! This makes xmlwf vulnerable to external entity attacks (XXE).)"
  86. ),
  87. )
  88. input_related.add_argument(
  89. "-e",
  90. action="store",
  91. metavar="ENCODING",
  92. help="override any in-document [e]ncoding declaration",
  93. )
  94. input_related.add_argument(
  95. "-w", action="store_true", help="enable support for [W]indows code pages"
  96. )
  97. input_related.add_argument(
  98. "-r",
  99. action="store_true",
  100. help="disable memory-mapping and use [r]ead calls instead",
  101. )
  102. input_related.add_argument(
  103. "-g",
  104. metavar="BYTES",
  105. help="buffer size to request per call pair to XML_[G]etBuffer and read (default: 8 KiB)",
  106. )
  107. input_related.add_argument(
  108. "-k",
  109. action="store_true",
  110. help="when processing multiple files, [k]eep processing after first file with error",
  111. )
  112. output_related = parser.add_argument_group("output control arguments")
  113. output_related.add_argument(
  114. "-d", action="store", metavar="DIRECTORY", help="output [d]estination directory"
  115. )
  116. output_mode = output_related.add_mutually_exclusive_group()
  117. output_mode.add_argument(
  118. "-c", action="store_true", help="write a [c]opy of input XML, not canonical XML"
  119. )
  120. output_mode.add_argument(
  121. "-m", action="store_true", help="write [m]eta XML, not canonical XML"
  122. )
  123. output_mode.add_argument(
  124. "-t", action="store_true", help="write no XML output for [t]iming of plain parsing"
  125. )
  126. output_related.add_argument(
  127. "-N", action="store_true", help="enable adding doctype and [n]otation declarations"
  128. )
  129. billion_laughs = parser.add_argument_group(
  130. "amplification attack protection (e.g. billion laughs)",
  131. description=(
  132. "NOTE: "
  133. "If you ever need to increase these values "
  134. "for non-attack payload, please file a bug report."
  135. ),
  136. )
  137. billion_laughs.add_argument(
  138. "-a",
  139. metavar="FACTOR",
  140. help="set maximum tolerated [a]mplification factor (default: 100.0)",
  141. )
  142. billion_laughs.add_argument(
  143. "-b",
  144. metavar="BYTES",
  145. help="set number of output [b]ytes needed to activate (default: 8 MiB/64 MiB)",
  146. )
  147. reparse_deferral = parser.add_argument_group("reparse deferral")
  148. reparse_deferral.add_argument(
  149. "-q",
  150. action="store_true",
  151. help="disable reparse deferral, and allow [q]uadratic parse runtime with large tokens",
  152. )
  153. parser.add_argument(
  154. "files", metavar="FILE", nargs="*", help="file to process (default: STDIN)"
  155. )
  156. info = parser.add_argument_group("info arguments")
  157. info = info.add_mutually_exclusive_group()
  158. info.add_argument(
  159. "-h", "--help", action="store_true", help="show this [h]elp message and exit"
  160. )
  161. info.add_argument(
  162. "-v",
  163. "--version",
  164. action="store_true",
  165. help="show program's [v]ersion number and exit",
  166. )
  167. if __name__ == "__main__":
  168. parser.print_help()