1 ;;; expect.el --- support for external process communication
3 ;; This file is not part of Emacs
5 ;; Copyright (C) 1997 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <lmi@gnus.org>
7 ;; Keywords: extensions, processes
9 ;; This file is soon to be part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with program; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
35 ;; Old XEmacs does not have this, ignore load error
36 (ignore-errors (require 'timer))
38 (when (and (not (fboundp 'run-at-time))
39 (locate-library "tinyliba"))
42 (defvar expect-message nil
43 "*If non-nil, report how much data has arrived in the process buffer.
44 This variable is buffer-local to all Expect buffers, and should be set
45 inside @code{with-expect} forms.")
47 (defvar expect-start nil
48 "If a number, start the Expect searches from that point.
49 If not, start searches from `(point-min)'.
50 This variable is typically `let' to t before calling `with-expect'
51 when waiting for output from a process that is already started and may
54 (defvar expect-timeout 10
55 "The number of seconds to wait before an Expect timeout element is triggered.")
57 ;;; Internal variables.
59 (defvar expect-processes nil)
60 (defvar expect-asynchronous nil)
61 (defvar expect-process nil) ; Dynamic variable
62 (defvar expect-current-info nil) ; Dynamic variable
66 (defun expect-make-info (process message point)
67 (list process message point nil nil))
69 (defmacro expect-info-process (info)
72 (defmacro expect-info-message (info)
75 (defmacro expect-info-point (info)
77 (defmacro expect-info-set-point (info point)
78 `(setcar (nthcdr 2 ,info) ,point))
80 (defmacro expect-info-sentinels (info)
82 (defmacro expect-info-set-sentinels (info sentinels)
83 `(setcar (nthcdr 3 ,info) ,sentinels))
85 (defmacro expect-info-timer (info)
87 (defmacro expect-info-set-timer (info timer)
88 `(setcar (nthcdr 4 ,info) ,timer))
90 (defmacro expect-info-queries (info)
92 (defmacro expect-info-set-queries (info queries)
93 `(setcdr (nthcdr 4 ,info) ,queries))
95 (defmacro expect-find-info (process)
96 `(assoc ,process expect-processes))
101 (defmacro with-expect (program &rest forms)
102 "Set things up for communication with PROGRAM.
103 FORMS will be evaluated in the normal manner. To talk to the process,
104 use `expect' and `expect-send'. See the manual for full documentation.
105 This macro returns nil.
107 If PROGRAM is a string, start that program. If PROGRAM is a list, use
108 the first element of that list as the program and the remainder as the
109 parameters. If PROGRAM is a process, talk to that process.
111 PROGRAM will be started up in a new, fresh temporary buffer. The
112 buffer will be killed upon completion. If PROGRAM is a process,
113 a new buffer won't be created, and the buffer won't be killed upon
115 (let ((buf (make-symbol "buf"))
116 (point (make-symbol "point")))
118 (let ((,buf (generate-new-buffer " *expect*"))
120 expect-process expect-current-info)
122 (unless (setq expect-process
123 (expect-start-process ,program))
124 (error "Can't start program"))
125 (expect-setup ,point)
127 (unless (expect-info-sentinels expect-current-info)
131 (defun expect-start-process (program)
134 (start-process "expect" (current-buffer) program))
136 (apply 'start-process
137 "expect" (current-buffer) (car program) (cdr program)))
141 (error "Illegal process spec"))))
143 (defmacro with-expect-asynchronous (program &rest forms)
144 "Set things up for asynchronous communication with PROGRAM.
145 This macro behaves like `with-expect', only that `expect' calls
146 contained in FORMS will be evaluated asyncronously.
148 See the documentation of the `with-expect' macro for documentation."
149 `(let ((expect-asynchronous t))
150 (with-expect ,program ,@forms)))
152 (defmacro expect (regexp &rest forms)
153 "Execute FORMS when REGEXP has arrived in the buffer."
154 `(expect-1 ,regexp #'(lambda () ,@forms)))
156 (defmacro expect-cond (&rest clauses)
157 "Try each clause until one succeeds.
158 Each clause looks like (CONDITION BODY). CONDITION should be
159 a regular expression to wait for, or a process status symbol.
160 If CONDITION is satisfied (i. e., the data has arrived or
161 the process has entered the specified status), BODY will be executed."
164 (push (if (stringp (caar clauses)) (caar clauses)
165 (list 'quote (caar clauses)))
167 (push (car `(#'(lambda () ,@(cdar clauses)))) result)
169 `(expect-1 ,@(nreverse result))))
171 (defmacro expect-exit (&rest forms)
172 "Execute FORMS when the process has exited."
173 `(expect-exit-1 #'(lambda () ,@forms)))
175 ;;; User utility functions.
177 (defmacro expect-send (string)
178 "Send STRING to the current buffer's process."
179 `(process-send-string expect-process ,string))
181 ;;; Internal functions.
183 (defun expect-setup (&optional point)
184 "Initialize Expect data, filter and sentinel."
185 (setq expect-current-info
186 (expect-make-info expect-process expect-message
187 (or point expect-start (point-min))))
188 (push expect-current-info expect-processes)
189 (set-process-filter expect-process 'expect-filter)
190 (set-process-sentinel expect-process 'expect-sentinel)
191 (set-buffer (process-buffer expect-process)))
193 (defun expect-shutdown (process)
194 "Remove Expect infestation of PROCESS."
195 (setq expect-processes (delq (expect-find-info process) expect-processes))
196 (set-process-filter process nil)
197 (set-process-sentinel process nil))
199 (defun expect-kill (process)
200 "Kill PROCESS and its buffer."
201 (let ((buffer (process-buffer process)))
202 (when (buffer-name buffer)
203 (kill-buffer buffer))
204 (expect-shutdown process)
205 (delete-process process)))
207 (defun expect-wait ()
208 "Wait until the current outstanding command has been performed."
209 (let ((info (expect-find-info expect-process)))
210 (expect-setup-timer info)
211 (while (and (car (expect-info-queries (expect-find-info expect-process)))
212 (memq (process-status expect-process) '(open run)))
213 (accept-process-output expect-process 1))
214 (expect-cancel-timer info))
218 (defun expect-1 (&rest clauses)
221 (unless expect-process
222 (error "No expect in this buffer"))
223 ;; Add this clause to the list of things to be executed.
225 (if (eq (car clauses) 'timeout)
226 (setq timeout (cadr clauses)
227 clauses (cddr clauses))
228 (push (list (pop clauses) (pop clauses))
231 (expect-info-set-timer expect-current-info
232 (list nil expect-timeout timeout)))
233 (nconc expect-current-info (list (nreverse entries)))
234 ;; We see whether we have to wait for the command to complete
236 (if expect-asynchronous
240 (defun expect-exit-1 (function)
241 (unless expect-process
242 (error "No expect in this buffer"))
243 (let ((info (expect-find-info expect-process)))
244 (expect-info-set-sentinels
246 (nconc (expect-info-sentinels info)
251 (defun expect-filter (process string)
252 "Controlling Expect function run as a process filter."
253 (let ((old-buffer (current-buffer))
254 (expect-process process))
257 (set-buffer (process-buffer process))
258 (setq moving (= (point) (process-mark process)))
260 ;; Insert the text, moving the process-marker.
261 (goto-char (process-mark process))
263 (set-marker (process-mark process) (point))
265 (expect-find-event process))
266 (when (memq (process-status process) '(open run))
267 (if moving (goto-char (process-mark process)))))
268 (when (buffer-name old-buffer)
269 (set-buffer old-buffer)))))
271 (defun expect-sentinel (process status)
272 "Controlling Expect sentinel."
273 ;; Perhaps we're waiting for one of the process events?
274 (when (memq (process-status process) '(open run))
275 (expect-find-event process))
276 ;; We do `expect-exit' calls.
277 (when (eq 'exit (process-status process))
279 (let ((expect-process process))
280 (when (and (process-buffer process)
281 (buffer-name (process-buffer process)))
282 (set-buffer (process-buffer process))
283 (let ((sentinels (expect-info-sentinels (expect-find-info process))))
286 (funcall (pop sentinels))))
287 (expect-shutdown process)))))))
289 (defun expect-find-event (process)
290 "Find (and execute) the next event."
291 (let* ((info (expect-find-info process))
292 (point (expect-info-point info))
293 (queries (expect-info-queries info))
294 (clause (car queries))
296 (expect-setup-timer info)
297 (when (expect-info-message info)
298 (message "Expect received %d bytes" (point-max)))
300 (if (eq (caar clause) t)
301 ;; We have handled all queries and want to die.
302 (expect-kill process)
303 (when (> (point-max) point)
306 (setq cond (caar clause))
310 (re-search-forward (caar clause) nil t))
315 ((memq cond '(exit run stop signal open closed))
316 (eq cond (process-status process)))
318 (error "Illegal condition: %s" cond)))
319 (expect-cancel-timer info)
320 (expect-info-set-point info (point))
321 (expect-info-set-queries info (cdr queries))
323 (funcall (cadar clause)))
325 ;; More than one event may have arrived, so we try again.
326 (when (memq (process-status process) '(open run))
327 (expect-find-event process)))
328 (setq clause (cdr clause))))))))
330 (defun expect-setup-timer (info)
331 (let ((timer (expect-info-timer info)))
333 (expect-cancel-timer info)
334 (setcar timer (run-at-time (cadr timer) nil (caddr timer))))))
336 (defun expect-cancel-timer (info)
337 (when (car (expect-info-timer info))
338 (ignore-errors (cancel-timer (car (expect-info-timer info))))))
340 ;;; Indentation and edebug specs.
342 (put 'expect 'lisp-indent-function 1)
343 (put 'expect 'edebug-form-spec '(form body))
344 (put 'expect-exit 'lisp-indent-function 0)
345 (put 'expect-exit 'edebug-form-spec '(body))
346 (put 'with-expect 'lisp-indent-function 1)
347 (put 'with-expect 'edebug-form-spec '(form body))
348 (put 'with-expect-asynchronous 'lisp-indent-function 1)
349 (put 'with-expect-asynchronous 'edebug-form-spec '(form body))
353 ;;; expect.el ends here