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 "\
1552 if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; }
1553 elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; }
1554 else { $l = \"nil\" };
1555 $u = ($n eq \"nil\") ? $s[4] : getpwuid($s[4]);
1556 $g = ($n eq \"nil\") ? $s[5] : getgrgid($s[5]);
1557 printf(\"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1558 $l, $s[3], $u, $g, $s[8] >> 16 & 0xffff, $s[8] & 0xffff,
1559 $s[9] >> 16 & 0xffff, $s[9] & 0xffff, $s[10] >> 16 & 0xffff, $s[10] & 0xffff,
1560 $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff);"
1561 "Perl script to produce output suitable for use with `file-attributes'
1562 on the remote file system.")
1564 ;; ;; These two use uu encoding.
1565 ;; (defvar tramp-perl-encode "%s -e'\
1566 ;; print qq(begin 644 xxx\n);
1569 ;; while (read(STDIN, $s, 45)) {
1570 ;; print pack(q(u), $s);
1575 ;; "Perl program to use for encoding a file.
1576 ;; Escape sequence %s is replaced with name of Perl binary.")
1578 ;; (defvar tramp-perl-decode "%s -ne '
1579 ;; print unpack q(u), $_;
1581 ;; "Perl program to use for decoding a file.
1582 ;; Escape sequence %s is replaced with name of Perl binary.")
1584 ;; These two use base64 encoding.
1585 (defvar tramp-perl-encode-with-module
1586 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1587 "Perl program to use for encoding a file.
1588 Escape sequence %s is replaced with name of Perl binary.
1589 This string is passed to `format', so percent characters need to be doubled.
1590 This implementation requires the MIME::Base64 Perl module to be installed
1591 on the remote host.")
1593 (defvar tramp-perl-decode-with-module
1594 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1595 "Perl program to use for decoding a file.
1596 Escape sequence %s is replaced with name of Perl binary.
1597 This string is passed to `format', so percent characters need to be doubled.
1598 This implementation requires the MIME::Base64 Perl module to be installed
1599 on the remote host.")
1601 (defvar tramp-perl-encode
1603 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1604 # Copyright (C) 2002 Free Software Foundation, Inc.
1609 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1610 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1615 # We read in chunks of 54 bytes, to generate output lines
1616 # of 72 chars (plus end of line)
1619 while (my $data = <STDIN>) {
1622 # Only for the last chunk, and only if did not fill the last three-byte packet
1624 my $mod = length($data) %% 3;
1625 $pad = q(=) x (3 - $mod) if $mod;
1628 # Not the fastest method, but it is simple: unpack to binary string, split
1629 # by groups of 6 bits and convert back from binary to byte; then map into
1630 # the translation table
1634 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1639 "Perl program to use for encoding a file.
1640 Escape sequence %s is replaced with name of Perl binary.
1641 This string is passed to `format', so percent characters need to be doubled.")
1643 (defvar tramp-perl-decode
1645 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1646 # Copyright (C) 2002 Free Software Foundation, Inc.
1651 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1652 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1655 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1659 # We are going to accumulate into $pending to accept any line length
1660 # (we do not check they are <= 76 chars as the RFC says)
1663 while (my $data = <STDIN>) {
1666 # If we find one or two =, we have reached the end and
1667 # any following data is to be discarded
1668 my $finished = $data =~ s/(==?).*/$1/;
1671 my $len = length($pending);
1672 my $chunk = substr($pending, 0, $len & ~3);
1673 $pending = substr($pending, $len & ~3 + 1);
1675 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1676 # split in 8-bit chunks and convert back to char.
1679 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1684 "Perl program to use for decoding a file.
1685 Escape sequence %s is replaced with name of Perl binary.
1686 This string is passed to `format', so percent characters need to be doubled.")
1688 ; These values conform to `file-attributes' from XEmacs 21.2.
1689 ; GNU Emacs and other tools not checked.
1690 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1692 (2 . "c") ; character device
1693 (3 . "m") ; multiplexed character device (v7)
1694 (4 . "d") ; directory
1695 (5 . "?") ; Named special file (XENIX)
1696 (6 . "b") ; block device
1697 (7 . "?") ; multiplexed block device (v7)
1698 (8 . "-") ; regular file
1699 (9 . "n") ; network special file (HP-UX)
1700 (10 . "l") ; symlink
1701 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1703 (13 . "D") ; door special (Solaris)
1704 (14 . "w")) ; whiteout (BSD)
1705 "A list of file types returned from the `stat' system call.
1706 This is used to map a mode number to a permission string.")
1708 (defvar tramp-dos-coding-system
1709 (if (and (fboundp 'coding-system-p)
1710 (funcall 'coding-system-p '(dos)))
1713 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1715 (defvar tramp-last-cmd nil
1716 "Internal Tramp variable recording the last command sent.
1717 This variable is buffer-local in every buffer.")
1718 (make-variable-buffer-local 'tramp-last-cmd)
1720 (defvar tramp-process-echoes nil
1721 "Whether to process echoes from the remote shell.")
1723 (defvar tramp-last-cmd-time nil
1724 "Internal Tramp variable recording the time when the last cmd was sent.
1725 This variable is buffer-local in every buffer.")
1726 (make-variable-buffer-local 'tramp-last-cmd-time)
1728 ;; This variable does not have the right value in XEmacs. What should
1729 ;; I use instead of find-operation-coding-system in XEmacs?
1730 (defvar tramp-feature-write-region-fix
1731 (when (fboundp 'find-operation-coding-system)
1732 (let ((file-coding-system-alist '(("test" emacs-mule))))
1733 (funcall (symbol-function 'find-operation-coding-system)
1734 'write-region 0 0 "" nil "test")))
1735 "Internal variable to say if `write-region' chooses the right coding.
1736 Older versions of Emacs chose the coding system for `write-region' based
1737 on the FILENAME argument, even if VISIT was a string.")
1739 ;; New handlers should be added here. The following operations can be
1740 ;; handled using the normal primitives: file-name-as-directory,
1741 ;; file-name-directory, file-name-nondirectory,
1742 ;; file-name-sans-versions, get-file-buffer.
1743 (defconst tramp-file-name-handler-alist
1745 (load . tramp-handle-load)
1746 (make-symbolic-link . tramp-handle-make-symbolic-link)
1747 (file-name-directory . tramp-handle-file-name-directory)
1748 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1749 (file-truename . tramp-handle-file-truename)
1750 (file-exists-p . tramp-handle-file-exists-p)
1751 (file-directory-p . tramp-handle-file-directory-p)
1752 (file-executable-p . tramp-handle-file-executable-p)
1753 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1754 (file-readable-p . tramp-handle-file-readable-p)
1755 (file-regular-p . tramp-handle-file-regular-p)
1756 (file-symlink-p . tramp-handle-file-symlink-p)
1757 (file-writable-p . tramp-handle-file-writable-p)
1758 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1759 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1760 (file-attributes . tramp-handle-file-attributes)
1761 (file-modes . tramp-handle-file-modes)
1762 (file-directory-files . tramp-handle-file-directory-files)
1763 (directory-files . tramp-handle-directory-files)
1764 (file-name-all-completions . tramp-handle-file-name-all-completions)
1765 (file-name-completion . tramp-handle-file-name-completion)
1766 (add-name-to-file . tramp-handle-add-name-to-file)
1767 (copy-file . tramp-handle-copy-file)
1768 (rename-file . tramp-handle-rename-file)
1769 (set-file-modes . tramp-handle-set-file-modes)
1770 (make-directory . tramp-handle-make-directory)
1771 (delete-directory . tramp-handle-delete-directory)
1772 (delete-file . tramp-handle-delete-file)
1773 (directory-file-name . tramp-handle-directory-file-name)
1774 (shell-command . tramp-handle-shell-command)
1775 (process-file . tramp-handle-process-file)
1776 (insert-directory . tramp-handle-insert-directory)
1777 (expand-file-name . tramp-handle-expand-file-name)
1778 (file-local-copy . tramp-handle-file-local-copy)
1779 (file-remote-p . tramp-handle-file-remote-p)
1780 (insert-file-contents . tramp-handle-insert-file-contents)
1781 (write-region . tramp-handle-write-region)
1782 (find-backup-file-name . tramp-handle-find-backup-file-name)
1783 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1784 (dired-compress-file . tramp-handle-dired-compress-file)
1785 (dired-call-process . tramp-handle-dired-call-process)
1786 (dired-recursive-delete-directory
1787 . tramp-handle-dired-recursive-delete-directory)
1788 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1789 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1790 "Alist of handler functions.
1791 Operations not mentioned here will be handled by the normal Emacs functions.")
1793 ;; Handlers for partial tramp file names. For GNU Emacs just
1794 ;; `file-name-all-completions' is needed. The other ones are necessary
1796 (defconst tramp-completion-file-name-handler-alist
1798 (file-name-directory . tramp-completion-handle-file-name-directory)
1799 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1800 (file-exists-p . tramp-completion-handle-file-exists-p)
1801 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1802 (file-name-completion . tramp-completion-handle-file-name-completion)
1803 (expand-file-name . tramp-completion-handle-expand-file-name))
1804 "Alist of completion handler functions.
1805 Used for file names matching `tramp-file-name-regexp'. Operations not
1806 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1807 normal Emacs functions.")
1809 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1810 (defvar tramp-foreign-file-name-handler-alist
1811 ;; (identity . tramp-sh-file-name-handler) should always be the last
1812 ;; entry, since `identity' always matches.
1813 '((identity . tramp-sh-file-name-handler))
1814 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1815 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1818 ;;; Internal functions which must come first.
1820 (defsubst tramp-message (level fmt-string &rest args)
1821 "Emit a message depending on verbosity level.
1822 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1823 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1824 Calls function `message' with FMT-STRING as control string and the remaining
1825 ARGS to actually emit the message (if applicable).
1827 This function expects to be called from the tramp buffer only!"
1828 (when (<= level tramp-verbose)
1829 (apply #'message (concat "tramp: " fmt-string) args)
1830 (when tramp-debug-buffer
1833 (tramp-get-debug-buffer
1834 tramp-current-multi-method tramp-current-method
1835 tramp-current-user tramp-current-host))
1836 (goto-char (point-max))
1837 (tramp-insert-with-face
1839 (concat "# " (apply #'format fmt-string args) "\n"))))))
1841 (defun tramp-message-for-buffer
1842 (multi-method method user host level fmt-string &rest args)
1843 "Like `tramp-message' but temporarily switches to the tramp buffer.
1844 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1845 remaining args passed to `tramp-message'."
1847 (set-buffer (tramp-get-buffer multi-method method user host))
1848 (apply 'tramp-message level fmt-string args)))
1850 (defsubst tramp-line-end-position nil
1851 "Return point at end of line.
1852 Calls `line-end-position' or `point-at-eol' if defined, else
1853 own implementation."
1855 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1856 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1857 (t (save-excursion (end-of-line) (point)))))
1859 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1860 "Parse a Tramp filename and make components available in the body.
1862 First arg FILENAME is evaluated and dissected into its components.
1863 Second arg VAR is a symbol. It is used as a variable name to hold
1864 the filename structure. It is also used as a prefix for the variables
1865 holding the components. For example, if VAR is the symbol `foo', then
1866 `foo' will be bound to the whole structure, `foo-multi-method' will
1867 be bound to the multi-method component, and so on for `foo-method',
1868 `foo-user', `foo-host', `foo-localname'.
1870 Remaining args are Lisp expressions to be evaluated (inside an implicit
1873 If VAR is nil, then we bind `v' to the structure and `multi-method',
1874 `method', `user', `host', `localname' to the components."
1875 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1876 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1877 (tramp-file-name-multi-method ,(or var 'v)))
1878 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1879 (tramp-file-name-method ,(or var 'v)))
1880 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1881 (tramp-file-name-user ,(or var 'v)))
1882 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1883 (tramp-file-name-host ,(or var 'v)))
1884 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1885 (tramp-file-name-localname ,(or var 'v))))
1888 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1889 ;; To be activated for debugging containing this macro
1890 ;; It works only when VAR is nil. Otherwise, it can be deactivated by
1891 ;; (def-edebug-spec with-parsed-tramp-file-name 0)
1892 ;; I'm too stupid to write a precise SPEC for it.
1893 (if (functionp 'def-edebug-spec)
1894 (def-edebug-spec with-parsed-tramp-file-name t))
1896 (defmacro tramp-let-maybe (variable value &rest body)
1897 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
1898 BODY is executed whether or not the variable is obsolete.
1899 The intent is to protect against `obsolete variable' warnings."
1900 `(if (get ',variable 'byte-obsolete-variable)
1902 (let ((,variable ,value))
1904 (put 'tramp-let-maybe 'lisp-indent-function 2)
1906 ;;; Config Manipulation Functions:
1908 (defun tramp-set-completion-function (method function-list)
1909 "Sets the list of completion functions for METHOD.
1910 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1911 The FUNCTION is intended to parse FILE according its syntax.
1912 It might be a predefined FUNCTION, or a user defined FUNCTION.
1913 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
1914 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
1915 and `tramp-parse-netrc'.
1919 (tramp-set-completion-function
1921 '((tramp-parse-sconfig \"/etc/ssh_config\")
1922 (tramp-parse-sconfig \"~/.ssh/config\")))"
1924 (let ((r function-list)
1926 (setq tramp-completion-function-alist
1927 (delete (assoc method tramp-completion-function-alist)
1928 tramp-completion-function-alist))
1931 ;; Remove double entries
1932 (when (member (car v) (cdr v))
1933 (setcdr v (delete (car v) (cdr v))))
1934 ;; Check for function and file
1935 (unless (and (functionp (nth 0 (car v)))
1936 (file-exists-p (nth 1 (car v))))
1937 (setq r (delete (car v) r)))
1941 (add-to-list 'tramp-completion-function-alist
1944 (defun tramp-get-completion-function (method)
1945 "Returns list of completion functions for METHOD.
1946 For definition of that list see `tramp-set-completion-function'."
1947 (cdr (assoc method tramp-completion-function-alist)))
1949 ;;; File Name Handler Functions:
1951 (defun tramp-handle-make-symbolic-link
1952 (filename linkname &optional ok-if-already-exists)
1953 "Like `make-symbolic-link' for tramp files.
1954 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1955 the symlink. If LINKNAME is a Tramp file, only the localname component is
1956 used as the target of the symlink.
1958 If LINKNAME is a Tramp file and the localname component is relative, then
1959 it is expanded first, before the localname component is taken. Note that
1960 this can give surprising results if the user/host for the source and
1961 target of the symlink differ."
1962 (with-parsed-tramp-file-name linkname l
1963 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
1964 (cwd (file-name-directory l-localname)))
1967 (list "Making a symbolic link."
1968 "ln(1) does not exist on the remote host.")))
1970 ;; Do the 'confirm if exists' thing.
1971 (when (file-exists-p linkname)
1973 (if (or (null ok-if-already-exists) ; not allowed to exist
1974 (and (numberp ok-if-already-exists)
1977 "File %s already exists; make it a link anyway? "
1979 (signal 'file-already-exists (list "File already exists" l-localname))
1980 (delete-file linkname)))
1982 ;; If FILENAME is a Tramp name, use just the localname component.
1983 (when (tramp-tramp-file-p filename)
1984 (setq filename (tramp-file-name-localname
1985 (tramp-dissect-file-name
1986 (expand-file-name filename)))))
1988 ;; Right, they are on the same host, regardless of user, method, etc.
1989 ;; We now make the link on the remote machine. This will occur as the user
1990 ;; that FILENAME belongs to.
1992 (tramp-send-command-and-check
1993 l-multi-method l-method l-user l-host
1994 (format "cd %s && %s -sf %s %s"
2001 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2002 "Like `load' for tramp files. Not implemented!"
2003 (unless (file-name-absolute-p file)
2004 (error "Tramp cannot `load' files without absolute file name"))
2005 (with-parsed-tramp-file-name file nil
2007 (cond ((file-exists-p (concat file ".elc"))
2008 (setq file (concat file ".elc")))
2009 ((file-exists-p (concat file ".el"))
2010 (setq file (concat file ".el")))))
2012 ;; The first condition is always true for absolute file names.
2013 ;; Included for safety's sake.
2014 (unless (or (file-name-directory file)
2015 (string-match "\\.elc?\\'" file))
2016 (error "File `%s' does not include a `.el' or `.elc' suffix"
2019 (when (not (file-exists-p file))
2020 (error "Cannot load nonexistant file `%s'" file)))
2021 (if (not (file-exists-p file))
2024 (message "Loading %s..." file))
2025 (let ((local-copy (file-local-copy file)))
2026 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2027 (load local-copy noerror t t)
2028 (delete-file local-copy))
2030 (message "Loading %s...done" file))
2033 ;; Localname manipulation functions that grok TRAMP localnames...
2034 (defun tramp-handle-file-name-directory (file)
2035 "Like `file-name-directory' but aware of TRAMP files."
2036 ;; everything except the last filename thing is the directory
2037 (with-parsed-tramp-file-name file nil
2038 ;; For the following condition, two possibilities should be tried:
2039 ;; (1) (string= localname "")
2040 ;; (2) (or (string= localname "") (string= localname "/"))
2041 ;; The second variant fails when completing a "/" directory on
2042 ;; the remote host, that is a filename which looks like
2043 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2044 ;; We should do some investigation.
2045 (if (string= localname "")
2046 ;; For a filename like "/[foo]", we return "/". The `else'
2047 ;; case would return "/[foo]" unchanged. But if we do that,
2048 ;; then `file-expand-wildcards' ceases to work. It's not
2049 ;; quite clear to me what's the intuition that tells that this
2050 ;; behavior is the right behavior, but oh, well.
2052 ;; run the command on the localname portion only
2053 ;; CCC: This should take into account the remote machine type, no?
2054 ;; --daniel <daniel@danann.net>
2055 (tramp-make-tramp-file-name multi-method method user host
2056 ;; This will not recurse...
2057 (or (file-name-directory localname) "")))))
2059 (defun tramp-handle-file-name-nondirectory (file)
2060 "Like `file-name-nondirectory' but aware of TRAMP files."
2061 (with-parsed-tramp-file-name file nil
2062 (file-name-nondirectory localname)))
2064 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2065 "Like `file-truename' for tramp files."
2066 (with-parsed-tramp-file-name (expand-file-name filename) nil
2067 (let* ((steps (tramp-split-string localname "/"))
2068 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2069 (file-name-as-directory localname)))
2070 (is-dir (string= localname localnamedir))
2073 ;; Don't make the following value larger than necessary.
2074 ;; People expect an error message in a timely fashion when
2075 ;; something is wrong; otherwise they might think that Emacs
2076 ;; is hung. Of course, correctness has to come first.
2078 (result nil) ;result steps in reverse order
2080 (tramp-message-for-buffer
2081 multi-method method user host
2082 10 "Finding true name for `%s'" filename)
2083 (while (and steps (< numchase numchase-limit))
2084 (setq thisstep (pop steps))
2085 (tramp-message-for-buffer
2086 multi-method method user host
2088 (mapconcat 'identity
2089 (append '("") (reverse result) (list thisstep))
2091 (setq symlink-target
2092 (nth 0 (file-attributes
2093 (tramp-make-tramp-file-name
2094 multi-method method user host
2095 (mapconcat 'identity
2100 (cond ((string= "." thisstep)
2101 (tramp-message-for-buffer multi-method method user host
2102 10 "Ignoring step `.'"))
2103 ((string= ".." thisstep)
2104 (tramp-message-for-buffer multi-method method user host
2105 10 "Processing step `..'")
2107 ((stringp symlink-target)
2108 ;; It's a symlink, follow it.
2109 (tramp-message-for-buffer
2110 multi-method method user host
2111 10 "Follow symlink to %s" symlink-target)
2112 (setq numchase (1+ numchase))
2113 (when (file-name-absolute-p symlink-target)
2115 ;; If the symlink was absolute, we'll get a string like
2116 ;; "/user@host:/some/target"; extract the
2117 ;; "/some/target" part from it.
2118 (when (tramp-tramp-file-p symlink-target)
2119 (with-parsed-tramp-file-name symlink-target sym
2120 (unless (equal (list multi-method method user host)
2121 (list sym-multi-method sym-method
2123 (error "Symlink target `%s' on wrong host"
2125 (setq symlink-target localname)))
2127 (append (tramp-split-string symlink-target "/") steps)))
2130 (setq result (cons thisstep result)))))
2131 (when (>= numchase numchase-limit)
2132 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2133 (setq result (reverse result))
2134 ;; Combine list to form string.
2137 (mapconcat 'identity (cons "" result) "/")
2139 (when (and is-dir (or (string= "" result)
2140 (not (string= (substring result -1) "/"))))
2141 (setq result (concat result "/")))
2142 (tramp-message-for-buffer
2143 multi-method method user host
2144 10 "True name of `%s' is `%s'" filename result)
2145 (tramp-make-tramp-file-name
2146 multi-method method user host result))))
2150 (defun tramp-handle-file-exists-p (filename)
2151 "Like `file-exists-p' for tramp files."
2152 (with-parsed-tramp-file-name filename nil
2154 (zerop (tramp-send-command-and-check
2155 multi-method method user host
2157 (tramp-get-file-exists-command multi-method method user host)
2158 (tramp-shell-quote-argument localname)))))))
2160 ;; Devices must distinguish physical file systems. The device numbers
2161 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2162 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2163 ;; EFS use device number "-1". In order to be different, we use device number
2164 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2165 (defvar tramp-devices nil
2166 "Keeps virtual device numbers.")
2168 ;; CCC: This should check for an error condition and signal failure
2169 ;; when something goes wrong.
2170 ;; Daniel Pittman <daniel@danann.net>
2171 (defun tramp-handle-file-attributes (filename &optional id-format)
2172 "Like `file-attributes' for tramp files."
2173 (let ((nonnumeric (and id-format (equal id-format 'string)))
2175 (with-parsed-tramp-file-name filename nil
2176 (when (file-exists-p filename)
2177 ;; file exists, find out stuff
2179 (if (tramp-get-remote-perl multi-method method user host)
2181 (tramp-handle-file-attributes-with-perl
2182 multi-method method user host localname nonnumeric))
2184 (tramp-handle-file-attributes-with-ls
2185 multi-method method user host localname nonnumeric)))
2186 ;; set virtual device number
2187 (setcar (nthcdr 11 result)
2188 (tramp-get-device multi-method method user host)))))
2191 (defun tramp-handle-file-attributes-with-ls
2192 (multi-method method user host localname &optional nonnumeric)
2193 "Implement `file-attributes' for tramp files using the ls(1) command."
2195 res-inode res-filemodes res-numlinks
2196 res-uid res-gid res-size res-symlink-target)
2197 (tramp-message-for-buffer multi-method method user host 10
2198 "file attributes with ls: %s"
2199 (tramp-make-tramp-file-name
2200 multi-method method user host localname))
2202 multi-method method user host
2204 (tramp-get-ls-command multi-method method user host)
2205 (if nonnumeric "-ild" "-ildn")
2206 (tramp-shell-quote-argument localname)))
2207 (tramp-wait-for-output)
2208 ;; parse `ls -l' output ...
2212 (read (current-buffer))
2213 (invalid-read-syntax
2214 (when (and (equal (cadr err)
2215 "Integer constant overflow in reader")
2217 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2219 (let* ((big (read (substring (caddr err) 0
2220 (match-beginning 1))))
2221 (small (read (match-string 1 (caddr err))))
2222 (twiddle (/ small 65536)))
2223 (cons (+ big twiddle)
2224 (- small (* twiddle 65536))))))))
2225 ;; ... file mode flags
2226 (setq res-filemodes (symbol-name (read (current-buffer))))
2228 (setq res-numlinks (read (current-buffer)))
2230 (setq res-uid (read (current-buffer)))
2231 (setq res-gid (read (current-buffer)))
2233 (unless (numberp res-uid) (setq res-uid -1))
2234 (unless (numberp res-gid) (setq res-gid -1)))
2236 (setq res-size (read (current-buffer)))
2237 ;; From the file modes, figure out other stuff.
2238 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2239 (setq dirp (eq ?d (aref res-filemodes 0)))
2240 ;; if symlink, find out file name pointed to
2242 (search-forward "-> ")
2243 (setq res-symlink-target
2244 (buffer-substring (point)
2245 (tramp-line-end-position))))
2246 ;; return data gathered
2248 ;; 0. t for directory, string (name linked to) for symbolic
2250 (or dirp res-symlink-target nil)
2251 ;; 1. Number of links to file.
2257 ;; 4. Last access time, as a list of two integers. First
2258 ;; integer has high-order 16 bits of time, second has low 16
2260 ;; 5. Last modification time, likewise.
2261 ;; 6. Last status change time, likewise.
2262 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2263 ;; 7. Size in bytes (-1, if number is out of range).
2265 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2267 ;; 9. t iff file's gid would change if file were deleted and
2270 ;; 10. inode number.
2272 ;; 11. Device number. Will be replaced by a virtual device number.
2276 (defun tramp-handle-file-attributes-with-perl
2277 (multi-method method user host localname &optional nonnumeric)
2278 "Implement `file-attributes' for tramp files using a Perl script.
2280 The Perl command is sent to the remote machine when the connection
2281 is initially created and is kept cached by the remote shell."
2282 (tramp-message-for-buffer multi-method method user host 10
2283 "file attributes with perl: %s"
2284 (tramp-make-tramp-file-name
2285 multi-method method user host localname))
2287 multi-method method user host
2288 (format "tramp_file_attributes %s %s"
2289 (tramp-shell-quote-argument localname) nonnumeric))
2290 (tramp-wait-for-output)
2291 (let ((result (read (current-buffer))))
2292 (setcar (nthcdr 8 result)
2293 (tramp-file-mode-from-int (nth 8 result)))
2296 (defun tramp-get-device (multi-method method user host)
2297 "Returns the virtual device number.
2298 If it doesn't exist, generate a new one."
2299 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
2300 (unless (assoc string tramp-devices)
2301 (add-to-list 'tramp-devices
2302 (list string (length tramp-devices))))
2303 (list -1 (nth 1 (assoc string tramp-devices)))))
2305 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2306 "Like `set-visited-file-modtime' for tramp files."
2307 (unless (buffer-file-name)
2308 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2311 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2312 (let ((f (buffer-file-name))
2313 (coding-system-used nil))
2314 (with-parsed-tramp-file-name f nil
2315 (let* ((attr (file-attributes f))
2316 ;; '(-1 65535) means file doesn't exists yet.
2317 (modtime (or (nth 5 attr) '(-1 65535))))
2318 ;; We use '(0 0) as a don't-know value. See also
2319 ;; `tramp-handle-file-attributes-with-ls'.
2320 (when (boundp 'last-coding-system-used)
2321 (setq coding-system-used last-coding-system-used))
2322 (if (not (equal modtime '(0 0)))
2323 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2326 multi-method method user host
2327 (format "%s -ild %s"
2328 (tramp-get-ls-command multi-method method user host)
2329 (tramp-shell-quote-argument localname)))
2330 (tramp-wait-for-output)
2331 (setq attr (buffer-substring (point)
2332 (progn (end-of-line) (point)))))
2333 (setq tramp-buffer-file-attributes attr))
2334 (when (boundp 'last-coding-system-used)
2335 (setq last-coding-system-used coding-system-used))
2338 ;; CCC continue here
2340 ;; This function makes the same assumption as
2341 ;; `tramp-handle-set-visited-file-modtime'.
2342 (defun tramp-handle-verify-visited-file-modtime (buf)
2343 "Like `verify-visited-file-modtime' for tramp files."
2344 (with-current-buffer buf
2345 ;; There is no file visiting the buffer, or the buffer has no
2346 ;; recorded last modification time.
2347 (if (or (not (buffer-file-name))
2348 (eq (visited-file-modtime) 0))
2350 (let ((f (buffer-file-name)))
2351 (with-parsed-tramp-file-name f nil
2352 (let* ((attr (file-attributes f))
2353 (modtime (nth 5 attr))
2354 (mt (visited-file-modtime)))
2357 ;; file exists, and has a known modtime.
2358 ((and attr (not (equal modtime '(0 0))))
2359 (< (abs (tramp-time-diff
2361 ;; For compatibility, deal with both the old
2362 ;; (HIGH . LOW) and the new (HIGH LOW)
2363 ;; return values of `visited-file-modtime'.
2365 (list (car mt) (cdr mt))
2368 ;; modtime has the don't know value.
2372 multi-method method user host
2373 (format "%s -ild %s"
2374 (tramp-get-ls-command multi-method method user host)
2375 (tramp-shell-quote-argument localname)))
2376 (tramp-wait-for-output)
2377 (setq attr (buffer-substring
2378 (point) (progn (end-of-line) (point)))))
2379 (equal tramp-buffer-file-attributes attr))
2380 ;; If file does not exist, say it is not modified
2381 ;; if and only if that agrees with the buffer's record.
2382 (t (equal mt '(-1 65535))))))))))
2384 (defadvice clear-visited-file-modtime (after tramp activate)
2385 "Set `tramp-buffer-file-attributes' back to nil.
2386 Tramp uses this variable as an emulation for the actual modtime of the file,
2387 if the remote host can't provide the modtime."
2388 (setq tramp-buffer-file-attributes nil))
2390 (defun tramp-handle-set-file-modes (filename mode)
2391 "Like `set-file-modes' for tramp files."
2392 (with-parsed-tramp-file-name filename nil
2394 (unless (zerop (tramp-send-command-and-check
2395 multi-method method user host
2396 (format "chmod %s %s"
2397 (tramp-decimal-to-octal mode)
2398 (tramp-shell-quote-argument localname))))
2401 ;; FIXME: extract the proper text from chmod's stderr.
2402 "error while changing file's mode"
2405 ;; Simple functions using the `test' command.
2407 (defun tramp-handle-file-executable-p (filename)
2408 "Like `file-executable-p' for tramp files."
2409 (with-parsed-tramp-file-name filename nil
2410 (zerop (tramp-run-test "-x" filename))))
2412 (defun tramp-handle-file-readable-p (filename)
2413 "Like `file-readable-p' for tramp files."
2414 (with-parsed-tramp-file-name filename nil
2415 (zerop (tramp-run-test "-r" filename))))
2417 (defun tramp-handle-file-accessible-directory-p (filename)
2418 "Like `file-accessible-directory-p' for tramp files."
2419 (with-parsed-tramp-file-name filename nil
2420 (and (zerop (tramp-run-test "-d" filename))
2421 (zerop (tramp-run-test "-r" filename))
2422 (zerop (tramp-run-test "-x" filename)))))
2424 ;; When the remote shell is started, it looks for a shell which groks
2425 ;; tilde expansion. Here, we assume that all shells which grok tilde
2426 ;; expansion will also provide a `test' command which groks `-nt' (for
2427 ;; newer than). If this breaks, tell me about it and I'll try to do
2428 ;; something smarter about it.
2429 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2430 "Like `file-newer-than-file-p' for tramp files."
2431 (cond ((not (file-exists-p file1))
2433 ((not (file-exists-p file2))
2435 ;; We are sure both files exist at this point.
2438 ;; We try to get the mtime of both files. If they are not
2439 ;; equal to the "dont-know" value, then we subtract the times
2440 ;; and obtain the result.
2441 (let ((fa1 (file-attributes file1))
2442 (fa2 (file-attributes file2)))
2443 (if (and (not (equal (nth 5 fa1) '(0 0)))
2444 (not (equal (nth 5 fa2) '(0 0))))
2445 (> 0 (tramp-time-diff (nth 5 fa1) (nth 5 fa2)))
2446 ;; If one of them is the dont-know value, then we can
2447 ;; still try to run a shell command on the remote host.
2448 ;; However, this only works if both files are Tramp
2449 ;; files and both have the same method, same user, same
2451 (unless (and (tramp-tramp-file-p file1)
2452 (tramp-tramp-file-p file2))
2456 "Cannot check if Tramp file is newer than non-Tramp file"
2458 (with-parsed-tramp-file-name file1 v1
2459 (with-parsed-tramp-file-name file2 v2
2460 (unless (and (equal v1-multi-method v2-multi-method)
2461 (equal v1-method v2-method)
2462 (equal v1-user v2-user)
2463 (equal v1-host v2-host))
2465 (list "Files must have same method, user, host"
2467 (unless (and (tramp-tramp-file-p file1)
2468 (tramp-tramp-file-p file2))
2470 (list "Files must be tramp files on same host"
2472 (if (tramp-get-test-groks-nt
2473 v1-multi-method v1-method v1-user v1-host)
2474 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2475 (zerop (tramp-run-test2
2476 "tramp_test_nt" file1 file2)))))))))))
2478 ;; Functions implemented using the basic functions above.
2480 (defun tramp-handle-file-modes (filename)
2481 "Like `file-modes' for tramp files."
2482 (with-parsed-tramp-file-name filename nil
2483 (when (file-exists-p filename)
2484 (tramp-mode-string-to-int
2485 (nth 8 (file-attributes filename))))))
2487 (defun tramp-handle-file-directory-p (filename)
2488 "Like `file-directory-p' for tramp files."
2489 ;; Care must be taken that this function returns `t' for symlinks
2490 ;; pointing to directories. Surely the most obvious implementation
2491 ;; would be `test -d', but that returns false for such symlinks.
2492 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2493 ;; I now think he's right. So we could be using `test -d', couldn't
2496 ;; Alternatives: `cd %s', `test -d %s'
2497 (with-parsed-tramp-file-name filename nil
2500 (tramp-send-command-and-check
2501 multi-method method user host
2502 (format "test -d %s"
2503 (tramp-shell-quote-argument localname))
2504 t))))) ;run command in subshell
2506 (defun tramp-handle-file-regular-p (filename)
2507 "Like `file-regular-p' for tramp files."
2508 (with-parsed-tramp-file-name filename nil
2509 (and (file-exists-p filename)
2510 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2512 (defun tramp-handle-file-symlink-p (filename)
2513 "Like `file-symlink-p' for tramp files."
2514 (with-parsed-tramp-file-name filename nil
2515 (let ((x (car (file-attributes filename))))
2517 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2518 ;; might do weird things.
2519 (if (file-name-absolute-p x)
2520 (tramp-make-tramp-file-name
2521 multi-method method user host x)
2524 (defun tramp-handle-file-writable-p (filename)
2525 "Like `file-writable-p' for tramp files."
2526 (with-parsed-tramp-file-name filename nil
2527 (if (file-exists-p filename)
2528 ;; Existing files must be writable.
2529 (zerop (tramp-run-test "-w" filename))
2530 ;; If file doesn't exist, check if directory is writable.
2531 (and (zerop (tramp-run-test
2532 "-d" (file-name-directory filename)))
2533 (zerop (tramp-run-test
2534 "-w" (file-name-directory filename)))))))
2536 (defun tramp-handle-file-ownership-preserved-p (filename)
2537 "Like `file-ownership-preserved-p' for tramp files."
2538 (with-parsed-tramp-file-name filename nil
2539 (or (not (file-exists-p filename))
2540 ;; Existing files must be writable.
2541 (zerop (tramp-run-test "-O" filename)))))
2543 ;; Other file name ops.
2545 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2546 ;; (defun tramp-handle-directory-file-name (directory)
2547 ;; "Like `directory-file-name' for tramp files."
2548 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2549 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2550 ;; (substring directory 0 (- (length directory) 1))
2553 ;; ;; Philippe Troin <phil@fifi.org>
2554 ;; (defun tramp-handle-directory-file-name (directory)
2555 ;; "Like `directory-file-name' for tramp files."
2556 ;; (with-parsed-tramp-file-name directory nil
2557 ;; (let ((directory-length-1 (1- (length directory))))
2559 ;; (if (and (eq (aref directory directory-length-1) ?/)
2560 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2561 ;; (/= (match-end 0) directory-length-1))
2562 ;; (substring directory 0 directory-length-1)
2565 (defun tramp-handle-directory-file-name (directory)
2566 "Like `directory-file-name' for tramp files."
2567 ;; If localname component of filename is "/", leave it unchanged.
2568 ;; Otherwise, remove any trailing slash from localname component.
2569 ;; Method, host, etc, are unchanged. Does it make sense to try
2570 ;; to avoid parsing the filename?
2571 (with-parsed-tramp-file-name directory nil
2572 (if (and (not (zerop (length localname)))
2573 (eq (aref localname (1- (length localname))) ?/)
2574 (not (string= localname "/")))
2575 (substring directory 0 -1)
2578 ;; Directory listings.
2580 (defun tramp-handle-directory-files (directory
2581 &optional full match nosort files-only)
2582 "Like `directory-files' for tramp files."
2583 (with-parsed-tramp-file-name directory nil
2586 (tramp-barf-unless-okay
2587 multi-method method user host
2588 (concat "cd " (tramp-shell-quote-argument localname))
2591 "tramp-handle-directory-files: couldn't `cd %s'"
2592 (tramp-shell-quote-argument localname))
2594 multi-method method user host
2595 (concat (tramp-get-ls-command multi-method method user host)
2597 (tramp-wait-for-output)
2598 (goto-char (point-max))
2599 (while (zerop (forward-line -1))
2600 (setq x (buffer-substring (point)
2601 (tramp-line-end-position)))
2602 (when (or (not match) (string-match match x))
2604 (push (concat (file-name-as-directory directory)
2608 (tramp-send-command multi-method method user host "cd")
2609 (tramp-wait-for-output)
2610 ;; Remove non-files or non-directories if necessary. Using
2611 ;; the remote shell for this would probably be way faster.
2612 ;; Maybe something could be adapted from
2613 ;; tramp-handle-file-name-all-completions.
2615 (let ((temp (nreverse result))
2618 (if (equal files-only t)
2621 (setq item (pop temp))
2622 (when (file-regular-p item)
2623 (push item result)))
2626 (setq item (pop temp))
2627 (when (file-directory-p item)
2628 (push item result)))))))
2631 ;; This function should return "foo/" for directories and "bar" for
2632 ;; files. We use `ls -ad' to get a list of files (including
2633 ;; directories), and `find . -type d \! -name . -prune' to get a list
2635 (defun tramp-handle-file-name-all-completions (filename directory)
2636 "Like `file-name-all-completions' for tramp files."
2637 (with-parsed-tramp-file-name directory nil
2638 (unless (save-match-data (string-match "/" filename))
2639 (let* ((nowild tramp-completion-without-shell-p)
2642 (tramp-barf-unless-okay
2643 multi-method method user host
2644 (format "cd %s" (tramp-shell-quote-argument localname))
2646 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2647 (tramp-shell-quote-argument localname))
2649 ;; Get a list of directories and files, including reliably
2650 ;; tagging the directories with a trailing '/'. Because I
2651 ;; rock. --daniel@danann.net
2653 multi-method method user host
2654 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2655 "if test -d \"$f\" 2>/dev/null; "
2656 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2657 (tramp-get-ls-command multi-method method user host)
2658 (if (or nowild (zerop (length filename)))
2661 (tramp-shell-quote-argument filename)))))
2663 ;; Now grab the output.
2664 (tramp-wait-for-output)
2665 (goto-char (point-max))
2666 (while (zerop (forward-line -1))
2667 (push (buffer-substring (point)
2668 (tramp-line-end-position))
2671 (tramp-send-command multi-method method user host "cd")
2672 (tramp-wait-for-output)
2676 (all-completions filename (mapcar 'list result))
2680 ;; The following isn't needed for Emacs 20 but for 19.34?
2681 (defun tramp-handle-file-name-completion (filename directory)
2682 "Like `file-name-completion' for tramp files."
2683 (unless (tramp-tramp-file-p directory)
2685 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2687 (with-parsed-tramp-file-name directory nil
2690 (mapcar (lambda (x) (cons x nil))
2691 (file-name-all-completions filename directory)))))
2695 (defun tramp-handle-add-name-to-file
2696 (filename newname &optional ok-if-already-exists)
2697 "Like `add-name-to-file' for tramp files."
2698 (with-parsed-tramp-file-name filename v1
2699 (with-parsed-tramp-file-name newname v2
2700 (let ((ln (when v1 (tramp-get-remote-ln
2701 v1-multi-method v1-method v1-user v1-host))))
2702 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2703 (equal v1-multi-method v2-multi-method)
2704 (equal v1-method v2-method)
2705 (equal v1-user v2-user)
2706 (equal v1-host v2-host))
2707 (error "add-name-to-file: %s"
2708 "only implemented for same method, same user, same host"))
2709 (when (and (not ok-if-already-exists)
2710 (file-exists-p newname)
2711 (not (numberp ok-if-already-exists))
2714 "File %s already exists; make it a new name anyway? "
2716 (error "add-name-to-file: file %s already exists" newname))
2717 (tramp-barf-unless-okay
2718 v1-multi-method v1-method v1-user v1-host
2719 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2720 (tramp-shell-quote-argument v2-localname))
2722 "error with add-name-to-file, see buffer `%s' for details"
2725 (defun tramp-handle-copy-file
2726 (filename newname &optional ok-if-already-exists keep-date)
2727 "Like `copy-file' for tramp files."
2728 ;; Check if both files are local -- invoke normal copy-file.
2729 ;; Otherwise, use tramp from local system.
2730 (setq filename (expand-file-name filename))
2731 (setq newname (expand-file-name newname))
2732 ;; At least one file a tramp file?
2733 (if (or (tramp-tramp-file-p filename)
2734 (tramp-tramp-file-p newname))
2735 (let ((modes (file-modes filename)))
2736 (tramp-do-copy-or-rename-file
2737 'copy filename newname ok-if-already-exists keep-date)
2738 (set-file-modes newname modes))
2739 (tramp-run-real-handler
2741 (list filename newname ok-if-already-exists keep-date))))
2743 (defun tramp-handle-rename-file
2744 (filename newname &optional ok-if-already-exists)
2745 "Like `rename-file' for tramp files."
2746 ;; Check if both files are local -- invoke normal rename-file.
2747 ;; Otherwise, use tramp from local system.
2748 (setq filename (expand-file-name filename))
2749 (setq newname (expand-file-name newname))
2750 ;; At least one file a tramp file?
2751 (if (or (tramp-tramp-file-p filename)
2752 (tramp-tramp-file-p newname))
2753 (tramp-do-copy-or-rename-file
2754 'rename filename newname ok-if-already-exists)
2755 (tramp-run-real-handler 'rename-file
2756 (list filename newname ok-if-already-exists))))
2758 (defun tramp-do-copy-or-rename-file
2759 (op filename newname &optional ok-if-already-exists keep-date)
2760 "Copy or rename a remote file.
2761 OP must be `copy' or `rename' and indicates the operation to perform.
2762 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2763 the new file (for copy) or the new name of the file (for rename).
2764 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2765 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2768 This function is invoked by `tramp-handle-copy-file' and
2769 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2770 and `rename'. FILENAME and NEWNAME must be absolute file names."
2771 (unless (memq op '(copy rename))
2772 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2773 (unless ok-if-already-exists
2774 (when (file-exists-p newname)
2775 (signal 'file-already-exists
2777 (let ((t1 (tramp-tramp-file-p filename))
2778 (t2 (tramp-tramp-file-p newname))
2779 v1-multi-method v1-method v1-user v1-host v1-localname
2780 v2-multi-method v2-method v2-user v2-host v2-localname)
2782 ;; Check which ones of source and target are Tramp files.
2783 ;; We cannot invoke `with-parsed-tramp-file-name';
2784 ;; it fails if the file isn't a Tramp file name.
2786 (with-parsed-tramp-file-name filename l
2787 (setq v1-multi-method l-multi-method
2791 v1-localname l-localname))
2792 (setq v1-localname filename))
2794 (with-parsed-tramp-file-name newname l
2795 (setq v2-multi-method l-multi-method
2799 v2-localname l-localname))
2800 (setq v2-localname newname))
2803 ;; Both are Tramp files.
2806 ;; Shortcut: if method, host, user are the same for both
2807 ;; files, we invoke `cp' or `mv' on the remote host
2809 ((and (equal v1-multi-method v2-multi-method)
2810 (equal v1-method v2-method)
2811 (equal v1-user v2-user)
2812 (equal v1-host v2-host))
2813 (tramp-do-copy-or-rename-file-directly
2814 op v1-multi-method v1-method v1-user v1-host
2815 v1-localname v2-localname keep-date))
2816 ;; If both source and target are Tramp files,
2817 ;; both are using the same copy-program, then we
2818 ;; can invoke rcp directly. Note that
2819 ;; default-directory should point to a local
2820 ;; directory if we want to invoke rcp.
2821 ((and (not v1-multi-method)
2822 (not v2-multi-method)
2823 (equal v1-method v2-method)
2824 (tramp-method-out-of-band-p
2825 v1-multi-method v1-method v1-user v1-host)
2826 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2827 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2828 (tramp-do-copy-or-rename-file-out-of-band
2829 op filename newname keep-date))
2830 ;; No shortcut was possible. So we copy the
2831 ;; file first. If the operation was `rename', we go
2832 ;; back and delete the original file (if the copy was
2833 ;; successful). The approach is simple-minded: we
2834 ;; create a new buffer, insert the contents of the
2835 ;; source file into it, then write out the buffer to
2836 ;; the target file. The advantage is that it doesn't
2837 ;; matter which filename handlers are used for the
2838 ;; source and target file.
2840 (tramp-do-copy-or-rename-file-via-buffer
2841 op filename newname keep-date))))
2843 ;; One file is a Tramp file, the other one is local.
2845 ;; If the Tramp file has an out-of-band method, the corresponding
2846 ;; copy-program can be invoked.
2847 (if (and (not v1-multi-method)
2848 (not v2-multi-method)
2849 (or (tramp-method-out-of-band-p
2850 v1-multi-method v1-method v1-user v1-host)
2851 (tramp-method-out-of-band-p
2852 v2-multi-method v2-method v2-user v2-host)))
2853 (tramp-do-copy-or-rename-file-out-of-band
2854 op filename newname keep-date)
2855 ;; Use the generic method via a Tramp buffer.
2856 (tramp-do-copy-or-rename-file-via-buffer
2857 op filename newname keep-date)))
2860 ;; One of them must be a Tramp file.
2861 (error "Tramp implementation says this cannot happen")))))
2863 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2864 "Use an Emacs buffer to copy or rename a file.
2865 First arg OP is either `copy' or `rename' and indicates the operation.
2866 FILENAME is the source file, NEWNAME the target file.
2867 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2868 (let ((trampbuf (get-buffer-create "*tramp output*"))
2869 (modtime (nth 5 (file-attributes filename))))
2870 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
2872 1 (concat "Warning: cannot preserve file time stamp"
2873 " with inline copying across machines")))
2875 (set-buffer trampbuf) (erase-buffer)
2876 (insert-file-contents-literally filename)
2877 ;; We don't want the target file to be compressed, so we let-bind
2878 ;; `jka-compr-inhibit' to t.
2879 (let ((coding-system-for-write 'binary)
2880 (jka-compr-inhibit t))
2881 (write-region (point-min) (point-max) newname))
2882 ;; KEEP-DATE handling.
2884 (when (and (not (null modtime))
2885 (not (equal modtime '(0 0))))
2886 (tramp-touch newname modtime))
2887 (set-file-modes newname (file-modes filename))))
2888 ;; If the operation was `rename', delete the original file.
2889 (unless (eq op 'copy)
2890 (delete-file filename))))
2892 (defun tramp-do-copy-or-rename-file-directly
2893 (op multi-method method user host localname1 localname2 keep-date)
2894 "Invokes `cp' or `mv' on the remote system.
2895 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2896 respectively. METHOD, USER, and HOST specify the connection.
2897 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
2898 If KEEP-DATE is non-nil, preserve the time stamp when copying."
2899 ;; CCC: What happens to the timestamp when renaming?
2900 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
2901 ((eq op 'copy) "cp -f")
2902 ((eq op 'rename) "mv -f")
2904 "Unknown operation `%s', must be `copy' or `rename'"
2907 (tramp-barf-unless-okay
2908 multi-method method user host
2911 (tramp-shell-quote-argument localname1)
2912 (tramp-shell-quote-argument localname2))
2914 "Copying directly failed, see buffer `%s' for details."
2917 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2918 "Invoke rcp program to copy.
2919 One of FILENAME and NEWNAME must be a Tramp name, the other must
2920 be a local filename. The method used must be an out-of-band method."
2921 (let ((t1 (tramp-tramp-file-p filename))
2922 (t2 (tramp-tramp-file-p newname))
2923 v1-multi-method v1-method v1-user v1-host v1-localname
2924 v2-multi-method v2-method v2-user v2-host v2-localname
2925 multi-method method user host copy-program copy-args
2926 source target trampbuf)
2928 ;; Check which ones of source and target are Tramp files.
2929 ;; We cannot invoke `with-parsed-tramp-file-name';
2930 ;; it fails if the file isn't a Tramp file name.
2932 (with-parsed-tramp-file-name filename l
2933 (setq v1-multi-method l-multi-method
2937 v1-localname l-localname
2938 multi-method l-multi-method
2939 method (tramp-find-method
2940 v1-multi-method v1-method v1-user v1-host)
2943 copy-program (tramp-get-method-parameter
2944 v1-multi-method method
2945 v1-user v1-host 'tramp-copy-program)
2946 copy-args (tramp-get-method-parameter
2947 v1-multi-method method
2948 v1-user v1-host 'tramp-copy-args)))
2949 (setq v1-localname filename))
2952 (with-parsed-tramp-file-name newname l
2953 (setq v2-multi-method l-multi-method
2957 v2-localname l-localname
2958 multi-method l-multi-method
2959 method (tramp-find-method
2960 v2-multi-method v2-method v2-user v2-host)
2963 copy-program (tramp-get-method-parameter
2964 v2-multi-method method
2965 v2-user v2-host 'tramp-copy-program)
2966 copy-args (tramp-get-method-parameter
2967 v2-multi-method method
2968 v2-user v2-host 'tramp-copy-args)))
2969 (setq v2-localname newname))
2971 ;; The following should be changed. We need a more general
2972 ;; mechanism to parse extra host args.
2974 (setq source v1-localname)
2975 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
2976 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
2977 (setq v1-host (match-string 1 v1-host)))
2979 (tramp-make-copy-program-file-name
2981 (tramp-shell-quote-argument v1-localname))))
2984 (setq target v2-localname)
2985 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
2986 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
2987 (setq v2-host (match-string 1 v2-host)))
2989 (tramp-make-copy-program-file-name
2991 (tramp-shell-quote-argument v2-localname))))
2993 ;; Handle keep-date argument
2997 (cons (tramp-get-method-parameter
2998 v1-multi-method method
2999 v1-user v1-host 'tramp-copy-keep-date-arg)
3002 (cons (tramp-get-method-parameter
3003 v2-multi-method method
3004 v2-user v2-host 'tramp-copy-keep-date-arg)
3007 (setq copy-args (append copy-args (list source target))
3008 trampbuf (generate-new-buffer
3009 (tramp-buffer-name multi-method method user host)))
3011 ;; Use an asynchronous process. By this, password can be handled.
3013 (set-buffer trampbuf)
3014 (setq tramp-current-multi-method multi-method
3015 tramp-current-method method
3016 tramp-current-user user
3017 tramp-current-host host)
3019 5 "Transferring %s to file %s..." filename newname)
3021 ;; Use rcp-like program for file transfer.
3022 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3023 copy-program copy-args)))
3024 (tramp-set-process-query-on-exit-flag p nil)
3025 (tramp-process-actions p multi-method method user host
3026 tramp-actions-copy-out-of-band))
3027 (kill-buffer trampbuf)
3029 5 "Transferring %s to file %s...done" filename newname))
3031 ;; If the operation was `rename', delete the original file.
3032 (unless (eq op 'copy)
3033 (delete-file filename))))
3036 (defun tramp-handle-make-directory (dir &optional parents)
3037 "Like `make-directory' for tramp files."
3038 (setq dir (expand-file-name dir))
3039 (with-parsed-tramp-file-name dir nil
3041 (tramp-barf-unless-okay
3042 multi-method method user host
3044 (if parents "mkdir -p" "mkdir")
3045 (tramp-shell-quote-argument localname))
3047 "Couldn't make directory %s" dir))))
3049 ;; CCC error checking?
3050 (defun tramp-handle-delete-directory (directory)
3051 "Like `delete-directory' for tramp files."
3052 (setq directory (expand-file-name directory))
3053 (with-parsed-tramp-file-name directory nil
3056 multi-method method user host
3057 (format "rmdir %s ; echo ok"
3058 (tramp-shell-quote-argument localname)))
3059 (tramp-wait-for-output))))
3061 (defun tramp-handle-delete-file (filename)
3062 "Like `delete-file' for tramp files."
3063 (setq filename (expand-file-name filename))
3064 (with-parsed-tramp-file-name filename nil
3066 (unless (zerop (tramp-send-command-and-check
3067 multi-method method user host
3069 (tramp-shell-quote-argument localname))))
3070 (signal 'file-error "Couldn't delete Tramp file")))))
3074 ;; CCC: This does not seem to be enough. Something dies when
3075 ;; we try and delete two directories under TRAMP :/
3076 (defun tramp-handle-dired-recursive-delete-directory (filename)
3077 "Recursively delete the directory given.
3078 This is like `dired-recursive-delete-directory' for tramp files."
3079 (with-parsed-tramp-file-name filename nil
3080 ;; run a shell command 'rm -r <localname>'
3081 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3082 (or (file-exists-p filename)
3085 (list "Removing old file name" "no such directory" filename)))
3086 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3087 (tramp-send-command multi-method method user host
3088 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3089 ;; Wait for the remote system to return to us...
3090 ;; This might take a while, allow it plenty of time.
3091 (tramp-wait-for-output 120)
3092 ;; Make sure that it worked...
3093 (and (file-exists-p filename)
3094 (error "Failed to recusively delete %s" filename))))
3096 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3097 "Like `dired-call-process' for tramp files."
3098 (with-parsed-tramp-file-name default-directory nil
3100 (tramp-barf-unless-okay
3101 multi-method method user host
3102 (format "cd %s" (tramp-shell-quote-argument localname))
3104 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3105 (tramp-shell-quote-argument localname))
3107 multi-method method user host
3108 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3109 (tramp-wait-for-output))
3111 (insert-buffer (tramp-get-buffer multi-method method user host)))
3114 (tramp-send-command-and-check multi-method method user host nil)
3115 (tramp-send-command multi-method method user host "cd")
3116 (tramp-wait-for-output)))))
3118 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3119 "Like `dired-compress-file' for tramp files."
3120 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3121 ;; Code stolen mainly from dired-aux.el.
3122 (with-parsed-tramp-file-name file nil
3125 (if (not (featurep 'xemacs))
3127 (symbol-value 'dired-compress-file-suffixes)
3128 ;; XEmacs has `dired-compression-method-alist', which is
3129 ;; transformed into `dired-compress-file-suffixes' structure.
3132 (list (concat (regexp-quote (nth 1 x)) "\\'")
3134 (mapconcat 'identity (nth 3 x) " ")))
3135 (symbol-value 'dired-compression-method-alist))))
3137 ;; See if any suffix rule matches this file name.
3139 (let (case-fold-search)
3140 (if (string-match (car (car suffixes)) localname)
3141 (setq suffix (car suffixes) suffixes nil))
3142 (setq suffixes (cdr suffixes))))
3144 (cond ((file-symlink-p file)
3146 ((and suffix (nth 2 suffix))
3147 ;; We found an uncompression rule.
3148 (message "Uncompressing %s..." file)
3149 (when (zerop (tramp-send-command-and-check
3150 multi-method method user host
3151 (concat (nth 2 suffix) " " localname)))
3152 (message "Uncompressing %s...done" file)
3153 ;; `dired-remove-file' is not defined in XEmacs
3154 (funcall (symbol-function 'dired-remove-file) file)
3155 (string-match (car suffix) file)
3156 (concat (substring file 0 (match-beginning 0)))))
3158 ;; We don't recognize the file as compressed, so compress it.
3160 (message "Compressing %s..." file)
3161 (when (zerop (tramp-send-command-and-check
3162 multi-method method user host
3163 (concat "gzip -f " localname)))
3164 (message "Compressing %s...done" file)
3165 ;; `dired-remove-file' is not defined in XEmacs
3166 (funcall (symbol-function 'dired-remove-file) file)
3167 (cond ((file-exists-p (concat file ".gz"))
3168 (concat file ".gz"))
3169 ((file-exists-p (concat file ".z"))
3173 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3174 ;; not sure at all that this is the right way to do it, but let's hope
3175 ;; it works for now, and wait for a guru to point out the Right Way to
3177 ;;(eval-when-compile
3178 ;; (unless (fboundp 'dired-insert-set-properties)
3179 ;; (fset 'dired-insert-set-properties 'ignore)))
3180 ;; Gerd suggests this:
3181 (eval-when-compile (require 'dired))
3182 ;; Note that dired is required at run-time, too, when it is needed.
3183 ;; It is only needed on XEmacs for the function
3184 ;; `dired-insert-set-properties'.
3186 (defun tramp-handle-insert-directory
3187 (filename switches &optional wildcard full-directory-p)
3188 "Like `insert-directory' for tramp files."
3189 ;; For the moment, we assume that the remote "ls" program does not
3190 ;; grok "--dired". In the future, we should detect this on
3191 ;; connection setup.
3192 (when (string-match "^--dired\\s-+" switches)
3193 (setq switches (replace-match "" nil t switches)))
3194 (setq filename (expand-file-name filename))
3195 (with-parsed-tramp-file-name filename nil
3196 (tramp-message-for-buffer
3197 multi-method method user host 10
3198 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3199 switches filename (if wildcard "yes" "no")
3200 (if full-directory-p "yes" "no"))
3202 (setq wildcard (file-name-nondirectory localname))
3203 (setq localname (file-name-directory localname)))
3204 (when (listp switches)
3205 (setq switches (mapconcat 'identity switches " ")))
3206 (unless full-directory-p
3207 (setq switches (concat "-d " switches)))
3209 (setq switches (concat switches " " wildcard)))
3211 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3212 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3213 (if full-directory-p
3215 multi-method method user host
3217 (tramp-get-ls-command multi-method method user host)
3221 (tramp-shell-quote-argument (concat localname ".")))))
3222 (tramp-barf-unless-okay
3223 multi-method method user host
3224 (format "cd %s" (tramp-shell-quote-argument
3225 (file-name-directory localname)))
3228 (tramp-shell-quote-argument (file-name-directory localname)))
3230 multi-method method user host
3232 (tramp-get-ls-command multi-method method user host)
3236 (tramp-shell-quote-argument
3237 (file-name-nondirectory localname))))))
3238 (sit-for 1) ;needed for rsh but not ssh?
3239 (tramp-wait-for-output))
3240 ;; The following let-binding is used by code that's commented
3241 ;; out. Let's leave the let-binding in for a while to see
3242 ;; that the commented-out code is really not needed. Commenting-out
3243 ;; happened on 2003-03-13.
3244 (let ((old-pos (point)))
3245 (insert-buffer-substring
3246 (tramp-get-buffer multi-method method user host))
3247 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3248 ;; that doesn't exist on Emacs, so we use this workaround instead.
3249 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3250 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3251 ;; (let ((zmacs-region-stays t))
3252 ;; (exchange-point-and-mark))
3254 (tramp-send-command multi-method method user host "cd")
3255 (tramp-wait-for-output))
3256 ;; For the time being, the XEmacs kludge is commented out.
3257 ;; Please test it on various XEmacs versions to see if it works.
3258 ;; ;; Another XEmacs specialty follows. What's the right way to do
3260 ;; (when (and (featurep 'xemacs)
3261 ;; (eq major-mode 'dired-mode))
3264 ;; (dired-insert-set-properties old-pos (point))))
3267 ;; Continuation of kluge to pacify byte-compiler.
3268 ;;(eval-when-compile
3269 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3270 ;; (fmakunbound 'dired-insert-set-properties)))
3272 ;; CCC is this the right thing to do?
3273 (defun tramp-handle-unhandled-file-name-directory (filename)
3274 "Like `unhandled-file-name-directory' for tramp files."
3275 (with-parsed-tramp-file-name filename nil
3276 (expand-file-name "~/")))
3278 ;; Canonicalization of file names.
3280 (defun tramp-drop-volume-letter (name)
3281 "Cut off unnecessary drive letter from file NAME.
3282 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3283 locally on a remote file name. When the local system is a W32 system
3284 but the remote system is Unix, this introduces a superfluous drive
3285 letter into the file name. This function removes it.
3287 Doesn't do anything if the NAME does not start with a drive letter."
3288 (if (and (> (length name) 1)
3289 (char-equal (aref name 1) ?:)
3290 (let ((c1 (aref name 0)))
3291 (or (and (>= c1 ?A) (<= c1 ?Z))
3292 (and (>= c1 ?a) (<= c1 ?z)))))
3296 (defun tramp-handle-expand-file-name (name &optional dir)
3297 "Like `expand-file-name' for tramp files.
3298 If the localname part of the given filename starts with \"/../\" then
3299 the result will be a local, non-Tramp, filename."
3300 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3301 (setq dir (or dir default-directory "/"))
3302 ;; Unless NAME is absolute, concat DIR and NAME.
3303 (unless (file-name-absolute-p name)
3304 (setq name (concat (file-name-as-directory dir) name)))
3305 ;; If NAME is not a tramp file, run the real handler
3306 (if (not (tramp-tramp-file-p name))
3307 (tramp-run-real-handler 'expand-file-name
3310 (with-parsed-tramp-file-name name nil
3311 (unless (file-name-absolute-p localname)
3312 (setq localname (concat "~/" localname)))
3314 ;; Tilde expansion if necessary. This needs a shell which
3315 ;; groks tilde expansion! The function `tramp-find-shell' is
3316 ;; supposed to find such a shell on the remote host. Please
3317 ;; tell me about it when this doesn't work on your system.
3318 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3319 (let ((uname (match-string 1 localname))
3320 (fname (match-string 2 localname)))
3321 ;; CCC fanatic error checking?
3322 (set-buffer (tramp-get-buffer multi-method method user host))
3325 multi-method method user host
3326 (format "cd %s; pwd" uname)
3328 (tramp-wait-for-output)
3329 (goto-char (point-min))
3330 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3331 (setq localname (concat uname fname))
3333 ;; No tilde characters in file name, do normal
3334 ;; expand-file-name (this does "/./" and "/../"). We bind
3335 ;; directory-sep-char here for XEmacs on Windows, which
3336 ;; would otherwise use backslash.
3337 (tramp-let-maybe directory-sep-char ?/
3338 (tramp-make-tramp-file-name
3339 multi-method (or method (tramp-find-default-method user host))
3341 (tramp-drop-volume-letter
3342 (tramp-run-real-handler 'expand-file-name
3343 (list localname)))))))))
3345 ;; old version follows. it uses ".." to cross file handler
3347 ;; ;; Look if localname starts with "/../" construct. If this is
3348 ;; ;; the case, then we return a local name instead of a remote name.
3349 ;; (if (string-match "^/\\.\\./" localname)
3350 ;; (expand-file-name (substring localname 3))
3351 ;; ;; No tilde characters in file name, do normal
3352 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3353 ;; ;; directory-sep-char here for XEmacs on Windows, which
3354 ;; ;; would otherwise use backslash.
3355 ;; (let ((directory-sep-char ?/))
3356 ;; (tramp-make-tramp-file-name
3357 ;; multi-method method user host
3358 ;; (tramp-drop-volume-letter
3359 ;; (tramp-run-real-handler 'expand-file-name
3360 ;; (list localname))))))))))
3364 (defvar tramp-async-proc nil
3365 "Global variable keeping asyncronous process object.
3366 Used in `tramp-handle-shell-command'")
3368 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3369 "Like `shell-command' for tramp files.
3370 This will break if COMMAND prints a newline, followed by the value of
3371 `tramp-end-of-output', followed by another newline."
3372 ;; Asynchronous processes are far from being perfect. But it works at least
3373 ;; for `find-grep-dired' and `find-name-dired' in Emacs 21.4.
3374 (if (tramp-tramp-file-p default-directory)
3375 (with-parsed-tramp-file-name default-directory nil
3376 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3378 (unless output-buffer
3382 "*Async Shell Command*"
3383 "*Shell Command Output*")))
3384 (set-buffer output-buffer)
3386 (unless (bufferp output-buffer)
3387 (setq output-buffer (current-buffer)))
3388 (set-buffer output-buffer)
3389 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3390 ;; process. Instead of, another asynchronous process is opened
3391 ;; which gets the output of the (synchronous) Tramp process
3392 ;; via process-filter. ERROR-BUFFER is disabled.
3394 (setq command (substring command 0 (match-beginning 0))
3396 tramp-async-proc (start-process (buffer-name output-buffer)
3397 output-buffer "cat")))
3399 (tramp-barf-unless-okay
3400 multi-method method user host
3401 (format "cd %s" (tramp-shell-quote-argument localname))
3403 "tramp-handle-shell-command: Couldn't `cd %s'"
3404 (tramp-shell-quote-argument localname))
3405 ;; Define the process filter
3409 (tramp-get-buffer multi-method method user host))
3410 '(lambda (process string)
3411 ;; Write the output into the Tramp Process
3412 (save-current-buffer
3413 (set-buffer (process-buffer process))
3414 (goto-char (point-max))
3416 ;; Hand-over output to asynchronous process.
3419 (regexp-quote tramp-end-of-output) string)))
3422 (substring string 0 (1- (match-beginning 0)))))
3423 (process-send-string tramp-async-proc string)
3425 (set-process-filter process nil)
3426 (process-send-eof tramp-async-proc))))))
3429 multi-method method user host
3431 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3433 (format "%s; tramp_old_status=$?" command)))
3434 (unless asynchronous
3435 (tramp-wait-for-output)))
3436 (unless asynchronous
3437 (insert-buffer (tramp-get-buffer multi-method method user host)))
3440 (unless (bufferp error-buffer)
3441 (setq error-buffer (get-buffer-create error-buffer)))
3443 multi-method method user host
3444 "cat /tmp/tramp.$$.err")
3445 (tramp-wait-for-output)
3446 (set-buffer error-buffer)
3447 (insert-buffer (tramp-get-buffer multi-method method user host))
3448 (tramp-send-command-and-check
3449 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3451 (tramp-send-command multi-method method user host "cd")
3452 (unless asynchronous
3453 (tramp-wait-for-output))
3455 multi-method method user host
3456 (concat "tramp_set_exit_status $tramp_old_status;"
3457 " echo tramp_exit_status $?"))
3458 (unless asynchronous
3459 (tramp-wait-for-output)
3460 (goto-char (point-max))
3461 (unless (search-backward "tramp_exit_status " nil t)
3462 (error "Couldn't find exit status of `%s'" command))
3463 (skip-chars-forward "^ ")
3464 (setq status (read (current-buffer)))))
3465 (unless (zerop (buffer-size))
3466 (display-buffer output-buffer))
3468 ;; The following is only executed if something strange was
3469 ;; happening. Emit a helpful message and do it anyway.
3470 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3472 (tramp-run-real-handler 'shell-command
3473 (list command output-buffer error-buffer))))
3475 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3476 "Like `process-file' for Tramp files."
3477 (when infile (error "Implementation does not handle input from file"))
3478 (when (and (numberp buffer) (zerop buffer))
3479 (error "Implementation does not handle immediate return"))
3480 (when (consp buffer) (error "Implementation does not handle error files"))
3482 (mapconcat 'tramp-shell-quote-argument
3489 (defsubst tramp-make-temp-file ()
3490 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3491 (expand-file-name tramp-temp-name-prefix
3492 (tramp-temporary-file-directory))))
3494 (defun tramp-handle-file-local-copy (filename)
3495 "Like `file-local-copy' for tramp files."
3496 (with-parsed-tramp-file-name filename nil
3497 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3498 ;; We used to bind the following as late as possible.
3499 ;; loc-enc and loc-dec were bound directly before the if
3500 ;; statement that checks them. But the functions
3501 ;; tramp-get-* might invoke the "are you awake" check in
3502 ;; tramp-maybe-open-connection, which is an unfortunate time
3503 ;; since we rely on the buffer contents at that spot.
3504 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3505 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3506 (loc-enc (tramp-get-local-encoding multi-method method user host))
3507 (loc-dec (tramp-get-local-decoding multi-method method user host))
3509 (unless (file-exists-p filename)
3510 (error "Cannot make local copy of non-existing file `%s'"
3512 (setq tmpfil (tramp-make-temp-file))
3514 (cond ((tramp-method-out-of-band-p multi-method method user host)
3515 ;; `copy-file' handles out-of-band methods
3516 (copy-file filename tmpfil t t))
3518 ((and rem-enc rem-dec)
3519 ;; Use inline encoding for file transfer.
3521 ;; Following line for setting tramp-current-method,
3522 ;; tramp-current-user, tramp-current-host.
3523 (set-buffer tramp-buf)
3524 (tramp-message 5 "Encoding remote file %s..." filename)
3525 (tramp-barf-unless-okay
3526 multi-method method user host
3527 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3529 "Encoding remote file failed, see buffer `%s' for details"
3531 ;; Remove trailing status code
3532 (goto-char (point-max))
3533 (delete-region (point) (progn (forward-line -1) (point)))
3535 (tramp-message 5 "Decoding remote file %s..." filename)
3537 ;; Here is where loc-enc and loc-dec used to be let-bound.
3538 (if (and (symbolp loc-dec) (fboundp loc-dec))
3539 ;; If local decoding is a function, we call it.
3540 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3543 (insert-buffer tramp-buf)
3544 (tramp-message-for-buffer
3545 multi-method method user host
3546 6 "Decoding remote file %s with function %s..."
3549 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3550 ;; says that we need to strip tramp_exit_status
3551 ;; line from the output here. Go to point-max,
3552 ;; search backward for tramp_exit_status, delete
3553 ;; between point and point-max if found.
3554 (let ((coding-system-for-write 'binary))
3555 (funcall loc-dec (point-min) (point-max))
3556 (write-region (point-min) (point-max) tmpfil))
3557 (kill-buffer tmpbuf))
3558 ;; If tramp-decoding-function is not defined for this
3559 ;; method, we invoke tramp-decoding-command instead.
3560 (let ((tmpfil2 (tramp-make-temp-file)))
3561 (write-region (point-min) (point-max) tmpfil2)
3563 6 "Decoding remote file %s with command %s..."
3565 (tramp-call-local-coding-command
3566 loc-dec tmpfil2 tmpfil)
3567 (delete-file tmpfil2)))
3568 (tramp-message-for-buffer
3569 multi-method method user host
3570 5 "Decoding remote file %s...done" filename)
3571 ;; Set proper permissions.
3572 (set-file-modes tmpfil (file-modes filename))))
3574 (t (error "Wrong method specification for `%s'" method)))
3577 (defun tramp-handle-file-remote-p (filename)
3578 "Like `file-remote-p' for tramp files."
3579 (when (tramp-tramp-file-p filename)
3580 (with-parsed-tramp-file-name filename nil
3581 (make-tramp-file-name
3582 :multi-method multi-method
3588 (defun tramp-handle-insert-file-contents
3589 (filename &optional visit beg end replace)
3590 "Like `insert-file-contents' for tramp files."
3591 (barf-if-buffer-read-only)
3592 (setq filename (expand-file-name filename))
3593 (with-parsed-tramp-file-name filename nil
3594 (if (not (file-exists-p filename))
3597 (setq buffer-file-name filename)
3598 (set-visited-file-modtime)
3599 (set-buffer-modified-p nil))
3601 (format "File `%s' not found on remote host" filename))
3602 (list (expand-file-name filename) 0))
3603 ;; `insert-file-contents-literally' takes care to avoid calling
3604 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3605 ;; propagate that care to the file-local-copy operation.
3607 (let ((inhibit-file-name-operation
3608 (when (eq inhibit-file-name-operation
3609 'insert-file-contents)
3611 (file-local-copy filename)))
3612 (coding-system-used nil)
3615 (setq buffer-file-name filename)
3616 (set-visited-file-modtime)
3617 (set-buffer-modified-p nil))
3618 (tramp-message-for-buffer
3619 multi-method method user host
3620 9 "Inserting local temp file `%s'..." local-copy)
3621 (setq result (insert-file-contents local-copy nil beg end replace))
3622 ;; Now `last-coding-system-used' has right value. Remember it.
3623 (when (boundp 'last-coding-system-used)
3624 (setq coding-system-used last-coding-system-used))
3625 (tramp-message-for-buffer
3626 multi-method method user host
3627 9 "Inserting local temp file `%s'...done" local-copy)
3628 (delete-file local-copy)
3629 (when (boundp 'last-coding-system-used)
3630 (setq last-coding-system-used coding-system-used))
3631 (list (expand-file-name filename)
3632 (second result))))))
3635 (defun tramp-handle-find-backup-file-name (filename)
3636 "Like `find-backup-file-name' for tramp files."
3637 (with-parsed-tramp-file-name filename nil
3638 ;; We set both variables. It doesn't matter whether it is
3640 (let ((backup-directory-alist
3642 (when (boundp 'backup-directory-alist)
3643 (if (boundp 'tramp-backup-directory-alist)
3648 (if (and (stringp (cdr x))
3649 (file-name-absolute-p (cdr x))
3650 (not (tramp-file-name-p (cdr x))))
3651 (tramp-make-tramp-file-name
3652 multi-method method user host (cdr x))
3654 (symbol-value 'tramp-backup-directory-alist))
3655 (symbol-value 'backup-directory-alist))))
3657 (bkup-backup-directory-info
3659 (when (boundp 'bkup-backup-directory-info)
3660 (if (boundp 'tramp-bkup-backup-directory-info)
3666 (if (and (stringp (car (cdr x)))
3667 (file-name-absolute-p (car (cdr x)))
3668 (not (tramp-file-name-p (car (cdr x)))))
3669 (tramp-make-tramp-file-name
3670 multi-method method user host (car (cdr x)))
3673 (symbol-value 'tramp-bkup-backup-directory-info))
3674 (symbol-value 'bkup-backup-directory-info)))))
3676 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3679 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3680 (defun tramp-handle-write-region
3681 (start end filename &optional append visit lockname confirm)
3682 "Like `write-region' for tramp files."
3683 (unless (eq append nil)
3684 (error "Cannot append to file using tramp (`%s')" filename))
3685 (setq filename (expand-file-name filename))
3686 ;; Following part commented out because we don't know what to do about
3687 ;; file locking, and it does not appear to be a problem to ignore it.
3688 ;; Ange-ftp ignores it, too.
3689 ;; (when (and lockname (stringp lockname))
3690 ;; (setq lockname (expand-file-name lockname)))
3691 ;; (unless (or (eq lockname nil)
3692 ;; (string= lockname filename))
3694 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3695 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
3696 (when (and (not (featurep 'xemacs))
3697 confirm (file-exists-p filename))
3698 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3700 (error "File not overwritten")))
3701 (with-parsed-tramp-file-name filename nil
3702 (let ((curbuf (current-buffer))
3703 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3704 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3705 (loc-enc (tramp-get-local-encoding multi-method method user host))
3706 (loc-dec (tramp-get-local-decoding multi-method method user host))
3707 (trampbuf (get-buffer-create "*tramp output*"))
3708 (modes (file-modes filename))
3709 ;; We use this to save the value of `last-coding-system-used'
3710 ;; after writing the tmp file. At the end of the function,
3711 ;; we set `last-coding-system-used' to this saved value.
3712 ;; This way, any intermediary coding systems used while
3713 ;; talking to the remote shell or suchlike won't hose this
3714 ;; variable. This approach was snarfed from ange-ftp.el.
3717 ;; Write region into a tmp file. This isn't really needed if we
3718 ;; use an encoding function, but currently we use it always
3719 ;; because this makes the logic simpler.
3720 (setq tmpfil (tramp-make-temp-file))
3721 ;; Set current buffer. If connection wasn't open, `file-modes' has
3722 ;; changed it accidently.
3724 ;; We say `no-message' here because we don't want the visited file
3725 ;; modtime data to be clobbered from the temp file. We call
3726 ;; `set-visited-file-modtime' ourselves later on.
3727 (tramp-run-real-handler
3729 (if confirm ; don't pass this arg unless defined for backward compat.
3730 (list start end tmpfil append 'no-message lockname confirm)
3731 (list start end tmpfil append 'no-message lockname)))
3732 ;; The permissions of the temporary file should be set. If
3733 ;; filename does not exist (eq modes nil) it has been renamed to
3734 ;; the backup file. This case `save-buffer' handles
3736 (when modes (set-file-modes tmpfil modes))
3737 ;; Now, `last-coding-system-used' has the right value. Remember it.
3738 (when (boundp 'last-coding-system-used)
3739 (setq coding-system-used last-coding-system-used))
3740 ;; This is a bit lengthy due to the different methods possible for
3741 ;; file transfer. First, we check whether the method uses an rcp
3742 ;; program. If so, we call it. Otherwise, both encoding and
3743 ;; decoding command must be specified. However, if the method
3744 ;; _also_ specifies an encoding function, then that is used for
3745 ;; encoding the contents of the tmp file.
3746 (cond ((tramp-method-out-of-band-p multi-method method user host)
3747 ;; `copy-file' handles out-of-band methods
3748 (copy-file tmpfil filename t t))
3750 ((and rem-enc rem-dec)
3751 ;; Use inline file transfer
3752 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
3754 ;; Encode tmpfil into tmpbuf
3755 (tramp-message-for-buffer multi-method method user host
3756 5 "Encoding region...")
3759 ;; Use encoding function or command.
3760 (if (and (symbolp loc-enc) (fboundp loc-enc))
3762 (tramp-message-for-buffer
3763 multi-method method user host
3764 6 "Encoding region using function `%s'..."
3765 (symbol-name loc-enc))
3766 (insert-file-contents-literally tmpfil)
3767 ;; CCC. The following `let' is a workaround for
3768 ;; the base64.el that comes with pgnus-0.84. If
3769 ;; both of the following conditions are
3770 ;; satisfied, it tries to write to a local file
3771 ;; in default-directory, but at this point,
3772 ;; default-directory is remote.
3773 ;; (CALL-PROCESS-REGION can't write to remote
3774 ;; files, it seems.) The file in question is a
3776 (let ((default-directory
3777 (tramp-temporary-file-directory)))
3778 (funcall loc-enc (point-min) (point-max)))
3779 (goto-char (point-max))
3782 (tramp-message-for-buffer
3783 multi-method method user host
3784 6 "Encoding region using command `%s'..." loc-enc)
3785 (unless (equal 0 (tramp-call-local-coding-command
3787 (pop-to-buffer trampbuf)
3788 (error (concat "Cannot write to `%s', local encoding"
3789 " command `%s' failed")
3791 ;; Send tmpbuf into remote decoding command which
3792 ;; writes to remote file. Because this happens on the
3793 ;; remote host, we cannot use the function.
3794 (tramp-message-for-buffer
3795 multi-method method user host
3796 5 "Decoding region into remote file %s..." filename)
3798 multi-method method user host
3799 (format "%s >%s <<'EOF'"
3801 (tramp-shell-quote-argument localname)))
3803 (tramp-message-for-buffer
3804 multi-method method user host
3805 6 "Sending data to remote host...")
3806 (tramp-send-string multi-method method user host
3808 ;; wait for remote decoding to complete
3809 (tramp-message-for-buffer
3810 multi-method method user host
3811 6 "Sending end of data token...")
3813 multi-method method user host "EOF" nil t)
3814 (tramp-message-for-buffer
3815 multi-method method user host 6
3816 "Waiting for remote host to process data...")
3817 (set-buffer (tramp-get-buffer multi-method method user host))
3818 (tramp-wait-for-output)
3819 (tramp-barf-unless-okay
3820 multi-method method user host nil nil 'file-error
3821 (concat "Couldn't write region to `%s',"
3822 " decode using `%s' failed")
3824 (tramp-message 5 "Decoding region into remote file %s...done"
3826 (kill-buffer tmpbuf))))
3829 (concat "Method `%s' should specify both encoding and "
3830 "decoding command or an rcp program")
3832 (delete-file tmpfil)
3833 (unless (equal curbuf (current-buffer))
3834 (error "Buffer has changed from `%s' to `%s'"
3835 curbuf (current-buffer)))
3836 (when (or (eq visit t) (stringp visit))
3837 (set-visited-file-modtime
3838 ;; We must pass modtime explicitely, because filename can be different
3839 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
3840 (nth 5 (file-attributes filename))))
3841 ;; Make `last-coding-system-used' have the right value.
3842 (when (boundp 'last-coding-system-used)
3843 (setq last-coding-system-used coding-system-used))
3844 (when (or (eq visit t)
3847 (message "Wrote %s" filename)))))
3849 ;; Call down to the real handler.
3850 ;; Because EFS does not play nicely with TRAMP (both systems match a
3851 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
3854 ;; Other than that, this is the canon file-handler code that the doco
3855 ;; says should be used here. Which is nice.
3857 ;; Under XEmacs current, EFS also hooks in as
3858 ;; efs-sifn-handler-function to handle any filename with environment
3859 ;; variables. This has two implications:
3860 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
3861 ;; 2) That TRAMP might want to do the same thing.
3862 ;; Details as they come in.
3864 ;; Daniel Pittman <daniel@danann.net>
3866 ;; (defun tramp-run-real-handler (operation args)
3867 ;; "Invoke normal file name handler for OPERATION.
3868 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
3869 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
3871 ;; (let ((inhibit-file-name-handlers
3872 ;; (list 'tramp-file-name-handler
3873 ;; 'efs-file-handler-function
3874 ;; 'ange-ftp-hook-function
3875 ;; (and (eq inhibit-file-name-operation operation)
3876 ;; inhibit-file-name-handlers)))
3877 ;; (inhibit-file-name-operation operation))
3878 ;; (apply operation args)))
3880 (defun tramp-run-real-handler (operation args)
3881 "Invoke normal file name handler for OPERATION.
3882 First arg specifies the OPERATION, second arg is a list of arguments to
3883 pass to the OPERATION."
3884 (let* ((inhibit-file-name-handlers
3885 `(tramp-file-name-handler
3886 tramp-completion-file-name-handler
3887 cygwin-mount-name-hook-function
3888 cygwin-mount-map-drive-hook-function
3890 ,(and (eq inhibit-file-name-operation operation)
3891 inhibit-file-name-handlers)))
3892 (inhibit-file-name-operation operation))
3893 (apply operation args)))
3895 ;; This function is used from `tramp-completion-file-name-handler' functions
3896 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
3897 ;; because the check is based on a full filename, not available for all
3898 ;; basic I/O operations.
3899 (defun tramp-completion-run-real-handler (operation args)
3900 "Invoke `tramp-file-name-handler' for OPERATION.
3901 First arg specifies the OPERATION, second arg is a list of arguments to
3902 pass to the OPERATION."
3903 (let* ((inhibit-file-name-handlers
3904 `(tramp-completion-file-name-handler
3905 cygwin-mount-name-hook-function
3906 cygwin-mount-map-drive-hook-function
3908 ,(and (eq inhibit-file-name-operation operation)
3909 inhibit-file-name-handlers)))
3910 (inhibit-file-name-operation operation))
3911 (apply operation args)))
3913 ;; We handle here all file primitives. Most of them have the file
3914 ;; name as first parameter; nevertheless we check for them explicitly
3915 ;; in order to be signalled if a new primitive appears. This
3916 ;; scenario is needed because there isn't a way to decide by
3917 ;; syntactical means whether a foreign method must be called. It would
3918 ;; ease the life if `file-name-handler-alist' would support a decision
3919 ;; function as well but regexp only.
3920 (defun tramp-file-name-for-operation (operation &rest args)
3921 "Return file name related to OPERATION file primitive.
3922 ARGS are the arguments OPERATION has been called with."
3924 ; FILE resp DIRECTORY
3926 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
3927 'delete-file 'diff-latest-backup-file 'directory-file-name
3928 'directory-files 'directory-files-and-attributes
3929 'dired-compress-file 'dired-uncache
3930 'file-accessible-directory-p 'file-attributes
3931 'file-directory-p 'file-executable-p 'file-exists-p
3932 'file-local-copy 'file-remote-p 'file-modes
3933 'file-name-as-directory 'file-name-directory
3934 'file-name-nondirectory 'file-name-sans-versions
3935 'file-ownership-preserved-p 'file-readable-p
3936 'file-regular-p 'file-symlink-p 'file-truename
3937 'file-writable-p 'find-backup-file-name 'find-file-noselect
3938 'get-file-buffer 'insert-directory 'insert-file-contents
3939 'load 'make-directory 'make-directory-internal
3940 'set-file-modes 'substitute-in-file-name
3941 'unhandled-file-name-directory 'vc-registered
3943 'abbreviate-file-name 'create-file-buffer
3944 'dired-file-modtime 'dired-make-compressed-filename
3945 'dired-recursive-delete-directory 'dired-set-file-modtime
3946 'dired-shell-unhandle-file-name 'dired-uucode-file
3947 'insert-file-contents-literally 'recover-file
3948 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
3949 (if (file-name-absolute-p (nth 0 args))
3951 (expand-file-name (nth 0 args))))
3952 ; FILE DIRECTORY resp FILE1 FILE2
3954 (list 'add-name-to-file 'copy-file 'expand-file-name
3955 'file-name-all-completions 'file-name-completion
3956 'file-newer-than-file-p 'make-symbolic-link 'rename-file
3958 'dired-make-relative-symlink
3959 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
3962 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
3963 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
3964 (t (buffer-file-name (current-buffer))))))
3966 ((eq operation 'write-region)
3970 (list 'set-visited-file-modtime 'verify-visited-file-modtime
3974 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
3977 (list 'dired-call-process 'shell-command
3978 ; Post Emacs 21.3 only
3981 'dired-print-file 'dired-shell-call-process))
3983 ; unknown file primitive
3984 (t (error "unknown file I/O primitive: %s" operation))))
3986 (defun tramp-find-foreign-file-name-handler (filename)
3987 "Return foreign file name handler if exists."
3988 (when (tramp-tramp-file-p filename)
3991 (handler-alist tramp-foreign-file-name-handler-alist))
3992 (while handler-alist
3993 (setq elt (car handler-alist)
3994 handler-alist (cdr handler-alist))
3995 (when (funcall (car elt) filename)
3996 (setq handler-alist nil)
3997 (setq res (cdr elt))))
4002 (defun tramp-file-name-handler (operation &rest args)
4003 "Invoke Tramp file name handler.
4004 Falls back to normal file name handler if no tramp file name handler exists."
4006 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4007 (foreign (tramp-find-foreign-file-name-handler filename)))
4009 (foreign (apply foreign operation args))
4010 (t (tramp-run-real-handler operation args))))))
4013 ;; In Emacs, there is some concurrency due to timers. If a timer
4014 ;; interrupts Tramp and wishes to use the same connection buffer as
4015 ;; the "main" Emacs, then garbage might occur in the connection
4016 ;; buffer. Therefore, we need to make sure that a timer does not use
4017 ;; the same connection buffer as the "main" Emacs. We implement a
4018 ;; cheap global lock, instead of locking each connection buffer
4019 ;; separately. The global lock is based on two variables,
4020 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4021 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4022 ;; processing of a single file operation, so we need to allow
4023 ;; recursive calls. That's where the `tramp-locker' variable comes in
4024 ;; -- it is let-bound to t during the execution of the current
4025 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4026 ;; then we should just proceed because we have been called
4027 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4028 ;; interrupting the "main" Emacs, and then we signal an error.
4030 (defvar tramp-locked nil
4031 "If non-nil, then Tramp is currently busy.
4032 Together with `tramp-locker', this implements a locking mechanism
4033 preventing reentrant calls of Tramp.")
4035 (defvar tramp-locker nil
4036 "If non-nil, then a caller has locked Tramp.
4037 Together with `tramp-locked', this implements a locking mechanism
4038 preventing reentrant calls of Tramp.")
4040 (defun tramp-sh-file-name-handler (operation &rest args)
4041 "Invoke remote-shell Tramp file name handler.
4042 Fall back to normal file name handler if no Tramp handler exists."
4043 (when (and tramp-locked (not tramp-locker))
4044 (signal 'file-error "Forbidden reentrant call of Tramp"))
4045 (let ((tl tramp-locked))
4048 (setq tramp-locked t)
4049 (let ((tramp-locker t))
4051 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4053 (apply (cdr fn) args)
4054 (tramp-run-real-handler operation args))))))
4055 (setq tramp-locked tl))))
4058 (defun tramp-completion-file-name-handler (operation &rest args)
4059 "Invoke tramp file name completion handler.
4060 Falls back to normal file name handler if no tramp file name handler exists."
4061 ;; (setq tramp-debug-buffer t)
4062 ;; (tramp-message 1 "%s %s" operation args)
4063 ;; (tramp-message 1 "%s %s\n%s"
4064 ;; operation args (with-output-to-string (backtrace)))
4065 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4067 (save-match-data (apply (cdr fn) args))
4068 (tramp-completion-run-real-handler operation args))))
4071 (put 'tramp-completion-file-name-handler 'safe-magic t)
4073 ;; Register in file name handler alist
4075 (add-to-list 'file-name-handler-alist
4076 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4077 (add-to-list 'file-name-handler-alist
4078 (cons tramp-completion-file-name-regexp
4079 'tramp-completion-file-name-handler))
4081 (defun tramp-repair-jka-compr ()
4082 "If jka-compr is already loaded, move it to the front of
4083 `file-name-handler-alist'. On Emacs 21.4 or so this will not be
4085 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4087 (setq file-name-handler-alist
4088 (cons jka (delete jka file-name-handler-alist))))))
4089 (tramp-repair-jka-compr)
4092 ;;; Interactions with other packages:
4094 ;; -- complete.el --
4096 ;; This function contributed by Ed Sabol
4097 (defun tramp-handle-expand-many-files (name)
4098 "Like `PC-expand-many-files' for tramp files."
4099 (with-parsed-tramp-file-name name nil
4101 (if (or (string-match "\\*" name)
4102 (string-match "\\?" name)
4103 (string-match "\\[.*\\]" name))
4106 ;; CCC: To do it right, we should quote certain characters
4107 ;; in the file name, but since the echo command is going to
4108 ;; break anyway when there are spaces in the file names, we
4110 ;;-(let ((comint-file-name-quote-list
4111 ;;- (set-difference tramp-file-name-quote-list
4112 ;;- '(?\* ?\? ?[ ?]))))
4113 ;;- (tramp-send-command
4114 ;;- multi-method method user host
4115 ;;- (format "echo %s" (comint-quote-filename localname)))
4116 ;;- (tramp-wait-for-output))
4117 (tramp-send-command multi-method method user host
4118 (format "echo %s" localname))
4119 (tramp-wait-for-output)
4120 (setq bufstr (buffer-substring (point-min)
4121 (tramp-line-end-position)))
4122 (goto-char (point-min))
4123 (if (string-equal localname bufstr)
4126 (while (search-forward " " nil t)
4127 (delete-backward-char 1)
4129 (goto-char (point-max))
4130 (delete-backward-char 1)
4132 (goto-char (point-min))
4134 (function (lambda (x)
4135 (tramp-make-tramp-file-name multi-method method
4137 (read (current-buffer))))))
4138 (list (expand-file-name name))))))
4140 ;; Check for complete.el and override PC-expand-many-files if appropriate.
4142 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
4144 (defun tramp-setup-complete ()
4145 (fset 'tramp-save-PC-expand-many-files
4146 (symbol-function 'PC-expand-many-files))
4147 (defun PC-expand-many-files (name)
4148 (if (tramp-tramp-file-p name)
4149 (expand-many-files name)
4150 (tramp-save-PC-expand-many-files name))))
4152 ;; Why isn't eval-after-load sufficient?
4153 (if (fboundp 'PC-expand-many-files)
4154 (tramp-setup-complete)
4155 (eval-after-load "complete" '(tramp-setup-complete)))
4157 ;;; File name handler functions for completion mode
4159 ;; Necessary because `tramp-file-name-regexp-unified' and
4160 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4161 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4162 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4163 ;; Using `last-input-event' is a little bit risky, because completing a file
4164 ;; might require loading other files, like "~/.netrc", and for them it
4165 ;; shouldn't be decided based on that variable. On the other hand, those files
4166 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4167 ;; be introduced overwriting this check in such cases. Or we change tramp
4168 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4169 ;; In case of non unified file names it can be always true (and wouldn't be
4170 ;; necessary, because there are different regexp).
4171 (defun tramp-completion-mode (file)
4172 "Checks whether method / user name / host name completion is active."
4174 ((not tramp-unified-filenames) t)
4175 ((string-match "^/.*:.*:$" file) nil)
4177 (concat tramp-prefix-regexp
4178 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4180 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4181 ((or (equal last-input-event 'tab)
4183 (and (integerp last-input-event)
4184 (not (event-modifiers last-input-event))
4185 (or (char-equal last-input-event ?\?)
4186 (char-equal last-input-event ?\t) ; handled by 'tab already?
4187 (char-equal last-input-event ?\ )))
4189 (and (featurep 'xemacs)
4190 (not (event-modifiers last-input-event))
4192 (funcall (symbol-function 'event-to-character)
4193 last-input-event) ?\?)
4195 (funcall (symbol-function 'event-to-character)
4196 last-input-event) ?\t)
4198 (funcall (symbol-function 'event-to-character)
4199 last-input-event) ?\ ))))
4202 (defun tramp-completion-handle-file-exists-p (filename)
4203 "Like `file-exists-p' for tramp files."
4204 (if (tramp-completion-mode filename)
4205 (tramp-run-real-handler
4206 'file-exists-p (list filename))
4207 (tramp-completion-run-real-handler
4208 'file-exists-p (list filename))))
4210 ;; Localname manipulation in case of partial TRAMP file names.
4211 (defun tramp-completion-handle-file-name-directory (file)
4212 "Like `file-name-directory' but aware of TRAMP files."
4213 (if (tramp-completion-mode file)
4215 (tramp-completion-run-real-handler
4216 'file-name-directory (list file))))
4218 ;; Localname manipulation in case of partial TRAMP file names.
4219 (defun tramp-completion-handle-file-name-nondirectory (file)
4220 "Like `file-name-nondirectory' but aware of TRAMP files."
4222 file (length (tramp-completion-handle-file-name-directory file))))
4224 ;; Method, host name and user name completion.
4225 ;; `tramp-completion-dissect-file-name' returns a list of
4226 ;; tramp-file-name structures. For all of them we return possible completions.
4227 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4228 "Like `file-name-all-completions' for partial tramp files."
4231 ((fullname (concat directory filename))
4234 (if (tramp-completion-mode fullname)
4235 (tramp-run-real-handler
4236 'file-name-all-completions (list filename directory))
4237 (tramp-completion-run-real-handler
4238 'file-name-all-completions (list filename directory))))
4239 ;; possible completion structures
4240 (v (tramp-completion-dissect-file-name fullname)))
4243 (let* ((car (car v))
4244 (multi-method (tramp-file-name-multi-method car))
4245 (method (tramp-file-name-method car))
4246 (user (tramp-file-name-user car))
4247 (host (tramp-file-name-host car))
4248 (localname (tramp-file-name-localname car))
4249 (m (tramp-find-method multi-method method user host))
4250 (tramp-current-user user) ; see `tramp-parse-passwd'
4253 (unless (or multi-method ;; Not handled (yet).
4254 localname) ;; Nothing to complete
4258 ;; Method dependent user / host combinations
4262 (setq all-user-hosts
4263 (append all-user-hosts
4264 (funcall (nth 0 x) (nth 1 x)))))
4265 (tramp-get-completion-function m))
4267 (setq result (append result
4270 (tramp-get-completion-user-host
4271 method user host (nth 0 x) (nth 1 x)))
4272 (delq nil all-user-hosts)))))
4276 (append result (tramp-get-completion-methods m)))))
4278 (setq v (delq car v))))
4280 ;;; unify list, remove nil elements
4283 (let ((car (car result)))
4284 (when car (add-to-list 'result1 car))
4285 (setq result (delq car result))))
4289 ;; Method, host name and user name completion for a file.
4290 (defun tramp-completion-handle-file-name-completion (filename directory)
4291 "Like `file-name-completion' for tramp files."
4292 (try-completion filename
4293 (mapcar 'list (file-name-all-completions filename directory))))
4295 ;; I misuse a little bit the tramp-file-name structure in order to handle
4296 ;; completion possibilities for partial methods / user names / host names.
4297 ;; Return value is a list of tramp-file-name structures according to possible
4298 ;; completions. If "multi-method" or "localname" is non-nil it means there
4299 ;; shouldn't be a completion anymore.
4301 ;; Expected results:
4303 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4304 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4305 ;; [nil nil "x" nil nil]
4306 ;; [nil "x" nil nil nil]
4308 ;; "/x:" "/x:y" "/x:y:"
4309 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4311 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4312 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4314 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4315 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4316 ;; "/[x/y@" "/[x/y@z"
4317 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4318 (defun tramp-completion-dissect-file-name (name)
4319 "Returns a list of `tramp-file-name' structures.
4320 They are collected by `tramp-completion-dissect-file-name1'."
4324 ;; "/method" "/[method"
4325 (tramp-completion-file-name-structure1
4326 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4329 (tramp-completion-file-name-structure2
4330 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4333 (tramp-completion-file-name-structure3
4334 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4336 ;; "/user@host" "/[user@host"
4337 (tramp-completion-file-name-structure4
4338 (list (concat tramp-prefix-regexp
4339 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4340 "\\(" tramp-host-regexp x-nil "\\)$")
4342 ;; "/method:user" "/[method/user"
4343 (tramp-completion-file-name-structure5
4344 (list (concat tramp-prefix-regexp
4345 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4346 "\\(" tramp-user-regexp x-nil "\\)$")
4348 ;; "/method:host" "/[method/host"
4349 (tramp-completion-file-name-structure6
4350 (list (concat tramp-prefix-regexp
4351 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4352 "\\(" tramp-host-regexp x-nil "\\)$")
4354 ;; "/method:user@host" "/[method/user@host"
4355 (tramp-completion-file-name-structure7
4356 (list (concat tramp-prefix-regexp
4357 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4358 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4359 "\\(" tramp-host-regexp x-nil "\\)$")
4362 (mapcar (lambda (regexp)
4363 (add-to-list 'result
4364 (tramp-completion-dissect-file-name1 regexp name)))
4366 tramp-completion-file-name-structure1
4367 tramp-completion-file-name-structure2
4368 tramp-completion-file-name-structure3
4369 tramp-completion-file-name-structure4
4370 tramp-completion-file-name-structure5
4371 tramp-completion-file-name-structure6
4372 tramp-completion-file-name-structure7
4373 tramp-file-name-structure))
4377 (defun tramp-completion-dissect-file-name1 (structure name)
4378 "Returns a `tramp-file-name' structure matching STRUCTURE.
4379 The structure consists of multi-method, remote method, remote user,
4380 remote host and localname (filename on remote host)."
4384 (when (string-match (nth 0 structure) name)
4385 (setq method (and (nth 1 structure)
4386 (match-string (nth 1 structure) name)))
4387 (if (and method (member method tramp-multi-methods))
4388 ;; Not handled (yet).
4389 (make-tramp-file-name
4390 :multi-method method
4395 (let ((user (and (nth 2 structure)
4396 (match-string (nth 2 structure) name)))
4397 (host (and (nth 3 structure)
4398 (match-string (nth 3 structure) name)))
4399 (localname (and (nth 4 structure)
4400 (match-string (nth 4 structure) name))))
4401 (make-tramp-file-name
4406 :localname localname)))))))
4408 ;; This function returns all possible method completions, adding the
4409 ;; trailing method delimeter.
4410 (defun tramp-get-completion-methods (partial-method)
4411 "Returns all method completions for PARTIAL-METHOD."
4415 (string-match (concat "^" (regexp-quote partial-method)) method)
4416 ;; we must remove leading "/".
4417 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4418 (delete "multi" (mapcar 'car tramp-methods))))
4420 ;; Compares partial user and host names with possible completions.
4421 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4422 "Returns the most expanded string for user and host name completion.
4423 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4426 ((and partial-user partial-host)
4428 (string-match (concat "^" (regexp-quote partial-host)) host)
4429 (string-equal partial-user (or user partial-user)))
4430 (setq user partial-user)
4437 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4443 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4449 (unless (zerop (+ (length user) (length host)))
4450 ;; we must remove leading "/".
4451 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4453 (defun tramp-parse-rhosts (filename)
4454 "Return a list of (user host) tuples allowed to access.
4455 Either user or host may be nil."
4458 (when (file-readable-p filename)
4460 (insert-file-contents filename)
4461 (goto-char (point-min))
4463 (push (tramp-parse-rhosts-group) res))))
4466 ;; Taken from gnus/netrc.el
4468 (defalias 'tramp-point-at-eol
4469 (if (fboundp 'point-at-eol)
4471 'line-end-position)))
4473 (defun tramp-parse-rhosts-group ()
4474 "Return a (user host) tuple allowed to access.
4475 Either user or host may be nil."
4480 "^\\(" tramp-host-regexp "\\)"
4481 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4483 (narrow-to-region (point) (tramp-point-at-eol))
4484 (when (re-search-forward regexp nil t)
4485 (setq result (append (list (match-string 3) (match-string 1)))))
4490 (defun tramp-parse-shosts (filename)
4491 "Return a list of (user host) tuples allowed to access.
4492 User is always nil."
4495 (when (file-readable-p filename)
4497 (insert-file-contents filename)
4498 (goto-char (point-min))
4500 (push (tramp-parse-shosts-group) res))))
4503 (defun tramp-parse-shosts-group ()
4504 "Return a (user host) tuple allowed to access.
4505 User is always nil."
4508 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4510 (narrow-to-region (point) (tramp-point-at-eol))
4511 (when (re-search-forward regexp nil t)
4512 (setq result (list nil (match-string 1))))
4515 (> (skip-chars-forward ",") 0)
4519 (defun tramp-parse-sconfig (filename)
4520 "Return a list of (user host) tuples allowed to access.
4521 User is always nil."
4524 (when (file-readable-p filename)
4526 (insert-file-contents filename)
4527 (goto-char (point-min))
4529 (push (tramp-parse-sconfig-group) res))))
4532 (defun tramp-parse-sconfig-group ()
4533 "Return a (user host) tuple allowed to access.
4534 User is always nil."
4537 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4539 (narrow-to-region (point) (tramp-point-at-eol))
4540 (when (re-search-forward regexp nil t)
4541 (setq result (list nil (match-string 1))))
4544 (> (skip-chars-forward ",") 0)
4548 (defun tramp-parse-shostkeys (dirname)
4549 "Return a list of (user host) tuples allowed to access.
4550 User is always nil."
4552 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4553 (files (when (file-directory-p dirname) (directory-files dirname)))
4557 (when (string-match regexp (car files))
4558 (push (list nil (match-string 1 (car files))) result))
4559 (setq files (cdr files)))
4562 (defun tramp-parse-sknownhosts (dirname)
4563 "Return a list of (user host) tuples allowed to access.
4564 User is always nil."
4566 (let ((regexp (concat "^\\(" tramp-host-regexp
4567 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4568 (files (when (file-directory-p dirname) (directory-files dirname)))
4572 (when (string-match regexp (car files))
4573 (push (list nil (match-string 1 (car files))) result))
4574 (setq files (cdr files)))
4577 (defun tramp-parse-hosts (filename)
4578 "Return a list of (user host) tuples allowed to access.
4579 User is always nil."
4582 (when (file-readable-p filename)
4584 (insert-file-contents filename)
4585 (goto-char (point-min))
4587 (push (tramp-parse-hosts-group) res))))
4590 (defun tramp-parse-hosts-group ()
4591 "Return a (user host) tuple allowed to access.
4592 User is always nil."
4595 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4597 (narrow-to-region (point) (tramp-point-at-eol))
4598 (when (re-search-forward regexp nil t)
4599 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4600 (setq result (list nil (match-string 1)))))
4603 (> (skip-chars-forward " \t") 0)
4607 ;; For su-alike methods it would be desirable to return "root@localhost"
4608 ;; as default. Unfortunately, we have no information whether any user name
4609 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4610 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4611 (defun tramp-parse-passwd (filename)
4612 "Return a list of (user host) tuples allowed to access.
4613 Host is always \"localhost\"."
4616 (if (zerop (length tramp-current-user))
4618 (when (file-readable-p filename)
4620 (insert-file-contents filename)
4621 (goto-char (point-min))
4623 (push (tramp-parse-passwd-group) res))))
4626 (defun tramp-parse-passwd-group ()
4627 "Return a (user host) tuple allowed to access.
4628 Host is always \"localhost\"."
4631 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4633 (narrow-to-region (point) (tramp-point-at-eol))
4634 (when (re-search-forward regexp nil t)
4635 (setq result (list (match-string 1) "localhost")))
4640 (defun tramp-parse-netrc (filename)
4641 "Return a list of (user host) tuples allowed to access.
4645 (when (file-readable-p filename)
4647 (insert-file-contents filename)
4648 (goto-char (point-min))
4650 (push (tramp-parse-netrc-group) res))))
4653 (defun tramp-parse-netrc-group ()
4654 "Return a (user host) tuple allowed to access.
4660 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4661 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4663 (narrow-to-region (point) (tramp-point-at-eol))
4664 (when (re-search-forward regexp nil t)
4665 (setq result (list (match-string 3) (match-string 1))))
4670 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4671 "Like `expand-file-name' for tramp files."
4672 (let ((fullname (concat (or dir default-directory) name)))
4673 (tramp-drop-volume-letter
4674 (if (tramp-completion-mode fullname)
4675 (tramp-run-real-handler
4676 'expand-file-name (list name dir))
4677 (tramp-completion-run-real-handler
4678 'expand-file-name (list name dir))))))
4680 ;;; Internal Functions:
4682 (defun tramp-set-auto-save ()
4683 (when (and (buffer-file-name)
4684 (tramp-tramp-file-p (buffer-file-name))
4686 (auto-save-mode 1)))
4687 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4689 (defun tramp-run-test (switch filename)
4690 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4691 Returns the exit code of the `test' program."
4692 (let ((v (tramp-dissect-file-name filename)))
4694 (tramp-send-command-and-check
4695 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4696 (tramp-file-name-user v) (tramp-file-name-host v)
4697 (format "test %s %s" switch
4698 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4700 (defun tramp-run-test2 (program file1 file2 &optional switch)
4701 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4702 The optional SWITCH is inserted between the two files.
4703 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4704 hosts, or files, disagree."
4705 (let* ((v1 (tramp-dissect-file-name file1))
4706 (v2 (tramp-dissect-file-name file2))
4707 (mmethod1 (tramp-file-name-multi-method v1))
4708 (mmethod2 (tramp-file-name-multi-method v2))
4709 (method1 (tramp-file-name-method v1))
4710 (method2 (tramp-file-name-method v2))
4711 (user1 (tramp-file-name-user v1))
4712 (user2 (tramp-file-name-user v2))
4713 (host1 (tramp-file-name-host v1))
4714 (host2 (tramp-file-name-host v2))
4715 (localname1 (tramp-file-name-localname v1))
4716 (localname2 (tramp-file-name-localname v2)))
4717 (unless (and method1 method2 host1 host2
4718 (equal mmethod1 mmethod2)
4719 (equal method1 method2)
4721 (equal host1 host2))
4722 (error "tramp-run-test2: %s"
4723 "only implemented for same method, same user, same host"))
4725 (tramp-send-command-and-check
4726 mmethod1 method1 user1 host1
4727 (format "%s %s %s %s"
4729 (tramp-shell-quote-argument localname1)
4731 (tramp-shell-quote-argument localname2))))))
4733 (defun tramp-touch (file time)
4734 "Set the last-modified timestamp of the given file.
4735 TIME is an Emacs internal time value as returned by `current-time'."
4736 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
4737 (if (tramp-tramp-file-p file)
4738 (with-parsed-tramp-file-name file nil
4739 (let ((buf (tramp-get-buffer multi-method method user host)))
4740 (unless (zerop (tramp-send-command-and-check
4741 multi-method method user host
4742 (format "touch -t %s %s"
4746 (error "tramp-touch: touch failed, see buffer `%s' for details"
4748 ;; It's a local file
4750 (unless (zerop (call-process
4751 "touch" nil (current-buffer) nil "-t" touch-time file))
4752 (pop-to-buffer (current-buffer))
4753 (error "tramp-touch: touch failed"))))))
4755 (defun tramp-buffer-name (multi-method method user host)
4756 "A name for the connection buffer for USER at HOST using METHOD."
4758 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
4759 (let ((method (tramp-find-method multi-method method user host)))
4761 (format "*tramp/%s %s@%s*" method user host)
4762 (format "*tramp/%s %s*" method host)))))
4764 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
4765 "A name for the multi method connection buffer.
4766 MULTI-METHOD gives the multi method, METHOD the array of methods,
4767 USER the array of user names, HOST the array of host names."
4768 (unless (and (= (length method) (length user))
4769 (= (length method) (length host)))
4770 (error "Syntax error in multi method (implementation error)"))
4771 (let ((len (length method))
4776 (cons (if (aref user i)
4777 (format "%s#%s@%s:" (aref method i)
4778 (aref user i) (aref host i))
4779 (format "%s@%s:" (aref method i) (aref host i)))
4782 (format "*%s/%s %s*"
4784 (apply 'concat (reverse string-list)))))
4786 (defun tramp-get-buffer (multi-method method user host)
4787 "Get the connection buffer to be used for USER at HOST using METHOD."
4788 (get-buffer-create (tramp-buffer-name multi-method method user host)))
4790 (defun tramp-debug-buffer-name (multi-method method user host)
4791 "A name for the debug buffer for USER at HOST using METHOD."
4793 (tramp-buffer-name-multi-method "debug tramp"
4794 multi-method method user host)
4795 (let ((method (tramp-find-method multi-method method user host)))
4797 (format "*debug tramp/%s %s@%s*" method user host)
4798 (format "*debug tramp/%s %s*" method host)))))
4800 (defun tramp-get-debug-buffer (multi-method method user host)
4801 "Get the debug buffer for USER at HOST using METHOD."
4802 (get-buffer-create (tramp-debug-buffer-name multi-method method user host)))
4804 (defun tramp-find-executable (multi-method method user host
4805 progname dirlist ignore-tilde)
4806 "Searches for PROGNAME in all directories mentioned in DIRLIST.
4807 First args METHOD, USER and HOST specify the connection, PROGNAME
4808 is the program to search for, and DIRLIST gives the list of directories
4809 to search. If IGNORE-TILDE is non-nil, directory names starting
4810 with `~' will be ignored.
4812 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
4814 This function expects to be in the right *tramp* buffer."
4817 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
4818 ;; `remove' is in CL, and we want to avoid CL dependencies.
4821 (setq d (car dirlist))
4822 (setq dirlist (cdr dirlist))
4823 (unless (char-equal ?~ (aref d 0))
4824 (setq newdl (cons d newdl))))
4825 (setq dirlist (nreverse newdl))))
4827 multi-method method user host
4828 (format (concat "while read d; "
4829 "do if test -x $d/%s -a -f $d/%s; "
4830 "then echo tramp_executable $d/%s; "
4831 "break; fi; done <<'EOF'")
4832 progname progname progname))
4834 (tramp-send-command multi-method method user host d))
4836 (tramp-send-command multi-method method user host "EOF")
4837 (tramp-wait-for-output)
4838 (goto-char (point-max))
4839 (when (search-backward "tramp_executable " nil t)
4840 (skip-chars-forward "^ ")
4841 (skip-chars-forward " ")
4842 (buffer-substring (point) (tramp-line-end-position)))))
4844 (defun tramp-set-remote-path (multi-method method user host var dirlist)
4845 "Sets the remote environment VAR to existing directories from DIRLIST.
4846 I.e., for each directory in DIRLIST, it is tested whether it exists and if
4847 so, it is added to the environment variable VAR."
4848 (let ((existing-dirs
4853 (tramp-make-tramp-file-name multi-method method user host x))
4855 (tramp-make-tramp-file-name multi-method method user host x)))
4859 multi-method method user host
4861 (mapconcat 'identity (delq nil existing-dirs) ":")
4863 (tramp-wait-for-output)))
4865 ;; -- communication with external shell --
4867 (defun tramp-find-file-exists-command (multi-method method user host)
4868 "Find a command on the remote host for checking if a file exists.
4869 Here, we are looking for a command which has zero exit status if the
4870 file exists and nonzero exit status otherwise."
4871 (make-local-variable 'tramp-file-exists-command)
4872 (tramp-message 9 "Finding command to check if file exists")
4874 (tramp-make-tramp-file-name
4875 multi-method method user host
4876 "/")) ;assume this file always exists
4878 (tramp-make-tramp-file-name
4879 multi-method method user host
4880 "/ this file does not exist "))) ;assume this never exists
4881 ;; The algorithm is as follows: we try a list of several commands.
4882 ;; For each command, we first run `$cmd /' -- this should return
4883 ;; true, as the root directory always exists. And then we run
4884 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
4885 ;; does not exist. This should return false. We use the first
4886 ;; command we find that seems to work.
4887 ;; The list of commands to try is as follows:
4888 ;; `ls -d' This works on most systems, but NetBSD 1.4
4889 ;; has a bug: `ls' always returns zero exit
4890 ;; status, even for files which don't exist.
4891 ;; `test -e' Some Bourne shells have a `test' builtin
4892 ;; which does not know the `-e' option.
4893 ;; `/bin/test -e' For those, the `test' binary on disk normally
4894 ;; provides the option. Alas, the binary
4895 ;; is sometimes `/bin/test' and sometimes it's
4897 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
4899 (and (setq tramp-file-exists-command "test -e %s")
4900 (file-exists-p existing)
4901 (not (file-exists-p nonexisting)))
4902 (and (setq tramp-file-exists-command "/bin/test -e %s")
4903 (file-exists-p existing)
4904 (not (file-exists-p nonexisting)))
4905 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
4906 (file-exists-p existing)
4907 (not (file-exists-p nonexisting)))
4908 (and (setq tramp-file-exists-command "ls -d %s")
4909 (file-exists-p existing)
4910 (not (file-exists-p nonexisting))))
4911 (error "Couldn't find command to check if file exists."))))
4914 ;; CCC test ksh or bash found for tilde expansion?
4915 (defun tramp-find-shell (multi-method method user host)
4916 "Find a shell on the remote host which groks tilde expansion."
4918 (tramp-send-command multi-method method user host "echo ~root")
4919 (tramp-wait-for-output)
4921 ((string-match "^~root$" (buffer-string))
4923 (or (tramp-find-executable multi-method method user host
4924 "bash" tramp-remote-path t)
4925 (tramp-find-executable multi-method method user host
4926 "ksh" tramp-remote-path t)))
4928 (error "Couldn't find a shell which groks tilde expansion"))
4929 ;; Find arguments for this shell.
4930 (let ((alist tramp-sh-extra-args)
4932 (while (and alist (null extra-args))
4933 (setq item (pop alist))
4934 (when (string-match (car item) shell)
4935 (setq extra-args (cdr item))))
4936 (when extra-args (setq shell (concat shell " " extra-args))))
4938 5 "Starting remote shell `%s' for tilde expansion..." shell)
4940 multi-method method user host
4941 (concat "PS1='$ ' exec " shell)) ;
4942 (unless (tramp-wait-for-regexp
4943 (get-buffer-process (current-buffer))
4944 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'"
4945 tramp-shell-prompt-pattern shell-prompt-pattern))
4946 (pop-to-buffer (buffer-name))
4947 (error "Couldn't find remote `%s' prompt." shell))
4949 9 "Setting remote shell prompt...")
4950 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
4951 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
4952 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
4954 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
4955 tramp-rsh-end-of-line
4957 tramp-rsh-end-of-line
4958 tramp-rsh-end-of-line))
4959 (tramp-wait-for-output)
4961 9 "Setting remote shell prompt...done")
4963 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
4964 (tramp-get-method-parameter
4965 multi-method method user host 'tramp-remote-sh))))))
4967 (defun tramp-check-ls-command (multi-method method user host cmd)
4968 "Checks whether the given `ls' executable groks `-n'.
4969 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
4970 the `ls' executable. Returns t if CMD supports the `-n' option, nil
4972 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
4973 (when (file-executable-p
4974 (tramp-make-tramp-file-name multi-method method user host cmd))
4976 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
4978 (tramp-send-command-and-check
4979 multi-method method user host
4980 (format "%s -lnd / >/dev/null"
4982 (tramp-message 7 "Testing remote command `%s' for -n...%s"
4984 (if (zerop result) "okay" "failed"))
4987 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
4988 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
4989 Returns nil if none was found, else the command is returned."
4992 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
4993 ;; It would be better to use the CL function `find', but
4994 ;; we don't want run-time dependencies on CL.
4995 (while (and dl (not result))
4996 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
4997 (when (tramp-check-ls-command multi-method method user host x)
5002 (defun tramp-find-ls-command (multi-method method user host)
5003 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5004 \(This option prints numeric user and group ids in a long listing.)"
5005 (tramp-message 9 "Finding a suitable `ls' command")
5007 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5008 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5009 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5011 ;; ------------------------------------------------------------
5012 ;; -- Functions for establishing connection --
5013 ;; ------------------------------------------------------------
5015 ;; The following functions are actions to be taken when seeing certain
5016 ;; prompts from the remote host. See the variable
5017 ;; `tramp-actions-before-shell' for usage of these functions.
5019 (defun tramp-action-login (p multi-method method user host)
5020 "Send the login name."
5021 (tramp-message 9 "Sending login name `%s'"
5022 (or user (user-login-name)))
5024 (process-send-string nil (concat (or user (user-login-name))
5025 tramp-rsh-end-of-line)))
5027 (defun tramp-action-password (p multi-method method user host)
5028 "Query the user for a password."
5029 (let ((pw-prompt (match-string 0)))
5030 (tramp-message 9 "Sending password")
5031 (tramp-enter-password p pw-prompt user host)))
5033 (defun tramp-action-succeed (p multi-method method user host)
5034 "Signal success in finding shell prompt."
5035 (tramp-message 9 "Found remote shell prompt.")
5037 (throw 'tramp-action 'ok))
5039 (defun tramp-action-permission-denied (p multi-method method user host)
5040 "Signal permission denied."
5041 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5042 (tramp-message 9 "Permission denied by remote host.")
5044 (throw 'tramp-action 'permission-denied))
5046 (defun tramp-action-yesno (p multi-method method user host)
5047 "Ask the user for confirmation using `yes-or-no-p'.
5048 Send \"yes\" to remote process on confirmation, abort otherwise.
5049 See also `tramp-action-yn'."
5050 (save-window-excursion
5051 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5052 (unless (yes-or-no-p (match-string 0))
5055 (throw 'tramp-action 'permission-denied))
5056 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5059 (defun tramp-action-yn (p multi-method method user host)
5060 "Ask the user for confirmation using `y-or-n-p'.
5061 Send \"y\" to remote process on confirmation, abort otherwise.
5062 See also `tramp-action-yesno'."
5063 (save-window-excursion
5064 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5065 (unless (y-or-n-p (match-string 0))
5067 (throw 'tramp-action 'permission-denied))
5069 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5071 (defun tramp-action-terminal (p multi-method method user host)
5072 "Tell the remote host which terminal type to use.
5073 The terminal type can be configured with `tramp-terminal-type'."
5074 (tramp-message 9 "Setting `%s' as terminal type."
5075 tramp-terminal-type)
5077 (process-send-string nil (concat tramp-terminal-type
5078 tramp-rsh-end-of-line)))
5080 (defun tramp-action-process-alive (p multi-method method user host)
5081 "Check whether a process has finished."
5082 (unless (memq (process-status p) '(run open))
5083 (throw 'tramp-action 'process-died)))
5085 (defun tramp-action-out-of-band (p multi-method method user host)
5086 "Check whether an out-of-band copy has finished."
5087 (cond ((and (memq (process-status p) '(stop exit))
5088 (zerop (process-exit-status p)))
5089 (tramp-message 9 "Process has finished.")
5090 (throw 'tramp-action 'ok))
5091 ((or (and (memq (process-status p) '(stop exit))
5092 (not (zerop (process-exit-status p))))
5093 (memq (process-status p) '(signal)))
5094 (tramp-message 9 "Process has died.")
5095 (throw 'tramp-action 'process-died))
5098 ;; The following functions are specifically for multi connections.
5100 (defun tramp-multi-action-login (p method user host)
5101 "Send the login name."
5102 (tramp-message 9 "Sending login name `%s'" user)
5104 (process-send-string p (concat user tramp-rsh-end-of-line)))
5106 (defun tramp-multi-action-password (p method user host)
5107 "Query the user for a password."
5108 (tramp-message 9 "Sending password")
5109 (tramp-enter-password p (match-string 0) user host))
5111 (defun tramp-multi-action-succeed (p method user host)
5112 "Signal success in finding shell prompt."
5113 (tramp-message 9 "Found shell prompt on `%s'" host)
5115 (throw 'tramp-action 'ok))
5117 (defun tramp-multi-action-permission-denied (p method user host)
5118 "Signal permission denied."
5119 (tramp-message 9 "Permission denied by remote host `%s'" host)
5122 (throw 'tramp-action 'permission-denied))
5124 (defun tramp-multi-action-process-alive (p method user host)
5125 "Check whether a process has finished."
5126 (unless (memq (process-status p) '(run open))
5127 (throw 'tramp-action 'process-died)))
5129 ;; Functions for processing the actions.
5131 (defun tramp-process-one-action (p multi-method method user host actions)
5132 "Wait for output from the shell and perform one action."
5133 (let (found item pattern action todo)
5135 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5136 (with-timeout (60 (throw 'tramp-action 'timeout))
5138 (accept-process-output p 1)
5139 (goto-char (point-min))
5142 (goto-char (point-min))
5143 (setq item (pop todo))
5144 (setq pattern (symbol-value (nth 0 item)))
5145 (setq action (nth 1 item))
5146 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5148 (when (re-search-forward (concat pattern "\\'") nil t)
5149 (setq found (funcall action p multi-method method user host)))))
5152 (defun tramp-process-actions (p multi-method method user host actions)
5153 "Perform actions until success."
5156 (tramp-message 9 "Waiting for prompts from remote shell")
5158 (catch 'tramp-action
5159 (tramp-process-one-action
5160 p multi-method method user host actions)
5162 (unless (eq exit 'ok)
5163 (tramp-clear-passwd user host)
5164 (error "Login failed"))))
5166 ;; For multi-actions.
5168 (defun tramp-process-one-multi-action (p method user host actions)
5169 "Wait for output from the shell and perform one action."
5170 (let (found item pattern action todo)
5172 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5173 (with-timeout (60 (throw 'tramp-action 'timeout))
5175 (accept-process-output p 1)
5177 (goto-char (point-min))
5179 (goto-char (point-min))
5180 (setq item (pop todo))
5181 (setq pattern (symbol-value (nth 0 item)))
5182 (setq action (nth 1 item))
5183 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5185 (when (re-search-forward (concat pattern "\\'") nil t)
5186 (setq found (funcall action p method user host)))))
5189 (defun tramp-process-multi-actions (p method user host actions)
5190 "Perform actions until success."
5193 (tramp-message 9 "Waiting for prompts from remote shell")
5195 (catch 'tramp-action
5196 (tramp-process-one-multi-action p method user host actions)
5198 (unless (eq exit 'ok)
5199 (tramp-clear-passwd user host)
5200 (error "Login failed"))))
5202 ;; Functions to execute when we have seen the remote shell prompt but
5203 ;; before we exec the Bourne-ish shell. Note that these commands
5204 ;; might be sent to any shell, not just a Bourne-ish shell. This
5205 ;; means that the commands need to work in all shells. (It is also
5206 ;; okay for some commands to just fail with an error message, but
5207 ;; please make sure that they at least don't crash the odd shell people
5208 ;; might be running...)
5209 (defun tramp-process-initial-commands (p
5210 multi-method method user host
5212 "Send list of commands to remote host, in order."
5215 (setq cmd (pop commands))
5217 (tramp-message 10 "Sending command to remote shell: %s"
5219 (tramp-send-command multi-method method user host cmd nil t)
5220 (tramp-barf-if-no-shell-prompt
5221 p 60 "Remote shell command failed: %s" cmd))
5224 ;; The actual functions for opening connections.
5226 (defun tramp-open-connection-telnet (multi-method method user host)
5227 "Open a connection using a telnet METHOD.
5228 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5229 login prompt, then sends the user name USER, then waits for a remote
5230 password prompt. It queries the user for the password, then sends the
5231 password to the remote host.
5233 If USER is nil, uses value returned by `(user-login-name)' instead.
5235 Recognition of the remote shell prompt is based on the variables
5236 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5239 Please note that it is NOT possible to use this connection method
5240 together with an out-of-band transfer method! You must use an inline
5243 Maybe the different regular expressions need to be tuned.
5245 * Actually, the telnet program as well as the args to be used can be
5246 specified in the method parameters, see the variable `tramp-methods'."
5248 (when (tramp-method-out-of-band-p multi-method method user host)
5249 (error "Cannot use out-of-band method `%s' with telnet connection method"
5252 (error "Cannot multi-connect using telnet connection method"))
5253 (tramp-pre-connection multi-method method user host)
5254 (tramp-message 7 "Opening connection for %s@%s using %s..."
5255 (or user (user-login-name)) host method)
5256 (let ((process-environment (copy-sequence process-environment)))
5257 (setenv "TERM" tramp-terminal-type)
5258 (let* ((default-directory (tramp-temporary-file-directory))
5259 ;; If we omit the conditional here, then we would use
5260 ;; `undecided-dos' in some cases. With the conditional,
5261 ;; we use nil in these cases. Which one is right?
5262 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5263 (> emacs-major-version 20))
5264 tramp-dos-coding-system))
5265 (p (apply 'start-process
5266 (tramp-buffer-name multi-method method user host)
5267 (tramp-get-buffer multi-method method user host)
5268 (tramp-get-method-parameter
5270 (tramp-find-method multi-method method user host)
5271 user host 'tramp-login-program)
5273 (tramp-get-method-parameter
5275 (tramp-find-method multi-method method user host)
5276 user host 'tramp-login-args)))
5279 (tramp-set-process-query-on-exit-flag p nil)
5280 (set-buffer (tramp-get-buffer multi-method method user host))
5282 (tramp-process-actions p multi-method method user host
5283 tramp-actions-before-shell)
5284 (tramp-open-connection-setup-interactive-shell
5285 p multi-method method user host)
5286 (tramp-post-connection multi-method method user host)))))
5289 (defun tramp-open-connection-rsh (multi-method method user host)
5290 "Open a connection using an rsh METHOD.
5291 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5292 password or shell prompt. If a password prompt is seen, the user is
5293 queried for a password, this function sends the password to the remote
5294 host and waits for a shell prompt.
5296 If USER is nil, start the command `rsh HOST'[*] instead
5298 Recognition of the remote shell prompt is based on the variables
5299 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5302 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5303 be a port number for ssh, and \"-p yy\" will be added to the list of
5304 arguments, and xx will be used as the host name to connect to.
5306 * Actually, the rsh program to be used can be specified in the
5307 method parameters, see the variable `tramp-methods'."
5310 (error "Cannot multi-connect using rsh connection method"))
5311 (tramp-pre-connection multi-method method user host)
5312 (if (and user (not (string= user "")))
5313 (tramp-message 7 "Opening connection for %s@%s using %s..."
5315 (tramp-message 7 "Opening connection at %s using %s..." host method))
5316 (let ((process-environment (copy-sequence process-environment))
5317 (bufnam (tramp-buffer-name multi-method method user host))
5318 (buf (tramp-get-buffer multi-method method user host))
5319 (login-program (tramp-get-method-parameter
5321 (tramp-find-method multi-method method user host)
5322 user host 'tramp-login-program))
5323 (login-args (tramp-get-method-parameter
5325 (tramp-find-method multi-method method user host)
5326 user host 'tramp-login-args))
5328 ;; The following should be changed. We need a more general
5329 ;; mechanism to parse extra host args.
5330 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5331 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5332 (setq real-host (match-string 1 host)))
5333 (setenv "TERM" tramp-terminal-type)
5334 (let* ((default-directory (tramp-temporary-file-directory))
5335 ;; If we omit the conditional, we would use
5336 ;; `undecided-dos' in some cases. With the conditional,
5337 ;; we use nil in these cases. Which one is right?
5338 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5339 (> emacs-major-version 20))
5340 tramp-dos-coding-system))
5341 (p (if (and user (not (string= user "")))
5342 (apply #'start-process bufnam buf login-program
5343 real-host "-l" user login-args)
5344 (apply #'start-process bufnam buf login-program
5345 real-host login-args)))
5347 (tramp-set-process-query-on-exit-flag p nil)
5350 (tramp-process-actions p multi-method method user host
5351 tramp-actions-before-shell)
5352 (tramp-message 7 "Initializing remote shell")
5353 (tramp-open-connection-setup-interactive-shell
5354 p multi-method method user host)
5355 (tramp-post-connection multi-method method user host)))))
5357 (defun tramp-open-connection-su (multi-method method user host)
5358 "Open a connection using the `su' program with METHOD.
5359 This starts `su - USER', then waits for a password prompt. The HOST
5360 name must be equal to the local host name or to `localhost'.
5362 If USER is nil, uses value returned by user-login-name instead.
5364 Recognition of the remote shell prompt is based on the variables
5365 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5366 set up correctly. Note that the other user may have a different shell
5367 prompt than you do, so it is not at all unlikely that the variable
5368 `shell-prompt-pattern' is set up wrongly!"
5370 (when (tramp-method-out-of-band-p multi-method method user host)
5371 (error "Cannot use out-of-band method `%s' with `su' connection method"
5373 (unless (or (string-match (concat "^" (regexp-quote host))
5375 (string= "localhost" host)
5378 "Cannot connect to different host `%s' with `su' connection method"
5380 (tramp-pre-connection multi-method method user host)
5381 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5382 (or user "<root>") method)
5383 (let ((process-environment (copy-sequence process-environment)))
5384 (setenv "TERM" tramp-terminal-type)
5385 (let* ((default-directory (tramp-temporary-file-directory))
5386 ;; If we omit the conditional, we use `undecided-dos' in
5387 ;; some cases. With the conditional, we use nil in these
5388 ;; cases. What's the difference? Which one is right?
5389 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5390 (> emacs-major-version 20))
5391 tramp-dos-coding-system))
5392 (p (apply 'start-process
5393 (tramp-buffer-name multi-method method user host)
5394 (tramp-get-buffer multi-method method user host)
5395 (tramp-get-method-parameter
5397 (tramp-find-method multi-method method user host)
5398 user host 'tramp-login-program)
5401 (format-spec x `((?u . ,(or user "root")))))
5402 (tramp-get-method-parameter
5404 (tramp-find-method multi-method method user host)
5405 user host 'tramp-login-args))))
5408 (tramp-set-process-query-on-exit-flag p nil)
5409 (set-buffer (tramp-get-buffer multi-method method user host))
5410 (tramp-process-actions p multi-method method user host
5411 tramp-actions-before-shell)
5412 (tramp-open-connection-setup-interactive-shell
5413 p multi-method method user host)
5414 (tramp-post-connection multi-method method
5417 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5418 ;; handle not giving a user name in the "file name".
5420 ;; This is more difficult than for the single-hop method. In the
5421 ;; multi-hop-method, the desired behaviour should be that the
5422 ;; user must specify names for the telnet hops of which the user
5423 ;; name is different than the "original" name (or different from
5424 ;; the previous hop.
5425 (defun tramp-open-connection-multi (multi-method method user host)
5426 "Open a multi-hop connection using METHOD.
5427 This uses a slightly changed file name syntax. The idea is to say
5428 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5429 This will use telnet to log in as u1 to h1, then use rsh from there to
5430 log in as u2 to h2."
5432 (unless multi-method
5433 (error "Multi-hop open connection function called on non-multi method"))
5434 (when (tramp-method-out-of-band-p multi-method method user host)
5435 (error "No out of band multi-hop connections"))
5436 (unless (and (arrayp method) (not (stringp method)))
5437 (error "METHOD must be an array of strings for multi methods"))
5438 (unless (and (arrayp user) (not (stringp user)))
5439 (error "USER must be an array of strings for multi methods"))
5440 (unless (and (arrayp host) (not (stringp host)))
5441 (error "HOST must be an array of strings for multi methods"))
5442 (unless (and (= (length method) (length user))
5443 (= (length method) (length host)))
5444 (error "Arrays METHOD, USER, HOST must have equal length"))
5445 (tramp-pre-connection multi-method method user host)
5446 (tramp-message 7 "Opening `%s' connection..." multi-method)
5447 (let ((process-environment (copy-sequence process-environment)))
5448 (setenv "TERM" tramp-terminal-type)
5449 (let* ((default-directory (tramp-temporary-file-directory))
5450 ;; If we omit the conditional, we use `undecided-dos' in
5451 ;; some cases. With the conditional, we use nil in these
5452 ;; cases. What's the difference? Which one is right?
5453 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5454 (> emacs-major-version 20))
5455 tramp-dos-coding-system))
5456 (p (start-process (tramp-buffer-name multi-method method user host)
5457 (tramp-get-buffer multi-method method user host)
5458 tramp-multi-sh-program))
5459 (num-hops (length method))
5461 (tramp-set-process-query-on-exit-flag p nil)
5462 (tramp-message 9 "Waiting 60s for local shell to come up...")
5463 (unless (tramp-wait-for-regexp
5464 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5465 shell-prompt-pattern tramp-shell-prompt-pattern))
5466 (pop-to-buffer (buffer-name))
5468 (error "Couldn't find local shell prompt"))
5469 ;; Now do all the connections as specified.
5470 (while (< i num-hops)
5471 (let* ((m (aref method i))
5474 (entry (assoc m tramp-multi-connection-function-alist))
5475 (multi-func (nth 1 entry))
5476 (command (nth 2 entry)))
5477 ;; The multi-funcs don't need to do save-match-data, as that
5479 (funcall multi-func p m u h command)
5482 (tramp-open-connection-setup-interactive-shell
5483 p multi-method method user host)
5484 (tramp-post-connection multi-method method user host)))))
5486 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5487 ;; of no user name provided. Hack to make it work as it did before:
5488 ;; changed `user' to `(or user (user-login-name))' in the places where
5489 ;; the value is actually used.
5490 (defun tramp-multi-connect-telnet (p method user host command)
5491 "Issue `telnet' command.
5492 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5493 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5494 the host name, and `%n' is replaced with an end of line character, as
5495 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5498 If USER is nil, uses the return value of (user-login-name) instead."
5499 (let ((cmd (format-spec command
5500 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5501 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5504 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5505 (process-send-string p cmd)
5506 (tramp-process-multi-actions p method user host
5507 tramp-multi-actions)))
5509 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5510 ;; of no user name provided. Hack to make it work as it did before:
5511 ;; changed `user' to `(or user (user-login-name))' in the places where
5512 ;; the value is actually used.
5513 (defun tramp-multi-connect-rlogin (p method user host command)
5514 "Issue `rlogin' command.
5515 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5516 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5517 with the user name, `%h' will be replaced with the host name, and `%n'
5518 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5519 `%%' if you want to use a literal percent character.
5521 If USER is nil, uses the return value of (user-login-name) instead."
5522 (let ((cmd (format-spec command `((?h . ,host)
5523 (?u . ,(or user (user-login-name)))
5524 (?n . ,tramp-rsh-end-of-line))))
5525 (cmd1 (format-spec command `((?h . ,host)
5526 (?u . ,(or user (user-login-name)))
5530 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5531 (process-send-string p cmd)
5532 (tramp-process-multi-actions p method user host
5533 tramp-multi-actions)))
5535 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5536 ;; of no user name provided. Hack to make it work as it did before:
5537 ;; changed `user' to `(or user (user-login-name))' in the places where
5538 ;; the value is actually used.
5539 (defun tramp-multi-connect-su (p method user host command)
5540 "Issue `su' command.
5541 Uses shell COMMAND to issue a `su' command to log in as USER on
5542 HOST. The HOST name is ignored, this just changes the user id on the
5543 host currently logged in to.
5545 If USER is nil, uses the return value of (user-login-name) instead.
5547 You can use percent escapes in the COMMAND. `%u' is replaced with the
5548 user name, and `%n' is replaced with the value of
5549 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5551 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5552 (?n . ,tramp-rsh-end-of-line))))
5553 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5557 (tramp-message 9 "Sending su command `%s'" cmd1)
5558 (process-send-string p cmd)
5559 (tramp-process-multi-actions p method user host
5560 tramp-multi-actions)))
5562 ;; Utility functions.
5564 (defun tramp-wait-for-regexp (proc timeout regexp)
5565 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5566 Expects the output of PROC to be sent to the current buffer. Returns
5567 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5570 (start-time (current-time)))
5572 ;; Work around a bug in XEmacs 21, where the timeout
5573 ;; expires faster than it should. This degenerates
5574 ;; to polling for buggy XEmacsen, but oh, well.
5575 (while (and (not found)
5576 (< (tramp-time-diff (current-time) start-time)
5578 (with-timeout (timeout)
5580 (accept-process-output proc 1)
5581 (unless (memq (process-status proc) '(run open))
5582 (error "Process has died"))
5583 (goto-char (point-min))
5584 (setq found (when (re-search-forward regexp nil t)
5585 (tramp-match-string-list)))))))
5588 (accept-process-output proc 1)
5589 (unless (memq (process-status proc) '(run open))
5590 (error "Process has died"))
5591 (goto-char (point-min))
5592 (setq found (when (re-search-forward regexp nil t)
5593 (tramp-match-string-list))))))
5594 (when tramp-debug-buffer
5596 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5597 tramp-current-user tramp-current-host)
5598 (point-min) (point-max))
5602 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5603 tramp-current-user tramp-current-host))
5604 (goto-char (point-max))
5605 (insert "[[Regexp `" regexp "' not found"
5606 (if timeout (format " in %d secs" timeout) "")
5610 (defun tramp-wait-for-shell-prompt (proc timeout)
5611 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5612 See `tramp-wait-for-regexp' for more details.
5613 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5614 and `tramp-shell-prompt-pattern'."
5615 (tramp-wait-for-regexp
5617 (format "\\(%s\\|%s\\)\\'"
5618 shell-prompt-pattern tramp-shell-prompt-pattern)))
5620 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5621 "Wait for shell prompt and barf if none appears.
5622 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5623 seconds. If not, it produces an error message with the given ERROR-ARGS."
5624 (unless (tramp-wait-for-shell-prompt proc timeout)
5625 (pop-to-buffer (buffer-name))
5626 (apply 'error error-args)))
5628 (defun tramp-enter-password (p prompt user host)
5629 "Prompt for a password and send it to the remote end.
5630 Uses PROMPT as a prompt and sends the password to process P."
5631 (let ((pw (tramp-read-passwd user host prompt)))
5633 (process-send-string
5635 (or (tramp-get-method-parameter
5636 tramp-current-multi-method
5637 tramp-current-method
5640 'tramp-password-end-of-line)
5641 tramp-default-password-end-of-line)))))
5643 ;; HHH: Not Changed. This might handle the case where USER is not
5644 ;; given in the "File name" very poorly. Then, the local
5645 ;; variable tramp-current-user will be set to nil.
5646 (defun tramp-pre-connection (multi-method method user host)
5647 "Do some setup before actually logging in.
5648 METHOD, USER and HOST specify the connection."
5649 (set-buffer (tramp-get-buffer multi-method method user host))
5650 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5651 (set (make-local-variable 'tramp-current-method) method)
5652 (set (make-local-variable 'tramp-current-user) user)
5653 (set (make-local-variable 'tramp-current-host) host)
5654 (set (make-local-variable 'inhibit-eol-conversion) nil)
5657 (defun tramp-open-connection-setup-interactive-shell
5658 (p multi-method method user host)
5659 "Set up an interactive shell.
5660 Mainly sets the prompt and the echo correctly. P is the shell process
5661 to set up. METHOD, USER and HOST specify the connection."
5662 ;; Wait a bit in case the remote end feels like sending a little
5663 ;; junk first. It seems that fencepost.gnu.org does this when doing
5664 ;; a Kerberos login.
5666 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5667 (tramp-process-initial-commands p multi-method method user host
5668 tramp-initial-commands)
5669 ;; It is useful to set the prompt in the following command because
5670 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5671 ;; about and thus /bin/sh will display a strange prompt. For
5672 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
5673 ;; the current working directory but /bin/sh will display a dollar
5674 ;; sign. The following command line sets $PS1 to a sane value, and
5675 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
5676 ;; Pittman reports that the unusual positioning of the single quotes
5677 ;; makes it work under `rc', too. We also unset the variable $ENV
5678 ;; because that is read by some sh implementations (eg, bash when
5679 ;; called as sh) on startup; this way, we avoid the startup file
5681 (tramp-send-command-internal
5682 multi-method method user host
5683 (format "exec env 'ENV=' 'PS1=$ ' %s"
5684 (tramp-get-method-parameter
5685 multi-method method user host 'tramp-remote-sh))
5686 (format "remote `%s' to come up"
5687 (tramp-get-method-parameter
5688 multi-method method user host 'tramp-remote-sh)))
5689 (tramp-barf-if-no-shell-prompt
5691 "Remote `%s' didn't come up. See buffer `%s' for details"
5692 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
5694 (tramp-message 8 "Setting up remote shell environment")
5695 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5696 (tramp-send-command-internal multi-method method user host
5697 "stty -inlcr -echo kill '^U'")
5699 ;; Ignore garbage after stty command.
5700 (tramp-send-command-internal multi-method method user host
5703 (tramp-send-command-internal multi-method method user host
5704 "TERM=dumb; export TERM")
5705 ;; Try to set up the coding system correctly.
5706 ;; CCC this can't be the right way to do it. Hm.
5709 (tramp-message 9 "Determining coding system")
5710 (tramp-send-command-internal multi-method method user host
5711 "echo foo ; echo bar")
5712 (goto-char (point-min))
5713 (if (featurep 'mule)
5714 ;; Use MULE to select the right EOL convention for communicating
5715 ;; with the process.
5716 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
5717 cs-decode cs-encode)
5718 (when (symbolp cs) (setq cs (cons cs cs)))
5719 (setq cs-decode (car cs))
5720 (setq cs-encode (cdr cs))
5721 (unless cs-decode (setq cs-decode 'undecided))
5722 (unless cs-encode (setq cs-encode 'undecided))
5723 (setq cs-encode (tramp-coding-system-change-eol-conversion
5725 (when (search-forward "\r" nil t)
5726 (setq cs-decode (tramp-coding-system-change-eol-conversion
5728 (set-buffer-process-coding-system cs-decode cs-encode))
5729 ;; Look for ^M and do something useful if found.
5730 (when (search-forward "\r" nil t)
5731 ;; We have found a ^M but cannot frob the process coding system
5732 ;; because we're running on a non-MULE Emacs. Let's try
5735 (tramp-message 9 "Trying `stty -onlcr'")
5736 (tramp-send-command-internal multi-method method user host
5740 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
5741 (tramp-send-command-internal
5742 multi-method method user host
5743 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
5745 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
5746 (tramp-send-command-internal multi-method method user host
5747 "set +o vi +o emacs")
5749 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
5750 (tramp-send-command-internal
5751 multi-method method user host
5752 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
5754 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
5755 (tramp-send-command-internal multi-method method user host
5758 (tramp-message 9 "Setting shell prompt")
5759 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
5760 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
5761 ;; the last time we sent a command, to avoid tramp-send-command to send
5762 ;; "echo are you awake".
5763 (setq tramp-last-cmd-time (current-time))
5765 multi-method method user host
5766 (format "PS1='%s%s%s'; PS2=''; PS3=''"
5767 tramp-rsh-end-of-line
5769 tramp-rsh-end-of-line))
5770 (tramp-wait-for-output))
5772 (defun tramp-post-connection (multi-method method user host)
5773 "Prepare a remote shell before being able to work on it.
5774 METHOD, USER and HOST specify the connection.
5775 Among other things, this finds a shell which groks tilde expansion,
5776 tries to find an `ls' command which groks the `-n' option, sets the
5777 locale to C and sets up the remote shell search path."
5778 ;; Search for a good shell before searching for a command which
5779 ;; checks if a file exists. This is done because Tramp wants to use
5780 ;; "test foo; echo $?" to check if various conditions hold, and
5781 ;; there are buggy /bin/sh implementations which don't execute the
5782 ;; "echo $?" part if the "test" part has an error. In particular,
5783 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
5784 ;; with buggy /bin/sh implementations will have a working bash or
5786 (tramp-find-shell multi-method method user host)
5787 ;; Without (sit-for 0.1) at least, my machine will almost always blow
5788 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
5789 ;; output of (tramp-find-shell) to show up along with the output of
5790 ;; (tramp-find-ls-command) testing.
5792 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
5793 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
5795 ;; After much debugging I couldn't find any problem with the implementation
5796 ;; of that function though. The workaround stays for me at least. :/
5798 ;; Daniel Pittman <daniel@danann.net>
5801 (tramp-find-file-exists-command multi-method method user host)
5802 (make-local-variable 'tramp-ls-command)
5803 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
5804 (unless tramp-ls-command
5807 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
5808 (setq tramp-ls-command
5809 (tramp-find-executable multi-method method user host
5810 "ls" tramp-remote-path nil)))
5811 (unless tramp-ls-command
5812 (error "Fatal error: Couldn't find remote executable `ls'"))
5813 (tramp-message 5 "Using remote command `%s' for getting directory listings"
5815 (tramp-send-command multi-method method user host
5816 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
5817 "return $1" tramp-rsh-end-of-line
5819 (tramp-wait-for-output)
5820 ;; Set remote PATH variable.
5821 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
5822 ;; Tell remote shell to use standard time format, needed for
5823 ;; parsing `ls -l' output.
5824 (tramp-send-command multi-method method user host
5825 "LC_TIME=C; export LC_TIME; echo huhu")
5826 (tramp-wait-for-output)
5827 (tramp-send-command multi-method method user host
5828 "mesg n; echo huhu")
5829 (tramp-wait-for-output)
5830 (tramp-send-command multi-method method user host
5831 "biff n ; echo huhu")
5832 (tramp-wait-for-output)
5833 ;; Unalias ls(1) to work around issues with those silly people who make it
5834 ;; spit out ANSI escapes or whatever.
5835 (tramp-send-command multi-method method user host
5836 "unalias ls; echo huhu")
5837 (tramp-wait-for-output)
5838 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5839 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5840 ;; for otherwise the shell crashes.
5842 (make-local-variable 'tramp-test-groks-nt)
5843 (tramp-send-command multi-method method user host
5845 (tramp-wait-for-output)
5846 (goto-char (point-min))
5847 (setq tramp-test-groks-nt
5848 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
5849 (unless tramp-test-groks-nt
5851 multi-method method user host
5852 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
5853 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
5855 (tramp-wait-for-output)
5856 ;; Send the fallback `uudecode' script.
5858 (tramp-send-string multi-method method user host tramp-uudecode)
5859 (tramp-wait-for-output)
5862 (let ((tramp-remote-perl
5863 (or (tramp-find-executable multi-method method user host
5864 "perl5" tramp-remote-path nil)
5865 (tramp-find-executable multi-method method user host
5866 "perl" tramp-remote-path nil))))
5867 (when tramp-remote-perl
5868 (tramp-set-connection-property "perl" tramp-remote-perl
5869 multi-method method user host)
5870 ;; Set up stat in Perl if we can.
5871 (when tramp-remote-perl
5872 (tramp-message 5 "Sending the Perl `file-attributes' implementation.")
5874 multi-method method user host
5875 (concat "tramp_file_attributes () {\n"
5877 " -e '" tramp-perl-file-attributes "'"
5878 " \"$1\" \"$2\" 2>/dev/null\n"
5880 (tramp-wait-for-output)
5881 (unless (tramp-method-out-of-band-p multi-method method user host)
5882 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
5884 multi-method method user host
5885 (concat "tramp_encode () {\n"
5886 (format tramp-perl-encode tramp-remote-perl)
5889 (tramp-wait-for-output)
5891 multi-method method user host
5892 (concat "tramp_encode_with_module () {\n"
5893 (format tramp-perl-encode-with-module tramp-remote-perl)
5896 (tramp-wait-for-output)
5897 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
5899 multi-method method user host
5900 (concat "tramp_decode () {\n"
5901 (format tramp-perl-decode tramp-remote-perl)
5904 (tramp-wait-for-output)
5906 multi-method method user host
5907 (concat "tramp_decode_with_module () {\n"
5908 (format tramp-perl-decode-with-module tramp-remote-perl)
5911 (tramp-wait-for-output)))))
5914 (let ((ln (tramp-find-executable multi-method method user host
5915 "ln" tramp-remote-path nil)))
5917 (tramp-set-connection-property "ln" ln multi-method method user host)))
5919 ;; Find the right encoding/decoding commands to use.
5920 (unless (tramp-method-out-of-band-p multi-method method user host)
5921 (tramp-find-inline-encoding multi-method method user host))
5922 ;; If encoding/decoding command are given, test to see if they work.
5923 ;; CCC: Maybe it would be useful to run the encoder both locally and
5924 ;; remotely to see if they produce the same result.
5925 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
5926 (rem-dec (tramp-get-remote-decoding multi-method method user host))
5927 (magic-string "xyzzy"))
5928 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
5929 (tramp-kill-process multi-method method user host)
5930 ;; Improve error message and/or error check.
5932 "Must give both decoding and encoding command in method definition"))
5933 (when (and rem-enc rem-dec)
5936 "Checking to see if encoding/decoding commands work on remote host...")
5938 multi-method method user host
5939 (format "echo %s | %s | %s"
5940 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
5941 (tramp-wait-for-output)
5942 (unless (looking-at (regexp-quote magic-string))
5943 (tramp-kill-process multi-method method user host)
5944 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
5948 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
5950 ;; CCC: We should either implement a Perl version of base64 encoding
5951 ;; and decoding. Then we just use that in the last item. The other
5952 ;; alternative is to use the Perl version of UU encoding. But then
5953 ;; we need a Lisp version of uuencode.
5955 ;; Old text from documentation of tramp-methods:
5956 ;; Using a uuencode/uudecode inline method is discouraged, please use one
5957 ;; of the base64 methods instead since base64 encoding is much more
5958 ;; reliable and the commands are more standardized between the different
5959 ;; Unix versions. But if you can't use base64 for some reason, please
5960 ;; note that the default uudecode command does not work well for some
5961 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
5962 ;; the following command for uudecode:
5964 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
5966 ;; For Irix, no solution is known yet.
5968 (defvar tramp-coding-commands
5969 '(("mimencode -b" "mimencode -u -b"
5970 base64-encode-region base64-decode-region)
5971 ("mmencode -b" "mmencode -u -b"
5972 base64-encode-region base64-decode-region)
5973 ("recode data..base64" "recode base64..data"
5974 base64-encode-region base64-decode-region)
5975 ("uuencode xxx" "uudecode -o /dev/stdout"
5976 tramp-uuencode-region uudecode-decode-region)
5977 ("uuencode xxx" "uudecode -o -"
5978 tramp-uuencode-region uudecode-decode-region)
5979 ("uuencode xxx" "uudecode -p"
5980 tramp-uuencode-region uudecode-decode-region)
5981 ("uuencode xxx" "tramp_uudecode"
5982 tramp-uuencode-region uudecode-decode-region)
5983 ("tramp_encode_with_module" "tramp_decode_with_module"
5984 base64-encode-region base64-decode-region)
5985 ("tramp_encode" "tramp_decode"
5986 base64-encode-region base64-decode-region))
5987 "List of coding commands for inline transfer.
5988 Each item is a list that looks like this:
5990 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
5992 The REMOTE-ENCODING should be a string, giving a command accepting a
5993 plain file on standard input and writing the encoded file to standard
5994 output. The REMOTE-DECODING should also be a string, giving a command
5995 accepting an encoded file on standard input and writing the decoded
5996 file to standard output.
5998 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
5999 symbols, giving functions. If they are strings, then they can contain
6000 the \"%s\" format specifier. If that specifier is present, the input
6001 filename will be put into the command line at that spot. If the
6002 specifier is not present, the input should be read from standard
6005 If they are functions, they will be called with two arguments, start
6006 and end of region, and are expected to replace the region contents
6007 with the encoded or decoded results, respectively.")
6009 (defun tramp-find-inline-encoding (multi-method method user host)
6010 "Find an inline transfer encoding that works.
6011 Goes through the list `tramp-coding-commands'."
6012 (let ((commands tramp-coding-commands)
6015 (while (and commands (null found))
6016 (setq item (pop commands))
6018 (let ((rem-enc (nth 0 item))
6019 (rem-dec (nth 1 item))
6020 (loc-enc (nth 2 item))
6021 (loc-dec (nth 3 item)))
6022 ;; Check if remote encoding and decoding commands can be
6023 ;; called remotely with null input and output. This makes
6024 ;; sure there are no syntax errors and the command is really
6025 ;; found. Note that we do not redirect stdout to /dev/null,
6026 ;; for two reaons: when checking the decoding command, we
6027 ;; actually check the output it gives. And also, when
6028 ;; redirecting "mimencode" output to /dev/null, then as root
6029 ;; it might change the permissions of /dev/null!
6030 (tramp-message-for-buffer
6031 multi-method method user host 9
6032 "Checking remote encoding command `%s' for sanity" rem-enc)
6033 (unless (zerop (tramp-send-command-and-check
6034 multi-method method user host
6035 (format "%s </dev/null" rem-enc) t))
6036 (throw 'wont-work nil))
6037 (tramp-message-for-buffer
6038 multi-method method user host 9
6039 "Checking remote decoding command `%s' for sanity" rem-dec)
6040 (unless (zerop (tramp-send-command-and-check
6041 multi-method method user host
6042 (format "echo %s | %s | %s"
6043 magic rem-enc rem-dec) t))
6044 (throw 'wont-work nil))
6046 (goto-char (point-min))
6047 (unless (looking-at (regexp-quote magic))
6048 (throw 'wont-work nil)))
6049 ;; If the local encoder or decoder is a string, the
6050 ;; corresponding command has to work locally.
6051 (when (stringp loc-enc)
6052 (tramp-message-for-buffer
6053 multi-method method user host 9
6054 "Checking local encoding command `%s' for sanity" loc-enc)
6055 (unless (zerop (tramp-call-local-coding-command
6057 (throw 'wont-work nil)))
6058 (when (stringp loc-dec)
6059 (tramp-message-for-buffer
6060 multi-method method user host 9
6061 "Checking local decoding command `%s' for sanity" loc-dec)
6062 (unless (zerop (tramp-call-local-coding-command
6064 (throw 'wont-work nil)))
6065 ;; CCC: At this point, maybe we should check that the output
6066 ;; of the commands is correct. But for the moment we will
6067 ;; assume that commands working on empty input will also
6068 ;; work in practice.
6069 (setq found item))))
6070 ;; Did we find something? If not, issue error. If so,
6071 ;; set connection properties.
6073 (error "Couldn't find an inline transfer encoding"))
6074 (let ((rem-enc (nth 0 found))
6075 (rem-dec (nth 1 found))
6076 (loc-enc (nth 2 found))
6077 (loc-dec (nth 3 found)))
6078 (tramp-message 10 "Using remote encoding %s" rem-enc)
6079 (tramp-set-remote-encoding multi-method method user host rem-enc)
6080 (tramp-message 10 "Using remote decoding %s" rem-dec)
6081 (tramp-set-remote-decoding multi-method method user host rem-dec)
6082 (tramp-message 10 "Using local encoding %s" loc-enc)
6083 (tramp-set-local-encoding multi-method method user host loc-enc)
6084 (tramp-message 10 "Using local decoding %s" loc-dec)
6085 (tramp-set-local-decoding multi-method method user host loc-dec))))
6087 (defun tramp-call-local-coding-command (cmd input output)
6088 "Call the local encoding or decoding command.
6089 If CMD contains \"%s\", provide input file INPUT there in command.
6090 Otherwise, INPUT is passed via standard input.
6091 INPUT can also be nil which means `/dev/null'.
6092 OUTPUT can be a string (which specifies a filename), or t (which
6093 means standard output and thus the current buffer), or nil (which
6096 tramp-encoding-shell ;program
6097 (when (and input (not (string-match "%s" cmd)))
6099 (if (eq output t) t nil) ;output
6101 tramp-encoding-command-switch
6102 ;; actual shell command
6104 (if (string-match "%s" cmd) (format cmd input) cmd)
6105 (if (stringp output) (concat "> " output) ""))))
6107 (defun tramp-maybe-open-connection (multi-method method user host)
6108 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6109 Does not do anything if a connection is already open, but re-opens the
6110 connection if a previous connection has died for some reason."
6111 (let ((p (get-buffer-process
6112 (tramp-get-buffer multi-method method user host)))
6114 ;; If too much time has passed since last command was sent, look
6115 ;; whether process is still alive. If it isn't, kill it. When
6116 ;; using ssh, it can sometimes happen that the remote end has hung
6117 ;; up but the local ssh client doesn't recognize this until it
6118 ;; tries to send some data to the remote end. So that's why we
6119 ;; try to send a command from time to time, then look again
6120 ;; whether the process is really alive.
6122 (set-buffer (tramp-get-buffer multi-method method user host))
6123 (when (and tramp-last-cmd-time
6124 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6125 p (processp p) (memq (process-status p) '(run open)))
6127 multi-method method user host "echo are you awake" nil t)
6128 (unless (tramp-wait-for-output 10)
6132 (unless (and p (processp p) (memq (process-status p) '(run open)))
6133 (when (and p (processp p))
6135 (let ((process-connection-type tramp-process-connection-type))
6136 (funcall (tramp-get-method-parameter
6138 (tramp-find-method multi-method method user host)
6139 user host 'tramp-connection-function)
6140 multi-method method user host)))))
6142 (defun tramp-send-command
6143 (multi-method method user host command &optional noerase neveropen)
6144 "Send the COMMAND to USER at HOST (logged in using METHOD).
6145 Erases temporary buffer before sending the command (unless NOERASE
6147 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6148 connection. This is meant to be used from
6149 `tramp-maybe-open-connection' only."
6151 (tramp-maybe-open-connection multi-method method user host))
6152 (setq tramp-last-cmd-time (current-time))
6153 (setq tramp-last-cmd command)
6154 (when tramp-debug-buffer
6156 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6157 (goto-char (point-max))
6158 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6160 (set-buffer (tramp-get-buffer multi-method method user host))
6161 (unless noerase (erase-buffer))
6162 (setq proc (get-buffer-process (current-buffer)))
6163 (process-send-string proc
6164 (concat command tramp-rsh-end-of-line))))
6166 (defun tramp-send-command-internal
6167 (multi-method method user host command &optional msg)
6168 "Send command to remote host and wait for success.
6169 Sends COMMAND, then waits 30 seconds for shell prompt."
6170 (tramp-send-command multi-method method user host command t t)
6172 (tramp-message 9 "Waiting 30s for %s..." msg))
6173 (tramp-barf-if-no-shell-prompt
6175 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6177 (defun tramp-wait-for-output (&optional timeout)
6178 "Wait for output from remote rsh command."
6179 (let ((proc (get-buffer-process (current-buffer)))
6181 (start-time (current-time))
6182 (start-point (point))
6183 (end-of-output (concat "^"
6184 (regexp-quote tramp-end-of-output)
6186 ;; Algorithm: get waiting output. See if last line contains
6187 ;; end-of-output sentinel. If not, wait a bit and again get
6188 ;; waiting output. Repeat until timeout expires or end-of-output
6189 ;; sentinel is seen. Will hang if timeout is nil and
6190 ;; end-of-output sentinel never appears.
6193 ;; Work around an XEmacs bug, where the timeout expires
6194 ;; faster than it should. This degenerates into polling
6195 ;; for buggy XEmacsen, but oh, well.
6196 (while (and (not found)
6197 (< (tramp-time-diff (current-time) start-time)
6199 (with-timeout (timeout)
6201 (accept-process-output proc 1)
6202 (unless (memq (process-status proc) '(run open))
6203 (error "Process has died"))
6204 (goto-char (point-max))
6206 (setq found (looking-at end-of-output))))))
6209 (accept-process-output proc 1)
6210 (unless (memq (process-status proc) '(run open))
6211 (error "Process has died"))
6212 (goto-char (point-max))
6214 (setq found (looking-at end-of-output))))))
6215 ;; At this point, either the timeout has expired or we have found
6216 ;; the end-of-output sentinel.
6218 (goto-char (point-max))
6220 (delete-region (point) (point-max)))
6221 ;; If processing echoes, look for it in the first line and delete.
6222 (when tramp-process-echoes
6224 (goto-char start-point)
6225 (when (looking-at (regexp-quote tramp-last-cmd))
6226 (delete-region (point) (forward-line 1)))))
6227 ;; Add output to debug buffer if appropriate.
6228 (when tramp-debug-buffer
6230 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6231 tramp-current-user tramp-current-host)
6232 (point-min) (point-max))
6236 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6237 tramp-current-user tramp-current-host))
6238 (goto-char (point-max))
6239 (insert "[[Remote prompt `" end-of-output "' not found"
6240 (if timeout (format " in %d secs" timeout) "")
6242 (goto-char (point-min))
6243 ;; Return value is whether end-of-output sentinel was found.
6246 (defun tramp-match-string-list (&optional string)
6247 "Returns list of all match strings.
6248 That is, (list (match-string 0) (match-string 1) ...), according to the
6250 (let* ((nmatches (/ (length (match-data)) 2))
6254 (setq res (cons (match-string i string) res))
6258 (defun tramp-send-command-and-check (multi-method method user host command
6260 "Run COMMAND and check its exit status.
6261 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6262 Sends `echo $?' along with the COMMAND for checking the exit status. If
6263 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6265 If the optional argument SUBSHELL is non-nil, the command is executed in
6266 a subshell, ie surrounded by parentheses."
6267 (tramp-send-command multi-method method user host
6268 (concat (if subshell "( " "")
6270 (if command " 2>/dev/null; " "")
6271 "echo tramp_exit_status $?"
6272 (if subshell " )" " ")))
6273 (tramp-wait-for-output)
6274 (goto-char (point-max))
6275 (unless (search-backward "tramp_exit_status " nil t)
6276 (error "Couldn't find exit status of `%s'" command))
6277 (skip-chars-forward "^ ")
6278 (read (current-buffer)))
6280 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6281 signal fmt &rest args)
6282 "Run COMMAND, check exit status, throw error if exit status not okay.
6283 Similar to `tramp-send-command-and-check' but accepts two more arguments
6284 FMT and ARGS which are passed to `error'."
6285 (unless (zerop (tramp-send-command-and-check
6286 multi-method method user host command subshell))
6287 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6289 (pop-to-buffer (current-buffer))
6290 (funcall 'signal signal (apply 'format fmt args))))
6292 ;; It seems that Tru64 Unix does not like it if long strings are sent
6293 ;; to it in one go. (This happens when sending the Perl
6294 ;; `file-attributes' implementation, for instance.) Therefore, we
6295 ;; have this function which waits a bit at each line.
6296 (defun tramp-send-string
6297 (multi-method method user host string)
6298 "Send the STRING to USER at HOST using METHOD.
6300 The STRING is expected to use Unix line-endings, but the lines sent to
6301 the remote host use line-endings as defined in the variable
6302 `tramp-rsh-end-of-line'."
6303 (let ((proc (get-buffer-process
6304 (tramp-get-buffer multi-method method user host))))
6306 (error "Can't send string to remote host -- not logged in"))
6308 (when tramp-debug-buffer
6310 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6311 (goto-char (point-max))
6312 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6313 ;; replace "\n" by `tramp-rsh-end-of-line'
6315 (mapconcat 'identity
6316 (split-string string "\n")
6317 tramp-rsh-end-of-line))
6318 (unless (or (string= string "")
6319 (string-equal (substring string -1) tramp-rsh-end-of-line))
6320 (setq string (concat string tramp-rsh-end-of-line)))
6322 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6324 (end (length string)))
6326 (tramp-message-for-buffer
6327 multi-method method user host 10
6328 "Sending chunk from %s to %s"
6329 pos (min (+ pos tramp-chunksize) end))
6330 (process-send-string
6331 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6332 (setq pos (+ pos tramp-chunksize))
6334 (process-send-string proc string))))
6336 (defun tramp-send-eof (multi-method method user host)
6337 "Send EOF to the remote end.
6338 METHOD, HOST and USER specify the connection."
6339 (let ((proc (get-buffer-process
6340 (tramp-get-buffer multi-method method user host))))
6342 (error "Can't send EOF to remote host -- not logged in"))
6343 (process-send-eof proc)))
6344 ; (process-send-string proc "\^D")))
6346 (defun tramp-kill-process (multi-method method user host)
6347 "Kill the connection process used by Tramp.
6348 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6349 (let ((proc (get-buffer-process
6350 (tramp-get-buffer multi-method method user host))))
6351 (kill-process proc)))
6353 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6354 "Erase buffer, then discard subsequent garbage.
6355 If `tramp-discard-garbage' is nil, just erase buffer."
6356 (if (not tramp-discard-garbage)
6358 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
6359 (when tramp-debug-buffer
6361 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6362 (goto-char (point-max))
6363 (tramp-insert-with-face
6364 'bold (format "Additional characters detected\n")))))))
6366 (defun tramp-mode-string-to-int (mode-string)
6367 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6368 (let* ((mode-chars (string-to-vector mode-string))
6369 (owner-read (aref mode-chars 1))
6370 (owner-write (aref mode-chars 2))
6371 (owner-execute-or-setid (aref mode-chars 3))
6372 (group-read (aref mode-chars 4))
6373 (group-write (aref mode-chars 5))
6374 (group-execute-or-setid (aref mode-chars 6))
6375 (other-read (aref mode-chars 7))
6376 (other-write (aref mode-chars 8))
6377 (other-execute-or-sticky (aref mode-chars 9)))
6381 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6382 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6384 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6385 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6386 (case owner-execute-or-setid
6387 (?x (tramp-octal-to-decimal "00100"))
6388 (?S (tramp-octal-to-decimal "04000"))
6389 (?s (tramp-octal-to-decimal "04100"))
6391 (t (error "Fourth char `%c' must be one of `xsS-'"
6392 owner-execute-or-setid)))
6394 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6395 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6397 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6398 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6399 (case group-execute-or-setid
6400 (?x (tramp-octal-to-decimal "00010"))
6401 (?S (tramp-octal-to-decimal "02000"))
6402 (?s (tramp-octal-to-decimal "02010"))
6404 (t (error "Seventh char `%c' must be one of `xsS-'"
6405 group-execute-or-setid)))
6407 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6408 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6410 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6411 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6412 (case other-execute-or-sticky
6413 (?x (tramp-octal-to-decimal "00001"))
6414 (?T (tramp-octal-to-decimal "01000"))
6415 (?t (tramp-octal-to-decimal "01001"))
6417 (t (error "Tenth char `%c' must be one of `xtT-'"
6418 other-execute-or-sticky)))))))
6421 (defun tramp-file-mode-from-int (mode)
6422 "Turn an integer representing a file mode into an ls(1)-like string."
6423 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6424 (user (logand (lsh mode -6) 7))
6425 (group (logand (lsh mode -3) 7))
6426 (other (logand (lsh mode -0) 7))
6427 (suid (> (logand (lsh mode -9) 4) 0))
6428 (sgid (> (logand (lsh mode -9) 2) 0))
6429 (sticky (> (logand (lsh mode -9) 1) 0)))
6430 (setq user (tramp-file-mode-permissions user suid "s"))
6431 (setq group (tramp-file-mode-permissions group sgid "s"))
6432 (setq other (tramp-file-mode-permissions other sticky "t"))
6433 (concat type user group other)))
6436 (defun tramp-file-mode-permissions (perm suid suid-text)
6437 "Convert a permission bitset into a string.
6438 This is used internally by `tramp-file-mode-from-int'."
6439 (let ((r (> (logand perm 4) 0))
6440 (w (> (logand perm 2) 0))
6441 (x (> (logand perm 1) 0)))
6442 (concat (or (and r "r") "-")
6443 (or (and w "w") "-")
6444 (or (and suid x suid-text) ; suid, execute
6445 (and suid (upcase suid-text)) ; suid, !execute
6446 (and x "x") "-")))) ; !suid
6449 (defun tramp-decimal-to-octal (i)
6450 "Return a string consisting of the octal digits of I.
6451 Not actually used. Use `(format \"%o\" i)' instead?"
6452 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6453 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6455 (t (concat (tramp-decimal-to-octal (/ i 8))
6456 (number-to-string (% i 8))))))
6459 ;;(defun tramp-octal-to-decimal (ostr)
6460 ;; "Given a string of octal digits, return a decimal number."
6461 ;; (cond ((null ostr) 0)
6462 ;; ((string= "" ostr) 0)
6463 ;; (t (let ((last (aref ostr (1- (length ostr))))
6464 ;; (rest (substring ostr 0 (1- (length ostr)))))
6465 ;; (unless (and (>= last ?0)
6467 ;; (error "Not an octal digit: %c" last))
6468 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6469 ;; Kudos to Gerd Moellmann for this suggestion.
6470 (defun tramp-octal-to-decimal (ostr)
6471 "Given a string of octal digits, return a decimal number."
6472 (let ((x (or ostr "")))
6473 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6474 (unless (string-match "\\`[0-7]*\\'" x)
6475 (error "Non-octal junk in string `%s'" x))
6476 (string-to-number ostr 8)))
6478 (defun tramp-shell-case-fold (string)
6479 "Converts STRING to shell glob pattern which ignores case."
6482 (if (equal (downcase c) (upcase c))
6484 (format "[%c%c]" (downcase c) (upcase c))))
6489 ;; ------------------------------------------------------------
6490 ;; -- TRAMP file names --
6491 ;; ------------------------------------------------------------
6492 ;; Conversion functions between external representation and
6493 ;; internal data structure. Convenience functions for internal
6496 (defstruct tramp-file-name multi-method method user host localname)
6498 (defun tramp-tramp-file-p (name)
6499 "Return t iff NAME is a tramp file."
6501 (string-match tramp-file-name-regexp name)))
6503 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6504 ;; to the `user' part of the structure if a user name was not
6505 ;; provided, now it assigns nil.
6506 (defun tramp-dissect-file-name (name)
6507 "Return an `tramp-file-name' structure.
6508 The structure consists of remote method, remote user, remote host and
6509 localname (file name on remote host)."
6511 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6514 (if match (match-string (nth 1 tramp-file-name-structure) name)
6517 (format (nth 0 tramp-multi-file-name-structure)
6518 (nth 0 tramp-multi-file-name-hop-structure)) name)
6519 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6520 (if (and method (member method tramp-multi-methods))
6521 ;; If it's a multi method, the file name structure contains
6522 ;; arrays of method, user and host.
6523 (tramp-dissect-multi-file-name name)
6524 ;; Normal method. First, find out default method.
6525 (unless match (error "Not a tramp file name: %s" name))
6526 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6527 (host (match-string (nth 3 tramp-file-name-structure) name))
6528 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6529 (make-tramp-file-name
6534 :localname localname))))))
6536 (defun tramp-find-default-method (user host)
6537 "Look up the right method to use in `tramp-default-method-alist'."
6538 (let ((choices tramp-default-method-alist)
6539 (method tramp-default-method)
6542 (setq item (pop choices))
6543 (when (and (string-match (nth 0 item) (or host ""))
6544 (string-match (nth 1 item) (or user "")))
6545 (setq method (nth 2 item))
6546 (setq choices nil)))
6549 (defun tramp-find-method (multi-method method user host)
6550 "Return the right method string to use.
6551 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6552 If both MULTI-METHOD and METHOD are nil, do a lookup in
6553 `tramp-default-method-alist'."
6554 (or multi-method method (tramp-find-default-method user host)))
6556 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6557 ;; a user name is not provided in the "file name" very well.
6558 (defun tramp-dissect-multi-file-name (name)
6559 "Not implemented yet."
6560 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6561 (method-index (nth 1 tramp-multi-file-name-structure))
6562 (hops-index (nth 2 tramp-multi-file-name-structure))
6563 (localname-index (nth 3 tramp-multi-file-name-structure))
6564 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6565 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6566 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6567 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6568 method hops len hop-methods hop-users hop-hosts localname)
6569 (unless (string-match (format regexp hop-regexp) name)
6570 (error "Not a multi tramp file name: %s" name))
6571 (setq method (match-string method-index name))
6572 (setq hops (match-string hops-index name))
6573 (setq len (/ (length (match-data t)) 2))
6574 (when (< localname-index 0) (incf localname-index len))
6575 (setq localname (match-string localname-index name))
6577 (while (string-match hop-regexp hops index)
6578 (setq index (match-end 0))
6580 (cons (match-string hop-method-index hops) hop-methods))
6582 (cons (match-string hop-user-index hops) hop-users))
6584 (cons (match-string hop-host-index hops) hop-hosts))))
6585 (make-tramp-file-name
6586 :multi-method method
6587 :method (apply 'vector (reverse hop-methods))
6588 :user (apply 'vector (reverse hop-users))
6589 :host (apply 'vector (reverse hop-hosts))
6590 :localname localname)))
6592 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6593 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6595 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6597 (concat tramp-prefix-format
6598 (when method (concat "%m" tramp-postfix-single-method-format))
6599 (when user (concat "%u" tramp-postfix-user-format))
6600 (when host (concat "%h" tramp-postfix-host-format))
6601 (when localname (concat "%p")))
6602 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6604 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6605 ;; with this when USER is nil?
6606 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6607 "Constructs a tramp file name for a multi-hop method."
6608 (unless tramp-make-multi-tramp-file-format
6609 (error "`tramp-make-multi-tramp-file-format' is nil"))
6610 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6611 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6612 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6613 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6615 (localname (format-spec localname-format `((?p . ,localname))))
6617 (len (length method)))
6619 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6620 (setq hops (concat hops (format-spec hop-format
6621 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6623 (concat prefix hops localname)))
6625 (defun tramp-make-copy-program-file-name (user host localname)
6626 "Create a file name suitable to be passed to `rcp' and workalikes."
6628 (format "%s@%s:%s" user host localname)
6629 (format "%s:%s" host localname)))
6631 (defun tramp-method-out-of-band-p (multi-method method user host)
6632 "Return t if this is an out-of-band method, nil otherwise."
6633 (tramp-get-method-parameter
6635 (tramp-find-method multi-method method user host)
6636 user host 'tramp-copy-program))
6638 ;; Variables local to connection.
6640 (defun tramp-get-ls-command (multi-method method user host)
6642 (tramp-maybe-open-connection multi-method method user host)
6643 (set-buffer (tramp-get-buffer multi-method method user host))
6646 (defun tramp-get-test-groks-nt (multi-method method user host)
6648 (tramp-maybe-open-connection multi-method method user host)
6649 (set-buffer (tramp-get-buffer multi-method method user host))
6650 tramp-test-groks-nt))
6652 (defun tramp-get-file-exists-command (multi-method method user host)
6654 (tramp-maybe-open-connection multi-method method user host)
6655 (set-buffer (tramp-get-buffer multi-method method user host))
6656 tramp-file-exists-command))
6658 (defun tramp-get-remote-perl (multi-method method user host)
6659 (tramp-get-connection-property "perl" nil multi-method method user host))
6661 (defun tramp-get-remote-ln (multi-method method user host)
6662 (tramp-get-connection-property "ln" nil multi-method method user host))
6664 ;; Get a property of a TRAMP connection.
6665 (defun tramp-get-connection-property
6666 (property default multi-method method user host)
6667 "Get the named property for the connection.
6668 If the value is not set for the connection, return `default'"
6669 (tramp-maybe-open-connection multi-method method user host)
6670 (with-current-buffer (tramp-get-buffer multi-method method user host)
6673 (symbol-value (intern (concat "tramp-connection-property-" property)))
6676 ;; Set a property of a TRAMP connection.
6677 (defun tramp-set-connection-property
6678 (property value multi-method method user host)
6679 "Set the named property of a TRAMP connection."
6680 (tramp-maybe-open-connection multi-method method user host)
6681 (with-current-buffer (tramp-get-buffer multi-method method user host)
6682 (set (make-local-variable
6683 (intern (concat "tramp-connection-property-" property)))
6686 ;; Some predefined connection properties.
6687 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6688 (tramp-set-connection-property "remote-encoding" rem-enc
6689 multi-method method user host))
6690 (defun tramp-get-remote-encoding (multi-method method user host)
6691 (tramp-get-connection-property "remote-encoding" nil
6692 multi-method method user host))
6694 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6695 (tramp-set-connection-property "remote-decoding" rem-dec
6696 multi-method method user host))
6697 (defun tramp-get-remote-decoding (multi-method method user host)
6698 (tramp-get-connection-property "remote-decoding" nil
6699 multi-method method user host))
6701 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6702 (tramp-set-connection-property "local-encoding" loc-enc
6703 multi-method method user host))
6704 (defun tramp-get-local-encoding (multi-method method user host)
6705 (tramp-get-connection-property "local-encoding" nil
6706 multi-method method user host))
6708 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6709 (tramp-set-connection-property "local-decoding" loc-dec
6710 multi-method method user host))
6711 (defun tramp-get-local-decoding (multi-method method user host)
6712 (tramp-get-connection-property "local-decoding" nil
6713 multi-method method user host))
6715 (defun tramp-get-method-parameter (multi-method method user host param)
6716 "Return the method parameter PARAM.
6717 If the `tramp-methods' entry does not exist, use the variable PARAM
6719 (unless (boundp param)
6720 (error "Non-existing method parameter `%s'" param))
6721 (let ((entry (assoc param
6722 (assoc (tramp-find-method multi-method method user host)
6726 (symbol-value param))))
6729 ;; Auto saving to a special directory.
6731 (defun tramp-make-auto-save-file-name (fn)
6732 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
6733 (when tramp-auto-save-directory
6734 (unless (file-exists-p tramp-auto-save-directory)
6735 (make-directory tramp-auto-save-directory t)))
6736 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
6737 ;; file name we make sure that jka-compr isn't used for the
6739 (let ((buffer-file-name (expand-file-name
6740 (tramp-subst-strs-in-string '(("_" . "|")
6747 tramp-auto-save-directory)))
6748 (make-auto-save-file-name)))
6750 (defadvice make-auto-save-file-name
6751 (around tramp-advice-make-auto-save-file-name () activate)
6752 "Invoke `tramp-make-auto-save-file-name' for tramp files."
6753 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
6754 tramp-auto-save-directory)
6755 (setq ad-return-value
6756 (tramp-make-auto-save-file-name (buffer-file-name)))
6759 ;; In Emacs < 21.4 and XEmacs < 21.5 autosaved remote files have
6760 ;; permission 666 minus umask. This is a security threat.
6762 (defun tramp-set-auto-save-file-modes ()
6763 "Set permissions of autosaved remote files to the original permissions."
6764 (let ((bfn (buffer-file-name)))
6765 (when (and (stringp bfn)
6766 (tramp-tramp-file-p bfn)
6767 (stringp buffer-auto-save-file-name)
6768 (not (equal bfn buffer-auto-save-file-name))
6769 (not (file-exists-p buffer-auto-save-file-name)))
6770 (write-region "" nil buffer-auto-save-file-name)
6771 (set-file-modes buffer-auto-save-file-name (file-modes bfn)))))
6773 (unless (or (> emacs-major-version 21)
6774 (and (featurep 'xemacs)
6775 (= emacs-major-version 21)
6776 (> emacs-minor-version 4))
6777 (and (not (featurep 'xemacs))
6778 (= emacs-major-version 21)
6779 (or (> emacs-minor-version 3)
6780 (and (string-match "^21\\.3\\.\\([0-9]+\\)" emacs-version)
6781 (>= (string-to-int (match-string 1 emacs-version)) 50)))))
6782 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))
6784 (defun tramp-subst-strs-in-string (alist string)
6785 "Replace all occurrences of the string FROM with TO in STRING.
6786 ALIST is of the form ((FROM . TO) ...)."
6789 (let* ((pr (car alist))
6792 (while (string-match (regexp-quote from) string)
6793 (setq string (replace-match to t t string)))
6794 (setq alist (cdr alist))))
6797 (defun tramp-insert-with-face (face string)
6798 "Insert text with a specific face."
6799 (let ((start (point)))
6801 (add-text-properties start (point) (list 'face face))))
6803 ;; ------------------------------------------------------------
6804 ;; -- Compatibility functions section --
6805 ;; ------------------------------------------------------------
6807 (defun tramp-temporary-file-directory ()
6808 "Return name of directory for temporary files (compat function).
6809 For Emacs, this is the variable `temporary-file-directory', for XEmacs
6810 this is the function `temp-directory'."
6811 (cond ((boundp 'temporary-file-directory)
6812 (symbol-value 'temporary-file-directory))
6813 ((fboundp 'temp-directory)
6814 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
6815 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
6816 (file-name-as-directory (getenv "TEMP")))
6817 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
6818 (file-name-as-directory (getenv "TMP")))
6819 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
6820 (file-name-as-directory (getenv "TMPDIR")))
6821 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
6822 (t (message (concat "Neither `temporary-file-directory' nor "
6823 "`temp-directory' is defined -- using /tmp."))
6824 (file-name-as-directory "/tmp"))))
6826 (defun tramp-read-passwd (user host prompt)
6827 "Read a password from user (compat function).
6828 Invokes `password-read' if available, `read-passwd' else."
6829 (if (functionp 'password-read)
6830 (let* ((key (concat (or user (user-login-name)) "@" host))
6831 (password (apply #'password-read (list prompt key))))
6832 (apply #'password-cache-add (list key password))
6834 (read-passwd prompt)))
6836 (defun tramp-clear-passwd (&optional user host)
6837 "Clear password cache for connection related to current-buffer."
6839 (let ((filename (or buffer-file-name list-buffers-directory "")))
6840 (when (and (functionp 'password-cache-remove)
6841 (or (and user host) (tramp-tramp-file-p filename)))
6842 (let* ((v (when (tramp-tramp-file-p filename)
6843 (tramp-dissect-file-name filename)))
6844 (luser (or user (tramp-file-name-user v) (user-login-name)))
6845 (lhost (or host (tramp-file-name-host v) (system-name)))
6846 (key (concat luser "@" lhost)))
6847 (apply #'password-cache-remove (list key))))))
6849 (defun tramp-time-diff (t1 t2)
6850 "Return the difference between the two times, in seconds.
6851 T1 and T2 are time values (as returned by `current-time' for example).
6853 NOTE: This function will fail if the time difference is too large to
6855 ;; Pacify byte-compiler with `symbol-function'.
6856 (cond ((and (fboundp 'subtract-time)
6857 (fboundp 'float-time))
6858 (funcall (symbol-function 'float-time)
6859 (funcall (symbol-function 'subtract-time) t1 t2)))
6860 ((and (fboundp 'subtract-time)
6861 (fboundp 'time-to-seconds))
6862 (funcall (symbol-function 'time-to-seconds)
6863 (funcall (symbol-function 'subtract-time) t1 t2)))
6864 ((fboundp 'itimer-time-difference)
6866 (symbol-function 'itimer-time-difference)
6867 (if (< (length t1) 3) (append t1 '(0)) t1)
6868 (if (< (length t2) 3) (append t2 '(0)) t2))))
6870 ;; snarfed from Emacs 21 time-date.el; combining
6871 ;; time-to-seconds and subtract-time
6872 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
6873 (list (- (car t1) (car t2) (if borrow 1 0))
6874 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
6875 (+ (* (car time) 65536.0)
6877 (/ (or (nth 2 time) 0) 1000000.0))))))
6879 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
6880 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
6881 EOL-TYPE can be one of `dos', `unix', or `mac'."
6882 (cond ((fboundp 'coding-system-change-eol-conversion)
6883 (apply #'coding-system-change-eol-conversion
6884 (list coding-system eol-type)))
6885 ((fboundp 'subsidiary-coding-system)
6887 #'subsidiary-coding-system
6889 (cond ((eq eol-type 'dos) 'crlf)
6890 ((eq eol-type 'unix) 'lf)
6891 ((eq eol-type 'mac) 'cr)
6893 (error "Unknown EOL-TYPE `%s', must be %s"
6895 "`dos', `unix', or `mac'"))))))
6896 (t (error "Can't change EOL conversion -- is MULE missing?"))))
6898 (defun tramp-split-string (string pattern)
6899 "Like `split-string' but omit empty strings.
6900 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
6901 This is, the first, empty, element is omitted. In XEmacs, the first
6902 element is not omitted.
6904 Note: this function has been written for `tramp-handle-file-truename'.
6905 If you want to use it for something else, you'll have to check whether
6906 it does the right thing."
6907 (delete "" (split-string string pattern)))
6909 (defun tramp-set-process-query-on-exit-flag (process flag)
6910 "Specify if query is needed for process when Emacs is exited.
6911 If the second argument flag is non-nil, Emacs will query the user before
6912 exiting if process is running."
6913 (if (fboundp 'set-process-query-on-exit-flag)
6914 (set-process-query-on-exit-flag process flag)
6915 (funcall (symbol-function 'process-kill-without-query)
6919 ;; ------------------------------------------------------------
6920 ;; -- Kludges section --
6921 ;; ------------------------------------------------------------
6923 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
6924 ;; does not deal well with newline characters. Newline is replaced by
6925 ;; backslash newline. But if, say, the string `a backslash newline b'
6926 ;; is passed to a shell, the shell will expand this into "ab",
6927 ;; completely omitting the newline. This is not what was intended.
6928 ;; It does not appear to be possible to make the function
6929 ;; `shell-quote-argument' work with newlines without making it
6930 ;; dependent on the shell used. But within this package, we know that
6931 ;; we will always use a Bourne-like shell, so we use an approach which
6934 ;; The approach is simple: we call `shell-quote-argument', then
6935 ;; massage the newline part of the result.
6937 ;; This function should produce a string which is grokked by a Unix
6938 ;; shell, even if the Emacs is running on Windows. Since this is the
6939 ;; kludges section, we bind `system-type' in such a way that
6940 ;; `shell-quote-arguments' behaves as if on Unix.
6942 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
6943 ;; function to work with Bourne-like shells.
6945 ;; CCC: This function should be rewritten so that
6946 ;; `shell-quote-argument' is not used. This way, we are safe from
6947 ;; changes in `shell-quote-argument'.
6948 (defun tramp-shell-quote-argument (s)
6949 "Similar to `shell-quote-argument', but groks newlines.
6950 Only works for Bourne-like shells."
6951 (let ((system-type 'not-windows))
6953 (let ((result (shell-quote-argument s))
6954 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
6955 (when (and (>= (length result) 2)
6956 (string= (substring result 0 2) "\\~"))
6957 (setq result (substring result 1)))
6958 (while (string-match nl result)
6959 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
6963 ;; ;; EFS hooks itself into the file name handling stuff in more places
6964 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
6965 ;; ;; away from tramp.el file names.
6967 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
6968 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
6969 ;; ;; connection to help it's dired process. Not that I have any real
6970 ;; ;; idea *why* this is helpful to dired.
6972 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
6974 ;; ;; Daniel Pittman <daniel@danann.net>
6976 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
6977 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
6978 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
6979 ;; ;; EFS might nicht be loaded yet.
6980 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
6981 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
6982 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
6986 ;; We currently (sometimes) use "[" and "]" in the filename format.
6987 ;; This means that Emacs wants to expand wildcards if
6988 ;; `find-file-wildcards' is non-nil, and then barfs because no
6989 ;; expansion could be found. We detect this situation and do
6990 ;; something really awful: we have `file-expand-wildcards' return the
6991 ;; original filename if it can't expand anything. Let's just hope
6992 ;; that this doesn't break anything else.
6993 ;; CCC: This check is now also really awful; we should search all
6994 ;; of the filename format, not just the prefix.
6995 (when (string-match "\\[" tramp-prefix-format)
6996 (defadvice file-expand-wildcards (around tramp-fix activate)
6997 (let ((name (ad-get-arg 0)))
6998 (if (tramp-tramp-file-p name)
6999 ;; If it's a Tramp file, dissect it and look if wildcards
7000 ;; need to be expanded at all.
7001 (let ((v (tramp-dissect-file-name name)))
7002 (if (string-match "[[*?]" (tramp-file-name-localname v))
7003 (let ((res ad-do-it))
7004 (setq ad-return-value (or res (list name))))
7005 (setq ad-return-value (list name))))
7006 ;; If it is not a Tramp file, just run the original function.
7007 (let ((res ad-do-it))
7008 (setq ad-return-value (or res (list name)))))))
7011 ;; Tramp version is useful in a number of situations.
7013 (defun tramp-version (arg)
7014 "Print version number of tramp.el in minibuffer or current buffer."
7016 (if arg (insert tramp-version) (message tramp-version)))
7018 ;; Make the `reporter` functionality available for making bug reports about
7019 ;; the package. A most useful piece of code.
7021 (unless (fboundp 'reporter-submit-bug-report)
7022 (autoload 'reporter-submit-bug-report "reporter"))
7025 "Submit a bug report to the TRAMP developers."
7028 (let ((reporter-prompt-for-summary-p t))
7029 (reporter-submit-bug-report
7030 tramp-bug-report-address ; to-address
7031 (format "tramp (%s)" tramp-version) ; package name and version
7035 tramp-file-exists-command
7036 tramp-current-multi-method
7037 tramp-current-method
7042 tramp-auto-save-directory ; vars to dump
7043 tramp-default-method
7044 tramp-rsh-end-of-line
7045 tramp-default-password-end-of-line
7047 tramp-login-prompt-regexp
7048 tramp-password-prompt-regexp
7049 tramp-wrong-passwd-regexp
7050 tramp-yesno-prompt-regexp
7051 tramp-yn-prompt-regexp
7052 tramp-terminal-prompt-regexp
7053 tramp-out-of-band-prompt-regexp
7054 tramp-temp-name-prefix
7055 tramp-file-name-structure
7056 tramp-file-name-regexp
7057 tramp-multi-file-name-structure
7058 tramp-multi-file-name-hop-structure
7060 tramp-multi-connection-function-alist
7063 tramp-coding-commands
7064 tramp-actions-before-shell
7065 tramp-actions-copy-out-of-band
7068 tramp-shell-prompt-pattern
7070 ,(when (boundp 'tramp-backup-directory-alist)
7071 'tramp-backup-directory-alist)
7072 ,(when (boundp 'tramp-bkup-backup-directory-info)
7073 'tramp-bkup-backup-directory-info)
7075 ;; Non-tramp variables of interest
7076 shell-prompt-pattern
7078 backup-by-copying-when-linked
7079 backup-by-copying-when-mismatch
7080 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7081 'backup-by-copying-when-privileged-mismatch)
7082 ,(when (boundp 'password-cache)
7084 ,(when (boundp 'password-cache-expiry)
7085 'password-cache-expiry)
7086 ,(when (boundp 'backup-directory-alist)
7087 'backup-directory-alist)
7088 ,(when (boundp 'bkup-backup-directory-info)
7089 'bkup-backup-directory-info)
7090 file-name-handler-alist)
7094 Enter your bug report in this message, including as much detail as you
7095 possibly can about the problem, what you did to cause it and what the
7096 local and remote machines are.
7098 If you can give a simple set of instructions to make this bug happen
7099 reliably, please include those. Thank you for helping kill bugs in
7102 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7103 the ~/.emacs file and to repeat the bug. Then, include the contents
7104 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7107 --bug report follows this line--
7110 (defalias 'tramp-submit-bug 'tramp-bug)
7114 ;; Make sure that we get integration with the VC package.
7115 ;; When it is loaded, we need to pull in the integration module.
7116 ;; This must come after (provide 'tramp) because tramp-vc.el
7118 (eval-after-load "vc"
7119 '(require 'tramp-vc))
7123 ;; * Allow putting passwords in the filename.
7124 ;; This should be implemented via a general mechanism to add
7125 ;; parameters in filenames. There is currently a kludge for
7126 ;; putting the port number into the filename for ssh and ftp
7127 ;; files. This could be subsumed by the new mechanism as well.
7128 ;; Another approach is to read a netrc file like ~/.authinfo
7130 ;; * Handle nonlocal exits such as C-g.
7131 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7132 ;; * Add fallback for inline encodings. This should be used
7133 ;; if the remote end doesn't support mimencode or a similar program.
7134 ;; For reading files from the remote host, we can just parse the output
7135 ;; of `od -b'. For writing files to the remote host, we construct
7136 ;; a shell program which contains only "safe" ascii characters
7137 ;; and which writes the right bytes to the file. We can use printf(1)
7138 ;; or "echo -e" or the printf function in awk and use octal escapes
7139 ;; for the "dangerous" characters. The null byte might be a problem.
7140 ;; On some systems, the octal escape doesn't work. So we try the following
7141 ;; two commands to write a null byte:
7142 ;; dd if=/dev/zero bs=1 count=1
7143 ;; echo | tr '\n' '\000'
7144 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7145 ;; the two via a format which can be `uu' or `b64'. Then we can search
7146 ;; for the right local commands and the right remote commands separately.
7147 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7148 ;; work for remote files.
7149 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7150 ;; `shell-quote-argument'.
7151 ;; * Completion gets confused when you leave out the method name.
7152 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7153 ;; by the files in that directory. Add this here.
7154 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7155 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7156 ;; * When logging in, keep looking for questions according to an alist
7157 ;; and then invoke the right function.
7158 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7159 ;; * Running CVS remotely doesn't appear to work right. It thinks
7160 ;; files are locked by somebody else even if I'm the locking user.
7161 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7163 ;; * Don't use globbing for directories with many files, as this is
7164 ;; likely to produce long command lines, and some shells choke on
7165 ;; long command lines.
7166 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7167 ;; do the right thing.
7168 ;; * `vc-directory' does not work. It never displays any files, even
7169 ;; if it does show files when run locally.
7170 ;; * Allow correction of passwords, if the remote end allows this.
7171 ;; (Mark Hershberger)
7172 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7173 ;; * Do asynchronous `shell-command's.
7174 ;; * Grok `append' parameter for `write-region'.
7175 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7176 ;; * abbreviate-file-name
7177 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7178 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7179 ;; what the user names should default to, though.
7180 ;; * better error checking. At least whenever we see something
7181 ;; strange when doing zerop, we should kill the process and start
7182 ;; again. (Greg Stark)
7183 ;; * Add caching for filename completion. (Greg Stark)
7184 ;; Of course, this has issues with usability (stale cache bites)
7185 ;; -- <daniel@danann.net>
7186 ;; * Provide a local cache of old versions of remote files for the rsync
7187 ;; transfer method to use. (Greg Stark)
7188 ;; * Remove unneeded parameters from methods.
7189 ;; * Invoke rsync once for copying a whole directory hierarchy.
7190 ;; (Francesco Potort
\e,Al
\e(B)
7191 ;; * Should we set PATH ourselves or should we rely on the remote end
7193 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7194 ;; * Make it work for different encodings, and for different file name
7195 ;; encodings, too. (Daniel Pittman)
7196 ;; * Change applicable functions to pass a struct tramp-file-name rather
7197 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7198 ;; * Implement asynchronous shell commands.
7199 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7200 ;; * Progress reports while copying files. (Michael Kifer)
7201 ;; * `Smart' connection method that uses inline for small and out of
7202 ;; band for large files. (Michael Kifer)
7203 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7204 ;; then look if it's the right version (with `perl -v').
7205 ;; * When editing a remote CVS controlled file as a different user, VC
7206 ;; gets confused about the file locking status. Try to find out why
7207 ;; the workaround doesn't work.
7208 ;; * Change `copy-file' to grok the case where the filename handler
7209 ;; for the source and the target file are different. Right now,
7210 ;; it looks at the source file and then calls that handler, if
7211 ;; there is one. But since ange-ftp, for instance, does not know
7212 ;; about Tramp, it does not do the right thing if the target file
7213 ;; name is a Tramp name.
7214 ;; * Username and hostname completion.
7215 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7216 ;; connect to host "blabla" already if that host is unique. No idea
7217 ;; how to suppress. Maybe not an essential problem.
7218 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7219 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7220 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7221 ;; Code is nearly identical.
7222 ;; ** Decide whiche files to take for searching user/host names depending on
7223 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7224 ;; ** Enhance variables for debug.
7225 ;; ** Implement "/multi:" completion.
7226 ;; ** Add a learning mode for completion. Make results persistent.
7227 ;; * Allow out-of-band methods as _last_ multi-hop.
7229 ;; Functions for file-name-handler-alist:
7230 ;; diff-latest-backup-file -- in diff.el
7231 ;; dired-uncache -- this will be needed when we do insert-directory caching
7232 ;; file-name-as-directory -- use primitive?
7233 ;; file-name-sans-versions -- use primitive?
7234 ;; get-file-buffer -- use primitive
7237 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7238 ;;; tramp.el ends here