chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / tests / gpgscm / repl.scm
1 ;; A read-evaluate-print-loop for gpgscm.
2 ;;
3 ;; Copyright (C) 2016 g10 Code GmbH
4 ;;
5 ;; This file is part of GnuPG.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19
20 ;; Interactive repl using 'prompt' function.  P must be a function
21 ;; that given the current entered prefix returns the prompt to
22 ;; display.
23 (define (repl p environment)
24   (call/cc
25    (lambda (exit)
26      (let loop ((prefix ""))
27        (let ((line (prompt (p prefix))))
28          (if (and (not (eof-object? line)) (= 0 (string-length line)))
29              (exit (loop prefix)))
30          (if (not (eof-object? line))
31              (let* ((next (string-append prefix line))
32                     (c (catch (begin (echo "Parse error:" *error*)
33                                      (loop prefix))
34                               (read (open-input-string next)))))
35                (if (not (eof-object? c))
36                    (begin
37                      (catch (begin
38                               (display (car *error*))
39                               (when (and (cadr *error*)
40                                          (not (null? (cadr *error*))))
41                                     (display ": ")
42                                     (write (cadr *error*)))
43                               (newline)
44                               (vm-history-print (caddr *error*)))
45                             (echo "    ===>" (eval c environment)))
46                      (exit (loop ""))))
47                (exit (loop next)))))))))
48
49 (define (prompt-append-prefix prompt prefix)
50   (string-append prompt (if (> (string-length prefix) 0)
51                             (string-append prefix "...")
52                             "> ")))
53
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))))