1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
4 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005 Free Software Foundation, Inc.
7 ;; Author: Kai Gro
\e,A_
\e(Bjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This package provides remote file editing, similar to ange-ftp.
31 ;; The difference is that ange-ftp uses FTP to transfer files between
32 ;; the local and the remote host, whereas tramp.el uses a combination
33 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
35 ;; For more detailed instructions, please see the info file.
40 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
41 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
42 ;; 19 is reported to have other problems. For XEmacs 21, you need the
43 ;; package `fsf-compat' for the `with-timeout' macro.)
45 ;; This version might not work with pre-Emacs 21 VC unless VC is
46 ;; loaded before tramp.el. Could you please test this and tell me about
47 ;; the result? Thanks.
49 ;; Also see the todo list at the bottom of this file.
51 ;; The current version of Tramp can be retrieved from the following URL:
52 ;; http://ftp.gnu.org/gnu/tramp/
54 ;; There's a mailing list for this, as well. Its name is:
55 ;; tramp-devel@gnu.org
56 ;; You can use the Web to subscribe, under the following URL:
57 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
59 ;; For the adventurous, the current development sources are available
60 ;; via CVS. You can find instructions about this at the following URL:
61 ;; http://savannah.gnu.org/projects/tramp/
62 ;; Click on "CVS" in the navigation bar near the top.
64 ;; Don't forget to put on your asbestos longjohns, first!
68 ;; The Tramp version number and bug report address, as prepared by configure.
72 (require 'format-spec) ;from Gnus 5.8, also in tar ball
73 ;; As long as password.el is not part of (X)Emacs, it shouldn't
75 (if (featurep 'xemacs)
76 (load "password" 'noerror)
77 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
79 ;; The explicit check is not necessary in Emacs, which provides the
80 ;; feature even if implemented in C, but it appears to be necessary
82 (unless (and (fboundp 'base64-encode-region)
83 (fboundp 'base64-decode-region))
84 (require 'base64)) ;for the mimencode methods
88 (autoload 'tramp-uuencode-region "tramp-uu"
89 "Implementation of `uuencode' in Lisp.")
91 (unless (fboundp 'uudecode-decode-region)
92 (autoload 'uudecode-decode-region "uudecode"))
94 ;; XEmacs is distributed with few Lisp packages. Further packages are
95 ;; installed using EFS. If we use a unified filename format, then
96 ;; Tramp is required in addition to EFS. (But why can't Tramp just
97 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
98 ;; just like before.) Another reason for using a separate filename
99 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
100 ;; Tramp only knows how to deal with `file-name-handler-alist', not
103 (defvar tramp-unified-filenames (not (featurep 'xemacs))
104 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
105 Nil means to use a separate filename syntax for Tramp.")
107 ;; Load foreign methods. Because they do require Tramp internally, this
108 ;; must be done with the `eval-after-load' trick.
110 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
111 (unless (featurep 'xemacs)
112 (eval-after-load "tramp"
113 '(require 'tramp-ftp)))
114 (when (and tramp-unified-filenames (featurep 'xemacs))
115 (eval-after-load "tramp"
116 '(require 'tramp-efs)))
118 ;; tramp-smb uses "smbclient" from Samba.
119 ;; Not available under Cygwin and Windows, because they don't offer
120 ;; "smbclient". And even not necessary there, because Emacs supports
121 ;; UNC file names like "//host/share/localname".
122 (unless (memq system-type '(cygwin windows-nt))
123 (eval-after-load "tramp"
124 '(require 'tramp-smb)))
129 ;; Emacs 19.34 compatibility hack -- is this needed?
130 (or (>= emacs-major-version 20)
133 (unless (boundp 'custom-print-functions)
134 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
136 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
137 ;; Currently, XEmacs supports this.
139 (when (featurep 'xemacs)
140 (let (unused-vars) ; Pacify Emacs byte-compiler
141 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
142 (byte-compiler-options (warnings (- unused-vars))))))
144 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
145 ;; used in XEmacs, so we set it here and there. The following is needed
146 ;; to pacify Emacs byte-compiler.
148 (when (boundp 'byte-compile-not-obsolete-var)
149 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
151 ;;; User Customizable Internal Variables:
154 "Edit remote files with a combination of rsh and rcp or similar programs."
158 (defcustom tramp-verbose 9
159 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
163 (defcustom tramp-debug-buffer nil
164 "*Whether to send all commands and responses to a debug buffer."
170 (when (boundp 'backup-directory-alist)
171 (defcustom tramp-backup-directory-alist nil
172 "Alist of filename patterns and backup directory names.
173 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
174 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
175 is a local file name, the backup directory is prepended with Tramp file
176 name prefix \(multi-method, method, user, host\) of file.
178 \(setq tramp-backup-directory-alist backup-directory-alist\)
180 gives the same backup policy for Tramp files on their hosts like the
181 policy for local files."
183 :type '(repeat (cons (regexp :tag "Regexp matching filename")
184 (directory :tag "Backup directory name"))))))
186 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
187 ;; the package "backup-dir" might not be loaded yet.
189 (when (featurep 'xemacs)
190 (defcustom tramp-bkup-backup-directory-info nil
191 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
192 It has the same meaning like `bkup-backup-directory-info' from package
193 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
194 file name, the backup directory is prepended with Tramp file name prefix
195 \(multi-method, method, user, host\) of file.
197 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
199 gives the same backup policy for Tramp files on their hosts like the
200 policy for local files."
202 (list (regexp :tag "File regexp")
203 (string :tag "Backup Dir")
208 (const search-upward))))
211 (defcustom tramp-auto-save-directory nil
212 "*Put auto-save files in this directory, if set.
213 The idea is to use a local directory so that auto-saving is faster."
215 :type '(choice (const nil)
218 (defcustom tramp-encoding-shell
219 (if (memq system-type '(windows-nt))
222 "*Use this program for encoding and decoding commands on the local host.
223 This shell is used to execute the encoding and decoding command on the
224 local host, so if you want to use `~' in those commands, you should
225 choose a shell here which groks tilde expansion. `/bin/sh' normally
226 does not understand tilde expansion.
228 For encoding and deocding, commands like the following are executed:
230 /bin/sh -c COMMAND < INPUT > OUTPUT
232 This variable can be used to change the \"/bin/sh\" part. See the
233 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
234 variable `tramp-encoding-reads-stdin' to specify whether the commands read
235 standard input or a file.
237 Note that this variable is not used for remote commands. There are
238 mechanisms in tramp.el which automatically determine the right shell to
239 use for the remote host."
241 :type '(file :must-match t))
243 (defcustom tramp-encoding-command-switch
244 (if (string-match "cmd\\.exe" tramp-encoding-shell)
247 "*Use this switch together with `tramp-encoding-shell' for local commands.
248 See the variable `tramp-encoding-shell' for more information."
252 (defcustom tramp-encoding-reads-stdin t
253 "*If non-nil, encoding commands read from standard input.
254 If nil, the filename is the last argument.
256 Note that the commands always must write to standard output."
260 (defcustom tramp-multi-sh-program
262 "*Use this program for bootstrapping multi-hop connections.
263 This variable is similar to `tramp-encoding-shell', but it is only used
264 when initializing a multi-hop connection. Therefore, the set of
265 commands sent to this shell is quite restricted, and if you are
266 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
267 shell which does not normally exist on Windows anyway).
269 To use multi-hop methods from Windows, you also need suitable entries
270 in `tramp-multi-connection-function-alist' for the first hop.
272 This variable defaults to the value of `tramp-encoding-shell'."
274 :type '(file :must-match t))
276 ;; CCC I have changed all occurrences of comint-quote-filename with
277 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
278 ;; There, comint-quote-filename was removed altogether. If it turns
279 ;; out to be necessary there, something will need to be done.
280 ;;-(defcustom tramp-file-name-quote-list
281 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
282 ;;- "*Protect these characters from the remote shell.
283 ;;-Any character in this list is quoted (preceded with a backslash)
284 ;;-because it means something special to the shell. This takes effect
285 ;;-when sending file and directory names to the remote shell.
287 ;;-See `comint-file-name-quote-list' for details."
289 ;;- :type '(repeat character))
291 (defcustom tramp-methods
292 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
293 (tramp-login-program "rsh")
294 (tramp-copy-program "rcp")
295 (tramp-remote-sh "/bin/sh")
296 (tramp-login-args nil)
297 (tramp-copy-args nil)
298 (tramp-copy-keep-date-arg "-p")
299 (tramp-password-end-of-line nil))
300 ("scp" (tramp-connection-function tramp-open-connection-rsh)
301 (tramp-login-program "ssh")
302 (tramp-copy-program "scp")
303 (tramp-remote-sh "/bin/sh")
304 (tramp-login-args ("-e" "none"))
305 (tramp-copy-args nil)
306 (tramp-copy-keep-date-arg "-p")
307 (tramp-password-end-of-line nil))
308 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
309 (tramp-login-program "ssh")
310 (tramp-copy-program "scp")
311 (tramp-remote-sh "/bin/sh")
312 (tramp-login-args ("-1" "-e" "none"))
313 (tramp-copy-args ("-1"))
314 (tramp-copy-keep-date-arg "-p")
315 (tramp-password-end-of-line nil))
316 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
317 (tramp-login-program "ssh")
318 (tramp-copy-program "scp")
319 (tramp-remote-sh "/bin/sh")
320 (tramp-login-args ("-2" "-e" "none"))
321 (tramp-copy-args ("-2"))
322 (tramp-copy-keep-date-arg "-p")
323 (tramp-password-end-of-line nil))
325 (tramp-connection-function tramp-open-connection-rsh)
326 (tramp-login-program "ssh1")
327 (tramp-copy-program "scp1")
328 (tramp-remote-sh "/bin/sh")
329 (tramp-login-args ("-e" "none"))
330 (tramp-copy-args nil)
331 (tramp-copy-keep-date-arg "-p")
332 (tramp-password-end-of-line nil))
334 (tramp-connection-function tramp-open-connection-rsh)
335 (tramp-login-program "ssh2")
336 (tramp-copy-program "scp2")
337 (tramp-remote-sh "/bin/sh")
338 (tramp-login-args ("-e" "none"))
339 (tramp-copy-args nil)
340 (tramp-copy-keep-date-arg "-p")
341 (tramp-password-end-of-line nil))
342 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
343 (tramp-login-program "ssh")
344 (tramp-copy-program "rsync")
345 (tramp-remote-sh "/bin/sh")
346 (tramp-login-args ("-e" "none"))
347 (tramp-copy-args ("-e" "ssh"))
348 (tramp-copy-keep-date-arg "-t")
349 (tramp-password-end-of-line nil))
350 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
351 (tramp-login-program "remsh")
352 (tramp-copy-program "rcp")
353 (tramp-remote-sh "/bin/sh")
354 (tramp-login-args nil)
355 (tramp-copy-args nil)
356 (tramp-copy-keep-date-arg "-p")
357 (tramp-password-end-of-line nil))
358 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
359 (tramp-login-program "rsh")
360 (tramp-copy-program nil)
361 (tramp-remote-sh "/bin/sh")
362 (tramp-login-args nil)
363 (tramp-copy-args nil)
364 (tramp-copy-keep-date-arg nil)
365 (tramp-password-end-of-line nil))
366 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
367 (tramp-login-program "ssh")
368 (tramp-copy-program nil)
369 (tramp-remote-sh "/bin/sh")
370 (tramp-login-args ("-e" "none"))
371 (tramp-copy-args nil)
372 (tramp-copy-keep-date-arg nil)
373 (tramp-password-end-of-line nil))
374 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
375 (tramp-login-program "ssh")
376 (tramp-copy-program nil)
377 (tramp-remote-sh "/bin/sh")
378 (tramp-login-args ("-1" "-e" "none"))
379 (tramp-copy-args ("-1"))
380 (tramp-copy-keep-date-arg nil)
381 (tramp-password-end-of-line nil))
382 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
383 (tramp-login-program "ssh")
384 (tramp-copy-program nil)
385 (tramp-remote-sh "/bin/sh")
386 (tramp-login-args ("-2" "-e" "none"))
387 (tramp-copy-args ("-2"))
388 (tramp-copy-keep-date-arg nil)
389 (tramp-password-end-of-line nil))
391 (tramp-connection-function tramp-open-connection-rsh)
392 (tramp-login-program "ssh1")
393 (tramp-copy-program nil)
394 (tramp-remote-sh "/bin/sh")
395 (tramp-login-args ("-e" "none"))
396 (tramp-copy-args nil)
397 (tramp-copy-keep-date-arg nil)
398 (tramp-password-end-of-line nil))
400 (tramp-connection-function tramp-open-connection-rsh)
401 (tramp-login-program "ssh2")
402 (tramp-copy-program nil)
403 (tramp-remote-sh "/bin/sh")
404 (tramp-login-args ("-e" "none"))
405 (tramp-copy-args nil)
406 (tramp-copy-keep-date-arg nil)
407 (tramp-password-end-of-line nil))
408 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
409 (tramp-login-program "remsh")
410 (tramp-copy-program nil)
411 (tramp-remote-sh "/bin/sh")
412 (tramp-login-args nil)
413 (tramp-copy-args nil)
414 (tramp-copy-keep-date-arg nil)
415 (tramp-password-end-of-line nil))
417 (tramp-connection-function tramp-open-connection-telnet)
418 (tramp-login-program "telnet")
419 (tramp-copy-program nil)
420 (tramp-remote-sh "/bin/sh")
421 (tramp-login-args nil)
422 (tramp-copy-args nil)
423 (tramp-copy-keep-date-arg nil)
424 (tramp-password-end-of-line nil))
425 ("su" (tramp-connection-function tramp-open-connection-su)
426 (tramp-login-program "su")
427 (tramp-copy-program nil)
428 (tramp-remote-sh "/bin/sh")
429 (tramp-login-args ("-" "%u"))
430 (tramp-copy-args nil)
431 (tramp-copy-keep-date-arg nil)
432 (tramp-password-end-of-line nil))
433 ("sudo" (tramp-connection-function tramp-open-connection-su)
434 (tramp-login-program "sudo")
435 (tramp-copy-program nil)
436 (tramp-remote-sh "/bin/sh")
437 (tramp-login-args ("-u" "%u" "-s"
439 (tramp-copy-args nil)
440 (tramp-copy-keep-date-arg nil)
441 (tramp-password-end-of-line nil))
442 ("multi" (tramp-connection-function tramp-open-connection-multi)
443 (tramp-login-program nil)
444 (tramp-copy-program nil)
445 (tramp-remote-sh "/bin/sh")
446 (tramp-login-args nil)
447 (tramp-copy-args nil)
448 (tramp-copy-keep-date-arg nil)
449 (tramp-password-end-of-line nil))
450 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
451 (tramp-login-program "ssh")
452 (tramp-copy-program "scp")
453 (tramp-remote-sh "/bin/sh")
454 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
455 (tramp-copy-args nil)
456 (tramp-copy-keep-date-arg "-p")
457 (tramp-password-end-of-line nil))
458 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
459 (tramp-login-program "ssh")
460 (tramp-copy-program nil)
461 (tramp-remote-sh "/bin/sh")
462 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
463 (tramp-copy-args nil)
464 (tramp-copy-keep-date-arg nil)
465 (tramp-password-end-of-line nil))
467 (tramp-connection-function tramp-open-connection-rsh)
468 (tramp-login-program "krlogin")
469 (tramp-copy-program nil)
470 (tramp-remote-sh "/bin/sh")
471 (tramp-login-args ("-x"))
472 (tramp-copy-args nil)
473 (tramp-copy-keep-date-arg nil)
474 (tramp-password-end-of-line nil))
476 (tramp-connection-function tramp-open-connection-rsh)
477 (tramp-login-program "plink")
478 (tramp-copy-program nil)
479 (tramp-remote-sh "/bin/sh")
480 (tramp-login-args ("-ssh")) ;optionally add "-v"
481 (tramp-copy-args nil)
482 (tramp-copy-keep-date-arg nil)
483 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
485 (tramp-connection-function tramp-open-connection-rsh)
486 (tramp-login-program "plink")
487 (tramp-copy-program nil)
488 (tramp-remote-sh "/bin/sh")
489 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
490 (tramp-copy-args nil)
491 (tramp-copy-keep-date-arg nil)
492 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
494 (tramp-connection-function tramp-open-connection-rsh)
495 (tramp-login-program "plink")
496 (tramp-copy-program "pscp")
497 (tramp-remote-sh "/bin/sh")
498 (tramp-login-args ("-ssh"))
499 (tramp-copy-args nil)
500 (tramp-copy-keep-date-arg "-p")
501 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
503 (tramp-connection-function tramp-open-connection-rsh)
504 (tramp-login-program "fsh")
505 (tramp-copy-program "fcp")
506 (tramp-remote-sh "/bin/sh -i")
507 (tramp-login-args ("sh" "-i"))
508 (tramp-copy-args nil)
509 (tramp-copy-keep-date-arg "-p")
510 (tramp-password-end-of-line nil))
512 "*Alist of methods for remote files.
513 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
514 Each NAME stands for a remote access method. Each PARAM is a
515 pair of the form (KEY VALUE). The following KEYs are defined:
516 * `tramp-connection-function'
517 This specifies the function to use to connect to the remote host.
518 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
519 and `tramp-open-connection-su' are defined. See the documentation
520 of these functions for more details.
522 This specifies the Bourne shell to use on the remote host. This
523 MUST be a Bourne-like shell. It is normally not necessary to set
524 this to any value other than \"/bin/sh\": tramp wants to use a shell
525 which groks tilde expansion, but it can search for it. Also note
526 that \"/bin/sh\" exists on all Unixen, this might not be true for
527 the value that you decide to use. You Have Been Warned.
528 * `tramp-login-program'
529 This specifies the name of the program to use for logging in to the
530 remote host. Depending on `tramp-connection-function', this may be
531 the name of rsh or a workalike program (when
532 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
533 name of telnet or a workalike (for `tramp-open-connection-telnet'),
534 or the name of su or a workalike (for `tramp-open-connection-su').
536 This specifies the list of arguments to pass to the above
537 mentioned program. Please note that this is a list of arguments,
538 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
539 here. Instead, you want two list elements, one for \"-a\" and one
540 for \"-b\", or one for \"-f\" and one for \"foo\".
541 If `tramp-connection-function' is `tramp-open-connection-su', then
542 \"%u\" in this list is replaced by the user name, and \"%%\" can
543 be used to obtain a literal percent character.
544 * `tramp-copy-program'
545 This specifies the name of the program to use for remotely copying
546 the file; this might be the absolute filename of rcp or the name of
549 This specifies the list of parameters to pass to the above mentioned
550 program, the hints for `tramp-login-args' also apply here.
551 * `tramp-copy-keep-date-arg'
552 This specifies the parameter to use for the copying program when the
553 timestamp of the original file should be kept. For `rcp', use `-p', for
555 * `tramp-password-end-of-line'
556 This specifies the string to use for terminating the line after
557 submitting the password. If this method parameter is nil, then the
558 value of the normal variable `tramp-default-password-end-of-line'
559 is used. This parameter is necessary because the \"plink\" program
560 requires any two characters after sending the password. These do
561 not have to be newline or carriage return characters. Other login
562 programs are happy with just one character, the newline character.
563 We use \"xy\" as the value for methods using \"plink\".
565 What does all this mean? Well, you should specify `tramp-login-program'
566 for all methods; this program is used to log in to the remote site. Then,
567 there are two ways to actually transfer the files between the local and the
568 remote side. One way is using an additional rcp-like program. If you want
569 to do this, set `tramp-copy-program' in the method.
571 Another possibility for file transfer is inline transfer, i.e. the
572 file is passed through the same buffer used by `tramp-login-program'. In
573 this case, the file contents need to be protected since the
574 `tramp-login-program' might use escape codes or the connection might not
575 be eight-bit clean. Therefore, file contents are encoded for transit.
576 See the variable `tramp-coding-commands' for details.
578 So, to summarize: if the method is an out-of-band method, then you
579 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
580 inline method, then these two parameters should be nil. Every method,
581 inline or out of band, must specify `tramp-connection-function' plus
582 the associated arguments (for example, the login program if you chose
583 `tramp-open-connection-telnet').
587 When using `tramp-open-connection-su' the phrase `open connection to a
588 remote host' sounds strange, but it is used nevertheless, for
589 consistency. No connection is opened to a remote host, but `su' is
590 started on the local host. You are not allowed to specify a remote
591 host other than `localhost' or the name of the local host."
595 (set (list (const tramp-connection-function) function)
596 (list (const tramp-login-program)
597 (choice (const nil) string))
598 (list (const tramp-copy-program)
599 (choice (const nil) string))
600 (list (const tramp-remote-sh)
601 (choice (const nil) string))
602 (list (const tramp-login-args) (repeat string))
603 (list (const tramp-copy-args) (repeat string))
604 (list (const tramp-copy-keep-date-arg)
605 (choice (const nil) string))
606 (list (const tramp-encoding-command)
607 (choice (const nil) string))
608 (list (const tramp-decoding-command)
609 (choice (const nil) string))
610 (list (const tramp-encoding-function)
611 (choice (const nil) function))
612 (list (const tramp-decoding-function)
613 (choice (const nil) function))
614 (list (const tramp-password-end-of-line)
615 (choice (const nil) string))))))
617 (defcustom tramp-multi-methods '("multi" "multiu")
618 "*List of multi-hop methods.
619 Each entry in this list should be a method name as mentioned in the
620 variable `tramp-methods'."
622 :type '(repeat string))
624 (defcustom tramp-multi-connection-function-alist
625 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
626 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
627 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
628 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
629 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
630 ("su" tramp-multi-connect-su "su - %u%n")
631 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
632 "*List of connection functions for multi-hop methods.
633 Each list item is a list of three items (METHOD FUNCTION COMMAND),
634 where METHOD is the name as used in the file name, FUNCTION is the
635 function to be executed, and COMMAND is the shell command used for
638 COMMAND may contain percent escapes. `%u' will be replaced with the
639 user name, `%h' will be replaced with the host name, and `%n' will be
640 replaced with an end-of-line character, as specified in the variable
641 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
642 Note that the interpretation of the percent escapes also depends on
643 the FUNCTION. For example, the `%u' escape is forbidden with the
644 function `tramp-multi-connect-telnet'. See the documentation of the
645 various functions for details."
647 :type '(repeat (list string function string)))
649 (defcustom tramp-default-method
650 (if (and (fboundp 'executable-find)
651 (executable-find "plink"))
654 "*Default method to use for transferring files.
655 See `tramp-methods' for possibilities.
656 Also see `tramp-default-method-alist'."
660 (defcustom tramp-default-method-alist
661 '(("\\`localhost\\'" "\\`root\\'" "su"))
662 "*Default method to use for specific user/host pairs.
663 This is an alist of items (HOST USER METHOD). The first matching item
664 specifies the method to use for a file name which does not specify a
665 method. HOST and USER are regular expressions or nil, which is
666 interpreted as a regular expression which always matches. If no entry
667 matches, the variable `tramp-default-method' takes effect.
669 If the file name does not specify the user, lookup is done using the
670 empty string for the user name.
672 See `tramp-methods' for a list of possibilities for METHOD."
674 :type '(repeat (list (regexp :tag "Host regexp")
675 (regexp :tag "User regexp")
676 (string :tag "Method"))))
678 ;; Default values for non-Unices seeked
679 (defconst tramp-completion-function-alist-rsh
680 (unless (memq system-type '(windows-nt))
681 '((tramp-parse-rhosts "/etc/hosts.equiv")
682 (tramp-parse-rhosts "~/.rhosts")))
683 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
685 ;; Default values for non-Unices seeked
686 (defconst tramp-completion-function-alist-ssh
687 (unless (memq system-type '(windows-nt))
688 '((tramp-parse-rhosts "/etc/hosts.equiv")
689 (tramp-parse-rhosts "/etc/shosts.equiv")
690 (tramp-parse-shosts "/etc/ssh_known_hosts")
691 (tramp-parse-sconfig "/etc/ssh_config")
692 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
693 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
694 (tramp-parse-rhosts "~/.rhosts")
695 (tramp-parse-rhosts "~/.shosts")
696 (tramp-parse-shosts "~/.ssh/known_hosts")
697 (tramp-parse-sconfig "~/.ssh/config")
698 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
699 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
700 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
702 ;; Default values for non-Unices seeked
703 (defconst tramp-completion-function-alist-telnet
704 (unless (memq system-type '(windows-nt))
705 '((tramp-parse-hosts "/etc/hosts")))
706 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
708 ;; Default values for non-Unices seeked
709 (defconst tramp-completion-function-alist-su
710 (unless (memq system-type '(windows-nt))
711 '((tramp-parse-passwd "/etc/passwd")))
712 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
714 (defvar tramp-completion-function-alist nil
715 "*Alist of methods for remote files.
716 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
717 Each NAME stands for a remote access method. Each PAIR is of the form
718 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
719 names from FILE for completion. The following predefined FUNCTIONs exists:
721 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
722 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
723 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
724 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
725 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
726 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
727 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
728 * `tramp-parse-netrc' for \"~/.netrc\" like files.
730 FUNCTION can also be a customer defined function. For more details see
733 (eval-after-load "tramp"
735 (tramp-set-completion-function
736 "rcp" tramp-completion-function-alist-rsh)
737 (tramp-set-completion-function
738 "scp" tramp-completion-function-alist-ssh)
739 (tramp-set-completion-function
740 "scp1" tramp-completion-function-alist-ssh)
741 (tramp-set-completion-function
742 "scp2" tramp-completion-function-alist-ssh)
743 (tramp-set-completion-function
744 "scp1_old" tramp-completion-function-alist-ssh)
745 (tramp-set-completion-function
746 "scp2_old" tramp-completion-function-alist-ssh)
747 (tramp-set-completion-function
748 "rsync" tramp-completion-function-alist-rsh)
749 (tramp-set-completion-function
750 "remcp" tramp-completion-function-alist-rsh)
751 (tramp-set-completion-function
752 "rsh" tramp-completion-function-alist-rsh)
753 (tramp-set-completion-function
754 "ssh" tramp-completion-function-alist-ssh)
755 (tramp-set-completion-function
756 "ssh1" tramp-completion-function-alist-ssh)
757 (tramp-set-completion-function
758 "ssh2" tramp-completion-function-alist-ssh)
759 (tramp-set-completion-function
760 "ssh1_old" tramp-completion-function-alist-ssh)
761 (tramp-set-completion-function
762 "ssh2_old" tramp-completion-function-alist-ssh)
763 (tramp-set-completion-function
764 "remsh" tramp-completion-function-alist-rsh)
765 (tramp-set-completion-function
766 "telnet" tramp-completion-function-alist-telnet)
767 (tramp-set-completion-function
768 "su" tramp-completion-function-alist-su)
769 (tramp-set-completion-function
770 "sudo" tramp-completion-function-alist-su)
771 (tramp-set-completion-function
773 (tramp-set-completion-function
774 "scpx" tramp-completion-function-alist-ssh)
775 (tramp-set-completion-function
776 "sshx" tramp-completion-function-alist-ssh)
777 (tramp-set-completion-function
778 "krlogin" tramp-completion-function-alist-rsh)
779 (tramp-set-completion-function
780 "plink" tramp-completion-function-alist-ssh)
781 (tramp-set-completion-function
782 "plink1" tramp-completion-function-alist-ssh)
783 (tramp-set-completion-function
784 "pscp" tramp-completion-function-alist-ssh)
785 (tramp-set-completion-function
786 "fcp" tramp-completion-function-alist-ssh)))
788 (defcustom tramp-rsh-end-of-line "\n"
789 "*String used for end of line in rsh connections.
790 I don't think this ever needs to be changed, so please tell me about it
791 if you need to change this.
792 Also see the method parameter `tramp-password-end-of-line' and the normal
793 variable `tramp-default-password-end-of-line'."
797 (defcustom tramp-default-password-end-of-line
798 tramp-rsh-end-of-line
799 "*String used for end of line after sending a password.
800 This variable provides the default value for the method parameter
801 `tramp-password-end-of-line', see `tramp-methods' for more details.
803 It seems that people using plink under Windows need to send
804 \"\\r\\n\" (carriage-return, then newline) after a password, but just
805 \"\\n\" after all other lines. This variable can be used for the
806 password, see `tramp-rsh-end-of-line' for the other cases.
808 The default value is to use the same value as `tramp-rsh-end-of-line'."
812 (defcustom tramp-remote-path
813 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
814 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
815 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
816 "*List of directories to search for executables on remote host.
817 Please notify me about other semi-standard directories to include here.
819 You can use `~' in this list, but when searching for a shell which groks
820 tilde expansion, all directory names starting with `~' will be ignored."
822 :type '(repeat string))
824 (defcustom tramp-login-prompt-regexp
825 ".*ogin\\( .*\\)?: *"
826 "*Regexp matching login-like prompts.
827 The regexp should match at end of buffer.
829 Sometimes the prompt is reported to look like \"login as:\"."
833 (defcustom tramp-shell-prompt-pattern
834 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
835 "Regexp to match prompts from remote shell.
836 Normally, Tramp expects you to configure `shell-prompt-pattern'
837 correctly, but sometimes it happens that you are connecting to a
838 remote host which sends a different kind of shell prompt. Therefore,
839 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
840 and also things matched by this variable. The default value of this
841 variable is similar to the default value of `shell-prompt-pattern',
842 which should work well in many cases."
846 (defcustom tramp-password-prompt-regexp
847 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
848 "*Regexp matching password-like prompts.
849 The regexp should match at end of buffer.
851 The `sudo' program appears to insert a `^@' character into the prompt."
855 (defcustom tramp-wrong-passwd-regexp
857 ;; These strings should be on the last line
858 (regexp-opt '("Permission denied."
864 "Name or service not known"
865 "Host key verification failed.") t)
869 ;; Here comes a list of regexes, separated by \\|
870 "Received signal [0-9]+"
872 "*Regexp matching a `login failed' message.
873 The regexp should match at end of buffer."
877 (defcustom tramp-yesno-prompt-regexp
879 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
881 "Regular expression matching all yes/no queries which need to be confirmed.
882 The confirmation should be done with yes or no.
883 The regexp should match at end of buffer.
884 See also `tramp-yn-prompt-regexp'."
888 (defcustom tramp-yn-prompt-regexp
889 (concat (regexp-opt '("Store key in cache? (y/n)") t)
891 "Regular expression matching all y/n queries which need to be confirmed.
892 The confirmation should be done with y or n.
893 The regexp should match at end of buffer.
894 See also `tramp-yesno-prompt-regexp'."
898 (defcustom tramp-terminal-prompt-regexp
902 "Terminal type\\? \\[.*\\]"
904 "Regular expression matching all terminal setting prompts.
905 The regexp should match at end of buffer.
906 The answer will be provided by `tramp-action-terminal', which see."
910 (defcustom tramp-operation-not-permitted-regexp
911 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
912 (regexp-opt '("Operation not permitted") t))
913 "Regular expression matching keep-date problems in (s)cp operations.
914 Copying has been performed successfully already, so this message can
919 (defcustom tramp-process-alive-regexp
921 "Regular expression indicating a process has finished.
922 In fact this expression is empty by intention, it will be used only to
923 check regularly the status of the associated process.
924 The answer will be provided by `tramp-action-process-alive',
925 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
929 (defcustom tramp-temp-name-prefix "tramp."
930 "*Prefix to use for temporary files.
931 If this is a relative file name (such as \"tramp.\"), it is considered
932 relative to the directory name returned by the function
933 `tramp-temporary-file-directory' (which see). It may also be an
934 absolute file name; don't forget to include a prefix for the filename
939 (defcustom tramp-discard-garbage nil
940 "*If non-nil, try to discard garbage sent by remote shell.
941 Some shells send such garbage upon connection setup."
945 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
946 "*Alist specifying extra arguments to pass to the remote shell.
947 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
948 matching the shell file name and ARGS is a string specifying the
951 This variable is only used when Tramp needs to start up another shell
952 for tilde expansion. The extra arguments should typically prevent the
953 shell from reading its init file."
955 ;; This might be the wrong way to test whether the widget type
956 ;; `alist' is available. Who knows the right way to test it?
957 :type (if (get 'alist 'widget-type)
958 '(alist :key-type string :value-type string)
959 '(repeat (cons string string))))
961 (defcustom tramp-prefix-format
962 (if tramp-unified-filenames "/" "/[")
963 "*String matching the very beginning of tramp file names.
964 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
968 (defcustom tramp-prefix-regexp
969 (concat "^" (regexp-quote tramp-prefix-format))
970 "*Regexp matching the very beginning of tramp file names.
971 Should always start with \"^\". Derived from `tramp-prefix-format'."
975 (defcustom tramp-method-regexp
977 "*Regexp matching methods identifiers."
981 ;; It is a little bit annoying that in XEmacs case this delimeter is different
982 ;; for single-hop and multi-hop cases.
983 (defcustom tramp-postfix-single-method-format
984 (if tramp-unified-filenames ":" "/")
985 "*String matching delimeter between method and user or host names.
986 Applicable for single-hop methods.
987 Used in `tramp-make-tramp-file-name'."
991 (defcustom tramp-postfix-single-method-regexp
992 (regexp-quote tramp-postfix-single-method-format)
993 "*Regexp matching delimeter between method and user or host names.
994 Applicable for single-hop methods.
995 Derived from `tramp-postfix-single-method-format'."
999 (defcustom tramp-postfix-multi-method-format
1001 "*String matching delimeter between method and user or host names.
1002 Applicable for multi-hop methods.
1003 Used in `tramp-make-tramp-multi-file-name'."
1007 (defcustom tramp-postfix-multi-method-regexp
1008 (regexp-quote tramp-postfix-multi-method-format)
1009 "*Regexp matching delimeter between method and user or host names.
1010 Applicable for multi-hop methods.
1011 Derived from `tramp-postfix-multi-method-format'."
1015 (defcustom tramp-postfix-multi-hop-format
1016 (if tramp-unified-filenames ":" "/")
1017 "*String matching delimeter between host and next method.
1018 Applicable for multi-hop methods.
1019 Used in `tramp-make-tramp-multi-file-name'."
1023 (defcustom tramp-postfix-multi-hop-regexp
1024 (regexp-quote tramp-postfix-multi-hop-format)
1025 "*Regexp matching delimeter between host and next method.
1026 Applicable for multi-hop methods.
1027 Derived from `tramp-postfix-multi-hop-format'."
1031 (defcustom tramp-user-regexp
1033 "*Regexp matching user names."
1037 (defcustom tramp-postfix-user-format
1039 "*String matching delimeter between user and host names.
1040 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1044 (defcustom tramp-postfix-user-regexp
1045 (regexp-quote tramp-postfix-user-format)
1046 "*Regexp matching delimeter between user and host names.
1047 Derived from `tramp-postfix-user-format'."
1051 (defcustom tramp-host-regexp
1053 "*Regexp matching host names."
1057 (defcustom tramp-host-with-port-regexp
1059 "*Regexp matching host names."
1063 (defcustom tramp-postfix-host-format
1064 (if tramp-unified-filenames ":" "]")
1065 "*String matching delimeter between host names and localnames.
1066 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1070 (defcustom tramp-postfix-host-regexp
1071 (regexp-quote tramp-postfix-host-format)
1072 "*Regexp matching delimeter between host names and localnames.
1073 Derived from `tramp-postfix-host-format'."
1077 (defcustom tramp-localname-regexp
1079 "*Regexp matching localnames."
1083 ;; File name format.
1085 (defcustom tramp-file-name-structure
1089 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1090 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1091 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1092 "\\(" tramp-localname-regexp "\\)")
1095 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1096 the tramp file name structure.
1098 The first element REGEXP is a regular expression matching a tramp file
1099 name. The regex should contain parentheses around the method name,
1100 the user name, the host name, and the file name parts.
1102 The second element METHOD is a number, saying which pair of
1103 parentheses matches the method name. The third element USER is
1104 similar, but for the user name. The fourth element HOST is similar,
1105 but for the host name. The fifth element FILE is for the file name.
1106 These numbers are passed directly to `match-string', which see. That
1107 means the opening parentheses are counted to identify the pair.
1109 See also `tramp-file-name-regexp'."
1111 :type '(list (regexp :tag "File name regexp")
1112 (integer :tag "Paren pair for method name")
1113 (integer :tag "Paren pair for user name ")
1114 (integer :tag "Paren pair for host name ")
1115 (integer :tag "Paren pair for file name ")))
1118 (defconst tramp-file-name-regexp-unified
1120 "Value for `tramp-file-name-regexp' for unified remoting.
1121 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1122 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1125 (defconst tramp-file-name-regexp-separate
1127 "Value for `tramp-file-name-regexp' for separate remoting.
1128 XEmacs uses a separate filename syntax for Tramp and EFS.
1129 See `tramp-file-name-structure-separate' for more explanations.")
1132 (defcustom tramp-file-name-regexp
1133 (if tramp-unified-filenames
1134 tramp-file-name-regexp-unified
1135 tramp-file-name-regexp-separate)
1136 "*Regular expression matching file names handled by tramp.
1137 This regexp should match tramp file names but no other file names.
1138 \(When tramp.el is loaded, this regular expression is prepended to
1139 `file-name-handler-alist', and that is searched sequentially. Thus,
1140 if the tramp entry appears rather early in the `file-name-handler-alist'
1141 and is a bit too general, then some files might be considered tramp
1142 files which are not really tramp files.
1144 Please note that the entry in `file-name-handler-alist' is made when
1145 this file (tramp.el) is loaded. This means that this variable must be set
1146 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1147 updated after changing this variable.
1149 Also see `tramp-file-name-structure'."
1154 (defconst tramp-completion-file-name-regexp-unified
1155 "^/$\\|^/[^/:][^/]*$"
1156 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1157 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1158 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1161 (defconst tramp-completion-file-name-regexp-separate
1162 "^/\\([[][^]]*\\)?$"
1163 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1164 XEmacs uses a separate filename syntax for Tramp and EFS.
1165 See `tramp-file-name-structure-separate' for more explanations.")
1168 (defcustom tramp-completion-file-name-regexp
1169 (if tramp-unified-filenames
1170 tramp-completion-file-name-regexp-unified
1171 tramp-completion-file-name-regexp-separate)
1172 "*Regular expression matching file names handled by tramp completion.
1173 This regexp should match partial tramp file names only.
1175 Please note that the entry in `file-name-handler-alist' is made when
1176 this file (tramp.el) is loaded. This means that this variable must be set
1177 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1178 updated after changing this variable.
1180 Also see `tramp-file-name-structure'."
1184 (defcustom tramp-multi-file-name-structure
1188 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1189 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1190 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1192 "*Describes the file name structure of `multi' files.
1193 Multi files allow you to contact a remote host in several hops.
1194 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1196 The first element, REGEXP, gives a regular expression to match against
1197 the file name. In this regular expression, `%s' is replaced with the
1198 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1199 allow multiple hops, you normally want to use something like
1200 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1201 of parentheses is used for the HOP element, see below.)
1203 All remaining elements are numbers. METHOD gives the number of the
1204 paren pair which matches the method name. HOP gives the number of the
1205 paren pair which matches the hop sequence. LOCALNAME gives the number of
1206 the paren pair which matches the localname (pathname) on the remote host.
1208 LOCALNAME can also be negative, which means to count from the end. Ie, a
1209 value of -1 means the last paren pair.
1211 I think it would be good if the regexp matches the whole of the
1212 string, but I haven't actually tried what happens if it doesn't..."
1214 :type '(list (regexp :tag "File name regexp")
1215 (integer :tag "Paren pair for method name")
1216 (integer :tag "Paren pair for hops")
1217 (integer :tag "Paren pair to match localname")))
1219 (defcustom tramp-multi-file-name-hop-structure
1222 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1223 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1224 "\\(" tramp-host-with-port-regexp "\\)")
1226 "*Describes the structure of a hop in multi files.
1227 This is a list of four elements (REGEXP METHOD USER HOST). First
1228 element REGEXP is used to match against the hop. Pair number METHOD
1229 matches the method of one hop, pair number USER matches the user of
1230 one hop, pair number HOST matches the host of one hop.
1232 This regular expression should match exactly all of one hop."
1234 :type '(list (regexp :tag "Hop regexp")
1235 (integer :tag "Paren pair for method name")
1236 (integer :tag "Paren pair for user name")
1237 (integer :tag "Paren pair for host name")))
1239 (defcustom tramp-make-multi-tramp-file-format
1241 (concat tramp-prefix-format "%m")
1242 (concat tramp-postfix-multi-hop-format
1243 "%m" tramp-postfix-multi-method-format
1244 "%u" tramp-postfix-user-format
1246 (concat tramp-postfix-host-format "%p"))
1247 "*Describes how to construct a `multi' file name.
1248 This is a list of three elements PREFIX, HOP and LOCALNAME.
1250 The first element PREFIX says how to construct the prefix, the second
1251 element HOP specifies what each hop looks like, and the final element
1252 LOCALNAME says how to construct the localname (pathname).
1254 In PREFIX, `%%' means `%' and `%m' means the method name.
1256 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1257 user and hop host, respectively.
1259 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1261 The resulting file name always contains one copy of PREFIX and one
1262 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1265 Note: the current implementation requires the prefix to contain the
1266 method name, followed by all the hops, and the localname must come
1269 :type '(list string string string))
1271 (defcustom tramp-terminal-type "dumb"
1272 "*Value of TERM environment variable for logging in to remote host.
1273 Because Tramp wants to parse the output of the remote shell, it is easily
1274 confused by ANSI color escape sequences and suchlike. Often, shell init
1275 files conditionalize this setup based on the TERM environment variable."
1279 (defcustom tramp-completion-without-shell-p nil
1280 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1281 Using shell wildcards for completions has the advantage that it can be
1282 fast even in large directories, but completion is always
1283 case-sensitive. Relying on Lisp only means that case-insensitive
1284 completion is possible (subject to the variable `completion-ignore-case'),
1285 but it might be slow on large directories."
1289 (defcustom tramp-actions-before-shell
1290 '((tramp-password-prompt-regexp tramp-action-password)
1291 (tramp-login-prompt-regexp tramp-action-login)
1292 (shell-prompt-pattern tramp-action-succeed)
1293 (tramp-shell-prompt-pattern tramp-action-succeed)
1294 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1295 (tramp-yesno-prompt-regexp tramp-action-yesno)
1296 (tramp-yn-prompt-regexp tramp-action-yn)
1297 (tramp-terminal-prompt-regexp tramp-action-terminal)
1298 (tramp-process-alive-regexp tramp-action-process-alive))
1299 "List of pattern/action pairs.
1300 Whenever a pattern matches, the corresponding action is performed.
1301 Each item looks like (PATTERN ACTION).
1303 The PATTERN should be a symbol, a variable. The value of this
1304 variable gives the regular expression to search for. Note that the
1305 regexp must match at the end of the buffer, \"\\'\" is implicitly
1308 The ACTION should also be a symbol, but a function. When the
1309 corresponding PATTERN matches, the ACTION function is called."
1311 :type '(repeat (list variable function)))
1313 (defcustom tramp-actions-copy-out-of-band
1314 '((tramp-password-prompt-regexp tramp-action-password)
1315 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1316 (tramp-process-alive-regexp tramp-action-out-of-band))
1317 "List of pattern/action pairs.
1318 This list is used for copying/renaming with out-of-band methods.
1319 See `tramp-actions-before-shell' for more info."
1321 :type '(repeat (list variable function)))
1323 (defcustom tramp-multi-actions
1324 '((tramp-password-prompt-regexp tramp-multi-action-password)
1325 (tramp-login-prompt-regexp tramp-multi-action-login)
1326 (shell-prompt-pattern tramp-multi-action-succeed)
1327 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1328 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1329 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1330 "List of pattern/action pairs.
1331 This list is used for each hop in multi-hop connections.
1332 See `tramp-actions-before-shell' for more info."
1334 :type '(repeat (list variable function)))
1336 (defcustom tramp-initial-commands
1339 "unset autocorrect")
1340 "List of commands to send to the first remote shell that we see.
1341 These commands will be sent to any shell, and thus they should be
1342 designed to work in such circumstances. Also, restrict the commands
1343 to the bare necessity for getting the remote shell into a state
1344 where it is possible to execute the Bourne-ish shell.
1346 At the moment, the command to execute the Bourne-ish shell uses strange
1347 quoting which `tcsh' tries to correct, so we send the command \"unset
1348 autocorrect\" to the remote host."
1350 :type '(repeat string))
1352 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1353 ;; known to have a bug in `process-send-string'; some ssh connections appear
1354 ;; to drop bytes when data is sent too quickly. There is also a connection
1355 ;; buffer local variable, which is computed depending on remote host properties
1356 ;; when `tramp-chunksize' is zero or nil.
1357 (defcustom tramp-chunksize
1358 (when (and (not (featurep 'xemacs))
1359 (memq system-type '(hpux)))
1361 "*If non-nil, chunksize for sending input to local process.
1362 It is necessary only on systems which have a buggy `process-send-string'
1363 implementation. The necessity, whether this variable must be set, can be
1364 checked via the following code:
1367 (let* ((user \"xxx\") (host \"yyy\")
1369 (sent init) (received init))
1370 (while (= sent received)
1371 (setq sent (+ sent step))
1373 (let ((proc (start-process (buffer-name) (current-buffer)
1374 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1375 (when (memq (process-status proc) '(run open))
1376 (process-send-string proc (make-string sent ?\\ ))
1377 (process-send-eof proc)
1378 (process-send-eof proc))
1379 (while (not (progn (goto-char (point-min))
1380 (re-search-forward \"\\\\w+\" (point-max) t)))
1381 (accept-process-output proc 1))
1382 (when (memq (process-status proc) '(run open))
1383 (setq received (string-to-number (match-string 0)))
1384 (delete-process proc)
1385 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1387 (if (> sent (+ init step))
1388 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1390 (message \"Test does not work\")
1391 (display-buffer (current-buffer))
1394 In the Emacs normally running Tramp, evaluate the above code
1395 (replace \"xxx\" and \"yyy\" by the remote user and host name,
1396 respectively). You can do this, for example, by pasting it into
1397 the `*scratch*' buffer and then hitting C-j with the cursor after the
1398 last closing parenthesis. Note that it works only if you have configured
1399 \"ssh\" to run without password query, see ssh-agent(1).
1401 You will see the number of bytes sent successfully to the remote host.
1402 If that number exceeds 1000, you can stop the execution by hitting
1403 C-g, because your Emacs is likely clean.
1405 If your Emacs is buggy, the code stops and gives you an indication
1406 about the value `tramp-chunksize' should be set. Maybe you could just
1407 experiment a bit, e.g. changing the values of `init' and `step'
1408 in the third line of the code.
1410 When it is necessary to set `tramp-chunksize', you might consider to
1411 use an out-of-the-band method (like \"scp\") instead of an internal one
1412 (like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1415 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1416 this variable to be set as well."
1418 :type '(choice (const nil) integer))
1420 ;; Logging in to a remote host normally requires obtaining a pty. But
1421 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1422 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1423 ;; for an override of the system default.
1424 (defcustom tramp-process-connection-type t
1425 "Overrides `process-connection-type' for connections from Tramp.
1426 Tramp binds process-connection-type to the value given here before
1427 opening a connection to a remote host."
1429 :type '(choice (const nil) (const t) (const pty)))
1431 ;;; Internal Variables:
1433 (defvar tramp-buffer-file-attributes nil
1434 "Holds the `ls -ild' output for the current buffer.
1435 This variable is local to each buffer. It is not used if the remote
1436 machine groks Perl. If it is used, it's used as an emulation for
1437 the visited file modtime.")
1438 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1440 (defvar tramp-md5-function
1441 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1442 ((fboundp 'md5-encode)
1443 (lambda (x) (base64-encode-string
1444 (funcall (symbol-function 'md5-encode) x))))
1445 (t (error "Coulnd't find an `md5' function")))
1446 "Function to call for running the MD5 algorithm.")
1448 (defvar tramp-end-of-output
1450 (funcall tramp-md5-function
1452 (prin1-to-string process-environment)
1453 (current-time-string)
1455 ;; (if (fboundp 'directory-files-and-attributes)
1456 ;; (funcall 'directory-files-and-attributes
1457 ;; (or (getenv "HOME")
1458 ;; (tramp-temporary-file-directory)))
1461 ;; (cons x (file-attributes x)))
1462 ;; (directory-files (or (getenv "HOME")
1463 ;; (tramp-temporary-file-directory))
1466 "String used to recognize end of output.")
1468 (defvar tramp-connection-function nil
1469 "This internal variable holds a parameter for `tramp-methods'.
1470 In the connection buffer, this variable has the value of the like-named
1471 method parameter, as specified in `tramp-methods' (which see).")
1473 (defvar tramp-remote-sh nil
1474 "This internal variable holds a parameter for `tramp-methods'.
1475 In the connection buffer, this variable has the value of the like-named
1476 method parameter, as specified in `tramp-methods' (which see).")
1478 (defvar tramp-login-program nil
1479 "This internal variable holds a parameter for `tramp-methods'.
1480 In the connection buffer, this variable has the value of the like-named
1481 method parameter, as specified in `tramp-methods' (which see).")
1483 (defvar tramp-login-args nil
1484 "This internal variable holds a parameter for `tramp-methods'.
1485 In the connection buffer, this variable has the value of the like-named
1486 method parameter, as specified in `tramp-methods' (which see).")
1488 (defvar tramp-copy-program nil
1489 "This internal variable holds a parameter for `tramp-methods'.
1490 In the connection buffer, this variable has the value of the like-named
1491 method parameter, as specified in `tramp-methods' (which see).")
1493 (defvar tramp-copy-args nil
1494 "This internal variable holds a parameter for `tramp-methods'.
1495 In the connection buffer, this variable has the value of the like-named
1496 method parameter, as specified in `tramp-methods' (which see).")
1498 (defvar tramp-copy-keep-date-arg nil
1499 "This internal variable holds a parameter for `tramp-methods'.
1500 In the connection buffer, this variable has the value of the like-named
1501 method parameter, as specified in `tramp-methods' (which see).")
1503 (defvar tramp-encoding-command nil
1504 "This internal variable holds a parameter for `tramp-methods'.
1505 In the connection buffer, this variable has the value of the like-named
1506 method parameter, as specified in `tramp-methods' (which see).")
1508 (defvar tramp-decoding-command nil
1509 "This internal variable holds a parameter for `tramp-methods'.
1510 In the connection buffer, this variable has the value of the like-named
1511 method parameter, as specified in `tramp-methods' (which see).")
1513 (defvar tramp-encoding-function nil
1514 "This internal variable holds a parameter for `tramp-methods'.
1515 In the connection buffer, this variable has the value of the like-named
1516 method parameter, as specified in `tramp-methods' (which see).")
1518 (defvar tramp-decoding-function nil
1519 "This internal variable holds a parameter for `tramp-methods'.
1520 In the connection buffer, this variable has the value of the like-named
1521 method parameter, as specified in `tramp-methods' (which see).")
1523 (defvar tramp-password-end-of-line nil
1524 "This internal variable holds a parameter for `tramp-methods'.
1525 In the connection buffer, this variable has the value of the like-named
1526 method parameter, as specified in `tramp-methods' (which see).")
1528 ;; CCC `local in each buffer'?
1529 (defvar tramp-ls-command nil
1530 "This command is used to get a long listing with numeric user and group ids.
1531 This variable is automatically made buffer-local to each rsh process buffer
1532 upon opening the connection.")
1534 (defvar tramp-current-multi-method nil
1535 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1536 This variable is automatically made buffer-local to each rsh process buffer
1537 upon opening the connection.")
1539 (defvar tramp-current-method nil
1540 "Connection method for this *tramp* buffer.
1541 This variable is automatically made buffer-local to each rsh process buffer
1542 upon opening the connection.")
1544 (defvar tramp-current-user nil
1545 "Remote login name for this *tramp* buffer.
1546 This variable is automatically made buffer-local to each rsh process buffer
1547 upon opening the connection.")
1549 (defvar tramp-current-host nil
1550 "Remote host for this *tramp* buffer.
1551 This variable is automatically made buffer-local to each rsh process buffer
1552 upon opening the connection.")
1554 (defvar tramp-test-groks-nt nil
1555 "Whether the `test' command groks the `-nt' switch.
1556 \(`test A -nt B' tests if file A is newer than file B.)
1557 This variable is automatically made buffer-local to each rsh process buffer
1558 upon opening the connection.")
1560 (defvar tramp-file-exists-command nil
1561 "Command to use for checking if a file exists.
1562 This variable is automatically made buffer-local to each rsh process buffer
1563 upon opening the connection.")
1565 (defconst tramp-uudecode "\
1567 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1571 "Shell function to implement `uudecode' to standard output.
1572 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1573 for this or `uudecode -p', but some systems don't, and for them
1574 we have this shell function.")
1576 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1577 ;; output. If you are hacking on this, note that you get *no* output
1578 ;; unless this spits out a complete line, including the '\n' at the
1580 ;; The device number is returned as "-1", because there will be a virtual
1581 ;; device number set in `tramp-handle-file-attributes'
1582 (defconst tramp-perl-file-attributes "\
1583 @stat = lstat($ARGV[0]);
1584 if (($stat[2] & 0170000) == 0120000)
1586 $type = readlink($ARGV[0]);
1587 $type = \"\\\"$type\\\"\";
1589 elsif (($stat[2] & 0170000) == 040000)
1597 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1598 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1600 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1605 $stat[8] >> 16 & 0xffff,
1607 $stat[9] >> 16 & 0xffff,
1609 $stat[10] >> 16 & 0xffff,
1613 $stat[1] >> 16 & 0xffff,
1616 "Perl script to produce output suitable for use with `file-attributes'
1617 on the remote file system.")
1619 (defconst tramp-perl-directory-files-and-attributes "\
1620 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1621 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1622 @list = readdir(DIR);
1626 for($i = 0; $i < $n; $i++)
1628 $filename = $list[$i];
1629 @stat = lstat($filename);
1630 if (($stat[2] & 0170000) == 0120000)
1632 $type = readlink($filename);
1633 $type = \"\\\"$type\\\"\";
1635 elsif (($stat[2] & 0170000) == 040000)
1643 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1644 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1646 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1652 $stat[8] >> 16 & 0xffff,
1654 $stat[9] >> 16 & 0xffff,
1656 $stat[10] >> 16 & 0xffff,
1660 $stat[1] >> 16 & 0xffff,
1662 $stat[0] >> 16 & 0xffff,
1666 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1669 ;; ;; These two use uu encoding.
1670 ;; (defvar tramp-perl-encode "%s -e'\
1671 ;; print qq(begin 644 xxx\n);
1674 ;; while (read(STDIN, $s, 45)) {
1675 ;; print pack(q(u), $s);
1680 ;; "Perl program to use for encoding a file.
1681 ;; Escape sequence %s is replaced with name of Perl binary.")
1683 ;; (defvar tramp-perl-decode "%s -ne '
1684 ;; print unpack q(u), $_;
1686 ;; "Perl program to use for decoding a file.
1687 ;; Escape sequence %s is replaced with name of Perl binary.")
1689 ;; These two use base64 encoding.
1690 (defvar tramp-perl-encode-with-module
1691 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1692 "Perl program to use for encoding a file.
1693 Escape sequence %s is replaced with name of Perl binary.
1694 This string is passed to `format', so percent characters need to be doubled.
1695 This implementation requires the MIME::Base64 Perl module to be installed
1696 on the remote host.")
1698 (defvar tramp-perl-decode-with-module
1699 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1700 "Perl program to use for decoding a file.
1701 Escape sequence %s is replaced with name of Perl binary.
1702 This string is passed to `format', so percent characters need to be doubled.
1703 This implementation requires the MIME::Base64 Perl module to be installed
1704 on the remote host.")
1706 (defvar tramp-perl-encode
1708 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1709 # Copyright (C) 2002 Free Software Foundation, Inc.
1714 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1715 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1720 # We read in chunks of 54 bytes, to generate output lines
1721 # of 72 chars (plus end of line)
1724 while (my $data = <STDIN>) {
1727 # Only for the last chunk, and only if did not fill the last three-byte packet
1729 my $mod = length($data) %% 3;
1730 $pad = q(=) x (3 - $mod) if $mod;
1733 # Not the fastest method, but it is simple: unpack to binary string, split
1734 # by groups of 6 bits and convert back from binary to byte; then map into
1735 # the translation table
1739 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1744 "Perl program to use for encoding a file.
1745 Escape sequence %s is replaced with name of Perl binary.
1746 This string is passed to `format', so percent characters need to be doubled.")
1748 (defvar tramp-perl-decode
1750 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1751 # Copyright (C) 2002 Free Software Foundation, Inc.
1756 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1757 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1760 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1764 # We are going to accumulate into $pending to accept any line length
1765 # (we do not check they are <= 76 chars as the RFC says)
1768 while (my $data = <STDIN>) {
1771 # If we find one or two =, we have reached the end and
1772 # any following data is to be discarded
1773 my $finished = $data =~ s/(==?).*/$1/;
1776 my $len = length($pending);
1777 my $chunk = substr($pending, 0, $len & ~3);
1778 $pending = substr($pending, $len & ~3 + 1);
1780 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1781 # split in 8-bit chunks and convert back to char.
1784 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1789 "Perl program to use for decoding a file.
1790 Escape sequence %s is replaced with name of Perl binary.
1791 This string is passed to `format', so percent characters need to be doubled.")
1793 ; These values conform to `file-attributes' from XEmacs 21.2.
1794 ; GNU Emacs and other tools not checked.
1795 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1797 (2 . "c") ; character device
1798 (3 . "m") ; multiplexed character device (v7)
1799 (4 . "d") ; directory
1800 (5 . "?") ; Named special file (XENIX)
1801 (6 . "b") ; block device
1802 (7 . "?") ; multiplexed block device (v7)
1803 (8 . "-") ; regular file
1804 (9 . "n") ; network special file (HP-UX)
1805 (10 . "l") ; symlink
1806 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1808 (13 . "D") ; door special (Solaris)
1809 (14 . "w")) ; whiteout (BSD)
1810 "A list of file types returned from the `stat' system call.
1811 This is used to map a mode number to a permission string.")
1813 (defvar tramp-dos-coding-system
1814 (if (and (fboundp 'coding-system-p)
1815 (funcall 'coding-system-p '(dos)))
1818 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1820 (defvar tramp-last-cmd nil
1821 "Internal Tramp variable recording the last command sent.
1822 This variable is buffer-local in every buffer.")
1823 (make-variable-buffer-local 'tramp-last-cmd)
1825 (defvar tramp-process-echoes nil
1826 "Whether to process echoes from the remote shell.")
1828 (defvar tramp-last-cmd-time nil
1829 "Internal Tramp variable recording the time when the last cmd was sent.
1830 This variable is buffer-local in every buffer.")
1831 (make-variable-buffer-local 'tramp-last-cmd-time)
1833 ;; This variable does not have the right value in XEmacs. What should
1834 ;; I use instead of find-operation-coding-system in XEmacs?
1835 (defvar tramp-feature-write-region-fix
1836 (when (fboundp 'find-operation-coding-system)
1837 (let ((file-coding-system-alist '(("test" emacs-mule))))
1838 (funcall (symbol-function 'find-operation-coding-system)
1839 'write-region 0 0 "" nil "test")))
1840 "Internal variable to say if `write-region' chooses the right coding.
1841 Older versions of Emacs chose the coding system for `write-region' based
1842 on the FILENAME argument, even if VISIT was a string.")
1844 ;; New handlers should be added here. The following operations can be
1845 ;; handled using the normal primitives: file-name-as-directory,
1846 ;; file-name-directory, file-name-nondirectory,
1847 ;; file-name-sans-versions, get-file-buffer.
1848 (defconst tramp-file-name-handler-alist
1850 (load . tramp-handle-load)
1851 (make-symbolic-link . tramp-handle-make-symbolic-link)
1852 (file-name-directory . tramp-handle-file-name-directory)
1853 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1854 (file-truename . tramp-handle-file-truename)
1855 (file-exists-p . tramp-handle-file-exists-p)
1856 (file-directory-p . tramp-handle-file-directory-p)
1857 (file-executable-p . tramp-handle-file-executable-p)
1858 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1859 (file-readable-p . tramp-handle-file-readable-p)
1860 (file-regular-p . tramp-handle-file-regular-p)
1861 (file-symlink-p . tramp-handle-file-symlink-p)
1862 (file-writable-p . tramp-handle-file-writable-p)
1863 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1864 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1865 (file-attributes . tramp-handle-file-attributes)
1866 (file-modes . tramp-handle-file-modes)
1867 (directory-files . tramp-handle-directory-files)
1868 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1869 (file-name-all-completions . tramp-handle-file-name-all-completions)
1870 (file-name-completion . tramp-handle-file-name-completion)
1871 (add-name-to-file . tramp-handle-add-name-to-file)
1872 (copy-file . tramp-handle-copy-file)
1873 (rename-file . tramp-handle-rename-file)
1874 (set-file-modes . tramp-handle-set-file-modes)
1875 (make-directory . tramp-handle-make-directory)
1876 (delete-directory . tramp-handle-delete-directory)
1877 (delete-file . tramp-handle-delete-file)
1878 (directory-file-name . tramp-handle-directory-file-name)
1879 (shell-command . tramp-handle-shell-command)
1880 (process-file . tramp-handle-process-file)
1881 (insert-directory . tramp-handle-insert-directory)
1882 (expand-file-name . tramp-handle-expand-file-name)
1883 (file-local-copy . tramp-handle-file-local-copy)
1884 (file-remote-p . tramp-handle-file-remote-p)
1885 (insert-file-contents . tramp-handle-insert-file-contents)
1886 (write-region . tramp-handle-write-region)
1887 (find-backup-file-name . tramp-handle-find-backup-file-name)
1888 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1889 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1890 (dired-compress-file . tramp-handle-dired-compress-file)
1891 (dired-call-process . tramp-handle-dired-call-process)
1892 (dired-recursive-delete-directory
1893 . tramp-handle-dired-recursive-delete-directory)
1894 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1895 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1896 "Alist of handler functions.
1897 Operations not mentioned here will be handled by the normal Emacs functions.")
1899 ;; Handlers for partial tramp file names. For GNU Emacs just
1900 ;; `file-name-all-completions' is needed. The other ones are necessary
1902 (defconst tramp-completion-file-name-handler-alist
1904 (file-name-directory . tramp-completion-handle-file-name-directory)
1905 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1906 (file-exists-p . tramp-completion-handle-file-exists-p)
1907 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1908 (file-name-completion . tramp-completion-handle-file-name-completion)
1909 (expand-file-name . tramp-completion-handle-expand-file-name))
1910 "Alist of completion handler functions.
1911 Used for file names matching `tramp-file-name-regexp'. Operations not
1912 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1913 normal Emacs functions.")
1915 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1916 (defvar tramp-foreign-file-name-handler-alist
1917 ;; (identity . tramp-sh-file-name-handler) should always be the last
1918 ;; entry, since `identity' always matches.
1919 '((identity . tramp-sh-file-name-handler))
1920 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1921 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1924 ;;; Internal functions which must come first.
1926 (defsubst tramp-message (level fmt-string &rest args)
1927 "Emit a message depending on verbosity level.
1928 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1929 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1930 Calls function `message' with FMT-STRING as control string and the remaining
1931 ARGS to actually emit the message (if applicable).
1933 This function expects to be called from the tramp buffer only!"
1934 (when (<= level tramp-verbose)
1935 (apply #'message (concat "tramp: " fmt-string) args)
1936 (when tramp-debug-buffer
1939 (tramp-get-debug-buffer
1940 tramp-current-multi-method tramp-current-method
1941 tramp-current-user tramp-current-host))
1942 (goto-char (point-max))
1945 (tramp-insert-with-face
1947 (concat "# " (apply #'format fmt-string args) "\n"))))))
1949 (defun tramp-message-for-buffer
1950 (multi-method method user host level fmt-string &rest args)
1951 "Like `tramp-message' but temporarily switches to the tramp buffer.
1952 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1953 remaining args passed to `tramp-message'."
1955 (set-buffer (tramp-get-buffer multi-method method user host))
1956 (apply 'tramp-message level fmt-string args)))
1958 (defsubst tramp-line-end-position nil
1959 "Return point at end of line.
1960 Calls `line-end-position' or `point-at-eol' if defined, else
1961 own implementation."
1963 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1964 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1965 (t (save-excursion (end-of-line) (point)))))
1967 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1968 "Parse a Tramp filename and make components available in the body.
1970 First arg FILENAME is evaluated and dissected into its components.
1971 Second arg VAR is a symbol. It is used as a variable name to hold
1972 the filename structure. It is also used as a prefix for the variables
1973 holding the components. For example, if VAR is the symbol `foo', then
1974 `foo' will be bound to the whole structure, `foo-multi-method' will
1975 be bound to the multi-method component, and so on for `foo-method',
1976 `foo-user', `foo-host', `foo-localname'.
1978 Remaining args are Lisp expressions to be evaluated (inside an implicit
1981 If VAR is nil, then we bind `v' to the structure and `multi-method',
1982 `method', `user', `host', `localname' to the components."
1983 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1984 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1985 (tramp-file-name-multi-method ,(or var 'v)))
1986 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1987 (tramp-file-name-method ,(or var 'v)))
1988 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1989 (tramp-file-name-user ,(or var 'v)))
1990 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1991 (tramp-file-name-host ,(or var 'v)))
1992 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1993 (tramp-file-name-localname ,(or var 'v))))
1996 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1997 ;; To be activated for debugging containing this macro
1998 ;; It works only when VAR is nil. Otherwise, it can be deactivated by
1999 ;; (put 'with-parsed-tramp-file-name 'edebug-form-spec 0)
2000 ;; I'm too stupid to write a precise SPEC for it.
2001 (put 'with-parsed-tramp-file-name 'edebug-form-spec t)
2003 (defmacro tramp-let-maybe (variable value &rest body)
2004 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2005 BODY is executed whether or not the variable is obsolete.
2006 The intent is to protect against `obsolete variable' warnings."
2007 `(if (get ',variable 'byte-obsolete-variable)
2009 (let ((,variable ,value))
2011 (put 'tramp-let-maybe 'lisp-indent-function 2)
2013 ;;; Config Manipulation Functions:
2015 (defun tramp-set-completion-function (method function-list)
2016 "Sets the list of completion functions for METHOD.
2017 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2018 The FUNCTION is intended to parse FILE according its syntax.
2019 It might be a predefined FUNCTION, or a user defined FUNCTION.
2020 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2021 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2022 and `tramp-parse-netrc'.
2026 (tramp-set-completion-function
2028 '((tramp-parse-sconfig \"/etc/ssh_config\")
2029 (tramp-parse-sconfig \"~/.ssh/config\")))"
2031 (let ((r function-list)
2033 (setq tramp-completion-function-alist
2034 (delete (assoc method tramp-completion-function-alist)
2035 tramp-completion-function-alist))
2038 ;; Remove double entries
2039 (when (member (car v) (cdr v))
2040 (setcdr v (delete (car v) (cdr v))))
2041 ;; Check for function and file
2042 (unless (and (functionp (nth 0 (car v)))
2043 (file-exists-p (nth 1 (car v))))
2044 (setq r (delete (car v) r)))
2048 (add-to-list 'tramp-completion-function-alist
2051 (defun tramp-get-completion-function (method)
2052 "Returns list of completion functions for METHOD.
2053 For definition of that list see `tramp-set-completion-function'."
2054 (cdr (assoc method tramp-completion-function-alist)))
2056 ;;; File Name Handler Functions:
2058 (defun tramp-handle-make-symbolic-link
2059 (filename linkname &optional ok-if-already-exists)
2060 "Like `make-symbolic-link' for tramp files.
2061 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2062 the symlink. If LINKNAME is a Tramp file, only the localname component is
2063 used as the target of the symlink.
2065 If LINKNAME is a Tramp file and the localname component is relative, then
2066 it is expanded first, before the localname component is taken. Note that
2067 this can give surprising results if the user/host for the source and
2068 target of the symlink differ."
2069 (with-parsed-tramp-file-name linkname l
2070 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2071 (cwd (file-name-directory l-localname)))
2074 (list "Making a symbolic link."
2075 "ln(1) does not exist on the remote host.")))
2077 ;; Do the 'confirm if exists' thing.
2078 (when (file-exists-p linkname)
2080 (if (or (null ok-if-already-exists) ; not allowed to exist
2081 (and (numberp ok-if-already-exists)
2084 "File %s already exists; make it a link anyway? "
2086 (signal 'file-already-exists (list "File already exists" l-localname))
2087 (delete-file linkname)))
2089 ;; If FILENAME is a Tramp name, use just the localname component.
2090 (when (tramp-tramp-file-p filename)
2091 (setq filename (tramp-file-name-localname
2092 (tramp-dissect-file-name
2093 (expand-file-name filename)))))
2095 ;; Right, they are on the same host, regardless of user, method, etc.
2096 ;; We now make the link on the remote machine. This will occur as the user
2097 ;; that FILENAME belongs to.
2099 (tramp-send-command-and-check
2100 l-multi-method l-method l-user l-host
2101 (format "cd %s && %s -sf %s %s"
2108 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2109 "Like `load' for tramp files. Not implemented!"
2110 (unless (file-name-absolute-p file)
2111 (error "Tramp cannot `load' files without absolute file name"))
2112 (with-parsed-tramp-file-name file nil
2114 (cond ((file-exists-p (concat file ".elc"))
2115 (setq file (concat file ".elc")))
2116 ((file-exists-p (concat file ".el"))
2117 (setq file (concat file ".el")))))
2119 ;; The first condition is always true for absolute file names.
2120 ;; Included for safety's sake.
2121 (unless (or (file-name-directory file)
2122 (string-match "\\.elc?\\'" file))
2123 (error "File `%s' does not include a `.el' or `.elc' suffix"
2126 (when (not (file-exists-p file))
2127 (error "Cannot load nonexistent file `%s'" file)))
2128 (if (not (file-exists-p file))
2131 (message "Loading %s..." file))
2132 (let ((local-copy (file-local-copy file)))
2133 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2134 (load local-copy noerror t t)
2135 (delete-file local-copy))
2137 (message "Loading %s...done" file))
2140 ;; Localname manipulation functions that grok TRAMP localnames...
2141 (defun tramp-handle-file-name-directory (file)
2142 "Like `file-name-directory' but aware of TRAMP files."
2143 ;; everything except the last filename thing is the directory
2144 (with-parsed-tramp-file-name file nil
2145 ;; For the following condition, two possibilities should be tried:
2146 ;; (1) (string= localname "")
2147 ;; (2) (or (string= localname "") (string= localname "/"))
2148 ;; The second variant fails when completing a "/" directory on
2149 ;; the remote host, that is a filename which looks like
2150 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2151 ;; We should do some investigation.
2152 (if (string= localname "")
2153 ;; For a filename like "/[foo]", we return "/". The `else'
2154 ;; case would return "/[foo]" unchanged. But if we do that,
2155 ;; then `file-expand-wildcards' ceases to work. It's not
2156 ;; quite clear to me what's the intuition that tells that this
2157 ;; behavior is the right behavior, but oh, well.
2159 ;; run the command on the localname portion only
2160 ;; CCC: This should take into account the remote machine type, no?
2161 ;; --daniel <daniel@danann.net>
2162 (tramp-make-tramp-file-name multi-method method user host
2163 ;; This will not recurse...
2164 (or (file-name-directory localname) "")))))
2166 (defun tramp-handle-file-name-nondirectory (file)
2167 "Like `file-name-nondirectory' but aware of TRAMP files."
2168 (with-parsed-tramp-file-name file nil
2169 (file-name-nondirectory localname)))
2171 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2172 "Like `file-truename' for tramp files."
2173 (with-parsed-tramp-file-name (expand-file-name filename) nil
2174 (let* ((steps (tramp-split-string localname "/"))
2175 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2176 (file-name-as-directory localname)))
2177 (is-dir (string= localname localnamedir))
2180 ;; Don't make the following value larger than necessary.
2181 ;; People expect an error message in a timely fashion when
2182 ;; something is wrong; otherwise they might think that Emacs
2183 ;; is hung. Of course, correctness has to come first.
2185 (result nil) ;result steps in reverse order
2187 (tramp-message-for-buffer
2188 multi-method method user host
2189 10 "Finding true name for `%s'" filename)
2190 (while (and steps (< numchase numchase-limit))
2191 (setq thisstep (pop steps))
2192 (tramp-message-for-buffer
2193 multi-method method user host
2195 (mapconcat 'identity
2196 (append '("") (reverse result) (list thisstep))
2198 (setq symlink-target
2199 (nth 0 (file-attributes
2200 (tramp-make-tramp-file-name
2201 multi-method method user host
2202 (mapconcat 'identity
2207 (cond ((string= "." thisstep)
2208 (tramp-message-for-buffer multi-method method user host
2209 10 "Ignoring step `.'"))
2210 ((string= ".." thisstep)
2211 (tramp-message-for-buffer multi-method method user host
2212 10 "Processing step `..'")
2214 ((stringp symlink-target)
2215 ;; It's a symlink, follow it.
2216 (tramp-message-for-buffer
2217 multi-method method user host
2218 10 "Follow symlink to %s" symlink-target)
2219 (setq numchase (1+ numchase))
2220 (when (file-name-absolute-p symlink-target)
2222 ;; If the symlink was absolute, we'll get a string like
2223 ;; "/user@host:/some/target"; extract the
2224 ;; "/some/target" part from it.
2225 (when (tramp-tramp-file-p symlink-target)
2226 (with-parsed-tramp-file-name symlink-target sym
2227 (unless (equal (list multi-method method user host)
2228 (list sym-multi-method sym-method
2230 (error "Symlink target `%s' on wrong host"
2232 (setq symlink-target localname)))
2234 (append (tramp-split-string symlink-target "/") steps)))
2237 (setq result (cons thisstep result)))))
2238 (when (>= numchase numchase-limit)
2239 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2240 (setq result (reverse result))
2241 ;; Combine list to form string.
2244 (mapconcat 'identity (cons "" result) "/")
2246 (when (and is-dir (or (string= "" result)
2247 (not (string= (substring result -1) "/"))))
2248 (setq result (concat result "/")))
2249 (tramp-message-for-buffer
2250 multi-method method user host
2251 10 "True name of `%s' is `%s'" filename result)
2252 (tramp-make-tramp-file-name
2253 multi-method method user host result))))
2257 (defun tramp-handle-file-exists-p (filename)
2258 "Like `file-exists-p' for tramp files."
2259 (with-parsed-tramp-file-name filename nil
2261 (zerop (tramp-send-command-and-check
2262 multi-method method user host
2264 (tramp-get-file-exists-command multi-method method user host)
2265 (tramp-shell-quote-argument localname)))))))
2267 ;; Devices must distinguish physical file systems. The device numbers
2268 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2269 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2270 ;; EFS use device number "-1". In order to be different, we use device number
2271 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2272 (defvar tramp-devices nil
2273 "Keeps virtual device numbers.")
2275 ;; CCC: This should check for an error condition and signal failure
2276 ;; when something goes wrong.
2277 ;; Daniel Pittman <daniel@danann.net>
2278 (defun tramp-handle-file-attributes (filename &optional id-format)
2279 "Like `file-attributes' for tramp files."
2280 (when (file-exists-p filename)
2281 ;; file exists, find out stuff
2282 (unless id-format (setq id-format 'integer))
2283 (with-parsed-tramp-file-name filename nil
2285 (tramp-convert-file-attributes
2286 multi-method method user host
2287 (if (tramp-get-remote-perl multi-method method user host)
2288 (tramp-handle-file-attributes-with-perl multi-method method user host
2289 localname id-format)
2290 (tramp-handle-file-attributes-with-ls multi-method method user host
2291 localname id-format)))))))
2293 (defun tramp-handle-file-attributes-with-ls
2294 (multi-method method user host localname &optional id-format)
2295 "Implement `file-attributes' for tramp files using the ls(1) command."
2297 res-inode res-filemodes res-numlinks
2298 res-uid res-gid res-size res-symlink-target)
2299 (tramp-message-for-buffer multi-method method user host 10
2300 "file attributes with ls: %s"
2301 (tramp-make-tramp-file-name
2302 multi-method method user host localname))
2304 multi-method method user host
2306 (tramp-get-ls-command multi-method method user host)
2307 (if (eq id-format 'integer) "-ildn" "-ild")
2308 (tramp-shell-quote-argument localname)))
2309 (tramp-wait-for-output)
2310 ;; parse `ls -l' output ...
2314 (read (current-buffer))
2315 (invalid-read-syntax
2316 (when (and (equal (cadr err)
2317 "Integer constant overflow in reader")
2319 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2321 (let* ((big (read (substring (caddr err) 0
2322 (match-beginning 1))))
2323 (small (read (match-string 1 (caddr err))))
2324 (twiddle (/ small 65536)))
2325 (cons (+ big twiddle)
2326 (- small (* twiddle 65536))))))))
2327 ;; ... file mode flags
2328 (setq res-filemodes (symbol-name (read (current-buffer))))
2330 (setq res-numlinks (read (current-buffer)))
2332 (setq res-uid (read (current-buffer)))
2333 (setq res-gid (read (current-buffer)))
2334 (when (eq id-format 'integer)
2335 (unless (numberp res-uid) (setq res-uid -1))
2336 (unless (numberp res-gid) (setq res-gid -1)))
2338 (setq res-size (read (current-buffer)))
2339 ;; From the file modes, figure out other stuff.
2340 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2341 (setq dirp (eq ?d (aref res-filemodes 0)))
2342 ;; if symlink, find out file name pointed to
2344 (search-forward "-> ")
2345 (setq res-symlink-target
2346 (buffer-substring (point)
2347 (tramp-line-end-position))))
2348 ;; return data gathered
2350 ;; 0. t for directory, string (name linked to) for symbolic
2352 (or dirp res-symlink-target nil)
2353 ;; 1. Number of links to file.
2359 ;; 4. Last access time, as a list of two integers. First
2360 ;; integer has high-order 16 bits of time, second has low 16
2362 ;; 5. Last modification time, likewise.
2363 ;; 6. Last status change time, likewise.
2364 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2365 ;; 7. Size in bytes (-1, if number is out of range).
2367 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2369 ;; 9. t iff file's gid would change if file were deleted and
2372 ;; 10. inode number.
2374 ;; 11. Device number. Will be replaced by a virtual device number.
2378 (defun tramp-handle-file-attributes-with-perl
2379 (multi-method method user host localname &optional id-format)
2380 "Implement `file-attributes' for tramp files using a Perl script."
2381 (tramp-message-for-buffer multi-method method user host 10
2382 "file attributes with perl: %s"
2383 (tramp-make-tramp-file-name
2384 multi-method method user host localname))
2385 (tramp-maybe-send-perl-script multi-method method user host
2386 tramp-perl-file-attributes
2387 "tramp_file_attributes")
2388 (tramp-send-command multi-method method user host
2389 (format "tramp_file_attributes %s %s"
2390 (tramp-shell-quote-argument localname) id-format))
2391 (tramp-wait-for-output)
2392 (read (current-buffer)))
2394 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2395 "Like `set-visited-file-modtime' for tramp files."
2396 (unless (buffer-file-name)
2397 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2400 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2401 (let ((f (buffer-file-name))
2403 (with-parsed-tramp-file-name f nil
2404 (let* ((attr (file-attributes f))
2405 ;; '(-1 65535) means file doesn't exists yet.
2406 (modtime (or (nth 5 attr) '(-1 65535))))
2407 (when (boundp 'last-coding-system-used)
2408 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2409 ;; We use '(0 0) as a don't-know value. See also
2410 ;; `tramp-handle-file-attributes-with-ls'.
2411 (if (not (equal modtime '(0 0)))
2412 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2415 multi-method method user host
2416 (format "%s -ild %s"
2417 (tramp-get-ls-command multi-method method user host)
2418 (tramp-shell-quote-argument localname)))
2419 (tramp-wait-for-output)
2420 (setq attr (buffer-substring (point)
2421 (progn (end-of-line) (point)))))
2422 (setq tramp-buffer-file-attributes attr))
2423 (when (boundp 'last-coding-system-used)
2424 (set 'last-coding-system-used coding-system-used))
2427 ;; CCC continue here
2429 ;; This function makes the same assumption as
2430 ;; `tramp-handle-set-visited-file-modtime'.
2431 (defun tramp-handle-verify-visited-file-modtime (buf)
2432 "Like `verify-visited-file-modtime' for tramp files.
2433 At the time `verify-visited-file-modtime' calls this function, we
2434 already know that the buffer is visiting a file and that
2435 `visited-file-modtime' does not return 0. Do not call this
2436 function directly, unless those two cases are already taken care
2438 (with-current-buffer buf
2439 ;; There is no file visiting the buffer, or the buffer has no
2440 ;; recorded last modification time.
2441 (if (or (not (buffer-file-name))
2442 (eq (visited-file-modtime) 0))
2444 (let ((f (buffer-file-name)))
2445 (with-parsed-tramp-file-name f nil
2446 (let* ((attr (file-attributes f))
2447 (modtime (nth 5 attr))
2448 (mt (visited-file-modtime)))
2451 ;; file exists, and has a known modtime.
2452 ((and attr (not (equal modtime '(0 0))))
2453 (< (abs (tramp-time-diff
2455 ;; For compatibility, deal with both the old
2456 ;; (HIGH . LOW) and the new (HIGH LOW)
2457 ;; return values of `visited-file-modtime'.
2459 (list (car mt) (cdr mt))
2462 ;; modtime has the don't know value.
2466 multi-method method user host
2467 (format "%s -ild %s"
2468 (tramp-get-ls-command multi-method method user host)
2469 (tramp-shell-quote-argument localname)))
2470 (tramp-wait-for-output)
2471 (setq attr (buffer-substring
2472 (point) (progn (end-of-line) (point)))))
2473 (equal tramp-buffer-file-attributes attr))
2474 ;; If file does not exist, say it is not modified
2475 ;; if and only if that agrees with the buffer's record.
2476 (t (equal mt '(-1 65535))))))))))
2478 (defun tramp-handle-set-file-modes (filename mode)
2479 "Like `set-file-modes' for tramp files."
2480 (with-parsed-tramp-file-name filename nil
2482 (unless (zerop (tramp-send-command-and-check
2483 multi-method method user host
2484 (format "chmod %s %s"
2485 (tramp-decimal-to-octal mode)
2486 (tramp-shell-quote-argument localname))))
2489 ;; FIXME: extract the proper text from chmod's stderr.
2490 "error while changing file's mode"
2493 ;; Simple functions using the `test' command.
2495 (defun tramp-handle-file-executable-p (filename)
2496 "Like `file-executable-p' for tramp files."
2497 (with-parsed-tramp-file-name filename nil
2498 (zerop (tramp-run-test "-x" filename))))
2500 (defun tramp-handle-file-readable-p (filename)
2501 "Like `file-readable-p' for tramp files."
2502 (with-parsed-tramp-file-name filename nil
2503 (zerop (tramp-run-test "-r" filename))))
2505 (defun tramp-handle-file-accessible-directory-p (filename)
2506 "Like `file-accessible-directory-p' for tramp files."
2507 (with-parsed-tramp-file-name filename nil
2508 (and (zerop (tramp-run-test "-d" filename))
2509 (zerop (tramp-run-test "-r" filename))
2510 (zerop (tramp-run-test "-x" filename)))))
2512 ;; When the remote shell is started, it looks for a shell which groks
2513 ;; tilde expansion. Here, we assume that all shells which grok tilde
2514 ;; expansion will also provide a `test' command which groks `-nt' (for
2515 ;; newer than). If this breaks, tell me about it and I'll try to do
2516 ;; something smarter about it.
2517 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2518 "Like `file-newer-than-file-p' for tramp files."
2519 (cond ((not (file-exists-p file1))
2521 ((not (file-exists-p file2))
2523 ;; We are sure both files exist at this point.
2526 ;; We try to get the mtime of both files. If they are not
2527 ;; equal to the "dont-know" value, then we subtract the times
2528 ;; and obtain the result.
2529 (let ((fa1 (file-attributes file1))
2530 (fa2 (file-attributes file2)))
2531 (if (and (not (equal (nth 5 fa1) '(0 0)))
2532 (not (equal (nth 5 fa2) '(0 0))))
2533 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2534 ;; If one of them is the dont-know value, then we can
2535 ;; still try to run a shell command on the remote host.
2536 ;; However, this only works if both files are Tramp
2537 ;; files and both have the same method, same user, same
2539 (unless (and (tramp-tramp-file-p file1)
2540 (tramp-tramp-file-p file2))
2544 "Cannot check if Tramp file is newer than non-Tramp file"
2546 (with-parsed-tramp-file-name file1 v1
2547 (with-parsed-tramp-file-name file2 v2
2548 (unless (and (equal v1-multi-method v2-multi-method)
2549 (equal v1-method v2-method)
2550 (equal v1-user v2-user)
2551 (equal v1-host v2-host))
2553 (list "Files must have same method, user, host"
2555 (unless (and (tramp-tramp-file-p file1)
2556 (tramp-tramp-file-p file2))
2558 (list "Files must be tramp files on same host"
2560 (if (tramp-get-test-groks-nt
2561 v1-multi-method v1-method v1-user v1-host)
2562 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2563 (zerop (tramp-run-test2
2564 "tramp_test_nt" file1 file2)))))))))))
2566 ;; Functions implemented using the basic functions above.
2568 (defun tramp-handle-file-modes (filename)
2569 "Like `file-modes' for tramp files."
2570 (with-parsed-tramp-file-name filename nil
2571 (when (file-exists-p filename)
2572 (tramp-mode-string-to-int
2573 (nth 8 (file-attributes filename))))))
2575 (defun tramp-handle-file-directory-p (filename)
2576 "Like `file-directory-p' for tramp files."
2577 ;; Care must be taken that this function returns `t' for symlinks
2578 ;; pointing to directories. Surely the most obvious implementation
2579 ;; would be `test -d', but that returns false for such symlinks.
2580 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2581 ;; I now think he's right. So we could be using `test -d', couldn't
2584 ;; Alternatives: `cd %s', `test -d %s'
2585 (with-parsed-tramp-file-name filename nil
2588 (tramp-send-command-and-check
2589 multi-method method user host
2590 (format "test -d %s"
2591 (tramp-shell-quote-argument localname))
2592 t))))) ;run command in subshell
2594 (defun tramp-handle-file-regular-p (filename)
2595 "Like `file-regular-p' for tramp files."
2596 (with-parsed-tramp-file-name filename nil
2597 (and (file-exists-p filename)
2598 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2600 (defun tramp-handle-file-symlink-p (filename)
2601 "Like `file-symlink-p' for tramp files."
2602 (with-parsed-tramp-file-name filename nil
2603 (let ((x (car (file-attributes filename))))
2605 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2606 ;; might do weird things.
2607 (if (file-name-absolute-p x)
2608 (tramp-make-tramp-file-name
2609 multi-method method user host x)
2612 (defun tramp-handle-file-writable-p (filename)
2613 "Like `file-writable-p' for tramp files."
2614 (with-parsed-tramp-file-name filename nil
2615 (if (file-exists-p filename)
2616 ;; Existing files must be writable.
2617 (zerop (tramp-run-test "-w" filename))
2618 ;; If file doesn't exist, check if directory is writable.
2619 (and (zerop (tramp-run-test
2620 "-d" (file-name-directory filename)))
2621 (zerop (tramp-run-test
2622 "-w" (file-name-directory filename)))))))
2624 (defun tramp-handle-file-ownership-preserved-p (filename)
2625 "Like `file-ownership-preserved-p' for tramp files."
2626 (with-parsed-tramp-file-name filename nil
2627 (or (not (file-exists-p filename))
2628 ;; Existing files must be writable.
2629 (zerop (tramp-run-test "-O" filename)))))
2631 ;; Other file name ops.
2633 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2634 ;; (defun tramp-handle-directory-file-name (directory)
2635 ;; "Like `directory-file-name' for tramp files."
2636 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2637 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2638 ;; (substring directory 0 (- (length directory) 1))
2641 ;; ;; Philippe Troin <phil@fifi.org>
2642 ;; (defun tramp-handle-directory-file-name (directory)
2643 ;; "Like `directory-file-name' for tramp files."
2644 ;; (with-parsed-tramp-file-name directory nil
2645 ;; (let ((directory-length-1 (1- (length directory))))
2647 ;; (if (and (eq (aref directory directory-length-1) ?/)
2648 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2649 ;; (/= (match-end 0) directory-length-1))
2650 ;; (substring directory 0 directory-length-1)
2653 (defun tramp-handle-directory-file-name (directory)
2654 "Like `directory-file-name' for tramp files."
2655 ;; If localname component of filename is "/", leave it unchanged.
2656 ;; Otherwise, remove any trailing slash from localname component.
2657 ;; Method, host, etc, are unchanged. Does it make sense to try
2658 ;; to avoid parsing the filename?
2659 (with-parsed-tramp-file-name directory nil
2660 (if (and (not (zerop (length localname)))
2661 (eq (aref localname (1- (length localname))) ?/)
2662 (not (string= localname "/")))
2663 (substring directory 0 -1)
2666 ;; Directory listings.
2668 (defun tramp-handle-directory-files (directory
2669 &optional full match nosort files-only)
2670 "Like `directory-files' for tramp files."
2671 (with-parsed-tramp-file-name directory nil
2674 (tramp-barf-unless-okay
2675 multi-method method user host
2676 (concat "cd " (tramp-shell-quote-argument localname))
2679 "tramp-handle-directory-files: couldn't `cd %s'"
2680 (tramp-shell-quote-argument localname))
2682 multi-method method user host
2683 (concat (tramp-get-ls-command multi-method method user host)
2685 (tramp-wait-for-output)
2686 (goto-char (point-max))
2687 (while (zerop (forward-line -1))
2688 (setq x (buffer-substring (point)
2689 (tramp-line-end-position)))
2690 (when (or (not match) (string-match match x))
2692 (push (concat (file-name-as-directory directory)
2696 (tramp-send-command multi-method method user host "cd")
2697 (tramp-wait-for-output)
2698 ;; Remove non-files or non-directories if necessary. Using
2699 ;; the remote shell for this would probably be way faster.
2700 ;; Maybe something could be adapted from
2701 ;; tramp-handle-file-name-all-completions.
2703 (let ((temp (nreverse result))
2706 (if (equal files-only t)
2709 (setq item (pop temp))
2710 (when (file-regular-p item)
2711 (push item result)))
2714 (setq item (pop temp))
2715 (when (file-directory-p item)
2716 (push item result)))))))
2719 (defun tramp-handle-directory-files-and-attributes
2720 (directory &optional full match nosort id-format)
2721 "Like `directory-files-and-attributes' for tramp files."
2722 (when (tramp-handle-file-exists-p directory)
2724 (setq directory (tramp-handle-expand-file-name directory))
2725 (with-parsed-tramp-file-name directory nil
2726 (tramp-maybe-send-perl-script multi-method method user host
2727 tramp-perl-directory-files-and-attributes
2728 "tramp_directory_files_and_attributes")
2729 (tramp-send-command multi-method method user host
2730 (format "tramp_directory_files_and_attributes %s %s"
2731 (tramp-shell-quote-argument localname)
2732 (or id-format 'integer)))
2733 (tramp-wait-for-output)
2734 (let* ((root (cons nil (let ((object (read (current-buffer))))
2735 (when (stringp object)
2740 (if (and match (not (string-match match (caadr cell))))
2742 (setcdr cell (cddr cell))
2744 (setq cell (cdr cell))
2745 (let ((l (car cell)))
2746 (tramp-convert-file-attributes multi-method method user host
2748 ;; If FULL, make file name absolute
2749 (when full (setcar l (concat directory "/" (car l)))))))
2752 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2754 ;; This function should return "foo/" for directories and "bar" for
2755 ;; files. We use `ls -ad' to get a list of files (including
2756 ;; directories), and `find . -type d \! -name . -prune' to get a list
2758 (defun tramp-handle-file-name-all-completions (filename directory)
2759 "Like `file-name-all-completions' for tramp files."
2760 (with-parsed-tramp-file-name directory nil
2761 (unless (save-match-data (string-match "/" filename))
2762 (let* ((nowild tramp-completion-without-shell-p)
2765 (tramp-barf-unless-okay
2766 multi-method method user host
2767 (format "cd %s" (tramp-shell-quote-argument localname))
2769 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2770 (tramp-shell-quote-argument localname))
2772 ;; Get a list of directories and files, including reliably
2773 ;; tagging the directories with a trailing '/'. Because I
2774 ;; rock. --daniel@danann.net
2776 multi-method method user host
2777 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2778 "if test -d \"$f\" 2>/dev/null; "
2779 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2780 (tramp-get-ls-command multi-method method user host)
2781 (if (or nowild (zerop (length filename)))
2784 (tramp-shell-quote-argument filename)))))
2786 ;; Now grab the output.
2787 (tramp-wait-for-output)
2788 (goto-char (point-max))
2789 (while (zerop (forward-line -1))
2790 (push (buffer-substring (point)
2791 (tramp-line-end-position))
2794 (tramp-send-command multi-method method user host "cd")
2795 (tramp-wait-for-output)
2799 (all-completions filename (mapcar 'list result))
2803 ;; The following isn't needed for Emacs 20 but for 19.34?
2804 (defun tramp-handle-file-name-completion (filename directory)
2805 "Like `file-name-completion' for tramp files."
2806 (unless (tramp-tramp-file-p directory)
2808 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2810 (with-parsed-tramp-file-name directory nil
2813 (mapcar (lambda (x) (cons x nil))
2814 (file-name-all-completions filename directory)))))
2818 (defun tramp-handle-add-name-to-file
2819 (filename newname &optional ok-if-already-exists)
2820 "Like `add-name-to-file' for tramp files."
2821 (with-parsed-tramp-file-name filename v1
2822 (with-parsed-tramp-file-name newname v2
2823 (let ((ln (when v1 (tramp-get-remote-ln
2824 v1-multi-method v1-method v1-user v1-host))))
2825 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2826 (equal v1-multi-method v2-multi-method)
2827 (equal v1-method v2-method)
2828 (equal v1-user v2-user)
2829 (equal v1-host v2-host))
2830 (error "add-name-to-file: %s"
2831 "only implemented for same method, same user, same host"))
2832 (when (and (not ok-if-already-exists)
2833 (file-exists-p newname)
2834 (not (numberp ok-if-already-exists))
2837 "File %s already exists; make it a new name anyway? "
2839 (error "add-name-to-file: file %s already exists" newname))
2840 (tramp-barf-unless-okay
2841 v1-multi-method v1-method v1-user v1-host
2842 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2843 (tramp-shell-quote-argument v2-localname))
2845 "error with add-name-to-file, see buffer `%s' for details"
2848 (defun tramp-handle-copy-file
2849 (filename newname &optional ok-if-already-exists keep-date)
2850 "Like `copy-file' for tramp files."
2851 ;; Check if both files are local -- invoke normal copy-file.
2852 ;; Otherwise, use tramp from local system.
2853 (setq filename (expand-file-name filename))
2854 (setq newname (expand-file-name newname))
2855 ;; At least one file a tramp file?
2856 (if (or (tramp-tramp-file-p filename)
2857 (tramp-tramp-file-p newname))
2858 (tramp-do-copy-or-rename-file
2859 'copy filename newname ok-if-already-exists keep-date)
2860 (tramp-run-real-handler
2862 (list filename newname ok-if-already-exists keep-date))))
2864 (defun tramp-handle-rename-file
2865 (filename newname &optional ok-if-already-exists)
2866 "Like `rename-file' for tramp files."
2867 ;; Check if both files are local -- invoke normal rename-file.
2868 ;; Otherwise, use tramp from local system.
2869 (setq filename (expand-file-name filename))
2870 (setq newname (expand-file-name newname))
2871 ;; At least one file a tramp file?
2872 (if (or (tramp-tramp-file-p filename)
2873 (tramp-tramp-file-p newname))
2874 (tramp-do-copy-or-rename-file
2875 'rename filename newname ok-if-already-exists)
2876 (tramp-run-real-handler 'rename-file
2877 (list filename newname ok-if-already-exists))))
2879 (defun tramp-do-copy-or-rename-file
2880 (op filename newname &optional ok-if-already-exists keep-date)
2881 "Copy or rename a remote file.
2882 OP must be `copy' or `rename' and indicates the operation to perform.
2883 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2884 the new file (for copy) or the new name of the file (for rename).
2885 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2886 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2889 This function is invoked by `tramp-handle-copy-file' and
2890 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2891 and `rename'. FILENAME and NEWNAME must be absolute file names."
2892 (unless (memq op '(copy rename))
2893 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2894 (unless ok-if-already-exists
2895 (when (file-exists-p newname)
2896 (signal 'file-already-exists
2898 (let ((t1 (tramp-tramp-file-p filename))
2899 (t2 (tramp-tramp-file-p newname))
2900 v1-multi-method v1-method v1-user v1-host v1-localname
2901 v2-multi-method v2-method v2-user v2-host v2-localname)
2903 ;; Check which ones of source and target are Tramp files.
2904 ;; We cannot invoke `with-parsed-tramp-file-name';
2905 ;; it fails if the file isn't a Tramp file name.
2907 (with-parsed-tramp-file-name filename l
2908 (setq v1-multi-method l-multi-method
2912 v1-localname l-localname))
2913 (setq v1-localname filename))
2915 (with-parsed-tramp-file-name newname l
2916 (setq v2-multi-method l-multi-method
2920 v2-localname l-localname))
2921 (setq v2-localname newname))
2924 ;; Both are Tramp files.
2927 ;; Shortcut: if method, host, user are the same for both
2928 ;; files, we invoke `cp' or `mv' on the remote host
2930 ((and (equal v1-multi-method v2-multi-method)
2931 (equal v1-method v2-method)
2932 (equal v1-user v2-user)
2933 (equal v1-host v2-host))
2934 (tramp-do-copy-or-rename-file-directly
2935 op v1-multi-method v1-method v1-user v1-host
2936 v1-localname v2-localname keep-date))
2937 ;; If both source and target are Tramp files,
2938 ;; both are using the same copy-program, then we
2939 ;; can invoke rcp directly. Note that
2940 ;; default-directory should point to a local
2941 ;; directory if we want to invoke rcp.
2942 ((and (not v1-multi-method)
2943 (not v2-multi-method)
2944 (equal v1-method v2-method)
2945 (tramp-method-out-of-band-p
2946 v1-multi-method v1-method v1-user v1-host)
2947 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2948 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2949 (tramp-do-copy-or-rename-file-out-of-band
2950 op filename newname keep-date))
2951 ;; No shortcut was possible. So we copy the
2952 ;; file first. If the operation was `rename', we go
2953 ;; back and delete the original file (if the copy was
2954 ;; successful). The approach is simple-minded: we
2955 ;; create a new buffer, insert the contents of the
2956 ;; source file into it, then write out the buffer to
2957 ;; the target file. The advantage is that it doesn't
2958 ;; matter which filename handlers are used for the
2959 ;; source and target file.
2961 (tramp-do-copy-or-rename-file-via-buffer
2962 op filename newname keep-date))))
2964 ;; One file is a Tramp file, the other one is local.
2966 ;; If the Tramp file has an out-of-band method, the corresponding
2967 ;; copy-program can be invoked.
2968 (if (and (not v1-multi-method)
2969 (not v2-multi-method)
2970 (or (tramp-method-out-of-band-p
2971 v1-multi-method v1-method v1-user v1-host)
2972 (tramp-method-out-of-band-p
2973 v2-multi-method v2-method v2-user v2-host)))
2974 (tramp-do-copy-or-rename-file-out-of-band
2975 op filename newname keep-date)
2976 ;; Use the generic method via a Tramp buffer.
2977 (tramp-do-copy-or-rename-file-via-buffer
2978 op filename newname keep-date)))
2981 ;; One of them must be a Tramp file.
2982 (error "Tramp implementation says this cannot happen")))))
2984 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2985 "Use an Emacs buffer to copy or rename a file.
2986 First arg OP is either `copy' or `rename' and indicates the operation.
2987 FILENAME is the source file, NEWNAME the target file.
2988 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2989 (let ((trampbuf (get-buffer-create "*tramp output*"))
2990 (modtime (nth 5 (file-attributes filename))))
2991 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
2993 1 (concat "Warning: cannot preserve file time stamp"
2994 " with inline copying across machines")))
2996 (set-buffer trampbuf) (erase-buffer)
2997 (insert-file-contents-literally filename)
2998 ;; We don't want the target file to be compressed, so we let-bind
2999 ;; `jka-compr-inhibit' to t.
3000 (let ((coding-system-for-write 'binary)
3001 (jka-compr-inhibit t))
3002 (write-region (point-min) (point-max) newname))
3003 ;; KEEP-DATE handling.
3005 (when (and (not (null modtime))
3006 (not (equal modtime '(0 0))))
3007 (tramp-touch newname modtime)))
3009 (set-file-modes newname (file-modes filename)))
3010 ;; If the operation was `rename', delete the original file.
3011 (unless (eq op 'copy)
3012 (delete-file filename))))
3014 (defun tramp-do-copy-or-rename-file-directly
3015 (op multi-method method user host localname1 localname2 keep-date)
3016 "Invokes `cp' or `mv' on the remote system.
3017 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3018 respectively. METHOD, USER, and HOST specify the connection.
3019 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3020 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3021 ;; CCC: What happens to the timestamp when renaming?
3022 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3023 ((eq op 'copy) "cp -f")
3024 ((eq op 'rename) "mv -f")
3026 "Unknown operation `%s', must be `copy' or `rename'"
3030 multi-method method user host
3033 (tramp-shell-quote-argument localname1)
3034 (tramp-shell-quote-argument localname2)))
3035 (tramp-wait-for-output)
3036 (goto-char (point-min))
3039 (and (eq op 'copy) keep-date
3040 ;; Mask cp -f error.
3041 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3042 (zerop (tramp-send-command-and-check
3043 multi-method method user host nil nil)))
3044 (pop-to-buffer (current-buffer))
3046 (format "Copying directly failed, see buffer `%s' for details."
3049 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3051 (unless (or (eq op 'rename) keep-date)
3053 (tramp-make-tramp-file-name multi-method method user host localname2)
3055 (tramp-make-tramp-file-name
3056 multi-method method user host localname1))))))
3058 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3059 "Invoke rcp program to copy.
3060 One of FILENAME and NEWNAME must be a Tramp name, the other must
3061 be a local filename. The method used must be an out-of-band method."
3062 (let ((t1 (tramp-tramp-file-p filename))
3063 (t2 (tramp-tramp-file-p newname))
3064 v1-multi-method v1-method v1-user v1-host v1-localname
3065 v2-multi-method v2-method v2-user v2-host v2-localname
3066 multi-method method user host copy-program copy-args
3067 source target trampbuf)
3069 ;; Check which ones of source and target are Tramp files.
3070 ;; We cannot invoke `with-parsed-tramp-file-name';
3071 ;; it fails if the file isn't a Tramp file name.
3073 (with-parsed-tramp-file-name filename l
3074 (setq v1-multi-method l-multi-method
3078 v1-localname l-localname
3079 multi-method l-multi-method
3080 method (tramp-find-method
3081 v1-multi-method v1-method v1-user v1-host)
3084 copy-program (tramp-get-method-parameter
3085 v1-multi-method method
3086 v1-user v1-host 'tramp-copy-program)
3087 copy-args (tramp-get-method-parameter
3088 v1-multi-method method
3089 v1-user v1-host 'tramp-copy-args)))
3090 (setq v1-localname filename))
3093 (with-parsed-tramp-file-name newname l
3094 (setq v2-multi-method l-multi-method
3098 v2-localname l-localname
3099 multi-method l-multi-method
3100 method (tramp-find-method
3101 v2-multi-method v2-method v2-user v2-host)
3104 copy-program (tramp-get-method-parameter
3105 v2-multi-method method
3106 v2-user v2-host 'tramp-copy-program)
3107 copy-args (tramp-get-method-parameter
3108 v2-multi-method method
3109 v2-user v2-host 'tramp-copy-args)))
3110 (setq v2-localname newname))
3112 ;; The following should be changed. We need a more general
3113 ;; mechanism to parse extra host args.
3115 (setq source v1-localname)
3116 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3117 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3118 (setq v1-host (match-string 1 v1-host)))
3120 (tramp-make-copy-program-file-name
3122 (tramp-shell-quote-argument v1-localname))))
3125 (setq target v2-localname)
3126 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3127 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3128 (setq v2-host (match-string 1 v2-host)))
3130 (tramp-make-copy-program-file-name
3132 (tramp-shell-quote-argument v2-localname))))
3134 ;; Handle keep-date argument
3138 (cons (tramp-get-method-parameter
3139 v1-multi-method method
3140 v1-user v1-host 'tramp-copy-keep-date-arg)
3143 (cons (tramp-get-method-parameter
3144 v2-multi-method method
3145 v2-user v2-host 'tramp-copy-keep-date-arg)
3148 (setq copy-args (append copy-args (list source target))
3149 trampbuf (generate-new-buffer
3150 (tramp-buffer-name multi-method method user host)))
3152 ;; Use an asynchronous process. By this, password can be handled.
3155 ;; Check for program.
3156 (when (and (fboundp 'executable-find)
3157 (not (executable-find copy-program)))
3158 (error "Cannot find copy program: %s" copy-program))
3160 (set-buffer trampbuf)
3161 (setq tramp-current-multi-method multi-method
3162 tramp-current-method method
3163 tramp-current-user user
3164 tramp-current-host host)
3165 (message "Transferring %s to %s..." filename newname)
3167 ;; Use rcp-like program for file transfer.
3168 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3169 copy-program copy-args)))
3170 (tramp-set-process-query-on-exit-flag p nil)
3171 (tramp-process-actions p multi-method method user host
3172 tramp-actions-copy-out-of-band))
3173 (kill-buffer trampbuf)
3174 (message "Transferring %s to %s...done" filename newname)
3178 (set-file-modes newname (file-modes filename))))
3180 ;; If the operation was `rename', delete the original file.
3181 (unless (eq op 'copy)
3182 (delete-file filename))))
3185 (defun tramp-handle-make-directory (dir &optional parents)
3186 "Like `make-directory' for tramp files."
3187 (setq dir (expand-file-name dir))
3188 (with-parsed-tramp-file-name dir nil
3190 (tramp-barf-unless-okay
3191 multi-method method user host
3193 (if parents "mkdir -p" "mkdir")
3194 (tramp-shell-quote-argument localname))
3196 "Couldn't make directory %s" dir))))
3198 ;; CCC error checking?
3199 (defun tramp-handle-delete-directory (directory)
3200 "Like `delete-directory' for tramp files."
3201 (setq directory (expand-file-name directory))
3202 (with-parsed-tramp-file-name directory nil
3205 multi-method method user host
3206 (format "rmdir %s ; echo ok"
3207 (tramp-shell-quote-argument localname)))
3208 (tramp-wait-for-output))))
3210 (defun tramp-handle-delete-file (filename)
3211 "Like `delete-file' for tramp files."
3212 (setq filename (expand-file-name filename))
3213 (with-parsed-tramp-file-name filename nil
3215 (unless (zerop (tramp-send-command-and-check
3216 multi-method method user host
3218 (tramp-shell-quote-argument localname))))
3219 (signal 'file-error "Couldn't delete Tramp file")))))
3223 ;; CCC: This does not seem to be enough. Something dies when
3224 ;; we try and delete two directories under TRAMP :/
3225 (defun tramp-handle-dired-recursive-delete-directory (filename)
3226 "Recursively delete the directory given.
3227 This is like `dired-recursive-delete-directory' for tramp files."
3228 (with-parsed-tramp-file-name filename nil
3229 ;; run a shell command 'rm -r <localname>'
3230 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3231 (or (file-exists-p filename)
3234 (list "Removing old file name" "no such directory" filename)))
3235 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3236 (tramp-send-command multi-method method user host
3237 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3238 ;; Wait for the remote system to return to us...
3239 ;; This might take a while, allow it plenty of time.
3240 (tramp-wait-for-output 120)
3241 ;; Make sure that it worked...
3242 (and (file-exists-p filename)
3243 (error "Failed to recursively delete %s" filename))))
3245 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3246 "Like `dired-call-process' for tramp files."
3247 (with-parsed-tramp-file-name default-directory nil
3249 (tramp-barf-unless-okay
3250 multi-method method user host
3251 (format "cd %s" (tramp-shell-quote-argument localname))
3253 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3254 (tramp-shell-quote-argument localname))
3256 multi-method method user host
3257 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3258 (tramp-wait-for-output))
3260 ;; We cannot use `insert-buffer' because the tramp buffer
3261 ;; changes its contents before insertion due to calling
3262 ;; `expand-file' and alike.
3264 (with-current-buffer
3265 (tramp-get-buffer multi-method method user host)
3269 (tramp-send-command-and-check multi-method method user host nil)
3270 (tramp-send-command multi-method method user host "cd")
3271 (tramp-wait-for-output)))))
3273 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3274 "Like `dired-compress-file' for tramp files."
3275 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3276 ;; Code stolen mainly from dired-aux.el.
3277 (with-parsed-tramp-file-name file nil
3280 (if (not (featurep 'xemacs))
3282 (symbol-value 'dired-compress-file-suffixes)
3283 ;; XEmacs has `dired-compression-method-alist', which is
3284 ;; transformed into `dired-compress-file-suffixes' structure.
3287 (list (concat (regexp-quote (nth 1 x)) "\\'")
3289 (mapconcat 'identity (nth 3 x) " ")))
3290 (symbol-value 'dired-compression-method-alist))))
3292 ;; See if any suffix rule matches this file name.
3294 (let (case-fold-search)
3295 (if (string-match (car (car suffixes)) localname)
3296 (setq suffix (car suffixes) suffixes nil))
3297 (setq suffixes (cdr suffixes))))
3299 (cond ((file-symlink-p file)
3301 ((and suffix (nth 2 suffix))
3302 ;; We found an uncompression rule.
3303 (message "Uncompressing %s..." file)
3304 (when (zerop (tramp-send-command-and-check
3305 multi-method method user host
3306 (concat (nth 2 suffix) " " localname)))
3307 (message "Uncompressing %s...done" file)
3308 ;; `dired-remove-file' is not defined in XEmacs
3309 (funcall (symbol-function 'dired-remove-file) file)
3310 (string-match (car suffix) file)
3311 (concat (substring file 0 (match-beginning 0)))))
3313 ;; We don't recognize the file as compressed, so compress it.
3315 (message "Compressing %s..." file)
3316 (when (zerop (tramp-send-command-and-check
3317 multi-method method user host
3318 (concat "gzip -f " localname)))
3319 (message "Compressing %s...done" file)
3320 ;; `dired-remove-file' is not defined in XEmacs
3321 (funcall (symbol-function 'dired-remove-file) file)
3322 (cond ((file-exists-p (concat file ".gz"))
3323 (concat file ".gz"))
3324 ((file-exists-p (concat file ".z"))
3328 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3329 ;; not sure at all that this is the right way to do it, but let's hope
3330 ;; it works for now, and wait for a guru to point out the Right Way to
3332 ;;(eval-when-compile
3333 ;; (unless (fboundp 'dired-insert-set-properties)
3334 ;; (fset 'dired-insert-set-properties 'ignore)))
3335 ;; Gerd suggests this:
3336 (eval-when-compile (require 'dired))
3337 ;; Note that dired is required at run-time, too, when it is needed.
3338 ;; It is only needed on XEmacs for the function
3339 ;; `dired-insert-set-properties'.
3341 (defun tramp-handle-insert-directory
3342 (filename switches &optional wildcard full-directory-p)
3343 "Like `insert-directory' for tramp files."
3344 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3345 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3346 (tramp-run-real-handler 'insert-directory
3347 (list filename switches wildcard full-directory-p))
3348 ;; For the moment, we assume that the remote "ls" program does not
3349 ;; grok "--dired". In the future, we should detect this on
3350 ;; connection setup.
3351 (when (string-match "^--dired\\s-+" switches)
3352 (setq switches (replace-match "" nil t switches)))
3353 (setq filename (expand-file-name filename))
3354 (with-parsed-tramp-file-name filename nil
3355 (tramp-message-for-buffer
3356 multi-method method user host 10
3357 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3358 switches filename (if wildcard "yes" "no")
3359 (if full-directory-p "yes" "no"))
3361 (setq wildcard (file-name-nondirectory localname))
3362 (setq localname (file-name-directory localname)))
3363 (when (listp switches)
3364 (setq switches (mapconcat 'identity switches " ")))
3365 (unless full-directory-p
3366 (setq switches (concat "-d " switches)))
3368 (setq switches (concat switches " " wildcard)))
3370 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3371 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3372 (if full-directory-p
3374 multi-method method user host
3376 (tramp-get-ls-command multi-method method user host)
3380 (tramp-shell-quote-argument (concat localname ".")))))
3381 (tramp-barf-unless-okay
3382 multi-method method user host
3383 (format "cd %s" (tramp-shell-quote-argument
3384 (file-name-directory localname)))
3387 (tramp-shell-quote-argument (file-name-directory localname)))
3389 multi-method method user host
3391 (tramp-get-ls-command multi-method method user host)
3395 (if (zerop (length (file-name-nondirectory localname)))
3397 (tramp-shell-quote-argument
3398 (file-name-nondirectory localname)))))))
3399 (sit-for 1) ;needed for rsh but not ssh?
3400 (tramp-wait-for-output))
3401 ;; The following let-binding is used by code that's commented
3402 ;; out. Let's leave the let-binding in for a while to see
3403 ;; that the commented-out code is really not needed. Commenting-out
3404 ;; happened on 2003-03-13.
3405 (let ((old-pos (point)))
3406 ;; We cannot use `insert-buffer' because the tramp buffer
3407 ;; changes its contents before insertion due to calling
3408 ;; `expand-file' and alike.
3410 (with-current-buffer
3411 (tramp-get-buffer multi-method method user host)
3413 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3414 ;; that doesn't exist on Emacs, so we use this workaround instead.
3415 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3416 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3417 ;; (let ((zmacs-region-stays t))
3418 ;; (exchange-point-and-mark))
3420 (tramp-send-command multi-method method user host "cd")
3421 (tramp-wait-for-output))
3422 ;; For the time being, the XEmacs kludge is commented out.
3423 ;; Please test it on various XEmacs versions to see if it works.
3424 ;; ;; Another XEmacs specialty follows. What's the right way to do
3426 ;; (when (and (featurep 'xemacs)
3427 ;; (eq major-mode 'dired-mode))
3430 ;; (dired-insert-set-properties old-pos (point))))
3433 ;; Continuation of kluge to pacify byte-compiler.
3434 ;;(eval-when-compile
3435 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3436 ;; (fmakunbound 'dired-insert-set-properties)))
3438 ;; CCC is this the right thing to do?
3439 (defun tramp-handle-unhandled-file-name-directory (filename)
3440 "Like `unhandled-file-name-directory' for tramp files."
3441 (with-parsed-tramp-file-name filename nil
3442 (expand-file-name "~/")))
3444 ;; Canonicalization of file names.
3446 (defun tramp-drop-volume-letter (name)
3447 "Cut off unnecessary drive letter from file NAME.
3448 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3449 locally on a remote file name. When the local system is a W32 system
3450 but the remote system is Unix, this introduces a superfluous drive
3451 letter into the file name. This function removes it.
3453 Doesn't do anything if the NAME does not start with a drive letter."
3454 (if (and (> (length name) 1)
3455 (char-equal (aref name 1) ?:)
3456 (let ((c1 (aref name 0)))
3457 (or (and (>= c1 ?A) (<= c1 ?Z))
3458 (and (>= c1 ?a) (<= c1 ?z)))))
3462 (defun tramp-handle-expand-file-name (name &optional dir)
3463 "Like `expand-file-name' for tramp files.
3464 If the localname part of the given filename starts with \"/../\" then
3465 the result will be a local, non-Tramp, filename."
3466 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3467 (setq dir (or dir default-directory "/"))
3468 ;; Unless NAME is absolute, concat DIR and NAME.
3469 (unless (file-name-absolute-p name)
3470 (setq name (concat (file-name-as-directory dir) name)))
3471 ;; If NAME is not a tramp file, run the real handler
3472 (if (not (tramp-tramp-file-p name))
3473 (tramp-run-real-handler 'expand-file-name
3476 (with-parsed-tramp-file-name name nil
3477 (unless (file-name-absolute-p localname)
3478 (setq localname (concat "~/" localname)))
3480 ;; Tilde expansion if necessary. This needs a shell which
3481 ;; groks tilde expansion! The function `tramp-find-shell' is
3482 ;; supposed to find such a shell on the remote host. Please
3483 ;; tell me about it when this doesn't work on your system.
3484 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3485 (let ((uname (match-string 1 localname))
3486 (fname (match-string 2 localname)))
3487 ;; CCC fanatic error checking?
3488 (set-buffer (tramp-get-buffer multi-method method user host))
3491 multi-method method user host
3492 (format "cd %s; pwd" uname)
3494 (tramp-wait-for-output)
3495 (goto-char (point-min))
3496 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3497 (setq localname (concat uname fname))
3499 ;; No tilde characters in file name, do normal
3500 ;; expand-file-name (this does "/./" and "/../"). We bind
3501 ;; directory-sep-char here for XEmacs on Windows, which
3502 ;; would otherwise use backslash.
3503 (tramp-let-maybe directory-sep-char ?/
3504 (tramp-make-tramp-file-name
3505 multi-method (or method (tramp-find-default-method user host))
3507 (tramp-drop-volume-letter
3508 (tramp-run-real-handler 'expand-file-name
3509 (list localname)))))))))
3511 ;; old version follows. it uses ".." to cross file handler
3513 ;; ;; Look if localname starts with "/../" construct. If this is
3514 ;; ;; the case, then we return a local name instead of a remote name.
3515 ;; (if (string-match "^/\\.\\./" localname)
3516 ;; (expand-file-name (substring localname 3))
3517 ;; ;; No tilde characters in file name, do normal
3518 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3519 ;; ;; directory-sep-char here for XEmacs on Windows, which
3520 ;; ;; would otherwise use backslash.
3521 ;; (let ((directory-sep-char ?/))
3522 ;; (tramp-make-tramp-file-name
3523 ;; multi-method method user host
3524 ;; (tramp-drop-volume-letter
3525 ;; (tramp-run-real-handler 'expand-file-name
3526 ;; (list localname))))))))))
3530 (defvar tramp-async-proc nil
3531 "Global variable keeping asynchronous process object.
3532 Used in `tramp-handle-shell-command'")
3534 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3535 "Like `shell-command' for tramp files.
3536 This will break if COMMAND prints a newline, followed by the value of
3537 `tramp-end-of-output', followed by another newline."
3538 ;; Asynchronous processes are far from being perfect. But it works at least
3539 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3540 (if (tramp-tramp-file-p default-directory)
3541 (with-parsed-tramp-file-name default-directory nil
3542 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3544 (unless output-buffer
3548 "*Async Shell Command*"
3549 "*Shell Command Output*")))
3550 (set-buffer output-buffer)
3552 (unless (bufferp output-buffer)
3553 (setq output-buffer (current-buffer)))
3554 (set-buffer output-buffer)
3555 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3556 ;; process. Instead of, another asynchronous process is opened
3557 ;; which gets the output of the (synchronous) Tramp process
3558 ;; via process-filter. ERROR-BUFFER is disabled.
3560 (setq command (substring command 0 (match-beginning 0))
3562 tramp-async-proc (start-process (buffer-name output-buffer)
3563 output-buffer "cat")))
3565 (tramp-barf-unless-okay
3566 multi-method method user host
3567 (format "cd %s" (tramp-shell-quote-argument localname))
3569 "tramp-handle-shell-command: Couldn't `cd %s'"
3570 (tramp-shell-quote-argument localname))
3571 ;; Define the process filter
3575 (tramp-get-buffer multi-method method user host))
3576 '(lambda (process string)
3577 ;; Write the output into the Tramp Process
3578 (save-current-buffer
3579 (set-buffer (process-buffer process))
3580 (goto-char (point-max))
3582 ;; Hand-over output to asynchronous process.
3585 (regexp-quote tramp-end-of-output) string)))
3588 (substring string 0 (1- (match-beginning 0)))))
3589 (process-send-string tramp-async-proc string)
3591 (set-process-filter process nil)
3592 (process-send-eof tramp-async-proc))))))
3595 multi-method method user host
3597 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3599 (format "%s; tramp_old_status=$?" command)))
3600 (unless asynchronous
3601 (tramp-wait-for-output)))
3602 (unless asynchronous
3603 ;; We cannot use `insert-buffer' because the tramp buffer
3604 ;; changes its contents before insertion due to calling
3605 ;; `expand-file' and alike.
3607 (with-current-buffer
3608 (tramp-get-buffer multi-method method user host)
3612 (unless (bufferp error-buffer)
3613 (setq error-buffer (get-buffer-create error-buffer)))
3615 multi-method method user host
3616 "cat /tmp/tramp.$$.err")
3617 (tramp-wait-for-output)
3618 (set-buffer error-buffer)
3619 ;; Same comment as above
3621 (with-current-buffer
3622 (tramp-get-buffer multi-method method user host)
3624 (tramp-send-command-and-check
3625 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3627 (tramp-send-command multi-method method user host "cd")
3628 (unless asynchronous
3629 (tramp-wait-for-output))
3631 multi-method method user host
3632 (concat "tramp_set_exit_status $tramp_old_status;"
3633 " echo tramp_exit_status $?"))
3634 (unless asynchronous
3635 (tramp-wait-for-output)
3636 (goto-char (point-max))
3637 (unless (search-backward "tramp_exit_status " nil t)
3638 (error "Couldn't find exit status of `%s'" command))
3639 (skip-chars-forward "^ ")
3640 (setq status (read (current-buffer)))))
3641 (unless (zerop (buffer-size))
3642 (display-buffer output-buffer))
3644 ;; The following is only executed if something strange was
3645 ;; happening. Emit a helpful message and do it anyway.
3646 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3648 (tramp-run-real-handler 'shell-command
3649 (list command output-buffer error-buffer))))
3651 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3652 "Like `process-file' for Tramp files."
3653 (when infile (error "Implementation does not handle input from file"))
3654 (when (and (numberp buffer) (zerop buffer))
3655 (error "Implementation does not handle immediate return"))
3656 (when (consp buffer) (error "Implementation does not handle error files"))
3658 (mapconcat 'tramp-shell-quote-argument
3665 (defsubst tramp-make-temp-file ()
3666 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3667 (expand-file-name tramp-temp-name-prefix
3668 (tramp-temporary-file-directory))))
3670 (defun tramp-handle-file-local-copy (filename)
3671 "Like `file-local-copy' for tramp files."
3672 (with-parsed-tramp-file-name filename nil
3673 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3674 ;; We used to bind the following as late as possible.
3675 ;; loc-enc and loc-dec were bound directly before the if
3676 ;; statement that checks them. But the functions
3677 ;; tramp-get-* might invoke the "are you awake" check in
3678 ;; tramp-maybe-open-connection, which is an unfortunate time
3679 ;; since we rely on the buffer contents at that spot.
3680 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3681 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3682 (loc-enc (tramp-get-local-encoding multi-method method user host))
3683 (loc-dec (tramp-get-local-decoding multi-method method user host))
3685 (unless (file-exists-p filename)
3686 (error "Cannot make local copy of non-existing file `%s'"
3688 (setq tmpfil (tramp-make-temp-file))
3690 (cond ((tramp-method-out-of-band-p multi-method method user host)
3691 ;; `copy-file' handles out-of-band methods
3692 (copy-file filename tmpfil t t))
3694 ((and rem-enc rem-dec)
3695 ;; Use inline encoding for file transfer.
3697 ;; Following line for setting tramp-current-method,
3698 ;; tramp-current-user, tramp-current-host.
3699 (set-buffer tramp-buf)
3700 (tramp-message 5 "Encoding remote file %s..." filename)
3701 (tramp-barf-unless-okay
3702 multi-method method user host
3703 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3705 "Encoding remote file failed, see buffer `%s' for details"
3707 ;; Remove trailing status code
3708 (goto-char (point-max))
3709 (delete-region (point) (progn (forward-line -1) (point)))
3711 (tramp-message 5 "Decoding remote file %s..." filename)
3713 ;; Here is where loc-enc and loc-dec used to be let-bound.
3714 (if (and (symbolp loc-dec) (fboundp loc-dec))
3715 ;; If local decoding is a function, we call it.
3716 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3719 (insert-buffer-substring tramp-buf)
3720 (tramp-message-for-buffer
3721 multi-method method user host
3722 6 "Decoding remote file %s with function %s..."
3725 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3726 ;; says that we need to strip tramp_exit_status
3727 ;; line from the output here. Go to point-max,
3728 ;; search backward for tramp_exit_status, delete
3729 ;; between point and point-max if found.
3730 (let ((coding-system-for-write 'binary))
3731 (funcall loc-dec (point-min) (point-max))
3732 (write-region (point-min) (point-max) tmpfil))
3733 (kill-buffer tmpbuf))
3734 ;; If tramp-decoding-function is not defined for this
3735 ;; method, we invoke tramp-decoding-command instead.
3736 (let ((tmpfil2 (tramp-make-temp-file)))
3737 (write-region (point-min) (point-max) tmpfil2)
3739 6 "Decoding remote file %s with command %s..."
3741 (tramp-call-local-coding-command
3742 loc-dec tmpfil2 tmpfil)
3743 (delete-file tmpfil2)))
3744 (tramp-message-for-buffer
3745 multi-method method user host
3746 5 "Decoding remote file %s...done" filename)
3747 ;; Set proper permissions.
3748 (set-file-modes tmpfil (file-modes filename))))
3750 (t (error "Wrong method specification for `%s'" method)))
3753 (defun tramp-handle-file-remote-p (filename)
3754 "Like `file-remote-p' for tramp files."
3755 (when (tramp-tramp-file-p filename)
3756 (with-parsed-tramp-file-name filename nil
3757 (make-tramp-file-name
3758 :multi-method multi-method
3764 (defun tramp-handle-insert-file-contents
3765 (filename &optional visit beg end replace)
3766 "Like `insert-file-contents' for tramp files."
3767 (barf-if-buffer-read-only)
3768 (setq filename (expand-file-name filename))
3769 (with-parsed-tramp-file-name filename nil
3770 (if (not (file-exists-p filename))
3773 (setq buffer-file-name filename)
3774 (set-visited-file-modtime)
3775 (set-buffer-modified-p nil))
3777 (format "File `%s' not found on remote host" filename))
3778 (list (expand-file-name filename) 0))
3779 ;; `insert-file-contents-literally' takes care to avoid calling
3780 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3781 ;; propagate that care to the file-local-copy operation.
3783 (let ((inhibit-file-name-operation
3784 (when (eq inhibit-file-name-operation
3785 'insert-file-contents)
3787 (file-local-copy filename)))
3788 coding-system-used result)
3790 (setq buffer-file-name filename)
3791 (set-visited-file-modtime)
3792 (set-buffer-modified-p nil))
3793 (tramp-message-for-buffer
3794 multi-method method user host
3795 9 "Inserting local temp file `%s'..." local-copy)
3796 (setq result (insert-file-contents local-copy nil beg end replace))
3797 ;; Now `last-coding-system-used' has right value. Remember it.
3798 (when (boundp 'last-coding-system-used)
3799 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3800 (tramp-message-for-buffer
3801 multi-method method user host
3802 9 "Inserting local temp file `%s'...done" local-copy)
3803 (delete-file local-copy)
3804 (when (boundp 'last-coding-system-used)
3805 (set 'last-coding-system-used coding-system-used))
3806 (list (expand-file-name filename)
3807 (second result))))))
3810 (defun tramp-handle-find-backup-file-name (filename)
3811 "Like `find-backup-file-name' for tramp files."
3812 (with-parsed-tramp-file-name filename nil
3813 ;; We set both variables. It doesn't matter whether it is
3815 (let ((backup-directory-alist
3817 (when (boundp 'backup-directory-alist)
3818 (if (boundp 'tramp-backup-directory-alist)
3823 (if (and (stringp (cdr x))
3824 (file-name-absolute-p (cdr x))
3825 (not (tramp-file-name-p (cdr x))))
3826 (tramp-make-tramp-file-name
3827 multi-method method user host (cdr x))
3829 (symbol-value 'tramp-backup-directory-alist))
3830 (symbol-value 'backup-directory-alist))))
3832 (bkup-backup-directory-info
3834 (when (boundp 'bkup-backup-directory-info)
3835 (if (boundp 'tramp-bkup-backup-directory-info)
3841 (if (and (stringp (car (cdr x)))
3842 (file-name-absolute-p (car (cdr x)))
3843 (not (tramp-file-name-p (car (cdr x)))))
3844 (tramp-make-tramp-file-name
3845 multi-method method user host (car (cdr x)))
3848 (symbol-value 'tramp-bkup-backup-directory-info))
3849 (symbol-value 'bkup-backup-directory-info)))))
3851 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3853 (defun tramp-handle-make-auto-save-file-name ()
3854 "Like `make-auto-save-file-name' for tramp files.
3855 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3856 (when tramp-auto-save-directory
3857 (unless (file-exists-p tramp-auto-save-directory)
3858 (make-directory tramp-auto-save-directory t)))
3859 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3860 ;; file name we make sure that jka-compr isn't used for the
3862 (let ((buffer-file-name
3863 (if tramp-auto-save-directory
3865 (tramp-subst-strs-in-string
3873 tramp-auto-save-directory)
3874 (buffer-file-name))))
3875 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3876 ;; it is not a magic file name operation (since Emacs 22).
3877 ;; We must deactivate it temporarily.
3878 (if (not (ad-is-active 'make-auto-save-file-name))
3879 (tramp-run-real-handler
3880 'make-auto-save-file-name nil)
3882 (ad-deactivate 'make-auto-save-file-name)
3884 (tramp-run-real-handler
3885 'make-auto-save-file-name nil)
3886 (ad-activate 'make-auto-save-file-name)))))
3889 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3890 (defun tramp-handle-write-region
3891 (start end filename &optional append visit lockname confirm)
3892 "Like `write-region' for tramp files."
3893 (unless (eq append nil)
3894 (error "Cannot append to file using tramp (`%s')" filename))
3895 (setq filename (expand-file-name filename))
3896 ;; Following part commented out because we don't know what to do about
3897 ;; file locking, and it does not appear to be a problem to ignore it.
3898 ;; Ange-ftp ignores it, too.
3899 ;; (when (and lockname (stringp lockname))
3900 ;; (setq lockname (expand-file-name lockname)))
3901 ;; (unless (or (eq lockname nil)
3902 ;; (string= lockname filename))
3904 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3905 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
3906 (when (and (not (featurep 'xemacs))
3907 confirm (file-exists-p filename))
3908 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3910 (error "File not overwritten")))
3911 (with-parsed-tramp-file-name filename nil
3912 (let ((curbuf (current-buffer))
3913 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3914 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3915 (loc-enc (tramp-get-local-encoding multi-method method user host))
3916 (loc-dec (tramp-get-local-decoding multi-method method user host))
3917 (trampbuf (get-buffer-create "*tramp output*"))
3918 (modes (file-modes filename))
3919 ;; We use this to save the value of `last-coding-system-used'
3920 ;; after writing the tmp file. At the end of the function,
3921 ;; we set `last-coding-system-used' to this saved value.
3922 ;; This way, any intermediary coding systems used while
3923 ;; talking to the remote shell or suchlike won't hose this
3924 ;; variable. This approach was snarfed from ange-ftp.el.
3927 ;; Write region into a tmp file. This isn't really needed if we
3928 ;; use an encoding function, but currently we use it always
3929 ;; because this makes the logic simpler.
3930 (setq tmpfil (tramp-make-temp-file))
3931 ;; Set current buffer. If connection wasn't open, `file-modes' has
3932 ;; changed it accidently.
3934 ;; We say `no-message' here because we don't want the visited file
3935 ;; modtime data to be clobbered from the temp file. We call
3936 ;; `set-visited-file-modtime' ourselves later on.
3937 (tramp-run-real-handler
3939 (if confirm ; don't pass this arg unless defined for backward compat.
3940 (list start end tmpfil append 'no-message lockname confirm)
3941 (list start end tmpfil append 'no-message lockname)))
3942 ;; Now, `last-coding-system-used' has the right value. Remember it.
3943 (when (boundp 'last-coding-system-used)
3944 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3945 ;; The permissions of the temporary file should be set. If
3946 ;; filename does not exist (eq modes nil) it has been renamed to
3947 ;; the backup file. This case `save-buffer' handles
3949 (when modes (set-file-modes tmpfil modes))
3950 ;; This is a bit lengthy due to the different methods possible for
3951 ;; file transfer. First, we check whether the method uses an rcp
3952 ;; program. If so, we call it. Otherwise, both encoding and
3953 ;; decoding command must be specified. However, if the method
3954 ;; _also_ specifies an encoding function, then that is used for
3955 ;; encoding the contents of the tmp file.
3956 (cond ((tramp-method-out-of-band-p multi-method method user host)
3957 ;; `copy-file' handles out-of-band methods
3958 (copy-file tmpfil filename t t))
3960 ((and rem-enc rem-dec)
3961 ;; Use inline file transfer
3962 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
3964 ;; Encode tmpfil into tmpbuf
3965 (tramp-message-for-buffer multi-method method user host
3966 5 "Encoding region...")
3969 ;; Use encoding function or command.
3970 (if (and (symbolp loc-enc) (fboundp loc-enc))
3972 (tramp-message-for-buffer
3973 multi-method method user host
3974 6 "Encoding region using function `%s'..."
3975 (symbol-name loc-enc))
3976 (insert-file-contents-literally tmpfil)
3977 ;; CCC. The following `let' is a workaround for
3978 ;; the base64.el that comes with pgnus-0.84. If
3979 ;; both of the following conditions are
3980 ;; satisfied, it tries to write to a local file
3981 ;; in default-directory, but at this point,
3982 ;; default-directory is remote.
3983 ;; (CALL-PROCESS-REGION can't write to remote
3984 ;; files, it seems.) The file in question is a
3986 (let ((default-directory
3987 (tramp-temporary-file-directory)))
3988 (funcall loc-enc (point-min) (point-max)))
3989 (goto-char (point-max))
3992 (tramp-message-for-buffer
3993 multi-method method user host
3994 6 "Encoding region using command `%s'..." loc-enc)
3995 (unless (equal 0 (tramp-call-local-coding-command
3997 (pop-to-buffer trampbuf)
3998 (error (concat "Cannot write to `%s', local encoding"
3999 " command `%s' failed")
4001 ;; Send tmpbuf into remote decoding command which
4002 ;; writes to remote file. Because this happens on the
4003 ;; remote host, we cannot use the function.
4004 (tramp-message-for-buffer
4005 multi-method method user host
4006 5 "Decoding region into remote file %s..." filename)
4008 multi-method method user host
4009 (format "%s >%s <<'EOF'"
4011 (tramp-shell-quote-argument localname)))
4013 (tramp-message-for-buffer
4014 multi-method method user host
4015 6 "Sending data to remote host...")
4016 (tramp-send-string multi-method method user host
4018 ;; wait for remote decoding to complete
4019 (tramp-message-for-buffer
4020 multi-method method user host
4021 6 "Sending end of data token...")
4023 multi-method method user host "EOF" nil t)
4024 (tramp-message-for-buffer
4025 multi-method method user host 6
4026 "Waiting for remote host to process data...")
4027 (set-buffer (tramp-get-buffer multi-method method user host))
4028 (tramp-wait-for-output)
4029 (tramp-barf-unless-okay
4030 multi-method method user host nil nil 'file-error
4031 (concat "Couldn't write region to `%s',"
4032 " decode using `%s' failed")
4034 (tramp-message 5 "Decoding region into remote file %s...done"
4036 (kill-buffer tmpbuf))))
4039 (concat "Method `%s' should specify both encoding and "
4040 "decoding command or an rcp program")
4042 (delete-file tmpfil)
4043 (unless (equal curbuf (current-buffer))
4044 (error "Buffer has changed from `%s' to `%s'"
4045 curbuf (current-buffer)))
4046 (when (or (eq visit t) (stringp visit))
4047 (set-visited-file-modtime
4048 ;; We must pass modtime explicitely, because filename can be different
4049 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4050 (nth 5 (file-attributes filename))))
4051 ;; Make `last-coding-system-used' have the right value.
4052 (when (boundp 'last-coding-system-used)
4053 (set 'last-coding-system-used coding-system-used))
4054 (when (or (eq visit t)
4057 (message "Wrote %s" filename)))))
4059 ;; Call down to the real handler.
4060 ;; Because EFS does not play nicely with TRAMP (both systems match a
4061 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4064 ;; Other than that, this is the canon file-handler code that the doco
4065 ;; says should be used here. Which is nice.
4067 ;; Under XEmacs current, EFS also hooks in as
4068 ;; efs-sifn-handler-function to handle any filename with environment
4069 ;; variables. This has two implications:
4070 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4071 ;; 2) That TRAMP might want to do the same thing.
4072 ;; Details as they come in.
4074 ;; Daniel Pittman <daniel@danann.net>
4076 ;; (defun tramp-run-real-handler (operation args)
4077 ;; "Invoke normal file name handler for OPERATION.
4078 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4079 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4081 ;; (let ((inhibit-file-name-handlers
4082 ;; (list 'tramp-file-name-handler
4083 ;; 'efs-file-handler-function
4084 ;; 'ange-ftp-hook-function
4085 ;; (and (eq inhibit-file-name-operation operation)
4086 ;; inhibit-file-name-handlers)))
4087 ;; (inhibit-file-name-operation operation))
4088 ;; (apply operation args)))
4090 (defun tramp-run-real-handler (operation args)
4091 "Invoke normal file name handler for OPERATION.
4092 First arg specifies the OPERATION, second arg is a list of arguments to
4093 pass to the OPERATION."
4094 (let* ((inhibit-file-name-handlers
4095 `(tramp-file-name-handler
4096 tramp-completion-file-name-handler
4097 cygwin-mount-name-hook-function
4098 cygwin-mount-map-drive-hook-function
4100 ,(and (eq inhibit-file-name-operation operation)
4101 inhibit-file-name-handlers)))
4102 (inhibit-file-name-operation operation))
4103 (apply operation args)))
4105 ;; This function is used from `tramp-completion-file-name-handler' functions
4106 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4107 ;; because the check is based on a full filename, not available for all
4108 ;; basic I/O operations.
4109 (defun tramp-completion-run-real-handler (operation args)
4110 "Invoke `tramp-file-name-handler' for OPERATION.
4111 First arg specifies the OPERATION, second arg is a list of arguments to
4112 pass to the OPERATION."
4113 (let* ((inhibit-file-name-handlers
4114 `(tramp-completion-file-name-handler
4115 cygwin-mount-name-hook-function
4116 cygwin-mount-map-drive-hook-function
4118 ,(and (eq inhibit-file-name-operation operation)
4119 inhibit-file-name-handlers)))
4120 (inhibit-file-name-operation operation))
4121 (apply operation args)))
4123 ;; We handle here all file primitives. Most of them have the file
4124 ;; name as first parameter; nevertheless we check for them explicitly
4125 ;; in order to be signalled if a new primitive appears. This
4126 ;; scenario is needed because there isn't a way to decide by
4127 ;; syntactical means whether a foreign method must be called. It would
4128 ;; ease the life if `file-name-handler-alist' would support a decision
4129 ;; function as well but regexp only.
4130 (defun tramp-file-name-for-operation (operation &rest args)
4131 "Return file name related to OPERATION file primitive.
4132 ARGS are the arguments OPERATION has been called with."
4134 ; FILE resp DIRECTORY
4136 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4137 'delete-file 'diff-latest-backup-file 'directory-file-name
4138 'directory-files 'directory-files-and-attributes
4139 'dired-compress-file 'dired-uncache
4140 'file-accessible-directory-p 'file-attributes
4141 'file-directory-p 'file-executable-p 'file-exists-p
4142 'file-local-copy 'file-remote-p 'file-modes
4143 'file-name-as-directory 'file-name-directory
4144 'file-name-nondirectory 'file-name-sans-versions
4145 'file-ownership-preserved-p 'file-readable-p
4146 'file-regular-p 'file-symlink-p 'file-truename
4147 'file-writable-p 'find-backup-file-name 'find-file-noselect
4148 'get-file-buffer 'insert-directory 'insert-file-contents
4149 'load 'make-directory 'make-directory-internal
4150 'set-file-modes 'substitute-in-file-name
4151 'unhandled-file-name-directory 'vc-registered
4153 'abbreviate-file-name 'create-file-buffer
4154 'dired-file-modtime 'dired-make-compressed-filename
4155 'dired-recursive-delete-directory 'dired-set-file-modtime
4156 'dired-shell-unhandle-file-name 'dired-uucode-file
4157 'insert-file-contents-literally 'recover-file
4158 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4159 (if (file-name-absolute-p (nth 0 args))
4161 (expand-file-name (nth 0 args))))
4162 ; FILE DIRECTORY resp FILE1 FILE2
4164 (list 'add-name-to-file 'copy-file 'expand-file-name
4165 'file-name-all-completions 'file-name-completion
4166 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4168 'dired-make-relative-symlink
4169 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4172 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4173 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4174 (t (buffer-file-name (current-buffer))))))
4176 ((eq operation 'write-region)
4180 (list 'make-auto-save-file-name
4181 'set-visited-file-modtime 'verify-visited-file-modtime
4185 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4188 (list 'dired-call-process
4194 'dired-print-file 'dired-shell-call-process))
4196 ; unknown file primitive
4197 (t (error "unknown file I/O primitive: %s" operation))))
4199 (defun tramp-find-foreign-file-name-handler (filename)
4200 "Return foreign file name handler if exists."
4201 (when (tramp-tramp-file-p filename)
4204 (handler-alist tramp-foreign-file-name-handler-alist))
4205 (while handler-alist
4206 (setq elt (car handler-alist)
4207 handler-alist (cdr handler-alist))
4208 (when (funcall (car elt) filename)
4209 (setq handler-alist nil)
4210 (setq res (cdr elt))))
4215 (defun tramp-file-name-handler (operation &rest args)
4216 "Invoke Tramp file name handler.
4217 Falls back to normal file name handler if no tramp file name handler exists."
4219 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4220 (foreign (tramp-find-foreign-file-name-handler filename)))
4222 (foreign (apply foreign operation args))
4223 (t (tramp-run-real-handler operation args))))))
4226 ;; In Emacs, there is some concurrency due to timers. If a timer
4227 ;; interrupts Tramp and wishes to use the same connection buffer as
4228 ;; the "main" Emacs, then garbage might occur in the connection
4229 ;; buffer. Therefore, we need to make sure that a timer does not use
4230 ;; the same connection buffer as the "main" Emacs. We implement a
4231 ;; cheap global lock, instead of locking each connection buffer
4232 ;; separately. The global lock is based on two variables,
4233 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4234 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4235 ;; processing of a single file operation, so we need to allow
4236 ;; recursive calls. That's where the `tramp-locker' variable comes in
4237 ;; -- it is let-bound to t during the execution of the current
4238 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4239 ;; then we should just proceed because we have been called
4240 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4241 ;; interrupting the "main" Emacs, and then we signal an error.
4243 (defvar tramp-locked nil
4244 "If non-nil, then Tramp is currently busy.
4245 Together with `tramp-locker', this implements a locking mechanism
4246 preventing reentrant calls of Tramp.")
4248 (defvar tramp-locker nil
4249 "If non-nil, then a caller has locked Tramp.
4250 Together with `tramp-locked', this implements a locking mechanism
4251 preventing reentrant calls of Tramp.")
4253 (defun tramp-sh-file-name-handler (operation &rest args)
4254 "Invoke remote-shell Tramp file name handler.
4255 Fall back to normal file name handler if no Tramp handler exists."
4256 (when (and tramp-locked (not tramp-locker))
4257 (signal 'file-error "Forbidden reentrant call of Tramp"))
4258 (let ((tl tramp-locked))
4261 (setq tramp-locked t)
4262 (let ((tramp-locker t))
4264 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4266 (apply (cdr fn) args)
4267 (tramp-run-real-handler operation args))))))
4268 (setq tramp-locked tl))))
4271 (defun tramp-completion-file-name-handler (operation &rest args)
4272 "Invoke tramp file name completion handler.
4273 Falls back to normal file name handler if no tramp file name handler exists."
4274 ;; (setq tramp-debug-buffer t)
4275 ;; (tramp-message 1 "%s %s" operation args)
4276 ;; (tramp-message 1 "%s %s\n%s"
4277 ;; operation args (with-output-to-string (backtrace)))
4278 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4280 (save-match-data (apply (cdr fn) args))
4281 (tramp-completion-run-real-handler operation args))))
4284 (put 'tramp-completion-file-name-handler 'safe-magic t)
4286 ;; Register in file name handler alist
4288 (add-to-list 'file-name-handler-alist
4289 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4290 (add-to-list 'file-name-handler-alist
4291 (cons tramp-completion-file-name-regexp
4292 'tramp-completion-file-name-handler))
4294 (defun tramp-repair-jka-compr ()
4295 "If jka-compr is already loaded, move it to the front of
4296 `file-name-handler-alist'. On Emacs 22 or so this will not be
4298 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4300 (setq file-name-handler-alist
4301 (cons jka (delete jka file-name-handler-alist))))))
4302 (tramp-repair-jka-compr)
4305 ;;; Interactions with other packages:
4307 ;; -- complete.el --
4309 ;; This function contributed by Ed Sabol
4310 (defun tramp-handle-expand-many-files (name)
4311 "Like `PC-expand-many-files' for tramp files."
4312 (with-parsed-tramp-file-name name nil
4314 (if (or (string-match "\\*" name)
4315 (string-match "\\?" name)
4316 (string-match "\\[.*\\]" name))
4319 ;; CCC: To do it right, we should quote certain characters
4320 ;; in the file name, but since the echo command is going to
4321 ;; break anyway when there are spaces in the file names, we
4323 ;;-(let ((comint-file-name-quote-list
4324 ;;- (set-difference tramp-file-name-quote-list
4325 ;;- '(?\* ?\? ?[ ?]))))
4326 ;;- (tramp-send-command
4327 ;;- multi-method method user host
4328 ;;- (format "echo %s" (comint-quote-filename localname)))
4329 ;;- (tramp-wait-for-output))
4330 (tramp-send-command multi-method method user host
4331 (format "echo %s" localname))
4332 (tramp-wait-for-output)
4333 (setq bufstr (buffer-substring (point-min)
4334 (tramp-line-end-position)))
4335 (goto-char (point-min))
4336 (if (string-equal localname bufstr)
4339 (while (search-forward " " nil t)
4340 (delete-backward-char 1)
4342 (goto-char (point-max))
4343 (delete-backward-char 1)
4345 (goto-char (point-min))
4347 (function (lambda (x)
4348 (tramp-make-tramp-file-name multi-method method
4350 (read (current-buffer))))))
4351 (list (expand-file-name name))))))
4353 ;; Check for complete.el and override PC-expand-many-files if appropriate.
4355 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
4357 (defun tramp-setup-complete ()
4358 (fset 'tramp-save-PC-expand-many-files
4359 (symbol-function 'PC-expand-many-files))
4360 (defun PC-expand-many-files (name)
4361 (if (tramp-tramp-file-p name)
4362 (funcall (symbol-function 'expand-many-files) name)
4363 (tramp-save-PC-expand-many-files name))))
4365 ;; Why isn't eval-after-load sufficient?
4366 (if (fboundp 'PC-expand-many-files)
4367 (tramp-setup-complete)
4368 (eval-after-load "complete" '(tramp-setup-complete)))
4370 ;;; File name handler functions for completion mode
4372 ;; Necessary because `tramp-file-name-regexp-unified' and
4373 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4374 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4375 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4376 ;; Using `last-input-event' is a little bit risky, because completing a file
4377 ;; might require loading other files, like "~/.netrc", and for them it
4378 ;; shouldn't be decided based on that variable. On the other hand, those files
4379 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4380 ;; be introduced overwriting this check in such cases. Or we change tramp
4381 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4382 ;; In case of non unified file names it can be always true (and wouldn't be
4383 ;; necessary, because there are different regexp).
4384 (defun tramp-completion-mode (file)
4385 "Checks whether method / user name / host name completion is active."
4387 (tramp-completion-mode t)
4388 ((not tramp-unified-filenames) t)
4389 ((string-match "^/.*:.*:$" file) nil)
4391 (concat tramp-prefix-regexp
4392 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4394 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4395 ((or (equal last-input-event 'tab)
4397 (and (integerp last-input-event)
4398 (not (event-modifiers last-input-event))
4399 (or (char-equal last-input-event ?\?)
4400 (char-equal last-input-event ?\t) ; handled by 'tab already?
4401 (char-equal last-input-event ?\ )))
4403 (and (featurep 'xemacs)
4404 (not (event-modifiers last-input-event))
4406 (funcall (symbol-function 'event-to-character)
4407 last-input-event) ?\?)
4409 (funcall (symbol-function 'event-to-character)
4410 last-input-event) ?\t)
4412 (funcall (symbol-function 'event-to-character)
4413 last-input-event) ?\ ))))
4416 (defun tramp-completion-handle-file-exists-p (filename)
4417 "Like `file-exists-p' for tramp files."
4418 (if (tramp-completion-mode filename)
4419 (tramp-run-real-handler
4420 'file-exists-p (list filename))
4421 (tramp-completion-run-real-handler
4422 'file-exists-p (list filename))))
4424 ;; Localname manipulation in case of partial TRAMP file names.
4425 (defun tramp-completion-handle-file-name-directory (file)
4426 "Like `file-name-directory' but aware of TRAMP files."
4427 (if (tramp-completion-mode file)
4429 (tramp-completion-run-real-handler
4430 'file-name-directory (list file))))
4432 ;; Localname manipulation in case of partial TRAMP file names.
4433 (defun tramp-completion-handle-file-name-nondirectory (file)
4434 "Like `file-name-nondirectory' but aware of TRAMP files."
4436 file (length (tramp-completion-handle-file-name-directory file))))
4438 (defvar tramp-completion-mode nil
4439 "If non-nil, we are in file name completion mode.")
4441 ;; Method, host name and user name completion.
4442 ;; `tramp-completion-dissect-file-name' returns a list of
4443 ;; tramp-file-name structures. For all of them we return possible completions.
4444 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4445 "Like `file-name-all-completions' for partial tramp files."
4448 ;; We need to reset `tramp-completion-mode'.
4450 (setq tramp-completion-mode t)
4452 ((fullname (concat directory filename))
4453 ;; possible completion structures
4454 (v (tramp-completion-dissect-file-name fullname))
4458 (let* ((car (car v))
4459 (multi-method (tramp-file-name-multi-method car))
4460 (method (tramp-file-name-method car))
4461 (user (tramp-file-name-user car))
4462 (host (tramp-file-name-host car))
4463 (localname (tramp-file-name-localname car))
4464 (m (tramp-find-method multi-method method user host))
4465 (tramp-current-user user) ; see `tramp-parse-passwd'
4468 (unless (or multi-method ;; Not handled (yet).
4469 localname) ;; Nothing to complete
4473 ;; Method dependent user / host combinations
4477 (setq all-user-hosts
4478 (append all-user-hosts
4479 (funcall (nth 0 x) (nth 1 x)))))
4480 (tramp-get-completion-function m))
4482 (setq result (append result
4485 (tramp-get-completion-user-host
4486 method user host (nth 0 x) (nth 1 x)))
4487 (delq nil all-user-hosts)))))
4491 (append result (tramp-get-completion-methods m)))))
4495 ;; unify list, remove nil elements
4497 (let ((car (car result)))
4498 (when car (add-to-list 'result1 car))
4499 (setq result (cdr result))))
4501 ;; Complete local parts
4506 ;; "/ssh:" does not need to be expanded as hostname.
4507 (tramp-run-real-handler
4508 'file-name-all-completions (list filename directory))
4509 ;; No method/user/host found to be expanded.
4510 (tramp-completion-run-real-handler
4511 'file-name-all-completions (list filename directory)))
4514 (setq tramp-completion-mode nil)))
4516 ;; Method, host name and user name completion for a file.
4517 (defun tramp-completion-handle-file-name-completion (filename directory)
4518 "Like `file-name-completion' for tramp files."
4519 (try-completion filename
4520 (mapcar 'list (file-name-all-completions filename directory))))
4522 ;; I misuse a little bit the tramp-file-name structure in order to handle
4523 ;; completion possibilities for partial methods / user names / host names.
4524 ;; Return value is a list of tramp-file-name structures according to possible
4525 ;; completions. If "multi-method" or "localname" is non-nil it means there
4526 ;; shouldn't be a completion anymore.
4528 ;; Expected results:
4530 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4531 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4532 ;; [nil nil "x" nil nil]
4533 ;; [nil "x" nil nil nil]
4535 ;; "/x:" "/x:y" "/x:y:"
4536 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4538 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4539 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4541 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4542 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4543 ;; "/[x/y@" "/[x/y@z"
4544 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4545 (defun tramp-completion-dissect-file-name (name)
4546 "Returns a list of `tramp-file-name' structures.
4547 They are collected by `tramp-completion-dissect-file-name1'."
4551 ;; "/method" "/[method"
4552 (tramp-completion-file-name-structure1
4553 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4556 (tramp-completion-file-name-structure2
4557 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4560 (tramp-completion-file-name-structure3
4561 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4563 ;; "/user@host" "/[user@host"
4564 (tramp-completion-file-name-structure4
4565 (list (concat tramp-prefix-regexp
4566 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4567 "\\(" tramp-host-regexp x-nil "\\)$")
4569 ;; "/method:user" "/[method/user"
4570 (tramp-completion-file-name-structure5
4571 (list (concat tramp-prefix-regexp
4572 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4573 "\\(" tramp-user-regexp x-nil "\\)$")
4575 ;; "/method:host" "/[method/host"
4576 (tramp-completion-file-name-structure6
4577 (list (concat tramp-prefix-regexp
4578 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4579 "\\(" tramp-host-regexp x-nil "\\)$")
4581 ;; "/method:user@host" "/[method/user@host"
4582 (tramp-completion-file-name-structure7
4583 (list (concat tramp-prefix-regexp
4584 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4585 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4586 "\\(" tramp-host-regexp x-nil "\\)$")
4589 (mapcar (lambda (regexp)
4590 (add-to-list 'result
4591 (tramp-completion-dissect-file-name1 regexp name)))
4593 tramp-completion-file-name-structure1
4594 tramp-completion-file-name-structure2
4595 tramp-completion-file-name-structure3
4596 tramp-completion-file-name-structure4
4597 tramp-completion-file-name-structure5
4598 tramp-completion-file-name-structure6
4599 tramp-completion-file-name-structure7
4600 tramp-file-name-structure))
4604 (defun tramp-completion-dissect-file-name1 (structure name)
4605 "Returns a `tramp-file-name' structure matching STRUCTURE.
4606 The structure consists of multi-method, remote method, remote user,
4607 remote host and localname (filename on remote host)."
4611 (when (string-match (nth 0 structure) name)
4612 (setq method (and (nth 1 structure)
4613 (match-string (nth 1 structure) name)))
4614 (if (and method (member method tramp-multi-methods))
4615 ;; Not handled (yet).
4616 (make-tramp-file-name
4617 :multi-method method
4622 (let ((user (and (nth 2 structure)
4623 (match-string (nth 2 structure) name)))
4624 (host (and (nth 3 structure)
4625 (match-string (nth 3 structure) name)))
4626 (localname (and (nth 4 structure)
4627 (match-string (nth 4 structure) name))))
4628 (make-tramp-file-name
4633 :localname localname)))))))
4635 ;; This function returns all possible method completions, adding the
4636 ;; trailing method delimeter.
4637 (defun tramp-get-completion-methods (partial-method)
4638 "Returns all method completions for PARTIAL-METHOD."
4642 (string-match (concat "^" (regexp-quote partial-method)) method)
4643 ;; we must remove leading "/".
4644 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4645 (delete "multi" (mapcar 'car tramp-methods))))
4647 ;; Compares partial user and host names with possible completions.
4648 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4649 "Returns the most expanded string for user and host name completion.
4650 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4653 ((and partial-user partial-host)
4655 (string-match (concat "^" (regexp-quote partial-host)) host)
4656 (string-equal partial-user (or user partial-user)))
4657 (setq user partial-user)
4664 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4670 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4676 (unless (zerop (+ (length user) (length host)))
4677 ;; we must remove leading "/".
4678 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4680 (defun tramp-parse-rhosts (filename)
4681 "Return a list of (user host) tuples allowed to access.
4682 Either user or host may be nil."
4685 (when (file-readable-p filename)
4687 (insert-file-contents filename)
4688 (goto-char (point-min))
4690 (push (tramp-parse-rhosts-group) res))))
4693 ;; Taken from gnus/netrc.el
4695 (defalias 'tramp-point-at-eol
4696 (if (fboundp 'point-at-eol)
4698 'line-end-position)))
4700 (defun tramp-parse-rhosts-group ()
4701 "Return a (user host) tuple allowed to access.
4702 Either user or host may be nil."
4707 "^\\(" tramp-host-regexp "\\)"
4708 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4710 (narrow-to-region (point) (tramp-point-at-eol))
4711 (when (re-search-forward regexp nil t)
4712 (setq result (append (list (match-string 3) (match-string 1)))))
4717 (defun tramp-parse-shosts (filename)
4718 "Return a list of (user host) tuples allowed to access.
4719 User is always nil."
4722 (when (file-readable-p filename)
4724 (insert-file-contents filename)
4725 (goto-char (point-min))
4727 (push (tramp-parse-shosts-group) res))))
4730 (defun tramp-parse-shosts-group ()
4731 "Return a (user host) tuple allowed to access.
4732 User is always nil."
4735 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4737 (narrow-to-region (point) (tramp-point-at-eol))
4738 (when (re-search-forward regexp nil t)
4739 (setq result (list nil (match-string 1))))
4742 (> (skip-chars-forward ",") 0)
4746 (defun tramp-parse-sconfig (filename)
4747 "Return a list of (user host) tuples allowed to access.
4748 User is always nil."
4751 (when (file-readable-p filename)
4753 (insert-file-contents filename)
4754 (goto-char (point-min))
4756 (push (tramp-parse-sconfig-group) res))))
4759 (defun tramp-parse-sconfig-group ()
4760 "Return a (user host) tuple allowed to access.
4761 User is always nil."
4764 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4766 (narrow-to-region (point) (tramp-point-at-eol))
4767 (when (re-search-forward regexp nil t)
4768 (setq result (list nil (match-string 1))))
4771 (> (skip-chars-forward ",") 0)
4775 (defun tramp-parse-shostkeys (dirname)
4776 "Return a list of (user host) tuples allowed to access.
4777 User is always nil."
4779 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4780 (files (when (file-directory-p dirname) (directory-files dirname)))
4784 (when (string-match regexp (car files))
4785 (push (list nil (match-string 1 (car files))) result))
4786 (setq files (cdr files)))
4789 (defun tramp-parse-sknownhosts (dirname)
4790 "Return a list of (user host) tuples allowed to access.
4791 User is always nil."
4793 (let ((regexp (concat "^\\(" tramp-host-regexp
4794 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4795 (files (when (file-directory-p dirname) (directory-files dirname)))
4799 (when (string-match regexp (car files))
4800 (push (list nil (match-string 1 (car files))) result))
4801 (setq files (cdr files)))
4804 (defun tramp-parse-hosts (filename)
4805 "Return a list of (user host) tuples allowed to access.
4806 User is always nil."
4809 (when (file-readable-p filename)
4811 (insert-file-contents filename)
4812 (goto-char (point-min))
4814 (push (tramp-parse-hosts-group) res))))
4817 (defun tramp-parse-hosts-group ()
4818 "Return a (user host) tuple allowed to access.
4819 User is always nil."
4822 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4824 (narrow-to-region (point) (tramp-point-at-eol))
4825 (when (re-search-forward regexp nil t)
4826 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4827 (setq result (list nil (match-string 1)))))
4830 (> (skip-chars-forward " \t") 0)
4834 ;; For su-alike methods it would be desirable to return "root@localhost"
4835 ;; as default. Unfortunately, we have no information whether any user name
4836 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4837 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4838 (defun tramp-parse-passwd (filename)
4839 "Return a list of (user host) tuples allowed to access.
4840 Host is always \"localhost\"."
4843 (if (zerop (length tramp-current-user))
4845 (when (file-readable-p filename)
4847 (insert-file-contents filename)
4848 (goto-char (point-min))
4850 (push (tramp-parse-passwd-group) res))))
4853 (defun tramp-parse-passwd-group ()
4854 "Return a (user host) tuple allowed to access.
4855 Host is always \"localhost\"."
4858 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4860 (narrow-to-region (point) (tramp-point-at-eol))
4861 (when (re-search-forward regexp nil t)
4862 (setq result (list (match-string 1) "localhost")))
4867 (defun tramp-parse-netrc (filename)
4868 "Return a list of (user host) tuples allowed to access.
4872 (when (file-readable-p filename)
4874 (insert-file-contents filename)
4875 (goto-char (point-min))
4877 (push (tramp-parse-netrc-group) res))))
4880 (defun tramp-parse-netrc-group ()
4881 "Return a (user host) tuple allowed to access.
4887 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4888 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4890 (narrow-to-region (point) (tramp-point-at-eol))
4891 (when (re-search-forward regexp nil t)
4892 (setq result (list (match-string 3) (match-string 1))))
4897 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4898 "Like `expand-file-name' for tramp files."
4899 (let ((fullname (concat (or dir default-directory) name)))
4900 (if (tramp-completion-mode fullname)
4901 (tramp-run-real-handler
4902 'expand-file-name (list name dir))
4903 (tramp-completion-run-real-handler
4904 'expand-file-name (list name dir)))))
4906 ;;; Internal Functions:
4908 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
4909 "Define in remote shell function NAME implemented as perl SCRIPT.
4910 Only send the definition if it has not already been done.
4911 Function may have 0-3 parameters."
4912 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4913 (unless remote-perl (error "No remote perl"))
4914 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4915 multi-method method user host)))
4916 (unless (memq name perl-scripts)
4917 (with-current-buffer (tramp-get-buffer multi-method method user host)
4918 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4919 (tramp-send-string multi-method method user host
4925 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4926 (tramp-wait-for-output)
4927 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4928 multi-method method user host)
4929 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4931 (defun tramp-set-auto-save ()
4932 (when (and (buffer-file-name)
4933 (tramp-tramp-file-p (buffer-file-name))
4934 ;; ange-ftp has its own auto-save mechanism
4935 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
4936 'tramp-sh-file-name-handler)
4938 (auto-save-mode 1)))
4939 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4941 (defun tramp-run-test (switch filename)
4942 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4943 Returns the exit code of the `test' program."
4944 (let ((v (tramp-dissect-file-name filename)))
4946 (tramp-send-command-and-check
4947 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4948 (tramp-file-name-user v) (tramp-file-name-host v)
4949 (format "test %s %s" switch
4950 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4952 (defun tramp-run-test2 (program file1 file2 &optional switch)
4953 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4954 The optional SWITCH is inserted between the two files.
4955 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4956 hosts, or files, disagree."
4957 (let* ((v1 (tramp-dissect-file-name file1))
4958 (v2 (tramp-dissect-file-name file2))
4959 (mmethod1 (tramp-file-name-multi-method v1))
4960 (mmethod2 (tramp-file-name-multi-method v2))
4961 (method1 (tramp-file-name-method v1))
4962 (method2 (tramp-file-name-method v2))
4963 (user1 (tramp-file-name-user v1))
4964 (user2 (tramp-file-name-user v2))
4965 (host1 (tramp-file-name-host v1))
4966 (host2 (tramp-file-name-host v2))
4967 (localname1 (tramp-file-name-localname v1))
4968 (localname2 (tramp-file-name-localname v2)))
4969 (unless (and method1 method2 host1 host2
4970 (equal mmethod1 mmethod2)
4971 (equal method1 method2)
4973 (equal host1 host2))
4974 (error "tramp-run-test2: %s"
4975 "only implemented for same method, same user, same host"))
4977 (tramp-send-command-and-check
4978 mmethod1 method1 user1 host1
4979 (format "%s %s %s %s"
4981 (tramp-shell-quote-argument localname1)
4983 (tramp-shell-quote-argument localname2))))))
4985 (defun tramp-touch (file time)
4986 "Set the last-modified timestamp of the given file.
4987 TIME is an Emacs internal time value as returned by `current-time'."
4988 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
4989 (if (tramp-tramp-file-p file)
4990 (with-parsed-tramp-file-name file nil
4991 (let ((buf (tramp-get-buffer multi-method method user host)))
4992 (unless (zerop (tramp-send-command-and-check
4993 multi-method method user host
4994 (format "touch -t %s %s"
4998 (error "tramp-touch: touch failed, see buffer `%s' for details"
5000 ;; It's a local file
5002 (unless (zerop (call-process
5003 "touch" nil (current-buffer) nil "-t" touch-time file))
5004 (pop-to-buffer (current-buffer))
5005 (error "tramp-touch: touch failed"))))))
5007 (defun tramp-buffer-name (multi-method method user host)
5008 "A name for the connection buffer for USER at HOST using METHOD."
5010 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5011 (let ((method (tramp-find-method multi-method method user host)))
5013 (format "*tramp/%s %s@%s*" method user host)
5014 (format "*tramp/%s %s*" method host)))))
5016 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5017 "A name for the multi method connection buffer.
5018 MULTI-METHOD gives the multi method, METHOD the array of methods,
5019 USER the array of user names, HOST the array of host names."
5020 (unless (and (= (length method) (length user))
5021 (= (length method) (length host)))
5022 (error "Syntax error in multi method (implementation error)"))
5023 (let ((len (length method))
5028 (cons (if (aref user i)
5029 (format "%s#%s@%s:" (aref method i)
5030 (aref user i) (aref host i))
5031 (format "%s@%s:" (aref method i) (aref host i)))
5034 (format "*%s/%s %s*"
5036 (apply 'concat (reverse string-list)))))
5038 (defun tramp-get-buffer (multi-method method user host)
5039 "Get the connection buffer to be used for USER at HOST using METHOD."
5040 (with-current-buffer
5041 (get-buffer-create (tramp-buffer-name multi-method method user host))
5042 (setq buffer-undo-list t)
5045 (defun tramp-debug-buffer-name (multi-method method user host)
5046 "A name for the debug buffer for USER at HOST using METHOD."
5048 (tramp-buffer-name-multi-method "debug tramp"
5049 multi-method method user host)
5050 (let ((method (tramp-find-method multi-method method user host)))
5052 (format "*debug tramp/%s %s@%s*" method user host)
5053 (format "*debug tramp/%s %s*" method host)))))
5055 (defun tramp-get-debug-buffer (multi-method method user host)
5056 "Get the debug buffer for USER at HOST using METHOD."
5057 (with-current-buffer
5059 (tramp-debug-buffer-name multi-method method user host))
5060 (setq buffer-undo-list t)
5063 (defun tramp-find-executable (multi-method method user host
5064 progname dirlist ignore-tilde)
5065 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5066 First args METHOD, USER and HOST specify the connection, PROGNAME
5067 is the program to search for, and DIRLIST gives the list of directories
5068 to search. If IGNORE-TILDE is non-nil, directory names starting
5069 with `~' will be ignored.
5071 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5073 This function expects to be in the right *tramp* buffer."
5076 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5077 ;; `remove' is in CL, and we want to avoid CL dependencies.
5080 (setq d (car dirlist))
5081 (setq dirlist (cdr dirlist))
5082 (unless (char-equal ?~ (aref d 0))
5083 (setq newdl (cons d newdl))))
5084 (setq dirlist (nreverse newdl))))
5086 multi-method method user host
5087 (format (concat "while read d; "
5088 "do if test -x $d/%s -a -f $d/%s; "
5089 "then echo tramp_executable $d/%s; "
5090 "break; fi; done <<'EOF'")
5091 progname progname progname))
5093 (tramp-send-command multi-method method user host d))
5095 (tramp-send-command multi-method method user host "EOF")
5096 (tramp-wait-for-output)
5097 (goto-char (point-max))
5098 (when (search-backward "tramp_executable " nil t)
5099 (skip-chars-forward "^ ")
5100 (skip-chars-forward " ")
5101 (buffer-substring (point) (tramp-line-end-position)))))
5103 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5104 "Sets the remote environment VAR to existing directories from DIRLIST.
5105 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5106 so, it is added to the environment variable VAR."
5107 (let ((existing-dirs
5112 (tramp-make-tramp-file-name multi-method method user host x))
5114 (tramp-make-tramp-file-name multi-method method user host x)))
5118 multi-method method user host
5120 (mapconcat 'identity (delq nil existing-dirs) ":")
5122 (tramp-wait-for-output)))
5124 ;; -- communication with external shell --
5126 (defun tramp-find-file-exists-command (multi-method method user host)
5127 "Find a command on the remote host for checking if a file exists.
5128 Here, we are looking for a command which has zero exit status if the
5129 file exists and nonzero exit status otherwise."
5130 (make-local-variable 'tramp-file-exists-command)
5131 (tramp-message 9 "Finding command to check if file exists")
5133 (tramp-make-tramp-file-name
5134 multi-method method user host
5135 "/")) ;assume this file always exists
5137 (tramp-make-tramp-file-name
5138 multi-method method user host
5139 "/ this file does not exist "))) ;assume this never exists
5140 ;; The algorithm is as follows: we try a list of several commands.
5141 ;; For each command, we first run `$cmd /' -- this should return
5142 ;; true, as the root directory always exists. And then we run
5143 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5144 ;; does not exist. This should return false. We use the first
5145 ;; command we find that seems to work.
5146 ;; The list of commands to try is as follows:
5147 ;; `ls -d' This works on most systems, but NetBSD 1.4
5148 ;; has a bug: `ls' always returns zero exit
5149 ;; status, even for files which don't exist.
5150 ;; `test -e' Some Bourne shells have a `test' builtin
5151 ;; which does not know the `-e' option.
5152 ;; `/bin/test -e' For those, the `test' binary on disk normally
5153 ;; provides the option. Alas, the binary
5154 ;; is sometimes `/bin/test' and sometimes it's
5156 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5158 (and (setq tramp-file-exists-command "test -e %s")
5159 (file-exists-p existing)
5160 (not (file-exists-p nonexisting)))
5161 (and (setq tramp-file-exists-command "/bin/test -e %s")
5162 (file-exists-p existing)
5163 (not (file-exists-p nonexisting)))
5164 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5165 (file-exists-p existing)
5166 (not (file-exists-p nonexisting)))
5167 (and (setq tramp-file-exists-command "ls -d %s")
5168 (file-exists-p existing)
5169 (not (file-exists-p nonexisting))))
5170 (error "Couldn't find command to check if file exists"))))
5173 ;; CCC test ksh or bash found for tilde expansion?
5174 (defun tramp-find-shell (multi-method method user host)
5175 "Find a shell on the remote host which groks tilde expansion."
5177 (tramp-send-command multi-method method user host "echo ~root")
5178 (tramp-wait-for-output)
5180 ((string-match "^~root$" (buffer-string))
5182 (or (tramp-find-executable multi-method method user host
5183 "bash" tramp-remote-path t)
5184 (tramp-find-executable multi-method method user host
5185 "ksh" tramp-remote-path t)))
5187 (error "Couldn't find a shell which groks tilde expansion"))
5188 ;; Find arguments for this shell.
5189 (let ((alist tramp-sh-extra-args)
5191 (while (and alist (null extra-args))
5192 (setq item (pop alist))
5193 (when (string-match (car item) shell)
5194 (setq extra-args (cdr item))))
5195 (when extra-args (setq shell (concat shell " " extra-args))))
5197 5 "Starting remote shell `%s' for tilde expansion..." shell)
5199 multi-method method user host
5200 (concat "PS1='$ ' exec " shell)) ;
5201 (tramp-barf-if-no-shell-prompt
5202 (get-buffer-process (current-buffer))
5203 60 "Couldn't find remote `%s' prompt" shell)
5205 9 "Setting remote shell prompt...")
5206 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5207 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5208 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5210 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5211 tramp-rsh-end-of-line
5213 tramp-rsh-end-of-line
5214 tramp-rsh-end-of-line))
5215 (tramp-wait-for-output)
5217 9 "Setting remote shell prompt...done")
5219 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5220 (tramp-get-method-parameter
5221 multi-method method user host 'tramp-remote-sh))))))
5223 (defun tramp-check-ls-command (multi-method method user host cmd)
5224 "Checks whether the given `ls' executable groks `-n'.
5225 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5226 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5228 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5229 (when (file-executable-p
5230 (tramp-make-tramp-file-name multi-method method user host cmd))
5232 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5234 (tramp-send-command-and-check
5235 multi-method method user host
5236 (format "%s -lnd / >/dev/null"
5238 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5240 (if (zerop result) "okay" "failed"))
5243 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5244 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5245 Returns nil if none was found, else the command is returned."
5248 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5249 ;; It would be better to use the CL function `find', but
5250 ;; we don't want run-time dependencies on CL.
5251 (while (and dl (not result))
5252 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5253 (when (tramp-check-ls-command multi-method method user host x)
5258 (defun tramp-find-ls-command (multi-method method user host)
5259 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5260 \(This option prints numeric user and group ids in a long listing.)"
5261 (tramp-message 9 "Finding a suitable `ls' command")
5263 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5264 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5265 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5267 ;; ------------------------------------------------------------
5268 ;; -- Functions for establishing connection --
5269 ;; ------------------------------------------------------------
5271 ;; The following functions are actions to be taken when seeing certain
5272 ;; prompts from the remote host. See the variable
5273 ;; `tramp-actions-before-shell' for usage of these functions.
5275 (defun tramp-action-login (p multi-method method user host)
5276 "Send the login name."
5277 (tramp-message 9 "Sending login name `%s'"
5278 (or user (user-login-name)))
5280 (process-send-string nil (concat (or user (user-login-name))
5281 tramp-rsh-end-of-line)))
5283 (defun tramp-action-password (p multi-method method user host)
5284 "Query the user for a password."
5286 (format "Password for %s "
5287 (tramp-make-tramp-file-name
5288 nil method user host ""))))
5289 (tramp-message 9 "Sending password")
5290 (tramp-enter-password p pw-prompt user host)))
5292 (defun tramp-action-succeed (p multi-method method user host)
5293 "Signal success in finding shell prompt."
5294 (tramp-message 9 "Found remote shell prompt.")
5296 (throw 'tramp-action 'ok))
5298 (defun tramp-action-permission-denied (p multi-method method user host)
5299 "Signal permission denied."
5300 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5301 (tramp-message 9 "Permission denied by remote host.")
5303 (throw 'tramp-action 'permission-denied))
5305 (defun tramp-action-yesno (p multi-method method user host)
5306 "Ask the user for confirmation using `yes-or-no-p'.
5307 Send \"yes\" to remote process on confirmation, abort otherwise.
5308 See also `tramp-action-yn'."
5309 (save-window-excursion
5310 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5311 (unless (yes-or-no-p (match-string 0))
5314 (throw 'tramp-action 'permission-denied))
5315 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5318 (defun tramp-action-yn (p multi-method method user host)
5319 "Ask the user for confirmation using `y-or-n-p'.
5320 Send \"y\" to remote process on confirmation, abort otherwise.
5321 See also `tramp-action-yesno'."
5322 (save-window-excursion
5323 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5324 (unless (y-or-n-p (match-string 0))
5326 (throw 'tramp-action 'permission-denied))
5328 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5330 (defun tramp-action-terminal (p multi-method method user host)
5331 "Tell the remote host which terminal type to use.
5332 The terminal type can be configured with `tramp-terminal-type'."
5333 (tramp-message 9 "Setting `%s' as terminal type."
5334 tramp-terminal-type)
5336 (process-send-string nil (concat tramp-terminal-type
5337 tramp-rsh-end-of-line)))
5339 (defun tramp-action-process-alive (p multi-method method user host)
5340 "Check whether a process has finished."
5341 (unless (memq (process-status p) '(run open))
5342 (throw 'tramp-action 'process-died)))
5344 (defun tramp-action-out-of-band (p multi-method method user host)
5345 "Check whether an out-of-band copy has finished."
5346 (cond ((and (memq (process-status p) '(stop exit))
5347 (zerop (process-exit-status p)))
5348 (tramp-message 9 "Process has finished.")
5349 (throw 'tramp-action 'ok))
5350 ((or (and (memq (process-status p) '(stop exit))
5351 (not (zerop (process-exit-status p))))
5352 (memq (process-status p) '(signal)))
5353 ;; `scp' could have copied correctly, but set modes could have failed.
5354 ;; This can be ignored.
5355 (goto-char (point-min))
5356 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5358 (tramp-message 10 "'set mode' error ignored.")
5359 (tramp-message 9 "Process has finished.")
5360 (throw 'tramp-action 'ok))
5361 (tramp-message 9 "Process has died.")
5362 (throw 'tramp-action 'process-died)))
5365 ;; The following functions are specifically for multi connections.
5367 (defun tramp-multi-action-login (p method user host)
5368 "Send the login name."
5369 (tramp-message 9 "Sending login name `%s'" user)
5371 (process-send-string p (concat user tramp-rsh-end-of-line)))
5373 (defun tramp-multi-action-password (p method user host)
5374 "Query the user for a password."
5376 (format "Password for %s "
5377 (tramp-make-tramp-file-name
5378 nil method user host ""))))
5379 (tramp-message 9 "Sending password")
5380 (tramp-enter-password p pw-prompt user host)))
5382 (defun tramp-multi-action-succeed (p method user host)
5383 "Signal success in finding shell prompt."
5384 (tramp-message 9 "Found shell prompt on `%s'" host)
5386 (throw 'tramp-action 'ok))
5388 (defun tramp-multi-action-permission-denied (p method user host)
5389 "Signal permission denied."
5390 (tramp-message 9 "Permission denied by remote host `%s'" host)
5393 (throw 'tramp-action 'permission-denied))
5395 (defun tramp-multi-action-process-alive (p method user host)
5396 "Check whether a process has finished."
5397 (unless (memq (process-status p) '(run open))
5398 (throw 'tramp-action 'process-died)))
5400 ;; Functions for processing the actions.
5402 (defun tramp-process-one-action (p multi-method method user host actions)
5403 "Wait for output from the shell and perform one action."
5404 (let (found item pattern action todo)
5406 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5407 (with-timeout (60 (throw 'tramp-action 'timeout))
5409 (tramp-accept-process-output p 1)
5410 (goto-char (point-min))
5413 (goto-char (point-min))
5414 (setq item (pop todo))
5415 (setq pattern (symbol-value (nth 0 item)))
5416 (setq action (nth 1 item))
5417 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5419 (when (re-search-forward (concat pattern "\\'") nil t)
5420 (setq found (funcall action p multi-method method user host)))))
5423 (defun tramp-process-actions (p multi-method method user host actions)
5424 "Perform actions until success."
5427 (tramp-message 9 "Waiting for prompts from remote shell")
5429 (catch 'tramp-action
5430 (tramp-process-one-action
5431 p multi-method method user host actions)
5433 (unless (eq exit 'ok)
5434 (tramp-clear-passwd user host)
5435 (error "Login failed"))))
5437 ;; For multi-actions.
5439 (defun tramp-process-one-multi-action (p method user host actions)
5440 "Wait for output from the shell and perform one action."
5441 (let (found item pattern action todo)
5443 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5444 (with-timeout (60 (throw 'tramp-action 'timeout))
5446 (tramp-accept-process-output p 1)
5448 (goto-char (point-min))
5450 (goto-char (point-min))
5451 (setq item (pop todo))
5452 (setq pattern (symbol-value (nth 0 item)))
5453 (setq action (nth 1 item))
5454 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5456 (when (re-search-forward (concat pattern "\\'") nil t)
5457 (setq found (funcall action p method user host)))))
5460 (defun tramp-process-multi-actions (p method user host actions)
5461 "Perform actions until success."
5464 (tramp-message 9 "Waiting for prompts from remote shell")
5466 (catch 'tramp-action
5467 (tramp-process-one-multi-action p method user host actions)
5469 (unless (eq exit 'ok)
5470 (tramp-clear-passwd user host)
5471 (error "Login failed"))))
5473 ;; Functions to execute when we have seen the remote shell prompt but
5474 ;; before we exec the Bourne-ish shell. Note that these commands
5475 ;; might be sent to any shell, not just a Bourne-ish shell. This
5476 ;; means that the commands need to work in all shells. (It is also
5477 ;; okay for some commands to just fail with an error message, but
5478 ;; please make sure that they at least don't crash the odd shell people
5479 ;; might be running...)
5480 (defun tramp-process-initial-commands (p
5481 multi-method method user host
5483 "Send list of commands to remote host, in order."
5486 (setq cmd (pop commands))
5488 (tramp-message 10 "Sending command to remote shell: %s"
5490 (tramp-send-command multi-method method user host cmd nil t)
5491 (tramp-barf-if-no-shell-prompt
5492 p 60 "Remote shell command failed: %s" cmd))
5495 ;; The actual functions for opening connections.
5497 (defun tramp-open-connection-telnet (multi-method method user host)
5498 "Open a connection using a telnet METHOD.
5499 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5500 login prompt, then sends the user name USER, then waits for a remote
5501 password prompt. It queries the user for the password, then sends the
5502 password to the remote host.
5504 If USER is nil, uses value returned by `(user-login-name)' instead.
5506 Recognition of the remote shell prompt is based on the variables
5507 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5510 Please note that it is NOT possible to use this connection method
5511 together with an out-of-band transfer method! You must use an inline
5514 Maybe the different regular expressions need to be tuned.
5516 * Actually, the telnet program as well as the args to be used can be
5517 specified in the method parameters, see the variable `tramp-methods'."
5519 (when (tramp-method-out-of-band-p multi-method method user host)
5520 (error "Cannot use out-of-band method `%s' with telnet connection method"
5523 (error "Cannot multi-connect using telnet connection method"))
5524 (tramp-pre-connection multi-method method user host tramp-chunksize)
5525 (tramp-message 7 "Opening connection for %s@%s using %s..."
5526 (or user (user-login-name)) host method)
5527 (let ((process-environment (copy-sequence process-environment)))
5528 (setenv "TERM" tramp-terminal-type)
5529 (let* ((default-directory (tramp-temporary-file-directory))
5530 ;; If we omit the conditional here, then we would use
5531 ;; `undecided-dos' in some cases. With the conditional,
5532 ;; we use nil in these cases. Which one is right?
5533 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5534 (> emacs-major-version 20))
5535 tramp-dos-coding-system))
5536 (p (apply 'start-process
5537 (tramp-buffer-name multi-method method user host)
5538 (tramp-get-buffer multi-method method user host)
5539 (tramp-get-method-parameter
5541 (tramp-find-method multi-method method user host)
5542 user host 'tramp-login-program)
5544 (tramp-get-method-parameter
5546 (tramp-find-method multi-method method user host)
5547 user host 'tramp-login-args)))
5550 (tramp-set-process-query-on-exit-flag p nil)
5551 (set-buffer (tramp-get-buffer multi-method method user host))
5553 (tramp-process-actions p multi-method method user host
5554 tramp-actions-before-shell)
5555 (tramp-open-connection-setup-interactive-shell
5556 p multi-method method user host)
5557 (tramp-post-connection multi-method method user host)))))
5560 (defun tramp-open-connection-rsh (multi-method method user host)
5561 "Open a connection using an rsh METHOD.
5562 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5563 password or shell prompt. If a password prompt is seen, the user is
5564 queried for a password, this function sends the password to the remote
5565 host and waits for a shell prompt.
5567 If USER is nil, start the command `rsh HOST'[*] instead
5569 Recognition of the remote shell prompt is based on the variables
5570 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5573 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5574 be a port number for ssh, and \"-p yy\" will be added to the list of
5575 arguments, and xx will be used as the host name to connect to.
5577 * Actually, the rsh program to be used can be specified in the
5578 method parameters, see the variable `tramp-methods'."
5581 (error "Cannot multi-connect using rsh connection method"))
5582 (tramp-pre-connection multi-method method user host tramp-chunksize)
5583 (if (and user (not (string= user "")))
5584 (tramp-message 7 "Opening connection for %s@%s using %s..."
5586 (tramp-message 7 "Opening connection at %s using %s..." host method))
5587 (let ((process-environment (copy-sequence process-environment))
5588 (bufnam (tramp-buffer-name multi-method method user host))
5589 (buf (tramp-get-buffer multi-method method user host))
5590 (login-program (tramp-get-method-parameter
5592 (tramp-find-method multi-method method user host)
5593 user host 'tramp-login-program))
5594 (login-args (tramp-get-method-parameter
5596 (tramp-find-method multi-method method user host)
5597 user host 'tramp-login-args))
5599 ;; The following should be changed. We need a more general
5600 ;; mechanism to parse extra host args.
5601 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5602 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5603 (setq real-host (match-string 1 host)))
5604 (setenv "TERM" tramp-terminal-type)
5605 (let* ((default-directory (tramp-temporary-file-directory))
5606 ;; If we omit the conditional, we would use
5607 ;; `undecided-dos' in some cases. With the conditional,
5608 ;; we use nil in these cases. Which one is right?
5609 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5610 (> emacs-major-version 20))
5611 tramp-dos-coding-system))
5612 (p (if (and user (not (string= user "")))
5613 (apply #'start-process bufnam buf login-program
5614 real-host "-l" user login-args)
5615 (apply #'start-process bufnam buf login-program
5616 real-host login-args)))
5618 (tramp-set-process-query-on-exit-flag p nil)
5621 (tramp-process-actions p multi-method method user host
5622 tramp-actions-before-shell)
5623 (tramp-message 7 "Initializing remote shell")
5624 (tramp-open-connection-setup-interactive-shell
5625 p multi-method method user host)
5626 (tramp-post-connection multi-method method user host)))))
5628 (defun tramp-open-connection-su (multi-method method user host)
5629 "Open a connection using the `su' program with METHOD.
5630 This starts `su - USER', then waits for a password prompt. The HOST
5631 name must be equal to the local host name or to `localhost'.
5633 If USER is nil, uses value returned by user-login-name instead.
5635 Recognition of the remote shell prompt is based on the variables
5636 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5637 set up correctly. Note that the other user may have a different shell
5638 prompt than you do, so it is not at all unlikely that the variable
5639 `shell-prompt-pattern' is set up wrongly!"
5641 (when (tramp-method-out-of-band-p multi-method method user host)
5642 (error "Cannot use out-of-band method `%s' with `su' connection method"
5644 (unless (or (string-match (concat "^" (regexp-quote host))
5646 (string= "localhost" host)
5649 "Cannot connect to different host `%s' with `su' connection method"
5651 (tramp-pre-connection multi-method method user host tramp-chunksize)
5652 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5653 (or user "<root>") method)
5654 (let ((process-environment (copy-sequence process-environment)))
5655 (setenv "TERM" tramp-terminal-type)
5656 (let* ((default-directory (tramp-temporary-file-directory))
5657 ;; If we omit the conditional, we use `undecided-dos' in
5658 ;; some cases. With the conditional, we use nil in these
5659 ;; cases. What's the difference? Which one is right?
5660 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5661 (> emacs-major-version 20))
5662 tramp-dos-coding-system))
5663 (p (apply 'start-process
5664 (tramp-buffer-name multi-method method user host)
5665 (tramp-get-buffer multi-method method user host)
5666 (tramp-get-method-parameter
5668 (tramp-find-method multi-method method user host)
5669 user host 'tramp-login-program)
5672 (format-spec x `((?u . ,(or user "root")))))
5673 (tramp-get-method-parameter
5675 (tramp-find-method multi-method method user host)
5676 user host 'tramp-login-args))))
5679 (tramp-set-process-query-on-exit-flag p nil)
5680 (set-buffer (tramp-get-buffer multi-method method user host))
5681 (tramp-process-actions p multi-method method user host
5682 tramp-actions-before-shell)
5683 (tramp-open-connection-setup-interactive-shell
5684 p multi-method method user host)
5685 (tramp-post-connection multi-method method
5688 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5689 ;; handle not giving a user name in the "file name".
5691 ;; This is more difficult than for the single-hop method. In the
5692 ;; multi-hop-method, the desired behaviour should be that the
5693 ;; user must specify names for the telnet hops of which the user
5694 ;; name is different than the "original" name (or different from
5695 ;; the previous hop.
5696 (defun tramp-open-connection-multi (multi-method method user host)
5697 "Open a multi-hop connection using METHOD.
5698 This uses a slightly changed file name syntax. The idea is to say
5699 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5700 This will use telnet to log in as u1 to h1, then use rsh from there to
5701 log in as u2 to h2."
5703 (unless multi-method
5704 (error "Multi-hop open connection function called on non-multi method"))
5705 (when (tramp-method-out-of-band-p multi-method method user host)
5706 (error "No out of band multi-hop connections"))
5707 (unless (and (arrayp method) (not (stringp method)))
5708 (error "METHOD must be an array of strings for multi methods"))
5709 (unless (and (arrayp user) (not (stringp user)))
5710 (error "USER must be an array of strings for multi methods"))
5711 (unless (and (arrayp host) (not (stringp host)))
5712 (error "HOST must be an array of strings for multi methods"))
5713 (unless (and (= (length method) (length user))
5714 (= (length method) (length host)))
5715 (error "Arrays METHOD, USER, HOST must have equal length"))
5716 (tramp-pre-connection multi-method method user host tramp-chunksize)
5717 (tramp-message 7 "Opening `%s' connection..." multi-method)
5718 (let ((process-environment (copy-sequence process-environment)))
5719 (setenv "TERM" tramp-terminal-type)
5720 (let* ((default-directory (tramp-temporary-file-directory))
5721 ;; If we omit the conditional, we use `undecided-dos' in
5722 ;; some cases. With the conditional, we use nil in these
5723 ;; cases. What's the difference? Which one is right?
5724 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5725 (> emacs-major-version 20))
5726 tramp-dos-coding-system))
5727 (p (start-process (tramp-buffer-name multi-method method user host)
5728 (tramp-get-buffer multi-method method user host)
5729 tramp-multi-sh-program))
5730 (num-hops (length method))
5732 (tramp-set-process-query-on-exit-flag p nil)
5733 (tramp-message 9 "Waiting 60s for local shell to come up...")
5734 (unless (tramp-wait-for-regexp
5735 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5736 shell-prompt-pattern tramp-shell-prompt-pattern))
5737 (pop-to-buffer (buffer-name))
5739 (error "Couldn't find local shell prompt"))
5740 ;; Now do all the connections as specified.
5741 (while (< i num-hops)
5742 (let* ((m (aref method i))
5745 (entry (assoc m tramp-multi-connection-function-alist))
5746 (multi-func (nth 1 entry))
5747 (command (nth 2 entry)))
5748 ;; The multi-funcs don't need to do save-match-data, as that
5750 (funcall multi-func p m u h command)
5753 (tramp-open-connection-setup-interactive-shell
5754 p multi-method method user host)
5755 (tramp-post-connection multi-method method user host)))))
5757 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5758 ;; of no user name provided. Hack to make it work as it did before:
5759 ;; changed `user' to `(or user (user-login-name))' in the places where
5760 ;; the value is actually used.
5761 (defun tramp-multi-connect-telnet (p method user host command)
5762 "Issue `telnet' command.
5763 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5764 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5765 the host name, and `%n' is replaced with an end of line character, as
5766 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5769 If USER is nil, uses the return value of (user-login-name) instead."
5770 (let ((cmd (format-spec command
5771 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5772 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5775 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5776 (process-send-string p cmd)
5777 (tramp-process-multi-actions p method user host
5778 tramp-multi-actions)))
5780 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5781 ;; of no user name provided. Hack to make it work as it did before:
5782 ;; changed `user' to `(or user (user-login-name))' in the places where
5783 ;; the value is actually used.
5784 (defun tramp-multi-connect-rlogin (p method user host command)
5785 "Issue `rlogin' command.
5786 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5787 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5788 with the user name, `%h' will be replaced with the host name, and `%n'
5789 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5790 `%%' if you want to use a literal percent character.
5792 If USER is nil, uses the return value of (user-login-name) instead."
5793 (let ((cmd (format-spec command `((?h . ,host)
5794 (?u . ,(or user (user-login-name)))
5795 (?n . ,tramp-rsh-end-of-line))))
5796 (cmd1 (format-spec command `((?h . ,host)
5797 (?u . ,(or user (user-login-name)))
5801 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5802 (process-send-string p cmd)
5803 (tramp-process-multi-actions p method user host
5804 tramp-multi-actions)))
5806 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5807 ;; of no user name provided. Hack to make it work as it did before:
5808 ;; changed `user' to `(or user (user-login-name))' in the places where
5809 ;; the value is actually used.
5810 (defun tramp-multi-connect-su (p method user host command)
5811 "Issue `su' command.
5812 Uses shell COMMAND to issue a `su' command to log in as USER on
5813 HOST. The HOST name is ignored, this just changes the user id on the
5814 host currently logged in to.
5816 If USER is nil, uses the return value of (user-login-name) instead.
5818 You can use percent escapes in the COMMAND. `%u' is replaced with the
5819 user name, and `%n' is replaced with the value of
5820 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5822 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5823 (?n . ,tramp-rsh-end-of-line))))
5824 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5828 (tramp-message 9 "Sending su command `%s'" cmd1)
5829 (process-send-string p cmd)
5830 (tramp-process-multi-actions p method user host
5831 tramp-multi-actions)))
5833 ;; Utility functions.
5835 (defun tramp-accept-process-output
5836 (&optional process timeout timeout-msecs)
5837 "Like `accept-process-output' for Tramp processes.
5838 This is needed in order to hide `last-coding-system-used', which is set
5839 for process communication also."
5840 (let (last-coding-system-used)
5841 (accept-process-output process timeout timeout-msecs)))
5843 (defun tramp-wait-for-regexp (proc timeout regexp)
5844 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5845 Expects the output of PROC to be sent to the current buffer. Returns
5846 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5849 (start-time (current-time)))
5851 ;; Work around a bug in XEmacs 21, where the timeout
5852 ;; expires faster than it should. This degenerates
5853 ;; to polling for buggy XEmacsen, but oh, well.
5854 (while (and (not found)
5855 (< (tramp-time-diff (current-time) start-time)
5857 (with-timeout (timeout)
5859 (tramp-accept-process-output proc 1)
5860 (unless (memq (process-status proc) '(run open))
5861 (error "Process has died"))
5862 (goto-char (point-min))
5863 (setq found (re-search-forward regexp nil t))))))
5866 (tramp-accept-process-output proc 1)
5867 (unless (memq (process-status proc) '(run open))
5868 (error "Process has died"))
5869 (goto-char (point-min))
5870 (setq found (re-search-forward regexp nil t)))))
5871 (when tramp-debug-buffer
5873 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5874 tramp-current-user tramp-current-host)
5875 (point-min) (point-max))
5879 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5880 tramp-current-user tramp-current-host))
5881 (goto-char (point-max))
5882 (insert "[[Regexp `" regexp "' not found"
5883 (if timeout (format " in %d secs" timeout) "")
5887 (defun tramp-wait-for-shell-prompt (proc timeout)
5888 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5889 See `tramp-wait-for-regexp' for more details.
5890 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5891 and `tramp-shell-prompt-pattern'."
5892 (tramp-wait-for-regexp
5894 (format "\\(%s\\|%s\\)\\'"
5895 shell-prompt-pattern tramp-shell-prompt-pattern)))
5897 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5898 "Wait for shell prompt and barf if none appears.
5899 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5900 seconds. If not, it produces an error message with the given ERROR-ARGS."
5901 (unless (tramp-wait-for-shell-prompt proc timeout)
5902 (pop-to-buffer (buffer-name))
5903 (apply 'error error-args)))
5905 (defun tramp-enter-password (p prompt user host)
5906 "Prompt for a password and send it to the remote end.
5907 Uses PROMPT as a prompt and sends the password to process P."
5908 (let ((pw (tramp-read-passwd user host prompt)))
5910 (process-send-string
5912 (or (tramp-get-method-parameter
5913 tramp-current-multi-method
5914 tramp-current-method
5917 'tramp-password-end-of-line)
5918 tramp-default-password-end-of-line)))))
5920 ;; HHH: Not Changed. This might handle the case where USER is not
5921 ;; given in the "File name" very poorly. Then, the local
5922 ;; variable tramp-current-user will be set to nil.
5923 (defun tramp-pre-connection (multi-method method user host chunksize)
5924 "Do some setup before actually logging in.
5925 METHOD, USER and HOST specify the connection."
5926 (set-buffer (tramp-get-buffer multi-method method user host))
5927 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5928 (set (make-local-variable 'tramp-current-method) method)
5929 (set (make-local-variable 'tramp-current-user) user)
5930 (set (make-local-variable 'tramp-current-host) host)
5931 (set (make-local-variable 'tramp-chunksize) chunksize)
5932 (set (make-local-variable 'inhibit-eol-conversion) nil)
5935 (defun tramp-open-connection-setup-interactive-shell
5936 (p multi-method method user host)
5937 "Set up an interactive shell.
5938 Mainly sets the prompt and the echo correctly. P is the shell process
5939 to set up. METHOD, USER and HOST specify the connection."
5940 ;; Wait a bit in case the remote end feels like sending a little
5941 ;; junk first. It seems that fencepost.gnu.org does this when doing
5942 ;; a Kerberos login.
5944 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5945 (tramp-process-initial-commands p multi-method method user host
5946 tramp-initial-commands)
5947 ;; It is useful to set the prompt in the following command because
5948 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5949 ;; about and thus /bin/sh will display a strange prompt. For
5950 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
5951 ;; the current working directory but /bin/sh will display a dollar
5952 ;; sign. The following command line sets $PS1 to a sane value, and
5953 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
5954 ;; Pittman reports that the unusual positioning of the single quotes
5955 ;; makes it work under `rc', too. We also unset the variable $ENV
5956 ;; because that is read by some sh implementations (eg, bash when
5957 ;; called as sh) on startup; this way, we avoid the startup file
5959 (tramp-send-command-internal
5960 multi-method method user host
5961 (format "exec env 'ENV=' 'PS1=$ ' %s"
5962 (tramp-get-method-parameter
5963 multi-method method user host 'tramp-remote-sh))
5964 (format "remote `%s' to come up"
5965 (tramp-get-method-parameter
5966 multi-method method user host 'tramp-remote-sh)))
5967 (tramp-barf-if-no-shell-prompt
5969 "Remote `%s' didn't come up. See buffer `%s' for details"
5970 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
5972 (tramp-message 8 "Setting up remote shell environment")
5973 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5974 (tramp-send-command-internal multi-method method user host
5975 "stty -inlcr -echo kill '^U'")
5977 ;; Ignore garbage after stty command.
5978 (tramp-send-command-internal multi-method method user host
5981 (tramp-send-command-internal multi-method method user host
5982 "TERM=dumb; export TERM")
5984 ;; Check whether the remote host suffers from buggy `send-process-string'.
5985 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
5986 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
5987 ;; Emacs makes a hack when this host type is detected locally. It cannot
5988 ;; handle remote hosts, though.
5989 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
5990 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
5991 (tramp-send-command-internal multi-method method user host
5992 "(uname -sr) 2>/dev/null")
5993 (goto-char (point-min))
5994 (when (looking-at "FreeBSD")
5995 (setq tramp-chunksize 500)))
5997 ;; Try to set up the coding system correctly.
5998 ;; CCC this can't be the right way to do it. Hm.
6001 (tramp-message 9 "Determining coding system")
6002 (tramp-send-command-internal multi-method method user host
6003 "echo foo ; echo bar")
6004 (goto-char (point-min))
6005 (if (featurep 'mule)
6006 ;; Use MULE to select the right EOL convention for communicating
6007 ;; with the process.
6008 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6009 cs-decode cs-encode)
6010 (when (symbolp cs) (setq cs (cons cs cs)))
6011 (setq cs-decode (car cs))
6012 (setq cs-encode (cdr cs))
6013 (unless cs-decode (setq cs-decode 'undecided))
6014 (unless cs-encode (setq cs-encode 'undecided))
6015 (setq cs-encode (tramp-coding-system-change-eol-conversion
6017 (when (search-forward "\r" nil t)
6018 (setq cs-decode (tramp-coding-system-change-eol-conversion
6020 (set-buffer-process-coding-system cs-decode cs-encode))
6021 ;; Look for ^M and do something useful if found.
6022 (when (search-forward "\r" nil t)
6023 ;; We have found a ^M but cannot frob the process coding system
6024 ;; because we're running on a non-MULE Emacs. Let's try
6027 (tramp-message 9 "Trying `stty -onlcr'")
6028 (tramp-send-command-internal multi-method method user host
6032 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6033 (tramp-send-command-internal
6034 multi-method method user host
6035 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6037 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6038 (tramp-send-command-internal multi-method method user host
6039 "set +o vi +o emacs")
6041 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6042 (tramp-send-command-internal
6043 multi-method method user host
6044 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6046 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6047 (tramp-send-command-internal multi-method method user host
6050 (tramp-message 9 "Setting shell prompt")
6051 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6052 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6053 ;; the last time we sent a command, to avoid tramp-send-command to send
6054 ;; "echo are you awake".
6055 (setq tramp-last-cmd-time (current-time))
6057 multi-method method user host
6058 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6059 tramp-rsh-end-of-line
6061 tramp-rsh-end-of-line))
6062 (tramp-wait-for-output))
6064 (defun tramp-post-connection (multi-method method user host)
6065 "Prepare a remote shell before being able to work on it.
6066 METHOD, USER and HOST specify the connection.
6067 Among other things, this finds a shell which groks tilde expansion,
6068 tries to find an `ls' command which groks the `-n' option, sets the
6069 locale to C and sets up the remote shell search path."
6070 ;; Search for a good shell before searching for a command which
6071 ;; checks if a file exists. This is done because Tramp wants to use
6072 ;; "test foo; echo $?" to check if various conditions hold, and
6073 ;; there are buggy /bin/sh implementations which don't execute the
6074 ;; "echo $?" part if the "test" part has an error. In particular,
6075 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6076 ;; with buggy /bin/sh implementations will have a working bash or
6078 (tramp-find-shell multi-method method user host)
6079 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6080 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6081 ;; output of (tramp-find-shell) to show up along with the output of
6082 ;; (tramp-find-ls-command) testing.
6084 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6085 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6087 ;; After much debugging I couldn't find any problem with the implementation
6088 ;; of that function though. The workaround stays for me at least. :/
6090 ;; Daniel Pittman <daniel@danann.net>
6093 (tramp-find-file-exists-command multi-method method user host)
6094 (make-local-variable 'tramp-ls-command)
6095 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6096 (unless tramp-ls-command
6099 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6100 (setq tramp-ls-command
6101 (tramp-find-executable multi-method method user host
6102 "ls" tramp-remote-path nil)))
6103 (unless tramp-ls-command
6104 (error "Fatal error: Couldn't find remote executable `ls'"))
6105 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6107 (tramp-send-command multi-method method user host
6108 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6109 "return $1" tramp-rsh-end-of-line
6111 (tramp-wait-for-output)
6112 ;; Set remote PATH variable.
6113 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6114 ;; Tell remote shell to use standard time format, needed for
6115 ;; parsing `ls -l' output.
6116 (tramp-send-command multi-method method user host
6117 "LC_TIME=C; export LC_TIME; echo huhu")
6118 (tramp-wait-for-output)
6119 (tramp-send-command multi-method method user host
6120 "mesg n; echo huhu")
6121 (tramp-wait-for-output)
6122 (tramp-send-command multi-method method user host
6123 "biff n ; echo huhu")
6124 (tramp-wait-for-output)
6125 ;; Unalias ls(1) to work around issues with those silly people who make it
6126 ;; spit out ANSI escapes or whatever.
6127 (tramp-send-command multi-method method user host
6128 "unalias ls; echo huhu")
6129 (tramp-wait-for-output)
6130 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6131 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6132 ;; for otherwise the shell crashes.
6134 (make-local-variable 'tramp-test-groks-nt)
6135 (tramp-send-command multi-method method user host
6137 (tramp-wait-for-output)
6138 (goto-char (point-min))
6139 (setq tramp-test-groks-nt
6140 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6141 (unless tramp-test-groks-nt
6143 multi-method method user host
6144 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6145 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6147 (tramp-wait-for-output)
6148 ;; Send the fallback `uudecode' script.
6150 (tramp-send-string multi-method method user host tramp-uudecode)
6151 (tramp-wait-for-output)
6154 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6155 (let ((tramp-remote-perl
6156 (or (tramp-find-executable multi-method method user host
6157 "perl5" tramp-remote-path nil)
6158 (tramp-find-executable multi-method method user host
6159 "perl" tramp-remote-path nil))))
6160 (when tramp-remote-perl
6161 (tramp-set-connection-property "perl" tramp-remote-perl
6162 multi-method method user host)
6163 (unless (tramp-method-out-of-band-p multi-method method user host)
6164 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6166 multi-method method user host
6167 (concat "tramp_encode () {\n"
6168 (format tramp-perl-encode tramp-remote-perl)
6171 (tramp-wait-for-output)
6173 multi-method method user host
6174 (concat "tramp_encode_with_module () {\n"
6175 (format tramp-perl-encode-with-module tramp-remote-perl)
6178 (tramp-wait-for-output)
6179 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6181 multi-method method user host
6182 (concat "tramp_decode () {\n"
6183 (format tramp-perl-decode tramp-remote-perl)
6186 (tramp-wait-for-output)
6188 multi-method method user host
6189 (concat "tramp_decode_with_module () {\n"
6190 (format tramp-perl-decode-with-module tramp-remote-perl)
6193 (tramp-wait-for-output))))
6196 (let ((ln (tramp-find-executable multi-method method user host
6197 "ln" tramp-remote-path nil)))
6199 (tramp-set-connection-property "ln" ln multi-method method user host)))
6201 ;; Find the right encoding/decoding commands to use.
6202 (unless (tramp-method-out-of-band-p multi-method method user host)
6203 (tramp-find-inline-encoding multi-method method user host))
6204 ;; If encoding/decoding command are given, test to see if they work.
6205 ;; CCC: Maybe it would be useful to run the encoder both locally and
6206 ;; remotely to see if they produce the same result.
6207 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6208 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6209 (magic-string "xyzzy"))
6210 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6211 (tramp-kill-process multi-method method user host)
6212 ;; Improve error message and/or error check.
6214 "Must give both decoding and encoding command in method definition"))
6215 (when (and rem-enc rem-dec)
6218 "Checking to see if encoding/decoding commands work on remote host...")
6220 multi-method method user host
6221 (format "echo %s | %s | %s"
6222 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6223 (tramp-wait-for-output)
6224 (unless (looking-at (regexp-quote magic-string))
6225 (tramp-kill-process multi-method method user host)
6226 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6230 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6232 ;; CCC: We should either implement a Perl version of base64 encoding
6233 ;; and decoding. Then we just use that in the last item. The other
6234 ;; alternative is to use the Perl version of UU encoding. But then
6235 ;; we need a Lisp version of uuencode.
6237 ;; Old text from documentation of tramp-methods:
6238 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6239 ;; of the base64 methods instead since base64 encoding is much more
6240 ;; reliable and the commands are more standardized between the different
6241 ;; Unix versions. But if you can't use base64 for some reason, please
6242 ;; note that the default uudecode command does not work well for some
6243 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6244 ;; the following command for uudecode:
6246 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6248 ;; For Irix, no solution is known yet.
6250 (defvar tramp-coding-commands
6251 '(("mimencode -b" "mimencode -u -b"
6252 base64-encode-region base64-decode-region)
6253 ("mmencode -b" "mmencode -u -b"
6254 base64-encode-region base64-decode-region)
6255 ("recode data..base64" "recode base64..data"
6256 base64-encode-region base64-decode-region)
6257 ("uuencode xxx" "uudecode -o /dev/stdout"
6258 tramp-uuencode-region uudecode-decode-region)
6259 ("uuencode xxx" "uudecode -o -"
6260 tramp-uuencode-region uudecode-decode-region)
6261 ("uuencode xxx" "uudecode -p"
6262 tramp-uuencode-region uudecode-decode-region)
6263 ("uuencode xxx" "tramp_uudecode"
6264 tramp-uuencode-region uudecode-decode-region)
6265 ("tramp_encode_with_module" "tramp_decode_with_module"
6266 base64-encode-region base64-decode-region)
6267 ("tramp_encode" "tramp_decode"
6268 base64-encode-region base64-decode-region))
6269 "List of coding commands for inline transfer.
6270 Each item is a list that looks like this:
6272 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6274 The REMOTE-ENCODING should be a string, giving a command accepting a
6275 plain file on standard input and writing the encoded file to standard
6276 output. The REMOTE-DECODING should also be a string, giving a command
6277 accepting an encoded file on standard input and writing the decoded
6278 file to standard output.
6280 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6281 symbols, giving functions. If they are strings, then they can contain
6282 the \"%s\" format specifier. If that specifier is present, the input
6283 filename will be put into the command line at that spot. If the
6284 specifier is not present, the input should be read from standard
6287 If they are functions, they will be called with two arguments, start
6288 and end of region, and are expected to replace the region contents
6289 with the encoded or decoded results, respectively.")
6291 (defun tramp-find-inline-encoding (multi-method method user host)
6292 "Find an inline transfer encoding that works.
6293 Goes through the list `tramp-coding-commands'."
6294 (let ((commands tramp-coding-commands)
6297 (while (and commands (null found))
6298 (setq item (pop commands))
6300 (let ((rem-enc (nth 0 item))
6301 (rem-dec (nth 1 item))
6302 (loc-enc (nth 2 item))
6303 (loc-dec (nth 3 item)))
6304 ;; Check if remote encoding and decoding commands can be
6305 ;; called remotely with null input and output. This makes
6306 ;; sure there are no syntax errors and the command is really
6307 ;; found. Note that we do not redirect stdout to /dev/null,
6308 ;; for two reaons: when checking the decoding command, we
6309 ;; actually check the output it gives. And also, when
6310 ;; redirecting "mimencode" output to /dev/null, then as root
6311 ;; it might change the permissions of /dev/null!
6312 (tramp-message-for-buffer
6313 multi-method method user host 9
6314 "Checking remote encoding command `%s' for sanity" rem-enc)
6315 (unless (zerop (tramp-send-command-and-check
6316 multi-method method user host
6317 (format "%s </dev/null" rem-enc) t))
6318 (throw 'wont-work nil))
6319 (tramp-message-for-buffer
6320 multi-method method user host 9
6321 "Checking remote decoding command `%s' for sanity" rem-dec)
6322 (unless (zerop (tramp-send-command-and-check
6323 multi-method method user host
6324 (format "echo %s | %s | %s"
6325 magic rem-enc rem-dec) t))
6326 (throw 'wont-work nil))
6328 (goto-char (point-min))
6329 (unless (looking-at (regexp-quote magic))
6330 (throw 'wont-work nil)))
6331 ;; If the local encoder or decoder is a string, the
6332 ;; corresponding command has to work locally.
6333 (when (stringp loc-enc)
6334 (tramp-message-for-buffer
6335 multi-method method user host 9
6336 "Checking local encoding command `%s' for sanity" loc-enc)
6337 (unless (zerop (tramp-call-local-coding-command
6339 (throw 'wont-work nil)))
6340 (when (stringp loc-dec)
6341 (tramp-message-for-buffer
6342 multi-method method user host 9
6343 "Checking local decoding command `%s' for sanity" loc-dec)
6344 (unless (zerop (tramp-call-local-coding-command
6346 (throw 'wont-work nil)))
6347 ;; CCC: At this point, maybe we should check that the output
6348 ;; of the commands is correct. But for the moment we will
6349 ;; assume that commands working on empty input will also
6350 ;; work in practice.
6351 (setq found item))))
6352 ;; Did we find something? If not, issue error. If so,
6353 ;; set connection properties.
6355 (error "Couldn't find an inline transfer encoding"))
6356 (let ((rem-enc (nth 0 found))
6357 (rem-dec (nth 1 found))
6358 (loc-enc (nth 2 found))
6359 (loc-dec (nth 3 found)))
6360 (tramp-message 10 "Using remote encoding %s" rem-enc)
6361 (tramp-set-remote-encoding multi-method method user host rem-enc)
6362 (tramp-message 10 "Using remote decoding %s" rem-dec)
6363 (tramp-set-remote-decoding multi-method method user host rem-dec)
6364 (tramp-message 10 "Using local encoding %s" loc-enc)
6365 (tramp-set-local-encoding multi-method method user host loc-enc)
6366 (tramp-message 10 "Using local decoding %s" loc-dec)
6367 (tramp-set-local-decoding multi-method method user host loc-dec))))
6369 (defun tramp-call-local-coding-command (cmd input output)
6370 "Call the local encoding or decoding command.
6371 If CMD contains \"%s\", provide input file INPUT there in command.
6372 Otherwise, INPUT is passed via standard input.
6373 INPUT can also be nil which means `/dev/null'.
6374 OUTPUT can be a string (which specifies a filename), or t (which
6375 means standard output and thus the current buffer), or nil (which
6378 tramp-encoding-shell ;program
6379 (when (and input (not (string-match "%s" cmd)))
6381 (if (eq output t) t nil) ;output
6383 tramp-encoding-command-switch
6384 ;; actual shell command
6386 (if (string-match "%s" cmd) (format cmd input) cmd)
6387 (if (stringp output) (concat "> " output) ""))))
6389 (defun tramp-maybe-open-connection (multi-method method user host)
6390 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6391 Does not do anything if a connection is already open, but re-opens the
6392 connection if a previous connection has died for some reason."
6393 (let ((p (get-buffer-process
6394 (tramp-get-buffer multi-method method user host)))
6396 ;; If too much time has passed since last command was sent, look
6397 ;; whether process is still alive. If it isn't, kill it. When
6398 ;; using ssh, it can sometimes happen that the remote end has hung
6399 ;; up but the local ssh client doesn't recognize this until it
6400 ;; tries to send some data to the remote end. So that's why we
6401 ;; try to send a command from time to time, then look again
6402 ;; whether the process is really alive.
6404 (set-buffer (tramp-get-buffer multi-method method user host))
6405 (when (and tramp-last-cmd-time
6406 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6407 p (processp p) (memq (process-status p) '(run open)))
6409 multi-method method user host "echo are you awake" nil t)
6410 (unless (tramp-wait-for-output 10)
6414 (unless (and p (processp p) (memq (process-status p) '(run open)))
6415 (when (and p (processp p))
6417 (let ((process-connection-type tramp-process-connection-type))
6418 (funcall (tramp-get-method-parameter
6420 (tramp-find-method multi-method method user host)
6421 user host 'tramp-connection-function)
6422 multi-method method user host)))))
6424 (defun tramp-send-command
6425 (multi-method method user host command &optional noerase neveropen)
6426 "Send the COMMAND to USER at HOST (logged in using METHOD).
6427 Erases temporary buffer before sending the command (unless NOERASE
6429 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6430 connection. This is meant to be used from
6431 `tramp-maybe-open-connection' only."
6433 (tramp-maybe-open-connection multi-method method user host))
6434 (setq tramp-last-cmd-time (current-time))
6435 (setq tramp-last-cmd command)
6436 (when tramp-debug-buffer
6438 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6439 (goto-char (point-max))
6440 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6442 (set-buffer (tramp-get-buffer multi-method method user host))
6443 (unless noerase (erase-buffer))
6444 (setq proc (get-buffer-process (current-buffer)))
6445 (process-send-string proc
6446 (concat command tramp-rsh-end-of-line))))
6448 (defun tramp-send-command-internal
6449 (multi-method method user host command &optional msg)
6450 "Send command to remote host and wait for success.
6451 Sends COMMAND, then waits 30 seconds for shell prompt."
6452 (tramp-send-command multi-method method user host command t t)
6454 (tramp-message 9 "Waiting 30s for %s..." msg))
6455 (tramp-barf-if-no-shell-prompt
6457 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6459 (defun tramp-wait-for-output (&optional timeout)
6460 "Wait for output from remote rsh command."
6461 (let ((proc (get-buffer-process (current-buffer)))
6463 (start-time (current-time))
6464 (start-point (point))
6465 (end-of-output (concat "^"
6466 (regexp-quote tramp-end-of-output)
6468 ;; Algorithm: get waiting output. See if last line contains
6469 ;; end-of-output sentinel. If not, wait a bit and again get
6470 ;; waiting output. Repeat until timeout expires or end-of-output
6471 ;; sentinel is seen. Will hang if timeout is nil and
6472 ;; end-of-output sentinel never appears.
6475 ;; Work around an XEmacs bug, where the timeout expires
6476 ;; faster than it should. This degenerates into polling
6477 ;; for buggy XEmacsen, but oh, well.
6478 (while (and (not found)
6479 (< (tramp-time-diff (current-time) start-time)
6481 (with-timeout (timeout)
6483 (tramp-accept-process-output proc 1)
6484 (unless (memq (process-status proc) '(run open))
6485 (error "Process has died"))
6486 (goto-char (point-max))
6488 (setq found (looking-at end-of-output))))))
6491 (tramp-accept-process-output proc 1)
6492 (unless (memq (process-status proc) '(run open))
6493 (error "Process has died"))
6494 (goto-char (point-max))
6496 (setq found (looking-at end-of-output))))))
6497 ;; At this point, either the timeout has expired or we have found
6498 ;; the end-of-output sentinel.
6500 (goto-char (point-max))
6502 (delete-region (point) (point-max)))
6503 ;; If processing echoes, look for it in the first line and delete.
6504 (when tramp-process-echoes
6506 (goto-char start-point)
6507 (when (looking-at (regexp-quote tramp-last-cmd))
6508 (delete-region (point) (progn (forward-line 1) (point))))))
6509 ;; Add output to debug buffer if appropriate.
6510 (when tramp-debug-buffer
6512 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6513 tramp-current-user tramp-current-host)
6514 (point-min) (point-max))
6518 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6519 tramp-current-user tramp-current-host))
6520 (goto-char (point-max))
6521 (insert "[[Remote prompt `" end-of-output "' not found"
6522 (if timeout (format " in %d secs" timeout) "")
6524 (goto-char (point-min))
6525 ;; Return value is whether end-of-output sentinel was found.
6528 (defun tramp-send-command-and-check (multi-method method user host command
6530 "Run COMMAND and check its exit status.
6531 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6532 Sends `echo $?' along with the COMMAND for checking the exit status. If
6533 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6535 If the optional argument SUBSHELL is non-nil, the command is executed in
6536 a subshell, ie surrounded by parentheses."
6537 (tramp-send-command multi-method method user host
6538 (concat (if subshell "( " "")
6540 (if command " 2>/dev/null; " "")
6541 "echo tramp_exit_status $?"
6542 (if subshell " )" " ")))
6543 (tramp-wait-for-output)
6544 (goto-char (point-max))
6545 (unless (search-backward "tramp_exit_status " nil t)
6546 (error "Couldn't find exit status of `%s'" command))
6547 (skip-chars-forward "^ ")
6548 (read (current-buffer)))
6550 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6551 signal fmt &rest args)
6552 "Run COMMAND, check exit status, throw error if exit status not okay.
6553 Similar to `tramp-send-command-and-check' but accepts two more arguments
6554 FMT and ARGS which are passed to `error'."
6555 (unless (zerop (tramp-send-command-and-check
6556 multi-method method user host command subshell))
6557 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6559 (pop-to-buffer (current-buffer))
6560 (funcall 'signal signal (apply 'format fmt args))))
6562 ;; It seems that Tru64 Unix does not like it if long strings are sent
6563 ;; to it in one go. (This happens when sending the Perl
6564 ;; `file-attributes' implementation, for instance.) Therefore, we
6565 ;; have this function which waits a bit at each line.
6566 (defun tramp-send-string
6567 (multi-method method user host string)
6568 "Send the STRING to USER at HOST using METHOD.
6570 The STRING is expected to use Unix line-endings, but the lines sent to
6571 the remote host use line-endings as defined in the variable
6572 `tramp-rsh-end-of-line'."
6573 (let ((proc (get-buffer-process
6574 (tramp-get-buffer multi-method method user host))))
6576 (error "Can't send string to remote host -- not logged in"))
6578 (when tramp-debug-buffer
6580 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6581 (goto-char (point-max))
6582 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6583 ;; replace "\n" by `tramp-rsh-end-of-line'
6585 (mapconcat 'identity
6586 (split-string string "\n")
6587 tramp-rsh-end-of-line))
6588 (unless (or (string= string "")
6589 (string-equal (substring string -1) tramp-rsh-end-of-line))
6590 (setq string (concat string tramp-rsh-end-of-line)))
6592 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6594 (end (length string)))
6596 (tramp-message-for-buffer
6597 multi-method method user host 10
6598 "Sending chunk from %s to %s"
6599 pos (min (+ pos tramp-chunksize) end))
6600 (process-send-string
6601 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6602 (setq pos (+ pos tramp-chunksize))
6604 (process-send-string proc string))))
6606 (defun tramp-send-eof (multi-method method user host)
6607 "Send EOF to the remote end.
6608 METHOD, HOST and USER specify the connection."
6609 (let ((proc (get-buffer-process
6610 (tramp-get-buffer multi-method method user host))))
6612 (error "Can't send EOF to remote host -- not logged in"))
6613 (process-send-eof proc)))
6614 ; (process-send-string proc "\^D")))
6616 (defun tramp-kill-process (multi-method method user host)
6617 "Kill the connection process used by Tramp.
6618 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6619 (let ((proc (get-buffer-process
6620 (tramp-get-buffer multi-method method user host))))
6621 (kill-process proc)))
6623 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6624 "Erase buffer, then discard subsequent garbage.
6625 If `tramp-discard-garbage' is nil, just erase buffer."
6626 (if (not tramp-discard-garbage)
6628 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6629 (when tramp-debug-buffer
6631 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6632 (goto-char (point-max))
6633 (tramp-insert-with-face
6634 'bold (format "Additional characters detected\n")))))))
6636 (defun tramp-mode-string-to-int (mode-string)
6637 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6638 (let* ((mode-chars (string-to-vector mode-string))
6639 (owner-read (aref mode-chars 1))
6640 (owner-write (aref mode-chars 2))
6641 (owner-execute-or-setid (aref mode-chars 3))
6642 (group-read (aref mode-chars 4))
6643 (group-write (aref mode-chars 5))
6644 (group-execute-or-setid (aref mode-chars 6))
6645 (other-read (aref mode-chars 7))
6646 (other-write (aref mode-chars 8))
6647 (other-execute-or-sticky (aref mode-chars 9)))
6651 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6652 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6654 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6655 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6656 (case owner-execute-or-setid
6657 (?x (tramp-octal-to-decimal "00100"))
6658 (?S (tramp-octal-to-decimal "04000"))
6659 (?s (tramp-octal-to-decimal "04100"))
6661 (t (error "Fourth char `%c' must be one of `xsS-'"
6662 owner-execute-or-setid)))
6664 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6665 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6667 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6668 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6669 (case group-execute-or-setid
6670 (?x (tramp-octal-to-decimal "00010"))
6671 (?S (tramp-octal-to-decimal "02000"))
6672 (?s (tramp-octal-to-decimal "02010"))
6674 (t (error "Seventh char `%c' must be one of `xsS-'"
6675 group-execute-or-setid)))
6677 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6678 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6680 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6681 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6682 (case other-execute-or-sticky
6683 (?x (tramp-octal-to-decimal "00001"))
6684 (?T (tramp-octal-to-decimal "01000"))
6685 (?t (tramp-octal-to-decimal "01001"))
6687 (t (error "Tenth char `%c' must be one of `xtT-'"
6688 other-execute-or-sticky)))))))
6690 (defun tramp-convert-file-attributes (multi-method method user host attr)
6691 "Convert file-attributes ATTR generated by perl script or ls.
6692 Convert file mode bits to string and set virtual device number.
6694 (unless (stringp (nth 8 attr))
6695 ;; Convert file mode bits to string.
6696 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6697 ;; Set virtual device number.
6698 (setcar (nthcdr 11 attr)
6699 (tramp-get-device multi-method method user host))
6702 (defun tramp-get-device (multi-method method user host)
6703 "Returns the virtual device number.
6704 If it doesn't exist, generate a new one."
6705 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6706 (unless (assoc string tramp-devices)
6707 (add-to-list 'tramp-devices
6708 (list string (length tramp-devices))))
6709 (list -1 (nth 1 (assoc string tramp-devices)))))
6711 (defun tramp-file-mode-from-int (mode)
6712 "Turn an integer representing a file mode into an ls(1)-like string."
6713 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6714 (user (logand (lsh mode -6) 7))
6715 (group (logand (lsh mode -3) 7))
6716 (other (logand (lsh mode -0) 7))
6717 (suid (> (logand (lsh mode -9) 4) 0))
6718 (sgid (> (logand (lsh mode -9) 2) 0))
6719 (sticky (> (logand (lsh mode -9) 1) 0)))
6720 (setq user (tramp-file-mode-permissions user suid "s"))
6721 (setq group (tramp-file-mode-permissions group sgid "s"))
6722 (setq other (tramp-file-mode-permissions other sticky "t"))
6723 (concat type user group other)))
6726 (defun tramp-file-mode-permissions (perm suid suid-text)
6727 "Convert a permission bitset into a string.
6728 This is used internally by `tramp-file-mode-from-int'."
6729 (let ((r (> (logand perm 4) 0))
6730 (w (> (logand perm 2) 0))
6731 (x (> (logand perm 1) 0)))
6732 (concat (or (and r "r") "-")
6733 (or (and w "w") "-")
6734 (or (and suid x suid-text) ; suid, execute
6735 (and suid (upcase suid-text)) ; suid, !execute
6736 (and x "x") "-")))) ; !suid
6739 (defun tramp-decimal-to-octal (i)
6740 "Return a string consisting of the octal digits of I.
6741 Not actually used. Use `(format \"%o\" i)' instead?"
6742 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6743 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6745 (t (concat (tramp-decimal-to-octal (/ i 8))
6746 (number-to-string (% i 8))))))
6749 ;;(defun tramp-octal-to-decimal (ostr)
6750 ;; "Given a string of octal digits, return a decimal number."
6751 ;; (cond ((null ostr) 0)
6752 ;; ((string= "" ostr) 0)
6753 ;; (t (let ((last (aref ostr (1- (length ostr))))
6754 ;; (rest (substring ostr 0 (1- (length ostr)))))
6755 ;; (unless (and (>= last ?0)
6757 ;; (error "Not an octal digit: %c" last))
6758 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6759 ;; Kudos to Gerd Moellmann for this suggestion.
6760 (defun tramp-octal-to-decimal (ostr)
6761 "Given a string of octal digits, return a decimal number."
6762 (let ((x (or ostr "")))
6763 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6764 (unless (string-match "\\`[0-7]*\\'" x)
6765 (error "Non-octal junk in string `%s'" x))
6766 (string-to-number ostr 8)))
6768 (defun tramp-shell-case-fold (string)
6769 "Converts STRING to shell glob pattern which ignores case."
6772 (if (equal (downcase c) (upcase c))
6774 (format "[%c%c]" (downcase c) (upcase c))))
6779 ;; ------------------------------------------------------------
6780 ;; -- TRAMP file names --
6781 ;; ------------------------------------------------------------
6782 ;; Conversion functions between external representation and
6783 ;; internal data structure. Convenience functions for internal
6786 (defstruct tramp-file-name multi-method method user host localname)
6788 (defun tramp-tramp-file-p (name)
6789 "Return t iff NAME is a tramp file."
6791 (string-match tramp-file-name-regexp name)))
6793 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6794 ;; to the `user' part of the structure if a user name was not
6795 ;; provided, now it assigns nil.
6796 (defun tramp-dissect-file-name (name)
6797 "Return an `tramp-file-name' structure.
6798 The structure consists of remote method, remote user, remote host and
6799 localname (file name on remote host)."
6801 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6804 (if match (match-string (nth 1 tramp-file-name-structure) name)
6807 (format (nth 0 tramp-multi-file-name-structure)
6808 (nth 0 tramp-multi-file-name-hop-structure)) name)
6809 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6810 (if (and method (member method tramp-multi-methods))
6811 ;; If it's a multi method, the file name structure contains
6812 ;; arrays of method, user and host.
6813 (tramp-dissect-multi-file-name name)
6814 ;; Normal method. First, find out default method.
6815 (unless match (error "Not a tramp file name: %s" name))
6816 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6817 (host (match-string (nth 3 tramp-file-name-structure) name))
6818 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6819 (make-tramp-file-name
6824 :localname localname))))))
6826 (defun tramp-find-default-method (user host)
6827 "Look up the right method to use in `tramp-default-method-alist'."
6828 (let ((choices tramp-default-method-alist)
6829 (method tramp-default-method)
6832 (setq item (pop choices))
6833 (when (and (string-match (nth 0 item) (or host ""))
6834 (string-match (nth 1 item) (or user "")))
6835 (setq method (nth 2 item))
6836 (setq choices nil)))
6839 (defun tramp-find-method (multi-method method user host)
6840 "Return the right method string to use.
6841 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6842 If both MULTI-METHOD and METHOD are nil, do a lookup in
6843 `tramp-default-method-alist'."
6844 (or multi-method method (tramp-find-default-method user host)))
6846 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6847 ;; a user name is not provided in the "file name" very well.
6848 (defun tramp-dissect-multi-file-name (name)
6849 "Not implemented yet."
6850 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6851 (method-index (nth 1 tramp-multi-file-name-structure))
6852 (hops-index (nth 2 tramp-multi-file-name-structure))
6853 (localname-index (nth 3 tramp-multi-file-name-structure))
6854 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6855 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6856 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6857 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6858 method hops len hop-methods hop-users hop-hosts localname)
6859 (unless (string-match (format regexp hop-regexp) name)
6860 (error "Not a multi tramp file name: %s" name))
6861 (setq method (match-string method-index name))
6862 (setq hops (match-string hops-index name))
6863 (setq len (/ (length (match-data t)) 2))
6864 (when (< localname-index 0) (incf localname-index len))
6865 (setq localname (match-string localname-index name))
6867 (while (string-match hop-regexp hops index)
6868 (setq index (match-end 0))
6870 (cons (match-string hop-method-index hops) hop-methods))
6872 (cons (match-string hop-user-index hops) hop-users))
6874 (cons (match-string hop-host-index hops) hop-hosts))))
6875 (make-tramp-file-name
6876 :multi-method method
6877 :method (apply 'vector (reverse hop-methods))
6878 :user (apply 'vector (reverse hop-users))
6879 :host (apply 'vector (reverse hop-hosts))
6880 :localname localname)))
6882 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6883 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6885 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6887 (concat tramp-prefix-format
6888 (when method (concat "%m" tramp-postfix-single-method-format))
6889 (when user (concat "%u" tramp-postfix-user-format))
6890 (when host (concat "%h" tramp-postfix-host-format))
6891 (when localname (concat "%p")))
6892 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6894 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6895 ;; with this when USER is nil?
6896 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6897 "Constructs a tramp file name for a multi-hop method."
6898 (unless tramp-make-multi-tramp-file-format
6899 (error "`tramp-make-multi-tramp-file-format' is nil"))
6900 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6901 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6902 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6903 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6905 (localname (format-spec localname-format `((?p . ,localname))))
6907 (len (length method)))
6909 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6910 (setq hops (concat hops (format-spec hop-format
6911 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6913 (concat prefix hops localname)))
6915 (defun tramp-make-copy-program-file-name (user host localname)
6916 "Create a file name suitable to be passed to `rcp' and workalikes."
6918 (format "%s@%s:%s" user host localname)
6919 (format "%s:%s" host localname)))
6921 (defun tramp-method-out-of-band-p (multi-method method user host)
6922 "Return t if this is an out-of-band method, nil otherwise."
6923 (tramp-get-method-parameter
6925 (tramp-find-method multi-method method user host)
6926 user host 'tramp-copy-program))
6928 ;; Variables local to connection.
6930 (defun tramp-get-ls-command (multi-method method user host)
6932 (tramp-maybe-open-connection multi-method method user host)
6933 (set-buffer (tramp-get-buffer multi-method method user host))
6936 (defun tramp-get-test-groks-nt (multi-method method user host)
6938 (tramp-maybe-open-connection multi-method method user host)
6939 (set-buffer (tramp-get-buffer multi-method method user host))
6940 tramp-test-groks-nt))
6942 (defun tramp-get-file-exists-command (multi-method method user host)
6944 (tramp-maybe-open-connection multi-method method user host)
6945 (set-buffer (tramp-get-buffer multi-method method user host))
6946 tramp-file-exists-command))
6948 (defun tramp-get-remote-perl (multi-method method user host)
6949 (tramp-get-connection-property "perl" nil multi-method method user host))
6951 (defun tramp-get-remote-ln (multi-method method user host)
6952 (tramp-get-connection-property "ln" nil multi-method method user host))
6954 ;; Get a property of a TRAMP connection.
6955 (defun tramp-get-connection-property
6956 (property default multi-method method user host)
6957 "Get the named property for the connection.
6958 If the value is not set for the connection, return `default'"
6959 (tramp-maybe-open-connection multi-method method user host)
6960 (with-current-buffer (tramp-get-buffer multi-method method user host)
6963 (symbol-value (intern (concat "tramp-connection-property-" property)))
6966 ;; Set a property of a TRAMP connection.
6967 (defun tramp-set-connection-property
6968 (property value multi-method method user host)
6969 "Set the named property of a TRAMP connection."
6970 (tramp-maybe-open-connection multi-method method user host)
6971 (with-current-buffer (tramp-get-buffer multi-method method user host)
6972 (set (make-local-variable
6973 (intern (concat "tramp-connection-property-" property)))
6976 ;; Some predefined connection properties.
6977 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6978 (tramp-set-connection-property "remote-encoding" rem-enc
6979 multi-method method user host))
6980 (defun tramp-get-remote-encoding (multi-method method user host)
6981 (tramp-get-connection-property "remote-encoding" nil
6982 multi-method method user host))
6984 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6985 (tramp-set-connection-property "remote-decoding" rem-dec
6986 multi-method method user host))
6987 (defun tramp-get-remote-decoding (multi-method method user host)
6988 (tramp-get-connection-property "remote-decoding" nil
6989 multi-method method user host))
6991 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6992 (tramp-set-connection-property "local-encoding" loc-enc
6993 multi-method method user host))
6994 (defun tramp-get-local-encoding (multi-method method user host)
6995 (tramp-get-connection-property "local-encoding" nil
6996 multi-method method user host))
6998 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6999 (tramp-set-connection-property "local-decoding" loc-dec
7000 multi-method method user host))
7001 (defun tramp-get-local-decoding (multi-method method user host)
7002 (tramp-get-connection-property "local-decoding" nil
7003 multi-method method user host))
7005 (defun tramp-get-method-parameter (multi-method method user host param)
7006 "Return the method parameter PARAM.
7007 If the `tramp-methods' entry does not exist, use the variable PARAM
7009 (unless (boundp param)
7010 (error "Non-existing method parameter `%s'" param))
7011 (let ((entry (assoc param
7012 (assoc (tramp-find-method multi-method method user host)
7016 (symbol-value param))))
7019 ;; Auto saving to a special directory.
7021 (defun tramp-exists-file-name-handler (operation &rest args)
7022 (let ((buffer-file-name "/")
7023 (fnha file-name-handler-alist)
7024 (check-file-name-operation operation)
7025 (file-name-handler-alist
7028 '(lambda (operation &rest args)
7029 "Returns OPERATION if it is the one to be checked"
7030 (if (equal check-file-name-operation operation)
7032 (let ((file-name-handler-alist fnha))
7033 (apply operation args))))))))
7034 (eq (apply operation args) operation)))
7036 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7037 (defadvice make-auto-save-file-name
7038 (around tramp-advice-make-auto-save-file-name () activate)
7039 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7040 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7041 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7044 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7045 ;; permission 0666 minus umask. This is a security threat.
7047 (defun tramp-set-auto-save-file-modes ()
7048 "Set permissions of autosaved remote files to the original permissions."
7049 (let ((bfn (buffer-file-name)))
7050 (when (and (stringp bfn)
7051 (tramp-tramp-file-p bfn)
7052 (stringp buffer-auto-save-file-name)
7053 (not (equal bfn buffer-auto-save-file-name)))
7054 (unless (file-exists-p buffer-auto-save-file-name)
7055 (write-region "" nil buffer-auto-save-file-name))
7056 ;; Permissions should be set always, because there might be an old
7057 ;; auto-saved file belonging to another original file. This could
7058 ;; be a security threat.
7059 (set-file-modes buffer-auto-save-file-name
7060 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7062 (unless (or (> emacs-major-version 21)
7063 (and (featurep 'xemacs)
7064 (= emacs-major-version 21)
7065 (> emacs-minor-version 4)))
7066 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))
7068 (defun tramp-subst-strs-in-string (alist string)
7069 "Replace all occurrences of the string FROM with TO in STRING.
7070 ALIST is of the form ((FROM . TO) ...)."
7073 (let* ((pr (car alist))
7076 (while (string-match (regexp-quote from) string)
7077 (setq string (replace-match to t t string)))
7078 (setq alist (cdr alist))))
7081 (defun tramp-insert-with-face (face string)
7082 "Insert text with a specific face."
7083 (let ((start (point)))
7085 (add-text-properties start (point) (list 'face face))))
7087 ;; ------------------------------------------------------------
7088 ;; -- Compatibility functions section --
7089 ;; ------------------------------------------------------------
7091 (defun tramp-temporary-file-directory ()
7092 "Return name of directory for temporary files (compat function).
7093 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7094 this is the function `temp-directory'."
7095 (cond ((boundp 'temporary-file-directory)
7096 (symbol-value 'temporary-file-directory))
7097 ((fboundp 'temp-directory)
7098 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7099 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7100 (file-name-as-directory (getenv "TEMP")))
7101 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7102 (file-name-as-directory (getenv "TMP")))
7103 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7104 (file-name-as-directory (getenv "TMPDIR")))
7105 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7106 (t (message (concat "Neither `temporary-file-directory' nor "
7107 "`temp-directory' is defined -- using /tmp."))
7108 (file-name-as-directory "/tmp"))))
7110 (defun tramp-read-passwd (user host prompt)
7111 "Read a password from user (compat function).
7112 Invokes `password-read' if available, `read-passwd' else."
7113 (if (functionp 'password-read)
7114 (let* ((key (concat (or user (user-login-name)) "@" host))
7115 (password (apply #'password-read (list prompt key))))
7116 (apply #'password-cache-add (list key password))
7118 (read-passwd prompt)))
7120 (defun tramp-clear-passwd (&optional user host)
7121 "Clear password cache for connection related to current-buffer."
7123 (let ((filename (or buffer-file-name list-buffers-directory "")))
7124 (when (and (functionp 'password-cache-remove)
7125 (or (and user host) (tramp-tramp-file-p filename)))
7126 (let* ((v (when (tramp-tramp-file-p filename)
7127 (tramp-dissect-file-name filename)))
7128 (luser (or user (tramp-file-name-user v) (user-login-name)))
7129 (lhost (or host (tramp-file-name-host v) (system-name)))
7130 (key (concat luser "@" lhost)))
7131 (apply #'password-cache-remove (list key))))))
7133 (defun tramp-time-diff (t1 t2)
7134 "Return the difference between the two times, in seconds.
7135 T1 and T2 are time values (as returned by `current-time' for example).
7137 NOTE: This function will fail if the time difference is too large to
7139 ;; Pacify byte-compiler with `symbol-function'.
7140 (cond ((and (fboundp 'subtract-time)
7141 (fboundp 'float-time))
7142 (funcall (symbol-function 'float-time)
7143 (funcall (symbol-function 'subtract-time) t1 t2)))
7144 ((and (fboundp 'subtract-time)
7145 (fboundp 'time-to-seconds))
7146 (funcall (symbol-function 'time-to-seconds)
7147 (funcall (symbol-function 'subtract-time) t1 t2)))
7148 ((fboundp 'itimer-time-difference)
7150 (symbol-function 'itimer-time-difference)
7151 (if (< (length t1) 3) (append t1 '(0)) t1)
7152 (if (< (length t2) 3) (append t2 '(0)) t2))))
7154 ;; snarfed from Emacs 21 time-date.el; combining
7155 ;; time-to-seconds and subtract-time
7156 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7157 (list (- (car t1) (car t2) (if borrow 1 0))
7158 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7159 (+ (* (car time) 65536.0)
7161 (/ (or (nth 2 time) 0) 1000000.0))))))
7163 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7164 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7165 EOL-TYPE can be one of `dos', `unix', or `mac'."
7166 (cond ((fboundp 'coding-system-change-eol-conversion)
7167 (apply #'coding-system-change-eol-conversion
7168 (list coding-system eol-type)))
7169 ((fboundp 'subsidiary-coding-system)
7171 #'subsidiary-coding-system
7173 (cond ((eq eol-type 'dos) 'crlf)
7174 ((eq eol-type 'unix) 'lf)
7175 ((eq eol-type 'mac) 'cr)
7177 (error "Unknown EOL-TYPE `%s', must be %s"
7179 "`dos', `unix', or `mac'"))))))
7180 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7182 (defun tramp-split-string (string pattern)
7183 "Like `split-string' but omit empty strings.
7184 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7185 This is, the first, empty, element is omitted. In XEmacs, the first
7186 element is not omitted.
7188 Note: this function has been written for `tramp-handle-file-truename'.
7189 If you want to use it for something else, you'll have to check whether
7190 it does the right thing."
7191 (delete "" (split-string string pattern)))
7193 (defun tramp-set-process-query-on-exit-flag (process flag)
7194 "Specify if query is needed for process when Emacs is exited.
7195 If the second argument flag is non-nil, Emacs will query the user before
7196 exiting if process is running."
7198 (if (fboundp 'set-process-query-on-exit-flag)
7199 (symbol-function 'set-process-query-on-exit-flag)
7200 (symbol-function 'process-kill-without-query))
7204 ;; ------------------------------------------------------------
7205 ;; -- Kludges section --
7206 ;; ------------------------------------------------------------
7208 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7209 ;; does not deal well with newline characters. Newline is replaced by
7210 ;; backslash newline. But if, say, the string `a backslash newline b'
7211 ;; is passed to a shell, the shell will expand this into "ab",
7212 ;; completely omitting the newline. This is not what was intended.
7213 ;; It does not appear to be possible to make the function
7214 ;; `shell-quote-argument' work with newlines without making it
7215 ;; dependent on the shell used. But within this package, we know that
7216 ;; we will always use a Bourne-like shell, so we use an approach which
7219 ;; The approach is simple: we call `shell-quote-argument', then
7220 ;; massage the newline part of the result.
7222 ;; This function should produce a string which is grokked by a Unix
7223 ;; shell, even if the Emacs is running on Windows. Since this is the
7224 ;; kludges section, we bind `system-type' in such a way that
7225 ;; `shell-quote-arguments' behaves as if on Unix.
7227 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7228 ;; function to work with Bourne-like shells.
7230 ;; CCC: This function should be rewritten so that
7231 ;; `shell-quote-argument' is not used. This way, we are safe from
7232 ;; changes in `shell-quote-argument'.
7233 (defun tramp-shell-quote-argument (s)
7234 "Similar to `shell-quote-argument', but groks newlines.
7235 Only works for Bourne-like shells."
7236 (let ((system-type 'not-windows))
7238 (let ((result (shell-quote-argument s))
7239 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7240 (when (and (>= (length result) 2)
7241 (string= (substring result 0 2) "\\~"))
7242 (setq result (substring result 1)))
7243 (while (string-match nl result)
7244 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7248 ;; ;; EFS hooks itself into the file name handling stuff in more places
7249 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7250 ;; ;; away from tramp.el file names.
7252 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7253 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7254 ;; ;; connection to help it's dired process. Not that I have any real
7255 ;; ;; idea *why* this is helpful to dired.
7257 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7259 ;; ;; Daniel Pittman <daniel@danann.net>
7261 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7262 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7263 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7264 ;; ;; EFS might nicht be loaded yet.
7265 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7266 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7267 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7271 ;; We currently (sometimes) use "[" and "]" in the filename format.
7272 ;; This means that Emacs wants to expand wildcards if
7273 ;; `find-file-wildcards' is non-nil, and then barfs because no
7274 ;; expansion could be found. We detect this situation and do
7275 ;; something really awful: we have `file-expand-wildcards' return the
7276 ;; original filename if it can't expand anything. Let's just hope
7277 ;; that this doesn't break anything else.
7278 ;; CCC: This check is now also really awful; we should search all
7279 ;; of the filename format, not just the prefix.
7280 (when (string-match "\\[" tramp-prefix-format)
7281 (defadvice file-expand-wildcards (around tramp-fix activate)
7282 (let ((name (ad-get-arg 0)))
7283 (if (tramp-tramp-file-p name)
7284 ;; If it's a Tramp file, dissect it and look if wildcards
7285 ;; need to be expanded at all.
7286 (let ((v (tramp-dissect-file-name name)))
7287 (if (string-match "[[*?]" (tramp-file-name-localname v))
7288 (let ((res ad-do-it))
7289 (setq ad-return-value (or res (list name))))
7290 (setq ad-return-value (list name))))
7291 ;; If it is not a Tramp file, just run the original function.
7292 (let ((res ad-do-it))
7293 (setq ad-return-value (or res (list name))))))))
7295 ;; Tramp version is useful in a number of situations.
7297 (defun tramp-version (arg)
7298 "Print version number of tramp.el in minibuffer or current buffer."
7300 (if arg (insert tramp-version) (message tramp-version)))
7302 ;; Make the `reporter` functionality available for making bug reports about
7303 ;; the package. A most useful piece of code.
7305 (unless (fboundp 'reporter-submit-bug-report)
7306 (autoload 'reporter-submit-bug-report "reporter"))
7309 "Submit a bug report to the TRAMP developers."
7313 (let ((reporter-prompt-for-summary-p t))
7314 (reporter-submit-bug-report
7315 tramp-bug-report-address ; to-address
7316 (format "tramp (%s)" tramp-version) ; package name and version
7321 tramp-file-exists-command
7322 tramp-current-multi-method
7323 tramp-current-method
7328 tramp-auto-save-directory ; vars to dump
7329 tramp-default-method
7330 tramp-rsh-end-of-line
7331 tramp-default-password-end-of-line
7333 tramp-login-prompt-regexp
7334 ;; Mask non-7bit characters
7335 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7336 tramp-wrong-passwd-regexp
7337 tramp-yesno-prompt-regexp
7338 tramp-yn-prompt-regexp
7339 tramp-terminal-prompt-regexp
7340 tramp-temp-name-prefix
7341 tramp-file-name-structure
7342 tramp-file-name-regexp
7343 tramp-multi-file-name-structure
7344 tramp-multi-file-name-hop-structure
7346 tramp-multi-connection-function-alist
7349 tramp-coding-commands
7350 tramp-actions-before-shell
7351 tramp-actions-copy-out-of-band
7354 ;; Mask non-7bit characters
7355 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7357 ,(when (boundp 'tramp-backup-directory-alist)
7358 'tramp-backup-directory-alist)
7359 ,(when (boundp 'tramp-bkup-backup-directory-info)
7360 'tramp-bkup-backup-directory-info)
7362 ;; Non-tramp variables of interest
7363 ;; Mask non-7bit characters
7364 (shell-prompt-pattern . tramp-reporter-dump-variable)
7366 backup-by-copying-when-linked
7367 backup-by-copying-when-mismatch
7368 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7369 'backup-by-copying-when-privileged-mismatch)
7370 ,(when (boundp 'password-cache)
7372 ,(when (boundp 'password-cache-expiry)
7373 'password-cache-expiry)
7374 ,(when (boundp 'backup-directory-alist)
7375 'backup-directory-alist)
7376 ,(when (boundp 'bkup-backup-directory-info)
7377 'bkup-backup-directory-info)
7378 file-name-handler-alist))
7380 'tramp-load-report-modules ; pre-hook
7381 'tramp-append-tramp-buffers ; post-hook
7383 Enter your bug report in this message, including as much detail as you
7384 possibly can about the problem, what you did to cause it and what the
7385 local and remote machines are.
7387 If you can give a simple set of instructions to make this bug happen
7388 reliably, please include those. Thank you for helping kill bugs in
7391 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7392 the ~/.emacs file and to repeat the bug. Then, include the contents
7393 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7396 --bug report follows this line--
7399 (defun tramp-reporter-dump-variable (varsym mailbuf)
7400 "Pretty-print the value of the variable in symbol VARSYM.
7401 Used for non-7bit chars in strings."
7402 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7403 (val (with-current-buffer reporter-eval-buffer
7404 (symbol-value varsym))))
7406 ;; There are characters to be masked.
7407 (when (and (boundp 'mm-7bit-chars)
7409 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7410 (with-current-buffer reporter-eval-buffer
7411 (set varsym (concat "(base64-decode-string \""
7412 (base64-encode-string val)
7416 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7418 ;; Remove string quotation.
7421 (concat "\\(^.*\\)" "\"" ;; \1 "
7422 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7423 "\\(\".*\\)" "\\\\" ;; \3 \
7424 "\\(\")\\)" "\"$")) ;; \4 "
7425 (replace-match "\\1\\2\\3\\4")
7427 (insert " ;; variable encoded due to non-printable characters\n"))
7430 ;; Reset VARSYM to old value.
7431 (with-current-buffer reporter-eval-buffer
7434 (defun tramp-load-report-modules ()
7435 "Load needed modules for reporting."
7437 ;; We load message.el and mml.el from Gnus.
7438 (if (featurep 'xemacs)
7440 (load "message" 'noerror)
7441 (load "mml" 'noerror))
7442 (require 'message nil 'noerror)
7443 (require 'mml nil 'noerror))
7444 (when (functionp 'message-mode)
7445 (funcall (symbol-function 'message-mode)))
7446 (when (functionp 'mml-mode)
7447 (funcall (symbol-function 'mml-mode) t)))
7449 (defun tramp-append-tramp-buffers ()
7450 "Append Tramp buffers into the bug report."
7453 (eq major-mode 'message-mode)
7455 (symbol-value 'mml-mode))
7457 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7460 (mapcar '(lambda (b)
7461 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7463 (curbuf (current-buffer)))
7465 ;; There is at least one Tramp buffer.
7467 (switch-to-buffer (list-buffers-noselect nil))
7468 (delete-other-windows)
7469 (setq buffer-read-only nil)
7470 (goto-char (point-min))
7472 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7475 (let ((start (point)))
7477 (kill-region start (point)))))
7479 The buffer(s) above will be appended to this message. If you don't want
7480 to append a buffer because it contains sensible data, or because the buffer
7481 is too large, you should delete the respective buffer. The buffer(s) will
7482 contain user and host names. Passwords will never be included there.")
7484 (when (and tramp-debug-buffer (> tramp-verbose 9))
7486 (let ((start (point)))
7488 Please note that you have set `tramp-verbose' to a value greater than 9.
7489 Therefore, the contents of files might be included in the debug buffer(s).")
7490 (add-text-properties start (point) (list 'face 'italic))))
7492 (set-buffer-modified-p nil)
7493 (setq buffer-read-only t)
7494 (goto-char (point-min))
7496 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7497 ;; OK, let's send. First we delete the buffer list.
7500 (switch-to-buffer curbuf)
7501 (goto-char (point-max))
7503 (dolist (buffer buffer-list)
7504 (funcall (symbol-function 'mml-insert-empty-tag)
7505 'part 'type "text/plain" 'encoding "base64"
7506 'disposition "attachment" 'buffer (buffer-name buffer)
7507 'description (buffer-name buffer)))
7508 (set-buffer-modified-p nil))
7510 ;; Don't send. Delete the message buffer.
7512 (set-buffer-modified-p nil)
7514 (throw 'dont-send nil))))))
7516 (defalias 'tramp-submit-bug 'tramp-bug)
7520 ;; Make sure that we get integration with the VC package.
7521 ;; When it is loaded, we need to pull in the integration module.
7522 ;; This must come after (provide 'tramp) because tramp-vc.el
7524 (eval-after-load "vc"
7525 '(require 'tramp-vc))
7529 ;; * Allow putting passwords in the filename.
7530 ;; This should be implemented via a general mechanism to add
7531 ;; parameters in filenames. There is currently a kludge for
7532 ;; putting the port number into the filename for ssh and ftp
7533 ;; files. This could be subsumed by the new mechanism as well.
7534 ;; Another approach is to read a netrc file like ~/.authinfo
7536 ;; * Handle nonlocal exits such as C-g.
7537 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7538 ;; * Add fallback for inline encodings. This should be used
7539 ;; if the remote end doesn't support mimencode or a similar program.
7540 ;; For reading files from the remote host, we can just parse the output
7541 ;; of `od -b'. For writing files to the remote host, we construct
7542 ;; a shell program which contains only "safe" ascii characters
7543 ;; and which writes the right bytes to the file. We can use printf(1)
7544 ;; or "echo -e" or the printf function in awk and use octal escapes
7545 ;; for the "dangerous" characters. The null byte might be a problem.
7546 ;; On some systems, the octal escape doesn't work. So we try the following
7547 ;; two commands to write a null byte:
7548 ;; dd if=/dev/zero bs=1 count=1
7549 ;; echo | tr '\n' '\000'
7550 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7551 ;; the two via a format which can be `uu' or `b64'. Then we can search
7552 ;; for the right local commands and the right remote commands separately.
7553 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7554 ;; work for remote files.
7555 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7556 ;; `shell-quote-argument'.
7557 ;; * Completion gets confused when you leave out the method name.
7558 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7559 ;; by the files in that directory. Add this here.
7560 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7561 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7562 ;; * When logging in, keep looking for questions according to an alist
7563 ;; and then invoke the right function.
7564 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7565 ;; * Running CVS remotely doesn't appear to work right. It thinks
7566 ;; files are locked by somebody else even if I'm the locking user.
7567 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7569 ;; * Don't use globbing for directories with many files, as this is
7570 ;; likely to produce long command lines, and some shells choke on
7571 ;; long command lines.
7572 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7573 ;; do the right thing.
7574 ;; * `vc-directory' does not work. It never displays any files, even
7575 ;; if it does show files when run locally.
7576 ;; * Allow correction of passwords, if the remote end allows this.
7577 ;; (Mark Hershberger)
7578 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7579 ;; * Do asynchronous `shell-command's.
7580 ;; * Grok `append' parameter for `write-region'.
7581 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7582 ;; * abbreviate-file-name
7583 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7584 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7585 ;; what the user names should default to, though.
7586 ;; * better error checking. At least whenever we see something
7587 ;; strange when doing zerop, we should kill the process and start
7588 ;; again. (Greg Stark)
7589 ;; * Add caching for filename completion. (Greg Stark)
7590 ;; Of course, this has issues with usability (stale cache bites)
7591 ;; -- <daniel@danann.net>
7592 ;; * Provide a local cache of old versions of remote files for the rsync
7593 ;; transfer method to use. (Greg Stark)
7594 ;; * Remove unneeded parameters from methods.
7595 ;; * Invoke rsync once for copying a whole directory hierarchy.
7596 ;; (Francesco Potort
\e,Al
\e(B)
7597 ;; * Should we set PATH ourselves or should we rely on the remote end
7599 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7600 ;; * Make it work for different encodings, and for different file name
7601 ;; encodings, too. (Daniel Pittman)
7602 ;; * Change applicable functions to pass a struct tramp-file-name rather
7603 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7604 ;; * Implement asynchronous shell commands.
7605 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7606 ;; * Progress reports while copying files. (Michael Kifer)
7607 ;; * `Smart' connection method that uses inline for small and out of
7608 ;; band for large files. (Michael Kifer)
7609 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7610 ;; then look if it's the right version (with `perl -v').
7611 ;; * When editing a remote CVS controlled file as a different user, VC
7612 ;; gets confused about the file locking status. Try to find out why
7613 ;; the workaround doesn't work.
7614 ;; * Change `copy-file' to grok the case where the filename handler
7615 ;; for the source and the target file are different. Right now,
7616 ;; it looks at the source file and then calls that handler, if
7617 ;; there is one. But since ange-ftp, for instance, does not know
7618 ;; about Tramp, it does not do the right thing if the target file
7619 ;; name is a Tramp name.
7620 ;; * Username and hostname completion.
7621 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7622 ;; connect to host "blabla" already if that host is unique. No idea
7623 ;; how to suppress. Maybe not an essential problem.
7624 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7625 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7626 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7627 ;; Code is nearly identical.
7628 ;; ** Decide whiche files to take for searching user/host names depending on
7629 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7630 ;; ** Enhance variables for debug.
7631 ;; ** Implement "/multi:" completion.
7632 ;; ** Add a learning mode for completion. Make results persistent.
7633 ;; * Allow out-of-band methods as _last_ multi-hop.
7635 ;; Functions for file-name-handler-alist:
7636 ;; diff-latest-backup-file -- in diff.el
7637 ;; dired-uncache -- this will be needed when we do insert-directory caching
7638 ;; file-name-as-directory -- use primitive?
7639 ;; file-name-sans-versions -- use primitive?
7640 ;; get-file-buffer -- use primitive
7643 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7644 ;;; tramp.el ends here