expect.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;;; Copyright (C) 1996, 1998, 1999 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. (define-module (ice-9 expect) :use-module (ice-9 regex))
  19. ;;; Expect: a macro for selecting actions based on what it reads from a port.
  20. ;;; The idea is from Don Libes' expect based on Tcl.
  21. ;;; This version by Gary Houston incorporating ideas from Aubrey Jaffer.
  22. (define-public expect-port #f)
  23. (define-public expect-timeout #f)
  24. (define-public expect-timeout-proc #f)
  25. (define-public expect-eof-proc #f)
  26. (define-public expect-char-proc #f)
  27. ;;; expect: each test is a procedure which is applied to the accumulating
  28. ;;; string.
  29. (defmacro-public expect clauses
  30. (let ((s (gentemp))
  31. (c (gentemp))
  32. (port (gentemp))
  33. (timeout (gentemp)))
  34. `(let ((,s "")
  35. (,port (or expect-port (current-input-port)))
  36. ;; when timeout occurs, in floating point seconds.
  37. (,timeout (if expect-timeout
  38. (let* ((secs-usecs (gettimeofday)))
  39. (+ (car secs-usecs)
  40. expect-timeout
  41. (/ (cdr secs-usecs)
  42. 1000000))) ; one million.
  43. #f)))
  44. (let next-char ()
  45. (if (and expect-timeout
  46. (not (expect-select ,port ,timeout)))
  47. (if expect-timeout-proc
  48. (expect-timeout-proc ,s)
  49. #f)
  50. (let ((,c (read-char ,port)))
  51. (if expect-char-proc
  52. (expect-char-proc ,c))
  53. (if (not (eof-object? ,c))
  54. (set! ,s (string-append ,s (string ,c))))
  55. (cond
  56. ;; this expands to clauses where the car invokes the
  57. ;; match proc and the cdr is the return value from expect
  58. ;; if the proc matched.
  59. ,@(let next-expr ((tests (map car clauses))
  60. (exprs (map cdr clauses))
  61. (body '()))
  62. (cond
  63. ((null? tests)
  64. (reverse body))
  65. (else
  66. (next-expr
  67. (cdr tests)
  68. (cdr exprs)
  69. (cons
  70. `((,(car tests) ,s (eof-object? ,c))
  71. ,@(cond ((null? (car exprs))
  72. '())
  73. ((eq? (caar exprs) '=>)
  74. (if (not (= (length (car exprs))
  75. 2))
  76. (scm-error 'misc-error
  77. "expect"
  78. "bad recipient: ~S"
  79. (list (car exprs))
  80. #f)
  81. `((apply ,(cadar exprs)
  82. (,(car tests) ,s ,port)))))
  83. (else
  84. (car exprs))))
  85. body)))))
  86. ;; if none of the clauses matched the current string.
  87. (else (cond ((eof-object? ,c)
  88. (if expect-eof-proc
  89. (expect-eof-proc ,s)
  90. #f))
  91. (else
  92. (next-char)))))))))))
  93. (define-public expect-strings-compile-flags regexp/newline)
  94. (define-public expect-strings-exec-flags regexp/noteol)
  95. ;;; the regexec front-end to expect:
  96. ;;; each test must evaluate to a regular expression.
  97. (defmacro-public expect-strings clauses
  98. `(let ,@(let next-test ((tests (map car clauses))
  99. (exprs (map cdr clauses))
  100. (defs '())
  101. (body '()))
  102. (cond ((null? tests)
  103. (list (reverse defs) `(expect ,@(reverse body))))
  104. (else
  105. (let ((rxname (gentemp)))
  106. (next-test (cdr tests)
  107. (cdr exprs)
  108. (cons `(,rxname (make-regexp
  109. ,(car tests)
  110. expect-strings-compile-flags))
  111. defs)
  112. (cons `((lambda (s eof?)
  113. (expect-regexec ,rxname s eof?))
  114. ,@(car exprs))
  115. body))))))))
  116. ;;; simplified select: returns #t if input is waiting or #f if timed out or
  117. ;;; select was interrupted by a signal.
  118. ;;; timeout is an absolute time in floating point seconds.
  119. (define-public (expect-select port timeout)
  120. (let* ((secs-usecs (gettimeofday))
  121. (relative (- timeout
  122. (car secs-usecs)
  123. (/ (cdr secs-usecs)
  124. 1000000)))) ; one million.
  125. (and (> relative 0)
  126. (pair? (car (select (list port) '() '()
  127. relative))))))
  128. ;;; match a string against a regexp, returning a list of strings (required
  129. ;;; by the => syntax) or #f. called once each time a character is added
  130. ;;; to s (eof? will be #f), and once when eof is reached (with eof? #t).
  131. (define-public (expect-regexec rx s eof?)
  132. ;; if expect-strings-exec-flags contains regexp/noteol,
  133. ;; remove it for the eof test.
  134. (let* ((flags (if (and eof?
  135. (logand expect-strings-exec-flags regexp/noteol))
  136. (logxor expect-strings-exec-flags regexp/noteol)
  137. expect-strings-exec-flags))
  138. (match (regexp-exec rx s 0 flags)))
  139. (if match
  140. (do ((i (- (match:count match) 1) (- i 1))
  141. (result '() (cons (match:substring match i) result)))
  142. ((< i 0) result))
  143. #f)))