Function: tramp-wait-for-regexp

tramp-wait-for-regexp is a byte-compiled function defined in tramp.el.gz.

Signature

(tramp-wait-for-regexp PROC TIMEOUT REGEXP)

Documentation

Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.

Expects the output of PROC to be sent to the current buffer. Returns the string that matched, or nil. Waits indefinitely if TIMEOUT is nil.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp.el.gz
(defun tramp-wait-for-regexp (proc timeout regexp)
  "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
Expects the output of PROC to be sent to the current buffer.  Returns
the string that matched, or nil.  Waits indefinitely if TIMEOUT is
nil."
  (let ((found (tramp-check-for-regexp proc regexp)))
    (with-tramp-timeout (timeout)
      (while (not found)
	;; This is needed to yield the CPU, otherwise we'll see 100% CPU load.
	(sit-for 0 'nodisp)
	(tramp-accept-process-output proc)
	(unless (process-live-p proc)
	  (tramp-error-with-buffer
	   nil proc 'file-error "Process has died"))
	(setq found (tramp-check-for-regexp proc regexp))))
    ;; The process could have timed out, for example due to session
    ;; timeout of sudo.  The process buffer does not exist any longer then.
    (ignore-errors
      (tramp-message
       proc 6 "%S\n%s" proc (tramp-get-buffer-string (process-buffer proc))))
    (unless found
      (if timeout
	  (tramp-error
	   proc 'file-error "[[Regexp `%s' not found in %d secs]]"
	   regexp timeout)
	(tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
    found))