ebuild-autogen/ebuild/bash-utils.scm
2025-07-25 17:22:19 +02:00

62 lines
2.4 KiB
Scheme

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; bash-utils.scm
;; Copyright (C) 2025 Cor Legemaat <cor@cor.za.net>
;;
;; This file is part of ebuild-autogen: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published by the
;; Free Software Foundation, version 3 of the License.
;;
;; ebuild-autogen is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
;; more details.
;;
;; You should have received a copy of the GNU General Public License along with
;; ebuild-autogen. If not, see <https://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (ebuild bash-utils)
#:use-module (rnrs io ports)
#:use-module (ice-9 expect)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim))
(define-public bash-source-vars
(lambda (source var-list)
""
(let* ((in-pipe (pipe))
(out-pipe (pipe O_NONBLOCK))
(pid (spawn "bash" '("bash")
#:input (car in-pipe)
#:output (cdr out-pipe)))
(answ '())
(expect-port (car out-pipe))
(expect-timeout 1))
;; Make line buffered.
(setvbuf (cdr in-pipe) 'line)
(setvbuf (cdr out-pipe) 'line)
;; Do the sourcing stuff.
(write-line source (cdr in-pipe))
;; Eliminate extra data to stdout from sourcing.
(write-line "echo \"Done sourcing!\"" (cdr in-pipe))
(expect ((lambda (s eof?)
(string=? s "Done sourcing!\n"))
(lambda () #t)))
;; Read the variables.
(set! answ
(map (lambda (var)
(write-line (string-append "echo \"${"
var
"}\"") (cdr in-pipe))
(cons var (get-line (car out-pipe))))
var-list))
;; Tel bash to exit.
(write-line "exit 0" (cdr in-pipe))
;; Close my side of the pipes
(close-port (car in-pipe))
(close-port (cdr out-pipe))
;; Close bash's side of the pipes.
(close-port (cdr in-pipe))
(close-port (car out-pipe))
;; Finish.
(waitpid pid)
answ)))