expect.texi 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Expect
  7. @section Expect
  8. The macros in this section are made available with:
  9. @lisp
  10. (use-modules (ice-9 expect))
  11. @end lisp
  12. @code{expect} is a macro for selecting actions based on the output from
  13. a port. The name comes from a tool of similar functionality by Don Libes.
  14. Actions can be taken when a particular string is matched, when a timeout
  15. occurs, or when end-of-file is seen on the port. The @code{expect} macro
  16. is described below; @code{expect-strings} is a front-end to @code{expect}
  17. based on regexec (see the regular expression documentation).
  18. @defmac expect-strings clause @dots{}
  19. By default, @code{expect-strings} will read from the current input port.
  20. The first term in each clause consists of an expression evaluating to
  21. a string pattern (regular expression). As characters
  22. are read one-by-one from the port, they are accumulated in a buffer string
  23. which is matched against each of the patterns. When a
  24. pattern matches, the remaining expression(s) in
  25. the clause are evaluated and the value of the last is returned. For example:
  26. @lisp
  27. (with-input-from-file "/etc/passwd"
  28. (lambda ()
  29. (expect-strings
  30. ("^nobody" (display "Got a nobody user.\n")
  31. (display "That's no problem.\n"))
  32. ("^daemon" (display "Got a daemon user.\n")))))
  33. @end lisp
  34. The regular expression is compiled with the @code{REG_NEWLINE} flag, so
  35. that the ^ and $ anchors will match at any newline, not just at the start
  36. and end of the string.
  37. There are two other ways to write a clause:
  38. The expression(s) to evaluate
  39. can be omitted, in which case the result of the regular expression match
  40. (converted to strings, as obtained from regexec with match-pick set to "")
  41. will be returned if the pattern matches.
  42. The symbol @code{=>} can be used to indicate that the expression is a
  43. procedure which will accept the result of a successful regular expression
  44. match. E.g.,
  45. @lisp
  46. ("^daemon" => write)
  47. ("^d(aemon)" => (lambda args (for-each write args)))
  48. ("^da(em)on" => (lambda (all sub)
  49. (write all) (newline)
  50. (write sub) (newline)))
  51. @end lisp
  52. The order of the substrings corresponds to the order in which the
  53. opening brackets occur.
  54. A number of variables can be used to control the behaviour
  55. of @code{expect} (and @code{expect-strings}).
  56. Most have default top-level bindings to the value @code{#f},
  57. which produces the default behaviour.
  58. They can be redefined at the
  59. top level or locally bound in a form enclosing the expect expression.
  60. @table @code
  61. @item expect-port
  62. A port to read characters from, instead of the current input port.
  63. @item expect-timeout
  64. @code{expect} will terminate after this number of
  65. seconds, returning @code{#f} or the value returned by expect-timeout-proc.
  66. @item expect-timeout-proc
  67. A procedure called if timeout occurs. The procedure takes a single argument:
  68. the accumulated string.
  69. @item expect-eof-proc
  70. A procedure called if end-of-file is detected on the input port. The
  71. procedure takes a single argument: the accumulated string.
  72. @item expect-char-proc
  73. A procedure to be called every time a character is read from the
  74. port. The procedure takes a single argument: the character which was read.
  75. @item expect-strings-compile-flags
  76. Flags to be used when compiling a regular expression, which are passed
  77. to @code{make-regexp} @xref{Regexp Functions}. The default value
  78. is @code{regexp/newline}.
  79. @item expect-strings-exec-flags
  80. Flags to be used when executing a regular expression, which are
  81. passed to regexp-exec @xref{Regexp Functions}.
  82. The default value is @code{regexp/noteol}, which prevents @code{$}
  83. from matching the end of the string while it is still accumulating,
  84. but still allows it to match after a line break or at the end of file.
  85. @end table
  86. Here's an example using all of the variables:
  87. @smalllisp
  88. (let ((expect-port (open-input-file "/etc/passwd"))
  89. (expect-timeout 1)
  90. (expect-timeout-proc
  91. (lambda (s) (display "Times up!\n")))
  92. (expect-eof-proc
  93. (lambda (s) (display "Reached the end of the file!\n")))
  94. (expect-char-proc display)
  95. (expect-strings-compile-flags (logior regexp/newline regexp/icase))
  96. (expect-strings-exec-flags 0))
  97. (expect-strings
  98. ("^nobody" (display "Got a nobody user\n"))))
  99. @end smalllisp
  100. @end defmac
  101. @defmac expect clause @dots{}
  102. @code{expect} is used in the same way as @code{expect-strings},
  103. but tests are specified not as patterns, but as procedures. The
  104. procedures are called in turn after each character is read from the
  105. port, with two arguments: the value of the accumulated string and
  106. a flag to indicate whether end-of-file has been reached. The flag
  107. will usually be @code{#f}, but if end-of-file is reached, the procedures
  108. are called an additional time with the final accumulated string and
  109. @code{#t}.
  110. The test is successful if the procedure returns a non-false value.
  111. If the @code{=>} syntax is used, then if the test succeeds it must return
  112. a list containing the arguments to be provided to the corresponding
  113. expression.
  114. In the following example, a string will only be matched at the beginning
  115. of the file:
  116. @lisp
  117. (let ((expect-port (open-input-file "/etc/passwd")))
  118. (expect
  119. ((lambda (s eof?) (string=? s "fnord!"))
  120. (display "Got a nobody user!\n"))))
  121. @end lisp
  122. The control variables described for @code{expect-strings} also
  123. influence the behaviour of @code{expect}, with the exception of
  124. variables whose names begin with @code{expect-strings-}.
  125. @end defmac