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."
164 (defcustom tramp-verbose 9
165 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
169 (defcustom tramp-debug-buffer nil
170 "*Whether to send all commands and responses to a debug buffer."
176 (when (boundp 'backup-directory-alist)
177 (defcustom tramp-backup-directory-alist nil
178 "Alist of filename patterns and backup directory names.
179 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
180 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
181 is a local file name, the backup directory is prepended with Tramp file
182 name prefix \(multi-method, method, user, host\) of file.
184 \(setq tramp-backup-directory-alist backup-directory-alist\)
186 gives the same backup policy for Tramp files on their hosts like the
187 policy for local files."
189 :type '(repeat (cons (regexp :tag "Regexp matching filename")
190 (directory :tag "Backup directory name"))))))
192 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
193 ;; the package "backup-dir" might not be loaded yet.
195 (when (featurep 'xemacs)
196 (defcustom tramp-bkup-backup-directory-info nil
197 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
198 It has the same meaning like `bkup-backup-directory-info' from package
199 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
200 file name, the backup directory is prepended with Tramp file name prefix
201 \(multi-method, method, user, host\) of file.
203 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
205 gives the same backup policy for Tramp files on their hosts like the
206 policy for local files."
208 (list (regexp :tag "File regexp")
209 (string :tag "Backup Dir")
214 (const search-upward))))
217 (defcustom tramp-auto-save-directory nil
218 "*Put auto-save files in this directory, if set.
219 The idea is to use a local directory so that auto-saving is faster."
221 :type '(choice (const nil)
224 (defcustom tramp-encoding-shell
225 (if (memq system-type '(windows-nt))
228 "*Use this program for encoding and decoding commands on the local host.
229 This shell is used to execute the encoding and decoding command on the
230 local host, so if you want to use `~' in those commands, you should
231 choose a shell here which groks tilde expansion. `/bin/sh' normally
232 does not understand tilde expansion.
234 For encoding and deocding, commands like the following are executed:
236 /bin/sh -c COMMAND < INPUT > OUTPUT
238 This variable can be used to change the \"/bin/sh\" part. See the
239 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
240 variable `tramp-encoding-reads-stdin' to specify whether the commands read
241 standard input or a file.
243 Note that this variable is not used for remote commands. There are
244 mechanisms in tramp.el which automatically determine the right shell to
245 use for the remote host."
247 :type '(file :must-match t))
249 (defcustom tramp-encoding-command-switch
250 (if (string-match "cmd\\.exe" tramp-encoding-shell)
253 "*Use this switch together with `tramp-encoding-shell' for local commands.
254 See the variable `tramp-encoding-shell' for more information."
258 (defcustom tramp-encoding-reads-stdin t
259 "*If non-nil, encoding commands read from standard input.
260 If nil, the filename is the last argument.
262 Note that the commands always must write to standard output."
266 (defcustom tramp-multi-sh-program
268 "*Use this program for bootstrapping multi-hop connections.
269 This variable is similar to `tramp-encoding-shell', but it is only used
270 when initializing a multi-hop connection. Therefore, the set of
271 commands sent to this shell is quite restricted, and if you are
272 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
273 shell which does not normally exist on Windows anyway).
275 To use multi-hop methods from Windows, you also need suitable entries
276 in `tramp-multi-connection-function-alist' for the first hop.
278 This variable defaults to the value of `tramp-encoding-shell'."
280 :type '(file :must-match t))
282 ;; CCC I have changed all occurrences of comint-quote-filename with
283 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
284 ;; There, comint-quote-filename was removed altogether. If it turns
285 ;; out to be necessary there, something will need to be done.
286 ;;-(defcustom tramp-file-name-quote-list
287 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
288 ;;- "*Protect these characters from the remote shell.
289 ;;-Any character in this list is quoted (preceded with a backslash)
290 ;;-because it means something special to the shell. This takes effect
291 ;;-when sending file and directory names to the remote shell.
293 ;;-See `comint-file-name-quote-list' for details."
295 ;;- :type '(repeat character))
297 (defcustom tramp-methods
298 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
299 (tramp-login-program "rsh")
300 (tramp-copy-program "rcp")
301 (tramp-remote-sh "/bin/sh")
302 (tramp-login-args nil)
303 (tramp-copy-args nil)
304 (tramp-copy-keep-date-arg "-p")
305 (tramp-password-end-of-line nil))
306 ("scp" (tramp-connection-function tramp-open-connection-rsh)
307 (tramp-login-program "ssh")
308 (tramp-copy-program "scp")
309 (tramp-remote-sh "/bin/sh")
310 (tramp-login-args ("-e" "none"))
311 (tramp-copy-args nil)
312 (tramp-copy-keep-date-arg "-p")
313 (tramp-password-end-of-line nil))
314 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
315 (tramp-login-program "ssh")
316 (tramp-copy-program "scp")
317 (tramp-remote-sh "/bin/sh")
318 (tramp-login-args ("-1" "-e" "none"))
319 (tramp-copy-args ("-1"))
320 (tramp-copy-keep-date-arg "-p")
321 (tramp-password-end-of-line nil))
322 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
323 (tramp-login-program "ssh")
324 (tramp-copy-program "scp")
325 (tramp-remote-sh "/bin/sh")
326 (tramp-login-args ("-2" "-e" "none"))
327 (tramp-copy-args ("-2"))
328 (tramp-copy-keep-date-arg "-p")
329 (tramp-password-end-of-line nil))
331 (tramp-connection-function tramp-open-connection-rsh)
332 (tramp-login-program "ssh1")
333 (tramp-copy-program "scp1")
334 (tramp-remote-sh "/bin/sh")
335 (tramp-login-args ("-e" "none"))
336 (tramp-copy-args nil)
337 (tramp-copy-keep-date-arg "-p")
338 (tramp-password-end-of-line nil))
340 (tramp-connection-function tramp-open-connection-rsh)
341 (tramp-login-program "ssh2")
342 (tramp-copy-program "scp2")
343 (tramp-remote-sh "/bin/sh")
344 (tramp-login-args ("-e" "none"))
345 (tramp-copy-args nil)
346 (tramp-copy-keep-date-arg "-p")
347 (tramp-password-end-of-line nil))
348 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
349 (tramp-login-program "ssh")
350 (tramp-copy-program "rsync")
351 (tramp-remote-sh "/bin/sh")
352 (tramp-login-args ("-e" "none"))
353 (tramp-copy-args ("-e" "ssh"))
354 (tramp-copy-keep-date-arg "-t")
355 (tramp-password-end-of-line nil))
356 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
357 (tramp-login-program "remsh")
358 (tramp-copy-program "rcp")
359 (tramp-remote-sh "/bin/sh")
360 (tramp-login-args nil)
361 (tramp-copy-args nil)
362 (tramp-copy-keep-date-arg "-p")
363 (tramp-password-end-of-line nil))
364 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
365 (tramp-login-program "rsh")
366 (tramp-copy-program nil)
367 (tramp-remote-sh "/bin/sh")
368 (tramp-login-args nil)
369 (tramp-copy-args nil)
370 (tramp-copy-keep-date-arg nil)
371 (tramp-password-end-of-line nil))
372 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
373 (tramp-login-program "ssh")
374 (tramp-copy-program nil)
375 (tramp-remote-sh "/bin/sh")
376 (tramp-login-args ("-e" "none"))
377 (tramp-copy-args nil)
378 (tramp-copy-keep-date-arg nil)
379 (tramp-password-end-of-line nil))
380 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
381 (tramp-login-program "ssh")
382 (tramp-copy-program nil)
383 (tramp-remote-sh "/bin/sh")
384 (tramp-login-args ("-1" "-e" "none"))
385 (tramp-copy-args ("-1"))
386 (tramp-copy-keep-date-arg nil)
387 (tramp-password-end-of-line nil))
388 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
389 (tramp-login-program "ssh")
390 (tramp-copy-program nil)
391 (tramp-remote-sh "/bin/sh")
392 (tramp-login-args ("-2" "-e" "none"))
393 (tramp-copy-args ("-2"))
394 (tramp-copy-keep-date-arg nil)
395 (tramp-password-end-of-line nil))
397 (tramp-connection-function tramp-open-connection-rsh)
398 (tramp-login-program "ssh1")
399 (tramp-copy-program nil)
400 (tramp-remote-sh "/bin/sh")
401 (tramp-login-args ("-e" "none"))
402 (tramp-copy-args nil)
403 (tramp-copy-keep-date-arg nil)
404 (tramp-password-end-of-line nil))
406 (tramp-connection-function tramp-open-connection-rsh)
407 (tramp-login-program "ssh2")
408 (tramp-copy-program nil)
409 (tramp-remote-sh "/bin/sh")
410 (tramp-login-args ("-e" "none"))
411 (tramp-copy-args nil)
412 (tramp-copy-keep-date-arg nil)
413 (tramp-password-end-of-line nil))
414 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
415 (tramp-login-program "remsh")
416 (tramp-copy-program nil)
417 (tramp-remote-sh "/bin/sh")
418 (tramp-login-args nil)
419 (tramp-copy-args nil)
420 (tramp-copy-keep-date-arg nil)
421 (tramp-password-end-of-line nil))
423 (tramp-connection-function tramp-open-connection-telnet)
424 (tramp-login-program "telnet")
425 (tramp-copy-program nil)
426 (tramp-remote-sh "/bin/sh")
427 (tramp-login-args nil)
428 (tramp-copy-args nil)
429 (tramp-copy-keep-date-arg nil)
430 (tramp-password-end-of-line nil))
431 ("su" (tramp-connection-function tramp-open-connection-su)
432 (tramp-login-program "su")
433 (tramp-copy-program nil)
434 (tramp-remote-sh "/bin/sh")
435 (tramp-login-args ("-" "%u"))
436 (tramp-copy-args nil)
437 (tramp-copy-keep-date-arg nil)
438 (tramp-password-end-of-line nil))
439 ("sudo" (tramp-connection-function tramp-open-connection-su)
440 (tramp-login-program "sudo")
441 (tramp-copy-program nil)
442 (tramp-remote-sh "/bin/sh")
443 (tramp-login-args ("-u" "%u" "-s"
445 (tramp-copy-args nil)
446 (tramp-copy-keep-date-arg nil)
447 (tramp-password-end-of-line nil))
448 ("multi" (tramp-connection-function tramp-open-connection-multi)
449 (tramp-login-program nil)
450 (tramp-copy-program nil)
451 (tramp-remote-sh "/bin/sh")
452 (tramp-login-args nil)
453 (tramp-copy-args nil)
454 (tramp-copy-keep-date-arg nil)
455 (tramp-password-end-of-line nil))
456 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
457 (tramp-login-program "ssh")
458 (tramp-copy-program "scp")
459 (tramp-remote-sh "/bin/sh")
460 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
461 (tramp-copy-args nil)
462 (tramp-copy-keep-date-arg "-p")
463 (tramp-password-end-of-line nil))
464 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
465 (tramp-login-program "ssh")
466 (tramp-copy-program nil)
467 (tramp-remote-sh "/bin/sh")
468 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
469 (tramp-copy-args nil)
470 (tramp-copy-keep-date-arg nil)
471 (tramp-password-end-of-line nil))
473 (tramp-connection-function tramp-open-connection-rsh)
474 (tramp-login-program "krlogin")
475 (tramp-copy-program nil)
476 (tramp-remote-sh "/bin/sh")
477 (tramp-login-args ("-x"))
478 (tramp-copy-args nil)
479 (tramp-copy-keep-date-arg nil)
480 (tramp-password-end-of-line nil))
482 (tramp-connection-function tramp-open-connection-rsh)
483 (tramp-login-program "plink")
484 (tramp-copy-program nil)
485 (tramp-remote-sh "/bin/sh")
486 (tramp-login-args ("-ssh")) ;optionally add "-v"
487 (tramp-copy-args nil)
488 (tramp-copy-keep-date-arg nil)
489 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
491 (tramp-connection-function tramp-open-connection-rsh)
492 (tramp-login-program "plink")
493 (tramp-copy-program nil)
494 (tramp-remote-sh "/bin/sh")
495 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
496 (tramp-copy-args nil)
497 (tramp-copy-keep-date-arg nil)
498 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
500 (tramp-connection-function tramp-open-connection-rsh)
501 (tramp-login-program "plink")
502 (tramp-copy-program "pscp")
503 (tramp-remote-sh "/bin/sh")
504 (tramp-login-args ("-ssh"))
505 (tramp-copy-args nil)
506 (tramp-copy-keep-date-arg "-p")
507 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
509 (tramp-connection-function tramp-open-connection-rsh)
510 (tramp-login-program "fsh")
511 (tramp-copy-program "fcp")
512 (tramp-remote-sh "/bin/sh -i")
513 (tramp-login-args ("sh" "-i"))
514 (tramp-copy-args nil)
515 (tramp-copy-keep-date-arg "-p")
516 (tramp-password-end-of-line nil))
518 "*Alist of methods for remote files.
519 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
520 Each NAME stands for a remote access method. Each PARAM is a
521 pair of the form (KEY VALUE). The following KEYs are defined:
522 * `tramp-connection-function'
523 This specifies the function to use to connect to the remote host.
524 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
525 and `tramp-open-connection-su' are defined. See the documentation
526 of these functions for more details.
528 This specifies the Bourne shell to use on the remote host. This
529 MUST be a Bourne-like shell. It is normally not necessary to set
530 this to any value other than \"/bin/sh\": tramp wants to use a shell
531 which groks tilde expansion, but it can search for it. Also note
532 that \"/bin/sh\" exists on all Unixen, this might not be true for
533 the value that you decide to use. You Have Been Warned.
534 * `tramp-login-program'
535 This specifies the name of the program to use for logging in to the
536 remote host. Depending on `tramp-connection-function', this may be
537 the name of rsh or a workalike program (when
538 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
539 name of telnet or a workalike (for `tramp-open-connection-telnet'),
540 or the name of su or a workalike (for `tramp-open-connection-su').
542 This specifies the list of arguments to pass to the above
543 mentioned program. Please note that this is a list of arguments,
544 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
545 here. Instead, you want two list elements, one for \"-a\" and one
546 for \"-b\", or one for \"-f\" and one for \"foo\".
547 If `tramp-connection-function' is `tramp-open-connection-su', then
548 \"%u\" in this list is replaced by the user name, and \"%%\" can
549 be used to obtain a literal percent character.
550 * `tramp-copy-program'
551 This specifies the name of the program to use for remotely copying
552 the file; this might be the absolute filename of rcp or the name of
555 This specifies the list of parameters to pass to the above mentioned
556 program, the hints for `tramp-login-args' also apply here.
557 * `tramp-copy-keep-date-arg'
558 This specifies the parameter to use for the copying program when the
559 timestamp of the original file should be kept. For `rcp', use `-p', for
561 * `tramp-password-end-of-line'
562 This specifies the string to use for terminating the line after
563 submitting the password. If this method parameter is nil, then the
564 value of the normal variable `tramp-default-password-end-of-line'
565 is used. This parameter is necessary because the \"plink\" program
566 requires any two characters after sending the password. These do
567 not have to be newline or carriage return characters. Other login
568 programs are happy with just one character, the newline character.
569 We use \"xy\" as the value for methods using \"plink\".
571 What does all this mean? Well, you should specify `tramp-login-program'
572 for all methods; this program is used to log in to the remote site. Then,
573 there are two ways to actually transfer the files between the local and the
574 remote side. One way is using an additional rcp-like program. If you want
575 to do this, set `tramp-copy-program' in the method.
577 Another possibility for file transfer is inline transfer, i.e. the
578 file is passed through the same buffer used by `tramp-login-program'. In
579 this case, the file contents need to be protected since the
580 `tramp-login-program' might use escape codes or the connection might not
581 be eight-bit clean. Therefore, file contents are encoded for transit.
582 See the variable `tramp-coding-commands' for details.
584 So, to summarize: if the method is an out-of-band method, then you
585 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
586 inline method, then these two parameters should be nil. Every method,
587 inline or out of band, must specify `tramp-connection-function' plus
588 the associated arguments (for example, the login program if you chose
589 `tramp-open-connection-telnet').
593 When using `tramp-open-connection-su' the phrase `open connection to a
594 remote host' sounds strange, but it is used nevertheless, for
595 consistency. No connection is opened to a remote host, but `su' is
596 started on the local host. You are not allowed to specify a remote
597 host other than `localhost' or the name of the local host."
601 (set (list (const tramp-connection-function) function)
602 (list (const tramp-login-program)
603 (choice (const nil) string))
604 (list (const tramp-copy-program)
605 (choice (const nil) string))
606 (list (const tramp-remote-sh)
607 (choice (const nil) string))
608 (list (const tramp-login-args) (repeat string))
609 (list (const tramp-copy-args) (repeat string))
610 (list (const tramp-copy-keep-date-arg)
611 (choice (const nil) string))
612 (list (const tramp-encoding-command)
613 (choice (const nil) string))
614 (list (const tramp-decoding-command)
615 (choice (const nil) string))
616 (list (const tramp-encoding-function)
617 (choice (const nil) function))
618 (list (const tramp-decoding-function)
619 (choice (const nil) function))
620 (list (const tramp-password-end-of-line)
621 (choice (const nil) string))))))
623 (defcustom tramp-multi-methods '("multi" "multiu")
624 "*List of multi-hop methods.
625 Each entry in this list should be a method name as mentioned in the
626 variable `tramp-methods'."
628 :type '(repeat string))
630 (defcustom tramp-multi-connection-function-alist
631 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
632 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
633 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
634 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
635 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
636 ("su" tramp-multi-connect-su "su - %u%n")
637 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
638 "*List of connection functions for multi-hop methods.
639 Each list item is a list of three items (METHOD FUNCTION COMMAND),
640 where METHOD is the name as used in the file name, FUNCTION is the
641 function to be executed, and COMMAND is the shell command used for
644 COMMAND may contain percent escapes. `%u' will be replaced with the
645 user name, `%h' will be replaced with the host name, and `%n' will be
646 replaced with an end-of-line character, as specified in the variable
647 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
648 Note that the interpretation of the percent escapes also depends on
649 the FUNCTION. For example, the `%u' escape is forbidden with the
650 function `tramp-multi-connect-telnet'. See the documentation of the
651 various functions for details."
653 :type '(repeat (list string function string)))
655 (defcustom tramp-default-method
656 (if (and (fboundp 'executable-find)
657 (executable-find "plink"))
660 "*Default method to use for transferring files.
661 See `tramp-methods' for possibilities.
662 Also see `tramp-default-method-alist'."
666 (defcustom tramp-default-method-alist
667 '(("\\`localhost\\'" "\\`root\\'" "su"))
668 "*Default method to use for specific user/host pairs.
669 This is an alist of items (HOST USER METHOD). The first matching item
670 specifies the method to use for a file name which does not specify a
671 method. HOST and USER are regular expressions or nil, which is
672 interpreted as a regular expression which always matches. If no entry
673 matches, the variable `tramp-default-method' takes effect.
675 If the file name does not specify the user, lookup is done using the
676 empty string for the user name.
678 See `tramp-methods' for a list of possibilities for METHOD."
680 :type '(repeat (list (regexp :tag "Host regexp")
681 (regexp :tag "User regexp")
682 (string :tag "Method"))))
684 ;; Default values for non-Unices seeked
685 (defconst tramp-completion-function-alist-rsh
686 (unless (memq system-type '(windows-nt))
687 '((tramp-parse-rhosts "/etc/hosts.equiv")
688 (tramp-parse-rhosts "~/.rhosts")))
689 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
691 ;; Default values for non-Unices seeked
692 (defconst tramp-completion-function-alist-ssh
693 (unless (memq system-type '(windows-nt))
694 '((tramp-parse-rhosts "/etc/hosts.equiv")
695 (tramp-parse-rhosts "/etc/shosts.equiv")
696 (tramp-parse-shosts "/etc/ssh_known_hosts")
697 (tramp-parse-sconfig "/etc/ssh_config")
698 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
699 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
700 (tramp-parse-rhosts "~/.rhosts")
701 (tramp-parse-rhosts "~/.shosts")
702 (tramp-parse-shosts "~/.ssh/known_hosts")
703 (tramp-parse-sconfig "~/.ssh/config")
704 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
705 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
706 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
708 ;; Default values for non-Unices seeked
709 (defconst tramp-completion-function-alist-telnet
710 (unless (memq system-type '(windows-nt))
711 '((tramp-parse-hosts "/etc/hosts")))
712 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
714 ;; Default values for non-Unices seeked
715 (defconst tramp-completion-function-alist-su
716 (unless (memq system-type '(windows-nt))
717 '((tramp-parse-passwd "/etc/passwd")))
718 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
720 (defvar tramp-completion-function-alist nil
721 "*Alist of methods for remote files.
722 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
723 Each NAME stands for a remote access method. Each PAIR is of the form
724 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
725 names from FILE for completion. The following predefined FUNCTIONs exists:
727 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
728 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
729 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
730 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
731 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
732 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
733 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
734 * `tramp-parse-netrc' for \"~/.netrc\" like files.
736 FUNCTION can also be a customer defined function. For more details see
739 (eval-after-load "tramp"
741 (tramp-set-completion-function
742 "rcp" tramp-completion-function-alist-rsh)
743 (tramp-set-completion-function
744 "scp" tramp-completion-function-alist-ssh)
745 (tramp-set-completion-function
746 "scp1" tramp-completion-function-alist-ssh)
747 (tramp-set-completion-function
748 "scp2" tramp-completion-function-alist-ssh)
749 (tramp-set-completion-function
750 "scp1_old" tramp-completion-function-alist-ssh)
751 (tramp-set-completion-function
752 "scp2_old" tramp-completion-function-alist-ssh)
753 (tramp-set-completion-function
754 "rsync" tramp-completion-function-alist-rsh)
755 (tramp-set-completion-function
756 "remcp" tramp-completion-function-alist-rsh)
757 (tramp-set-completion-function
758 "rsh" tramp-completion-function-alist-rsh)
759 (tramp-set-completion-function
760 "ssh" tramp-completion-function-alist-ssh)
761 (tramp-set-completion-function
762 "ssh1" tramp-completion-function-alist-ssh)
763 (tramp-set-completion-function
764 "ssh2" tramp-completion-function-alist-ssh)
765 (tramp-set-completion-function
766 "ssh1_old" tramp-completion-function-alist-ssh)
767 (tramp-set-completion-function
768 "ssh2_old" tramp-completion-function-alist-ssh)
769 (tramp-set-completion-function
770 "remsh" tramp-completion-function-alist-rsh)
771 (tramp-set-completion-function
772 "telnet" tramp-completion-function-alist-telnet)
773 (tramp-set-completion-function
774 "su" tramp-completion-function-alist-su)
775 (tramp-set-completion-function
776 "sudo" tramp-completion-function-alist-su)
777 (tramp-set-completion-function
779 (tramp-set-completion-function
780 "scpx" tramp-completion-function-alist-ssh)
781 (tramp-set-completion-function
782 "sshx" tramp-completion-function-alist-ssh)
783 (tramp-set-completion-function
784 "krlogin" tramp-completion-function-alist-rsh)
785 (tramp-set-completion-function
786 "plink" tramp-completion-function-alist-ssh)
787 (tramp-set-completion-function
788 "plink1" tramp-completion-function-alist-ssh)
789 (tramp-set-completion-function
790 "pscp" tramp-completion-function-alist-ssh)
791 (tramp-set-completion-function
792 "fcp" tramp-completion-function-alist-ssh)))
794 (defcustom tramp-rsh-end-of-line "\n"
795 "*String used for end of line in rsh connections.
796 I don't think this ever needs to be changed, so please tell me about it
797 if you need to change this.
798 Also see the method parameter `tramp-password-end-of-line' and the normal
799 variable `tramp-default-password-end-of-line'."
803 (defcustom tramp-default-password-end-of-line
804 tramp-rsh-end-of-line
805 "*String used for end of line after sending a password.
806 This variable provides the default value for the method parameter
807 `tramp-password-end-of-line', see `tramp-methods' for more details.
809 It seems that people using plink under Windows need to send
810 \"\\r\\n\" (carriage-return, then newline) after a password, but just
811 \"\\n\" after all other lines. This variable can be used for the
812 password, see `tramp-rsh-end-of-line' for the other cases.
814 The default value is to use the same value as `tramp-rsh-end-of-line'."
818 (defcustom tramp-remote-path
819 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
820 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
821 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
822 "*List of directories to search for executables on remote host.
823 Please notify me about other semi-standard directories to include here.
825 You can use `~' in this list, but when searching for a shell which groks
826 tilde expansion, all directory names starting with `~' will be ignored."
828 :type '(repeat string))
830 (defcustom tramp-login-prompt-regexp
832 "*Regexp matching login-like prompts.
833 The regexp should match at end of buffer."
837 (defcustom tramp-shell-prompt-pattern
838 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
839 "Regexp to match prompts from remote shell.
840 Normally, Tramp expects you to configure `shell-prompt-pattern'
841 correctly, but sometimes it happens that you are connecting to a
842 remote host which sends a different kind of shell prompt. Therefore,
843 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
844 and also things matched by this variable. The default value of this
845 variable is similar to the default value of `shell-prompt-pattern',
846 which should work well in many cases."
850 (defcustom tramp-password-prompt-regexp
851 "^.*\\([pP]assword\\|passphrase.*\\):\^@? *"
852 "*Regexp matching password-like prompts.
853 The regexp should match at end of buffer.
855 The `sudo' program appears to insert a `^@' character into the prompt."
859 (defcustom tramp-wrong-passwd-regexp
861 ;; These strings should be on the last line
862 (regexp-opt '("Permission denied."
868 "Name or service not known"
869 "Host key verification failed.") t)
873 ;; Here comes a list of regexes, separated by \\|
874 "Received signal [0-9]+"
876 "*Regexp matching a `login failed' message.
877 The regexp should match at end of buffer."
881 (defcustom tramp-yesno-prompt-regexp
883 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
885 "Regular expression matching all yes/no queries which need to be confirmed.
886 The confirmation should be done with yes or no.
887 The regexp should match at end of buffer.
888 See also `tramp-yn-prompt-regexp'."
892 (defcustom tramp-yn-prompt-regexp
893 (concat (regexp-opt '("Store key in cache? (y/n)") t)
895 "Regular expression matching all y/n queries which need to be confirmed.
896 The confirmation should be done with y or n.
897 The regexp should match at end of buffer.
898 See also `tramp-yesno-prompt-regexp'."
902 (defcustom tramp-terminal-prompt-regexp
906 "Terminal type\\? \\[.*\\]"
908 "Regular expression matching all terminal setting prompts.
909 The regexp should match at end of buffer.
910 The answer will be provided by `tramp-action-terminal', which see."
914 (defcustom tramp-process-alive-regexp
916 "Regular expression indicating a process has finished.
917 In fact this expression is empty by intention, it will be used only to
918 check regularly the status of the associated process.
919 The answer will be provided by `tramp-action-process-alive',
920 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
924 (defcustom tramp-temp-name-prefix "tramp."
925 "*Prefix to use for temporary files.
926 If this is a relative file name (such as \"tramp.\"), it is considered
927 relative to the directory name returned by the function
928 `tramp-temporary-file-directory' (which see). It may also be an
929 absolute file name; don't forget to include a prefix for the filename
934 (defcustom tramp-discard-garbage nil
935 "*If non-nil, try to discard garbage sent by remote shell.
936 Some shells send such garbage upon connection setup."
940 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
941 "*Alist specifying extra arguments to pass to the remote shell.
942 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
943 matching the shell file name and ARGS is a string specifying the
946 This variable is only used when Tramp needs to start up another shell
947 for tilde expansion. The extra arguments should typically prevent the
948 shell from reading its init file."
950 ;; This might be the wrong way to test whether the widget type
951 ;; `alist' is available. Who knows the right way to test it?
952 :type (if (get 'alist 'widget-type)
953 '(alist :key-type string :value-type string)
954 '(repeat (cons string string))))
956 (defcustom tramp-prefix-format
957 (if tramp-unified-filenames "/" "/[")
958 "*String matching the very beginning of tramp file names.
959 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
963 (defcustom tramp-prefix-regexp
964 (concat "^" (regexp-quote tramp-prefix-format))
965 "*Regexp matching the very beginning of tramp file names.
966 Should always start with \"^\". Derived from `tramp-prefix-format'."
970 (defcustom tramp-method-regexp
972 "*Regexp matching methods identifiers."
976 ;; It is a little bit annoying that in XEmacs case this delimeter is different
977 ;; for single-hop and multi-hop cases.
978 (defcustom tramp-postfix-single-method-format
979 (if tramp-unified-filenames ":" "/")
980 "*String matching delimeter between method and user or host names.
981 Applicable for single-hop methods.
982 Used in `tramp-make-tramp-file-name'."
986 (defcustom tramp-postfix-single-method-regexp
987 (regexp-quote tramp-postfix-single-method-format)
988 "*Regexp matching delimeter between method and user or host names.
989 Applicable for single-hop methods.
990 Derived from `tramp-postfix-single-method-format'."
994 (defcustom tramp-postfix-multi-method-format
996 "*String matching delimeter between method and user or host names.
997 Applicable for multi-hop methods.
998 Used in `tramp-make-tramp-multi-file-name'."
1002 (defcustom tramp-postfix-multi-method-regexp
1003 (regexp-quote tramp-postfix-multi-method-format)
1004 "*Regexp matching delimeter between method and user or host names.
1005 Applicable for multi-hop methods.
1006 Derived from `tramp-postfix-multi-method-format'."
1010 (defcustom tramp-postfix-multi-hop-format
1011 (if tramp-unified-filenames ":" "/")
1012 "*String matching delimeter between host and next method.
1013 Applicable for multi-hop methods.
1014 Used in `tramp-make-tramp-multi-file-name'."
1018 (defcustom tramp-postfix-multi-hop-regexp
1019 (regexp-quote tramp-postfix-multi-hop-format)
1020 "*Regexp matching delimeter between host and next method.
1021 Applicable for multi-hop methods.
1022 Derived from `tramp-postfix-multi-hop-format'."
1026 (defcustom tramp-user-regexp
1028 "*Regexp matching user names."
1032 (defcustom tramp-postfix-user-format
1034 "*String matching delimeter between user and host names.
1035 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1039 (defcustom tramp-postfix-user-regexp
1040 (regexp-quote tramp-postfix-user-format)
1041 "*Regexp matching delimeter between user and host names.
1042 Derived from `tramp-postfix-user-format'."
1046 (defcustom tramp-host-regexp
1048 "*Regexp matching host names."
1052 (defcustom tramp-host-with-port-regexp
1054 "*Regexp matching host names."
1058 (defcustom tramp-postfix-host-format
1059 (if tramp-unified-filenames ":" "]")
1060 "*String matching delimeter between host names and localnames.
1061 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1065 (defcustom tramp-postfix-host-regexp
1066 (regexp-quote tramp-postfix-host-format)
1067 "*Regexp matching delimeter between host names and localnames.
1068 Derived from `tramp-postfix-host-format'."
1072 (defcustom tramp-localname-regexp
1074 "*Regexp matching localnames."
1078 ;; File name format.
1080 (defcustom tramp-file-name-structure
1084 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1085 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1086 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1087 "\\(" tramp-localname-regexp "\\)")
1090 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1091 the tramp file name structure.
1093 The first element REGEXP is a regular expression matching a tramp file
1094 name. The regex should contain parentheses around the method name,
1095 the user name, the host name, and the file name parts.
1097 The second element METHOD is a number, saying which pair of
1098 parentheses matches the method name. The third element USER is
1099 similar, but for the user name. The fourth element HOST is similar,
1100 but for the host name. The fifth element FILE is for the file name.
1101 These numbers are passed directly to `match-string', which see. That
1102 means the opening parentheses are counted to identify the pair.
1104 See also `tramp-file-name-regexp'."
1106 :type '(list (regexp :tag "File name regexp")
1107 (integer :tag "Paren pair for method name")
1108 (integer :tag "Paren pair for user name ")
1109 (integer :tag "Paren pair for host name ")
1110 (integer :tag "Paren pair for file name ")))
1113 (defconst tramp-file-name-regexp-unified
1115 "Value for `tramp-file-name-regexp' for unified remoting.
1116 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1117 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1120 (defconst tramp-file-name-regexp-separate
1122 "Value for `tramp-file-name-regexp' for separate remoting.
1123 XEmacs uses a separate filename syntax for Tramp and EFS.
1124 See `tramp-file-name-structure-separate' for more explanations.")
1127 (defcustom tramp-file-name-regexp
1128 (if tramp-unified-filenames
1129 tramp-file-name-regexp-unified
1130 tramp-file-name-regexp-separate)
1131 "*Regular expression matching file names handled by tramp.
1132 This regexp should match tramp file names but no other file names.
1133 \(When tramp.el is loaded, this regular expression is prepended to
1134 `file-name-handler-alist', and that is searched sequentially. Thus,
1135 if the tramp entry appears rather early in the `file-name-handler-alist'
1136 and is a bit too general, then some files might be considered tramp
1137 files which are not really tramp files.
1139 Please note that the entry in `file-name-handler-alist' is made when
1140 this file (tramp.el) is loaded. This means that this variable must be set
1141 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1142 updated after changing this variable.
1144 Also see `tramp-file-name-structure'."
1149 (defconst tramp-completion-file-name-regexp-unified
1150 "^/$\\|^/[^/:][^/]*$"
1151 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1152 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1153 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1156 (defconst tramp-completion-file-name-regexp-separate
1157 "^/\\([[][^]]*\\)?$"
1158 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1159 XEmacs uses a separate filename syntax for Tramp and EFS.
1160 See `tramp-file-name-structure-separate' for more explanations.")
1163 (defcustom tramp-completion-file-name-regexp
1164 (if tramp-unified-filenames
1165 tramp-completion-file-name-regexp-unified
1166 tramp-completion-file-name-regexp-separate)
1167 "*Regular expression matching file names handled by tramp completion.
1168 This regexp should match partial tramp file names only.
1170 Please note that the entry in `file-name-handler-alist' is made when
1171 this file (tramp.el) is loaded. This means that this variable must be set
1172 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1173 updated after changing this variable.
1175 Also see `tramp-file-name-structure'."
1179 (defcustom tramp-multi-file-name-structure
1183 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1184 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1185 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1187 "*Describes the file name structure of `multi' files.
1188 Multi files allow you to contact a remote host in several hops.
1189 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1191 The first element, REGEXP, gives a regular expression to match against
1192 the file name. In this regular expression, `%s' is replaced with the
1193 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1194 allow multiple hops, you normally want to use something like
1195 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1196 of parentheses is used for the HOP element, see below.)
1198 All remaining elements are numbers. METHOD gives the number of the
1199 paren pair which matches the method name. HOP gives the number of the
1200 paren pair which matches the hop sequence. LOCALNAME gives the number of
1201 the paren pair which matches the localname (pathname) on the remote host.
1203 LOCALNAME can also be negative, which means to count from the end. Ie, a
1204 value of -1 means the last paren pair.
1206 I think it would be good if the regexp matches the whole of the
1207 string, but I haven't actually tried what happens if it doesn't..."
1209 :type '(list (regexp :tag "File name regexp")
1210 (integer :tag "Paren pair for method name")
1211 (integer :tag "Paren pair for hops")
1212 (integer :tag "Paren pair to match localname")))
1214 (defcustom tramp-multi-file-name-hop-structure
1217 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1218 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1219 "\\(" tramp-host-with-port-regexp "\\)")
1221 "*Describes the structure of a hop in multi files.
1222 This is a list of four elements (REGEXP METHOD USER HOST). First
1223 element REGEXP is used to match against the hop. Pair number METHOD
1224 matches the method of one hop, pair number USER matches the user of
1225 one hop, pair number HOST matches the host of one hop.
1227 This regular expression should match exactly all of one hop."
1229 :type '(list (regexp :tag "Hop regexp")
1230 (integer :tag "Paren pair for method name")
1231 (integer :tag "Paren pair for user name")
1232 (integer :tag "Paren pair for host name")))
1234 (defcustom tramp-make-multi-tramp-file-format
1236 (concat tramp-prefix-format "%m")
1237 (concat tramp-postfix-multi-hop-format
1238 "%m" tramp-postfix-multi-method-format
1239 "%u" tramp-postfix-user-format
1241 (concat tramp-postfix-host-format "%p"))
1242 "*Describes how to construct a `multi' file name.
1243 This is a list of three elements PREFIX, HOP and LOCALNAME.
1245 The first element PREFIX says how to construct the prefix, the second
1246 element HOP specifies what each hop looks like, and the final element
1247 LOCALNAME says how to construct the localname (pathname).
1249 In PREFIX, `%%' means `%' and `%m' means the method name.
1251 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1252 user and hop host, respectively.
1254 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1256 The resulting file name always contains one copy of PREFIX and one
1257 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1260 Note: the current implementation requires the prefix to contain the
1261 method name, followed by all the hops, and the localname must come
1264 :type '(list string string string))
1266 (defcustom tramp-terminal-type "dumb"
1267 "*Value of TERM environment variable for logging in to remote host.
1268 Because Tramp wants to parse the output of the remote shell, it is easily
1269 confused by ANSI color escape sequences and suchlike. Often, shell init
1270 files conditionalize this setup based on the TERM environment variable."
1274 (defcustom tramp-completion-without-shell-p nil
1275 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1276 Using shell wildcards for completions has the advantage that it can be
1277 fast even in large directories, but completion is always
1278 case-sensitive. Relying on Lisp only means that case-insensitive
1279 completion is possible (subject to the variable `completion-ignore-case'),
1280 but it might be slow on large directories."
1284 (defcustom tramp-actions-before-shell
1285 '((tramp-password-prompt-regexp tramp-action-password)
1286 (tramp-login-prompt-regexp tramp-action-login)
1287 (shell-prompt-pattern tramp-action-succeed)
1288 (tramp-shell-prompt-pattern tramp-action-succeed)
1289 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1290 (tramp-yesno-prompt-regexp tramp-action-yesno)
1291 (tramp-yn-prompt-regexp tramp-action-yn)
1292 (tramp-terminal-prompt-regexp tramp-action-terminal)
1293 (tramp-process-alive-regexp tramp-action-process-alive))
1294 "List of pattern/action pairs.
1295 Whenever a pattern matches, the corresponding action is performed.
1296 Each item looks like (PATTERN ACTION).
1298 The PATTERN should be a symbol, a variable. The value of this
1299 variable gives the regular expression to search for. Note that the
1300 regexp must match at the end of the buffer, \"\\'\" is implicitly
1303 The ACTION should also be a symbol, but a function. When the
1304 corresponding PATTERN matches, the ACTION function is called."
1306 :type '(repeat (list variable function)))
1308 (defcustom tramp-actions-copy-out-of-band
1309 '((tramp-password-prompt-regexp tramp-action-password)
1310 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1311 (tramp-process-alive-regexp tramp-action-out-of-band))
1312 "List of pattern/action pairs.
1313 This list is used for copying/renaming with out-of-band methods.
1314 See `tramp-actions-before-shell' for more info."
1316 :type '(repeat (list variable function)))
1318 (defcustom tramp-multi-actions
1319 '((tramp-password-prompt-regexp tramp-multi-action-password)
1320 (tramp-login-prompt-regexp tramp-multi-action-login)
1321 (shell-prompt-pattern tramp-multi-action-succeed)
1322 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1323 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1324 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1325 "List of pattern/action pairs.
1326 This list is used for each hop in multi-hop connections.
1327 See `tramp-actions-before-shell' for more info."
1329 :type '(repeat (list variable function)))
1331 (defcustom tramp-initial-commands
1334 "unset autocorrect")
1335 "List of commands to send to the first remote shell that we see.
1336 These commands will be sent to any shell, and thus they should be
1337 designed to work in such circumstances. Also, restrict the commands
1338 to the bare necessity for getting the remote shell into a state
1339 where it is possible to execute the Bourne-ish shell.
1341 At the moment, the command to execute the Bourne-ish shell uses strange
1342 quoting which `tcsh' tries to correct, so we send the command \"unset
1343 autocorrect\" to the remote host."
1345 :type '(repeat string))
1347 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1348 ;; known to have a bug in `process-send-string'; some ssh connections appear
1349 ;; to drop bytes when data is sent too quickly.
1350 (defcustom tramp-chunksize
1351 (when (and (not (featurep 'xemacs))
1352 (memq system-type '(hpux)))
1354 "*If non-nil, chunksize for sending input to local process.
1355 It is necessary only on systems which have a buggy `process-send-string'
1356 implementation. The necessity, whether this variable must be set, can be
1357 checked via the following code:
1361 (proc (start-process (buffer-name) (current-buffer) \"wc\" \"-c\")))
1362 (process-send-string proc (make-string bytes ?x))
1363 (process-send-eof proc)
1364 (process-send-eof proc)
1365 (accept-process-output proc 1)
1366 (goto-char (point-min))
1367 (re-search-forward \"\\\\w+\")
1368 (message \"Bytes sent: %s\\tBytes received: %s\" bytes (match-string 0))))
1370 In the Emacs normally running Tramp, evaluate the above code.
1371 You can do this, for example, by pasting it into the `*scratch*'
1372 buffer and then hitting C-j with the cursor after the last
1373 closing parenthesis.
1375 If your Emacs is buggy, the sent and received numbers will be
1376 different. In that case, you'll want to set this variable to
1377 some number. For those people who have needed it, the value 500
1378 seems to have worked well. There is no way to predict what value
1379 you need; maybe you could just experiment a bit.
1381 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1382 this variable to be set as well."
1384 :type '(choice (const nil) integer))
1386 ;; Logging in to a remote host normally requires obtaining a pty. But
1387 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1388 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1389 ;; for an override of the system default.
1390 (defcustom tramp-process-connection-type t
1391 "Overrides `process-connection-type' for connections from Tramp.
1392 Tramp binds process-connection-type to the value given here before
1393 opening a connection to a remote host."
1395 :type '(choice (const nil) (const t) (const pty)))
1397 ;;; Internal Variables:
1399 (defvar tramp-buffer-file-attributes nil
1400 "Holds the `ls -ild' output for the current buffer.
1401 This variable is local to each buffer. It is not used if the remote
1402 machine groks Perl. If it is used, it's used as an emulation for
1403 the visited file modtime.")
1404 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1406 (defvar tramp-md5-function
1407 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1408 ((fboundp 'md5-encode)
1409 (lambda (x) (base64-encode-string
1410 (funcall (symbol-function 'md5-encode) x))))
1411 (t (error "Coulnd't find an `md5' function")))
1412 "Function to call for running the MD5 algorithm.")
1414 (defvar tramp-end-of-output
1416 (funcall tramp-md5-function
1418 (prin1-to-string process-environment)
1419 (current-time-string)
1421 ;; (if (fboundp 'directory-files-and-attributes)
1422 ;; (funcall 'directory-files-and-attributes
1423 ;; (or (getenv "HOME")
1424 ;; (tramp-temporary-file-directory)))
1427 ;; (cons x (file-attributes x)))
1428 ;; (directory-files (or (getenv "HOME")
1429 ;; (tramp-temporary-file-directory))
1432 "String used to recognize end of output.")
1434 (defvar tramp-connection-function nil
1435 "This internal variable holds a parameter for `tramp-methods'.
1436 In the connection buffer, this variable has the value of the like-named
1437 method parameter, as specified in `tramp-methods' (which see).")
1439 (defvar tramp-remote-sh nil
1440 "This internal variable holds a parameter for `tramp-methods'.
1441 In the connection buffer, this variable has the value of the like-named
1442 method parameter, as specified in `tramp-methods' (which see).")
1444 (defvar tramp-login-program nil
1445 "This internal variable holds a parameter for `tramp-methods'.
1446 In the connection buffer, this variable has the value of the like-named
1447 method parameter, as specified in `tramp-methods' (which see).")
1449 (defvar tramp-login-args nil
1450 "This internal variable holds a parameter for `tramp-methods'.
1451 In the connection buffer, this variable has the value of the like-named
1452 method parameter, as specified in `tramp-methods' (which see).")
1454 (defvar tramp-copy-program nil
1455 "This internal variable holds a parameter for `tramp-methods'.
1456 In the connection buffer, this variable has the value of the like-named
1457 method parameter, as specified in `tramp-methods' (which see).")
1459 (defvar tramp-copy-args nil
1460 "This internal variable holds a parameter for `tramp-methods'.
1461 In the connection buffer, this variable has the value of the like-named
1462 method parameter, as specified in `tramp-methods' (which see).")
1464 (defvar tramp-copy-keep-date-arg nil
1465 "This internal variable holds a parameter for `tramp-methods'.
1466 In the connection buffer, this variable has the value of the like-named
1467 method parameter, as specified in `tramp-methods' (which see).")
1469 (defvar tramp-encoding-command nil
1470 "This internal variable holds a parameter for `tramp-methods'.
1471 In the connection buffer, this variable has the value of the like-named
1472 method parameter, as specified in `tramp-methods' (which see).")
1474 (defvar tramp-decoding-command nil
1475 "This internal variable holds a parameter for `tramp-methods'.
1476 In the connection buffer, this variable has the value of the like-named
1477 method parameter, as specified in `tramp-methods' (which see).")
1479 (defvar tramp-encoding-function nil
1480 "This internal variable holds a parameter for `tramp-methods'.
1481 In the connection buffer, this variable has the value of the like-named
1482 method parameter, as specified in `tramp-methods' (which see).")
1484 (defvar tramp-decoding-function nil
1485 "This internal variable holds a parameter for `tramp-methods'.
1486 In the connection buffer, this variable has the value of the like-named
1487 method parameter, as specified in `tramp-methods' (which see).")
1489 (defvar tramp-password-end-of-line nil
1490 "This internal variable holds a parameter for `tramp-methods'.
1491 In the connection buffer, this variable has the value of the like-named
1492 method parameter, as specified in `tramp-methods' (which see).")
1494 ;; CCC `local in each buffer'?
1495 (defvar tramp-ls-command nil
1496 "This command is used to get a long listing with numeric user and group ids.
1497 This variable is automatically made buffer-local to each rsh process buffer
1498 upon opening the connection.")
1500 (defvar tramp-current-multi-method nil
1501 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1502 This variable is automatically made buffer-local to each rsh process buffer
1503 upon opening the connection.")
1505 (defvar tramp-current-method nil
1506 "Connection method for this *tramp* buffer.
1507 This variable is automatically made buffer-local to each rsh process buffer
1508 upon opening the connection.")
1510 (defvar tramp-current-user nil
1511 "Remote login name for this *tramp* buffer.
1512 This variable is automatically made buffer-local to each rsh process buffer
1513 upon opening the connection.")
1515 (defvar tramp-current-host nil
1516 "Remote host for this *tramp* buffer.
1517 This variable is automatically made buffer-local to each rsh process buffer
1518 upon opening the connection.")
1520 (defvar tramp-test-groks-nt nil
1521 "Whether the `test' command groks the `-nt' switch.
1522 \(`test A -nt B' tests if file A is newer than file B.)
1523 This variable is automatically made buffer-local to each rsh process buffer
1524 upon opening the connection.")
1526 (defvar tramp-file-exists-command nil
1527 "Command to use for checking if a file exists.
1528 This variable is automatically made buffer-local to each rsh process buffer
1529 upon opening the connection.")
1531 (defconst tramp-uudecode "\
1533 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1537 "Shell function to implement `uudecode' to standard output.
1538 Many systems support `uudecode -o -' for this or `uudecode -p', but
1539 some systems don't, and for them we have this shell function.")
1541 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1542 ;; output. If you are hacking on this, note that you get *no* output
1543 ;; unless this spits out a complete line, including the '\n' at the
1545 ;; The device number is returned as "-1", because there will be a virtual
1546 ;; device number set in `tramp-handle-file-attributes'
1547 (defconst tramp-perl-file-attributes "\
1550 if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; }
1551 elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; }
1552 else { $l = \"nil\" };
1553 $u = ($n eq \"nil\") ? $s[4] : getpwuid($s[4]);
1554 $g = ($n eq \"nil\") ? $s[5] : getgrgid($s[5]);
1555 printf(\"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1556 $l, $s[3], $u, $g, $s[8] >> 16 & 0xffff, $s[8] & 0xffff,
1557 $s[9] >> 16 & 0xffff, $s[9] & 0xffff, $s[10] >> 16 & 0xffff, $s[10] & 0xffff,
1558 $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff);"
1559 "Perl script to produce output suitable for use with `file-attributes'
1560 on the remote file system.")
1562 ;; ;; These two use uu encoding.
1563 ;; (defvar tramp-perl-encode "%s -e'\
1564 ;; print qq(begin 644 xxx\n);
1567 ;; while (read(STDIN, $s, 45)) {
1568 ;; print pack(q(u), $s);
1573 ;; "Perl program to use for encoding a file.
1574 ;; Escape sequence %s is replaced with name of Perl binary.")
1576 ;; (defvar tramp-perl-decode "%s -ne '
1577 ;; print unpack q(u), $_;
1579 ;; "Perl program to use for decoding a file.
1580 ;; Escape sequence %s is replaced with name of Perl binary.")
1582 ;; These two use base64 encoding.
1583 (defvar tramp-perl-encode-with-module
1584 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1585 "Perl program to use for encoding a file.
1586 Escape sequence %s is replaced with name of Perl binary.
1587 This string is passed to `format', so percent characters need to be doubled.
1588 This implementation requires the MIME::Base64 Perl module to be installed
1589 on the remote host.")
1591 (defvar tramp-perl-decode-with-module
1592 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1593 "Perl program to use for decoding a file.
1594 Escape sequence %s is replaced with name of Perl binary.
1595 This string is passed to `format', so percent characters need to be doubled.
1596 This implementation requires the MIME::Base64 Perl module to be installed
1597 on the remote host.")
1599 (defvar tramp-perl-encode
1601 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1602 # Copyright (C) 2002 Free Software Foundation, Inc.
1607 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1608 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1613 # We read in chunks of 54 bytes, to generate output lines
1614 # of 72 chars (plus end of line)
1617 while (my $data = <STDIN>) {
1620 # Only for the last chunk, and only if did not fill the last three-byte packet
1622 my $mod = length($data) %% 3;
1623 $pad = q(=) x (3 - $mod) if $mod;
1626 # Not the fastest method, but it is simple: unpack to binary string, split
1627 # by groups of 6 bits and convert back from binary to byte; then map into
1628 # the translation table
1632 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1637 "Perl program to use for encoding a file.
1638 Escape sequence %s is replaced with name of Perl binary.
1639 This string is passed to `format', so percent characters need to be doubled.")
1641 (defvar tramp-perl-decode
1643 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1644 # Copyright (C) 2002 Free Software Foundation, Inc.
1649 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1650 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1653 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1657 # We are going to accumulate into $pending to accept any line length
1658 # (we do not check they are <= 76 chars as the RFC says)
1661 while (my $data = <STDIN>) {
1664 # If we find one or two =, we have reached the end and
1665 # any following data is to be discarded
1666 my $finished = $data =~ s/(==?).*/$1/;
1669 my $len = length($pending);
1670 my $chunk = substr($pending, 0, $len & ~3);
1672 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1673 # split in 8-bit chunks and convert back to char.
1676 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1681 "Perl program to use for decoding a file.
1682 Escape sequence %s is replaced with name of Perl binary.
1683 This string is passed to `format', so percent characters need to be doubled.")
1685 ; These values conform to `file-attributes' from XEmacs 21.2.
1686 ; GNU Emacs and other tools not checked.
1687 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1689 (2 . "c") ; character device
1690 (3 . "m") ; multiplexed character device (v7)
1691 (4 . "d") ; directory
1692 (5 . "?") ; Named special file (XENIX)
1693 (6 . "b") ; block device
1694 (7 . "?") ; multiplexed block device (v7)
1695 (8 . "-") ; regular file
1696 (9 . "n") ; network special file (HP-UX)
1697 (10 . "l") ; symlink
1698 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1700 (13 . "D") ; door special (Solaris)
1701 (14 . "w")) ; whiteout (BSD)
1702 "A list of file types returned from the `stat' system call.
1703 This is used to map a mode number to a permission string.")
1705 (defvar tramp-dos-coding-system
1706 (if (and (fboundp 'coding-system-p)
1707 (funcall 'coding-system-p '(dos)))
1710 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1712 (defvar tramp-last-cmd nil
1713 "Internal Tramp variable recording the last command sent.
1714 This variable is buffer-local in every buffer.")
1715 (make-variable-buffer-local 'tramp-last-cmd)
1717 (defvar tramp-process-echoes nil
1718 "Whether to process echoes from the remote shell.")
1720 (defvar tramp-last-cmd-time nil
1721 "Internal Tramp variable recording the time when the last cmd was sent.
1722 This variable is buffer-local in every buffer.")
1723 (make-variable-buffer-local 'tramp-last-cmd-time)
1725 ;; This variable does not have the right value in XEmacs. What should
1726 ;; I use instead of find-operation-coding-system in XEmacs?
1727 (defvar tramp-feature-write-region-fix
1728 (when (fboundp 'find-operation-coding-system)
1729 (let ((file-coding-system-alist '(("test" emacs-mule))))
1730 (funcall (symbol-function 'find-operation-coding-system)
1731 'write-region 0 0 "" nil "test")))
1732 "Internal variable to say if `write-region' chooses the right coding.
1733 Older versions of Emacs chose the coding system for `write-region' based
1734 on the FILENAME argument, even if VISIT was a string.")
1736 ;; New handlers should be added here. The following operations can be
1737 ;; handled using the normal primitives: file-name-as-directory,
1738 ;; file-name-directory, file-name-nondirectory,
1739 ;; file-name-sans-versions, get-file-buffer.
1740 (defconst tramp-file-name-handler-alist
1742 (load . tramp-handle-load)
1743 (make-symbolic-link . tramp-handle-make-symbolic-link)
1744 (file-name-directory . tramp-handle-file-name-directory)
1745 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1746 (file-truename . tramp-handle-file-truename)
1747 (file-exists-p . tramp-handle-file-exists-p)
1748 (file-directory-p . tramp-handle-file-directory-p)
1749 (file-executable-p . tramp-handle-file-executable-p)
1750 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1751 (file-readable-p . tramp-handle-file-readable-p)
1752 (file-regular-p . tramp-handle-file-regular-p)
1753 (file-symlink-p . tramp-handle-file-symlink-p)
1754 (file-writable-p . tramp-handle-file-writable-p)
1755 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1756 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1757 (file-attributes . tramp-handle-file-attributes)
1758 (file-modes . tramp-handle-file-modes)
1759 (file-directory-files . tramp-handle-file-directory-files)
1760 (directory-files . tramp-handle-directory-files)
1761 (file-name-all-completions . tramp-handle-file-name-all-completions)
1762 (file-name-completion . tramp-handle-file-name-completion)
1763 (add-name-to-file . tramp-handle-add-name-to-file)
1764 (copy-file . tramp-handle-copy-file)
1765 (rename-file . tramp-handle-rename-file)
1766 (set-file-modes . tramp-handle-set-file-modes)
1767 (make-directory . tramp-handle-make-directory)
1768 (delete-directory . tramp-handle-delete-directory)
1769 (delete-file . tramp-handle-delete-file)
1770 (directory-file-name . tramp-handle-directory-file-name)
1771 (shell-command . tramp-handle-shell-command)
1772 (insert-directory . tramp-handle-insert-directory)
1773 (expand-file-name . tramp-handle-expand-file-name)
1774 (file-local-copy . tramp-handle-file-local-copy)
1775 (file-remote-p . tramp-handle-file-remote-p)
1776 (insert-file-contents . tramp-handle-insert-file-contents)
1777 (write-region . tramp-handle-write-region)
1778 (find-backup-file-name . tramp-handle-find-backup-file-name)
1779 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1780 (dired-compress-file . tramp-handle-dired-compress-file)
1781 (dired-call-process . tramp-handle-dired-call-process)
1782 (dired-recursive-delete-directory
1783 . tramp-handle-dired-recursive-delete-directory)
1784 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1785 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1786 "Alist of handler functions.
1787 Operations not mentioned here will be handled by the normal Emacs functions.")
1789 ;; Handlers for partial tramp file names. For GNU Emacs just
1790 ;; `file-name-all-completions' is needed. The other ones are necessary
1792 (defconst tramp-completion-file-name-handler-alist
1794 (file-name-directory . tramp-completion-handle-file-name-directory)
1795 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1796 (file-exists-p . tramp-completion-handle-file-exists-p)
1797 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1798 (file-name-completion . tramp-completion-handle-file-name-completion)
1799 (expand-file-name . tramp-completion-handle-expand-file-name))
1800 "Alist of completion handler functions.
1801 Used for file names matching `tramp-file-name-regexp'. Operations not
1802 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1803 normal Emacs functions.")
1805 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1806 (defvar tramp-foreign-file-name-handler-alist
1807 ;; (identity . tramp-sh-file-name-handler) should always be the last
1808 ;; entry, since `identity' always matches.
1809 '((identity . tramp-sh-file-name-handler))
1810 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1811 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1814 ;;; Internal functions which must come first.
1816 (defsubst tramp-message (level fmt-string &rest args)
1817 "Emit a message depending on verbosity level.
1818 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1819 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1820 Calls function `message' with FMT-STRING as control string and the remaining
1821 ARGS to actually emit the message (if applicable).
1823 This function expects to be called from the tramp buffer only!"
1824 (when (<= level tramp-verbose)
1825 (apply #'message (concat "tramp: " fmt-string) args)
1826 (when tramp-debug-buffer
1829 (tramp-get-debug-buffer
1830 tramp-current-multi-method tramp-current-method
1831 tramp-current-user tramp-current-host))
1832 (goto-char (point-max))
1833 (tramp-insert-with-face
1835 (concat "# " (apply #'format fmt-string args) "\n"))))))
1837 (defun tramp-message-for-buffer
1838 (multi-method method user host level fmt-string &rest args)
1839 "Like `tramp-message' but temporarily switches to the tramp buffer.
1840 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1841 remaining args passed to `tramp-message'."
1843 (set-buffer (tramp-get-buffer multi-method method user host))
1844 (apply 'tramp-message level fmt-string args)))
1846 (defsubst tramp-line-end-position nil
1847 "Return point at end of line.
1848 Calls `line-end-position' or `point-at-eol' if defined, else
1849 own implementation."
1851 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1852 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1853 (t (save-excursion (end-of-line) (point)))))
1855 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1856 "Parse a Tramp filename and make components available in the body.
1858 First arg FILENAME is evaluated and dissected into its components.
1859 Second arg VAR is a symbol. It is used as a variable name to hold
1860 the filename structure. It is also used as a prefix for the variables
1861 holding the components. For example, if VAR is the symbol `foo', then
1862 `foo' will be bound to the whole structure, `foo-multi-method' will
1863 be bound to the multi-method component, and so on for `foo-method',
1864 `foo-user', `foo-host', `foo-localname'.
1866 Remaining args are Lisp expressions to be evaluated (inside an implicit
1869 If VAR is nil, then we bind `v' to the structure and `multi-method',
1870 `method', `user', `host', `localname' to the components."
1871 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1872 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1873 (tramp-file-name-multi-method ,(or var 'v)))
1874 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1875 (tramp-file-name-method ,(or var 'v)))
1876 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1877 (tramp-file-name-user ,(or var 'v)))
1878 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1879 (tramp-file-name-host ,(or var 'v)))
1880 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1881 (tramp-file-name-localname ,(or var 'v))))
1884 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1885 ;; To be activated for debugging containing this macro
1886 (def-edebug-spec with-parsed-tramp-file-name t)
1888 (defmacro tramp-let-maybe (variable value &rest body)
1889 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
1890 BODY is executed whether or not the variable is obsolete.
1891 The intent is to protect against `obsolete variable' warnings."
1892 `(if (get ',variable 'byte-obsolete-variable)
1894 (let ((,variable ,value))
1896 (put 'tramp-let-maybe 'lisp-indent-function 2)
1898 ;;; Config Manipulation Functions:
1900 (defun tramp-set-completion-function (method function-list)
1901 "Sets the list of completion functions for METHOD.
1902 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1903 The FUNCTION is intended to parse FILE according its syntax.
1904 It might be a predefined FUNCTION, or a user defined FUNCTION.
1905 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
1906 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
1907 and `tramp-parse-netrc'.
1911 (tramp-set-completion-function
1913 '((tramp-parse-sconfig \"/etc/ssh_config\")
1914 (tramp-parse-sconfig \"~/.ssh/config\")))"
1916 (let ((r function-list)
1918 (setq tramp-completion-function-alist
1919 (delete (assoc method tramp-completion-function-alist)
1920 tramp-completion-function-alist))
1923 ;; Remove double entries
1924 (when (member (car v) (cdr v))
1925 (setcdr v (delete (car v) (cdr v))))
1926 ;; Check for function and file
1927 (unless (and (functionp (nth 0 (car v)))
1928 (file-exists-p (nth 1 (car v))))
1929 (setq r (delete (car v) r)))
1933 (add-to-list 'tramp-completion-function-alist
1936 (defun tramp-get-completion-function (method)
1937 "Returns list of completion functions for METHOD.
1938 For definition of that list see `tramp-set-completion-function'."
1939 (cdr (assoc method tramp-completion-function-alist)))
1941 ;;; File Name Handler Functions:
1943 (defun tramp-handle-make-symbolic-link
1944 (filename linkname &optional ok-if-already-exists)
1945 "Like `make-symbolic-link' for tramp files.
1946 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1947 the symlink. If LINKNAME is a Tramp file, only the localname component is
1948 used as the target of the symlink.
1950 If LINKNAME is a Tramp file and the localname component is relative, then
1951 it is expanded first, before the localname component is taken. Note that
1952 this can give surprising results if the user/host for the source and
1953 target of the symlink differ."
1954 (with-parsed-tramp-file-name linkname l
1955 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
1956 (cwd (file-name-directory l-localname)))
1959 (list "Making a symbolic link."
1960 "ln(1) does not exist on the remote host.")))
1962 ;; Do the 'confirm if exists' thing.
1963 (when (file-exists-p linkname)
1965 (if (or (null ok-if-already-exists) ; not allowed to exist
1966 (and (numberp ok-if-already-exists)
1969 "File %s already exists; make it a link anyway? "
1971 (signal 'file-already-exists (list "File already exists" l-localname))
1972 (delete-file linkname)))
1974 ;; If FILENAME is a Tramp name, use just the localname component.
1975 (when (tramp-tramp-file-p filename)
1976 (setq filename (tramp-file-name-localname
1977 (tramp-dissect-file-name
1978 (expand-file-name filename)))))
1980 ;; Right, they are on the same host, regardless of user, method, etc.
1981 ;; We now make the link on the remote machine. This will occur as the user
1982 ;; that FILENAME belongs to.
1984 (tramp-send-command-and-check
1985 l-multi-method l-method l-user l-host
1986 (format "cd %s && %s -sf %s %s"
1993 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
1994 "Like `load' for tramp files. Not implemented!"
1995 (unless (file-name-absolute-p file)
1996 (error "Tramp cannot `load' files without absolute file name"))
1997 (with-parsed-tramp-file-name file nil
1999 (cond ((file-exists-p (concat file ".elc"))
2000 (setq file (concat file ".elc")))
2001 ((file-exists-p (concat file ".el"))
2002 (setq file (concat file ".el")))))
2004 ;; The first condition is always true for absolute file names.
2005 ;; Included for safety's sake.
2006 (unless (or (file-name-directory file)
2007 (string-match "\\.elc?\\'" file))
2008 (error "File `%s' does not include a `.el' or `.elc' suffix"
2011 (when (not (file-exists-p file))
2012 (error "Cannot load nonexistant file `%s'" file)))
2013 (if (not (file-exists-p file))
2016 (message "Loading %s..." file))
2017 (let ((local-copy (file-local-copy file)))
2018 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2019 (load local-copy noerror t t)
2020 (delete-file local-copy))
2022 (message "Loading %s...done" file))
2025 ;; Localname manipulation functions that grok TRAMP localnames...
2026 (defun tramp-handle-file-name-directory (file)
2027 "Like `file-name-directory' but aware of TRAMP files."
2028 ;; everything except the last filename thing is the directory
2029 (with-parsed-tramp-file-name file nil
2030 ;; For the following condition, two possibilities should be tried:
2031 ;; (1) (string= localname "")
2032 ;; (2) (or (string= localname "") (string= localname "/"))
2033 ;; The second variant fails when completing a "/" directory on
2034 ;; the remote host, that is a filename which looks like
2035 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2036 ;; We should do some investigation.
2037 (if (string= localname "")
2038 ;; For a filename like "/[foo]", we return "/". The `else'
2039 ;; case would return "/[foo]" unchanged. But if we do that,
2040 ;; then `file-expand-wildcards' ceases to work. It's not
2041 ;; quite clear to me what's the intuition that tells that this
2042 ;; behavior is the right behavior, but oh, well.
2044 ;; run the command on the localname portion only
2045 ;; CCC: This should take into account the remote machine type, no?
2046 ;; --daniel <daniel@danann.net>
2047 (tramp-make-tramp-file-name multi-method method user host
2048 ;; This will not recurse...
2049 (or (file-name-directory localname) "")))))
2051 (defun tramp-handle-file-name-nondirectory (file)
2052 "Like `file-name-nondirectory' but aware of TRAMP files."
2053 (with-parsed-tramp-file-name file nil
2054 (file-name-nondirectory localname)))
2056 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2057 "Like `file-truename' for tramp files."
2058 (with-parsed-tramp-file-name filename nil
2059 (let* ((steps (tramp-split-string localname "/"))
2060 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2061 (file-name-as-directory localname)))
2062 (is-dir (string= localname localnamedir))
2065 ;; Don't make the following value larger than necessary.
2066 ;; People expect an error message in a timely fashion when
2067 ;; something is wrong; otherwise they might think that Emacs
2068 ;; is hung. Of course, correctness has to come first.
2070 (result nil) ;result steps in reverse order
2072 (tramp-message-for-buffer
2073 multi-method method user host
2074 10 "Finding true name for `%s'" filename)
2075 (while (and steps (< numchase numchase-limit))
2076 (setq thisstep (pop steps))
2077 (tramp-message-for-buffer
2078 multi-method method user host
2080 (mapconcat 'identity
2081 (append '("") (reverse result) (list thisstep))
2083 (setq symlink-target
2084 (nth 0 (file-attributes
2085 (tramp-make-tramp-file-name
2086 multi-method method user host
2087 (mapconcat 'identity
2092 (cond ((string= "." thisstep)
2093 (tramp-message-for-buffer multi-method method user host
2094 10 "Ignoring step `.'"))
2095 ((string= ".." thisstep)
2096 (tramp-message-for-buffer multi-method method user host
2097 10 "Processing step `..'")
2099 ((stringp symlink-target)
2100 ;; It's a symlink, follow it.
2101 (tramp-message-for-buffer
2102 multi-method method user host
2103 10 "Follow symlink to %s" symlink-target)
2104 (setq numchase (1+ numchase))
2105 (when (file-name-absolute-p symlink-target)
2107 ;; If the symlink was absolute, we'll get a string like
2108 ;; "/user@host:/some/target"; extract the
2109 ;; "/some/target" part from it.
2110 (when (tramp-tramp-file-p symlink-target)
2111 (with-parsed-tramp-file-name symlink-target sym
2112 (unless (equal (list multi-method method user host)
2113 (list sym-multi-method sym-method
2115 (error "Symlink target `%s' on wrong host"
2117 (setq symlink-target localname)))
2119 (append (tramp-split-string symlink-target "/") steps)))
2122 (setq result (cons thisstep result)))))
2123 (when (>= numchase numchase-limit)
2124 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2125 (setq result (reverse result))
2126 ;; Combine list to form string.
2129 (mapconcat 'identity (cons "" result) "/")
2131 (when (and is-dir (or (string= "" result)
2132 (not (string= (substring result -1) "/"))))
2133 (setq result (concat result "/")))
2134 (tramp-message-for-buffer
2135 multi-method method user host
2136 10 "True name of `%s' is `%s'" filename result)
2137 (tramp-make-tramp-file-name
2138 multi-method method user host result))))
2142 (defun tramp-handle-file-exists-p (filename)
2143 "Like `file-exists-p' for tramp files."
2144 (with-parsed-tramp-file-name filename nil
2146 (zerop (tramp-send-command-and-check
2147 multi-method method user host
2149 (tramp-get-file-exists-command multi-method method user host)
2150 (tramp-shell-quote-argument localname)))))))
2152 ;; Devices must distinguish physical file systems. The device numbers
2153 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2154 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2155 ;; EFS use device number "-1". In order to be different, we use device number
2156 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2157 (defvar tramp-devices nil
2158 "Keeps virtual device numbers.")
2160 ;; CCC: This should check for an error condition and signal failure
2161 ;; when something goes wrong.
2162 ;; Daniel Pittman <daniel@danann.net>
2163 (defun tramp-handle-file-attributes (filename &optional id-format)
2164 "Like `file-attributes' for tramp files."
2165 (let ((nonnumeric (and id-format (equal id-format 'string)))
2167 (with-parsed-tramp-file-name filename nil
2168 (when (file-exists-p filename)
2169 ;; file exists, find out stuff
2171 (if (tramp-get-remote-perl multi-method method user host)
2173 (tramp-handle-file-attributes-with-perl
2174 multi-method method user host localname nonnumeric))
2176 (tramp-handle-file-attributes-with-ls
2177 multi-method method user host localname nonnumeric)))
2178 ;; set virtual device number
2179 (setcar (nthcdr 11 result)
2180 (tramp-get-device multi-method method user host)))))
2183 (defun tramp-handle-file-attributes-with-ls
2184 (multi-method method user host localname &optional nonnumeric)
2185 "Implement `file-attributes' for tramp files using the ls(1) command."
2187 res-inode res-filemodes res-numlinks
2188 res-uid res-gid res-size res-symlink-target)
2189 (tramp-message-for-buffer multi-method method user host 10
2190 "file attributes with ls: %s"
2191 (tramp-make-tramp-file-name
2192 multi-method method user host localname))
2194 multi-method method user host
2196 (tramp-get-ls-command multi-method method user host)
2197 (if nonnumeric "-ild" "-ildn")
2198 (tramp-shell-quote-argument localname)))
2199 (tramp-wait-for-output)
2200 ;; parse `ls -l' output ...
2204 (read (current-buffer))
2205 (invalid-read-syntax
2206 (when (and (equal (cadr err)
2207 "Integer constant overflow in reader")
2209 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2211 (let* ((big (read (substring (caddr err) 0
2212 (match-beginning 1))))
2213 (small (read (match-string 1 (caddr err))))
2214 (twiddle (/ small 65536)))
2215 (cons (+ big twiddle)
2216 (- small (* twiddle 65536))))))))
2217 ;; ... file mode flags
2218 (setq res-filemodes (symbol-name (read (current-buffer))))
2220 (setq res-numlinks (read (current-buffer)))
2222 (setq res-uid (read (current-buffer)))
2223 (setq res-gid (read (current-buffer)))
2225 (unless (numberp res-uid) (setq res-uid -1))
2226 (unless (numberp res-gid) (setq res-gid -1)))
2228 (setq res-size (read (current-buffer)))
2229 ;; From the file modes, figure out other stuff.
2230 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2231 (setq dirp (eq ?d (aref res-filemodes 0)))
2232 ;; if symlink, find out file name pointed to
2234 (search-forward "-> ")
2235 (setq res-symlink-target
2236 (buffer-substring (point)
2237 (tramp-line-end-position))))
2238 ;; return data gathered
2240 ;; 0. t for directory, string (name linked to) for symbolic
2242 (or dirp res-symlink-target nil)
2243 ;; 1. Number of links to file.
2249 ;; 4. Last access time, as a list of two integers. First
2250 ;; integer has high-order 16 bits of time, second has low 16
2252 ;; 5. Last modification time, likewise.
2253 ;; 6. Last status change time, likewise.
2254 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2255 ;; 7. Size in bytes (-1, if number is out of range).
2257 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2259 ;; 9. t iff file's gid would change if file were deleted and
2262 ;; 10. inode number.
2264 ;; 11. Device number. Will be replaced by a virtual device number.
2268 (defun tramp-handle-file-attributes-with-perl
2269 (multi-method method user host localname &optional nonnumeric)
2270 "Implement `file-attributes' for tramp files using a Perl script.
2272 The Perl command is sent to the remote machine when the connection
2273 is initially created and is kept cached by the remote shell."
2274 (tramp-message-for-buffer multi-method method user host 10
2275 "file attributes with perl: %s"
2276 (tramp-make-tramp-file-name
2277 multi-method method user host localname))
2279 multi-method method user host
2280 (format "tramp_file_attributes %s %s"
2281 (tramp-shell-quote-argument localname) nonnumeric))
2282 (tramp-wait-for-output)
2283 (let ((result (read (current-buffer))))
2284 (setcar (nthcdr 8 result)
2285 (tramp-file-mode-from-int (nth 8 result)))
2288 (defun tramp-get-device (multi-method method user host)
2289 "Returns the virtual device number.
2290 If it doesn't exist, generate a new one."
2291 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
2292 (unless (assoc string tramp-devices)
2293 (add-to-list 'tramp-devices
2294 (list string (length tramp-devices))))
2295 (list -1 (nth 1 (assoc string tramp-devices)))))
2297 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2298 "Like `set-visited-file-modtime' for tramp files."
2299 (unless (buffer-file-name)
2300 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2303 (tramp-run-real-handler 'set-visited-file-modtime (list time-list)))
2304 (let ((f (buffer-file-name))
2305 (coding-system-used nil))
2306 (with-parsed-tramp-file-name f nil
2307 (let* ((attr (file-attributes f))
2308 (modtime (nth 5 attr)))
2309 ;; We use '(0 0) as a don't-know value. See also
2310 ;; `tramp-handle-file-attributes-with-ls'.
2311 (when (boundp 'last-coding-system-used)
2312 (setq coding-system-used last-coding-system-used))
2313 (if (not (equal modtime '(0 0)))
2314 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2317 multi-method method user host
2318 (format "%s -ild %s"
2319 (tramp-get-ls-command multi-method method user host)
2320 (tramp-shell-quote-argument localname)))
2321 (tramp-wait-for-output)
2322 (setq attr (buffer-substring (point)
2323 (progn (end-of-line) (point)))))
2324 (setq tramp-buffer-file-attributes attr))
2325 (when (boundp 'last-coding-system-used)
2326 (setq last-coding-system-used coding-system-used))
2329 ;; CCC continue here
2331 ;; This function makes the same assumption as
2332 ;; `tramp-handle-set-visited-file-modtime'.
2333 (defun tramp-handle-verify-visited-file-modtime (buf)
2334 "Like `verify-visited-file-modtime' for tramp files."
2335 (with-current-buffer buf
2336 (let ((f (buffer-file-name)))
2337 (with-parsed-tramp-file-name f nil
2338 (let* ((attr (file-attributes f))
2339 (modtime (nth 5 attr)))
2340 (cond ((and attr (not (equal modtime '(0 0))))
2341 ;; Why does `file-attributes' return a list (HIGH
2342 ;; LOW), but `visited-file-modtime' returns a cons
2344 (let ((mt (visited-file-modtime)))
2345 (< (abs (tramp-time-diff
2347 ;; For compatibility, deal with both the old
2348 ;; (HIGH . LOW) and the new (HIGH LOW)
2349 ;; return values of `visited-file-modtime'.
2351 (list (car mt) (cdr mt))
2357 multi-method method user host
2358 (format "%s -ild %s"
2359 (tramp-get-ls-command multi-method method
2361 (tramp-shell-quote-argument localname)))
2362 (tramp-wait-for-output)
2363 (setq attr (buffer-substring
2364 (point) (progn (end-of-line) (point)))))
2365 (equal tramp-buffer-file-attributes attr))
2366 ;; If file does not exist, say it is not modified.
2369 (defadvice clear-visited-file-modtime (after tramp activate)
2370 "Set `tramp-buffer-file-attributes' back to nil.
2371 Tramp uses this variable as an emulation for the actual modtime of the file,
2372 if the remote host can't provide the modtime."
2373 (setq tramp-buffer-file-attributes nil))
2375 (defun tramp-handle-set-file-modes (filename mode)
2376 "Like `set-file-modes' for tramp files."
2377 (with-parsed-tramp-file-name filename nil
2379 (unless (zerop (tramp-send-command-and-check
2380 multi-method method user host
2381 (format "chmod %s %s"
2382 (tramp-decimal-to-octal mode)
2383 (tramp-shell-quote-argument localname))))
2386 ;; FIXME: extract the proper text from chmod's stderr.
2387 "error while changing file's mode"
2390 ;; Simple functions using the `test' command.
2392 (defun tramp-handle-file-executable-p (filename)
2393 "Like `file-executable-p' for tramp files."
2394 (with-parsed-tramp-file-name filename nil
2395 (zerop (tramp-run-test "-x" filename))))
2397 (defun tramp-handle-file-readable-p (filename)
2398 "Like `file-readable-p' for tramp files."
2399 (with-parsed-tramp-file-name filename nil
2400 (zerop (tramp-run-test "-r" filename))))
2402 (defun tramp-handle-file-accessible-directory-p (filename)
2403 "Like `file-accessible-directory-p' for tramp files."
2404 (with-parsed-tramp-file-name filename nil
2405 (and (zerop (tramp-run-test "-d" filename))
2406 (zerop (tramp-run-test "-r" filename))
2407 (zerop (tramp-run-test "-x" filename)))))
2409 ;; When the remote shell is started, it looks for a shell which groks
2410 ;; tilde expansion. Here, we assume that all shells which grok tilde
2411 ;; expansion will also provide a `test' command which groks `-nt' (for
2412 ;; newer than). If this breaks, tell me about it and I'll try to do
2413 ;; something smarter about it.
2414 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2415 "Like `file-newer-than-file-p' for tramp files."
2416 (cond ((not (file-exists-p file1))
2418 ((not (file-exists-p file2))
2420 ;; We are sure both files exist at this point.
2423 ;; We try to get the mtime of both files. If they are not
2424 ;; equal to the "dont-know" value, then we subtract the times
2425 ;; and obtain the result.
2426 (let ((fa1 (file-attributes file1))
2427 (fa2 (file-attributes file2)))
2428 (if (and (not (equal (nth 5 fa1) '(0 0)))
2429 (not (equal (nth 5 fa2) '(0 0))))
2430 (> 0 (tramp-time-diff (nth 5 fa1) (nth 5 fa2)))
2431 ;; If one of them is the dont-know value, then we can
2432 ;; still try to run a shell command on the remote host.
2433 ;; However, this only works if both files are Tramp
2434 ;; files and both have the same method, same user, same
2436 (unless (and (tramp-tramp-file-p file1)
2437 (tramp-tramp-file-p file2))
2441 "Cannot check if Tramp file is newer than non-Tramp file"
2443 (with-parsed-tramp-file-name file1 v1
2444 (with-parsed-tramp-file-name file2 v2
2445 (unless (and (equal v1-multi-method v2-multi-method)
2446 (equal v1-method v2-method)
2447 (equal v1-user v2-user)
2448 (equal v1-host v2-host))
2450 (list "Files must have same method, user, host"
2452 (unless (and (tramp-tramp-file-p file1)
2453 (tramp-tramp-file-p file2))
2455 (list "Files must be tramp files on same host"
2457 (if (tramp-get-test-groks-nt
2458 v1-multi-method v1-method v1-user v1-host)
2459 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2460 (zerop (tramp-run-test2
2461 "tramp_test_nt" file1 file2)))))))))))
2463 ;; Functions implemented using the basic functions above.
2465 (defun tramp-handle-file-modes (filename)
2466 "Like `file-modes' for tramp files."
2467 (with-parsed-tramp-file-name filename nil
2468 (when (file-exists-p filename)
2469 (tramp-mode-string-to-int
2470 (nth 8 (file-attributes filename))))))
2472 (defun tramp-handle-file-directory-p (filename)
2473 "Like `file-directory-p' for tramp files."
2474 ;; Care must be taken that this function returns `t' for symlinks
2475 ;; pointing to directories. Surely the most obvious implementation
2476 ;; would be `test -d', but that returns false for such symlinks.
2477 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2478 ;; I now think he's right. So we could be using `test -d', couldn't
2481 ;; Alternatives: `cd %s', `test -d %s'
2482 (with-parsed-tramp-file-name filename nil
2485 (tramp-send-command-and-check
2486 multi-method method user host
2487 (format "test -d %s"
2488 (tramp-shell-quote-argument localname))
2489 t))))) ;run command in subshell
2491 (defun tramp-handle-file-regular-p (filename)
2492 "Like `file-regular-p' for tramp files."
2493 (with-parsed-tramp-file-name filename nil
2494 (and (file-exists-p filename)
2495 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2497 (defun tramp-handle-file-symlink-p (filename)
2498 "Like `file-symlink-p' for tramp files."
2499 (with-parsed-tramp-file-name filename nil
2500 (let ((x (car (file-attributes filename))))
2502 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2503 ;; might do weird things.
2504 (if (file-name-absolute-p x)
2505 (tramp-make-tramp-file-name
2506 multi-method method user host x)
2509 (defun tramp-handle-file-writable-p (filename)
2510 "Like `file-writable-p' for tramp files."
2511 (with-parsed-tramp-file-name filename nil
2512 (if (file-exists-p filename)
2513 ;; Existing files must be writable.
2514 (zerop (tramp-run-test "-w" filename))
2515 ;; If file doesn't exist, check if directory is writable.
2516 (and (zerop (tramp-run-test
2517 "-d" (file-name-directory filename)))
2518 (zerop (tramp-run-test
2519 "-w" (file-name-directory filename)))))))
2521 (defun tramp-handle-file-ownership-preserved-p (filename)
2522 "Like `file-ownership-preserved-p' for tramp files."
2523 (with-parsed-tramp-file-name filename nil
2524 (or (not (file-exists-p filename))
2525 ;; Existing files must be writable.
2526 (zerop (tramp-run-test "-O" filename)))))
2528 ;; Other file name ops.
2530 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2531 ;; (defun tramp-handle-directory-file-name (directory)
2532 ;; "Like `directory-file-name' for tramp files."
2533 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2534 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2535 ;; (substring directory 0 (- (length directory) 1))
2538 ;; ;; Philippe Troin <phil@fifi.org>
2539 ;; (defun tramp-handle-directory-file-name (directory)
2540 ;; "Like `directory-file-name' for tramp files."
2541 ;; (with-parsed-tramp-file-name directory nil
2542 ;; (let ((directory-length-1 (1- (length directory))))
2544 ;; (if (and (eq (aref directory directory-length-1) ?/)
2545 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2546 ;; (/= (match-end 0) directory-length-1))
2547 ;; (substring directory 0 directory-length-1)
2550 (defun tramp-handle-directory-file-name (directory)
2551 "Like `directory-file-name' for tramp files."
2552 ;; If localname component of filename is "/", leave it unchanged.
2553 ;; Otherwise, remove any trailing slash from localname component.
2554 ;; Method, host, etc, are unchanged. Does it make sense to try
2555 ;; to avoid parsing the filename?
2556 (with-parsed-tramp-file-name directory nil
2557 (if (and (not (zerop (length localname)))
2558 (eq (aref localname (1- (length localname))) ?/)
2559 (not (string= localname "/")))
2560 (substring directory 0 -1)
2563 ;; Directory listings.
2565 (defun tramp-handle-directory-files (directory
2566 &optional full match nosort files-only)
2567 "Like `directory-files' for tramp files."
2568 (with-parsed-tramp-file-name directory nil
2571 (tramp-barf-unless-okay
2572 multi-method method user host
2573 (concat "cd " (tramp-shell-quote-argument localname))
2576 "tramp-handle-directory-files: couldn't `cd %s'"
2577 (tramp-shell-quote-argument localname))
2579 multi-method method user host
2580 (concat (tramp-get-ls-command multi-method method user host)
2582 (tramp-wait-for-output)
2583 (goto-char (point-max))
2584 (while (zerop (forward-line -1))
2585 (setq x (buffer-substring (point)
2586 (tramp-line-end-position)))
2587 (when (or (not match) (string-match match x))
2589 (push (concat (file-name-as-directory directory)
2593 (tramp-send-command multi-method method user host "cd")
2594 (tramp-wait-for-output)
2595 ;; Remove non-files or non-directories if necessary. Using
2596 ;; the remote shell for this would probably be way faster.
2597 ;; Maybe something could be adapted from
2598 ;; tramp-handle-file-name-all-completions.
2600 (let ((temp (nreverse result))
2603 (if (equal files-only t)
2606 (setq item (pop temp))
2607 (when (file-regular-p item)
2608 (push item result)))
2611 (setq item (pop temp))
2612 (when (file-directory-p item)
2613 (push item result)))))))
2616 ;; This function should return "foo/" for directories and "bar" for
2617 ;; files. We use `ls -ad' to get a list of files (including
2618 ;; directories), and `find . -type d \! -name . -prune' to get a list
2620 (defun tramp-handle-file-name-all-completions (filename directory)
2621 "Like `file-name-all-completions' for tramp files."
2622 (with-parsed-tramp-file-name directory nil
2623 (unless (save-match-data (string-match "/" filename))
2624 (let* ((nowild tramp-completion-without-shell-p)
2627 (tramp-barf-unless-okay
2628 multi-method method user host
2629 (format "cd %s" (tramp-shell-quote-argument localname))
2631 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2632 (tramp-shell-quote-argument localname))
2634 ;; Get a list of directories and files, including reliably
2635 ;; tagging the directories with a trailing '/'. Because I
2636 ;; rock. --daniel@danann.net
2638 multi-method method user host
2639 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2640 "if test -d \"$f\" 2>/dev/null; "
2641 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2642 (tramp-get-ls-command multi-method method user host)
2643 (if (or nowild (zerop (length filename)))
2646 (tramp-shell-quote-argument filename)))))
2648 ;; Now grab the output.
2649 (tramp-wait-for-output)
2650 (goto-char (point-max))
2651 (while (zerop (forward-line -1))
2652 (push (buffer-substring (point)
2653 (tramp-line-end-position))
2656 (tramp-send-command multi-method method user host "cd")
2657 (tramp-wait-for-output)
2661 (all-completions filename (mapcar 'list result))
2665 ;; The following isn't needed for Emacs 20 but for 19.34?
2666 (defun tramp-handle-file-name-completion (filename directory)
2667 "Like `file-name-completion' for tramp files."
2668 (unless (tramp-tramp-file-p directory)
2670 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2672 (with-parsed-tramp-file-name directory nil
2675 (mapcar (lambda (x) (cons x nil))
2676 (file-name-all-completions filename directory)))))
2680 (defun tramp-handle-add-name-to-file
2681 (filename newname &optional ok-if-already-exists)
2682 "Like `add-name-to-file' for tramp files."
2683 (with-parsed-tramp-file-name filename v1
2684 (with-parsed-tramp-file-name newname v2
2685 (let ((ln (when v1 (tramp-get-remote-ln
2686 v1-multi-method v1-method v1-user v1-host))))
2687 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2688 (equal v1-multi-method v2-multi-method)
2689 (equal v1-method v2-method)
2690 (equal v1-user v2-user)
2691 (equal v1-host v2-host))
2692 (error "add-name-to-file: %s"
2693 "only implemented for same method, same user, same host"))
2694 (when (and (not ok-if-already-exists)
2695 (file-exists-p newname)
2696 (not (numberp ok-if-already-exists))
2699 "File %s already exists; make it a new name anyway? "
2701 (error "add-name-to-file: file %s already exists" newname))
2702 (tramp-barf-unless-okay
2703 v1-multi-method v1-method v1-user v1-host
2704 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2705 (tramp-shell-quote-argument v2-localname))
2707 "error with add-name-to-file, see buffer `%s' for details"
2710 (defun tramp-handle-copy-file
2711 (filename newname &optional ok-if-already-exists keep-date)
2712 "Like `copy-file' for tramp files."
2713 ;; Check if both files are local -- invoke normal copy-file.
2714 ;; Otherwise, use tramp from local system.
2715 (setq filename (expand-file-name filename))
2716 (setq newname (expand-file-name newname))
2717 ;; At least one file a tramp file?
2718 (if (or (tramp-tramp-file-p filename)
2719 (tramp-tramp-file-p newname))
2720 (let ((modes (file-modes filename)))
2721 (tramp-do-copy-or-rename-file
2722 'copy filename newname ok-if-already-exists keep-date)
2723 (set-file-modes newname modes))
2724 (tramp-run-real-handler
2726 (list filename newname ok-if-already-exists keep-date))))
2728 (defun tramp-handle-rename-file
2729 (filename newname &optional ok-if-already-exists)
2730 "Like `rename-file' for tramp files."
2731 ;; Check if both files are local -- invoke normal rename-file.
2732 ;; Otherwise, use tramp from local system.
2733 (setq filename (expand-file-name filename))
2734 (setq newname (expand-file-name newname))
2735 ;; At least one file a tramp file?
2736 (if (or (tramp-tramp-file-p filename)
2737 (tramp-tramp-file-p newname))
2738 (tramp-do-copy-or-rename-file
2739 'rename filename newname ok-if-already-exists)
2740 (tramp-run-real-handler 'rename-file
2741 (list filename newname ok-if-already-exists))))
2743 (defun tramp-do-copy-or-rename-file
2744 (op filename newname &optional ok-if-already-exists keep-date)
2745 "Copy or rename a remote file.
2746 OP must be `copy' or `rename' and indicates the operation to perform.
2747 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2748 the new file (for copy) or the new name of the file (for rename).
2749 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2750 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2753 This function is invoked by `tramp-handle-copy-file' and
2754 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2755 and `rename'. FILENAME and NEWNAME must be absolute file names."
2756 (unless (memq op '(copy rename))
2757 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2758 (unless ok-if-already-exists
2759 (when (file-exists-p newname)
2760 (signal 'file-already-exists
2762 (let ((t1 (tramp-tramp-file-p filename))
2763 (t2 (tramp-tramp-file-p newname))
2764 v1-multi-method v1-method v1-user v1-host v1-localname
2765 v2-multi-method v2-method v2-user v2-host v2-localname)
2767 ;; Check which ones of source and target are Tramp files.
2768 ;; We cannot invoke `with-parsed-tramp-file-name';
2769 ;; it fails if the file isn't a Tramp file name.
2771 (with-parsed-tramp-file-name filename l
2772 (setq v1-multi-method l-multi-method
2776 v1-localname l-localname))
2777 (setq v1-localname filename))
2779 (with-parsed-tramp-file-name newname l
2780 (setq v2-multi-method l-multi-method
2784 v2-localname l-localname))
2785 (setq v2-localname newname))
2788 ;; Both are Tramp files.
2791 ;; Shortcut: if method, host, user are the same for both
2792 ;; files, we invoke `cp' or `mv' on the remote host
2794 ((and (equal v1-multi-method v2-multi-method)
2795 (equal v1-method v2-method)
2796 (equal v1-user v2-user)
2797 (equal v1-host v2-host))
2798 (tramp-do-copy-or-rename-file-directly
2799 op v1-multi-method v1-method v1-user v1-host
2800 v1-localname v2-localname keep-date))
2801 ;; If both source and target are Tramp files,
2802 ;; both are using the same copy-program, then we
2803 ;; can invoke rcp directly. Note that
2804 ;; default-directory should point to a local
2805 ;; directory if we want to invoke rcp.
2806 ((and (not v1-multi-method)
2807 (not v2-multi-method)
2808 (equal v1-method v2-method)
2809 (tramp-method-out-of-band-p
2810 v1-multi-method v1-method v1-user v1-host)
2811 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2812 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2813 (tramp-do-copy-or-rename-file-out-of-band
2814 op filename newname keep-date))
2815 ;; No shortcut was possible. So we copy the
2816 ;; file first. If the operation was `rename', we go
2817 ;; back and delete the original file (if the copy was
2818 ;; successful). The approach is simple-minded: we
2819 ;; create a new buffer, insert the contents of the
2820 ;; source file into it, then write out the buffer to
2821 ;; the target file. The advantage is that it doesn't
2822 ;; matter which filename handlers are used for the
2823 ;; source and target file.
2825 (tramp-do-copy-or-rename-file-via-buffer
2826 op filename newname keep-date))))
2828 ;; One file is a Tramp file, the other one is local.
2830 ;; If the Tramp file has an out-of-band method, the corresponding
2831 ;; copy-program can be invoked.
2832 (if (and (not v1-multi-method)
2833 (not v2-multi-method)
2834 (or (tramp-method-out-of-band-p
2835 v1-multi-method v1-method v1-user v1-host)
2836 (tramp-method-out-of-band-p
2837 v2-multi-method v2-method v2-user v2-host)))
2838 (tramp-do-copy-or-rename-file-out-of-band
2839 op filename newname keep-date)
2840 ;; Use the generic method via a Tramp buffer.
2841 (tramp-do-copy-or-rename-file-via-buffer
2842 op filename newname keep-date)))
2845 ;; One of them must be a Tramp file.
2846 (error "Tramp implementation says this cannot happen")))))
2848 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2849 "Use an Emacs buffer to copy or rename a file.
2850 First arg OP is either `copy' or `rename' and indicates the operation.
2851 FILENAME is the source file, NEWNAME the target file.
2852 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2853 (let ((trampbuf (get-buffer-create "*tramp output*"))
2854 (modtime (nth 5 (file-attributes filename))))
2855 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
2857 1 (concat "Warning: cannot preserve file time stamp"
2858 " with inline copying across machines")))
2860 (set-buffer trampbuf) (erase-buffer)
2861 (insert-file-contents-literally filename)
2862 ;; We don't want the target file to be compressed, so we let-bind
2863 ;; `jka-compr-inhibit' to t.
2864 (let ((coding-system-for-write 'binary)
2865 (jka-compr-inhibit t))
2866 (write-region (point-min) (point-max) newname))
2867 ;; KEEP-DATE handling.
2869 (when (and (not (null modtime))
2870 (not (equal modtime '(0 0))))
2871 (tramp-touch newname modtime))
2872 (set-file-modes newname (file-modes filename))))
2873 ;; If the operation was `rename', delete the original file.
2874 (unless (eq op 'copy)
2875 (delete-file filename))))
2877 (defun tramp-do-copy-or-rename-file-directly
2878 (op multi-method method user host localname1 localname2 keep-date)
2879 "Invokes `cp' or `mv' on the remote system.
2880 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2881 respectively. METHOD, USER, and HOST specify the connection.
2882 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
2883 If KEEP-DATE is non-nil, preserve the time stamp when copying."
2884 ;; CCC: What happens to the timestamp when renaming?
2885 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
2886 ((eq op 'copy) "cp -f")
2887 ((eq op 'rename) "mv -f")
2889 "Unknown operation `%s', must be `copy' or `rename'"
2892 (tramp-barf-unless-okay
2893 multi-method method user host
2896 (tramp-shell-quote-argument localname1)
2897 (tramp-shell-quote-argument localname2))
2899 "Copying directly failed, see buffer `%s' for details."
2902 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2903 "Invoke rcp program to copy.
2904 One of FILENAME and NEWNAME must be a Tramp name, the other must
2905 be a local filename. The method used must be an out-of-band method."
2906 (let ((t1 (tramp-tramp-file-p filename))
2907 (t2 (tramp-tramp-file-p newname))
2908 v1-multi-method v1-method v1-user v1-host v1-localname
2909 v2-multi-method v2-method v2-user v2-host v2-localname
2910 multi-method method user host copy-program copy-args
2911 source target trampbuf)
2913 ;; Check which ones of source and target are Tramp files.
2914 ;; We cannot invoke `with-parsed-tramp-file-name';
2915 ;; it fails if the file isn't a Tramp file name.
2917 (with-parsed-tramp-file-name filename l
2918 (setq v1-multi-method l-multi-method
2922 v1-localname l-localname
2923 multi-method l-multi-method
2924 method (tramp-find-method
2925 v1-multi-method v1-method v1-user v1-host)
2928 copy-program (tramp-get-method-parameter
2929 v1-multi-method method
2930 v1-user v1-host 'tramp-copy-program)
2931 copy-args (tramp-get-method-parameter
2932 v1-multi-method method
2933 v1-user v1-host 'tramp-copy-args)))
2934 (setq v1-localname filename))
2937 (with-parsed-tramp-file-name newname l
2938 (setq v2-multi-method l-multi-method
2942 v2-localname l-localname
2943 multi-method l-multi-method
2944 method (tramp-find-method
2945 v2-multi-method v2-method v2-user v2-host)
2948 copy-program (tramp-get-method-parameter
2949 v2-multi-method method
2950 v2-user v2-host 'tramp-copy-program)
2951 copy-args (tramp-get-method-parameter
2952 v2-multi-method method
2953 v2-user v2-host 'tramp-copy-args)))
2954 (setq v2-localname newname))
2956 ;; The following should be changed. We need a more general
2957 ;; mechanism to parse extra host args.
2959 (setq source v1-localname)
2960 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
2961 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
2962 (setq v1-host (match-string 1 v1-host)))
2964 (tramp-make-copy-program-file-name
2966 (tramp-shell-quote-argument v1-localname))))
2969 (setq target v2-localname)
2970 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
2971 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
2972 (setq v2-host (match-string 1 v2-host)))
2974 (tramp-make-copy-program-file-name
2976 (tramp-shell-quote-argument v2-localname))))
2978 ;; Handle keep-date argument
2982 (cons (tramp-get-method-parameter
2983 v1-multi-method method
2984 v1-user v1-host 'tramp-copy-keep-date-arg)
2987 (cons (tramp-get-method-parameter
2988 v2-multi-method method
2989 v2-user v2-host 'tramp-copy-keep-date-arg)
2992 (setq copy-args (append copy-args (list source target))
2993 trampbuf (generate-new-buffer
2994 (tramp-buffer-name multi-method method user host)))
2996 ;; Use an asynchronous process. By this, password can be handled.
2998 (set-buffer trampbuf)
2999 (setq tramp-current-multi-method multi-method
3000 tramp-current-method method
3001 tramp-current-user user
3002 tramp-current-host host)
3004 5 "Transferring %s to file %s..." filename newname)
3006 ;; Use rcp-like program for file transfer.
3007 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3008 copy-program copy-args)))
3009 (tramp-set-process-query-on-exit-flag p nil)
3010 (tramp-process-actions p multi-method method user host
3011 tramp-actions-copy-out-of-band))
3012 (kill-buffer trampbuf)
3014 5 "Transferring %s to file %s...done" filename newname))
3016 ;; If the operation was `rename', delete the original file.
3017 (unless (eq op 'copy)
3018 (delete-file filename))))
3021 (defun tramp-handle-make-directory (dir &optional parents)
3022 "Like `make-directory' for tramp files."
3023 (setq dir (expand-file-name dir))
3024 (with-parsed-tramp-file-name dir nil
3026 (tramp-barf-unless-okay
3027 multi-method method user host
3029 (if parents "mkdir -p" "mkdir")
3030 (tramp-shell-quote-argument localname))
3032 "Couldn't make directory %s" dir))))
3034 ;; CCC error checking?
3035 (defun tramp-handle-delete-directory (directory)
3036 "Like `delete-directory' for tramp files."
3037 (setq directory (expand-file-name directory))
3038 (with-parsed-tramp-file-name directory nil
3041 multi-method method user host
3042 (format "rmdir %s ; echo ok"
3043 (tramp-shell-quote-argument localname)))
3044 (tramp-wait-for-output))))
3046 (defun tramp-handle-delete-file (filename)
3047 "Like `delete-file' for tramp files."
3048 (setq filename (expand-file-name filename))
3049 (with-parsed-tramp-file-name filename nil
3051 (unless (zerop (tramp-send-command-and-check
3052 multi-method method user host
3054 (tramp-shell-quote-argument localname))))
3055 (signal 'file-error "Couldn't delete Tramp file")))))
3059 ;; CCC: This does not seem to be enough. Something dies when
3060 ;; we try and delete two directories under TRAMP :/
3061 (defun tramp-handle-dired-recursive-delete-directory (filename)
3062 "Recursively delete the directory given.
3063 This is like `dired-recursive-delete-directory' for tramp files."
3064 (with-parsed-tramp-file-name filename nil
3065 ;; run a shell command 'rm -r <localname>'
3066 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3067 (or (file-exists-p filename)
3070 (list "Removing old file name" "no such directory" filename)))
3071 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3072 (tramp-send-command multi-method method user host
3073 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3074 ;; Wait for the remote system to return to us...
3075 ;; This might take a while, allow it plenty of time.
3076 (tramp-wait-for-output 120)
3077 ;; Make sure that it worked...
3078 (and (file-exists-p filename)
3079 (error "Failed to recusively delete %s" filename))))
3081 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3082 "Like `dired-call-process' for tramp files."
3083 (with-parsed-tramp-file-name default-directory nil
3085 (tramp-barf-unless-okay
3086 multi-method method user host
3087 (format "cd %s" (tramp-shell-quote-argument localname))
3089 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3090 (tramp-shell-quote-argument localname))
3092 multi-method method user host
3093 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3094 (tramp-wait-for-output))
3096 (insert-buffer (tramp-get-buffer multi-method method user host)))
3099 (tramp-send-command-and-check multi-method method user host nil)
3100 (tramp-send-command multi-method method user host "cd")
3101 (tramp-wait-for-output)))))
3103 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3104 "Like `dired-compress-file' for tramp files."
3105 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3106 ;; Code stolen mainly from dired-aux.el.
3107 (with-parsed-tramp-file-name file nil
3110 (if (not (featurep 'xemacs))
3112 (symbol-value 'dired-compress-file-suffixes)
3113 ;; XEmacs has `dired-compression-method-alist', which is
3114 ;; transformed into `dired-compress-file-suffixes' structure.
3117 (list (concat (regexp-quote (nth 1 x)) "\\'")
3119 (mapconcat 'identity (nth 3 x) " ")))
3120 (symbol-value 'dired-compression-method-alist))))
3122 ;; See if any suffix rule matches this file name.
3124 (let (case-fold-search)
3125 (if (string-match (car (car suffixes)) localname)
3126 (setq suffix (car suffixes) suffixes nil))
3127 (setq suffixes (cdr suffixes))))
3129 (cond ((file-symlink-p file)
3131 ((and suffix (nth 2 suffix))
3132 ;; We found an uncompression rule.
3133 (message "Uncompressing %s..." file)
3134 (when (zerop (tramp-send-command-and-check
3135 multi-method method user host
3136 (concat (nth 2 suffix) " " localname)))
3137 (message "Uncompressing %s...done" file)
3138 ;; `dired-remove-file' is not defined in XEmacs
3139 (funcall (symbol-function 'dired-remove-file) file)
3140 (string-match (car suffix) file)
3141 (concat (substring file 0 (match-beginning 0)))))
3143 ;; We don't recognize the file as compressed, so compress it.
3145 (message "Compressing %s..." file)
3146 (when (zerop (tramp-send-command-and-check
3147 multi-method method user host
3148 (concat "gzip -f " localname)))
3149 (message "Compressing %s...done" file)
3150 ;; `dired-remove-file' is not defined in XEmacs
3151 (funcall (symbol-function 'dired-remove-file) file)
3152 (cond ((file-exists-p (concat file ".gz"))
3153 (concat file ".gz"))
3154 ((file-exists-p (concat file ".z"))
3158 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3159 ;; not sure at all that this is the right way to do it, but let's hope
3160 ;; it works for now, and wait for a guru to point out the Right Way to
3162 ;;(eval-when-compile
3163 ;; (unless (fboundp 'dired-insert-set-properties)
3164 ;; (fset 'dired-insert-set-properties 'ignore)))
3165 ;; Gerd suggests this:
3166 (eval-when-compile (require 'dired))
3167 ;; Note that dired is required at run-time, too, when it is needed.
3168 ;; It is only needed on XEmacs for the function
3169 ;; `dired-insert-set-properties'.
3171 (defun tramp-handle-insert-directory
3172 (filename switches &optional wildcard full-directory-p)
3173 "Like `insert-directory' for tramp files."
3174 ;; For the moment, we assume that the remote "ls" program does not
3175 ;; grok "--dired". In the future, we should detect this on
3176 ;; connection setup.
3177 (when (string-match "^--dired\\s-+" switches)
3178 (setq switches (replace-match "" nil t switches)))
3179 (setq filename (expand-file-name filename))
3180 (with-parsed-tramp-file-name filename nil
3181 (tramp-message-for-buffer
3182 multi-method method user host 10
3183 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3184 switches filename (if wildcard "yes" "no")
3185 (if full-directory-p "yes" "no"))
3187 (setq wildcard (file-name-nondirectory localname))
3188 (setq localname (file-name-directory localname)))
3189 (when (listp switches)
3190 (setq switches (mapconcat 'identity switches " ")))
3191 (unless full-directory-p
3192 (setq switches (concat "-d " switches)))
3194 (setq switches (concat switches " " wildcard)))
3196 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3197 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3198 (if full-directory-p
3200 multi-method method user host
3202 (tramp-get-ls-command multi-method method user host)
3206 (tramp-shell-quote-argument (concat localname ".")))))
3207 (tramp-barf-unless-okay
3208 multi-method method user host
3209 (format "cd %s" (tramp-shell-quote-argument
3210 (file-name-directory localname)))
3213 (tramp-shell-quote-argument (file-name-directory localname)))
3215 multi-method method user host
3217 (tramp-get-ls-command multi-method method user host)
3221 (tramp-shell-quote-argument
3222 (file-name-nondirectory localname))))))
3223 (sit-for 1) ;needed for rsh but not ssh?
3224 (tramp-wait-for-output))
3225 ;; The following let-binding is used by code that's commented
3226 ;; out. Let's leave the let-binding in for a while to see
3227 ;; that the commented-out code is really not needed. Commenting-out
3228 ;; happened on 2003-03-13.
3229 (let ((old-pos (point)))
3230 (insert-buffer-substring
3231 (tramp-get-buffer multi-method method user host))
3232 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3233 ;; that doesn't exist on Emacs, so we use this workaround instead.
3234 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3235 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3236 ;; (let ((zmacs-region-stays t))
3237 ;; (exchange-point-and-mark))
3239 (tramp-send-command multi-method method user host "cd")
3240 (tramp-wait-for-output))
3241 ;; For the time being, the XEmacs kludge is commented out.
3242 ;; Please test it on various XEmacs versions to see if it works.
3243 ;; ;; Another XEmacs specialty follows. What's the right way to do
3245 ;; (when (and (featurep 'xemacs)
3246 ;; (eq major-mode 'dired-mode))
3249 ;; (dired-insert-set-properties old-pos (point))))
3252 ;; Continuation of kluge to pacify byte-compiler.
3253 ;;(eval-when-compile
3254 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3255 ;; (fmakunbound 'dired-insert-set-properties)))
3257 ;; CCC is this the right thing to do?
3258 (defun tramp-handle-unhandled-file-name-directory (filename)
3259 "Like `unhandled-file-name-directory' for tramp files."
3260 (with-parsed-tramp-file-name filename nil
3261 (expand-file-name "~/")))
3263 ;; Canonicalization of file names.
3265 (defun tramp-drop-volume-letter (name)
3266 "Cut off unnecessary drive letter from file NAME.
3267 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3268 locally on a remote file name. When the local system is a W32 system
3269 but the remote system is Unix, this introduces a superfluous drive
3270 letter into the file name. This function removes it.
3272 Doesn't do anything if the NAME does not start with a drive letter."
3273 (if (and (> (length name) 1)
3274 (char-equal (aref name 1) ?:)
3275 (let ((c1 (aref name 0)))
3276 (or (and (>= c1 ?A) (<= c1 ?Z))
3277 (and (>= c1 ?a) (<= c1 ?z)))))
3281 (defun tramp-handle-expand-file-name (name &optional dir)
3282 "Like `expand-file-name' for tramp files.
3283 If the localname part of the given filename starts with \"/../\" then
3284 the result will be a local, non-Tramp, filename."
3285 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3286 (setq dir (or dir default-directory "/"))
3287 ;; Unless NAME is absolute, concat DIR and NAME.
3288 (unless (file-name-absolute-p name)
3289 (setq name (concat (file-name-as-directory dir) name)))
3290 ;; If NAME is not a tramp file, run the real handler
3291 (if (not (tramp-tramp-file-p name))
3292 (tramp-run-real-handler 'expand-file-name
3295 (with-parsed-tramp-file-name name nil
3296 (unless (file-name-absolute-p localname)
3297 (setq localname (concat "~/" localname)))
3299 ;; Tilde expansion if necessary. This needs a shell which
3300 ;; groks tilde expansion! The function `tramp-find-shell' is
3301 ;; supposed to find such a shell on the remote host. Please
3302 ;; tell me about it when this doesn't work on your system.
3303 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3304 (let ((uname (match-string 1 localname))
3305 (fname (match-string 2 localname)))
3306 ;; CCC fanatic error checking?
3307 (set-buffer (tramp-get-buffer multi-method method user host))
3310 multi-method method user host
3311 (format "cd %s; pwd" uname)
3313 (tramp-wait-for-output)
3314 (goto-char (point-min))
3315 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3316 (setq localname (concat uname fname))
3318 ;; No tilde characters in file name, do normal
3319 ;; expand-file-name (this does "/./" and "/../"). We bind
3320 ;; directory-sep-char here for XEmacs on Windows, which
3321 ;; would otherwise use backslash.
3322 (tramp-let-maybe directory-sep-char ?/
3323 (tramp-make-tramp-file-name
3324 multi-method (or method (tramp-find-default-method user host))
3326 (tramp-drop-volume-letter
3327 (tramp-run-real-handler 'expand-file-name
3328 (list localname)))))))))
3330 ;; old version follows. it uses ".." to cross file handler
3332 ;; ;; Look if localname starts with "/../" construct. If this is
3333 ;; ;; the case, then we return a local name instead of a remote name.
3334 ;; (if (string-match "^/\\.\\./" localname)
3335 ;; (expand-file-name (substring localname 3))
3336 ;; ;; No tilde characters in file name, do normal
3337 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3338 ;; ;; directory-sep-char here for XEmacs on Windows, which
3339 ;; ;; would otherwise use backslash.
3340 ;; (let ((directory-sep-char ?/))
3341 ;; (tramp-make-tramp-file-name
3342 ;; multi-method method user host
3343 ;; (tramp-drop-volume-letter
3344 ;; (tramp-run-real-handler 'expand-file-name
3345 ;; (list localname))))))))))
3349 (defvar tramp-async-proc nil
3350 "Global variable keeping asyncronous process object.
3351 Used in `tramp-handle-shell-command'")
3353 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3354 "Like `shell-command' for tramp files.
3355 This will break if COMMAND prints a newline, followed by the value of
3356 `tramp-end-of-output', followed by another newline."
3357 ;; Asynchronous processes are far from being perfect. But it works at least
3358 ;; for `find-grep-dired' and `find-name-dired' in Emacs 21.4.
3359 (if (tramp-tramp-file-p default-directory)
3360 (with-parsed-tramp-file-name default-directory nil
3361 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3363 (unless output-buffer
3367 "*Async Shell Command*"
3368 "*Shell Command Output*")))
3369 (set-buffer output-buffer)
3371 (unless (bufferp output-buffer)
3372 (setq output-buffer (current-buffer)))
3373 (set-buffer output-buffer)
3374 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3375 ;; process. Instead of, another asynchronous process is opened
3376 ;; which gets the output of the (synchronous) Tramp process
3377 ;; via process-filter. ERROR-BUFFER is disabled.
3379 (setq command (substring command 0 (match-beginning 0))
3381 tramp-async-proc (start-process (buffer-name output-buffer)
3382 output-buffer "cat")))
3384 (tramp-barf-unless-okay
3385 multi-method method user host
3386 (format "cd %s" (tramp-shell-quote-argument localname))
3388 "tramp-handle-shell-command: Couldn't `cd %s'"
3389 (tramp-shell-quote-argument localname))
3390 ;; Define the process filter
3394 (tramp-get-buffer multi-method method user host))
3395 '(lambda (process string)
3396 ;; Write the output into the Tramp Process
3397 (save-current-buffer
3398 (set-buffer (process-buffer process))
3399 (goto-char (point-max))
3401 ;; Hand-over output to asynchronous process.
3404 (regexp-quote tramp-end-of-output) string)))
3407 (substring string 0 (1- (match-beginning 0)))))
3408 (process-send-string tramp-async-proc string)
3410 (set-process-filter process nil)
3411 (process-send-eof tramp-async-proc))))))
3414 multi-method method user host
3416 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3418 (format "%s; tramp_old_status=$?" command)))
3419 (unless asynchronous
3420 (tramp-wait-for-output)))
3421 (unless asynchronous
3422 (insert-buffer (tramp-get-buffer multi-method method user host)))
3425 (unless (bufferp error-buffer)
3426 (setq error-buffer (get-buffer-create error-buffer)))
3428 multi-method method user host
3429 "cat /tmp/tramp.$$.err")
3430 (tramp-wait-for-output)
3431 (set-buffer error-buffer)
3432 (insert-buffer (tramp-get-buffer multi-method method user host))
3433 (tramp-send-command-and-check
3434 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3436 (tramp-send-command multi-method method user host "cd")
3437 (unless asynchronous
3438 (tramp-wait-for-output))
3440 multi-method method user host
3441 (concat "tramp_set_exit_status $tramp_old_status;"
3442 " echo tramp_exit_status $?"))
3443 (unless asynchronous
3444 (tramp-wait-for-output)
3445 (goto-char (point-max))
3446 (unless (search-backward "tramp_exit_status " nil t)
3447 (error "Couldn't find exit status of `%s'" command))
3448 (skip-chars-forward "^ ")
3449 (setq status (read (current-buffer)))))
3450 (unless (zerop (buffer-size))
3451 (display-buffer output-buffer))
3453 ;; The following is only executed if something strange was
3454 ;; happening. Emit a helpful message and do it anyway.
3455 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3457 (tramp-run-real-handler 'shell-command
3458 (list command output-buffer error-buffer))))
3462 (defsubst tramp-make-temp-file ()
3463 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3464 (expand-file-name tramp-temp-name-prefix
3465 (tramp-temporary-file-directory))))
3467 (defun tramp-handle-file-local-copy (filename)
3468 "Like `file-local-copy' for tramp files."
3469 (with-parsed-tramp-file-name filename nil
3470 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3471 ;; We used to bind the following as late as possible.
3472 ;; loc-enc and loc-dec were bound directly before the if
3473 ;; statement that checks them. But the functions
3474 ;; tramp-get-* might invoke the "are you awake" check in
3475 ;; tramp-maybe-open-connection, which is an unfortunate time
3476 ;; since we rely on the buffer contents at that spot.
3477 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3478 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3479 (loc-enc (tramp-get-local-encoding multi-method method user host))
3480 (loc-dec (tramp-get-local-decoding multi-method method user host))
3482 (unless (file-exists-p filename)
3483 (error "Cannot make local copy of non-existing file `%s'"
3485 (setq tmpfil (tramp-make-temp-file))
3487 (cond ((tramp-method-out-of-band-p multi-method method user host)
3488 ;; `copy-file' handles out-of-band methods
3489 (copy-file filename tmpfil t t))
3491 ((and rem-enc rem-dec)
3492 ;; Use inline encoding for file transfer.
3494 ;; Following line for setting tramp-current-method,
3495 ;; tramp-current-user, tramp-current-host.
3496 (set-buffer tramp-buf)
3497 (tramp-message 5 "Encoding remote file %s..." filename)
3498 (tramp-barf-unless-okay
3499 multi-method method user host
3500 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3502 "Encoding remote file failed, see buffer `%s' for details"
3504 ;; Remove trailing status code
3505 (goto-char (point-max))
3506 (delete-region (point) (progn (forward-line -1) (point)))
3508 (tramp-message 5 "Decoding remote file %s..." filename)
3510 ;; Here is where loc-enc and loc-dec used to be let-bound.
3511 (if (and (symbolp loc-dec) (fboundp loc-dec))
3512 ;; If local decoding is a function, we call it.
3513 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3516 (insert-buffer tramp-buf)
3517 (tramp-message-for-buffer
3518 multi-method method user host
3519 6 "Decoding remote file %s with function %s..."
3522 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3523 ;; says that we need to strip tramp_exit_status
3524 ;; line from the output here. Go to point-max,
3525 ;; search backward for tramp_exit_status, delete
3526 ;; between point and point-max if found.
3527 (let ((coding-system-for-write 'binary))
3528 (funcall loc-dec (point-min) (point-max))
3529 (write-region (point-min) (point-max) tmpfil))
3530 (kill-buffer tmpbuf))
3531 ;; If tramp-decoding-function is not defined for this
3532 ;; method, we invoke tramp-decoding-command instead.
3533 (let ((tmpfil2 (tramp-make-temp-file)))
3534 (write-region (point-min) (point-max) tmpfil2)
3536 6 "Decoding remote file %s with command %s..."
3538 (tramp-call-local-coding-command
3539 loc-dec tmpfil2 tmpfil)
3540 (delete-file tmpfil2)))
3541 (tramp-message-for-buffer
3542 multi-method method user host
3543 5 "Decoding remote file %s...done" filename)
3544 ;; Set proper permissions.
3545 (set-file-modes tmpfil (file-modes filename))))
3547 (t (error "Wrong method specification for `%s'" method)))
3550 (defun tramp-handle-file-remote-p (filename)
3551 "Like `file-remote-p' for tramp files."
3552 (when (tramp-tramp-file-p filename)
3553 (with-parsed-tramp-file-name filename nil
3554 (make-tramp-file-name
3555 :multi-method multi-method
3561 (defun tramp-handle-insert-file-contents
3562 (filename &optional visit beg end replace)
3563 "Like `insert-file-contents' for tramp files."
3564 (barf-if-buffer-read-only)
3565 (setq filename (expand-file-name filename))
3566 (with-parsed-tramp-file-name filename nil
3567 (if (not (file-exists-p filename))
3570 (setq buffer-file-name filename)
3571 (set-visited-file-modtime)
3572 (set-buffer-modified-p nil))
3574 (format "File `%s' not found on remote host" filename))
3575 (list (expand-file-name filename) 0))
3576 ;; `insert-file-contents-literally' takes care to avoid calling
3577 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3578 ;; propagate that care to the file-local-copy operation.
3580 (let ((inhibit-file-name-operation
3581 (when (eq inhibit-file-name-operation
3582 'insert-file-contents)
3584 (file-local-copy filename)))
3585 (coding-system-used nil)
3588 (setq buffer-file-name filename)
3589 (set-visited-file-modtime)
3590 (set-buffer-modified-p nil))
3591 (tramp-message-for-buffer
3592 multi-method method user host
3593 9 "Inserting local temp file `%s'..." local-copy)
3594 (setq result (insert-file-contents local-copy nil beg end replace))
3595 ;; Now `last-coding-system-used' has right value. Remember it.
3596 (when (boundp 'last-coding-system-used)
3597 (setq coding-system-used last-coding-system-used))
3598 (tramp-message-for-buffer
3599 multi-method method user host
3600 9 "Inserting local temp file `%s'...done" local-copy)
3601 (delete-file local-copy)
3602 (when (boundp 'last-coding-system-used)
3603 (setq last-coding-system-used coding-system-used))
3604 (list (expand-file-name filename)
3605 (second result))))))
3608 (defun tramp-handle-find-backup-file-name (filename)
3609 "Like `find-backup-file-name' for tramp files."
3610 (with-parsed-tramp-file-name filename nil
3611 ;; We set both variables. It doesn't matter whether it is
3613 (let ((backup-directory-alist
3615 (when (boundp 'backup-directory-alist)
3616 (if (boundp 'tramp-backup-directory-alist)
3621 (if (and (stringp (cdr x))
3622 (file-name-absolute-p (cdr x))
3623 (not (tramp-file-name-p (cdr x))))
3624 (tramp-make-tramp-file-name
3625 multi-method method user host (cdr x))
3627 (symbol-value 'tramp-backup-directory-alist))
3628 (symbol-value 'backup-directory-alist))))
3630 (bkup-backup-directory-info
3632 (when (boundp 'bkup-backup-directory-info)
3633 (if (boundp 'tramp-bkup-backup-directory-info)
3639 (if (and (stringp (car (cdr x)))
3640 (file-name-absolute-p (car (cdr x)))
3641 (not (tramp-file-name-p (car (cdr x)))))
3642 (tramp-make-tramp-file-name
3643 multi-method method user host (car (cdr x)))
3646 (symbol-value 'tramp-bkup-backup-directory-info))
3647 (symbol-value 'bkup-backup-directory-info)))))
3649 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3652 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3653 (defun tramp-handle-write-region
3654 (start end filename &optional append visit lockname confirm)
3655 "Like `write-region' for tramp files."
3656 (unless (eq append nil)
3657 (error "Cannot append to file using tramp (`%s')" filename))
3658 (setq filename (expand-file-name filename))
3659 ;; Following part commented out because we don't know what to do about
3660 ;; file locking, and it does not appear to be a problem to ignore it.
3661 ;; Ange-ftp ignores it, too.
3662 ;; (when (and lockname (stringp lockname))
3663 ;; (setq lockname (expand-file-name lockname)))
3664 ;; (unless (or (eq lockname nil)
3665 ;; (string= lockname filename))
3667 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3668 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
3669 (when (and (not (featurep 'xemacs))
3670 confirm (file-exists-p filename))
3671 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3673 (error "File not overwritten")))
3674 (with-parsed-tramp-file-name filename nil
3675 (let ((curbuf (current-buffer))
3676 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3677 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3678 (loc-enc (tramp-get-local-encoding multi-method method user host))
3679 (loc-dec (tramp-get-local-decoding multi-method method user host))
3680 (trampbuf (get-buffer-create "*tramp output*"))
3681 (modes (file-modes filename))
3682 ;; We use this to save the value of `last-coding-system-used'
3683 ;; after writing the tmp file. At the end of the function,
3684 ;; we set `last-coding-system-used' to this saved value.
3685 ;; This way, any intermediary coding systems used while
3686 ;; talking to the remote shell or suchlike won't hose this
3687 ;; variable. This approach was snarfed from ange-ftp.el.
3690 ;; Write region into a tmp file. This isn't really needed if we
3691 ;; use an encoding function, but currently we use it always
3692 ;; because this makes the logic simpler.
3693 (setq tmpfil (tramp-make-temp-file))
3694 ;; Set current buffer. If connection wasn't open, `file-modes' has
3695 ;; changed it accidently.
3697 ;; We say `no-message' here because we don't want the visited file
3698 ;; modtime data to be clobbered from the temp file. We call
3699 ;; `set-visited-file-modtime' ourselves later on.
3700 (tramp-run-real-handler
3702 (if confirm ; don't pass this arg unless defined for backward compat.
3703 (list start end tmpfil append 'no-message lockname confirm)
3704 (list start end tmpfil append 'no-message lockname)))
3705 ;; The permissions of the temporary file should be set. If
3706 ;; filename does not exist (eq modes nil) it has been renamed to
3707 ;; the backup file. This case `save-buffer' handles
3709 (when modes (set-file-modes tmpfil modes))
3710 ;; Now, `last-coding-system-used' has the right value. Remember it.
3711 (when (boundp 'last-coding-system-used)
3712 (setq coding-system-used last-coding-system-used))
3713 ;; This is a bit lengthy due to the different methods possible for
3714 ;; file transfer. First, we check whether the method uses an rcp
3715 ;; program. If so, we call it. Otherwise, both encoding and
3716 ;; decoding command must be specified. However, if the method
3717 ;; _also_ specifies an encoding function, then that is used for
3718 ;; encoding the contents of the tmp file.
3719 (cond ((tramp-method-out-of-band-p multi-method method user host)
3720 ;; `copy-file' handles out-of-band methods
3721 (copy-file tmpfil filename t t))
3723 ((and rem-enc rem-dec)
3724 ;; Use inline file transfer
3725 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
3727 ;; Encode tmpfil into tmpbuf
3728 (tramp-message-for-buffer multi-method method user host
3729 5 "Encoding region...")
3732 ;; Use encoding function or command.
3733 (if (and (symbolp loc-enc) (fboundp loc-enc))
3735 (tramp-message-for-buffer
3736 multi-method method user host
3737 6 "Encoding region using function `%s'..."
3738 (symbol-name loc-enc))
3739 (insert-file-contents-literally tmpfil)
3740 ;; CCC. The following `let' is a workaround for
3741 ;; the base64.el that comes with pgnus-0.84. If
3742 ;; both of the following conditions are
3743 ;; satisfied, it tries to write to a local file
3744 ;; in default-directory, but at this point,
3745 ;; default-directory is remote.
3746 ;; (CALL-PROCESS-REGION can't write to remote
3747 ;; files, it seems.) The file in question is a
3749 (let ((default-directory
3750 (tramp-temporary-file-directory)))
3751 (funcall loc-enc (point-min) (point-max)))
3752 (goto-char (point-max))
3755 (tramp-message-for-buffer
3756 multi-method method user host
3757 6 "Encoding region using command `%s'..." loc-enc)
3758 (unless (equal 0 (tramp-call-local-coding-command
3760 (pop-to-buffer trampbuf)
3761 (error (concat "Cannot write to `%s', local encoding"
3762 " command `%s' failed")
3764 ;; Send tmpbuf into remote decoding command which
3765 ;; writes to remote file. Because this happens on the
3766 ;; remote host, we cannot use the function.
3767 (tramp-message-for-buffer
3768 multi-method method user host
3769 5 "Decoding region into remote file %s..." filename)
3771 multi-method method user host
3772 (format "%s >%s <<'EOF'"
3774 (tramp-shell-quote-argument localname)))
3776 (tramp-message-for-buffer
3777 multi-method method user host
3778 6 "Sending data to remote host...")
3779 (tramp-send-string multi-method method user host
3781 ;; wait for remote decoding to complete
3782 (tramp-message-for-buffer
3783 multi-method method user host
3784 6 "Sending end of data token...")
3786 multi-method method user host "EOF" nil t)
3787 (tramp-message-for-buffer
3788 multi-method method user host 6
3789 "Waiting for remote host to process data...")
3790 (set-buffer (tramp-get-buffer multi-method method user host))
3791 (tramp-wait-for-output)
3792 (tramp-barf-unless-okay
3793 multi-method method user host nil nil 'file-error
3794 (concat "Couldn't write region to `%s',"
3795 " decode using `%s' failed")
3797 (tramp-message 5 "Decoding region into remote file %s...done"
3799 (kill-buffer tmpbuf))))
3802 (concat "Method `%s' should specify both encoding and "
3803 "decoding command or an rcp program")
3805 (delete-file tmpfil)
3806 (unless (equal curbuf (current-buffer))
3807 (error "Buffer has changed from `%s' to `%s'"
3808 curbuf (current-buffer)))
3810 (set-visited-file-modtime))
3811 ;; Make `last-coding-system-used' have the right value.
3812 (when (boundp 'last-coding-system-used)
3813 (setq last-coding-system-used coding-system-used))
3814 (when (or (eq visit t)
3817 (message "Wrote %s" filename)))))
3819 ;; Call down to the real handler.
3820 ;; Because EFS does not play nicely with TRAMP (both systems match a
3821 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
3824 ;; Other than that, this is the canon file-handler code that the doco
3825 ;; says should be used here. Which is nice.
3827 ;; Under XEmacs current, EFS also hooks in as
3828 ;; efs-sifn-handler-function to handle any filename with environment
3829 ;; variables. This has two implications:
3830 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
3831 ;; 2) That TRAMP might want to do the same thing.
3832 ;; Details as they come in.
3834 ;; Daniel Pittman <daniel@danann.net>
3836 ;; (defun tramp-run-real-handler (operation args)
3837 ;; "Invoke normal file name handler for OPERATION.
3838 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
3839 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
3841 ;; (let ((inhibit-file-name-handlers
3842 ;; (list 'tramp-file-name-handler
3843 ;; 'efs-file-handler-function
3844 ;; 'ange-ftp-hook-function
3845 ;; (and (eq inhibit-file-name-operation operation)
3846 ;; inhibit-file-name-handlers)))
3847 ;; (inhibit-file-name-operation operation))
3848 ;; (apply operation args)))
3850 (defun tramp-run-real-handler (operation args)
3851 "Invoke normal file name handler for OPERATION.
3852 First arg specifies the OPERATION, second arg is a list of arguments to
3853 pass to the OPERATION."
3854 (let* ((inhibit-file-name-handlers
3855 `(tramp-file-name-handler
3856 tramp-completion-file-name-handler
3857 cygwin-mount-name-hook-function
3858 cygwin-mount-map-drive-hook-function
3860 ,(and (eq inhibit-file-name-operation operation)
3861 inhibit-file-name-handlers)))
3862 (inhibit-file-name-operation operation))
3863 (apply operation args)))
3865 ;; This function is used from `tramp-completion-file-name-handler' functions
3866 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
3867 ;; because the check is based on a full filename, not available for all
3868 ;; basic I/O operations.
3869 (defun tramp-completion-run-real-handler (operation args)
3870 "Invoke `tramp-file-name-handler' for OPERATION.
3871 First arg specifies the OPERATION, second arg is a list of arguments to
3872 pass to the OPERATION."
3873 (let* ((inhibit-file-name-handlers
3874 `(tramp-completion-file-name-handler
3875 cygwin-mount-name-hook-function
3876 cygwin-mount-map-drive-hook-function
3878 ,(and (eq inhibit-file-name-operation operation)
3879 inhibit-file-name-handlers)))
3880 (inhibit-file-name-operation operation))
3881 (apply operation args)))
3883 ;; We handle here all file primitives. Most of them have the file
3884 ;; name as first parameter; nevertheless we check for them explicitly
3885 ;; in order to be signalled if a new primitive appears. This
3886 ;; scenario is needed because there isn't a way to decide by
3887 ;; syntactical means whether a foreign method must be called. It would
3888 ;; ease the life if `file-name-handler-alist' would support a decision
3889 ;; function as well but regexp only.
3890 (defun tramp-file-name-for-operation (operation &rest args)
3891 "Return file name related to OPERATION file primitive.
3892 ARGS are the arguments OPERATION has been called with."
3894 ; FILE resp DIRECTORY
3896 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
3897 'delete-file 'diff-latest-backup-file 'directory-file-name
3898 'directory-files 'directory-files-and-attributes
3899 'dired-compress-file 'dired-uncache
3900 'file-accessible-directory-p 'file-attributes
3901 'file-directory-p 'file-executable-p 'file-exists-p
3902 'file-local-copy 'file-remote-p 'file-modes
3903 'file-name-as-directory 'file-name-directory
3904 'file-name-nondirectory 'file-name-sans-versions
3905 'file-ownership-preserved-p 'file-readable-p
3906 'file-regular-p 'file-symlink-p 'file-truename
3907 'file-writable-p 'find-backup-file-name 'find-file-noselect
3908 'get-file-buffer 'insert-directory 'insert-file-contents
3909 'load 'make-directory 'make-directory-internal
3910 'set-file-modes 'substitute-in-file-name
3911 'unhandled-file-name-directory 'vc-registered
3913 'abbreviate-file-name 'create-file-buffer
3914 'dired-file-modtime 'dired-make-compressed-filename
3915 'dired-recursive-delete-directory 'dired-set-file-modtime
3916 'dired-shell-unhandle-file-name 'dired-uucode-file
3917 'insert-file-contents-literally 'recover-file
3918 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
3919 (if (file-name-absolute-p (nth 0 args))
3921 (expand-file-name (nth 0 args))))
3922 ; FILE DIRECTORY resp FILE1 FILE2
3924 (list 'add-name-to-file 'copy-file 'expand-file-name
3925 'file-name-all-completions 'file-name-completion
3926 'file-newer-than-file-p 'make-symbolic-link 'rename-file
3928 'dired-make-relative-symlink
3929 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
3932 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
3933 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
3934 (t (buffer-file-name (current-buffer))))))
3936 ((eq operation 'write-region)
3940 (list 'set-visited-file-modtime 'verify-visited-file-modtime
3944 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
3947 (list 'dired-call-process 'shell-command
3949 'dired-print-file 'dired-shell-call-process))
3951 ; unknown file primitive
3952 (t (error "unknown file I/O primitive: %s" operation))))
3954 (defun tramp-find-foreign-file-name-handler (filename)
3955 "Return foreign file name handler if exists."
3956 (when (tramp-tramp-file-p filename)
3959 (handler-alist tramp-foreign-file-name-handler-alist))
3960 (while handler-alist
3961 (setq elt (car handler-alist)
3962 handler-alist (cdr handler-alist))
3963 (when (funcall (car elt) filename)
3964 (setq handler-alist nil)
3965 (setq res (cdr elt))))
3970 (defun tramp-file-name-handler (operation &rest args)
3971 "Invoke Tramp file name handler.
3972 Falls back to normal file name handler if no tramp file name handler exists."
3974 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
3975 (foreign (tramp-find-foreign-file-name-handler filename)))
3977 (foreign (apply foreign operation args))
3978 (t (tramp-run-real-handler operation args))))))
3981 ;; In Emacs, there is some concurrency due to timers. If a timer
3982 ;; interrupts Tramp and wishes to use the same connection buffer as
3983 ;; the "main" Emacs, then garbage might occur in the connection
3984 ;; buffer. Therefore, we need to make sure that a timer does not use
3985 ;; the same connection buffer as the "main" Emacs. We implement a
3986 ;; cheap global lock, instead of locking each connection buffer
3987 ;; separately. The global lock is based on two variables,
3988 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
3989 ;; (with setq) to indicate a lock. But Tramp also calls itself during
3990 ;; processing of a single file operation, so we need to allow
3991 ;; recursive calls. That's where the `tramp-locker' variable comes in
3992 ;; -- it is let-bound to t during the execution of the current
3993 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
3994 ;; then we should just proceed because we have been called
3995 ;; recursively. But if `tramp-locker' is nil, then we are a timer
3996 ;; interrupting the "main" Emacs, and then we signal an error.
3998 (defvar tramp-locked nil
3999 "If non-nil, then Tramp is currently busy.
4000 Together with `tramp-locker', this implements a locking mechanism
4001 preventing reentrant calls of Tramp.")
4003 (defvar tramp-locker nil
4004 "If non-nil, then a caller has locked Tramp.
4005 Together with `tramp-locked', this implements a locking mechanism
4006 preventing reentrant calls of Tramp.")
4008 (defun tramp-sh-file-name-handler (operation &rest args)
4009 "Invoke remote-shell Tramp file name handler.
4010 Fall back to normal file name handler if no Tramp handler exists."
4011 (when (and tramp-locked (not tramp-locker))
4012 (signal 'file-error "Forbidden reentrant call of Tramp"))
4013 (let ((tl tramp-locked))
4016 (setq tramp-locked t)
4017 (let ((tramp-locker t))
4019 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4021 (apply (cdr fn) args)
4022 (tramp-run-real-handler operation args))))))
4023 (setq tramp-locked tl))))
4026 (defun tramp-completion-file-name-handler (operation &rest args)
4027 "Invoke tramp file name completion handler.
4028 Falls back to normal file name handler if no tramp file name handler exists."
4029 ;; (setq tramp-debug-buffer t)
4030 ;; (tramp-message 1 "%s %s" operation args)
4031 ;; (tramp-message 1 "%s %s\n%s"
4032 ;; operation args (with-output-to-string (backtrace)))
4033 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4035 (save-match-data (apply (cdr fn) args))
4036 (tramp-completion-run-real-handler operation args))))
4039 (put 'tramp-completion-file-name-handler 'safe-magic t)
4041 ;; Register in file name handler alist
4043 (add-to-list 'file-name-handler-alist
4044 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4045 (add-to-list 'file-name-handler-alist
4046 (cons tramp-completion-file-name-regexp
4047 'tramp-completion-file-name-handler))
4049 (defun tramp-repair-jka-compr ()
4050 "If jka-compr is already loaded, move it to the front of
4051 `file-name-handler-alist'. On Emacs 21.4 or so this will not be
4053 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4055 (setq file-name-handler-alist
4056 (cons jka (delete jka file-name-handler-alist))))))
4057 (tramp-repair-jka-compr)
4060 ;;; Interactions with other packages:
4062 ;; -- complete.el --
4064 ;; This function contributed by Ed Sabol
4065 (defun tramp-handle-expand-many-files (name)
4066 "Like `PC-expand-many-files' for tramp files."
4067 (with-parsed-tramp-file-name name nil
4069 (if (or (string-match "\\*" name)
4070 (string-match "\\?" name)
4071 (string-match "\\[.*\\]" name))
4074 ;; CCC: To do it right, we should quote certain characters
4075 ;; in the file name, but since the echo command is going to
4076 ;; break anyway when there are spaces in the file names, we
4078 ;;-(let ((comint-file-name-quote-list
4079 ;;- (set-difference tramp-file-name-quote-list
4080 ;;- '(?\* ?\? ?[ ?]))))
4081 ;;- (tramp-send-command
4082 ;;- multi-method method user host
4083 ;;- (format "echo %s" (comint-quote-filename localname)))
4084 ;;- (tramp-wait-for-output))
4085 (tramp-send-command multi-method method user host
4086 (format "echo %s" localname))
4087 (tramp-wait-for-output)
4088 (setq bufstr (buffer-substring (point-min)
4089 (tramp-line-end-position)))
4090 (goto-char (point-min))
4091 (if (string-equal localname bufstr)
4094 (while (search-forward " " nil t)
4095 (delete-backward-char 1)
4097 (goto-char (point-max))
4098 (delete-backward-char 1)
4100 (goto-char (point-min))
4102 (function (lambda (x)
4103 (tramp-make-tramp-file-name multi-method method
4105 (read (current-buffer))))))
4106 (list (expand-file-name name))))))
4108 ;; Check for complete.el and override PC-expand-many-files if appropriate.
4110 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
4112 (defun tramp-setup-complete ()
4113 (fset 'tramp-save-PC-expand-many-files
4114 (symbol-function 'PC-expand-many-files))
4115 (defun PC-expand-many-files (name)
4116 (if (tramp-tramp-file-p name)
4117 (expand-many-files name)
4118 (tramp-save-PC-expand-many-files name))))
4120 ;; Why isn't eval-after-load sufficient?
4121 (if (fboundp 'PC-expand-many-files)
4122 (tramp-setup-complete)
4123 (eval-after-load "complete" '(tramp-setup-complete)))
4125 ;;; File name handler functions for completion mode
4127 ;; Necessary because `tramp-file-name-regexp-unified' and
4128 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4129 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4130 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4131 ;; Using `last-input-event' is a little bit risky, because completing a file
4132 ;; might require loading other files, like "~/.netrc", and for them it
4133 ;; shouldn't be decided based on that variable. On the other hand, those files
4134 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4135 ;; be introduced overwriting this check in such cases. Or we change tramp
4136 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4137 ;; In case of non unified file names it can be always true (and wouldn't be
4138 ;; necessary, because there are different regexp).
4139 (defun tramp-completion-mode (file)
4140 "Checks whether method / user name / host name completion is active."
4142 ((not tramp-unified-filenames) t)
4143 ((string-match "^/.*:.*:$" file) nil)
4145 (concat tramp-prefix-regexp
4146 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4148 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4149 ((or (equal last-input-event 'tab)
4151 (and (integerp last-input-event)
4152 (not (event-modifiers last-input-event))
4153 (or (char-equal last-input-event ?\?)
4154 (char-equal last-input-event ?\t) ; handled by 'tab already?
4155 (char-equal last-input-event ?\ )))
4157 (and (featurep 'xemacs)
4158 (not (event-modifiers last-input-event))
4160 (funcall (symbol-function 'event-to-character)
4161 last-input-event) ?\?)
4163 (funcall (symbol-function 'event-to-character)
4164 last-input-event) ?\t)
4166 (funcall (symbol-function 'event-to-character)
4167 last-input-event) ?\ ))))
4170 (defun tramp-completion-handle-file-exists-p (filename)
4171 "Like `file-exists-p' for tramp files."
4172 (if (tramp-completion-mode filename)
4173 (tramp-run-real-handler
4174 'file-exists-p (list filename))
4175 (tramp-completion-run-real-handler
4176 'file-exists-p (list filename))))
4178 ;; Localname manipulation in case of partial TRAMP file names.
4179 (defun tramp-completion-handle-file-name-directory (file)
4180 "Like `file-name-directory' but aware of TRAMP files."
4181 (if (tramp-completion-mode file)
4183 (tramp-completion-run-real-handler
4184 'file-name-directory (list file))))
4186 ;; Localname manipulation in case of partial TRAMP file names.
4187 (defun tramp-completion-handle-file-name-nondirectory (file)
4188 "Like `file-name-nondirectory' but aware of TRAMP files."
4190 file (length (tramp-completion-handle-file-name-directory file))))
4192 ;; Method, host name and user name completion.
4193 ;; `tramp-completion-dissect-file-name' returns a list of
4194 ;; tramp-file-name structures. For all of them we return possible completions.
4195 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4196 "Like `file-name-all-completions' for partial tramp files."
4199 ((fullname (concat directory filename))
4202 (if (tramp-completion-mode fullname)
4203 (tramp-run-real-handler
4204 'file-name-all-completions (list filename directory))
4205 (tramp-completion-run-real-handler
4206 'file-name-all-completions (list filename directory))))
4207 ;; possible completion structures
4208 (v (tramp-completion-dissect-file-name fullname)))
4211 (let* ((car (car v))
4212 (multi-method (tramp-file-name-multi-method car))
4213 (method (tramp-file-name-method car))
4214 (user (tramp-file-name-user car))
4215 (host (tramp-file-name-host car))
4216 (localname (tramp-file-name-localname car))
4217 (m (tramp-find-method multi-method method user host))
4218 (tramp-current-user user) ; see `tramp-parse-passwd'
4221 (unless (or multi-method ;; Not handled (yet).
4222 localname) ;; Nothing to complete
4226 ;; Method dependent user / host combinations
4230 (setq all-user-hosts
4231 (append all-user-hosts
4232 (funcall (nth 0 x) (nth 1 x)))))
4233 (tramp-get-completion-function m))
4235 (setq result (append result
4238 (tramp-get-completion-user-host
4239 method user host (nth 0 x) (nth 1 x)))
4240 (delq nil all-user-hosts)))))
4244 (append result (tramp-get-completion-methods m)))))
4246 (setq v (delq car v))))
4248 ;;; unify list, remove nil elements
4251 (let ((car (car result)))
4252 (when car (add-to-list 'result1 car))
4253 (setq result (delq car result))))
4257 ;; Method, host name and user name completion for a file.
4258 (defun tramp-completion-handle-file-name-completion (filename directory)
4259 "Like `file-name-completion' for tramp files."
4260 (try-completion filename
4261 (mapcar 'list (file-name-all-completions filename directory))))
4263 ;; I misuse a little bit the tramp-file-name structure in order to handle
4264 ;; completion possibilities for partial methods / user names / host names.
4265 ;; Return value is a list of tramp-file-name structures according to possible
4266 ;; completions. If "multi-method" or "localname" is non-nil it means there
4267 ;; shouldn't be a completion anymore.
4269 ;; Expected results:
4271 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4272 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4273 ;; [nil nil "x" nil nil]
4274 ;; [nil "x" nil nil nil]
4276 ;; "/x:" "/x:y" "/x:y:"
4277 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4279 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4280 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4282 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4283 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4284 ;; "/[x/y@" "/[x/y@z"
4285 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4286 (defun tramp-completion-dissect-file-name (name)
4287 "Returns a list of `tramp-file-name' structures.
4288 They are collected by `tramp-completion-dissect-file-name1'."
4292 ;; "/method" "/[method"
4293 (tramp-completion-file-name-structure1
4294 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4297 (tramp-completion-file-name-structure2
4298 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4301 (tramp-completion-file-name-structure3
4302 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4304 ;; "/user@host" "/[user@host"
4305 (tramp-completion-file-name-structure4
4306 (list (concat tramp-prefix-regexp
4307 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4308 "\\(" tramp-host-regexp x-nil "\\)$")
4310 ;; "/method:user" "/[method/user"
4311 (tramp-completion-file-name-structure5
4312 (list (concat tramp-prefix-regexp
4313 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4314 "\\(" tramp-user-regexp x-nil "\\)$")
4316 ;; "/method:host" "/[method/host"
4317 (tramp-completion-file-name-structure6
4318 (list (concat tramp-prefix-regexp
4319 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4320 "\\(" tramp-host-regexp x-nil "\\)$")
4322 ;; "/method:user@host" "/[method/user@host"
4323 (tramp-completion-file-name-structure7
4324 (list (concat tramp-prefix-regexp
4325 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4326 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4327 "\\(" tramp-host-regexp x-nil "\\)$")
4330 (mapcar (lambda (regexp)
4331 (add-to-list 'result
4332 (tramp-completion-dissect-file-name1 regexp name)))
4334 tramp-completion-file-name-structure1
4335 tramp-completion-file-name-structure2
4336 tramp-completion-file-name-structure3
4337 tramp-completion-file-name-structure4
4338 tramp-completion-file-name-structure5
4339 tramp-completion-file-name-structure6
4340 tramp-completion-file-name-structure7
4341 tramp-file-name-structure))
4345 (defun tramp-completion-dissect-file-name1 (structure name)
4346 "Returns a `tramp-file-name' structure matching STRUCTURE.
4347 The structure consists of multi-method, remote method, remote user,
4348 remote host and localname (filename on remote host)."
4352 (when (string-match (nth 0 structure) name)
4353 (setq method (and (nth 1 structure)
4354 (match-string (nth 1 structure) name)))
4355 (if (and method (member method tramp-multi-methods))
4356 ;; Not handled (yet).
4357 (make-tramp-file-name
4358 :multi-method method
4363 (let ((user (and (nth 2 structure)
4364 (match-string (nth 2 structure) name)))
4365 (host (and (nth 3 structure)
4366 (match-string (nth 3 structure) name)))
4367 (localname (and (nth 4 structure)
4368 (match-string (nth 4 structure) name))))
4369 (make-tramp-file-name
4374 :localname localname)))))))
4376 ;; This function returns all possible method completions, adding the
4377 ;; trailing method delimeter.
4378 (defun tramp-get-completion-methods (partial-method)
4379 "Returns all method completions for PARTIAL-METHOD."
4383 (string-match (concat "^" (regexp-quote partial-method)) method)
4384 ;; we must remove leading "/".
4385 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4386 (delete "multi" (mapcar 'car tramp-methods))))
4388 ;; Compares partial user and host names with possible completions.
4389 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4390 "Returns the most expanded string for user and host name completion.
4391 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4394 ((and partial-user partial-host)
4396 (string-match (concat "^" (regexp-quote partial-host)) host)
4397 (string-equal partial-user (or user partial-user)))
4398 (setq user partial-user)
4405 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4411 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4417 (unless (zerop (+ (length user) (length host)))
4418 ;; we must remove leading "/".
4419 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4421 (defun tramp-parse-rhosts (filename)
4422 "Return a list of (user host) tuples allowed to access.
4423 Either user or host may be nil."
4426 (when (file-readable-p filename)
4428 (insert-file-contents filename)
4429 (goto-char (point-min))
4431 (push (tramp-parse-rhosts-group) res))))
4434 ;; Taken from gnus/netrc.el
4436 (defalias 'tramp-point-at-eol
4437 (if (fboundp 'point-at-eol)
4439 'line-end-position)))
4441 (defun tramp-parse-rhosts-group ()
4442 "Return a (user host) tuple allowed to access.
4443 Either user or host may be nil."
4448 "^\\(" tramp-host-regexp "\\)"
4449 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4451 (narrow-to-region (point) (tramp-point-at-eol))
4452 (when (re-search-forward regexp nil t)
4453 (setq result (append (list (match-string 3) (match-string 1)))))
4458 (defun tramp-parse-shosts (filename)
4459 "Return a list of (user host) tuples allowed to access.
4460 User is always nil."
4463 (when (file-readable-p filename)
4465 (insert-file-contents filename)
4466 (goto-char (point-min))
4468 (push (tramp-parse-shosts-group) res))))
4471 (defun tramp-parse-shosts-group ()
4472 "Return a (user host) tuple allowed to access.
4473 User is always nil."
4476 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4478 (narrow-to-region (point) (tramp-point-at-eol))
4479 (when (re-search-forward regexp nil t)
4480 (setq result (list nil (match-string 1))))
4483 (> (skip-chars-forward ",") 0)
4487 (defun tramp-parse-sconfig (filename)
4488 "Return a list of (user host) tuples allowed to access.
4489 User is always nil."
4492 (when (file-readable-p filename)
4494 (insert-file-contents filename)
4495 (goto-char (point-min))
4497 (push (tramp-parse-sconfig-group) res))))
4500 (defun tramp-parse-sconfig-group ()
4501 "Return a (user host) tuple allowed to access.
4502 User is always nil."
4505 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4507 (narrow-to-region (point) (tramp-point-at-eol))
4508 (when (re-search-forward regexp nil t)
4509 (setq result (list nil (match-string 1))))
4512 (> (skip-chars-forward ",") 0)
4516 (defun tramp-parse-shostkeys (dirname)
4517 "Return a list of (user host) tuples allowed to access.
4518 User is always nil."
4520 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4521 (files (when (file-directory-p dirname) (directory-files dirname)))
4525 (when (string-match regexp (car files))
4526 (push (list nil (match-string 1 (car files))) result))
4527 (setq files (cdr files)))
4530 (defun tramp-parse-sknownhosts (dirname)
4531 "Return a list of (user host) tuples allowed to access.
4532 User is always nil."
4534 (let ((regexp (concat "^\\(" tramp-host-regexp
4535 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4536 (files (when (file-directory-p dirname) (directory-files dirname)))
4540 (when (string-match regexp (car files))
4541 (push (list nil (match-string 1 (car files))) result))
4542 (setq files (cdr files)))
4545 (defun tramp-parse-hosts (filename)
4546 "Return a list of (user host) tuples allowed to access.
4547 User is always nil."
4550 (when (file-readable-p filename)
4552 (insert-file-contents filename)
4553 (goto-char (point-min))
4555 (push (tramp-parse-hosts-group) res))))
4558 (defun tramp-parse-hosts-group ()
4559 "Return a (user host) tuple allowed to access.
4560 User is always nil."
4563 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4565 (narrow-to-region (point) (tramp-point-at-eol))
4566 (when (re-search-forward regexp nil t)
4567 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4568 (setq result (list nil (match-string 1)))))
4571 (> (skip-chars-forward " \t") 0)
4575 ;; For su-alike methods it would be desirable to return "root@localhost"
4576 ;; as default. Unfortunately, we have no information whether any user name
4577 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4578 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4579 (defun tramp-parse-passwd (filename)
4580 "Return a list of (user host) tuples allowed to access.
4581 Host is always \"localhost\"."
4584 (if (zerop (length tramp-current-user))
4586 (when (file-readable-p filename)
4588 (insert-file-contents filename)
4589 (goto-char (point-min))
4591 (push (tramp-parse-passwd-group) res))))
4594 (defun tramp-parse-passwd-group ()
4595 "Return a (user host) tuple allowed to access.
4596 Host is always \"localhost\"."
4599 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4601 (narrow-to-region (point) (tramp-point-at-eol))
4602 (when (re-search-forward regexp nil t)
4603 (setq result (list (match-string 1) "localhost")))
4608 (defun tramp-parse-netrc (filename)
4609 "Return a list of (user host) tuples allowed to access.
4613 (when (file-readable-p filename)
4615 (insert-file-contents filename)
4616 (goto-char (point-min))
4618 (push (tramp-parse-netrc-group) res))))
4621 (defun tramp-parse-netrc-group ()
4622 "Return a (user host) tuple allowed to access.
4628 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4629 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4631 (narrow-to-region (point) (tramp-point-at-eol))
4632 (when (re-search-forward regexp nil t)
4633 (setq result (list (match-string 3) (match-string 1))))
4638 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4639 "Like `expand-file-name' for tramp files."
4640 (let ((fullname (concat (or dir default-directory) name)))
4641 (tramp-drop-volume-letter
4642 (if (tramp-completion-mode fullname)
4643 (tramp-run-real-handler
4644 'expand-file-name (list name dir))
4645 (tramp-completion-run-real-handler
4646 'expand-file-name (list name dir))))))
4648 ;;; Internal Functions:
4650 (defun tramp-set-auto-save ()
4651 (when (and (buffer-file-name)
4652 (tramp-tramp-file-p (buffer-file-name))
4654 (auto-save-mode 1)))
4655 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4657 (defun tramp-run-test (switch filename)
4658 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4659 Returns the exit code of the `test' program."
4660 (let ((v (tramp-dissect-file-name filename)))
4662 (tramp-send-command-and-check
4663 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4664 (tramp-file-name-user v) (tramp-file-name-host v)
4665 (format "test %s %s" switch
4666 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4668 (defun tramp-run-test2 (program file1 file2 &optional switch)
4669 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4670 The optional SWITCH is inserted between the two files.
4671 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4672 hosts, or files, disagree."
4673 (let* ((v1 (tramp-dissect-file-name file1))
4674 (v2 (tramp-dissect-file-name file2))
4675 (mmethod1 (tramp-file-name-multi-method v1))
4676 (mmethod2 (tramp-file-name-multi-method v2))
4677 (method1 (tramp-file-name-method v1))
4678 (method2 (tramp-file-name-method v2))
4679 (user1 (tramp-file-name-user v1))
4680 (user2 (tramp-file-name-user v2))
4681 (host1 (tramp-file-name-host v1))
4682 (host2 (tramp-file-name-host v2))
4683 (localname1 (tramp-file-name-localname v1))
4684 (localname2 (tramp-file-name-localname v2)))
4685 (unless (and method1 method2 host1 host2
4686 (equal mmethod1 mmethod2)
4687 (equal method1 method2)
4689 (equal host1 host2))
4690 (error "tramp-run-test2: %s"
4691 "only implemented for same method, same user, same host"))
4693 (tramp-send-command-and-check
4694 mmethod1 method1 user1 host1
4695 (format "%s %s %s %s"
4697 (tramp-shell-quote-argument localname1)
4699 (tramp-shell-quote-argument localname2))))))
4701 (defun tramp-touch (file time)
4702 "Set the last-modified timestamp of the given file.
4703 TIME is an Emacs internal time value as returned by `current-time'."
4704 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
4705 (if (tramp-tramp-file-p file)
4706 (with-parsed-tramp-file-name file nil
4707 (let ((buf (tramp-get-buffer multi-method method user host)))
4708 (unless (zerop (tramp-send-command-and-check
4709 multi-method method user host
4710 (format "touch -t %s %s"
4714 (error "tramp-touch: touch failed, see buffer `%s' for details"
4716 ;; It's a local file
4718 (unless (zerop (call-process
4719 "touch" nil (current-buffer) nil "-t" touch-time file))
4720 (pop-to-buffer (current-buffer))
4721 (error "tramp-touch: touch failed"))))))
4723 (defun tramp-buffer-name (multi-method method user host)
4724 "A name for the connection buffer for USER at HOST using METHOD."
4726 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
4727 (let ((method (tramp-find-method multi-method method user host)))
4729 (format "*tramp/%s %s@%s*" method user host)
4730 (format "*tramp/%s %s*" method host)))))
4732 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
4733 "A name for the multi method connection buffer.
4734 MULTI-METHOD gives the multi method, METHOD the array of methods,
4735 USER the array of user names, HOST the array of host names."
4736 (unless (and (= (length method) (length user))
4737 (= (length method) (length host)))
4738 (error "Syntax error in multi method (implementation error)"))
4739 (let ((len (length method))
4744 (cons (if (aref user i)
4745 (format "%s#%s@%s:" (aref method i)
4746 (aref user i) (aref host i))
4747 (format "%s@%s:" (aref method i) (aref host i)))
4750 (format "*%s/%s %s*"
4752 (apply 'concat (reverse string-list)))))
4754 (defun tramp-get-buffer (multi-method method user host)
4755 "Get the connection buffer to be used for USER at HOST using METHOD."
4756 (get-buffer-create (tramp-buffer-name multi-method method user host)))
4758 (defun tramp-debug-buffer-name (multi-method method user host)
4759 "A name for the debug buffer for USER at HOST using METHOD."
4761 (tramp-buffer-name-multi-method "debug tramp"
4762 multi-method method user host)
4763 (let ((method (tramp-find-method multi-method method user host)))
4765 (format "*debug tramp/%s %s@%s*" method user host)
4766 (format "*debug tramp/%s %s*" method host)))))
4768 (defun tramp-get-debug-buffer (multi-method method user host)
4769 "Get the debug buffer for USER at HOST using METHOD."
4770 (get-buffer-create (tramp-debug-buffer-name multi-method method user host)))
4772 (defun tramp-find-executable (multi-method method user host
4773 progname dirlist ignore-tilde)
4774 "Searches for PROGNAME in all directories mentioned in DIRLIST.
4775 First args METHOD, USER and HOST specify the connection, PROGNAME
4776 is the program to search for, and DIRLIST gives the list of directories
4777 to search. If IGNORE-TILDE is non-nil, directory names starting
4778 with `~' will be ignored.
4780 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
4782 This function expects to be in the right *tramp* buffer."
4785 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
4786 ;; `remove' is in CL, and we want to avoid CL dependencies.
4789 (setq d (car dirlist))
4790 (setq dirlist (cdr dirlist))
4791 (unless (char-equal ?~ (aref d 0))
4792 (setq newdl (cons d newdl))))
4793 (setq dirlist (nreverse newdl))))
4795 multi-method method user host
4796 (format (concat "while read d; "
4797 "do if test -x $d/%s -a -f $d/%s; "
4798 "then echo tramp_executable $d/%s; "
4799 "break; fi; done <<'EOF'")
4800 progname progname progname))
4802 (tramp-send-command multi-method method user host d))
4804 (tramp-send-command multi-method method user host "EOF")
4805 (tramp-wait-for-output)
4806 (goto-char (point-max))
4807 (when (search-backward "tramp_executable " nil t)
4808 (skip-chars-forward "^ ")
4809 (skip-chars-forward " ")
4810 (buffer-substring (point) (tramp-line-end-position)))))
4812 (defun tramp-set-remote-path (multi-method method user host var dirlist)
4813 "Sets the remote environment VAR to existing directories from DIRLIST.
4814 I.e., for each directory in DIRLIST, it is tested whether it exists and if
4815 so, it is added to the environment variable VAR."
4816 (let ((existing-dirs
4821 (tramp-make-tramp-file-name multi-method method user host x))
4823 (tramp-make-tramp-file-name multi-method method user host x)))
4827 multi-method method user host
4829 (mapconcat 'identity (delq nil existing-dirs) ":")
4831 (tramp-wait-for-output)))
4833 ;; -- communication with external shell --
4835 (defun tramp-find-file-exists-command (multi-method method user host)
4836 "Find a command on the remote host for checking if a file exists.
4837 Here, we are looking for a command which has zero exit status if the
4838 file exists and nonzero exit status otherwise."
4839 (make-local-variable 'tramp-file-exists-command)
4840 (tramp-message 9 "Finding command to check if file exists")
4842 (tramp-make-tramp-file-name
4843 multi-method method user host
4844 "/")) ;assume this file always exists
4846 (tramp-make-tramp-file-name
4847 multi-method method user host
4848 "/ this file does not exist "))) ;assume this never exists
4849 ;; The algorithm is as follows: we try a list of several commands.
4850 ;; For each command, we first run `$cmd /' -- this should return
4851 ;; true, as the root directory always exists. And then we run
4852 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
4853 ;; does not exist. This should return false. We use the first
4854 ;; command we find that seems to work.
4855 ;; The list of commands to try is as follows:
4856 ;; `ls -d' This works on most systems, but NetBSD 1.4
4857 ;; has a bug: `ls' always returns zero exit
4858 ;; status, even for files which don't exist.
4859 ;; `test -e' Some Bourne shells have a `test' builtin
4860 ;; which does not know the `-e' option.
4861 ;; `/bin/test -e' For those, the `test' binary on disk normally
4862 ;; provides the option. Alas, the binary
4863 ;; is sometimes `/bin/test' and sometimes it's
4865 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
4867 (and (setq tramp-file-exists-command "test -e %s")
4868 (file-exists-p existing)
4869 (not (file-exists-p nonexisting)))
4870 (and (setq tramp-file-exists-command "/bin/test -e %s")
4871 (file-exists-p existing)
4872 (not (file-exists-p nonexisting)))
4873 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
4874 (file-exists-p existing)
4875 (not (file-exists-p nonexisting)))
4876 (and (setq tramp-file-exists-command "ls -d %s")
4877 (file-exists-p existing)
4878 (not (file-exists-p nonexisting))))
4879 (error "Couldn't find command to check if file exists."))))
4882 ;; CCC test ksh or bash found for tilde expansion?
4883 (defun tramp-find-shell (multi-method method user host)
4884 "Find a shell on the remote host which groks tilde expansion."
4886 (tramp-send-command multi-method method user host "echo ~root")
4887 (tramp-wait-for-output)
4889 ((string-match "^~root$" (buffer-string))
4891 (or (tramp-find-executable multi-method method user host
4892 "bash" tramp-remote-path t)
4893 (tramp-find-executable multi-method method user host
4894 "ksh" tramp-remote-path t)))
4896 (error "Couldn't find a shell which groks tilde expansion"))
4897 ;; Find arguments for this shell.
4898 (let ((alist tramp-sh-extra-args)
4900 (while (and alist (null extra-args))
4901 (setq item (pop alist))
4902 (when (string-match (car item) shell)
4903 (setq extra-args (cdr item))))
4904 (when extra-args (setq shell (concat shell " " extra-args))))
4906 5 "Starting remote shell `%s' for tilde expansion..." shell)
4908 multi-method method user host
4909 (concat "PS1='$ ' exec " shell)) ;
4910 (unless (tramp-wait-for-regexp
4911 (get-buffer-process (current-buffer))
4912 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'"
4913 tramp-shell-prompt-pattern shell-prompt-pattern))
4914 (pop-to-buffer (buffer-name))
4915 (error "Couldn't find remote `%s' prompt." shell))
4917 9 "Setting remote shell prompt...")
4918 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
4919 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
4920 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
4922 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
4923 tramp-rsh-end-of-line
4925 tramp-rsh-end-of-line
4926 tramp-rsh-end-of-line))
4927 (tramp-wait-for-output)
4929 9 "Setting remote shell prompt...done")
4931 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
4932 (tramp-get-method-parameter
4933 multi-method method user host 'tramp-remote-sh))))))
4935 (defun tramp-check-ls-command (multi-method method user host cmd)
4936 "Checks whether the given `ls' executable groks `-n'.
4937 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
4938 the `ls' executable. Returns t if CMD supports the `-n' option, nil
4940 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
4941 (when (file-executable-p
4942 (tramp-make-tramp-file-name multi-method method user host cmd))
4944 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
4946 (tramp-send-command-and-check
4947 multi-method method user host
4948 (format "%s -lnd / >/dev/null"
4950 (tramp-message 7 "Testing remote command `%s' for -n...%s"
4952 (if (zerop result) "okay" "failed"))
4955 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
4956 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
4957 Returns nil if none was found, else the command is returned."
4960 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
4961 ;; It would be better to use the CL function `find', but
4962 ;; we don't want run-time dependencies on CL.
4963 (while (and dl (not result))
4964 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
4965 (when (tramp-check-ls-command multi-method method user host x)
4970 (defun tramp-find-ls-command (multi-method method user host)
4971 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
4972 \(This option prints numeric user and group ids in a long listing.)"
4973 (tramp-message 9 "Finding a suitable `ls' command")
4975 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
4976 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
4977 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
4979 ;; ------------------------------------------------------------
4980 ;; -- Functions for establishing connection --
4981 ;; ------------------------------------------------------------
4983 ;; The following functions are actions to be taken when seeing certain
4984 ;; prompts from the remote host. See the variable
4985 ;; `tramp-actions-before-shell' for usage of these functions.
4987 (defun tramp-action-login (p multi-method method user host)
4988 "Send the login name."
4989 (tramp-message 9 "Sending login name `%s'"
4990 (or user (user-login-name)))
4992 (process-send-string nil (concat (or user (user-login-name))
4993 tramp-rsh-end-of-line)))
4995 (defun tramp-action-password (p multi-method method user host)
4996 "Query the user for a password."
4997 (let ((pw-prompt (match-string 0)))
4998 (tramp-message 9 "Sending password")
4999 (tramp-enter-password p pw-prompt user host)))
5001 (defun tramp-action-succeed (p multi-method method user host)
5002 "Signal success in finding shell prompt."
5003 (tramp-message 9 "Found remote shell prompt.")
5005 (throw 'tramp-action 'ok))
5007 (defun tramp-action-permission-denied (p multi-method method user host)
5008 "Signal permission denied."
5009 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5010 (tramp-message 9 "Permission denied by remote host.")
5012 (throw 'tramp-action 'permission-denied))
5014 (defun tramp-action-yesno (p multi-method method user host)
5015 "Ask the user for confirmation using `yes-or-no-p'.
5016 Send \"yes\" to remote process on confirmation, abort otherwise.
5017 See also `tramp-action-yn'."
5018 (save-window-excursion
5019 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5020 (unless (yes-or-no-p (match-string 0))
5023 (throw 'tramp-action 'permission-denied))
5024 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5027 (defun tramp-action-yn (p multi-method method user host)
5028 "Ask the user for confirmation using `y-or-n-p'.
5029 Send \"y\" to remote process on confirmation, abort otherwise.
5030 See also `tramp-action-yesno'."
5031 (save-window-excursion
5032 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5033 (unless (y-or-n-p (match-string 0))
5035 (throw 'tramp-action 'permission-denied))
5037 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5039 (defun tramp-action-terminal (p multi-method method user host)
5040 "Tell the remote host which terminal type to use.
5041 The terminal type can be configured with `tramp-terminal-type'."
5042 (tramp-message 9 "Setting `%s' as terminal type."
5043 tramp-terminal-type)
5045 (process-send-string nil (concat tramp-terminal-type
5046 tramp-rsh-end-of-line)))
5048 (defun tramp-action-process-alive (p multi-method method user host)
5049 "Check whether a process has finished."
5050 (unless (memq (process-status p) '(run open))
5051 (throw 'tramp-action 'process-died)))
5053 (defun tramp-action-out-of-band (p multi-method method user host)
5054 "Check whether an out-of-band copy has finished."
5055 (cond ((and (memq (process-status p) '(stop exit))
5056 (zerop (process-exit-status p)))
5057 (tramp-message 9 "Process has finished.")
5058 (throw 'tramp-action 'ok))
5059 ((or (and (memq (process-status p) '(stop exit))
5060 (not (zerop (process-exit-status p))))
5061 (memq (process-status p) '(signal)))
5062 (tramp-message 9 "Process has died.")
5063 (throw 'tramp-action 'process-died))
5066 ;; The following functions are specifically for multi connections.
5068 (defun tramp-multi-action-login (p method user host)
5069 "Send the login name."
5070 (tramp-message 9 "Sending login name `%s'" user)
5072 (process-send-string p (concat user tramp-rsh-end-of-line)))
5074 (defun tramp-multi-action-password (p method user host)
5075 "Query the user for a password."
5076 (tramp-message 9 "Sending password")
5077 (tramp-enter-password p (match-string 0) user host))
5079 (defun tramp-multi-action-succeed (p method user host)
5080 "Signal success in finding shell prompt."
5081 (tramp-message 9 "Found shell prompt on `%s'" host)
5083 (throw 'tramp-action 'ok))
5085 (defun tramp-multi-action-permission-denied (p method user host)
5086 "Signal permission denied."
5087 (tramp-message 9 "Permission denied by remote host `%s'" host)
5090 (throw 'tramp-action 'permission-denied))
5092 (defun tramp-multi-action-process-alive (p method user host)
5093 "Check whether a process has finished."
5094 (unless (memq (process-status p) '(run open))
5095 (throw 'tramp-action 'process-died)))
5097 ;; Functions for processing the actions.
5099 (defun tramp-process-one-action (p multi-method method user host actions)
5100 "Wait for output from the shell and perform one action."
5101 (let (found item pattern action todo)
5103 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5104 (with-timeout (60 (throw 'tramp-action 'timeout))
5106 (accept-process-output p 1)
5107 (goto-char (point-min))
5110 (goto-char (point-min))
5111 (setq item (pop todo))
5112 (setq pattern (symbol-value (nth 0 item)))
5113 (setq action (nth 1 item))
5114 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5116 (when (re-search-forward (concat pattern "\\'") nil t)
5117 (setq found (funcall action p multi-method method user host)))))
5120 (defun tramp-process-actions (p multi-method method user host actions)
5121 "Perform actions until success."
5124 (tramp-message 9 "Waiting for prompts from remote shell")
5126 (catch 'tramp-action
5127 (tramp-process-one-action
5128 p multi-method method user host actions)
5130 (unless (eq exit 'ok)
5131 (tramp-clear-passwd user host)
5132 (error "Login failed"))))
5134 ;; For multi-actions.
5136 (defun tramp-process-one-multi-action (p method user host actions)
5137 "Wait for output from the shell and perform one action."
5138 (let (found item pattern action todo)
5140 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5141 (with-timeout (60 (throw 'tramp-action 'timeout))
5143 (accept-process-output p 1)
5145 (goto-char (point-min))
5147 (goto-char (point-min))
5148 (setq item (pop todo))
5149 (setq pattern (symbol-value (nth 0 item)))
5150 (setq action (nth 1 item))
5151 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5153 (when (re-search-forward (concat pattern "\\'") nil t)
5154 (setq found (funcall action p method user host)))))
5157 (defun tramp-process-multi-actions (p method user host actions)
5158 "Perform actions until success."
5161 (tramp-message 9 "Waiting for prompts from remote shell")
5163 (catch 'tramp-action
5164 (tramp-process-one-multi-action p method user host actions)
5166 (unless (eq exit 'ok)
5167 (tramp-clear-passwd user host)
5168 (error "Login failed"))))
5170 ;; Functions to execute when we have seen the remote shell prompt but
5171 ;; before we exec the Bourne-ish shell. Note that these commands
5172 ;; might be sent to any shell, not just a Bourne-ish shell. This
5173 ;; means that the commands need to work in all shells. (It is also
5174 ;; okay for some commands to just fail with an error message, but
5175 ;; please make sure that they at least don't crash the odd shell people
5176 ;; might be running...)
5177 (defun tramp-process-initial-commands (p
5178 multi-method method user host
5180 "Send list of commands to remote host, in order."
5183 (setq cmd (pop commands))
5185 (tramp-message 10 "Sending command to remote shell: %s"
5187 (tramp-send-command multi-method method user host cmd nil t)
5188 (tramp-barf-if-no-shell-prompt
5189 p 60 "Remote shell command failed: %s" cmd))
5192 ;; The actual functions for opening connections.
5194 (defun tramp-open-connection-telnet (multi-method method user host)
5195 "Open a connection using a telnet METHOD.
5196 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5197 login prompt, then sends the user name USER, then waits for a remote
5198 password prompt. It queries the user for the password, then sends the
5199 password to the remote host.
5201 If USER is nil, uses value returned by `(user-login-name)' instead.
5203 Recognition of the remote shell prompt is based on the variables
5204 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5207 Please note that it is NOT possible to use this connection method
5208 together with an out-of-band transfer method! You must use an inline
5211 Maybe the different regular expressions need to be tuned.
5213 * Actually, the telnet program as well as the args to be used can be
5214 specified in the method parameters, see the variable `tramp-methods'."
5216 (when (tramp-method-out-of-band-p multi-method method user host)
5217 (error "Cannot use out-of-band method `%s' with telnet connection method"
5220 (error "Cannot multi-connect using telnet connection method"))
5221 (tramp-pre-connection multi-method method user host)
5222 (tramp-message 7 "Opening connection for %s@%s using %s..."
5223 (or user (user-login-name)) host method)
5224 (let ((process-environment (copy-sequence process-environment)))
5225 (setenv "TERM" tramp-terminal-type)
5226 (let* ((default-directory (tramp-temporary-file-directory))
5227 ;; If we omit the conditional here, then we would use
5228 ;; `undecided-dos' in some cases. With the conditional,
5229 ;; we use nil in these cases. Which one is right?
5230 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5231 (> emacs-major-version 20))
5232 tramp-dos-coding-system))
5233 (p (apply 'start-process
5234 (tramp-buffer-name multi-method method user host)
5235 (tramp-get-buffer multi-method method user host)
5236 (tramp-get-method-parameter
5238 (tramp-find-method multi-method method user host)
5239 user host 'tramp-login-program)
5241 (tramp-get-method-parameter
5243 (tramp-find-method multi-method method user host)
5244 user host 'tramp-login-args)))
5247 (tramp-set-process-query-on-exit-flag p nil)
5248 (set-buffer (tramp-get-buffer multi-method method user host))
5250 (tramp-process-actions p multi-method method user host
5251 tramp-actions-before-shell)
5252 (tramp-open-connection-setup-interactive-shell
5253 p multi-method method user host)
5254 (tramp-post-connection multi-method method user host)))))
5257 (defun tramp-open-connection-rsh (multi-method method user host)
5258 "Open a connection using an rsh METHOD.
5259 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5260 password or shell prompt. If a password prompt is seen, the user is
5261 queried for a password, this function sends the password to the remote
5262 host and waits for a shell prompt.
5264 If USER is nil, start the command `rsh HOST'[*] instead
5266 Recognition of the remote shell prompt is based on the variables
5267 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5270 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5271 be a port number for ssh, and \"-p yy\" will be added to the list of
5272 arguments, and xx will be used as the host name to connect to.
5274 * Actually, the rsh program to be used can be specified in the
5275 method parameters, see the variable `tramp-methods'."
5278 (error "Cannot multi-connect using rsh connection method"))
5279 (tramp-pre-connection multi-method method user host)
5280 (if (and user (not (string= user "")))
5281 (tramp-message 7 "Opening connection for %s@%s using %s..."
5283 (tramp-message 7 "Opening connection at %s using %s..." host method))
5284 (let ((process-environment (copy-sequence process-environment))
5285 (bufnam (tramp-buffer-name multi-method method user host))
5286 (buf (tramp-get-buffer multi-method method user host))
5287 (login-program (tramp-get-method-parameter
5289 (tramp-find-method multi-method method user host)
5290 user host 'tramp-login-program))
5291 (login-args (tramp-get-method-parameter
5293 (tramp-find-method multi-method method user host)
5294 user host 'tramp-login-args))
5296 ;; The following should be changed. We need a more general
5297 ;; mechanism to parse extra host args.
5298 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5299 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5300 (setq real-host (match-string 1 host)))
5301 (setenv "TERM" tramp-terminal-type)
5302 (let* ((default-directory (tramp-temporary-file-directory))
5303 ;; If we omit the conditional, we would use
5304 ;; `undecided-dos' in some cases. With the conditional,
5305 ;; we use nil in these cases. Which one is right?
5306 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5307 (> emacs-major-version 20))
5308 tramp-dos-coding-system))
5309 (p (if (and user (not (string= user "")))
5310 (apply #'start-process bufnam buf login-program
5311 real-host "-l" user login-args)
5312 (apply #'start-process bufnam buf login-program
5313 real-host login-args)))
5315 (tramp-set-process-query-on-exit-flag p nil)
5318 (tramp-process-actions p multi-method method user host
5319 tramp-actions-before-shell)
5320 (tramp-message 7 "Initializing remote shell")
5321 (tramp-open-connection-setup-interactive-shell
5322 p multi-method method user host)
5323 (tramp-post-connection multi-method method user host)))))
5325 (defun tramp-open-connection-su (multi-method method user host)
5326 "Open a connection using the `su' program with METHOD.
5327 This starts `su - USER', then waits for a password prompt. The HOST
5328 name must be equal to the local host name or to `localhost'.
5330 If USER is nil, uses value returned by user-login-name instead.
5332 Recognition of the remote shell prompt is based on the variables
5333 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5334 set up correctly. Note that the other user may have a different shell
5335 prompt than you do, so it is not at all unlikely that the variable
5336 `shell-prompt-pattern' is set up wrongly!"
5338 (when (tramp-method-out-of-band-p multi-method method user host)
5339 (error "Cannot use out-of-band method `%s' with `su' connection method"
5341 (unless (or (string-match (concat "^" (regexp-quote host))
5343 (string= "localhost" host)
5346 "Cannot connect to different host `%s' with `su' connection method"
5348 (tramp-pre-connection multi-method method user host)
5349 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5350 (or user "<root>") method)
5351 (let ((process-environment (copy-sequence process-environment)))
5352 (setenv "TERM" tramp-terminal-type)
5353 (let* ((default-directory (tramp-temporary-file-directory))
5354 ;; If we omit the conditional, we use `undecided-dos' in
5355 ;; some cases. With the conditional, we use nil in these
5356 ;; cases. What's the difference? Which one is right?
5357 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5358 (> emacs-major-version 20))
5359 tramp-dos-coding-system))
5360 (p (apply 'start-process
5361 (tramp-buffer-name multi-method method user host)
5362 (tramp-get-buffer multi-method method user host)
5363 (tramp-get-method-parameter
5365 (tramp-find-method multi-method method user host)
5366 user host 'tramp-login-program)
5369 (format-spec x `((?u . ,(or user "root")))))
5370 (tramp-get-method-parameter
5372 (tramp-find-method multi-method method user host)
5373 user host 'tramp-login-args))))
5376 (tramp-set-process-query-on-exit-flag p nil)
5377 (set-buffer (tramp-get-buffer multi-method method user host))
5378 (tramp-process-actions p multi-method method user host
5379 tramp-actions-before-shell)
5380 (tramp-open-connection-setup-interactive-shell
5381 p multi-method method user host)
5382 (tramp-post-connection multi-method method
5385 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5386 ;; handle not giving a user name in the "file name".
5388 ;; This is more difficult than for the single-hop method. In the
5389 ;; multi-hop-method, the desired behaviour should be that the
5390 ;; user must specify names for the telnet hops of which the user
5391 ;; name is different than the "original" name (or different from
5392 ;; the previous hop.
5393 (defun tramp-open-connection-multi (multi-method method user host)
5394 "Open a multi-hop connection using METHOD.
5395 This uses a slightly changed file name syntax. The idea is to say
5396 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5397 This will use telnet to log in as u1 to h1, then use rsh from there to
5398 log in as u2 to h2."
5400 (unless multi-method
5401 (error "Multi-hop open connection function called on non-multi method"))
5402 (when (tramp-method-out-of-band-p multi-method method user host)
5403 (error "No out of band multi-hop connections"))
5404 (unless (and (arrayp method) (not (stringp method)))
5405 (error "METHOD must be an array of strings for multi methods"))
5406 (unless (and (arrayp user) (not (stringp user)))
5407 (error "USER must be an array of strings for multi methods"))
5408 (unless (and (arrayp host) (not (stringp host)))
5409 (error "HOST must be an array of strings for multi methods"))
5410 (unless (and (= (length method) (length user))
5411 (= (length method) (length host)))
5412 (error "Arrays METHOD, USER, HOST must have equal length"))
5413 (tramp-pre-connection multi-method method user host)
5414 (tramp-message 7 "Opening `%s' connection..." multi-method)
5415 (let ((process-environment (copy-sequence process-environment)))
5416 (setenv "TERM" tramp-terminal-type)
5417 (let* ((default-directory (tramp-temporary-file-directory))
5418 ;; If we omit the conditional, we use `undecided-dos' in
5419 ;; some cases. With the conditional, we use nil in these
5420 ;; cases. What's the difference? Which one is right?
5421 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5422 (> emacs-major-version 20))
5423 tramp-dos-coding-system))
5424 (p (start-process (tramp-buffer-name multi-method method user host)
5425 (tramp-get-buffer multi-method method user host)
5426 tramp-multi-sh-program))
5427 (num-hops (length method))
5429 (tramp-set-process-query-on-exit-flag p nil)
5430 (tramp-message 9 "Waiting 60s for local shell to come up...")
5431 (unless (tramp-wait-for-regexp
5432 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5433 shell-prompt-pattern tramp-shell-prompt-pattern))
5434 (pop-to-buffer (buffer-name))
5436 (error "Couldn't find local shell prompt"))
5437 ;; Now do all the connections as specified.
5438 (while (< i num-hops)
5439 (let* ((m (aref method i))
5442 (entry (assoc m tramp-multi-connection-function-alist))
5443 (multi-func (nth 1 entry))
5444 (command (nth 2 entry)))
5445 ;; The multi-funcs don't need to do save-match-data, as that
5447 (funcall multi-func p m u h command)
5450 (tramp-open-connection-setup-interactive-shell
5451 p multi-method method user host)
5452 (tramp-post-connection multi-method method user host)))))
5454 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5455 ;; of no user name provided. Hack to make it work as it did before:
5456 ;; changed `user' to `(or user (user-login-name))' in the places where
5457 ;; the value is actually used.
5458 (defun tramp-multi-connect-telnet (p method user host command)
5459 "Issue `telnet' command.
5460 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5461 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5462 the host name, and `%n' is replaced with an end of line character, as
5463 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5466 If USER is nil, uses the return value of (user-login-name) instead."
5467 (let ((cmd (format-spec command
5468 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5469 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5472 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5473 (process-send-string p cmd)
5474 (tramp-process-multi-actions p method user host
5475 tramp-multi-actions)))
5477 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5478 ;; of no user name provided. Hack to make it work as it did before:
5479 ;; changed `user' to `(or user (user-login-name))' in the places where
5480 ;; the value is actually used.
5481 (defun tramp-multi-connect-rlogin (p method user host command)
5482 "Issue `rlogin' command.
5483 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5484 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5485 with the user name, `%h' will be replaced with the host name, and `%n'
5486 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5487 `%%' if you want to use a literal percent character.
5489 If USER is nil, uses the return value of (user-login-name) instead."
5490 (let ((cmd (format-spec command `((?h . ,host)
5491 (?u . ,(or user (user-login-name)))
5492 (?n . ,tramp-rsh-end-of-line))))
5493 (cmd1 (format-spec command `((?h . ,host)
5494 (?u . ,(or user (user-login-name)))
5498 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5499 (process-send-string p cmd)
5500 (tramp-process-multi-actions p method user host
5501 tramp-multi-actions)))
5503 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5504 ;; of no user name provided. Hack to make it work as it did before:
5505 ;; changed `user' to `(or user (user-login-name))' in the places where
5506 ;; the value is actually used.
5507 (defun tramp-multi-connect-su (p method user host command)
5508 "Issue `su' command.
5509 Uses shell COMMAND to issue a `su' command to log in as USER on
5510 HOST. The HOST name is ignored, this just changes the user id on the
5511 host currently logged in to.
5513 If USER is nil, uses the return value of (user-login-name) instead.
5515 You can use percent escapes in the COMMAND. `%u' is replaced with the
5516 user name, and `%n' is replaced with the value of
5517 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5519 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5520 (?n . ,tramp-rsh-end-of-line))))
5521 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5525 (tramp-message 9 "Sending su command `%s'" cmd1)
5526 (process-send-string p cmd)
5527 (tramp-process-multi-actions p method user host
5528 tramp-multi-actions)))
5530 ;; Utility functions.
5532 (defun tramp-wait-for-regexp (proc timeout regexp)
5533 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5534 Expects the output of PROC to be sent to the current buffer. Returns
5535 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5538 (start-time (current-time)))
5540 ;; Work around a bug in XEmacs 21, where the timeout
5541 ;; expires faster than it should. This degenerates
5542 ;; to polling for buggy XEmacsen, but oh, well.
5543 (while (and (not found)
5544 (< (tramp-time-diff (current-time) start-time)
5546 (with-timeout (timeout)
5548 (accept-process-output proc 1)
5549 (unless (memq (process-status proc) '(run open))
5550 (error "Process has died"))
5551 (goto-char (point-min))
5552 (setq found (when (re-search-forward regexp nil t)
5553 (tramp-match-string-list)))))))
5556 (accept-process-output proc 1)
5557 (unless (memq (process-status proc) '(run open))
5558 (error "Process has died"))
5559 (goto-char (point-min))
5560 (setq found (when (re-search-forward regexp nil t)
5561 (tramp-match-string-list))))))
5562 (when tramp-debug-buffer
5564 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5565 tramp-current-user tramp-current-host)
5566 (point-min) (point-max))
5570 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5571 tramp-current-user tramp-current-host))
5572 (goto-char (point-max))
5573 (insert "[[Regexp `" regexp "' not found"
5574 (if timeout (format " in %d secs" timeout) "")
5578 (defun tramp-wait-for-shell-prompt (proc timeout)
5579 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5580 See `tramp-wait-for-regexp' for more details.
5581 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5582 and `tramp-shell-prompt-pattern'."
5583 (tramp-wait-for-regexp
5585 (format "\\(%s\\|%s\\)\\'"
5586 shell-prompt-pattern tramp-shell-prompt-pattern)))
5588 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5589 "Wait for shell prompt and barf if none appears.
5590 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5591 seconds. If not, it produces an error message with the given ERROR-ARGS."
5592 (unless (tramp-wait-for-shell-prompt proc timeout)
5593 (pop-to-buffer (buffer-name))
5594 (apply 'error error-args)))
5596 (defun tramp-enter-password (p prompt user host)
5597 "Prompt for a password and send it to the remote end.
5598 Uses PROMPT as a prompt and sends the password to process P."
5599 (let ((pw (tramp-read-passwd user host prompt)))
5601 (process-send-string
5603 (or (tramp-get-method-parameter
5604 tramp-current-multi-method
5605 tramp-current-method
5608 'tramp-password-end-of-line)
5609 tramp-default-password-end-of-line)))))
5611 ;; HHH: Not Changed. This might handle the case where USER is not
5612 ;; given in the "File name" very poorly. Then, the local
5613 ;; variable tramp-current-user will be set to nil.
5614 (defun tramp-pre-connection (multi-method method user host)
5615 "Do some setup before actually logging in.
5616 METHOD, USER and HOST specify the connection."
5617 (set-buffer (tramp-get-buffer multi-method method user host))
5618 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5619 (set (make-local-variable 'tramp-current-method) method)
5620 (set (make-local-variable 'tramp-current-user) user)
5621 (set (make-local-variable 'tramp-current-host) host)
5622 (set (make-local-variable 'inhibit-eol-conversion) nil)
5625 (defun tramp-open-connection-setup-interactive-shell
5626 (p multi-method method user host)
5627 "Set up an interactive shell.
5628 Mainly sets the prompt and the echo correctly. P is the shell process
5629 to set up. METHOD, USER and HOST specify the connection."
5630 ;; Wait a bit in case the remote end feels like sending a little
5631 ;; junk first. It seems that fencepost.gnu.org does this when doing
5632 ;; a Kerberos login.
5634 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5635 (tramp-process-initial-commands p multi-method method user host
5636 tramp-initial-commands)
5637 ;; It is useful to set the prompt in the following command because
5638 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5639 ;; about and thus /bin/sh will display a strange prompt. For
5640 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
5641 ;; the current working directory but /bin/sh will display a dollar
5642 ;; sign. The following command line sets $PS1 to a sane value, and
5643 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
5644 ;; Pittman reports that the unusual positioning of the single quotes
5645 ;; makes it work under `rc', too. We also unset the variable $ENV
5646 ;; because that is read by some sh implementations (eg, bash when
5647 ;; called as sh) on startup; this way, we avoid the startup file
5649 (tramp-send-command-internal
5650 multi-method method user host
5651 (format "exec env 'ENV=' 'PS1=$ ' %s"
5652 (tramp-get-method-parameter
5653 multi-method method user host 'tramp-remote-sh))
5654 (format "remote `%s' to come up"
5655 (tramp-get-method-parameter
5656 multi-method method user host 'tramp-remote-sh)))
5657 (tramp-barf-if-no-shell-prompt
5659 "Remote `%s' didn't come up. See buffer `%s' for details"
5660 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
5662 (tramp-message 8 "Setting up remote shell environment")
5663 (tramp-discard-garbage-erase-buffer p multi-method method user host)
5664 (tramp-send-command-internal multi-method method user host
5665 "stty -inlcr -echo kill '^U'")
5667 ;; Ignore garbage after stty command.
5668 (tramp-send-command-internal multi-method method user host
5671 (tramp-send-command-internal multi-method method user host
5672 "TERM=dumb; export TERM")
5673 ;; Try to set up the coding system correctly.
5674 ;; CCC this can't be the right way to do it. Hm.
5677 (tramp-message 9 "Determining coding system")
5678 (tramp-send-command-internal multi-method method user host
5679 "echo foo ; echo bar")
5680 (goto-char (point-min))
5681 (if (featurep 'mule)
5682 ;; Use MULE to select the right EOL convention for communicating
5683 ;; with the process.
5684 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
5685 cs-decode cs-encode)
5686 (when (symbolp cs) (setq cs (cons cs cs)))
5687 (setq cs-decode (car cs))
5688 (setq cs-encode (cdr cs))
5689 (unless cs-decode (setq cs-decode 'undecided))
5690 (unless cs-encode (setq cs-encode 'undecided))
5691 (setq cs-encode (tramp-coding-system-change-eol-conversion
5693 (when (search-forward "\r" nil t)
5694 (setq cs-decode (tramp-coding-system-change-eol-conversion
5696 (set-buffer-process-coding-system cs-decode cs-encode))
5697 ;; Look for ^M and do something useful if found.
5698 (when (search-forward "\r" nil t)
5699 ;; We have found a ^M but cannot frob the process coding system
5700 ;; because we're running on a non-MULE Emacs. Let's try
5703 (tramp-message 9 "Trying `stty -onlcr'")
5704 (tramp-send-command-internal multi-method method user host
5708 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
5709 (tramp-send-command-internal
5710 multi-method method user host
5711 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
5713 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
5714 (tramp-send-command-internal multi-method method user host
5715 "set +o vi +o emacs")
5717 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
5718 (tramp-send-command-internal
5719 multi-method method user host
5720 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
5722 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
5723 (tramp-send-command-internal multi-method method user host
5726 (tramp-message 9 "Setting shell prompt")
5727 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
5728 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
5729 ;; the last time we sent a command, to avoid tramp-send-command to send
5730 ;; "echo are you awake".
5731 (setq tramp-last-cmd-time (current-time))
5733 multi-method method user host
5734 (format "PS1='%s%s%s'; PS2=''; PS3=''"
5735 tramp-rsh-end-of-line
5737 tramp-rsh-end-of-line))
5738 (tramp-wait-for-output))
5740 (defun tramp-post-connection (multi-method method user host)
5741 "Prepare a remote shell before being able to work on it.
5742 METHOD, USER and HOST specify the connection.
5743 Among other things, this finds a shell which groks tilde expansion,
5744 tries to find an `ls' command which groks the `-n' option, sets the
5745 locale to C and sets up the remote shell search path."
5746 ;; Search for a good shell before searching for a command which
5747 ;; checks if a file exists. This is done because Tramp wants to use
5748 ;; "test foo; echo $?" to check if various conditions hold, and
5749 ;; there are buggy /bin/sh implementations which don't execute the
5750 ;; "echo $?" part if the "test" part has an error. In particular,
5751 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
5752 ;; with buggy /bin/sh implementations will have a working bash or
5754 (tramp-find-shell multi-method method user host)
5755 ;; Without (sit-for 0.1) at least, my machine will almost always blow
5756 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
5757 ;; output of (tramp-find-shell) to show up along with the output of
5758 ;; (tramp-find-ls-command) testing.
5760 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
5761 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
5763 ;; After much debugging I couldn't find any problem with the implementation
5764 ;; of that function though. The workaround stays for me at least. :/
5766 ;; Daniel Pittman <daniel@danann.net>
5769 (tramp-find-file-exists-command multi-method method user host)
5770 (make-local-variable 'tramp-ls-command)
5771 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
5772 (unless tramp-ls-command
5775 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
5776 (setq tramp-ls-command
5777 (tramp-find-executable multi-method method user host
5778 "ls" tramp-remote-path nil)))
5779 (unless tramp-ls-command
5780 (error "Fatal error: Couldn't find remote executable `ls'"))
5781 (tramp-message 5 "Using remote command `%s' for getting directory listings"
5783 (tramp-send-command multi-method method user host
5784 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
5785 "return $1" tramp-rsh-end-of-line
5787 (tramp-wait-for-output)
5788 ;; Set remote PATH variable.
5789 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
5790 ;; Tell remote shell to use standard time format, needed for
5791 ;; parsing `ls -l' output.
5792 (tramp-send-command multi-method method user host
5793 "LC_TIME=C; export LC_TIME; echo huhu")
5794 (tramp-wait-for-output)
5795 (tramp-send-command multi-method method user host
5796 "mesg n; echo huhu")
5797 (tramp-wait-for-output)
5798 (tramp-send-command multi-method method user host
5799 "biff n ; echo huhu")
5800 (tramp-wait-for-output)
5801 ;; Unalias ls(1) to work around issues with those silly people who make it
5802 ;; spit out ANSI escapes or whatever.
5803 (tramp-send-command multi-method method user host
5804 "unalias ls; echo huhu")
5805 (tramp-wait-for-output)
5806 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5807 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5808 ;; for otherwise the shell crashes.
5810 (make-local-variable 'tramp-test-groks-nt)
5811 (tramp-send-command multi-method method user host
5813 (tramp-wait-for-output)
5814 (goto-char (point-min))
5815 (setq tramp-test-groks-nt
5816 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
5817 (unless tramp-test-groks-nt
5819 multi-method method user host
5820 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
5821 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
5823 (tramp-wait-for-output)
5824 ;; Send the fallback `uudecode' script.
5826 (tramp-send-string multi-method method user host tramp-uudecode)
5827 (tramp-wait-for-output)
5830 (let ((tramp-remote-perl
5831 (or (tramp-find-executable multi-method method user host
5832 "perl5" tramp-remote-path nil)
5833 (tramp-find-executable multi-method method user host
5834 "perl" tramp-remote-path nil))))
5835 (when tramp-remote-perl
5836 (tramp-set-connection-property "perl" tramp-remote-perl
5837 multi-method method user host)
5838 ;; Set up stat in Perl if we can.
5839 (when tramp-remote-perl
5840 (tramp-message 5 "Sending the Perl `file-attributes' implementation.")
5842 multi-method method user host
5843 (concat "tramp_file_attributes () {\n"
5845 " -e '" tramp-perl-file-attributes "' $1 $2 2>/dev/null\n"
5847 (tramp-wait-for-output)
5848 (unless (tramp-method-out-of-band-p multi-method method user host)
5849 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
5851 multi-method method user host
5852 (concat "tramp_encode () {\n"
5853 (format tramp-perl-encode tramp-remote-perl)
5856 (tramp-wait-for-output)
5858 multi-method method user host
5859 (concat "tramp_encode_with_module () {\n"
5860 (format tramp-perl-encode-with-module tramp-remote-perl)
5863 (tramp-wait-for-output)
5864 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
5866 multi-method method user host
5867 (concat "tramp_decode () {\n"
5868 (format tramp-perl-decode tramp-remote-perl)
5871 (tramp-wait-for-output)
5873 multi-method method user host
5874 (concat "tramp_decode_with_module () {\n"
5875 (format tramp-perl-decode-with-module tramp-remote-perl)
5878 (tramp-wait-for-output)))))
5881 (let ((ln (tramp-find-executable multi-method method user host
5882 "ln" tramp-remote-path nil)))
5884 (tramp-set-connection-property "ln" ln multi-method method user host)))
5886 ;; Find the right encoding/decoding commands to use.
5887 (unless (tramp-method-out-of-band-p multi-method method user host)
5888 (tramp-find-inline-encoding multi-method method user host))
5889 ;; If encoding/decoding command are given, test to see if they work.
5890 ;; CCC: Maybe it would be useful to run the encoder both locally and
5891 ;; remotely to see if they produce the same result.
5892 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
5893 (rem-dec (tramp-get-remote-decoding multi-method method user host))
5894 (magic-string "xyzzy"))
5895 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
5896 (tramp-kill-process multi-method method user host)
5897 ;; Improve error message and/or error check.
5899 "Must give both decoding and encoding command in method definition"))
5900 (when (and rem-enc rem-dec)
5903 "Checking to see if encoding/decoding commands work on remote host...")
5905 multi-method method user host
5906 (format "echo %s | %s | %s"
5907 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
5908 (tramp-wait-for-output)
5909 (unless (looking-at (regexp-quote magic-string))
5910 (tramp-kill-process multi-method method user host)
5911 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
5915 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
5917 ;; CCC: We should either implement a Perl version of base64 encoding
5918 ;; and decoding. Then we just use that in the last item. The other
5919 ;; alternative is to use the Perl version of UU encoding. But then
5920 ;; we need a Lisp version of uuencode.
5922 ;; Old text from documentation of tramp-methods:
5923 ;; Using a uuencode/uudecode inline method is discouraged, please use one
5924 ;; of the base64 methods instead since base64 encoding is much more
5925 ;; reliable and the commands are more standardized between the different
5926 ;; Unix versions. But if you can't use base64 for some reason, please
5927 ;; note that the default uudecode command does not work well for some
5928 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
5929 ;; the following command for uudecode:
5931 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
5933 ;; For Irix, no solution is known yet.
5935 (defvar tramp-coding-commands
5936 '(("mimencode -b" "mimencode -u -b"
5937 base64-encode-region base64-decode-region)
5938 ("mmencode -b" "mmencode -u -b"
5939 base64-encode-region base64-decode-region)
5940 ("recode data..base64" "recode base64..data"
5941 base64-encode-region base64-decode-region)
5942 ("uuencode xxx" "uudecode -o -"
5943 tramp-uuencode-region uudecode-decode-region)
5944 ("uuencode xxx" "uudecode -p"
5945 tramp-uuencode-region uudecode-decode-region)
5946 ("uuencode xxx" "tramp_uudecode"
5947 tramp-uuencode-region uudecode-decode-region)
5948 ("tramp_encode_with_module" "tramp_decode_with_module"
5949 base64-encode-region base64-decode-region)
5950 ("tramp_encode" "tramp_decode"
5951 base64-encode-region base64-decode-region))
5952 "List of coding commands for inline transfer.
5953 Each item is a list that looks like this:
5955 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
5957 The REMOTE-ENCODING should be a string, giving a command accepting a
5958 plain file on standard input and writing the encoded file to standard
5959 output. The REMOTE-DECODING should also be a string, giving a command
5960 accepting an encoded file on standard input and writing the decoded
5961 file to standard output.
5963 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
5964 symbols, giving functions. If they are strings, then they can contain
5965 the \"%s\" format specifier. If that specifier is present, the input
5966 filename will be put into the command line at that spot. If the
5967 specifier is not present, the input should be read from standard
5970 If they are functions, they will be called with two arguments, start
5971 and end of region, and are expected to replace the region contents
5972 with the encoded or decoded results, respectively.")
5974 (defun tramp-find-inline-encoding (multi-method method user host)
5975 "Find an inline transfer encoding that works.
5976 Goes through the list `tramp-coding-commands'."
5977 (let ((commands tramp-coding-commands)
5980 (while (and commands (null found))
5981 (setq item (pop commands))
5983 (let ((rem-enc (nth 0 item))
5984 (rem-dec (nth 1 item))
5985 (loc-enc (nth 2 item))
5986 (loc-dec (nth 3 item)))
5987 ;; Check if remote encoding and decoding commands can be
5988 ;; called remotely with null input and output. This makes
5989 ;; sure there are no syntax errors and the command is really
5990 ;; found. Note that we do not redirect stdout to /dev/null,
5991 ;; for two reaons: when checking the decoding command, we
5992 ;; actually check the output it gives. And also, when
5993 ;; redirecting "mimencode" output to /dev/null, then as root
5994 ;; it might change the permissions of /dev/null!
5995 (tramp-message-for-buffer
5996 multi-method method user host 9
5997 "Checking remote encoding command `%s' for sanity" rem-enc)
5998 (unless (zerop (tramp-send-command-and-check
5999 multi-method method user host
6000 (format "%s </dev/null" rem-enc) t))
6001 (throw 'wont-work nil))
6002 (tramp-message-for-buffer
6003 multi-method method user host 9
6004 "Checking remote decoding command `%s' for sanity" rem-dec)
6005 (unless (zerop (tramp-send-command-and-check
6006 multi-method method user host
6007 (format "echo %s | %s | %s"
6008 magic rem-enc rem-dec) t))
6009 (throw 'wont-work nil))
6011 (goto-char (point-min))
6012 (unless (looking-at (regexp-quote magic))
6013 (throw 'wont-work nil)))
6014 ;; If the local encoder or decoder is a string, the
6015 ;; corresponding command has to work locally.
6016 (when (stringp loc-enc)
6017 (tramp-message-for-buffer
6018 multi-method method user host 9
6019 "Checking local encoding command `%s' for sanity" loc-enc)
6020 (unless (zerop (tramp-call-local-coding-command
6022 (throw 'wont-work nil)))
6023 (when (stringp loc-dec)
6024 (tramp-message-for-buffer
6025 multi-method method user host 9
6026 "Checking local decoding command `%s' for sanity" loc-dec)
6027 (unless (zerop (tramp-call-local-coding-command
6029 (throw 'wont-work nil)))
6030 ;; CCC: At this point, maybe we should check that the output
6031 ;; of the commands is correct. But for the moment we will
6032 ;; assume that commands working on empty input will also
6033 ;; work in practice.
6034 (setq found item))))
6035 ;; Did we find something? If not, issue error. If so,
6036 ;; set connection properties.
6038 (error "Couldn't find an inline transfer encoding"))
6039 (let ((rem-enc (nth 0 found))
6040 (rem-dec (nth 1 found))
6041 (loc-enc (nth 2 found))
6042 (loc-dec (nth 3 found)))
6043 (tramp-message 10 "Using remote encoding %s" rem-enc)
6044 (tramp-set-remote-encoding multi-method method user host rem-enc)
6045 (tramp-message 10 "Using remote decoding %s" rem-dec)
6046 (tramp-set-remote-decoding multi-method method user host rem-dec)
6047 (tramp-message 10 "Using local encoding %s" loc-enc)
6048 (tramp-set-local-encoding multi-method method user host loc-enc)
6049 (tramp-message 10 "Using local decoding %s" loc-dec)
6050 (tramp-set-local-decoding multi-method method user host loc-dec))))
6052 (defun tramp-call-local-coding-command (cmd input output)
6053 "Call the local encoding or decoding command.
6054 If CMD contains \"%s\", provide input file INPUT there in command.
6055 Otherwise, INPUT is passed via standard input.
6056 INPUT can also be nil which means `/dev/null'.
6057 OUTPUT can be a string (which specifies a filename), or t (which
6058 means standard output and thus the current buffer), or nil (which
6061 tramp-encoding-shell ;program
6062 (when (and input (not (string-match "%s" cmd)))
6064 (if (eq output t) t nil) ;output
6066 tramp-encoding-command-switch
6067 ;; actual shell command
6069 (if (string-match "%s" cmd) (format cmd input) cmd)
6070 (if (stringp output) (concat "> " output) ""))))
6072 (defun tramp-maybe-open-connection (multi-method method user host)
6073 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6074 Does not do anything if a connection is already open, but re-opens the
6075 connection if a previous connection has died for some reason."
6076 (let ((p (get-buffer-process
6077 (tramp-get-buffer multi-method method user host)))
6079 ;; If too much time has passed since last command was sent, look
6080 ;; whether process is still alive. If it isn't, kill it. When
6081 ;; using ssh, it can sometimes happen that the remote end has hung
6082 ;; up but the local ssh client doesn't recognize this until it
6083 ;; tries to send some data to the remote end. So that's why we
6084 ;; try to send a command from time to time, then look again
6085 ;; whether the process is really alive.
6087 (set-buffer (tramp-get-buffer multi-method method user host))
6088 (when (and tramp-last-cmd-time
6089 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6090 p (processp p) (memq (process-status p) '(run open)))
6092 multi-method method user host "echo are you awake" nil t)
6093 (unless (tramp-wait-for-output 10)
6097 (unless (and p (processp p) (memq (process-status p) '(run open)))
6098 (when (and p (processp p))
6100 (let ((process-connection-type tramp-process-connection-type))
6101 (funcall (tramp-get-method-parameter
6103 (tramp-find-method multi-method method user host)
6104 user host 'tramp-connection-function)
6105 multi-method method user host)))))
6107 (defun tramp-send-command
6108 (multi-method method user host command &optional noerase neveropen)
6109 "Send the COMMAND to USER at HOST (logged in using METHOD).
6110 Erases temporary buffer before sending the command (unless NOERASE
6112 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6113 connection. This is meant to be used from
6114 `tramp-maybe-open-connection' only."
6116 (tramp-maybe-open-connection multi-method method user host))
6117 (setq tramp-last-cmd-time (current-time))
6118 (setq tramp-last-cmd command)
6119 (when tramp-debug-buffer
6121 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6122 (goto-char (point-max))
6123 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6125 (set-buffer (tramp-get-buffer multi-method method user host))
6126 (unless noerase (erase-buffer))
6127 (setq proc (get-buffer-process (current-buffer)))
6128 (process-send-string proc
6129 (concat command tramp-rsh-end-of-line))))
6131 (defun tramp-send-command-internal
6132 (multi-method method user host command &optional msg)
6133 "Send command to remote host and wait for success.
6134 Sends COMMAND, then waits 30 seconds for shell prompt."
6135 (tramp-send-command multi-method method user host command t t)
6137 (tramp-message 9 "Waiting 30s for %s..." msg))
6138 (tramp-barf-if-no-shell-prompt
6140 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6142 (defun tramp-wait-for-output (&optional timeout)
6143 "Wait for output from remote rsh command."
6144 (let ((proc (get-buffer-process (current-buffer)))
6146 (start-time (current-time))
6147 (start-point (point))
6148 (end-of-output (concat "^"
6149 (regexp-quote tramp-end-of-output)
6151 ;; Algorithm: get waiting output. See if last line contains
6152 ;; end-of-output sentinel. If not, wait a bit and again get
6153 ;; waiting output. Repeat until timeout expires or end-of-output
6154 ;; sentinel is seen. Will hang if timeout is nil and
6155 ;; end-of-output sentinel never appears.
6158 ;; Work around an XEmacs bug, where the timeout expires
6159 ;; faster than it should. This degenerates into polling
6160 ;; for buggy XEmacsen, but oh, well.
6161 (while (and (not found)
6162 (< (tramp-time-diff (current-time) start-time)
6164 (with-timeout (timeout)
6166 (accept-process-output proc 1)
6167 (unless (memq (process-status proc) '(run open))
6168 (error "Process has died"))
6169 (goto-char (point-max))
6171 (setq found (looking-at end-of-output))))))
6174 (accept-process-output proc 1)
6175 (unless (memq (process-status proc) '(run open))
6176 (error "Process has died"))
6177 (goto-char (point-max))
6179 (setq found (looking-at end-of-output))))))
6180 ;; At this point, either the timeout has expired or we have found
6181 ;; the end-of-output sentinel.
6183 (goto-char (point-max))
6185 (delete-region (point) (point-max)))
6186 ;; If processing echoes, look for it in the first line and delete.
6187 (when tramp-process-echoes
6189 (goto-char start-point)
6190 (when (looking-at (regexp-quote tramp-last-cmd))
6191 (delete-region (point) (forward-line 1)))))
6192 ;; Add output to debug buffer if appropriate.
6193 (when tramp-debug-buffer
6195 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6196 tramp-current-user tramp-current-host)
6197 (point-min) (point-max))
6201 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6202 tramp-current-user tramp-current-host))
6203 (goto-char (point-max))
6204 (insert "[[Remote prompt `" end-of-output "' not found"
6205 (if timeout (format " in %d secs" timeout) "")
6207 (goto-char (point-min))
6208 ;; Return value is whether end-of-output sentinel was found.
6211 (defun tramp-match-string-list (&optional string)
6212 "Returns list of all match strings.
6213 That is, (list (match-string 0) (match-string 1) ...), according to the
6215 (let* ((nmatches (/ (length (match-data)) 2))
6219 (setq res (cons (match-string i string) res))
6223 (defun tramp-send-command-and-check (multi-method method user host command
6225 "Run COMMAND and check its exit status.
6226 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6227 Sends `echo $?' along with the COMMAND for checking the exit status. If
6228 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6230 If the optional argument SUBSHELL is non-nil, the command is executed in
6231 a subshell, ie surrounded by parentheses."
6232 (tramp-send-command multi-method method user host
6233 (concat (if subshell "( " "")
6235 (if command " 2>/dev/null; " "")
6236 "echo tramp_exit_status $?"
6237 (if subshell " )" " ")))
6238 (tramp-wait-for-output)
6239 (goto-char (point-max))
6240 (unless (search-backward "tramp_exit_status " nil t)
6241 (error "Couldn't find exit status of `%s'" command))
6242 (skip-chars-forward "^ ")
6243 (read (current-buffer)))
6245 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6246 signal fmt &rest args)
6247 "Run COMMAND, check exit status, throw error if exit status not okay.
6248 Similar to `tramp-send-command-and-check' but accepts two more arguments
6249 FMT and ARGS which are passed to `error'."
6250 (unless (zerop (tramp-send-command-and-check
6251 multi-method method user host command subshell))
6252 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6254 (pop-to-buffer (current-buffer))
6255 (funcall 'signal signal (apply 'format fmt args))))
6257 ;; It seems that Tru64 Unix does not like it if long strings are sent
6258 ;; to it in one go. (This happens when sending the Perl
6259 ;; `file-attributes' implementation, for instance.) Therefore, we
6260 ;; have this function which waits a bit at each line.
6261 (defun tramp-send-string
6262 (multi-method method user host string)
6263 "Send the STRING to USER at HOST using METHOD.
6265 The STRING is expected to use Unix line-endings, but the lines sent to
6266 the remote host use line-endings as defined in the variable
6267 `tramp-rsh-end-of-line'."
6268 (let ((proc (get-buffer-process
6269 (tramp-get-buffer multi-method method user host))))
6271 (error "Can't send string to remote host -- not logged in"))
6273 (when tramp-debug-buffer
6275 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6276 (goto-char (point-max))
6277 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6278 ;; replace "\n" by `tramp-rsh-end-of-line'
6280 (mapconcat 'identity
6281 (split-string string "\n")
6282 tramp-rsh-end-of-line))
6283 (unless (or (string= string "")
6284 (string-equal (substring string -1) tramp-rsh-end-of-line))
6285 (setq string (concat string tramp-rsh-end-of-line)))
6287 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6289 (end (length string)))
6291 (tramp-message-for-buffer
6292 multi-method method user host 10
6293 "Sending chunk from %s to %s"
6294 pos (min (+ pos tramp-chunksize) end))
6295 (process-send-string
6296 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6297 (setq pos (+ pos tramp-chunksize))
6299 (process-send-string proc string))))
6301 (defun tramp-send-eof (multi-method method user host)
6302 "Send EOF to the remote end.
6303 METHOD, HOST and USER specify the connection."
6304 (let ((proc (get-buffer-process
6305 (tramp-get-buffer multi-method method user host))))
6307 (error "Can't send EOF to remote host -- not logged in"))
6308 (process-send-eof proc)))
6309 ; (process-send-string proc "\^D")))
6311 (defun tramp-kill-process (multi-method method user host)
6312 "Kill the connection process used by Tramp.
6313 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6314 (let ((proc (get-buffer-process
6315 (tramp-get-buffer multi-method method user host))))
6316 (kill-process proc)))
6318 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6319 "Erase buffer, then discard subsequent garbage.
6320 If `tramp-discard-garbage' is nil, just erase buffer."
6321 (if (not tramp-discard-garbage)
6323 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
6324 (when tramp-debug-buffer
6326 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6327 (goto-char (point-max))
6328 (tramp-insert-with-face
6329 'bold (format "Additional characters detected\n")))))))
6331 (defun tramp-mode-string-to-int (mode-string)
6332 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6333 (let* ((mode-chars (string-to-vector mode-string))
6334 (owner-read (aref mode-chars 1))
6335 (owner-write (aref mode-chars 2))
6336 (owner-execute-or-setid (aref mode-chars 3))
6337 (group-read (aref mode-chars 4))
6338 (group-write (aref mode-chars 5))
6339 (group-execute-or-setid (aref mode-chars 6))
6340 (other-read (aref mode-chars 7))
6341 (other-write (aref mode-chars 8))
6342 (other-execute-or-sticky (aref mode-chars 9)))
6346 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6347 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6349 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6350 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6351 (case owner-execute-or-setid
6352 (?x (tramp-octal-to-decimal "00100"))
6353 (?S (tramp-octal-to-decimal "04000"))
6354 (?s (tramp-octal-to-decimal "04100"))
6356 (t (error "Fourth char `%c' must be one of `xsS-'"
6357 owner-execute-or-setid)))
6359 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6360 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6362 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6363 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6364 (case group-execute-or-setid
6365 (?x (tramp-octal-to-decimal "00010"))
6366 (?S (tramp-octal-to-decimal "02000"))
6367 (?s (tramp-octal-to-decimal "02010"))
6369 (t (error "Seventh char `%c' must be one of `xsS-'"
6370 group-execute-or-setid)))
6372 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6373 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6375 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6376 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6377 (case other-execute-or-sticky
6378 (?x (tramp-octal-to-decimal "00001"))
6379 (?T (tramp-octal-to-decimal "01000"))
6380 (?t (tramp-octal-to-decimal "01001"))
6382 (t (error "Tenth char `%c' must be one of `xtT-'"
6383 other-execute-or-sticky)))))))
6386 (defun tramp-file-mode-from-int (mode)
6387 "Turn an integer representing a file mode into an ls(1)-like string."
6388 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6389 (user (logand (lsh mode -6) 7))
6390 (group (logand (lsh mode -3) 7))
6391 (other (logand (lsh mode -0) 7))
6392 (suid (> (logand (lsh mode -9) 4) 0))
6393 (sgid (> (logand (lsh mode -9) 2) 0))
6394 (sticky (> (logand (lsh mode -9) 1) 0)))
6395 (setq user (tramp-file-mode-permissions user suid "s"))
6396 (setq group (tramp-file-mode-permissions group sgid "s"))
6397 (setq other (tramp-file-mode-permissions other sticky "t"))
6398 (concat type user group other)))
6401 (defun tramp-file-mode-permissions (perm suid suid-text)
6402 "Convert a permission bitset into a string.
6403 This is used internally by `tramp-file-mode-from-int'."
6404 (let ((r (> (logand perm 4) 0))
6405 (w (> (logand perm 2) 0))
6406 (x (> (logand perm 1) 0)))
6407 (concat (or (and r "r") "-")
6408 (or (and w "w") "-")
6409 (or (and suid x suid-text) ; suid, execute
6410 (and suid (upcase suid-text)) ; suid, !execute
6411 (and x "x") "-")))) ; !suid
6414 (defun tramp-decimal-to-octal (i)
6415 "Return a string consisting of the octal digits of I.
6416 Not actually used. Use `(format \"%o\" i)' instead?"
6417 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6418 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6420 (t (concat (tramp-decimal-to-octal (/ i 8))
6421 (number-to-string (% i 8))))))
6424 ;;(defun tramp-octal-to-decimal (ostr)
6425 ;; "Given a string of octal digits, return a decimal number."
6426 ;; (cond ((null ostr) 0)
6427 ;; ((string= "" ostr) 0)
6428 ;; (t (let ((last (aref ostr (1- (length ostr))))
6429 ;; (rest (substring ostr 0 (1- (length ostr)))))
6430 ;; (unless (and (>= last ?0)
6432 ;; (error "Not an octal digit: %c" last))
6433 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6434 ;; Kudos to Gerd Moellmann for this suggestion.
6435 (defun tramp-octal-to-decimal (ostr)
6436 "Given a string of octal digits, return a decimal number."
6437 (let ((x (or ostr "")))
6438 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6439 (unless (string-match "\\`[0-7]*\\'" x)
6440 (error "Non-octal junk in string `%s'" x))
6441 (string-to-number ostr 8)))
6443 (defun tramp-shell-case-fold (string)
6444 "Converts STRING to shell glob pattern which ignores case."
6447 (if (equal (downcase c) (upcase c))
6449 (format "[%c%c]" (downcase c) (upcase c))))
6454 ;; ------------------------------------------------------------
6455 ;; -- TRAMP file names --
6456 ;; ------------------------------------------------------------
6457 ;; Conversion functions between external representation and
6458 ;; internal data structure. Convenience functions for internal
6461 (defstruct tramp-file-name multi-method method user host localname)
6463 (defun tramp-tramp-file-p (name)
6464 "Return t iff NAME is a tramp file."
6466 (string-match tramp-file-name-regexp name)))
6468 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6469 ;; to the `user' part of the structure if a user name was not
6470 ;; provided, now it assigns nil.
6471 (defun tramp-dissect-file-name (name)
6472 "Return an `tramp-file-name' structure.
6473 The structure consists of remote method, remote user, remote host and
6474 localname (file name on remote host)."
6476 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6479 (if match (match-string (nth 1 tramp-file-name-structure) name)
6482 (format (nth 0 tramp-multi-file-name-structure)
6483 (nth 0 tramp-multi-file-name-hop-structure)) name)
6484 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6485 (if (and method (member method tramp-multi-methods))
6486 ;; If it's a multi method, the file name structure contains
6487 ;; arrays of method, user and host.
6488 (tramp-dissect-multi-file-name name)
6489 ;; Normal method. First, find out default method.
6490 (unless match (error "Not a tramp file name: %s" name))
6491 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6492 (host (match-string (nth 3 tramp-file-name-structure) name))
6493 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6494 (make-tramp-file-name
6499 :localname localname))))))
6501 (defun tramp-find-default-method (user host)
6502 "Look up the right method to use in `tramp-default-method-alist'."
6503 (let ((choices tramp-default-method-alist)
6504 (method tramp-default-method)
6507 (setq item (pop choices))
6508 (when (and (string-match (nth 0 item) (or host ""))
6509 (string-match (nth 1 item) (or user "")))
6510 (setq method (nth 2 item))
6511 (setq choices nil)))
6514 (defun tramp-find-method (multi-method method user host)
6515 "Return the right method string to use.
6516 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6517 If both MULTI-METHOD and METHOD are nil, do a lookup in
6518 `tramp-default-method-alist'."
6519 (or multi-method method (tramp-find-default-method user host)))
6521 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6522 ;; a user name is not provided in the "file name" very well.
6523 (defun tramp-dissect-multi-file-name (name)
6524 "Not implemented yet."
6525 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6526 (method-index (nth 1 tramp-multi-file-name-structure))
6527 (hops-index (nth 2 tramp-multi-file-name-structure))
6528 (localname-index (nth 3 tramp-multi-file-name-structure))
6529 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6530 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6531 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6532 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6533 method hops len hop-methods hop-users hop-hosts localname)
6534 (unless (string-match (format regexp hop-regexp) name)
6535 (error "Not a multi tramp file name: %s" name))
6536 (setq method (match-string method-index name))
6537 (setq hops (match-string hops-index name))
6538 (setq len (/ (length (match-data t)) 2))
6539 (when (< localname-index 0) (incf localname-index len))
6540 (setq localname (match-string localname-index name))
6542 (while (string-match hop-regexp hops index)
6543 (setq index (match-end 0))
6545 (cons (match-string hop-method-index hops) hop-methods))
6547 (cons (match-string hop-user-index hops) hop-users))
6549 (cons (match-string hop-host-index hops) hop-hosts))))
6550 (make-tramp-file-name
6551 :multi-method method
6552 :method (apply 'vector (reverse hop-methods))
6553 :user (apply 'vector (reverse hop-users))
6554 :host (apply 'vector (reverse hop-hosts))
6555 :localname localname)))
6557 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6558 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6560 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6562 (concat tramp-prefix-format
6563 (when method (concat "%m" tramp-postfix-single-method-format))
6564 (when user (concat "%u" tramp-postfix-user-format))
6565 (when host (concat "%h" tramp-postfix-host-format))
6566 (when localname (concat "%p")))
6567 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6569 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6570 ;; with this when USER is nil?
6571 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6572 "Constructs a tramp file name for a multi-hop method."
6573 (unless tramp-make-multi-tramp-file-format
6574 (error "`tramp-make-multi-tramp-file-format' is nil"))
6575 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6576 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6577 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6578 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6580 (localname (format-spec localname-format `((?p . ,localname))))
6582 (len (length method)))
6584 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6585 (setq hops (concat hops (format-spec hop-format
6586 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6588 (concat prefix hops localname)))
6590 (defun tramp-make-copy-program-file-name (user host localname)
6591 "Create a file name suitable to be passed to `rcp' and workalikes."
6593 (format "%s@%s:%s" user host localname)
6594 (format "%s:%s" host localname)))
6596 (defun tramp-method-out-of-band-p (multi-method method user host)
6597 "Return t if this is an out-of-band method, nil otherwise."
6598 (tramp-get-method-parameter
6600 (tramp-find-method multi-method method user host)
6601 user host 'tramp-copy-program))
6603 ;; Variables local to connection.
6605 (defun tramp-get-ls-command (multi-method method user host)
6607 (tramp-maybe-open-connection multi-method method user host)
6608 (set-buffer (tramp-get-buffer multi-method method user host))
6611 (defun tramp-get-test-groks-nt (multi-method method user host)
6613 (tramp-maybe-open-connection multi-method method user host)
6614 (set-buffer (tramp-get-buffer multi-method method user host))
6615 tramp-test-groks-nt))
6617 (defun tramp-get-file-exists-command (multi-method method user host)
6619 (tramp-maybe-open-connection multi-method method user host)
6620 (set-buffer (tramp-get-buffer multi-method method user host))
6621 tramp-file-exists-command))
6623 (defun tramp-get-remote-perl (multi-method method user host)
6624 (tramp-get-connection-property "perl" nil multi-method method user host))
6626 (defun tramp-get-remote-ln (multi-method method user host)
6627 (tramp-get-connection-property "ln" nil multi-method method user host))
6629 ;; Get a property of a TRAMP connection.
6630 (defun tramp-get-connection-property
6631 (property default multi-method method user host)
6632 "Get the named property for the connection.
6633 If the value is not set for the connection, return `default'"
6634 (tramp-maybe-open-connection multi-method method user host)
6635 (with-current-buffer (tramp-get-buffer multi-method method user host)
6638 (symbol-value (intern (concat "tramp-connection-property-" property)))
6641 ;; Set a property of a TRAMP connection.
6642 (defun tramp-set-connection-property
6643 (property value multi-method method user host)
6644 "Set the named property of a TRAMP connection."
6645 (tramp-maybe-open-connection multi-method method user host)
6646 (with-current-buffer (tramp-get-buffer multi-method method user host)
6647 (set (make-local-variable
6648 (intern (concat "tramp-connection-property-" property)))
6651 ;; Some predefined connection properties.
6652 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6653 (tramp-set-connection-property "remote-encoding" rem-enc
6654 multi-method method user host))
6655 (defun tramp-get-remote-encoding (multi-method method user host)
6656 (tramp-get-connection-property "remote-encoding" nil
6657 multi-method method user host))
6659 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6660 (tramp-set-connection-property "remote-decoding" rem-dec
6661 multi-method method user host))
6662 (defun tramp-get-remote-decoding (multi-method method user host)
6663 (tramp-get-connection-property "remote-decoding" nil
6664 multi-method method user host))
6666 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6667 (tramp-set-connection-property "local-encoding" loc-enc
6668 multi-method method user host))
6669 (defun tramp-get-local-encoding (multi-method method user host)
6670 (tramp-get-connection-property "local-encoding" nil
6671 multi-method method user host))
6673 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6674 (tramp-set-connection-property "local-decoding" loc-dec
6675 multi-method method user host))
6676 (defun tramp-get-local-decoding (multi-method method user host)
6677 (tramp-get-connection-property "local-decoding" nil
6678 multi-method method user host))
6680 (defun tramp-get-method-parameter (multi-method method user host param)
6681 "Return the method parameter PARAM.
6682 If the `tramp-methods' entry does not exist, use the variable PARAM
6684 (unless (boundp param)
6685 (error "Non-existing method parameter `%s'" param))
6686 (let ((entry (assoc param
6687 (assoc (tramp-find-method multi-method method user host)
6691 (symbol-value param))))
6694 ;; Auto saving to a special directory.
6696 (defun tramp-make-auto-save-file-name (fn)
6697 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
6698 (when tramp-auto-save-directory
6699 (unless (file-exists-p tramp-auto-save-directory)
6700 (make-directory tramp-auto-save-directory t)))
6701 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
6702 ;; file name we make sure that jka-compr isn't used for the
6704 (let ((buffer-file-name (expand-file-name
6705 (tramp-subst-strs-in-string '(("_" . "|")
6712 tramp-auto-save-directory)))
6713 (make-auto-save-file-name)))
6715 (defadvice make-auto-save-file-name
6716 (around tramp-advice-make-auto-save-file-name () activate)
6717 "Invoke `tramp-make-auto-save-file-name' for tramp files."
6718 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
6719 tramp-auto-save-directory)
6720 (setq ad-return-value
6721 (tramp-make-auto-save-file-name (buffer-file-name)))
6724 (defun tramp-subst-strs-in-string (alist string)
6725 "Replace all occurrences of the string FROM with TO in STRING.
6726 ALIST is of the form ((FROM . TO) ...)."
6729 (let* ((pr (car alist))
6732 (while (string-match (regexp-quote from) string)
6733 (setq string (replace-match to t t string)))
6734 (setq alist (cdr alist))))
6737 (defun tramp-insert-with-face (face string)
6738 "Insert text with a specific face."
6739 (let ((start (point)))
6741 (add-text-properties start (point) (list 'face face))))
6743 ;; ------------------------------------------------------------
6744 ;; -- Compatibility functions section --
6745 ;; ------------------------------------------------------------
6747 (defun tramp-temporary-file-directory ()
6748 "Return name of directory for temporary files (compat function).
6749 For Emacs, this is the variable `temporary-file-directory', for XEmacs
6750 this is the function `temp-directory'."
6751 (cond ((boundp 'temporary-file-directory)
6752 (symbol-value 'temporary-file-directory))
6753 ((fboundp 'temp-directory)
6754 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
6755 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
6756 (file-name-as-directory (getenv "TEMP")))
6757 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
6758 (file-name-as-directory (getenv "TMP")))
6759 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
6760 (file-name-as-directory (getenv "TMPDIR")))
6761 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
6762 (t (message (concat "Neither `temporary-file-directory' nor "
6763 "`temp-directory' is defined -- using /tmp."))
6764 (file-name-as-directory "/tmp"))))
6766 (defun tramp-read-passwd (user host prompt)
6767 "Read a password from user (compat function).
6768 Invokes `password-read' if available, `read-passwd' else."
6769 (if (functionp 'password-read)
6770 (let* ((key (concat (or user (user-login-name)) "@" host))
6771 (password (apply #'password-read (list prompt key))))
6772 (apply #'password-cache-add (list key password))
6774 (read-passwd prompt)))
6776 (defun tramp-clear-passwd (&optional user host)
6777 "Clear password cache for connection related to current-buffer."
6779 (let ((filename (or buffer-file-name list-buffers-directory "")))
6780 (when (and (functionp 'password-cache-remove)
6781 (or (and user host) (tramp-tramp-file-p filename)))
6782 (let* ((v (when (tramp-tramp-file-p filename)
6783 (tramp-dissect-file-name filename)))
6784 (luser (or user (tramp-file-name-user v) (user-login-name)))
6785 (lhost (or host (tramp-file-name-host v) (system-name)))
6786 (key (concat luser "@" lhost)))
6787 (apply #'password-cache-remove (list key))))))
6789 (defun tramp-time-diff (t1 t2)
6790 "Return the difference between the two times, in seconds.
6791 T1 and T2 are time values (as returned by `current-time' for example).
6793 NOTE: This function will fail if the time difference is too large to
6795 ;; Pacify byte-compiler with `symbol-function'.
6796 (cond ((and (fboundp 'subtract-time)
6797 (fboundp 'float-time))
6798 (funcall (symbol-function 'float-time)
6799 (funcall (symbol-function 'subtract-time) t1 t2)))
6800 ((and (fboundp 'subtract-time)
6801 (fboundp 'time-to-seconds))
6802 (funcall (symbol-function 'time-to-seconds)
6803 (funcall (symbol-function 'subtract-time) t1 t2)))
6804 ((fboundp 'itimer-time-difference)
6806 (symbol-function 'itimer-time-difference)
6807 (if (< (length t1) 3) (append t1 '(0)) t1)
6808 (if (< (length t2) 3) (append t2 '(0)) t2))))
6810 ;; snarfed from Emacs 21 time-date.el; combining
6811 ;; time-to-seconds and subtract-time
6812 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
6813 (list (- (car t1) (car t2) (if borrow 1 0))
6814 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
6815 (+ (* (car time) 65536.0)
6817 (/ (or (nth 2 time) 0) 1000000.0))))))
6819 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
6820 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
6821 EOL-TYPE can be one of `dos', `unix', or `mac'."
6822 (cond ((fboundp 'coding-system-change-eol-conversion)
6823 (apply #'coding-system-change-eol-conversion
6824 (list coding-system eol-type)))
6825 ((fboundp 'subsidiary-coding-system)
6827 #'subsidiary-coding-system
6829 (cond ((eq eol-type 'dos) 'crlf)
6830 ((eq eol-type 'unix) 'lf)
6831 ((eq eol-type 'mac) 'cr)
6833 (error "Unknown EOL-TYPE `%s', must be %s"
6835 "`dos', `unix', or `mac'"))))))
6836 (t (error "Can't change EOL conversion -- is MULE missing?"))))
6838 (defun tramp-split-string (string pattern)
6839 "Like `split-string' but omit empty strings.
6840 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
6841 This is, the first, empty, element is omitted. In XEmacs, the first
6842 element is not omitted.
6844 Note: this function has been written for `tramp-handle-file-truename'.
6845 If you want to use it for something else, you'll have to check whether
6846 it does the right thing."
6847 (delete "" (split-string string pattern)))
6849 (defun tramp-set-process-query-on-exit-flag (process flag)
6850 "Specify if query is needed for process when Emacs is exited.
6851 If the second argument flag is non-nil, Emacs will query the user before
6852 exiting if process is running."
6853 (if (fboundp 'set-process-query-on-exit-flag)
6854 (set-process-query-on-exit-flag process flag)
6855 (funcall (symbol-function 'process-kill-without-query)
6859 ;; ------------------------------------------------------------
6860 ;; -- Kludges section --
6861 ;; ------------------------------------------------------------
6863 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
6864 ;; does not deal well with newline characters. Newline is replaced by
6865 ;; backslash newline. But if, say, the string `a backslash newline b'
6866 ;; is passed to a shell, the shell will expand this into "ab",
6867 ;; completely omitting the newline. This is not what was intended.
6868 ;; It does not appear to be possible to make the function
6869 ;; `shell-quote-argument' work with newlines without making it
6870 ;; dependent on the shell used. But within this package, we know that
6871 ;; we will always use a Bourne-like shell, so we use an approach which
6874 ;; The approach is simple: we call `shell-quote-argument', then
6875 ;; massage the newline part of the result.
6877 ;; This function should produce a string which is grokked by a Unix
6878 ;; shell, even if the Emacs is running on Windows. Since this is the
6879 ;; kludges section, we bind `system-type' in such a way that
6880 ;; `shell-quote-arguments' behaves as if on Unix.
6882 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
6883 ;; function to work with Bourne-like shells.
6885 ;; CCC: This function should be rewritten so that
6886 ;; `shell-quote-argument' is not used. This way, we are safe from
6887 ;; changes in `shell-quote-argument'.
6888 (defun tramp-shell-quote-argument (s)
6889 "Similar to `shell-quote-argument', but groks newlines.
6890 Only works for Bourne-like shells."
6891 (let ((system-type 'not-windows))
6893 (let ((result (shell-quote-argument s))
6894 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
6895 (when (and (>= (length result) 2)
6896 (string= (substring result 0 2) "\\~"))
6897 (setq result (substring result 1)))
6898 (while (string-match nl result)
6899 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
6903 ;; ;; EFS hooks itself into the file name handling stuff in more places
6904 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
6905 ;; ;; away from tramp.el file names.
6907 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
6908 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
6909 ;; ;; connection to help it's dired process. Not that I have any real
6910 ;; ;; idea *why* this is helpful to dired.
6912 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
6914 ;; ;; Daniel Pittman <daniel@danann.net>
6916 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
6917 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
6918 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
6919 ;; ;; EFS might nicht be loaded yet.
6920 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
6921 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
6922 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
6926 ;; We currently (sometimes) use "[" and "]" in the filename format.
6927 ;; This means that Emacs wants to expand wildcards if
6928 ;; `find-file-wildcards' is non-nil, and then barfs because no
6929 ;; expansion could be found. We detect this situation and do
6930 ;; something really awful: we have `file-expand-wildcards' return the
6931 ;; original filename if it can't expand anything. Let's just hope
6932 ;; that this doesn't break anything else.
6933 ;; CCC: This check is now also really awful; we should search all
6934 ;; of the filename format, not just the prefix.
6935 (when (string-match "\\[" tramp-prefix-format)
6936 (defadvice file-expand-wildcards (around tramp-fix activate)
6937 (let ((name (ad-get-arg 0)))
6938 (if (tramp-tramp-file-p name)
6939 ;; If it's a Tramp file, dissect it and look if wildcards
6940 ;; need to be expanded at all.
6941 (let ((v (tramp-dissect-file-name name)))
6942 (if (string-match "[[*?]" (tramp-file-name-localname v))
6943 (let ((res ad-do-it))
6944 (setq ad-return-value (or res (list name))))
6945 (setq ad-return-value (list name))))
6946 ;; If it is not a Tramp file, just run the original function.
6947 (let ((res ad-do-it))
6948 (setq ad-return-value (or res (list name)))))))
6951 ;; Tramp version is useful in a number of situations.
6953 (defun tramp-version (arg)
6954 "Print version number of tramp.el in minibuffer or current buffer."
6956 (if arg (insert tramp-version) (message tramp-version)))
6958 ;; Make the `reporter` functionality available for making bug reports about
6959 ;; the package. A most useful piece of code.
6961 (unless (fboundp 'reporter-submit-bug-report)
6962 (autoload 'reporter-submit-bug-report "reporter"))
6965 "Submit a bug report to the TRAMP developers."
6968 (let ((reporter-prompt-for-summary-p t))
6969 (reporter-submit-bug-report
6970 tramp-bug-report-address ; to-address
6971 (format "tramp (%s)" tramp-version) ; package name and version
6975 tramp-file-exists-command
6976 tramp-current-multi-method
6977 tramp-current-method
6982 tramp-auto-save-directory ; vars to dump
6983 tramp-default-method
6984 tramp-rsh-end-of-line
6985 tramp-default-password-end-of-line
6987 tramp-login-prompt-regexp
6988 tramp-password-prompt-regexp
6989 tramp-wrong-passwd-regexp
6990 tramp-yesno-prompt-regexp
6991 tramp-yn-prompt-regexp
6992 tramp-terminal-prompt-regexp
6993 tramp-out-of-band-prompt-regexp
6994 tramp-temp-name-prefix
6995 tramp-file-name-structure
6996 tramp-file-name-regexp
6997 tramp-multi-file-name-structure
6998 tramp-multi-file-name-hop-structure
7000 tramp-multi-connection-function-alist
7003 tramp-coding-commands
7004 tramp-actions-before-shell
7005 tramp-actions-copy-out-of-band
7008 tramp-shell-prompt-pattern
7010 ,(when (boundp 'tramp-backup-directory-alist)
7011 'tramp-backup-directory-alist)
7012 ,(when (boundp 'tramp-bkup-backup-directory-info)
7013 'tramp-bkup-backup-directory-info)
7015 ;; Non-tramp variables of interest
7016 shell-prompt-pattern
7018 backup-by-copying-when-linked
7019 backup-by-copying-when-mismatch
7020 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7021 'backup-by-copying-when-privileged-mismatch)
7022 ,(when (boundp 'password-cache)
7024 ,(when (boundp 'password-cache-expiry)
7025 'password-cache-expiry)
7026 ,(when (boundp 'backup-directory-alist)
7027 'backup-directory-alist)
7028 ,(when (boundp 'bkup-backup-directory-info)
7029 'bkup-backup-directory-info)
7030 file-name-handler-alist)
7034 Enter your bug report in this message, including as much detail as you
7035 possibly can about the problem, what you did to cause it and what the
7036 local and remote machines are.
7038 If you can give a simple set of instructions to make this bug happen
7039 reliably, please include those. Thank you for helping kill bugs in
7042 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7043 the ~/.emacs file and to repeat the bug. Then, include the contents
7044 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7047 --bug report follows this line--
7050 (defalias 'tramp-submit-bug 'tramp-bug)
7054 ;; Make sure that we get integration with the VC package.
7055 ;; When it is loaded, we need to pull in the integration module.
7056 ;; This must come after (provide 'tramp) because tramp-vc.el
7058 (eval-after-load "vc"
7059 '(require 'tramp-vc))
7063 ;; * Allow putting passwords in the filename.
7064 ;; This should be implemented via a general mechanism to add
7065 ;; parameters in filenames. There is currently a kludge for
7066 ;; putting the port number into the filename for ssh and ftp
7067 ;; files. This could be subsumed by the new mechanism as well.
7068 ;; Another approach is to read a netrc file like ~/.authinfo
7070 ;; * Handle nonlocal exits such as C-g.
7071 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7072 ;; * Add fallback for inline encodings. This should be used
7073 ;; if the remote end doesn't support mimencode or a similar program.
7074 ;; For reading files from the remote host, we can just parse the output
7075 ;; of `od -b'. For writing files to the remote host, we construct
7076 ;; a shell program which contains only "safe" ascii characters
7077 ;; and which writes the right bytes to the file. We can use printf(1)
7078 ;; or "echo -e" or the printf function in awk and use octal escapes
7079 ;; for the "dangerous" characters. The null byte might be a problem.
7080 ;; On some systems, the octal escape doesn't work. So we try the following
7081 ;; two commands to write a null byte:
7082 ;; dd if=/dev/zero bs=1 count=1
7083 ;; echo | tr '\n' '\000'
7084 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7085 ;; the two via a format which can be `uu' or `b64'. Then we can search
7086 ;; for the right local commands and the right remote commands separately.
7087 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7088 ;; work for remote files.
7089 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7090 ;; `shell-quote-argument'.
7091 ;; * Completion gets confused when you leave out the method name.
7092 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7093 ;; by the files in that directory. Add this here.
7094 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7095 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7096 ;; * When logging in, keep looking for questions according to an alist
7097 ;; and then invoke the right function.
7098 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7099 ;; * Running CVS remotely doesn't appear to work right. It thinks
7100 ;; files are locked by somebody else even if I'm the locking user.
7101 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7103 ;; * Don't use globbing for directories with many files, as this is
7104 ;; likely to produce long command lines, and some shells choke on
7105 ;; long command lines.
7106 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7107 ;; do the right thing.
7108 ;; * `vc-directory' does not work. It never displays any files, even
7109 ;; if it does show files when run locally.
7110 ;; * Allow correction of passwords, if the remote end allows this.
7111 ;; (Mark Hershberger)
7112 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7113 ;; * Do asynchronous `shell-command's.
7114 ;; * Grok `append' parameter for `write-region'.
7115 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7116 ;; * abbreviate-file-name
7117 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7118 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7119 ;; what the user names should default to, though.
7120 ;; * better error checking. At least whenever we see something
7121 ;; strange when doing zerop, we should kill the process and start
7122 ;; again. (Greg Stark)
7123 ;; * Add caching for filename completion. (Greg Stark)
7124 ;; Of course, this has issues with usability (stale cache bites)
7125 ;; -- <daniel@danann.net>
7126 ;; * Provide a local cache of old versions of remote files for the rsync
7127 ;; transfer method to use. (Greg Stark)
7128 ;; * Remove unneeded parameters from methods.
7129 ;; * Invoke rsync once for copying a whole directory hierarchy.
7130 ;; (Francesco Potort
\e,Al
\e(B)
7131 ;; * Should we set PATH ourselves or should we rely on the remote end
7133 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7134 ;; * Make it work for different encodings, and for different file name
7135 ;; encodings, too. (Daniel Pittman)
7136 ;; * Change applicable functions to pass a struct tramp-file-name rather
7137 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7138 ;; * Implement asynchronous shell commands.
7139 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7140 ;; * Progress reports while copying files. (Michael Kifer)
7141 ;; * `Smart' connection method that uses inline for small and out of
7142 ;; band for large files. (Michael Kifer)
7143 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7144 ;; then look if it's the right version (with `perl -v').
7145 ;; * When editing a remote CVS controlled file as a different user, VC
7146 ;; gets confused about the file locking status. Try to find out why
7147 ;; the workaround doesn't work.
7148 ;; * Change `copy-file' to grok the case where the filename handler
7149 ;; for the source and the target file are different. Right now,
7150 ;; it looks at the source file and then calls that handler, if
7151 ;; there is one. But since ange-ftp, for instance, does not know
7152 ;; about Tramp, it does not do the right thing if the target file
7153 ;; name is a Tramp name.
7154 ;; * Username and hostname completion.
7155 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7156 ;; connect to host "blabla" already if that host is unique. No idea
7157 ;; how to suppress. Maybe not an essential problem.
7158 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7159 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7160 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7161 ;; Code is nearly identical.
7162 ;; ** Decide whiche files to take for searching user/host names depending on
7163 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7164 ;; ** Enhance variables for debug.
7165 ;; ** Implement "/multi:" completion.
7166 ;; ** Add a learning mode for completion. Make results persistent.
7167 ;; * Allow out-of-band methods as _last_ multi-hop.
7169 ;; Functions for file-name-handler-alist:
7170 ;; diff-latest-backup-file -- in diff.el
7171 ;; dired-uncache -- this will be needed when we do insert-directory caching
7172 ;; file-name-as-directory -- use primitive?
7173 ;; file-name-sans-versions -- use primitive?
7174 ;; get-file-buffer -- use primitive
7177 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7178 ;;; tramp.el ends here