spell.lisp 1.5 KB

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