/usr/share/emacs/site-lisp/mailcrypt/expect.el is in mailcrypt 3.5.9-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | ;;; expect.el --- support for external process communication
;; Copyright (C) 1997 Free Software Foundation, Inc.
;; Author: Lars Magne Ingebrigtsen <[22]lmi@gnus.org>
;; Keywords: extensions, processes
;; This file is soon to be part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;; Code:
(require 'cl)
(require 'timer)
(defvar expect-message nil
"*If non-nil, report how much data has arrived in the process buffer.
This variable is buffer-local to all Expect buffers, and should be set
inside @code{with-expect} forms.")
(defvar expect-start nil
"If a number, start the Expect searches from that point.
If not, start searches from `(point-min)'.
This variable is typically `let' to t before calling `with-expect'
when waiting for output from a process that is already started and may
have output data.")
(defvar expect-timeout 10
"The number of seconds to wait before an Expect timeout element is triggered.
")
;;; Internal variables.
(defvar expect-processes nil)
(defvar expect-asynchronous nil)
(defvar expect-process nil) ; Dynamic variable
(defvar expect-current-info nil) ; Dynamic variable
;;; Utility macros.
(defun expect-make-info (process message point)
(list process message point nil nil))
(defmacro expect-info-process (info)
`(nth 0 ,info))
(defmacro expect-info-message (info)
`(nth 1 ,info))
(defmacro expect-info-point (info)
`(nth 2 ,info))
(defmacro expect-info-set-point (info point)
`(setcar (nthcdr 2 ,info) ,point))
(defmacro expect-info-sentinels (info)
`(nth 3 ,info))
(defmacro expect-info-set-sentinels (info sentinels)
`(setcar (nthcdr 3 ,info) ,sentinels))
(defmacro expect-info-timer (info)
`(nth 4 ,info))
(defmacro expect-info-set-timer (info timer)
`(setcar (nthcdr 4 ,info) ,timer))
(defmacro expect-info-queries (info)
`(nthcdr 5 ,info))
(defmacro expect-info-set-queries (info queries)
`(setcdr (nthcdr 4 ,info) ,queries))
(defmacro expect-find-info (process)
`(assoc ,process expect-processes))
;;; Interface macros.
;;;###autoload
(defmacro with-expect (program &rest forms)
"Set things up for communication with PROGRAM.
FORMS will be evaluated in the normal manner. To talk to the process,
use `expect' and `expect-send'. See the manual for full documentation.
This macro returns nil.
If PROGRAM is a string, start that program. If PROGRAM is a list, use
the first element of that list as the program and the remainder as the
parameters. If PROGRAM is a process, talk to that process.
PROGRAM will be started up in a new, fresh temporary buffer. The
buffer will be killed upon completion. If PROGRAM is a process,
a new buffer won't be created, and the buffer won't be killed upon
completion."
(let ((buf (make-symbol "buf"))
(point (make-symbol "point")))
`(save-excursion
(let ((,buf (generate-new-buffer " *expect*"))
(,point (point))
expect-process expect-current-info)
(set-buffer ,buf)
(unless (setq expect-process
(expect-start-process ,program))
(error "Can't start program"))
(expect-setup ,point)
,@forms
(unless (expect-info-sentinels expect-current-info)
(expect t))
nil))))
(defun expect-start-process (program)
(cond
((stringp program)
(start-process "expect" (current-buffer) program))
((consp program)
(apply 'start-process
"expect" (current-buffer) (car program) (cdr program)))
((processp program)
program)
(t
(error "Illegal process spec"))))
(defmacro with-expect-asynchronous (program &rest forms)
"Set things up for asynchronous communication with PROGRAM.
This macro behaves like `with-expect', only that `expect' calls
contained in FORMS will be evaluated asyncronously.
See the documentation of the `with-expect' macro for documentation."
`(let ((expect-asynchronous t))
(with-expect ,program ,@forms)))
(defmacro expect (regexp &rest forms)
"Execute FORMS when REGEXP has arrived in the buffer."
`(expect-1 ,regexp #'(lambda () ,@forms)))
(defmacro expect-cond (&rest clauses)
"Try each clause until one succeeds.
Each clause looks like (CONDITION BODY). CONDITION should be
a regular expression to wait for, or a process status symbol.
If CONDITION is satisfied (i. e., the data has arrived or
the process has entered the specified status), BODY will be executed."
(let (result)
(while clauses
(push (if (stringp (caar clauses)) (caar clauses)
(list 'quote (caar clauses)))
result)
(push (car `(#'(lambda () ,@(cdar clauses)))) result)
(pop clauses))
`(expect-1 ,@(nreverse result))))
(defmacro expect-exit (&rest forms)
"Execute FORMS when the process has exited."
`(expect-exit-1 #'(lambda () ,@forms)))
;;; User utility functions.
(defmacro expect-send (string)
"Send STRING to the current buffer's process."
`(process-send-string expect-process ,string))
;;; Internal functions.
(defun expect-setup (&optional point)
"Initialize Expect data, filter and sentinel."
(setq expect-current-info
(expect-make-info expect-process expect-message
(or point expect-start (point-min))))
(push expect-current-info expect-processes)
(set-process-filter expect-process 'expect-filter)
(set-process-sentinel expect-process 'expect-sentinel)
(set-buffer (process-buffer expect-process)))
(defun expect-shutdown (process)
"Remove Expect infestation of PROCESS."
(setq expect-processes (delq (expect-find-info process) expect-processes))
(set-process-filter process nil)
(set-process-sentinel process nil))
(defun expect-kill (process)
"Kill PROCESS and its buffer."
(let ((buffer (process-buffer process)))
(when (buffer-name buffer)
(kill-buffer buffer))
(expect-shutdown process)
(delete-process process)))
(defun expect-wait ()
"Wait until the current outstanding command has been performed."
(let ((info (expect-find-info expect-process)))
(expect-setup-timer info)
(while (and (car (expect-info-queries (expect-find-info expect-process)))
(memq (process-status expect-process) '(open run)))
(accept-process-output expect-process 1))
(expect-cancel-timer info))
;; We return nil.
nil)
(defun expect-1 (&rest clauses)
(let (entry entries timeout)
(unless expect-process
(error "No expect in this buffer"))
;; Add this clause to the list of things to be executed.
(while clauses
(if (eq (car clauses) 'timeout)
(setq timeout (cadr clauses)
clauses (cddr clauses))
(push (list (pop clauses) (pop clauses))
entries)))
(when timeout
(expect-info-set-timer expect-current-info
(list nil expect-timeout timeout)))
(nconc expect-current-info (list (nreverse entries)))
;; We see whether we have to wait for the command to complete
;; or not.
(if expect-asynchronous
nil
(expect-wait))))
(defun expect-exit-1 (function)
(unless expect-process
(error "No expect in this buffer"))
(let ((info (expect-find-info expect-process)))
(expect-info-set-sentinels
info
(nconc (expect-info-sentinels info)
(list function))))
;; We return nil.
nil)
(defun expect-filter (process string)
"Controlling Expect function run as a process filter."
(let ((old-buffer (current-buffer))
(expect-process process))
(unwind-protect
(let (moving)
(set-buffer (process-buffer process))
(setq moving (= (point) (process-mark process)))
(save-excursion
;; Insert the text, moving the process-marker.
(goto-char (process-mark process))
(insert string)
(set-marker (process-mark process) (point))
;; Do Expect things.
(expect-find-event process))
(when (memq (process-status process) '(open run))
(if moving (goto-char (process-mark process)))))
(when (buffer-name old-buffer)
(set-buffer old-buffer)))))
(defun expect-sentinel (process status)
"Controlling Expect sentinel."
;; Perhaps we're waiting for one of the process events?
(when (memq (process-status process) '(open run))
(expect-find-event process))
;; We do `expect-exit' calls.
(when (eq 'exit (process-status process))
(save-excursion
(let ((expect-process process))
(when (and (process-buffer process)
(buffer-name (process-buffer process)))
(set-buffer (process-buffer process))
(let ((sentinels (expect-info-sentinels (expect-find-info process))))
(while sentinels
(save-excursion
(funcall (pop sentinels))))
(expect-shutdown process)))))))
(defun expect-find-event (process)
"Find (and execute) the next event."
(let* ((info (expect-find-info process))
(point (expect-info-point info))
(queries (expect-info-queries info))
(clause (car queries))
cond)
(expect-setup-timer info)
(when (expect-info-message info)
(message "Expect received %d bytes" (point-max)))
(when clause
(if (eq (caar clause) t)
;; We have handled all queries and want to die.
(expect-kill process)
(when (> (point-max) point)
(goto-char point)
(while clause
(setq cond (caar clause))
(when (cond
;; Regexp
((stringp cond)
(re-search-forward (caar clause) nil t))
;; Fall-through
((eq t cond)
t)
;; Process state
((memq cond '(exit run stop signal open closed))
(eq cond (process-status process)))
(t
(error "Illegal condition: %s" cond)))
(expect-cancel-timer info)
(expect-info-set-point info (point))
(expect-info-set-queries info (cdr queries))
(save-excursion
(funcall (cadar clause)))
(setq clause nil)
;; More than one event may have arrived, so we try again.
(when (memq (process-status process) '(open run))
(expect-find-event process)))
(setq clause (cdr clause))))))))
(defun expect-setup-timer (info)
(let ((timer (expect-info-timer info)))
(when timer
(expect-cancel-timer info)
(setcar timer (run-at-time (cadr timer) nil (caddr timer))))))
(defun expect-cancel-timer (info)
(when (car (expect-info-timer info))
(ignore-errors (cancel-timer (car (expect-info-timer info))))))
;;; Indentation and edebug specs.
(put 'expect 'lisp-indent-function 1)
(put 'expect 'edebug-form-spec '(form body))
(put 'expect-exit 'lisp-indent-function 0)
(put 'expect-exit 'edebug-form-spec '(body))
(put 'with-expect 'lisp-indent-function 1)
(put 'with-expect 'edebug-form-spec '(form body))
(put 'with-expect-asynchronous 'lisp-indent-function 1)
(put 'with-expect-asynchronous 'edebug-form-spec '(form body))
(provide 'expect)
;;; expect.el ends here
|