disassemble.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;; Disarchive
  2. ;;; Copyright © 2020-2022 Timothy Sample <samplet@ngyro.com>
  3. ;;;
  4. ;;; This file is part of Disarchive.
  5. ;;;
  6. ;;; Disarchive is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Disarchive is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Disarchive. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (disarchive scripts disassemble)
  19. #:use-module (disarchive)
  20. #:use-module (disarchive config)
  21. #:use-module (ice-9 getopt-long)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 pretty-print)
  24. #:export (disassemble-main))
  25. (define options-grammar
  26. '((name (single-char #\n) (value #t))
  27. (output (single-char #\o) (value #t))
  28. (help (single-char #\h))
  29. (version)))
  30. (define help-message "\
  31. Usage: disarchive disassemble [OPTION]... FILE
  32. Disassemble the archive FILE into its metadata specification.
  33. Options:
  34. -n, --name=NAME use NAME as the basename of the archive
  35. -o, --output=FILE write the specification to FILE
  36. -h, --help display this help and exit
  37. --version display version information and exit
  38. ")
  39. (define short-help-message "\
  40. Run 'disarchive disassmeble --help' for usage instructions.
  41. ")
  42. (define (getopt-long* . args)
  43. (catch 'quit
  44. (lambda ()
  45. (apply getopt-long args))
  46. (lambda (_ status)
  47. (display short-help-message (current-error-port))
  48. (exit status))))
  49. (define (disassemble-main . args)
  50. (%disarchive-log-port (current-error-port))
  51. (let ((options (getopt-long* args options-grammar)))
  52. (when (option-ref options 'help #f)
  53. (display help-message)
  54. (exit EXIT_SUCCESS))
  55. (when (option-ref options 'version #f)
  56. (display version-message)
  57. (exit EXIT_SUCCESS))
  58. (match (option-ref options '() '())
  59. ((filename)
  60. (let ((port (or (and=> (option-ref options 'output #f)
  61. open-output-file)
  62. (current-output-port))))
  63. (match (option-ref options 'name #f)
  64. (#f (pretty-print (disarchive-disassemble filename) port))
  65. (name (pretty-print (disarchive-disassemble filename
  66. #:name name)
  67. port))))
  68. (exit EXIT_SUCCESS))
  69. (_
  70. (with-output-to-port (current-error-port)
  71. (lambda ()
  72. (display "disarchive disassemble: No input file specified\n")
  73. (display short-help-message)
  74. (exit EXIT_FAILURE)))))))