1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix scripts repl)
20 #:use-module (guix ui)
21 #:use-module (guix scripts)
22 #:use-module (guix utils)
23 #:use-module (guix packages)
24 #:use-module (gnu packages)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-37)
27 #:use-module (ice-9 match)
28 #:use-module (rnrs bytevectors)
29 #:autoload (system repl repl) (start-repl)
30 #:autoload (system repl server)
31 (make-tcp-server-socket make-unix-domain-server-socket)
32 #:export (machine-repl
37 ;;; This command provides a Guile REPL
39 (define %default-options
43 (list (option '(#\h "help") #f #f
47 (option '(#\V "version") #f #f
49 (show-version-and-exit "guix repl")))
50 (option '(#\t "type") #t #f
51 (lambda (opt name arg result)
52 (alist-cons 'type (string->symbol arg) result)))
53 (option '("listen") #t #f
54 (lambda (opt name arg result)
55 (alist-cons 'listen arg result)))))
59 (display (G_ "Usage: guix repl [OPTIONS...]
60 Start a Guile REPL in the Guix execution environment.\n"))
62 -t, --type=TYPE start a REPL of the given TYPE"))
65 -h, --help display this help and exit"))
67 -V, --version display version information and exit"))
69 (show-bug-report-information))
71 (define (self-quoting? x)
72 "Return #t if X is self-quoting."
73 (letrec-syntax ((one-of (syntax-rules ()
77 (one-of rest ...))))))
78 (one-of symbol? string? pair? null? vector?
79 bytevector? number? boolean?)))
82 ;; Module where we execute user code.
83 (let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
84 (beautify-user-module! module)
87 (define* (machine-repl #:optional
88 (input (current-input-port))
89 (output (current-output-port)))
90 "Run a machine-usable REPL over ports INPUT and OUTPUT.
92 The protocol of this REPL is meant to be machine-readable and provides proper
93 support to represent multiple-value returns, exceptions, objects that lack a
94 read syntax, and so on. As such it is more convenient and robust than parsing
96 (define (value->sexp value)
97 (if (self-quoting? value)
99 `(non-self-quoting ,(object-address value)
100 ,(object->string value))))
102 (write `(repl-version 0 0) output)
104 (force-output output)
112 (let ((results (call-with-values
115 (primitive-eval exp))
117 (write `(values ,@(map value->sexp results))
120 (force-output output)))
122 (write `(exception ,key ,@(map value->sexp args)))
124 (force-output output)))
127 (define (call-with-connection spec thunk)
128 "Dynamically-bind the current input and output ports according to SPEC and
133 ;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
134 ;; parse things like "fd:123" in a non-ambiguous way.
135 (match (string-index spec #\:)
137 (leave (G_ "~A: invalid listen specification~%") spec))
139 (let ((protocol (string-take spec index))
140 (address (string-drop spec (+ index 1))))
144 (make-tcp-server-socket #:port (string->number address)))
146 (make-unix-domain-server-socket #:path address))
148 (leave (G_ "~A: unsupported protocol family~%")
153 (match (accept socket)
154 ((connection . address)
155 (if (= AF_UNIX (sockaddr:fam address))
156 (info (G_ "accepted connection~%"))
157 (info (G_ "accepted connection from ~a~%")
158 (inet-ntop (sockaddr:fam address)
159 (sockaddr:addr address))))
163 (parameterize ((current-input-port connection)
164 (current-output-port connection))
167 (false-if-exception (close-port connection))
168 (info (G_ "connection closed~%"))))))
172 (define (guix-repl . args)
174 ;; Return the list of package names.
175 (args-fold* args %options
176 (lambda (opt name arg result)
177 (leave (G_ "~A: unrecognized option~%") name))
179 (leave (G_ "~A: extraneous argument~%") arg))
183 (let ((type (assoc-ref opts 'type)))
184 (call-with-connection (assoc-ref opts 'listen)
188 (save-module-excursion
190 (set-current-module user-module)
195 (leave (G_ "~a: unknown type of REPL~%") type))))))))
198 ;; eval: (put 'call-with-connection 'scheme-indent-function 1)