Function: tramp-wait-for-output

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

Signature

(tramp-wait-for-output PROC &optional TIMEOUT)

Documentation

Wait for output from remote command.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
(defun tramp-wait-for-output (proc &optional timeout)
  "Wait for output from remote command."
  (unless (buffer-live-p (process-buffer proc))
    (delete-process proc)
    (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
  (with-current-buffer (process-buffer proc)
    (let* (;; Initially, `tramp-end-of-output' is "#$ ".  There might
	   ;; be leading ANSI control escape sequences, which must be
	   ;; ignored.  Busyboxes built with the EDITING_ASK_TERMINAL
	   ;; config option send also ANSI control escape sequences,
	   ;; which must be ignored.
	   (regexp (rx
		    (* (not (any "#$\n")))
		    (literal tramp-end-of-output)
		    (? (regexp ansi-color-control-seq-regexp))
		    (? "\r") eol))
	   ;; Sometimes, the commands do not return a newline but a
	   ;; null byte before the shell prompt, for example "git
	   ;; ls-files -c -z ...".
	   (regexp1 (rx (| bol "\000") (regexp regexp)))
	   (found (tramp-wait-for-regexp proc timeout regexp1)))
      (if found
	  (let ((inhibit-read-only t))
	    ;; A simple-minded busybox has sent " ^H" sequences.
	    ;; Delete them.
	    (goto-char (point-min))
	    (when (search-forward-regexp
		   (rx bol (+ nonl "\b") eol) (line-end-position) t)
	      (forward-line 1)
	      (delete-region (point-min) (point)))
	    ;; Delete the prompt.
	    (when (tramp-search-regexp regexp)
	      (delete-region (point) (point-max))))
	(if timeout
	    (tramp-error
	     proc 'file-error
	     "[[Remote prompt `%s' not found in %d secs]]"
	     tramp-end-of-output timeout)
	  (tramp-error
	   proc 'file-error
	   "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
      ;; Return value is whether end-of-output sentinel was found.
      found)))