ff1f2088940aaf765400035ea44610a30026c956
[jackhill/guix/guix.git] / guix / scripts / repl.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix scripts repl)
21 #:use-module (guix ui)
22 #:use-module (guix scripts)
23 #:use-module (guix repl)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-37)
26 #:use-module (ice-9 match)
27 #:use-module (rnrs bytevectors)
28 #:autoload (system repl repl) (start-repl)
29 #:autoload (system repl server)
30 (make-tcp-server-socket make-unix-domain-server-socket)
31 #:export (guix-repl))
32
33 ;;; Commentary:
34 ;;;
35 ;;; This command provides a Guile REPL
36
37 (define %default-options
38 `((type . guile)))
39
40 (define %options
41 (list (option '(#\h "help") #f #f
42 (lambda args
43 (show-help)
44 (exit 0)))
45 (option '(#\V "version") #f #f
46 (lambda args
47 (show-version-and-exit "guix repl")))
48 (option '(#\t "type") #t #f
49 (lambda (opt name arg result)
50 (alist-cons 'type (string->symbol arg) result)))
51 (option '("listen") #t #f
52 (lambda (opt name arg result)
53 (alist-cons 'listen arg result)))
54 (option '(#\q) #f #f
55 (lambda (opt name arg result)
56 (alist-cons 'ignore-dot-guile? #t result)))
57 (option '(#\L "load-path") #t #f
58 (lambda (opt name arg result)
59 ;; XXX: Imperatively modify the search paths.
60 (set! %load-path (cons arg %load-path))
61 (set! %load-compiled-path (cons arg %load-compiled-path))
62 result))))
63
64
65 (define (show-help)
66 (display (G_ "Usage: guix repl [OPTIONS...]
67 Start a Guile REPL in the Guix execution environment.\n"))
68 (display (G_ "
69 -t, --type=TYPE start a REPL of the given TYPE"))
70 (display (G_ "
71 --listen=ENDPOINT listen to ENDPOINT instead of standard input"))
72 (display (G_ "
73 -q inhibit loading of ~/.guile"))
74 (newline)
75 (display (G_ "
76 -L, --load-path=DIR prepend DIR to the package module search path"))
77 (newline)
78 (display (G_ "
79 -h, --help display this help and exit"))
80 (display (G_ "
81 -V, --version display version information and exit"))
82 (newline)
83 (show-bug-report-information))
84
85 (define user-module
86 ;; Module where we execute user code.
87 (let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
88 (beautify-user-module! module)
89 module))
90
91 (define (call-with-connection spec thunk)
92 "Dynamically-bind the current input and output ports according to SPEC and
93 call THUNK."
94 (if (not spec)
95 (thunk)
96
97 ;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
98 ;; parse things like "fd:123" in a non-ambiguous way.
99 (match (string-index spec #\:)
100 (#f
101 (leave (G_ "~A: invalid listen specification~%") spec))
102 (index
103 (let ((protocol (string-take spec index))
104 (address (string-drop spec (+ index 1))))
105 (define socket
106 (match protocol
107 ("tcp"
108 (make-tcp-server-socket #:port (string->number address)))
109 ("unix"
110 (make-unix-domain-server-socket #:path address))
111 (_
112 (leave (G_ "~A: unsupported protocol family~%")
113 protocol))))
114
115 (listen socket 10)
116 (let loop ()
117 (match (accept socket)
118 ((connection . address)
119 (if (= AF_UNIX (sockaddr:fam address))
120 (info (G_ "accepted connection~%"))
121 (info (G_ "accepted connection from ~a~%")
122 (inet-ntop (sockaddr:fam address)
123 (sockaddr:addr address))))
124 (dynamic-wind
125 (const #t)
126 (lambda ()
127 (parameterize ((current-input-port connection)
128 (current-output-port connection))
129 (thunk)))
130 (lambda ()
131 (false-if-exception (close-port connection))
132 (info (G_ "connection closed~%"))))))
133 (loop)))))))
134
135 \f
136 (define (guix-repl . args)
137 (define opts
138 ;; Return the list of package names.
139 (args-fold* args %options
140 (lambda (opt name arg result)
141 (leave (G_ "~A: unrecognized option~%") name))
142 (lambda (arg result)
143 (leave (G_ "~A: extraneous argument~%") arg))
144 %default-options))
145
146 (define user-config
147 (and=> (getenv "HOME")
148 (lambda (home)
149 (string-append home "/.guile"))))
150
151 (with-error-handling
152 (let ((type (assoc-ref opts 'type)))
153 (call-with-connection (assoc-ref opts 'listen)
154 (lambda ()
155 (case type
156 ((guile)
157 (save-module-excursion
158 (lambda ()
159 (set-current-module user-module)
160 (when (and (not (assoc-ref opts 'ignore-dot-guile?))
161 user-config
162 (file-exists? user-config))
163 (load user-config))
164
165 ;; Do not exit repl on SIGINT.
166 ((@@ (ice-9 top-repl) call-with-sigint)
167 (lambda ()
168 (start-repl))))))
169 ((machine)
170 (machine-repl))
171 (else
172 (leave (G_ "~a: unknown type of REPL~%") type))))))))
173
174 ;; Local Variables:
175 ;; eval: (put 'call-with-connection 'scheme-indent-function 1)
176 ;; End: