Hash :
b0ab5bb5
Author :
Thomas de Grivel
Date :
2022-12-04T18:50:53
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
;; cl-unix-cybernetics
;; Copyright 2013-2022 Thomas de Grivel <thodg@kmx.io>
;;
;; Permission is hereby granted to use this software granted
;; the above copyright notice and this permission paragraph
;; are included in all copies and substantial portions of this
;; software.
;;
;; THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
;; PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
;; AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
;; THIS SOFTWARE.
(in-package :cl-unix-cybernetics)
(defvar *default-shell-command* "/bin/sh")
(defparameter *shell-signal-errors* nil)
(setf (debug-p :shell) t)
;; String functions
(defun read-string (stream)
(with-output-to-string (out)
(loop for c = (when (listen stream)
(read-char stream))
while c
do (write-char c out))))
(defun make-random-bytes (length)
(let ((seq (make-array length :element-type '(unsigned-byte 8))))
(with-open-file (r #P"/dev/random" :element-type '(unsigned-byte 8))
(read-sequence seq r))
seq))
(defun make-random-string (length)
(subseq (cl-base64:usb8-array-to-base64-string (make-random-bytes
(ceiling length 4/3))
:uri t)
0 length))
(defun debug-out (fmt &rest args)
(let ((out *debug-io*))
(apply #'format out fmt args)
(force-output out)))
;; Errors
(define-condition shell-error (error)
((command :type string
:initarg :command
:reader shell-error-command)
(status :type fixnum
:initarg :status
:reader shell-error-status)
(out :initarg :out
:reader shell-error-out)
(err :initarg :err
:reader shell-error-err))
(:report (lambda (e stream)
(with-slots (command status out err) e
(format stream "Shell command returned ~D
Command: ~S
Output: ~S
Error: ~S"
status command out err)))))
;; Shell
(defun sh-quote (&rest str)
(let ((str (str str)))
(declare (type string str))
(if (and (not (position #\Newline str))
(cl-ppcre:scan "^[-+/=.,:^_0-9A-Za-z]*$" str))
str
(str #\" (re-subst "([$`\\\\\"])" "\\\\\\1" str) #\"))))
(defun sh-parse-integer (string)
(when (< 0 (length string))
(parse-integer string :radix (cond ((char= #\0 (char string 0)) 8)
(:otherwise 10)))))
(defun ascii-set-graphics-mode (stream &rest modes)
(format stream "~C[~{~D~^;~}m" #\Esc modes))
(defun make-delimiter ()
(format nil "---- ~A~A~A "
(ascii-set-graphics-mode nil 0 0 34 34 40 40)
(make-random-string 16)
(ascii-set-graphics-mode nil 0 0 37 37 40 40)))
(defclass shell ()
((command :type (or string list)
:initarg :command
:reader shell-command)
(delimiter :type string
:reader shell-delimiter
:initform (make-delimiter))
(log-stream :initarg :log-stream
:initform t
:reader shell-log-stream)))
(defgeneric shell-pid (shell))
(defgeneric shell-new-delimiter (shell))
(defgeneric shell-in (data shell))
(defgeneric shell-out/line (shell))
(defgeneric shell-err (shell))
(defgeneric shell-err/line (shell))
(defgeneric shell-status (shell))
(defgeneric shell-close (shell))
(defgeneric shell-closed-p (shell))
(defgeneric shell-run-command (command shell))
(defgeneric shell-log (shell fmt &rest args))
(defgeneric shell-log-p (shell))
(defmethod shell-log-p ((shell shell))
(when (shell-log-stream shell)
t))
(defmethod shell-log ((shell shell) (fmt string) &rest args)
(let ((log (shell-log-stream shell)))
(when log
(format log "~&~D" (shell-pid shell))
(apply #'format log fmt args)
(force-output log))))
(defmethod shell-status ((shell shell))
(let* ((delim (the string (make-delimiter)))
(len (length delim))
(lines-head (cons (str #\Newline) nil))
(lines-tail lines-head))
(shell-in (format nil " ; echo \"~%~A $?\"~%" delim) shell)
(let* ((prev nil)
(status
(loop (let ((line (shell-out/line shell)))
(when (or (null line)
(and (< len (length line))
(string= delim line :end2 len)))
(unless (or (null prev) (string= "" prev))
(setf (cdr lines-tail) (cons prev nil)))
(return (when line
(when (debug-p :shell*)
(debug-out "$ "))
(parse-integer line :start len))))
(when prev
(when (debug-p :shell*)
(debug-out "~A~%" prev))
(setf (cdr lines-tail) (cons (str prev #\Newline) nil)
lines-tail (cdr lines-tail)))
(setf prev line))))
(out (cdr lines-head))
(err (shell-err/line shell)))
(when (shell-log-p shell)
(dolist (line out)
(shell-log shell "| ~A~%" line))
(dolist (line err)
(shell-log shell "# ~A~&" line))
(unless (and status (= 0 status))
(shell-log shell " ⇒ ~D~%" status)))
(values status out err))))
;; Run command
(defmethod shell-run-command ((command string) (shell shell))
(when (debug-p :shell)
(format t "~&~D╭ $ ~A~%" (shell-pid shell) command))
(shell-in command shell)
(shell-status shell))
;; High level interface
(defmacro with-shell ((shell &optional (command *default-shell-command*))
&body body)
(let ((g!shell (gensym "SHELL-")))
`(let ((,g!shell (make-shell ,command)))
(unwind-protect (let ((,shell ,g!shell)) ,@body)
(shell-close ,g!shell)))))
(defun shell-run (shell &rest command)
(let ((cmd (str command)))
(multiple-value-bind (status out err) (shell-run-command cmd shell)
(when *shell-signal-errors*
(assert (= 0 status) ()
'shell-error :command cmd :status status :out out :err err))
(values out status err))))