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