decrypt.scm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/run/current-system/profile/bin/guile \
  2. -e main -s
  3. !#
  4. (add-to-load-path (dirname (current-filename)))
  5. (use-modules (ice-9 getopt-long)
  6. (ice-9 textual-ports) ;;open-output-file
  7. (encrypt))
  8. (define (discover-shift amount)
  9. (let ((amount (string->number amount)))
  10. (if (not amount) ;; Only execute if the amount is a number.
  11. #f
  12. (let ((amount (abs amount)))
  13. (if (>= amount 26)
  14. (set! shift (modulo amount 26))
  15. (set! shift amount))))))
  16. ;; loop through each character in the file, and output the encrypted file
  17. (define (decrypt)
  18. (let ((char (get-char (current-input-port))
  19. (shift (discover-shift))))
  20. (when (not (eof-object? char))
  21. (if (char-alphabetic? char)
  22. (put-char (current-output-port)
  23. (encrypt shift char))
  24. (put-char (current-output-port) char))
  25. (decrypt))))
  26. (define (main args)
  27. ;;the option specification tells getopt-long how
  28. ;; to parse the command line
  29. (let* ((option-spec `((version (single-char #\v) (value #f))
  30. (help (single-char #\h) (value #f))
  31. (file (single-char #\f) (value #t)
  32. (predicate ,file-exists?))
  33. (shift (single-char #\s) (value #t)
  34. (predicate ,set-shift))))
  35. (program-name "decrypt")
  36. ;; tell getopt-long to parse the command line and put
  37. ;; the data in options
  38. (options (getopt-long args option-spec))
  39. (help-wanted (option-ref options 'help #f))
  40. (version-wanted (option-ref options 'version #f))
  41. (file-wanted (option-ref options 'file #f))
  42. (shift-wanted (option-ref options 'shift #f)))
  43. (if (or help-wanted version-wanted file-wanted)
  44. (cond
  45. (version-wanted (print-version program-name))
  46. (help-wanted (print-help program-name))
  47. (file-wanted
  48. (with-input-from-file file-wanted decrypt))))))