Commit | Line | Data |
---|---|---|
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 | ;;;; |
a482f2cc MV |
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 | ;;;; | |
d7189b49 | 42 | \f |
f32e992f | 43 | ;;; Commentary: |
d7189b49 | 44 | |
f32e992f TTN |
45 | ;; This module is documented in the Guile Reference Manual. |
46 | ;; Briefly, these are exported: | |
2b1621ac | 47 | ;; procedures: expect-select, expect-regexec |
f32e992f TTN |
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)) | |
ec8469e7 | 57 | |
d7189b49 GH |
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 | ||
ec8469e7 JB |
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) | |
d7189b49 GH |
68 | |
69 | ;;; expect: each test is a procedure which is applied to the accumulating | |
70 | ;;; string. | |
ec8469e7 | 71 | (defmacro-public expect clauses |
872bd194 MV |
72 | (let ((s (gensym)) |
73 | (c (gensym)) | |
74 | (port (gensym)) | |
75 | (timeout (gensym))) | |
d7189b49 GH |
76 | `(let ((,s "") |
77 | (,port (or expect-port (current-input-port))) | |
cafa4c68 | 78 | ;; when timeout occurs, in floating point seconds. |
d7189b49 | 79 | (,timeout (if expect-timeout |
cafa4c68 GH |
80 | (let* ((secs-usecs (gettimeofday))) |
81 | (+ (car secs-usecs) | |
82 | expect-timeout | |
83 | (/ (cdr secs-usecs) | |
84 | 1000000))) ; one million. | |
d7189b49 GH |
85 | #f))) |
86 | (let next-char () | |
87 | (if (and expect-timeout | |
28d77376 | 88 | (not (expect-select ,port ,timeout))) |
d7189b49 GH |
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)) | |
8f59c14e JB |
95 | (if (not (eof-object? ,c)) |
96 | (set! ,s (string-append ,s (string ,c)))) | |
97 | (cond | |
28d77376 GH |
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. | |
8f59c14e JB |
101 | ,@(let next-expr ((tests (map car clauses)) |
102 | (exprs (map cdr clauses)) | |
103 | (body '())) | |
104 | (cond | |
105 | ((null? tests) | |
106 | (reverse body)) | |
d7189b49 | 107 | (else |
8f59c14e JB |
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" | |
8641dd9e | 120 | "bad recipient: ~S" |
8f59c14e JB |
121 | (list (car exprs)) |
122 | #f) | |
123 | `((apply ,(cadar exprs) | |
124 | (,(car tests) ,s ,port))))) | |
f32e992f | 125 | (else |
8f59c14e JB |
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)) | |
d7189b49 | 133 | (else |
8f59c14e | 134 | (next-char))))))))))) |
d7189b49 | 135 | |
156ecad5 JB |
136 | |
137 | (define-public expect-strings-compile-flags regexp/newline) | |
2645b7b8 | 138 | (define-public expect-strings-exec-flags regexp/noteol) |
156ecad5 | 139 | |
d7189b49 GH |
140 | ;;; the regexec front-end to expect: |
141 | ;;; each test must evaluate to a regular expression. | |
ec8469e7 | 142 | (defmacro-public expect-strings clauses |
d7189b49 GH |
143 | `(let ,@(let next-test ((tests (map car clauses)) |
144 | (exprs (map cdr clauses)) | |
28d8ab3c GH |
145 | (defs '()) |
146 | (body '())) | |
d7189b49 GH |
147 | (cond ((null? tests) |
148 | (list (reverse defs) `(expect ,@(reverse body)))) | |
149 | (else | |
872bd194 | 150 | (let ((rxname (gensym))) |
d7189b49 GH |
151 | (next-test (cdr tests) |
152 | (cdr exprs) | |
156ecad5 JB |
153 | (cons `(,rxname (make-regexp |
154 | ,(car tests) | |
155 | expect-strings-compile-flags)) | |
d7189b49 | 156 | defs) |
8f59c14e JB |
157 | (cons `((lambda (s eof?) |
158 | (expect-regexec ,rxname s eof?)) | |
d7189b49 GH |
159 | ,@(car exprs)) |
160 | body)))))))) | |
161 | ||
28d77376 GH |
162 | ;;; simplified select: returns #t if input is waiting or #f if timed out or |
163 | ;;; select was interrupted by a signal. | |
cafa4c68 | 164 | ;;; timeout is an absolute time in floating point seconds. |
ec8469e7 | 165 | (define-public (expect-select port timeout) |
cafa4c68 | 166 | (let* ((secs-usecs (gettimeofday)) |
f32e992f | 167 | (relative (- timeout |
cafa4c68 GH |
168 | (car secs-usecs) |
169 | (/ (cdr secs-usecs) | |
170 | 1000000)))) ; one million. | |
d7189b49 | 171 | (and (> relative 0) |
28d8ab3c | 172 | (pair? (car (select (list port) '() '() |
cafa4c68 | 173 | relative)))))) |
28d8ab3c | 174 | |
8f59c14e JB |
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?) | |
2645b7b8 | 179 | ;; if expect-strings-exec-flags contains regexp/noteol, |
8f59c14e JB |
180 | ;; remove it for the eof test. |
181 | (let* ((flags (if (and eof? | |
182 | (logand expect-strings-exec-flags regexp/noteol)) | |
2645b7b8 JB |
183 | (logxor expect-strings-exec-flags regexp/noteol) |
184 | expect-strings-exec-flags)) | |
156ecad5 | 185 | (match (regexp-exec rx s 0 flags))) |
28d8ab3c GH |
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))) | |
2645b7b8 | 191 | |
f32e992f | 192 | ;;; expect.scm ends here |