blob: 5f417e44ac42f5f215b3358db96507238aa7ac22 (
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
|
;;;; running.lisp -- functions for running oneliners
(in-package :oneliners.cli.running)
(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)
finally (return nil))))
(defun run-with-shell
(command
&key
shell-name
await-output-p
(output-stream *standard-output*))
"run COMMAND, a string, in a fresh shell environment, initialized
with SHELL-NAME. The output from the command read line by line and is
printed to OUTPUT-STREAM. "
(let ((shell
(uiop:launch-program shell-name :input :stream :output :stream)))
(symbol-macrolet ((shell-input (uiop:process-info-input shell))
(shell-output (uiop:process-info-output shell)))
(write-line command shell-input)
(finish-output shell-input)
(if (and await-output-p
(plusp await-output-p)
(wait-until (:timeout await-output-p :poll-every 0.005)
(listen shell-output)))
(loop while (listen shell-output)
do (princ (read-line shell-output) output-stream)
(terpri output-stream)
(sleep 0.005))))))
|