(process-define-module): Do not call
[bpt/guile.git] / ice-9 / expect.scm
CommitLineData
2b1621ac 1;;;; Copyright (C) 1996, 1998, 1999, 2001 Free Software Foundation, Inc.
f32e992f 2;;;;
d7189b49
GH
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.
f32e992f 7;;;;
d7189b49
GH
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.
f32e992f 12;;;;
d7189b49
GH
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
15328041
JB
15;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16;;;; Boston, MA 02111-1307 USA
f32e992f 17;;;;
d7189b49 18\f
f32e992f 19;;; Commentary:
d7189b49 20
f32e992f
TTN
21;; This module is documented in the Guile Reference Manual.
22;; Briefly, these are exported:
2b1621ac 23;; procedures: expect-select, expect-regexec
f32e992f
TTN
24;; variables: expect-port, expect-timeout, expect-timeout-proc,
25;; expect-eof-proc, expect-char-proc,
26;; expect-strings-compile-flags, expect-strings-exec-flags,
27;; macros: expect
28
29;;; Code:
30
31(define-module (ice-9 expect)
32 :use-module (ice-9 regex))
ec8469e7 33
d7189b49
GH
34;;; Expect: a macro for selecting actions based on what it reads from a port.
35;;; The idea is from Don Libes' expect based on Tcl.
36;;; This version by Gary Houston incorporating ideas from Aubrey Jaffer.
37\f
38
ec8469e7
JB
39(define-public expect-port #f)
40(define-public expect-timeout #f)
41(define-public expect-timeout-proc #f)
42(define-public expect-eof-proc #f)
43(define-public expect-char-proc #f)
d7189b49
GH
44
45;;; expect: each test is a procedure which is applied to the accumulating
46;;; string.
ec8469e7 47(defmacro-public expect clauses
d7189b49
GH
48 (let ((s (gentemp))
49 (c (gentemp))
50 (port (gentemp))
51 (timeout (gentemp)))
52 `(let ((,s "")
53 (,port (or expect-port (current-input-port)))
cafa4c68 54 ;; when timeout occurs, in floating point seconds.
d7189b49 55 (,timeout (if expect-timeout
cafa4c68
GH
56 (let* ((secs-usecs (gettimeofday)))
57 (+ (car secs-usecs)
58 expect-timeout
59 (/ (cdr secs-usecs)
60 1000000))) ; one million.
d7189b49
GH
61 #f)))
62 (let next-char ()
63 (if (and expect-timeout
28d77376 64 (not (expect-select ,port ,timeout)))
d7189b49
GH
65 (if expect-timeout-proc
66 (expect-timeout-proc ,s)
67 #f)
68 (let ((,c (read-char ,port)))
69 (if expect-char-proc
70 (expect-char-proc ,c))
8f59c14e
JB
71 (if (not (eof-object? ,c))
72 (set! ,s (string-append ,s (string ,c))))
73 (cond
28d77376
GH
74 ;; this expands to clauses where the car invokes the
75 ;; match proc and the cdr is the return value from expect
76 ;; if the proc matched.
8f59c14e
JB
77 ,@(let next-expr ((tests (map car clauses))
78 (exprs (map cdr clauses))
79 (body '()))
80 (cond
81 ((null? tests)
82 (reverse body))
d7189b49 83 (else
8f59c14e
JB
84 (next-expr
85 (cdr tests)
86 (cdr exprs)
87 (cons
88 `((,(car tests) ,s (eof-object? ,c))
89 ,@(cond ((null? (car exprs))
90 '())
91 ((eq? (caar exprs) '=>)
92 (if (not (= (length (car exprs))
93 2))
94 (scm-error 'misc-error
95 "expect"
8641dd9e 96 "bad recipient: ~S"
8f59c14e
JB
97 (list (car exprs))
98 #f)
99 `((apply ,(cadar exprs)
100 (,(car tests) ,s ,port)))))
f32e992f 101 (else
8f59c14e
JB
102 (car exprs))))
103 body)))))
104 ;; if none of the clauses matched the current string.
105 (else (cond ((eof-object? ,c)
106 (if expect-eof-proc
107 (expect-eof-proc ,s)
108 #f))
d7189b49 109 (else
8f59c14e 110 (next-char)))))))))))
d7189b49 111
156ecad5
JB
112
113(define-public expect-strings-compile-flags regexp/newline)
2645b7b8 114(define-public expect-strings-exec-flags regexp/noteol)
156ecad5 115
d7189b49
GH
116;;; the regexec front-end to expect:
117;;; each test must evaluate to a regular expression.
ec8469e7 118(defmacro-public expect-strings clauses
d7189b49
GH
119 `(let ,@(let next-test ((tests (map car clauses))
120 (exprs (map cdr clauses))
28d8ab3c
GH
121 (defs '())
122 (body '()))
d7189b49
GH
123 (cond ((null? tests)
124 (list (reverse defs) `(expect ,@(reverse body))))
125 (else
126 (let ((rxname (gentemp)))
127 (next-test (cdr tests)
128 (cdr exprs)
156ecad5
JB
129 (cons `(,rxname (make-regexp
130 ,(car tests)
131 expect-strings-compile-flags))
d7189b49 132 defs)
8f59c14e
JB
133 (cons `((lambda (s eof?)
134 (expect-regexec ,rxname s eof?))
d7189b49
GH
135 ,@(car exprs))
136 body))))))))
137
28d77376
GH
138;;; simplified select: returns #t if input is waiting or #f if timed out or
139;;; select was interrupted by a signal.
cafa4c68 140;;; timeout is an absolute time in floating point seconds.
ec8469e7 141(define-public (expect-select port timeout)
cafa4c68 142 (let* ((secs-usecs (gettimeofday))
f32e992f 143 (relative (- timeout
cafa4c68
GH
144 (car secs-usecs)
145 (/ (cdr secs-usecs)
146 1000000)))) ; one million.
d7189b49 147 (and (> relative 0)
28d8ab3c 148 (pair? (car (select (list port) '() '()
cafa4c68 149 relative))))))
28d8ab3c 150
8f59c14e
JB
151;;; match a string against a regexp, returning a list of strings (required
152;;; by the => syntax) or #f. called once each time a character is added
153;;; to s (eof? will be #f), and once when eof is reached (with eof? #t).
154(define-public (expect-regexec rx s eof?)
2645b7b8 155 ;; if expect-strings-exec-flags contains regexp/noteol,
8f59c14e
JB
156 ;; remove it for the eof test.
157 (let* ((flags (if (and eof?
158 (logand expect-strings-exec-flags regexp/noteol))
2645b7b8
JB
159 (logxor expect-strings-exec-flags regexp/noteol)
160 expect-strings-exec-flags))
156ecad5 161 (match (regexp-exec rx s 0 flags)))
28d8ab3c
GH
162 (if match
163 (do ((i (- (match:count match) 1) (- i 1))
164 (result '() (cons (match:substring match i) result)))
165 ((< i 0) result))
166 #f)))
2645b7b8 167
f32e992f 168;;; expect.scm ends here