spell.lisp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ;;#!/usr/bin/sbcl --script
  2. (defvar *randomness* (make-random-state t))
  3. (defun split-string (string delimiter)
  4. (loop with len = (length string)
  5. with start = 0
  6. for end = (position delimiter string :start start)
  7. for substr = (if end
  8. (subseq string start end)
  9. (subseq string start))
  10. while end
  11. append (list substr)
  12. do (setf start (1+ end))
  13. until (= end len)))
  14. (defun list-files-in-directory (directory-path)
  15. (let* ((files (directory (merge-pathnames "*.*" (pathname directory-path))))
  16. (directory-pathname (pathname directory-path)))
  17. (mapcar #'(lambda (file)
  18. (merge-pathnames (pathname file) directory-pathname))
  19. files)))
  20. (defun bins-list ()
  21. (apply #'append
  22. (mapcar #'list-files-in-directory
  23. (loop for path in (split-string (sb-ext:posix-getenv "PATH") #\:)
  24. collect (format NIL "~a~c" path #\/)))))
  25. (defun random-bin (bins-list)
  26. (namestring (nth (random (length bins-list) *randomness*) bins-list)))
  27. (defun random-cmd (bins-list)
  28. (let ((number-of-pipes (random 4 *randomness*))
  29. (cmd-str ""))
  30. (if (> number-of-pipes 0)
  31. (progn
  32. (dotimes (i (+ 1 number-of-pipes))
  33. (setq cmd-str (concatenate 'string cmd-str (format NIL "~A | " (random-bin bins-list)))))
  34. (setq cmd-str (concatenate 'string (subseq cmd-str 0 (- (length cmd-str) 3)) (format NIL "~%"))))
  35. (setq cmd-str (concatenate 'string cmd-str (format NIL "~A~%" (random-bin bins-list)))))
  36. cmd-str))
  37. (format T "~a" (random-cmd (bins-list)))