diff --git a/lisp/tramp-sh.el b/lisp/tramp-sh.el index 8de88d35..506c33df 100644 --- a/lisp/tramp-sh.el +++ b/lisp/tramp-sh.el @@ -5102,9 +5102,8 @@ function waits for output unless NOOUTPUT is set." (forward-line 1) (delete-region (point-min) (point))) ;; Delete the prompt. - (goto-char (point-max)) - (re-search-backward regexp nil t) - (delete-region (point) (point-max))) + (when (tramp-search-regexp regexp) + (delete-region (point) (point-max)))) (if timeout (tramp-error proc 'file-error @@ -5134,8 +5133,7 @@ DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null." "echo tramp_exit_status $?" (if subshell " )" ""))) (with-current-buffer (tramp-get-connection-buffer vec) - (goto-char (point-max)) - (unless (re-search-backward "tramp_exit_status [0-9]+" nil t) + (unless (tramp-search-regexp "tramp_exit_status [0-9]+") (tramp-error vec 'file-error "Couldn't find exit status of `%s'" command)) (skip-chars-forward "^ ") diff --git a/lisp/tramp.el b/lisp/tramp.el index 03e04568..e05e0965 100644 --- a/lisp/tramp.el +++ b/lisp/tramp.el @@ -4196,19 +4196,35 @@ for process communication also." (buffer-string)) result))) +(defun tramp-search-regexp (regexp) + "Search for REGEXP backwards, starting at point-max. +If found, set point to the end of the occurrence found, and return point. +Otherwise, return nil." + (goto-char (point-max)) + ;; We restrict ourselves to the last 256 characters. There were + ;; reports of 85kB output, which has blocked Tramp forever. + (re-search-backward regexp (max (point-min) (- (point) 256)) 'noerror)) + (defun tramp-check-for-regexp (proc regexp) "Check, whether REGEXP is contained in process buffer of PROC. Erase echoed commands if exists." (with-current-buffer (process-buffer proc) (goto-char (point-min)) - ;; Check whether we need to remove echo output. + ;; Check whether we need to remove echo output. The max length of + ;; the echo mark regexp is taken for search. We restrict the + ;; search for the second echo mark to PIPE_BUF characters. (when (and (tramp-get-connection-property proc "check-remote-echo" nil) - (re-search-forward tramp-echoed-echo-mark-regexp nil t)) + (re-search-forward + tramp-echoed-echo-mark-regexp + (+ (point) (* 5 tramp-echo-mark-marker-length)) t)) (let ((begin (match-beginning 0))) - (when (re-search-forward tramp-echoed-echo-mark-regexp nil t) + (when + (re-search-forward + tramp-echoed-echo-mark-regexp + (+ (point) (tramp-get-connection-property proc "pipe-buf" 4096)) t) ;; Discard echo from remote output. - (tramp-set-connection-property proc "check-remote-echo" nil) + (tramp-flush-connection-property proc "check-remote-echo") (tramp-message proc 5 "echo-mark found") (forward-line 1) (delete-region begin (point)) @@ -4229,8 +4245,7 @@ Erase echoed commands if exists." ;; overflow in regexp matcher". For example, //DIRED// lines of ;; directory listings with some thousand files. Therefore, we ;; look from the end. - (goto-char (point-max)) - (ignore-errors (re-search-backward regexp nil t))))) + (tramp-search-regexp regexp)))) (defun tramp-wait-for-regexp (proc timeout regexp) "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.