Subject: bug#31924: 27.0.50; tramp handler /sudo::FILE fails when /bin/sh points to zsh

From: Tino Calancha <[email-masked]>
Date: Sun, 24 Jun 2018 16:07:35 +0900

Michael Albinus <[email-masked]> writes:

> Tino Calancha <[email-masked]> writes:
>
> Hi Tino,
>
> thanks for the traces. Tramp doesn't run too much. First, it invokes the
> local shell:
>
>> 17:57:38.934983 tramp-maybe-open-connection (6) # /bin/sh -i
>
> And this returns a nifty prompt:
>
>> 17:57:38.936893 tramp-accept-process-output (10) # *tramp/sudo [email-masked]* run t
>> %                                                                               #$ [?2004h
>
> That's it. According to
> <https://emacs.stackexchange.com/questions/19848/weird-characters-in-shell-mode-with-zsh>,
> "^[[?2004h" is good for bracketed paste mode. This must be disabled as
> well, the reference I've quoted recommends "unset
> zle_bracketed_paste". As said, I have no idea how this plays together
> with what is already recommended for zsh in the Tramp manual.
Hi Michael,

I got a patch that works in my box.  It does 2 things:

1) Ensure that zsh is invoked as zsh, that is, not as sh.  Otherwise
   our dotfiles are ignored, and we keep getting the bracketed paste
   codes.
   
2) I also need to disable the zsh option promptcr, otherwise after I
introduce my root password, Emacs gets stuck in `tramp-wait-for-output';
this is becuse some '\r' are added in front the prompt, so the
regexp in ``tramp-wait-for-regexp' cannot match.
--8<-----------------------------cut here---------------start------------->8---
commit 30f90a77db4dc1ebe5c98ab33f36ddffd5302b50
Author: Tino Calancha <[email-masked]>
Date:   Sun Jun 24 16:03:40 2018 +0900

    Tramp: Handle /sudo::FILE when /bin/sh points to zsh
    
    If zsh is invoked as sh, then the dotfiles, e.g. ~/.zshrc., aren't read.
    We also need to unset the option promptcr, otherwise
    `tramp-wait-for-output' gets stuck.
    * lisp/net/tramp-sh.el (tramp-maybe-open-connection):
    Use (file-truename tramp-encoding-shell) as the PROGRAM
    in the `start-process' call (Bug#31924).
    
    * lisp/net/tramp-sh.el (tramp-open-connection-setup-interactive-shell):
    Same.
    
    * lisp/net/tramp-sh.el (tramp-sh-extra-args): Don't add CR to the
    zsh prompt.

diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 26bf3cd0c0..49963bff9d 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -550,7 +550,9 @@ tramp-remote-process-environment
   :type '(repeat string))
 
 ;;;###tramp-autoload
-(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
+(defcustom tramp-sh-extra-args
+  '(("/bash\\'" . "-norc -noprofile")
+    ("/zsh\\'" . "--no-promptcr"))
   "Alist specifying extra arguments to pass to the remote shell.
 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
 matching the shell file name and ARGS is a string specifying the
@@ -4110,7 +4112,11 @@ tramp-open-connection-setup-interactive-shell
 process to set up.  VEC specifies the connection."
   (let ((tramp-end-of-output tramp-initial-end-of-output)
 	(case-fold-search t))
-    (tramp-open-shell vec (tramp-get-method-parameter vec 'tramp-remote-shell))
+    (tramp-open-shell
+     vec
+     (if (not (equal "sudo" (tramp-file-name-method vec)))
+         (tramp-get-method-parameter vec 'tramp-remote-shell)
+       (file-truename (tramp-get-method-parameter vec 'tramp-remote-shell))))
 
     ;; Disable echo expansion.
     (tramp-message vec 5 "Setting up remote shell environment")
@@ -4793,6 +4799,7 @@ tramp-maybe-open-connection
 		     ;; W32 systems.
 		     (process-coding-system-alist nil)
 		     (coding-system-for-read nil)
+		     (enconding-shell (file-truename tramp-encoding-shell))
 		     ;; This must be done in order to avoid our file
 		     ;; name handler.
 		     (p (let ((default-directory
@@ -4802,9 +4809,9 @@ tramp-maybe-open-connection
 			   (tramp-get-connection-name vec)
 			   (tramp-get-connection-buffer vec)
 			   (if tramp-encoding-command-interactive
-			       (list tramp-encoding-shell
+			       (list encoding-shell
 				     tramp-encoding-command-interactive)
-			     (list tramp-encoding-shell))))))
+			     (list encoding-shell))))))
 
 		;; Set sentinel and query flag.  Initialize variables.
 		(set-process-sentinel p 'tramp-process-sentinel)
@@ -4819,7 +4826,7 @@ tramp-maybe-open-connection
 		;; Check whether process is alive.
 		(tramp-barf-if-no-shell-prompt
 		 p 10
-		 "Couldn't find local shell prompt for %s" tramp-encoding-shell)
+		 "Couldn't find local shell prompt for %s" encoding-shell)
 
 		;; Now do all the connections as specified.
 		(while target-alist
@@ -4834,7 +4841,12 @@ tramp-maybe-open-connection
 			 (login-args
 			  (tramp-get-method-parameter hop 'tramp-login-args))
 			 (login-env
-			  (tramp-get-method-parameter hop 'tramp-login-env))
+			  (if (not (equal "sudo" (tramp-file-name-method vec)))
+			      (tramp-get-method-parameter hop 'tramp-login-env)
+			    (cl-letf ((tmp-login-env
+			               (tramp-get-method-parameter hop 'tramp-login-env)))
+			      (setf (caadr tmp-login-env) (file-truename (caadr tmp-login-env)))
+			      tmp-login-env)))
 			 (async-args
 			  (tramp-get-method-parameter hop 'tramp-async-args))
 			 (connection-timeout
--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 27.0.50 (build 49, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2018-06-24 built on calancha-pc.dy.bbexcite.jp
Repository revision: 6ffc6a698f5d425e402c35010394cdb17d8888ce
Windowing system distributor 'The X.Org Foundation', version 11.0.11902000
System Description: Debian GNU/Linux 9 (stretch)