aboutsummaryrefslogtreecommitdiff
path: root/src/main.lisp
blob: ce73d786ee12e44297a19ab90db4b220abbc3ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;;; main.lisp -- oneliners.cli entrypoint

(defpackage oneliners.cli
  (:use :cl))
(in-package :oneliners.cli)

;;; CLI OPTIONS

;; (opts:define-opts
;;   (:name ))


;;; UTILITIES

(defun parent-process-name ()
  "Prints the name of the parent process of the current process."
  (let ((ppidfile (format nil "/proc/~a/status" (osicat-posix:getppid))))
    (first (last 
            (ppcre:split "\\s" 
                         (with-open-file (input ppidfile)
                           (read-line input)))))))

(defmacro wait-until ((&key (timeout 1) (poll-every 0.01)) &body check)
  "Run CHECK every POLL-EVERY seconds until either TIMEOUT seconds
have passed or CHECK returns non-nil."
  (let ((clockvar (gensym))
        (var (gensym)))
    `(loop
       for ,clockvar from 0 by ,poll-every to ,timeout
       for ,var = (progn ,@check)
       when ,var
         return ,var
       do (sleep ,poll-every))))

(defun run-with-shell
    (command
     &key
       (shell-name (parent-process-name))
       (await-output-p 0.8)
       (output-stream *standard-output*))
  (let ((shell
          (uiop:launch-program shell-name :input :stream :output :stream)))
    (write-line command (uiop:process-info-input shell))
    (finish-output (uiop:process-info-input shell))
    (when await-output-p
      (wait-until (:timeout await-output-p :poll-every 0.005)
        (listen (uiop:process-info-output shell)))
      (loop while (listen (uiop:process-info-output shell))
            do (princ (read-line (uiop:process-info-output shell)) output-stream)
               (terpri output-stream)))))