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 Free Software Foundation, Inc.
6 ;; Author: kai.grossjohann@gmx.net
7 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package provides remote file editing, similar to ange-ftp.
29 ;; The difference is that ange-ftp uses FTP to transfer files between
30 ;; the local and the remote host, whereas tramp.el uses a combination
31 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
33 ;; For more detailed instructions, please see the info file.
38 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
39 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
40 ;; 19 is reported to have other problems. For XEmacs 21, you need the
41 ;; package `fsf-compat' for the `with-timeout' macro.)
43 ;; This version might not work with pre-Emacs 21 VC unless VC is
44 ;; loaded before tramp.el. Could you please test this and tell me about
45 ;; the result? Thanks.
47 ;; Also see the todo list at the bottom of this file.
49 ;; The current version of Tramp can be retrieved from the following URL:
50 ;; http://savannah.nongnu.org/download/tramp/
52 ;; There's a mailing list for this, as well. Its name is:
53 ;; tramp-devel@mail.freesoftware.fsf.org
54 ;; Send a mail with `help' in the subject (!) to the administration
55 ;; address for instructions on joining the list. The administration
57 ;; tramp-devel-request@mail.freesoftware.fsf.org
58 ;; You can also use the Web to subscribe, under the following URL:
59 ;; http://mail.freesoftware.fsf.org/mailman/listinfo/tramp-devel
61 ;; For the adventurous, the current development sources are available
62 ;; via CVS. You can find instructions about this at the following URL:
63 ;; http://savannah.gnu.org/projects/tramp/
64 ;; Click on "CVS" in the navigation bar near the top.
66 ;; Don't forget to put on your asbestos longjohns, first!
70 ;; The Tramp version number and bug report address, as prepared by configure.
74 (require 'format-spec) ;from Gnus 5.8, also in tar ball
75 ;; As long as password.el is not part of (X)Emacs, it shouldn't
77 (if (featurep 'xemacs)
78 (load "password" 'noerror)
79 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
81 ;; The explicit check is not necessary in Emacs, which provides the
82 ;; feature even if implemented in C, but it appears to be necessary
84 (unless (and (fboundp 'base64-encode-region)
85 (fboundp 'base64-decode-region))
86 (require 'base64)) ;for the mimencode methods
90 (autoload 'tramp-uuencode-region "tramp-uu"
91 "Implementation of `uuencode' in Lisp.")
93 (unless (fboundp 'uudecode-decode-region)
94 (autoload 'uudecode-decode-region "uudecode"))
96 ;; XEmacs is distributed with few Lisp packages. Further packages are
97 ;; installed using EFS. If we use a unified filename format, then
98 ;; Tramp is required in addition to EFS. (But why can't Tramp just
99 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
100 ;; just like before.) Another reason for using a separate filename
101 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
102 ;; Tramp only knows how to deal with `file-name-handler-alist', not
105 (defvar tramp-unified-filenames (not (featurep 'xemacs))
106 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
107 Nil means to use a separate filename syntax for Tramp.")
109 ;; Load foreign methods. Because they do require Tramp internally, this
110 ;; must be done with the `eval-after-load' trick.
112 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
113 (unless (featurep 'xemacs)
114 (eval-after-load "tramp"
115 '(require 'tramp-ftp)))
116 (when (and tramp-unified-filenames (featurep 'xemacs))
117 (eval-after-load "tramp"
118 '(require 'tramp-efs)))
120 ;; tramp-smb uses "smbclient" from Samba.
121 ;; Not available under Cygwin and Windows, because they don't offer
122 ;; "smbclient". And even not necessary there, because Emacs supports
123 ;; UNC file names like "//host/share/localname".
124 (unless (memq system-type '(cygwin windows-nt))
125 (eval-after-load "tramp"
126 '(require 'tramp-smb)))
131 ;; Emacs 19.34 compatibility hack -- is this needed?
132 (or (>= emacs-major-version 20)
135 (unless (boundp 'custom-print-functions)
136 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
138 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
139 ;; Currently, XEmacs supports this.
141 (when (fboundp 'byte-compiler-options)
142 (let (unused-vars) ; Pacify Emacs byte-compiler
143 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
144 (byte-compiler-options (warnings (- unused-vars))))))
146 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
147 ;; used in XEmacs, so we set it here and there. The following is needed
148 ;; to pacify Emacs byte-compiler.
150 (when (boundp 'byte-compile-not-obsolete-var)
151 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
153 ;; XEmacs byte-compiler raises warning abouts `last-coding-system-used'.
155 (unless (boundp 'last-coding-system-used)
156 (defvar last-coding-system-used nil)))
158 ;;; User Customizable Internal Variables:
161 "Edit remote files with a combination of rsh and rcp or similar programs."
165 (defcustom tramp-verbose 9
166 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
170 (defcustom tramp-debug-buffer nil
171 "*Whether to send all commands and responses to a debug buffer."
177 (when (boundp 'backup-directory-alist)
178 (defcustom tramp-backup-directory-alist nil
179 "Alist of filename patterns and backup directory names.
180 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
181 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
182 is a local file name, the backup directory is prepended with Tramp file
183 name prefix \(multi-method, method, user, host\) of file.
185 \(setq tramp-backup-directory-alist backup-directory-alist\)
187 gives the same backup policy for Tramp files on their hosts like the
188 policy for local files."
190 :type '(repeat (cons (regexp :tag "Regexp matching filename")
191 (directory :tag "Backup directory name"))))))
193 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
194 ;; the package "backup-dir" might not be loaded yet.
196 (when (featurep 'xemacs)
197 (defcustom tramp-bkup-backup-directory-info nil
198 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
199 It has the same meaning like `bkup-backup-directory-info' from package
200 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
201 file name, the backup directory is prepended with Tramp file name prefix
202 \(multi-method, method, user, host\) of file.
204 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
206 gives the same backup policy for Tramp files on their hosts like the
207 policy for local files."
209 (list (regexp :tag "File regexp")
210 (string :tag "Backup Dir")
215 (const search-upward))))
218 (defcustom tramp-auto-save-directory nil
219 "*Put auto-save files in this directory, if set.
220 The idea is to use a local directory so that auto-saving is faster."
222 :type '(choice (const nil)
225 (defcustom tramp-encoding-shell
226 (if (memq system-type '(windows-nt))
229 "*Use this program for encoding and decoding commands on the local host.
230 This shell is used to execute the encoding and decoding command on the
231 local host, so if you want to use `~' in those commands, you should
232 choose a shell here which groks tilde expansion. `/bin/sh' normally
233 does not understand tilde expansion.
235 For encoding and deocding, commands like the following are executed:
237 /bin/sh -c COMMAND < INPUT > OUTPUT
239 This variable can be used to change the \"/bin/sh\" part. See the
240 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
241 variable `tramp-encoding-reads-stdin' to specify whether the commands read
242 standard input or a file.
244 Note that this variable is not used for remote commands. There are
245 mechanisms in tramp.el which automatically determine the right shell to
246 use for the remote host."
248 :type '(file :must-match t))
250 (defcustom tramp-encoding-command-switch
251 (if (string-match "cmd\\.exe" tramp-encoding-shell)
254 "*Use this switch together with `tramp-encoding-shell' for local commands.
255 See the variable `tramp-encoding-shell' for more information."
259 (defcustom tramp-encoding-reads-stdin t
260 "*If non-nil, encoding commands read from standard input.
261 If nil, the filename is the last argument.
263 Note that the commands always must write to standard output."
267 (defcustom tramp-multi-sh-program
269 "*Use this program for bootstrapping multi-hop connections.
270 This variable is similar to `tramp-encoding-shell', but it is only used
271 when initializing a multi-hop connection. Therefore, the set of
272 commands sent to this shell is quite restricted, and if you are
273 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
274 shell which does not normally exist on Windows anyway).
276 To use multi-hop methods from Windows, you also need suitable entries
277 in `tramp-multi-connection-function-alist' for the first hop.
279 This variable defaults to the value of `tramp-encoding-shell'."
281 :type '(file :must-match t))
283 ;; CCC I have changed all occurrences of comint-quote-filename with
284 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
285 ;; There, comint-quote-filename was removed altogether. If it turns
286 ;; out to be necessary there, something will need to be done.
287 ;;-(defcustom tramp-file-name-quote-list
288 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
289 ;;- "*Protect these characters from the remote shell.
290 ;;-Any character in this list is quoted (preceded with a backslash)
291 ;;-because it means something special to the shell. This takes effect
292 ;;-when sending file and directory names to the remote shell.
294 ;;-See `comint-file-name-quote-list' for details."
296 ;;- :type '(repeat character))
298 (defcustom tramp-methods
299 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
300 (tramp-login-program "rsh")
301 (tramp-copy-program "rcp")
302 (tramp-remote-sh "/bin/sh")
303 (tramp-login-args nil)
304 (tramp-copy-args nil)
305 (tramp-copy-keep-date-arg "-p")
306 (tramp-password-end-of-line nil))
307 ("scp" (tramp-connection-function tramp-open-connection-rsh)
308 (tramp-login-program "ssh")
309 (tramp-copy-program "scp")
310 (tramp-remote-sh "/bin/sh")
311 (tramp-login-args ("-e" "none"))
312 (tramp-copy-args nil)
313 (tramp-copy-keep-date-arg "-p")
314 (tramp-password-end-of-line nil))
315 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
316 (tramp-login-program "ssh")
317 (tramp-copy-program "scp")
318 (tramp-remote-sh "/bin/sh")
319 (tramp-login-args ("-1" "-e" "none"))
320 (tramp-copy-args ("-1"))
321 (tramp-copy-keep-date-arg "-p")
322 (tramp-password-end-of-line nil))
323 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
324 (tramp-login-program "ssh")
325 (tramp-copy-program "scp")
326 (tramp-remote-sh "/bin/sh")
327 (tramp-login-args ("-2" "-e" "none"))
328 (tramp-copy-args ("-2"))
329 (tramp-copy-keep-date-arg "-p")
330 (tramp-password-end-of-line nil))
332 (tramp-connection-function tramp-open-connection-rsh)
333 (tramp-login-program "ssh1")
334 (tramp-copy-program "scp1")
335 (tramp-remote-sh "/bin/sh")
336 (tramp-login-args ("-e" "none"))
337 (tramp-copy-args nil)
338 (tramp-copy-keep-date-arg "-p")
339 (tramp-password-end-of-line nil))
341 (tramp-connection-function tramp-open-connection-rsh)
342 (tramp-login-program "ssh2")
343 (tramp-copy-program "scp2")
344 (tramp-remote-sh "/bin/sh")
345 (tramp-login-args ("-e" "none"))
346 (tramp-copy-args nil)
347 (tramp-copy-keep-date-arg "-p")
348 (tramp-password-end-of-line nil))
349 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
350 (tramp-login-program "ssh")
351 (tramp-copy-program "rsync")
352 (tramp-remote-sh "/bin/sh")
353 (tramp-login-args ("-e" "none"))
354 (tramp-copy-args ("-e" "ssh"))
355 (tramp-copy-keep-date-arg "-t")
356 (tramp-password-end-of-line nil))
357 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
358 (tramp-login-program "remsh")
359 (tramp-copy-program "rcp")
360 (tramp-remote-sh "/bin/sh")
361 (tramp-login-args nil)
362 (tramp-copy-args nil)
363 (tramp-copy-keep-date-arg "-p")
364 (tramp-password-end-of-line nil))
365 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
366 (tramp-login-program "rsh")
367 (tramp-copy-program nil)
368 (tramp-remote-sh "/bin/sh")
369 (tramp-login-args nil)
370 (tramp-copy-args nil)
371 (tramp-copy-keep-date-arg nil)
372 (tramp-password-end-of-line nil))
373 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
374 (tramp-login-program "ssh")
375 (tramp-copy-program nil)
376 (tramp-remote-sh "/bin/sh")
377 (tramp-login-args ("-e" "none"))
378 (tramp-copy-args nil)
379 (tramp-copy-keep-date-arg nil)
380 (tramp-password-end-of-line nil))
381 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
382 (tramp-login-program "ssh")
383 (tramp-copy-program nil)
384 (tramp-remote-sh "/bin/sh")
385 (tramp-login-args ("-1" "-e" "none"))
386 (tramp-copy-args ("-1"))
387 (tramp-copy-keep-date-arg nil)
388 (tramp-password-end-of-line nil))
389 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
390 (tramp-login-program "ssh")
391 (tramp-copy-program nil)
392 (tramp-remote-sh "/bin/sh")
393 (tramp-login-args ("-2" "-e" "none"))
394 (tramp-copy-args ("-2"))
395 (tramp-copy-keep-date-arg nil)
396 (tramp-password-end-of-line nil))
398 (tramp-connection-function tramp-open-connection-rsh)
399 (tramp-login-program "ssh1")
400 (tramp-copy-program nil)
401 (tramp-remote-sh "/bin/sh")
402 (tramp-login-args ("-e" "none"))
403 (tramp-copy-args nil)
404 (tramp-copy-keep-date-arg nil)
405 (tramp-password-end-of-line nil))
407 (tramp-connection-function tramp-open-connection-rsh)
408 (tramp-login-program "ssh2")
409 (tramp-copy-program nil)
410 (tramp-remote-sh "/bin/sh")
411 (tramp-login-args ("-e" "none"))
412 (tramp-copy-args nil)
413 (tramp-copy-keep-date-arg nil)
414 (tramp-password-end-of-line nil))
415 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
416 (tramp-login-program "remsh")
417 (tramp-copy-program nil)
418 (tramp-remote-sh "/bin/sh")
419 (tramp-login-args nil)
420 (tramp-copy-args nil)
421 (tramp-copy-keep-date-arg nil)
422 (tramp-password-end-of-line nil))
424 (tramp-connection-function tramp-open-connection-telnet)
425 (tramp-login-program "telnet")
426 (tramp-copy-program nil)
427 (tramp-remote-sh "/bin/sh")
428 (tramp-login-args nil)
429 (tramp-copy-args nil)
430 (tramp-copy-keep-date-arg nil)
431 (tramp-password-end-of-line nil))
432 ("su" (tramp-connection-function tramp-open-connection-su)
433 (tramp-login-program "su")
434 (tramp-copy-program nil)
435 (tramp-remote-sh "/bin/sh")
436 (tramp-login-args ("-" "%u"))
437 (tramp-copy-args nil)
438 (tramp-copy-keep-date-arg nil)
439 (tramp-password-end-of-line nil))
440 ("sudo" (tramp-connection-function tramp-open-connection-su)
441 (tramp-login-program "sudo")
442 (tramp-copy-program nil)
443 (tramp-remote-sh "/bin/sh")
444 (tramp-login-args ("-u" "%u" "-s"
446 (tramp-copy-args nil)
447 (tramp-copy-keep-date-arg nil)
448 (tramp-password-end-of-line nil))
449 ("multi" (tramp-connection-function tramp-open-connection-multi)
450 (tramp-login-program nil)
451 (tramp-copy-program nil)
452 (tramp-remote-sh "/bin/sh")
453 (tramp-login-args nil)
454 (tramp-copy-args nil)
455 (tramp-copy-keep-date-arg nil)
456 (tramp-password-end-of-line nil))
457 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
458 (tramp-login-program "ssh")
459 (tramp-copy-program "scp")
460 (tramp-remote-sh "/bin/sh")
461 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
462 (tramp-copy-args nil)
463 (tramp-copy-keep-date-arg "-p")
464 (tramp-password-end-of-line nil))
465 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
466 (tramp-login-program "ssh")
467 (tramp-copy-program nil)
468 (tramp-remote-sh "/bin/sh")
469 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
470 (tramp-copy-args nil)
471 (tramp-copy-keep-date-arg nil)
472 (tramp-password-end-of-line nil))
474 (tramp-connection-function tramp-open-connection-rsh)
475 (tramp-login-program "krlogin")
476 (tramp-copy-program nil)
477 (tramp-remote-sh "/bin/sh")
478 (tramp-login-args ("-x"))
479 (tramp-copy-args nil)
480 (tramp-copy-keep-date-arg nil)
481 (tramp-password-end-of-line nil))
483 (tramp-connection-function tramp-open-connection-rsh)
484 (tramp-login-program "plink")
485 (tramp-copy-program nil)
486 (tramp-remote-sh "/bin/sh")
487 (tramp-login-args ("-ssh")) ;optionally add "-v"
488 (tramp-copy-args nil)
489 (tramp-copy-keep-date-arg nil)
490 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
492 (tramp-connection-function tramp-open-connection-rsh)
493 (tramp-login-program "plink")
494 (tramp-copy-program nil)
495 (tramp-remote-sh "/bin/sh")
496 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
497 (tramp-copy-args nil)
498 (tramp-copy-keep-date-arg nil)
499 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
501 (tramp-connection-function tramp-open-connection-rsh)
502 (tramp-login-program "plink")
503 (tramp-copy-program "pscp")
504 (tramp-remote-sh "/bin/sh")
505 (tramp-login-args ("-ssh"))
506 (tramp-copy-args nil)
507 (tramp-copy-keep-date-arg "-p")
508 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
510 (tramp-connection-function tramp-open-connection-rsh)
511 (tramp-login-program "fsh")
512 (tramp-copy-program "fcp")
513 (tramp-remote-sh "/bin/sh -i")
514 (tramp-login-args ("sh" "-i"))
515 (tramp-copy-args nil)
516 (tramp-copy-keep-date-arg "-p")
517 (tramp-password-end-of-line nil))
519 "*Alist of methods for remote files.
520 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
521 Each NAME stands for a remote access method. Each PARAM is a
522 pair of the form (KEY VALUE). The following KEYs are defined:
523 * `tramp-connection-function'
524 This specifies the function to use to connect to the remote host.
525 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
526 and `tramp-open-connection-su' are defined. See the documentation
527 of these functions for more details.
529 This specifies the Bourne shell to use on the remote host. This
530 MUST be a Bourne-like shell. It is normally not necessary to set
531 this to any value other than \"/bin/sh\": tramp wants to use a shell
532 which groks tilde expansion, but it can search for it. Also note
533 that \"/bin/sh\" exists on all Unixen, this might not be true for
534 the value that you decide to use. You Have Been Warned.
535 * `tramp-login-program'
536 This specifies the name of the program to use for logging in to the
537 remote host. Depending on `tramp-connection-function', this may be
538 the name of rsh or a workalike program (when
539 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
540 name of telnet or a workalike (for `tramp-open-connection-telnet'),
541 or the name of su or a workalike (for `tramp-open-connection-su').
543 This specifies the list of arguments to pass to the above
544 mentioned program. Please note that this is a list of arguments,
545 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
546 here. Instead, you want two list elements, one for \"-a\" and one
547 for \"-b\", or one for \"-f\" and one for \"foo\".
548 If `tramp-connection-function' is `tramp-open-connection-su', then
549 \"%u\" in this list is replaced by the user name, and \"%%\" can
550 be used to obtain a literal percent character.
551 * `tramp-copy-program'
552 This specifies the name of the program to use for remotely copying
553 the file; this might be the absolute filename of rcp or the name of
556 This specifies the list of parameters to pass to the above mentioned
557 program, the hints for `tramp-login-args' also apply here.
558 * `tramp-copy-keep-date-arg'
559 This specifies the parameter to use for the copying program when the
560 timestamp of the original file should be kept. For `rcp', use `-p', for
562 * `tramp-password-end-of-line'
563 This specifies the string to use for terminating the line after
564 submitting the password. If this method parameter is nil, then the
565 value of the normal variable `tramp-default-password-end-of-line'
566 is used. This parameter is necessary because the \"plink\" program
567 requires any two characters after sending the password. These do
568 not have to be newline or carriage return characters. Other login
569 programs are happy with just one character, the newline character.
570 We use \"xy\" as the value for methods using \"plink\".
572 What does all this mean? Well, you should specify `tramp-login-program'
573 for all methods; this program is used to log in to the remote site. Then,
574 there are two ways to actually transfer the files between the local and the
575 remote side. One way is using an additional rcp-like program. If you want
576 to do this, set `tramp-copy-program' in the method.
578 Another possibility for file transfer is inline transfer, i.e. the
579 file is passed through the same buffer used by `tramp-login-program'. In
580 this case, the file contents need to be protected since the
581 `tramp-login-program' might use escape codes or the connection might not
582 be eight-bit clean. Therefore, file contents are encoded for transit.
583 See the variable `tramp-coding-commands' for details.
585 So, to summarize: if the method is an out-of-band method, then you
586 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
587 inline method, then these two parameters should be nil. Every method,
588 inline or out of band, must specify `tramp-connection-function' plus
589 the associated arguments (for example, the login program if you chose
590 `tramp-open-connection-telnet').
594 When using `tramp-open-connection-su' the phrase `open connection to a
595 remote host' sounds strange, but it is used nevertheless, for
596 consistency. No connection is opened to a remote host, but `su' is
597 started on the local host. You are not allowed to specify a remote
598 host other than `localhost' or the name of the local host."
602 (set (list (const tramp-connection-function) function)
603 (list (const tramp-login-program)
604 (choice (const nil) string))
605 (list (const tramp-copy-program)
606 (choice (const nil) string))
607 (list (const tramp-remote-sh)
608 (choice (const nil) string))
609 (list (const tramp-login-args) (repeat string))
610 (list (const tramp-copy-args) (repeat string))
611 (list (const tramp-copy-keep-date-arg)
612 (choice (const nil) string))
613 (list (const tramp-encoding-command)
614 (choice (const nil) string))
615 (list (const tramp-decoding-command)
616 (choice (const nil) string))
617 (list (const tramp-encoding-function)
618 (choice (const nil) function))
619 (list (const tramp-decoding-function)
620 (choice (const nil) function))
621 (list (const tramp-password-end-of-line)
622 (choice (const nil) string))))))
624 (defcustom tramp-multi-methods '("multi" "multiu")
625 "*List of multi-hop methods.
626 Each entry in this list should be a method name as mentioned in the
627 variable `tramp-methods'."
629 :type '(repeat string))
631 (defcustom tramp-multi-connection-function-alist
632 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
633 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
634 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
635 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
636 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
637 ("su" tramp-multi-connect-su "su - %u%n")
638 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
639 "*List of connection functions for multi-hop methods.
640 Each list item is a list of three items (METHOD FUNCTION COMMAND),
641 where METHOD is the name as used in the file name, FUNCTION is the
642 function to be executed, and COMMAND is the shell command used for
645 COMMAND may contain percent escapes. `%u' will be replaced with the
646 user name, `%h' will be replaced with the host name, and `%n' will be
647 replaced with an end-of-line character, as specified in the variable
648 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
649 Note that the interpretation of the percent escapes also depends on
650 the FUNCTION. For example, the `%u' escape is forbidden with the
651 function `tramp-multi-connect-telnet'. See the documentation of the
652 various functions for details."
654 :type '(repeat (list string function string)))
656 (defcustom tramp-default-method
657 (if (and (fboundp 'executable-find)
658 (executable-find "plink"))
661 "*Default method to use for transferring files.
662 See `tramp-methods' for possibilities.
663 Also see `tramp-default-method-alist'."
667 (defcustom tramp-default-method-alist
668 '(("\\`localhost\\'" "\\`root\\'" "su"))
669 "*Default method to use for specific user/host pairs.
670 This is an alist of items (HOST USER METHOD). The first matching item
671 specifies the method to use for a file name which does not specify a
672 method. HOST and USER are regular expressions or nil, which is
673 interpreted as a regular expression which always matches. If no entry
674 matches, the variable `tramp-default-method' takes effect.
676 If the file name does not specify the user, lookup is done using the
677 empty string for the user name.
679 See `tramp-methods' for a list of possibilities for METHOD."
681 :type '(repeat (list (regexp :tag "Host regexp")
682 (regexp :tag "User regexp")
683 (string :tag "Method"))))
685 ;; Default values for non-Unices seeked
686 (defconst tramp-completion-function-alist-rsh
687 (unless (memq system-type '(windows-nt))
688 '((tramp-parse-rhosts "/etc/hosts.equiv")
689 (tramp-parse-rhosts "~/.rhosts")))
690 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
692 ;; Default values for non-Unices seeked
693 (defconst tramp-completion-function-alist-ssh
694 (unless (memq system-type '(windows-nt))
695 '((tramp-parse-rhosts "/etc/hosts.equiv")
696 (tramp-parse-rhosts "/etc/shosts.equiv")
697 (tramp-parse-shosts "/etc/ssh_known_hosts")
698 (tramp-parse-sconfig "/etc/ssh_config")
699 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
700 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
701 (tramp-parse-rhosts "~/.rhosts")
702 (tramp-parse-rhosts "~/.shosts")
703 (tramp-parse-shosts "~/.ssh/known_hosts")
704 (tramp-parse-sconfig "~/.ssh/config")
705 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
706 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
707 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
709 ;; Default values for non-Unices seeked
710 (defconst tramp-completion-function-alist-telnet
711 (unless (memq system-type '(windows-nt))
712 '((tramp-parse-hosts "/etc/hosts")))
713 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
715 ;; Default values for non-Unices seeked
716 (defconst tramp-completion-function-alist-su
717 (unless (memq system-type '(windows-nt))
718 '((tramp-parse-passwd "/etc/passwd")))
719 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
721 (defvar tramp-completion-function-alist nil
722 "*Alist of methods for remote files.
723 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
724 Each NAME stands for a remote access method. Each PAIR is of the form
725 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
726 names from FILE for completion. The following predefined FUNCTIONs exists:
728 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
729 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
730 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
731 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
732 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
733 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
734 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
735 * `tramp-parse-netrc' for \"~/.netrc\" like files.
737 FUNCTION can also be a customer defined function. For more details see
740 (eval-after-load "tramp"
742 (tramp-set-completion-function
743 "rcp" tramp-completion-function-alist-rsh)
744 (tramp-set-completion-function
745 "scp" tramp-completion-function-alist-ssh)
746 (tramp-set-completion-function
747 "scp1" tramp-completion-function-alist-ssh)
748 (tramp-set-completion-function
749 "scp2" tramp-completion-function-alist-ssh)
750 (tramp-set-completion-function
751 "scp1_old" tramp-completion-function-alist-ssh)
752 (tramp-set-completion-function
753 "scp2_old" tramp-completion-function-alist-ssh)
754 (tramp-set-completion-function
755 "rsync" tramp-completion-function-alist-rsh)
756 (tramp-set-completion-function
757 "remcp" tramp-completion-function-alist-rsh)
758 (tramp-set-completion-function
759 "rsh" tramp-completion-function-alist-rsh)
760 (tramp-set-completion-function
761 "ssh" tramp-completion-function-alist-ssh)
762 (tramp-set-completion-function
763 "ssh1" tramp-completion-function-alist-ssh)
764 (tramp-set-completion-function
765 "ssh2" tramp-completion-function-alist-ssh)
766 (tramp-set-completion-function
767 "ssh1_old" tramp-completion-function-alist-ssh)
768 (tramp-set-completion-function
769 "ssh2_old" tramp-completion-function-alist-ssh)
770 (tramp-set-completion-function
771 "remsh" tramp-completion-function-alist-rsh)
772 (tramp-set-completion-function
773 "telnet" tramp-completion-function-alist-telnet)
774 (tramp-set-completion-function
775 "su" tramp-completion-function-alist-su)
776 (tramp-set-completion-function
777 "sudo" tramp-completion-function-alist-su)
778 (tramp-set-completion-function
780 (tramp-set-completion-function
781 "scpx" tramp-completion-function-alist-ssh)
782 (tramp-set-completion-function
783 "sshx" tramp-completion-function-alist-ssh)
784 (tramp-set-completion-function
785 "krlogin" tramp-completion-function-alist-rsh)
786 (tramp-set-completion-function
787 "plink" tramp-completion-function-alist-ssh)
788 (tramp-set-completion-function
789 "plink1" tramp-completion-function-alist-ssh)
790 (tramp-set-completion-function
791 "pscp" tramp-completion-function-alist-ssh)
792 (tramp-set-completion-function
793 "fcp" tramp-completion-function-alist-ssh)))
795 (defcustom tramp-rsh-end-of-line "\n"
796 "*String used for end of line in rsh connections.
797 I don't think this ever needs to be changed, so please tell me about it
798 if you need to change this.
799 Also see the method parameter `tramp-password-end-of-line' and the normal
800 variable `tramp-default-password-end-of-line'."
804 (defcustom tramp-default-password-end-of-line
805 tramp-rsh-end-of-line
806 "*String used for end of line after sending a password.
807 This variable provides the default value for the method parameter
808 `tramp-password-end-of-line', see `tramp-methods' for more details.
810 It seems that people using plink under Windows need to send
811 \"\\r\\n\" (carriage-return, then newline) after a password, but just
812 \"\\n\" after all other lines. This variable can be used for the
813 password, see `tramp-rsh-end-of-line' for the other cases.
815 The default value is to use the same value as `tramp-rsh-end-of-line'."
819 (defcustom tramp-remote-path
820 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
821 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
822 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
823 "*List of directories to search for executables on remote host.
824 Please notify me about other semi-standard directories to include here.
826 You can use `~' in this list, but when searching for a shell which groks
827 tilde expansion, all directory names starting with `~' will be ignored."
829 :type '(repeat string))
831 (defcustom tramp-login-prompt-regexp
833 "*Regexp matching login-like prompts.
834 The regexp should match at end of buffer."
838 (defcustom tramp-shell-prompt-pattern
839 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
840 "Regexp to match prompts from remote shell.
841 Normally, Tramp expects you to configure `shell-prompt-pattern'
842 correctly, but sometimes it happens that you are connecting to a
843 remote host which sends a different kind of shell prompt. Therefore,
844 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
845 and also things matched by this variable. The default value of this
846 variable is similar to the default value of `shell-prompt-pattern',
847 which should work well in many cases."
851 (defcustom tramp-password-prompt-regexp
852 "^.*\\([pP]assword\\|passphrase.*\\):\^@? *"
853 "*Regexp matching password-like prompts.
854 The regexp should match at end of buffer.
856 The `sudo' program appears to insert a `^@' character into the prompt."
860 (defcustom tramp-wrong-passwd-regexp
862 ;; These strings should be on the last line
863 (regexp-opt '("Permission denied."
869 "Name or service not known"
870 "Host key verification failed.") t)
874 ;; Here comes a list of regexes, separated by \\|
875 "Received signal [0-9]+"
877 "*Regexp matching a `login failed' message.
878 The regexp should match at end of buffer."
882 (defcustom tramp-yesno-prompt-regexp
884 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
886 "Regular expression matching all yes/no queries which need to be confirmed.
887 The confirmation should be done with yes or no.
888 The regexp should match at end of buffer.
889 See also `tramp-yn-prompt-regexp'."
893 (defcustom tramp-yn-prompt-regexp
894 (concat (regexp-opt '("Store key in cache? (y/n)") t)
896 "Regular expression matching all y/n queries which need to be confirmed.
897 The confirmation should be done with y or n.
898 The regexp should match at end of buffer.
899 See also `tramp-yesno-prompt-regexp'."
903 (defcustom tramp-terminal-prompt-regexp
907 "Terminal type\\? \\[.*\\]"
909 "Regular expression matching all terminal setting prompts.
910 The regexp should match at end of buffer.
911 The answer will be provided by `tramp-action-terminal', which see."
915 (defcustom tramp-process-alive-regexp
917 "Regular expression indicating a process has finished.
918 In fact this expression is empty by intention, it will be used only to
919 check regularly the status of the associated process.
920 The answer will be provided by `tramp-action-process-alive',
921 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
925 (defcustom tramp-temp-name-prefix "tramp."
926 "*Prefix to use for temporary files.
927 If this is a relative file name (such as \"tramp.\"), it is considered
928 relative to the directory name returned by the function
929 `tramp-temporary-file-directory' (which see). It may also be an
930 absolute file name; don't forget to include a prefix for the filename
935 (defcustom tramp-discard-garbage nil
936 "*If non-nil, try to discard garbage sent by remote shell.
937 Some shells send such garbage upon connection setup."
941 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
942 "*Alist specifying extra arguments to pass to the remote shell.
943 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
944 matching the shell file name and ARGS is a string specifying the
947 This variable is only used when Tramp needs to start up another shell
948 for tilde expansion. The extra arguments should typically prevent the
949 shell from reading its init file."
951 ;; This might be the wrong way to test whether the widget type
952 ;; `alist' is available. Who knows the right way to test it?
953 :type (if (get 'alist 'widget-type)
954 '(alist :key-type string :value-type string)
955 '(repeat (cons string string))))
957 (defcustom tramp-prefix-format
958 (if tramp-unified-filenames "/" "/[")
959 "*String matching the very beginning of tramp file names.
960 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
964 (defcustom tramp-prefix-regexp
965 (concat "^" (regexp-quote tramp-prefix-format))
966 "*Regexp matching the very beginning of tramp file names.
967 Should always start with \"^\". Derived from `tramp-prefix-format'."
971 (defcustom tramp-method-regexp
973 "*Regexp matching methods identifiers."
977 ;; It is a little bit annoying that in XEmacs case this delimeter is different
978 ;; for single-hop and multi-hop cases.
979 (defcustom tramp-postfix-single-method-format
980 (if tramp-unified-filenames ":" "/")
981 "*String matching delimeter between method and user or host names.
982 Applicable for single-hop methods.
983 Used in `tramp-make-tramp-file-name'."
987 (defcustom tramp-postfix-single-method-regexp
988 (regexp-quote tramp-postfix-single-method-format)
989 "*Regexp matching delimeter between method and user or host names.
990 Applicable for single-hop methods.
991 Derived from `tramp-postfix-single-method-format'."
995 (defcustom tramp-postfix-multi-method-format
997 "*String matching delimeter between method and user or host names.
998 Applicable for multi-hop methods.
999 Used in `tramp-make-tramp-multi-file-name'."
1003 (defcustom tramp-postfix-multi-method-regexp
1004 (regexp-quote tramp-postfix-multi-method-format)
1005 "*Regexp matching delimeter between method and user or host names.
1006 Applicable for multi-hop methods.
1007 Derived from `tramp-postfix-multi-method-format'."
1011 (defcustom tramp-postfix-multi-hop-format
1012 (if tramp-unified-filenames ":" "/")
1013 "*String matching delimeter between host and next method.
1014 Applicable for multi-hop methods.
1015 Used in `tramp-make-tramp-multi-file-name'."
1019 (defcustom tramp-postfix-multi-hop-regexp
1020 (regexp-quote tramp-postfix-multi-hop-format)
1021 "*Regexp matching delimeter between host and next method.
1022 Applicable for multi-hop methods.
1023 Derived from `tramp-postfix-multi-hop-format'."
1027 (defcustom tramp-user-regexp
1029 "*Regexp matching user names."
1033 (defcustom tramp-postfix-user-format
1035 "*String matching delimeter between user and host names.
1036 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1040 (defcustom tramp-postfix-user-regexp
1041 (regexp-quote tramp-postfix-user-format)
1042 "*Regexp matching delimeter between user and host names.
1043 Derived from `tramp-postfix-user-format'."
1047 (defcustom tramp-host-regexp
1049 "*Regexp matching host names."
1053 (defcustom tramp-host-with-port-regexp
1055 "*Regexp matching host names."
1059 (defcustom tramp-postfix-host-format
1060 (if tramp-unified-filenames ":" "]")
1061 "*String matching delimeter between host names and localnames.
1062 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1066 (defcustom tramp-postfix-host-regexp
1067 (regexp-quote tramp-postfix-host-format)
1068 "*Regexp matching delimeter between host names and localnames.
1069 Derived from `tramp-postfix-host-format'."
1073 (defcustom tramp-localname-regexp
1075 "*Regexp matching localnames."
1079 ;; File name format.
1081 (defcustom tramp-file-name-structure
1085 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1086 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1087 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1088 "\\(" tramp-localname-regexp "\\)")
1091 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1092 the tramp file name structure.
1094 The first element REGEXP is a regular expression matching a tramp file
1095 name. The regex should contain parentheses around the method name,
1096 the user name, the host name, and the file name parts.
1098 The second element METHOD is a number, saying which pair of
1099 parentheses matches the method name. The third element USER is
1100 similar, but for the user name. The fourth element HOST is similar,
1101 but for the host name. The fifth element FILE is for the file name.
1102 These numbers are passed directly to `match-string', which see. That
1103 means the opening parentheses are counted to identify the pair.
1105 See also `tramp-file-name-regexp'."
1107 :type '(list (regexp :tag "File name regexp")
1108 (integer :tag "Paren pair for method name")
1109 (integer :tag "Paren pair for user name ")
1110 (integer :tag "Paren pair for host name ")
1111 (integer :tag "Paren pair for file name ")))
1114 (defconst tramp-file-name-regexp-unified
1116 "Value for `tramp-file-name-regexp' for unified remoting.
1117 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1118 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1121 (defconst tramp-file-name-regexp-separate
1123 "Value for `tramp-file-name-regexp' for separate remoting.
1124 XEmacs uses a separate filename syntax for Tramp and EFS.
1125 See `tramp-file-name-structure-separate' for more explanations.")
1128 (defcustom tramp-file-name-regexp
1129 (if tramp-unified-filenames
1130 tramp-file-name-regexp-unified
1131 tramp-file-name-regexp-separate)
1132 "*Regular expression matching file names handled by tramp.
1133 This regexp should match tramp file names but no other file names.
1134 \(When tramp.el is loaded, this regular expression is prepended to
1135 `file-name-handler-alist', and that is searched sequentially. Thus,
1136 if the tramp entry appears rather early in the `file-name-handler-alist'
1137 and is a bit too general, then some files might be considered tramp
1138 files which are not really tramp files.
1140 Please note that the entry in `file-name-handler-alist' is made when
1141 this file (tramp.el) is loaded. This means that this variable must be set
1142 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1143 updated after changing this variable.
1145 Also see `tramp-file-name-structure'."
1150 (defconst tramp-completion-file-name-regexp-unified
1151 "^/$\\|^/[^/:][^/]*$"
1152 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1153 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1154 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1157 (defconst tramp-completion-file-name-regexp-separate
1158 "^/\\([[][^]]*\\)?$"
1159 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1160 XEmacs uses a separate filename syntax for Tramp and EFS.
1161 See `tramp-file-name-structure-separate' for more explanations.")
1164 (defcustom tramp-completion-file-name-regexp
1165 (if tramp-unified-filenames
1166 tramp-completion-file-name-regexp-unified
1167 tramp-completion-file-name-regexp-separate)
1168 "*Regular expression matching file names handled by tramp completion.
1169 This regexp should match partial tramp file names only.
1171 Please note that the entry in `file-name-handler-alist' is made when
1172 this file (tramp.el) is loaded. This means that this variable must be set
1173 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1174 updated after changing this variable.
1176 Also see `tramp-file-name-structure'."
1180 (defcustom tramp-multi-file-name-structure
1184 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1185 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1186 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1188 "*Describes the file name structure of `multi' files.
1189 Multi files allow you to contact a remote host in several hops.
1190 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1192 The first element, REGEXP, gives a regular expression to match against
1193 the file name. In this regular expression, `%s' is replaced with the
1194 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1195 allow multiple hops, you normally want to use something like
1196 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1197 of parentheses is used for the HOP element, see below.)
1199 All remaining elements are numbers. METHOD gives the number of the
1200 paren pair which matches the method name. HOP gives the number of the
1201 paren pair which matches the hop sequence. LOCALNAME gives the number of
1202 the paren pair which matches the localname (pathname) on the remote host.
1204 LOCALNAME can also be negative, which means to count from the end. Ie, a
1205 value of -1 means the last paren pair.
1207 I think it would be good if the regexp matches the whole of the
1208 string, but I haven't actually tried what happens if it doesn't..."
1210 :type '(list (regexp :tag "File name regexp")
1211 (integer :tag "Paren pair for method name")
1212 (integer :tag "Paren pair for hops")
1213 (integer :tag "Paren pair to match localname")))
1215 (defcustom tramp-multi-file-name-hop-structure
1218 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1219 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1220 "\\(" tramp-host-with-port-regexp "\\)")
1222 "*Describes the structure of a hop in multi files.
1223 This is a list of four elements (REGEXP METHOD USER HOST). First
1224 element REGEXP is used to match against the hop. Pair number METHOD
1225 matches the method of one hop, pair number USER matches the user of
1226 one hop, pair number HOST matches the host of one hop.
1228 This regular expression should match exactly all of one hop."
1230 :type '(list (regexp :tag "Hop regexp")
1231 (integer :tag "Paren pair for method name")
1232 (integer :tag "Paren pair for user name")
1233 (integer :tag "Paren pair for host name")))
1235 (defcustom tramp-make-multi-tramp-file-format
1237 (concat tramp-prefix-format "%m")
1238 (concat tramp-postfix-multi-hop-format
1239 "%m" tramp-postfix-multi-method-format
1240 "%u" tramp-postfix-user-format
1242 (concat tramp-postfix-host-format "%p"))
1243 "*Describes how to construct a `multi' file name.
1244 This is a list of three elements PREFIX, HOP and LOCALNAME.
1246 The first element PREFIX says how to construct the prefix, the second
1247 element HOP specifies what each hop looks like, and the final element
1248 LOCALNAME says how to construct the localname (pathname).
1250 In PREFIX, `%%' means `%' and `%m' means the method name.
1252 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1253 user and hop host, respectively.
1255 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1257 The resulting file name always contains one copy of PREFIX and one
1258 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1261 Note: the current implementation requires the prefix to contain the
1262 method name, followed by all the hops, and the localname must come
1265 :type '(list string string string))
1267 (defcustom tramp-terminal-type "dumb"
1268 "*Value of TERM environment variable for logging in to remote host.
1269 Because Tramp wants to parse the output of the remote shell, it is easily
1270 confused by ANSI color escape sequences and suchlike. Often, shell init
1271 files conditionalize this setup based on the TERM environment variable."
1275 (defcustom tramp-completion-without-shell-p nil
1276 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1277 Using shell wildcards for completions has the advantage that it can be
1278 fast even in large directories, but completion is always
1279 case-sensitive. Relying on Lisp only means that case-insensitive
1280 completion is possible (subject to the variable `completion-ignore-case'),
1281 but it might be slow on large directories."
1285 (defcustom tramp-actions-before-shell
1286 '((tramp-password-prompt-regexp tramp-action-password)
1287 (tramp-login-prompt-regexp tramp-action-login)
1288 (shell-prompt-pattern tramp-action-succeed)
1289 (tramp-shell-prompt-pattern tramp-action-succeed)
1290 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1291 (tramp-yesno-prompt-regexp tramp-action-yesno)
1292 (tramp-yn-prompt-regexp tramp-action-yn)
1293 (tramp-terminal-prompt-regexp tramp-action-terminal)
1294 (tramp-process-alive-regexp tramp-action-process-alive))
1295 "List of pattern/action pairs.
1296 Whenever a pattern matches, the corresponding action is performed.
1297 Each item looks like (PATTERN ACTION).
1299 The PATTERN should be a symbol, a variable. The value of this
1300 variable gives the regular expression to search for. Note that the
1301 regexp must match at the end of the buffer, \"\\'\" is implicitly
1304 The ACTION should also be a symbol, but a function. When the
1305 corresponding PATTERN matches, the ACTION function is called."
1307 :type '(repeat (list variable function)))
1309 (defcustom tramp-actions-copy-out-of-band
1310 '((tramp-password-prompt-regexp tramp-action-password)
1311 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1312 (tramp-process-alive-regexp tramp-action-out-of-band))
1313 "List of pattern/action pairs.
1314 This list is used for copying/renaming with out-of-band methods.
1315 See `tramp-actions-before-shell' for more info."
1317 :type '(repeat (list variable function)))
1319 (defcustom tramp-multi-actions
1320 '((tramp-password-prompt-regexp tramp-multi-action-password)
1321 (tramp-login-prompt-regexp tramp-multi-action-login)
1322 (shell-prompt-pattern tramp-multi-action-succeed)
1323 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1324 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1325 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1326 "List of pattern/action pairs.
1327 This list is used for each hop in multi-hop connections.
1328 See `tramp-actions-before-shell' for more info."
1330 :type '(repeat (list variable function)))
1332 (defcustom tramp-initial-commands
1335 "unset autocorrect")
1336 "List of commands to send to the first remote shell that we see.
1337 These commands will be sent to any shell, and thus they should be
1338 designed to work in such circumstances. Also, restrict the commands
1339 to the bare necessity for getting the remote shell into a state
1340 where it is possible to execute the Bourne-ish shell.
1342 At the moment, the command to execute the Bourne-ish shell uses strange
1343 quoting which `tcsh' tries to correct, so we send the command \"unset
1344 autocorrect\" to the remote host."
1346 :type '(repeat string))
1348 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1349 ;; known to have a bug in `process-send-string'; some ssh connections appear
1350 ;; to drop bytes when data is sent too quickly.
1351 (defcustom tramp-chunksize
1352 (when (and (not (featurep 'xemacs))
1353 (memq system-type '(hpux)))
1355 "*If non-nil, chunksize for sending input to local process.
1356 It is necessary only on systems which have a buggy `process-send-string'
1357 implementation. The necessity, whether this variable must be set, can be
1358 checked via the following code:
1362 (proc (start-process (buffer-name) (current-buffer) \"wc\" \"-c\")))
1363 (process-send-string proc (make-string bytes ?x))
1364 (process-send-eof proc)
1365 (process-send-eof proc)
1366 (accept-process-output proc 1)
1367 (goto-char (point-min))
1368 (re-search-forward \"\\\\w+\")
1369 (message \"Bytes sent: %s\\tBytes received: %s\" bytes (match-string 0))))
1371 In the Emacs normally running Tramp, evaluate the above code.
1372 You can do this, for example, by pasting it into the `*scratch*'
1373 buffer and then hitting C-j with the cursor after the last
1374 closing parenthesis.
1376 If your Emacs is buggy, the sent and received numbers will be
1377 different. In that case, you'll want to set this variable to
1378 some number. For those people who have needed it, the value 500
1379 seems to have worked well. There is no way to predict what value
1380 you need; maybe you could just experiment a bit.
1382 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1383 this variable to be set as well."
1385 :type '(choice (const nil) integer))
1387 ;; Logging in to a remote host normally requires obtaining a pty. But
1388 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1389 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1390 ;; for an override of the system default.
1391 (defcustom tramp-process-connection-type t
1392 "Overrides `process-connection-type' for connections from Tramp.
1393 Tramp binds process-connection-type to the value given here before
1394 opening a connection to a remote host."
1396 :type '(choice (const nil) (const t) (const pty)))
1398 ;;; Internal Variables:
1400 (defvar tramp-buffer-file-attributes nil
1401 "Holds the `ls -ild' output for the current buffer.
1402 This variable is local to each buffer. It is not used if the remote
1403 machine groks Perl. If it is used, it's used as an emulation for
1404 the visited file modtime.")
1405 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1407 (defvar tramp-md5-function
1408 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1409 ((fboundp 'md5-encode)
1410 (lambda (x) (base64-encode-string
1411 (funcall (symbol-function 'md5-encode) x))))
1412 (t (error "Coulnd't find an `md5' function")))
1413 "Function to call for running the MD5 algorithm.")
1415 (defvar tramp-end-of-output
1417 (funcall tramp-md5-function
1419 (prin1-to-string process-environment)
1420 (current-time-string)
1422 ;; (if (fboundp 'directory-files-and-attributes)
1423 ;; (funcall 'directory-files-and-attributes
1424 ;; (or (getenv "HOME")
1425 ;; (tramp-temporary-file-directory)))
1428 ;; (cons x (file-attributes x)))
1429 ;; (directory-files (or (getenv "HOME")
1430 ;; (tramp-temporary-file-directory))
1433 "String used to recognize end of output.")
1435 (defvar tramp-connection-function nil
1436 "This internal variable holds a parameter for `tramp-methods'.
1437 In the connection buffer, this variable has the value of the like-named
1438 method parameter, as specified in `tramp-methods' (which see).")
1440 (defvar tramp-remote-sh nil
1441 "This internal variable holds a parameter for `tramp-methods'.
1442 In the connection buffer, this variable has the value of the like-named
1443 method parameter, as specified in `tramp-methods' (which see).")
1445 (defvar tramp-login-program nil
1446 "This internal variable holds a parameter for `tramp-methods'.
1447 In the connection buffer, this variable has the value of the like-named
1448 method parameter, as specified in `tramp-methods' (which see).")
1450 (defvar tramp-login-args nil
1451 "This internal variable holds a parameter for `tramp-methods'.
1452 In the connection buffer, this variable has the value of the like-named
1453 method parameter, as specified in `tramp-methods' (which see).")
1455 (defvar tramp-copy-program nil
1456 "This internal variable holds a parameter for `tramp-methods'.
1457 In the connection buffer, this variable has the value of the like-named
1458 method parameter, as specified in `tramp-methods' (which see).")
1460 (defvar tramp-copy-args nil
1461 "This internal variable holds a parameter for `tramp-methods'.
1462 In the connection buffer, this variable has the value of the like-named
1463 method parameter, as specified in `tramp-methods' (which see).")
1465 (defvar tramp-copy-keep-date-arg nil
1466 "This internal variable holds a parameter for `tramp-methods'.
1467 In the connection buffer, this variable has the value of the like-named
1468 method parameter, as specified in `tramp-methods' (which see).")
1470 (defvar tramp-encoding-command nil
1471 "This internal variable holds a parameter for `tramp-methods'.
1472 In the connection buffer, this variable has the value of the like-named
1473 method parameter, as specified in `tramp-methods' (which see).")
1475 (defvar tramp-decoding-command nil
1476 "This internal variable holds a parameter for `tramp-methods'.
1477 In the connection buffer, this variable has the value of the like-named
1478 method parameter, as specified in `tramp-methods' (which see).")
1480 (defvar tramp-encoding-function nil
1481 "This internal variable holds a parameter for `tramp-methods'.
1482 In the connection buffer, this variable has the value of the like-named
1483 method parameter, as specified in `tramp-methods' (which see).")
1485 (defvar tramp-decoding-function nil
1486 "This internal variable holds a parameter for `tramp-methods'.
1487 In the connection buffer, this variable has the value of the like-named
1488 method parameter, as specified in `tramp-methods' (which see).")
1490 (defvar tramp-password-end-of-line nil
1491 "This internal variable holds a parameter for `tramp-methods'.
1492 In the connection buffer, this variable has the value of the like-named
1493 method parameter, as specified in `tramp-methods' (which see).")
1495 ;; CCC `local in each buffer'?
1496 (defvar tramp-ls-command nil
1497 "This command is used to get a long listing with numeric user and group ids.
1498 This variable is automatically made buffer-local to each rsh process buffer
1499 upon opening the connection.")
1501 (defvar tramp-current-multi-method nil
1502 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1503 This variable is automatically made buffer-local to each rsh process buffer
1504 upon opening the connection.")
1506 (defvar tramp-current-method nil
1507 "Connection method for this *tramp* buffer.
1508 This variable is automatically made buffer-local to each rsh process buffer
1509 upon opening the connection.")
1511 (defvar tramp-current-user nil
1512 "Remote login name for this *tramp* buffer.
1513 This variable is automatically made buffer-local to each rsh process buffer
1514 upon opening the connection.")
1516 (defvar tramp-current-host nil
1517 "Remote host for this *tramp* buffer.
1518 This variable is automatically made buffer-local to each rsh process buffer
1519 upon opening the connection.")
1521 (defvar tramp-test-groks-nt nil
1522 "Whether the `test' command groks the `-nt' switch.
1523 \(`test A -nt B' tests if file A is newer than file B.)
1524 This variable is automatically made buffer-local to each rsh process buffer
1525 upon opening the connection.")
1527 (defvar tramp-file-exists-command nil
1528 "Command to use for checking if a file exists.
1529 This variable is automatically made buffer-local to each rsh process buffer
1530 upon opening the connection.")
1532 (defconst tramp-uudecode "\
1534 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1538 "Shell function to implement `uudecode' to standard output.
1539 Many systems support `uudecode -o /dev/stdout' for this or
1540 `uudecode -o -' or `uudecode -p', but some systems don't, and for
1541 them we have this shell function.")
1543 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1544 ;; output. If you are hacking on this, note that you get *no* output
1545 ;; unless this spits out a complete line, including the '\n' at the
1547 ;; The device number is returned as "-1", because there will be a virtual
1548 ;; device number set in `tramp-handle-file-attributes'
1549 (defconst tramp-perl-file-attributes "\
1550 @stat = lstat($ARGV[0]);
1551 if (($stat[2] & 0170000) == 0120000)
1553 $type = readlink($ARGV[0]);
1554 $type = \"\\\"$type\\\"\";
1556 elsif (($stat[2] & 0170000) == 040000)
1564 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1565 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1567 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1572 $stat[8] >> 16 & 0xffff,
1574 $stat[9] >> 16 & 0xffff,
1576 $stat[10] >> 16 & 0xffff,
1580 $stat[1] >> 16 & 0xffff,
1583 "Perl script to produce output suitable for use with `file-attributes'
1584 on the remote file system.")
1586 (defconst tramp-perl-directory-files-and-attributes "\
1589 @list = readdir(DIR);
1593 for($i = 0; $i < $n; $i++)
1595 $filename = $list[$i];
1596 @stat = lstat($filename);
1597 if (($stat[2] & 0170000) == 0120000)
1599 $type = readlink($filename);
1600 $type = \"\\\"$type\\\"\";
1602 elsif (($stat[2] & 0170000) == 040000)
1610 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1611 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1613 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1619 $stat[8] >> 16 & 0xffff,
1621 $stat[9] >> 16 & 0xffff,
1623 $stat[10] >> 16 & 0xffff,
1627 $stat[1] >> 16 & 0xffff,
1629 $stat[0] >> 16 & 0xffff,
1633 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1636 ;; ;; These two use uu encoding.
1637 ;; (defvar tramp-perl-encode "%s -e'\
1638 ;; print qq(begin 644 xxx\n);
1641 ;; while (read(STDIN, $s, 45)) {
1642 ;; print pack(q(u), $s);
1647 ;; "Perl program to use for encoding a file.
1648 ;; Escape sequence %s is replaced with name of Perl binary.")
1650 ;; (defvar tramp-perl-decode "%s -ne '
1651 ;; print unpack q(u), $_;
1653 ;; "Perl program to use for decoding a file.
1654 ;; Escape sequence %s is replaced with name of Perl binary.")
1656 ;; These two use base64 encoding.
1657 (defvar tramp-perl-encode-with-module
1658 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1659 "Perl program to use for encoding a file.
1660 Escape sequence %s is replaced with name of Perl binary.
1661 This string is passed to `format', so percent characters need to be doubled.
1662 This implementation requires the MIME::Base64 Perl module to be installed
1663 on the remote host.")
1665 (defvar tramp-perl-decode-with-module
1666 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1667 "Perl program to use for decoding a file.
1668 Escape sequence %s is replaced with name of Perl binary.
1669 This string is passed to `format', so percent characters need to be doubled.
1670 This implementation requires the MIME::Base64 Perl module to be installed
1671 on the remote host.")
1673 (defvar tramp-perl-encode
1675 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1676 # Copyright (C) 2002 Free Software Foundation, Inc.
1681 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1682 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1687 # We read in chunks of 54 bytes, to generate output lines
1688 # of 72 chars (plus end of line)
1691 while (my $data = <STDIN>) {
1694 # Only for the last chunk, and only if did not fill the last three-byte packet
1696 my $mod = length($data) %% 3;
1697 $pad = q(=) x (3 - $mod) if $mod;
1700 # Not the fastest method, but it is simple: unpack to binary string, split
1701 # by groups of 6 bits and convert back from binary to byte; then map into
1702 # the translation table
1706 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1711 "Perl program to use for encoding a file.
1712 Escape sequence %s is replaced with name of Perl binary.
1713 This string is passed to `format', so percent characters need to be doubled.")
1715 (defvar tramp-perl-decode
1717 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1718 # Copyright (C) 2002 Free Software Foundation, Inc.
1723 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1724 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1727 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1731 # We are going to accumulate into $pending to accept any line length
1732 # (we do not check they are <= 76 chars as the RFC says)
1735 while (my $data = <STDIN>) {
1738 # If we find one or two =, we have reached the end and
1739 # any following data is to be discarded
1740 my $finished = $data =~ s/(==?).*/$1/;
1743 my $len = length($pending);
1744 my $chunk = substr($pending, 0, $len & ~3);
1745 $pending = substr($pending, $len & ~3 + 1);
1747 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1748 # split in 8-bit chunks and convert back to char.
1751 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1756 "Perl program to use for decoding a file.
1757 Escape sequence %s is replaced with name of Perl binary.
1758 This string is passed to `format', so percent characters need to be doubled.")
1760 ; These values conform to `file-attributes' from XEmacs 21.2.
1761 ; GNU Emacs and other tools not checked.
1762 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1764 (2 . "c") ; character device
1765 (3 . "m") ; multiplexed character device (v7)
1766 (4 . "d") ; directory
1767 (5 . "?") ; Named special file (XENIX)
1768 (6 . "b") ; block device
1769 (7 . "?") ; multiplexed block device (v7)
1770 (8 . "-") ; regular file
1771 (9 . "n") ; network special file (HP-UX)
1772 (10 . "l") ; symlink
1773 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1775 (13 . "D") ; door special (Solaris)
1776 (14 . "w")) ; whiteout (BSD)
1777 "A list of file types returned from the `stat' system call.
1778 This is used to map a mode number to a permission string.")
1780 (defvar tramp-dos-coding-system
1781 (if (and (fboundp 'coding-system-p)
1782 (funcall 'coding-system-p '(dos)))
1785 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1787 (defvar tramp-last-cmd nil
1788 "Internal Tramp variable recording the last command sent.
1789 This variable is buffer-local in every buffer.")
1790 (make-variable-buffer-local 'tramp-last-cmd)
1792 (defvar tramp-process-echoes nil
1793 "Whether to process echoes from the remote shell.")
1795 (defvar tramp-last-cmd-time nil
1796 "Internal Tramp variable recording the time when the last cmd was sent.
1797 This variable is buffer-local in every buffer.")
1798 (make-variable-buffer-local 'tramp-last-cmd-time)
1800 ;; This variable does not have the right value in XEmacs. What should
1801 ;; I use instead of find-operation-coding-system in XEmacs?
1802 (defvar tramp-feature-write-region-fix
1803 (when (fboundp 'find-operation-coding-system)
1804 (let ((file-coding-system-alist '(("test" emacs-mule))))
1805 (funcall (symbol-function 'find-operation-coding-system)
1806 'write-region 0 0 "" nil "test")))
1807 "Internal variable to say if `write-region' chooses the right coding.
1808 Older versions of Emacs chose the coding system for `write-region' based
1809 on the FILENAME argument, even if VISIT was a string.")
1811 ;; New handlers should be added here. The following operations can be
1812 ;; handled using the normal primitives: file-name-as-directory,
1813 ;; file-name-directory, file-name-nondirectory,
1814 ;; file-name-sans-versions, get-file-buffer.
1815 (defconst tramp-file-name-handler-alist
1817 (load . tramp-handle-load)
1818 (make-symbolic-link . tramp-handle-make-symbolic-link)
1819 (file-name-directory . tramp-handle-file-name-directory)
1820 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1821 (file-truename . tramp-handle-file-truename)
1822 (file-exists-p . tramp-handle-file-exists-p)
1823 (file-directory-p . tramp-handle-file-directory-p)
1824 (file-executable-p . tramp-handle-file-executable-p)
1825 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1826 (file-readable-p . tramp-handle-file-readable-p)
1827 (file-regular-p . tramp-handle-file-regular-p)
1828 (file-symlink-p . tramp-handle-file-symlink-p)
1829 (file-writable-p . tramp-handle-file-writable-p)
1830 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1831 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1832 (file-attributes . tramp-handle-file-attributes)
1833 (file-modes . tramp-handle-file-modes)
1834 (directory-files . tramp-handle-directory-files)
1835 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1836 (file-name-all-completions . tramp-handle-file-name-all-completions)
1837 (file-name-completion . tramp-handle-file-name-completion)
1838 (add-name-to-file . tramp-handle-add-name-to-file)
1839 (copy-file . tramp-handle-copy-file)
1840 (rename-file . tramp-handle-rename-file)
1841 (set-file-modes . tramp-handle-set-file-modes)
1842 (make-directory . tramp-handle-make-directory)
1843 (delete-directory . tramp-handle-delete-directory)
1844 (delete-file . tramp-handle-delete-file)
1845 (directory-file-name . tramp-handle-directory-file-name)
1846 (shell-command . tramp-handle-shell-command)
1847 (process-file . tramp-handle-process-file)
1848 (insert-directory . tramp-handle-insert-directory)
1849 (expand-file-name . tramp-handle-expand-file-name)
1850 (file-local-copy . tramp-handle-file-local-copy)
1851 (file-remote-p . tramp-handle-file-remote-p)
1852 (insert-file-contents . tramp-handle-insert-file-contents)
1853 (write-region . tramp-handle-write-region)
1854 (find-backup-file-name . tramp-handle-find-backup-file-name)
1855 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1856 (dired-compress-file . tramp-handle-dired-compress-file)
1857 (dired-call-process . tramp-handle-dired-call-process)
1858 (dired-recursive-delete-directory
1859 . tramp-handle-dired-recursive-delete-directory)
1860 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1861 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1862 "Alist of handler functions.
1863 Operations not mentioned here will be handled by the normal Emacs functions.")
1865 ;; Handlers for partial tramp file names. For GNU Emacs just
1866 ;; `file-name-all-completions' is needed. The other ones are necessary
1868 (defconst tramp-completion-file-name-handler-alist
1870 (file-name-directory . tramp-completion-handle-file-name-directory)
1871 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1872 (file-exists-p . tramp-completion-handle-file-exists-p)
1873 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1874 (file-name-completion . tramp-completion-handle-file-name-completion)
1875 (expand-file-name . tramp-completion-handle-expand-file-name))
1876 "Alist of completion handler functions.
1877 Used for file names matching `tramp-file-name-regexp'. Operations not
1878 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1879 normal Emacs functions.")
1881 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1882 (defvar tramp-foreign-file-name-handler-alist
1883 ;; (identity . tramp-sh-file-name-handler) should always be the last
1884 ;; entry, since `identity' always matches.
1885 '((identity . tramp-sh-file-name-handler))
1886 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1887 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1890 ;;; Internal functions which must come first.
1892 (defsubst tramp-message (level fmt-string &rest args)
1893 "Emit a message depending on verbosity level.
1894 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1895 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1896 Calls function `message' with FMT-STRING as control string and the remaining
1897 ARGS to actually emit the message (if applicable).
1899 This function expects to be called from the tramp buffer only!"
1900 (when (<= level tramp-verbose)
1901 (apply #'message (concat "tramp: " fmt-string) args)
1902 (when tramp-debug-buffer
1905 (tramp-get-debug-buffer
1906 tramp-current-multi-method tramp-current-method
1907 tramp-current-user tramp-current-host))
1908 (goto-char (point-max))
1909 (tramp-insert-with-face
1911 (concat "# " (apply #'format fmt-string args) "\n"))))))
1913 (defun tramp-message-for-buffer
1914 (multi-method method user host level fmt-string &rest args)
1915 "Like `tramp-message' but temporarily switches to the tramp buffer.
1916 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1917 remaining args passed to `tramp-message'."
1919 (set-buffer (tramp-get-buffer multi-method method user host))
1920 (apply 'tramp-message level fmt-string args)))
1922 (defsubst tramp-line-end-position nil
1923 "Return point at end of line.
1924 Calls `line-end-position' or `point-at-eol' if defined, else
1925 own implementation."
1927 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1928 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1929 (t (save-excursion (end-of-line) (point)))))
1931 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1932 "Parse a Tramp filename and make components available in the body.
1934 First arg FILENAME is evaluated and dissected into its components.
1935 Second arg VAR is a symbol. It is used as a variable name to hold
1936 the filename structure. It is also used as a prefix for the variables
1937 holding the components. For example, if VAR is the symbol `foo', then
1938 `foo' will be bound to the whole structure, `foo-multi-method' will
1939 be bound to the multi-method component, and so on for `foo-method',
1940 `foo-user', `foo-host', `foo-localname'.
1942 Remaining args are Lisp expressions to be evaluated (inside an implicit
1945 If VAR is nil, then we bind `v' to the structure and `multi-method',
1946 `method', `user', `host', `localname' to the components."
1947 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1948 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1949 (tramp-file-name-multi-method ,(or var 'v)))
1950 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1951 (tramp-file-name-method ,(or var 'v)))
1952 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1953 (tramp-file-name-user ,(or var 'v)))
1954 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1955 (tramp-file-name-host ,(or var 'v)))
1956 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1957 (tramp-file-name-localname ,(or var 'v))))
1960 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1961 ;; To be activated for debugging containing this macro
1962 ;; It works only when VAR is nil. Otherwise, it can be deactivated by
1963 ;; (def-edebug-spec with-parsed-tramp-file-name 0)
1964 ;; I'm too stupid to write a precise SPEC for it.
1965 (if (functionp 'def-edebug-spec)
1966 (def-edebug-spec with-parsed-tramp-file-name t))
1968 (defmacro tramp-let-maybe (variable value &rest body)
1969 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
1970 BODY is executed whether or not the variable is obsolete.
1971 The intent is to protect against `obsolete variable' warnings."
1972 `(if (get ',variable 'byte-obsolete-variable)
1974 (let ((,variable ,value))
1976 (put 'tramp-let-maybe 'lisp-indent-function 2)
1978 ;;; Config Manipulation Functions:
1980 (defun tramp-set-completion-function (method function-list)
1981 "Sets the list of completion functions for METHOD.
1982 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1983 The FUNCTION is intended to parse FILE according its syntax.
1984 It might be a predefined FUNCTION, or a user defined FUNCTION.
1985 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
1986 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
1987 and `tramp-parse-netrc'.
1991 (tramp-set-completion-function
1993 '((tramp-parse-sconfig \"/etc/ssh_config\")
1994 (tramp-parse-sconfig \"~/.ssh/config\")))"
1996 (let ((r function-list)
1998 (setq tramp-completion-function-alist
1999 (delete (assoc method tramp-completion-function-alist)
2000 tramp-completion-function-alist))
2003 ;; Remove double entries
2004 (when (member (car v) (cdr v))
2005 (setcdr v (delete (car v) (cdr v))))
2006 ;; Check for function and file
2007 (unless (and (functionp (nth 0 (car v)))
2008 (file-exists-p (nth 1 (car v))))
2009 (setq r (delete (car v) r)))
2013 (add-to-list 'tramp-completion-function-alist
2016 (defun tramp-get-completion-function (method)
2017 "Returns list of completion functions for METHOD.
2018 For definition of that list see `tramp-set-completion-function'."
2019 (cdr (assoc method tramp-completion-function-alist)))
2021 ;;; File Name Handler Functions:
2023 (defun tramp-handle-make-symbolic-link
2024 (filename linkname &optional ok-if-already-exists)
2025 "Like `make-symbolic-link' for tramp files.
2026 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2027 the symlink. If LINKNAME is a Tramp file, only the localname component is
2028 used as the target of the symlink.
2030 If LINKNAME is a Tramp file and the localname component is relative, then
2031 it is expanded first, before the localname component is taken. Note that
2032 this can give surprising results if the user/host for the source and
2033 target of the symlink differ."
2034 (with-parsed-tramp-file-name linkname l
2035 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2036 (cwd (file-name-directory l-localname)))
2039 (list "Making a symbolic link."
2040 "ln(1) does not exist on the remote host.")))
2042 ;; Do the 'confirm if exists' thing.
2043 (when (file-exists-p linkname)
2045 (if (or (null ok-if-already-exists) ; not allowed to exist
2046 (and (numberp ok-if-already-exists)
2049 "File %s already exists; make it a link anyway? "
2051 (signal 'file-already-exists (list "File already exists" l-localname))
2052 (delete-file linkname)))
2054 ;; If FILENAME is a Tramp name, use just the localname component.
2055 (when (tramp-tramp-file-p filename)
2056 (setq filename (tramp-file-name-localname
2057 (tramp-dissect-file-name
2058 (expand-file-name filename)))))
2060 ;; Right, they are on the same host, regardless of user, method, etc.
2061 ;; We now make the link on the remote machine. This will occur as the user
2062 ;; that FILENAME belongs to.
2064 (tramp-send-command-and-check
2065 l-multi-method l-method l-user l-host
2066 (format "cd %s && %s -sf %s %s"
2073 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2074 "Like `load' for tramp files. Not implemented!"
2075 (unless (file-name-absolute-p file)
2076 (error "Tramp cannot `load' files without absolute file name"))
2077 (with-parsed-tramp-file-name file nil
2079 (cond ((file-exists-p (concat file ".elc"))
2080 (setq file (concat file ".elc")))
2081 ((file-exists-p (concat file ".el"))
2082 (setq file (concat file ".el")))))
2084 ;; The first condition is always true for absolute file names.
2085 ;; Included for safety's sake.
2086 (unless (or (file-name-directory file)
2087 (string-match "\\.elc?\\'" file))
2088 (error "File `%s' does not include a `.el' or `.elc' suffix"
2091 (when (not (file-exists-p file))
2092 (error "Cannot load nonexistant file `%s'" file)))
2093 (if (not (file-exists-p file))
2096 (message "Loading %s..." file))
2097 (let ((local-copy (file-local-copy file)))
2098 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2099 (load local-copy noerror t t)
2100 (delete-file local-copy))
2102 (message "Loading %s...done" file))
2105 ;; Localname manipulation functions that grok TRAMP localnames...
2106 (defun tramp-handle-file-name-directory (file)
2107 "Like `file-name-directory' but aware of TRAMP files."
2108 ;; everything except the last filename thing is the directory
2109 (with-parsed-tramp-file-name file nil
2110 ;; For the following condition, two possibilities should be tried:
2111 ;; (1) (string= localname "")
2112 ;; (2) (or (string= localname "") (string= localname "/"))
2113 ;; The second variant fails when completing a "/" directory on
2114 ;; the remote host, that is a filename which looks like
2115 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2116 ;; We should do some investigation.
2117 (if (string= localname "")
2118 ;; For a filename like "/[foo]", we return "/". The `else'
2119 ;; case would return "/[foo]" unchanged. But if we do that,
2120 ;; then `file-expand-wildcards' ceases to work. It's not
2121 ;; quite clear to me what's the intuition that tells that this
2122 ;; behavior is the right behavior, but oh, well.
2124 ;; run the command on the localname portion only
2125 ;; CCC: This should take into account the remote machine type, no?
2126 ;; --daniel <daniel@danann.net>
2127 (tramp-make-tramp-file-name multi-method method user host
2128 ;; This will not recurse...
2129 (or (file-name-directory localname) "")))))
2131 (defun tramp-handle-file-name-nondirectory (file)
2132 "Like `file-name-nondirectory' but aware of TRAMP files."
2133 (with-parsed-tramp-file-name file nil
2134 (file-name-nondirectory localname)))
2136 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2137 "Like `file-truename' for tramp files."
2138 (with-parsed-tramp-file-name (expand-file-name filename) nil
2139 (let* ((steps (tramp-split-string localname "/"))
2140 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2141 (file-name-as-directory localname)))
2142 (is-dir (string= localname localnamedir))
2145 ;; Don't make the following value larger than necessary.
2146 ;; People expect an error message in a timely fashion when
2147 ;; something is wrong; otherwise they might think that Emacs
2148 ;; is hung. Of course, correctness has to come first.
2150 (result nil) ;result steps in reverse order
2152 (tramp-message-for-buffer
2153 multi-method method user host
2154 10 "Finding true name for `%s'" filename)
2155 (while (and steps (< numchase numchase-limit))
2156 (setq thisstep (pop steps))
2157 (tramp-message-for-buffer
2158 multi-method method user host
2160 (mapconcat 'identity
2161 (append '("") (reverse result) (list thisstep))
2163 (setq symlink-target
2164 (nth 0 (file-attributes
2165 (tramp-make-tramp-file-name
2166 multi-method method user host
2167 (mapconcat 'identity
2172 (cond ((string= "." thisstep)
2173 (tramp-message-for-buffer multi-method method user host
2174 10 "Ignoring step `.'"))
2175 ((string= ".." thisstep)
2176 (tramp-message-for-buffer multi-method method user host
2177 10 "Processing step `..'")
2179 ((stringp symlink-target)
2180 ;; It's a symlink, follow it.
2181 (tramp-message-for-buffer
2182 multi-method method user host
2183 10 "Follow symlink to %s" symlink-target)
2184 (setq numchase (1+ numchase))
2185 (when (file-name-absolute-p symlink-target)
2187 ;; If the symlink was absolute, we'll get a string like
2188 ;; "/user@host:/some/target"; extract the
2189 ;; "/some/target" part from it.
2190 (when (tramp-tramp-file-p symlink-target)
2191 (with-parsed-tramp-file-name symlink-target sym
2192 (unless (equal (list multi-method method user host)
2193 (list sym-multi-method sym-method
2195 (error "Symlink target `%s' on wrong host"
2197 (setq symlink-target localname)))
2199 (append (tramp-split-string symlink-target "/") steps)))
2202 (setq result (cons thisstep result)))))
2203 (when (>= numchase numchase-limit)
2204 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2205 (setq result (reverse result))
2206 ;; Combine list to form string.
2209 (mapconcat 'identity (cons "" result) "/")
2211 (when (and is-dir (or (string= "" result)
2212 (not (string= (substring result -1) "/"))))
2213 (setq result (concat result "/")))
2214 (tramp-message-for-buffer
2215 multi-method method user host
2216 10 "True name of `%s' is `%s'" filename result)
2217 (tramp-make-tramp-file-name
2218 multi-method method user host result))))
2222 (defun tramp-handle-file-exists-p (filename)
2223 "Like `file-exists-p' for tramp files."
2224 (with-parsed-tramp-file-name filename nil
2226 (zerop (tramp-send-command-and-check
2227 multi-method method user host
2229 (tramp-get-file-exists-command multi-method method user host)
2230 (tramp-shell-quote-argument localname)))))))
2232 ;; Devices must distinguish physical file systems. The device numbers
2233 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2234 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2235 ;; EFS use device number "-1". In order to be different, we use device number
2236 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2237 (defvar tramp-devices nil
2238 "Keeps virtual device numbers.")
2240 ;; CCC: This should check for an error condition and signal failure
2241 ;; when something goes wrong.
2242 ;; Daniel Pittman <daniel@danann.net>
2243 (defun tramp-handle-file-attributes (filename &optional id-format)
2244 "Like `file-attributes' for tramp files."
2245 (when (file-exists-p filename)
2246 ;; file exists, find out stuff
2247 (unless id-format (setq id-format 'integer))
2248 (with-parsed-tramp-file-name filename nil
2250 (tramp-convert-file-attributes
2251 multi-method method user host
2252 (if (tramp-get-remote-perl multi-method method user host)
2253 (tramp-handle-file-attributes-with-perl multi-method method user host
2254 localname id-format)
2255 (tramp-handle-file-attributes-with-ls multi-method method user host
2256 localname id-format)))))))
2258 (defun tramp-handle-file-attributes-with-ls
2259 (multi-method method user host localname &optional id-format)
2260 "Implement `file-attributes' for tramp files using the ls(1) command."
2262 res-inode res-filemodes res-numlinks
2263 res-uid res-gid res-size res-symlink-target)
2264 (tramp-message-for-buffer multi-method method user host 10
2265 "file attributes with ls: %s"
2266 (tramp-make-tramp-file-name
2267 multi-method method user host localname))
2269 multi-method method user host
2271 (tramp-get-ls-command multi-method method user host)
2272 (if (eq id-format 'integer) "-ildn" "-ild")
2273 (tramp-shell-quote-argument localname)))
2274 (tramp-wait-for-output)
2275 ;; parse `ls -l' output ...
2279 (read (current-buffer))
2280 (invalid-read-syntax
2281 (when (and (equal (cadr err)
2282 "Integer constant overflow in reader")
2284 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2286 (let* ((big (read (substring (caddr err) 0
2287 (match-beginning 1))))
2288 (small (read (match-string 1 (caddr err))))
2289 (twiddle (/ small 65536)))
2290 (cons (+ big twiddle)
2291 (- small (* twiddle 65536))))))))
2292 ;; ... file mode flags
2293 (setq res-filemodes (symbol-name (read (current-buffer))))
2295 (setq res-numlinks (read (current-buffer)))
2297 (setq res-uid (read (current-buffer)))
2298 (setq res-gid (read (current-buffer)))
2299 (when (eq id-format 'integer)
2300 (unless (numberp res-uid) (setq res-uid -1))
2301 (unless (numberp res-gid) (setq res-gid -1)))
2303 (setq res-size (read (current-buffer)))
2304 ;; From the file modes, figure out other stuff.
2305 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2306 (setq dirp (eq ?d (aref res-filemodes 0)))
2307 ;; if symlink, find out file name pointed to
2309 (search-forward "-> ")
2310 (setq res-symlink-target
2311 (buffer-substring (point)
2312 (tramp-line-end-position))))
2313 ;; return data gathered
2315 ;; 0. t for directory, string (name linked to) for symbolic
2317 (or dirp res-symlink-target nil)
2318 ;; 1. Number of links to file.
2324 ;; 4. Last access time, as a list of two integers. First
2325 ;; integer has high-order 16 bits of time, second has low 16
2327 ;; 5. Last modification time, likewise.
2328 ;; 6. Last status change time, likewise.
2329 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2330 ;; 7. Size in bytes (-1, if number is out of range).
2332 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2334 ;; 9. t iff file's gid would change if file were deleted and
2337 ;; 10. inode number.
2339 ;; 11. Device number. Will be replaced by a virtual device number.
2343 (defun tramp-handle-file-attributes-with-perl
2344 (multi-method method user host localname &optional id-format)
2345 "Implement `file-attributes' for tramp files using a Perl script."
2346 (tramp-message-for-buffer multi-method method user host 10
2347 "file attributes with perl: %s"
2348 (tramp-make-tramp-file-name
2349 multi-method method user host localname))
2350 (tramp-maybe-send-perl-script tramp-perl-file-attributes
2351 "tramp_file_attributes"
2352 multi-method method user host)
2353 (tramp-send-command multi-method method user host
2354 (format "tramp_file_attributes %s %s"
2355 (tramp-shell-quote-argument localname) id-format))
2356 (tramp-wait-for-output)
2357 (read (current-buffer)))
2359 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2360 "Like `set-visited-file-modtime' for tramp files."
2361 (unless (buffer-file-name)
2362 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2365 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2366 (let ((f (buffer-file-name))
2367 (coding-system-used nil))
2368 (with-parsed-tramp-file-name f nil
2369 (let* ((attr (file-attributes f))
2370 ;; '(-1 65535) means file doesn't exists yet.
2371 (modtime (or (nth 5 attr) '(-1 65535))))
2372 ;; We use '(0 0) as a don't-know value. See also
2373 ;; `tramp-handle-file-attributes-with-ls'.
2374 (when (boundp 'last-coding-system-used)
2375 (setq coding-system-used last-coding-system-used))
2376 (if (not (equal modtime '(0 0)))
2377 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2380 multi-method method user host
2381 (format "%s -ild %s"
2382 (tramp-get-ls-command multi-method method user host)
2383 (tramp-shell-quote-argument localname)))
2384 (tramp-wait-for-output)
2385 (setq attr (buffer-substring (point)
2386 (progn (end-of-line) (point)))))
2387 (setq tramp-buffer-file-attributes attr))
2388 (when (boundp 'last-coding-system-used)
2389 (setq last-coding-system-used coding-system-used))
2392 ;; CCC continue here
2394 ;; This function makes the same assumption as
2395 ;; `tramp-handle-set-visited-file-modtime'.
2396 (defun tramp-handle-verify-visited-file-modtime (buf)
2397 "Like `verify-visited-file-modtime' for tramp files."
2398 (with-current-buffer buf
2399 ;; There is no file visiting the buffer, or the buffer has no
2400 ;; recorded last modification time.
2401 (if (or (not (buffer-file-name))
2402 (eq (visited-file-modtime) 0))
2404 (let ((f (buffer-file-name)))
2405 (with-parsed-tramp-file-name f nil
2406 (let* ((attr (file-attributes f))
2407 (modtime (nth 5 attr))
2408 (mt (visited-file-modtime)))
2411 ;; file exists, and has a known modtime.
2412 ((and attr (not (equal modtime '(0 0))))
2413 (< (abs (tramp-time-diff
2415 ;; For compatibility, deal with both the old
2416 ;; (HIGH . LOW) and the new (HIGH LOW)
2417 ;; return values of `visited-file-modtime'.
2419 (list (car mt) (cdr mt))
2422 ;; modtime has the don't know value.
2426 multi-method method user host
2427 (format "%s -ild %s"
2428 (tramp-get-ls-command multi-method method user host)
2429 (tramp-shell-quote-argument localname)))
2430 (tramp-wait-for-output)
2431 (setq attr (buffer-substring
2432 (point) (progn (end-of-line) (point)))))
2433 (equal tramp-buffer-file-attributes attr))
2434 ;; If file does not exist, say it is not modified
2435 ;; if and only if that agrees with the buffer's record.
2436 (t (equal mt '(-1 65535))))))))))
2438 (defadvice clear-visited-file-modtime (after tramp activate)
2439 "Set `tramp-buffer-file-attributes' back to nil.
2440 Tramp uses this variable as an emulation for the actual modtime of the file,
2441 if the remote host can't provide the modtime."
2442 (setq tramp-buffer-file-attributes nil))
2444 (defun tramp-handle-set-file-modes (filename mode)
2445 "Like `set-file-modes' for tramp files."
2446 (with-parsed-tramp-file-name filename nil
2448 (unless (zerop (tramp-send-command-and-check
2449 multi-method method user host
2450 (format "chmod %s %s"
2451 (tramp-decimal-to-octal mode)
2452 (tramp-shell-quote-argument localname))))
2455 ;; FIXME: extract the proper text from chmod's stderr.
2456 "error while changing file's mode"
2459 ;; Simple functions using the `test' command.
2461 (defun tramp-handle-file-executable-p (filename)
2462 "Like `file-executable-p' for tramp files."
2463 (with-parsed-tramp-file-name filename nil
2464 (zerop (tramp-run-test "-x" filename))))
2466 (defun tramp-handle-file-readable-p (filename)
2467 "Like `file-readable-p' for tramp files."
2468 (with-parsed-tramp-file-name filename nil
2469 (zerop (tramp-run-test "-r" filename))))
2471 (defun tramp-handle-file-accessible-directory-p (filename)
2472 "Like `file-accessible-directory-p' for tramp files."
2473 (with-parsed-tramp-file-name filename nil
2474 (and (zerop (tramp-run-test "-d" filename))
2475 (zerop (tramp-run-test "-r" filename))
2476 (zerop (tramp-run-test "-x" filename)))))
2478 ;; When the remote shell is started, it looks for a shell which groks
2479 ;; tilde expansion. Here, we assume that all shells which grok tilde
2480 ;; expansion will also provide a `test' command which groks `-nt' (for
2481 ;; newer than). If this breaks, tell me about it and I'll try to do
2482 ;; something smarter about it.
2483 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2484 "Like `file-newer-than-file-p' for tramp files."
2485 (cond ((not (file-exists-p file1))
2487 ((not (file-exists-p file2))
2489 ;; We are sure both files exist at this point.
2492 ;; We try to get the mtime of both files. If they are not
2493 ;; equal to the "dont-know" value, then we subtract the times
2494 ;; and obtain the result.
2495 (let ((fa1 (file-attributes file1))
2496 (fa2 (file-attributes file2)))
2497 (if (and (not (equal (nth 5 fa1) '(0 0)))
2498 (not (equal (nth 5 fa2) '(0 0))))
2499 (< 0 (tramp-time-diff (nth 5 fa1) (nth 5 fa2)))
2500 ;; If one of them is the dont-know value, then we can
2501 ;; still try to run a shell command on the remote host.
2502 ;; However, this only works if both files are Tramp
2503 ;; files and both have the same method, same user, same
2505 (unless (and (tramp-tramp-file-p file1)
2506 (tramp-tramp-file-p file2))
2510 "Cannot check if Tramp file is newer than non-Tramp file"
2512 (with-parsed-tramp-file-name file1 v1
2513 (with-parsed-tramp-file-name file2 v2
2514 (unless (and (equal v1-multi-method v2-multi-method)
2515 (equal v1-method v2-method)
2516 (equal v1-user v2-user)
2517 (equal v1-host v2-host))
2519 (list "Files must have same method, user, host"
2521 (unless (and (tramp-tramp-file-p file1)
2522 (tramp-tramp-file-p file2))
2524 (list "Files must be tramp files on same host"
2526 (if (tramp-get-test-groks-nt
2527 v1-multi-method v1-method v1-user v1-host)
2528 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2529 (zerop (tramp-run-test2
2530 "tramp_test_nt" file1 file2)))))))))))
2532 ;; Functions implemented using the basic functions above.
2534 (defun tramp-handle-file-modes (filename)
2535 "Like `file-modes' for tramp files."
2536 (with-parsed-tramp-file-name filename nil
2537 (when (file-exists-p filename)
2538 (tramp-mode-string-to-int
2539 (nth 8 (file-attributes filename))))))
2541 (defun tramp-handle-file-directory-p (filename)
2542 "Like `file-directory-p' for tramp files."
2543 ;; Care must be taken that this function returns `t' for symlinks
2544 ;; pointing to directories. Surely the most obvious implementation
2545 ;; would be `test -d', but that returns false for such symlinks.
2546 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2547 ;; I now think he's right. So we could be using `test -d', couldn't
2550 ;; Alternatives: `cd %s', `test -d %s'
2551 (with-parsed-tramp-file-name filename nil
2554 (tramp-send-command-and-check
2555 multi-method method user host
2556 (format "test -d %s"
2557 (tramp-shell-quote-argument localname))
2558 t))))) ;run command in subshell
2560 (defun tramp-handle-file-regular-p (filename)
2561 "Like `file-regular-p' for tramp files."
2562 (with-parsed-tramp-file-name filename nil
2563 (and (file-exists-p filename)
2564 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2566 (defun tramp-handle-file-symlink-p (filename)
2567 "Like `file-symlink-p' for tramp files."
2568 (with-parsed-tramp-file-name filename nil
2569 (let ((x (car (file-attributes filename))))
2571 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2572 ;; might do weird things.
2573 (if (file-name-absolute-p x)
2574 (tramp-make-tramp-file-name
2575 multi-method method user host x)
2578 (defun tramp-handle-file-writable-p (filename)
2579 "Like `file-writable-p' for tramp files."
2580 (with-parsed-tramp-file-name filename nil
2581 (if (file-exists-p filename)
2582 ;; Existing files must be writable.
2583 (zerop (tramp-run-test "-w" filename))
2584 ;; If file doesn't exist, check if directory is writable.
2585 (and (zerop (tramp-run-test
2586 "-d" (file-name-directory filename)))
2587 (zerop (tramp-run-test
2588 "-w" (file-name-directory filename)))))))
2590 (defun tramp-handle-file-ownership-preserved-p (filename)
2591 "Like `file-ownership-preserved-p' for tramp files."
2592 (with-parsed-tramp-file-name filename nil
2593 (or (not (file-exists-p filename))
2594 ;; Existing files must be writable.
2595 (zerop (tramp-run-test "-O" filename)))))
2597 ;; Other file name ops.
2599 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2600 ;; (defun tramp-handle-directory-file-name (directory)
2601 ;; "Like `directory-file-name' for tramp files."
2602 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2603 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2604 ;; (substring directory 0 (- (length directory) 1))
2607 ;; ;; Philippe Troin <phil@fifi.org>
2608 ;; (defun tramp-handle-directory-file-name (directory)
2609 ;; "Like `directory-file-name' for tramp files."
2610 ;; (with-parsed-tramp-file-name directory nil
2611 ;; (let ((directory-length-1 (1- (length directory))))
2613 ;; (if (and (eq (aref directory directory-length-1) ?/)
2614 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2615 ;; (/= (match-end 0) directory-length-1))
2616 ;; (substring directory 0 directory-length-1)
2619 (defun tramp-handle-directory-file-name (directory)
2620 "Like `directory-file-name' for tramp files."
2621 ;; If localname component of filename is "/", leave it unchanged.
2622 ;; Otherwise, remove any trailing slash from localname component.
2623 ;; Method, host, etc, are unchanged. Does it make sense to try
2624 ;; to avoid parsing the filename?
2625 (with-parsed-tramp-file-name directory nil
2626 (if (and (not (zerop (length localname)))
2627 (eq (aref localname (1- (length localname))) ?/)
2628 (not (string= localname "/")))
2629 (substring directory 0 -1)
2632 ;; Directory listings.
2634 (defun tramp-handle-directory-files (directory
2635 &optional full match nosort files-only)
2636 "Like `directory-files' for tramp files."
2637 (with-parsed-tramp-file-name directory nil
2640 (tramp-barf-unless-okay
2641 multi-method method user host
2642 (concat "cd " (tramp-shell-quote-argument localname))
2645 "tramp-handle-directory-files: couldn't `cd %s'"
2646 (tramp-shell-quote-argument localname))
2648 multi-method method user host
2649 (concat (tramp-get-ls-command multi-method method user host)
2651 (tramp-wait-for-output)
2652 (goto-char (point-max))
2653 (while (zerop (forward-line -1))
2654 (setq x (buffer-substring (point)
2655 (tramp-line-end-position)))
2656 (when (or (not match) (string-match match x))
2658 (push (concat (file-name-as-directory directory)
2662 (tramp-send-command multi-method method user host "cd")
2663 (tramp-wait-for-output)
2664 ;; Remove non-files or non-directories if necessary. Using
2665 ;; the remote shell for this would probably be way faster.
2666 ;; Maybe something could be adapted from
2667 ;; tramp-handle-file-name-all-completions.
2669 (let ((temp (nreverse result))
2672 (if (equal files-only t)
2675 (setq item (pop temp))
2676 (when (file-regular-p item)
2677 (push item result)))
2680 (setq item (pop temp))
2681 (when (file-directory-p item)
2682 (push item result)))))))
2685 (defun tramp-handle-directory-files-and-attributes
2686 (directory &optional full match nosort id-format)
2687 "Like `directory-files-and-attributes' for tramp files."
2688 (when (tramp-handle-file-exists-p directory)
2690 (setq directory (tramp-handle-expand-file-name directory))
2691 (with-parsed-tramp-file-name directory nil
2692 (tramp-maybe-send-perl-script tramp-perl-directory-files-and-attributes
2693 "tramp_directory_files_and_attributes"
2694 multi-method method user host)
2695 (tramp-send-command multi-method method user host
2696 (format "tramp_directory_files_and_attributes %s %s"
2697 (tramp-shell-quote-argument localname)
2698 (or id-format 'integer)))
2699 (tramp-wait-for-output)
2700 (let* ((root (cons nil (read (current-buffer))))
2703 (if (and match (not (string-match match (caadr cell))))
2705 (setcdr cell (cddr cell))
2707 (setq cell (cdr cell))
2708 (let ((l (car cell)))
2709 (tramp-convert-file-attributes multi-method method user host
2711 ;; If FULL, make file name absolute
2712 (when full (setcar l (concat directory "/" (car l)))))))
2715 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2717 ;; This function should return "foo/" for directories and "bar" for
2718 ;; files. We use `ls -ad' to get a list of files (including
2719 ;; directories), and `find . -type d \! -name . -prune' to get a list
2721 (defun tramp-handle-file-name-all-completions (filename directory)
2722 "Like `file-name-all-completions' for tramp files."
2723 (with-parsed-tramp-file-name directory nil
2724 (unless (save-match-data (string-match "/" filename))
2725 (let* ((nowild tramp-completion-without-shell-p)
2728 (tramp-barf-unless-okay
2729 multi-method method user host
2730 (format "cd %s" (tramp-shell-quote-argument localname))
2732 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2733 (tramp-shell-quote-argument localname))
2735 ;; Get a list of directories and files, including reliably
2736 ;; tagging the directories with a trailing '/'. Because I
2737 ;; rock. --daniel@danann.net
2739 multi-method method user host
2740 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2741 "if test -d \"$f\" 2>/dev/null; "
2742 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2743 (tramp-get-ls-command multi-method method user host)
2744 (if (or nowild (zerop (length filename)))
2747 (tramp-shell-quote-argument filename)))))
2749 ;; Now grab the output.
2750 (tramp-wait-for-output)
2751 (goto-char (point-max))
2752 (while (zerop (forward-line -1))
2753 (push (buffer-substring (point)
2754 (tramp-line-end-position))
2757 (tramp-send-command multi-method method user host "cd")
2758 (tramp-wait-for-output)
2762 (all-completions filename (mapcar 'list result))
2766 ;; The following isn't needed for Emacs 20 but for 19.34?
2767 (defun tramp-handle-file-name-completion (filename directory)
2768 "Like `file-name-completion' for tramp files."
2769 (unless (tramp-tramp-file-p directory)
2771 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2773 (with-parsed-tramp-file-name directory nil
2776 (mapcar (lambda (x) (cons x nil))
2777 (file-name-all-completions filename directory)))))
2781 (defun tramp-handle-add-name-to-file
2782 (filename newname &optional ok-if-already-exists)
2783 "Like `add-name-to-file' for tramp files."
2784 (with-parsed-tramp-file-name filename v1
2785 (with-parsed-tramp-file-name newname v2
2786 (let ((ln (when v1 (tramp-get-remote-ln
2787 v1-multi-method v1-method v1-user v1-host))))
2788 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2789 (equal v1-multi-method v2-multi-method)
2790 (equal v1-method v2-method)
2791 (equal v1-user v2-user)
2792 (equal v1-host v2-host))
2793 (error "add-name-to-file: %s"
2794 "only implemented for same method, same user, same host"))
2795 (when (and (not ok-if-already-exists)
2796 (file-exists-p newname)
2797 (not (numberp ok-if-already-exists))
2800 "File %s already exists; make it a new name anyway? "
2802 (error "add-name-to-file: file %s already exists" newname))
2803 (tramp-barf-unless-okay
2804 v1-multi-method v1-method v1-user v1-host
2805 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2806 (tramp-shell-quote-argument v2-localname))
2808 "error with add-name-to-file, see buffer `%s' for details"
2811 (defun tramp-handle-copy-file
2812 (filename newname &optional ok-if-already-exists keep-date)
2813 "Like `copy-file' for tramp files."
2814 ;; Check if both files are local -- invoke normal copy-file.
2815 ;; Otherwise, use tramp from local system.
2816 (setq filename (expand-file-name filename))
2817 (setq newname (expand-file-name newname))
2818 ;; At least one file a tramp file?
2819 (if (or (tramp-tramp-file-p filename)
2820 (tramp-tramp-file-p newname))
2821 (let ((modes (file-modes filename)))
2822 (tramp-do-copy-or-rename-file
2823 'copy filename newname ok-if-already-exists keep-date)
2824 (set-file-modes newname modes))
2825 (tramp-run-real-handler
2827 (list filename newname ok-if-already-exists keep-date))))
2829 (defun tramp-handle-rename-file
2830 (filename newname &optional ok-if-already-exists)
2831 "Like `rename-file' for tramp files."
2832 ;; Check if both files are local -- invoke normal rename-file.
2833 ;; Otherwise, use tramp from local system.
2834 (setq filename (expand-file-name filename))
2835 (setq newname (expand-file-name newname))
2836 ;; At least one file a tramp file?
2837 (if (or (tramp-tramp-file-p filename)
2838 (tramp-tramp-file-p newname))
2839 (tramp-do-copy-or-rename-file
2840 'rename filename newname ok-if-already-exists)
2841 (tramp-run-real-handler 'rename-file
2842 (list filename newname ok-if-already-exists))))
2844 (defun tramp-do-copy-or-rename-file
2845 (op filename newname &optional ok-if-already-exists keep-date)
2846 "Copy or rename a remote file.
2847 OP must be `copy' or `rename' and indicates the operation to perform.
2848 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2849 the new file (for copy) or the new name of the file (for rename).
2850 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2851 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2854 This function is invoked by `tramp-handle-copy-file' and
2855 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2856 and `rename'. FILENAME and NEWNAME must be absolute file names."
2857 (unless (memq op '(copy rename))
2858 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2859 (unless ok-if-already-exists
2860 (when (file-exists-p newname)
2861 (signal 'file-already-exists
2863 (let ((t1 (tramp-tramp-file-p filename))
2864 (t2 (tramp-tramp-file-p newname))
2865 v1-multi-method v1-method v1-user v1-host v1-localname
2866 v2-multi-method v2-method v2-user v2-host v2-localname)
2868 ;; Check which ones of source and target are Tramp files.
2869 ;; We cannot invoke `with-parsed-tramp-file-name';
2870 ;; it fails if the file isn't a Tramp file name.
2872 (with-parsed-tramp-file-name filename l
2873 (setq v1-multi-method l-multi-method
2877 v1-localname l-localname))
2878 (setq v1-localname filename))
2880 (with-parsed-tramp-file-name newname l
2881 (setq v2-multi-method l-multi-method
2885 v2-localname l-localname))
2886 (setq v2-localname newname))
2889 ;; Both are Tramp files.
2892 ;; Shortcut: if method, host, user are the same for both
2893 ;; files, we invoke `cp' or `mv' on the remote host
2895 ((and (equal v1-multi-method v2-multi-method)
2896 (equal v1-method v2-method)
2897 (equal v1-user v2-user)
2898 (equal v1-host v2-host))
2899 (tramp-do-copy-or-rename-file-directly
2900 op v1-multi-method v1-method v1-user v1-host
2901 v1-localname v2-localname keep-date))
2902 ;; If both source and target are Tramp files,
2903 ;; both are using the same copy-program, then we
2904 ;; can invoke rcp directly. Note that
2905 ;; default-directory should point to a local
2906 ;; directory if we want to invoke rcp.
2907 ((and (not v1-multi-method)
2908 (not v2-multi-method)
2909 (equal v1-method v2-method)
2910 (tramp-method-out-of-band-p
2911 v1-multi-method v1-method v1-user v1-host)
2912 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2913 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2914 (tramp-do-copy-or-rename-file-out-of-band
2915 op filename newname keep-date))
2916 ;; No shortcut was possible. So we copy the
2917 ;; file first. If the operation was `rename', we go
2918 ;; back and delete the original file (if the copy was
2919 ;; successful). The approach is simple-minded: we
2920 ;; create a new buffer, insert the contents of the
2921 ;; source file into it, then write out the buffer to
2922 ;; the target file. The advantage is that it doesn't
2923 ;; matter which filename handlers are used for the
2924 ;; source and target file.
2926 (tramp-do-copy-or-rename-file-via-buffer
2927 op filename newname keep-date))))
2929 ;; One file is a Tramp file, the other one is local.
2931 ;; If the Tramp file has an out-of-band method, the corresponding
2932 ;; copy-program can be invoked.
2933 (if (and (not v1-multi-method)
2934 (not v2-multi-method)
2935 (or (tramp-method-out-of-band-p
2936 v1-multi-method v1-method v1-user v1-host)
2937 (tramp-method-out-of-band-p
2938 v2-multi-method v2-method v2-user v2-host)))
2939 (tramp-do-copy-or-rename-file-out-of-band
2940 op filename newname keep-date)
2941 ;; Use the generic method via a Tramp buffer.
2942 (tramp-do-copy-or-rename-file-via-buffer
2943 op filename newname keep-date)))
2946 ;; One of them must be a Tramp file.
2947 (error "Tramp implementation says this cannot happen")))))
2949 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2950 "Use an Emacs buffer to copy or rename a file.
2951 First arg OP is either `copy' or `rename' and indicates the operation.
2952 FILENAME is the source file, NEWNAME the target file.
2953 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2954 (let ((trampbuf (get-buffer-create "*tramp output*"))
2955 (modtime (nth 5 (file-attributes filename))))
2956 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
2958 1 (concat "Warning: cannot preserve file time stamp"
2959 " with inline copying across machines")))
2961 (set-buffer trampbuf) (erase-buffer)
2962 (insert-file-contents-literally filename)
2963 ;; We don't want the target file to be compressed, so we let-bind
2964 ;; `jka-compr-inhibit' to t.
2965 (let ((coding-system-for-write 'binary)
2966 (jka-compr-inhibit t))
2967 (write-region (point-min) (point-max) newname))
2968 ;; KEEP-DATE handling.
2970 (when (and (not (null modtime))
2971 (not (equal modtime '(0 0))))
2972 (tramp-touch newname modtime))
2973 (set-file-modes newname (file-modes filename))))
2974 ;; If the operation was `rename', delete the original file.
2975 (unless (eq op 'copy)
2976 (delete-file filename))))
2978 (defun tramp-do-copy-or-rename-file-directly
2979 (op multi-method method user host localname1 localname2 keep-date)
2980 "Invokes `cp' or `mv' on the remote system.
2981 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2982 respectively. METHOD, USER, and HOST specify the connection.
2983 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
2984 If KEEP-DATE is non-nil, preserve the time stamp when copying."
2985 ;; CCC: What happens to the timestamp when renaming?
2986 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
2987 ((eq op 'copy) "cp -f")
2988 ((eq op 'rename) "mv -f")
2990 "Unknown operation `%s', must be `copy' or `rename'"
2993 (tramp-barf-unless-okay
2994 multi-method method user host
2997 (tramp-shell-quote-argument localname1)
2998 (tramp-shell-quote-argument localname2))
3000 "Copying directly failed, see buffer `%s' for details."
3003 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3004 "Invoke rcp program to copy.
3005 One of FILENAME and NEWNAME must be a Tramp name, the other must
3006 be a local filename. The method used must be an out-of-band method."
3007 (let ((t1 (tramp-tramp-file-p filename))
3008 (t2 (tramp-tramp-file-p newname))
3009 v1-multi-method v1-method v1-user v1-host v1-localname
3010 v2-multi-method v2-method v2-user v2-host v2-localname
3011 multi-method method user host copy-program copy-args
3012 source target trampbuf)
3014 ;; Check which ones of source and target are Tramp files.
3015 ;; We cannot invoke `with-parsed-tramp-file-name';
3016 ;; it fails if the file isn't a Tramp file name.
3018 (with-parsed-tramp-file-name filename l
3019 (setq v1-multi-method l-multi-method
3023 v1-localname l-localname
3024 multi-method l-multi-method
3025 method (tramp-find-method
3026 v1-multi-method v1-method v1-user v1-host)
3029 copy-program (tramp-get-method-parameter
3030 v1-multi-method method
3031 v1-user v1-host 'tramp-copy-program)
3032 copy-args (tramp-get-method-parameter
3033 v1-multi-method method
3034 v1-user v1-host 'tramp-copy-args)))
3035 (setq v1-localname filename))
3038 (with-parsed-tramp-file-name newname l
3039 (setq v2-multi-method l-multi-method
3043 v2-localname l-localname
3044 multi-method l-multi-method
3045 method (tramp-find-method
3046 v2-multi-method v2-method v2-user v2-host)
3049 copy-program (tramp-get-method-parameter
3050 v2-multi-method method
3051 v2-user v2-host 'tramp-copy-program)
3052 copy-args (tramp-get-method-parameter
3053 v2-multi-method method
3054 v2-user v2-host 'tramp-copy-args)))
3055 (setq v2-localname newname))
3057 ;; The following should be changed. We need a more general
3058 ;; mechanism to parse extra host args.
3060 (setq source v1-localname)
3061 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3062 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3063 (setq v1-host (match-string 1 v1-host)))
3065 (tramp-make-copy-program-file-name
3067 (tramp-shell-quote-argument v1-localname))))
3070 (setq target v2-localname)
3071 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3072 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3073 (setq v2-host (match-string 1 v2-host)))
3075 (tramp-make-copy-program-file-name
3077 (tramp-shell-quote-argument v2-localname))))
3079 ;; Handle keep-date argument
3083 (cons (tramp-get-method-parameter
3084 v1-multi-method method
3085 v1-user v1-host 'tramp-copy-keep-date-arg)
3088 (cons (tramp-get-method-parameter
3089 v2-multi-method method
3090 v2-user v2-host 'tramp-copy-keep-date-arg)
3093 (setq copy-args (append copy-args (list source target))
3094 trampbuf (generate-new-buffer
3095 (tramp-buffer-name multi-method method user host)))
3097 ;; Use an asynchronous process. By this, password can be handled.
3099 (set-buffer trampbuf)
3100 (setq tramp-current-multi-method multi-method
3101 tramp-current-method method
3102 tramp-current-user user
3103 tramp-current-host host)
3105 5 "Transferring %s to file %s..." filename newname)
3107 ;; Use rcp-like program for file transfer.
3108 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3109 copy-program copy-args)))
3110 (tramp-set-process-query-on-exit-flag p nil)
3111 (tramp-process-actions p multi-method method user host
3112 tramp-actions-copy-out-of-band))
3113 (kill-buffer trampbuf)
3115 5 "Transferring %s to file %s...done" filename newname))
3117 ;; If the operation was `rename', delete the original file.
3118 (unless (eq op 'copy)
3119 (delete-file filename))))
3122 (defun tramp-handle-make-directory (dir &optional parents)
3123 "Like `make-directory' for tramp files."
3124 (setq dir (expand-file-name dir))
3125 (with-parsed-tramp-file-name dir nil
3127 (tramp-barf-unless-okay
3128 multi-method method user host
3130 (if parents "mkdir -p" "mkdir")
3131 (tramp-shell-quote-argument localname))
3133 "Couldn't make directory %s" dir))))
3135 ;; CCC error checking?
3136 (defun tramp-handle-delete-directory (directory)
3137 "Like `delete-directory' for tramp files."
3138 (setq directory (expand-file-name directory))
3139 (with-parsed-tramp-file-name directory nil
3142 multi-method method user host
3143 (format "rmdir %s ; echo ok"
3144 (tramp-shell-quote-argument localname)))
3145 (tramp-wait-for-output))))
3147 (defun tramp-handle-delete-file (filename)
3148 "Like `delete-file' for tramp files."
3149 (setq filename (expand-file-name filename))
3150 (with-parsed-tramp-file-name filename nil
3152 (unless (zerop (tramp-send-command-and-check
3153 multi-method method user host
3155 (tramp-shell-quote-argument localname))))
3156 (signal 'file-error "Couldn't delete Tramp file")))))
3160 ;; CCC: This does not seem to be enough. Something dies when
3161 ;; we try and delete two directories under TRAMP :/
3162 (defun tramp-handle-dired-recursive-delete-directory (filename)
3163 "Recursively delete the directory given.
3164 This is like `dired-recursive-delete-directory' for tramp files."
3165 (with-parsed-tramp-file-name filename nil
3166 ;; run a shell command 'rm -r <localname>'
3167 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3168 (or (file-exists-p filename)
3171 (list "Removing old file name" "no such directory" filename)))
3172 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3173 (tramp-send-command multi-method method user host
3174 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3175 ;; Wait for the remote system to return to us...
3176 ;; This might take a while, allow it plenty of time.
3177 (tramp-wait-for-output 120)
3178 ;; Make sure that it worked...
3179 (and (file-exists-p filename)
3180 (error "Failed to recusively delete %s" filename))))
3182 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3183 "Like `dired-call-process' for tramp files."
3184 (with-parsed-tramp-file-name default-directory nil
3186 (tramp-barf-unless-okay
3187 multi-method method user host
3188 (format "cd %s" (tramp-shell-quote-argument localname))
3190 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3191 (tramp-shell-quote-argument localname))
3193 multi-method method user host
3194 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3195 (tramp-wait-for-output))
3197 (insert-buffer (tramp-get-buffer multi-method method user host)))
3200 (tramp-send-command-and-check multi-method method user host nil)
3201 (tramp-send-command multi-method method user host "cd")
3202 (tramp-wait-for-output)))))
3204 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3205 "Like `dired-compress-file' for tramp files."
3206 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3207 ;; Code stolen mainly from dired-aux.el.
3208 (with-parsed-tramp-file-name file nil
3211 (if (not (featurep 'xemacs))
3213 (symbol-value 'dired-compress-file-suffixes)
3214 ;; XEmacs has `dired-compression-method-alist', which is
3215 ;; transformed into `dired-compress-file-suffixes' structure.
3218 (list (concat (regexp-quote (nth 1 x)) "\\'")
3220 (mapconcat 'identity (nth 3 x) " ")))
3221 (symbol-value 'dired-compression-method-alist))))
3223 ;; See if any suffix rule matches this file name.
3225 (let (case-fold-search)
3226 (if (string-match (car (car suffixes)) localname)
3227 (setq suffix (car suffixes) suffixes nil))
3228 (setq suffixes (cdr suffixes))))
3230 (cond ((file-symlink-p file)
3232 ((and suffix (nth 2 suffix))
3233 ;; We found an uncompression rule.
3234 (message "Uncompressing %s..." file)
3235 (when (zerop (tramp-send-command-and-check
3236 multi-method method user host
3237 (concat (nth 2 suffix) " " localname)))
3238 (message "Uncompressing %s...done" file)
3239 ;; `dired-remove-file' is not defined in XEmacs
3240 (funcall (symbol-function 'dired-remove-file) file)
3241 (string-match (car suffix) file)
3242 (concat (substring file 0 (match-beginning 0)))))
3244 ;; We don't recognize the file as compressed, so compress it.
3246 (message "Compressing %s..." file)
3247 (when (zerop (tramp-send-command-and-check
3248 multi-method method user host
3249 (concat "gzip -f " localname)))
3250 (message "Compressing %s...done" file)
3251 ;; `dired-remove-file' is not defined in XEmacs
3252 (funcall (symbol-function 'dired-remove-file) file)
3253 (cond ((file-exists-p (concat file ".gz"))
3254 (concat file ".gz"))
3255 ((file-exists-p (concat file ".z"))
3259 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3260 ;; not sure at all that this is the right way to do it, but let's hope
3261 ;; it works for now, and wait for a guru to point out the Right Way to
3263 ;;(eval-when-compile
3264 ;; (unless (fboundp 'dired-insert-set-properties)
3265 ;; (fset 'dired-insert-set-properties 'ignore)))
3266 ;; Gerd suggests this:
3267 (eval-when-compile (require 'dired))
3268 ;; Note that dired is required at run-time, too, when it is needed.
3269 ;; It is only needed on XEmacs for the function
3270 ;; `dired-insert-set-properties'.
3272 (defun tramp-handle-insert-directory
3273 (filename switches &optional wildcard full-directory-p)
3274 "Like `insert-directory' for tramp files."
3275 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3276 (not ls-lisp-use-insert-directory-program))
3277 (tramp-run-real-handler 'insert-directory
3278 (list filename switches wildcard full-directory-p))
3279 ;; For the moment, we assume that the remote "ls" program does not
3280 ;; grok "--dired". In the future, we should detect this on
3281 ;; connection setup.
3282 (when (string-match "^--dired\\s-+" switches)
3283 (setq switches (replace-match "" nil t switches)))
3284 (setq filename (expand-file-name filename))
3285 (with-parsed-tramp-file-name filename nil
3286 (tramp-message-for-buffer
3287 multi-method method user host 10
3288 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3289 switches filename (if wildcard "yes" "no")
3290 (if full-directory-p "yes" "no"))
3292 (setq wildcard (file-name-nondirectory localname))
3293 (setq localname (file-name-directory localname)))
3294 (when (listp switches)
3295 (setq switches (mapconcat 'identity switches " ")))
3296 (unless full-directory-p
3297 (setq switches (concat "-d " switches)))
3299 (setq switches (concat switches " " wildcard)))
3301 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3302 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3303 (if full-directory-p
3305 multi-method method user host
3307 (tramp-get-ls-command multi-method method user host)
3311 (tramp-shell-quote-argument (concat localname ".")))))
3312 (tramp-barf-unless-okay
3313 multi-method method user host
3314 (format "cd %s" (tramp-shell-quote-argument
3315 (file-name-directory localname)))
3318 (tramp-shell-quote-argument (file-name-directory localname)))
3320 multi-method method user host
3322 (tramp-get-ls-command multi-method method user host)
3326 (tramp-shell-quote-argument
3327 (file-name-nondirectory localname))))))
3328 (sit-for 1) ;needed for rsh but not ssh?
3329 (tramp-wait-for-output))
3330 ;; The following let-binding is used by code that's commented
3331 ;; out. Let's leave the let-binding in for a while to see
3332 ;; that the commented-out code is really not needed. Commenting-out
3333 ;; happened on 2003-03-13.
3334 (let ((old-pos (point)))
3335 (insert-buffer-substring
3336 (tramp-get-buffer multi-method method user host))
3337 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3338 ;; that doesn't exist on Emacs, so we use this workaround instead.
3339 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3340 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3341 ;; (let ((zmacs-region-stays t))
3342 ;; (exchange-point-and-mark))
3344 (tramp-send-command multi-method method user host "cd")
3345 (tramp-wait-for-output))
3346 ;; For the time being, the XEmacs kludge is commented out.
3347 ;; Please test it on various XEmacs versions to see if it works.
3348 ;; ;; Another XEmacs specialty follows. What's the right way to do
3350 ;; (when (and (featurep 'xemacs)
3351 ;; (eq major-mode 'dired-mode))
3354 ;; (dired-insert-set-properties old-pos (point))))
3357 ;; Continuation of kluge to pacify byte-compiler.
3358 ;;(eval-when-compile
3359 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3360 ;; (fmakunbound 'dired-insert-set-properties)))
3362 ;; CCC is this the right thing to do?
3363 (defun tramp-handle-unhandled-file-name-directory (filename)
3364 "Like `unhandled-file-name-directory' for tramp files."
3365 (with-parsed-tramp-file-name filename nil
3366 (expand-file-name "~/")))
3368 ;; Canonicalization of file names.
3370 (defun tramp-drop-volume-letter (name)
3371 "Cut off unnecessary drive letter from file NAME.
3372 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3373 locally on a remote file name. When the local system is a W32 system
3374 but the remote system is Unix, this introduces a superfluous drive
3375 letter into the file name. This function removes it.
3377 Doesn't do anything if the NAME does not start with a drive letter."
3378 (if (and (> (length name) 1)
3379 (char-equal (aref name 1) ?:)
3380 (let ((c1 (aref name 0)))
3381 (or (and (>= c1 ?A) (<= c1 ?Z))
3382 (and (>= c1 ?a) (<= c1 ?z)))))
3386 (defun tramp-handle-expand-file-name (name &optional dir)
3387 "Like `expand-file-name' for tramp files.
3388 If the localname part of the given filename starts with \"/../\" then
3389 the result will be a local, non-Tramp, filename."
3390 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3391 (setq dir (or dir default-directory "/"))
3392 ;; Unless NAME is absolute, concat DIR and NAME.
3393 (unless (file-name-absolute-p name)
3394 (setq name (concat (file-name-as-directory dir) name)))
3395 ;; If NAME is not a tramp file, run the real handler
3396 (if (not (tramp-tramp-file-p name))
3397 (tramp-run-real-handler 'expand-file-name
3400 (with-parsed-tramp-file-name name nil
3401 (unless (file-name-absolute-p localname)
3402 (setq localname (concat "~/" localname)))
3404 ;; Tilde expansion if necessary. This needs a shell which
3405 ;; groks tilde expansion! The function `tramp-find-shell' is
3406 ;; supposed to find such a shell on the remote host. Please
3407 ;; tell me about it when this doesn't work on your system.
3408 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3409 (let ((uname (match-string 1 localname))
3410 (fname (match-string 2 localname)))
3411 ;; CCC fanatic error checking?
3412 (set-buffer (tramp-get-buffer multi-method method user host))
3415 multi-method method user host
3416 (format "cd %s; pwd" uname)
3418 (tramp-wait-for-output)
3419 (goto-char (point-min))
3420 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3421 (setq localname (concat uname fname))
3423 ;; No tilde characters in file name, do normal
3424 ;; expand-file-name (this does "/./" and "/../"). We bind
3425 ;; directory-sep-char here for XEmacs on Windows, which
3426 ;; would otherwise use backslash.
3427 (tramp-let-maybe directory-sep-char ?/
3428 (tramp-make-tramp-file-name
3429 multi-method (or method (tramp-find-default-method user host))
3431 (tramp-drop-volume-letter
3432 (tramp-run-real-handler 'expand-file-name
3433 (list localname)))))))))
3435 ;; old version follows. it uses ".." to cross file handler
3437 ;; ;; Look if localname starts with "/../" construct. If this is
3438 ;; ;; the case, then we return a local name instead of a remote name.
3439 ;; (if (string-match "^/\\.\\./" localname)
3440 ;; (expand-file-name (substring localname 3))
3441 ;; ;; No tilde characters in file name, do normal
3442 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3443 ;; ;; directory-sep-char here for XEmacs on Windows, which
3444 ;; ;; would otherwise use backslash.
3445 ;; (let ((directory-sep-char ?/))
3446 ;; (tramp-make-tramp-file-name
3447 ;; multi-method method user host
3448 ;; (tramp-drop-volume-letter
3449 ;; (tramp-run-real-handler 'expand-file-name
3450 ;; (list localname))))))))))
3454 (defvar tramp-async-proc nil
3455 "Global variable keeping asyncronous process object.
3456 Used in `tramp-handle-shell-command'")
3458 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3459 "Like `shell-command' for tramp files.
3460 This will break if COMMAND prints a newline, followed by the value of
3461 `tramp-end-of-output', followed by another newline."
3462 ;; Asynchronous processes are far from being perfect. But it works at least
3463 ;; for `find-grep-dired' and `find-name-dired' in Emacs 21.4.
3464 (if (tramp-tramp-file-p default-directory)
3465 (with-parsed-tramp-file-name default-directory nil
3466 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3468 (unless output-buffer
3472 "*Async Shell Command*"
3473 "*Shell Command Output*")))
3474 (set-buffer output-buffer)
3476 (unless (bufferp output-buffer)
3477 (setq output-buffer (current-buffer)))
3478 (set-buffer output-buffer)
3479 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3480 ;; process. Instead of, another asynchronous process is opened
3481 ;; which gets the output of the (synchronous) Tramp process
3482 ;; via process-filter. ERROR-BUFFER is disabled.
3484 (setq command (substring command 0 (match-beginning 0))
3486 tramp-async-proc (start-process (buffer-name output-buffer)
3487 output-buffer "cat")))
3489 (tramp-barf-unless-okay
3490 multi-method method user host
3491 (format "cd %s" (tramp-shell-quote-argument localname))
3493 "tramp-handle-shell-command: Couldn't `cd %s'"
3494 (tramp-shell-quote-argument localname))
3495 ;; Define the process filter
3499 (tramp-get-buffer multi-method method user host))
3500 '(lambda (process string)
3501 ;; Write the output into the Tramp Process
3502 (save-current-buffer
3503 (set-buffer (process-buffer process))
3504 (goto-char (point-max))
3506 ;; Hand-over output to asynchronous process.
3509 (regexp-quote tramp-end-of-output) string)))
3512 (substring string 0 (1- (match-beginning 0)))))
3513 (process-send-string tramp-async-proc string)
3515 (set-process-filter process nil)
3516 (process-send-eof tramp-async-proc))))))
3519 multi-method method user host
3521 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3523 (format "%s; tramp_old_status=$?" command)))
3524 (unless asynchronous
3525 (tramp-wait-for-output)))
3526 (unless asynchronous
3527 (insert-buffer (tramp-get-buffer multi-method method user host)))
3530 (unless (bufferp error-buffer)
3531 (setq error-buffer (get-buffer-create error-buffer)))
3533 multi-method method user host
3534 "cat /tmp/tramp.$$.err")
3535 (tramp-wait-for-output)
3536 (set-buffer error-buffer)
3537 (insert-buffer (tramp-get-buffer multi-method method user host))
3538 (tramp-send-command-and-check
3539 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3541 (tramp-send-command multi-method method user host "cd")
3542 (unless asynchronous
3543 (tramp-wait-for-output))
3545 multi-method method user host
3546 (concat "tramp_set_exit_status $tramp_old_status;"
3547 " echo tramp_exit_status $?"))
3548 (unless asynchronous
3549 (tramp-wait-for-output)
3550 (goto-char (point-max))
3551 (unless (search-backward "tramp_exit_status " nil t)
3552 (error "Couldn't find exit status of `%s'" command))
3553 (skip-chars-forward "^ ")
3554 (setq status (read (current-buffer)))))
3555 (unless (zerop (buffer-size))
3556 (display-buffer output-buffer))
3558 ;; The following is only executed if something strange was
3559 ;; happening. Emit a helpful message and do it anyway.
3560 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3562 (tramp-run-real-handler 'shell-command
3563 (list command output-buffer error-buffer))))
3565 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3566 "Like `process-file' for Tramp files."
3567 (when infile (error "Implementation does not handle input from file"))
3568 (when (and (numberp buffer) (zerop buffer))
3569 (error "Implementation does not handle immediate return"))
3570 (when (consp buffer) (error "Implementation does not handle error files"))
3572 (mapconcat 'tramp-shell-quote-argument
3579 (defsubst tramp-make-temp-file ()
3580 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3581 (expand-file-name tramp-temp-name-prefix
3582 (tramp-temporary-file-directory))))
3584 (defun tramp-handle-file-local-copy (filename)
3585 "Like `file-local-copy' for tramp files."
3586 (with-parsed-tramp-file-name filename nil
3587 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3588 ;; We used to bind the following as late as possible.
3589 ;; loc-enc and loc-dec were bound directly before the if
3590 ;; statement that checks them. But the functions
3591 ;; tramp-get-* might invoke the "are you awake" check in
3592 ;; tramp-maybe-open-connection, which is an unfortunate time
3593 ;; since we rely on the buffer contents at that spot.
3594 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3595 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3596 (loc-enc (tramp-get-local-encoding multi-method method user host))
3597 (loc-dec (tramp-get-local-decoding multi-method method user host))
3599 (unless (file-exists-p filename)
3600 (error "Cannot make local copy of non-existing file `%s'"
3602 (setq tmpfil (tramp-make-temp-file))
3604 (cond ((tramp-method-out-of-band-p multi-method method user host)
3605 ;; `copy-file' handles out-of-band methods
3606 (copy-file filename tmpfil t t))
3608 ((and rem-enc rem-dec)
3609 ;; Use inline encoding for file transfer.
3611 ;; Following line for setting tramp-current-method,
3612 ;; tramp-current-user, tramp-current-host.
3613 (set-buffer tramp-buf)
3614 (tramp-message 5 "Encoding remote file %s..." filename)
3615 (tramp-barf-unless-okay
3616 multi-method method user host
3617 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3619 "Encoding remote file failed, see buffer `%s' for details"
3621 ;; Remove trailing status code
3622 (goto-char (point-max))
3623 (delete-region (point) (progn (forward-line -1) (point)))
3625 (tramp-message 5 "Decoding remote file %s..." filename)
3627 ;; Here is where loc-enc and loc-dec used to be let-bound.
3628 (if (and (symbolp loc-dec) (fboundp loc-dec))
3629 ;; If local decoding is a function, we call it.
3630 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3633 (insert-buffer tramp-buf)
3634 (tramp-message-for-buffer
3635 multi-method method user host
3636 6 "Decoding remote file %s with function %s..."
3639 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3640 ;; says that we need to strip tramp_exit_status
3641 ;; line from the output here. Go to point-max,
3642 ;; search backward for tramp_exit_status, delete
3643 ;; between point and point-max if found.
3644 (let ((coding-system-for-write 'binary))
3645 (funcall loc-dec (point-min) (point-max))
3646 (write-region (point-min) (point-max) tmpfil))
3647 (kill-buffer tmpbuf))
3648 ;; If tramp-decoding-function is not defined for this
3649 ;; method, we invoke tramp-decoding-command instead.
3650 (let ((tmpfil2 (tramp-make-temp-file)))
3651 (write-region (point-min) (point-max) tmpfil2)
3653 6 "Decoding remote file %s with command %s..."
3655 (tramp-call-local-coding-command
3656 loc-dec tmpfil2 tmpfil)
3657 (delete-file tmpfil2)))
3658 (tramp-message-for-buffer
3659 multi-method method user host
3660 5 "Decoding remote file %s...done" filename)
3661 ;; Set proper permissions.
3662 (set-file-modes tmpfil (file-modes filename))))
3664 (t (error "Wrong method specification for `%s'" method)))
3667 (defun tramp-handle-file-remote-p (filename)
3668 "Like `file-remote-p' for tramp files."
3669 (when (tramp-tramp-file-p filename)
3670 (with-parsed-tramp-file-name filename nil
3671 (make-tramp-file-name
3672 :multi-method multi-method
3678 (defun tramp-handle-insert-file-contents
3679 (filename &optional visit beg end replace)
3680 "Like `insert-file-contents' for tramp files."
3681 (barf-if-buffer-read-only)
3682 (setq filename (expand-file-name filename))
3683 (with-parsed-tramp-file-name filename nil
3684 (if (not (file-exists-p filename))
3687 (setq buffer-file-name filename)
3688 (set-visited-file-modtime)
3689 (set-buffer-modified-p nil))
3691 (format "File `%s' not found on remote host" filename))
3692 (list (expand-file-name filename) 0))
3693 ;; `insert-file-contents-literally' takes care to avoid calling
3694 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3695 ;; propagate that care to the file-local-copy operation.
3697 (let ((inhibit-file-name-operation
3698 (when (eq inhibit-file-name-operation
3699 'insert-file-contents)
3701 (file-local-copy filename)))
3702 (coding-system-used nil)
3705 (setq buffer-file-name filename)
3706 (set-visited-file-modtime)
3707 (set-buffer-modified-p nil))
3708 (tramp-message-for-buffer
3709 multi-method method user host
3710 9 "Inserting local temp file `%s'..." local-copy)
3711 (setq result (insert-file-contents local-copy nil beg end replace))
3712 ;; Now `last-coding-system-used' has right value. Remember it.
3713 (when (boundp 'last-coding-system-used)
3714 (setq coding-system-used last-coding-system-used))
3715 (tramp-message-for-buffer
3716 multi-method method user host
3717 9 "Inserting local temp file `%s'...done" local-copy)
3718 (delete-file local-copy)
3719 (when (boundp 'last-coding-system-used)
3720 (setq last-coding-system-used coding-system-used))
3721 (list (expand-file-name filename)
3722 (second result))))))
3725 (defun tramp-handle-find-backup-file-name (filename)
3726 "Like `find-backup-file-name' for tramp files."
3727 (with-parsed-tramp-file-name filename nil
3728 ;; We set both variables. It doesn't matter whether it is
3730 (let ((backup-directory-alist
3732 (when (boundp 'backup-directory-alist)
3733 (if (boundp 'tramp-backup-directory-alist)
3738 (if (and (stringp (cdr x))
3739 (file-name-absolute-p (cdr x))
3740 (not (tramp-file-name-p (cdr x))))
3741 (tramp-make-tramp-file-name
3742 multi-method method user host (cdr x))
3744 (symbol-value 'tramp-backup-directory-alist))
3745 (symbol-value 'backup-directory-alist))))
3747 (bkup-backup-directory-info
3749 (when (boundp 'bkup-backup-directory-info)
3750 (if (boundp 'tramp-bkup-backup-directory-info)
3756 (if (and (stringp (car (cdr x)))
3757 (file-name-absolute-p (car (cdr x)))
3758 (not (tramp-file-name-p (car (cdr x)))))
3759 (tramp-make-tramp-file-name
3760 multi-method method user host (car (cdr x)))
3763 (symbol-value 'tramp-bkup-backup-directory-info))
3764 (symbol-value 'bkup-backup-directory-info)))))
3766 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3769 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3770 (defun tramp-handle-write-region
3771 (start end filename &optional append visit lockname confirm)
3772 "Like `write-region' for tramp files."
3773 (unless (eq append nil)
3774 (error "Cannot append to file using tramp (`%s')" filename))
3775 (setq filename (expand-file-name filename))
3776 ;; Following part commented out because we don't know what to do about
3777 ;; file locking, and it does not appear to be a problem to ignore it.
3778 ;; Ange-ftp ignores it, too.
3779 ;; (when (and lockname (stringp lockname))
3780 ;; (setq lockname (expand-file-name lockname)))
3781 ;; (unless (or (eq lockname nil)
3782 ;; (string= lockname filename))
3784 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3785 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
3786 (when (and (not (featurep 'xemacs))
3787 confirm (file-exists-p filename))
3788 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3790 (error "File not overwritten")))
3791 (with-parsed-tramp-file-name filename nil
3792 (let ((curbuf (current-buffer))
3793 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3794 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3795 (loc-enc (tramp-get-local-encoding multi-method method user host))
3796 (loc-dec (tramp-get-local-decoding multi-method method user host))
3797 (trampbuf (get-buffer-create "*tramp output*"))
3798 (modes (file-modes filename))
3799 ;; We use this to save the value of `last-coding-system-used'
3800 ;; after writing the tmp file. At the end of the function,
3801 ;; we set `last-coding-system-used' to this saved value.
3802 ;; This way, any intermediary coding systems used while
3803 ;; talking to the remote shell or suchlike won't hose this
3804 ;; variable. This approach was snarfed from ange-ftp.el.
3807 ;; Write region into a tmp file. This isn't really needed if we
3808 ;; use an encoding function, but currently we use it always
3809 ;; because this makes the logic simpler.
3810 (setq tmpfil (tramp-make-temp-file))
3811 ;; Set current buffer. If connection wasn't open, `file-modes' has
3812 ;; changed it accidently.
3814 ;; We say `no-message' here because we don't want the visited file
3815 ;; modtime data to be clobbered from the temp file. We call
3816 ;; `set-visited-file-modtime' ourselves later on.
3817 (tramp-run-real-handler
3819 (if confirm ; don't pass this arg unless defined for backward compat.
3820 (list start end tmpfil append 'no-message lockname confirm)
3821 (list start end tmpfil append 'no-message lockname)))
3822 ;; The permissions of the temporary file should be set. If
3823 ;; filename does not exist (eq modes nil) it has been renamed to
3824 ;; the backup file. This case `save-buffer' handles
3826 (when modes (set-file-modes tmpfil modes))
3827 ;; Now, `last-coding-system-used' has the right value. Remember it.
3828 (when (boundp 'last-coding-system-used)
3829 (setq coding-system-used last-coding-system-used))
3830 ;; This is a bit lengthy due to the different methods possible for
3831 ;; file transfer. First, we check whether the method uses an rcp
3832 ;; program. If so, we call it. Otherwise, both encoding and
3833 ;; decoding command must be specified. However, if the method
3834 ;; _also_ specifies an encoding function, then that is used for
3835 ;; encoding the contents of the tmp file.
3836 (cond ((tramp-method-out-of-band-p multi-method method user host)
3837 ;; `copy-file' handles out-of-band methods
3838 (copy-file tmpfil filename t t))
3840 ((and rem-enc rem-dec)
3841 ;; Use inline file transfer
3842 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
3844 ;; Encode tmpfil into tmpbuf
3845 (tramp-message-for-buffer multi-method method user host
3846 5 "Encoding region...")
3849 ;; Use encoding function or command.
3850 (if (and (symbolp loc-enc) (fboundp loc-enc))
3852 (tramp-message-for-buffer
3853 multi-method method user host
3854 6 "Encoding region using function `%s'..."
3855 (symbol-name loc-enc))
3856 (insert-file-contents-literally tmpfil)
3857 ;; CCC. The following `let' is a workaround for
3858 ;; the base64.el that comes with pgnus-0.84. If
3859 ;; both of the following conditions are
3860 ;; satisfied, it tries to write to a local file
3861 ;; in default-directory, but at this point,
3862 ;; default-directory is remote.
3863 ;; (CALL-PROCESS-REGION can't write to remote
3864 ;; files, it seems.) The file in question is a
3866 (let ((default-directory
3867 (tramp-temporary-file-directory)))
3868 (funcall loc-enc (point-min) (point-max)))
3869 (goto-char (point-max))
3872 (tramp-message-for-buffer
3873 multi-method method user host
3874 6 "Encoding region using command `%s'..." loc-enc)
3875 (unless (equal 0 (tramp-call-local-coding-command
3877 (pop-to-buffer trampbuf)
3878 (error (concat "Cannot write to `%s', local encoding"
3879 " command `%s' failed")
3881 ;; Send tmpbuf into remote decoding command which
3882 ;; writes to remote file. Because this happens on the
3883 ;; remote host, we cannot use the function.
3884 (tramp-message-for-buffer
3885 multi-method method user host
3886 5 "Decoding region into remote file %s..." filename)
3888 multi-method method user host
3889 (format "%s >%s <<'EOF'"
3891 (tramp-shell-quote-argument localname)))
3893 (tramp-message-for-buffer
3894 multi-method method user host
3895 6 "Sending data to remote host...")
3896 (tramp-send-string multi-method method user host
3898 ;; wait for remote decoding to complete
3899 (tramp-message-for-buffer
3900 multi-method method user host
3901 6 "Sending end of data token...")
3903 multi-method method user host "EOF" nil t)
3904 (tramp-message-for-buffer
3905 multi-method method user host 6
3906 "Waiting for remote host to process data...")
3907 (set-buffer (tramp-get-buffer multi-method method user host))
3908 (tramp-wait-for-output)
3909 (tramp-barf-unless-okay
3910 multi-method method user host nil nil 'file-error
3911 (concat "Couldn't write region to `%s',"
3912 " decode using `%s' failed")
3914 (tramp-message 5 "Decoding region into remote file %s...done"
3916 (kill-buffer tmpbuf))))
3919 (concat "Method `%s' should specify both encoding and "
3920 "decoding command or an rcp program")
3922 (delete-file tmpfil)
3923 (unless (equal curbuf (current-buffer))
3924 (error "Buffer has changed from `%s' to `%s'"
3925 curbuf (current-buffer)))
3926 (when (or (eq visit t) (stringp visit))
3927 (set-visited-file-modtime
3928 ;; We must pass modtime explicitely, because filename can be different
3929 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
3930 (nth 5 (file-attributes filename))))
3931 ;; Make `last-coding-system-used' have the right value.
3932 (when (boundp 'last-coding-system-used)
3933 (setq last-coding-system-used coding-system-used))
3934 (when (or (eq visit t)
3937 (message "Wrote %s" filename)))))
3939 ;; Call down to the real handler.
3940 ;; Because EFS does not play nicely with TRAMP (both systems match a
3941 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
3944 ;; Other than that, this is the canon file-handler code that the doco
3945 ;; says should be used here. Which is nice.
3947 ;; Under XEmacs current, EFS also hooks in as
3948 ;; efs-sifn-handler-function to handle any filename with environment
3949 ;; variables. This has two implications:
3950 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
3951 ;; 2) That TRAMP might want to do the same thing.
3952 ;; Details as they come in.
3954 ;; Daniel Pittman <daniel@danann.net>
3956 ;; (defun tramp-run-real-handler (operation args)
3957 ;; "Invoke normal file name handler for OPERATION.
3958 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
3959 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
3961 ;; (let ((inhibit-file-name-handlers
3962 ;; (list 'tramp-file-name-handler
3963 ;; 'efs-file-handler-function
3964 ;; 'ange-ftp-hook-function
3965 ;; (and (eq inhibit-file-name-operation operation)
3966 ;; inhibit-file-name-handlers)))
3967 ;; (inhibit-file-name-operation operation))
3968 ;; (apply operation args)))
3970 (defun tramp-run-real-handler (operation args)
3971 "Invoke normal file name handler for OPERATION.
3972 First arg specifies the OPERATION, second arg is a list of arguments to
3973 pass to the OPERATION."
3974 (let* ((inhibit-file-name-handlers
3975 `(tramp-file-name-handler
3976 tramp-completion-file-name-handler
3977 cygwin-mount-name-hook-function
3978 cygwin-mount-map-drive-hook-function
3980 ,(and (eq inhibit-file-name-operation operation)
3981 inhibit-file-name-handlers)))
3982 (inhibit-file-name-operation operation))
3983 (apply operation args)))
3985 ;; This function is used from `tramp-completion-file-name-handler' functions
3986 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
3987 ;; because the check is based on a full filename, not available for all
3988 ;; basic I/O operations.
3989 (defun tramp-completion-run-real-handler (operation args)
3990 "Invoke `tramp-file-name-handler' for OPERATION.
3991 First arg specifies the OPERATION, second arg is a list of arguments to
3992 pass to the OPERATION."
3993 (let* ((inhibit-file-name-handlers
3994 `(tramp-completion-file-name-handler
3995 cygwin-mount-name-hook-function
3996 cygwin-mount-map-drive-hook-function
3998 ,(and (eq inhibit-file-name-operation operation)
3999 inhibit-file-name-handlers)))
4000 (inhibit-file-name-operation operation))
4001 (apply operation args)))
4003 ;; We handle here all file primitives. Most of them have the file
4004 ;; name as first parameter; nevertheless we check for them explicitly
4005 ;; in order to be signalled if a new primitive appears. This
4006 ;; scenario is needed because there isn't a way to decide by
4007 ;; syntactical means whether a foreign method must be called. It would
4008 ;; ease the life if `file-name-handler-alist' would support a decision
4009 ;; function as well but regexp only.
4010 (defun tramp-file-name-for-operation (operation &rest args)
4011 "Return file name related to OPERATION file primitive.
4012 ARGS are the arguments OPERATION has been called with."
4014 ; FILE resp DIRECTORY
4016 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4017 'delete-file 'diff-latest-backup-file 'directory-file-name
4018 'directory-files 'directory-files-and-attributes
4019 'dired-compress-file 'dired-uncache
4020 'file-accessible-directory-p 'file-attributes
4021 'file-directory-p 'file-executable-p 'file-exists-p
4022 'file-local-copy 'file-remote-p 'file-modes
4023 'file-name-as-directory 'file-name-directory
4024 'file-name-nondirectory 'file-name-sans-versions
4025 'file-ownership-preserved-p 'file-readable-p
4026 'file-regular-p 'file-symlink-p 'file-truename
4027 'file-writable-p 'find-backup-file-name 'find-file-noselect
4028 'get-file-buffer 'insert-directory 'insert-file-contents
4029 'load 'make-directory 'make-directory-internal
4030 'set-file-modes 'substitute-in-file-name
4031 'unhandled-file-name-directory 'vc-registered
4033 'abbreviate-file-name 'create-file-buffer
4034 'dired-file-modtime 'dired-make-compressed-filename
4035 'dired-recursive-delete-directory 'dired-set-file-modtime
4036 'dired-shell-unhandle-file-name 'dired-uucode-file
4037 'insert-file-contents-literally 'recover-file
4038 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4039 (if (file-name-absolute-p (nth 0 args))
4041 (expand-file-name (nth 0 args))))
4042 ; FILE DIRECTORY resp FILE1 FILE2
4044 (list 'add-name-to-file 'copy-file 'expand-file-name
4045 'file-name-all-completions 'file-name-completion
4046 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4048 'dired-make-relative-symlink
4049 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4052 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4053 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4054 (t (buffer-file-name (current-buffer))))))
4056 ((eq operation 'write-region)
4060 (list 'set-visited-file-modtime 'verify-visited-file-modtime
4064 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4067 (list 'dired-call-process 'shell-command
4068 ; Post Emacs 21.3 only
4071 'dired-print-file 'dired-shell-call-process))
4073 ; unknown file primitive
4074 (t (error "unknown file I/O primitive: %s" operation))))
4076 (defun tramp-find-foreign-file-name-handler (filename)
4077 "Return foreign file name handler if exists."
4078 (when (tramp-tramp-file-p filename)
4081 (handler-alist tramp-foreign-file-name-handler-alist))
4082 (while handler-alist
4083 (setq elt (car handler-alist)
4084 handler-alist (cdr handler-alist))
4085 (when (funcall (car elt) filename)
4086 (setq handler-alist nil)
4087 (setq res (cdr elt))))
4092 (defun tramp-file-name-handler (operation &rest args)
4093 "Invoke Tramp file name handler.
4094 Falls back to normal file name handler if no tramp file name handler exists."
4096 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4097 (foreign (tramp-find-foreign-file-name-handler filename)))
4099 (foreign (apply foreign operation args))
4100 (t (tramp-run-real-handler operation args))))))
4103 ;; In Emacs, there is some concurrency due to timers. If a timer
4104 ;; interrupts Tramp and wishes to use the same connection buffer as
4105 ;; the "main" Emacs, then garbage might occur in the connection
4106 ;; buffer. Therefore, we need to make sure that a timer does not use
4107 ;; the same connection buffer as the "main" Emacs. We implement a
4108 ;; cheap global lock, instead of locking each connection buffer
4109 ;; separately. The global lock is based on two variables,
4110 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4111 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4112 ;; processing of a single file operation, so we need to allow
4113 ;; recursive calls. That's where the `tramp-locker' variable comes in
4114 ;; -- it is let-bound to t during the execution of the current
4115 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4116 ;; then we should just proceed because we have been called
4117 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4118 ;; interrupting the "main" Emacs, and then we signal an error.
4120 (defvar tramp-locked nil
4121 "If non-nil, then Tramp is currently busy.
4122 Together with `tramp-locker', this implements a locking mechanism
4123 preventing reentrant calls of Tramp.")
4125 (defvar tramp-locker nil
4126 "If non-nil, then a caller has locked Tramp.
4127 Together with `tramp-locked', this implements a locking mechanism
4128 preventing reentrant calls of Tramp.")
4130 (defun tramp-sh-file-name-handler (operation &rest args)
4131 "Invoke remote-shell Tramp file name handler.
4132 Fall back to normal file name handler if no Tramp handler exists."
4133 (when (and tramp-locked (not tramp-locker))
4134 (signal 'file-error "Forbidden reentrant call of Tramp"))
4135 (let ((tl tramp-locked))
4138 (setq tramp-locked t)
4139 (let ((tramp-locker t))
4141 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4143 (apply (cdr fn) args)
4144 (tramp-run-real-handler operation args))))))
4145 (setq tramp-locked tl))))
4148 (defun tramp-completion-file-name-handler (operation &rest args)
4149 "Invoke tramp file name completion handler.
4150 Falls back to normal file name handler if no tramp file name handler exists."
4151 ;; (setq tramp-debug-buffer t)
4152 ;; (tramp-message 1 "%s %s" operation args)
4153 ;; (tramp-message 1 "%s %s\n%s"
4154 ;; operation args (with-output-to-string (backtrace)))
4155 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4157 (save-match-data (apply (cdr fn) args))
4158 (tramp-completion-run-real-handler operation args))))
4161 (put 'tramp-completion-file-name-handler 'safe-magic t)
4163 ;; Register in file name handler alist
4165 (add-to-list 'file-name-handler-alist
4166 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4167 (add-to-list 'file-name-handler-alist
4168 (cons tramp-completion-file-name-regexp
4169 'tramp-completion-file-name-handler))
4171 (defun tramp-repair-jka-compr ()
4172 "If jka-compr is already loaded, move it to the front of
4173 `file-name-handler-alist'. On Emacs 21.4 or so this will not be
4175 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4177 (setq file-name-handler-alist
4178 (cons jka (delete jka file-name-handler-alist))))))
4179 (tramp-repair-jka-compr)
4182 ;;; Interactions with other packages:
4184 ;; -- complete.el --
4186 ;; This function contributed by Ed Sabol
4187 (defun tramp-handle-expand-many-files (name)
4188 "Like `PC-expand-many-files' for tramp files."
4189 (with-parsed-tramp-file-name name nil
4191 (if (or (string-match "\\*" name)
4192 (string-match "\\?" name)
4193 (string-match "\\[.*\\]" name))
4196 ;; CCC: To do it right, we should quote certain characters
4197 ;; in the file name, but since the echo command is going to
4198 ;; break anyway when there are spaces in the file names, we
4200 ;;-(let ((comint-file-name-quote-list
4201 ;;- (set-difference tramp-file-name-quote-list
4202 ;;- '(?\* ?\? ?[ ?]))))
4203 ;;- (tramp-send-command
4204 ;;- multi-method method user host
4205 ;;- (format "echo %s" (comint-quote-filename localname)))
4206 ;;- (tramp-wait-for-output))
4207 (tramp-send-command multi-method method user host
4208 (format "echo %s" localname))
4209 (tramp-wait-for-output)
4210 (setq bufstr (buffer-substring (point-min)
4211 (tramp-line-end-position)))
4212 (goto-char (point-min))
4213 (if (string-equal localname bufstr)
4216 (while (search-forward " " nil t)
4217 (delete-backward-char 1)
4219 (goto-char (point-max))
4220 (delete-backward-char 1)
4222 (goto-char (point-min))
4224 (function (lambda (x)
4225 (tramp-make-tramp-file-name multi-method method
4227 (read (current-buffer))))))
4228 (list (expand-file-name name))))))
4230 ;; Check for complete.el and override PC-expand-many-files if appropriate.
4232 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
4234 (defun tramp-setup-complete ()
4235 (fset 'tramp-save-PC-expand-many-files
4236 (symbol-function 'PC-expand-many-files))
4237 (defun PC-expand-many-files (name)
4238 (if (tramp-tramp-file-p name)
4239 (expand-many-files name)
4240 (tramp-save-PC-expand-many-files name))))
4242 ;; Why isn't eval-after-load sufficient?
4243 (if (fboundp 'PC-expand-many-files)
4244 (tramp-setup-complete)
4245 (eval-after-load "complete" '(tramp-setup-complete)))
4247 ;;; File name handler functions for completion mode
4249 ;; Necessary because `tramp-file-name-regexp-unified' and
4250 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4251 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4252 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4253 ;; Using `last-input-event' is a little bit risky, because completing a file
4254 ;; might require loading other files, like "~/.netrc", and for them it
4255 ;; shouldn't be decided based on that variable. On the other hand, those files
4256 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4257 ;; be introduced overwriting this check in such cases. Or we change tramp
4258 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4259 ;; In case of non unified file names it can be always true (and wouldn't be
4260 ;; necessary, because there are different regexp).
4261 (defun tramp-completion-mode (file)
4262 "Checks whether method / user name / host name completion is active."
4264 ((not tramp-unified-filenames) t)
4265 ((string-match "^/.*:.*:$" file) nil)
4267 (concat tramp-prefix-regexp
4268 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4270 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4271 ((or (equal last-input-event 'tab)
4273 (and (integerp last-input-event)
4274 (not (event-modifiers last-input-event))
4275 (or (char-equal last-input-event ?\?)
4276 (char-equal last-input-event ?\t) ; handled by 'tab already?
4277 (char-equal last-input-event ?\ )))
4279 (and (featurep 'xemacs)
4280 (not (event-modifiers last-input-event))
4282 (funcall (symbol-function 'event-to-character)
4283 last-input-event) ?\?)
4285 (funcall (symbol-function 'event-to-character)
4286 last-input-event) ?\t)
4288 (funcall (symbol-function 'event-to-character)
4289 last-input-event) ?\ ))))
4292 (defun tramp-completion-handle-file-exists-p (filename)
4293 "Like `file-exists-p' for tramp files."
4294 (if (tramp-completion-mode filename)
4295 (tramp-run-real-handler
4296 'file-exists-p (list filename))
4297 (tramp-completion-run-real-handler
4298 'file-exists-p (list filename))))
4300 ;; Localname manipulation in case of partial TRAMP file names.
4301 (defun tramp-completion-handle-file-name-directory (file)
4302 "Like `file-name-directory' but aware of TRAMP files."
4303 (if (tramp-completion-mode file)
4305 (tramp-completion-run-real-handler
4306 'file-name-directory (list file))))
4308 ;; Localname manipulation in case of partial TRAMP file names.
4309 (defun tramp-completion-handle-file-name-nondirectory (file)
4310 "Like `file-name-nondirectory' but aware of TRAMP files."
4312 file (length (tramp-completion-handle-file-name-directory file))))
4314 ;; Method, host name and user name completion.
4315 ;; `tramp-completion-dissect-file-name' returns a list of
4316 ;; tramp-file-name structures. For all of them we return possible completions.
4317 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4318 "Like `file-name-all-completions' for partial tramp files."
4321 ((fullname (concat directory filename))
4324 (if (tramp-completion-mode fullname)
4325 (tramp-run-real-handler
4326 'file-name-all-completions (list filename directory))
4327 (tramp-completion-run-real-handler
4328 'file-name-all-completions (list filename directory))))
4329 ;; possible completion structures
4330 (v (tramp-completion-dissect-file-name fullname)))
4333 (let* ((car (car v))
4334 (multi-method (tramp-file-name-multi-method car))
4335 (method (tramp-file-name-method car))
4336 (user (tramp-file-name-user car))
4337 (host (tramp-file-name-host car))
4338 (localname (tramp-file-name-localname car))
4339 (m (tramp-find-method multi-method method user host))
4340 (tramp-current-user user) ; see `tramp-parse-passwd'
4343 (unless (or multi-method ;; Not handled (yet).
4344 localname) ;; Nothing to complete
4348 ;; Method dependent user / host combinations
4352 (setq all-user-hosts
4353 (append all-user-hosts
4354 (funcall (nth 0 x) (nth 1 x)))))
4355 (tramp-get-completion-function m))
4357 (setq result (append result
4360 (tramp-get-completion-user-host
4361 method user host (nth 0 x) (nth 1 x)))
4362 (delq nil all-user-hosts)))))
4366 (append result (tramp-get-completion-methods m)))))
4368 (setq v (delq car v))))
4370 ;;; unify list, remove nil elements
4373 (let ((car (car result)))
4374 (when car (add-to-list 'result1 car))
4375 (setq result (delq car result))))
4379 ;; Method, host name and user name completion for a file.
4380 (defun tramp-completion-handle-file-name-completion (filename directory)
4381 "Like `file-name-completion' for tramp files."
4382 (try-completion filename
4383 (mapcar 'list (file-name-all-completions filename directory))))
4385 ;; I misuse a little bit the tramp-file-name structure in order to handle
4386 ;; completion possibilities for partial methods / user names / host names.
4387 ;; Return value is a list of tramp-file-name structures according to possible
4388 ;; completions. If "multi-method" or "localname" is non-nil it means there
4389 ;; shouldn't be a completion anymore.
4391 ;; Expected results:
4393 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4394 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4395 ;; [nil nil "x" nil nil]
4396 ;; [nil "x" nil nil nil]
4398 ;; "/x:" "/x:y" "/x:y:"
4399 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4401 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4402 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4404 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4405 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4406 ;; "/[x/y@" "/[x/y@z"
4407 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4408 (defun tramp-completion-dissect-file-name (name)
4409 "Returns a list of `tramp-file-name' structures.
4410 They are collected by `tramp-completion-dissect-file-name1'."
4414 ;; "/method" "/[method"
4415 (tramp-completion-file-name-structure1
4416 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4419 (tramp-completion-file-name-structure2
4420 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4423 (tramp-completion-file-name-structure3
4424 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4426 ;; "/user@host" "/[user@host"
4427 (tramp-completion-file-name-structure4
4428 (list (concat tramp-prefix-regexp
4429 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4430 "\\(" tramp-host-regexp x-nil "\\)$")
4432 ;; "/method:user" "/[method/user"
4433 (tramp-completion-file-name-structure5
4434 (list (concat tramp-prefix-regexp
4435 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4436 "\\(" tramp-user-regexp x-nil "\\)$")
4438 ;; "/method:host" "/[method/host"
4439 (tramp-completion-file-name-structure6
4440 (list (concat tramp-prefix-regexp
4441 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4442 "\\(" tramp-host-regexp x-nil "\\)$")
4444 ;; "/method:user@host" "/[method/user@host"
4445 (tramp-completion-file-name-structure7
4446 (list (concat tramp-prefix-regexp
4447 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4448 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4449 "\\(" tramp-host-regexp x-nil "\\)$")
4452 (mapcar (lambda (regexp)
4453 (add-to-list 'result
4454 (tramp-completion-dissect-file-name1 regexp name)))
4456 tramp-completion-file-name-structure1
4457 tramp-completion-file-name-structure2
4458 tramp-completion-file-name-structure3
4459 tramp-completion-file-name-structure4
4460 tramp-completion-file-name-structure5
4461 tramp-completion-file-name-structure6
4462 tramp-completion-file-name-structure7
4463 tramp-file-name-structure))
4467 (defun tramp-completion-dissect-file-name1 (structure name)
4468 "Returns a `tramp-file-name' structure matching STRUCTURE.
4469 The structure consists of multi-method, remote method, remote user,
4470 remote host and localname (filename on remote host)."
4474 (when (string-match (nth 0 structure) name)
4475 (setq method (and (nth 1 structure)
4476 (match-string (nth 1 structure) name)))
4477 (if (and method (member method tramp-multi-methods))
4478 ;; Not handled (yet).
4479 (make-tramp-file-name
4480 :multi-method method
4485 (let ((user (and (nth 2 structure)
4486 (match-string (nth 2 structure) name)))
4487 (host (and (nth 3 structure)
4488 (match-string (nth 3 structure) name)))
4489 (localname (and (nth 4 structure)
4490 (match-string (nth 4 structure) name))))
4491 (make-tramp-file-name
4496 :localname localname)))))))
4498 ;; This function returns all possible method completions, adding the
4499 ;; trailing method delimeter.
4500 (defun tramp-get-completion-methods (partial-method)
4501 "Returns all method completions for PARTIAL-METHOD."
4505 (string-match (concat "^" (regexp-quote partial-method)) method)
4506 ;; we must remove leading "/".
4507 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4508 (delete "multi" (mapcar 'car tramp-methods))))
4510 ;; Compares partial user and host names with possible completions.
4511 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4512 "Returns the most expanded string for user and host name completion.
4513 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4516 ((and partial-user partial-host)
4518 (string-match (concat "^" (regexp-quote partial-host)) host)
4519 (string-equal partial-user (or user partial-user)))
4520 (setq user partial-user)
4527 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4533 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4539 (unless (zerop (+ (length user) (length host)))
4540 ;; we must remove leading "/".
4541 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4543 (defun tramp-parse-rhosts (filename)
4544 "Return a list of (user host) tuples allowed to access.
4545 Either user or host may be nil."
4548 (when (file-readable-p filename)
4550 (insert-file-contents filename)
4551 (goto-char (point-min))
4553 (push (tramp-parse-rhosts-group) res))))
4556 ;; Taken from gnus/netrc.el
4558 (defalias 'tramp-point-at-eol
4559 (if (fboundp 'point-at-eol)
4561 'line-end-position)))
4563 (defun tramp-parse-rhosts-group ()
4564 "Return a (user host) tuple allowed to access.
4565 Either user or host may be nil."
4570 "^\\(" tramp-host-regexp "\\)"
4571 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4573 (narrow-to-region (point) (tramp-point-at-eol))
4574 (when (re-search-forward regexp nil t)
4575 (setq result (append (list (match-string 3) (match-string 1)))))
4580 (defun tramp-parse-shosts (filename)
4581 "Return a list of (user host) tuples allowed to access.
4582 User is always nil."
4585 (when (file-readable-p filename)
4587 (insert-file-contents filename)
4588 (goto-char (point-min))
4590 (push (tramp-parse-shosts-group) res))))
4593 (defun tramp-parse-shosts-group ()
4594 "Return a (user host) tuple allowed to access.
4595 User is always nil."
4598 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4600 (narrow-to-region (point) (tramp-point-at-eol))
4601 (when (re-search-forward regexp nil t)
4602 (setq result (list nil (match-string 1))))
4605 (> (skip-chars-forward ",") 0)
4609 (defun tramp-parse-sconfig (filename)
4610 "Return a list of (user host) tuples allowed to access.
4611 User is always nil."
4614 (when (file-readable-p filename)
4616 (insert-file-contents filename)
4617 (goto-char (point-min))
4619 (push (tramp-parse-sconfig-group) res))))
4622 (defun tramp-parse-sconfig-group ()
4623 "Return a (user host) tuple allowed to access.
4624 User is always nil."
4627 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4629 (narrow-to-region (point) (tramp-point-at-eol))
4630 (when (re-search-forward regexp nil t)
4631 (setq result (list nil (match-string 1))))
4634 (> (skip-chars-forward ",") 0)
4638 (defun tramp-parse-shostkeys (dirname)
4639 "Return a list of (user host) tuples allowed to access.
4640 User is always nil."
4642 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4643 (files (when (file-directory-p dirname) (directory-files dirname)))
4647 (when (string-match regexp (car files))
4648 (push (list nil (match-string 1 (car files))) result))
4649 (setq files (cdr files)))
4652 (defun tramp-parse-sknownhosts (dirname)
4653 "Return a list of (user host) tuples allowed to access.
4654 User is always nil."
4656 (let ((regexp (concat "^\\(" tramp-host-regexp
4657 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4658 (files (when (file-directory-p dirname) (directory-files dirname)))
4662 (when (string-match regexp (car files))
4663 (push (list nil (match-string 1 (car files))) result))
4664 (setq files (cdr files)))
4667 (defun tramp-parse-hosts (filename)
4668 "Return a list of (user host) tuples allowed to access.
4669 User is always nil."
4672 (when (file-readable-p filename)
4674 (insert-file-contents filename)
4675 (goto-char (point-min))
4677 (push (tramp-parse-hosts-group) res))))
4680 (defun tramp-parse-hosts-group ()
4681 "Return a (user host) tuple allowed to access.
4682 User is always nil."
4685 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4687 (narrow-to-region (point) (tramp-point-at-eol))
4688 (when (re-search-forward regexp nil t)
4689 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4690 (setq result (list nil (match-string 1)))))
4693 (> (skip-chars-forward " \t") 0)
4697 ;; For su-alike methods it would be desirable to return "root@localhost"
4698 ;; as default. Unfortunately, we have no information whether any user name
4699 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4700 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4701 (defun tramp-parse-passwd (filename)
4702 "Return a list of (user host) tuples allowed to access.
4703 Host is always \"localhost\"."
4706 (if (zerop (length tramp-current-user))
4708 (when (file-readable-p filename)
4710 (insert-file-contents filename)
4711 (goto-char (point-min))
4713 (push (tramp-parse-passwd-group) res))))
4716 (defun tramp-parse-passwd-group ()
4717 "Return a (user host) tuple allowed to access.
4718 Host is always \"localhost\"."
4721 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4723 (narrow-to-region (point) (tramp-point-at-eol))
4724 (when (re-search-forward regexp nil t)
4725 (setq result (list (match-string 1) "localhost")))
4730 (defun tramp-parse-netrc (filename)
4731 "Return a list of (user host) tuples allowed to access.
4735 (when (file-readable-p filename)
4737 (insert-file-contents filename)
4738 (goto-char (point-min))
4740 (push (tramp-parse-netrc-group) res))))
4743 (defun tramp-parse-netrc-group ()
4744 "Return a (user host) tuple allowed to access.
4750 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4751 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4753 (narrow-to-region (point) (tramp-point-at-eol))
4754 (when (re-search-forward regexp nil t)
4755 (setq result (list (match-string 3) (match-string 1))))
4760 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4761 "Like `expand-file-name' for tramp files."
4762 (let ((fullname (concat (or dir default-directory) name)))
4763 (tramp-drop-volume-letter
4764 (if (tramp-completion-mode fullname)
4765 (tramp-run-real-handler
4766 'expand-file-name (list name dir))
4767 (tramp-completion-run-real-handler
4768 'expand-file-name (list name dir))))))
4770 ;;; Internal Functions:
4772 (defun tramp-maybe-send-perl-script (script name multi-method method user host)
4773 "Define in remote shell function NAME implemented as perl SCRIPT.
4774 Only send the definition if it has not already been done.
4775 Function may have 0-3 parameters."
4776 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4777 (unless remote-perl (error "No remote perl"))
4778 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4779 multi-method method user host)))
4780 (unless (memq name perl-scripts)
4781 (with-current-buffer (tramp-get-buffer multi-method method user host)
4782 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4783 (tramp-send-string multi-method method user host
4789 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4790 (tramp-wait-for-output)
4791 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4792 multi-method method user host)
4793 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4795 (defun tramp-set-auto-save ()
4796 (when (and (buffer-file-name)
4797 (tramp-tramp-file-p (buffer-file-name))
4799 (auto-save-mode 1)))
4800 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4802 (defun tramp-run-test (switch filename)
4803 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4804 Returns the exit code of the `test' program."
4805 (let ((v (tramp-dissect-file-name filename)))
4807 (tramp-send-command-and-check
4808 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4809 (tramp-file-name-user v) (tramp-file-name-host v)
4810 (format "test %s %s" switch
4811 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4813 (defun tramp-run-test2 (program file1 file2 &optional switch)
4814 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4815 The optional SWITCH is inserted between the two files.
4816 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4817 hosts, or files, disagree."
4818 (let* ((v1 (tramp-dissect-file-name file1))
4819 (v2 (tramp-dissect-file-name file2))
4820 (mmethod1 (tramp-file-name-multi-method v1))
4821 (mmethod2 (tramp-file-name-multi-method v2))
4822 (method1 (tramp-file-name-method v1))
4823 (method2 (tramp-file-name-method v2))
4824 (user1 (tramp-file-name-user v1))
4825 (user2 (tramp-file-name-user v2))
4826 (host1 (tramp-file-name-host v1))
4827 (host2 (tramp-file-name-host v2))
4828 (localname1 (tramp-file-name-localname v1))
4829 (localname2 (tramp-file-name-localname v2)))
4830 (unless (and method1 method2 host1 host2
4831 (equal mmethod1 mmethod2)
4832 (equal method1 method2)
4834 (equal host1 host2))
4835 (error "tramp-run-test2: %s"
4836 "only implemented for same method, same user, same host"))
4838 (tramp-send-command-and-check
4839 mmethod1 method1 user1 host1
4840 (format "%s %s %s %s"
4842 (tramp-shell-quote-argument localname1)
4844 (tramp-shell-quote-argument localname2))))))
4846 (defun tramp-touch (file time)
4847 "Set the last-modified timestamp of the given file.
4848 TIME is an Emacs internal time value as returned by `current-time'."
4849 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
4850 (if (tramp-tramp-file-p file)
4851 (with-parsed-tramp-file-name file nil
4852 (let ((buf (tramp-get-buffer multi-method method user host)))
4853 (unless (zerop (tramp-send-command-and-check
4854 multi-method method user host
4855 (format "touch -t %s %s"
4859 (error "tramp-touch: touch failed, see buffer `%s' for details"
4861 ;; It's a local file
4863 (unless (zerop (call-process
4864 "touch" nil (current-buffer) nil "-t" touch-time file))
4865 (pop-to-buffer (current-buffer))
4866 (error "tramp-touch: touch failed"))))))
4868 (defun tramp-buffer-name (multi-method method user host)
4869 "A name for the connection buffer for USER at HOST using METHOD."
4871 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
4872 (let ((method (tramp-find-method multi-method method user host)))
4874 (format "*tramp/%s %s@%s*" method user host)
4875 (format "*tramp/%s %s*" method host)))))
4877 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
4878 "A name for the multi method connection buffer.
4879 MULTI-METHOD gives the multi method, METHOD the array of methods,
4880 USER the array of user names, HOST the array of host names."
4881 (unless (and (= (length method) (length user))
4882 (= (length method) (length host)))
4883 (error "Syntax error in multi method (implementation error)"))
4884 (let ((len (length method))
4889 (cons (if (aref user i)
4890 (format "%s#%s@%s:" (aref method i)
4891 (aref user i) (aref host i))
4892 (format "%s@%s:" (aref method i) (aref host i)))
4895 (format "*%s/%s %s*"
4897 (apply 'concat (reverse string-list)))))
4899 (defun tramp-get-buffer (multi-method method user host)
4900 "Get the connection buffer to be used for USER at HOST using METHOD."
4901 (get-buffer-create (tramp-buffer-name multi-method method user host)))
4903 (defun tramp-debug-buffer-name (multi-method method user host)
4904 "A name for the debug buffer for USER at HOST using METHOD."
4906 (tramp-buffer-name-multi-method "debug tramp"
4907 multi-method method user host)
4908 (let ((method (tramp-find-method multi-method method user host)))
4910 (format "*debug tramp/%s %s@%s*" method user host)
4911 (format "*debug tramp/%s %s*" method host)))))
4913 (defun tramp-get-debug-buffer (multi-method method user host)
4914 "Get the debug buffer for USER at HOST using METHOD."
4915 (get-buffer-create (tramp-debug-buffer-name multi-method method user host)))
4917 (defun tramp-find-executable (multi-method method user host
4918 progname dirlist ignore-tilde)
4919 "Searches for PROGNAME in all directories mentioned in DIRLIST.
4920 First args METHOD, USER and HOST specify the connection, PROGNAME
4921 is the program to search for, and DIRLIST gives the list of directories
4922 to search. If IGNORE-TILDE is non-nil, directory names starting
4923 with `~' will be ignored.
4925 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
4927 This function expects to be in the right *tramp* buffer."
4930 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
4931 ;; `remove' is in CL, and we want to avoid CL dependencies.
4934 (setq d (car dirlist))
4935 (setq dirlist (cdr dirlist))
4936 (unless (char-equal ?~ (aref d 0))
4937 (setq newdl (cons d newdl))))
4938 (setq dirlist (nreverse newdl))))
4940 multi-method method user host
4941 (format (concat "while read d; "
4942 "do if test -x $d/%s -a -f $d/%s; "
4943 "then echo tramp_executable $d/%s; "
4944 "break; fi; done <<'EOF'")
4945 progname progname progname))
4947 (tramp-send-command multi-method method user host d))
4949 (tramp-send-command multi-method method user host "EOF")
4950 (tramp-wait-for-output)
4951 (goto-char (point-max))
4952 (when (search-backward "tramp_executable " nil t)
4953 (skip-chars-forward "^ ")
4954 (skip-chars-forward " ")
4955 (buffer-substring (point) (tramp-line-end-position)))))
4957 (defun tramp-set-remote-path (multi-method method user host var dirlist)
4958 "Sets the remote environment VAR to existing directories from DIRLIST.
4959 I.e., for each directory in DIRLIST, it is tested whether it exists and if
4960 so, it is added to the environment variable VAR."
4961 (let ((existing-dirs
4966 (tramp-make-tramp-file-name multi-method method user host x))
4968 (tramp-make-tramp-file-name multi-method method user host x)))
4972 multi-method method user host
4974 (mapconcat 'identity (delq nil existing-dirs) ":")
4976 (tramp-wait-for-output)))
4978 ;; -- communication with external shell --
4980 (defun tramp-find-file-exists-command (multi-method method user host)
4981 "Find a command on the remote host for checking if a file exists.
4982 Here, we are looking for a command which has zero exit status if the
4983 file exists and nonzero exit status otherwise."
4984 (make-local-variable 'tramp-file-exists-command)
4985 (tramp-message 9 "Finding command to check if file exists")
4987 (tramp-make-tramp-file-name
4988 multi-method method user host
4989 "/")) ;assume this file always exists
4991 (tramp-make-tramp-file-name
4992 multi-method method user host
4993 "/ this file does not exist "))) ;assume this never exists
4994 ;; The algorithm is as follows: we try a list of several commands.
4995 ;; For each command, we first run `$cmd /' -- this should return
4996 ;; true, as the root directory always exists. And then we run
4997 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
4998 ;; does not exist. This should return false. We use the first
4999 ;; command we find that seems to work.
5000 ;; The list of commands to try is as follows:
5001 ;; `ls -d' This works on most systems, but NetBSD 1.4
5002 ;; has a bug: `ls' always returns zero exit
5003 ;; status, even for files which don't exist.
5004 ;; `test -e' Some Bourne shells have a `test' builtin
5005 ;; which does not know the `-e' option.
5006 ;; `/bin/test -e' For those, the `test' binary on disk normally
5007 ;; provides the option. Alas, the binary
5008 ;; is sometimes `/bin/test' and sometimes it's
5010 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5012 (and (setq tramp-file-exists-command "test -e %s")
5013 (file-exists-p existing)
5014 (not (file-exists-p nonexisting)))
5015 (and (setq tramp-file-exists-command "/bin/test -e %s")
5016 (file-exists-p existing)
5017 (not (file-exists-p nonexisting)))
5018 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5019 (file-exists-p existing)
5020 (not (file-exists-p nonexisting)))
5021 (and (setq tramp-file-exists-command "ls -d %s")
5022 (file-exists-p existing)
5023 (not (file-exists-p nonexisting))))
5024 (error "Couldn't find command to check if file exists."))))
5027 ;; CCC test ksh or bash found for tilde expansion?
5028 (defun tramp-find-shell (multi-method method user host)
5029 "Find a shell on the remote host which groks tilde expansion."
5031 (tramp-send-command multi-method method user host "echo ~root")
5032 (tramp-wait-for-output)
5034 ((string-match "^~root$" (buffer-string))
5036 (or (tramp-find-executable multi-method method user host
5037 "bash" tramp-remote-path t)
5038 (tramp-find-executable multi-method method user host
5039 "ksh" tramp-remote-path t)))
5041 (error "Couldn't find a shell which groks tilde expansion"))
5042 ;; Find arguments for this shell.
5043 (let ((alist tramp-sh-extra-args)
5045 (while (and alist (null extra-args))
5046 (setq item (pop alist))
5047 (when (string-match (car item) shell)
5048 (setq extra-args (cdr item))))
5049 (when extra-args (setq shell (concat shell " " extra-args))))
5051 5 "Starting remote shell `%s' for tilde expansion..." shell)
5053 multi-method method user host
5054 (concat "PS1='$ ' exec " shell)) ;
5055 (unless (tramp-wait-for-regexp
5056 (get-buffer-process (current-buffer))
5057 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'"
5058 tramp-shell-prompt-pattern shell-prompt-pattern))
5059 (pop-to-buffer (buffer-name))
5060 (error "Couldn't find remote `%s' prompt." shell))
5062 9 "Setting remote shell prompt...")
5063 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5064 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5065 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5067 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5068 tramp-rsh-end-of-line
5070 tramp-rsh-end-of-line
5071 tramp-rsh-end-of-line))
5072 (tramp-wait-for-output)
5074 9 "Setting remote shell prompt...done")
5076 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5077 (tramp-get-method-parameter
5078 multi-method method user host 'tramp-remote-sh))))))
5080 (defun tramp-check-ls-command (multi-method method user host cmd)
5081 "Checks whether the given `ls' executable groks `-n'.
5082 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5083 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5085 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5086 (when (file-executable-p
5087 (tramp-make-tramp-file-name multi-method method user host cmd))
5089 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5091 (tramp-send-command-and-check
5092 multi-method method user host
5093 (format "%s -lnd / >/dev/null"
5095 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5097 (if (zerop result) "okay" "failed"))
5100 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5101 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5102 Returns nil if none was found, else the command is returned."
5105 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5106 ;; It would be better to use the CL function `find', but
5107 ;; we don't want run-time dependencies on CL.
5108 (while (and dl (not result))
5109 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5110 (when (tramp-check-ls-command multi-method method user host x)
5115 (defun tramp-find-ls-command (multi-method method user host)
5116 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5117 \(This option prints numeric user and group ids in a long listing.)"
5118 (tramp-message 9 "Finding a suitable `ls' command")
5120 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5121 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5122 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5124 ;; ------------------------------------------------------------
5125 ;; -- Functions for establishing connection --
5126 ;; ------------------------------------------------------------
5128 ;; The following functions are actions to be taken when seeing certain
5129 ;; prompts from the remote host. See the variable
5130 ;; `tramp-actions-before-shell' for usage of these functions.
5132 (defun tramp-action-login (p multi-method method user host)
5133 "Send the login name."
5134 (tramp-message 9 "Sending login name `%s'"
5135 (or user (user-login-name)))
5137 (process-send-string nil (concat (or user (user-login-name))
5138 tramp-rsh-end-of-line)))
5140 (defun tramp-action-password (p multi-method method user host)
5141 "Query the user for a password."
5142 (let ((pw-prompt (match-string 0)))
5143 (tramp-message 9 "Sending password")
5144 (tramp-enter-password p pw-prompt user host)))
5146 (defun tramp-action-succeed (p multi-method method user host)
5147 "Signal success in finding shell prompt."
5148 (tramp-message 9 "Found remote shell prompt.")
5150 (throw 'tramp-action 'ok))
5152 (defun tramp-action-permission-denied (p multi-method method user host)
5153 "Signal permission denied."
5154 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5155 (tramp-message 9 "Permission denied by remote host.")
5157 (throw 'tramp-action 'permission-denied))
5159 (defun tramp-action-yesno (p multi-method method user host)
5160 "Ask the user for confirmation using `yes-or-no-p'.
5161 Send \"yes\" to remote process on confirmation, abort otherwise.
5162 See also `tramp-action-yn'."
5163 (save-window-excursion
5164 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5165 (unless (yes-or-no-p (match-string 0))
5168 (throw 'tramp-action 'permission-denied))
5169 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5172 (defun tramp-action-yn (p multi-method method user host)
5173 "Ask the user for confirmation using `y-or-n-p'.
5174 Send \"y\" to remote process on confirmation, abort otherwise.
5175 See also `tramp-action-yesno'."
5176 (save-window-excursion
5177 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5178 (unless (y-or-n-p (match-string 0))
5180 (throw 'tramp-action 'permission-denied))
5182 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5184 (defun tramp-action-terminal (p multi-method method user host)
5185 "Tell the remote host which terminal type to use.
5186 The terminal type can be configured with `tramp-terminal-type'."
5187 (tramp-message 9 "Setting `%s' as terminal type."
5188 tramp-terminal-type)
5190 (process-send-string nil (concat tramp-terminal-type
5191 tramp-rsh-end-of-line)))
5193 (defun tramp-action-process-alive (p multi-method method user host)
5194 "Check whether a process has finished."
5195 (unless (memq (process-status p) '(run open))
5196 (throw 'tramp-action 'process-died)))
5198 (defun tramp-action-out-of-band (p multi-method method user host)
5199 "Check whether an out-of-band copy has finished."
5200 (cond ((and (memq (process-status p) '(stop exit))
5201 (zerop (process-exit-status p)))
5202 (tramp-message 9 "Process has finished.")
5203 (throw 'tramp-action 'ok))
5204 ((or (and (memq (process-status p) '(stop exit))
5205 (not (zerop (process-exit-status p))))
5206 (memq (process-status p) '(signal)))
5207 (tramp-message 9 "Process has died.")
5208 (throw 'tramp-action 'process-died))
5211 ;; The following functions are specifically for multi connections.
5213 (defun tramp-multi-action-login (p method user host)
5214 "Send the login name."
5215 (tramp-message 9 "Sending login name `%s'" user)
5217 (process-send-string p (concat user tramp-rsh-end-of-line)))
5219 (defun tramp-multi-action-password (p method user host)
5220 "Query the user for a password."
5221 (tramp-message 9 "Sending password")
5222 (tramp-enter-password p (match-string 0) user host))
5224 (defun tramp-multi-action-succeed (p method user host)
5225 "Signal success in finding shell prompt."
5226 (tramp-message 9 "Found shell prompt on `%s'" host)
5228 (throw 'tramp-action 'ok))
5230 (defun tramp-multi-action-permission-denied (p method user host)
5231 "Signal permission denied."
5232 (tramp-message 9 "Permission denied by remote host `%s'" host)
5235 (throw 'tramp-action 'permission-denied))
5237 (defun tramp-multi-action-process-alive (p method user host)
5238 "Check whether a process has finished."
5239 (unless (memq (process-status p) '(run open))
5240 (throw 'tramp-action 'process-died)))
5242 ;; Functions for processing the actions.
5244 (defun tramp-process-one-action (p multi-method method user host actions)
5245 "Wait for output from the shell and perform one action."
5246 (let (found item pattern action todo)
5248 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5249 (with-timeout (60 (throw 'tramp-action 'timeout))
5251 (accept-process-output p 1)
5252 (goto-char (point-min))
5255 (goto-char (point-min))
5256 (setq item (pop todo))
5257 (setq pattern (symbol-value (nth 0 item)))
5258 (setq action (nth 1 item))
5259 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5261 (when (re-search-forward (concat pattern "\\'") nil t)
5262 (setq found (funcall action p multi-method method user host)))))
5265 (defun tramp-process-actions (p multi-method method user host actions)
5266 "Perform actions until success."
5269 (tramp-message 9 "Waiting for prompts from remote shell")
5271 (catch 'tramp-action
5272 (tramp-process-one-action
5273 p multi-method method user host actions)
5275 (unless (eq exit 'ok)
5276 (tramp-clear-passwd user host)
5277 (error "Login failed"))))
5279 ;; For multi-actions.
5281 (defun tramp-process-one-multi-action (p method user host actions)
5282 "Wait for output from the shell and perform one action."
5283 (let (found item pattern action todo)
5285 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5286 (with-timeout (60 (throw 'tramp-action 'timeout))
5288 (accept-process-output p 1)
5290 (goto-char (point-min))
5292 (goto-char (point-min))
5293 (setq item (pop todo))
5294 (setq pattern (symbol-value (nth 0 item)))
5295 (setq action (nth 1 item))
5296 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5298 (when (re-search-forward (concat pattern "\\'") nil t)
5299 (setq found (funcall action p method user host)))))
5302 (defun tramp-process-multi-actions (p method user host actions)
5303 "Perform actions until success."
5306 (tramp-message 9 "Waiting for prompts from remote shell")
5308 (catch 'tramp-action
5309 (tramp-process-one-multi-action p method user host actions)
5311 (unless (eq exit 'ok)
5312 (tramp-clear-passwd user host)
5313 (error "Login failed"))))
5315 ;; Functions to execute when we have seen the remote shell prompt but
5316 ;; before we exec the Bourne-ish shell. Note that these commands
5317 ;; might be sent to any shell, not just a Bourne-ish shell. This
5318 ;; means that the commands need to work in all shells. (It is also
5319 ;; okay for some commands to just fail with an error message, but
5320 ;; please make sure that they at least don't crash the odd shell people
5321 ;; might be running...)
5322 (defun tramp-process-initial-commands (p
5323 multi-method method user host
5325 "Send list of commands to remote host, in order."
5328 (setq cmd (pop commands))
5330 (tramp-message 10 "Sending command to remote shell: %s"
5332 (tramp-send-command multi-method method user host cmd nil t)
5333 (tramp-barf-if-no-shell-prompt
5334 p 60 "Remote shell command failed: %s" cmd))
5337 ;; The actual functions for opening connections.
5339 (defun tramp-open-connection-telnet (multi-method method user host)
5340 "Open a connection using a telnet METHOD.
5341 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5342 login prompt, then sends the user name USER, then waits for a remote
5343 password prompt. It queries the user for the password, then sends the
5344 password to the remote host.
5346 If USER is nil, uses value returned by `(user-login-name)' instead.
5348 Recognition of the remote shell prompt is based on the variables
5349 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5352 Please note that it is NOT possible to use this connection method
5353 together with an out-of-band transfer method! You must use an inline
5356 Maybe the different regular expressions need to be tuned.
5358 * Actually, the telnet program as well as the args to be used can be
5359 specified in the method parameters, see the variable `tramp-methods'."
5361 (when (tramp-method-out-of-band-p multi-method method user host)
5362 (error "Cannot use out-of-band method `%s' with telnet connection method"
5365 (error "Cannot multi-connect using telnet connection method"))
5366 (tramp-pre-connection multi-method method user host)
5367 (tramp-message 7 "Opening connection for %s@%s using %s..."
5368 (or user (user-login-name)) host method)
5369 (let ((process-environment (copy-sequence process-environment)))
5370 (setenv "TERM" tramp-terminal-type)
5371 (let* ((default-directory (tramp-temporary-file-directory))
5372 ;; If we omit the conditional here, then we would use
5373 ;; `undecided-dos' in some cases. With the conditional,
5374 ;; we use nil in these cases. Which one is right?
5375 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5376 (> emacs-major-version 20))
5377 tramp-dos-coding-system))
5378 (p (apply 'start-process
5379 (tramp-buffer-name multi-method method user host)
5380 (tramp-get-buffer multi-method method user host)
5381 (tramp-get-method-parameter
5383 (tramp-find-method multi-method method user host)
5384 user host 'tramp-login-program)
5386 (tramp-get-method-parameter
5388 (tramp-find-method multi-method method user host)
5389 user host 'tramp-login-args)))
5392 (tramp-set-process-query-on-exit-flag p nil)
5393 (set-buffer (tramp-get-buffer multi-method method user host))
5395 (tramp-process-actions p multi-method method user host
5396 tramp-actions-before-shell)
5397 (tramp-open-connection-setup-interactive-shell
5398 p multi-method method user host)
5399 (tramp-post-connection multi-method method user host)))))
5402 (defun tramp-open-connection-rsh (multi-method method user host)
5403 "Open a connection using an rsh METHOD.
5404 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5405 password or shell prompt. If a password prompt is seen, the user is
5406 queried for a password, this function sends the password to the remote
5407 host and waits for a shell prompt.
5409 If USER is nil, start the command `rsh HOST'[*] instead
5411 Recognition of the remote shell prompt is based on the variables
5412 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5415 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5416 be a port number for ssh, and \"-p yy\" will be added to the list of
5417 arguments, and xx will be used as the host name to connect to.
5419 * Actually, the rsh program to be used can be specified in the
5420 method parameters, see the variable `tramp-methods'."
5423 (error "Cannot multi-connect using rsh connection method"))
5424 (tramp-pre-connection multi-method method user host)
5425 (if (and user (not (string= user "")))
5426 (tramp-message 7 "Opening connection for %s@%s using %s..."
5428 (tramp-message 7 "Opening connection at %s using %s..." host method))
5429 (let ((process-environment (copy-sequence process-environment))
5430 (bufnam (tramp-buffer-name multi-method method user host))
5431 (buf (tramp-get-buffer multi-method method user host))
5432 (login-program (tramp-get-method-parameter
5434 (tramp-find-method multi-method method user host)
5435 user host 'tramp-login-program))
5436 (login-args (tramp-get-method-parameter
5438 (tramp-find-method multi-method method user host)
5439 user host 'tramp-login-args))
5441 ;; The following should be changed. We need a more general
5442 ;; mechanism to parse extra host args.
5443 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5444 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5445 (setq real-host (match-string 1 host)))
5446 (setenv "TERM" tramp-terminal-type)
5447 (let* ((default-directory (tramp-temporary-file-directory))
5448 ;; If we omit the conditional, we would use
5449 ;; `undecided-dos' in some cases. With the conditional,
5450 ;; we use nil in these cases. Which one is right?
5451 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5452 (> emacs-major-version 20))
5453 tramp-dos-coding-system))
5454 (p (if (and user (not (string= user "")))
5455 (apply #'start-process bufnam buf login-program
5456 real-host "-l" user login-args)
5457 (apply #'start-process bufnam buf login-program
5458 real-host login-args)))
5460 (tramp-set-process-query-on-exit-flag p nil)
5463 (tramp-process-actions p multi-method method user host
5464 tramp-actions-before-shell)
5465 (tramp-message 7 "Initializing remote shell")
5466 (tramp-open-connection-setup-interactive-shell
5467 p multi-method method user host)
5468 (tramp-post-connection multi-method method user host)))))
5470 (defun tramp-open-connection-su (multi-method method user host)
5471 "Open a connection using the `su' program with METHOD.
5472 This starts `su - USER', then waits for a password prompt. The HOST
5473 name must be equal to the local host name or to `localhost'.
5475 If USER is nil, uses value returned by user-login-name instead.
5477 Recognition of the remote shell prompt is based on the variables
5478 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5479 set up correctly. Note that the other user may have a different shell
5480 prompt than you do, so it is not at all unlikely that the variable
5481 `shell-prompt-pattern' is set up wrongly!"
5483 (when (tramp-method-out-of-band-p multi-method method user host)
5484 (error "Cannot use out-of-band method `%s' with `su' connection method"
5486 (unless (or (string-match (concat "^" (regexp-quote host))
5488 (string= "localhost" host)
5491 "Cannot connect to different host `%s' with `su' connection method"
5493 (tramp-pre-connection multi-method method user host)
5494 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5495 (or user "<root>") method)
5496 (let ((process-environment (copy-sequence process-environment)))
5497 (setenv "TERM" tramp-terminal-type)
5498 (let* ((default-directory (tramp-temporary-file-directory))
5499 ;; If we omit the conditional, we use `undecided-dos' in
5500 ;; some cases. With the conditional, we use nil in these
5501 ;; cases. What's the difference? Which one is right?
5502 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5503 (> emacs-major-version 20))
5504 tramp-dos-coding-system))
5505 (p (apply 'start-process
5506 (tramp-buffer-name multi-method method user host)
5507 (tramp-get-buffer multi-method method user host)
5508 (tramp-get-method-parameter
5510 (tramp-find-method multi-method method user host)
5511 user host 'tramp-login-program)
5514 (format-spec x `((?u . ,(or user "root")))))
5515 (tramp-get-method-parameter
5517 (tramp-find-method multi-method method user host)
5518 user host 'tramp-login-args))))
5521 (tramp-set-process-query-on-exit-flag p nil)
5522 (set-buffer (tramp-get-buffer multi-method method user host))
5523 (tramp-process-actions p multi-method method user host
5524 tramp-actions-before-shell)
5525 (tramp-open-connection-setup-interactive-shell
5526 p multi-method method user host)
5527 (tramp-post-connection multi-method method
5530 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5531 ;; handle not giving a user name in the "file name".
5533 ;; This is more difficult than for the single-hop method. In the
5534 ;; multi-hop-method, the desired behaviour should be that the
5535 ;; user must specify names for the telnet hops of which the user
5536 ;; name is different than the "original" name (or different from
5537 ;; the previous hop.
5538 (defun tramp-open-connection-multi (multi-method method user host)
5539 "Open a multi-hop connection using METHOD.
5540 This uses a slightly changed file name syntax. The idea is to say
5541 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5542 This will use telnet to log in as u1 to h1, then use rsh from there to
5543 log in as u2 to h2."
5545 (unless multi-method
5546 (error "Multi-hop open connection function called on non-multi method"))
5547 (when (tramp-method-out-of-band-p multi-method method user host)
5548 (error "No out of band multi-hop connections"))
5549 (unless (and (arrayp method) (not (stringp method)))
5550 (error "METHOD must be an array of strings for multi methods"))
5551 (unless (and (arrayp user) (not (stringp user)))
5552 (error "USER must be an array of strings for multi methods"))
5553 (unless (and (arrayp host) (not (stringp host)))
5554 (error "HOST must be an array of strings for multi methods"))
5555 (unless (and (= (length method) (length user))
5556 (= (length method) (length host)))
5557 (error "Arrays METHOD, USER, HOST must have equal length"))
5558 (tramp-pre-connection multi-method method user host)
5559 (tramp-message 7 "Opening `%s' connection..." multi-method)
5560 (let ((process-environment (copy-sequence process-environment)))
5561 (setenv "TERM" tramp-terminal-type)
5562 (let* ((default-directory (tramp-temporary-file-directory))
5563 ;; If we omit the conditional, we use `undecided-dos' in
5564 ;; some cases. With the conditional, we use nil in these
5565 ;; cases. What's the difference? Which one is right?
5566 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5567 (> emacs-major-version 20))
5568 tramp-dos-coding-system))
5569 (p (start-process (tramp-buffer-name multi-method method user host)
5570 (tramp-get-buffer multi-method method user host)
5571 tramp-multi-sh-program))
5572 (num-hops (length method))
5574 (tramp-set-process-query-on-exit-flag p nil)
5575 (tramp-message 9 "Waiting 60s for local shell to come up...")
5576 (unless (tramp-wait-for-regexp
5577 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5578 shell-prompt-pattern tramp-shell-prompt-pattern))
5579 (pop-to-buffer (buffer-name))
5581 (error "Couldn't find local shell prompt"))
5582 ;; Now do all the connections as specified.
5583 (while (< i num-hops)
5584 (let* ((m (aref method i))
5587 (entry (assoc m tramp-multi-connection-function-alist))
5588 (multi-func (nth 1 entry))
5589 (command (nth 2 entry)))
5590 ;; The multi-funcs don't need to do save-match-data, as that
5592 (funcall multi-func p m u h command)
5595 (tramp-open-connection-setup-interactive-shell
5596 p multi-method method user host)
5597 (tramp-post-connection multi-method method user host)))))
5599 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5600 ;; of no user name provided. Hack to make it work as it did before:
5601 ;; changed `user' to `(or user (user-login-name))' in the places where
5602 ;; the value is actually used.
5603 (defun tramp-multi-connect-telnet (p method user host command)
5604 "Issue `telnet' command.
5605 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5606 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5607 the host name, and `%n' is replaced with an end of line character, as
5608 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5611 If USER is nil, uses the return value of (user-login-name) instead."
5612 (let ((cmd (format-spec command
5613 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5614 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5617 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5618 (process-send-string p cmd)
5619 (tramp-process-multi-actions p method user host
5620 tramp-multi-actions)))
5622 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5623 ;; of no user name provided. Hack to make it work as it did before:
5624 ;; changed `user' to `(or user (user-login-name))' in the places where
5625 ;; the value is actually used.
5626 (defun tramp-multi-connect-rlogin (p method user host command)
5627 "Issue `rlogin' command.
5628 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5629 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5630 with the user name, `%h' will be replaced with the host name, and `%n'
5631 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5632 `%%' if you want to use a literal percent character.
5634 If USER is nil, uses the return value of (user-login-name) instead."
5635 (let ((cmd (format-spec command `((?h . ,host)
5636 (?u . ,(or user (user-login-name)))
5637 (?n . ,tramp-rsh-end-of-line))))
5638 (cmd1 (format-spec command `((?h . ,host)
5639 (?u . ,(or user (user-login-name)))
5643 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5644 (process-send-string p cmd)
5645 (tramp-process-multi-actions p method user host
5646 tramp-multi-actions)))
5648 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5649 ;; of no user name provided. Hack to make it work as it did before:
5650 ;; changed `user' to `(or user (user-login-name))' in the places where
5651 ;; the value is actually used.
5652 (defun tramp-multi-connect-su (p method user host command)
5653 "Issue `su' command.
5654 Uses shell COMMAND to issue a `su' command to log in as USER on
5655 HOST. The HOST name is ignored, this just changes the user id on the
5656 host currently logged in to.
5658 If USER is nil, uses the return value of (user-login-name) instead.
5660 You can use percent escapes in the COMMAND. `%u' is replaced with the
5661 user name, and `%n' is replaced with the value of
5662 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5664 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5665 (?n . ,tramp-rsh-end-of-line))))
5666 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5670 (tramp-message 9 "Sending su command `%s'" cmd1)
5671 (process-send-string p cmd)
5672 (tramp-process-multi-actions p method user host
5673 tramp-multi-actions)))
5675 ;; Utility functions.
5677 (defun tramp-wait-for-regexp (proc timeout regexp)
5678 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5679 Expects the output of PROC to be sent to the current buffer. Returns
5680 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5683 (start-time (current-time)))
5685 ;; Work around a bug in XEmacs 21, where the timeout
5686 ;; expires faster than it should. This degenerates
5687 ;; to polling for buggy XEmacsen, but oh, well.
5688 (while (and (not found)
5689 (< (tramp-time-diff (current-time) start-time)
5691 (with-timeout (timeout)
5693 (accept-process-output proc 1)
5694 (unless (memq (process-status proc) '(run open))
5695 (error "Process has died"))
5696 (goto-char (point-min))
5697 (setq found (when (re-search-forward regexp nil t)
5698 (tramp-match-string-list)))))))
5701 (accept-process-output proc 1)
5702 (unless (memq (process-status proc) '(run open))
5703 (error "Process has died"))
5704 (goto-char (point-min))
5705 (setq found (when (re-search-forward regexp nil t)
5706 (tramp-match-string-list))))))
5707 (when tramp-debug-buffer
5709 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5710 tramp-current-user tramp-current-host)
5711 (point-min) (point-max))
5715 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5716 tramp-current-user tramp-current-host))
5717 (goto-char (point-max))
5718 (insert "[[Regexp `" regexp "' not found"
5719 (if timeout (format " in %d secs" timeout) "")
5723 (defun tramp-wait-for-shell-prompt (proc timeout)
5724 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5725 See `tramp-wait-for-regexp' for more details.
5726 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5727 and `tramp-shell-prompt-pattern'."
5728 (tramp-wait-for-regexp
5730 (format "\\(%s\\|%s\\)\\'"
5731 shell-prompt-pattern tramp-shell-prompt-pattern)))
5733 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5734 "Wait for shell prompt and barf if none appears.
5735 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5736 seconds. If not, it produces an error message with the given ERROR-ARGS."
5737 (unless (tramp-wait-for-shell-prompt proc timeout)
5738 (pop-to-buffer (buffer-name))
5739 (apply 'error error-args)))
5741 (defun tramp-enter-password (p prompt user host)
5742 "Prompt for a password and send it to the remote end.
5743 Uses PROMPT as a prompt and sends the password to process P."
5744 (let ((pw (tramp-read-passwd user host prompt)))
5746 (process-send-string
5748 (or (tramp-get-method-parameter
5749 tramp-current-multi-method
5750 tramp-current-method
5753 'tramp-password-end-of-line)
5754 tramp-default-password-end-of-line)))))
5756 ;; HHH: Not Changed. This might handle the case where USER is not
5757 ;; given in the "File name" very poorly. Then, the local
5758 ;; variable tramp-current-user will be set to nil.
5759 (defun tramp-pre-connection (multi-method method user host)
5760 "Do some setup before actually logging in.
5761 METHOD, USER and HOST specify the connection."
5762 (set-buffer (tramp-get-buffer multi-method method user host))
5763 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5764 (set (make-local-variable 'tramp-current-method) method)
5765 (set (make-local-variable 'tramp-current-user) user)
5766 (set (make-local-variable 'tramp-current-host) host)
5767 (set (make-local-variable 'inhibit-eol-conversion) nil)
5770 (defun tramp-open-connection-setup-interactive-shell
5771 (p multi-method method user host)
5772 "Set up an interactive shell.
5773 Mainly sets the prompt and the echo correctly. P is the shell process
5774 to set up. METHOD, USER and HOST specify the connection."
5775 ;; Wait a bit in case the remote end feels like sending a little
5776 ;; junk first. It seems that fencepost.gnu.org does this when doing
5777 ;; a Kerberos login.
5779 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5780 (tramp-process-initial-commands p multi-method method user host
5781 tramp-initial-commands)
5782 ;; It is useful to set the prompt in the following command because
5783 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5784 ;; about and thus /bin/sh will display a strange prompt. For
5785 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
5786 ;; the current working directory but /bin/sh will display a dollar
5787 ;; sign. The following command line sets $PS1 to a sane value, and
5788 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
5789 ;; Pittman reports that the unusual positioning of the single quotes
5790 ;; makes it work under `rc', too. We also unset the variable $ENV
5791 ;; because that is read by some sh implementations (eg, bash when
5792 ;; called as sh) on startup; this way, we avoid the startup file
5794 (tramp-send-command-internal
5795 multi-method method user host
5796 (format "exec env 'ENV=' 'PS1=$ ' %s"
5797 (tramp-get-method-parameter
5798 multi-method method user host 'tramp-remote-sh))
5799 (format "remote `%s' to come up"
5800 (tramp-get-method-parameter
5801 multi-method method user host 'tramp-remote-sh)))
5802 (tramp-barf-if-no-shell-prompt
5804 "Remote `%s' didn't come up. See buffer `%s' for details"
5805 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
5807 (tramp-message 8 "Setting up remote shell environment")
5808 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5809 (tramp-send-command-internal multi-method method user host
5810 "stty -inlcr -echo kill '^U'")
5812 ;; Ignore garbage after stty command.
5813 (tramp-send-command-internal multi-method method user host
5816 (tramp-send-command-internal multi-method method user host
5817 "TERM=dumb; export TERM")
5818 ;; Try to set up the coding system correctly.
5819 ;; CCC this can't be the right way to do it. Hm.
5822 (tramp-message 9 "Determining coding system")
5823 (tramp-send-command-internal multi-method method user host
5824 "echo foo ; echo bar")
5825 (goto-char (point-min))
5826 (if (featurep 'mule)
5827 ;; Use MULE to select the right EOL convention for communicating
5828 ;; with the process.
5829 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
5830 cs-decode cs-encode)
5831 (when (symbolp cs) (setq cs (cons cs cs)))
5832 (setq cs-decode (car cs))
5833 (setq cs-encode (cdr cs))
5834 (unless cs-decode (setq cs-decode 'undecided))
5835 (unless cs-encode (setq cs-encode 'undecided))
5836 (setq cs-encode (tramp-coding-system-change-eol-conversion
5838 (when (search-forward "\r" nil t)
5839 (setq cs-decode (tramp-coding-system-change-eol-conversion
5841 (set-buffer-process-coding-system cs-decode cs-encode))
5842 ;; Look for ^M and do something useful if found.
5843 (when (search-forward "\r" nil t)
5844 ;; We have found a ^M but cannot frob the process coding system
5845 ;; because we're running on a non-MULE Emacs. Let's try
5848 (tramp-message 9 "Trying `stty -onlcr'")
5849 (tramp-send-command-internal multi-method method user host
5853 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
5854 (tramp-send-command-internal
5855 multi-method method user host
5856 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
5858 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
5859 (tramp-send-command-internal multi-method method user host
5860 "set +o vi +o emacs")
5862 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
5863 (tramp-send-command-internal
5864 multi-method method user host
5865 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
5867 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
5868 (tramp-send-command-internal multi-method method user host
5871 (tramp-message 9 "Setting shell prompt")
5872 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
5873 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
5874 ;; the last time we sent a command, to avoid tramp-send-command to send
5875 ;; "echo are you awake".
5876 (setq tramp-last-cmd-time (current-time))
5878 multi-method method user host
5879 (format "PS1='%s%s%s'; PS2=''; PS3=''"
5880 tramp-rsh-end-of-line
5882 tramp-rsh-end-of-line))
5883 (tramp-wait-for-output))
5885 (defun tramp-post-connection (multi-method method user host)
5886 "Prepare a remote shell before being able to work on it.
5887 METHOD, USER and HOST specify the connection.
5888 Among other things, this finds a shell which groks tilde expansion,
5889 tries to find an `ls' command which groks the `-n' option, sets the
5890 locale to C and sets up the remote shell search path."
5891 ;; Search for a good shell before searching for a command which
5892 ;; checks if a file exists. This is done because Tramp wants to use
5893 ;; "test foo; echo $?" to check if various conditions hold, and
5894 ;; there are buggy /bin/sh implementations which don't execute the
5895 ;; "echo $?" part if the "test" part has an error. In particular,
5896 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
5897 ;; with buggy /bin/sh implementations will have a working bash or
5899 (tramp-find-shell multi-method method user host)
5900 ;; Without (sit-for 0.1) at least, my machine will almost always blow
5901 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
5902 ;; output of (tramp-find-shell) to show up along with the output of
5903 ;; (tramp-find-ls-command) testing.
5905 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
5906 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
5908 ;; After much debugging I couldn't find any problem with the implementation
5909 ;; of that function though. The workaround stays for me at least. :/
5911 ;; Daniel Pittman <daniel@danann.net>
5914 (tramp-find-file-exists-command multi-method method user host)
5915 (make-local-variable 'tramp-ls-command)
5916 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
5917 (unless tramp-ls-command
5920 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
5921 (setq tramp-ls-command
5922 (tramp-find-executable multi-method method user host
5923 "ls" tramp-remote-path nil)))
5924 (unless tramp-ls-command
5925 (error "Fatal error: Couldn't find remote executable `ls'"))
5926 (tramp-message 5 "Using remote command `%s' for getting directory listings"
5928 (tramp-send-command multi-method method user host
5929 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
5930 "return $1" tramp-rsh-end-of-line
5932 (tramp-wait-for-output)
5933 ;; Set remote PATH variable.
5934 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
5935 ;; Tell remote shell to use standard time format, needed for
5936 ;; parsing `ls -l' output.
5937 (tramp-send-command multi-method method user host
5938 "LC_TIME=C; export LC_TIME; echo huhu")
5939 (tramp-wait-for-output)
5940 (tramp-send-command multi-method method user host
5941 "mesg n; echo huhu")
5942 (tramp-wait-for-output)
5943 (tramp-send-command multi-method method user host
5944 "biff n ; echo huhu")
5945 (tramp-wait-for-output)
5946 ;; Unalias ls(1) to work around issues with those silly people who make it
5947 ;; spit out ANSI escapes or whatever.
5948 (tramp-send-command multi-method method user host
5949 "unalias ls; echo huhu")
5950 (tramp-wait-for-output)
5951 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5952 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5953 ;; for otherwise the shell crashes.
5955 (make-local-variable 'tramp-test-groks-nt)
5956 (tramp-send-command multi-method method user host
5958 (tramp-wait-for-output)
5959 (goto-char (point-min))
5960 (setq tramp-test-groks-nt
5961 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
5962 (unless tramp-test-groks-nt
5964 multi-method method user host
5965 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
5966 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
5968 (tramp-wait-for-output)
5969 ;; Send the fallback `uudecode' script.
5971 (tramp-send-string multi-method method user host tramp-uudecode)
5972 (tramp-wait-for-output)
5975 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
5976 (let ((tramp-remote-perl
5977 (or (tramp-find-executable multi-method method user host
5978 "perl5" tramp-remote-path nil)
5979 (tramp-find-executable multi-method method user host
5980 "perl" tramp-remote-path nil))))
5981 (when tramp-remote-perl
5982 (tramp-set-connection-property "perl" tramp-remote-perl
5983 multi-method method user host)
5984 (unless (tramp-method-out-of-band-p multi-method method user host)
5985 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
5987 multi-method method user host
5988 (concat "tramp_encode () {\n"
5989 (format tramp-perl-encode tramp-remote-perl)
5992 (tramp-wait-for-output)
5994 multi-method method user host
5995 (concat "tramp_encode_with_module () {\n"
5996 (format tramp-perl-encode-with-module tramp-remote-perl)
5999 (tramp-wait-for-output)
6000 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6002 multi-method method user host
6003 (concat "tramp_decode () {\n"
6004 (format tramp-perl-decode tramp-remote-perl)
6007 (tramp-wait-for-output)
6009 multi-method method user host
6010 (concat "tramp_decode_with_module () {\n"
6011 (format tramp-perl-decode-with-module tramp-remote-perl)
6014 (tramp-wait-for-output))))
6017 (let ((ln (tramp-find-executable multi-method method user host
6018 "ln" tramp-remote-path nil)))
6020 (tramp-set-connection-property "ln" ln multi-method method user host)))
6022 ;; Find the right encoding/decoding commands to use.
6023 (unless (tramp-method-out-of-band-p multi-method method user host)
6024 (tramp-find-inline-encoding multi-method method user host))
6025 ;; If encoding/decoding command are given, test to see if they work.
6026 ;; CCC: Maybe it would be useful to run the encoder both locally and
6027 ;; remotely to see if they produce the same result.
6028 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6029 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6030 (magic-string "xyzzy"))
6031 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6032 (tramp-kill-process multi-method method user host)
6033 ;; Improve error message and/or error check.
6035 "Must give both decoding and encoding command in method definition"))
6036 (when (and rem-enc rem-dec)
6039 "Checking to see if encoding/decoding commands work on remote host...")
6041 multi-method method user host
6042 (format "echo %s | %s | %s"
6043 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6044 (tramp-wait-for-output)
6045 (unless (looking-at (regexp-quote magic-string))
6046 (tramp-kill-process multi-method method user host)
6047 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6051 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6053 ;; CCC: We should either implement a Perl version of base64 encoding
6054 ;; and decoding. Then we just use that in the last item. The other
6055 ;; alternative is to use the Perl version of UU encoding. But then
6056 ;; we need a Lisp version of uuencode.
6058 ;; Old text from documentation of tramp-methods:
6059 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6060 ;; of the base64 methods instead since base64 encoding is much more
6061 ;; reliable and the commands are more standardized between the different
6062 ;; Unix versions. But if you can't use base64 for some reason, please
6063 ;; note that the default uudecode command does not work well for some
6064 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6065 ;; the following command for uudecode:
6067 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6069 ;; For Irix, no solution is known yet.
6071 (defvar tramp-coding-commands
6072 '(("mimencode -b" "mimencode -u -b"
6073 base64-encode-region base64-decode-region)
6074 ("mmencode -b" "mmencode -u -b"
6075 base64-encode-region base64-decode-region)
6076 ("recode data..base64" "recode base64..data"
6077 base64-encode-region base64-decode-region)
6078 ("uuencode xxx" "uudecode -o /dev/stdout"
6079 tramp-uuencode-region uudecode-decode-region)
6080 ("uuencode xxx" "uudecode -o -"
6081 tramp-uuencode-region uudecode-decode-region)
6082 ("uuencode xxx" "uudecode -p"
6083 tramp-uuencode-region uudecode-decode-region)
6084 ("uuencode xxx" "tramp_uudecode"
6085 tramp-uuencode-region uudecode-decode-region)
6086 ("tramp_encode_with_module" "tramp_decode_with_module"
6087 base64-encode-region base64-decode-region)
6088 ("tramp_encode" "tramp_decode"
6089 base64-encode-region base64-decode-region))
6090 "List of coding commands for inline transfer.
6091 Each item is a list that looks like this:
6093 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6095 The REMOTE-ENCODING should be a string, giving a command accepting a
6096 plain file on standard input and writing the encoded file to standard
6097 output. The REMOTE-DECODING should also be a string, giving a command
6098 accepting an encoded file on standard input and writing the decoded
6099 file to standard output.
6101 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6102 symbols, giving functions. If they are strings, then they can contain
6103 the \"%s\" format specifier. If that specifier is present, the input
6104 filename will be put into the command line at that spot. If the
6105 specifier is not present, the input should be read from standard
6108 If they are functions, they will be called with two arguments, start
6109 and end of region, and are expected to replace the region contents
6110 with the encoded or decoded results, respectively.")
6112 (defun tramp-find-inline-encoding (multi-method method user host)
6113 "Find an inline transfer encoding that works.
6114 Goes through the list `tramp-coding-commands'."
6115 (let ((commands tramp-coding-commands)
6118 (while (and commands (null found))
6119 (setq item (pop commands))
6121 (let ((rem-enc (nth 0 item))
6122 (rem-dec (nth 1 item))
6123 (loc-enc (nth 2 item))
6124 (loc-dec (nth 3 item)))
6125 ;; Check if remote encoding and decoding commands can be
6126 ;; called remotely with null input and output. This makes
6127 ;; sure there are no syntax errors and the command is really
6128 ;; found. Note that we do not redirect stdout to /dev/null,
6129 ;; for two reaons: when checking the decoding command, we
6130 ;; actually check the output it gives. And also, when
6131 ;; redirecting "mimencode" output to /dev/null, then as root
6132 ;; it might change the permissions of /dev/null!
6133 (tramp-message-for-buffer
6134 multi-method method user host 9
6135 "Checking remote encoding command `%s' for sanity" rem-enc)
6136 (unless (zerop (tramp-send-command-and-check
6137 multi-method method user host
6138 (format "%s </dev/null" rem-enc) t))
6139 (throw 'wont-work nil))
6140 (tramp-message-for-buffer
6141 multi-method method user host 9
6142 "Checking remote decoding command `%s' for sanity" rem-dec)
6143 (unless (zerop (tramp-send-command-and-check
6144 multi-method method user host
6145 (format "echo %s | %s | %s"
6146 magic rem-enc rem-dec) t))
6147 (throw 'wont-work nil))
6149 (goto-char (point-min))
6150 (unless (looking-at (regexp-quote magic))
6151 (throw 'wont-work nil)))
6152 ;; If the local encoder or decoder is a string, the
6153 ;; corresponding command has to work locally.
6154 (when (stringp loc-enc)
6155 (tramp-message-for-buffer
6156 multi-method method user host 9
6157 "Checking local encoding command `%s' for sanity" loc-enc)
6158 (unless (zerop (tramp-call-local-coding-command
6160 (throw 'wont-work nil)))
6161 (when (stringp loc-dec)
6162 (tramp-message-for-buffer
6163 multi-method method user host 9
6164 "Checking local decoding command `%s' for sanity" loc-dec)
6165 (unless (zerop (tramp-call-local-coding-command
6167 (throw 'wont-work nil)))
6168 ;; CCC: At this point, maybe we should check that the output
6169 ;; of the commands is correct. But for the moment we will
6170 ;; assume that commands working on empty input will also
6171 ;; work in practice.
6172 (setq found item))))
6173 ;; Did we find something? If not, issue error. If so,
6174 ;; set connection properties.
6176 (error "Couldn't find an inline transfer encoding"))
6177 (let ((rem-enc (nth 0 found))
6178 (rem-dec (nth 1 found))
6179 (loc-enc (nth 2 found))
6180 (loc-dec (nth 3 found)))
6181 (tramp-message 10 "Using remote encoding %s" rem-enc)
6182 (tramp-set-remote-encoding multi-method method user host rem-enc)
6183 (tramp-message 10 "Using remote decoding %s" rem-dec)
6184 (tramp-set-remote-decoding multi-method method user host rem-dec)
6185 (tramp-message 10 "Using local encoding %s" loc-enc)
6186 (tramp-set-local-encoding multi-method method user host loc-enc)
6187 (tramp-message 10 "Using local decoding %s" loc-dec)
6188 (tramp-set-local-decoding multi-method method user host loc-dec))))
6190 (defun tramp-call-local-coding-command (cmd input output)
6191 "Call the local encoding or decoding command.
6192 If CMD contains \"%s\", provide input file INPUT there in command.
6193 Otherwise, INPUT is passed via standard input.
6194 INPUT can also be nil which means `/dev/null'.
6195 OUTPUT can be a string (which specifies a filename), or t (which
6196 means standard output and thus the current buffer), or nil (which
6199 tramp-encoding-shell ;program
6200 (when (and input (not (string-match "%s" cmd)))
6202 (if (eq output t) t nil) ;output
6204 tramp-encoding-command-switch
6205 ;; actual shell command
6207 (if (string-match "%s" cmd) (format cmd input) cmd)
6208 (if (stringp output) (concat "> " output) ""))))
6210 (defun tramp-maybe-open-connection (multi-method method user host)
6211 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6212 Does not do anything if a connection is already open, but re-opens the
6213 connection if a previous connection has died for some reason."
6214 (let ((p (get-buffer-process
6215 (tramp-get-buffer multi-method method user host)))
6217 ;; If too much time has passed since last command was sent, look
6218 ;; whether process is still alive. If it isn't, kill it. When
6219 ;; using ssh, it can sometimes happen that the remote end has hung
6220 ;; up but the local ssh client doesn't recognize this until it
6221 ;; tries to send some data to the remote end. So that's why we
6222 ;; try to send a command from time to time, then look again
6223 ;; whether the process is really alive.
6225 (set-buffer (tramp-get-buffer multi-method method user host))
6226 (when (and tramp-last-cmd-time
6227 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6228 p (processp p) (memq (process-status p) '(run open)))
6230 multi-method method user host "echo are you awake" nil t)
6231 (unless (tramp-wait-for-output 10)
6235 (unless (and p (processp p) (memq (process-status p) '(run open)))
6236 (when (and p (processp p))
6238 (let ((process-connection-type tramp-process-connection-type))
6239 (funcall (tramp-get-method-parameter
6241 (tramp-find-method multi-method method user host)
6242 user host 'tramp-connection-function)
6243 multi-method method user host)))))
6245 (defun tramp-send-command
6246 (multi-method method user host command &optional noerase neveropen)
6247 "Send the COMMAND to USER at HOST (logged in using METHOD).
6248 Erases temporary buffer before sending the command (unless NOERASE
6250 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6251 connection. This is meant to be used from
6252 `tramp-maybe-open-connection' only."
6254 (tramp-maybe-open-connection multi-method method user host))
6255 (setq tramp-last-cmd-time (current-time))
6256 (setq tramp-last-cmd command)
6257 (when tramp-debug-buffer
6259 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6260 (goto-char (point-max))
6261 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6263 (set-buffer (tramp-get-buffer multi-method method user host))
6264 (unless noerase (erase-buffer))
6265 (setq proc (get-buffer-process (current-buffer)))
6266 (process-send-string proc
6267 (concat command tramp-rsh-end-of-line))))
6269 (defun tramp-send-command-internal
6270 (multi-method method user host command &optional msg)
6271 "Send command to remote host and wait for success.
6272 Sends COMMAND, then waits 30 seconds for shell prompt."
6273 (tramp-send-command multi-method method user host command t t)
6275 (tramp-message 9 "Waiting 30s for %s..." msg))
6276 (tramp-barf-if-no-shell-prompt
6278 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6280 (defun tramp-wait-for-output (&optional timeout)
6281 "Wait for output from remote rsh command."
6282 (let ((proc (get-buffer-process (current-buffer)))
6284 (start-time (current-time))
6285 (start-point (point))
6286 (end-of-output (concat "^"
6287 (regexp-quote tramp-end-of-output)
6289 ;; Algorithm: get waiting output. See if last line contains
6290 ;; end-of-output sentinel. If not, wait a bit and again get
6291 ;; waiting output. Repeat until timeout expires or end-of-output
6292 ;; sentinel is seen. Will hang if timeout is nil and
6293 ;; end-of-output sentinel never appears.
6296 ;; Work around an XEmacs bug, where the timeout expires
6297 ;; faster than it should. This degenerates into polling
6298 ;; for buggy XEmacsen, but oh, well.
6299 (while (and (not found)
6300 (< (tramp-time-diff (current-time) start-time)
6302 (with-timeout (timeout)
6304 (accept-process-output proc 1)
6305 (unless (memq (process-status proc) '(run open))
6306 (error "Process has died"))
6307 (goto-char (point-max))
6309 (setq found (looking-at end-of-output))))))
6312 (accept-process-output proc 1)
6313 (unless (memq (process-status proc) '(run open))
6314 (error "Process has died"))
6315 (goto-char (point-max))
6317 (setq found (looking-at end-of-output))))))
6318 ;; At this point, either the timeout has expired or we have found
6319 ;; the end-of-output sentinel.
6321 (goto-char (point-max))
6323 (delete-region (point) (point-max)))
6324 ;; If processing echoes, look for it in the first line and delete.
6325 (when tramp-process-echoes
6327 (goto-char start-point)
6328 (when (looking-at (regexp-quote tramp-last-cmd))
6329 (delete-region (point) (forward-line 1)))))
6330 ;; Add output to debug buffer if appropriate.
6331 (when tramp-debug-buffer
6333 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6334 tramp-current-user tramp-current-host)
6335 (point-min) (point-max))
6339 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6340 tramp-current-user tramp-current-host))
6341 (goto-char (point-max))
6342 (insert "[[Remote prompt `" end-of-output "' not found"
6343 (if timeout (format " in %d secs" timeout) "")
6345 (goto-char (point-min))
6346 ;; Return value is whether end-of-output sentinel was found.
6349 (defun tramp-match-string-list (&optional string)
6350 "Returns list of all match strings.
6351 That is, (list (match-string 0) (match-string 1) ...), according to the
6353 (let* ((nmatches (/ (length (match-data)) 2))
6357 (setq res (cons (match-string i string) res))
6361 (defun tramp-send-command-and-check (multi-method method user host command
6363 "Run COMMAND and check its exit status.
6364 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6365 Sends `echo $?' along with the COMMAND for checking the exit status. If
6366 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6368 If the optional argument SUBSHELL is non-nil, the command is executed in
6369 a subshell, ie surrounded by parentheses."
6370 (tramp-send-command multi-method method user host
6371 (concat (if subshell "( " "")
6373 (if command " 2>/dev/null; " "")
6374 "echo tramp_exit_status $?"
6375 (if subshell " )" " ")))
6376 (tramp-wait-for-output)
6377 (goto-char (point-max))
6378 (unless (search-backward "tramp_exit_status " nil t)
6379 (error "Couldn't find exit status of `%s'" command))
6380 (skip-chars-forward "^ ")
6381 (read (current-buffer)))
6383 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6384 signal fmt &rest args)
6385 "Run COMMAND, check exit status, throw error if exit status not okay.
6386 Similar to `tramp-send-command-and-check' but accepts two more arguments
6387 FMT and ARGS which are passed to `error'."
6388 (unless (zerop (tramp-send-command-and-check
6389 multi-method method user host command subshell))
6390 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6392 (pop-to-buffer (current-buffer))
6393 (funcall 'signal signal (apply 'format fmt args))))
6395 ;; It seems that Tru64 Unix does not like it if long strings are sent
6396 ;; to it in one go. (This happens when sending the Perl
6397 ;; `file-attributes' implementation, for instance.) Therefore, we
6398 ;; have this function which waits a bit at each line.
6399 (defun tramp-send-string
6400 (multi-method method user host string)
6401 "Send the STRING to USER at HOST using METHOD.
6403 The STRING is expected to use Unix line-endings, but the lines sent to
6404 the remote host use line-endings as defined in the variable
6405 `tramp-rsh-end-of-line'."
6406 (let ((proc (get-buffer-process
6407 (tramp-get-buffer multi-method method user host))))
6409 (error "Can't send string to remote host -- not logged in"))
6411 (when tramp-debug-buffer
6413 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6414 (goto-char (point-max))
6415 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6416 ;; replace "\n" by `tramp-rsh-end-of-line'
6418 (mapconcat 'identity
6419 (split-string string "\n")
6420 tramp-rsh-end-of-line))
6421 (unless (or (string= string "")
6422 (string-equal (substring string -1) tramp-rsh-end-of-line))
6423 (setq string (concat string tramp-rsh-end-of-line)))
6425 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6427 (end (length string)))
6429 (tramp-message-for-buffer
6430 multi-method method user host 10
6431 "Sending chunk from %s to %s"
6432 pos (min (+ pos tramp-chunksize) end))
6433 (process-send-string
6434 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6435 (setq pos (+ pos tramp-chunksize))
6437 (process-send-string proc string))))
6439 (defun tramp-send-eof (multi-method method user host)
6440 "Send EOF to the remote end.
6441 METHOD, HOST and USER specify the connection."
6442 (let ((proc (get-buffer-process
6443 (tramp-get-buffer multi-method method user host))))
6445 (error "Can't send EOF to remote host -- not logged in"))
6446 (process-send-eof proc)))
6447 ; (process-send-string proc "\^D")))
6449 (defun tramp-kill-process (multi-method method user host)
6450 "Kill the connection process used by Tramp.
6451 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6452 (let ((proc (get-buffer-process
6453 (tramp-get-buffer multi-method method user host))))
6454 (kill-process proc)))
6456 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6457 "Erase buffer, then discard subsequent garbage.
6458 If `tramp-discard-garbage' is nil, just erase buffer."
6459 (if (not tramp-discard-garbage)
6461 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
6462 (when tramp-debug-buffer
6464 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6465 (goto-char (point-max))
6466 (tramp-insert-with-face
6467 'bold (format "Additional characters detected\n")))))))
6469 (defun tramp-mode-string-to-int (mode-string)
6470 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6471 (let* ((mode-chars (string-to-vector mode-string))
6472 (owner-read (aref mode-chars 1))
6473 (owner-write (aref mode-chars 2))
6474 (owner-execute-or-setid (aref mode-chars 3))
6475 (group-read (aref mode-chars 4))
6476 (group-write (aref mode-chars 5))
6477 (group-execute-or-setid (aref mode-chars 6))
6478 (other-read (aref mode-chars 7))
6479 (other-write (aref mode-chars 8))
6480 (other-execute-or-sticky (aref mode-chars 9)))
6484 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6485 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6487 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6488 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6489 (case owner-execute-or-setid
6490 (?x (tramp-octal-to-decimal "00100"))
6491 (?S (tramp-octal-to-decimal "04000"))
6492 (?s (tramp-octal-to-decimal "04100"))
6494 (t (error "Fourth char `%c' must be one of `xsS-'"
6495 owner-execute-or-setid)))
6497 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6498 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6500 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6501 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6502 (case group-execute-or-setid
6503 (?x (tramp-octal-to-decimal "00010"))
6504 (?S (tramp-octal-to-decimal "02000"))
6505 (?s (tramp-octal-to-decimal "02010"))
6507 (t (error "Seventh char `%c' must be one of `xsS-'"
6508 group-execute-or-setid)))
6510 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6511 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6513 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6514 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6515 (case other-execute-or-sticky
6516 (?x (tramp-octal-to-decimal "00001"))
6517 (?T (tramp-octal-to-decimal "01000"))
6518 (?t (tramp-octal-to-decimal "01001"))
6520 (t (error "Tenth char `%c' must be one of `xtT-'"
6521 other-execute-or-sticky)))))))
6523 (defun tramp-convert-file-attributes (multi-method method user host attr)
6524 "Convert file-attributes ATTR generated by perl script or ls.
6525 Convert file mode bits to string and set virtual device number.
6527 (unless (stringp (nth 8 attr))
6528 ;; Convert file mode bits to string.
6529 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6530 ;; Set virtual device number.
6531 (setcar (nthcdr 11 attr)
6532 (tramp-get-device multi-method method user host))
6535 (defun tramp-get-device (multi-method method user host)
6536 "Returns the virtual device number.
6537 If it doesn't exist, generate a new one."
6538 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6539 (unless (assoc string tramp-devices)
6540 (add-to-list 'tramp-devices
6541 (list string (length tramp-devices))))
6542 (list -1 (nth 1 (assoc string tramp-devices)))))
6544 (defun tramp-file-mode-from-int (mode)
6545 "Turn an integer representing a file mode into an ls(1)-like string."
6546 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6547 (user (logand (lsh mode -6) 7))
6548 (group (logand (lsh mode -3) 7))
6549 (other (logand (lsh mode -0) 7))
6550 (suid (> (logand (lsh mode -9) 4) 0))
6551 (sgid (> (logand (lsh mode -9) 2) 0))
6552 (sticky (> (logand (lsh mode -9) 1) 0)))
6553 (setq user (tramp-file-mode-permissions user suid "s"))
6554 (setq group (tramp-file-mode-permissions group sgid "s"))
6555 (setq other (tramp-file-mode-permissions other sticky "t"))
6556 (concat type user group other)))
6559 (defun tramp-file-mode-permissions (perm suid suid-text)
6560 "Convert a permission bitset into a string.
6561 This is used internally by `tramp-file-mode-from-int'."
6562 (let ((r (> (logand perm 4) 0))
6563 (w (> (logand perm 2) 0))
6564 (x (> (logand perm 1) 0)))
6565 (concat (or (and r "r") "-")
6566 (or (and w "w") "-")
6567 (or (and suid x suid-text) ; suid, execute
6568 (and suid (upcase suid-text)) ; suid, !execute
6569 (and x "x") "-")))) ; !suid
6572 (defun tramp-decimal-to-octal (i)
6573 "Return a string consisting of the octal digits of I.
6574 Not actually used. Use `(format \"%o\" i)' instead?"
6575 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6576 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6578 (t (concat (tramp-decimal-to-octal (/ i 8))
6579 (number-to-string (% i 8))))))
6582 ;;(defun tramp-octal-to-decimal (ostr)
6583 ;; "Given a string of octal digits, return a decimal number."
6584 ;; (cond ((null ostr) 0)
6585 ;; ((string= "" ostr) 0)
6586 ;; (t (let ((last (aref ostr (1- (length ostr))))
6587 ;; (rest (substring ostr 0 (1- (length ostr)))))
6588 ;; (unless (and (>= last ?0)
6590 ;; (error "Not an octal digit: %c" last))
6591 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6592 ;; Kudos to Gerd Moellmann for this suggestion.
6593 (defun tramp-octal-to-decimal (ostr)
6594 "Given a string of octal digits, return a decimal number."
6595 (let ((x (or ostr "")))
6596 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6597 (unless (string-match "\\`[0-7]*\\'" x)
6598 (error "Non-octal junk in string `%s'" x))
6599 (string-to-number ostr 8)))
6601 (defun tramp-shell-case-fold (string)
6602 "Converts STRING to shell glob pattern which ignores case."
6605 (if (equal (downcase c) (upcase c))
6607 (format "[%c%c]" (downcase c) (upcase c))))
6612 ;; ------------------------------------------------------------
6613 ;; -- TRAMP file names --
6614 ;; ------------------------------------------------------------
6615 ;; Conversion functions between external representation and
6616 ;; internal data structure. Convenience functions for internal
6619 (defstruct tramp-file-name multi-method method user host localname)
6621 (defun tramp-tramp-file-p (name)
6622 "Return t iff NAME is a tramp file."
6624 (string-match tramp-file-name-regexp name)))
6626 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6627 ;; to the `user' part of the structure if a user name was not
6628 ;; provided, now it assigns nil.
6629 (defun tramp-dissect-file-name (name)
6630 "Return an `tramp-file-name' structure.
6631 The structure consists of remote method, remote user, remote host and
6632 localname (file name on remote host)."
6634 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6637 (if match (match-string (nth 1 tramp-file-name-structure) name)
6640 (format (nth 0 tramp-multi-file-name-structure)
6641 (nth 0 tramp-multi-file-name-hop-structure)) name)
6642 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6643 (if (and method (member method tramp-multi-methods))
6644 ;; If it's a multi method, the file name structure contains
6645 ;; arrays of method, user and host.
6646 (tramp-dissect-multi-file-name name)
6647 ;; Normal method. First, find out default method.
6648 (unless match (error "Not a tramp file name: %s" name))
6649 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6650 (host (match-string (nth 3 tramp-file-name-structure) name))
6651 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6652 (make-tramp-file-name
6657 :localname localname))))))
6659 (defun tramp-find-default-method (user host)
6660 "Look up the right method to use in `tramp-default-method-alist'."
6661 (let ((choices tramp-default-method-alist)
6662 (method tramp-default-method)
6665 (setq item (pop choices))
6666 (when (and (string-match (nth 0 item) (or host ""))
6667 (string-match (nth 1 item) (or user "")))
6668 (setq method (nth 2 item))
6669 (setq choices nil)))
6672 (defun tramp-find-method (multi-method method user host)
6673 "Return the right method string to use.
6674 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6675 If both MULTI-METHOD and METHOD are nil, do a lookup in
6676 `tramp-default-method-alist'."
6677 (or multi-method method (tramp-find-default-method user host)))
6679 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6680 ;; a user name is not provided in the "file name" very well.
6681 (defun tramp-dissect-multi-file-name (name)
6682 "Not implemented yet."
6683 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6684 (method-index (nth 1 tramp-multi-file-name-structure))
6685 (hops-index (nth 2 tramp-multi-file-name-structure))
6686 (localname-index (nth 3 tramp-multi-file-name-structure))
6687 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6688 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6689 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6690 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6691 method hops len hop-methods hop-users hop-hosts localname)
6692 (unless (string-match (format regexp hop-regexp) name)
6693 (error "Not a multi tramp file name: %s" name))
6694 (setq method (match-string method-index name))
6695 (setq hops (match-string hops-index name))
6696 (setq len (/ (length (match-data t)) 2))
6697 (when (< localname-index 0) (incf localname-index len))
6698 (setq localname (match-string localname-index name))
6700 (while (string-match hop-regexp hops index)
6701 (setq index (match-end 0))
6703 (cons (match-string hop-method-index hops) hop-methods))
6705 (cons (match-string hop-user-index hops) hop-users))
6707 (cons (match-string hop-host-index hops) hop-hosts))))
6708 (make-tramp-file-name
6709 :multi-method method
6710 :method (apply 'vector (reverse hop-methods))
6711 :user (apply 'vector (reverse hop-users))
6712 :host (apply 'vector (reverse hop-hosts))
6713 :localname localname)))
6715 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6716 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6718 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6720 (concat tramp-prefix-format
6721 (when method (concat "%m" tramp-postfix-single-method-format))
6722 (when user (concat "%u" tramp-postfix-user-format))
6723 (when host (concat "%h" tramp-postfix-host-format))
6724 (when localname (concat "%p")))
6725 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6727 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6728 ;; with this when USER is nil?
6729 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6730 "Constructs a tramp file name for a multi-hop method."
6731 (unless tramp-make-multi-tramp-file-format
6732 (error "`tramp-make-multi-tramp-file-format' is nil"))
6733 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6734 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6735 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6736 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6738 (localname (format-spec localname-format `((?p . ,localname))))
6740 (len (length method)))
6742 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6743 (setq hops (concat hops (format-spec hop-format
6744 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6746 (concat prefix hops localname)))
6748 (defun tramp-make-copy-program-file-name (user host localname)
6749 "Create a file name suitable to be passed to `rcp' and workalikes."
6751 (format "%s@%s:%s" user host localname)
6752 (format "%s:%s" host localname)))
6754 (defun tramp-method-out-of-band-p (multi-method method user host)
6755 "Return t if this is an out-of-band method, nil otherwise."
6756 (tramp-get-method-parameter
6758 (tramp-find-method multi-method method user host)
6759 user host 'tramp-copy-program))
6761 ;; Variables local to connection.
6763 (defun tramp-get-ls-command (multi-method method user host)
6765 (tramp-maybe-open-connection multi-method method user host)
6766 (set-buffer (tramp-get-buffer multi-method method user host))
6769 (defun tramp-get-test-groks-nt (multi-method method user host)
6771 (tramp-maybe-open-connection multi-method method user host)
6772 (set-buffer (tramp-get-buffer multi-method method user host))
6773 tramp-test-groks-nt))
6775 (defun tramp-get-file-exists-command (multi-method method user host)
6777 (tramp-maybe-open-connection multi-method method user host)
6778 (set-buffer (tramp-get-buffer multi-method method user host))
6779 tramp-file-exists-command))
6781 (defun tramp-get-remote-perl (multi-method method user host)
6782 (tramp-get-connection-property "perl" nil multi-method method user host))
6784 (defun tramp-get-remote-ln (multi-method method user host)
6785 (tramp-get-connection-property "ln" nil multi-method method user host))
6787 ;; Get a property of a TRAMP connection.
6788 (defun tramp-get-connection-property
6789 (property default multi-method method user host)
6790 "Get the named property for the connection.
6791 If the value is not set for the connection, return `default'"
6792 (tramp-maybe-open-connection multi-method method user host)
6793 (with-current-buffer (tramp-get-buffer multi-method method user host)
6796 (symbol-value (intern (concat "tramp-connection-property-" property)))
6799 ;; Set a property of a TRAMP connection.
6800 (defun tramp-set-connection-property
6801 (property value multi-method method user host)
6802 "Set the named property of a TRAMP connection."
6803 (tramp-maybe-open-connection multi-method method user host)
6804 (with-current-buffer (tramp-get-buffer multi-method method user host)
6805 (set (make-local-variable
6806 (intern (concat "tramp-connection-property-" property)))
6809 ;; Some predefined connection properties.
6810 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6811 (tramp-set-connection-property "remote-encoding" rem-enc
6812 multi-method method user host))
6813 (defun tramp-get-remote-encoding (multi-method method user host)
6814 (tramp-get-connection-property "remote-encoding" nil
6815 multi-method method user host))
6817 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6818 (tramp-set-connection-property "remote-decoding" rem-dec
6819 multi-method method user host))
6820 (defun tramp-get-remote-decoding (multi-method method user host)
6821 (tramp-get-connection-property "remote-decoding" nil
6822 multi-method method user host))
6824 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6825 (tramp-set-connection-property "local-encoding" loc-enc
6826 multi-method method user host))
6827 (defun tramp-get-local-encoding (multi-method method user host)
6828 (tramp-get-connection-property "local-encoding" nil
6829 multi-method method user host))
6831 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6832 (tramp-set-connection-property "local-decoding" loc-dec
6833 multi-method method user host))
6834 (defun tramp-get-local-decoding (multi-method method user host)
6835 (tramp-get-connection-property "local-decoding" nil
6836 multi-method method user host))
6838 (defun tramp-get-method-parameter (multi-method method user host param)
6839 "Return the method parameter PARAM.
6840 If the `tramp-methods' entry does not exist, use the variable PARAM
6842 (unless (boundp param)
6843 (error "Non-existing method parameter `%s'" param))
6844 (let ((entry (assoc param
6845 (assoc (tramp-find-method multi-method method user host)
6849 (symbol-value param))))
6852 ;; Auto saving to a special directory.
6854 (defun tramp-make-auto-save-file-name (fn)
6855 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
6856 (when tramp-auto-save-directory
6857 (unless (file-exists-p tramp-auto-save-directory)
6858 (make-directory tramp-auto-save-directory t)))
6859 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
6860 ;; file name we make sure that jka-compr isn't used for the
6862 (let ((buffer-file-name (expand-file-name
6863 (tramp-subst-strs-in-string '(("_" . "|")
6870 tramp-auto-save-directory)))
6871 (make-auto-save-file-name)))
6873 (defadvice make-auto-save-file-name
6874 (around tramp-advice-make-auto-save-file-name () activate)
6875 "Invoke `tramp-make-auto-save-file-name' for tramp files."
6876 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
6877 tramp-auto-save-directory)
6878 (setq ad-return-value
6879 (tramp-make-auto-save-file-name (buffer-file-name)))
6882 ;; In Emacs < 21.4 and XEmacs < 21.5 autosaved remote files have
6883 ;; permission 666 minus umask. This is a security threat.
6885 (defun tramp-set-auto-save-file-modes ()
6886 "Set permissions of autosaved remote files to the original permissions."
6887 (let ((bfn (buffer-file-name)))
6888 (when (and (stringp bfn)
6889 (tramp-tramp-file-p bfn)
6890 (stringp buffer-auto-save-file-name)
6891 (not (equal bfn buffer-auto-save-file-name))
6892 (not (file-exists-p buffer-auto-save-file-name)))
6893 (write-region "" nil buffer-auto-save-file-name)
6894 (set-file-modes buffer-auto-save-file-name (file-modes bfn)))))
6896 (unless (or (> emacs-major-version 21)
6897 (and (featurep 'xemacs)
6898 (= emacs-major-version 21)
6899 (> emacs-minor-version 4))
6900 (and (not (featurep 'xemacs))
6901 (= emacs-major-version 21)
6902 (or (> emacs-minor-version 3)
6903 (and (string-match "^21\\.3\\.\\([0-9]+\\)" emacs-version)
6904 (>= (string-to-int (match-string 1 emacs-version)) 50)))))
6905 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))
6907 (defun tramp-subst-strs-in-string (alist string)
6908 "Replace all occurrences of the string FROM with TO in STRING.
6909 ALIST is of the form ((FROM . TO) ...)."
6912 (let* ((pr (car alist))
6915 (while (string-match (regexp-quote from) string)
6916 (setq string (replace-match to t t string)))
6917 (setq alist (cdr alist))))
6920 (defun tramp-insert-with-face (face string)
6921 "Insert text with a specific face."
6922 (let ((start (point)))
6924 (add-text-properties start (point) (list 'face face))))
6926 ;; ------------------------------------------------------------
6927 ;; -- Compatibility functions section --
6928 ;; ------------------------------------------------------------
6930 (defun tramp-temporary-file-directory ()
6931 "Return name of directory for temporary files (compat function).
6932 For Emacs, this is the variable `temporary-file-directory', for XEmacs
6933 this is the function `temp-directory'."
6934 (cond ((boundp 'temporary-file-directory)
6935 (symbol-value 'temporary-file-directory))
6936 ((fboundp 'temp-directory)
6937 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
6938 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
6939 (file-name-as-directory (getenv "TEMP")))
6940 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
6941 (file-name-as-directory (getenv "TMP")))
6942 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
6943 (file-name-as-directory (getenv "TMPDIR")))
6944 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
6945 (t (message (concat "Neither `temporary-file-directory' nor "
6946 "`temp-directory' is defined -- using /tmp."))
6947 (file-name-as-directory "/tmp"))))
6949 (defun tramp-read-passwd (user host prompt)
6950 "Read a password from user (compat function).
6951 Invokes `password-read' if available, `read-passwd' else."
6952 (if (functionp 'password-read)
6953 (let* ((key (concat (or user (user-login-name)) "@" host))
6954 (password (apply #'password-read (list prompt key))))
6955 (apply #'password-cache-add (list key password))
6957 (read-passwd prompt)))
6959 (defun tramp-clear-passwd (&optional user host)
6960 "Clear password cache for connection related to current-buffer."
6962 (let ((filename (or buffer-file-name list-buffers-directory "")))
6963 (when (and (functionp 'password-cache-remove)
6964 (or (and user host) (tramp-tramp-file-p filename)))
6965 (let* ((v (when (tramp-tramp-file-p filename)
6966 (tramp-dissect-file-name filename)))
6967 (luser (or user (tramp-file-name-user v) (user-login-name)))
6968 (lhost (or host (tramp-file-name-host v) (system-name)))
6969 (key (concat luser "@" lhost)))
6970 (apply #'password-cache-remove (list key))))))
6972 (defun tramp-time-diff (t1 t2)
6973 "Return the difference between the two times, in seconds.
6974 T1 and T2 are time values (as returned by `current-time' for example).
6976 NOTE: This function will fail if the time difference is too large to
6978 ;; Pacify byte-compiler with `symbol-function'.
6979 (cond ((and (fboundp 'subtract-time)
6980 (fboundp 'float-time))
6981 (funcall (symbol-function 'float-time)
6982 (funcall (symbol-function 'subtract-time) t1 t2)))
6983 ((and (fboundp 'subtract-time)
6984 (fboundp 'time-to-seconds))
6985 (funcall (symbol-function 'time-to-seconds)
6986 (funcall (symbol-function 'subtract-time) t1 t2)))
6987 ((fboundp 'itimer-time-difference)
6989 (symbol-function 'itimer-time-difference)
6990 (if (< (length t1) 3) (append t1 '(0)) t1)
6991 (if (< (length t2) 3) (append t2 '(0)) t2))))
6993 ;; snarfed from Emacs 21 time-date.el; combining
6994 ;; time-to-seconds and subtract-time
6995 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
6996 (list (- (car t1) (car t2) (if borrow 1 0))
6997 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
6998 (+ (* (car time) 65536.0)
7000 (/ (or (nth 2 time) 0) 1000000.0))))))
7002 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7003 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7004 EOL-TYPE can be one of `dos', `unix', or `mac'."
7005 (cond ((fboundp 'coding-system-change-eol-conversion)
7006 (apply #'coding-system-change-eol-conversion
7007 (list coding-system eol-type)))
7008 ((fboundp 'subsidiary-coding-system)
7010 #'subsidiary-coding-system
7012 (cond ((eq eol-type 'dos) 'crlf)
7013 ((eq eol-type 'unix) 'lf)
7014 ((eq eol-type 'mac) 'cr)
7016 (error "Unknown EOL-TYPE `%s', must be %s"
7018 "`dos', `unix', or `mac'"))))))
7019 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7021 (defun tramp-split-string (string pattern)
7022 "Like `split-string' but omit empty strings.
7023 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7024 This is, the first, empty, element is omitted. In XEmacs, the first
7025 element is not omitted.
7027 Note: this function has been written for `tramp-handle-file-truename'.
7028 If you want to use it for something else, you'll have to check whether
7029 it does the right thing."
7030 (delete "" (split-string string pattern)))
7032 (defun tramp-set-process-query-on-exit-flag (process flag)
7033 "Specify if query is needed for process when Emacs is exited.
7034 If the second argument flag is non-nil, Emacs will query the user before
7035 exiting if process is running."
7036 (if (fboundp 'set-process-query-on-exit-flag)
7037 (set-process-query-on-exit-flag process flag)
7038 (funcall (symbol-function 'process-kill-without-query)
7042 ;; ------------------------------------------------------------
7043 ;; -- Kludges section --
7044 ;; ------------------------------------------------------------
7046 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7047 ;; does not deal well with newline characters. Newline is replaced by
7048 ;; backslash newline. But if, say, the string `a backslash newline b'
7049 ;; is passed to a shell, the shell will expand this into "ab",
7050 ;; completely omitting the newline. This is not what was intended.
7051 ;; It does not appear to be possible to make the function
7052 ;; `shell-quote-argument' work with newlines without making it
7053 ;; dependent on the shell used. But within this package, we know that
7054 ;; we will always use a Bourne-like shell, so we use an approach which
7057 ;; The approach is simple: we call `shell-quote-argument', then
7058 ;; massage the newline part of the result.
7060 ;; This function should produce a string which is grokked by a Unix
7061 ;; shell, even if the Emacs is running on Windows. Since this is the
7062 ;; kludges section, we bind `system-type' in such a way that
7063 ;; `shell-quote-arguments' behaves as if on Unix.
7065 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7066 ;; function to work with Bourne-like shells.
7068 ;; CCC: This function should be rewritten so that
7069 ;; `shell-quote-argument' is not used. This way, we are safe from
7070 ;; changes in `shell-quote-argument'.
7071 (defun tramp-shell-quote-argument (s)
7072 "Similar to `shell-quote-argument', but groks newlines.
7073 Only works for Bourne-like shells."
7074 (let ((system-type 'not-windows))
7076 (let ((result (shell-quote-argument s))
7077 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7078 (when (and (>= (length result) 2)
7079 (string= (substring result 0 2) "\\~"))
7080 (setq result (substring result 1)))
7081 (while (string-match nl result)
7082 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7086 ;; ;; EFS hooks itself into the file name handling stuff in more places
7087 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7088 ;; ;; away from tramp.el file names.
7090 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7091 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7092 ;; ;; connection to help it's dired process. Not that I have any real
7093 ;; ;; idea *why* this is helpful to dired.
7095 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7097 ;; ;; Daniel Pittman <daniel@danann.net>
7099 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7100 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7101 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7102 ;; ;; EFS might nicht be loaded yet.
7103 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7104 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7105 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7109 ;; We currently (sometimes) use "[" and "]" in the filename format.
7110 ;; This means that Emacs wants to expand wildcards if
7111 ;; `find-file-wildcards' is non-nil, and then barfs because no
7112 ;; expansion could be found. We detect this situation and do
7113 ;; something really awful: we have `file-expand-wildcards' return the
7114 ;; original filename if it can't expand anything. Let's just hope
7115 ;; that this doesn't break anything else.
7116 ;; CCC: This check is now also really awful; we should search all
7117 ;; of the filename format, not just the prefix.
7118 (when (string-match "\\[" tramp-prefix-format)
7119 (defadvice file-expand-wildcards (around tramp-fix activate)
7120 (let ((name (ad-get-arg 0)))
7121 (if (tramp-tramp-file-p name)
7122 ;; If it's a Tramp file, dissect it and look if wildcards
7123 ;; need to be expanded at all.
7124 (let ((v (tramp-dissect-file-name name)))
7125 (if (string-match "[[*?]" (tramp-file-name-localname v))
7126 (let ((res ad-do-it))
7127 (setq ad-return-value (or res (list name))))
7128 (setq ad-return-value (list name))))
7129 ;; If it is not a Tramp file, just run the original function.
7130 (let ((res ad-do-it))
7131 (setq ad-return-value (or res (list name)))))))
7134 ;; Tramp version is useful in a number of situations.
7136 (defun tramp-version (arg)
7137 "Print version number of tramp.el in minibuffer or current buffer."
7139 (if arg (insert tramp-version) (message tramp-version)))
7141 ;; Make the `reporter` functionality available for making bug reports about
7142 ;; the package. A most useful piece of code.
7144 (unless (fboundp 'reporter-submit-bug-report)
7145 (autoload 'reporter-submit-bug-report "reporter"))
7148 "Submit a bug report to the TRAMP developers."
7151 (let ((reporter-prompt-for-summary-p t))
7152 (reporter-submit-bug-report
7153 tramp-bug-report-address ; to-address
7154 (format "tramp (%s)" tramp-version) ; package name and version
7158 tramp-file-exists-command
7159 tramp-current-multi-method
7160 tramp-current-method
7165 tramp-auto-save-directory ; vars to dump
7166 tramp-default-method
7167 tramp-rsh-end-of-line
7168 tramp-default-password-end-of-line
7170 tramp-login-prompt-regexp
7171 tramp-password-prompt-regexp
7172 tramp-wrong-passwd-regexp
7173 tramp-yesno-prompt-regexp
7174 tramp-yn-prompt-regexp
7175 tramp-terminal-prompt-regexp
7176 tramp-out-of-band-prompt-regexp
7177 tramp-temp-name-prefix
7178 tramp-file-name-structure
7179 tramp-file-name-regexp
7180 tramp-multi-file-name-structure
7181 tramp-multi-file-name-hop-structure
7183 tramp-multi-connection-function-alist
7186 tramp-coding-commands
7187 tramp-actions-before-shell
7188 tramp-actions-copy-out-of-band
7191 tramp-shell-prompt-pattern
7193 ,(when (boundp 'tramp-backup-directory-alist)
7194 'tramp-backup-directory-alist)
7195 ,(when (boundp 'tramp-bkup-backup-directory-info)
7196 'tramp-bkup-backup-directory-info)
7198 ;; Non-tramp variables of interest
7199 shell-prompt-pattern
7201 backup-by-copying-when-linked
7202 backup-by-copying-when-mismatch
7203 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7204 'backup-by-copying-when-privileged-mismatch)
7205 ,(when (boundp 'password-cache)
7207 ,(when (boundp 'password-cache-expiry)
7208 'password-cache-expiry)
7209 ,(when (boundp 'backup-directory-alist)
7210 'backup-directory-alist)
7211 ,(when (boundp 'bkup-backup-directory-info)
7212 'bkup-backup-directory-info)
7213 file-name-handler-alist)
7217 Enter your bug report in this message, including as much detail as you
7218 possibly can about the problem, what you did to cause it and what the
7219 local and remote machines are.
7221 If you can give a simple set of instructions to make this bug happen
7222 reliably, please include those. Thank you for helping kill bugs in
7225 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7226 the ~/.emacs file and to repeat the bug. Then, include the contents
7227 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7230 --bug report follows this line--
7233 (defalias 'tramp-submit-bug 'tramp-bug)
7237 ;; Make sure that we get integration with the VC package.
7238 ;; When it is loaded, we need to pull in the integration module.
7239 ;; This must come after (provide 'tramp) because tramp-vc.el
7241 (eval-after-load "vc"
7242 '(require 'tramp-vc))
7246 ;; * Allow putting passwords in the filename.
7247 ;; This should be implemented via a general mechanism to add
7248 ;; parameters in filenames. There is currently a kludge for
7249 ;; putting the port number into the filename for ssh and ftp
7250 ;; files. This could be subsumed by the new mechanism as well.
7251 ;; Another approach is to read a netrc file like ~/.authinfo
7253 ;; * Handle nonlocal exits such as C-g.
7254 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7255 ;; * Add fallback for inline encodings. This should be used
7256 ;; if the remote end doesn't support mimencode or a similar program.
7257 ;; For reading files from the remote host, we can just parse the output
7258 ;; of `od -b'. For writing files to the remote host, we construct
7259 ;; a shell program which contains only "safe" ascii characters
7260 ;; and which writes the right bytes to the file. We can use printf(1)
7261 ;; or "echo -e" or the printf function in awk and use octal escapes
7262 ;; for the "dangerous" characters. The null byte might be a problem.
7263 ;; On some systems, the octal escape doesn't work. So we try the following
7264 ;; two commands to write a null byte:
7265 ;; dd if=/dev/zero bs=1 count=1
7266 ;; echo | tr '\n' '\000'
7267 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7268 ;; the two via a format which can be `uu' or `b64'. Then we can search
7269 ;; for the right local commands and the right remote commands separately.
7270 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7271 ;; work for remote files.
7272 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7273 ;; `shell-quote-argument'.
7274 ;; * Completion gets confused when you leave out the method name.
7275 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7276 ;; by the files in that directory. Add this here.
7277 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7278 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7279 ;; * When logging in, keep looking for questions according to an alist
7280 ;; and then invoke the right function.
7281 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7282 ;; * Running CVS remotely doesn't appear to work right. It thinks
7283 ;; files are locked by somebody else even if I'm the locking user.
7284 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7286 ;; * Don't use globbing for directories with many files, as this is
7287 ;; likely to produce long command lines, and some shells choke on
7288 ;; long command lines.
7289 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7290 ;; do the right thing.
7291 ;; * `vc-directory' does not work. It never displays any files, even
7292 ;; if it does show files when run locally.
7293 ;; * Allow correction of passwords, if the remote end allows this.
7294 ;; (Mark Hershberger)
7295 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7296 ;; * Do asynchronous `shell-command's.
7297 ;; * Grok `append' parameter for `write-region'.
7298 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7299 ;; * abbreviate-file-name
7300 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7301 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7302 ;; what the user names should default to, though.
7303 ;; * better error checking. At least whenever we see something
7304 ;; strange when doing zerop, we should kill the process and start
7305 ;; again. (Greg Stark)
7306 ;; * Add caching for filename completion. (Greg Stark)
7307 ;; Of course, this has issues with usability (stale cache bites)
7308 ;; -- <daniel@danann.net>
7309 ;; * Provide a local cache of old versions of remote files for the rsync
7310 ;; transfer method to use. (Greg Stark)
7311 ;; * Remove unneeded parameters from methods.
7312 ;; * Invoke rsync once for copying a whole directory hierarchy.
7313 ;; (Francesco Potort
\e,Al
\e(B)
7314 ;; * Should we set PATH ourselves or should we rely on the remote end
7316 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7317 ;; * Make it work for different encodings, and for different file name
7318 ;; encodings, too. (Daniel Pittman)
7319 ;; * Change applicable functions to pass a struct tramp-file-name rather
7320 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7321 ;; * Implement asynchronous shell commands.
7322 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7323 ;; * Progress reports while copying files. (Michael Kifer)
7324 ;; * `Smart' connection method that uses inline for small and out of
7325 ;; band for large files. (Michael Kifer)
7326 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7327 ;; then look if it's the right version (with `perl -v').
7328 ;; * When editing a remote CVS controlled file as a different user, VC
7329 ;; gets confused about the file locking status. Try to find out why
7330 ;; the workaround doesn't work.
7331 ;; * Change `copy-file' to grok the case where the filename handler
7332 ;; for the source and the target file are different. Right now,
7333 ;; it looks at the source file and then calls that handler, if
7334 ;; there is one. But since ange-ftp, for instance, does not know
7335 ;; about Tramp, it does not do the right thing if the target file
7336 ;; name is a Tramp name.
7337 ;; * Username and hostname completion.
7338 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7339 ;; connect to host "blabla" already if that host is unique. No idea
7340 ;; how to suppress. Maybe not an essential problem.
7341 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7342 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7343 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7344 ;; Code is nearly identical.
7345 ;; ** Decide whiche files to take for searching user/host names depending on
7346 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7347 ;; ** Enhance variables for debug.
7348 ;; ** Implement "/multi:" completion.
7349 ;; ** Add a learning mode for completion. Make results persistent.
7350 ;; * Allow out-of-band methods as _last_ multi-hop.
7352 ;; Functions for file-name-handler-alist:
7353 ;; diff-latest-backup-file -- in diff.el
7354 ;; dired-uncache -- this will be needed when we do insert-directory caching
7355 ;; file-name-as-directory -- use primitive?
7356 ;; file-name-sans-versions -- use primitive?
7357 ;; get-file-buffer -- use primitive
7360 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7361 ;;; tramp.el ends here