1 ;; A read-evaluate-print-loop for gpgscm.
3 ;; Copyright (C) 2016 g10 Code GmbH
5 ;; This file is part of GnuPG.
7 ;; GnuPG is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GnuPG is distributed in the hope that it will be useful,
13 ;; but 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.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, see <http://www.gnu.org/licenses/>.
20 ;; Interactive repl using 'prompt' function. P must be a function
21 ;; that given the current entered prefix returns the prompt to
23 (define (repl p environment)
26 (let loop ((prefix ""))
27 (let ((line (prompt (p prefix))))
28 (if (and (not (eof-object? line)) (= 0 (string-length line)))
30 (if (not (eof-object? line))
31 (let* ((next (string-append prefix line))
32 (c (catch (begin (echo "Parse error:" *error*)
34 (read (open-input-string next)))))
35 (if (not (eof-object? c))
38 (display (car *error*))
39 (when (and (cadr *error*)
40 (not (null? (cadr *error*))))
42 (write (cadr *error*)))
44 (vm-history-print (caddr *error*)))
45 (echo " ===>" (eval c environment)))
47 (exit (loop next)))))))))
49 (define (prompt-append-prefix prompt prefix)
50 (string-append prompt (if (> (string-length prefix) 0)
51 (string-append prefix "...")
54 ;; Default repl run by main.c.
55 (define (interactive-repl . environment)
56 (repl (lambda (p) (prompt-append-prefix "gpgscm " p))
57 (if (null? environment) (interaction-environment) (car environment))))