*** empty log message ***
[bpt/guile.git] / ice-9 / expect.scm
1 ;;;; Copyright (C) 1996, 1998, 1999, 2001 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
7 ;;;;
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
17 ;;;;
18 ;;;; As a special exception, the Free Software Foundation gives permission
19 ;;;; for additional uses of the text contained in its release of GUILE.
20 ;;;;
21 ;;;; The exception is that, if you link the GUILE library with other files
22 ;;;; to produce an executable, this does not by itself cause the
23 ;;;; resulting executable to be covered by the GNU General Public License.
24 ;;;; Your use of that executable is in no way restricted on account of
25 ;;;; linking the GUILE library code into it.
26 ;;;;
27 ;;;; This exception does not however invalidate any other reasons why
28 ;;;; the executable file might be covered by the GNU General Public License.
29 ;;;;
30 ;;;; This exception applies only to the code released by the
31 ;;;; Free Software Foundation under the name GUILE. If you copy
32 ;;;; code from other Free Software Foundation releases into a copy of
33 ;;;; GUILE, as the General Public License permits, the exception does
34 ;;;; not apply to the code that you add in this way. To avoid misleading
35 ;;;; anyone as to the status of such modified files, you must delete
36 ;;;; this exception notice from them.
37 ;;;;
38 ;;;; If you write modifications of your own for GUILE, it is your choice
39 ;;;; whether to permit this exception to apply to your modifications.
40 ;;;; If you do not wish that, delete this exception notice.
41 ;;;;
42 \f
43 ;;; Commentary:
44
45 ;; This module is documented in the Guile Reference Manual.
46 ;; Briefly, these are exported:
47 ;; procedures: expect-select, expect-regexec
48 ;; variables: expect-port, expect-timeout, expect-timeout-proc,
49 ;; expect-eof-proc, expect-char-proc,
50 ;; expect-strings-compile-flags, expect-strings-exec-flags,
51 ;; macros: expect
52
53 ;;; Code:
54
55 (define-module (ice-9 expect)
56 :use-module (ice-9 regex))
57
58 ;;; Expect: a macro for selecting actions based on what it reads from a port.
59 ;;; The idea is from Don Libes' expect based on Tcl.
60 ;;; This version by Gary Houston incorporating ideas from Aubrey Jaffer.
61 \f
62
63 (define-public expect-port #f)
64 (define-public expect-timeout #f)
65 (define-public expect-timeout-proc #f)
66 (define-public expect-eof-proc #f)
67 (define-public expect-char-proc #f)
68
69 ;;; expect: each test is a procedure which is applied to the accumulating
70 ;;; string.
71 (defmacro-public expect clauses
72 (let ((s (gensym))
73 (c (gensym))
74 (port (gensym))
75 (timeout (gensym)))
76 `(let ((,s "")
77 (,port (or expect-port (current-input-port)))
78 ;; when timeout occurs, in floating point seconds.
79 (,timeout (if expect-timeout
80 (let* ((secs-usecs (gettimeofday)))
81 (+ (car secs-usecs)
82 expect-timeout
83 (/ (cdr secs-usecs)
84 1000000))) ; one million.
85 #f)))
86 (let next-char ()
87 (if (and expect-timeout
88 (not (expect-select ,port ,timeout)))
89 (if expect-timeout-proc
90 (expect-timeout-proc ,s)
91 #f)
92 (let ((,c (read-char ,port)))
93 (if expect-char-proc
94 (expect-char-proc ,c))
95 (if (not (eof-object? ,c))
96 (set! ,s (string-append ,s (string ,c))))
97 (cond
98 ;; this expands to clauses where the car invokes the
99 ;; match proc and the cdr is the return value from expect
100 ;; if the proc matched.
101 ,@(let next-expr ((tests (map car clauses))
102 (exprs (map cdr clauses))
103 (body '()))
104 (cond
105 ((null? tests)
106 (reverse body))
107 (else
108 (next-expr
109 (cdr tests)
110 (cdr exprs)
111 (cons
112 `((,(car tests) ,s (eof-object? ,c))
113 ,@(cond ((null? (car exprs))
114 '())
115 ((eq? (caar exprs) '=>)
116 (if (not (= (length (car exprs))
117 2))
118 (scm-error 'misc-error
119 "expect"
120 "bad recipient: ~S"
121 (list (car exprs))
122 #f)
123 `((apply ,(cadar exprs)
124 (,(car tests) ,s ,port)))))
125 (else
126 (car exprs))))
127 body)))))
128 ;; if none of the clauses matched the current string.
129 (else (cond ((eof-object? ,c)
130 (if expect-eof-proc
131 (expect-eof-proc ,s)
132 #f))
133 (else
134 (next-char)))))))))))
135
136
137 (define-public expect-strings-compile-flags regexp/newline)
138 (define-public expect-strings-exec-flags regexp/noteol)
139
140 ;;; the regexec front-end to expect:
141 ;;; each test must evaluate to a regular expression.
142 (defmacro-public expect-strings clauses
143 `(let ,@(let next-test ((tests (map car clauses))
144 (exprs (map cdr clauses))
145 (defs '())
146 (body '()))
147 (cond ((null? tests)
148 (list (reverse defs) `(expect ,@(reverse body))))
149 (else
150 (let ((rxname (gensym)))
151 (next-test (cdr tests)
152 (cdr exprs)
153 (cons `(,rxname (make-regexp
154 ,(car tests)
155 expect-strings-compile-flags))
156 defs)
157 (cons `((lambda (s eof?)
158 (expect-regexec ,rxname s eof?))
159 ,@(car exprs))
160 body))))))))
161
162 ;;; simplified select: returns #t if input is waiting or #f if timed out or
163 ;;; select was interrupted by a signal.
164 ;;; timeout is an absolute time in floating point seconds.
165 (define-public (expect-select port timeout)
166 (let* ((secs-usecs (gettimeofday))
167 (relative (- timeout
168 (car secs-usecs)
169 (/ (cdr secs-usecs)
170 1000000)))) ; one million.
171 (and (> relative 0)
172 (pair? (car (select (list port) '() '()
173 relative))))))
174
175 ;;; match a string against a regexp, returning a list of strings (required
176 ;;; by the => syntax) or #f. called once each time a character is added
177 ;;; to s (eof? will be #f), and once when eof is reached (with eof? #t).
178 (define-public (expect-regexec rx s eof?)
179 ;; if expect-strings-exec-flags contains regexp/noteol,
180 ;; remove it for the eof test.
181 (let* ((flags (if (and eof?
182 (logand expect-strings-exec-flags regexp/noteol))
183 (logxor expect-strings-exec-flags regexp/noteol)
184 expect-strings-exec-flags))
185 (match (regexp-exec rx s 0 flags)))
186 (if match
187 (do ((i (- (match:count match) 1) (- i 1))
188 (result '() (cons (match:substring match i) result)))
189 ((< i 0) result))
190 #f)))
191
192 ;;; expect.scm ends here