1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- ;;#!/usr/bin/sbcl --script
- (defvar *randomness* (make-random-state t))
- (defun split-string (string delimiter)
- (loop with len = (length string)
- with start = 0
- for end = (position delimiter string :start start)
- for substr = (if end
- (subseq string start end)
- (subseq string start))
- while end
- append (list substr)
- do (setf start (1+ end))
- until (= end len)))
- (defun list-files-in-directory (directory-path)
- (let* ((files (directory (merge-pathnames "*.*" (pathname directory-path))))
- (directory-pathname (pathname directory-path)))
- (mapcar #'(lambda (file)
- (merge-pathnames (pathname file) directory-pathname))
- files)))
- (defun bins-list ()
- (apply #'append
- (mapcar #'list-files-in-directory
- (loop for path in (split-string (sb-ext:posix-getenv "PATH") #\:)
- collect (format NIL "~a~c" path #\/)))))
- (defun random-bin (bins-list)
- (namestring (nth (random (length bins-list) *randomness*) bins-list)))
- (defun random-cmd (bins-list)
- (let ((number-of-pipes (random 4 *randomness*))
- (cmd-str ""))
- (if (> number-of-pipes 0)
- (progn
- (dotimes (i (+ 1 number-of-pipes))
- (setq cmd-str (concatenate 'string cmd-str (format NIL "~A | " (random-bin bins-list)))))
- (setq cmd-str (concatenate 'string (subseq cmd-str 0 (- (length cmd-str) 3)) (format NIL "~%"))))
- (setq cmd-str (concatenate 'string cmd-str (format NIL "~A~%" (random-bin bins-list)))))
- cmd-str))
-
- (format T "~a" (random-cmd (bins-list)))
|