*** empty log message ***
[bpt/emacs.git] / lisp / net / tramp.el
CommitLineData
83bbd71b 1;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
b1a2b924 2;;; tramp.el --- Transparent Remote Access, Multiple Protocol
fb7933a3 3
5fd6d89f 4;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
a69c01a0 5;; 2005, 2006 Free Software Foundation, Inc.
fb7933a3 6
d2a2c17f
MA
7;; Author: Kai Gro\e,A_\e(Bjohann <kai.grossjohann@gmx.net>
8;; Michael Albinus <michael.albinus@gmx.de>
fb7933a3
KG
9;; Keywords: comm, processes
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
fb7933a3
KG
27
28;;; Commentary:
29
30;; This package provides remote file editing, similar to ange-ftp.
31;; The difference is that ange-ftp uses FTP to transfer files between
32;; the local and the remote host, whereas tramp.el uses a combination
33;; of rsh and rcp or other work-alike programs, such as ssh/scp.
34;;
b1a2b924 35;; For more detailed instructions, please see the info file.
fb7933a3
KG
36;;
37;; Notes:
38;; -----
bf247b6e 39;;
fb7933a3
KG
40;; This package only works for Emacs 20 and higher, and for XEmacs 21
41;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
42;; 19 is reported to have other problems. For XEmacs 21, you need the
43;; package `fsf-compat' for the `with-timeout' macro.)
44;;
45;; This version might not work with pre-Emacs 21 VC unless VC is
46;; loaded before tramp.el. Could you please test this and tell me about
47;; the result? Thanks.
48;;
49;; Also see the todo list at the bottom of this file.
50;;
b1a2b924 51;; The current version of Tramp can be retrieved from the following URL:
340b8d4f 52;; http://ftp.gnu.org/gnu/tramp/
fb7933a3
KG
53;;
54;; There's a mailing list for this, as well. Its name is:
340b8d4f
MA
55;; tramp-devel@gnu.org
56;; You can use the Web to subscribe, under the following URL:
57;; http://lists.gnu.org/mailman/listinfo/tramp-devel
fb7933a3
KG
58;;
59;; For the adventurous, the current development sources are available
60;; via CVS. You can find instructions about this at the following URL:
c62c9d08 61;; http://savannah.gnu.org/projects/tramp/
fb7933a3
KG
62;; Click on "CVS" in the navigation bar near the top.
63;;
64;; Don't forget to put on your asbestos longjohns, first!
65
66;;; Code:
67
b1a2b924
KG
68;; The Tramp version number and bug report address, as prepared by configure.
69(require 'trampver)
a69c01a0
MA
70(add-hook 'tramp-unload-hook
71 '(lambda ()
72 (when (featurep 'trampver)
73 (unload-feature 'trampver 'force))))
74
75(if (featurep 'xemacs)
76 (require 'timer-funcs)
77 (require 'timer))
fb7933a3 78
fb7933a3 79(require 'format-spec) ;from Gnus 5.8, also in tar ball
5ec2cc41
KG
80;; As long as password.el is not part of (X)Emacs, it shouldn't
81;; be mandatory
82(if (featurep 'xemacs)
83 (load "password" 'noerror)
84 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
85
dba28077
KG
86;; The explicit check is not necessary in Emacs, which provides the
87;; feature even if implemented in C, but it appears to be necessary
88;; in XEmacs.
89(unless (and (fboundp 'base64-encode-region)
90 (fboundp 'base64-decode-region))
91 (require 'base64)) ;for the mimencode methods
fb7933a3
KG
92(require 'shell)
93(require 'advice)
94
16674e4f
KG
95(autoload 'tramp-uuencode-region "tramp-uu"
96 "Implementation of `uuencode' in Lisp.")
a69c01a0
MA
97(add-hook 'tramp-unload-hook
98 '(lambda ()
99 (when (featurep 'tramp-uu)
100 (unload-feature 'tramp-uu 'force))))
16674e4f
KG
101
102(unless (fboundp 'uudecode-decode-region)
103 (autoload 'uudecode-decode-region "uudecode"))
104
b25a52cc
KG
105;; XEmacs is distributed with few Lisp packages. Further packages are
106;; installed using EFS. If we use a unified filename format, then
107;; Tramp is required in addition to EFS. (But why can't Tramp just
108;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
109;; just like before.) Another reason for using a separate filename
110;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
111;; Tramp only knows how to deal with `file-name-handler-alist', not
112;; the other places.
113;;;###autoload
114(defvar tramp-unified-filenames (not (featurep 'xemacs))
115 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
95bb178f 116Otherwise, use a separate filename syntax for Tramp.")
fb7933a3 117
4007ba5b
KG
118;; Load foreign methods. Because they do require Tramp internally, this
119;; must be done with the `eval-after-load' trick.
120
121;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
122(unless (featurep 'xemacs)
123 (eval-after-load "tramp"
a69c01a0
MA
124 '(progn
125 (require 'tramp-ftp)
126 (add-hook 'tramp-unload-hook
127 '(lambda ()
128 (when (featurep 'tramp-ftp)
129 (unload-feature 'tramp-ftp 'force)))))))
b25a52cc
KG
130(when (and tramp-unified-filenames (featurep 'xemacs))
131 (eval-after-load "tramp"
a69c01a0
MA
132 '(progn
133 (require 'tramp-efs)
134 (add-hook 'tramp-unload-hook
135 '(lambda ()
136 (when (featurep 'tramp-efs)
137 (unload-feature 'tramp-efs 'force)))))))
4007ba5b
KG
138
139;; tramp-smb uses "smbclient" from Samba.
140;; Not available under Cygwin and Windows, because they don't offer
141;; "smbclient". And even not necessary there, because Emacs supports
7432277c 142;; UNC file names like "//host/share/localname".
4007ba5b
KG
143(unless (memq system-type '(cygwin windows-nt))
144 (eval-after-load "tramp"
a69c01a0
MA
145 '(progn
146 (require 'tramp-smb)
147 (add-hook 'tramp-unload-hook
148 '(lambda ()
149 (when (featurep 'tramp-smb)
150 (unload-feature 'tramp-smb 'force)))))))
4007ba5b 151
d8d555f2
MA
152(require 'cl)
153(require 'custom)
154;; Emacs 19.34 compatibility hack -- is this needed?
155(or (>= emacs-major-version 20)
156 (load "cl-seq"))
fb7933a3
KG
157
158(unless (boundp 'custom-print-functions)
159 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
160
38c65fca 161;; Avoid byte-compiler warnings if the byte-compiler supports this.
8daea7fc
KG
162;; Currently, XEmacs supports this.
163(eval-when-compile
f9f51bbf 164 (when (featurep 'xemacs)
38c65fca
KG
165 (let (unused-vars) ; Pacify Emacs byte-compiler
166 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
167 (byte-compiler-options (warnings (- unused-vars))))))
168
169;; `directory-sep-char' is an obsolete variable in Emacs. But it is
170;; used in XEmacs, so we set it here and there. The following is needed
171;; to pacify Emacs byte-compiler.
172(eval-when-compile
173 (when (boundp 'byte-compile-not-obsolete-var)
174 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
175
fb7933a3
KG
176;;; User Customizable Internal Variables:
177
178(defgroup tramp nil
179 "Edit remote files with a combination of rsh and rcp or similar programs."
589d30dd 180 :group 'files
bf247b6e 181 :version "22.1")
fb7933a3 182
821e6e36 183(defcustom tramp-verbose 9
fb7933a3
KG
184 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
185 :group 'tramp
186 :type 'integer)
187
188(defcustom tramp-debug-buffer nil
189 "*Whether to send all commands and responses to a debug buffer."
190 :group 'tramp
191 :type 'boolean)
192
38c65fca
KG
193;; Emacs case
194(eval-and-compile
195 (when (boundp 'backup-directory-alist)
196 (defcustom tramp-backup-directory-alist nil
197 "Alist of filename patterns and backup directory names.
198Each element looks like (REGEXP . DIRECTORY), with the same meaning like
199in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
200is a local file name, the backup directory is prepended with Tramp file
201name prefix \(multi-method, method, user, host\) of file.
202
203\(setq tramp-backup-directory-alist backup-directory-alist\)
204
205gives the same backup policy for Tramp files on their hosts like the
206policy for local files."
207 :group 'tramp
208 :type '(repeat (cons (regexp :tag "Regexp matching filename")
209 (directory :tag "Backup directory name"))))))
210
211;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
212;; the package "backup-dir" might not be loaded yet.
213(eval-and-compile
214 (when (featurep 'xemacs)
215 (defcustom tramp-bkup-backup-directory-info nil
216 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
217It has the same meaning like `bkup-backup-directory-info' from package
218`backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
219file name, the backup directory is prepended with Tramp file name prefix
220\(multi-method, method, user, host\) of file.
221
222\(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
223
224gives the same backup policy for Tramp files on their hosts like the
225policy for local files."
bf247b6e 226 :type '(repeat
38c65fca
KG
227 (list (regexp :tag "File regexp")
228 (string :tag "Backup Dir")
229 (set :inline t
230 (const ok-create)
231 (const full-path)
232 (const prepend-name)
233 (const search-upward))))
234 :group 'tramp)))
235
fb7933a3
KG
236(defcustom tramp-auto-save-directory nil
237 "*Put auto-save files in this directory, if set.
238The idea is to use a local directory so that auto-saving is faster."
239 :group 'tramp
240 :type '(choice (const nil)
241 string))
242
16674e4f
KG
243(defcustom tramp-encoding-shell
244 (if (memq system-type '(windows-nt))
245 (getenv "COMSPEC")
246 "/bin/sh")
247 "*Use this program for encoding and decoding commands on the local host.
248This shell is used to execute the encoding and decoding command on the
249local host, so if you want to use `~' in those commands, you should
250choose a shell here which groks tilde expansion. `/bin/sh' normally
251does not understand tilde expansion.
252
253For encoding and deocding, commands like the following are executed:
254
255 /bin/sh -c COMMAND < INPUT > OUTPUT
256
257This variable can be used to change the \"/bin/sh\" part. See the
258variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
259variable `tramp-encoding-reads-stdin' to specify whether the commands read
260standard input or a file.
fb7933a3
KG
261
262Note that this variable is not used for remote commands. There are
263mechanisms in tramp.el which automatically determine the right shell to
264use for the remote host."
265 :group 'tramp
266 :type '(file :must-match t))
267
16674e4f
KG
268(defcustom tramp-encoding-command-switch
269 (if (string-match "cmd\\.exe" tramp-encoding-shell)
270 "/c"
271 "-c")
272 "*Use this switch together with `tramp-encoding-shell' for local commands.
273See the variable `tramp-encoding-shell' for more information."
274 :group 'tramp
275 :type 'string)
276
277(defcustom tramp-encoding-reads-stdin t
278 "*If non-nil, encoding commands read from standard input.
279If nil, the filename is the last argument.
280
281Note that the commands always must write to standard output."
282 :group 'tramp
283 :type 'boolean)
284
90dc758d 285(defcustom tramp-multi-sh-program
16674e4f 286 tramp-encoding-shell
90dc758d 287 "*Use this program for bootstrapping multi-hop connections.
16674e4f 288This variable is similar to `tramp-encoding-shell', but it is only used
90dc758d
KG
289when initializing a multi-hop connection. Therefore, the set of
290commands sent to this shell is quite restricted, and if you are
291careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
292shell which does not normally exist on Windows anyway).
293
294To use multi-hop methods from Windows, you also need suitable entries
295in `tramp-multi-connection-function-alist' for the first hop.
296
16674e4f 297This variable defaults to the value of `tramp-encoding-shell'."
90dc758d
KG
298 :group 'tramp
299 :type '(file :must-match t))
300
fb7933a3
KG
301;; CCC I have changed all occurrences of comint-quote-filename with
302;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
303;; There, comint-quote-filename was removed altogether. If it turns
304;; out to be necessary there, something will need to be done.
305;;-(defcustom tramp-file-name-quote-list
306;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
307;;- "*Protect these characters from the remote shell.
308;;-Any character in this list is quoted (preceded with a backslash)
309;;-because it means something special to the shell. This takes effect
310;;-when sending file and directory names to the remote shell.
311;;-
312;;-See `comint-file-name-quote-list' for details."
313;;- :group 'tramp
314;;- :type '(repeat character))
315
316(defcustom tramp-methods
317 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
318 (tramp-login-program "rsh")
319 (tramp-copy-program "rcp")
fb7933a3 320 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
321 (tramp-login-args nil)
322 (tramp-copy-args nil)
90f8dc03
KG
323 (tramp-copy-keep-date-arg "-p")
324 (tramp-password-end-of-line nil))
fb7933a3 325 ("scp" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
326 (tramp-login-program "ssh")
327 (tramp-copy-program "scp")
fb7933a3 328 (tramp-remote-sh "/bin/sh")
572b79d8
MA
329 (tramp-login-args ("-e" "none"))
330 (tramp-copy-args nil)
90f8dc03
KG
331 (tramp-copy-keep-date-arg "-p")
332 (tramp-password-end-of-line nil))
fb7933a3 333 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
334 (tramp-login-program "ssh")
335 (tramp-copy-program "scp")
90dc758d 336 (tramp-remote-sh "/bin/sh")
572b79d8
MA
337 (tramp-login-args ("-1" "-e" "none"))
338 (tramp-copy-args ("-1"))
90f8dc03
KG
339 (tramp-copy-keep-date-arg "-p")
340 (tramp-password-end-of-line nil))
90dc758d 341 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
342 (tramp-login-program "ssh")
343 (tramp-copy-program "scp")
90dc758d 344 (tramp-remote-sh "/bin/sh")
572b79d8
MA
345 (tramp-login-args ("-2" "-e" "none"))
346 (tramp-copy-args ("-2"))
90f8dc03
KG
347 (tramp-copy-keep-date-arg "-p")
348 (tramp-password-end-of-line nil))
3b89d388 349 ("scp1_old"
ac474af1 350 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
351 (tramp-login-program "ssh1")
352 (tramp-copy-program "scp1")
fb7933a3 353 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
354 (tramp-login-args ("-e" "none"))
355 (tramp-copy-args nil)
90f8dc03
KG
356 (tramp-copy-keep-date-arg "-p")
357 (tramp-password-end-of-line nil))
3b89d388 358 ("scp2_old"
ac474af1 359 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
360 (tramp-login-program "ssh2")
361 (tramp-copy-program "scp2")
fb7933a3 362 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
363 (tramp-login-args ("-e" "none"))
364 (tramp-copy-args nil)
90f8dc03
KG
365 (tramp-copy-keep-date-arg "-p")
366 (tramp-password-end-of-line nil))
fb7933a3 367 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
368 (tramp-login-program "ssh")
369 (tramp-copy-program "rsync")
370 (tramp-remote-sh "/bin/sh")
371 (tramp-login-args ("-e" "none"))
372 (tramp-copy-args ("-e" "ssh"))
90f8dc03
KG
373 (tramp-copy-keep-date-arg "-t")
374 (tramp-password-end-of-line nil))
b25a52cc
KG
375 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
376 (tramp-login-program "remsh")
377 (tramp-copy-program "rcp")
fb7933a3 378 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
379 (tramp-login-args nil)
380 (tramp-copy-args nil)
90f8dc03
KG
381 (tramp-copy-keep-date-arg "-p")
382 (tramp-password-end-of-line nil))
ac474af1 383 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
384 (tramp-login-program "rsh")
385 (tramp-copy-program nil)
fb7933a3 386 (tramp-remote-sh "/bin/sh")
b25a52cc 387 (tramp-login-args nil)
ea9d1443 388 (tramp-copy-args nil)
90f8dc03
KG
389 (tramp-copy-keep-date-arg nil)
390 (tramp-password-end-of-line nil))
ac474af1 391 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
392 (tramp-login-program "ssh")
393 (tramp-copy-program nil)
fb7933a3 394 (tramp-remote-sh "/bin/sh")
b25a52cc 395 (tramp-login-args ("-e" "none"))
ea9d1443 396 (tramp-copy-args nil)
90f8dc03
KG
397 (tramp-copy-keep-date-arg nil)
398 (tramp-password-end-of-line nil))
ac474af1 399 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
400 (tramp-login-program "ssh")
401 (tramp-copy-program nil)
90dc758d 402 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
403 (tramp-login-args ("-1" "-e" "none"))
404 (tramp-copy-args ("-1"))
90f8dc03
KG
405 (tramp-copy-keep-date-arg nil)
406 (tramp-password-end-of-line nil))
ac474af1 407 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
408 (tramp-login-program "ssh")
409 (tramp-copy-program nil)
90dc758d 410 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
411 (tramp-login-args ("-2" "-e" "none"))
412 (tramp-copy-args ("-2"))
90f8dc03
KG
413 (tramp-copy-keep-date-arg nil)
414 (tramp-password-end-of-line nil))
3b89d388 415 ("ssh1_old"
ac474af1 416 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
417 (tramp-login-program "ssh1")
418 (tramp-copy-program nil)
fb7933a3 419 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
420 (tramp-login-args ("-e" "none"))
421 (tramp-copy-args nil)
90f8dc03
KG
422 (tramp-copy-keep-date-arg nil)
423 (tramp-password-end-of-line nil))
3b89d388 424 ("ssh2_old"
ac474af1 425 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
426 (tramp-login-program "ssh2")
427 (tramp-copy-program nil)
428 (tramp-remote-sh "/bin/sh")
429 (tramp-login-args ("-e" "none"))
430 (tramp-copy-args nil)
90f8dc03
KG
431 (tramp-copy-keep-date-arg nil)
432 (tramp-password-end-of-line nil))
b25a52cc
KG
433 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
434 (tramp-login-program "remsh")
435 (tramp-copy-program nil)
fb7933a3 436 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
437 (tramp-login-args nil)
438 (tramp-copy-args nil)
90f8dc03
KG
439 (tramp-copy-keep-date-arg nil)
440 (tramp-password-end-of-line nil))
ac474af1
KG
441 ("telnet"
442 (tramp-connection-function tramp-open-connection-telnet)
b25a52cc
KG
443 (tramp-login-program "telnet")
444 (tramp-copy-program nil)
fb7933a3 445 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
446 (tramp-login-args nil)
447 (tramp-copy-args nil)
90f8dc03
KG
448 (tramp-copy-keep-date-arg nil)
449 (tramp-password-end-of-line nil))
ac474af1 450 ("su" (tramp-connection-function tramp-open-connection-su)
b25a52cc
KG
451 (tramp-login-program "su")
452 (tramp-copy-program nil)
fb7933a3 453 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
454 (tramp-login-args ("-" "%u"))
455 (tramp-copy-args nil)
90f8dc03
KG
456 (tramp-copy-keep-date-arg nil)
457 (tramp-password-end-of-line nil))
ac474af1 458 ("sudo" (tramp-connection-function tramp-open-connection-su)
b25a52cc
KG
459 (tramp-login-program "sudo")
460 (tramp-copy-program nil)
fb7933a3 461 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
462 (tramp-login-args ("-u" "%u" "-s"
463 "-p" "Password:"))
464 (tramp-copy-args nil)
90f8dc03
KG
465 (tramp-copy-keep-date-arg nil)
466 (tramp-password-end-of-line nil))
fb7933a3 467 ("multi" (tramp-connection-function tramp-open-connection-multi)
b25a52cc
KG
468 (tramp-login-program nil)
469 (tramp-copy-program nil)
fb7933a3 470 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
471 (tramp-login-args nil)
472 (tramp-copy-args nil)
90f8dc03
KG
473 (tramp-copy-keep-date-arg nil)
474 (tramp-password-end-of-line nil))
572b79d8
MA
475 ("scpc" (tramp-connection-function tramp-open-connection-rsh)
476 (tramp-login-program "ssh")
477 (tramp-copy-program "scp")
478 (tramp-remote-sh "/bin/sh")
479 (tramp-login-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
480 "-o" "ControlMaster=yes"
481 "-e" "none"))
482 (tramp-copy-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
483 "-o" "ControlMaster=auto"))
484 (tramp-copy-keep-date-arg "-p")
485 (tramp-password-end-of-line nil))
fb7933a3 486 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
487 (tramp-login-program "ssh")
488 (tramp-copy-program "scp")
fb7933a3 489 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
490 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
491 (tramp-copy-args nil)
90f8dc03
KG
492 (tramp-copy-keep-date-arg "-p")
493 (tramp-password-end-of-line nil))
ac474af1 494 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
495 (tramp-login-program "ssh")
496 (tramp-copy-program nil)
fb7933a3 497 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
498 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
499 (tramp-copy-args nil)
90f8dc03
KG
500 (tramp-copy-keep-date-arg nil)
501 (tramp-password-end-of-line nil))
ac474af1 502 ("krlogin"
fb7933a3 503 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
504 (tramp-login-program "krlogin")
505 (tramp-copy-program nil)
fb7933a3 506 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
507 (tramp-login-args ("-x"))
508 (tramp-copy-args nil)
90f8dc03
KG
509 (tramp-copy-keep-date-arg nil)
510 (tramp-password-end-of-line nil))
ac474af1 511 ("plink"
fb7933a3 512 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
513 (tramp-login-program "plink")
514 (tramp-copy-program nil)
fb7933a3 515 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
516 (tramp-login-args ("-ssh")) ;optionally add "-v"
517 (tramp-copy-args nil)
90f8dc03
KG
518 (tramp-copy-keep-date-arg nil)
519 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
b25a52cc
KG
520 ("plink1"
521 (tramp-connection-function tramp-open-connection-rsh)
522 (tramp-login-program "plink")
523 (tramp-copy-program nil)
524 (tramp-remote-sh "/bin/sh")
525 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
526 (tramp-copy-args nil)
90f8dc03
KG
527 (tramp-copy-keep-date-arg nil)
528 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
fb7933a3
KG
529 ("pscp"
530 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
531 (tramp-login-program "plink")
532 (tramp-copy-program "pscp")
fb7933a3 533 (tramp-remote-sh "/bin/sh")
b25a52cc
KG
534 (tramp-login-args ("-ssh"))
535 (tramp-copy-args nil)
90f8dc03
KG
536 (tramp-copy-keep-date-arg "-p")
537 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
bf247b6e 538 ("fcp"
fb7933a3 539 (tramp-connection-function tramp-open-connection-rsh)
b25a52cc
KG
540 (tramp-login-program "fsh")
541 (tramp-copy-program "fcp")
fb7933a3 542 (tramp-remote-sh "/bin/sh -i")
b25a52cc
KG
543 (tramp-login-args ("sh" "-i"))
544 (tramp-copy-args nil)
90f8dc03
KG
545 (tramp-copy-keep-date-arg "-p")
546 (tramp-password-end-of-line nil))
fb7933a3
KG
547 )
548 "*Alist of methods for remote files.
549This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
550Each NAME stands for a remote access method. Each PARAM is a
551pair of the form (KEY VALUE). The following KEYs are defined:
16674e4f 552 * `tramp-connection-function'
fb7933a3
KG
553 This specifies the function to use to connect to the remote host.
554 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
555 and `tramp-open-connection-su' are defined. See the documentation
556 of these functions for more details.
557 * `tramp-remote-sh'
558 This specifies the Bourne shell to use on the remote host. This
559 MUST be a Bourne-like shell. It is normally not necessary to set
560 this to any value other than \"/bin/sh\": tramp wants to use a shell
561 which groks tilde expansion, but it can search for it. Also note
562 that \"/bin/sh\" exists on all Unixen, this might not be true for
563 the value that you decide to use. You Have Been Warned.
b25a52cc
KG
564 * `tramp-login-program'
565 This specifies the name of the program to use for logging in to the
566 remote host. Depending on `tramp-connection-function', this may be
567 the name of rsh or a workalike program (when
568 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
569 name of telnet or a workalike (for `tramp-open-connection-telnet'),
570 or the name of su or a workalike (for `tramp-open-connection-su').
571 * `tramp-login-args'
fb7933a3
KG
572 This specifies the list of arguments to pass to the above
573 mentioned program. Please note that this is a list of arguments,
574 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
575 here. Instead, you want two list elements, one for \"-a\" and one
576 for \"-b\", or one for \"-f\" and one for \"foo\".
b25a52cc
KG
577 If `tramp-connection-function' is `tramp-open-connection-su', then
578 \"%u\" in this list is replaced by the user name, and \"%%\" can
579 be used to obtain a literal percent character.
57176422 580 \"%t\" is replaced by the temporary file name for `scp'-like methods.
b25a52cc
KG
581 * `tramp-copy-program'
582 This specifies the name of the program to use for remotely copying
583 the file; this might be the absolute filename of rcp or the name of
584 a workalike program.
585 * `tramp-copy-args'
fb7933a3 586 This specifies the list of parameters to pass to the above mentioned
b25a52cc
KG
587 program, the hints for `tramp-login-args' also apply here.
588 * `tramp-copy-keep-date-arg'
589 This specifies the parameter to use for the copying program when the
590 timestamp of the original file should be kept. For `rcp', use `-p', for
fb7933a3 591 `rsync', use `-t'.
90f8dc03
KG
592 * `tramp-password-end-of-line'
593 This specifies the string to use for terminating the line after
594 submitting the password. If this method parameter is nil, then the
595 value of the normal variable `tramp-default-password-end-of-line'
596 is used. This parameter is necessary because the \"plink\" program
597 requires any two characters after sending the password. These do
598 not have to be newline or carriage return characters. Other login
599 programs are happy with just one character, the newline character.
600 We use \"xy\" as the value for methods using \"plink\".
b25a52cc
KG
601
602What does all this mean? Well, you should specify `tramp-login-program'
603for all methods; this program is used to log in to the remote site. Then,
604there are two ways to actually transfer the files between the local and the
605remote side. One way is using an additional rcp-like program. If you want
606to do this, set `tramp-copy-program' in the method.
fb7933a3
KG
607
608Another possibility for file transfer is inline transfer, i.e. the
b25a52cc 609file is passed through the same buffer used by `tramp-login-program'. In
fb7933a3 610this case, the file contents need to be protected since the
b25a52cc 611`tramp-login-program' might use escape codes or the connection might not
fb7933a3 612be eight-bit clean. Therefore, file contents are encoded for transit.
16674e4f 613See the variable `tramp-coding-commands' for details.
fb7933a3 614
16674e4f 615So, to summarize: if the method is an out-of-band method, then you
b25a52cc 616must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
16674e4f
KG
617inline method, then these two parameters should be nil. Every method,
618inline or out of band, must specify `tramp-connection-function' plus
b25a52cc 619the associated arguments (for example, the login program if you chose
fb7933a3
KG
620`tramp-open-connection-telnet').
621
622Notes:
623
624When using `tramp-open-connection-su' the phrase `open connection to a
625remote host' sounds strange, but it is used nevertheless, for
626consistency. No connection is opened to a remote host, but `su' is
627started on the local host. You are not allowed to specify a remote
16674e4f 628host other than `localhost' or the name of the local host."
fb7933a3
KG
629 :group 'tramp
630 :type '(repeat
631 (cons string
632 (set (list (const tramp-connection-function) function)
b25a52cc 633 (list (const tramp-login-program)
fb7933a3 634 (choice (const nil) string))
b25a52cc 635 (list (const tramp-copy-program)
fb7933a3
KG
636 (choice (const nil) string))
637 (list (const tramp-remote-sh)
638 (choice (const nil) string))
b25a52cc
KG
639 (list (const tramp-login-args) (repeat string))
640 (list (const tramp-copy-args) (repeat string))
641 (list (const tramp-copy-keep-date-arg)
fb7933a3 642 (choice (const nil) string))
fb7933a3
KG
643 (list (const tramp-encoding-command)
644 (choice (const nil) string))
645 (list (const tramp-decoding-command)
646 (choice (const nil) string))
647 (list (const tramp-encoding-function)
648 (choice (const nil) function))
649 (list (const tramp-decoding-function)
90f8dc03
KG
650 (choice (const nil) function))
651 (list (const tramp-password-end-of-line)
652 (choice (const nil) string))))))
fb7933a3
KG
653
654(defcustom tramp-multi-methods '("multi" "multiu")
655 "*List of multi-hop methods.
656Each entry in this list should be a method name as mentioned in the
657variable `tramp-methods'."
658 :group 'tramp
659 :type '(repeat string))
660
661(defcustom tramp-multi-connection-function-alist
662 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
663 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
b25a52cc 664 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
fb7933a3 665 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
bf247b6e 666 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
fb7933a3 667 ("su" tramp-multi-connect-su "su - %u%n")
b25a52cc 668 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
fb7933a3
KG
669 "*List of connection functions for multi-hop methods.
670Each list item is a list of three items (METHOD FUNCTION COMMAND),
671where METHOD is the name as used in the file name, FUNCTION is the
672function to be executed, and COMMAND is the shell command used for
673connecting.
674
675COMMAND may contain percent escapes. `%u' will be replaced with the
676user name, `%h' will be replaced with the host name, and `%n' will be
677replaced with an end-of-line character, as specified in the variable
678`tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
679Note that the interpretation of the percent escapes also depends on
680the FUNCTION. For example, the `%u' escape is forbidden with the
681function `tramp-multi-connect-telnet'. See the documentation of the
682various functions for details."
683 :group 'tramp
684 :type '(repeat (list string function string)))
685
b25a52cc 686(defcustom tramp-default-method
572b79d8 687 (cond
210a7c2c
MA
688 ;; An external copy method seems to be preferred, because it is
689 ;; much more performant for large files, and it hasn't too serious
690 ;; delays for small files. But it must be ensured that there
572b79d8
MA
691 ;; aren't permanent password queries. Either a password agent like
692 ;; "ssh-agent" or "Pageant" shall run, or the optional password.el
693 ;; package shall be active for password caching.
694 ((executable-find "pscp")
695 ;; PuTTY is installed.
696 (if (or (fboundp 'password-read)
697 ;; Pageant is running.
698 (and (fboundp 'w32-window-exists-p)
699 (funcall (symbol-function 'w32-window-exists-p)
700 "Pageant" "Pageant")))
701 "pscp"
702 "plink"))
703 ;; There is an ssh installation.
704 ((executable-find "scp")
705 (if (or (fboundp 'password-read)
706 ;; ssh-agent is running.
707 (getenv "SSH_AUTH_SOCK")
bb8792de 708 (getenv "SSH_AGENT_PID"))
572b79d8
MA
709 "scp"
710 "ssh"))
711 ;; Fallback.
712 (t "ftp"))
fb7933a3 713 "*Default method to use for transferring files.
c62c9d08 714See `tramp-methods' for possibilities.
4007ba5b 715Also see `tramp-default-method-alist'."
c62c9d08
KG
716 :group 'tramp
717 :type 'string)
718
505edaeb 719(defcustom tramp-default-method-alist
4007ba5b 720 '(("\\`localhost\\'" "\\`root\\'" "su"))
c62c9d08
KG
721 "*Default method to use for specific user/host pairs.
722This is an alist of items (HOST USER METHOD). The first matching item
723specifies the method to use for a file name which does not specify a
724method. HOST and USER are regular expressions or nil, which is
725interpreted as a regular expression which always matches. If no entry
726matches, the variable `tramp-default-method' takes effect.
727
728If the file name does not specify the user, lookup is done using the
729empty string for the user name.
730
731See `tramp-methods' for a list of possibilities for METHOD."
732 :group 'tramp
733 :type '(repeat (list (regexp :tag "Host regexp")
734 (regexp :tag "User regexp")
735 (string :tag "Method"))))
736
16674e4f
KG
737;; Default values for non-Unices seeked
738(defconst tramp-completion-function-alist-rsh
739 (unless (memq system-type '(windows-nt))
740 '((tramp-parse-rhosts "/etc/hosts.equiv")
741 (tramp-parse-rhosts "~/.rhosts")))
b25a52cc 742 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
16674e4f
KG
743
744;; Default values for non-Unices seeked
745(defconst tramp-completion-function-alist-ssh
746 (unless (memq system-type '(windows-nt))
5ec2cc41
KG
747 '((tramp-parse-rhosts "/etc/hosts.equiv")
748 (tramp-parse-rhosts "/etc/shosts.equiv")
749 (tramp-parse-shosts "/etc/ssh_known_hosts")
750 (tramp-parse-sconfig "/etc/ssh_config")
751 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
752 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
753 (tramp-parse-rhosts "~/.rhosts")
754 (tramp-parse-rhosts "~/.shosts")
755 (tramp-parse-shosts "~/.ssh/known_hosts")
756 (tramp-parse-sconfig "~/.ssh/config")
757 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
758 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
b25a52cc 759 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
16674e4f
KG
760
761;; Default values for non-Unices seeked
762(defconst tramp-completion-function-alist-telnet
763 (unless (memq system-type '(windows-nt))
764 '((tramp-parse-hosts "/etc/hosts")))
b25a52cc 765 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
16674e4f
KG
766
767;; Default values for non-Unices seeked
768(defconst tramp-completion-function-alist-su
769 (unless (memq system-type '(windows-nt))
770 '((tramp-parse-passwd "/etc/passwd")))
b25a52cc 771 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
292ffc15 772
5ec2cc41 773(defvar tramp-completion-function-alist nil
16674e4f
KG
774 "*Alist of methods for remote files.
775This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
776Each NAME stands for a remote access method. Each PAIR is of the form
777\(FUNCTION FILE). FUNCTION is responsible to extract user names and host
778names from FILE for completion. The following predefined FUNCTIONs exists:
779
5ec2cc41
KG
780 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
781 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
782 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
783 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
784 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
785 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
786 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
787 * `tramp-parse-netrc' for \"~/.netrc\" like files.
788
789FUNCTION can also be a customer defined function. For more details see
790the info pages.")
791
792(eval-after-load "tramp"
793 '(progn
794 (tramp-set-completion-function
795 "rcp" tramp-completion-function-alist-rsh)
796 (tramp-set-completion-function
797 "scp" tramp-completion-function-alist-ssh)
798 (tramp-set-completion-function
799 "scp1" tramp-completion-function-alist-ssh)
800 (tramp-set-completion-function
801 "scp2" tramp-completion-function-alist-ssh)
802 (tramp-set-completion-function
803 "scp1_old" tramp-completion-function-alist-ssh)
804 (tramp-set-completion-function
805 "scp2_old" tramp-completion-function-alist-ssh)
806 (tramp-set-completion-function
807 "rsync" tramp-completion-function-alist-rsh)
808 (tramp-set-completion-function
809 "remcp" tramp-completion-function-alist-rsh)
810 (tramp-set-completion-function
811 "rsh" tramp-completion-function-alist-rsh)
812 (tramp-set-completion-function
813 "ssh" tramp-completion-function-alist-ssh)
814 (tramp-set-completion-function
815 "ssh1" tramp-completion-function-alist-ssh)
816 (tramp-set-completion-function
817 "ssh2" tramp-completion-function-alist-ssh)
818 (tramp-set-completion-function
819 "ssh1_old" tramp-completion-function-alist-ssh)
820 (tramp-set-completion-function
821 "ssh2_old" tramp-completion-function-alist-ssh)
822 (tramp-set-completion-function
823 "remsh" tramp-completion-function-alist-rsh)
824 (tramp-set-completion-function
825 "telnet" tramp-completion-function-alist-telnet)
826 (tramp-set-completion-function
827 "su" tramp-completion-function-alist-su)
828 (tramp-set-completion-function
829 "sudo" tramp-completion-function-alist-su)
830 (tramp-set-completion-function
831 "multi" nil)
bf247b6e 832 (tramp-set-completion-function
5ec2cc41
KG
833 "scpx" tramp-completion-function-alist-ssh)
834 (tramp-set-completion-function
835 "sshx" tramp-completion-function-alist-ssh)
836 (tramp-set-completion-function
837 "krlogin" tramp-completion-function-alist-rsh)
838 (tramp-set-completion-function
839 "plink" tramp-completion-function-alist-ssh)
840 (tramp-set-completion-function
841 "plink1" tramp-completion-function-alist-ssh)
842 (tramp-set-completion-function
843 "pscp" tramp-completion-function-alist-ssh)
844 (tramp-set-completion-function
845 "fcp" tramp-completion-function-alist-ssh)))
16674e4f 846
fb7933a3
KG
847(defcustom tramp-rsh-end-of-line "\n"
848 "*String used for end of line in rsh connections.
849I don't think this ever needs to be changed, so please tell me about it
16674e4f 850if you need to change this.
90f8dc03
KG
851Also see the method parameter `tramp-password-end-of-line' and the normal
852variable `tramp-default-password-end-of-line'."
16674e4f
KG
853 :group 'tramp
854 :type 'string)
855
90f8dc03
KG
856(defcustom tramp-default-password-end-of-line
857 tramp-rsh-end-of-line
16674e4f 858 "*String used for end of line after sending a password.
90f8dc03
KG
859This variable provides the default value for the method parameter
860`tramp-password-end-of-line', see `tramp-methods' for more details.
861
16674e4f
KG
862It seems that people using plink under Windows need to send
863\"\\r\\n\" (carriage-return, then newline) after a password, but just
864\"\\n\" after all other lines. This variable can be used for the
865password, see `tramp-rsh-end-of-line' for the other cases.
866
867The default value is to use the same value as `tramp-rsh-end-of-line'."
fb7933a3
KG
868 :group 'tramp
869 :type 'string)
870
871(defcustom tramp-remote-path
7dbde7dd
MA
872 ;; "/usr/xpg4/bin" has been placed first, because on Solaris a POSIX
873 ;; compatible "id" is needed.
874 '("/usr/xpg4/bin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
875 "/usr/ccs/bin" "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
fb7933a3
KG
876 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
877 "*List of directories to search for executables on remote host.
878Please notify me about other semi-standard directories to include here.
879
880You can use `~' in this list, but when searching for a shell which groks
881tilde expansion, all directory names starting with `~' will be ignored."
882 :group 'tramp
883 :type '(repeat string))
884
885(defcustom tramp-login-prompt-regexp
bc103d00 886 ".*ogin\\( .*\\)?: *"
fb7933a3 887 "*Regexp matching login-like prompts.
bc103d00
MA
888The regexp should match at end of buffer.
889
890Sometimes the prompt is reported to look like \"login as:\"."
fb7933a3
KG
891 :group 'tramp
892 :type 'regexp)
893
821e6e36 894(defcustom tramp-shell-prompt-pattern
ea9d1443 895 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
821e6e36
KG
896 "Regexp to match prompts from remote shell.
897Normally, Tramp expects you to configure `shell-prompt-pattern'
898correctly, but sometimes it happens that you are connecting to a
899remote host which sends a different kind of shell prompt. Therefore,
900Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
901and also things matched by this variable. The default value of this
b25a52cc 902variable is similar to the default value of `shell-prompt-pattern',
821e6e36
KG
903which should work well in many cases."
904 :group 'tramp
905 :type 'regexp)
906
fb7933a3 907(defcustom tramp-password-prompt-regexp
11948172 908 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
fb7933a3 909 "*Regexp matching password-like prompts.
ac474af1 910The regexp should match at end of buffer.
fb7933a3
KG
911
912The `sudo' program appears to insert a `^@' character into the prompt."
913 :group 'tramp
914 :type 'regexp)
915
916(defcustom tramp-wrong-passwd-regexp
b1d06e75
KG
917 (concat "^.*"
918 ;; These strings should be on the last line
919 (regexp-opt '("Permission denied."
920 "Login incorrect"
921 "Login Incorrect"
922 "Connection refused"
923 "Connection closed"
924 "Sorry, try again."
925 "Name or service not known"
926 "Host key verification failed.") t)
927 ".*"
928 "\\|"
929 "^.*\\("
930 ;; Here comes a list of regexes, separated by \\|
931 "Received signal [0-9]+"
932 "\\).*")
fb7933a3 933 "*Regexp matching a `login failed' message.
ac474af1
KG
934The regexp should match at end of buffer."
935 :group 'tramp
936 :type 'regexp)
937
938(defcustom tramp-yesno-prompt-regexp
3cdaec13
KG
939 (concat
940 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
941 "\\s-*")
942 "Regular expression matching all yes/no queries which need to be confirmed.
ac474af1 943The confirmation should be done with yes or no.
3cdaec13
KG
944The regexp should match at end of buffer.
945See also `tramp-yn-prompt-regexp'."
fb7933a3
KG
946 :group 'tramp
947 :type 'regexp)
948
3cdaec13
KG
949(defcustom tramp-yn-prompt-regexp
950 (concat (regexp-opt '("Store key in cache? (y/n)") t)
951 "\\s-*")
952 "Regular expression matching all y/n queries which need to be confirmed.
953The confirmation should be done with y or n.
954The regexp should match at end of buffer.
955See also `tramp-yesno-prompt-regexp'."
956 :group 'tramp
957 :type 'regexp)
487f4fb7
KG
958
959(defcustom tramp-terminal-prompt-regexp
960 (concat "\\("
961 "TERM = (.*)"
962 "\\|"
963 "Terminal type\\? \\[.*\\]"
964 "\\)\\s-*")
965 "Regular expression matching all terminal setting prompts.
966The regexp should match at end of buffer.
967The answer will be provided by `tramp-action-terminal', which see."
968 :group 'tramp
969 :type 'regexp)
3cdaec13 970
01917a18
MA
971(defcustom tramp-operation-not-permitted-regexp
972 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
973 (regexp-opt '("Operation not permitted") t))
974 "Regular expression matching keep-date problems in (s)cp operations.
975Copying has been performed successfully already, so this message can
976be ignored safely."
977 :group 'tramp
978 :type 'regexp)
979
6b2633cc
LH
980(defcustom tramp-copy-failed-regexp
981 (concat "\\(.+: "
982 (regexp-opt '("Permission denied"
983 "not a regular file"
984 "is a directory"
985 "No such file or directory") t)
986 "\\)\\s-*")
987 "Regular expression matching copy problems in (s)cp operations."
988 :group 'tramp
989 :type 'regexp)
990
19a87064 991(defcustom tramp-process-alive-regexp
38c65fca 992 ""
19a87064 993 "Regular expression indicating a process has finished.
38c65fca
KG
994In fact this expression is empty by intention, it will be used only to
995check regularly the status of the associated process.
07dfe738
KG
996The answer will be provided by `tramp-action-process-alive',
997`tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
38c65fca
KG
998 :group 'tramp
999 :type 'regexp)
1000
fb7933a3
KG
1001(defcustom tramp-temp-name-prefix "tramp."
1002 "*Prefix to use for temporary files.
1003If this is a relative file name (such as \"tramp.\"), it is considered
1004relative to the directory name returned by the function
1005`tramp-temporary-file-directory' (which see). It may also be an
1006absolute file name; don't forget to include a prefix for the filename
1007part, though."
1008 :group 'tramp
1009 :type 'string)
1010
1011(defcustom tramp-discard-garbage nil
1012 "*If non-nil, try to discard garbage sent by remote shell.
1013Some shells send such garbage upon connection setup."
1014 :group 'tramp
1015 :type 'boolean)
1016
4007ba5b 1017(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
c62c9d08
KG
1018 "*Alist specifying extra arguments to pass to the remote shell.
1019Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1020matching the shell file name and ARGS is a string specifying the
1021arguments.
1022
1023This variable is only used when Tramp needs to start up another shell
1024for tilde expansion. The extra arguments should typically prevent the
1025shell from reading its init file."
1026 :group 'tramp
90f8dc03
KG
1027 ;; This might be the wrong way to test whether the widget type
1028 ;; `alist' is available. Who knows the right way to test it?
1029 :type (if (get 'alist 'widget-type)
1030 '(alist :key-type string :value-type string)
1031 '(repeat (cons string string))))
c62c9d08 1032
16674e4f
KG
1033(defcustom tramp-prefix-format
1034 (if tramp-unified-filenames "/" "/[")
1035 "*String matching the very beginning of tramp file names.
1036Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1037 :group 'tramp
1038 :type 'string)
1039
1040(defcustom tramp-prefix-regexp
1041 (concat "^" (regexp-quote tramp-prefix-format))
1042 "*Regexp matching the very beginning of tramp file names.
1043Should always start with \"^\". Derived from `tramp-prefix-format'."
1044 :group 'tramp
1045 :type 'regexp)
1046
1047(defcustom tramp-method-regexp
1048 "[a-zA-Z_0-9-]+"
1049 "*Regexp matching methods identifiers."
1050 :group 'tramp
1051 :type 'regexp)
1052
1053;; It is a little bit annoying that in XEmacs case this delimeter is different
1054;; for single-hop and multi-hop cases.
1055(defcustom tramp-postfix-single-method-format
1056 (if tramp-unified-filenames ":" "/")
1057 "*String matching delimeter between method and user or host names.
1058Applicable for single-hop methods.
1059Used in `tramp-make-tramp-file-name'."
1060 :group 'tramp
1061 :type 'string)
1062
1063(defcustom tramp-postfix-single-method-regexp
1064 (regexp-quote tramp-postfix-single-method-format)
1065 "*Regexp matching delimeter between method and user or host names.
1066Applicable for single-hop methods.
1067Derived from `tramp-postfix-single-method-format'."
1068 :group 'tramp
1069 :type 'regexp)
1070
1071(defcustom tramp-postfix-multi-method-format
1072 ":"
1073 "*String matching delimeter between method and user or host names.
1074Applicable for multi-hop methods.
1075Used in `tramp-make-tramp-multi-file-name'."
1076 :group 'tramp
1077 :type 'string)
1078
1079(defcustom tramp-postfix-multi-method-regexp
1080 (regexp-quote tramp-postfix-multi-method-format)
1081 "*Regexp matching delimeter between method and user or host names.
1082Applicable for multi-hop methods.
1083Derived from `tramp-postfix-multi-method-format'."
1084 :group 'tramp
1085 :type 'regexp)
fb7933a3 1086
16674e4f
KG
1087(defcustom tramp-postfix-multi-hop-format
1088 (if tramp-unified-filenames ":" "/")
7432277c 1089 "*String matching delimeter between host and next method.
16674e4f
KG
1090Applicable for multi-hop methods.
1091Used in `tramp-make-tramp-multi-file-name'."
1092 :group 'tramp
1093 :type 'string)
1094
1095(defcustom tramp-postfix-multi-hop-regexp
1096 (regexp-quote tramp-postfix-multi-hop-format)
7432277c 1097 "*Regexp matching delimeter between host and next method.
16674e4f
KG
1098Applicable for multi-hop methods.
1099Derived from `tramp-postfix-multi-hop-format'."
1100 :group 'tramp
1101 :type 'regexp)
1102
1103(defcustom tramp-user-regexp
d2a2c17f 1104 "[^:/ \t]*"
16674e4f
KG
1105 "*Regexp matching user names."
1106 :group 'tramp
1107 :type 'regexp)
1108
1109(defcustom tramp-postfix-user-format
1110 "@"
1111 "*String matching delimeter between user and host names.
1112Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1113 :group 'tramp
1114 :type 'string)
1115
1116(defcustom tramp-postfix-user-regexp
1117 (regexp-quote tramp-postfix-user-format)
1118 "*Regexp matching delimeter between user and host names.
1119Derived from `tramp-postfix-user-format'."
1120 :group 'tramp
1121 :type 'regexp)
1122
1123(defcustom tramp-host-regexp
487f4fb7
KG
1124 "[a-zA-Z0-9_.-]*"
1125 "*Regexp matching host names."
1126 :group 'tramp
1127 :type 'regexp)
1128
1129(defcustom tramp-host-with-port-regexp
3b89d388 1130 "[a-zA-Z0-9_.#-]*"
16674e4f
KG
1131 "*Regexp matching host names."
1132 :group 'tramp
1133 :type 'regexp)
1134
1135(defcustom tramp-postfix-host-format
1136 (if tramp-unified-filenames ":" "]")
7432277c 1137 "*String matching delimeter between host names and localnames.
16674e4f
KG
1138Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1139 :group 'tramp
1140 :type 'string)
1141
1142(defcustom tramp-postfix-host-regexp
1143 (regexp-quote tramp-postfix-host-format)
7432277c 1144 "*Regexp matching delimeter between host names and localnames.
16674e4f
KG
1145Derived from `tramp-postfix-host-format'."
1146 :group 'tramp
1147 :type 'regexp)
1148
7432277c 1149(defcustom tramp-localname-regexp
16674e4f 1150 ".*$"
7432277c 1151 "*Regexp matching localnames."
16674e4f
KG
1152 :group 'tramp
1153 :type 'regexp)
1154
1155;; File name format.
505edaeb
KG
1156
1157(defcustom tramp-file-name-structure
16674e4f
KG
1158 (list
1159 (concat
1160 tramp-prefix-regexp
1161 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
487f4fb7
KG
1162 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1163 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
7432277c 1164 "\\(" tramp-localname-regexp "\\)")
16674e4f
KG
1165 2 4 5 6)
1166
fb7933a3
KG
1167 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1168the tramp file name structure.
1169
1170The first element REGEXP is a regular expression matching a tramp file
1171name. The regex should contain parentheses around the method name,
1172the user name, the host name, and the file name parts.
1173
1174The second element METHOD is a number, saying which pair of
1175parentheses matches the method name. The third element USER is
1176similar, but for the user name. The fourth element HOST is similar,
1177but for the host name. The fifth element FILE is for the file name.
1178These numbers are passed directly to `match-string', which see. That
1179means the opening parentheses are counted to identify the pair.
1180
16674e4f 1181See also `tramp-file-name-regexp'."
fb7933a3
KG
1182 :group 'tramp
1183 :type '(list (regexp :tag "File name regexp")
1184 (integer :tag "Paren pair for method name")
1185 (integer :tag "Paren pair for user name ")
1186 (integer :tag "Paren pair for host name ")
1187 (integer :tag "Paren pair for file name ")))
1188
1189;;;###autoload
505edaeb
KG
1190(defconst tramp-file-name-regexp-unified
1191 "\\`/[^/:]+:"
1192 "Value for `tramp-file-name-regexp' for unified remoting.
1193Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1194Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1195
1196;;;###autoload
1197(defconst tramp-file-name-regexp-separate
1198 "\\`/\\[.*\\]"
1199 "Value for `tramp-file-name-regexp' for separate remoting.
1200XEmacs uses a separate filename syntax for Tramp and EFS.
1201See `tramp-file-name-structure-separate' for more explanations.")
1202
1203;;;###autoload
1204(defcustom tramp-file-name-regexp
16674e4f
KG
1205 (if tramp-unified-filenames
1206 tramp-file-name-regexp-unified
1207 tramp-file-name-regexp-separate)
fb7933a3
KG
1208 "*Regular expression matching file names handled by tramp.
1209This regexp should match tramp file names but no other file names.
1210\(When tramp.el is loaded, this regular expression is prepended to
1211`file-name-handler-alist', and that is searched sequentially. Thus,
1212if the tramp entry appears rather early in the `file-name-handler-alist'
1213and is a bit too general, then some files might be considered tramp
1214files which are not really tramp files.
1215
1216Please note that the entry in `file-name-handler-alist' is made when
1217this file (tramp.el) is loaded. This means that this variable must be set
1218before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1219updated after changing this variable.
1220
16674e4f 1221Also see `tramp-file-name-structure'."
fb7933a3
KG
1222 :group 'tramp
1223 :type 'regexp)
1224
16674e4f
KG
1225;;;###autoload
1226(defconst tramp-completion-file-name-regexp-unified
19a87064 1227 "^/$\\|^/[^/:][^/]*$"
16674e4f
KG
1228 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1229Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1230Tramp. See `tramp-file-name-structure-unified' for more explanations.")
fb7933a3 1231
16674e4f
KG
1232;;;###autoload
1233(defconst tramp-completion-file-name-regexp-separate
1234 "^/\\([[][^]]*\\)?$"
1235 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1236XEmacs uses a separate filename syntax for Tramp and EFS.
1237See `tramp-file-name-structure-separate' for more explanations.")
fb7933a3 1238
16674e4f
KG
1239;;;###autoload
1240(defcustom tramp-completion-file-name-regexp
1241 (if tramp-unified-filenames
1242 tramp-completion-file-name-regexp-unified
1243 tramp-completion-file-name-regexp-separate)
1244 "*Regular expression matching file names handled by tramp completion.
1245This regexp should match partial tramp file names only.
1246
1247Please note that the entry in `file-name-handler-alist' is made when
1248this file (tramp.el) is loaded. This means that this variable must be set
1249before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1250updated after changing this variable.
1251
1252Also see `tramp-file-name-structure'."
1253 :group 'tramp
1254 :type 'regexp)
505edaeb
KG
1255
1256(defcustom tramp-multi-file-name-structure
16674e4f
KG
1257 (list
1258 (concat
1259 tramp-prefix-regexp
1260 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1261 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
7432277c 1262 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
16674e4f 1263 2 3 -1)
fb7933a3
KG
1264 "*Describes the file name structure of `multi' files.
1265Multi files allow you to contact a remote host in several hops.
7432277c 1266This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
fb7933a3
KG
1267
1268The first element, REGEXP, gives a regular expression to match against
1269the file name. In this regular expression, `%s' is replaced with the
1270value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1271allow multiple hops, you normally want to use something like
1272\"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1273of parentheses is used for the HOP element, see below.)
1274
1275All remaining elements are numbers. METHOD gives the number of the
1276paren pair which matches the method name. HOP gives the number of the
7432277c
KG
1277paren pair which matches the hop sequence. LOCALNAME gives the number of
1278the paren pair which matches the localname (pathname) on the remote host.
fb7933a3 1279
7432277c 1280LOCALNAME can also be negative, which means to count from the end. Ie, a
fb7933a3
KG
1281value of -1 means the last paren pair.
1282
1283I think it would be good if the regexp matches the whole of the
1284string, but I haven't actually tried what happens if it doesn't..."
1285 :group 'tramp
1286 :type '(list (regexp :tag "File name regexp")
1287 (integer :tag "Paren pair for method name")
1288 (integer :tag "Paren pair for hops")
7432277c 1289 (integer :tag "Paren pair to match localname")))
fb7933a3 1290
505edaeb 1291(defcustom tramp-multi-file-name-hop-structure
16674e4f
KG
1292 (list
1293 (concat
1294 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
487f4fb7
KG
1295 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1296 "\\(" tramp-host-with-port-regexp "\\)")
16674e4f 1297 1 2 3)
fb7933a3
KG
1298 "*Describes the structure of a hop in multi files.
1299This is a list of four elements (REGEXP METHOD USER HOST). First
1300element REGEXP is used to match against the hop. Pair number METHOD
1301matches the method of one hop, pair number USER matches the user of
1302one hop, pair number HOST matches the host of one hop.
1303
1304This regular expression should match exactly all of one hop."
1305 :group 'tramp
1306 :type '(list (regexp :tag "Hop regexp")
1307 (integer :tag "Paren pair for method name")
1308 (integer :tag "Paren pair for user name")
1309 (integer :tag "Paren pair for host name")))
1310
505edaeb 1311(defcustom tramp-make-multi-tramp-file-format
16674e4f
KG
1312 (list
1313 (concat tramp-prefix-format "%m")
1314 (concat tramp-postfix-multi-hop-format
1315 "%m" tramp-postfix-multi-method-format
1316 "%u" tramp-postfix-user-format
1317 "%h")
1318 (concat tramp-postfix-host-format "%p"))
fb7933a3 1319 "*Describes how to construct a `multi' file name.
7432277c 1320This is a list of three elements PREFIX, HOP and LOCALNAME.
fb7933a3
KG
1321
1322The first element PREFIX says how to construct the prefix, the second
1323element HOP specifies what each hop looks like, and the final element
7432277c 1324LOCALNAME says how to construct the localname (pathname).
fb7933a3
KG
1325
1326In PREFIX, `%%' means `%' and `%m' means the method name.
1327
1328In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1329user and hop host, respectively.
1330
7432277c 1331In LOCALNAME, `%%' means `%' and `%p' means the localname.
fb7933a3
KG
1332
1333The resulting file name always contains one copy of PREFIX and one
7432277c 1334copy of LOCALNAME, but there is one copy of HOP for each hop in the file
fb7933a3
KG
1335name.
1336
1337Note: the current implementation requires the prefix to contain the
7432277c 1338method name, followed by all the hops, and the localname must come
fb7933a3
KG
1339last."
1340 :group 'tramp
1341 :type '(list string string string))
1342
1343(defcustom tramp-terminal-type "dumb"
1344 "*Value of TERM environment variable for logging in to remote host.
1345Because Tramp wants to parse the output of the remote shell, it is easily
1346confused by ANSI color escape sequences and suchlike. Often, shell init
1347files conditionalize this setup based on the TERM environment variable."
1348 :group 'tramp
1349 :type 'string)
1350
1351(defcustom tramp-completion-without-shell-p nil
1352 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1353Using shell wildcards for completions has the advantage that it can be
1354fast even in large directories, but completion is always
1355case-sensitive. Relying on Lisp only means that case-insensitive
1356completion is possible (subject to the variable `completion-ignore-case'),
1357but it might be slow on large directories."
1358 :group 'tramp
1359 :type 'boolean)
1360
ac474af1
KG
1361(defcustom tramp-actions-before-shell
1362 '((tramp-password-prompt-regexp tramp-action-password)
1363 (tramp-login-prompt-regexp tramp-action-login)
1364 (shell-prompt-pattern tramp-action-succeed)
821e6e36 1365 (tramp-shell-prompt-pattern tramp-action-succeed)
ac474af1 1366 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
3cdaec13 1367 (tramp-yesno-prompt-regexp tramp-action-yesno)
487f4fb7 1368 (tramp-yn-prompt-regexp tramp-action-yn)
19a87064
MA
1369 (tramp-terminal-prompt-regexp tramp-action-terminal)
1370 (tramp-process-alive-regexp tramp-action-process-alive))
ac474af1
KG
1371 "List of pattern/action pairs.
1372Whenever a pattern matches, the corresponding action is performed.
1373Each item looks like (PATTERN ACTION).
1374
1375The PATTERN should be a symbol, a variable. The value of this
1376variable gives the regular expression to search for. Note that the
1377regexp must match at the end of the buffer, \"\\'\" is implicitly
1378appended to it.
1379
1380The ACTION should also be a symbol, but a function. When the
1381corresponding PATTERN matches, the ACTION function is called."
1382 :group 'tramp
1383 :type '(repeat (list variable function)))
1384
38c65fca
KG
1385(defcustom tramp-actions-copy-out-of-band
1386 '((tramp-password-prompt-regexp tramp-action-password)
1387 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
6b2633cc 1388 (tramp-copy-failed-regexp tramp-action-copy-failed)
19a87064 1389 (tramp-process-alive-regexp tramp-action-out-of-band))
38c65fca
KG
1390 "List of pattern/action pairs.
1391This list is used for copying/renaming with out-of-band methods.
1392See `tramp-actions-before-shell' for more info."
1393 :group 'tramp
1394 :type '(repeat (list variable function)))
1395
ac474af1
KG
1396(defcustom tramp-multi-actions
1397 '((tramp-password-prompt-regexp tramp-multi-action-password)
1398 (tramp-login-prompt-regexp tramp-multi-action-login)
1399 (shell-prompt-pattern tramp-multi-action-succeed)
821e6e36 1400 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
19a87064 1401 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
07dfe738 1402 (tramp-process-alive-regexp tramp-multi-action-process-alive))
ac474af1
KG
1403 "List of pattern/action pairs.
1404This list is used for each hop in multi-hop connections.
1405See `tramp-actions-before-shell' for more info."
1406 :group 'tramp
1407 :type '(repeat (list variable function)))
1408
90f8dc03 1409(defcustom tramp-initial-commands
19a87064
MA
1410 '("unset HISTORY"
1411 "unset correct"
90f8dc03
KG
1412 "unset autocorrect")
1413 "List of commands to send to the first remote shell that we see.
1414These commands will be sent to any shell, and thus they should be
1415designed to work in such circumstances. Also, restrict the commands
1416to the bare necessity for getting the remote shell into a state
1417where it is possible to execute the Bourne-ish shell.
1418
1419At the moment, the command to execute the Bourne-ish shell uses strange
1420quoting which `tcsh' tries to correct, so we send the command \"unset
1421autocorrect\" to the remote host."
1422 :group 'tramp
1423 :type '(repeat string))
1424
7432277c
KG
1425;; Chunked sending kluge. We set this to 500 for black-listed constellations
1426;; known to have a bug in `process-send-string'; some ssh connections appear
7177e2a3
MA
1427;; to drop bytes when data is sent too quickly. There is also a connection
1428;; buffer local variable, which is computed depending on remote host properties
1429;; when `tramp-chunksize' is zero or nil.
7432277c
KG
1430(defcustom tramp-chunksize
1431 (when (and (not (featurep 'xemacs))
1432 (memq system-type '(hpux)))
1433 500)
55880756
MA
1434;; Parentheses in docstring starting at beginning of line are escaped.
1435;; Fontification is messed up when
1436;; `open-paren-in-column-0-is-defun-start' set to t.
7432277c
KG
1437 "*If non-nil, chunksize for sending input to local process.
1438It is necessary only on systems which have a buggy `process-send-string'
1439implementation. The necessity, whether this variable must be set, can be
1440checked via the following code:
1441
1442 (with-temp-buffer
11948172
MA
1443 (let* ((user \"xxx\") (host \"yyy\")
1444 (init 0) (step 50)
1445 (sent init) (received init))
1446 (while (= sent received)
1447 (setq sent (+ sent step))
1448 (erase-buffer)
1449 (let ((proc (start-process (buffer-name) (current-buffer)
1450 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1451 (when (memq (process-status proc) '(run open))
1452 (process-send-string proc (make-string sent ?\\ ))
1453 (process-send-eof proc)
1454 (process-send-eof proc))
1455 (while (not (progn (goto-char (point-min))
1456 (re-search-forward \"\\\\w+\" (point-max) t)))
1457 (accept-process-output proc 1))
1458 (when (memq (process-status proc) '(run open))
1459 (setq received (string-to-number (match-string 0)))
1460 (delete-process proc)
1461 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1462 (sit-for 0))))
1463 (if (> sent (+ init step))
1464 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1465 (- sent step))
1466 (message \"Test does not work\")
1467 (display-buffer (current-buffer))
1468 (sit-for 30))))
1469
1470In the Emacs normally running Tramp, evaluate the above code
55880756 1471\(replace \"xxx\" and \"yyy\" by the remote user and host name,
11948172
MA
1472respectively). You can do this, for example, by pasting it into
1473the `*scratch*' buffer and then hitting C-j with the cursor after the
1474last closing parenthesis. Note that it works only if you have configured
1475\"ssh\" to run without password query, see ssh-agent(1).
1476
1477You will see the number of bytes sent successfully to the remote host.
1478If that number exceeds 1000, you can stop the execution by hitting
1479C-g, because your Emacs is likely clean.
1480
1481If your Emacs is buggy, the code stops and gives you an indication
1482about the value `tramp-chunksize' should be set. Maybe you could just
1483experiment a bit, e.g. changing the values of `init' and `step'
1484in the third line of the code.
1485
1486When it is necessary to set `tramp-chunksize', you might consider to
1487use an out-of-the-band method (like \"scp\") instead of an internal one
55880756 1488\(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
11948172 1489performance.
c951aecb 1490
7432277c
KG
1491Please raise a bug report via \"M-x tramp-bug\" if your system needs
1492this variable to be set as well."
1493 :group 'tramp
b1a2b924 1494 :type '(choice (const nil) integer))
7432277c 1495
5ec2cc41
KG
1496;; Logging in to a remote host normally requires obtaining a pty. But
1497;; Emacs on MacOS X has process-connection-type set to nil by default,
1498;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1499;; for an override of the system default.
1500(defcustom tramp-process-connection-type t
1501 "Overrides `process-connection-type' for connections from Tramp.
1502Tramp binds process-connection-type to the value given here before
1503opening a connection to a remote host."
1504 :group 'tramp
1505 :type '(choice (const nil) (const t) (const pty)))
1506
fb7933a3
KG
1507;;; Internal Variables:
1508
1509(defvar tramp-buffer-file-attributes nil
1510 "Holds the `ls -ild' output for the current buffer.
1511This variable is local to each buffer. It is not used if the remote
1512machine groks Perl. If it is used, it's used as an emulation for
1513the visited file modtime.")
1514(make-variable-buffer-local 'tramp-buffer-file-attributes)
1515
4007ba5b 1516(defvar tramp-md5-function
8daea7fc
KG
1517 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1518 ((fboundp 'md5-encode)
38c65fca
KG
1519 (lambda (x) (base64-encode-string
1520 (funcall (symbol-function 'md5-encode) x))))
a69c01a0 1521 (t (error "Couldn't find an `md5' function")))
4007ba5b
KG
1522 "Function to call for running the MD5 algorithm.")
1523
1524(defvar tramp-end-of-output
1525 (concat "///"
1526 (funcall tramp-md5-function
1527 (concat
1528 (prin1-to-string process-environment)
1529 (current-time-string)
1530;; (prin1-to-string
1531;; (if (fboundp 'directory-files-and-attributes)
1532;; (funcall 'directory-files-and-attributes
1533;; (or (getenv "HOME")
1534;; (tramp-temporary-file-directory)))
1535;; (mapcar
1536;; (lambda (x)
1537;; (cons x (file-attributes x)))
1538;; (directory-files (or (getenv "HOME")
1539;; (tramp-temporary-file-directory))
1540;; t))))
1541 )))
fb7933a3
KG
1542 "String used to recognize end of output.")
1543
1544(defvar tramp-connection-function nil
1545 "This internal variable holds a parameter for `tramp-methods'.
1546In the connection buffer, this variable has the value of the like-named
1547method parameter, as specified in `tramp-methods' (which see).")
1548
1549(defvar tramp-remote-sh nil
1550 "This internal variable holds a parameter for `tramp-methods'.
1551In the connection buffer, this variable has the value of the like-named
1552method parameter, as specified in `tramp-methods' (which see).")
1553
b25a52cc 1554(defvar tramp-login-program nil
fb7933a3
KG
1555 "This internal variable holds a parameter for `tramp-methods'.
1556In the connection buffer, this variable has the value of the like-named
1557method parameter, as specified in `tramp-methods' (which see).")
1558
b25a52cc 1559(defvar tramp-login-args nil
fb7933a3
KG
1560 "This internal variable holds a parameter for `tramp-methods'.
1561In the connection buffer, this variable has the value of the like-named
1562method parameter, as specified in `tramp-methods' (which see).")
1563
b25a52cc 1564(defvar tramp-copy-program nil
fb7933a3
KG
1565 "This internal variable holds a parameter for `tramp-methods'.
1566In the connection buffer, this variable has the value of the like-named
1567method parameter, as specified in `tramp-methods' (which see).")
1568
b25a52cc 1569(defvar tramp-copy-args nil
fb7933a3
KG
1570 "This internal variable holds a parameter for `tramp-methods'.
1571In the connection buffer, this variable has the value of the like-named
1572method parameter, as specified in `tramp-methods' (which see).")
1573
b25a52cc 1574(defvar tramp-copy-keep-date-arg nil
fb7933a3
KG
1575 "This internal variable holds a parameter for `tramp-methods'.
1576In the connection buffer, this variable has the value of the like-named
1577method parameter, as specified in `tramp-methods' (which see).")
1578
1579(defvar tramp-encoding-command nil
1580 "This internal variable holds a parameter for `tramp-methods'.
1581In the connection buffer, this variable has the value of the like-named
1582method parameter, as specified in `tramp-methods' (which see).")
1583
1584(defvar tramp-decoding-command nil
1585 "This internal variable holds a parameter for `tramp-methods'.
1586In the connection buffer, this variable has the value of the like-named
1587method parameter, as specified in `tramp-methods' (which see).")
1588
1589(defvar tramp-encoding-function nil
1590 "This internal variable holds a parameter for `tramp-methods'.
1591In the connection buffer, this variable has the value of the like-named
1592method parameter, as specified in `tramp-methods' (which see).")
1593
1594(defvar tramp-decoding-function nil
1595 "This internal variable holds a parameter for `tramp-methods'.
1596In the connection buffer, this variable has the value of the like-named
1597method parameter, as specified in `tramp-methods' (which see).")
1598
90f8dc03
KG
1599(defvar tramp-password-end-of-line nil
1600 "This internal variable holds a parameter for `tramp-methods'.
1601In the connection buffer, this variable has the value of the like-named
1602method parameter, as specified in `tramp-methods' (which see).")
1603
fb7933a3
KG
1604;; CCC `local in each buffer'?
1605(defvar tramp-ls-command nil
1606 "This command is used to get a long listing with numeric user and group ids.
1607This variable is automatically made buffer-local to each rsh process buffer
1608upon opening the connection.")
1609
1610(defvar tramp-current-multi-method nil
1611 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1612This variable is automatically made buffer-local to each rsh process buffer
1613upon opening the connection.")
1614
1615(defvar tramp-current-method nil
1616 "Connection method for this *tramp* buffer.
1617This variable is automatically made buffer-local to each rsh process buffer
1618upon opening the connection.")
1619
1620(defvar tramp-current-user nil
1621 "Remote login name for this *tramp* buffer.
1622This variable is automatically made buffer-local to each rsh process buffer
1623upon opening the connection.")
1624
1625(defvar tramp-current-host nil
1626 "Remote host for this *tramp* buffer.
1627This variable is automatically made buffer-local to each rsh process buffer
1628upon opening the connection.")
1629
1630(defvar tramp-test-groks-nt nil
1631 "Whether the `test' command groks the `-nt' switch.
1632\(`test A -nt B' tests if file A is newer than file B.)
1633This variable is automatically made buffer-local to each rsh process buffer
1634upon opening the connection.")
1635
1636(defvar tramp-file-exists-command nil
1637 "Command to use for checking if a file exists.
1638This variable is automatically made buffer-local to each rsh process buffer
1639upon opening the connection.")
1640
fabf2143
KG
1641(defconst tramp-uudecode "\
1642tramp_uudecode () {
1643\(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1644cat /tmp/tramp.$$
1645rm -f /tmp/tramp.$$
1646}"
1647 "Shell function to implement `uudecode' to standard output.
c08e6004
MA
1648Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1649for this or `uudecode -p', but some systems don't, and for them
1650we have this shell function.")
fabf2143
KG
1651
1652;; Perl script to implement `file-attributes' in a Lisp `read'able
1653;; output. If you are hacking on this, note that you get *no* output
1654;; unless this spits out a complete line, including the '\n' at the
1655;; end.
8daea7fc
KG
1656;; The device number is returned as "-1", because there will be a virtual
1657;; device number set in `tramp-handle-file-attributes'
5beaf831 1658(defconst tramp-perl-file-attributes "\
c82c5727
LH
1659@stat = lstat($ARGV[0]);
1660if (($stat[2] & 0170000) == 0120000)
1661{
1662 $type = readlink($ARGV[0]);
1663 $type = \"\\\"$type\\\"\";
1664}
1665elsif (($stat[2] & 0170000) == 040000)
1666{
1667 $type = \"t\";
1668}
1669else
1670{
1671 $type = \"nil\"
1672};
1673$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1674$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1675printf(
1676 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1677 $type,
1678 $stat[3],
1679 $uid,
1680 $gid,
1681 $stat[8] >> 16 & 0xffff,
1682 $stat[8] & 0xffff,
1683 $stat[9] >> 16 & 0xffff,
1684 $stat[9] & 0xffff,
1685 $stat[10] >> 16 & 0xffff,
1686 $stat[10] & 0xffff,
1687 $stat[7],
1688 $stat[2],
1689 $stat[1] >> 16 & 0xffff,
1690 $stat[1] & 0xffff
1691);"
fb7933a3
KG
1692 "Perl script to produce output suitable for use with `file-attributes'
1693on the remote file system.")
1694
c82c5727 1695(defconst tramp-perl-directory-files-and-attributes "\
8cb0a559
LH
1696chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1697opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
c82c5727
LH
1698@list = readdir(DIR);
1699closedir(DIR);
1700$n = scalar(@list);
1701printf(\"(\\n\");
1702for($i = 0; $i < $n; $i++)
1703{
1704 $filename = $list[$i];
1705 @stat = lstat($filename);
1706 if (($stat[2] & 0170000) == 0120000)
1707 {
1708 $type = readlink($filename);
1709 $type = \"\\\"$type\\\"\";
1710 }
1711 elsif (($stat[2] & 0170000) == 040000)
1712 {
1713 $type = \"t\";
1714 }
1715 else
1716 {
1717 $type = \"nil\"
1718 };
1719 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1720 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1721 printf(
1722 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1723 $filename,
1724 $type,
1725 $stat[3],
1726 $uid,
1727 $gid,
1728 $stat[8] >> 16 & 0xffff,
1729 $stat[8] & 0xffff,
1730 $stat[9] >> 16 & 0xffff,
1731 $stat[9] & 0xffff,
1732 $stat[10] >> 16 & 0xffff,
1733 $stat[10] & 0xffff,
1734 $stat[7],
1735 $stat[2],
1736 $stat[1] >> 16 & 0xffff,
1737 $stat[1] & 0xffff,
1738 $stat[0] >> 16 & 0xffff,
1739 $stat[0] & 0xffff);
1740}
1741printf(\")\\n\");"
1742 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1743output.")
1744
ac474af1
KG
1745;; ;; These two use uu encoding.
1746;; (defvar tramp-perl-encode "%s -e'\
1747;; print qq(begin 644 xxx\n);
1748;; my $s = q();
1749;; my $res = q();
1750;; while (read(STDIN, $s, 45)) {
1751;; print pack(q(u), $s);
1752;; }
1753;; print qq(`\n);
1754;; print qq(end\n);
1755;; '"
1756;; "Perl program to use for encoding a file.
1757;; Escape sequence %s is replaced with name of Perl binary.")
1758
1759;; (defvar tramp-perl-decode "%s -ne '
1760;; print unpack q(u), $_;
1761;; '"
1762;; "Perl program to use for decoding a file.
1763;; Escape sequence %s is replaced with name of Perl binary.")
1764
1765;; These two use base64 encoding.
b1d06e75 1766(defvar tramp-perl-encode-with-module
ac474af1
KG
1767 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1768 "Perl program to use for encoding a file.
b1d06e75 1769Escape sequence %s is replaced with name of Perl binary.
89509ea0 1770This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1771This implementation requires the MIME::Base64 Perl module to be installed
1772on the remote host.")
1773
1774(defvar tramp-perl-decode-with-module
1775 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1776 "Perl program to use for decoding a file.
1777Escape sequence %s is replaced with name of Perl binary.
89509ea0 1778This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1779This implementation requires the MIME::Base64 Perl module to be installed
1780on the remote host.")
1781
1782(defvar tramp-perl-encode
1783 "%s -e '
1784# This script contributed by Juanma Barranquero <lektu@terra.es>.
7dbde7dd 1785# Copyright (C) 2002, 2006 Free Software Foundation, Inc.
b1d06e75
KG
1786use strict;
1787
fa32e96a 1788my %%trans = do {
b1d06e75
KG
1789 my $i = 0;
1790 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1791 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1792};
1793
36541701 1794binmode(\\*STDIN);
b1d06e75
KG
1795
1796# We read in chunks of 54 bytes, to generate output lines
1797# of 72 chars (plus end of line)
36541701 1798$/ = \\54;
b1d06e75
KG
1799
1800while (my $data = <STDIN>) {
1801 my $pad = q();
1802
1803 # Only for the last chunk, and only if did not fill the last three-byte packet
1804 if (eof) {
fa32e96a 1805 my $mod = length($data) %% 3;
b1d06e75
KG
1806 $pad = q(=) x (3 - $mod) if $mod;
1807 }
1808
1809 # Not the fastest method, but it is simple: unpack to binary string, split
1810 # by groups of 6 bits and convert back from binary to byte; then map into
1811 # the translation table
1812 print
1813 join q(),
1814 map($trans{$_},
1815 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1816 $pad,
36541701 1817 qq(\\n);
b1d06e75
KG
1818}
1819'"
1820 "Perl program to use for encoding a file.
fa32e96a 1821Escape sequence %s is replaced with name of Perl binary.
ccf29586 1822This string is passed to `format', so percent characters need to be doubled.")
ac474af1
KG
1823
1824(defvar tramp-perl-decode
b1d06e75
KG
1825 "%s -e '
1826# This script contributed by Juanma Barranquero <lektu@terra.es>.
7dbde7dd 1827# Copyright (C) 2002, 2006 Free Software Foundation, Inc.
b1d06e75
KG
1828use strict;
1829
fa32e96a 1830my %%trans = do {
b1d06e75 1831 my $i = 0;
16674e4f 1832 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
b1d06e75
KG
1833 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1834};
1835
fa32e96a 1836my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
b1d06e75 1837
36541701 1838binmode(\\*STDOUT);
b1d06e75
KG
1839
1840# We are going to accumulate into $pending to accept any line length
1841# (we do not check they are <= 76 chars as the RFC says)
1842my $pending = q();
1843
1844while (my $data = <STDIN>) {
1845 chomp $data;
1846
1847 # If we find one or two =, we have reached the end and
1848 # any following data is to be discarded
1849 my $finished = $data =~ s/(==?).*/$1/;
1850 $pending .= $data;
1851
1852 my $len = length($pending);
16674e4f 1853 my $chunk = substr($pending, 0, $len & ~3);
414da5ab 1854 $pending = substr($pending, $len & ~3 + 1);
b1d06e75
KG
1855
1856 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1857 # split in 8-bit chunks and convert back to char.
1858 print join q(),
1859 map $bytes{$_},
1860 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1861
1862 last if $finished;
1863}
1864'"
ac474af1 1865 "Perl program to use for decoding a file.
fa32e96a 1866Escape sequence %s is replaced with name of Perl binary.
ccf29586 1867This string is passed to `format', so percent characters need to be doubled.")
fb7933a3 1868
fb7933a3
KG
1869(defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1870 (1 . "p") ; fifo
1871 (2 . "c") ; character device
1872 (3 . "m") ; multiplexed character device (v7)
1873 (4 . "d") ; directory
1874 (5 . "?") ; Named special file (XENIX)
1875 (6 . "b") ; block device
1876 (7 . "?") ; multiplexed block device (v7)
1877 (8 . "-") ; regular file
1878 (9 . "n") ; network special file (HP-UX)
1879 (10 . "l") ; symlink
1880 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1881 (12 . "s") ; socket
1882 (13 . "D") ; door special (Solaris)
1883 (14 . "w")) ; whiteout (BSD)
1884 "A list of file types returned from the `stat' system call.
1885This is used to map a mode number to a permission string.")
1886
1887(defvar tramp-dos-coding-system
1888 (if (and (fboundp 'coding-system-p)
1889 (funcall 'coding-system-p '(dos)))
1890 'dos
1891 'undecided-dos)
1892 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1893
38c65fca
KG
1894(defvar tramp-last-cmd nil
1895 "Internal Tramp variable recording the last command sent.
1896This variable is buffer-local in every buffer.")
1897(make-variable-buffer-local 'tramp-last-cmd)
1898
1899(defvar tramp-process-echoes nil
1900 "Whether to process echoes from the remote shell.")
1901
ac474af1
KG
1902(defvar tramp-last-cmd-time nil
1903 "Internal Tramp variable recording the time when the last cmd was sent.
1904This variable is buffer-local in every buffer.")
1905(make-variable-buffer-local 'tramp-last-cmd-time)
fb7933a3 1906
dba28077
KG
1907;; This variable does not have the right value in XEmacs. What should
1908;; I use instead of find-operation-coding-system in XEmacs?
3cdaec13 1909(defvar tramp-feature-write-region-fix
16674e4f 1910 (when (fboundp 'find-operation-coding-system)
dba28077 1911 (let ((file-coding-system-alist '(("test" emacs-mule))))
38c65fca
KG
1912 (funcall (symbol-function 'find-operation-coding-system)
1913 'write-region 0 0 "" nil "test")))
dba28077 1914 "Internal variable to say if `write-region' chooses the right coding.
3cdaec13
KG
1915Older versions of Emacs chose the coding system for `write-region' based
1916on the FILENAME argument, even if VISIT was a string.")
1917
fb7933a3
KG
1918;; New handlers should be added here. The following operations can be
1919;; handled using the normal primitives: file-name-as-directory,
1920;; file-name-directory, file-name-nondirectory,
1921;; file-name-sans-versions, get-file-buffer.
1922(defconst tramp-file-name-handler-alist
1923 '(
1924 (load . tramp-handle-load)
1925 (make-symbolic-link . tramp-handle-make-symbolic-link)
1926 (file-name-directory . tramp-handle-file-name-directory)
1927 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1928 (file-truename . tramp-handle-file-truename)
1929 (file-exists-p . tramp-handle-file-exists-p)
1930 (file-directory-p . tramp-handle-file-directory-p)
1931 (file-executable-p . tramp-handle-file-executable-p)
1932 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1933 (file-readable-p . tramp-handle-file-readable-p)
1934 (file-regular-p . tramp-handle-file-regular-p)
1935 (file-symlink-p . tramp-handle-file-symlink-p)
1936 (file-writable-p . tramp-handle-file-writable-p)
1937 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1938 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1939 (file-attributes . tramp-handle-file-attributes)
1940 (file-modes . tramp-handle-file-modes)
fb7933a3 1941 (directory-files . tramp-handle-directory-files)
c82c5727 1942 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
fb7933a3
KG
1943 (file-name-all-completions . tramp-handle-file-name-all-completions)
1944 (file-name-completion . tramp-handle-file-name-completion)
1945 (add-name-to-file . tramp-handle-add-name-to-file)
1946 (copy-file . tramp-handle-copy-file)
1947 (rename-file . tramp-handle-rename-file)
1948 (set-file-modes . tramp-handle-set-file-modes)
1949 (make-directory . tramp-handle-make-directory)
1950 (delete-directory . tramp-handle-delete-directory)
1951 (delete-file . tramp-handle-delete-file)
1952 (directory-file-name . tramp-handle-directory-file-name)
1953 (shell-command . tramp-handle-shell-command)
0457dd55 1954 (process-file . tramp-handle-process-file)
fb7933a3
KG
1955 (insert-directory . tramp-handle-insert-directory)
1956 (expand-file-name . tramp-handle-expand-file-name)
1957 (file-local-copy . tramp-handle-file-local-copy)
19a87064 1958 (file-remote-p . tramp-handle-file-remote-p)
fb7933a3
KG
1959 (insert-file-contents . tramp-handle-insert-file-contents)
1960 (write-region . tramp-handle-write-region)
38c65fca 1961 (find-backup-file-name . tramp-handle-find-backup-file-name)
c1105d05 1962 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
fb7933a3 1963 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
5ec2cc41 1964 (dired-compress-file . tramp-handle-dired-compress-file)
fb7933a3
KG
1965 (dired-call-process . tramp-handle-dired-call-process)
1966 (dired-recursive-delete-directory
1967 . tramp-handle-dired-recursive-delete-directory)
1968 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1969 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
c1105d05 1970 "Alist of handler functions.
fb7933a3
KG
1971Operations not mentioned here will be handled by the normal Emacs functions.")
1972
41c8e348
RS
1973;; Handlers for partial tramp file names. For Emacs just
1974;; `file-name-all-completions' is needed.
a01b1e22 1975;;;###autoload
16674e4f 1976(defconst tramp-completion-file-name-handler-alist
a01b1e22 1977 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
41c8e348 1978 (file-name-completion . tramp-completion-handle-file-name-completion))
16674e4f
KG
1979 "Alist of completion handler functions.
1980Used for file names matching `tramp-file-name-regexp'. Operations not
1981mentioned here will be handled by `tramp-file-name-handler-alist' or the
1982normal Emacs functions.")
1983
4007ba5b 1984;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
ea9d1443
KG
1985(defvar tramp-foreign-file-name-handler-alist
1986 ;; (identity . tramp-sh-file-name-handler) should always be the last
1987 ;; entry, since `identity' always matches.
1988 '((identity . tramp-sh-file-name-handler))
4007ba5b
KG
1989 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1990If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1991calling HANDLER.")
1992
fb7933a3
KG
1993;;; Internal functions which must come first.
1994
1995(defsubst tramp-message (level fmt-string &rest args)
1996 "Emit a message depending on verbosity level.
1997First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1998message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1999Calls function `message' with FMT-STRING as control string and the remaining
2000ARGS to actually emit the message (if applicable).
2001
2002This function expects to be called from the tramp buffer only!"
2003 (when (<= level tramp-verbose)
2004 (apply #'message (concat "tramp: " fmt-string) args)
2005 (when tramp-debug-buffer
2006 (save-excursion
2007 (set-buffer
2008 (tramp-get-debug-buffer
2009 tramp-current-multi-method tramp-current-method
2010 tramp-current-user tramp-current-host))
2011 (goto-char (point-max))
d2a2c17f
MA
2012 (unless (bolp)
2013 (insert "\n"))
2014 (tramp-insert-with-face
fb7933a3
KG
2015 'italic
2016 (concat "# " (apply #'format fmt-string args) "\n"))))))
2017
2018(defun tramp-message-for-buffer
2019 (multi-method method user host level fmt-string &rest args)
2020 "Like `tramp-message' but temporarily switches to the tramp buffer.
2021First three args METHOD, USER, and HOST identify the tramp buffer to use,
2022remaining args passed to `tramp-message'."
2023 (save-excursion
2024 (set-buffer (tramp-get-buffer multi-method method user host))
2025 (apply 'tramp-message level fmt-string args)))
2026
2027(defsubst tramp-line-end-position nil
2028 "Return point at end of line.
2029Calls `line-end-position' or `point-at-eol' if defined, else
2030own implementation."
2031 (cond
38c65fca
KG
2032 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
2033 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
fb7933a3
KG
2034 (t (save-excursion (end-of-line) (point)))))
2035
c62c9d08
KG
2036(defmacro with-parsed-tramp-file-name (filename var &rest body)
2037 "Parse a Tramp filename and make components available in the body.
2038
2039First arg FILENAME is evaluated and dissected into its components.
2040Second arg VAR is a symbol. It is used as a variable name to hold
2041the filename structure. It is also used as a prefix for the variables
2042holding the components. For example, if VAR is the symbol `foo', then
2043`foo' will be bound to the whole structure, `foo-multi-method' will
2044be bound to the multi-method component, and so on for `foo-method',
7432277c 2045`foo-user', `foo-host', `foo-localname'.
c62c9d08
KG
2046
2047Remaining args are Lisp expressions to be evaluated (inside an implicit
2048`progn').
2049
2050If VAR is nil, then we bind `v' to the structure and `multi-method',
7432277c 2051`method', `user', `host', `localname' to the components."
c62c9d08
KG
2052 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2053 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2054 (tramp-file-name-multi-method ,(or var 'v)))
2055 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2056 (tramp-file-name-method ,(or var 'v)))
2057 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2058 (tramp-file-name-user ,(or var 'v)))
2059 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2060 (tramp-file-name-host ,(or var 'v)))
7432277c
KG
2061 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2062 (tramp-file-name-localname ,(or var 'v))))
c62c9d08
KG
2063 ,@body))
2064
2065(put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
8e7225a2
LH
2066;; Enable debugging.
2067(def-edebug-spec with-parsed-tramp-file-name (form symbolp body))
2068;; Highlight as keyword.
2069(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
c62c9d08 2070
2fcaee47
KG
2071(defmacro tramp-let-maybe (variable value &rest body)
2072 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2073BODY is executed whether or not the variable is obsolete.
2074The intent is to protect against `obsolete variable' warnings."
2075 `(if (get ',variable 'byte-obsolete-variable)
2076 (progn ,@body)
2077 (let ((,variable ,value))
2078 ,@body)))
2079(put 'tramp-let-maybe 'lisp-indent-function 2)
a69c01a0 2080(put 'tramp-let-maybe 'edebug-form-spec t)
2fcaee47 2081
16674e4f
KG
2082;;; Config Manipulation Functions:
2083
2084(defun tramp-set-completion-function (method function-list)
2085 "Sets the list of completion functions for METHOD.
2086FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2087The FUNCTION is intended to parse FILE according its syntax.
2088It might be a predefined FUNCTION, or a user defined FUNCTION.
2089Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
8daea7fc
KG
2090`tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2091and `tramp-parse-netrc'.
2092
16674e4f
KG
2093Example:
2094
2095 (tramp-set-completion-function
2096 \"ssh\"
8daea7fc
KG
2097 '((tramp-parse-sconfig \"/etc/ssh_config\")
2098 (tramp-parse-sconfig \"~/.ssh/config\")))"
16674e4f 2099
5ec2cc41
KG
2100 (let ((r function-list)
2101 (v function-list))
2102 (setq tramp-completion-function-alist
2103 (delete (assoc method tramp-completion-function-alist)
2104 tramp-completion-function-alist))
2105
2106 (while v
2107 ;; Remove double entries
2108 (when (member (car v) (cdr v))
2109 (setcdr v (delete (car v) (cdr v))))
2110 ;; Check for function and file
2111 (unless (and (functionp (nth 0 (car v)))
2112 (file-exists-p (nth 1 (car v))))
2113 (setq r (delete (car v) r)))
2114 (setq v (cdr v)))
2115
2116 (when r
4007ba5b 2117 (add-to-list 'tramp-completion-function-alist
5ec2cc41 2118 (cons method r)))))
16674e4f
KG
2119
2120(defun tramp-get-completion-function (method)
2121 "Returns list of completion functions for METHOD.
2122For definition of that list see `tramp-set-completion-function'."
5ec2cc41 2123 (cdr (assoc method tramp-completion-function-alist)))
16674e4f 2124
fb7933a3
KG
2125;;; File Name Handler Functions:
2126
fb7933a3
KG
2127(defun tramp-handle-make-symbolic-link
2128 (filename linkname &optional ok-if-already-exists)
2129 "Like `make-symbolic-link' for tramp files.
cebb4ec6 2130If LINKNAME is a non-Tramp file, it is used verbatim as the target of
7432277c 2131the symlink. If LINKNAME is a Tramp file, only the localname component is
cebb4ec6
KG
2132used as the target of the symlink.
2133
7432277c
KG
2134If LINKNAME is a Tramp file and the localname component is relative, then
2135it is expanded first, before the localname component is taken. Note that
cebb4ec6
KG
2136this can give surprising results if the user/host for the source and
2137target of the symlink differ."
c62c9d08 2138 (with-parsed-tramp-file-name linkname l
487fa986 2139 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
7432277c 2140 (cwd (file-name-directory l-localname)))
c62c9d08
KG
2141 (unless ln
2142 (signal 'file-error
2143 (list "Making a symbolic link."
2144 "ln(1) does not exist on the remote host.")))
2145
2146 ;; Do the 'confirm if exists' thing.
cebb4ec6 2147 (when (file-exists-p linkname)
c62c9d08
KG
2148 ;; What to do?
2149 (if (or (null ok-if-already-exists) ; not allowed to exist
2150 (and (numberp ok-if-already-exists)
2151 (not (yes-or-no-p
2152 (format
2153 "File %s already exists; make it a link anyway? "
7432277c
KG
2154 l-localname)))))
2155 (signal 'file-already-exists (list "File already exists" l-localname))
cebb4ec6
KG
2156 (delete-file linkname)))
2157
7432277c 2158 ;; If FILENAME is a Tramp name, use just the localname component.
cebb4ec6 2159 (when (tramp-tramp-file-p filename)
7432277c 2160 (setq filename (tramp-file-name-localname
cebb4ec6
KG
2161 (tramp-dissect-file-name
2162 (expand-file-name filename)))))
bf247b6e 2163
c62c9d08
KG
2164 ;; Right, they are on the same host, regardless of user, method, etc.
2165 ;; We now make the link on the remote machine. This will occur as the user
2166 ;; that FILENAME belongs to.
2167 (zerop
2168 (tramp-send-command-and-check
487fa986 2169 l-multi-method l-method l-user l-host
c62c9d08
KG
2170 (format "cd %s && %s -sf %s %s"
2171 cwd ln
bf247b6e 2172 filename
7432277c 2173 l-localname)
c62c9d08 2174 t)))))
fb7933a3
KG
2175
2176
2177(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2178 "Like `load' for tramp files. Not implemented!"
2179 (unless (file-name-absolute-p file)
7432277c 2180 (error "Tramp cannot `load' files without absolute file name"))
c62c9d08 2181 (with-parsed-tramp-file-name file nil
c62c9d08
KG
2182 (unless nosuffix
2183 (cond ((file-exists-p (concat file ".elc"))
2184 (setq file (concat file ".elc")))
2185 ((file-exists-p (concat file ".el"))
2186 (setq file (concat file ".el")))))
2187 (when must-suffix
2188 ;; The first condition is always true for absolute file names.
2189 ;; Included for safety's sake.
2190 (unless (or (file-name-directory file)
2191 (string-match "\\.elc?\\'" file))
2192 (error "File `%s' does not include a `.el' or `.elc' suffix"
2193 file)))
2194 (unless noerror
2195 (when (not (file-exists-p file))
540bd575 2196 (error "Cannot load nonexistent file `%s'" file)))
c62c9d08
KG
2197 (if (not (file-exists-p file))
2198 nil
2199 (unless nomessage
2200 (message "Loading %s..." file))
2201 (let ((local-copy (file-local-copy file)))
2202 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2203 (load local-copy noerror t t)
2204 (delete-file local-copy))
2205 (unless nomessage
2206 (message "Loading %s...done" file))
2207 t)))
fb7933a3 2208
7432277c 2209;; Localname manipulation functions that grok TRAMP localnames...
fb7933a3
KG
2210(defun tramp-handle-file-name-directory (file)
2211 "Like `file-name-directory' but aware of TRAMP files."
a01b1e22 2212 ;; Everything except the last filename thing is the directory.
c62c9d08 2213 (with-parsed-tramp-file-name file nil
a01b1e22
MA
2214 ;; Run the command on the localname portion only.
2215 (tramp-make-tramp-file-name
2216 multi-method method user host (file-name-directory (or localname "")))))
fb7933a3
KG
2217
2218(defun tramp-handle-file-name-nondirectory (file)
2219 "Like `file-name-nondirectory' but aware of TRAMP files."
c62c9d08 2220 (with-parsed-tramp-file-name file nil
7432277c 2221 (file-name-nondirectory localname)))
fb7933a3
KG
2222
2223(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2224 "Like `file-truename' for tramp files."
48ddd622 2225 (with-parsed-tramp-file-name (expand-file-name filename) nil
7432277c 2226 (let* ((steps (tramp-split-string localname "/"))
19a87064
MA
2227 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2228 (file-name-as-directory localname)))
7432277c 2229 (is-dir (string= localname localnamedir))
c62c9d08
KG
2230 (thisstep nil)
2231 (numchase 0)
2232 ;; Don't make the following value larger than necessary.
2233 ;; People expect an error message in a timely fashion when
2234 ;; something is wrong; otherwise they might think that Emacs
2235 ;; is hung. Of course, correctness has to come first.
2236 (numchase-limit 20)
2237 (result nil) ;result steps in reverse order
c62c9d08 2238 symlink-target)
fb7933a3
KG
2239 (tramp-message-for-buffer
2240 multi-method method user host
c62c9d08
KG
2241 10 "Finding true name for `%s'" filename)
2242 (while (and steps (< numchase numchase-limit))
2243 (setq thisstep (pop steps))
2244 (tramp-message-for-buffer
2245 multi-method method user host
2246 10 "Check %s"
2247 (mapconcat 'identity
2248 (append '("") (reverse result) (list thisstep))
2249 "/"))
2250 (setq symlink-target
c951aecb 2251 (nth 0 (file-attributes
c62c9d08
KG
2252 (tramp-make-tramp-file-name
2253 multi-method method user host
2254 (mapconcat 'identity
b1d06e75
KG
2255 (append '("")
2256 (reverse result)
2257 (list thisstep))
c62c9d08
KG
2258 "/")))))
2259 (cond ((string= "." thisstep)
2260 (tramp-message-for-buffer multi-method method user host
2261 10 "Ignoring step `.'"))
2262 ((string= ".." thisstep)
2263 (tramp-message-for-buffer multi-method method user host
2264 10 "Processing step `..'")
2265 (pop result))
2266 ((stringp symlink-target)
2267 ;; It's a symlink, follow it.
2268 (tramp-message-for-buffer
2269 multi-method method user host
2270 10 "Follow symlink to %s" symlink-target)
2271 (setq numchase (1+ numchase))
2272 (when (file-name-absolute-p symlink-target)
2273 (setq result nil))
b25a52cc
KG
2274 ;; If the symlink was absolute, we'll get a string like
2275 ;; "/user@host:/some/target"; extract the
2276 ;; "/some/target" part from it.
2277 (when (tramp-tramp-file-p symlink-target)
2278 (with-parsed-tramp-file-name symlink-target sym
2279 (unless (equal (list multi-method method user host)
2280 (list sym-multi-method sym-method
2281 sym-user sym-host))
2282 (error "Symlink target `%s' on wrong host"
2283 symlink-target))
2284 (setq symlink-target localname)))
c62c9d08
KG
2285 (setq steps
2286 (append (tramp-split-string symlink-target "/") steps)))
2287 (t
2288 ;; It's a file.
2289 (setq result (cons thisstep result)))))
2290 (when (>= numchase numchase-limit)
2291 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2292 (setq result (reverse result))
487f4fb7
KG
2293 ;; Combine list to form string.
2294 (setq result
2295 (if result
2296 (mapconcat 'identity (cons "" result) "/")
2297 "/"))
2298 (when (and is-dir (or (string= "" result)
2299 (not (string= (substring result -1) "/"))))
2300 (setq result (concat result "/")))
c62c9d08
KG
2301 (tramp-message-for-buffer
2302 multi-method method user host
487f4fb7 2303 10 "True name of `%s' is `%s'" filename result)
c62c9d08 2304 (tramp-make-tramp-file-name
487f4fb7 2305 multi-method method user host result))))
fb7933a3
KG
2306
2307;; Basic functions.
2308
2309(defun tramp-handle-file-exists-p (filename)
2310 "Like `file-exists-p' for tramp files."
c62c9d08 2311 (with-parsed-tramp-file-name filename nil
fb7933a3
KG
2312 (save-excursion
2313 (zerop (tramp-send-command-and-check
2314 multi-method method user host
2315 (format
c62c9d08 2316 (tramp-get-file-exists-command multi-method method user host)
7432277c 2317 (tramp-shell-quote-argument localname)))))))
fb7933a3 2318
8daea7fc
KG
2319;; Devices must distinguish physical file systems. The device numbers
2320;; provided by "lstat" aren't unique, because we operate on different hosts.
2321;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2322;; EFS use device number "-1". In order to be different, we use device number
2323;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2324(defvar tramp-devices nil
2325 "Keeps virtual device numbers.")
2326
fb7933a3
KG
2327;; CCC: This should check for an error condition and signal failure
2328;; when something goes wrong.
2329;; Daniel Pittman <daniel@danann.net>
c951aecb
KG
2330(defun tramp-handle-file-attributes (filename &optional id-format)
2331 "Like `file-attributes' for tramp files."
c82c5727
LH
2332 (when (file-exists-p filename)
2333 ;; file exists, find out stuff
2334 (unless id-format (setq id-format 'integer))
ac474af1 2335 (with-parsed-tramp-file-name filename nil
c82c5727
LH
2336 (save-excursion
2337 (tramp-convert-file-attributes
2338 multi-method method user host
2339 (if (tramp-get-remote-perl multi-method method user host)
2340 (tramp-handle-file-attributes-with-perl multi-method method user host
2341 localname id-format)
2342 (tramp-handle-file-attributes-with-ls multi-method method user host
2343 localname id-format)))))))
fb7933a3 2344
fb7933a3 2345(defun tramp-handle-file-attributes-with-ls
c82c5727 2346 (multi-method method user host localname &optional id-format)
fb7933a3
KG
2347 "Implement `file-attributes' for tramp files using the ls(1) command."
2348 (let (symlinkp dirp
2349 res-inode res-filemodes res-numlinks
2350 res-uid res-gid res-size res-symlink-target)
ac474af1
KG
2351 (tramp-message-for-buffer multi-method method user host 10
2352 "file attributes with ls: %s"
2353 (tramp-make-tramp-file-name
7432277c 2354 multi-method method user host localname))
fb7933a3
KG
2355 (tramp-send-command
2356 multi-method method user host
2357 (format "%s %s %s"
2358 (tramp-get-ls-command multi-method method user host)
c82c5727 2359 (if (eq id-format 'integer) "-ildn" "-ild")
7432277c 2360 (tramp-shell-quote-argument localname)))
fb7933a3
KG
2361 (tramp-wait-for-output)
2362 ;; parse `ls -l' output ...
2363 ;; ... inode
2364 (setq res-inode
2365 (condition-case err
2366 (read (current-buffer))
2367 (invalid-read-syntax
2368 (when (and (equal (cadr err)
2369 "Integer constant overflow in reader")
2370 (string-match
2371 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2372 (caddr err)))
2373 (let* ((big (read (substring (caddr err) 0
2374 (match-beginning 1))))
2375 (small (read (match-string 1 (caddr err))))
2376 (twiddle (/ small 65536)))
2377 (cons (+ big twiddle)
2378 (- small (* twiddle 65536))))))))
2379 ;; ... file mode flags
2380 (setq res-filemodes (symbol-name (read (current-buffer))))
2381 ;; ... number links
2382 (setq res-numlinks (read (current-buffer)))
2383 ;; ... uid and gid
2384 (setq res-uid (read (current-buffer)))
2385 (setq res-gid (read (current-buffer)))
c82c5727 2386 (when (eq id-format 'integer)
fb7933a3
KG
2387 (unless (numberp res-uid) (setq res-uid -1))
2388 (unless (numberp res-gid) (setq res-gid -1)))
2389 ;; ... size
2390 (setq res-size (read (current-buffer)))
2391 ;; From the file modes, figure out other stuff.
2392 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2393 (setq dirp (eq ?d (aref res-filemodes 0)))
2394 ;; if symlink, find out file name pointed to
2395 (when symlinkp
2396 (search-forward "-> ")
2397 (setq res-symlink-target
2398 (buffer-substring (point)
2399 (tramp-line-end-position))))
2400 ;; return data gathered
2401 (list
2402 ;; 0. t for directory, string (name linked to) for symbolic
2403 ;; link, or nil.
2404 (or dirp res-symlink-target nil)
2405 ;; 1. Number of links to file.
2406 res-numlinks
2407 ;; 2. File uid.
2408 res-uid
2409 ;; 3. File gid.
2410 res-gid
2411 ;; 4. Last access time, as a list of two integers. First
2412 ;; integer has high-order 16 bits of time, second has low 16
2413 ;; bits.
2414 ;; 5. Last modification time, likewise.
2415 ;; 6. Last status change time, likewise.
2416 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2417 ;; 7. Size in bytes (-1, if number is out of range).
2418 res-size
2419 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2420 res-filemodes
2421 ;; 9. t iff file's gid would change if file were deleted and
a69c01a0
MA
2422 ;; recreated. Will be set in `tramp-convert-file-attributes'
2423 t
fb7933a3
KG
2424 ;; 10. inode number.
2425 res-inode
8daea7fc
KG
2426 ;; 11. Device number. Will be replaced by a virtual device number.
2427 -1
fb7933a3
KG
2428 )))
2429
2430(defun tramp-handle-file-attributes-with-perl
c82c5727
LH
2431 (multi-method method user host localname &optional id-format)
2432 "Implement `file-attributes' for tramp files using a Perl script."
ac474af1
KG
2433 (tramp-message-for-buffer multi-method method user host 10
2434 "file attributes with perl: %s"
2435 (tramp-make-tramp-file-name
7432277c 2436 multi-method method user host localname))
c08e6004
MA
2437 (tramp-maybe-send-perl-script multi-method method user host
2438 tramp-perl-file-attributes
2439 "tramp_file_attributes")
c82c5727
LH
2440 (tramp-send-command multi-method method user host
2441 (format "tramp_file_attributes %s %s"
2442 (tramp-shell-quote-argument localname) id-format))
fb7933a3 2443 (tramp-wait-for-output)
c82c5727 2444 (read (current-buffer)))
8daea7fc 2445
fb7933a3
KG
2446(defun tramp-handle-set-visited-file-modtime (&optional time-list)
2447 "Like `set-visited-file-modtime' for tramp files."
2448 (unless (buffer-file-name)
2449 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2450 (buffer-name)))
48ddd622
MA
2451 (if time-list
2452 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
11948172
MA
2453 (let ((f (buffer-file-name))
2454 coding-system-used)
48ddd622
MA
2455 (with-parsed-tramp-file-name f nil
2456 (let* ((attr (file-attributes f))
2457 ;; '(-1 65535) means file doesn't exists yet.
2458 (modtime (or (nth 5 attr) '(-1 65535))))
11948172
MA
2459 (when (boundp 'last-coding-system-used)
2460 (setq coding-system-used (symbol-value 'last-coding-system-used)))
48ddd622
MA
2461 ;; We use '(0 0) as a don't-know value. See also
2462 ;; `tramp-handle-file-attributes-with-ls'.
48ddd622
MA
2463 (if (not (equal modtime '(0 0)))
2464 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2465 (save-excursion
2466 (tramp-send-command
2467 multi-method method user host
2468 (format "%s -ild %s"
2469 (tramp-get-ls-command multi-method method user host)
2470 (tramp-shell-quote-argument localname)))
2471 (tramp-wait-for-output)
2472 (setq attr (buffer-substring (point)
2473 (progn (end-of-line) (point)))))
2474 (setq tramp-buffer-file-attributes attr))
11948172
MA
2475 (when (boundp 'last-coding-system-used)
2476 (set 'last-coding-system-used coding-system-used))
d2a2c17f 2477 nil)))))
fb7933a3 2478
c62c9d08
KG
2479;; CCC continue here
2480
2481;; This function makes the same assumption as
2482;; `tramp-handle-set-visited-file-modtime'.
2483(defun tramp-handle-verify-visited-file-modtime (buf)
c08e6004
MA
2484 "Like `verify-visited-file-modtime' for tramp files.
2485At the time `verify-visited-file-modtime' calls this function, we
2486already know that the buffer is visiting a file and that
2487`visited-file-modtime' does not return 0. Do not call this
2488function directly, unless those two cases are already taken care
2489of."
c62c9d08 2490 (with-current-buffer buf
b15d0c4c
MA
2491 ;; There is no file visiting the buffer, or the buffer has no
2492 ;; recorded last modification time.
2493 (if (or (not (buffer-file-name))
2494 (eq (visited-file-modtime) 0))
d2a2c17f 2495 t
b15d0c4c
MA
2496 (let ((f (buffer-file-name)))
2497 (with-parsed-tramp-file-name f nil
2498 (let* ((attr (file-attributes f))
2499 (modtime (nth 5 attr))
2500 (mt (visited-file-modtime)))
bf247b6e 2501
b15d0c4c
MA
2502 (cond
2503 ;; file exists, and has a known modtime.
2504 ((and attr (not (equal modtime '(0 0))))
2505 (< (abs (tramp-time-diff
2506 modtime
2507 ;; For compatibility, deal with both the old
2508 ;; (HIGH . LOW) and the new (HIGH LOW)
2509 ;; return values of `visited-file-modtime'.
2510 (if (atom (cdr mt))
2511 (list (car mt) (cdr mt))
2512 mt)))
2513 2))
2514 ;; modtime has the don't know value.
2515 (attr
2516 (save-excursion
2517 (tramp-send-command
2518 multi-method method user host
2519 (format "%s -ild %s"
2520 (tramp-get-ls-command multi-method method user host)
2521 (tramp-shell-quote-argument localname)))
2522 (tramp-wait-for-output)
2523 (setq attr (buffer-substring
2524 (point) (progn (end-of-line) (point)))))
2525 (equal tramp-buffer-file-attributes attr))
2526 ;; If file does not exist, say it is not modified
2527 ;; if and only if that agrees with the buffer's record.
2528 (t (equal mt '(-1 65535))))))))))
c62c9d08 2529
fb7933a3
KG
2530(defun tramp-handle-set-file-modes (filename mode)
2531 "Like `set-file-modes' for tramp files."
c62c9d08 2532 (with-parsed-tramp-file-name filename nil
fb7933a3
KG
2533 (save-excursion
2534 (unless (zerop (tramp-send-command-and-check
c62c9d08
KG
2535 multi-method method user host
2536 (format "chmod %s %s"
2537 (tramp-decimal-to-octal mode)
7432277c 2538 (tramp-shell-quote-argument localname))))
fb7933a3
KG
2539 (signal 'file-error
2540 (list "Doing chmod"
2541 ;; FIXME: extract the proper text from chmod's stderr.
2542 "error while changing file's mode"
2543 filename))))))
2544
2545;; Simple functions using the `test' command.
2546
2547(defun tramp-handle-file-executable-p (filename)
2548 "Like `file-executable-p' for tramp files."
c62c9d08 2549 (with-parsed-tramp-file-name filename nil
c62c9d08 2550 (zerop (tramp-run-test "-x" filename))))
fb7933a3
KG
2551
2552(defun tramp-handle-file-readable-p (filename)
2553 "Like `file-readable-p' for tramp files."
c62c9d08 2554 (with-parsed-tramp-file-name filename nil
c62c9d08 2555 (zerop (tramp-run-test "-r" filename))))
fb7933a3
KG
2556
2557(defun tramp-handle-file-accessible-directory-p (filename)
2558 "Like `file-accessible-directory-p' for tramp files."
c62c9d08 2559 (with-parsed-tramp-file-name filename nil
c62c9d08
KG
2560 (and (zerop (tramp-run-test "-d" filename))
2561 (zerop (tramp-run-test "-r" filename))
2562 (zerop (tramp-run-test "-x" filename)))))
fb7933a3
KG
2563
2564;; When the remote shell is started, it looks for a shell which groks
2565;; tilde expansion. Here, we assume that all shells which grok tilde
2566;; expansion will also provide a `test' command which groks `-nt' (for
2567;; newer than). If this breaks, tell me about it and I'll try to do
2568;; something smarter about it.
2569(defun tramp-handle-file-newer-than-file-p (file1 file2)
2570 "Like `file-newer-than-file-p' for tramp files."
2571 (cond ((not (file-exists-p file1))
2572 nil)
2573 ((not (file-exists-p file2))
2574 t)
91879624 2575 ;; We are sure both files exist at this point.
fb7933a3
KG
2576 (t
2577 (save-excursion
91879624
KG
2578 ;; We try to get the mtime of both files. If they are not
2579 ;; equal to the "dont-know" value, then we subtract the times
2580 ;; and obtain the result.
2581 (let ((fa1 (file-attributes file1))
2582 (fa2 (file-attributes file2)))
2583 (if (and (not (equal (nth 5 fa1) '(0 0)))
2584 (not (equal (nth 5 fa2) '(0 0))))
01917a18 2585 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
91879624
KG
2586 ;; If one of them is the dont-know value, then we can
2587 ;; still try to run a shell command on the remote host.
2588 ;; However, this only works if both files are Tramp
2589 ;; files and both have the same method, same user, same
2590 ;; host.
c62c9d08
KG
2591 (unless (and (tramp-tramp-file-p file1)
2592 (tramp-tramp-file-p file2))
91879624
KG
2593 (signal
2594 'file-error
2595 (list
2596 "Cannot check if Tramp file is newer than non-Tramp file"
2597 file1 file2)))
2598 (with-parsed-tramp-file-name file1 v1
2599 (with-parsed-tramp-file-name file2 v2
91879624
KG
2600 (unless (and (equal v1-multi-method v2-multi-method)
2601 (equal v1-method v2-method)
2602 (equal v1-user v2-user)
2603 (equal v1-host v2-host))
2604 (signal 'file-error
2605 (list "Files must have same method, user, host"
2606 file1 file2)))
2607 (unless (and (tramp-tramp-file-p file1)
2608 (tramp-tramp-file-p file2))
2609 (signal 'file-error
2610 (list "Files must be tramp files on same host"
2611 file1 file2)))
2612 (if (tramp-get-test-groks-nt
2613 v1-multi-method v1-method v1-user v1-host)
2614 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2615 (zerop (tramp-run-test2
2616 "tramp_test_nt" file1 file2)))))))))))
fb7933a3
KG
2617
2618;; Functions implemented using the basic functions above.
2619
2620(defun tramp-handle-file-modes (filename)
2621 "Like `file-modes' for tramp files."
c62c9d08 2622 (with-parsed-tramp-file-name filename nil
c62c9d08
KG
2623 (when (file-exists-p filename)
2624 (tramp-mode-string-to-int
4007ba5b 2625 (nth 8 (file-attributes filename))))))
fb7933a3
KG
2626
2627(defun tramp-handle-file-directory-p (filename)
2628 "Like `file-directory-p' for tramp files."
2629 ;; Care must be taken that this function returns `t' for symlinks
2630 ;; pointing to directories. Surely the most obvious implementation
2631 ;; would be `test -d', but that returns false for such symlinks.
2632 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2633 ;; I now think he's right. So we could be using `test -d', couldn't
2634 ;; we?
2635 ;;
2636 ;; Alternatives: `cd %s', `test -d %s'
c62c9d08 2637 (with-parsed-tramp-file-name filename nil
c62c9d08 2638 (save-excursion
fb7933a3
KG
2639 (zerop
2640 (tramp-send-command-and-check
c62c9d08
KG
2641 multi-method method user host
2642 (format "test -d %s"
7432277c 2643 (tramp-shell-quote-argument localname))
c62c9d08 2644 t))))) ;run command in subshell
fb7933a3
KG
2645
2646(defun tramp-handle-file-regular-p (filename)
2647 "Like `file-regular-p' for tramp files."
c62c9d08 2648 (with-parsed-tramp-file-name filename nil
8daea7fc
KG
2649 (and (file-exists-p filename)
2650 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
fb7933a3
KG
2651
2652(defun tramp-handle-file-symlink-p (filename)
2653 "Like `file-symlink-p' for tramp files."
c62c9d08 2654 (with-parsed-tramp-file-name filename nil
c951aecb 2655 (let ((x (car (file-attributes filename))))
b25a52cc
KG
2656 (when (stringp x)
2657 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2658 ;; might do weird things.
2659 (if (file-name-absolute-p x)
2660 (tramp-make-tramp-file-name
2661 multi-method method user host x)
2662 x)))))
fb7933a3
KG
2663
2664(defun tramp-handle-file-writable-p (filename)
2665 "Like `file-writable-p' for tramp files."
c62c9d08 2666 (with-parsed-tramp-file-name filename nil
07dfe738 2667 (if (file-exists-p filename)
c62c9d08
KG
2668 ;; Existing files must be writable.
2669 (zerop (tramp-run-test "-w" filename))
2670 ;; If file doesn't exist, check if directory is writable.
2671 (and (zerop (tramp-run-test
07dfe738 2672 "-d" (file-name-directory filename)))
c62c9d08 2673 (zerop (tramp-run-test
07dfe738 2674 "-w" (file-name-directory filename)))))))
fb7933a3
KG
2675
2676(defun tramp-handle-file-ownership-preserved-p (filename)
2677 "Like `file-ownership-preserved-p' for tramp files."
c62c9d08 2678 (with-parsed-tramp-file-name filename nil
a69c01a0
MA
2679 (let ((attributes (file-attributes filename)))
2680 ;; Return t if the file doesn't exist, since it's true that no
2681 ;; information would be lost by an (attempted) delete and create.
2682 (or (null attributes)
2683 (= (nth 2 attributes)
2684 (tramp-get-remote-uid multi-method method user host))))))
fb7933a3
KG
2685
2686;; Other file name ops.
2687
83bbd71b 2688;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
fb7933a3
KG
2689;; (defun tramp-handle-directory-file-name (directory)
2690;; "Like `directory-file-name' for tramp files."
2691;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2692;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2693;; (substring directory 0 (- (length directory) 1))
2694;; directory))
2695
8daea7fc
KG
2696;; ;; Philippe Troin <phil@fifi.org>
2697;; (defun tramp-handle-directory-file-name (directory)
2698;; "Like `directory-file-name' for tramp files."
2699;; (with-parsed-tramp-file-name directory nil
2700;; (let ((directory-length-1 (1- (length directory))))
2701;; (save-match-data
2702;; (if (and (eq (aref directory directory-length-1) ?/)
2703;; (eq (string-match tramp-file-name-regexp directory) 0)
2704;; (/= (match-end 0) directory-length-1))
2705;; (substring directory 0 directory-length-1)
2706;; directory)))))
2707
fb7933a3
KG
2708(defun tramp-handle-directory-file-name (directory)
2709 "Like `directory-file-name' for tramp files."
7432277c
KG
2710 ;; If localname component of filename is "/", leave it unchanged.
2711 ;; Otherwise, remove any trailing slash from localname component.
8daea7fc
KG
2712 ;; Method, host, etc, are unchanged. Does it make sense to try
2713 ;; to avoid parsing the filename?
c62c9d08 2714 (with-parsed-tramp-file-name directory nil
7432277c
KG
2715 (if (and (not (zerop (length localname)))
2716 (eq (aref localname (1- (length localname))) ?/)
2717 (not (string= localname "/")))
8daea7fc
KG
2718 (substring directory 0 -1)
2719 directory)))
fb7933a3
KG
2720
2721;; Directory listings.
2722
ac474af1
KG
2723(defun tramp-handle-directory-files (directory
2724 &optional full match nosort files-only)
fb7933a3 2725 "Like `directory-files' for tramp files."
c62c9d08 2726 (with-parsed-tramp-file-name directory nil
c62c9d08 2727 (let (result x)
fb7933a3
KG
2728 (save-excursion
2729 (tramp-barf-unless-okay
2730 multi-method method user host
7432277c 2731 (concat "cd " (tramp-shell-quote-argument localname))
c62c9d08
KG
2732 nil
2733 'file-error
2734 "tramp-handle-directory-files: couldn't `cd %s'"
7432277c 2735 (tramp-shell-quote-argument localname))
fb7933a3
KG
2736 (tramp-send-command
2737 multi-method method user host
c62c9d08
KG
2738 (concat (tramp-get-ls-command multi-method method user host)
2739 " -a | cat"))
fb7933a3
KG
2740 (tramp-wait-for-output)
2741 (goto-char (point-max))
2742 (while (zerop (forward-line -1))
c62c9d08
KG
2743 (setq x (buffer-substring (point)
2744 (tramp-line-end-position)))
2745 (when (or (not match) (string-match match x))
2746 (if full
2747 (push (concat (file-name-as-directory directory)
2748 x)
2749 result)
2750 (push x result))))
fb7933a3 2751 (tramp-send-command multi-method method user host "cd")
ac474af1
KG
2752 (tramp-wait-for-output)
2753 ;; Remove non-files or non-directories if necessary. Using
2754 ;; the remote shell for this would probably be way faster.
2755 ;; Maybe something could be adapted from
2756 ;; tramp-handle-file-name-all-completions.
2757 (when files-only
2758 (let ((temp (nreverse result))
2759 item)
2760 (setq result nil)
2761 (if (equal files-only t)
2762 ;; files only
2763 (while temp
2764 (setq item (pop temp))
2765 (when (file-regular-p item)
2766 (push item result)))
2767 ;; directories only
2768 (while temp
2769 (setq item (pop temp))
2770 (when (file-directory-p item)
2771 (push item result)))))))
c62c9d08
KG
2772 result)))
2773
c82c5727
LH
2774(defun tramp-handle-directory-files-and-attributes
2775 (directory &optional full match nosort id-format)
2776 "Like `directory-files-and-attributes' for tramp files."
2777 (when (tramp-handle-file-exists-p directory)
2778 (save-excursion
2779 (setq directory (tramp-handle-expand-file-name directory))
2780 (with-parsed-tramp-file-name directory nil
c08e6004
MA
2781 (tramp-maybe-send-perl-script multi-method method user host
2782 tramp-perl-directory-files-and-attributes
2783 "tramp_directory_files_and_attributes")
c82c5727
LH
2784 (tramp-send-command multi-method method user host
2785 (format "tramp_directory_files_and_attributes %s %s"
2786 (tramp-shell-quote-argument localname)
2787 (or id-format 'integer)))
2788 (tramp-wait-for-output)
8cb0a559
LH
2789 (let* ((root (cons nil (let ((object (read (current-buffer))))
2790 (when (stringp object)
2791 (error object))
2792 object)))
c82c5727
LH
2793 (cell root))
2794 (while (cdr cell)
2795 (if (and match (not (string-match match (caadr cell))))
2796 ;; Remove from list
2797 (setcdr cell (cddr cell))
2798 ;; Include in list
2799 (setq cell (cdr cell))
2800 (let ((l (car cell)))
2801 (tramp-convert-file-attributes multi-method method user host
2802 (cdr l))
2803 ;; If FULL, make file name absolute
2804 (when full (setcar l (concat directory "/" (car l)))))))
2805 (if nosort
2806 (cdr root)
2807 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2808
c62c9d08
KG
2809;; This function should return "foo/" for directories and "bar" for
2810;; files. We use `ls -ad' to get a list of files (including
2811;; directories), and `find . -type d \! -name . -prune' to get a list
2812;; of directories.
2813(defun tramp-handle-file-name-all-completions (filename directory)
2814 "Like `file-name-all-completions' for tramp files."
2815 (with-parsed-tramp-file-name directory nil
c62c9d08
KG
2816 (unless (save-match-data (string-match "/" filename))
2817 (let* ((nowild tramp-completion-without-shell-p)
2818 result)
2819 (save-excursion
2820 (tramp-barf-unless-okay
2821 multi-method method user host
7432277c 2822 (format "cd %s" (tramp-shell-quote-argument localname))
c62c9d08
KG
2823 nil 'file-error
2824 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
7432277c 2825 (tramp-shell-quote-argument localname))
c62c9d08
KG
2826
2827 ;; Get a list of directories and files, including reliably
2828 ;; tagging the directories with a trailing '/'. Because I
2829 ;; rock. --daniel@danann.net
2830 (tramp-send-command
2831 multi-method method user host
2832 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2833 "if test -d \"$f\" 2>/dev/null; "
2834 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2835 (tramp-get-ls-command multi-method method user host)
2836 (if (or nowild (zerop (length filename)))
2837 ""
2838 (format "-d %s*"
2839 (tramp-shell-quote-argument filename)))))
2840
2841 ;; Now grab the output.
2842 (tramp-wait-for-output)
2843 (goto-char (point-max))
2844 (while (zerop (forward-line -1))
2845 (push (buffer-substring (point)
2846 (tramp-line-end-position))
2847 result))
bf247b6e 2848
c62c9d08
KG
2849 (tramp-send-command multi-method method user host "cd")
2850 (tramp-wait-for-output)
fb7933a3 2851
c62c9d08
KG
2852 ;; Return the list.
2853 (if nowild
2854 (all-completions filename (mapcar 'list result))
2855 result))))))
fb7933a3
KG
2856
2857
2858;; The following isn't needed for Emacs 20 but for 19.34?
e1e17cae
MA
2859(defun tramp-handle-file-name-completion
2860 (filename directory &optional predicate)
fb7933a3
KG
2861 "Like `file-name-completion' for tramp files."
2862 (unless (tramp-tramp-file-p directory)
2863 (error
2864 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2865 directory))
c62c9d08 2866 (with-parsed-tramp-file-name directory nil
c62c9d08
KG
2867 (try-completion
2868 filename
2869 (mapcar (lambda (x) (cons x nil))
e1e17cae
MA
2870 (file-name-all-completions filename directory))
2871 predicate)))
fb7933a3
KG
2872
2873;; cp, mv and ln
2874
2875(defun tramp-handle-add-name-to-file
2876 (filename newname &optional ok-if-already-exists)
2877 "Like `add-name-to-file' for tramp files."
c62c9d08
KG
2878 (with-parsed-tramp-file-name filename v1
2879 (with-parsed-tramp-file-name newname v2
2880 (let ((ln (when v1 (tramp-get-remote-ln
2881 v1-multi-method v1-method v1-user v1-host))))
2882 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2883 (equal v1-multi-method v2-multi-method)
2884 (equal v1-method v2-method)
2885 (equal v1-user v2-user)
2886 (equal v1-host v2-host))
2887 (error "add-name-to-file: %s"
2888 "only implemented for same method, same user, same host"))
c62c9d08
KG
2889 (when (and (not ok-if-already-exists)
2890 (file-exists-p newname)
2891 (not (numberp ok-if-already-exists))
2892 (y-or-n-p
2893 (format
2894 "File %s already exists; make it a new name anyway? "
2895 newname)))
2896 (error "add-name-to-file: file %s already exists" newname))
2897 (tramp-barf-unless-okay
2898 v1-multi-method v1-method v1-user v1-host
7432277c
KG
2899 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2900 (tramp-shell-quote-argument v2-localname))
c62c9d08
KG
2901 nil 'file-error
2902 "error with add-name-to-file, see buffer `%s' for details"
2903 (buffer-name))))))
fb7933a3
KG
2904
2905(defun tramp-handle-copy-file
2906 (filename newname &optional ok-if-already-exists keep-date)
2907 "Like `copy-file' for tramp files."
2908 ;; Check if both files are local -- invoke normal copy-file.
2909 ;; Otherwise, use tramp from local system.
2910 (setq filename (expand-file-name filename))
2911 (setq newname (expand-file-name newname))
2912 ;; At least one file a tramp file?
2913 (if (or (tramp-tramp-file-p filename)
2914 (tramp-tramp-file-p newname))
01917a18
MA
2915 (tramp-do-copy-or-rename-file
2916 'copy filename newname ok-if-already-exists keep-date)
fb7933a3
KG
2917 (tramp-run-real-handler
2918 'copy-file
2919 (list filename newname ok-if-already-exists keep-date))))
2920
2921(defun tramp-handle-rename-file
2922 (filename newname &optional ok-if-already-exists)
2923 "Like `rename-file' for tramp files."
2924 ;; Check if both files are local -- invoke normal rename-file.
2925 ;; Otherwise, use tramp from local system.
2926 (setq filename (expand-file-name filename))
2927 (setq newname (expand-file-name newname))
2928 ;; At least one file a tramp file?
2929 (if (or (tramp-tramp-file-p filename)
2930 (tramp-tramp-file-p newname))
2931 (tramp-do-copy-or-rename-file
2932 'rename filename newname ok-if-already-exists)
2933 (tramp-run-real-handler 'rename-file
2934 (list filename newname ok-if-already-exists))))
2935
2936(defun tramp-do-copy-or-rename-file
2937 (op filename newname &optional ok-if-already-exists keep-date)
2938 "Copy or rename a remote file.
2939OP must be `copy' or `rename' and indicates the operation to perform.
2940FILENAME specifies the file to copy or rename, NEWNAME is the name of
2941the new file (for copy) or the new name of the file (for rename).
2942OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2943KEEP-DATE means to make sure that NEWNAME has the same timestamp
2944as FILENAME.
2945
2946This function is invoked by `tramp-handle-copy-file' and
2947`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2948and `rename'. FILENAME and NEWNAME must be absolute file names."
2949 (unless (memq op '(copy rename))
2950 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2951 (unless ok-if-already-exists
2952 (when (file-exists-p newname)
2953 (signal 'file-already-exists
8e7225a2 2954 (list "File already exists" newname))))
90dc758d 2955 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41
KG
2956 (t2 (tramp-tramp-file-p newname))
2957 v1-multi-method v1-method v1-user v1-host v1-localname
2958 v2-multi-method v2-method v2-user v2-host v2-localname)
2959
90dc758d 2960 ;; Check which ones of source and target are Tramp files.
5ec2cc41
KG
2961 ;; We cannot invoke `with-parsed-tramp-file-name';
2962 ;; it fails if the file isn't a Tramp file name.
2963 (if t1
2964 (with-parsed-tramp-file-name filename l
2965 (setq v1-multi-method l-multi-method
2966 v1-method l-method
2967 v1-user l-user
2968 v1-host l-host
2969 v1-localname l-localname))
2970 (setq v1-localname filename))
2971 (if t2
2972 (with-parsed-tramp-file-name newname l
2973 (setq v2-multi-method l-multi-method
2974 v2-method l-method
2975 v2-user l-user
2976 v2-host l-host
2977 v2-localname l-localname))
2978 (setq v2-localname newname))
2979
90dc758d 2980 (cond
5ec2cc41 2981 ;; Both are Tramp files.
90dc758d 2982 ((and t1 t2)
5ec2cc41
KG
2983 (cond
2984 ;; Shortcut: if method, host, user are the same for both
2985 ;; files, we invoke `cp' or `mv' on the remote host
2986 ;; directly.
2987 ((and (equal v1-multi-method v2-multi-method)
2988 (equal v1-method v2-method)
2989 (equal v1-user v2-user)
2990 (equal v1-host v2-host))
2991 (tramp-do-copy-or-rename-file-directly
2992 op v1-multi-method v1-method v1-user v1-host
2993 v1-localname v2-localname keep-date))
2994 ;; If both source and target are Tramp files,
2995 ;; both are using the same copy-program, then we
2996 ;; can invoke rcp directly. Note that
2997 ;; default-directory should point to a local
2998 ;; directory if we want to invoke rcp.
2999 ((and (not v1-multi-method)
3000 (not v2-multi-method)
3001 (equal v1-method v2-method)
3002 (tramp-method-out-of-band-p
3003 v1-multi-method v1-method v1-user v1-host)
3004 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
3005 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
3006 (tramp-do-copy-or-rename-file-out-of-band
3007 op filename newname keep-date))
3008 ;; No shortcut was possible. So we copy the
3009 ;; file first. If the operation was `rename', we go
3010 ;; back and delete the original file (if the copy was
3011 ;; successful). The approach is simple-minded: we
3012 ;; create a new buffer, insert the contents of the
3013 ;; source file into it, then write out the buffer to
3014 ;; the target file. The advantage is that it doesn't
3015 ;; matter which filename handlers are used for the
3016 ;; source and target file.
3017 (t
38c65fca 3018 (tramp-do-copy-or-rename-file-via-buffer
5ec2cc41
KG
3019 op filename newname keep-date))))
3020
3021 ;; One file is a Tramp file, the other one is local.
7432277c 3022 ((or t1 t2)
5ec2cc41
KG
3023 ;; If the Tramp file has an out-of-band method, the corresponding
3024 ;; copy-program can be invoked.
3025 (if (and (not v1-multi-method)
3026 (not v2-multi-method)
8e7225a2
LH
3027 (or (and t1 (tramp-method-out-of-band-p
3028 v1-multi-method v1-method v1-user v1-host))
3029 (and t2 (tramp-method-out-of-band-p
3030 v2-multi-method v2-method v2-user v2-host))))
5ec2cc41
KG
3031 (tramp-do-copy-or-rename-file-out-of-band
3032 op filename newname keep-date)
3033 ;; Use the generic method via a Tramp buffer.
38c65fca
KG
3034 (tramp-do-copy-or-rename-file-via-buffer
3035 op filename newname keep-date)))
5ec2cc41 3036
7432277c
KG
3037 (t
3038 ;; One of them must be a Tramp file.
3039 (error "Tramp implementation says this cannot happen")))))
3040
38c65fca 3041(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
90dc758d
KG
3042 "Use an Emacs buffer to copy or rename a file.
3043First arg OP is either `copy' or `rename' and indicates the operation.
3044FILENAME is the source file, NEWNAME the target file.
3045KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
5ec2cc41
KG
3046 (let ((trampbuf (get-buffer-create "*tramp output*"))
3047 (modtime (nth 5 (file-attributes filename))))
3048 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
90dc758d
KG
3049 (tramp-message
3050 1 (concat "Warning: cannot preserve file time stamp"
3051 " with inline copying across machines")))
3052 (save-excursion
3053 (set-buffer trampbuf) (erase-buffer)
3054 (insert-file-contents-literally filename)
ea9d1443
KG
3055 ;; We don't want the target file to be compressed, so we let-bind
3056 ;; `jka-compr-inhibit' to t.
3057 (let ((coding-system-for-write 'binary)
3058 (jka-compr-inhibit t))
5ec2cc41
KG
3059 (write-region (point-min) (point-max) newname))
3060 ;; KEEP-DATE handling.
38c65fca
KG
3061 (when keep-date
3062 (when (and (not (null modtime))
3063 (not (equal modtime '(0 0))))
01917a18
MA
3064 (tramp-touch newname modtime)))
3065 ;; Set the mode.
3066 (set-file-modes newname (file-modes filename)))
90dc758d
KG
3067 ;; If the operation was `rename', delete the original file.
3068 (unless (eq op 'copy)
3069 (delete-file filename))))
fb7933a3
KG
3070
3071(defun tramp-do-copy-or-rename-file-directly
7432277c 3072 (op multi-method method user host localname1 localname2 keep-date)
fb7933a3
KG
3073 "Invokes `cp' or `mv' on the remote system.
3074OP must be one of `copy' or `rename', indicating `cp' or `mv',
3075respectively. METHOD, USER, and HOST specify the connection.
7432277c 3076LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
fb7933a3
KG
3077If KEEP-DATE is non-nil, preserve the time stamp when copying."
3078 ;; CCC: What happens to the timestamp when renaming?
3079 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3080 ((eq op 'copy) "cp -f")
3081 ((eq op 'rename) "mv -f")
3082 (t (error
3083 "Unknown operation `%s', must be `copy' or `rename'"
3084 op)))))
3085 (save-excursion
01917a18 3086 (tramp-send-command
fb7933a3
KG
3087 multi-method method user host
3088 (format "%s %s %s"
3089 cmd
7432277c 3090 (tramp-shell-quote-argument localname1)
01917a18
MA
3091 (tramp-shell-quote-argument localname2)))
3092 (tramp-wait-for-output)
3093 (goto-char (point-min))
3094 (unless
3095 (or
3096 (and (eq op 'copy) keep-date
3097 ;; Mask cp -f error.
3098 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3099 (zerop (tramp-send-command-and-check
3100 multi-method method user host nil nil)))
3101 (pop-to-buffer (current-buffer))
3102 (signal 'file-error
3103 (format "Copying directly failed, see buffer `%s' for details."
3104 (buffer-name)))))
3105 ;; Set the mode.
3106 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3107 ;; where available?
3108 (unless (or (eq op 'rename) keep-date)
3109 (set-file-modes
3110 (tramp-make-tramp-file-name multi-method method user host localname2)
3111 (file-modes
3112 (tramp-make-tramp-file-name
3113 multi-method method user host localname1))))))
fb7933a3 3114
5ec2cc41 3115(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
7432277c
KG
3116 "Invoke rcp program to copy.
3117One of FILENAME and NEWNAME must be a Tramp name, the other must
3118be a local filename. The method used must be an out-of-band method."
38c65fca 3119 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41
KG
3120 (t2 (tramp-tramp-file-p newname))
3121 v1-multi-method v1-method v1-user v1-host v1-localname
3122 v2-multi-method v2-method v2-user v2-host v2-localname
38c65fca
KG
3123 multi-method method user host copy-program copy-args
3124 source target trampbuf)
5ec2cc41
KG
3125
3126 ;; Check which ones of source and target are Tramp files.
3127 ;; We cannot invoke `with-parsed-tramp-file-name';
3128 ;; it fails if the file isn't a Tramp file name.
3129 (if t1
3130 (with-parsed-tramp-file-name filename l
3131 (setq v1-multi-method l-multi-method
3132 v1-method l-method
3133 v1-user l-user
3134 v1-host l-host
3135 v1-localname l-localname
38c65fca 3136 multi-method l-multi-method
5ec2cc41
KG
3137 method (tramp-find-method
3138 v1-multi-method v1-method v1-user v1-host)
38c65fca
KG
3139 user l-user
3140 host l-host
5ec2cc41
KG
3141 copy-program (tramp-get-method-parameter
3142 v1-multi-method method
3143 v1-user v1-host 'tramp-copy-program)
3144 copy-args (tramp-get-method-parameter
3145 v1-multi-method method
3146 v1-user v1-host 'tramp-copy-args)))
3147 (setq v1-localname filename))
3148
3149 (if t2
3150 (with-parsed-tramp-file-name newname l
3151 (setq v2-multi-method l-multi-method
3152 v2-method l-method
3153 v2-user l-user
3154 v2-host l-host
3155 v2-localname l-localname
38c65fca 3156 multi-method l-multi-method
5ec2cc41
KG
3157 method (tramp-find-method
3158 v2-multi-method v2-method v2-user v2-host)
38c65fca
KG
3159 user l-user
3160 host l-host
5ec2cc41
KG
3161 copy-program (tramp-get-method-parameter
3162 v2-multi-method method
3163 v2-user v2-host 'tramp-copy-program)
3164 copy-args (tramp-get-method-parameter
3165 v2-multi-method method
3166 v2-user v2-host 'tramp-copy-args)))
3167 (setq v2-localname newname))
3168
3169 ;; The following should be changed. We need a more general
3170 ;; mechanism to parse extra host args.
3171 (if (not t1)
3172 (setq source v1-localname)
3173 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3174 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3175 (setq v1-host (match-string 1 v1-host)))
3176 (setq source
3177 (tramp-make-copy-program-file-name
3178 v1-user v1-host
3179 (tramp-shell-quote-argument v1-localname))))
3180
3181 (if (not t2)
3182 (setq target v2-localname)
3183 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3184 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3185 (setq v2-host (match-string 1 v2-host)))
3186 (setq target
3187 (tramp-make-copy-program-file-name
3188 v2-user v2-host
3189 (tramp-shell-quote-argument v2-localname))))
3190
57176422
MA
3191 ;; Handle ControlMaster/ControlPath
3192 (setq copy-args
3193 (mapcar
3194 (lambda (x)
3195 (format-spec
3196 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
3197 copy-args))
3198
5ec2cc41
KG
3199 ;; Handle keep-date argument
3200 (when keep-date
3201 (if t1
3202 (setq copy-args
3203 (cons (tramp-get-method-parameter
3204 v1-multi-method method
3205 v1-user v1-host 'tramp-copy-keep-date-arg)
3206 copy-args))
3207 (setq copy-args
3208 (cons (tramp-get-method-parameter
3209 v2-multi-method method
3210 v2-user v2-host 'tramp-copy-keep-date-arg)
3211 copy-args))))
3212
38c65fca
KG
3213 (setq copy-args (append copy-args (list source target))
3214 trampbuf (generate-new-buffer
3215 (tramp-buffer-name multi-method method user host)))
5ec2cc41 3216
38c65fca
KG
3217 ;; Use an asynchronous process. By this, password can be handled.
3218 (save-excursion
c08e6004
MA
3219
3220 ;; Check for program.
3221 (when (and (fboundp 'executable-find)
3222 (not (executable-find copy-program)))
3223 (error "Cannot find copy program: %s" copy-program))
3224
38c65fca
KG
3225 (set-buffer trampbuf)
3226 (setq tramp-current-multi-method multi-method
3227 tramp-current-method method
3228 tramp-current-user user
3229 tramp-current-host host)
d2a2c17f 3230 (message "Transferring %s to %s..." filename newname)
38c65fca
KG
3231
3232 ;; Use rcp-like program for file transfer.
6b2633cc
LH
3233 (unwind-protect
3234 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3235 copy-program copy-args)))
3236 (tramp-set-process-query-on-exit-flag p nil)
3237 (tramp-process-actions p multi-method method user host
3238 tramp-actions-copy-out-of-band))
3239 (kill-buffer trampbuf))
d2a2c17f 3240 (message "Transferring %s to %s...done" filename newname)
01917a18
MA
3241
3242 ;; Set the mode.
3243 (unless keep-date
3244 (set-file-modes newname (file-modes filename))))
5ec2cc41
KG
3245
3246 ;; If the operation was `rename', delete the original file.
3247 (unless (eq op 'copy)
3248 (delete-file filename))))
7432277c 3249
fb7933a3
KG
3250;; mkdir
3251(defun tramp-handle-make-directory (dir &optional parents)
3252 "Like `make-directory' for tramp files."
ac474af1 3253 (setq dir (expand-file-name dir))
c62c9d08 3254 (with-parsed-tramp-file-name dir nil
b1d06e75
KG
3255 (save-excursion
3256 (tramp-barf-unless-okay
3257 multi-method method user host
3258 (format " %s %s"
3259 (if parents "mkdir -p" "mkdir")
7432277c 3260 (tramp-shell-quote-argument localname))
b1d06e75
KG
3261 nil 'file-error
3262 "Couldn't make directory %s" dir))))
fb7933a3
KG
3263
3264;; CCC error checking?
3265(defun tramp-handle-delete-directory (directory)
3266 "Like `delete-directory' for tramp files."
ac474af1 3267 (setq directory (expand-file-name directory))
c62c9d08 3268 (with-parsed-tramp-file-name directory nil
fb7933a3
KG
3269 (save-excursion
3270 (tramp-send-command
c62c9d08 3271 multi-method method user host
fb7933a3 3272 (format "rmdir %s ; echo ok"
7432277c 3273 (tramp-shell-quote-argument localname)))
fb7933a3
KG
3274 (tramp-wait-for-output))))
3275
3276(defun tramp-handle-delete-file (filename)
3277 "Like `delete-file' for tramp files."
ac474af1 3278 (setq filename (expand-file-name filename))
c62c9d08 3279 (with-parsed-tramp-file-name filename nil
487fa986
KG
3280 (save-excursion
3281 (unless (zerop (tramp-send-command-and-check
3282 multi-method method user host
3283 (format "rm -f %s"
7432277c 3284 (tramp-shell-quote-argument localname))))
487fa986 3285 (signal 'file-error "Couldn't delete Tramp file")))))
fb7933a3
KG
3286
3287;; Dired.
3288
3289;; CCC: This does not seem to be enough. Something dies when
3290;; we try and delete two directories under TRAMP :/
3291(defun tramp-handle-dired-recursive-delete-directory (filename)
3292 "Recursively delete the directory given.
3293This is like `dired-recursive-delete-directory' for tramp files."
c62c9d08 3294 (with-parsed-tramp-file-name filename nil
7432277c 3295 ;; run a shell command 'rm -r <localname>'
fb7933a3 3296 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
07dfe738 3297 (or (file-exists-p filename)
fb7933a3
KG
3298 (signal
3299 'file-error
3300 (list "Removing old file name" "no such directory" filename)))
3301 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
bf247b6e 3302 (tramp-send-command multi-method method user host
7432277c 3303 (format "rm -r %s" (tramp-shell-quote-argument localname)))
fb7933a3
KG
3304 ;; Wait for the remote system to return to us...
3305 ;; This might take a while, allow it plenty of time.
3306 (tramp-wait-for-output 120)
3307 ;; Make sure that it worked...
07dfe738 3308 (and (file-exists-p filename)
c08e6004 3309 (error "Failed to recursively delete %s" filename))))
bf247b6e 3310
fb7933a3
KG
3311(defun tramp-handle-dired-call-process (program discard &rest arguments)
3312 "Like `dired-call-process' for tramp files."
c62c9d08 3313 (with-parsed-tramp-file-name default-directory nil
fb7933a3
KG
3314 (save-excursion
3315 (tramp-barf-unless-okay
3316 multi-method method user host
7432277c 3317 (format "cd %s" (tramp-shell-quote-argument localname))
fb7933a3
KG
3318 nil 'file-error
3319 "tramp-handle-dired-call-process: Couldn't `cd %s'"
7432277c 3320 (tramp-shell-quote-argument localname))
fb7933a3
KG
3321 (tramp-send-command
3322 multi-method method user host
3323 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3324 (tramp-wait-for-output))
3325 (unless discard
7177e2a3
MA
3326 ;; We cannot use `insert-buffer' because the tramp buffer
3327 ;; changes its contents before insertion due to calling
3328 ;; `expand-file' and alike.
3329 (insert
3330 (with-current-buffer
3331 (tramp-get-buffer multi-method method user host)
3332 (buffer-string))))
fb7933a3
KG
3333 (save-excursion
3334 (prog1
3335 (tramp-send-command-and-check multi-method method user host nil)
3336 (tramp-send-command multi-method method user host "cd")
3337 (tramp-wait-for-output)))))
bf247b6e 3338
5ec2cc41
KG
3339(defun tramp-handle-dired-compress-file (file &rest ok-flag)
3340 "Like `dired-compress-file' for tramp files."
3341 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3342 ;; Code stolen mainly from dired-aux.el.
3343 (with-parsed-tramp-file-name file nil
3344 (save-excursion
3345 (let ((suffixes
3346 (if (not (featurep 'xemacs))
3347 ;; Emacs case
3348 (symbol-value 'dired-compress-file-suffixes)
3349 ;; XEmacs has `dired-compression-method-alist', which is
3350 ;; transformed into `dired-compress-file-suffixes' structure.
3351 (mapcar
3352 '(lambda (x)
3353 (list (concat (regexp-quote (nth 1 x)) "\\'")
3354 nil
3355 (mapconcat 'identity (nth 3 x) " ")))
3356 (symbol-value 'dired-compression-method-alist))))
3357 suffix)
3358 ;; See if any suffix rule matches this file name.
3359 (while suffixes
3360 (let (case-fold-search)
3361 (if (string-match (car (car suffixes)) localname)
3362 (setq suffix (car suffixes) suffixes nil))
3363 (setq suffixes (cdr suffixes))))
3364
3365 (cond ((file-symlink-p file)
3366 nil)
3367 ((and suffix (nth 2 suffix))
3368 ;; We found an uncompression rule.
3369 (message "Uncompressing %s..." file)
3370 (when (zerop (tramp-send-command-and-check
3371 multi-method method user host
3372 (concat (nth 2 suffix) " " localname)))
3373 (message "Uncompressing %s...done" file)
38c65fca
KG
3374 ;; `dired-remove-file' is not defined in XEmacs
3375 (funcall (symbol-function 'dired-remove-file) file)
5ec2cc41
KG
3376 (string-match (car suffix) file)
3377 (concat (substring file 0 (match-beginning 0)))))
3378 (t
3379 ;; We don't recognize the file as compressed, so compress it.
3380 ;; Try gzip.
3381 (message "Compressing %s..." file)
3382 (when (zerop (tramp-send-command-and-check
3383 multi-method method user host
3384 (concat "gzip -f " localname)))
3385 (message "Compressing %s...done" file)
38c65fca
KG
3386 ;; `dired-remove-file' is not defined in XEmacs
3387 (funcall (symbol-function 'dired-remove-file) file)
5ec2cc41
KG
3388 (cond ((file-exists-p (concat file ".gz"))
3389 (concat file ".gz"))
3390 ((file-exists-p (concat file ".z"))
3391 (concat file ".z"))
3392 (t nil)))))))))
fb7933a3
KG
3393
3394;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3395;; not sure at all that this is the right way to do it, but let's hope
3396;; it works for now, and wait for a guru to point out the Right Way to
3397;; achieve this.
3398;;(eval-when-compile
3399;; (unless (fboundp 'dired-insert-set-properties)
3400;; (fset 'dired-insert-set-properties 'ignore)))
3401;; Gerd suggests this:
3402(eval-when-compile (require 'dired))
3403;; Note that dired is required at run-time, too, when it is needed.
3404;; It is only needed on XEmacs for the function
3405;; `dired-insert-set-properties'.
3406
3407(defun tramp-handle-insert-directory
3408 (filename switches &optional wildcard full-directory-p)
3409 "Like `insert-directory' for tramp files."
c82c5727 3410 (if (and (boundp 'ls-lisp-use-insert-directory-program)
d2a2c17f 3411 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
c82c5727
LH
3412 (tramp-run-real-handler 'insert-directory
3413 (list filename switches wildcard full-directory-p))
3414 ;; For the moment, we assume that the remote "ls" program does not
3415 ;; grok "--dired". In the future, we should detect this on
3416 ;; connection setup.
3417 (when (string-match "^--dired\\s-+" switches)
3418 (setq switches (replace-match "" nil t switches)))
3419 (setq filename (expand-file-name filename))
3420 (with-parsed-tramp-file-name filename nil
3421 (tramp-message-for-buffer
3422 multi-method method user host 10
3423 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3424 switches filename (if wildcard "yes" "no")
3425 (if full-directory-p "yes" "no"))
3426 (when wildcard
3427 (setq wildcard (file-name-nondirectory localname))
3428 (setq localname (file-name-directory localname)))
3429 (when (listp switches)
3430 (setq switches (mapconcat 'identity switches " ")))
3431 (unless full-directory-p
3432 (setq switches (concat "-d " switches)))
3433 (when wildcard
3434 (setq switches (concat switches " " wildcard)))
fb7933a3 3435 (save-excursion
c82c5727
LH
3436 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3437 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3438 (if full-directory-p
3439 (tramp-send-command
3440 multi-method method user host
3441 (format "%s %s %s"
3442 (tramp-get-ls-command multi-method method user host)
3443 switches
3444 (if wildcard
3445 localname
3446 (tramp-shell-quote-argument (concat localname ".")))))
3447 (tramp-barf-unless-okay
3448 multi-method method user host
3449 (format "cd %s" (tramp-shell-quote-argument
3450 (file-name-directory localname)))
3451 nil 'file-error
3452 "Couldn't `cd %s'"
3453 (tramp-shell-quote-argument (file-name-directory localname)))
3454 (tramp-send-command
3455 multi-method method user host
3456 (format "%s %s %s"
3457 (tramp-get-ls-command multi-method method user host)
3458 switches
3459 (if wildcard
3460 localname
7177e2a3
MA
3461 (if (zerop (length (file-name-nondirectory localname)))
3462 ""
3463 (tramp-shell-quote-argument
3464 (file-name-nondirectory localname)))))))
c82c5727
LH
3465 (sit-for 1) ;needed for rsh but not ssh?
3466 (tramp-wait-for-output))
3467 ;; The following let-binding is used by code that's commented
3468 ;; out. Let's leave the let-binding in for a while to see
3469 ;; that the commented-out code is really not needed. Commenting-out
3470 ;; happened on 2003-03-13.
3471 (let ((old-pos (point)))
7177e2a3
MA
3472 ;; We cannot use `insert-buffer' because the tramp buffer
3473 ;; changes its contents before insertion due to calling
3474 ;; `expand-file' and alike.
3475 (insert
3476 (with-current-buffer
3477 (tramp-get-buffer multi-method method user host)
3478 (buffer-string)))
c82c5727
LH
3479 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3480 ;; that doesn't exist on Emacs, so we use this workaround instead.
3481 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3482 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3483 ;; (let ((zmacs-region-stays t))
3484 ;; (exchange-point-and-mark))
3485 (save-excursion
3486 (tramp-send-command multi-method method user host "cd")
3487 (tramp-wait-for-output))
3488 ;; For the time being, the XEmacs kludge is commented out.
3489 ;; Please test it on various XEmacs versions to see if it works.
3490 ;; ;; Another XEmacs specialty follows. What's the right way to do
3491 ;; ;; it?
3492 ;; (when (and (featurep 'xemacs)
3493 ;; (eq major-mode 'dired-mode))
3494 ;; (save-excursion
3495 ;; (require 'dired)
3496 ;; (dired-insert-set-properties old-pos (point))))
3497 ))))
fb7933a3
KG
3498
3499;; Continuation of kluge to pacify byte-compiler.
3500;;(eval-when-compile
3501;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3502;; (fmakunbound 'dired-insert-set-properties)))
3503
3504;; CCC is this the right thing to do?
3505(defun tramp-handle-unhandled-file-name-directory (filename)
3506 "Like `unhandled-file-name-directory' for tramp files."
c62c9d08 3507 (with-parsed-tramp-file-name filename nil
c62c9d08 3508 (expand-file-name "~/")))
fb7933a3
KG
3509
3510;; Canonicalization of file names.
3511
3512(defun tramp-drop-volume-letter (name)
3513 "Cut off unnecessary drive letter from file NAME.
3514The function `tramp-handle-expand-file-name' calls `expand-file-name'
3515locally on a remote file name. When the local system is a W32 system
3516but the remote system is Unix, this introduces a superfluous drive
3517letter into the file name. This function removes it.
3518
3519Doesn't do anything if the NAME does not start with a drive letter."
3520 (if (and (> (length name) 1)
3521 (char-equal (aref name 1) ?:)
3522 (let ((c1 (aref name 0)))
3523 (or (and (>= c1 ?A) (<= c1 ?Z))
3524 (and (>= c1 ?a) (<= c1 ?z)))))
3525 (substring name 2)
3526 name))
3527
3528(defun tramp-handle-expand-file-name (name &optional dir)
7432277c
KG
3529 "Like `expand-file-name' for tramp files.
3530If the localname part of the given filename starts with \"/../\" then
3531the result will be a local, non-Tramp, filename."
fb7933a3
KG
3532 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3533 (setq dir (or dir default-directory "/"))
3534 ;; Unless NAME is absolute, concat DIR and NAME.
3535 (unless (file-name-absolute-p name)
3536 (setq name (concat (file-name-as-directory dir) name)))
3537 ;; If NAME is not a tramp file, run the real handler
3538 (if (not (tramp-tramp-file-p name))
3539 (tramp-run-real-handler 'expand-file-name
3540 (list name nil))
3541 ;; Dissect NAME.
c62c9d08 3542 (with-parsed-tramp-file-name name nil
7432277c
KG
3543 (unless (file-name-absolute-p localname)
3544 (setq localname (concat "~/" localname)))
fb7933a3
KG
3545 (save-excursion
3546 ;; Tilde expansion if necessary. This needs a shell which
3547 ;; groks tilde expansion! The function `tramp-find-shell' is
3548 ;; supposed to find such a shell on the remote host. Please
3549 ;; tell me about it when this doesn't work on your system.
7432277c
KG
3550 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3551 (let ((uname (match-string 1 localname))
3552 (fname (match-string 2 localname)))
a69c01a0
MA
3553 ;; We cannot simply apply "~/", because under sudo "~/" is
3554 ;; expanded to the local user home directory but to the
3555 ;; root home directory. On the other hand, using always
3556 ;; the default user name for tilde expansion is not
3557 ;; appropriate either, because ssh and companions might
3558 ;; use a user name from the config file.
3559 (when (and (string-equal uname "~")
3560 (string-match
3561 "\\`su\\(do\\)?\\'"
3562 (tramp-find-method multi-method method user host)))
3563 (setq uname (concat uname (or user "root"))))
fb7933a3
KG
3564 ;; CCC fanatic error checking?
3565 (set-buffer (tramp-get-buffer multi-method method user host))
3566 (erase-buffer)
3567 (tramp-send-command
3568 multi-method method user host
3569 (format "cd %s; pwd" uname)
3570 t)
3571 (tramp-wait-for-output)
3572 (goto-char (point-min))
3573 (setq uname (buffer-substring (point) (tramp-line-end-position)))
7432277c 3574 (setq localname (concat uname fname))
fb7933a3 3575 (erase-buffer)))
a69c01a0
MA
3576 ;; There might be a double slash, for example when "~/"
3577 ;; expands to "/". Remove this.
3578 (while (string-match "//" localname)
3579 (setq localname (replace-match "/" t t localname)))
b1a2b924
KG
3580 ;; No tilde characters in file name, do normal
3581 ;; expand-file-name (this does "/./" and "/../"). We bind
a69c01a0
MA
3582 ;; directory-sep-char here for XEmacs on Windows, which would
3583 ;; otherwise use backslash. `default-directory' is bound to
3584 ;; "/", because on Windows there would be problems with UNC
3585 ;; shares or Cygwin mounts.
19a87064 3586 (tramp-let-maybe directory-sep-char ?/
a69c01a0
MA
3587 (let ((default-directory "/"))
3588 (tramp-make-tramp-file-name
3589 multi-method (or method (tramp-find-default-method user host))
3590 user host
3591 (tramp-drop-volume-letter
3592 (tramp-run-real-handler 'expand-file-name
3593 (list localname))))))))))
b1a2b924
KG
3594
3595;; old version follows. it uses ".." to cross file handler
3596;; boundaries.
3597;; ;; Look if localname starts with "/../" construct. If this is
3598;; ;; the case, then we return a local name instead of a remote name.
3599;; (if (string-match "^/\\.\\./" localname)
3600;; (expand-file-name (substring localname 3))
3601;; ;; No tilde characters in file name, do normal
3602;; ;; expand-file-name (this does "/./" and "/../"). We bind
3603;; ;; directory-sep-char here for XEmacs on Windows, which
3604;; ;; would otherwise use backslash.
3605;; (let ((directory-sep-char ?/))
3606;; (tramp-make-tramp-file-name
3607;; multi-method method user host
3608;; (tramp-drop-volume-letter
3609;; (tramp-run-real-handler 'expand-file-name
3610;; (list localname))))))))))
fb7933a3
KG
3611
3612;; Remote commands.
3613
5ec2cc41 3614(defvar tramp-async-proc nil
d2a2c17f 3615 "Global variable keeping asynchronous process object.
5ec2cc41
KG
3616Used in `tramp-handle-shell-command'")
3617
32f9593c
MA
3618(defvar tramp-display-shell-command-buffer t
3619 "Whether to display output buffer of `shell-command'.
3620This is necessary for handling DISPLAY of `process-file'.")
3621
fb7933a3
KG
3622(defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3623 "Like `shell-command' for tramp files.
3624This will break if COMMAND prints a newline, followed by the value of
3625`tramp-end-of-output', followed by another newline."
5ec2cc41 3626 ;; Asynchronous processes are far from being perfect. But it works at least
340b8d4f 3627 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
487f4fb7
KG
3628 (if (tramp-tramp-file-p default-directory)
3629 (with-parsed-tramp-file-name default-directory nil
d163f71e
MA
3630 (let ((curbuf (current-buffer))
3631 (asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
5ec2cc41
KG
3632 status)
3633 (unless output-buffer
3634 (setq output-buffer
3635 (get-buffer-create
3636 (if asynchronous
3637 "*Async Shell Command*"
3638 "*Shell Command Output*")))
3639 (set-buffer output-buffer)
3640 (erase-buffer))
3641 (unless (bufferp output-buffer)
3642 (setq output-buffer (current-buffer)))
3643 (set-buffer output-buffer)
3644 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3645 ;; process. Instead of, another asynchronous process is opened
3646 ;; which gets the output of the (synchronous) Tramp process
3647 ;; via process-filter. ERROR-BUFFER is disabled.
3648 (when asynchronous
3649 (setq command (substring command 0 (match-beginning 0))
3650 error-buffer nil
3651 tramp-async-proc (start-process (buffer-name output-buffer)
3652 output-buffer "cat")))
487f4fb7
KG
3653 (save-excursion
3654 (tramp-barf-unless-okay
3655 multi-method method user host
7432277c 3656 (format "cd %s" (tramp-shell-quote-argument localname))
487f4fb7
KG
3657 nil 'file-error
3658 "tramp-handle-shell-command: Couldn't `cd %s'"
7432277c 3659 (tramp-shell-quote-argument localname))
5ec2cc41
KG
3660 ;; Define the process filter
3661 (when asynchronous
3662 (set-process-filter
3663 (get-buffer-process
3664 (tramp-get-buffer multi-method method user host))
3665 '(lambda (process string)
3666 ;; Write the output into the Tramp Process
3667 (save-current-buffer
3668 (set-buffer (process-buffer process))
3669 (goto-char (point-max))
3670 (insert string))
3671 ;; Hand-over output to asynchronous process.
3672 (let ((end
3673 (string-match
3674 (regexp-quote tramp-end-of-output) string)))
3675 (when end
3676 (setq string
3677 (substring string 0 (1- (match-beginning 0)))))
3678 (process-send-string tramp-async-proc string)
3679 (when end
3680 (set-process-filter process nil)
3681 (process-send-eof tramp-async-proc))))))
3682 ;; Send the command
90f8dc03
KG
3683 (tramp-send-command
3684 multi-method method user host
3685 (if error-buffer
3686 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3687 command)
5ec2cc41
KG
3688 (format "%s; tramp_old_status=$?" command)))
3689 (unless asynchronous
3690 (tramp-wait-for-output)))
3691 (unless asynchronous
7177e2a3
MA
3692 ;; We cannot use `insert-buffer' because the tramp buffer
3693 ;; changes its contents before insertion due to calling
3694 ;; `expand-file' and alike.
3695 (insert
3696 (with-current-buffer
3697 (tramp-get-buffer multi-method method user host)
3698 (buffer-string))))
90f8dc03
KG
3699 (when error-buffer
3700 (save-excursion
3701 (unless (bufferp error-buffer)
3702 (setq error-buffer (get-buffer-create error-buffer)))
3703 (tramp-send-command
3704 multi-method method user host
3705 "cat /tmp/tramp.$$.err")
3706 (tramp-wait-for-output)
3707 (set-buffer error-buffer)
7177e2a3
MA
3708 ;; Same comment as above
3709 (insert
3710 (with-current-buffer
3711 (tramp-get-buffer multi-method method user host)
3712 (buffer-string)))
90f8dc03
KG
3713 (tramp-send-command-and-check
3714 multi-method method user host "rm -f /tmp/tramp.$$.err")))
487f4fb7
KG
3715 (save-excursion
3716 (tramp-send-command multi-method method user host "cd")
5ec2cc41
KG
3717 (unless asynchronous
3718 (tramp-wait-for-output))
487f4fb7
KG
3719 (tramp-send-command
3720 multi-method method user host
3721 (concat "tramp_set_exit_status $tramp_old_status;"
3722 " echo tramp_exit_status $?"))
5ec2cc41
KG
3723 (unless asynchronous
3724 (tramp-wait-for-output)
3725 (goto-char (point-max))
3726 (unless (search-backward "tramp_exit_status " nil t)
3727 (error "Couldn't find exit status of `%s'" command))
3728 (skip-chars-forward "^ ")
3729 (setq status (read (current-buffer)))))
487f4fb7 3730 (unless (zerop (buffer-size))
32f9593c
MA
3731 (when tramp-display-shell-command-buffer
3732 (display-buffer output-buffer)))
d163f71e 3733 (set-buffer curbuf)
487f4fb7
KG
3734 status))
3735 ;; The following is only executed if something strange was
3736 ;; happening. Emit a helpful message and do it anyway.
3737 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3738 default-directory)
3739 (tramp-run-real-handler 'shell-command
3740 (list command output-buffer error-buffer))))
fb7933a3 3741
0457dd55
KG
3742(defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3743 "Like `process-file' for Tramp files."
3744 (when infile (error "Implementation does not handle input from file"))
3745 (when (and (numberp buffer) (zerop buffer))
3746 (error "Implementation does not handle immediate return"))
3747 (when (consp buffer) (error "Implementation does not handle error files"))
32f9593c
MA
3748 (let ((tramp-display-shell-command-buffer display))
3749 (shell-command
3750 (mapconcat 'tramp-shell-quote-argument (cons program args) " ")
3751 buffer)))
0457dd55 3752
fb7933a3
KG
3753;; File Editing.
3754
3755(defsubst tramp-make-temp-file ()
3756 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3757 (expand-file-name tramp-temp-name-prefix
3758 (tramp-temporary-file-directory))))
3759
3760(defun tramp-handle-file-local-copy (filename)
3761 "Like `file-local-copy' for tramp files."
c62c9d08 3762 (with-parsed-tramp-file-name filename nil
5ec2cc41 3763 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3b89d388
KG
3764 ;; We used to bind the following as late as possible.
3765 ;; loc-enc and loc-dec were bound directly before the if
3766 ;; statement that checks them. But the functions
3767 ;; tramp-get-* might invoke the "are you awake" check in
3768 ;; tramp-maybe-open-connection, which is an unfortunate time
3769 ;; since we rely on the buffer contents at that spot.
3770 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3771 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3772 (loc-enc (tramp-get-local-encoding multi-method method user host))
3773 (loc-dec (tramp-get-local-decoding multi-method method user host))
c62c9d08
KG
3774 tmpfil)
3775 (unless (file-exists-p filename)
3776 (error "Cannot make local copy of non-existing file `%s'"
3777 filename))
3778 (setq tmpfil (tramp-make-temp-file))
5ec2cc41 3779
5ec2cc41
KG
3780 (cond ((tramp-method-out-of-band-p multi-method method user host)
3781 ;; `copy-file' handles out-of-band methods
3782 (copy-file filename tmpfil t t))
3783
3b89d388 3784 ((and rem-enc rem-dec)
c62c9d08
KG
3785 ;; Use inline encoding for file transfer.
3786 (save-excursion
3787 ;; Following line for setting tramp-current-method,
3788 ;; tramp-current-user, tramp-current-host.
3b89d388 3789 (set-buffer tramp-buf)
c62c9d08
KG
3790 (tramp-message 5 "Encoding remote file %s..." filename)
3791 (tramp-barf-unless-okay
3792 multi-method method user host
7432277c 3793 (concat rem-enc " < " (tramp-shell-quote-argument localname))
c62c9d08
KG
3794 nil 'file-error
3795 "Encoding remote file failed, see buffer `%s' for details"
3b89d388 3796 tramp-buf)
c62c9d08
KG
3797 ;; Remove trailing status code
3798 (goto-char (point-max))
3799 (delete-region (point) (progn (forward-line -1) (point)))
3800
3801 (tramp-message 5 "Decoding remote file %s..." filename)
16674e4f 3802
3b89d388 3803 ;; Here is where loc-enc and loc-dec used to be let-bound.
16674e4f
KG
3804 (if (and (symbolp loc-dec) (fboundp loc-dec))
3805 ;; If local decoding is a function, we call it.
c62c9d08
KG
3806 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3807 (set-buffer tmpbuf)
3808 (erase-buffer)
f9f51bbf 3809 (insert-buffer-substring tramp-buf)
c62c9d08
KG
3810 (tramp-message-for-buffer
3811 multi-method method user host
3812 6 "Decoding remote file %s with function %s..."
16674e4f 3813 filename loc-dec)
c62c9d08 3814 (set-buffer tmpbuf)
16674e4f
KG
3815 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3816 ;; says that we need to strip tramp_exit_status
3817 ;; line from the output here. Go to point-max,
3818 ;; search backward for tramp_exit_status, delete
3819 ;; between point and point-max if found.
ea9d1443 3820 (let ((coding-system-for-write 'binary))
16674e4f 3821 (funcall loc-dec (point-min) (point-max))
c62c9d08
KG
3822 (write-region (point-min) (point-max) tmpfil))
3823 (kill-buffer tmpbuf))
3824 ;; If tramp-decoding-function is not defined for this
3825 ;; method, we invoke tramp-decoding-command instead.
3826 (let ((tmpfil2 (tramp-make-temp-file)))
3827 (write-region (point-min) (point-max) tmpfil2)
3828 (tramp-message
3829 6 "Decoding remote file %s with command %s..."
16674e4f
KG
3830 filename loc-dec)
3831 (tramp-call-local-coding-command
3832 loc-dec tmpfil2 tmpfil)
c62c9d08
KG
3833 (delete-file tmpfil2)))
3834 (tramp-message-for-buffer
3835 multi-method method user host
38c65fca
KG
3836 5 "Decoding remote file %s...done" filename)
3837 ;; Set proper permissions.
3838 (set-file-modes tmpfil (file-modes filename))))
c62c9d08
KG
3839
3840 (t (error "Wrong method specification for `%s'" method)))
3841 tmpfil)))
fb7933a3 3842
19a87064
MA
3843(defun tramp-handle-file-remote-p (filename)
3844 "Like `file-remote-p' for tramp files."
15cc764c
KG
3845 (when (tramp-tramp-file-p filename)
3846 (with-parsed-tramp-file-name filename nil
3847 (make-tramp-file-name
3848 :multi-method multi-method
3849 :method method
3850 :user user
3851 :host host
3852 :localname ""))))
fb7933a3
KG
3853
3854(defun tramp-handle-insert-file-contents
3855 (filename &optional visit beg end replace)
3856 "Like `insert-file-contents' for tramp files."
3857 (barf-if-buffer-read-only)
3858 (setq filename (expand-file-name filename))
c62c9d08 3859 (with-parsed-tramp-file-name filename nil
4007ba5b 3860 (if (not (file-exists-p filename))
fb7933a3
KG
3861 (progn
3862 (when visit
3863 (setq buffer-file-name filename)
3864 (set-visited-file-modtime)
3865 (set-buffer-modified-p nil))
3866 (signal 'file-error
3867 (format "File `%s' not found on remote host" filename))
4007ba5b 3868 (list (expand-file-name filename) 0))
ea9d1443
KG
3869 ;; `insert-file-contents-literally' takes care to avoid calling
3870 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3871 ;; propagate that care to the file-local-copy operation.
3872 (let ((local-copy
90f8dc03
KG
3873 (let ((inhibit-file-name-operation
3874 (when (eq inhibit-file-name-operation
3875 'insert-file-contents)
3876 'file-local-copy)))
ea9d1443 3877 (file-local-copy filename)))
11948172 3878 coding-system-used result)
fb7933a3
KG
3879 (when visit
3880 (setq buffer-file-name filename)
3881 (set-visited-file-modtime)
3882 (set-buffer-modified-p nil))
3883 (tramp-message-for-buffer
3884 multi-method method user host
3885 9 "Inserting local temp file `%s'..." local-copy)
4007ba5b 3886 (setq result (insert-file-contents local-copy nil beg end replace))
11948172
MA
3887 ;; Now `last-coding-system-used' has right value. Remember it.
3888 (when (boundp 'last-coding-system-used)
3889 (setq coding-system-used (symbol-value 'last-coding-system-used)))
16674e4f
KG
3890 (tramp-message-for-buffer
3891 multi-method method user host
3892 9 "Inserting local temp file `%s'...done" local-copy)
fb7933a3 3893 (delete-file local-copy)
11948172
MA
3894 (when (boundp 'last-coding-system-used)
3895 (set 'last-coding-system-used coding-system-used))
fb7933a3
KG
3896 (list (expand-file-name filename)
3897 (second result))))))
3898
38c65fca
KG
3899
3900(defun tramp-handle-find-backup-file-name (filename)
3901 "Like `find-backup-file-name' for tramp files."
07dfe738
KG
3902 (with-parsed-tramp-file-name filename nil
3903 ;; We set both variables. It doesn't matter whether it is
3904 ;; Emacs or XEmacs
3905 (let ((backup-directory-alist
3906 ;; Emacs case
3907 (when (boundp 'backup-directory-alist)
3908 (if (boundp 'tramp-backup-directory-alist)
3909 (mapcar
3910 '(lambda (x)
3911 (cons
3912 (car x)
3913 (if (and (stringp (cdr x))
3914 (file-name-absolute-p (cdr x))
3915 (not (tramp-file-name-p (cdr x))))
3916 (tramp-make-tramp-file-name
3917 multi-method method user host (cdr x))
3918 (cdr x))))
3919 (symbol-value 'tramp-backup-directory-alist))
3920 (symbol-value 'backup-directory-alist))))
3921
3922 (bkup-backup-directory-info
3923 ;; XEmacs case
3924 (when (boundp 'bkup-backup-directory-info)
3925 (if (boundp 'tramp-bkup-backup-directory-info)
3926 (mapcar
3927 '(lambda (x)
3928 (nconc
3929 (list (car x))
3930 (list
3931 (if (and (stringp (car (cdr x)))
3932 (file-name-absolute-p (car (cdr x)))
3933 (not (tramp-file-name-p (car (cdr x)))))
3934 (tramp-make-tramp-file-name
3935 multi-method method user host (car (cdr x)))
3936 (car (cdr x))))
3937 (cdr (cdr x))))
3938 (symbol-value 'tramp-bkup-backup-directory-info))
3939 (symbol-value 'bkup-backup-directory-info)))))
3940
3941 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
38c65fca 3942
c1105d05
MA
3943(defun tramp-handle-make-auto-save-file-name ()
3944 "Like `make-auto-save-file-name' for tramp files.
3945Returns a file name in `tramp-auto-save-directory' for autosaving this file."
1a762140
MA
3946 (let ((tramp-auto-save-directory tramp-auto-save-directory))
3947 ;; File name must be unique. This is ensured with Emacs 22 (see
3948 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3949 ;; all other cases we must do it ourselves.
3950 (when (boundp 'auto-save-file-name-transforms)
3951 (mapcar
3952 '(lambda (x)
3953 (when (and (string-match (car x) buffer-file-name)
3954 (not (car (cddr x))))
3955 (setq tramp-auto-save-directory
3956 (or tramp-auto-save-directory temporary-file-directory))))
3957 (symbol-value 'auto-save-file-name-transforms)))
3958 ;; Create directory.
3959 (when tramp-auto-save-directory
3960 (unless (file-exists-p tramp-auto-save-directory)
3961 (make-directory tramp-auto-save-directory t)))
3962 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3963 ;; file name we make sure that jka-compr isn't used for the
3964 ;; auto-save file.
3965 (let ((buffer-file-name
3966 (if tramp-auto-save-directory
3967 (expand-file-name
3968 (tramp-subst-strs-in-string
3969 '(("_" . "|")
3970 ("/" . "_a")
3971 (":" . "_b")
3972 ("|" . "__")
3973 ("[" . "_l")
3974 ("]" . "_r"))
3975 (buffer-file-name))
3976 tramp-auto-save-directory)
3977 (buffer-file-name))))
3978 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3979 ;; it is not a magic file name operation (since Emacs 22).
3980 ;; We must deactivate it temporarily.
3981 (if (not (ad-is-active 'make-auto-save-file-name))
3982 (tramp-run-real-handler
3983 'make-auto-save-file-name nil)
3984 ;; else
3985 (ad-deactivate 'make-auto-save-file-name)
3986 (prog1
3987 (tramp-run-real-handler
3988 'make-auto-save-file-name nil)
3989 (ad-activate 'make-auto-save-file-name))))))
c1105d05 3990
38c65fca 3991
fb7933a3
KG
3992;; CCC grok APPEND, LOCKNAME, CONFIRM
3993(defun tramp-handle-write-region
3994 (start end filename &optional append visit lockname confirm)
3995 "Like `write-region' for tramp files."
3996 (unless (eq append nil)
3997 (error "Cannot append to file using tramp (`%s')" filename))
3998 (setq filename (expand-file-name filename))
c62c9d08
KG
3999 ;; Following part commented out because we don't know what to do about
4000 ;; file locking, and it does not appear to be a problem to ignore it.
4001 ;; Ange-ftp ignores it, too.
4002 ;; (when (and lockname (stringp lockname))
4003 ;; (setq lockname (expand-file-name lockname)))
4004 ;; (unless (or (eq lockname nil)
4005 ;; (string= lockname filename))
4006 ;; (error
4007 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
d2a2c17f 4008 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
fb7933a3 4009 (when (and (not (featurep 'xemacs))
c62c9d08 4010 confirm (file-exists-p filename))
fb7933a3
KG
4011 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
4012 filename))
4013 (error "File not overwritten")))
c62c9d08 4014 (with-parsed-tramp-file-name filename nil
c62c9d08 4015 (let ((curbuf (current-buffer))
16674e4f
KG
4016 (rem-enc (tramp-get-remote-encoding multi-method method user host))
4017 (rem-dec (tramp-get-remote-decoding multi-method method user host))
4018 (loc-enc (tramp-get-local-encoding multi-method method user host))
4019 (loc-dec (tramp-get-local-decoding multi-method method user host))
c62c9d08 4020 (trampbuf (get-buffer-create "*tramp output*"))
38c65fca 4021 (modes (file-modes filename))
11948172
MA
4022 ;; We use this to save the value of `last-coding-system-used'
4023 ;; after writing the tmp file. At the end of the function,
4024 ;; we set `last-coding-system-used' to this saved value.
4025 ;; This way, any intermediary coding systems used while
4026 ;; talking to the remote shell or suchlike won't hose this
4027 ;; variable. This approach was snarfed from ange-ftp.el.
4028 coding-system-used
c62c9d08
KG
4029 tmpfil)
4030 ;; Write region into a tmp file. This isn't really needed if we
4031 ;; use an encoding function, but currently we use it always
4032 ;; because this makes the logic simpler.
4033 (setq tmpfil (tramp-make-temp-file))
07dfe738
KG
4034 ;; Set current buffer. If connection wasn't open, `file-modes' has
4035 ;; changed it accidently.
4036 (set-buffer curbuf)
c62c9d08
KG
4037 ;; We say `no-message' here because we don't want the visited file
4038 ;; modtime data to be clobbered from the temp file. We call
4039 ;; `set-visited-file-modtime' ourselves later on.
4040 (tramp-run-real-handler
4041 'write-region
4042 (if confirm ; don't pass this arg unless defined for backward compat.
4043 (list start end tmpfil append 'no-message lockname confirm)
4044 (list start end tmpfil append 'no-message lockname)))
11948172
MA
4045 ;; Now, `last-coding-system-used' has the right value. Remember it.
4046 (when (boundp 'last-coding-system-used)
4047 (setq coding-system-used (symbol-value 'last-coding-system-used)))
38c65fca
KG
4048 ;; The permissions of the temporary file should be set. If
4049 ;; filename does not exist (eq modes nil) it has been renamed to
4050 ;; the backup file. This case `save-buffer' handles
4051 ;; permissions.
4052 (when modes (set-file-modes tmpfil modes))
c62c9d08
KG
4053 ;; This is a bit lengthy due to the different methods possible for
4054 ;; file transfer. First, we check whether the method uses an rcp
4055 ;; program. If so, we call it. Otherwise, both encoding and
4056 ;; decoding command must be specified. However, if the method
4057 ;; _also_ specifies an encoding function, then that is used for
4058 ;; encoding the contents of the tmp file.
5ec2cc41
KG
4059 (cond ((tramp-method-out-of-band-p multi-method method user host)
4060 ;; `copy-file' handles out-of-band methods
4061 (copy-file tmpfil filename t t))
4062
16674e4f 4063 ((and rem-enc rem-dec)
c62c9d08
KG
4064 ;; Use inline file transfer
4065 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4066 (save-excursion
4067 ;; Encode tmpfil into tmpbuf
4068 (tramp-message-for-buffer multi-method method user host
4069 5 "Encoding region...")
4070 (set-buffer tmpbuf)
4071 (erase-buffer)
4072 ;; Use encoding function or command.
16674e4f 4073 (if (and (symbolp loc-enc) (fboundp loc-enc))
c62c9d08
KG
4074 (progn
4075 (tramp-message-for-buffer
4076 multi-method method user host
5ec2cc41
KG
4077 6 "Encoding region using function `%s'..."
4078 (symbol-name loc-enc))
c62c9d08
KG
4079 (insert-file-contents-literally tmpfil)
4080 ;; CCC. The following `let' is a workaround for
4081 ;; the base64.el that comes with pgnus-0.84. If
4082 ;; both of the following conditions are
4083 ;; satisfied, it tries to write to a local file
4084 ;; in default-directory, but at this point,
4085 ;; default-directory is remote.
4086 ;; (CALL-PROCESS-REGION can't write to remote
4087 ;; files, it seems.) The file in question is a
4088 ;; tmp file anyway.
4089 (let ((default-directory
4090 (tramp-temporary-file-directory)))
16674e4f 4091 (funcall loc-enc (point-min) (point-max)))
c62c9d08
KG
4092 (goto-char (point-max))
4093 (unless (bolp)
4094 (newline)))
4095 (tramp-message-for-buffer
4096 multi-method method user host
16674e4f
KG
4097 6 "Encoding region using command `%s'..." loc-enc)
4098 (unless (equal 0 (tramp-call-local-coding-command
4099 loc-enc tmpfil t))
c62c9d08
KG
4100 (pop-to-buffer trampbuf)
4101 (error (concat "Cannot write to `%s', local encoding"
4102 " command `%s' failed")
16674e4f 4103 filename loc-enc)))
c62c9d08
KG
4104 ;; Send tmpbuf into remote decoding command which
4105 ;; writes to remote file. Because this happens on the
4106 ;; remote host, we cannot use the function.
4107 (tramp-message-for-buffer
4108 multi-method method user host
4109 5 "Decoding region into remote file %s..." filename)
4110 (tramp-send-command
4111 multi-method method user host
4112 (format "%s >%s <<'EOF'"
16674e4f 4113 rem-dec
7432277c 4114 (tramp-shell-quote-argument localname)))
c62c9d08
KG
4115 (set-buffer tmpbuf)
4116 (tramp-message-for-buffer
4117 multi-method method user host
4118 6 "Sending data to remote host...")
7432277c
KG
4119 (tramp-send-string multi-method method user host
4120 (buffer-string))
c62c9d08
KG
4121 ;; wait for remote decoding to complete
4122 (tramp-message-for-buffer
4123 multi-method method user host
4124 6 "Sending end of data token...")
4125 (tramp-send-command
3cdaec13 4126 multi-method method user host "EOF" nil t)
c62c9d08
KG
4127 (tramp-message-for-buffer
4128 multi-method method user host 6
4129 "Waiting for remote host to process data...")
4130 (set-buffer (tramp-get-buffer multi-method method user host))
4131 (tramp-wait-for-output)
4132 (tramp-barf-unless-okay
4133 multi-method method user host nil nil 'file-error
4134 (concat "Couldn't write region to `%s',"
4135 " decode using `%s' failed")
16674e4f 4136 filename rem-dec)
c62c9d08
KG
4137 (tramp-message 5 "Decoding region into remote file %s...done"
4138 filename)
4139 (kill-buffer tmpbuf))))
4140 (t
4141 (error
4142 (concat "Method `%s' should specify both encoding and "
4143 "decoding command or an rcp program")
4144 method)))
4145 (delete-file tmpfil)
4146 (unless (equal curbuf (current-buffer))
4147 (error "Buffer has changed from `%s' to `%s'"
4148 curbuf (current-buffer)))
48ddd622
MA
4149 (when (or (eq visit t) (stringp visit))
4150 (set-visited-file-modtime
4151 ;; We must pass modtime explicitely, because filename can be different
4152 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4153 (nth 5 (file-attributes filename))))
11948172
MA
4154 ;; Make `last-coding-system-used' have the right value.
4155 (when (boundp 'last-coding-system-used)
4156 (set 'last-coding-system-used coding-system-used))
c62c9d08
KG
4157 (when (or (eq visit t)
4158 (eq visit nil)
4159 (stringp visit))
4160 (message "Wrote %s" filename)))))
fb7933a3
KG
4161
4162;; Call down to the real handler.
7432277c
KG
4163;; Because EFS does not play nicely with TRAMP (both systems match a
4164;; TRAMP file name) it is needed to disable efs as well as tramp for the
fb7933a3
KG
4165;; operation.
4166;;
4167;; Other than that, this is the canon file-handler code that the doco
4168;; says should be used here. Which is nice.
4169;;
4170;; Under XEmacs current, EFS also hooks in as
7432277c 4171;; efs-sifn-handler-function to handle any filename with environment
fb7933a3 4172;; variables. This has two implications:
7432277c 4173;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
fb7933a3
KG
4174;; 2) That TRAMP might want to do the same thing.
4175;; Details as they come in.
4176;;
4177;; Daniel Pittman <daniel@danann.net>
4178
4179;; (defun tramp-run-real-handler (operation args)
4180;; "Invoke normal file name handler for OPERATION.
4181;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4182;; First arg specifies the OPERATION, remaining ARGS are passed to the
4183;; OPERATION."
4184;; (let ((inhibit-file-name-handlers
4185;; (list 'tramp-file-name-handler
4186;; 'efs-file-handler-function
4187;; 'ange-ftp-hook-function
4188;; (and (eq inhibit-file-name-operation operation)
4189;; inhibit-file-name-handlers)))
4190;; (inhibit-file-name-operation operation))
4191;; (apply operation args)))
4192
a01b1e22
MA
4193;;;###autoload
4194(progn (defun tramp-run-real-handler (operation args)
fb7933a3 4195 "Invoke normal file name handler for OPERATION.
c62c9d08
KG
4196First arg specifies the OPERATION, second arg is a list of arguments to
4197pass to the OPERATION."
4007ba5b
KG
4198 (let* ((inhibit-file-name-handlers
4199 `(tramp-file-name-handler
4200 tramp-completion-file-name-handler
4201 cygwin-mount-name-hook-function
4202 cygwin-mount-map-drive-hook-function
4203 .
4204 ,(and (eq inhibit-file-name-operation operation)
4205 inhibit-file-name-handlers)))
4206 (inhibit-file-name-operation operation))
a01b1e22 4207 (apply operation args))))
16674e4f
KG
4208
4209;; This function is used from `tramp-completion-file-name-handler' functions
4210;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4211;; because the check is based on a full filename, not available for all
4212;; basic I/O operations.
a01b1e22
MA
4213;;;###autoload
4214(progn (defun tramp-completion-run-real-handler (operation args)
16674e4f
KG
4215 "Invoke `tramp-file-name-handler' for OPERATION.
4216First arg specifies the OPERATION, second arg is a list of arguments to
4217pass to the OPERATION."
4007ba5b
KG
4218 (let* ((inhibit-file-name-handlers
4219 `(tramp-completion-file-name-handler
4220 cygwin-mount-name-hook-function
4221 cygwin-mount-map-drive-hook-function
4222 .
4223 ,(and (eq inhibit-file-name-operation operation)
4224 inhibit-file-name-handlers)))
4225 (inhibit-file-name-operation operation))
a01b1e22 4226 (apply operation args))))
fb7933a3 4227
4007ba5b
KG
4228;; We handle here all file primitives. Most of them have the file
4229;; name as first parameter; nevertheless we check for them explicitly
19a87064 4230;; in order to be signalled if a new primitive appears. This
4007ba5b
KG
4231;; scenario is needed because there isn't a way to decide by
4232;; syntactical means whether a foreign method must be called. It would
19a87064 4233;; ease the life if `file-name-handler-alist' would support a decision
4007ba5b
KG
4234;; function as well but regexp only.
4235(defun tramp-file-name-for-operation (operation &rest args)
4236 "Return file name related to OPERATION file primitive.
4237ARGS are the arguments OPERATION has been called with."
4238 (cond
4239 ; FILE resp DIRECTORY
4240 ((member operation
4241 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4242 'delete-file 'diff-latest-backup-file 'directory-file-name
4243 'directory-files 'directory-files-and-attributes
4244 'dired-compress-file 'dired-uncache
4245 'file-accessible-directory-p 'file-attributes
4246 'file-directory-p 'file-executable-p 'file-exists-p
19a87064
MA
4247 'file-local-copy 'file-remote-p 'file-modes
4248 'file-name-as-directory 'file-name-directory
4249 'file-name-nondirectory 'file-name-sans-versions
4250 'file-ownership-preserved-p 'file-readable-p
4251 'file-regular-p 'file-symlink-p 'file-truename
4252 'file-writable-p 'find-backup-file-name 'find-file-noselect
4253 'get-file-buffer 'insert-directory 'insert-file-contents
4254 'load 'make-directory 'make-directory-internal
4255 'set-file-modes 'substitute-in-file-name
4256 'unhandled-file-name-directory 'vc-registered
4007ba5b
KG
4257 ; XEmacs only
4258 'abbreviate-file-name 'create-file-buffer
4259 'dired-file-modtime 'dired-make-compressed-filename
4260 'dired-recursive-delete-directory 'dired-set-file-modtime
4261 'dired-shell-unhandle-file-name 'dired-uucode-file
4262 'insert-file-contents-literally 'recover-file
4263 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
8daea7fc
KG
4264 (if (file-name-absolute-p (nth 0 args))
4265 (nth 0 args)
4266 (expand-file-name (nth 0 args))))
4007ba5b
KG
4267 ; FILE DIRECTORY resp FILE1 FILE2
4268 ((member operation
4269 (list 'add-name-to-file 'copy-file 'expand-file-name
4270 'file-name-all-completions 'file-name-completion
4271 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4272 ; XEmacs only
4273 'dired-make-relative-symlink
4274 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4275 (save-match-data
4276 (cond
4277 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4278 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4279 (t (buffer-file-name (current-buffer))))))
4280 ; START END FILE
4281 ((eq operation 'write-region)
4282 (nth 2 args))
4283 ; BUF
4284 ((member operation
c1105d05
MA
4285 (list 'make-auto-save-file-name
4286 'set-visited-file-modtime 'verify-visited-file-modtime
4287 ; XEmacs only
4007ba5b
KG
4288 'backup-buffer))
4289 (buffer-file-name
4290 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4291 ; COMMAND
4292 ((member operation
74f69b93 4293 (list 'dired-call-process
01917a18 4294 ; Emacs only
b71c9e75 4295 'shell-command
340b8d4f 4296 ; Emacs 22 only
0457dd55 4297 'process-file
4007ba5b
KG
4298 ; XEmacs only
4299 'dired-print-file 'dired-shell-call-process))
4300 default-directory)
4301 ; unknown file primitive
4302 (t (error "unknown file I/O primitive: %s" operation))))
4303
4304(defun tramp-find-foreign-file-name-handler (filename)
4305 "Return foreign file name handler if exists."
8daea7fc 4306 (when (tramp-tramp-file-p filename)
ea9d1443
KG
4307 (let (elt
4308 res
4309 (handler-alist tramp-foreign-file-name-handler-alist))
4310 (while handler-alist
4311 (setq elt (car handler-alist)
4312 handler-alist (cdr handler-alist))
8daea7fc 4313 (when (funcall (car elt) filename)
ea9d1443 4314 (setq handler-alist nil)
8daea7fc
KG
4315 (setq res (cdr elt))))
4316 res)))
4007ba5b 4317
fb7933a3
KG
4318;; Main function.
4319;;;###autoload
4320(defun tramp-file-name-handler (operation &rest args)
ea9d1443 4321 "Invoke Tramp file name handler.
fb7933a3 4322Falls back to normal file name handler if no tramp file name handler exists."
a01b1e22
MA
4323;; (setq edebug-trace t)
4324;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4007ba5b 4325 (save-match-data
ea9d1443 4326 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
a01b1e22 4327 (completion (tramp-completion-mode filename))
4007ba5b 4328 (foreign (tramp-find-foreign-file-name-handler filename)))
a01b1e22
MA
4329 (with-parsed-tramp-file-name filename nil
4330 (cond
4331 ;; When we are in completion mode, some operations shouldn' be
4332 ;; handled by backend.
4333 ((and completion (memq operation '(expand-file-name)))
4334 (tramp-run-real-handler operation args))
4335 ((and completion (zerop (length localname))
4336 (memq operation '(file-exists-p file-directory-p)))
4337 t)
4338 ;; Call the backend function.
4339 (foreign (apply foreign operation args))
4340 ;; Nothing to do for us.
4341 (t (tramp-run-real-handler operation args)))))))
fb7933a3 4342
07dfe738
KG
4343
4344;; In Emacs, there is some concurrency due to timers. If a timer
4345;; interrupts Tramp and wishes to use the same connection buffer as
4346;; the "main" Emacs, then garbage might occur in the connection
4347;; buffer. Therefore, we need to make sure that a timer does not use
4348;; the same connection buffer as the "main" Emacs. We implement a
4349;; cheap global lock, instead of locking each connection buffer
4350;; separately. The global lock is based on two variables,
4351;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4352;; (with setq) to indicate a lock. But Tramp also calls itself during
4353;; processing of a single file operation, so we need to allow
4354;; recursive calls. That's where the `tramp-locker' variable comes in
4355;; -- it is let-bound to t during the execution of the current
4356;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4357;; then we should just proceed because we have been called
4358;; recursively. But if `tramp-locker' is nil, then we are a timer
4359;; interrupting the "main" Emacs, and then we signal an error.
4360
4361(defvar tramp-locked nil
4362 "If non-nil, then Tramp is currently busy.
4363Together with `tramp-locker', this implements a locking mechanism
4364preventing reentrant calls of Tramp.")
4365
4366(defvar tramp-locker nil
4367 "If non-nil, then a caller has locked Tramp.
4368Together with `tramp-locked', this implements a locking mechanism
4369preventing reentrant calls of Tramp.")
4370
ea9d1443
KG
4371(defun tramp-sh-file-name-handler (operation &rest args)
4372 "Invoke remote-shell Tramp file name handler.
4373Fall back to normal file name handler if no Tramp handler exists."
07dfe738
KG
4374 (when (and tramp-locked (not tramp-locker))
4375 (signal 'file-error "Forbidden reentrant call of Tramp"))
4376 (let ((tl tramp-locked))
4377 (unwind-protect
4378 (progn
4379 (setq tramp-locked t)
4380 (let ((tramp-locker t))
4381 (save-match-data
4382 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4383 (if fn
4384 (apply (cdr fn) args)
4385 (tramp-run-real-handler operation args))))))
4386 (setq tramp-locked tl))))
ea9d1443 4387
16674e4f 4388;;;###autoload
1ecc6145 4389(progn (defun tramp-completion-file-name-handler (operation &rest args)
16674e4f
KG
4390 "Invoke tramp file name completion handler.
4391Falls back to normal file name handler if no tramp file name handler exists."
a01b1e22
MA
4392;; (setq edebug-trace t)
4393;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4394 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4395 (if fn
4396 (save-match-data (apply (cdr fn) args))
4397 (tramp-completion-run-real-handler operation args)))))
4398
b25a52cc 4399;;;###autoload
1ecc6145 4400(defsubst tramp-register-file-name-handlers ()
8c04e197 4401 "Add tramp file name handlers to `file-name-handler-alist'."
a01b1e22
MA
4402 (add-to-list 'file-name-handler-alist
4403 (cons tramp-file-name-regexp 'tramp-file-name-handler))
1a762140
MA
4404 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4405 ;; load it unconditionally there. In the GNU Emacs case, method/
4406 ;; user/host name completion shall be bound to `partial-completion-mode'.
4407 (when (or (not (boundp 'partial-completion-mode))
4408 (symbol-value 'partial-completion-mode)
4409 (featurep 'ido))
8c04e197
MA
4410 (add-to-list 'file-name-handler-alist
4411 (cons tramp-completion-file-name-regexp
4412 'tramp-completion-file-name-handler))
a01b1e22
MA
4413 (put 'tramp-completion-file-name-handler 'safe-magic t))
4414 ;; If jka-compr is already loaded, move it to the front of
4415 ;; `file-name-handler-alist'.
4416 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4417 (when jka
4418 (setq file-name-handler-alist
4419 (cons jka (delete jka file-name-handler-alist))))))
4420
4421;; During autoload, it shall be checked whether
4422;; `partial-completion-mode' is active. Therefore registering will be
4423;; delayed.
8c04e197 4424;;;###autoload(add-hook
1ecc6145
MA
4425;;;###autoload 'after-init-hook
4426;;;###autoload '(lambda () (tramp-register-file-name-handlers)))
8c04e197 4427(tramp-register-file-name-handlers)
b25a52cc 4428
fb7933a3 4429;;;###autoload
8c04e197 4430(defun tramp-unload-file-name-handlers ()
a69c01a0
MA
4431 (setq file-name-handler-alist
4432 (delete (rassoc 'tramp-file-name-handler
4433 file-name-handler-alist)
4434 (delete (rassoc 'tramp-completion-file-name-handler
4435 file-name-handler-alist)
4436 file-name-handler-alist))))
4437
8c04e197 4438(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
a69c01a0 4439
c62c9d08 4440
fb7933a3
KG
4441;;; Interactions with other packages:
4442
4443;; -- complete.el --
4444
4445;; This function contributed by Ed Sabol
4446(defun tramp-handle-expand-many-files (name)
4447 "Like `PC-expand-many-files' for tramp files."
c62c9d08 4448 (with-parsed-tramp-file-name name nil
c62c9d08
KG
4449 (save-match-data
4450 (if (or (string-match "\\*" name)
4451 (string-match "\\?" name)
4452 (string-match "\\[.*\\]" name))
4453 (save-excursion
c62c9d08 4454 (let (bufstr)
c62c9d08
KG
4455 ;; CCC: To do it right, we should quote certain characters
4456 ;; in the file name, but since the echo command is going to
4457 ;; break anyway when there are spaces in the file names, we
4458 ;; don't bother.
4459 ;;-(let ((comint-file-name-quote-list
4460 ;;- (set-difference tramp-file-name-quote-list
4461 ;;- '(?\* ?\? ?[ ?]))))
4462 ;;- (tramp-send-command
4463 ;;- multi-method method user host
7432277c 4464 ;;- (format "echo %s" (comint-quote-filename localname)))
c62c9d08
KG
4465 ;;- (tramp-wait-for-output))
4466 (tramp-send-command multi-method method user host
7432277c 4467 (format "echo %s" localname))
c62c9d08
KG
4468 (tramp-wait-for-output)
4469 (setq bufstr (buffer-substring (point-min)
4470 (tramp-line-end-position)))
4471 (goto-char (point-min))
7432277c 4472 (if (string-equal localname bufstr)
c62c9d08
KG
4473 nil
4474 (insert "(\"")
4475 (while (search-forward " " nil t)
4476 (delete-backward-char 1)
4477 (insert "\" \""))
4478 (goto-char (point-max))
4479 (delete-backward-char 1)
4480 (insert "\")")
4481 (goto-char (point-min))
4482 (mapcar
4483 (function (lambda (x)
4484 (tramp-make-tramp-file-name multi-method method
4485 user host x)))
4486 (read (current-buffer))))))
07dfe738 4487 (list (expand-file-name name))))))
fb7933a3 4488
a69c01a0
MA
4489(eval-after-load "complete"
4490 '(progn
4491 (defadvice PC-expand-many-files
4492 (around tramp-advice-PC-expand-many-files (name) activate)
4493 "Invoke `tramp-handle-expand-many-files' for tramp files."
4494 (if (tramp-tramp-file-p name)
4495 (setq ad-return-value (tramp-handle-expand-many-files name))
4496 ad-do-it))
4497 (add-hook 'tramp-unload-hook
4498 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
fb7933a3 4499
16674e4f
KG
4500;;; File name handler functions for completion mode
4501
8cee983d
RS
4502(defvar tramp-completion-mode nil
4503 "If non-nil, we are in file name completion mode.")
4504
16674e4f
KG
4505;; Necessary because `tramp-file-name-regexp-unified' and
4506;; `tramp-completion-file-name-regexp-unified' aren't different.
4507;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4508;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
bf247b6e 4509;; Using `last-input-event' is a little bit risky, because completing a file
16674e4f
KG
4510;; might require loading other files, like "~/.netrc", and for them it
4511;; shouldn't be decided based on that variable. On the other hand, those files
4512;; shouldn't have partial tramp file name syntax. Maybe another variable should
4513;; be introduced overwriting this check in such cases. Or we change tramp
4514;; file name syntax in order to avoid ambiguities, like in XEmacs ...
5ec2cc41
KG
4515;; In case of non unified file names it can be always true (and wouldn't be
4516;; necessary, because there are different regexp).
16674e4f
KG
4517(defun tramp-completion-mode (file)
4518 "Checks whether method / user name / host name completion is active."
4519 (cond
c1253aad 4520 (tramp-completion-mode t)
5ec2cc41 4521 ((not tramp-unified-filenames) t)
16674e4f
KG
4522 ((string-match "^/.*:.*:$" file) nil)
4523 ((string-match
487f4fb7
KG
4524 (concat tramp-prefix-regexp
4525 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
16674e4f 4526 file)
4007ba5b 4527 (member (match-string 1 file) (mapcar 'car tramp-methods)))
16674e4f 4528 ((or (equal last-input-event 'tab)
85806390
MA
4529 ;; Emacs
4530 (and (integerp last-input-event)
4531 (or
4532 ;; ?\t has event-modifier 'control
4533 (char-equal last-input-event ?\t)
4534 (and (not (event-modifiers last-input-event))
4535 (or (char-equal last-input-event ?\?)
4536 (char-equal last-input-event ?\ )))))
5ec2cc41
KG
4537 ;; XEmacs
4538 (and (featurep 'xemacs)
85806390
MA
4539 (or
4540 ;; ?\t has event-modifier 'control
4541 (char-equal
4542 (funcall (symbol-function 'event-to-character)
4543 last-input-event) ?\t)
4544 (and (not (event-modifiers last-input-event))
4545 (or (char-equal
4546 (funcall (symbol-function 'event-to-character)
4547 last-input-event) ?\?)
4548 (char-equal
4549 (funcall (symbol-function 'event-to-character)
4550 last-input-event) ?\ ))))))
16674e4f
KG
4551 t)))
4552
16674e4f
KG
4553;; Method, host name and user name completion.
4554;; `tramp-completion-dissect-file-name' returns a list of
4555;; tramp-file-name structures. For all of them we return possible completions.
a01b1e22 4556;;;###autoload
16674e4f
KG
4557(defun tramp-completion-handle-file-name-all-completions (filename directory)
4558 "Like `file-name-all-completions' for partial tramp files."
4559
c1253aad
MA
4560 (unwind-protect
4561 ;; We need to reset `tramp-completion-mode'.
4562 (progn
4563 (setq tramp-completion-mode t)
4564 (let*
4565 ((fullname (concat directory filename))
4566 ;; possible completion structures
4567 (v (tramp-completion-dissect-file-name fullname))
4568 result result1)
4569
4570 (while v
4571 (let* ((car (car v))
4572 (multi-method (tramp-file-name-multi-method car))
4573 (method (tramp-file-name-method car))
4574 (user (tramp-file-name-user car))
4575 (host (tramp-file-name-host car))
4576 (localname (tramp-file-name-localname car))
4577 (m (tramp-find-method multi-method method user host))
4578 (tramp-current-user user) ; see `tramp-parse-passwd'
4579 all-user-hosts)
4580
4581 (unless (or multi-method ;; Not handled (yet).
4582 localname) ;; Nothing to complete
4583
4584 (if (or user host)
4585
4586 ;; Method dependent user / host combinations
4587 (progn
4588 (mapcar
4589 (lambda (x)
4590 (setq all-user-hosts
4591 (append all-user-hosts
4592 (funcall (nth 0 x) (nth 1 x)))))
4593 (tramp-get-completion-function m))
4594
4595 (setq result (append result
4596 (mapcar
4597 (lambda (x)
4598 (tramp-get-completion-user-host
4599 method user host (nth 0 x) (nth 1 x)))
4600 (delq nil all-user-hosts)))))
4601
4602 ;; Possible methods
4603 (setq result
4604 (append result (tramp-get-completion-methods m)))))
4605
4606 (setq v (cdr v))))
4607
4608 ;; unify list, remove nil elements
4609 (while result
4610 (let ((car (car result)))
a01b1e22
MA
4611 (when car (add-to-list
4612 'result1 (substring car (length directory))))
c1253aad
MA
4613 (setq result (cdr result))))
4614
4615 ;; Complete local parts
4616 (append
4617 result1
4618 (condition-case nil
4619 (if result1
4620 ;; "/ssh:" does not need to be expanded as hostname.
4621 (tramp-run-real-handler
4622 'file-name-all-completions (list filename directory))
4623 ;; No method/user/host found to be expanded.
4624 (tramp-completion-run-real-handler
4625 'file-name-all-completions (list filename directory)))
4626 (error nil)))))
4627 ;; unwindform
4628 (setq tramp-completion-mode nil)))
16674e4f
KG
4629
4630;; Method, host name and user name completion for a file.
a01b1e22 4631;;;###autoload
e1e17cae
MA
4632(defun tramp-completion-handle-file-name-completion
4633 (filename directory &optional predicate)
16674e4f 4634 "Like `file-name-completion' for tramp files."
e1e17cae
MA
4635 (try-completion
4636 filename
4637 (mapcar 'list (file-name-all-completions filename directory))
4638 predicate))
16674e4f
KG
4639
4640;; I misuse a little bit the tramp-file-name structure in order to handle
4641;; completion possibilities for partial methods / user names / host names.
4642;; Return value is a list of tramp-file-name structures according to possible
7432277c 4643;; completions. If "multi-method" or "localname" is non-nil it means there
16674e4f
KG
4644;; shouldn't be a completion anymore.
4645
4646;; Expected results:
4647
4648;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4649;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4650;; [nil nil "x" nil nil]
4651;; [nil "x" nil nil nil]
4652
bf247b6e 4653;; "/x:" "/x:y" "/x:y:"
16674e4f
KG
4654;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4655;; "/[x/" "/[x/y"
4656;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4657;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4658
4659;; "/x:y@" "/x:y@z" "/x:y@z:"
4660;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4661;; "/[x/y@" "/[x/y@z"
4662;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4663(defun tramp-completion-dissect-file-name (name)
4664 "Returns a list of `tramp-file-name' structures.
4665They are collected by `tramp-completion-dissect-file-name1'."
4666
4667 (let* ((result)
4007ba5b
KG
4668 (x-nil "\\|\\(\\)")
4669 ;; "/method" "/[method"
4670 (tramp-completion-file-name-structure1
4671 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4672 1 nil nil nil))
4673 ;; "/user" "/[user"
4674 (tramp-completion-file-name-structure2
4675 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4676 nil 1 nil nil))
4677 ;; "/host" "/[host"
4678 (tramp-completion-file-name-structure3
4679 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4680 nil nil 1 nil))
4681 ;; "/user@host" "/[user@host"
4682 (tramp-completion-file-name-structure4
4683 (list (concat tramp-prefix-regexp
4684 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4685 "\\(" tramp-host-regexp x-nil "\\)$")
4686 nil 1 2 nil))
4687 ;; "/method:user" "/[method/user"
4688 (tramp-completion-file-name-structure5
4689 (list (concat tramp-prefix-regexp
4690 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4691 "\\(" tramp-user-regexp x-nil "\\)$")
4692 1 2 nil nil))
4693 ;; "/method:host" "/[method/host"
4694 (tramp-completion-file-name-structure6
4695 (list (concat tramp-prefix-regexp
4696 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4697 "\\(" tramp-host-regexp x-nil "\\)$")
4698 1 nil 2 nil))
4699 ;; "/method:user@host" "/[method/user@host"
4700 (tramp-completion-file-name-structure7
4701 (list (concat tramp-prefix-regexp
4702 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4703 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4704 "\\(" tramp-host-regexp x-nil "\\)$")
4705 1 2 3 nil)))
4706
4707 (mapcar (lambda (regexp)
16674e4f
KG
4708 (add-to-list 'result
4709 (tramp-completion-dissect-file-name1 regexp name)))
4710 (list
4711 tramp-completion-file-name-structure1
4712 tramp-completion-file-name-structure2
4713 tramp-completion-file-name-structure3
4714 tramp-completion-file-name-structure4
4715 tramp-completion-file-name-structure5
4716 tramp-completion-file-name-structure6
4717 tramp-completion-file-name-structure7
4718 tramp-file-name-structure))
4719
4720 (delq nil result)))
4721
4722(defun tramp-completion-dissect-file-name1 (structure name)
4723 "Returns a `tramp-file-name' structure matching STRUCTURE.
4724The structure consists of multi-method, remote method, remote user,
7432277c 4725remote host and localname (filename on remote host)."
fb7933a3 4726
16674e4f
KG
4727 (let (method)
4728 (save-match-data
4729 (when (string-match (nth 0 structure) name)
4007ba5b
KG
4730 (setq method (and (nth 1 structure)
4731 (match-string (nth 1 structure) name)))
16674e4f
KG
4732 (if (and method (member method tramp-multi-methods))
4733 ;; Not handled (yet).
4734 (make-tramp-file-name
4735 :multi-method method
4736 :method nil
4737 :user nil
4738 :host nil
7432277c 4739 :localname nil)
4007ba5b
KG
4740 (let ((user (and (nth 2 structure)
4741 (match-string (nth 2 structure) name)))
4742 (host (and (nth 3 structure)
4743 (match-string (nth 3 structure) name)))
7432277c 4744 (localname (and (nth 4 structure)
4007ba5b 4745 (match-string (nth 4 structure) name))))
16674e4f
KG
4746 (make-tramp-file-name
4747 :multi-method nil
4748 :method method
4749 :user user
4750 :host host
7432277c 4751 :localname localname)))))))
16674e4f
KG
4752
4753;; This function returns all possible method completions, adding the
4754;; trailing method delimeter.
16674e4f
KG
4755(defun tramp-get-completion-methods (partial-method)
4756 "Returns all method completions for PARTIAL-METHOD."
4007ba5b
KG
4757 (mapcar
4758 (lambda (method)
4759 (and method
4760 (string-match (concat "^" (regexp-quote partial-method)) method)
a01b1e22 4761 (tramp-make-tramp-file-name nil method nil nil nil)))
4007ba5b 4762 (delete "multi" (mapcar 'car tramp-methods))))
16674e4f
KG
4763
4764;; Compares partial user and host names with possible completions.
4765(defun tramp-get-completion-user-host (method partial-user partial-host user host)
4766 "Returns the most expanded string for user and host name completion.
4767PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4768 (cond
4769
4770 ((and partial-user partial-host)
4771 (if (and host
4772 (string-match (concat "^" (regexp-quote partial-host)) host)
4773 (string-equal partial-user (or user partial-user)))
4774 (setq user partial-user)
4775 (setq user nil
4776 host nil)))
4777
4778 (partial-user
4779 (setq host nil)
4780 (unless
4781 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4782 (setq user nil)))
4783
4784 (partial-host
4785 (setq user nil)
4786 (unless
4787 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4788 (setq host nil)))
4789
4790 (t (setq user nil
4791 host nil)))
4792
292ffc15 4793 (unless (zerop (+ (length user) (length host)))
a01b1e22 4794 (tramp-make-tramp-file-name nil method user host nil)))
16674e4f
KG
4795
4796(defun tramp-parse-rhosts (filename)
4797 "Return a list of (user host) tuples allowed to access.
292ffc15 4798Either user or host may be nil."
16674e4f
KG
4799
4800 (let (res)
8daea7fc 4801 (when (file-readable-p filename)
16674e4f
KG
4802 (with-temp-buffer
4803 (insert-file-contents filename)
4804 (goto-char (point-min))
4805 (while (not (eobp))
292ffc15 4806 (push (tramp-parse-rhosts-group) res))))
16674e4f
KG
4807 res))
4808
4809;; Taken from gnus/netrc.el
4810(eval-and-compile
4811 (defalias 'tramp-point-at-eol
4812 (if (fboundp 'point-at-eol)
4813 'point-at-eol
4814 'line-end-position)))
4815
4816(defun tramp-parse-rhosts-group ()
4817 "Return a (user host) tuple allowed to access.
292ffc15 4818Either user or host may be nil."
16674e4f
KG
4819
4820 (let ((result)
4821 (regexp
4822 (concat
4823 "^\\(" tramp-host-regexp "\\)"
4824 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4825
4826 (narrow-to-region (point) (tramp-point-at-eol))
4827 (when (re-search-forward regexp nil t)
4828 (setq result (append (list (match-string 3) (match-string 1)))))
4829 (widen)
4830 (forward-line 1)
4831 result))
4832
4833(defun tramp-parse-shosts (filename)
4834 "Return a list of (user host) tuples allowed to access.
4835User is always nil."
4836
4837 (let (res)
8daea7fc 4838 (when (file-readable-p filename)
16674e4f
KG
4839 (with-temp-buffer
4840 (insert-file-contents filename)
4841 (goto-char (point-min))
4842 (while (not (eobp))
292ffc15 4843 (push (tramp-parse-shosts-group) res))))
16674e4f
KG
4844 res))
4845
4846(defun tramp-parse-shosts-group ()
4847 "Return a (user host) tuple allowed to access.
4848User is always nil."
4849
4850 (let ((result)
4851 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4852
4853 (narrow-to-region (point) (tramp-point-at-eol))
4854 (when (re-search-forward regexp nil t)
4855 (setq result (list nil (match-string 1))))
4856 (widen)
4857 (or
4858 (> (skip-chars-forward ",") 0)
4859 (forward-line 1))
4860 result))
4861
8daea7fc
KG
4862(defun tramp-parse-sconfig (filename)
4863 "Return a list of (user host) tuples allowed to access.
4864User is always nil."
4865
4866 (let (res)
4867 (when (file-readable-p filename)
4868 (with-temp-buffer
4869 (insert-file-contents filename)
4870 (goto-char (point-min))
4871 (while (not (eobp))
4872 (push (tramp-parse-sconfig-group) res))))
4873 res))
4874
4875(defun tramp-parse-sconfig-group ()
4876 "Return a (user host) tuple allowed to access.
4877User is always nil."
4878
4879 (let ((result)
4880 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4881
4882 (narrow-to-region (point) (tramp-point-at-eol))
4883 (when (re-search-forward regexp nil t)
4884 (setq result (list nil (match-string 1))))
4885 (widen)
4886 (or
4887 (> (skip-chars-forward ",") 0)
4888 (forward-line 1))
4889 result))
4890
5ec2cc41
KG
4891(defun tramp-parse-shostkeys (dirname)
4892 "Return a list of (user host) tuples allowed to access.
4893User is always nil."
4894
4895 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4896 (files (when (file-directory-p dirname) (directory-files dirname)))
4897 result)
4898
4899 (while files
4900 (when (string-match regexp (car files))
4901 (push (list nil (match-string 1 (car files))) result))
4902 (setq files (cdr files)))
4903 result))
4904
4905(defun tramp-parse-sknownhosts (dirname)
4906 "Return a list of (user host) tuples allowed to access.
4907User is always nil."
4908
4909 (let ((regexp (concat "^\\(" tramp-host-regexp
4910 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4911 (files (when (file-directory-p dirname) (directory-files dirname)))
4912 result)
4913
4914 (while files
4915 (when (string-match regexp (car files))
4916 (push (list nil (match-string 1 (car files))) result))
4917 (setq files (cdr files)))
4918 result))
4919
16674e4f
KG
4920(defun tramp-parse-hosts (filename)
4921 "Return a list of (user host) tuples allowed to access.
4922User is always nil."
4923
4924 (let (res)
8daea7fc 4925 (when (file-readable-p filename)
16674e4f
KG
4926 (with-temp-buffer
4927 (insert-file-contents filename)
4928 (goto-char (point-min))
4929 (while (not (eobp))
292ffc15 4930 (push (tramp-parse-hosts-group) res))))
16674e4f
KG
4931 res))
4932
4933(defun tramp-parse-hosts-group ()
4934 "Return a (user host) tuple allowed to access.
4935User is always nil."
4936
4937 (let ((result)
4938 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4939
4940 (narrow-to-region (point) (tramp-point-at-eol))
4941 (when (re-search-forward regexp nil t)
4942 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4943 (setq result (list nil (match-string 1)))))
4944 (widen)
4945 (or
4946 (> (skip-chars-forward " \t") 0)
4947 (forward-line 1))
4948 result))
4949
8daea7fc
KG
4950;; For su-alike methods it would be desirable to return "root@localhost"
4951;; as default. Unfortunately, we have no information whether any user name
4952;; has been typed already. So we (mis-)use tramp-current-user as indication,
4953;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
16674e4f
KG
4954(defun tramp-parse-passwd (filename)
4955 "Return a list of (user host) tuples allowed to access.
4956Host is always \"localhost\"."
4957
4958 (let (res)
8daea7fc 4959 (if (zerop (length tramp-current-user))
16674e4f 4960 '(("root" nil))
8daea7fc 4961 (when (file-readable-p filename)
16674e4f
KG
4962 (with-temp-buffer
4963 (insert-file-contents filename)
4964 (goto-char (point-min))
4965 (while (not (eobp))
292ffc15 4966 (push (tramp-parse-passwd-group) res))))
16674e4f
KG
4967 res)))
4968
4969(defun tramp-parse-passwd-group ()
4970 "Return a (user host) tuple allowed to access.
292ffc15 4971Host is always \"localhost\"."
16674e4f
KG
4972
4973 (let ((result)
4974 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4975
4976 (narrow-to-region (point) (tramp-point-at-eol))
4977 (when (re-search-forward regexp nil t)
4978 (setq result (list (match-string 1) "localhost")))
4979 (widen)
4980 (forward-line 1)
4981 result))
4982
292ffc15
KG
4983(defun tramp-parse-netrc (filename)
4984 "Return a list of (user host) tuples allowed to access.
4985User may be nil."
4986
4987 (let (res)
8daea7fc 4988 (when (file-readable-p filename)
292ffc15
KG
4989 (with-temp-buffer
4990 (insert-file-contents filename)
4991 (goto-char (point-min))
4992 (while (not (eobp))
4993 (push (tramp-parse-netrc-group) res))))
4994 res))
4995
4996(defun tramp-parse-netrc-group ()
4997 "Return a (user host) tuple allowed to access.
4998User may be nil."
4999
5000 (let ((result)
5001 (regexp
5002 (concat
5003 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
5004 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
5005
5006 (narrow-to-region (point) (tramp-point-at-eol))
5007 (when (re-search-forward regexp nil t)
5008 (setq result (list (match-string 3) (match-string 1))))
5009 (widen)
5010 (forward-line 1)
5011 result))
5012
fb7933a3
KG
5013;;; Internal Functions:
5014
c08e6004 5015(defun tramp-maybe-send-perl-script (multi-method method user host script name)
c82c5727
LH
5016 "Define in remote shell function NAME implemented as perl SCRIPT.
5017Only send the definition if it has not already been done.
5018Function may have 0-3 parameters."
5019 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
5020 (unless remote-perl (error "No remote perl"))
5021 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
5022 multi-method method user host)))
5023 (unless (memq name perl-scripts)
5024 (with-current-buffer (tramp-get-buffer multi-method method user host)
5025 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
5026 (tramp-send-string multi-method method user host
5027 (concat name
5028 " () {\n"
5029 remote-perl
5030 " -e '"
5031 script
5032 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
5033 (tramp-wait-for-output)
5034 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
5035 multi-method method user host)
5036 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
5037
fb7933a3
KG
5038(defun tramp-set-auto-save ()
5039 (when (and (buffer-file-name)
5040 (tramp-tramp-file-p (buffer-file-name))
7177e2a3
MA
5041 ;; ange-ftp has its own auto-save mechanism
5042 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5043 'tramp-sh-file-name-handler)
fb7933a3
KG
5044 auto-save-default)
5045 (auto-save-mode 1)))
5046(add-hook 'find-file-hooks 'tramp-set-auto-save t)
a69c01a0
MA
5047(add-hook 'tramp-unload-hook
5048 '(lambda ()
5049 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
fb7933a3
KG
5050
5051(defun tramp-run-test (switch filename)
5052 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5053Returns the exit code of the `test' program."
5054 (let ((v (tramp-dissect-file-name filename)))
5055 (save-excursion
5056 (tramp-send-command-and-check
5057 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5058 (tramp-file-name-user v) (tramp-file-name-host v)
5059 (format "test %s %s" switch
7432277c 5060 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
fb7933a3
KG
5061
5062(defun tramp-run-test2 (program file1 file2 &optional switch)
5063 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5064The optional SWITCH is inserted between the two files.
5065Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5066hosts, or files, disagree."
5067 (let* ((v1 (tramp-dissect-file-name file1))
5068 (v2 (tramp-dissect-file-name file2))
5069 (mmethod1 (tramp-file-name-multi-method v1))
5070 (mmethod2 (tramp-file-name-multi-method v2))
5071 (method1 (tramp-file-name-method v1))
5072 (method2 (tramp-file-name-method v2))
5073 (user1 (tramp-file-name-user v1))
5074 (user2 (tramp-file-name-user v2))
5075 (host1 (tramp-file-name-host v1))
5076 (host2 (tramp-file-name-host v2))
7432277c
KG
5077 (localname1 (tramp-file-name-localname v1))
5078 (localname2 (tramp-file-name-localname v2)))
fb7933a3
KG
5079 (unless (and method1 method2 host1 host2
5080 (equal mmethod1 mmethod2)
5081 (equal method1 method2)
5082 (equal user1 user2)
5083 (equal host1 host2))
5084 (error "tramp-run-test2: %s"
5085 "only implemented for same method, same user, same host"))
5086 (save-excursion
5087 (tramp-send-command-and-check
5088 mmethod1 method1 user1 host1
5089 (format "%s %s %s %s"
5090 program
7432277c 5091 (tramp-shell-quote-argument localname1)
fb7933a3 5092 (or switch "")
7432277c 5093 (tramp-shell-quote-argument localname2))))))
fb7933a3 5094
5ec2cc41
KG
5095(defun tramp-touch (file time)
5096 "Set the last-modified timestamp of the given file.
5097TIME is an Emacs internal time value as returned by `current-time'."
8a7269eb 5098 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time t)))
38c65fca
KG
5099 (if (tramp-tramp-file-p file)
5100 (with-parsed-tramp-file-name file nil
5101 (let ((buf (tramp-get-buffer multi-method method user host)))
5102 (unless (zerop (tramp-send-command-and-check
5103 multi-method method user host
8a7269eb 5104 (format "TZ=UTC; export TZ; touch -t %s %s"
38c65fca 5105 touch-time
8e7225a2 5106 (tramp-shell-quote-argument localname))
8a7269eb 5107 t))
38c65fca
KG
5108 (pop-to-buffer buf)
5109 (error "tramp-touch: touch failed, see buffer `%s' for details"
5110 buf))))
5111 ;; It's a local file
5112 (with-temp-buffer
5113 (unless (zerop (call-process
5114 "touch" nil (current-buffer) nil "-t" touch-time file))
5115 (pop-to-buffer (current-buffer))
5116 (error "tramp-touch: touch failed"))))))
bf247b6e 5117
fb7933a3
KG
5118(defun tramp-buffer-name (multi-method method user host)
5119 "A name for the connection buffer for USER at HOST using METHOD."
90f8dc03
KG
5120 (if multi-method
5121 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5122 (let ((method (tramp-find-method multi-method method user host)))
5123 (if user
5ec2cc41
KG
5124 (format "*tramp/%s %s@%s*" method user host)
5125 (format "*tramp/%s %s*" method host)))))
fb7933a3
KG
5126
5127(defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5128 "A name for the multi method connection buffer.
5129MULTI-METHOD gives the multi method, METHOD the array of methods,
5130USER the array of user names, HOST the array of host names."
5131 (unless (and (= (length method) (length user))
5132 (= (length method) (length host)))
5133 (error "Syntax error in multi method (implementation error)"))
5134 (let ((len (length method))
5135 (i 0)
5136 string-list)
5137 (while (< i len)
5138 (setq string-list
5139 (cons (if (aref user i)
5140 (format "%s#%s@%s:" (aref method i)
5141 (aref user i) (aref host i))
5142 (format "%s@%s:" (aref method i) (aref host i)))
5143 string-list))
5144 (incf i))
5145 (format "*%s/%s %s*"
5146 prefix multi-method
5147 (apply 'concat (reverse string-list)))))
5148
5149(defun tramp-get-buffer (multi-method method user host)
5150 "Get the connection buffer to be used for USER at HOST using METHOD."
01917a18
MA
5151 (with-current-buffer
5152 (get-buffer-create (tramp-buffer-name multi-method method user host))
5153 (setq buffer-undo-list t)
5154 (current-buffer)))
fb7933a3
KG
5155
5156(defun tramp-debug-buffer-name (multi-method method user host)
5157 "A name for the debug buffer for USER at HOST using METHOD."
90f8dc03
KG
5158 (if multi-method
5159 (tramp-buffer-name-multi-method "debug tramp"
5160 multi-method method user host)
5161 (let ((method (tramp-find-method multi-method method user host)))
5162 (if user
5163 (format "*debug tramp/%s %s@%s*" method user host)
5164 (format "*debug tramp/%s %s*" method host)))))
fb7933a3
KG
5165
5166(defun tramp-get-debug-buffer (multi-method method user host)
5167 "Get the debug buffer for USER at HOST using METHOD."
01917a18
MA
5168 (with-current-buffer
5169 (get-buffer-create
5170 (tramp-debug-buffer-name multi-method method user host))
5171 (setq buffer-undo-list t)
5172 (current-buffer)))
fb7933a3
KG
5173
5174(defun tramp-find-executable (multi-method method user host
5175 progname dirlist ignore-tilde)
5176 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5177First args METHOD, USER and HOST specify the connection, PROGNAME
5178is the program to search for, and DIRLIST gives the list of directories
5179to search. If IGNORE-TILDE is non-nil, directory names starting
5180with `~' will be ignored.
5181
7432277c 5182Returns the absolute file name of PROGNAME, if found, and nil otherwise.
fb7933a3
KG
5183
5184This function expects to be in the right *tramp* buffer."
5185 (let (result)
5186 (when ignore-tilde
5187 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5188 ;; `remove' is in CL, and we want to avoid CL dependencies.
5189 (let (newdl d)
5190 (while dirlist
5191 (setq d (car dirlist))
5192 (setq dirlist (cdr dirlist))
5193 (unless (char-equal ?~ (aref d 0))
5194 (setq newdl (cons d newdl))))
5195 (setq dirlist (nreverse newdl))))
5196 (tramp-send-command
5197 multi-method method user host
5198 (format (concat "while read d; "
5199 "do if test -x $d/%s -a -f $d/%s; "
5200 "then echo tramp_executable $d/%s; "
5201 "break; fi; done <<'EOF'")
5202 progname progname progname))
5203 (mapcar (lambda (d)
5204 (tramp-send-command multi-method method user host d))
5205 dirlist)
5206 (tramp-send-command multi-method method user host "EOF")
5207 (tramp-wait-for-output)
5208 (goto-char (point-max))
5209 (when (search-backward "tramp_executable " nil t)
5210 (skip-chars-forward "^ ")
5211 (skip-chars-forward " ")
5212 (buffer-substring (point) (tramp-line-end-position)))))
5213
5214(defun tramp-set-remote-path (multi-method method user host var dirlist)
5215 "Sets the remote environment VAR to existing directories from DIRLIST.
5216I.e., for each directory in DIRLIST, it is tested whether it exists and if
5217so, it is added to the environment variable VAR."
5218 (let ((existing-dirs
5219 (mapcar
5220 (lambda (x)
5221 (when (and
5222 (file-exists-p
5223 (tramp-make-tramp-file-name multi-method method user host x))
5224 (file-directory-p
5225 (tramp-make-tramp-file-name multi-method method user host x)))
5226 x))
5227 dirlist)))
5228 (tramp-send-command
5229 multi-method method user host
5230 (concat var "="
5231 (mapconcat 'identity (delq nil existing-dirs) ":")
5232 "; export " var))
5233 (tramp-wait-for-output)))
5234
5235;; -- communication with external shell --
5236
5237(defun tramp-find-file-exists-command (multi-method method user host)
5238 "Find a command on the remote host for checking if a file exists.
5239Here, we are looking for a command which has zero exit status if the
5240file exists and nonzero exit status otherwise."
5241 (make-local-variable 'tramp-file-exists-command)
821e6e36 5242 (tramp-message 9 "Finding command to check if file exists")
fb7933a3
KG
5243 (let ((existing
5244 (tramp-make-tramp-file-name
5245 multi-method method user host
5246 "/")) ;assume this file always exists
5247 (nonexisting
5248 (tramp-make-tramp-file-name
5249 multi-method method user host
5250 "/ this file does not exist "))) ;assume this never exists
5251 ;; The algorithm is as follows: we try a list of several commands.
5252 ;; For each command, we first run `$cmd /' -- this should return
5253 ;; true, as the root directory always exists. And then we run
5254 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5255 ;; does not exist. This should return false. We use the first
5256 ;; command we find that seems to work.
5257 ;; The list of commands to try is as follows:
5258 ;; `ls -d' This works on most systems, but NetBSD 1.4
5259 ;; has a bug: `ls' always returns zero exit
5260 ;; status, even for files which don't exist.
5261 ;; `test -e' Some Bourne shells have a `test' builtin
5262 ;; which does not know the `-e' option.
5263 ;; `/bin/test -e' For those, the `test' binary on disk normally
5264 ;; provides the option. Alas, the binary
5265 ;; is sometimes `/bin/test' and sometimes it's
5266 ;; `/usr/bin/test'.
5267 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5268 (unless (or
fb7933a3 5269 (and (setq tramp-file-exists-command "test -e %s")
07dfe738
KG
5270 (file-exists-p existing)
5271 (not (file-exists-p nonexisting)))
fb7933a3 5272 (and (setq tramp-file-exists-command "/bin/test -e %s")
07dfe738
KG
5273 (file-exists-p existing)
5274 (not (file-exists-p nonexisting)))
fb7933a3 5275 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
07dfe738
KG
5276 (file-exists-p existing)
5277 (not (file-exists-p nonexisting)))
5beaf831 5278 (and (setq tramp-file-exists-command "ls -d %s")
07dfe738
KG
5279 (file-exists-p existing)
5280 (not (file-exists-p nonexisting))))
ff12e78d 5281 (error "Couldn't find command to check if file exists"))))
bf247b6e 5282
fb7933a3
KG
5283
5284;; CCC test ksh or bash found for tilde expansion?
5285(defun tramp-find-shell (multi-method method user host)
5286 "Find a shell on the remote host which groks tilde expansion."
5287 (let ((shell nil))
5288 (tramp-send-command multi-method method user host "echo ~root")
5289 (tramp-wait-for-output)
5290 (cond
5291 ((string-match "^~root$" (buffer-string))
5292 (setq shell
5293 (or (tramp-find-executable multi-method method user host
c62c9d08 5294 "bash" tramp-remote-path t)
fb7933a3 5295 (tramp-find-executable multi-method method user host
c62c9d08 5296 "ksh" tramp-remote-path t)))
fb7933a3
KG
5297 (unless shell
5298 (error "Couldn't find a shell which groks tilde expansion"))
c62c9d08
KG
5299 ;; Find arguments for this shell.
5300 (let ((alist tramp-sh-extra-args)
5301 item extra-args)
5302 (while (and alist (null extra-args))
5303 (setq item (pop alist))
5304 (when (string-match (car item) shell)
5305 (setq extra-args (cdr item))))
5306 (when extra-args (setq shell (concat shell " " extra-args))))
fb7933a3
KG
5307 (tramp-message
5308 5 "Starting remote shell `%s' for tilde expansion..." shell)
5309 (tramp-send-command
5310 multi-method method user host
89509ea0 5311 (concat "PS1='$ ' exec " shell)) ;
d2a2c17f
MA
5312 (tramp-barf-if-no-shell-prompt
5313 (get-buffer-process (current-buffer))
5314 60 "Couldn't find remote `%s' prompt" shell)
89509ea0 5315 (tramp-message
821e6e36 5316 9 "Setting remote shell prompt...")
16674e4f
KG
5317 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5318 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5319 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5320 ;; as well.
fb7933a3 5321 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
16674e4f 5322 tramp-rsh-end-of-line
fb7933a3 5323 tramp-end-of-output
16674e4f 5324 tramp-rsh-end-of-line
fb7933a3
KG
5325 tramp-rsh-end-of-line))
5326 (tramp-wait-for-output)
89509ea0 5327 (tramp-message
821e6e36 5328 9 "Setting remote shell prompt...done")
685f5858 5329 )
fb7933a3 5330 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
c951aecb
KG
5331 (tramp-get-method-parameter
5332 multi-method method user host 'tramp-remote-sh))))))
fb7933a3
KG
5333
5334(defun tramp-check-ls-command (multi-method method user host cmd)
5335 "Checks whether the given `ls' executable groks `-n'.
7432277c 5336METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
fb7933a3
KG
5337the `ls' executable. Returns t if CMD supports the `-n' option, nil
5338otherwise."
07dfe738
KG
5339 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5340 (when (file-executable-p
fb7933a3
KG
5341 (tramp-make-tramp-file-name multi-method method user host cmd))
5342 (let ((result nil))
5343 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5344 (setq result
5345 (tramp-send-command-and-check
5346 multi-method method user host
5347 (format "%s -lnd / >/dev/null"
5348 cmd)))
5349 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5350 cmd
5351 (if (zerop result) "okay" "failed"))
5352 (zerop result))))
5353
5354(defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5355 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5356Returns nil if none was found, else the command is returned."
5357 (let ((dl dirlist)
19a87064
MA
5358 (result nil))
5359 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5360 ;; It would be better to use the CL function `find', but
5361 ;; we don't want run-time dependencies on CL.
5362 (while (and dl (not result))
5363 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5364 (when (tramp-check-ls-command multi-method method user host x)
5365 (setq result x)))
5366 (setq dl (cdr dl)))
5367 result)))
fb7933a3
KG
5368
5369(defun tramp-find-ls-command (multi-method method user host)
5370 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5371\(This option prints numeric user and group ids in a long listing.)"
5372 (tramp-message 9 "Finding a suitable `ls' command")
5373 (or
5374 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5375 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5376 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5377
bf247b6e
KS
5378;; ------------------------------------------------------------
5379;; -- Functions for establishing connection --
5380;; ------------------------------------------------------------
fb7933a3 5381
ac474af1
KG
5382;; The following functions are actions to be taken when seeing certain
5383;; prompts from the remote host. See the variable
5384;; `tramp-actions-before-shell' for usage of these functions.
5385
5386(defun tramp-action-login (p multi-method method user host)
5387 "Send the login name."
5388 (tramp-message 9 "Sending login name `%s'"
5389 (or user (user-login-name)))
5390 (erase-buffer)
5391 (process-send-string nil (concat (or user (user-login-name))
5392 tramp-rsh-end-of-line)))
5393
5394(defun tramp-action-password (p multi-method method user host)
5395 "Query the user for a password."
553f03bc
MA
5396 (let ((pw-prompt
5397 (format "Password for %s "
5398 (tramp-make-tramp-file-name
5399 nil method user host ""))))
3b89d388 5400 (tramp-message 9 "Sending password")
07dfe738 5401 (tramp-enter-password p pw-prompt user host)))
ac474af1
KG
5402
5403(defun tramp-action-succeed (p multi-method method user host)
5404 "Signal success in finding shell prompt."
5405 (tramp-message 9 "Found remote shell prompt.")
5406 (erase-buffer)
5407 (throw 'tramp-action 'ok))
5408
5409(defun tramp-action-permission-denied (p multi-method method user host)
5410 "Signal permission denied."
b1d06e75 5411 (pop-to-buffer (tramp-get-buffer multi-method method user host))
ac474af1
KG
5412 (tramp-message 9 "Permission denied by remote host.")
5413 (kill-process p)
ac474af1
KG
5414 (throw 'tramp-action 'permission-denied))
5415
6b2633cc
LH
5416(defun tramp-action-copy-failed (p multi-method method user host)
5417 "Signal copy failed."
5418 (kill-process p)
5419 (error "%s" (match-string 1)))
5420
ac474af1 5421(defun tramp-action-yesno (p multi-method method user host)
3cdaec13
KG
5422 "Ask the user for confirmation using `yes-or-no-p'.
5423Send \"yes\" to remote process on confirmation, abort otherwise.
5424See also `tramp-action-yn'."
ac474af1
KG
5425 (save-window-excursion
5426 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5427 (unless (yes-or-no-p (match-string 0))
5428 (kill-process p)
5429 (erase-buffer)
5430 (throw 'tramp-action 'permission-denied))
5431 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5432 (erase-buffer)))
5433
3cdaec13
KG
5434(defun tramp-action-yn (p multi-method method user host)
5435 "Ask the user for confirmation using `y-or-n-p'.
5436Send \"y\" to remote process on confirmation, abort otherwise.
5437See also `tramp-action-yesno'."
5438 (save-window-excursion
5439 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5440 (unless (y-or-n-p (match-string 0))
5441 (kill-process p)
3cdaec13 5442 (throw 'tramp-action 'permission-denied))
4007ba5b 5443 (erase-buffer)
3cdaec13
KG
5444 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5445
487f4fb7
KG
5446(defun tramp-action-terminal (p multi-method method user host)
5447 "Tell the remote host which terminal type to use.
5448The terminal type can be configured with `tramp-terminal-type'."
5449 (tramp-message 9 "Setting `%s' as terminal type."
5450 tramp-terminal-type)
5451 (erase-buffer)
5452 (process-send-string nil (concat tramp-terminal-type
5453 tramp-rsh-end-of-line)))
5454
19a87064
MA
5455(defun tramp-action-process-alive (p multi-method method user host)
5456 "Check whether a process has finished."
5457 (unless (memq (process-status p) '(run open))
5458 (throw 'tramp-action 'process-died)))
5459
38c65fca
KG
5460(defun tramp-action-out-of-band (p multi-method method user host)
5461 "Check whether an out-of-band copy has finished."
5462 (cond ((and (memq (process-status p) '(stop exit))
5463 (zerop (process-exit-status p)))
5464 (tramp-message 9 "Process has finished.")
5465 (throw 'tramp-action 'ok))
5466 ((or (and (memq (process-status p) '(stop exit))
5467 (not (zerop (process-exit-status p))))
5468 (memq (process-status p) '(signal)))
01917a18
MA
5469 ;; `scp' could have copied correctly, but set modes could have failed.
5470 ;; This can be ignored.
5471 (goto-char (point-min))
5472 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5473 (progn
5474 (tramp-message 10 "'set mode' error ignored.")
5475 (tramp-message 9 "Process has finished.")
5476 (throw 'tramp-action 'ok))
5477 (tramp-message 9 "Process has died.")
5478 (throw 'tramp-action 'process-died)))
38c65fca
KG
5479 (t nil)))
5480
ac474af1
KG
5481;; The following functions are specifically for multi connections.
5482
5483(defun tramp-multi-action-login (p method user host)
5484 "Send the login name."
5485 (tramp-message 9 "Sending login name `%s'" user)
5486 (erase-buffer)
5487 (process-send-string p (concat user tramp-rsh-end-of-line)))
5488
5489(defun tramp-multi-action-password (p method user host)
5490 "Query the user for a password."
553f03bc
MA
5491 (let ((pw-prompt
5492 (format "Password for %s "
5493 (tramp-make-tramp-file-name
5494 nil method user host ""))))
5495 (tramp-message 9 "Sending password")
5496 (tramp-enter-password p pw-prompt user host)))
ac474af1
KG
5497
5498(defun tramp-multi-action-succeed (p method user host)
5499 "Signal success in finding shell prompt."
5500 (tramp-message 9 "Found shell prompt on `%s'" host)
5501 (erase-buffer)
5502 (throw 'tramp-action 'ok))
5503
5504(defun tramp-multi-action-permission-denied (p method user host)
5505 "Signal permission denied."
5506 (tramp-message 9 "Permission denied by remote host `%s'" host)
5507 (kill-process p)
5508 (erase-buffer)
5509 (throw 'tramp-action 'permission-denied))
5510
07dfe738
KG
5511(defun tramp-multi-action-process-alive (p method user host)
5512 "Check whether a process has finished."
5513 (unless (memq (process-status p) '(run open))
5514 (throw 'tramp-action 'process-died)))
5515
ac474af1
KG
5516;; Functions for processing the actions.
5517
5518(defun tramp-process-one-action (p multi-method method user host actions)
5519 "Wait for output from the shell and perform one action."
5520 (let (found item pattern action todo)
5521 (erase-buffer)
5522 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5523 (with-timeout (60 (throw 'tramp-action 'timeout))
5524 (while (not found)
d2a2c17f 5525 (tramp-accept-process-output p 1)
ac474af1
KG
5526 (goto-char (point-min))
5527 (setq todo actions)
5528 (while todo
5529 (goto-char (point-min))
5530 (setq item (pop todo))
5531 (setq pattern (symbol-value (nth 0 item)))
5532 (setq action (nth 1 item))
821e6e36
KG
5533 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5534 pattern)
ac474af1
KG
5535 (when (re-search-forward (concat pattern "\\'") nil t)
5536 (setq found (funcall action p multi-method method user host)))))
5537 found)))
5538
5539(defun tramp-process-actions (p multi-method method user host actions)
5540 "Perform actions until success."
57176422 5541 (tramp-message 10 "%s" (mapconcat 'identity (process-command p) " "))
ac474af1
KG
5542 (let (exit)
5543 (while (not exit)
821e6e36 5544 (tramp-message 9 "Waiting for prompts from remote shell")
ac474af1
KG
5545 (setq exit
5546 (catch 'tramp-action
5547 (tramp-process-one-action
5548 p multi-method method user host actions)
5549 nil)))
5550 (unless (eq exit 'ok)
5ec2cc41 5551 (tramp-clear-passwd user host)
ac474af1
KG
5552 (error "Login failed"))))
5553
5554;; For multi-actions.
5555
5556(defun tramp-process-one-multi-action (p method user host actions)
5557 "Wait for output from the shell and perform one action."
5558 (let (found item pattern action todo)
5559 (erase-buffer)
5560 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5561 (with-timeout (60 (throw 'tramp-action 'timeout))
5562 (while (not found)
d2a2c17f 5563 (tramp-accept-process-output p 1)
ac474af1
KG
5564 (setq todo actions)
5565 (goto-char (point-min))
5566 (while todo
5567 (goto-char (point-min))
5568 (setq item (pop todo))
5569 (setq pattern (symbol-value (nth 0 item)))
5570 (setq action (nth 1 item))
821e6e36
KG
5571 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5572 pattern)
ac474af1
KG
5573 (when (re-search-forward (concat pattern "\\'") nil t)
5574 (setq found (funcall action p method user host)))))
5575 found)))
5576
5577(defun tramp-process-multi-actions (p method user host actions)
5578 "Perform actions until success."
5579 (let (exit)
5580 (while (not exit)
821e6e36 5581 (tramp-message 9 "Waiting for prompts from remote shell")
ac474af1
KG
5582 (setq exit
5583 (catch 'tramp-action
5584 (tramp-process-one-multi-action p method user host actions)
5585 nil)))
5586 (unless (eq exit 'ok)
5ec2cc41 5587 (tramp-clear-passwd user host)
ac474af1
KG
5588 (error "Login failed"))))
5589
90f8dc03
KG
5590;; Functions to execute when we have seen the remote shell prompt but
5591;; before we exec the Bourne-ish shell. Note that these commands
5592;; might be sent to any shell, not just a Bourne-ish shell. This
5593;; means that the commands need to work in all shells. (It is also
5594;; okay for some commands to just fail with an error message, but
5595;; please make sure that they at least don't crash the odd shell people
5596;; might be running...)
5597(defun tramp-process-initial-commands (p
5598 multi-method method user host
5599 commands)
5600 "Send list of commands to remote host, in order."
5601 (let (cmd)
5602 (while commands
5603 (setq cmd (pop commands))
5604 (erase-buffer)
5605 (tramp-message 10 "Sending command to remote shell: %s"
5606 cmd)
38c65fca 5607 (tramp-send-command multi-method method user host cmd nil t)
90f8dc03
KG
5608 (tramp-barf-if-no-shell-prompt
5609 p 60 "Remote shell command failed: %s" cmd))
5610 (erase-buffer)))
5611
ac474af1 5612;; The actual functions for opening connections.
fb7933a3
KG
5613
5614(defun tramp-open-connection-telnet (multi-method method user host)
5615 "Open a connection using a telnet METHOD.
5616This starts the command `telnet HOST ARGS'[*], then waits for a remote
5617login prompt, then sends the user name USER, then waits for a remote
5618password prompt. It queries the user for the password, then sends the
5619password to the remote host.
5620
5621If USER is nil, uses value returned by `(user-login-name)' instead.
5622
821e6e36
KG
5623Recognition of the remote shell prompt is based on the variables
5624`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5625set up correctly.
fb7933a3
KG
5626
5627Please note that it is NOT possible to use this connection method
5628together with an out-of-band transfer method! You must use an inline
5629transfer method.
5630
5631Maybe the different regular expressions need to be tuned.
5632
5633* Actually, the telnet program as well as the args to be used can be
5634 specified in the method parameters, see the variable `tramp-methods'."
5635 (save-match-data
16674e4f 5636 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5637 (error "Cannot use out-of-band method `%s' with telnet connection method"
5638 method))
5639 (when multi-method
5640 (error "Cannot multi-connect using telnet connection method"))
7177e2a3 5641 (tramp-pre-connection multi-method method user host tramp-chunksize)
bf247b6e 5642 (tramp-message 7 "Opening connection for %s@%s using %s..."
fb7933a3
KG
5643 (or user (user-login-name)) host method)
5644 (let ((process-environment (copy-sequence process-environment)))
5645 (setenv "TERM" tramp-terminal-type)
a69c01a0 5646 (setenv "PS1" "$ ")
fb7933a3 5647 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5648 ;; If we omit the conditional here, then we would use
5649 ;; `undecided-dos' in some cases. With the conditional,
5650 ;; we use nil in these cases. Which one is right?
fb7933a3
KG
5651 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5652 (> emacs-major-version 20))
5653 tramp-dos-coding-system))
5654 (p (apply 'start-process
5655 (tramp-buffer-name multi-method method user host)
5656 (tramp-get-buffer multi-method method user host)
c951aecb 5657 (tramp-get-method-parameter
16674e4f 5658 multi-method
91879624 5659 (tramp-find-method multi-method method user host)
c951aecb 5660 user host 'tramp-login-program)
fb7933a3 5661 host
c951aecb 5662 (tramp-get-method-parameter
16674e4f 5663 multi-method
91879624 5664 (tramp-find-method multi-method method user host)
c951aecb 5665 user host 'tramp-login-args)))
fb7933a3
KG
5666 (found nil)
5667 (pw nil))
19a87064 5668 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5669 (set-buffer (tramp-get-buffer multi-method method user host))
5670 (erase-buffer)
5671 (tramp-process-actions p multi-method method user host
5672 tramp-actions-before-shell)
fb7933a3
KG
5673 (tramp-open-connection-setup-interactive-shell
5674 p multi-method method user host)
5675 (tramp-post-connection multi-method method user host)))))
5676
bf247b6e 5677
fb7933a3
KG
5678(defun tramp-open-connection-rsh (multi-method method user host)
5679 "Open a connection using an rsh METHOD.
5680This starts the command `rsh HOST -l USER'[*], then waits for a remote
5681password or shell prompt. If a password prompt is seen, the user is
5682queried for a password, this function sends the password to the remote
5683host and waits for a shell prompt.
5684
5685If USER is nil, start the command `rsh HOST'[*] instead
5686
821e6e36
KG
5687Recognition of the remote shell prompt is based on the variables
5688`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5689set up correctly.
fb7933a3 5690
8e3a1104
KG
5691Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5692be a port number for ssh, and \"-p yy\" will be added to the list of
5693arguments, and xx will be used as the host name to connect to.
5694
fb7933a3
KG
5695* Actually, the rsh program to be used can be specified in the
5696 method parameters, see the variable `tramp-methods'."
5697 (save-match-data
5698 (when multi-method
5699 (error "Cannot multi-connect using rsh connection method"))
7177e2a3 5700 (tramp-pre-connection multi-method method user host tramp-chunksize)
16674e4f 5701 (if (and user (not (string= user "")))
bf247b6e 5702 (tramp-message 7 "Opening connection for %s@%s using %s..."
fb7933a3
KG
5703 user host method)
5704 (tramp-message 7 "Opening connection at %s using %s..." host method))
8e3a1104
KG
5705 (let ((process-environment (copy-sequence process-environment))
5706 (bufnam (tramp-buffer-name multi-method method user host))
5707 (buf (tramp-get-buffer multi-method method user host))
c951aecb 5708 (login-program (tramp-get-method-parameter
16674e4f 5709 multi-method
91879624 5710 (tramp-find-method multi-method method user host)
c951aecb 5711 user host 'tramp-login-program))
57176422
MA
5712 (login-args (mapcar
5713 (lambda (x)
5714 (format-spec
5715 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
5716 (tramp-get-method-parameter
5717 multi-method
5718 (tramp-find-method multi-method method user host)
5719 user host 'tramp-login-args)))
07dfe738 5720 (real-host host))
8e3a1104
KG
5721 ;; The following should be changed. We need a more general
5722 ;; mechanism to parse extra host args.
5723 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5ec2cc41 5724 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
07dfe738 5725 (setq real-host (match-string 1 host)))
fb7933a3 5726 (setenv "TERM" tramp-terminal-type)
a69c01a0 5727 (setenv "PS1" "$ ")
fb7933a3 5728 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5729 ;; If we omit the conditional, we would use
5730 ;; `undecided-dos' in some cases. With the conditional,
5731 ;; we use nil in these cases. Which one is right?
fb7933a3
KG
5732 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5733 (> emacs-major-version 20))
5734 tramp-dos-coding-system))
16674e4f 5735 (p (if (and user (not (string= user "")))
bf247b6e 5736 (apply #'start-process bufnam buf login-program
07dfe738 5737 real-host "-l" user login-args)
bf247b6e 5738 (apply #'start-process bufnam buf login-program
07dfe738 5739 real-host login-args)))
fb7933a3 5740 (found nil))
19a87064 5741 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5742
5743 (set-buffer buf)
5744 (tramp-process-actions p multi-method method user host
5745 tramp-actions-before-shell)
fb7933a3
KG
5746 (tramp-message 7 "Initializing remote shell")
5747 (tramp-open-connection-setup-interactive-shell
5748 p multi-method method user host)
5749 (tramp-post-connection multi-method method user host)))))
5750
fb7933a3
KG
5751(defun tramp-open-connection-su (multi-method method user host)
5752 "Open a connection using the `su' program with METHOD.
5753This starts `su - USER', then waits for a password prompt. The HOST
5754name must be equal to the local host name or to `localhost'.
5755
5756If USER is nil, uses value returned by user-login-name instead.
5757
821e6e36
KG
5758Recognition of the remote shell prompt is based on the variables
5759`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5760set up correctly. Note that the other user may have a different shell
5761prompt than you do, so it is not at all unlikely that the variable
5762`shell-prompt-pattern' is set up wrongly!"
fb7933a3 5763 (save-match-data
16674e4f 5764 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5765 (error "Cannot use out-of-band method `%s' with `su' connection method"
5766 method))
5767 (unless (or (string-match (concat "^" (regexp-quote host))
5768 (system-name))
16674e4f
KG
5769 (string= "localhost" host)
5770 (string= "" host))
fb7933a3
KG
5771 (error
5772 "Cannot connect to different host `%s' with `su' connection method"
5773 host))
7177e2a3 5774 (tramp-pre-connection multi-method method user host tramp-chunksize)
16674e4f
KG
5775 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5776 (or user "<root>") method)
fb7933a3
KG
5777 (let ((process-environment (copy-sequence process-environment)))
5778 (setenv "TERM" tramp-terminal-type)
a69c01a0 5779 (setenv "PS1" "$ ")
fb7933a3 5780 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5781 ;; If we omit the conditional, we use `undecided-dos' in
5782 ;; some cases. With the conditional, we use nil in these
5783 ;; cases. What's the difference? Which one is right?
fb7933a3
KG
5784 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5785 (> emacs-major-version 20))
5786 tramp-dos-coding-system))
5787 (p (apply 'start-process
ac474af1
KG
5788 (tramp-buffer-name multi-method method user host)
5789 (tramp-get-buffer multi-method method user host)
c951aecb 5790 (tramp-get-method-parameter
16674e4f 5791 multi-method
91879624 5792 (tramp-find-method multi-method method user host)
c951aecb 5793 user host 'tramp-login-program)
fb7933a3 5794 (mapcar
4007ba5b
KG
5795 (lambda (x)
5796 (format-spec x `((?u . ,(or user "root")))))
c951aecb 5797 (tramp-get-method-parameter
16674e4f 5798 multi-method
91879624 5799 (tramp-find-method multi-method method user host)
c951aecb 5800 user host 'tramp-login-args))))
fb7933a3
KG
5801 (found nil)
5802 (pw nil))
19a87064 5803 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5804 (set-buffer (tramp-get-buffer multi-method method user host))
5805 (tramp-process-actions p multi-method method user host
5806 tramp-actions-before-shell)
fb7933a3
KG
5807 (tramp-open-connection-setup-interactive-shell
5808 p multi-method method user host)
bf247b6e 5809 (tramp-post-connection multi-method method
fb7933a3
KG
5810 user host)))))
5811
bf247b6e 5812;; HHH: Not Changed. Multi method. It is not clear to me how this can
fb7933a3
KG
5813;; handle not giving a user name in the "file name".
5814;;
5815;; This is more difficult than for the single-hop method. In the
5816;; multi-hop-method, the desired behaviour should be that the
5817;; user must specify names for the telnet hops of which the user
5818;; name is different than the "original" name (or different from
5819;; the previous hop.
5820(defun tramp-open-connection-multi (multi-method method user host)
5821 "Open a multi-hop connection using METHOD.
5822This uses a slightly changed file name syntax. The idea is to say
5823 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5824This will use telnet to log in as u1 to h1, then use rsh from there to
5825log in as u2 to h2."
5826 (save-match-data
5827 (unless multi-method
5828 (error "Multi-hop open connection function called on non-multi method"))
16674e4f 5829 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5830 (error "No out of band multi-hop connections"))
5831 (unless (and (arrayp method) (not (stringp method)))
5832 (error "METHOD must be an array of strings for multi methods"))
5833 (unless (and (arrayp user) (not (stringp user)))
5834 (error "USER must be an array of strings for multi methods"))
5835 (unless (and (arrayp host) (not (stringp host)))
5836 (error "HOST must be an array of strings for multi methods"))
5837 (unless (and (= (length method) (length user))
5838 (= (length method) (length host)))
5839 (error "Arrays METHOD, USER, HOST must have equal length"))
7177e2a3 5840 (tramp-pre-connection multi-method method user host tramp-chunksize)
fb7933a3
KG
5841 (tramp-message 7 "Opening `%s' connection..." multi-method)
5842 (let ((process-environment (copy-sequence process-environment)))
5843 (setenv "TERM" tramp-terminal-type)
a69c01a0 5844 (setenv "PS1" "$ ")
fb7933a3 5845 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5846 ;; If we omit the conditional, we use `undecided-dos' in
5847 ;; some cases. With the conditional, we use nil in these
5848 ;; cases. What's the difference? Which one is right?
fb7933a3
KG
5849 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5850 (> emacs-major-version 20))
5851 tramp-dos-coding-system))
5852 (p (start-process (tramp-buffer-name multi-method method user host)
5853 (tramp-get-buffer multi-method method user host)
90dc758d 5854 tramp-multi-sh-program))
fb7933a3
KG
5855 (num-hops (length method))
5856 (i 0))
19a87064 5857 (tramp-set-process-query-on-exit-flag p nil)
fb7933a3
KG
5858 (tramp-message 9 "Waiting 60s for local shell to come up...")
5859 (unless (tramp-wait-for-regexp
821e6e36
KG
5860 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5861 shell-prompt-pattern tramp-shell-prompt-pattern))
fb7933a3
KG
5862 (pop-to-buffer (buffer-name))
5863 (kill-process p)
5864 (error "Couldn't find local shell prompt"))
5865 ;; Now do all the connections as specified.
5866 (while (< i num-hops)
5867 (let* ((m (aref method i))
5868 (u (aref user i))
5869 (h (aref host i))
5870 (entry (assoc m tramp-multi-connection-function-alist))
5871 (multi-func (nth 1 entry))
5872 (command (nth 2 entry)))
dba28077 5873 ;; The multi-funcs don't need to do save-match-data, as that
fb7933a3
KG
5874 ;; is done here.
5875 (funcall multi-func p m u h command)
5876 (erase-buffer)
5877 (incf i)))
5878 (tramp-open-connection-setup-interactive-shell
5879 p multi-method method user host)
5880 (tramp-post-connection multi-method method user host)))))
5881
5882;; HHH: Changed. Multi method. Don't know how to handle this in the case
bf247b6e 5883;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5884;; changed `user' to `(or user (user-login-name))' in the places where
5885;; the value is actually used.
5886(defun tramp-multi-connect-telnet (p method user host command)
5887 "Issue `telnet' command.
5888Uses shell COMMAND to issue a `telnet' command to log in as USER to
5889HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5890the host name, and `%n' is replaced with an end of line character, as
5891set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5892character.
5893
5894If USER is nil, uses the return value of (user-login-name) instead."
ac474af1
KG
5895 (let ((cmd (format-spec command
5896 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5897 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
fb7933a3
KG
5898 found pw)
5899 (erase-buffer)
5900 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5901 (process-send-string p cmd)
ac474af1 5902 (tramp-process-multi-actions p method user host
dba28077 5903 tramp-multi-actions)))
fb7933a3 5904
bf247b6e
KS
5905;; HHH: Changed. Multi method. Don't know how to handle this in the case
5906;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5907;; changed `user' to `(or user (user-login-name))' in the places where
5908;; the value is actually used.
5909(defun tramp-multi-connect-rlogin (p method user host command)
5910 "Issue `rlogin' command.
5911Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5912HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5913with the user name, `%h' will be replaced with the host name, and `%n'
5914will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5915`%%' if you want to use a literal percent character.
5916
5917If USER is nil, uses the return value of (user-login-name) instead."
ac474af1
KG
5918 (let ((cmd (format-spec command `((?h . ,host)
5919 (?u . ,(or user (user-login-name)))
5920 (?n . ,tramp-rsh-end-of-line))))
5921 (cmd1 (format-spec command `((?h . ,host)
5922 (?u . ,(or user (user-login-name)))
5923 (?n . ""))))
fb7933a3
KG
5924 found)
5925 (erase-buffer)
5926 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5927 (process-send-string p cmd)
ac474af1 5928 (tramp-process-multi-actions p method user host
dba28077 5929 tramp-multi-actions)))
fb7933a3 5930
bf247b6e
KS
5931;; HHH: Changed. Multi method. Don't know how to handle this in the case
5932;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5933;; changed `user' to `(or user (user-login-name))' in the places where
5934;; the value is actually used.
5935(defun tramp-multi-connect-su (p method user host command)
5936 "Issue `su' command.
5937Uses shell COMMAND to issue a `su' command to log in as USER on
5938HOST. The HOST name is ignored, this just changes the user id on the
5939host currently logged in to.
5940
5941If USER is nil, uses the return value of (user-login-name) instead.
5942
5943You can use percent escapes in the COMMAND. `%u' is replaced with the
5944user name, and `%n' is replaced with the value of
5945`tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5946character."
ac474af1
KG
5947 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5948 (?n . ,tramp-rsh-end-of-line))))
5949 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5950 (?n . ""))))
fb7933a3
KG
5951 found)
5952 (erase-buffer)
5953 (tramp-message 9 "Sending su command `%s'" cmd1)
5954 (process-send-string p cmd)
ac474af1 5955 (tramp-process-multi-actions p method user host
dba28077 5956 tramp-multi-actions)))
fb7933a3
KG
5957
5958;; Utility functions.
5959
d2a2c17f
MA
5960(defun tramp-accept-process-output
5961 (&optional process timeout timeout-msecs)
5962 "Like `accept-process-output' for Tramp processes.
5963This is needed in order to hide `last-coding-system-used', which is set
5964for process communication also."
5965 (let (last-coding-system-used)
5966 (accept-process-output process timeout timeout-msecs)))
5967
fb7933a3
KG
5968(defun tramp-wait-for-regexp (proc timeout regexp)
5969 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5970Expects the output of PROC to be sent to the current buffer. Returns
5971the string that matched, or nil. Waits indefinitely if TIMEOUT is
5972nil."
5973 (let ((found nil)
5974 (start-time (current-time)))
5975 (cond (timeout
5976 ;; Work around a bug in XEmacs 21, where the timeout
5977 ;; expires faster than it should. This degenerates
5978 ;; to polling for buggy XEmacsen, but oh, well.
5979 (while (and (not found)
5980 (< (tramp-time-diff (current-time) start-time)
5981 timeout))
5982 (with-timeout (timeout)
5983 (while (not found)
d2a2c17f 5984 (tramp-accept-process-output proc 1)
19a87064
MA
5985 (unless (memq (process-status proc) '(run open))
5986 (error "Process has died"))
fb7933a3 5987 (goto-char (point-min))
d2a2c17f 5988 (setq found (re-search-forward regexp nil t))))))
fb7933a3
KG
5989 (t
5990 (while (not found)
d2a2c17f 5991 (tramp-accept-process-output proc 1)
19a87064
MA
5992 (unless (memq (process-status proc) '(run open))
5993 (error "Process has died"))
fb7933a3 5994 (goto-char (point-min))
d2a2c17f 5995 (setq found (re-search-forward regexp nil t)))))
fb7933a3
KG
5996 (when tramp-debug-buffer
5997 (append-to-buffer
5998 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5999 tramp-current-user tramp-current-host)
6000 (point-min) (point-max))
6001 (when (not found)
6002 (save-excursion
6003 (set-buffer
6004 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6005 tramp-current-user tramp-current-host))
6006 (goto-char (point-max))
6007 (insert "[[Regexp `" regexp "' not found"
487fa986 6008 (if timeout (format " in %d secs" timeout) "")
fb7933a3
KG
6009 "]]"))))
6010 found))
6011
b25a52cc
KG
6012(defun tramp-wait-for-shell-prompt (proc timeout)
6013 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
6014See `tramp-wait-for-regexp' for more details.
6015Shell prompt pattern is determined by variables `shell-prompt-pattern'
6016and `tramp-shell-prompt-pattern'."
6017 (tramp-wait-for-regexp
6018 proc timeout
6019 (format "\\(%s\\|%s\\)\\'"
6020 shell-prompt-pattern tramp-shell-prompt-pattern)))
6021
6022(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6023 "Wait for shell prompt and barf if none appears.
6024Looks at process PROC to see if a shell prompt appears in TIMEOUT
6025seconds. If not, it produces an error message with the given ERROR-ARGS."
6026 (unless (tramp-wait-for-shell-prompt proc timeout)
6027 (pop-to-buffer (buffer-name))
6028 (apply 'error error-args)))
6029
07dfe738 6030(defun tramp-enter-password (p prompt user host)
fb7933a3
KG
6031 "Prompt for a password and send it to the remote end.
6032Uses PROMPT as a prompt and sends the password to process P."
07dfe738 6033 (let ((pw (tramp-read-passwd user host prompt)))
ac474af1 6034 (erase-buffer)
90f8dc03
KG
6035 (process-send-string
6036 p (concat pw
c951aecb
KG
6037 (or (tramp-get-method-parameter
6038 tramp-current-multi-method
6039 tramp-current-method
6040 tramp-current-user
6041 tramp-current-host
6042 'tramp-password-end-of-line)
6043 tramp-default-password-end-of-line)))))
fb7933a3
KG
6044
6045;; HHH: Not Changed. This might handle the case where USER is not
6046;; given in the "File name" very poorly. Then, the local
19a87064 6047;; variable tramp-current-user will be set to nil.
7177e2a3 6048(defun tramp-pre-connection (multi-method method user host chunksize)
fb7933a3
KG
6049 "Do some setup before actually logging in.
6050METHOD, USER and HOST specify the connection."
6051 (set-buffer (tramp-get-buffer multi-method method user host))
6052 (set (make-local-variable 'tramp-current-multi-method) multi-method)
6053 (set (make-local-variable 'tramp-current-method) method)
6054 (set (make-local-variable 'tramp-current-user) user)
6055 (set (make-local-variable 'tramp-current-host) host)
7177e2a3 6056 (set (make-local-variable 'tramp-chunksize) chunksize)
fb7933a3
KG
6057 (set (make-local-variable 'inhibit-eol-conversion) nil)
6058 (erase-buffer))
6059
6060(defun tramp-open-connection-setup-interactive-shell
6061 (p multi-method method user host)
6062 "Set up an interactive shell.
6063Mainly sets the prompt and the echo correctly. P is the shell process
6064to set up. METHOD, USER and HOST specify the connection."
6065 ;; Wait a bit in case the remote end feels like sending a little
6066 ;; junk first. It seems that fencepost.gnu.org does this when doing
6067 ;; a Kerberos login.
6068 (sit-for 1)
6069 (tramp-discard-garbage-erase-buffer p multi-method method user host)
90f8dc03
KG
6070 (tramp-process-initial-commands p multi-method method user host
6071 tramp-initial-commands)
16674e4f
KG
6072 ;; It is useful to set the prompt in the following command because
6073 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6074 ;; about and thus /bin/sh will display a strange prompt. For
6075 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6076 ;; the current working directory but /bin/sh will display a dollar
6077 ;; sign. The following command line sets $PS1 to a sane value, and
6078 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6079 ;; Pittman reports that the unusual positioning of the single quotes
7432277c
KG
6080 ;; makes it work under `rc', too. We also unset the variable $ENV
6081 ;; because that is read by some sh implementations (eg, bash when
6082 ;; called as sh) on startup; this way, we avoid the startup file
6083 ;; clobbering $PS1.
b25a52cc
KG
6084 (tramp-send-command-internal
6085 multi-method method user host
6086 (format "exec env 'ENV=' 'PS1=$ ' %s"
c951aecb
KG
6087 (tramp-get-method-parameter
6088 multi-method method user host 'tramp-remote-sh))
b25a52cc 6089 (format "remote `%s' to come up"
c951aecb
KG
6090 (tramp-get-method-parameter
6091 multi-method method user host 'tramp-remote-sh)))
b25a52cc
KG
6092 (tramp-barf-if-no-shell-prompt
6093 p 30
6094 "Remote `%s' didn't come up. See buffer `%s' for details"
c951aecb 6095 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
b25a52cc
KG
6096 (buffer-name))
6097 (tramp-message 8 "Setting up remote shell environment")
fb7933a3 6098 (tramp-discard-garbage-erase-buffer p multi-method method user host)
b25a52cc
KG
6099 (tramp-send-command-internal multi-method method user host
6100 "stty -inlcr -echo kill '^U'")
fb7933a3 6101 (erase-buffer)
38c65fca
KG
6102 ;; Ignore garbage after stty command.
6103 (tramp-send-command-internal multi-method method user host
6104 "echo foo")
6105 (erase-buffer)
b25a52cc
KG
6106 (tramp-send-command-internal multi-method method user host
6107 "TERM=dumb; export TERM")
7177e2a3
MA
6108 (erase-buffer)
6109 ;; Check whether the remote host suffers from buggy `send-process-string'.
6110 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6111 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6112 ;; Emacs makes a hack when this host type is detected locally. It cannot
6113 ;; handle remote hosts, though.
6114 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6115 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6116 (tramp-send-command-internal multi-method method user host
6117 "(uname -sr) 2>/dev/null")
6118 (goto-char (point-min))
6119 (when (looking-at "FreeBSD")
6120 (setq tramp-chunksize 500)))
6121
fb7933a3
KG
6122 ;; Try to set up the coding system correctly.
6123 ;; CCC this can't be the right way to do it. Hm.
6124 (save-excursion
6125 (erase-buffer)
6126 (tramp-message 9 "Determining coding system")
b25a52cc
KG
6127 (tramp-send-command-internal multi-method method user host
6128 "echo foo ; echo bar")
fb7933a3
KG
6129 (goto-char (point-min))
6130 (if (featurep 'mule)
6131 ;; Use MULE to select the right EOL convention for communicating
6132 ;; with the process.
6133 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6134 cs-decode cs-encode)
6135 (when (symbolp cs) (setq cs (cons cs cs)))
6136 (setq cs-decode (car cs))
6137 (setq cs-encode (cdr cs))
6138 (unless cs-decode (setq cs-decode 'undecided))
6139 (unless cs-encode (setq cs-encode 'undecided))
6140 (setq cs-encode (tramp-coding-system-change-eol-conversion
6141 cs-encode 'unix))
6142 (when (search-forward "\r" nil t)
6143 (setq cs-decode (tramp-coding-system-change-eol-conversion
6144 cs-decode 'dos)))
6145 (set-buffer-process-coding-system cs-decode cs-encode))
6146 ;; Look for ^M and do something useful if found.
6147 (when (search-forward "\r" nil t)
6148 ;; We have found a ^M but cannot frob the process coding system
6149 ;; because we're running on a non-MULE Emacs. Let's try
6150 ;; stty, instead.
90f8dc03 6151 (erase-buffer)
fb7933a3 6152 (tramp-message 9 "Trying `stty -onlcr'")
b25a52cc
KG
6153 (tramp-send-command-internal multi-method method user host
6154 "stty -onlcr"))))
fb7933a3
KG
6155 (erase-buffer)
6156 (tramp-message
19a87064
MA
6157 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6158 (tramp-send-command-internal
6159 multi-method method user host
6160 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
fb7933a3
KG
6161 (erase-buffer)
6162 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
b25a52cc
KG
6163 (tramp-send-command-internal multi-method method user host
6164 "set +o vi +o emacs")
fb7933a3
KG
6165 (erase-buffer)
6166 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
b25a52cc
KG
6167 (tramp-send-command-internal
6168 multi-method method user host
6169 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
fb7933a3
KG
6170 (erase-buffer)
6171 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
b25a52cc
KG
6172 (tramp-send-command-internal multi-method method user host
6173 "unset CDPATH")
fb7933a3
KG
6174 (erase-buffer)
6175 (tramp-message 9 "Setting shell prompt")
16674e4f 6176 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
3b89d388
KG
6177 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6178 ;; the last time we sent a command, to avoid tramp-send-command to send
6179 ;; "echo are you awake".
6180 (setq tramp-last-cmd-time (current-time))
fb7933a3
KG
6181 (tramp-send-command
6182 multi-method method user host
6183 (format "PS1='%s%s%s'; PS2=''; PS3=''"
16674e4f 6184 tramp-rsh-end-of-line
fb7933a3 6185 tramp-end-of-output
16674e4f
KG
6186 tramp-rsh-end-of-line))
6187 (tramp-wait-for-output))
fb7933a3
KG
6188
6189(defun tramp-post-connection (multi-method method user host)
6190 "Prepare a remote shell before being able to work on it.
6191METHOD, USER and HOST specify the connection.
6192Among other things, this finds a shell which groks tilde expansion,
6193tries to find an `ls' command which groks the `-n' option, sets the
6194locale to C and sets up the remote shell search path."
6195 ;; Search for a good shell before searching for a command which
6196 ;; checks if a file exists. This is done because Tramp wants to use
6197 ;; "test foo; echo $?" to check if various conditions hold, and
6198 ;; there are buggy /bin/sh implementations which don't execute the
6199 ;; "echo $?" part if the "test" part has an error. In particular,
6200 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6201 ;; with buggy /bin/sh implementations will have a working bash or
6202 ;; ksh. Whee...
6203 (tramp-find-shell multi-method method user host)
fb7933a3
KG
6204 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6205 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6206 ;; output of (tramp-find-shell) to show up along with the output of
6207 ;; (tramp-find-ls-command) testing.
6208 ;;
6209 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6210 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6211 ;;
6212 ;; After much debugging I couldn't find any problem with the implementation
6213 ;; of that function though. The workaround stays for me at least. :/
6214 ;;
6215 ;; Daniel Pittman <daniel@danann.net>
fabf2143 6216 (sleep-for 1)
5beaf831 6217 (erase-buffer)
fabf2143 6218 (tramp-find-file-exists-command multi-method method user host)
fb7933a3
KG
6219 (make-local-variable 'tramp-ls-command)
6220 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6221 (unless tramp-ls-command
6222 (tramp-message
6223 1
6224 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6225 (setq tramp-ls-command
6226 (tramp-find-executable multi-method method user host
6227 "ls" tramp-remote-path nil)))
6228 (unless tramp-ls-command
6229 (error "Fatal error: Couldn't find remote executable `ls'"))
6230 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6231 tramp-ls-command)
6232 (tramp-send-command multi-method method user host
6233 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6234 "return $1" tramp-rsh-end-of-line
6235 "}"))
6236 (tramp-wait-for-output)
6237 ;; Set remote PATH variable.
6238 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6239 ;; Tell remote shell to use standard time format, needed for
6240 ;; parsing `ls -l' output.
6241 (tramp-send-command multi-method method user host
6242 "LC_TIME=C; export LC_TIME; echo huhu")
6243 (tramp-wait-for-output)
6244 (tramp-send-command multi-method method user host
6245 "mesg n; echo huhu")
6246 (tramp-wait-for-output)
6247 (tramp-send-command multi-method method user host
6248 "biff n ; echo huhu")
6249 (tramp-wait-for-output)
6250 ;; Unalias ls(1) to work around issues with those silly people who make it
6251 ;; spit out ANSI escapes or whatever.
6252 (tramp-send-command multi-method method user host
6253 "unalias ls; echo huhu")
6254 (tramp-wait-for-output)
6255 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6256 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6257 ;; for otherwise the shell crashes.
6258 (erase-buffer)
6259 (make-local-variable 'tramp-test-groks-nt)
6260 (tramp-send-command multi-method method user host
6261 "( test / -nt / )")
6262 (tramp-wait-for-output)
6263 (goto-char (point-min))
6264 (setq tramp-test-groks-nt
16674e4f 6265 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
fb7933a3
KG
6266 (unless tramp-test-groks-nt
6267 (tramp-send-command
6268 multi-method method user host
6269 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6270 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6271 "}")))
6272 (tramp-wait-for-output)
fabf2143
KG
6273 ;; Send the fallback `uudecode' script.
6274 (erase-buffer)
7432277c 6275 (tramp-send-string multi-method method user host tramp-uudecode)
fabf2143 6276 (tramp-wait-for-output)
fb7933a3
KG
6277 ;; Find a `perl'.
6278 (erase-buffer)
c82c5727 6279 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
fb7933a3
KG
6280 (let ((tramp-remote-perl
6281 (or (tramp-find-executable multi-method method user host
fabf2143 6282 "perl5" tramp-remote-path nil)
fb7933a3 6283 (tramp-find-executable multi-method method user host
fabf2143 6284 "perl" tramp-remote-path nil))))
fb7933a3 6285 (when tramp-remote-perl
fabf2143
KG
6286 (tramp-set-connection-property "perl" tramp-remote-perl
6287 multi-method method user host)
c82c5727
LH
6288 (unless (tramp-method-out-of-band-p multi-method method user host)
6289 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6290 (tramp-send-string
6291 multi-method method user host
6292 (concat "tramp_encode () {\n"
6293 (format tramp-perl-encode tramp-remote-perl)
6294 " 2>/dev/null"
6295 "\n}"))
6296 (tramp-wait-for-output)
6297 (tramp-send-string
6298 multi-method method user host
6299 (concat "tramp_encode_with_module () {\n"
6300 (format tramp-perl-encode-with-module tramp-remote-perl)
6301 " 2>/dev/null"
6302 "\n}"))
6303 (tramp-wait-for-output)
6304 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6305 (tramp-send-string
6306 multi-method method user host
6307 (concat "tramp_decode () {\n"
6308 (format tramp-perl-decode tramp-remote-perl)
6309 " 2>/dev/null"
6310 "\n}"))
6311 (tramp-wait-for-output)
6312 (tramp-send-string
6313 multi-method method user host
6314 (concat "tramp_decode_with_module () {\n"
6315 (format tramp-perl-decode-with-module tramp-remote-perl)
6316 " 2>/dev/null"
6317 "\n}"))
6318 (tramp-wait-for-output))))
fb7933a3
KG
6319 ;; Find ln(1)
6320 (erase-buffer)
6321 (let ((ln (tramp-find-executable multi-method method user host
6322 "ln" tramp-remote-path nil)))
6323 (when ln
6324 (tramp-set-connection-property "ln" ln multi-method method user host)))
a69c01a0 6325 ;; Set uid and gid.
fb7933a3 6326 (erase-buffer)
a69c01a0
MA
6327 (tramp-send-command multi-method method user host "id -u; id -g")
6328 (tramp-wait-for-output)
6329 (goto-char (point-min))
6330 (tramp-set-connection-property
6331 "uid" (read (current-buffer)) multi-method method user host)
6332 (tramp-set-connection-property
6333 "gid" (read (current-buffer)) multi-method method user host)
ac474af1 6334 ;; Find the right encoding/decoding commands to use.
a69c01a0 6335 (erase-buffer)
5ec2cc41 6336 (unless (tramp-method-out-of-band-p multi-method method user host)
ac474af1 6337 (tramp-find-inline-encoding multi-method method user host))
fb7933a3
KG
6338 ;; If encoding/decoding command are given, test to see if they work.
6339 ;; CCC: Maybe it would be useful to run the encoder both locally and
6340 ;; remotely to see if they produce the same result.
16674e4f
KG
6341 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6342 (rem-dec (tramp-get-remote-decoding multi-method method user host))
fb7933a3 6343 (magic-string "xyzzy"))
16674e4f 6344 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
fb7933a3 6345 (tramp-kill-process multi-method method user host)
16674e4f 6346 ;; Improve error message and/or error check.
fb7933a3
KG
6347 (error
6348 "Must give both decoding and encoding command in method definition"))
16674e4f 6349 (when (and rem-enc rem-dec)
fb7933a3
KG
6350 (tramp-message
6351 5
6352 "Checking to see if encoding/decoding commands work on remote host...")
6353 (tramp-send-command
6354 multi-method method user host
6355 (format "echo %s | %s | %s"
16674e4f 6356 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
fb7933a3
KG
6357 (tramp-wait-for-output)
6358 (unless (looking-at (regexp-quote magic-string))
6359 (tramp-kill-process multi-method method user host)
6360 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6361 (buffer-name)))
6362 (erase-buffer)
6363 (tramp-message
6364 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6365
ac474af1
KG
6366;; CCC: We should either implement a Perl version of base64 encoding
6367;; and decoding. Then we just use that in the last item. The other
6368;; alternative is to use the Perl version of UU encoding. But then
6369;; we need a Lisp version of uuencode.
16674e4f
KG
6370;;
6371;; Old text from documentation of tramp-methods:
6372;; Using a uuencode/uudecode inline method is discouraged, please use one
6373;; of the base64 methods instead since base64 encoding is much more
6374;; reliable and the commands are more standardized between the different
6375;; Unix versions. But if you can't use base64 for some reason, please
6376;; note that the default uudecode command does not work well for some
6377;; Unices, in particular AIX and Irix. For AIX, you might want to use
6378;; the following command for uudecode:
6379;;
6380;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6381;;
6382;; For Irix, no solution is known yet.
6383
ac474af1
KG
6384(defvar tramp-coding-commands
6385 '(("mimencode -b" "mimencode -u -b"
6386 base64-encode-region base64-decode-region)
6387 ("mmencode -b" "mmencode -u -b"
6388 base64-encode-region base64-decode-region)
6389 ("recode data..base64" "recode base64..data"
6390 base64-encode-region base64-decode-region)
e7b52b6d
KG
6391 ("uuencode xxx" "uudecode -o /dev/stdout"
6392 tramp-uuencode-region uudecode-decode-region)
ac474af1 6393 ("uuencode xxx" "uudecode -o -"
16674e4f 6394 tramp-uuencode-region uudecode-decode-region)
ac474af1 6395 ("uuencode xxx" "uudecode -p"
16674e4f 6396 tramp-uuencode-region uudecode-decode-region)
fabf2143 6397 ("uuencode xxx" "tramp_uudecode"
16674e4f 6398 tramp-uuencode-region uudecode-decode-region)
b1d06e75
KG
6399 ("tramp_encode_with_module" "tramp_decode_with_module"
6400 base64-encode-region base64-decode-region)
ac474af1
KG
6401 ("tramp_encode" "tramp_decode"
6402 base64-encode-region base64-decode-region))
6403 "List of coding commands for inline transfer.
16674e4f
KG
6404Each item is a list that looks like this:
6405
6406\(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
ac474af1 6407
16674e4f
KG
6408The REMOTE-ENCODING should be a string, giving a command accepting a
6409plain file on standard input and writing the encoded file to standard
6410output. The REMOTE-DECODING should also be a string, giving a command
6411accepting an encoded file on standard input and writing the decoded
6412file to standard output.
ac474af1 6413
16674e4f
KG
6414LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6415symbols, giving functions. If they are strings, then they can contain
6416the \"%s\" format specifier. If that specifier is present, the input
6417filename will be put into the command line at that spot. If the
6418specifier is not present, the input should be read from standard
6419input.
ac474af1 6420
16674e4f
KG
6421If they are functions, they will be called with two arguments, start
6422and end of region, and are expected to replace the region contents
6423with the encoded or decoded results, respectively.")
ac474af1
KG
6424
6425(defun tramp-find-inline-encoding (multi-method method user host)
6426 "Find an inline transfer encoding that works.
6427Goes through the list `tramp-coding-commands'."
6428 (let ((commands tramp-coding-commands)
3b89d388 6429 (magic "xyzzy")
ac474af1
KG
6430 item found)
6431 (while (and commands (null found))
6432 (setq item (pop commands))
6433 (catch 'wont-work
16674e4f
KG
6434 (let ((rem-enc (nth 0 item))
6435 (rem-dec (nth 1 item))
6436 (loc-enc (nth 2 item))
6437 (loc-dec (nth 3 item)))
6438 ;; Check if remote encoding and decoding commands can be
6439 ;; called remotely with null input and output. This makes
6440 ;; sure there are no syntax errors and the command is really
3b89d388
KG
6441 ;; found. Note that we do not redirect stdout to /dev/null,
6442 ;; for two reaons: when checking the decoding command, we
6443 ;; actually check the output it gives. And also, when
6444 ;; redirecting "mimencode" output to /dev/null, then as root
6445 ;; it might change the permissions of /dev/null!
ac474af1 6446 (tramp-message-for-buffer
821e6e36 6447 multi-method method user host 9
16674e4f 6448 "Checking remote encoding command `%s' for sanity" rem-enc)
ac474af1
KG
6449 (unless (zerop (tramp-send-command-and-check
6450 multi-method method user host
3b89d388 6451 (format "%s </dev/null" rem-enc) t))
ac474af1
KG
6452 (throw 'wont-work nil))
6453 (tramp-message-for-buffer
821e6e36 6454 multi-method method user host 9
16674e4f 6455 "Checking remote decoding command `%s' for sanity" rem-dec)
ac474af1
KG
6456 (unless (zerop (tramp-send-command-and-check
6457 multi-method method user host
3b89d388
KG
6458 (format "echo %s | %s | %s"
6459 magic rem-enc rem-dec) t))
ac474af1 6460 (throw 'wont-work nil))
3b89d388
KG
6461 (save-excursion
6462 (goto-char (point-min))
6463 (unless (looking-at (regexp-quote magic))
6464 (throw 'wont-work nil)))
16674e4f
KG
6465 ;; If the local encoder or decoder is a string, the
6466 ;; corresponding command has to work locally.
6467 (when (stringp loc-enc)
ac474af1 6468 (tramp-message-for-buffer
821e6e36 6469 multi-method method user host 9
16674e4f
KG
6470 "Checking local encoding command `%s' for sanity" loc-enc)
6471 (unless (zerop (tramp-call-local-coding-command
6472 loc-enc nil nil))
ac474af1 6473 (throw 'wont-work nil)))
16674e4f 6474 (when (stringp loc-dec)
ac474af1 6475 (tramp-message-for-buffer
821e6e36 6476 multi-method method user host 9
16674e4f
KG
6477 "Checking local decoding command `%s' for sanity" loc-dec)
6478 (unless (zerop (tramp-call-local-coding-command
6479 loc-dec nil nil))
ac474af1
KG
6480 (throw 'wont-work nil)))
6481 ;; CCC: At this point, maybe we should check that the output
6482 ;; of the commands is correct. But for the moment we will
6483 ;; assume that commands working on empty input will also
6484 ;; work in practice.
6485 (setq found item))))
6486 ;; Did we find something? If not, issue error. If so,
6487 ;; set connection properties.
6488 (unless found
6489 (error "Couldn't find an inline transfer encoding"))
16674e4f
KG
6490 (let ((rem-enc (nth 0 found))
6491 (rem-dec (nth 1 found))
6492 (loc-enc (nth 2 found))
6493 (loc-dec (nth 3 found)))
6494 (tramp-message 10 "Using remote encoding %s" rem-enc)
6495 (tramp-set-remote-encoding multi-method method user host rem-enc)
6496 (tramp-message 10 "Using remote decoding %s" rem-dec)
6497 (tramp-set-remote-decoding multi-method method user host rem-dec)
6498 (tramp-message 10 "Using local encoding %s" loc-enc)
6499 (tramp-set-local-encoding multi-method method user host loc-enc)
6500 (tramp-message 10 "Using local decoding %s" loc-dec)
6501 (tramp-set-local-decoding multi-method method user host loc-dec))))
6502
6503(defun tramp-call-local-coding-command (cmd input output)
6504 "Call the local encoding or decoding command.
6505If CMD contains \"%s\", provide input file INPUT there in command.
6506Otherwise, INPUT is passed via standard input.
6507INPUT can also be nil which means `/dev/null'.
6508OUTPUT can be a string (which specifies a filename), or t (which
6509means standard output and thus the current buffer), or nil (which
6510means discard it)."
6511 (call-process
6512 tramp-encoding-shell ;program
6513 (when (and input (not (string-match "%s" cmd)))
6514 input) ;input
6515 (if (eq output t) t nil) ;output
6516 nil ;redisplay
6517 tramp-encoding-command-switch
6518 ;; actual shell command
6519 (concat
6520 (if (string-match "%s" cmd) (format cmd input) cmd)
6521 (if (stringp output) (concat "> " output) ""))))
fb7933a3
KG
6522
6523(defun tramp-maybe-open-connection (multi-method method user host)
6524 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6525Does not do anything if a connection is already open, but re-opens the
6526connection if a previous connection has died for some reason."
16674e4f
KG
6527 (let ((p (get-buffer-process
6528 (tramp-get-buffer multi-method method user host)))
ac474af1
KG
6529 last-cmd-time)
6530 ;; If too much time has passed since last command was sent, look
6531 ;; whether process is still alive. If it isn't, kill it. When
6532 ;; using ssh, it can sometimes happen that the remote end has hung
6533 ;; up but the local ssh client doesn't recognize this until it
6534 ;; tries to send some data to the remote end. So that's why we
6535 ;; try to send a command from time to time, then look again
6536 ;; whether the process is really alive.
6537 (save-excursion
6538 (set-buffer (tramp-get-buffer multi-method method user host))
6539 (when (and tramp-last-cmd-time
3b89d388
KG
6540 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6541 p (processp p) (memq (process-status p) '(run open)))
685f5858
KG
6542 (tramp-send-command
6543 multi-method method user host "echo are you awake" nil t)
df2a9b12
SS
6544 (unless (and (memq (process-status p) '(run open))
6545 (tramp-wait-for-output 10))
ac474af1
KG
6546 (delete-process p)
6547 (setq p nil))
6548 (erase-buffer)))
6549 (unless (and p (processp p) (memq (process-status p) '(run open)))
fb7933a3
KG
6550 (when (and p (processp p))
6551 (delete-process p))
5ec2cc41
KG
6552 (let ((process-connection-type tramp-process-connection-type))
6553 (funcall (tramp-get-method-parameter
6554 multi-method
6555 (tramp-find-method multi-method method user host)
6556 user host 'tramp-connection-function)
6557 multi-method method user host)))))
fb7933a3
KG
6558
6559(defun tramp-send-command
685f5858 6560 (multi-method method user host command &optional noerase neveropen)
fb7933a3
KG
6561 "Send the COMMAND to USER at HOST (logged in using METHOD).
6562Erases temporary buffer before sending the command (unless NOERASE
685f5858
KG
6563is true).
6564If optional seventh arg NEVEROPEN is non-nil, never try to open the
6565connection. This is meant to be used from
6566`tramp-maybe-open-connection' only."
6567 (or neveropen
6568 (tramp-maybe-open-connection multi-method method user host))
ac474af1 6569 (setq tramp-last-cmd-time (current-time))
38c65fca 6570 (setq tramp-last-cmd command)
fb7933a3
KG
6571 (when tramp-debug-buffer
6572 (save-excursion
6573 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6574 (goto-char (point-max))
6575 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6576 (let ((proc nil))
6577 (set-buffer (tramp-get-buffer multi-method method user host))
6578 (unless noerase (erase-buffer))
6579 (setq proc (get-buffer-process (current-buffer)))
6580 (process-send-string proc
6581 (concat command tramp-rsh-end-of-line))))
6582
b25a52cc
KG
6583(defun tramp-send-command-internal
6584 (multi-method method user host command &optional msg)
6585 "Send command to remote host and wait for success.
6586Sends COMMAND, then waits 30 seconds for shell prompt."
6587 (tramp-send-command multi-method method user host command t t)
6588 (when msg
6589 (tramp-message 9 "Waiting 30s for %s..." msg))
6590 (tramp-barf-if-no-shell-prompt
6591 nil 30
6592 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
bf247b6e 6593
fb7933a3
KG
6594(defun tramp-wait-for-output (&optional timeout)
6595 "Wait for output from remote rsh command."
6596 (let ((proc (get-buffer-process (current-buffer)))
6597 (found nil)
6598 (start-time (current-time))
38c65fca 6599 (start-point (point))
fb7933a3
KG
6600 (end-of-output (concat "^"
6601 (regexp-quote tramp-end-of-output)
16674e4f 6602 "\r?$")))
fb7933a3
KG
6603 ;; Algorithm: get waiting output. See if last line contains
6604 ;; end-of-output sentinel. If not, wait a bit and again get
6605 ;; waiting output. Repeat until timeout expires or end-of-output
6606 ;; sentinel is seen. Will hang if timeout is nil and
6607 ;; end-of-output sentinel never appears.
6608 (save-match-data
6609 (cond (timeout
6610 ;; Work around an XEmacs bug, where the timeout expires
6611 ;; faster than it should. This degenerates into polling
6612 ;; for buggy XEmacsen, but oh, well.
6613 (while (and (not found)
6614 (< (tramp-time-diff (current-time) start-time)
6615 timeout))
6616 (with-timeout (timeout)
6617 (while (not found)
d2a2c17f 6618 (tramp-accept-process-output proc 1)
19a87064
MA
6619 (unless (memq (process-status proc) '(run open))
6620 (error "Process has died"))
fb7933a3
KG
6621 (goto-char (point-max))
6622 (forward-line -1)
6623 (setq found (looking-at end-of-output))))))
6624 (t
6625 (while (not found)
d2a2c17f 6626 (tramp-accept-process-output proc 1)
19a87064
MA
6627 (unless (memq (process-status proc) '(run open))
6628 (error "Process has died"))
fb7933a3
KG
6629 (goto-char (point-max))
6630 (forward-line -1)
6631 (setq found (looking-at end-of-output))))))
6632 ;; At this point, either the timeout has expired or we have found
6633 ;; the end-of-output sentinel.
6634 (when found
6635 (goto-char (point-max))
6636 (forward-line -2)
6637 (delete-region (point) (point-max)))
38c65fca
KG
6638 ;; If processing echoes, look for it in the first line and delete.
6639 (when tramp-process-echoes
6640 (save-excursion
6641 (goto-char start-point)
6642 (when (looking-at (regexp-quote tramp-last-cmd))
01917a18 6643 (delete-region (point) (progn (forward-line 1) (point))))))
fb7933a3
KG
6644 ;; Add output to debug buffer if appropriate.
6645 (when tramp-debug-buffer
6646 (append-to-buffer
6647 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6648 tramp-current-user tramp-current-host)
6649 (point-min) (point-max))
6650 (when (not found)
6651 (save-excursion
6652 (set-buffer
6653 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6654 tramp-current-user tramp-current-host))
6655 (goto-char (point-max))
6656 (insert "[[Remote prompt `" end-of-output "' not found"
487fa986 6657 (if timeout (format " in %d secs" timeout) "")
fb7933a3
KG
6658 "]]"))))
6659 (goto-char (point-min))
6660 ;; Return value is whether end-of-output sentinel was found.
6661 found))
6662
fb7933a3
KG
6663(defun tramp-send-command-and-check (multi-method method user host command
6664 &optional subshell)
6665 "Run COMMAND and check its exit status.
6666MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6667Sends `echo $?' along with the COMMAND for checking the exit status. If
6668COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6669
6670If the optional argument SUBSHELL is non-nil, the command is executed in
6671a subshell, ie surrounded by parentheses."
6672 (tramp-send-command multi-method method user host
6673 (concat (if subshell "( " "")
6674 command
6675 (if command " 2>/dev/null; " "")
6676 "echo tramp_exit_status $?"
6677 (if subshell " )" " ")))
6678 (tramp-wait-for-output)
6679 (goto-char (point-max))
6680 (unless (search-backward "tramp_exit_status " nil t)
6681 (error "Couldn't find exit status of `%s'" command))
6682 (skip-chars-forward "^ ")
6683 (read (current-buffer)))
6684
6685(defun tramp-barf-unless-okay (multi-method method user host command subshell
6686 signal fmt &rest args)
6687 "Run COMMAND, check exit status, throw error if exit status not okay.
6688Similar to `tramp-send-command-and-check' but accepts two more arguments
6689FMT and ARGS which are passed to `error'."
6690 (unless (zerop (tramp-send-command-and-check
6691 multi-method method user host command subshell))
6692 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6693 ;; silent.
6694 (pop-to-buffer (current-buffer))
6695 (funcall 'signal signal (apply 'format fmt args))))
6696
7432277c
KG
6697;; It seems that Tru64 Unix does not like it if long strings are sent
6698;; to it in one go. (This happens when sending the Perl
6699;; `file-attributes' implementation, for instance.) Therefore, we
6700;; have this function which waits a bit at each line.
6701(defun tramp-send-string
6702 (multi-method method user host string)
6703 "Send the STRING to USER at HOST using METHOD.
6704
6705The STRING is expected to use Unix line-endings, but the lines sent to
6706the remote host use line-endings as defined in the variable
6707`tramp-rsh-end-of-line'."
fb7933a3
KG
6708 (let ((proc (get-buffer-process
6709 (tramp-get-buffer multi-method method user host))))
6710 (unless proc
7432277c
KG
6711 (error "Can't send string to remote host -- not logged in"))
6712 ;; debug message
6713 (when tramp-debug-buffer
6714 (save-excursion
6715 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6716 (goto-char (point-max))
6717 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6718 ;; replace "\n" by `tramp-rsh-end-of-line'
6719 (setq string
6720 (mapconcat 'identity
6721 (split-string string "\n")
6722 tramp-rsh-end-of-line))
49914e04
AS
6723 (unless (or (string= string "")
6724 (string-equal (substring string -1) tramp-rsh-end-of-line))
7432277c
KG
6725 (setq string (concat string tramp-rsh-end-of-line)))
6726 ;; send the string
8daea7fc 6727 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
7432277c
KG
6728 (let ((pos 0)
6729 (end (length string)))
16674e4f
KG
6730 (while (< pos end)
6731 (tramp-message-for-buffer
6732 multi-method method user host 10
7432277c
KG
6733 "Sending chunk from %s to %s"
6734 pos (min (+ pos tramp-chunksize) end))
6735 (process-send-string
6736 proc (substring string pos (min (+ pos tramp-chunksize) end)))
16674e4f
KG
6737 (setq pos (+ pos tramp-chunksize))
6738 (sleep-for 0.1)))
7432277c 6739 (process-send-string proc string))))
fb7933a3
KG
6740
6741(defun tramp-send-eof (multi-method method user host)
6742 "Send EOF to the remote end.
25f78d18 6743METHOD, HOST and USER specify the connection."
fb7933a3
KG
6744 (let ((proc (get-buffer-process
6745 (tramp-get-buffer multi-method method user host))))
6746 (unless proc
6747 (error "Can't send EOF to remote host -- not logged in"))
6748 (process-send-eof proc)))
6749; (process-send-string proc "\^D")))
6750
6751(defun tramp-kill-process (multi-method method user host)
6752 "Kill the connection process used by Tramp.
25f78d18 6753MULTI-METHOD, METHOD, USER, and HOST specify the connection."
fb7933a3
KG
6754 (let ((proc (get-buffer-process
6755 (tramp-get-buffer multi-method method user host))))
6756 (kill-process proc)))
6757
6758(defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6759 "Erase buffer, then discard subsequent garbage.
6760If `tramp-discard-garbage' is nil, just erase buffer."
6761 (if (not tramp-discard-garbage)
6762 (erase-buffer)
d2a2c17f 6763 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
fb7933a3
KG
6764 (when tramp-debug-buffer
6765 (save-excursion
6766 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6767 (goto-char (point-max))
6768 (tramp-insert-with-face
6769 'bold (format "Additional characters detected\n")))))))
6770
6771(defun tramp-mode-string-to-int (mode-string)
6772 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6773 (let* ((mode-chars (string-to-vector mode-string))
6774 (owner-read (aref mode-chars 1))
6775 (owner-write (aref mode-chars 2))
6776 (owner-execute-or-setid (aref mode-chars 3))
6777 (group-read (aref mode-chars 4))
6778 (group-write (aref mode-chars 5))
6779 (group-execute-or-setid (aref mode-chars 6))
6780 (other-read (aref mode-chars 7))
6781 (other-write (aref mode-chars 8))
6782 (other-execute-or-sticky (aref mode-chars 9)))
6783 (save-match-data
6784 (logior
6785 (case owner-read
6786 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6787 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6788 (case owner-write
6789 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6790 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6791 (case owner-execute-or-setid
6792 (?x (tramp-octal-to-decimal "00100"))
6793 (?S (tramp-octal-to-decimal "04000"))
6794 (?s (tramp-octal-to-decimal "04100"))
6795 (?- 0)
6796 (t (error "Fourth char `%c' must be one of `xsS-'"
6797 owner-execute-or-setid)))
6798 (case group-read
6799 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6800 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6801 (case group-write
6802 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6803 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6804 (case group-execute-or-setid
6805 (?x (tramp-octal-to-decimal "00010"))
6806 (?S (tramp-octal-to-decimal "02000"))
6807 (?s (tramp-octal-to-decimal "02010"))
6808 (?- 0)
6809 (t (error "Seventh char `%c' must be one of `xsS-'"
6810 group-execute-or-setid)))
6811 (case other-read
6812 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6813 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6814 (case other-write
6815 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6816 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6817 (case other-execute-or-sticky
6818 (?x (tramp-octal-to-decimal "00001"))
6819 (?T (tramp-octal-to-decimal "01000"))
6820 (?t (tramp-octal-to-decimal "01001"))
6821 (?- 0)
6822 (t (error "Tenth char `%c' must be one of `xtT-'"
6823 other-execute-or-sticky)))))))
6824
c82c5727
LH
6825(defun tramp-convert-file-attributes (multi-method method user host attr)
6826 "Convert file-attributes ATTR generated by perl script or ls.
6827Convert file mode bits to string and set virtual device number.
6828Return ATTR."
ca637b2a 6829 ;; Convert file mode bits to string.
c82c5727 6830 (unless (stringp (nth 8 attr))
c82c5727 6831 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
ca637b2a
MA
6832 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6833 (when (numberp (nth 3 attr))
6834 (setcar (nthcdr 9 attr)
1a762140
MA
6835 (not (eql (nth 3 attr)
6836 (tramp-get-remote-gid multi-method method user host)))))
c82c5727
LH
6837 ;; Set virtual device number.
6838 (setcar (nthcdr 11 attr)
6839 (tramp-get-device multi-method method user host))
6840 attr)
6841
6842(defun tramp-get-device (multi-method method user host)
6843 "Returns the virtual device number.
6844If it doesn't exist, generate a new one."
6845 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6846 (unless (assoc string tramp-devices)
6847 (add-to-list 'tramp-devices
6848 (list string (length tramp-devices))))
6849 (list -1 (nth 1 (assoc string tramp-devices)))))
fb7933a3
KG
6850
6851(defun tramp-file-mode-from-int (mode)
6852 "Turn an integer representing a file mode into an ls(1)-like string."
6853 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6854 (user (logand (lsh mode -6) 7))
6855 (group (logand (lsh mode -3) 7))
6856 (other (logand (lsh mode -0) 7))
6857 (suid (> (logand (lsh mode -9) 4) 0))
6858 (sgid (> (logand (lsh mode -9) 2) 0))
6859 (sticky (> (logand (lsh mode -9) 1) 0)))
6860 (setq user (tramp-file-mode-permissions user suid "s"))
6861 (setq group (tramp-file-mode-permissions group sgid "s"))
6862 (setq other (tramp-file-mode-permissions other sticky "t"))
6863 (concat type user group other)))
6864
6865
6866(defun tramp-file-mode-permissions (perm suid suid-text)
6867 "Convert a permission bitset into a string.
6868This is used internally by `tramp-file-mode-from-int'."
6869 (let ((r (> (logand perm 4) 0))
6870 (w (> (logand perm 2) 0))
6871 (x (> (logand perm 1) 0)))
6872 (concat (or (and r "r") "-")
6873 (or (and w "w") "-")
6874 (or (and suid x suid-text) ; suid, execute
6875 (and suid (upcase suid-text)) ; suid, !execute
6876 (and x "x") "-")))) ; !suid
6877
6878
6879(defun tramp-decimal-to-octal (i)
6880 "Return a string consisting of the octal digits of I.
6881Not actually used. Use `(format \"%o\" i)' instead?"
6882 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6883 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6884 ((zerop i) "0")
6885 (t (concat (tramp-decimal-to-octal (/ i 8))
6886 (number-to-string (% i 8))))))
6887
6888
6889;;(defun tramp-octal-to-decimal (ostr)
6890;; "Given a string of octal digits, return a decimal number."
6891;; (cond ((null ostr) 0)
6892;; ((string= "" ostr) 0)
6893;; (t (let ((last (aref ostr (1- (length ostr))))
6894;; (rest (substring ostr 0 (1- (length ostr)))))
6895;; (unless (and (>= last ?0)
6896;; (<= last ?7))
6897;; (error "Not an octal digit: %c" last))
6898;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6899;; Kudos to Gerd Moellmann for this suggestion.
6900(defun tramp-octal-to-decimal (ostr)
6901 "Given a string of octal digits, return a decimal number."
6902 (let ((x (or ostr "")))
6903 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6904 (unless (string-match "\\`[0-7]*\\'" x)
6905 (error "Non-octal junk in string `%s'" x))
6906 (string-to-number ostr 8)))
6907
6908(defun tramp-shell-case-fold (string)
6909 "Converts STRING to shell glob pattern which ignores case."
6910 (mapconcat
6911 (lambda (c)
6912 (if (equal (downcase c) (upcase c))
6913 (vector c)
6914 (format "[%c%c]" (downcase c) (upcase c))))
6915 string
6916 ""))
6917
6918
bf247b6e
KS
6919;; ------------------------------------------------------------
6920;; -- TRAMP file names --
6921;; ------------------------------------------------------------
fb7933a3
KG
6922;; Conversion functions between external representation and
6923;; internal data structure. Convenience functions for internal
6924;; data structure.
6925
7432277c 6926(defstruct tramp-file-name multi-method method user host localname)
fb7933a3
KG
6927
6928(defun tramp-tramp-file-p (name)
6929 "Return t iff NAME is a tramp file."
6930 (save-match-data
6931 (string-match tramp-file-name-regexp name)))
bf247b6e 6932
fb7933a3
KG
6933;; HHH: Changed. Used to assign the return value of (user-login-name)
6934;; to the `user' part of the structure if a user name was not
6935;; provided, now it assigns nil.
6936(defun tramp-dissect-file-name (name)
6937 "Return an `tramp-file-name' structure.
6938The structure consists of remote method, remote user, remote host and
7432277c 6939localname (file name on remote host)."
4007ba5b
KG
6940 (save-match-data
6941 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6942 (method
6943 ; single-hop
6944 (if match (match-string (nth 1 tramp-file-name-structure) name)
6945 ; maybe multi-hop
6946 (string-match
6947 (format (nth 0 tramp-multi-file-name-structure)
6948 (nth 0 tramp-multi-file-name-hop-structure)) name)
6949 (match-string (nth 1 tramp-multi-file-name-structure) name))))
c62c9d08 6950 (if (and method (member method tramp-multi-methods))
fb7933a3
KG
6951 ;; If it's a multi method, the file name structure contains
6952 ;; arrays of method, user and host.
6953 (tramp-dissect-multi-file-name name)
c62c9d08 6954 ;; Normal method. First, find out default method.
4007ba5b 6955 (unless match (error "Not a tramp file name: %s" name))
c62c9d08
KG
6956 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6957 (host (match-string (nth 3 tramp-file-name-structure) name))
7432277c 6958 (localname (match-string (nth 4 tramp-file-name-structure) name)))
c62c9d08
KG
6959 (make-tramp-file-name
6960 :multi-method nil
6961 :method method
6962 :user (or user nil)
6963 :host host
7432277c 6964 :localname localname))))))
c62c9d08
KG
6965
6966(defun tramp-find-default-method (user host)
6967 "Look up the right method to use in `tramp-default-method-alist'."
6968 (let ((choices tramp-default-method-alist)
6969 (method tramp-default-method)
6970 item)
6971 (while choices
6972 (setq item (pop choices))
e1e17cae
MA
6973 (when (and (string-match (or (nth 0 item) "") (or host ""))
6974 (string-match (or (nth 1 item) "") (or user "")))
c62c9d08
KG
6975 (setq method (nth 2 item))
6976 (setq choices nil)))
6977 method))
16674e4f
KG
6978
6979(defun tramp-find-method (multi-method method user host)
6980 "Return the right method string to use.
6981This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6982If both MULTI-METHOD and METHOD are nil, do a lookup in
6983`tramp-default-method-alist'."
6984 (or multi-method method (tramp-find-default-method user host)))
bf247b6e 6985
fb7933a3
KG
6986;; HHH: Not Changed. Multi method. Will probably not handle the case where
6987;; a user name is not provided in the "file name" very well.
6988(defun tramp-dissect-multi-file-name (name)
6989 "Not implemented yet."
6990 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6991 (method-index (nth 1 tramp-multi-file-name-structure))
6992 (hops-index (nth 2 tramp-multi-file-name-structure))
7432277c 6993 (localname-index (nth 3 tramp-multi-file-name-structure))
fb7933a3
KG
6994 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6995 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6996 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6997 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
7432277c 6998 method hops len hop-methods hop-users hop-hosts localname)
fb7933a3
KG
6999 (unless (string-match (format regexp hop-regexp) name)
7000 (error "Not a multi tramp file name: %s" name))
7001 (setq method (match-string method-index name))
7002 (setq hops (match-string hops-index name))
7003 (setq len (/ (length (match-data t)) 2))
7432277c
KG
7004 (when (< localname-index 0) (incf localname-index len))
7005 (setq localname (match-string localname-index name))
fb7933a3
KG
7006 (let ((index 0))
7007 (while (string-match hop-regexp hops index)
7008 (setq index (match-end 0))
7009 (setq hop-methods
7010 (cons (match-string hop-method-index hops) hop-methods))
7011 (setq hop-users
7012 (cons (match-string hop-user-index hops) hop-users))
7013 (setq hop-hosts
7014 (cons (match-string hop-host-index hops) hop-hosts))))
7015 (make-tramp-file-name
7016 :multi-method method
7017 :method (apply 'vector (reverse hop-methods))
7018 :user (apply 'vector (reverse hop-users))
7019 :host (apply 'vector (reverse hop-hosts))
7432277c 7020 :localname localname)))
fb7933a3 7021
7432277c
KG
7022(defun tramp-make-tramp-file-name (multi-method method user host localname)
7023 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
fb7933a3 7024 (if multi-method
7432277c 7025 (tramp-make-tramp-multi-file-name multi-method method user host localname)
16674e4f
KG
7026 (format-spec
7027 (concat tramp-prefix-format
7028 (when method (concat "%m" tramp-postfix-single-method-format))
7029 (when user (concat "%u" tramp-postfix-user-format))
7030 (when host (concat "%h" tramp-postfix-host-format))
7432277c
KG
7031 (when localname (concat "%p")))
7032 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
fb7933a3
KG
7033
7034;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
7035;; with this when USER is nil?
7432277c 7036(defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
fb7933a3
KG
7037 "Constructs a tramp file name for a multi-hop method."
7038 (unless tramp-make-multi-tramp-file-format
7039 (error "`tramp-make-multi-tramp-file-format' is nil"))
7040 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
7041 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
7432277c 7042 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
ac474af1 7043 (prefix (format-spec prefix-format `((?m . ,multi-method))))
fb7933a3 7044 (hops "")
7432277c 7045 (localname (format-spec localname-format `((?p . ,localname))))
fb7933a3
KG
7046 (i 0)
7047 (len (length method)))
7048 (while (< i len)
90dc758d
KG
7049 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
7050 (setq hops (concat hops (format-spec hop-format
ac474af1 7051 `((?m . ,m) (?u . ,u) (?h . ,h)))))
fb7933a3 7052 (incf i)))
7432277c 7053 (concat prefix hops localname)))
fb7933a3 7054
b25a52cc
KG
7055(defun tramp-make-copy-program-file-name (user host localname)
7056 "Create a file name suitable to be passed to `rcp' and workalikes."
fb7933a3 7057 (if user
7432277c
KG
7058 (format "%s@%s:%s" user host localname)
7059 (format "%s:%s" host localname)))
fb7933a3 7060
16674e4f 7061(defun tramp-method-out-of-band-p (multi-method method user host)
38c65fca 7062 "Return t if this is an out-of-band method, nil otherwise."
c951aecb 7063 (tramp-get-method-parameter
16674e4f 7064 multi-method
91879624 7065 (tramp-find-method multi-method method user host)
c951aecb 7066 user host 'tramp-copy-program))
fb7933a3
KG
7067
7068;; Variables local to connection.
7069
7070(defun tramp-get-ls-command (multi-method method user host)
7071 (save-excursion
7072 (tramp-maybe-open-connection multi-method method user host)
7073 (set-buffer (tramp-get-buffer multi-method method user host))
7074 tramp-ls-command))
7075
7076(defun tramp-get-test-groks-nt (multi-method method user host)
7077 (save-excursion
7078 (tramp-maybe-open-connection multi-method method user host)
7079 (set-buffer (tramp-get-buffer multi-method method user host))
7080 tramp-test-groks-nt))
7081
7082(defun tramp-get-file-exists-command (multi-method method user host)
7083 (save-excursion
7084 (tramp-maybe-open-connection multi-method method user host)
7085 (set-buffer (tramp-get-buffer multi-method method user host))
7086 tramp-file-exists-command))
7087
7088(defun tramp-get-remote-perl (multi-method method user host)
7089 (tramp-get-connection-property "perl" nil multi-method method user host))
7090
7091(defun tramp-get-remote-ln (multi-method method user host)
7092 (tramp-get-connection-property "ln" nil multi-method method user host))
7093
a69c01a0
MA
7094(defun tramp-get-remote-uid (multi-method method user host)
7095 (tramp-get-connection-property "uid" nil multi-method method user host))
7096
7097(defun tramp-get-remote-gid (multi-method method user host)
7098 (tramp-get-connection-property "gid" nil multi-method method user host))
7099
fb7933a3 7100;; Get a property of a TRAMP connection.
ac474af1
KG
7101(defun tramp-get-connection-property
7102 (property default multi-method method user host)
fb7933a3
KG
7103 "Get the named property for the connection.
7104If the value is not set for the connection, return `default'"
7105 (tramp-maybe-open-connection multi-method method user host)
7106 (with-current-buffer (tramp-get-buffer multi-method method user host)
7107 (let (error)
7108 (condition-case nil
7109 (symbol-value (intern (concat "tramp-connection-property-" property)))
a69c01a0 7110 (error default)))))
fb7933a3
KG
7111
7112;; Set a property of a TRAMP connection.
ac474af1
KG
7113(defun tramp-set-connection-property
7114 (property value multi-method method user host)
fb7933a3
KG
7115 "Set the named property of a TRAMP connection."
7116 (tramp-maybe-open-connection multi-method method user host)
7117 (with-current-buffer (tramp-get-buffer multi-method method user host)
7118 (set (make-local-variable
7119 (intern (concat "tramp-connection-property-" property)))
7120 value)))
7121
ac474af1 7122;; Some predefined connection properties.
16674e4f
KG
7123(defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7124 (tramp-set-connection-property "remote-encoding" rem-enc
ac474af1 7125 multi-method method user host))
16674e4f
KG
7126(defun tramp-get-remote-encoding (multi-method method user host)
7127 (tramp-get-connection-property "remote-encoding" nil
ac474af1 7128 multi-method method user host))
16674e4f
KG
7129
7130(defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7131 (tramp-set-connection-property "remote-decoding" rem-dec
ac474af1 7132 multi-method method user host))
16674e4f
KG
7133(defun tramp-get-remote-decoding (multi-method method user host)
7134 (tramp-get-connection-property "remote-decoding" nil
ac474af1 7135 multi-method method user host))
16674e4f
KG
7136
7137(defun tramp-set-local-encoding (multi-method method user host loc-enc)
7138 (tramp-set-connection-property "local-encoding" loc-enc
ac474af1 7139 multi-method method user host))
16674e4f
KG
7140(defun tramp-get-local-encoding (multi-method method user host)
7141 (tramp-get-connection-property "local-encoding" nil
ac474af1 7142 multi-method method user host))
16674e4f
KG
7143
7144(defun tramp-set-local-decoding (multi-method method user host loc-dec)
7145 (tramp-set-connection-property "local-decoding" loc-dec
ac474af1 7146 multi-method method user host))
16674e4f
KG
7147(defun tramp-get-local-decoding (multi-method method user host)
7148 (tramp-get-connection-property "local-decoding" nil
ac474af1 7149 multi-method method user host))
fb7933a3 7150
c951aecb
KG
7151(defun tramp-get-method-parameter (multi-method method user host param)
7152 "Return the method parameter PARAM.
7153If the `tramp-methods' entry does not exist, use the variable PARAM
7154as default."
7155 (unless (boundp param)
7156 (error "Non-existing method parameter `%s'" param))
7157 (let ((entry (assoc param
90f8dc03
KG
7158 (assoc (tramp-find-method multi-method method user host)
7159 tramp-methods))))
c951aecb
KG
7160 (if entry
7161 (second entry)
7162 (symbol-value param))))
bf247b6e 7163
90f8dc03 7164
fb7933a3
KG
7165;; Auto saving to a special directory.
7166
00cec167
MA
7167(defun tramp-exists-file-name-handler (operation &rest args)
7168 (let ((buffer-file-name "/")
7169 (fnha file-name-handler-alist)
7170 (check-file-name-operation operation)
7171 (file-name-handler-alist
7172 (list
7173 (cons "/"
7174 '(lambda (operation &rest args)
7175 "Returns OPERATION if it is the one to be checked"
7176 (if (equal check-file-name-operation operation)
7177 operation
7178 (let ((file-name-handler-alist fnha))
7179 (apply operation args))))))))
7180 (eq (apply operation args) operation)))
c1105d05
MA
7181
7182(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7183 (defadvice make-auto-save-file-name
7184 (around tramp-advice-make-auto-save-file-name () activate)
7185 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7186 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
00cec167 7187 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
a69c01a0
MA
7188 ad-do-it))
7189 (add-hook 'tramp-unload-hook
7190 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
fb7933a3 7191
340b8d4f
MA
7192;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7193;; permission 0666 minus umask. This is a security threat.
414da5ab
MA
7194
7195(defun tramp-set-auto-save-file-modes ()
7196 "Set permissions of autosaved remote files to the original permissions."
7197 (let ((bfn (buffer-file-name)))
7198 (when (and (stringp bfn)
7199 (tramp-tramp-file-p bfn)
7200 (stringp buffer-auto-save-file-name)
340b8d4f
MA
7201 (not (equal bfn buffer-auto-save-file-name)))
7202 (unless (file-exists-p buffer-auto-save-file-name)
7203 (write-region "" nil buffer-auto-save-file-name))
7204 ;; Permissions should be set always, because there might be an old
7205 ;; auto-saved file belonging to another original file. This could
7206 ;; be a security threat.
7177e2a3 7207 (set-file-modes buffer-auto-save-file-name
11948172 7208 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
414da5ab
MA
7209
7210(unless (or (> emacs-major-version 21)
7211 (and (featurep 'xemacs)
7212 (= emacs-major-version 21)
340b8d4f 7213 (> emacs-minor-version 4)))
a69c01a0
MA
7214 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7215 (add-hook 'tramp-unload-hook
7216 '(lambda ()
7217 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
414da5ab 7218
fb7933a3
KG
7219(defun tramp-subst-strs-in-string (alist string)
7220 "Replace all occurrences of the string FROM with TO in STRING.
7221ALIST is of the form ((FROM . TO) ...)."
7222 (save-match-data
7223 (while alist
7224 (let* ((pr (car alist))
7225 (from (car pr))
7226 (to (cdr pr)))
7227 (while (string-match (regexp-quote from) string)
7228 (setq string (replace-match to t t string)))
7229 (setq alist (cdr alist))))
7230 string))
7231
7232(defun tramp-insert-with-face (face string)
7233 "Insert text with a specific face."
7234 (let ((start (point)))
7235 (insert string)
7236 (add-text-properties start (point) (list 'face face))))
7237
7238;; ------------------------------------------------------------
7239;; -- Compatibility functions section --
7240;; ------------------------------------------------------------
7241
7242(defun tramp-temporary-file-directory ()
7243 "Return name of directory for temporary files (compat function).
7244For Emacs, this is the variable `temporary-file-directory', for XEmacs
7245this is the function `temp-directory'."
7246 (cond ((boundp 'temporary-file-directory)
7247 (symbol-value 'temporary-file-directory))
7248 ((fboundp 'temp-directory)
7249 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7250 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7251 (file-name-as-directory (getenv "TEMP")))
7252 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7253 (file-name-as-directory (getenv "TMP")))
7254 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7255 (file-name-as-directory (getenv "TMPDIR")))
7256 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7257 (t (message (concat "Neither `temporary-file-directory' nor "
7258 "`temp-directory' is defined -- using /tmp."))
7259 (file-name-as-directory "/tmp"))))
7260
07dfe738 7261(defun tramp-read-passwd (user host prompt)
fb7933a3 7262 "Read a password from user (compat function).
5ec2cc41
KG
7263Invokes `password-read' if available, `read-passwd' else."
7264 (if (functionp 'password-read)
07dfe738 7265 (let* ((key (concat (or user (user-login-name)) "@" host))
5ec2cc41
KG
7266 (password (apply #'password-read (list prompt key))))
7267 (apply #'password-cache-add (list key password))
7268 password)
7269 (read-passwd prompt)))
7270
7271(defun tramp-clear-passwd (&optional user host)
7272 "Clear password cache for connection related to current-buffer."
7273 (interactive)
7274 (let ((filename (or buffer-file-name list-buffers-directory "")))
7275 (when (and (functionp 'password-cache-remove)
7276 (or (and user host) (tramp-tramp-file-p filename)))
7277 (let* ((v (when (tramp-tramp-file-p filename)
7278 (tramp-dissect-file-name filename)))
7279 (luser (or user (tramp-file-name-user v) (user-login-name)))
7280 (lhost (or host (tramp-file-name-host v) (system-name)))
7281 (key (concat luser "@" lhost)))
7282 (apply #'password-cache-remove (list key))))))
fb7933a3
KG
7283
7284(defun tramp-time-diff (t1 t2)
7285 "Return the difference between the two times, in seconds.
1a762140 7286T1 and T2 are time values (as returned by `current-time' for example)."
fb7933a3 7287 ;; Pacify byte-compiler with `symbol-function'.
ea9d1443
KG
7288 (cond ((and (fboundp 'subtract-time)
7289 (fboundp 'float-time))
7290 (funcall (symbol-function 'float-time)
7291 (funcall (symbol-function 'subtract-time) t1 t2)))
7292 ((and (fboundp 'subtract-time)
7293 (fboundp 'time-to-seconds))
7294 (funcall (symbol-function 'time-to-seconds)
7295 (funcall (symbol-function 'subtract-time) t1 t2)))
fb7933a3 7296 ((fboundp 'itimer-time-difference)
1a762140
MA
7297 (funcall (symbol-function 'itimer-time-difference)
7298 (if (< (length t1) 3) (append t1 '(0)) t1)
7299 (if (< (length t2) 3) (append t2 '(0)) t2)))
fb7933a3 7300 (t
ea9d1443
KG
7301 ;; snarfed from Emacs 21 time-date.el; combining
7302 ;; time-to-seconds and subtract-time
7303 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
fb7933a3 7304 (list (- (car t1) (car t2) (if borrow 1 0))
ea9d1443
KG
7305 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7306 (+ (* (car time) 65536.0)
7307 (cadr time)
7308 (/ (or (nth 2 time) 0) 1000000.0))))))
fb7933a3
KG
7309
7310(defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7311 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7312EOL-TYPE can be one of `dos', `unix', or `mac'."
7313 (cond ((fboundp 'coding-system-change-eol-conversion)
7314 (apply #'coding-system-change-eol-conversion
7315 (list coding-system eol-type)))
7316 ((fboundp 'subsidiary-coding-system)
7317 (apply
7318 #'subsidiary-coding-system
7319 (list coding-system
7320 (cond ((eq eol-type 'dos) 'crlf)
7321 ((eq eol-type 'unix) 'lf)
7322 ((eq eol-type 'mac) 'cr)
7323 (t
7324 (error "Unknown EOL-TYPE `%s', must be %s"
7325 eol-type
7326 "`dos', `unix', or `mac'"))))))
7327 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7328
7329(defun tramp-split-string (string pattern)
7330 "Like `split-string' but omit empty strings.
7331In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7332This is, the first, empty, element is omitted. In XEmacs, the first
7333element is not omitted.
7334
7335Note: this function has been written for `tramp-handle-file-truename'.
7336If you want to use it for something else, you'll have to check whether
7337it does the right thing."
7338 (delete "" (split-string string pattern)))
7339
19a87064
MA
7340(defun tramp-set-process-query-on-exit-flag (process flag)
7341 "Specify if query is needed for process when Emacs is exited.
7342If the second argument flag is non-nil, Emacs will query the user before
7343exiting if process is running."
d2a2c17f 7344 (funcall
19a87064 7345 (if (fboundp 'set-process-query-on-exit-flag)
d2a2c17f
MA
7346 (symbol-function 'set-process-query-on-exit-flag)
7347 (symbol-function 'process-kill-without-query))
7348 process flag))
19a87064 7349
19a87064 7350
bf247b6e
KS
7351;; ------------------------------------------------------------
7352;; -- Kludges section --
7353;; ------------------------------------------------------------
fb7933a3
KG
7354
7355;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7356;; does not deal well with newline characters. Newline is replaced by
7357;; backslash newline. But if, say, the string `a backslash newline b'
7358;; is passed to a shell, the shell will expand this into "ab",
7359;; completely omitting the newline. This is not what was intended.
7360;; It does not appear to be possible to make the function
7361;; `shell-quote-argument' work with newlines without making it
7362;; dependent on the shell used. But within this package, we know that
7363;; we will always use a Bourne-like shell, so we use an approach which
7364;; groks newlines.
7365;;
7366;; The approach is simple: we call `shell-quote-argument', then
7367;; massage the newline part of the result.
7368;;
7369;; This function should produce a string which is grokked by a Unix
7370;; shell, even if the Emacs is running on Windows. Since this is the
7371;; kludges section, we bind `system-type' in such a way that
7372;; `shell-quote-arguments' behaves as if on Unix.
7373;;
7374;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7375;; function to work with Bourne-like shells.
7376;;
7377;; CCC: This function should be rewritten so that
7378;; `shell-quote-argument' is not used. This way, we are safe from
7379;; changes in `shell-quote-argument'.
7380(defun tramp-shell-quote-argument (s)
7381 "Similar to `shell-quote-argument', but groks newlines.
7382Only works for Bourne-like shells."
7383 (let ((system-type 'not-windows))
7384 (save-match-data
7385 (let ((result (shell-quote-argument s))
7386 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7387 (when (and (>= (length result) 2)
7388 (string= (substring result 0 2) "\\~"))
7389 (setq result (substring result 1)))
7390 (while (string-match nl result)
7391 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7392 t t result)))
7393 result))))
7394
7395;; ;; EFS hooks itself into the file name handling stuff in more places
7396;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7432277c 7397;; ;; away from tramp.el file names.
fb7933a3
KG
7398;; ;;
7399;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7400;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7401;; ;; connection to help it's dired process. Not that I have any real
7402;; ;; idea *why* this is helpful to dired.
7403;; ;;
7404;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7405;; ;;
7406;; ;; Daniel Pittman <daniel@danann.net>
7407;; ;;
7408;; ;; CCC: when the other defadvice calls have disappeared, make sure
7409;; ;; not to call defadvice unless it's necessary. How do we find out whether
7410;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7411;; ;; EFS might nicht be loaded yet.
7432277c
KG
7412;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7413;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
fb7933a3
KG
7414;; (if (tramp-tramp-file-p (ad-get-arg 0))
7415;; nil
7416;; ad-do-it))
7417
16674e4f
KG
7418;; We currently (sometimes) use "[" and "]" in the filename format.
7419;; This means that Emacs wants to expand wildcards if
fb7933a3
KG
7420;; `find-file-wildcards' is non-nil, and then barfs because no
7421;; expansion could be found. We detect this situation and do
7422;; something really awful: we have `file-expand-wildcards' return the
7423;; original filename if it can't expand anything. Let's just hope
7424;; that this doesn't break anything else.
16674e4f
KG
7425;; CCC: This check is now also really awful; we should search all
7426;; of the filename format, not just the prefix.
7427(when (string-match "\\[" tramp-prefix-format)
d2a2c17f
MA
7428 (defadvice file-expand-wildcards (around tramp-fix activate)
7429 (let ((name (ad-get-arg 0)))
7430 (if (tramp-tramp-file-p name)
7431 ;; If it's a Tramp file, dissect it and look if wildcards
7432 ;; need to be expanded at all.
7433 (let ((v (tramp-dissect-file-name name)))
7434 (if (string-match "[[*?]" (tramp-file-name-localname v))
7435 (let ((res ad-do-it))
7436 (setq ad-return-value (or res (list name))))
7437 (setq ad-return-value (list name))))
7438 ;; If it is not a Tramp file, just run the original function.
7439 (let ((res ad-do-it))
a69c01a0
MA
7440 (setq ad-return-value (or res (list name)))))))
7441 (add-hook 'tramp-unload-hook
7442 '(lambda () (ad-unadvise 'file-expand-wildcards))))
fb7933a3 7443
ac474af1
KG
7444;; Tramp version is useful in a number of situations.
7445
7446(defun tramp-version (arg)
7447 "Print version number of tramp.el in minibuffer or current buffer."
7448 (interactive "P")
7449 (if arg (insert tramp-version) (message tramp-version)))
7450
fb7933a3
KG
7451;; Make the `reporter` functionality available for making bug reports about
7452;; the package. A most useful piece of code.
7453
7454(unless (fboundp 'reporter-submit-bug-report)
7455 (autoload 'reporter-submit-bug-report "reporter"))
7456
7457(defun tramp-bug ()
7458 "Submit a bug report to the TRAMP developers."
7459 (interactive)
7460 (require 'reporter)
340b8d4f 7461 (catch 'dont-send
d2a2c17f 7462 (let ((reporter-prompt-for-summary-p t))
340b8d4f
MA
7463 (reporter-submit-bug-report
7464 tramp-bug-report-address ; to-address
7465 (format "tramp (%s)" tramp-version) ; package name and version
d2a2c17f
MA
7466 (delq nil
7467 `(;; Current state
7468 tramp-ls-command
7469 tramp-test-groks-nt
7470 tramp-file-exists-command
7471 tramp-current-multi-method
7472 tramp-current-method
7473 tramp-current-user
7474 tramp-current-host
7475
7476 ;; System defaults
7477 tramp-auto-save-directory ; vars to dump
7478 tramp-default-method
7479 tramp-rsh-end-of-line
7480 tramp-default-password-end-of-line
7481 tramp-remote-path
7482 tramp-login-prompt-regexp
7483 ;; Mask non-7bit characters
7484 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7485 tramp-wrong-passwd-regexp
7486 tramp-yesno-prompt-regexp
7487 tramp-yn-prompt-regexp
7488 tramp-terminal-prompt-regexp
7489 tramp-temp-name-prefix
7490 tramp-file-name-structure
7491 tramp-file-name-regexp
7492 tramp-multi-file-name-structure
7493 tramp-multi-file-name-hop-structure
7494 tramp-multi-methods
7495 tramp-multi-connection-function-alist
7496 tramp-methods
7497 tramp-end-of-output
7498 tramp-coding-commands
7499 tramp-actions-before-shell
7500 tramp-actions-copy-out-of-band
7501 tramp-multi-actions
7502 tramp-terminal-type
7503 ;; Mask non-7bit characters
7504 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7505 tramp-chunksize
7506 ,(when (boundp 'tramp-backup-directory-alist)
7507 'tramp-backup-directory-alist)
7508 ,(when (boundp 'tramp-bkup-backup-directory-info)
7509 'tramp-bkup-backup-directory-info)
7510
7511 ;; Non-tramp variables of interest
7512 ;; Mask non-7bit characters
7513 (shell-prompt-pattern . tramp-reporter-dump-variable)
7514 backup-by-copying
7515 backup-by-copying-when-linked
7516 backup-by-copying-when-mismatch
7517 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7518 'backup-by-copying-when-privileged-mismatch)
7519 ,(when (boundp 'password-cache)
7520 'password-cache)
7521 ,(when (boundp 'password-cache-expiry)
7522 'password-cache-expiry)
7523 ,(when (boundp 'backup-directory-alist)
7524 'backup-directory-alist)
7525 ,(when (boundp 'bkup-backup-directory-info)
7526 'bkup-backup-directory-info)
7527 file-name-handler-alist))
7528
7529 'tramp-load-report-modules ; pre-hook
340b8d4f
MA
7530 'tramp-append-tramp-buffers ; post-hook
7531 "\
fb7933a3
KG
7532Enter your bug report in this message, including as much detail as you
7533possibly can about the problem, what you did to cause it and what the
7534local and remote machines are.
7535
7536If you can give a simple set of instructions to make this bug happen
7537reliably, please include those. Thank you for helping kill bugs in
7538TRAMP.
89509ea0
KG
7539
7540Another useful thing to do is to put (setq tramp-debug-buffer t) in
7541the ~/.emacs file and to repeat the bug. Then, include the contents
7542of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7543report.
7544
fabf2143 7545--bug report follows this line--
340b8d4f
MA
7546"))))
7547
d2a2c17f
MA
7548(defun tramp-reporter-dump-variable (varsym mailbuf)
7549 "Pretty-print the value of the variable in symbol VARSYM.
7550Used for non-7bit chars in strings."
7551 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7552 (val (with-current-buffer reporter-eval-buffer
7553 (symbol-value varsym))))
7554
7555 ;; There are characters to be masked.
7556 (when (and (boundp 'mm-7bit-chars)
7557 (string-match
7558 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7559 (with-current-buffer reporter-eval-buffer
7560 (set varsym (concat "(base64-decode-string \""
7561 (base64-encode-string val)
7562 "\")"))))
7563
7564 ;; Dump variable.
7565 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7566
7567 ;; Remove string quotation.
7568 (forward-line -1)
7569 (when (looking-at
7570 (concat "\\(^.*\\)" "\"" ;; \1 "
7571 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7572 "\\(\".*\\)" "\\\\" ;; \3 \
7573 "\\(\")\\)" "\"$")) ;; \4 "
7574 (replace-match "\\1\\2\\3\\4")
7575 (beginning-of-line)
7576 (insert " ;; variable encoded due to non-printable characters\n"))
7577 (forward-line 1)
7578
7579 ;; Reset VARSYM to old value.
7580 (with-current-buffer reporter-eval-buffer
7581 (set varsym val))))
7582
7583(defun tramp-load-report-modules ()
7584 "Load needed modules for reporting."
340b8d4f 7585
7177e2a3 7586 ;; We load message.el and mml.el from Gnus.
340b8d4f 7587 (if (featurep 'xemacs)
7177e2a3
MA
7588 (progn
7589 (load "message" 'noerror)
7590 (load "mml" 'noerror))
7591 (require 'message nil 'noerror)
340b8d4f 7592 (require 'mml nil 'noerror))
7177e2a3 7593 (when (functionp 'message-mode)
d2a2c17f 7594 (funcall (symbol-function 'message-mode)))
7177e2a3 7595 (when (functionp 'mml-mode)
d2a2c17f
MA
7596 (funcall (symbol-function 'mml-mode) t)))
7597
7598(defun tramp-append-tramp-buffers ()
7599 "Append Tramp buffers into the bug report."
340b8d4f
MA
7600
7601 (when (and
7177e2a3
MA
7602 (eq major-mode 'message-mode)
7603 (boundp 'mml-mode)
7604 (symbol-value 'mml-mode))
7605
7606 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7607 (buffer-list
7608 (delq nil
7609 (mapcar '(lambda (b)
7610 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7611 (buffer-list))))
7612 (curbuf (current-buffer)))
340b8d4f
MA
7613
7614 ;; There is at least one Tramp buffer.
7615 (when buffer-list
7177e2a3 7616 (switch-to-buffer (list-buffers-noselect nil))
340b8d4f
MA
7617 (delete-other-windows)
7618 (setq buffer-read-only nil)
7177e2a3
MA
7619 (goto-char (point-min))
7620 (while (not (eobp))
7621 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7622 (forward-line 1)
7623 (forward-line 0)
7624 (let ((start (point)))
7625 (forward-line 1)
7626 (kill-region start (point)))))
340b8d4f
MA
7627 (insert "
7628The buffer(s) above will be appended to this message. If you don't want
7629to append a buffer because it contains sensible data, or because the buffer
7630is too large, you should delete the respective buffer. The buffer(s) will
7631contain user and host names. Passwords will never be included there.")
7632
7633 (when (and tramp-debug-buffer (> tramp-verbose 9))
7634 (insert "\n\n")
7635 (let ((start (point)))
7636 (insert "\
7637Please note that you have set `tramp-verbose' to a value greater than 9.
7638Therefore, the contents of files might be included in the debug buffer(s).")
7639 (add-text-properties start (point) (list 'face 'italic))))
7640
7641 (set-buffer-modified-p nil)
7642 (setq buffer-read-only t)
7643 (goto-char (point-min))
7644
7645 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7646 ;; OK, let's send. First we delete the buffer list.
7647 (progn
7648 (kill-buffer nil)
7649 (switch-to-buffer curbuf)
7650 (goto-char (point-max))
7651 (insert "\n\n")
7652 (dolist (buffer buffer-list)
d2a2c17f
MA
7653 (funcall (symbol-function 'mml-insert-empty-tag)
7654 'part 'type "text/plain" 'encoding "base64"
7655 'disposition "attachment" 'buffer (buffer-name buffer)
7656 'description (buffer-name buffer)))
340b8d4f
MA
7657 (set-buffer-modified-p nil))
7658
7659 ;; Don't send. Delete the message buffer.
7660 (set-buffer curbuf)
7661 (set-buffer-modified-p nil)
7662 (kill-buffer nil)
7663 (throw 'dont-send nil))))))
fb7933a3
KG
7664
7665(defalias 'tramp-submit-bug 'tramp-bug)
7666
a69c01a0
MA
7667;; Checklist for `tramp-unload-hook'
7668;; - Unload all `tramp-*' packages
7669;; - Reset `file-name-handler-alist'
7670;; - Cleanup hooks where Tramp functions are in
7671;; - Cleanup advised functions
7672;; - Cleanup autoloads
7673;;;###autoload
7674(defun tramp-unload-tramp ()
08b1eb21 7675 "Discard Tramp from loading remote files."
a69c01a0
MA
7676 (interactive)
7677 ;; When Tramp is not loaded yet, its autoloads are still active.
8c04e197 7678 (tramp-unload-file-name-handlers)
a69c01a0
MA
7679 ;; ange-ftp settings must be enabled.
7680 (when (functionp 'tramp-ftp-enable-ange-ftp)
7681 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7682 ;; `tramp-util' unloads also `tramp'.
7683 (condition-case nil ;; maybe its not loaded yet.
7684 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7685 (error nil)))
7686
fb7933a3
KG
7687(provide 'tramp)
7688
7689;; Make sure that we get integration with the VC package.
7690;; When it is loaded, we need to pull in the integration module.
7691;; This must come after (provide 'tramp) because tramp-vc.el
7692;; requires tramp.
7693(eval-after-load "vc"
a69c01a0
MA
7694 '(progn
7695 (require 'tramp-vc)
7696 (add-hook 'tramp-unload-hook
7697 '(lambda ()
7698 (when (featurep 'tramp-vc)
7699 (unload-feature 'tramp-vc 'force))))))
fb7933a3
KG
7700
7701;;; TODO:
7702
4007ba5b
KG
7703;; * Allow putting passwords in the filename.
7704;; This should be implemented via a general mechanism to add
7705;; parameters in filenames. There is currently a kludge for
7706;; putting the port number into the filename for ssh and ftp
7707;; files. This could be subsumed by the new mechanism as well.
7708;; Another approach is to read a netrc file like ~/.authinfo
7709;; from Gnus.
7710;; * Handle nonlocal exits such as C-g.
16674e4f 7711;; * Autodetect if remote `ls' groks the "--dired" switch.
b1d06e75
KG
7712;; * Add fallback for inline encodings. This should be used
7713;; if the remote end doesn't support mimencode or a similar program.
7714;; For reading files from the remote host, we can just parse the output
7715;; of `od -b'. For writing files to the remote host, we construct
7716;; a shell program which contains only "safe" ascii characters
7717;; and which writes the right bytes to the file. We can use printf(1)
7718;; or "echo -e" or the printf function in awk and use octal escapes
7719;; for the "dangerous" characters. The null byte might be a problem.
7720;; On some systems, the octal escape doesn't work. So we try the following
7721;; two commands to write a null byte:
7722;; dd if=/dev/zero bs=1 count=1
7723;; echo | tr '\n' '\000'
16674e4f
KG
7724;; * Separate local `tramp-coding-commands' from remote ones. Connect
7725;; the two via a format which can be `uu' or `b64'. Then we can search
7726;; for the right local commands and the right remote commands separately.
fb7933a3
KG
7727;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7728;; work for remote files.
fb7933a3 7729;; * Rewrite `tramp-shell-quote-argument' to abstain from using
b1d06e75 7730;; `shell-quote-argument'.
fb7933a3 7731;; * Completion gets confused when you leave out the method name.
fb7933a3
KG
7732;; * In Emacs 21, `insert-directory' shows total number of bytes used
7733;; by the files in that directory. Add this here.
7734;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7735;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7736;; * When logging in, keep looking for questions according to an alist
7737;; and then invoke the right function.
7738;; * Case-insensitive filename completion. (Norbert Goevert.)
7739;; * Running CVS remotely doesn't appear to work right. It thinks
7740;; files are locked by somebody else even if I'm the locking user.
7741;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7742;; (Skip Montanaro)
7743;; * Don't use globbing for directories with many files, as this is
7744;; likely to produce long command lines, and some shells choke on
7745;; long command lines.
fb7933a3
KG
7746;; * Find out about the new auto-save mechanism in Emacs 21 and
7747;; do the right thing.
7748;; * `vc-directory' does not work. It never displays any files, even
7749;; if it does show files when run locally.
7750;; * Allow correction of passwords, if the remote end allows this.
7751;; (Mark Hershberger)
fb7933a3
KG
7752;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7753;; * Do asynchronous `shell-command's.
7754;; * Grok `append' parameter for `write-region'.
7755;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7756;; * abbreviate-file-name
7757;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
fb7933a3
KG
7758;; * Also allow to omit user names when doing multi-hop. Not sure yet
7759;; what the user names should default to, though.
7760;; * better error checking. At least whenever we see something
7761;; strange when doing zerop, we should kill the process and start
7762;; again. (Greg Stark)
7763;; * Add caching for filename completion. (Greg Stark)
bf247b6e 7764;; Of course, this has issues with usability (stale cache bites)
fb7933a3
KG
7765;; -- <daniel@danann.net>
7766;; * Provide a local cache of old versions of remote files for the rsync
7767;; transfer method to use. (Greg Stark)
7768;; * Remove unneeded parameters from methods.
7769;; * Invoke rsync once for copying a whole directory hierarchy.
83bbd71b 7770;; (Francesco Potort\e,Al\e(B)
fb7933a3
KG
7771;; * Should we set PATH ourselves or should we rely on the remote end
7772;; to do it?
fb7933a3 7773;; * Make it work for XEmacs 20, which is missing `with-timeout'.
fb7933a3
KG
7774;; * Make it work for different encodings, and for different file name
7775;; encodings, too. (Daniel Pittman)
7776;; * Change applicable functions to pass a struct tramp-file-name rather
7432277c 7777;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
fb7933a3
KG
7778;; * Implement asynchronous shell commands.
7779;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7780;; * Progress reports while copying files. (Michael Kifer)
7781;; * `Smart' connection method that uses inline for small and out of
7782;; band for large files. (Michael Kifer)
7783;; * Don't search for perl5 and perl. Instead, only search for perl and
7784;; then look if it's the right version (with `perl -v').
7785;; * When editing a remote CVS controlled file as a different user, VC
7786;; gets confused about the file locking status. Try to find out why
7787;; the workaround doesn't work.
fb7933a3
KG
7788;; * Change `copy-file' to grok the case where the filename handler
7789;; for the source and the target file are different. Right now,
7790;; it looks at the source file and then calls that handler, if
7791;; there is one. But since ange-ftp, for instance, does not know
7792;; about Tramp, it does not do the right thing if the target file
7793;; name is a Tramp name.
3cdaec13 7794;; * Username and hostname completion.
16674e4f
KG
7795;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7796;; connect to host "blabla" already if that host is unique. No idea
7797;; how to suppress. Maybe not an essential problem.
16674e4f 7798;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
16674e4f 7799;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
8daea7fc 7800;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
16674e4f
KG
7801;; Code is nearly identical.
7802;; ** Decide whiche files to take for searching user/host names depending on
7803;; operating system (windows-nt) in `tramp-completion-function-alist'.
7804;; ** Enhance variables for debug.
7805;; ** Implement "/multi:" completion.
7806;; ** Add a learning mode for completion. Make results persistent.
c951aecb 7807;; * Allow out-of-band methods as _last_ multi-hop.
fb7933a3
KG
7808
7809;; Functions for file-name-handler-alist:
7810;; diff-latest-backup-file -- in diff.el
fb7933a3
KG
7811;; dired-uncache -- this will be needed when we do insert-directory caching
7812;; file-name-as-directory -- use primitive?
fb7933a3 7813;; file-name-sans-versions -- use primitive?
fb7933a3 7814;; get-file-buffer -- use primitive
fb7933a3
KG
7815;; vc-registered
7816
ab5796a9 7817;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
fb7933a3 7818;;; tramp.el ends here