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 ((not tramp-unified-filenames) t)
4388 ((string-match "^/.*:.*:$" file) nil)
4390 (concat tramp-prefix-regexp
4391 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4393 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4394 ((or (equal last-input-event 'tab)
4396 (and (integerp last-input-event)
4397 (not (event-modifiers last-input-event))
4398 (or (char-equal last-input-event ?\?)
4399 (char-equal last-input-event ?\t) ; handled by 'tab already?
4400 (char-equal last-input-event ?\ )))
4402 (and (featurep 'xemacs)
4403 (not (event-modifiers last-input-event))
4405 (funcall (symbol-function 'event-to-character)
4406 last-input-event) ?\?)
4408 (funcall (symbol-function 'event-to-character)
4409 last-input-event) ?\t)
4411 (funcall (symbol-function 'event-to-character)
4412 last-input-event) ?\ ))))
4415 (defun tramp-completion-handle-file-exists-p (filename)
4416 "Like `file-exists-p' for tramp files."
4417 (if (tramp-completion-mode filename)
4418 (tramp-run-real-handler
4419 'file-exists-p (list filename))
4420 (tramp-completion-run-real-handler
4421 'file-exists-p (list filename))))
4423 ;; Localname manipulation in case of partial TRAMP file names.
4424 (defun tramp-completion-handle-file-name-directory (file)
4425 "Like `file-name-directory' but aware of TRAMP files."
4426 (if (tramp-completion-mode file)
4428 (tramp-completion-run-real-handler
4429 'file-name-directory (list file))))
4431 ;; Localname manipulation in case of partial TRAMP file names.
4432 (defun tramp-completion-handle-file-name-nondirectory (file)
4433 "Like `file-name-nondirectory' but aware of TRAMP files."
4435 file (length (tramp-completion-handle-file-name-directory file))))
4437 ;; Method, host name and user name completion.
4438 ;; `tramp-completion-dissect-file-name' returns a list of
4439 ;; tramp-file-name structures. For all of them we return possible completions.
4440 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4441 "Like `file-name-all-completions' for partial tramp files."
4444 ((fullname (concat directory filename))
4447 (if (tramp-completion-mode fullname)
4448 (tramp-run-real-handler
4449 'file-name-all-completions (list filename directory))
4450 (tramp-completion-run-real-handler
4451 'file-name-all-completions (list filename directory))))
4452 ;; possible completion structures
4453 (v (tramp-completion-dissect-file-name fullname)))
4456 (let* ((car (car v))
4457 (multi-method (tramp-file-name-multi-method car))
4458 (method (tramp-file-name-method car))
4459 (user (tramp-file-name-user car))
4460 (host (tramp-file-name-host car))
4461 (localname (tramp-file-name-localname car))
4462 (m (tramp-find-method multi-method method user host))
4463 (tramp-current-user user) ; see `tramp-parse-passwd'
4466 (unless (or multi-method ;; Not handled (yet).
4467 localname) ;; Nothing to complete
4471 ;; Method dependent user / host combinations
4475 (setq all-user-hosts
4476 (append all-user-hosts
4477 (funcall (nth 0 x) (nth 1 x)))))
4478 (tramp-get-completion-function m))
4480 (setq result (append result
4483 (tramp-get-completion-user-host
4484 method user host (nth 0 x) (nth 1 x)))
4485 (delq nil all-user-hosts)))))
4489 (append result (tramp-get-completion-methods m)))))
4491 (setq v (delq car v))))
4493 ;;; unify list, remove nil elements
4496 (let ((car (car result)))
4497 (when car (add-to-list 'result1 car))
4498 (setq result (delq car result))))
4502 ;; Method, host name and user name completion for a file.
4503 (defun tramp-completion-handle-file-name-completion (filename directory)
4504 "Like `file-name-completion' for tramp files."
4505 (try-completion filename
4506 (mapcar 'list (file-name-all-completions filename directory))))
4508 ;; I misuse a little bit the tramp-file-name structure in order to handle
4509 ;; completion possibilities for partial methods / user names / host names.
4510 ;; Return value is a list of tramp-file-name structures according to possible
4511 ;; completions. If "multi-method" or "localname" is non-nil it means there
4512 ;; shouldn't be a completion anymore.
4514 ;; Expected results:
4516 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4517 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4518 ;; [nil nil "x" nil nil]
4519 ;; [nil "x" nil nil nil]
4521 ;; "/x:" "/x:y" "/x:y:"
4522 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4524 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4525 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4527 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4528 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4529 ;; "/[x/y@" "/[x/y@z"
4530 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4531 (defun tramp-completion-dissect-file-name (name)
4532 "Returns a list of `tramp-file-name' structures.
4533 They are collected by `tramp-completion-dissect-file-name1'."
4537 ;; "/method" "/[method"
4538 (tramp-completion-file-name-structure1
4539 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4542 (tramp-completion-file-name-structure2
4543 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4546 (tramp-completion-file-name-structure3
4547 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4549 ;; "/user@host" "/[user@host"
4550 (tramp-completion-file-name-structure4
4551 (list (concat tramp-prefix-regexp
4552 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4553 "\\(" tramp-host-regexp x-nil "\\)$")
4555 ;; "/method:user" "/[method/user"
4556 (tramp-completion-file-name-structure5
4557 (list (concat tramp-prefix-regexp
4558 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4559 "\\(" tramp-user-regexp x-nil "\\)$")
4561 ;; "/method:host" "/[method/host"
4562 (tramp-completion-file-name-structure6
4563 (list (concat tramp-prefix-regexp
4564 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4565 "\\(" tramp-host-regexp x-nil "\\)$")
4567 ;; "/method:user@host" "/[method/user@host"
4568 (tramp-completion-file-name-structure7
4569 (list (concat tramp-prefix-regexp
4570 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4571 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4572 "\\(" tramp-host-regexp x-nil "\\)$")
4575 (mapcar (lambda (regexp)
4576 (add-to-list 'result
4577 (tramp-completion-dissect-file-name1 regexp name)))
4579 tramp-completion-file-name-structure1
4580 tramp-completion-file-name-structure2
4581 tramp-completion-file-name-structure3
4582 tramp-completion-file-name-structure4
4583 tramp-completion-file-name-structure5
4584 tramp-completion-file-name-structure6
4585 tramp-completion-file-name-structure7
4586 tramp-file-name-structure))
4590 (defun tramp-completion-dissect-file-name1 (structure name)
4591 "Returns a `tramp-file-name' structure matching STRUCTURE.
4592 The structure consists of multi-method, remote method, remote user,
4593 remote host and localname (filename on remote host)."
4597 (when (string-match (nth 0 structure) name)
4598 (setq method (and (nth 1 structure)
4599 (match-string (nth 1 structure) name)))
4600 (if (and method (member method tramp-multi-methods))
4601 ;; Not handled (yet).
4602 (make-tramp-file-name
4603 :multi-method method
4608 (let ((user (and (nth 2 structure)
4609 (match-string (nth 2 structure) name)))
4610 (host (and (nth 3 structure)
4611 (match-string (nth 3 structure) name)))
4612 (localname (and (nth 4 structure)
4613 (match-string (nth 4 structure) name))))
4614 (make-tramp-file-name
4619 :localname localname)))))))
4621 ;; This function returns all possible method completions, adding the
4622 ;; trailing method delimeter.
4623 (defun tramp-get-completion-methods (partial-method)
4624 "Returns all method completions for PARTIAL-METHOD."
4628 (string-match (concat "^" (regexp-quote partial-method)) method)
4629 ;; we must remove leading "/".
4630 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4631 (delete "multi" (mapcar 'car tramp-methods))))
4633 ;; Compares partial user and host names with possible completions.
4634 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4635 "Returns the most expanded string for user and host name completion.
4636 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4639 ((and partial-user partial-host)
4641 (string-match (concat "^" (regexp-quote partial-host)) host)
4642 (string-equal partial-user (or user partial-user)))
4643 (setq user partial-user)
4650 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4656 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4662 (unless (zerop (+ (length user) (length host)))
4663 ;; we must remove leading "/".
4664 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4666 (defun tramp-parse-rhosts (filename)
4667 "Return a list of (user host) tuples allowed to access.
4668 Either user or host may be nil."
4671 (when (file-readable-p filename)
4673 (insert-file-contents filename)
4674 (goto-char (point-min))
4676 (push (tramp-parse-rhosts-group) res))))
4679 ;; Taken from gnus/netrc.el
4681 (defalias 'tramp-point-at-eol
4682 (if (fboundp 'point-at-eol)
4684 'line-end-position)))
4686 (defun tramp-parse-rhosts-group ()
4687 "Return a (user host) tuple allowed to access.
4688 Either user or host may be nil."
4693 "^\\(" tramp-host-regexp "\\)"
4694 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4696 (narrow-to-region (point) (tramp-point-at-eol))
4697 (when (re-search-forward regexp nil t)
4698 (setq result (append (list (match-string 3) (match-string 1)))))
4703 (defun tramp-parse-shosts (filename)
4704 "Return a list of (user host) tuples allowed to access.
4705 User is always nil."
4708 (when (file-readable-p filename)
4710 (insert-file-contents filename)
4711 (goto-char (point-min))
4713 (push (tramp-parse-shosts-group) res))))
4716 (defun tramp-parse-shosts-group ()
4717 "Return a (user host) tuple allowed to access.
4718 User is always nil."
4721 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4723 (narrow-to-region (point) (tramp-point-at-eol))
4724 (when (re-search-forward regexp nil t)
4725 (setq result (list nil (match-string 1))))
4728 (> (skip-chars-forward ",") 0)
4732 (defun tramp-parse-sconfig (filename)
4733 "Return a list of (user host) tuples allowed to access.
4734 User is always nil."
4737 (when (file-readable-p filename)
4739 (insert-file-contents filename)
4740 (goto-char (point-min))
4742 (push (tramp-parse-sconfig-group) res))))
4745 (defun tramp-parse-sconfig-group ()
4746 "Return a (user host) tuple allowed to access.
4747 User is always nil."
4750 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4752 (narrow-to-region (point) (tramp-point-at-eol))
4753 (when (re-search-forward regexp nil t)
4754 (setq result (list nil (match-string 1))))
4757 (> (skip-chars-forward ",") 0)
4761 (defun tramp-parse-shostkeys (dirname)
4762 "Return a list of (user host) tuples allowed to access.
4763 User is always nil."
4765 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4766 (files (when (file-directory-p dirname) (directory-files dirname)))
4770 (when (string-match regexp (car files))
4771 (push (list nil (match-string 1 (car files))) result))
4772 (setq files (cdr files)))
4775 (defun tramp-parse-sknownhosts (dirname)
4776 "Return a list of (user host) tuples allowed to access.
4777 User is always nil."
4779 (let ((regexp (concat "^\\(" tramp-host-regexp
4780 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4781 (files (when (file-directory-p dirname) (directory-files dirname)))
4785 (when (string-match regexp (car files))
4786 (push (list nil (match-string 1 (car files))) result))
4787 (setq files (cdr files)))
4790 (defun tramp-parse-hosts (filename)
4791 "Return a list of (user host) tuples allowed to access.
4792 User is always nil."
4795 (when (file-readable-p filename)
4797 (insert-file-contents filename)
4798 (goto-char (point-min))
4800 (push (tramp-parse-hosts-group) res))))
4803 (defun tramp-parse-hosts-group ()
4804 "Return a (user host) tuple allowed to access.
4805 User is always nil."
4808 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4810 (narrow-to-region (point) (tramp-point-at-eol))
4811 (when (re-search-forward regexp nil t)
4812 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4813 (setq result (list nil (match-string 1)))))
4816 (> (skip-chars-forward " \t") 0)
4820 ;; For su-alike methods it would be desirable to return "root@localhost"
4821 ;; as default. Unfortunately, we have no information whether any user name
4822 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4823 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4824 (defun tramp-parse-passwd (filename)
4825 "Return a list of (user host) tuples allowed to access.
4826 Host is always \"localhost\"."
4829 (if (zerop (length tramp-current-user))
4831 (when (file-readable-p filename)
4833 (insert-file-contents filename)
4834 (goto-char (point-min))
4836 (push (tramp-parse-passwd-group) res))))
4839 (defun tramp-parse-passwd-group ()
4840 "Return a (user host) tuple allowed to access.
4841 Host is always \"localhost\"."
4844 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4846 (narrow-to-region (point) (tramp-point-at-eol))
4847 (when (re-search-forward regexp nil t)
4848 (setq result (list (match-string 1) "localhost")))
4853 (defun tramp-parse-netrc (filename)
4854 "Return a list of (user host) tuples allowed to access.
4858 (when (file-readable-p filename)
4860 (insert-file-contents filename)
4861 (goto-char (point-min))
4863 (push (tramp-parse-netrc-group) res))))
4866 (defun tramp-parse-netrc-group ()
4867 "Return a (user host) tuple allowed to access.
4873 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4874 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4876 (narrow-to-region (point) (tramp-point-at-eol))
4877 (when (re-search-forward regexp nil t)
4878 (setq result (list (match-string 3) (match-string 1))))
4883 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4884 "Like `expand-file-name' for tramp files."
4885 (let ((fullname (concat (or dir default-directory) name)))
4886 (if (tramp-completion-mode fullname)
4887 (tramp-run-real-handler
4888 'expand-file-name (list name dir))
4889 (tramp-completion-run-real-handler
4890 'expand-file-name (list name dir)))))
4892 ;;; Internal Functions:
4894 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
4895 "Define in remote shell function NAME implemented as perl SCRIPT.
4896 Only send the definition if it has not already been done.
4897 Function may have 0-3 parameters."
4898 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4899 (unless remote-perl (error "No remote perl"))
4900 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4901 multi-method method user host)))
4902 (unless (memq name perl-scripts)
4903 (with-current-buffer (tramp-get-buffer multi-method method user host)
4904 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4905 (tramp-send-string multi-method method user host
4911 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4912 (tramp-wait-for-output)
4913 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4914 multi-method method user host)
4915 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4917 (defun tramp-set-auto-save ()
4918 (when (and (buffer-file-name)
4919 (tramp-tramp-file-p (buffer-file-name))
4920 ;; ange-ftp has its own auto-save mechanism
4921 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
4922 'tramp-sh-file-name-handler)
4924 (auto-save-mode 1)))
4925 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4927 (defun tramp-run-test (switch filename)
4928 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4929 Returns the exit code of the `test' program."
4930 (let ((v (tramp-dissect-file-name filename)))
4932 (tramp-send-command-and-check
4933 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4934 (tramp-file-name-user v) (tramp-file-name-host v)
4935 (format "test %s %s" switch
4936 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4938 (defun tramp-run-test2 (program file1 file2 &optional switch)
4939 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4940 The optional SWITCH is inserted between the two files.
4941 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4942 hosts, or files, disagree."
4943 (let* ((v1 (tramp-dissect-file-name file1))
4944 (v2 (tramp-dissect-file-name file2))
4945 (mmethod1 (tramp-file-name-multi-method v1))
4946 (mmethod2 (tramp-file-name-multi-method v2))
4947 (method1 (tramp-file-name-method v1))
4948 (method2 (tramp-file-name-method v2))
4949 (user1 (tramp-file-name-user v1))
4950 (user2 (tramp-file-name-user v2))
4951 (host1 (tramp-file-name-host v1))
4952 (host2 (tramp-file-name-host v2))
4953 (localname1 (tramp-file-name-localname v1))
4954 (localname2 (tramp-file-name-localname v2)))
4955 (unless (and method1 method2 host1 host2
4956 (equal mmethod1 mmethod2)
4957 (equal method1 method2)
4959 (equal host1 host2))
4960 (error "tramp-run-test2: %s"
4961 "only implemented for same method, same user, same host"))
4963 (tramp-send-command-and-check
4964 mmethod1 method1 user1 host1
4965 (format "%s %s %s %s"
4967 (tramp-shell-quote-argument localname1)
4969 (tramp-shell-quote-argument localname2))))))
4971 (defun tramp-touch (file time)
4972 "Set the last-modified timestamp of the given file.
4973 TIME is an Emacs internal time value as returned by `current-time'."
4974 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
4975 (if (tramp-tramp-file-p file)
4976 (with-parsed-tramp-file-name file nil
4977 (let ((buf (tramp-get-buffer multi-method method user host)))
4978 (unless (zerop (tramp-send-command-and-check
4979 multi-method method user host
4980 (format "touch -t %s %s"
4984 (error "tramp-touch: touch failed, see buffer `%s' for details"
4986 ;; It's a local file
4988 (unless (zerop (call-process
4989 "touch" nil (current-buffer) nil "-t" touch-time file))
4990 (pop-to-buffer (current-buffer))
4991 (error "tramp-touch: touch failed"))))))
4993 (defun tramp-buffer-name (multi-method method user host)
4994 "A name for the connection buffer for USER at HOST using METHOD."
4996 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
4997 (let ((method (tramp-find-method multi-method method user host)))
4999 (format "*tramp/%s %s@%s*" method user host)
5000 (format "*tramp/%s %s*" method host)))))
5002 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5003 "A name for the multi method connection buffer.
5004 MULTI-METHOD gives the multi method, METHOD the array of methods,
5005 USER the array of user names, HOST the array of host names."
5006 (unless (and (= (length method) (length user))
5007 (= (length method) (length host)))
5008 (error "Syntax error in multi method (implementation error)"))
5009 (let ((len (length method))
5014 (cons (if (aref user i)
5015 (format "%s#%s@%s:" (aref method i)
5016 (aref user i) (aref host i))
5017 (format "%s@%s:" (aref method i) (aref host i)))
5020 (format "*%s/%s %s*"
5022 (apply 'concat (reverse string-list)))))
5024 (defun tramp-get-buffer (multi-method method user host)
5025 "Get the connection buffer to be used for USER at HOST using METHOD."
5026 (with-current-buffer
5027 (get-buffer-create (tramp-buffer-name multi-method method user host))
5028 (setq buffer-undo-list t)
5031 (defun tramp-debug-buffer-name (multi-method method user host)
5032 "A name for the debug buffer for USER at HOST using METHOD."
5034 (tramp-buffer-name-multi-method "debug tramp"
5035 multi-method method user host)
5036 (let ((method (tramp-find-method multi-method method user host)))
5038 (format "*debug tramp/%s %s@%s*" method user host)
5039 (format "*debug tramp/%s %s*" method host)))))
5041 (defun tramp-get-debug-buffer (multi-method method user host)
5042 "Get the debug buffer for USER at HOST using METHOD."
5043 (with-current-buffer
5045 (tramp-debug-buffer-name multi-method method user host))
5046 (setq buffer-undo-list t)
5049 (defun tramp-find-executable (multi-method method user host
5050 progname dirlist ignore-tilde)
5051 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5052 First args METHOD, USER and HOST specify the connection, PROGNAME
5053 is the program to search for, and DIRLIST gives the list of directories
5054 to search. If IGNORE-TILDE is non-nil, directory names starting
5055 with `~' will be ignored.
5057 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5059 This function expects to be in the right *tramp* buffer."
5062 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5063 ;; `remove' is in CL, and we want to avoid CL dependencies.
5066 (setq d (car dirlist))
5067 (setq dirlist (cdr dirlist))
5068 (unless (char-equal ?~ (aref d 0))
5069 (setq newdl (cons d newdl))))
5070 (setq dirlist (nreverse newdl))))
5072 multi-method method user host
5073 (format (concat "while read d; "
5074 "do if test -x $d/%s -a -f $d/%s; "
5075 "then echo tramp_executable $d/%s; "
5076 "break; fi; done <<'EOF'")
5077 progname progname progname))
5079 (tramp-send-command multi-method method user host d))
5081 (tramp-send-command multi-method method user host "EOF")
5082 (tramp-wait-for-output)
5083 (goto-char (point-max))
5084 (when (search-backward "tramp_executable " nil t)
5085 (skip-chars-forward "^ ")
5086 (skip-chars-forward " ")
5087 (buffer-substring (point) (tramp-line-end-position)))))
5089 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5090 "Sets the remote environment VAR to existing directories from DIRLIST.
5091 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5092 so, it is added to the environment variable VAR."
5093 (let ((existing-dirs
5098 (tramp-make-tramp-file-name multi-method method user host x))
5100 (tramp-make-tramp-file-name multi-method method user host x)))
5104 multi-method method user host
5106 (mapconcat 'identity (delq nil existing-dirs) ":")
5108 (tramp-wait-for-output)))
5110 ;; -- communication with external shell --
5112 (defun tramp-find-file-exists-command (multi-method method user host)
5113 "Find a command on the remote host for checking if a file exists.
5114 Here, we are looking for a command which has zero exit status if the
5115 file exists and nonzero exit status otherwise."
5116 (make-local-variable 'tramp-file-exists-command)
5117 (tramp-message 9 "Finding command to check if file exists")
5119 (tramp-make-tramp-file-name
5120 multi-method method user host
5121 "/")) ;assume this file always exists
5123 (tramp-make-tramp-file-name
5124 multi-method method user host
5125 "/ this file does not exist "))) ;assume this never exists
5126 ;; The algorithm is as follows: we try a list of several commands.
5127 ;; For each command, we first run `$cmd /' -- this should return
5128 ;; true, as the root directory always exists. And then we run
5129 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5130 ;; does not exist. This should return false. We use the first
5131 ;; command we find that seems to work.
5132 ;; The list of commands to try is as follows:
5133 ;; `ls -d' This works on most systems, but NetBSD 1.4
5134 ;; has a bug: `ls' always returns zero exit
5135 ;; status, even for files which don't exist.
5136 ;; `test -e' Some Bourne shells have a `test' builtin
5137 ;; which does not know the `-e' option.
5138 ;; `/bin/test -e' For those, the `test' binary on disk normally
5139 ;; provides the option. Alas, the binary
5140 ;; is sometimes `/bin/test' and sometimes it's
5142 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5144 (and (setq tramp-file-exists-command "test -e %s")
5145 (file-exists-p existing)
5146 (not (file-exists-p nonexisting)))
5147 (and (setq tramp-file-exists-command "/bin/test -e %s")
5148 (file-exists-p existing)
5149 (not (file-exists-p nonexisting)))
5150 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5151 (file-exists-p existing)
5152 (not (file-exists-p nonexisting)))
5153 (and (setq tramp-file-exists-command "ls -d %s")
5154 (file-exists-p existing)
5155 (not (file-exists-p nonexisting))))
5156 (error "Couldn't find command to check if file exists"))))
5159 ;; CCC test ksh or bash found for tilde expansion?
5160 (defun tramp-find-shell (multi-method method user host)
5161 "Find a shell on the remote host which groks tilde expansion."
5163 (tramp-send-command multi-method method user host "echo ~root")
5164 (tramp-wait-for-output)
5166 ((string-match "^~root$" (buffer-string))
5168 (or (tramp-find-executable multi-method method user host
5169 "bash" tramp-remote-path t)
5170 (tramp-find-executable multi-method method user host
5171 "ksh" tramp-remote-path t)))
5173 (error "Couldn't find a shell which groks tilde expansion"))
5174 ;; Find arguments for this shell.
5175 (let ((alist tramp-sh-extra-args)
5177 (while (and alist (null extra-args))
5178 (setq item (pop alist))
5179 (when (string-match (car item) shell)
5180 (setq extra-args (cdr item))))
5181 (when extra-args (setq shell (concat shell " " extra-args))))
5183 5 "Starting remote shell `%s' for tilde expansion..." shell)
5185 multi-method method user host
5186 (concat "PS1='$ ' exec " shell)) ;
5187 (tramp-barf-if-no-shell-prompt
5188 (get-buffer-process (current-buffer))
5189 60 "Couldn't find remote `%s' prompt" shell)
5191 9 "Setting remote shell prompt...")
5192 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5193 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5194 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5196 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5197 tramp-rsh-end-of-line
5199 tramp-rsh-end-of-line
5200 tramp-rsh-end-of-line))
5201 (tramp-wait-for-output)
5203 9 "Setting remote shell prompt...done")
5205 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5206 (tramp-get-method-parameter
5207 multi-method method user host 'tramp-remote-sh))))))
5209 (defun tramp-check-ls-command (multi-method method user host cmd)
5210 "Checks whether the given `ls' executable groks `-n'.
5211 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5212 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5214 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5215 (when (file-executable-p
5216 (tramp-make-tramp-file-name multi-method method user host cmd))
5218 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5220 (tramp-send-command-and-check
5221 multi-method method user host
5222 (format "%s -lnd / >/dev/null"
5224 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5226 (if (zerop result) "okay" "failed"))
5229 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5230 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5231 Returns nil if none was found, else the command is returned."
5234 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5235 ;; It would be better to use the CL function `find', but
5236 ;; we don't want run-time dependencies on CL.
5237 (while (and dl (not result))
5238 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5239 (when (tramp-check-ls-command multi-method method user host x)
5244 (defun tramp-find-ls-command (multi-method method user host)
5245 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5246 \(This option prints numeric user and group ids in a long listing.)"
5247 (tramp-message 9 "Finding a suitable `ls' command")
5249 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5250 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5251 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5253 ;; ------------------------------------------------------------
5254 ;; -- Functions for establishing connection --
5255 ;; ------------------------------------------------------------
5257 ;; The following functions are actions to be taken when seeing certain
5258 ;; prompts from the remote host. See the variable
5259 ;; `tramp-actions-before-shell' for usage of these functions.
5261 (defun tramp-action-login (p multi-method method user host)
5262 "Send the login name."
5263 (tramp-message 9 "Sending login name `%s'"
5264 (or user (user-login-name)))
5266 (process-send-string nil (concat (or user (user-login-name))
5267 tramp-rsh-end-of-line)))
5269 (defun tramp-action-password (p multi-method method user host)
5270 "Query the user for a password."
5272 (format "Password for %s "
5273 (tramp-make-tramp-file-name
5274 nil method user host ""))))
5275 (tramp-message 9 "Sending password")
5276 (tramp-enter-password p pw-prompt user host)))
5278 (defun tramp-action-succeed (p multi-method method user host)
5279 "Signal success in finding shell prompt."
5280 (tramp-message 9 "Found remote shell prompt.")
5282 (throw 'tramp-action 'ok))
5284 (defun tramp-action-permission-denied (p multi-method method user host)
5285 "Signal permission denied."
5286 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5287 (tramp-message 9 "Permission denied by remote host.")
5289 (throw 'tramp-action 'permission-denied))
5291 (defun tramp-action-yesno (p multi-method method user host)
5292 "Ask the user for confirmation using `yes-or-no-p'.
5293 Send \"yes\" to remote process on confirmation, abort otherwise.
5294 See also `tramp-action-yn'."
5295 (save-window-excursion
5296 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5297 (unless (yes-or-no-p (match-string 0))
5300 (throw 'tramp-action 'permission-denied))
5301 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5304 (defun tramp-action-yn (p multi-method method user host)
5305 "Ask the user for confirmation using `y-or-n-p'.
5306 Send \"y\" to remote process on confirmation, abort otherwise.
5307 See also `tramp-action-yesno'."
5308 (save-window-excursion
5309 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5310 (unless (y-or-n-p (match-string 0))
5312 (throw 'tramp-action 'permission-denied))
5314 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5316 (defun tramp-action-terminal (p multi-method method user host)
5317 "Tell the remote host which terminal type to use.
5318 The terminal type can be configured with `tramp-terminal-type'."
5319 (tramp-message 9 "Setting `%s' as terminal type."
5320 tramp-terminal-type)
5322 (process-send-string nil (concat tramp-terminal-type
5323 tramp-rsh-end-of-line)))
5325 (defun tramp-action-process-alive (p multi-method method user host)
5326 "Check whether a process has finished."
5327 (unless (memq (process-status p) '(run open))
5328 (throw 'tramp-action 'process-died)))
5330 (defun tramp-action-out-of-band (p multi-method method user host)
5331 "Check whether an out-of-band copy has finished."
5332 (cond ((and (memq (process-status p) '(stop exit))
5333 (zerop (process-exit-status p)))
5334 (tramp-message 9 "Process has finished.")
5335 (throw 'tramp-action 'ok))
5336 ((or (and (memq (process-status p) '(stop exit))
5337 (not (zerop (process-exit-status p))))
5338 (memq (process-status p) '(signal)))
5339 ;; `scp' could have copied correctly, but set modes could have failed.
5340 ;; This can be ignored.
5341 (goto-char (point-min))
5342 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5344 (tramp-message 10 "'set mode' error ignored.")
5345 (tramp-message 9 "Process has finished.")
5346 (throw 'tramp-action 'ok))
5347 (tramp-message 9 "Process has died.")
5348 (throw 'tramp-action 'process-died)))
5351 ;; The following functions are specifically for multi connections.
5353 (defun tramp-multi-action-login (p method user host)
5354 "Send the login name."
5355 (tramp-message 9 "Sending login name `%s'" user)
5357 (process-send-string p (concat user tramp-rsh-end-of-line)))
5359 (defun tramp-multi-action-password (p method user host)
5360 "Query the user for a password."
5362 (format "Password for %s "
5363 (tramp-make-tramp-file-name
5364 nil method user host ""))))
5365 (tramp-message 9 "Sending password")
5366 (tramp-enter-password p pw-prompt user host)))
5368 (defun tramp-multi-action-succeed (p method user host)
5369 "Signal success in finding shell prompt."
5370 (tramp-message 9 "Found shell prompt on `%s'" host)
5372 (throw 'tramp-action 'ok))
5374 (defun tramp-multi-action-permission-denied (p method user host)
5375 "Signal permission denied."
5376 (tramp-message 9 "Permission denied by remote host `%s'" host)
5379 (throw 'tramp-action 'permission-denied))
5381 (defun tramp-multi-action-process-alive (p method user host)
5382 "Check whether a process has finished."
5383 (unless (memq (process-status p) '(run open))
5384 (throw 'tramp-action 'process-died)))
5386 ;; Functions for processing the actions.
5388 (defun tramp-process-one-action (p multi-method method user host actions)
5389 "Wait for output from the shell and perform one action."
5390 (let (found item pattern action todo)
5392 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5393 (with-timeout (60 (throw 'tramp-action 'timeout))
5395 (tramp-accept-process-output p 1)
5396 (goto-char (point-min))
5399 (goto-char (point-min))
5400 (setq item (pop todo))
5401 (setq pattern (symbol-value (nth 0 item)))
5402 (setq action (nth 1 item))
5403 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5405 (when (re-search-forward (concat pattern "\\'") nil t)
5406 (setq found (funcall action p multi-method method user host)))))
5409 (defun tramp-process-actions (p multi-method method user host actions)
5410 "Perform actions until success."
5413 (tramp-message 9 "Waiting for prompts from remote shell")
5415 (catch 'tramp-action
5416 (tramp-process-one-action
5417 p multi-method method user host actions)
5419 (unless (eq exit 'ok)
5420 (tramp-clear-passwd user host)
5421 (error "Login failed"))))
5423 ;; For multi-actions.
5425 (defun tramp-process-one-multi-action (p method user host actions)
5426 "Wait for output from the shell and perform one action."
5427 (let (found item pattern action todo)
5429 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5430 (with-timeout (60 (throw 'tramp-action 'timeout))
5432 (tramp-accept-process-output p 1)
5434 (goto-char (point-min))
5436 (goto-char (point-min))
5437 (setq item (pop todo))
5438 (setq pattern (symbol-value (nth 0 item)))
5439 (setq action (nth 1 item))
5440 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5442 (when (re-search-forward (concat pattern "\\'") nil t)
5443 (setq found (funcall action p method user host)))))
5446 (defun tramp-process-multi-actions (p method user host actions)
5447 "Perform actions until success."
5450 (tramp-message 9 "Waiting for prompts from remote shell")
5452 (catch 'tramp-action
5453 (tramp-process-one-multi-action p method user host actions)
5455 (unless (eq exit 'ok)
5456 (tramp-clear-passwd user host)
5457 (error "Login failed"))))
5459 ;; Functions to execute when we have seen the remote shell prompt but
5460 ;; before we exec the Bourne-ish shell. Note that these commands
5461 ;; might be sent to any shell, not just a Bourne-ish shell. This
5462 ;; means that the commands need to work in all shells. (It is also
5463 ;; okay for some commands to just fail with an error message, but
5464 ;; please make sure that they at least don't crash the odd shell people
5465 ;; might be running...)
5466 (defun tramp-process-initial-commands (p
5467 multi-method method user host
5469 "Send list of commands to remote host, in order."
5472 (setq cmd (pop commands))
5474 (tramp-message 10 "Sending command to remote shell: %s"
5476 (tramp-send-command multi-method method user host cmd nil t)
5477 (tramp-barf-if-no-shell-prompt
5478 p 60 "Remote shell command failed: %s" cmd))
5481 ;; The actual functions for opening connections.
5483 (defun tramp-open-connection-telnet (multi-method method user host)
5484 "Open a connection using a telnet METHOD.
5485 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5486 login prompt, then sends the user name USER, then waits for a remote
5487 password prompt. It queries the user for the password, then sends the
5488 password to the remote host.
5490 If USER is nil, uses value returned by `(user-login-name)' instead.
5492 Recognition of the remote shell prompt is based on the variables
5493 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5496 Please note that it is NOT possible to use this connection method
5497 together with an out-of-band transfer method! You must use an inline
5500 Maybe the different regular expressions need to be tuned.
5502 * Actually, the telnet program as well as the args to be used can be
5503 specified in the method parameters, see the variable `tramp-methods'."
5505 (when (tramp-method-out-of-band-p multi-method method user host)
5506 (error "Cannot use out-of-band method `%s' with telnet connection method"
5509 (error "Cannot multi-connect using telnet connection method"))
5510 (tramp-pre-connection multi-method method user host tramp-chunksize)
5511 (tramp-message 7 "Opening connection for %s@%s using %s..."
5512 (or user (user-login-name)) host method)
5513 (let ((process-environment (copy-sequence process-environment)))
5514 (setenv "TERM" tramp-terminal-type)
5515 (let* ((default-directory (tramp-temporary-file-directory))
5516 ;; If we omit the conditional here, then we would use
5517 ;; `undecided-dos' in some cases. With the conditional,
5518 ;; we use nil in these cases. Which one is right?
5519 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5520 (> emacs-major-version 20))
5521 tramp-dos-coding-system))
5522 (p (apply 'start-process
5523 (tramp-buffer-name multi-method method user host)
5524 (tramp-get-buffer multi-method method user host)
5525 (tramp-get-method-parameter
5527 (tramp-find-method multi-method method user host)
5528 user host 'tramp-login-program)
5530 (tramp-get-method-parameter
5532 (tramp-find-method multi-method method user host)
5533 user host 'tramp-login-args)))
5536 (tramp-set-process-query-on-exit-flag p nil)
5537 (set-buffer (tramp-get-buffer multi-method method user host))
5539 (tramp-process-actions p multi-method method user host
5540 tramp-actions-before-shell)
5541 (tramp-open-connection-setup-interactive-shell
5542 p multi-method method user host)
5543 (tramp-post-connection multi-method method user host)))))
5546 (defun tramp-open-connection-rsh (multi-method method user host)
5547 "Open a connection using an rsh METHOD.
5548 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5549 password or shell prompt. If a password prompt is seen, the user is
5550 queried for a password, this function sends the password to the remote
5551 host and waits for a shell prompt.
5553 If USER is nil, start the command `rsh HOST'[*] instead
5555 Recognition of the remote shell prompt is based on the variables
5556 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5559 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5560 be a port number for ssh, and \"-p yy\" will be added to the list of
5561 arguments, and xx will be used as the host name to connect to.
5563 * Actually, the rsh program to be used can be specified in the
5564 method parameters, see the variable `tramp-methods'."
5567 (error "Cannot multi-connect using rsh connection method"))
5568 (tramp-pre-connection multi-method method user host tramp-chunksize)
5569 (if (and user (not (string= user "")))
5570 (tramp-message 7 "Opening connection for %s@%s using %s..."
5572 (tramp-message 7 "Opening connection at %s using %s..." host method))
5573 (let ((process-environment (copy-sequence process-environment))
5574 (bufnam (tramp-buffer-name multi-method method user host))
5575 (buf (tramp-get-buffer multi-method method user host))
5576 (login-program (tramp-get-method-parameter
5578 (tramp-find-method multi-method method user host)
5579 user host 'tramp-login-program))
5580 (login-args (tramp-get-method-parameter
5582 (tramp-find-method multi-method method user host)
5583 user host 'tramp-login-args))
5585 ;; The following should be changed. We need a more general
5586 ;; mechanism to parse extra host args.
5587 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5588 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5589 (setq real-host (match-string 1 host)))
5590 (setenv "TERM" tramp-terminal-type)
5591 (let* ((default-directory (tramp-temporary-file-directory))
5592 ;; If we omit the conditional, we would use
5593 ;; `undecided-dos' in some cases. With the conditional,
5594 ;; we use nil in these cases. Which one is right?
5595 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5596 (> emacs-major-version 20))
5597 tramp-dos-coding-system))
5598 (p (if (and user (not (string= user "")))
5599 (apply #'start-process bufnam buf login-program
5600 real-host "-l" user login-args)
5601 (apply #'start-process bufnam buf login-program
5602 real-host login-args)))
5604 (tramp-set-process-query-on-exit-flag p nil)
5607 (tramp-process-actions p multi-method method user host
5608 tramp-actions-before-shell)
5609 (tramp-message 7 "Initializing remote shell")
5610 (tramp-open-connection-setup-interactive-shell
5611 p multi-method method user host)
5612 (tramp-post-connection multi-method method user host)))))
5614 (defun tramp-open-connection-su (multi-method method user host)
5615 "Open a connection using the `su' program with METHOD.
5616 This starts `su - USER', then waits for a password prompt. The HOST
5617 name must be equal to the local host name or to `localhost'.
5619 If USER is nil, uses value returned by user-login-name instead.
5621 Recognition of the remote shell prompt is based on the variables
5622 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5623 set up correctly. Note that the other user may have a different shell
5624 prompt than you do, so it is not at all unlikely that the variable
5625 `shell-prompt-pattern' is set up wrongly!"
5627 (when (tramp-method-out-of-band-p multi-method method user host)
5628 (error "Cannot use out-of-band method `%s' with `su' connection method"
5630 (unless (or (string-match (concat "^" (regexp-quote host))
5632 (string= "localhost" host)
5635 "Cannot connect to different host `%s' with `su' connection method"
5637 (tramp-pre-connection multi-method method user host tramp-chunksize)
5638 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5639 (or user "<root>") method)
5640 (let ((process-environment (copy-sequence process-environment)))
5641 (setenv "TERM" tramp-terminal-type)
5642 (let* ((default-directory (tramp-temporary-file-directory))
5643 ;; If we omit the conditional, we use `undecided-dos' in
5644 ;; some cases. With the conditional, we use nil in these
5645 ;; cases. What's the difference? Which one is right?
5646 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5647 (> emacs-major-version 20))
5648 tramp-dos-coding-system))
5649 (p (apply 'start-process
5650 (tramp-buffer-name multi-method method user host)
5651 (tramp-get-buffer multi-method method user host)
5652 (tramp-get-method-parameter
5654 (tramp-find-method multi-method method user host)
5655 user host 'tramp-login-program)
5658 (format-spec x `((?u . ,(or user "root")))))
5659 (tramp-get-method-parameter
5661 (tramp-find-method multi-method method user host)
5662 user host 'tramp-login-args))))
5665 (tramp-set-process-query-on-exit-flag p nil)
5666 (set-buffer (tramp-get-buffer multi-method method user host))
5667 (tramp-process-actions p multi-method method user host
5668 tramp-actions-before-shell)
5669 (tramp-open-connection-setup-interactive-shell
5670 p multi-method method user host)
5671 (tramp-post-connection multi-method method
5674 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5675 ;; handle not giving a user name in the "file name".
5677 ;; This is more difficult than for the single-hop method. In the
5678 ;; multi-hop-method, the desired behaviour should be that the
5679 ;; user must specify names for the telnet hops of which the user
5680 ;; name is different than the "original" name (or different from
5681 ;; the previous hop.
5682 (defun tramp-open-connection-multi (multi-method method user host)
5683 "Open a multi-hop connection using METHOD.
5684 This uses a slightly changed file name syntax. The idea is to say
5685 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5686 This will use telnet to log in as u1 to h1, then use rsh from there to
5687 log in as u2 to h2."
5689 (unless multi-method
5690 (error "Multi-hop open connection function called on non-multi method"))
5691 (when (tramp-method-out-of-band-p multi-method method user host)
5692 (error "No out of band multi-hop connections"))
5693 (unless (and (arrayp method) (not (stringp method)))
5694 (error "METHOD must be an array of strings for multi methods"))
5695 (unless (and (arrayp user) (not (stringp user)))
5696 (error "USER must be an array of strings for multi methods"))
5697 (unless (and (arrayp host) (not (stringp host)))
5698 (error "HOST must be an array of strings for multi methods"))
5699 (unless (and (= (length method) (length user))
5700 (= (length method) (length host)))
5701 (error "Arrays METHOD, USER, HOST must have equal length"))
5702 (tramp-pre-connection multi-method method user host tramp-chunksize)
5703 (tramp-message 7 "Opening `%s' connection..." multi-method)
5704 (let ((process-environment (copy-sequence process-environment)))
5705 (setenv "TERM" tramp-terminal-type)
5706 (let* ((default-directory (tramp-temporary-file-directory))
5707 ;; If we omit the conditional, we use `undecided-dos' in
5708 ;; some cases. With the conditional, we use nil in these
5709 ;; cases. What's the difference? Which one is right?
5710 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5711 (> emacs-major-version 20))
5712 tramp-dos-coding-system))
5713 (p (start-process (tramp-buffer-name multi-method method user host)
5714 (tramp-get-buffer multi-method method user host)
5715 tramp-multi-sh-program))
5716 (num-hops (length method))
5718 (tramp-set-process-query-on-exit-flag p nil)
5719 (tramp-message 9 "Waiting 60s for local shell to come up...")
5720 (unless (tramp-wait-for-regexp
5721 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5722 shell-prompt-pattern tramp-shell-prompt-pattern))
5723 (pop-to-buffer (buffer-name))
5725 (error "Couldn't find local shell prompt"))
5726 ;; Now do all the connections as specified.
5727 (while (< i num-hops)
5728 (let* ((m (aref method i))
5731 (entry (assoc m tramp-multi-connection-function-alist))
5732 (multi-func (nth 1 entry))
5733 (command (nth 2 entry)))
5734 ;; The multi-funcs don't need to do save-match-data, as that
5736 (funcall multi-func p m u h command)
5739 (tramp-open-connection-setup-interactive-shell
5740 p multi-method method user host)
5741 (tramp-post-connection multi-method method user host)))))
5743 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5744 ;; of no user name provided. Hack to make it work as it did before:
5745 ;; changed `user' to `(or user (user-login-name))' in the places where
5746 ;; the value is actually used.
5747 (defun tramp-multi-connect-telnet (p method user host command)
5748 "Issue `telnet' command.
5749 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5750 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5751 the host name, and `%n' is replaced with an end of line character, as
5752 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5755 If USER is nil, uses the return value of (user-login-name) instead."
5756 (let ((cmd (format-spec command
5757 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5758 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5761 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5762 (process-send-string p cmd)
5763 (tramp-process-multi-actions p method user host
5764 tramp-multi-actions)))
5766 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5767 ;; of no user name provided. Hack to make it work as it did before:
5768 ;; changed `user' to `(or user (user-login-name))' in the places where
5769 ;; the value is actually used.
5770 (defun tramp-multi-connect-rlogin (p method user host command)
5771 "Issue `rlogin' command.
5772 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5773 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5774 with the user name, `%h' will be replaced with the host name, and `%n'
5775 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5776 `%%' if you want to use a literal percent character.
5778 If USER is nil, uses the return value of (user-login-name) instead."
5779 (let ((cmd (format-spec command `((?h . ,host)
5780 (?u . ,(or user (user-login-name)))
5781 (?n . ,tramp-rsh-end-of-line))))
5782 (cmd1 (format-spec command `((?h . ,host)
5783 (?u . ,(or user (user-login-name)))
5787 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5788 (process-send-string p cmd)
5789 (tramp-process-multi-actions p method user host
5790 tramp-multi-actions)))
5792 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5793 ;; of no user name provided. Hack to make it work as it did before:
5794 ;; changed `user' to `(or user (user-login-name))' in the places where
5795 ;; the value is actually used.
5796 (defun tramp-multi-connect-su (p method user host command)
5797 "Issue `su' command.
5798 Uses shell COMMAND to issue a `su' command to log in as USER on
5799 HOST. The HOST name is ignored, this just changes the user id on the
5800 host currently logged in to.
5802 If USER is nil, uses the return value of (user-login-name) instead.
5804 You can use percent escapes in the COMMAND. `%u' is replaced with the
5805 user name, and `%n' is replaced with the value of
5806 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5808 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5809 (?n . ,tramp-rsh-end-of-line))))
5810 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5814 (tramp-message 9 "Sending su command `%s'" cmd1)
5815 (process-send-string p cmd)
5816 (tramp-process-multi-actions p method user host
5817 tramp-multi-actions)))
5819 ;; Utility functions.
5821 (defun tramp-accept-process-output
5822 (&optional process timeout timeout-msecs)
5823 "Like `accept-process-output' for Tramp processes.
5824 This is needed in order to hide `last-coding-system-used', which is set
5825 for process communication also."
5826 (let (last-coding-system-used)
5827 (accept-process-output process timeout timeout-msecs)))
5829 (defun tramp-wait-for-regexp (proc timeout regexp)
5830 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5831 Expects the output of PROC to be sent to the current buffer. Returns
5832 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5835 (start-time (current-time)))
5837 ;; Work around a bug in XEmacs 21, where the timeout
5838 ;; expires faster than it should. This degenerates
5839 ;; to polling for buggy XEmacsen, but oh, well.
5840 (while (and (not found)
5841 (< (tramp-time-diff (current-time) start-time)
5843 (with-timeout (timeout)
5845 (tramp-accept-process-output proc 1)
5846 (unless (memq (process-status proc) '(run open))
5847 (error "Process has died"))
5848 (goto-char (point-min))
5849 (setq found (re-search-forward regexp nil t))))))
5852 (tramp-accept-process-output proc 1)
5853 (unless (memq (process-status proc) '(run open))
5854 (error "Process has died"))
5855 (goto-char (point-min))
5856 (setq found (re-search-forward regexp nil t)))))
5857 (when tramp-debug-buffer
5859 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5860 tramp-current-user tramp-current-host)
5861 (point-min) (point-max))
5865 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5866 tramp-current-user tramp-current-host))
5867 (goto-char (point-max))
5868 (insert "[[Regexp `" regexp "' not found"
5869 (if timeout (format " in %d secs" timeout) "")
5873 (defun tramp-wait-for-shell-prompt (proc timeout)
5874 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5875 See `tramp-wait-for-regexp' for more details.
5876 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5877 and `tramp-shell-prompt-pattern'."
5878 (tramp-wait-for-regexp
5880 (format "\\(%s\\|%s\\)\\'"
5881 shell-prompt-pattern tramp-shell-prompt-pattern)))
5883 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5884 "Wait for shell prompt and barf if none appears.
5885 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5886 seconds. If not, it produces an error message with the given ERROR-ARGS."
5887 (unless (tramp-wait-for-shell-prompt proc timeout)
5888 (pop-to-buffer (buffer-name))
5889 (apply 'error error-args)))
5891 (defun tramp-enter-password (p prompt user host)
5892 "Prompt for a password and send it to the remote end.
5893 Uses PROMPT as a prompt and sends the password to process P."
5894 (let ((pw (tramp-read-passwd user host prompt)))
5896 (process-send-string
5898 (or (tramp-get-method-parameter
5899 tramp-current-multi-method
5900 tramp-current-method
5903 'tramp-password-end-of-line)
5904 tramp-default-password-end-of-line)))))
5906 ;; HHH: Not Changed. This might handle the case where USER is not
5907 ;; given in the "File name" very poorly. Then, the local
5908 ;; variable tramp-current-user will be set to nil.
5909 (defun tramp-pre-connection (multi-method method user host chunksize)
5910 "Do some setup before actually logging in.
5911 METHOD, USER and HOST specify the connection."
5912 (set-buffer (tramp-get-buffer multi-method method user host))
5913 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5914 (set (make-local-variable 'tramp-current-method) method)
5915 (set (make-local-variable 'tramp-current-user) user)
5916 (set (make-local-variable 'tramp-current-host) host)
5917 (set (make-local-variable 'tramp-chunksize) chunksize)
5918 (set (make-local-variable 'inhibit-eol-conversion) nil)
5921 (defun tramp-open-connection-setup-interactive-shell
5922 (p multi-method method user host)
5923 "Set up an interactive shell.
5924 Mainly sets the prompt and the echo correctly. P is the shell process
5925 to set up. METHOD, USER and HOST specify the connection."
5926 ;; Wait a bit in case the remote end feels like sending a little
5927 ;; junk first. It seems that fencepost.gnu.org does this when doing
5928 ;; a Kerberos login.
5930 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5931 (tramp-process-initial-commands p multi-method method user host
5932 tramp-initial-commands)
5933 ;; It is useful to set the prompt in the following command because
5934 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5935 ;; about and thus /bin/sh will display a strange prompt. For
5936 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
5937 ;; the current working directory but /bin/sh will display a dollar
5938 ;; sign. The following command line sets $PS1 to a sane value, and
5939 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
5940 ;; Pittman reports that the unusual positioning of the single quotes
5941 ;; makes it work under `rc', too. We also unset the variable $ENV
5942 ;; because that is read by some sh implementations (eg, bash when
5943 ;; called as sh) on startup; this way, we avoid the startup file
5945 (tramp-send-command-internal
5946 multi-method method user host
5947 (format "exec env 'ENV=' 'PS1=$ ' %s"
5948 (tramp-get-method-parameter
5949 multi-method method user host 'tramp-remote-sh))
5950 (format "remote `%s' to come up"
5951 (tramp-get-method-parameter
5952 multi-method method user host 'tramp-remote-sh)))
5953 (tramp-barf-if-no-shell-prompt
5955 "Remote `%s' didn't come up. See buffer `%s' for details"
5956 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
5958 (tramp-message 8 "Setting up remote shell environment")
5959 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5960 (tramp-send-command-internal multi-method method user host
5961 "stty -inlcr -echo kill '^U'")
5963 ;; Ignore garbage after stty command.
5964 (tramp-send-command-internal multi-method method user host
5967 (tramp-send-command-internal multi-method method user host
5968 "TERM=dumb; export TERM")
5970 ;; Check whether the remote host suffers from buggy `send-process-string'.
5971 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
5972 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
5973 ;; Emacs makes a hack when this host type is detected locally. It cannot
5974 ;; handle remote hosts, though.
5975 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
5976 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
5977 (tramp-send-command-internal multi-method method user host
5978 "(uname -sr) 2>/dev/null")
5979 (goto-char (point-min))
5980 (when (looking-at "FreeBSD")
5981 (setq tramp-chunksize 500)))
5983 ;; Try to set up the coding system correctly.
5984 ;; CCC this can't be the right way to do it. Hm.
5987 (tramp-message 9 "Determining coding system")
5988 (tramp-send-command-internal multi-method method user host
5989 "echo foo ; echo bar")
5990 (goto-char (point-min))
5991 (if (featurep 'mule)
5992 ;; Use MULE to select the right EOL convention for communicating
5993 ;; with the process.
5994 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
5995 cs-decode cs-encode)
5996 (when (symbolp cs) (setq cs (cons cs cs)))
5997 (setq cs-decode (car cs))
5998 (setq cs-encode (cdr cs))
5999 (unless cs-decode (setq cs-decode 'undecided))
6000 (unless cs-encode (setq cs-encode 'undecided))
6001 (setq cs-encode (tramp-coding-system-change-eol-conversion
6003 (when (search-forward "\r" nil t)
6004 (setq cs-decode (tramp-coding-system-change-eol-conversion
6006 (set-buffer-process-coding-system cs-decode cs-encode))
6007 ;; Look for ^M and do something useful if found.
6008 (when (search-forward "\r" nil t)
6009 ;; We have found a ^M but cannot frob the process coding system
6010 ;; because we're running on a non-MULE Emacs. Let's try
6013 (tramp-message 9 "Trying `stty -onlcr'")
6014 (tramp-send-command-internal multi-method method user host
6018 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6019 (tramp-send-command-internal
6020 multi-method method user host
6021 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6023 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6024 (tramp-send-command-internal multi-method method user host
6025 "set +o vi +o emacs")
6027 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6028 (tramp-send-command-internal
6029 multi-method method user host
6030 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6032 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6033 (tramp-send-command-internal multi-method method user host
6036 (tramp-message 9 "Setting shell prompt")
6037 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6038 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6039 ;; the last time we sent a command, to avoid tramp-send-command to send
6040 ;; "echo are you awake".
6041 (setq tramp-last-cmd-time (current-time))
6043 multi-method method user host
6044 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6045 tramp-rsh-end-of-line
6047 tramp-rsh-end-of-line))
6048 (tramp-wait-for-output))
6050 (defun tramp-post-connection (multi-method method user host)
6051 "Prepare a remote shell before being able to work on it.
6052 METHOD, USER and HOST specify the connection.
6053 Among other things, this finds a shell which groks tilde expansion,
6054 tries to find an `ls' command which groks the `-n' option, sets the
6055 locale to C and sets up the remote shell search path."
6056 ;; Search for a good shell before searching for a command which
6057 ;; checks if a file exists. This is done because Tramp wants to use
6058 ;; "test foo; echo $?" to check if various conditions hold, and
6059 ;; there are buggy /bin/sh implementations which don't execute the
6060 ;; "echo $?" part if the "test" part has an error. In particular,
6061 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6062 ;; with buggy /bin/sh implementations will have a working bash or
6064 (tramp-find-shell multi-method method user host)
6065 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6066 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6067 ;; output of (tramp-find-shell) to show up along with the output of
6068 ;; (tramp-find-ls-command) testing.
6070 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6071 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6073 ;; After much debugging I couldn't find any problem with the implementation
6074 ;; of that function though. The workaround stays for me at least. :/
6076 ;; Daniel Pittman <daniel@danann.net>
6079 (tramp-find-file-exists-command multi-method method user host)
6080 (make-local-variable 'tramp-ls-command)
6081 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6082 (unless tramp-ls-command
6085 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6086 (setq tramp-ls-command
6087 (tramp-find-executable multi-method method user host
6088 "ls" tramp-remote-path nil)))
6089 (unless tramp-ls-command
6090 (error "Fatal error: Couldn't find remote executable `ls'"))
6091 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6093 (tramp-send-command multi-method method user host
6094 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6095 "return $1" tramp-rsh-end-of-line
6097 (tramp-wait-for-output)
6098 ;; Set remote PATH variable.
6099 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6100 ;; Tell remote shell to use standard time format, needed for
6101 ;; parsing `ls -l' output.
6102 (tramp-send-command multi-method method user host
6103 "LC_TIME=C; export LC_TIME; echo huhu")
6104 (tramp-wait-for-output)
6105 (tramp-send-command multi-method method user host
6106 "mesg n; echo huhu")
6107 (tramp-wait-for-output)
6108 (tramp-send-command multi-method method user host
6109 "biff n ; echo huhu")
6110 (tramp-wait-for-output)
6111 ;; Unalias ls(1) to work around issues with those silly people who make it
6112 ;; spit out ANSI escapes or whatever.
6113 (tramp-send-command multi-method method user host
6114 "unalias ls; echo huhu")
6115 (tramp-wait-for-output)
6116 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6117 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6118 ;; for otherwise the shell crashes.
6120 (make-local-variable 'tramp-test-groks-nt)
6121 (tramp-send-command multi-method method user host
6123 (tramp-wait-for-output)
6124 (goto-char (point-min))
6125 (setq tramp-test-groks-nt
6126 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6127 (unless tramp-test-groks-nt
6129 multi-method method user host
6130 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6131 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6133 (tramp-wait-for-output)
6134 ;; Send the fallback `uudecode' script.
6136 (tramp-send-string multi-method method user host tramp-uudecode)
6137 (tramp-wait-for-output)
6140 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6141 (let ((tramp-remote-perl
6142 (or (tramp-find-executable multi-method method user host
6143 "perl5" tramp-remote-path nil)
6144 (tramp-find-executable multi-method method user host
6145 "perl" tramp-remote-path nil))))
6146 (when tramp-remote-perl
6147 (tramp-set-connection-property "perl" tramp-remote-perl
6148 multi-method method user host)
6149 (unless (tramp-method-out-of-band-p multi-method method user host)
6150 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6152 multi-method method user host
6153 (concat "tramp_encode () {\n"
6154 (format tramp-perl-encode tramp-remote-perl)
6157 (tramp-wait-for-output)
6159 multi-method method user host
6160 (concat "tramp_encode_with_module () {\n"
6161 (format tramp-perl-encode-with-module tramp-remote-perl)
6164 (tramp-wait-for-output)
6165 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6167 multi-method method user host
6168 (concat "tramp_decode () {\n"
6169 (format tramp-perl-decode tramp-remote-perl)
6172 (tramp-wait-for-output)
6174 multi-method method user host
6175 (concat "tramp_decode_with_module () {\n"
6176 (format tramp-perl-decode-with-module tramp-remote-perl)
6179 (tramp-wait-for-output))))
6182 (let ((ln (tramp-find-executable multi-method method user host
6183 "ln" tramp-remote-path nil)))
6185 (tramp-set-connection-property "ln" ln multi-method method user host)))
6187 ;; Find the right encoding/decoding commands to use.
6188 (unless (tramp-method-out-of-band-p multi-method method user host)
6189 (tramp-find-inline-encoding multi-method method user host))
6190 ;; If encoding/decoding command are given, test to see if they work.
6191 ;; CCC: Maybe it would be useful to run the encoder both locally and
6192 ;; remotely to see if they produce the same result.
6193 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6194 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6195 (magic-string "xyzzy"))
6196 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6197 (tramp-kill-process multi-method method user host)
6198 ;; Improve error message and/or error check.
6200 "Must give both decoding and encoding command in method definition"))
6201 (when (and rem-enc rem-dec)
6204 "Checking to see if encoding/decoding commands work on remote host...")
6206 multi-method method user host
6207 (format "echo %s | %s | %s"
6208 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6209 (tramp-wait-for-output)
6210 (unless (looking-at (regexp-quote magic-string))
6211 (tramp-kill-process multi-method method user host)
6212 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6216 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6218 ;; CCC: We should either implement a Perl version of base64 encoding
6219 ;; and decoding. Then we just use that in the last item. The other
6220 ;; alternative is to use the Perl version of UU encoding. But then
6221 ;; we need a Lisp version of uuencode.
6223 ;; Old text from documentation of tramp-methods:
6224 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6225 ;; of the base64 methods instead since base64 encoding is much more
6226 ;; reliable and the commands are more standardized between the different
6227 ;; Unix versions. But if you can't use base64 for some reason, please
6228 ;; note that the default uudecode command does not work well for some
6229 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6230 ;; the following command for uudecode:
6232 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6234 ;; For Irix, no solution is known yet.
6236 (defvar tramp-coding-commands
6237 '(("mimencode -b" "mimencode -u -b"
6238 base64-encode-region base64-decode-region)
6239 ("mmencode -b" "mmencode -u -b"
6240 base64-encode-region base64-decode-region)
6241 ("recode data..base64" "recode base64..data"
6242 base64-encode-region base64-decode-region)
6243 ("uuencode xxx" "uudecode -o /dev/stdout"
6244 tramp-uuencode-region uudecode-decode-region)
6245 ("uuencode xxx" "uudecode -o -"
6246 tramp-uuencode-region uudecode-decode-region)
6247 ("uuencode xxx" "uudecode -p"
6248 tramp-uuencode-region uudecode-decode-region)
6249 ("uuencode xxx" "tramp_uudecode"
6250 tramp-uuencode-region uudecode-decode-region)
6251 ("tramp_encode_with_module" "tramp_decode_with_module"
6252 base64-encode-region base64-decode-region)
6253 ("tramp_encode" "tramp_decode"
6254 base64-encode-region base64-decode-region))
6255 "List of coding commands for inline transfer.
6256 Each item is a list that looks like this:
6258 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6260 The REMOTE-ENCODING should be a string, giving a command accepting a
6261 plain file on standard input and writing the encoded file to standard
6262 output. The REMOTE-DECODING should also be a string, giving a command
6263 accepting an encoded file on standard input and writing the decoded
6264 file to standard output.
6266 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6267 symbols, giving functions. If they are strings, then they can contain
6268 the \"%s\" format specifier. If that specifier is present, the input
6269 filename will be put into the command line at that spot. If the
6270 specifier is not present, the input should be read from standard
6273 If they are functions, they will be called with two arguments, start
6274 and end of region, and are expected to replace the region contents
6275 with the encoded or decoded results, respectively.")
6277 (defun tramp-find-inline-encoding (multi-method method user host)
6278 "Find an inline transfer encoding that works.
6279 Goes through the list `tramp-coding-commands'."
6280 (let ((commands tramp-coding-commands)
6283 (while (and commands (null found))
6284 (setq item (pop commands))
6286 (let ((rem-enc (nth 0 item))
6287 (rem-dec (nth 1 item))
6288 (loc-enc (nth 2 item))
6289 (loc-dec (nth 3 item)))
6290 ;; Check if remote encoding and decoding commands can be
6291 ;; called remotely with null input and output. This makes
6292 ;; sure there are no syntax errors and the command is really
6293 ;; found. Note that we do not redirect stdout to /dev/null,
6294 ;; for two reaons: when checking the decoding command, we
6295 ;; actually check the output it gives. And also, when
6296 ;; redirecting "mimencode" output to /dev/null, then as root
6297 ;; it might change the permissions of /dev/null!
6298 (tramp-message-for-buffer
6299 multi-method method user host 9
6300 "Checking remote encoding command `%s' for sanity" rem-enc)
6301 (unless (zerop (tramp-send-command-and-check
6302 multi-method method user host
6303 (format "%s </dev/null" rem-enc) t))
6304 (throw 'wont-work nil))
6305 (tramp-message-for-buffer
6306 multi-method method user host 9
6307 "Checking remote decoding command `%s' for sanity" rem-dec)
6308 (unless (zerop (tramp-send-command-and-check
6309 multi-method method user host
6310 (format "echo %s | %s | %s"
6311 magic rem-enc rem-dec) t))
6312 (throw 'wont-work nil))
6314 (goto-char (point-min))
6315 (unless (looking-at (regexp-quote magic))
6316 (throw 'wont-work nil)))
6317 ;; If the local encoder or decoder is a string, the
6318 ;; corresponding command has to work locally.
6319 (when (stringp loc-enc)
6320 (tramp-message-for-buffer
6321 multi-method method user host 9
6322 "Checking local encoding command `%s' for sanity" loc-enc)
6323 (unless (zerop (tramp-call-local-coding-command
6325 (throw 'wont-work nil)))
6326 (when (stringp loc-dec)
6327 (tramp-message-for-buffer
6328 multi-method method user host 9
6329 "Checking local decoding command `%s' for sanity" loc-dec)
6330 (unless (zerop (tramp-call-local-coding-command
6332 (throw 'wont-work nil)))
6333 ;; CCC: At this point, maybe we should check that the output
6334 ;; of the commands is correct. But for the moment we will
6335 ;; assume that commands working on empty input will also
6336 ;; work in practice.
6337 (setq found item))))
6338 ;; Did we find something? If not, issue error. If so,
6339 ;; set connection properties.
6341 (error "Couldn't find an inline transfer encoding"))
6342 (let ((rem-enc (nth 0 found))
6343 (rem-dec (nth 1 found))
6344 (loc-enc (nth 2 found))
6345 (loc-dec (nth 3 found)))
6346 (tramp-message 10 "Using remote encoding %s" rem-enc)
6347 (tramp-set-remote-encoding multi-method method user host rem-enc)
6348 (tramp-message 10 "Using remote decoding %s" rem-dec)
6349 (tramp-set-remote-decoding multi-method method user host rem-dec)
6350 (tramp-message 10 "Using local encoding %s" loc-enc)
6351 (tramp-set-local-encoding multi-method method user host loc-enc)
6352 (tramp-message 10 "Using local decoding %s" loc-dec)
6353 (tramp-set-local-decoding multi-method method user host loc-dec))))
6355 (defun tramp-call-local-coding-command (cmd input output)
6356 "Call the local encoding or decoding command.
6357 If CMD contains \"%s\", provide input file INPUT there in command.
6358 Otherwise, INPUT is passed via standard input.
6359 INPUT can also be nil which means `/dev/null'.
6360 OUTPUT can be a string (which specifies a filename), or t (which
6361 means standard output and thus the current buffer), or nil (which
6364 tramp-encoding-shell ;program
6365 (when (and input (not (string-match "%s" cmd)))
6367 (if (eq output t) t nil) ;output
6369 tramp-encoding-command-switch
6370 ;; actual shell command
6372 (if (string-match "%s" cmd) (format cmd input) cmd)
6373 (if (stringp output) (concat "> " output) ""))))
6375 (defun tramp-maybe-open-connection (multi-method method user host)
6376 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6377 Does not do anything if a connection is already open, but re-opens the
6378 connection if a previous connection has died for some reason."
6379 (let ((p (get-buffer-process
6380 (tramp-get-buffer multi-method method user host)))
6382 ;; If too much time has passed since last command was sent, look
6383 ;; whether process is still alive. If it isn't, kill it. When
6384 ;; using ssh, it can sometimes happen that the remote end has hung
6385 ;; up but the local ssh client doesn't recognize this until it
6386 ;; tries to send some data to the remote end. So that's why we
6387 ;; try to send a command from time to time, then look again
6388 ;; whether the process is really alive.
6390 (set-buffer (tramp-get-buffer multi-method method user host))
6391 (when (and tramp-last-cmd-time
6392 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6393 p (processp p) (memq (process-status p) '(run open)))
6395 multi-method method user host "echo are you awake" nil t)
6396 (unless (tramp-wait-for-output 10)
6400 (unless (and p (processp p) (memq (process-status p) '(run open)))
6401 (when (and p (processp p))
6403 (let ((process-connection-type tramp-process-connection-type))
6404 (funcall (tramp-get-method-parameter
6406 (tramp-find-method multi-method method user host)
6407 user host 'tramp-connection-function)
6408 multi-method method user host)))))
6410 (defun tramp-send-command
6411 (multi-method method user host command &optional noerase neveropen)
6412 "Send the COMMAND to USER at HOST (logged in using METHOD).
6413 Erases temporary buffer before sending the command (unless NOERASE
6415 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6416 connection. This is meant to be used from
6417 `tramp-maybe-open-connection' only."
6419 (tramp-maybe-open-connection multi-method method user host))
6420 (setq tramp-last-cmd-time (current-time))
6421 (setq tramp-last-cmd command)
6422 (when tramp-debug-buffer
6424 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6425 (goto-char (point-max))
6426 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6428 (set-buffer (tramp-get-buffer multi-method method user host))
6429 (unless noerase (erase-buffer))
6430 (setq proc (get-buffer-process (current-buffer)))
6431 (process-send-string proc
6432 (concat command tramp-rsh-end-of-line))))
6434 (defun tramp-send-command-internal
6435 (multi-method method user host command &optional msg)
6436 "Send command to remote host and wait for success.
6437 Sends COMMAND, then waits 30 seconds for shell prompt."
6438 (tramp-send-command multi-method method user host command t t)
6440 (tramp-message 9 "Waiting 30s for %s..." msg))
6441 (tramp-barf-if-no-shell-prompt
6443 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6445 (defun tramp-wait-for-output (&optional timeout)
6446 "Wait for output from remote rsh command."
6447 (let ((proc (get-buffer-process (current-buffer)))
6449 (start-time (current-time))
6450 (start-point (point))
6451 (end-of-output (concat "^"
6452 (regexp-quote tramp-end-of-output)
6454 ;; Algorithm: get waiting output. See if last line contains
6455 ;; end-of-output sentinel. If not, wait a bit and again get
6456 ;; waiting output. Repeat until timeout expires or end-of-output
6457 ;; sentinel is seen. Will hang if timeout is nil and
6458 ;; end-of-output sentinel never appears.
6461 ;; Work around an XEmacs bug, where the timeout expires
6462 ;; faster than it should. This degenerates into polling
6463 ;; for buggy XEmacsen, but oh, well.
6464 (while (and (not found)
6465 (< (tramp-time-diff (current-time) start-time)
6467 (with-timeout (timeout)
6469 (tramp-accept-process-output proc 1)
6470 (unless (memq (process-status proc) '(run open))
6471 (error "Process has died"))
6472 (goto-char (point-max))
6474 (setq found (looking-at end-of-output))))))
6477 (tramp-accept-process-output proc 1)
6478 (unless (memq (process-status proc) '(run open))
6479 (error "Process has died"))
6480 (goto-char (point-max))
6482 (setq found (looking-at end-of-output))))))
6483 ;; At this point, either the timeout has expired or we have found
6484 ;; the end-of-output sentinel.
6486 (goto-char (point-max))
6488 (delete-region (point) (point-max)))
6489 ;; If processing echoes, look for it in the first line and delete.
6490 (when tramp-process-echoes
6492 (goto-char start-point)
6493 (when (looking-at (regexp-quote tramp-last-cmd))
6494 (delete-region (point) (progn (forward-line 1) (point))))))
6495 ;; Add output to debug buffer if appropriate.
6496 (when tramp-debug-buffer
6498 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6499 tramp-current-user tramp-current-host)
6500 (point-min) (point-max))
6504 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6505 tramp-current-user tramp-current-host))
6506 (goto-char (point-max))
6507 (insert "[[Remote prompt `" end-of-output "' not found"
6508 (if timeout (format " in %d secs" timeout) "")
6510 (goto-char (point-min))
6511 ;; Return value is whether end-of-output sentinel was found.
6514 (defun tramp-send-command-and-check (multi-method method user host command
6516 "Run COMMAND and check its exit status.
6517 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6518 Sends `echo $?' along with the COMMAND for checking the exit status. If
6519 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6521 If the optional argument SUBSHELL is non-nil, the command is executed in
6522 a subshell, ie surrounded by parentheses."
6523 (tramp-send-command multi-method method user host
6524 (concat (if subshell "( " "")
6526 (if command " 2>/dev/null; " "")
6527 "echo tramp_exit_status $?"
6528 (if subshell " )" " ")))
6529 (tramp-wait-for-output)
6530 (goto-char (point-max))
6531 (unless (search-backward "tramp_exit_status " nil t)
6532 (error "Couldn't find exit status of `%s'" command))
6533 (skip-chars-forward "^ ")
6534 (read (current-buffer)))
6536 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6537 signal fmt &rest args)
6538 "Run COMMAND, check exit status, throw error if exit status not okay.
6539 Similar to `tramp-send-command-and-check' but accepts two more arguments
6540 FMT and ARGS which are passed to `error'."
6541 (unless (zerop (tramp-send-command-and-check
6542 multi-method method user host command subshell))
6543 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6545 (pop-to-buffer (current-buffer))
6546 (funcall 'signal signal (apply 'format fmt args))))
6548 ;; It seems that Tru64 Unix does not like it if long strings are sent
6549 ;; to it in one go. (This happens when sending the Perl
6550 ;; `file-attributes' implementation, for instance.) Therefore, we
6551 ;; have this function which waits a bit at each line.
6552 (defun tramp-send-string
6553 (multi-method method user host string)
6554 "Send the STRING to USER at HOST using METHOD.
6556 The STRING is expected to use Unix line-endings, but the lines sent to
6557 the remote host use line-endings as defined in the variable
6558 `tramp-rsh-end-of-line'."
6559 (let ((proc (get-buffer-process
6560 (tramp-get-buffer multi-method method user host))))
6562 (error "Can't send string to remote host -- not logged in"))
6564 (when tramp-debug-buffer
6566 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6567 (goto-char (point-max))
6568 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6569 ;; replace "\n" by `tramp-rsh-end-of-line'
6571 (mapconcat 'identity
6572 (split-string string "\n")
6573 tramp-rsh-end-of-line))
6574 (unless (or (string= string "")
6575 (string-equal (substring string -1) tramp-rsh-end-of-line))
6576 (setq string (concat string tramp-rsh-end-of-line)))
6578 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6580 (end (length string)))
6582 (tramp-message-for-buffer
6583 multi-method method user host 10
6584 "Sending chunk from %s to %s"
6585 pos (min (+ pos tramp-chunksize) end))
6586 (process-send-string
6587 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6588 (setq pos (+ pos tramp-chunksize))
6590 (process-send-string proc string))))
6592 (defun tramp-send-eof (multi-method method user host)
6593 "Send EOF to the remote end.
6594 METHOD, HOST and USER specify the connection."
6595 (let ((proc (get-buffer-process
6596 (tramp-get-buffer multi-method method user host))))
6598 (error "Can't send EOF to remote host -- not logged in"))
6599 (process-send-eof proc)))
6600 ; (process-send-string proc "\^D")))
6602 (defun tramp-kill-process (multi-method method user host)
6603 "Kill the connection process used by Tramp.
6604 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6605 (let ((proc (get-buffer-process
6606 (tramp-get-buffer multi-method method user host))))
6607 (kill-process proc)))
6609 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6610 "Erase buffer, then discard subsequent garbage.
6611 If `tramp-discard-garbage' is nil, just erase buffer."
6612 (if (not tramp-discard-garbage)
6614 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6615 (when tramp-debug-buffer
6617 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6618 (goto-char (point-max))
6619 (tramp-insert-with-face
6620 'bold (format "Additional characters detected\n")))))))
6622 (defun tramp-mode-string-to-int (mode-string)
6623 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6624 (let* ((mode-chars (string-to-vector mode-string))
6625 (owner-read (aref mode-chars 1))
6626 (owner-write (aref mode-chars 2))
6627 (owner-execute-or-setid (aref mode-chars 3))
6628 (group-read (aref mode-chars 4))
6629 (group-write (aref mode-chars 5))
6630 (group-execute-or-setid (aref mode-chars 6))
6631 (other-read (aref mode-chars 7))
6632 (other-write (aref mode-chars 8))
6633 (other-execute-or-sticky (aref mode-chars 9)))
6637 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6638 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6640 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6641 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6642 (case owner-execute-or-setid
6643 (?x (tramp-octal-to-decimal "00100"))
6644 (?S (tramp-octal-to-decimal "04000"))
6645 (?s (tramp-octal-to-decimal "04100"))
6647 (t (error "Fourth char `%c' must be one of `xsS-'"
6648 owner-execute-or-setid)))
6650 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6651 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6653 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6654 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6655 (case group-execute-or-setid
6656 (?x (tramp-octal-to-decimal "00010"))
6657 (?S (tramp-octal-to-decimal "02000"))
6658 (?s (tramp-octal-to-decimal "02010"))
6660 (t (error "Seventh char `%c' must be one of `xsS-'"
6661 group-execute-or-setid)))
6663 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6664 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6666 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6667 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6668 (case other-execute-or-sticky
6669 (?x (tramp-octal-to-decimal "00001"))
6670 (?T (tramp-octal-to-decimal "01000"))
6671 (?t (tramp-octal-to-decimal "01001"))
6673 (t (error "Tenth char `%c' must be one of `xtT-'"
6674 other-execute-or-sticky)))))))
6676 (defun tramp-convert-file-attributes (multi-method method user host attr)
6677 "Convert file-attributes ATTR generated by perl script or ls.
6678 Convert file mode bits to string and set virtual device number.
6680 (unless (stringp (nth 8 attr))
6681 ;; Convert file mode bits to string.
6682 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6683 ;; Set virtual device number.
6684 (setcar (nthcdr 11 attr)
6685 (tramp-get-device multi-method method user host))
6688 (defun tramp-get-device (multi-method method user host)
6689 "Returns the virtual device number.
6690 If it doesn't exist, generate a new one."
6691 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6692 (unless (assoc string tramp-devices)
6693 (add-to-list 'tramp-devices
6694 (list string (length tramp-devices))))
6695 (list -1 (nth 1 (assoc string tramp-devices)))))
6697 (defun tramp-file-mode-from-int (mode)
6698 "Turn an integer representing a file mode into an ls(1)-like string."
6699 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6700 (user (logand (lsh mode -6) 7))
6701 (group (logand (lsh mode -3) 7))
6702 (other (logand (lsh mode -0) 7))
6703 (suid (> (logand (lsh mode -9) 4) 0))
6704 (sgid (> (logand (lsh mode -9) 2) 0))
6705 (sticky (> (logand (lsh mode -9) 1) 0)))
6706 (setq user (tramp-file-mode-permissions user suid "s"))
6707 (setq group (tramp-file-mode-permissions group sgid "s"))
6708 (setq other (tramp-file-mode-permissions other sticky "t"))
6709 (concat type user group other)))
6712 (defun tramp-file-mode-permissions (perm suid suid-text)
6713 "Convert a permission bitset into a string.
6714 This is used internally by `tramp-file-mode-from-int'."
6715 (let ((r (> (logand perm 4) 0))
6716 (w (> (logand perm 2) 0))
6717 (x (> (logand perm 1) 0)))
6718 (concat (or (and r "r") "-")
6719 (or (and w "w") "-")
6720 (or (and suid x suid-text) ; suid, execute
6721 (and suid (upcase suid-text)) ; suid, !execute
6722 (and x "x") "-")))) ; !suid
6725 (defun tramp-decimal-to-octal (i)
6726 "Return a string consisting of the octal digits of I.
6727 Not actually used. Use `(format \"%o\" i)' instead?"
6728 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6729 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6731 (t (concat (tramp-decimal-to-octal (/ i 8))
6732 (number-to-string (% i 8))))))
6735 ;;(defun tramp-octal-to-decimal (ostr)
6736 ;; "Given a string of octal digits, return a decimal number."
6737 ;; (cond ((null ostr) 0)
6738 ;; ((string= "" ostr) 0)
6739 ;; (t (let ((last (aref ostr (1- (length ostr))))
6740 ;; (rest (substring ostr 0 (1- (length ostr)))))
6741 ;; (unless (and (>= last ?0)
6743 ;; (error "Not an octal digit: %c" last))
6744 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6745 ;; Kudos to Gerd Moellmann for this suggestion.
6746 (defun tramp-octal-to-decimal (ostr)
6747 "Given a string of octal digits, return a decimal number."
6748 (let ((x (or ostr "")))
6749 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6750 (unless (string-match "\\`[0-7]*\\'" x)
6751 (error "Non-octal junk in string `%s'" x))
6752 (string-to-number ostr 8)))
6754 (defun tramp-shell-case-fold (string)
6755 "Converts STRING to shell glob pattern which ignores case."
6758 (if (equal (downcase c) (upcase c))
6760 (format "[%c%c]" (downcase c) (upcase c))))
6765 ;; ------------------------------------------------------------
6766 ;; -- TRAMP file names --
6767 ;; ------------------------------------------------------------
6768 ;; Conversion functions between external representation and
6769 ;; internal data structure. Convenience functions for internal
6772 (defstruct tramp-file-name multi-method method user host localname)
6774 (defun tramp-tramp-file-p (name)
6775 "Return t iff NAME is a tramp file."
6777 (string-match tramp-file-name-regexp name)))
6779 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6780 ;; to the `user' part of the structure if a user name was not
6781 ;; provided, now it assigns nil.
6782 (defun tramp-dissect-file-name (name)
6783 "Return an `tramp-file-name' structure.
6784 The structure consists of remote method, remote user, remote host and
6785 localname (file name on remote host)."
6787 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6790 (if match (match-string (nth 1 tramp-file-name-structure) name)
6793 (format (nth 0 tramp-multi-file-name-structure)
6794 (nth 0 tramp-multi-file-name-hop-structure)) name)
6795 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6796 (if (and method (member method tramp-multi-methods))
6797 ;; If it's a multi method, the file name structure contains
6798 ;; arrays of method, user and host.
6799 (tramp-dissect-multi-file-name name)
6800 ;; Normal method. First, find out default method.
6801 (unless match (error "Not a tramp file name: %s" name))
6802 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6803 (host (match-string (nth 3 tramp-file-name-structure) name))
6804 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6805 (make-tramp-file-name
6810 :localname localname))))))
6812 (defun tramp-find-default-method (user host)
6813 "Look up the right method to use in `tramp-default-method-alist'."
6814 (let ((choices tramp-default-method-alist)
6815 (method tramp-default-method)
6818 (setq item (pop choices))
6819 (when (and (string-match (nth 0 item) (or host ""))
6820 (string-match (nth 1 item) (or user "")))
6821 (setq method (nth 2 item))
6822 (setq choices nil)))
6825 (defun tramp-find-method (multi-method method user host)
6826 "Return the right method string to use.
6827 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6828 If both MULTI-METHOD and METHOD are nil, do a lookup in
6829 `tramp-default-method-alist'."
6830 (or multi-method method (tramp-find-default-method user host)))
6832 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6833 ;; a user name is not provided in the "file name" very well.
6834 (defun tramp-dissect-multi-file-name (name)
6835 "Not implemented yet."
6836 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6837 (method-index (nth 1 tramp-multi-file-name-structure))
6838 (hops-index (nth 2 tramp-multi-file-name-structure))
6839 (localname-index (nth 3 tramp-multi-file-name-structure))
6840 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6841 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6842 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6843 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6844 method hops len hop-methods hop-users hop-hosts localname)
6845 (unless (string-match (format regexp hop-regexp) name)
6846 (error "Not a multi tramp file name: %s" name))
6847 (setq method (match-string method-index name))
6848 (setq hops (match-string hops-index name))
6849 (setq len (/ (length (match-data t)) 2))
6850 (when (< localname-index 0) (incf localname-index len))
6851 (setq localname (match-string localname-index name))
6853 (while (string-match hop-regexp hops index)
6854 (setq index (match-end 0))
6856 (cons (match-string hop-method-index hops) hop-methods))
6858 (cons (match-string hop-user-index hops) hop-users))
6860 (cons (match-string hop-host-index hops) hop-hosts))))
6861 (make-tramp-file-name
6862 :multi-method method
6863 :method (apply 'vector (reverse hop-methods))
6864 :user (apply 'vector (reverse hop-users))
6865 :host (apply 'vector (reverse hop-hosts))
6866 :localname localname)))
6868 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6869 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6871 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6873 (concat tramp-prefix-format
6874 (when method (concat "%m" tramp-postfix-single-method-format))
6875 (when user (concat "%u" tramp-postfix-user-format))
6876 (when host (concat "%h" tramp-postfix-host-format))
6877 (when localname (concat "%p")))
6878 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6880 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6881 ;; with this when USER is nil?
6882 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6883 "Constructs a tramp file name for a multi-hop method."
6884 (unless tramp-make-multi-tramp-file-format
6885 (error "`tramp-make-multi-tramp-file-format' is nil"))
6886 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6887 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6888 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6889 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6891 (localname (format-spec localname-format `((?p . ,localname))))
6893 (len (length method)))
6895 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6896 (setq hops (concat hops (format-spec hop-format
6897 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6899 (concat prefix hops localname)))
6901 (defun tramp-make-copy-program-file-name (user host localname)
6902 "Create a file name suitable to be passed to `rcp' and workalikes."
6904 (format "%s@%s:%s" user host localname)
6905 (format "%s:%s" host localname)))
6907 (defun tramp-method-out-of-band-p (multi-method method user host)
6908 "Return t if this is an out-of-band method, nil otherwise."
6909 (tramp-get-method-parameter
6911 (tramp-find-method multi-method method user host)
6912 user host 'tramp-copy-program))
6914 ;; Variables local to connection.
6916 (defun tramp-get-ls-command (multi-method method user host)
6918 (tramp-maybe-open-connection multi-method method user host)
6919 (set-buffer (tramp-get-buffer multi-method method user host))
6922 (defun tramp-get-test-groks-nt (multi-method method user host)
6924 (tramp-maybe-open-connection multi-method method user host)
6925 (set-buffer (tramp-get-buffer multi-method method user host))
6926 tramp-test-groks-nt))
6928 (defun tramp-get-file-exists-command (multi-method method user host)
6930 (tramp-maybe-open-connection multi-method method user host)
6931 (set-buffer (tramp-get-buffer multi-method method user host))
6932 tramp-file-exists-command))
6934 (defun tramp-get-remote-perl (multi-method method user host)
6935 (tramp-get-connection-property "perl" nil multi-method method user host))
6937 (defun tramp-get-remote-ln (multi-method method user host)
6938 (tramp-get-connection-property "ln" nil multi-method method user host))
6940 ;; Get a property of a TRAMP connection.
6941 (defun tramp-get-connection-property
6942 (property default multi-method method user host)
6943 "Get the named property for the connection.
6944 If the value is not set for the connection, return `default'"
6945 (tramp-maybe-open-connection multi-method method user host)
6946 (with-current-buffer (tramp-get-buffer multi-method method user host)
6949 (symbol-value (intern (concat "tramp-connection-property-" property)))
6952 ;; Set a property of a TRAMP connection.
6953 (defun tramp-set-connection-property
6954 (property value multi-method method user host)
6955 "Set the named property of a TRAMP connection."
6956 (tramp-maybe-open-connection multi-method method user host)
6957 (with-current-buffer (tramp-get-buffer multi-method method user host)
6958 (set (make-local-variable
6959 (intern (concat "tramp-connection-property-" property)))
6962 ;; Some predefined connection properties.
6963 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6964 (tramp-set-connection-property "remote-encoding" rem-enc
6965 multi-method method user host))
6966 (defun tramp-get-remote-encoding (multi-method method user host)
6967 (tramp-get-connection-property "remote-encoding" nil
6968 multi-method method user host))
6970 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6971 (tramp-set-connection-property "remote-decoding" rem-dec
6972 multi-method method user host))
6973 (defun tramp-get-remote-decoding (multi-method method user host)
6974 (tramp-get-connection-property "remote-decoding" nil
6975 multi-method method user host))
6977 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6978 (tramp-set-connection-property "local-encoding" loc-enc
6979 multi-method method user host))
6980 (defun tramp-get-local-encoding (multi-method method user host)
6981 (tramp-get-connection-property "local-encoding" nil
6982 multi-method method user host))
6984 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6985 (tramp-set-connection-property "local-decoding" loc-dec
6986 multi-method method user host))
6987 (defun tramp-get-local-decoding (multi-method method user host)
6988 (tramp-get-connection-property "local-decoding" nil
6989 multi-method method user host))
6991 (defun tramp-get-method-parameter (multi-method method user host param)
6992 "Return the method parameter PARAM.
6993 If the `tramp-methods' entry does not exist, use the variable PARAM
6995 (unless (boundp param)
6996 (error "Non-existing method parameter `%s'" param))
6997 (let ((entry (assoc param
6998 (assoc (tramp-find-method multi-method method user host)
7002 (symbol-value param))))
7005 ;; Auto saving to a special directory.
7007 (defun tramp-exists-file-name-handler (operation &rest args)
7008 (let ((buffer-file-name "/")
7009 (fnha file-name-handler-alist)
7010 (check-file-name-operation operation)
7011 (file-name-handler-alist
7014 '(lambda (operation &rest args)
7015 "Returns OPERATION if it is the one to be checked"
7016 (if (equal check-file-name-operation operation)
7018 (let ((file-name-handler-alist fnha))
7019 (apply operation args))))))))
7020 (eq (apply operation args) operation)))
7022 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7023 (defadvice make-auto-save-file-name
7024 (around tramp-advice-make-auto-save-file-name () activate)
7025 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7026 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7027 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7030 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7031 ;; permission 0666 minus umask. This is a security threat.
7033 (defun tramp-set-auto-save-file-modes ()
7034 "Set permissions of autosaved remote files to the original permissions."
7035 (let ((bfn (buffer-file-name)))
7036 (when (and (stringp bfn)
7037 (tramp-tramp-file-p bfn)
7038 (stringp buffer-auto-save-file-name)
7039 (not (equal bfn buffer-auto-save-file-name)))
7040 (unless (file-exists-p buffer-auto-save-file-name)
7041 (write-region "" nil buffer-auto-save-file-name))
7042 ;; Permissions should be set always, because there might be an old
7043 ;; auto-saved file belonging to another original file. This could
7044 ;; be a security threat.
7045 (set-file-modes buffer-auto-save-file-name
7046 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7048 (unless (or (> emacs-major-version 21)
7049 (and (featurep 'xemacs)
7050 (= emacs-major-version 21)
7051 (> emacs-minor-version 4)))
7052 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))
7054 (defun tramp-subst-strs-in-string (alist string)
7055 "Replace all occurrences of the string FROM with TO in STRING.
7056 ALIST is of the form ((FROM . TO) ...)."
7059 (let* ((pr (car alist))
7062 (while (string-match (regexp-quote from) string)
7063 (setq string (replace-match to t t string)))
7064 (setq alist (cdr alist))))
7067 (defun tramp-insert-with-face (face string)
7068 "Insert text with a specific face."
7069 (let ((start (point)))
7071 (add-text-properties start (point) (list 'face face))))
7073 ;; ------------------------------------------------------------
7074 ;; -- Compatibility functions section --
7075 ;; ------------------------------------------------------------
7077 (defun tramp-temporary-file-directory ()
7078 "Return name of directory for temporary files (compat function).
7079 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7080 this is the function `temp-directory'."
7081 (cond ((boundp 'temporary-file-directory)
7082 (symbol-value 'temporary-file-directory))
7083 ((fboundp 'temp-directory)
7084 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7085 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7086 (file-name-as-directory (getenv "TEMP")))
7087 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7088 (file-name-as-directory (getenv "TMP")))
7089 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7090 (file-name-as-directory (getenv "TMPDIR")))
7091 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7092 (t (message (concat "Neither `temporary-file-directory' nor "
7093 "`temp-directory' is defined -- using /tmp."))
7094 (file-name-as-directory "/tmp"))))
7096 (defun tramp-read-passwd (user host prompt)
7097 "Read a password from user (compat function).
7098 Invokes `password-read' if available, `read-passwd' else."
7099 (if (functionp 'password-read)
7100 (let* ((key (concat (or user (user-login-name)) "@" host))
7101 (password (apply #'password-read (list prompt key))))
7102 (apply #'password-cache-add (list key password))
7104 (read-passwd prompt)))
7106 (defun tramp-clear-passwd (&optional user host)
7107 "Clear password cache for connection related to current-buffer."
7109 (let ((filename (or buffer-file-name list-buffers-directory "")))
7110 (when (and (functionp 'password-cache-remove)
7111 (or (and user host) (tramp-tramp-file-p filename)))
7112 (let* ((v (when (tramp-tramp-file-p filename)
7113 (tramp-dissect-file-name filename)))
7114 (luser (or user (tramp-file-name-user v) (user-login-name)))
7115 (lhost (or host (tramp-file-name-host v) (system-name)))
7116 (key (concat luser "@" lhost)))
7117 (apply #'password-cache-remove (list key))))))
7119 (defun tramp-time-diff (t1 t2)
7120 "Return the difference between the two times, in seconds.
7121 T1 and T2 are time values (as returned by `current-time' for example).
7123 NOTE: This function will fail if the time difference is too large to
7125 ;; Pacify byte-compiler with `symbol-function'.
7126 (cond ((and (fboundp 'subtract-time)
7127 (fboundp 'float-time))
7128 (funcall (symbol-function 'float-time)
7129 (funcall (symbol-function 'subtract-time) t1 t2)))
7130 ((and (fboundp 'subtract-time)
7131 (fboundp 'time-to-seconds))
7132 (funcall (symbol-function 'time-to-seconds)
7133 (funcall (symbol-function 'subtract-time) t1 t2)))
7134 ((fboundp 'itimer-time-difference)
7136 (symbol-function 'itimer-time-difference)
7137 (if (< (length t1) 3) (append t1 '(0)) t1)
7138 (if (< (length t2) 3) (append t2 '(0)) t2))))
7140 ;; snarfed from Emacs 21 time-date.el; combining
7141 ;; time-to-seconds and subtract-time
7142 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7143 (list (- (car t1) (car t2) (if borrow 1 0))
7144 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7145 (+ (* (car time) 65536.0)
7147 (/ (or (nth 2 time) 0) 1000000.0))))))
7149 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7150 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7151 EOL-TYPE can be one of `dos', `unix', or `mac'."
7152 (cond ((fboundp 'coding-system-change-eol-conversion)
7153 (apply #'coding-system-change-eol-conversion
7154 (list coding-system eol-type)))
7155 ((fboundp 'subsidiary-coding-system)
7157 #'subsidiary-coding-system
7159 (cond ((eq eol-type 'dos) 'crlf)
7160 ((eq eol-type 'unix) 'lf)
7161 ((eq eol-type 'mac) 'cr)
7163 (error "Unknown EOL-TYPE `%s', must be %s"
7165 "`dos', `unix', or `mac'"))))))
7166 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7168 (defun tramp-split-string (string pattern)
7169 "Like `split-string' but omit empty strings.
7170 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7171 This is, the first, empty, element is omitted. In XEmacs, the first
7172 element is not omitted.
7174 Note: this function has been written for `tramp-handle-file-truename'.
7175 If you want to use it for something else, you'll have to check whether
7176 it does the right thing."
7177 (delete "" (split-string string pattern)))
7179 (defun tramp-set-process-query-on-exit-flag (process flag)
7180 "Specify if query is needed for process when Emacs is exited.
7181 If the second argument flag is non-nil, Emacs will query the user before
7182 exiting if process is running."
7184 (if (fboundp 'set-process-query-on-exit-flag)
7185 (symbol-function 'set-process-query-on-exit-flag)
7186 (symbol-function 'process-kill-without-query))
7190 ;; ------------------------------------------------------------
7191 ;; -- Kludges section --
7192 ;; ------------------------------------------------------------
7194 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7195 ;; does not deal well with newline characters. Newline is replaced by
7196 ;; backslash newline. But if, say, the string `a backslash newline b'
7197 ;; is passed to a shell, the shell will expand this into "ab",
7198 ;; completely omitting the newline. This is not what was intended.
7199 ;; It does not appear to be possible to make the function
7200 ;; `shell-quote-argument' work with newlines without making it
7201 ;; dependent on the shell used. But within this package, we know that
7202 ;; we will always use a Bourne-like shell, so we use an approach which
7205 ;; The approach is simple: we call `shell-quote-argument', then
7206 ;; massage the newline part of the result.
7208 ;; This function should produce a string which is grokked by a Unix
7209 ;; shell, even if the Emacs is running on Windows. Since this is the
7210 ;; kludges section, we bind `system-type' in such a way that
7211 ;; `shell-quote-arguments' behaves as if on Unix.
7213 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7214 ;; function to work with Bourne-like shells.
7216 ;; CCC: This function should be rewritten so that
7217 ;; `shell-quote-argument' is not used. This way, we are safe from
7218 ;; changes in `shell-quote-argument'.
7219 (defun tramp-shell-quote-argument (s)
7220 "Similar to `shell-quote-argument', but groks newlines.
7221 Only works for Bourne-like shells."
7222 (let ((system-type 'not-windows))
7224 (let ((result (shell-quote-argument s))
7225 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7226 (when (and (>= (length result) 2)
7227 (string= (substring result 0 2) "\\~"))
7228 (setq result (substring result 1)))
7229 (while (string-match nl result)
7230 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7234 ;; ;; EFS hooks itself into the file name handling stuff in more places
7235 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7236 ;; ;; away from tramp.el file names.
7238 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7239 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7240 ;; ;; connection to help it's dired process. Not that I have any real
7241 ;; ;; idea *why* this is helpful to dired.
7243 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7245 ;; ;; Daniel Pittman <daniel@danann.net>
7247 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7248 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7249 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7250 ;; ;; EFS might nicht be loaded yet.
7251 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7252 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7253 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7257 ;; We currently (sometimes) use "[" and "]" in the filename format.
7258 ;; This means that Emacs wants to expand wildcards if
7259 ;; `find-file-wildcards' is non-nil, and then barfs because no
7260 ;; expansion could be found. We detect this situation and do
7261 ;; something really awful: we have `file-expand-wildcards' return the
7262 ;; original filename if it can't expand anything. Let's just hope
7263 ;; that this doesn't break anything else.
7264 ;; CCC: This check is now also really awful; we should search all
7265 ;; of the filename format, not just the prefix.
7266 (when (string-match "\\[" tramp-prefix-format)
7267 (defadvice file-expand-wildcards (around tramp-fix activate)
7268 (let ((name (ad-get-arg 0)))
7269 (if (tramp-tramp-file-p name)
7270 ;; If it's a Tramp file, dissect it and look if wildcards
7271 ;; need to be expanded at all.
7272 (let ((v (tramp-dissect-file-name name)))
7273 (if (string-match "[[*?]" (tramp-file-name-localname v))
7274 (let ((res ad-do-it))
7275 (setq ad-return-value (or res (list name))))
7276 (setq ad-return-value (list name))))
7277 ;; If it is not a Tramp file, just run the original function.
7278 (let ((res ad-do-it))
7279 (setq ad-return-value (or res (list name))))))))
7281 ;; Tramp version is useful in a number of situations.
7283 (defun tramp-version (arg)
7284 "Print version number of tramp.el in minibuffer or current buffer."
7286 (if arg (insert tramp-version) (message tramp-version)))
7288 ;; Make the `reporter` functionality available for making bug reports about
7289 ;; the package. A most useful piece of code.
7291 (unless (fboundp 'reporter-submit-bug-report)
7292 (autoload 'reporter-submit-bug-report "reporter"))
7295 "Submit a bug report to the TRAMP developers."
7299 (let ((reporter-prompt-for-summary-p t))
7300 (reporter-submit-bug-report
7301 tramp-bug-report-address ; to-address
7302 (format "tramp (%s)" tramp-version) ; package name and version
7307 tramp-file-exists-command
7308 tramp-current-multi-method
7309 tramp-current-method
7314 tramp-auto-save-directory ; vars to dump
7315 tramp-default-method
7316 tramp-rsh-end-of-line
7317 tramp-default-password-end-of-line
7319 tramp-login-prompt-regexp
7320 ;; Mask non-7bit characters
7321 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7322 tramp-wrong-passwd-regexp
7323 tramp-yesno-prompt-regexp
7324 tramp-yn-prompt-regexp
7325 tramp-terminal-prompt-regexp
7326 tramp-temp-name-prefix
7327 tramp-file-name-structure
7328 tramp-file-name-regexp
7329 tramp-multi-file-name-structure
7330 tramp-multi-file-name-hop-structure
7332 tramp-multi-connection-function-alist
7335 tramp-coding-commands
7336 tramp-actions-before-shell
7337 tramp-actions-copy-out-of-band
7340 ;; Mask non-7bit characters
7341 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7343 ,(when (boundp 'tramp-backup-directory-alist)
7344 'tramp-backup-directory-alist)
7345 ,(when (boundp 'tramp-bkup-backup-directory-info)
7346 'tramp-bkup-backup-directory-info)
7348 ;; Non-tramp variables of interest
7349 ;; Mask non-7bit characters
7350 (shell-prompt-pattern . tramp-reporter-dump-variable)
7352 backup-by-copying-when-linked
7353 backup-by-copying-when-mismatch
7354 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7355 'backup-by-copying-when-privileged-mismatch)
7356 ,(when (boundp 'password-cache)
7358 ,(when (boundp 'password-cache-expiry)
7359 'password-cache-expiry)
7360 ,(when (boundp 'backup-directory-alist)
7361 'backup-directory-alist)
7362 ,(when (boundp 'bkup-backup-directory-info)
7363 'bkup-backup-directory-info)
7364 file-name-handler-alist))
7366 'tramp-load-report-modules ; pre-hook
7367 'tramp-append-tramp-buffers ; post-hook
7369 Enter your bug report in this message, including as much detail as you
7370 possibly can about the problem, what you did to cause it and what the
7371 local and remote machines are.
7373 If you can give a simple set of instructions to make this bug happen
7374 reliably, please include those. Thank you for helping kill bugs in
7377 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7378 the ~/.emacs file and to repeat the bug. Then, include the contents
7379 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7382 --bug report follows this line--
7385 (defun tramp-reporter-dump-variable (varsym mailbuf)
7386 "Pretty-print the value of the variable in symbol VARSYM.
7387 Used for non-7bit chars in strings."
7388 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7389 (val (with-current-buffer reporter-eval-buffer
7390 (symbol-value varsym))))
7392 ;; There are characters to be masked.
7393 (when (and (boundp 'mm-7bit-chars)
7395 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7396 (with-current-buffer reporter-eval-buffer
7397 (set varsym (concat "(base64-decode-string \""
7398 (base64-encode-string val)
7402 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7404 ;; Remove string quotation.
7407 (concat "\\(^.*\\)" "\"" ;; \1 "
7408 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7409 "\\(\".*\\)" "\\\\" ;; \3 \
7410 "\\(\")\\)" "\"$")) ;; \4 "
7411 (replace-match "\\1\\2\\3\\4")
7413 (insert " ;; variable encoded due to non-printable characters\n"))
7416 ;; Reset VARSYM to old value.
7417 (with-current-buffer reporter-eval-buffer
7420 (defun tramp-load-report-modules ()
7421 "Load needed modules for reporting."
7423 ;; We load message.el and mml.el from Gnus.
7424 (if (featurep 'xemacs)
7426 (load "message" 'noerror)
7427 (load "mml" 'noerror))
7428 (require 'message nil 'noerror)
7429 (require 'mml nil 'noerror))
7430 (when (functionp 'message-mode)
7431 (funcall (symbol-function 'message-mode)))
7432 (when (functionp 'mml-mode)
7433 (funcall (symbol-function 'mml-mode) t)))
7435 (defun tramp-append-tramp-buffers ()
7436 "Append Tramp buffers into the bug report."
7439 (eq major-mode 'message-mode)
7441 (symbol-value 'mml-mode))
7443 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7446 (mapcar '(lambda (b)
7447 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7449 (curbuf (current-buffer)))
7451 ;; There is at least one Tramp buffer.
7453 (switch-to-buffer (list-buffers-noselect nil))
7454 (delete-other-windows)
7455 (setq buffer-read-only nil)
7456 (goto-char (point-min))
7458 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7461 (let ((start (point)))
7463 (kill-region start (point)))))
7465 The buffer(s) above will be appended to this message. If you don't want
7466 to append a buffer because it contains sensible data, or because the buffer
7467 is too large, you should delete the respective buffer. The buffer(s) will
7468 contain user and host names. Passwords will never be included there.")
7470 (when (and tramp-debug-buffer (> tramp-verbose 9))
7472 (let ((start (point)))
7474 Please note that you have set `tramp-verbose' to a value greater than 9.
7475 Therefore, the contents of files might be included in the debug buffer(s).")
7476 (add-text-properties start (point) (list 'face 'italic))))
7478 (set-buffer-modified-p nil)
7479 (setq buffer-read-only t)
7480 (goto-char (point-min))
7482 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7483 ;; OK, let's send. First we delete the buffer list.
7486 (switch-to-buffer curbuf)
7487 (goto-char (point-max))
7489 (dolist (buffer buffer-list)
7490 (funcall (symbol-function 'mml-insert-empty-tag)
7491 'part 'type "text/plain" 'encoding "base64"
7492 'disposition "attachment" 'buffer (buffer-name buffer)
7493 'description (buffer-name buffer)))
7494 (set-buffer-modified-p nil))
7496 ;; Don't send. Delete the message buffer.
7498 (set-buffer-modified-p nil)
7500 (throw 'dont-send nil))))))
7502 (defalias 'tramp-submit-bug 'tramp-bug)
7506 ;; Make sure that we get integration with the VC package.
7507 ;; When it is loaded, we need to pull in the integration module.
7508 ;; This must come after (provide 'tramp) because tramp-vc.el
7510 (eval-after-load "vc"
7511 '(require 'tramp-vc))
7515 ;; * Allow putting passwords in the filename.
7516 ;; This should be implemented via a general mechanism to add
7517 ;; parameters in filenames. There is currently a kludge for
7518 ;; putting the port number into the filename for ssh and ftp
7519 ;; files. This could be subsumed by the new mechanism as well.
7520 ;; Another approach is to read a netrc file like ~/.authinfo
7522 ;; * Handle nonlocal exits such as C-g.
7523 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7524 ;; * Add fallback for inline encodings. This should be used
7525 ;; if the remote end doesn't support mimencode or a similar program.
7526 ;; For reading files from the remote host, we can just parse the output
7527 ;; of `od -b'. For writing files to the remote host, we construct
7528 ;; a shell program which contains only "safe" ascii characters
7529 ;; and which writes the right bytes to the file. We can use printf(1)
7530 ;; or "echo -e" or the printf function in awk and use octal escapes
7531 ;; for the "dangerous" characters. The null byte might be a problem.
7532 ;; On some systems, the octal escape doesn't work. So we try the following
7533 ;; two commands to write a null byte:
7534 ;; dd if=/dev/zero bs=1 count=1
7535 ;; echo | tr '\n' '\000'
7536 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7537 ;; the two via a format which can be `uu' or `b64'. Then we can search
7538 ;; for the right local commands and the right remote commands separately.
7539 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7540 ;; work for remote files.
7541 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7542 ;; `shell-quote-argument'.
7543 ;; * Completion gets confused when you leave out the method name.
7544 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7545 ;; by the files in that directory. Add this here.
7546 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7547 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7548 ;; * When logging in, keep looking for questions according to an alist
7549 ;; and then invoke the right function.
7550 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7551 ;; * Running CVS remotely doesn't appear to work right. It thinks
7552 ;; files are locked by somebody else even if I'm the locking user.
7553 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7555 ;; * Don't use globbing for directories with many files, as this is
7556 ;; likely to produce long command lines, and some shells choke on
7557 ;; long command lines.
7558 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7559 ;; do the right thing.
7560 ;; * `vc-directory' does not work. It never displays any files, even
7561 ;; if it does show files when run locally.
7562 ;; * Allow correction of passwords, if the remote end allows this.
7563 ;; (Mark Hershberger)
7564 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7565 ;; * Do asynchronous `shell-command's.
7566 ;; * Grok `append' parameter for `write-region'.
7567 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7568 ;; * abbreviate-file-name
7569 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7570 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7571 ;; what the user names should default to, though.
7572 ;; * better error checking. At least whenever we see something
7573 ;; strange when doing zerop, we should kill the process and start
7574 ;; again. (Greg Stark)
7575 ;; * Add caching for filename completion. (Greg Stark)
7576 ;; Of course, this has issues with usability (stale cache bites)
7577 ;; -- <daniel@danann.net>
7578 ;; * Provide a local cache of old versions of remote files for the rsync
7579 ;; transfer method to use. (Greg Stark)
7580 ;; * Remove unneeded parameters from methods.
7581 ;; * Invoke rsync once for copying a whole directory hierarchy.
7582 ;; (Francesco Potort
\e,Al
\e(B)
7583 ;; * Should we set PATH ourselves or should we rely on the remote end
7585 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7586 ;; * Make it work for different encodings, and for different file name
7587 ;; encodings, too. (Daniel Pittman)
7588 ;; * Change applicable functions to pass a struct tramp-file-name rather
7589 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7590 ;; * Implement asynchronous shell commands.
7591 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7592 ;; * Progress reports while copying files. (Michael Kifer)
7593 ;; * `Smart' connection method that uses inline for small and out of
7594 ;; band for large files. (Michael Kifer)
7595 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7596 ;; then look if it's the right version (with `perl -v').
7597 ;; * When editing a remote CVS controlled file as a different user, VC
7598 ;; gets confused about the file locking status. Try to find out why
7599 ;; the workaround doesn't work.
7600 ;; * Change `copy-file' to grok the case where the filename handler
7601 ;; for the source and the target file are different. Right now,
7602 ;; it looks at the source file and then calls that handler, if
7603 ;; there is one. But since ange-ftp, for instance, does not know
7604 ;; about Tramp, it does not do the right thing if the target file
7605 ;; name is a Tramp name.
7606 ;; * Username and hostname completion.
7607 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7608 ;; connect to host "blabla" already if that host is unique. No idea
7609 ;; how to suppress. Maybe not an essential problem.
7610 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7611 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7612 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7613 ;; Code is nearly identical.
7614 ;; ** Decide whiche files to take for searching user/host names depending on
7615 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7616 ;; ** Enhance variables for debug.
7617 ;; ** Implement "/multi:" completion.
7618 ;; ** Add a learning mode for completion. Make results persistent.
7619 ;; * Allow out-of-band methods as _last_ multi-hop.
7621 ;; Functions for file-name-handler-alist:
7622 ;; diff-latest-backup-file -- in diff.el
7623 ;; dired-uncache -- this will be needed when we do insert-directory caching
7624 ;; file-name-as-directory -- use primitive?
7625 ;; file-name-sans-versions -- use primitive?
7626 ;; get-file-buffer -- use primitive
7629 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7630 ;;; tramp.el ends here