1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol -*- coding: iso-8859-1; -*-
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Kai.Grossjohann@CS.Uni-Dortmund.DE
6 ;; Keywords: comm, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This package provides remote file editing, similar to ange-ftp.
28 ;; The difference is that ange-ftp uses FTP to transfer files between
29 ;; the local and the remote host, whereas tramp.el uses a combination
30 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
32 ;; For more detailed instructions, please see the info file, which is
33 ;; included in the file `tramp.tar.gz' mentioned below.
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.el can be retrieved from the following
50 ;; URL: ftp://ls6-ftp.cs.uni-dortmund.de/pub/src/emacs/tramp.tar.gz
51 ;; For your convenience, the *.el file is available separately from
52 ;; the same directory.
54 ;; There's a mailing list for this, as well. Its name is:
55 ;; tramp-devel@mail.freesoftware.fsf.org
56 ;; Send a mail with `help' in the subject (!) to the administration
57 ;; address for instructions on joining the list. The administration
59 ;; tramp-devel-request@mail.freesoftware.fsf.org
60 ;; You can also use the Web to subscribe, under the following URL:
61 ;; http://mail.freesoftware.fsf.org/mailman/listinfo/tramp-devel
63 ;; For the adventurous, the current development sources are available
64 ;; via CVS. You can find instructions about this at the following URL:
65 ;; http://savannah.gnu.org/projects/tramp/
66 ;; Click on "CVS" in the navigation bar near the top.
68 ;; Don't forget to put on your asbestos longjohns, first!
72 ;; In the Tramp CVS repository, the version numer is auto-frobbed from
73 ;; the Makefile, so you should edit the top-level Makefile to change
74 ;; the version number.
75 (defconst tramp-version "2.0.22"
76 "This version of tramp.")
78 (defconst tramp-bug-report-address "tramp-devel@mail.freesoftware.fsf.org"
79 "Email address to send bug reports to.")
82 (require 'format-spec) ;from Gnus 5.8, also in tar ball
83 ;; The explicit check is not necessary in Emacs, which provides the
84 ;; feature even if implemented in C, but it appears to be necessary
86 (unless (and (fboundp 'base64-encode-region)
87 (fboundp 'base64-decode-region))
88 (require 'base64)) ;for the mimencode methods
92 (autoload 'tramp-uuencode-region "tramp-uu"
93 "Implementation of `uuencode' in Lisp.")
95 (unless (fboundp 'uudecode-decode-region)
96 (autoload 'uudecode-decode-region "uudecode"))
98 ;; ;; It does not work to load EFS after loading TRAMP.
99 ;; (when (fboundp 'efs-file-handler-function)
105 ;; Emacs 19.34 compatibility hack -- is this needed?
106 (or (>= emacs-major-version 20)
109 (unless (boundp 'custom-print-functions)
110 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
112 ;; XEmacs is distributed with few Lisp packages. Further packages are
113 ;; installed using EFS. If we use a unified filename format, then
114 ;; Tramp is required in addition to EFS. (But why can't Tramp just
115 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
116 ;; just like before.) Another reason for using a separate filename
117 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
118 ;; Tramp only knows how to deal with `file-name-handler-alist', not
120 (defvar tramp-unified-filenames (not (featurep 'xemacs))
121 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
122 Nil means to use a separate filename syntax for Tramp.")
124 ;;; User Customizable Internal Variables:
127 "Edit remote files with a combination of rsh and rcp or similar programs."
130 (defcustom tramp-verbose 9
131 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
135 (defcustom tramp-debug-buffer nil
136 "*Whether to send all commands and responses to a debug buffer."
140 (defcustom tramp-auto-save-directory nil
141 "*Put auto-save files in this directory, if set.
142 The idea is to use a local directory so that auto-saving is faster."
144 :type '(choice (const nil)
147 (defcustom tramp-encoding-shell
148 (if (memq system-type '(windows-nt))
151 "*Use this program for encoding and decoding commands on the local host.
152 This shell is used to execute the encoding and decoding command on the
153 local host, so if you want to use `~' in those commands, you should
154 choose a shell here which groks tilde expansion. `/bin/sh' normally
155 does not understand tilde expansion.
157 For encoding and deocding, commands like the following are executed:
159 /bin/sh -c COMMAND < INPUT > OUTPUT
161 This variable can be used to change the \"/bin/sh\" part. See the
162 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
163 variable `tramp-encoding-reads-stdin' to specify whether the commands read
164 standard input or a file.
166 Note that this variable is not used for remote commands. There are
167 mechanisms in tramp.el which automatically determine the right shell to
168 use for the remote host."
170 :type '(file :must-match t))
172 (defcustom tramp-encoding-command-switch
173 (if (string-match "cmd\\.exe" tramp-encoding-shell)
176 "*Use this switch together with `tramp-encoding-shell' for local commands.
177 See the variable `tramp-encoding-shell' for more information."
181 (defcustom tramp-encoding-reads-stdin t
182 "*If non-nil, encoding commands read from standard input.
183 If nil, the filename is the last argument.
185 Note that the commands always must write to standard output."
189 (defcustom tramp-multi-sh-program
191 "*Use this program for bootstrapping multi-hop connections.
192 This variable is similar to `tramp-encoding-shell', but it is only used
193 when initializing a multi-hop connection. Therefore, the set of
194 commands sent to this shell is quite restricted, and if you are
195 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
196 shell which does not normally exist on Windows anyway).
198 To use multi-hop methods from Windows, you also need suitable entries
199 in `tramp-multi-connection-function-alist' for the first hop.
201 This variable defaults to the value of `tramp-encoding-shell'."
203 :type '(file :must-match t))
205 ;; CCC I have changed all occurrences of comint-quote-filename with
206 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
207 ;; There, comint-quote-filename was removed altogether. If it turns
208 ;; out to be necessary there, something will need to be done.
209 ;;-(defcustom tramp-file-name-quote-list
210 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
211 ;;- "*Protect these characters from the remote shell.
212 ;;-Any character in this list is quoted (preceded with a backslash)
213 ;;-because it means something special to the shell. This takes effect
214 ;;-when sending file and directory names to the remote shell.
216 ;;-See `comint-file-name-quote-list' for details."
218 ;;- :type '(repeat character))
220 (defcustom tramp-methods
221 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
222 (tramp-rsh-program "rsh")
223 (tramp-rcp-program "rcp")
224 (tramp-remote-sh "/bin/sh")
227 (tramp-rcp-keep-date-arg "-p")
228 (tramp-su-program nil)
230 (tramp-telnet-program nil)
231 (tramp-telnet-args nil))
232 ("scp" (tramp-connection-function tramp-open-connection-rsh)
233 (tramp-rsh-program "ssh")
234 (tramp-rcp-program "scp")
235 (tramp-remote-sh "/bin/sh")
236 (tramp-rsh-args ("-e" "none"))
238 (tramp-rcp-keep-date-arg "-p")
239 (tramp-su-program nil)
241 (tramp-telnet-program nil)
242 (tramp-telnet-args nil))
243 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
244 (tramp-rsh-program "ssh")
245 (tramp-rcp-program "scp")
246 (tramp-remote-sh "/bin/sh")
247 (tramp-rsh-args ("-1" "-e" "none"))
248 (tramp-rcp-args ("-1"))
249 (tramp-rcp-keep-date-arg "-p")
250 (tramp-su-program nil)
252 (tramp-telnet-program nil)
253 (tramp-telnet-args nil))
254 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
255 (tramp-rsh-program "ssh")
256 (tramp-rcp-program "scp")
257 (tramp-remote-sh "/bin/sh")
258 (tramp-rsh-args ("-2" "-e" "none"))
259 (tramp-rcp-args ("-2"))
260 (tramp-rcp-keep-date-arg "-p")
261 (tramp-su-program nil)
263 (tramp-telnet-program nil)
264 (tramp-telnet-args nil))
266 (tramp-connection-function tramp-open-connection-rsh)
267 (tramp-rsh-program "ssh1")
268 (tramp-rcp-program "scp1")
269 (tramp-remote-sh "/bin/sh")
270 (tramp-rsh-args ("-e" "none"))
272 (tramp-rcp-keep-date-arg "-p")
273 (tramp-su-program nil)
275 (tramp-telnet-program nil)
276 (tramp-telnet-args nil))
278 (tramp-connection-function tramp-open-connection-rsh)
279 (tramp-rsh-program "ssh2")
280 (tramp-rcp-program "scp2")
281 (tramp-remote-sh "/bin/sh")
282 (tramp-rsh-args ("-e" "none"))
284 (tramp-rcp-keep-date-arg "-p")
285 (tramp-su-program nil)
287 (tramp-telnet-program nil)
288 (tramp-telnet-args nil))
289 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
290 (tramp-rsh-program "ssh")
291 (tramp-rcp-program "rsync")
292 (tramp-remote-sh "/bin/sh")
293 (tramp-rsh-args ("-e" "none"))
294 (tramp-rcp-args ("-e" "ssh"))
295 (tramp-rcp-keep-date-arg "-t")
296 (tramp-su-program nil)
298 (tramp-telnet-program nil)
299 (tramp-telnet-args nil))
300 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
301 (tramp-rsh-program "rsh")
302 (tramp-rcp-program nil)
303 (tramp-remote-sh "/bin/sh")
306 (tramp-rcp-keep-date-arg nil)
307 (tramp-su-program nil)
309 (tramp-telnet-program nil)
310 (tramp-telnet-args nil))
311 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
312 (tramp-rsh-program "ssh")
313 (tramp-rcp-program nil)
314 (tramp-remote-sh "/bin/sh")
315 (tramp-rsh-args ("-e" "none"))
317 (tramp-rcp-keep-date-arg nil)
318 (tramp-su-program nil)
320 (tramp-telnet-program nil)
321 (tramp-telnet-args nil))
322 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
323 (tramp-rsh-program "ssh")
324 (tramp-rcp-program nil)
325 (tramp-remote-sh "/bin/sh")
326 (tramp-rsh-args ("-1" "-e" "none"))
327 (tramp-rcp-args ("-1"))
328 (tramp-rcp-keep-date-arg nil)
329 (tramp-su-program nil)
331 (tramp-telnet-program nil)
332 (tramp-telnet-args nil))
333 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
334 (tramp-rsh-program "ssh")
335 (tramp-rcp-program nil)
336 (tramp-remote-sh "/bin/sh")
337 (tramp-rsh-args ("-2" "-e" "none"))
338 (tramp-rcp-args ("-2"))
339 (tramp-rcp-keep-date-arg nil)
340 (tramp-su-program nil)
342 (tramp-telnet-program nil)
343 (tramp-telnet-args nil))
345 (tramp-connection-function tramp-open-connection-rsh)
346 (tramp-rsh-program "ssh1")
347 (tramp-rcp-program nil)
348 (tramp-remote-sh "/bin/sh")
349 (tramp-rsh-args ("-e" "none"))
351 (tramp-rcp-keep-date-arg nil)
352 (tramp-su-program nil)
354 (tramp-telnet-program nil)
355 (tramp-telnet-args nil))
357 (tramp-connection-function tramp-open-connection-rsh)
358 (tramp-rsh-program "ssh2")
359 (tramp-rcp-program nil)
360 (tramp-remote-sh "/bin/sh")
361 (tramp-rsh-args ("-e" "none"))
363 (tramp-rcp-keep-date-arg nil)
364 (tramp-su-program nil)
366 (tramp-telnet-program nil)
367 (tramp-telnet-args nil))
369 (tramp-connection-function tramp-open-connection-telnet)
370 (tramp-rsh-program nil)
371 (tramp-rcp-program nil)
372 (tramp-remote-sh "/bin/sh")
375 (tramp-rcp-keep-date-arg nil)
376 (tramp-su-program nil)
378 (tramp-telnet-program "telnet")
379 (tramp-telnet-args nil))
380 ("su" (tramp-connection-function tramp-open-connection-su)
381 (tramp-rsh-program nil)
382 (tramp-rcp-program nil)
383 (tramp-remote-sh "/bin/sh")
386 (tramp-rcp-keep-date-arg nil)
387 (tramp-su-program "su")
388 (tramp-su-args ("-" "%u"))
389 (tramp-telnet-program nil)
390 (tramp-telnet-args nil))
391 ("sudo" (tramp-connection-function tramp-open-connection-su)
392 (tramp-rsh-program nil)
393 (tramp-rcp-program nil)
394 (tramp-remote-sh "/bin/sh")
397 (tramp-rcp-keep-date-arg nil)
398 (tramp-su-program "sudo")
399 (tramp-su-args ("-u" "%u" "-s"))
400 (tramp-telnet-program nil)
401 (tramp-telnet-args nil))
402 ("multi" (tramp-connection-function tramp-open-connection-multi)
403 (tramp-rsh-program nil)
404 (tramp-rcp-program nil)
405 (tramp-remote-sh "/bin/sh")
408 (tramp-rcp-keep-date-arg nil)
409 (tramp-su-program nil)
411 (tramp-telnet-program nil)
412 (tramp-telnet-args nil))
413 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
414 (tramp-rsh-program "ssh")
415 (tramp-rcp-program "scp")
416 (tramp-remote-sh "/bin/sh")
417 (tramp-rsh-args ("-e" "none" "-t" "-t" "/bin/sh"))
419 (tramp-rcp-keep-date-arg "-p")
420 (tramp-telnet-program nil)
421 (tramp-telnet-args nil))
422 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
423 (tramp-rsh-program "ssh")
424 (tramp-rcp-program nil)
425 (tramp-remote-sh "/bin/sh")
426 (tramp-rsh-args ("-e" "none" "-t" "-t" "/bin/sh"))
428 (tramp-rcp-keep-date-arg nil)
429 (tramp-su-program nil)
431 (tramp-telnet-program nil)
432 (tramp-telnet-args nil))
434 (tramp-connection-function tramp-open-connection-rsh)
435 (tramp-rsh-program "krlogin")
436 (tramp-rcp-program nil)
437 (tramp-remote-sh "/bin/sh")
438 (tramp-rsh-args ("-x"))
440 (tramp-rcp-keep-date-arg nil)
441 (tramp-su-program nil)
443 (tramp-telnet-program nil)
444 (tramp-telnet-args nil))
446 (tramp-connection-function tramp-open-connection-rsh)
447 (tramp-rsh-program "plink")
448 (tramp-rcp-program nil)
449 (tramp-remote-sh "/bin/sh")
450 (tramp-rsh-args ("-ssh")) ;optionally add "-v"
452 (tramp-rcp-keep-date-arg nil)
453 (tramp-su-program nil)
455 (tramp-telnet-program nil)
456 (tramp-telnet-args nil))
458 (tramp-connection-function tramp-open-connection-rsh)
459 (tramp-rsh-program "plink")
460 (tramp-rcp-program "pscp")
461 (tramp-remote-sh "/bin/sh")
462 (tramp-rsh-args ("-ssh"))
464 (tramp-rcp-keep-date-arg "-p")
465 (tramp-su-program nil)
467 (tramp-telnet-program nil)
468 (tramp-telnet-args nil))
470 (tramp-connection-function tramp-open-connection-rsh)
471 (tramp-rsh-program "fsh")
472 (tramp-rcp-program "fcp")
473 (tramp-remote-sh "/bin/sh -i")
474 (tramp-rsh-args ("sh" "-i"))
476 (tramp-rcp-keep-date-arg "-p")
477 (tramp-su-program nil)
479 (tramp-telnet-program nil)
480 (tramp-telnet-args nil))
482 "*Alist of methods for remote files.
483 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
484 Each NAME stands for a remote access method. Each PARAM is a
485 pair of the form (KEY VALUE). The following KEYs are defined:
486 * `tramp-connection-function'
487 This specifies the function to use to connect to the remote host.
488 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
489 and `tramp-open-connection-su' are defined. See the documentation
490 of these functions for more details.
492 This specifies the Bourne shell to use on the remote host. This
493 MUST be a Bourne-like shell. It is normally not necessary to set
494 this to any value other than \"/bin/sh\": tramp wants to use a shell
495 which groks tilde expansion, but it can search for it. Also note
496 that \"/bin/sh\" exists on all Unixen, this might not be true for
497 the value that you decide to use. You Have Been Warned.
498 * `tramp-rsh-program'
499 This specifies the name of the program to use for rsh; this might be
500 the full path to rsh or the name of a workalike program.
502 This specifies the list of arguments to pass to the above
503 mentioned program. Please note that this is a list of arguments,
504 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
505 here. Instead, you want two list elements, one for \"-a\" and one
506 for \"-b\", or one for \"-f\" and one for \"foo\".
507 * `tramp-rcp-program'
508 This specifies the name of the program to use for rcp; this might be
509 the full path to rcp or the name of a workalike program.
511 This specifies the list of parameters to pass to the above mentioned
512 program, the hints for `tramp-rsh-args' also apply here.
513 * `tramp-rcp-keep-date-arg'
514 This specifies the parameter to use for `rcp' when the timestamp
515 of the original file should be kept. For `rcp', use `-p', for
518 This specifies the name of the program to use for `su'.
520 This specifies the list of arguments to pass to `su'.
521 \"%u\" is replaced by the user name, use \"%%\" for a literal
523 * `tramp-telnet-program'
524 Specifies the telnet program to use when using
525 `tramp-open-connection-telnet' to log in.
526 * `tramp-telnet-args'
527 Specifies list of arguments to pass to `telnet'. The hints for
528 `tramp-rsh-args' also apply here.
530 What does all this mean? Well, you should specify `tramp-rsh-program',
531 `tramp-telnet-program' or `tramp-su-program' for all methods; this program
532 is used to log in to the remote site. Then, there are two ways to
533 actually transfer the files between the local and the remote side.
534 One way is using an additional rcp-like program. If you want to do
535 this, set `tramp-rcp-program' in the method.
537 Another possibility for file transfer is inline transfer, i.e. the
538 file is passed through the same buffer used by `tramp-rsh-program'. In
539 this case, the file contents need to be protected since the
540 `tramp-rsh-program' might use escape codes or the connection might not
541 be eight-bit clean. Therefore, file contents are encoded for transit.
542 See the variable `tramp-coding-commands' for details.
544 So, to summarize: if the method is an out-of-band method, then you
545 must specify `tramp-rcp-program' and `tramp-rcp-args'. If it is an
546 inline method, then these two parameters should be nil. Every method,
547 inline or out of band, must specify `tramp-connection-function' plus
548 the associated arguments (for example, the telnet program if you chose
549 `tramp-open-connection-telnet').
553 When using `tramp-open-connection-su' the phrase `open connection to a
554 remote host' sounds strange, but it is used nevertheless, for
555 consistency. No connection is opened to a remote host, but `su' is
556 started on the local host. You are not allowed to specify a remote
557 host other than `localhost' or the name of the local host."
561 (set (list (const tramp-connection-function) function)
562 (list (const tramp-rsh-program)
563 (choice (const nil) string))
564 (list (const tramp-rcp-program)
565 (choice (const nil) string))
566 (list (const tramp-remote-sh)
567 (choice (const nil) string))
568 (list (const tramp-rsh-args) (repeat string))
569 (list (const tramp-rcp-args) (repeat string))
570 (list (const tramp-rcp-keep-date-arg)
571 (choice (const nil) string))
572 (list (const tramp-su-program)
573 (choice (const nil) string))
574 (list (const tramp-su-args) (repeat string))
575 (list (const tramp-encoding-command)
576 (choice (const nil) string))
577 (list (const tramp-decoding-command)
578 (choice (const nil) string))
579 (list (const tramp-encoding-function)
580 (choice (const nil) function))
581 (list (const tramp-decoding-function)
582 (choice (const nil) function))
583 (list (const tramp-telnet-program)
584 (choice (const nil) string))
585 (list (const tramp-telnet-args) (repeat string))))))
587 (defcustom tramp-multi-methods '("multi" "multiu")
588 "*List of multi-hop methods.
589 Each entry in this list should be a method name as mentioned in the
590 variable `tramp-methods'."
592 :type '(repeat string))
594 (defcustom tramp-multi-connection-function-alist
595 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
596 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
597 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
598 ("su" tramp-multi-connect-su "su - %u%n")
599 ("sudo" tramp-multi-connect-su "sudo -u %u -s%n"))
600 "*List of connection functions for multi-hop methods.
601 Each list item is a list of three items (METHOD FUNCTION COMMAND),
602 where METHOD is the name as used in the file name, FUNCTION is the
603 function to be executed, and COMMAND is the shell command used for
606 COMMAND may contain percent escapes. `%u' will be replaced with the
607 user name, `%h' will be replaced with the host name, and `%n' will be
608 replaced with an end-of-line character, as specified in the variable
609 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
610 Note that the interpretation of the percent escapes also depends on
611 the FUNCTION. For example, the `%u' escape is forbidden with the
612 function `tramp-multi-connect-telnet'. See the documentation of the
613 various functions for details."
615 :type '(repeat (list string function string)))
617 (defcustom tramp-default-method "ssh"
618 "*Default method to use for transferring files.
619 See `tramp-methods' for possibilities.
620 Also see `tramp-default-method-alist'.
622 Emacs uses a unified filename syntax for Tramp and Ange-FTP.
623 For backward compatibility, the default value of this variable
624 is \"ftp\" on Emacs. But XEmacs uses a separate filename syntax
625 for Tramp and EFS, so there the default method is \"sm\"."
629 (defcustom tramp-default-method-alist
630 (when tramp-unified-filenames
631 '(("\\`ftp\\." "" "ftp")
632 ("" "\\`\\(anonymous\\|ftp\\)\\'" "ftp")))
633 "*Default method to use for specific user/host pairs.
634 This is an alist of items (HOST USER METHOD). The first matching item
635 specifies the method to use for a file name which does not specify a
636 method. HOST and USER are regular expressions or nil, which is
637 interpreted as a regular expression which always matches. If no entry
638 matches, the variable `tramp-default-method' takes effect.
640 If the file name does not specify the user, lookup is done using the
641 empty string for the user name.
643 See `tramp-methods' for a list of possibilities for METHOD."
645 :type '(repeat (list (regexp :tag "Host regexp")
646 (regexp :tag "User regexp")
647 (string :tag "Method"))))
649 (defcustom tramp-ftp-method "ftp"
650 "*When this method name is used, forward all calls to Ange-FTP."
654 ;; Default values for non-Unices seeked
655 (defconst tramp-completion-function-alist-rsh
656 (unless (memq system-type '(windows-nt))
657 '((tramp-parse-rhosts "/etc/hosts.equiv")
658 (tramp-parse-rhosts "~/.rhosts")))
659 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods."
662 ;; Default values for non-Unices seeked
663 (defconst tramp-completion-function-alist-ssh
664 (unless (memq system-type '(windows-nt))
665 '((tramp-parse-rhosts "/etc/hosts.equiv")
666 (tramp-parse-rhosts "/etc/shosts.equiv")
667 (tramp-parse-shosts "/etc/ssh_known_hosts")
668 (tramp-parse-rhosts "~/.rhosts")
669 (tramp-parse-rhosts "~/.shosts")
670 (tramp-parse-shosts "~/.ssh/known_hosts")))
671 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods."
674 ;; Default values for non-Unices seeked
675 (defconst tramp-completion-function-alist-telnet
676 (unless (memq system-type '(windows-nt))
677 '((tramp-parse-hosts "/etc/hosts")))
678 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods."
681 ;; Default values for non-Unices seeked
682 (defconst tramp-completion-function-alist-su
683 (unless (memq system-type '(windows-nt))
684 '((tramp-parse-passwd "/etc/passwd")))
685 "Default list of (FUNCTION FILE) pairs to be examined for su methods."
688 ;; Default values for non-Unices seeked
689 (defconst tramp-completion-function-alist-ftp
690 (unless (memq system-type '(windows-nt))
691 '((tramp-parse-netrc "~/.netrc")))
692 "Default list of (FUNCTION FILE) pairs to be examined for ftp methods."
695 (defcustom tramp-completion-function-alist
696 (list (cons "rcp" tramp-completion-function-alist-rsh)
697 (cons "scp" tramp-completion-function-alist-ssh)
698 (cons "scp1" tramp-completion-function-alist-ssh)
699 (cons "scp2" tramp-completion-function-alist-ssh)
700 (cons "scp1-old" tramp-completion-function-alist-ssh)
701 (cons "scp2-old" tramp-completion-function-alist-ssh)
702 (cons "rsync" tramp-completion-function-alist-rsh)
703 (cons "rsh" tramp-completion-function-alist-rsh)
704 (cons "ssh" tramp-completion-function-alist-ssh)
705 (cons "ssh1" tramp-completion-function-alist-ssh)
706 (cons "ssh2" tramp-completion-function-alist-ssh)
707 (cons "ssh1-old" tramp-completion-function-alist-ssh)
708 (cons "ssh2-old" tramp-completion-function-alist-ssh)
709 (cons "telnet" tramp-completion-function-alist-telnet)
710 (cons "su" tramp-completion-function-alist-su)
711 (cons "sudo" tramp-completion-function-alist-su)
713 (cons "scpx" tramp-completion-function-alist-ssh)
714 (cons "sshx" tramp-completion-function-alist-ssh)
715 (cons "krlogin" tramp-completion-function-alist-rsh)
716 (cons "plink" tramp-completion-function-alist-ssh)
717 (cons "pscp" tramp-completion-function-alist-ssh)
718 (cons "ftp" tramp-completion-function-alist-ftp)
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-hosts' for \"/etc/hosts\" like files, and
730 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
731 * `tramp-parse-netrc ' for \".netrc\" like files.
733 FUNCTION can also see a customer defined function. For more details see
738 (choice (const nil) (repeat (list function file))))))
740 (defcustom tramp-rsh-end-of-line "\n"
741 "*String used for end of line in rsh connections.
742 I don't think this ever needs to be changed, so please tell me about it
743 if you need to change this.
744 Also see `tramp-password-end-of-line'."
748 (defcustom tramp-password-end-of-line tramp-rsh-end-of-line
749 "*String used for end of line after sending a password.
750 It seems that people using plink under Windows need to send
751 \"\\r\\n\" (carriage-return, then newline) after a password, but just
752 \"\\n\" after all other lines. This variable can be used for the
753 password, see `tramp-rsh-end-of-line' for the other cases.
755 The default value is to use the same value as `tramp-rsh-end-of-line'."
759 (defcustom tramp-remote-path
760 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
761 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
762 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
763 "*List of directories to search for executables on remote host.
764 Please notify me about other semi-standard directories to include here.
766 You can use `~' in this list, but when searching for a shell which groks
767 tilde expansion, all directory names starting with `~' will be ignored."
769 :type '(repeat string))
771 (defcustom tramp-login-prompt-regexp
773 "*Regexp matching login-like prompts.
774 The regexp should match at end of buffer."
778 (defcustom tramp-shell-prompt-pattern
779 "^[^#$%>\n]*[#$%>] *"
780 "Regexp to match prompts from remote shell.
781 Normally, Tramp expects you to configure `shell-prompt-pattern'
782 correctly, but sometimes it happens that you are connecting to a
783 remote host which sends a different kind of shell prompt. Therefore,
784 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
785 and also things matched by this variable. The default value of this
786 variable is the same as the default value of `shell-prompt-pattern',
787 which should work well in many cases."
791 (defcustom tramp-password-prompt-regexp
792 "^.*\\([pP]assword\\|passphrase.*\\):\^@? *"
793 "*Regexp matching password-like prompts.
794 The regexp should match at end of buffer.
796 The `sudo' program appears to insert a `^@' character into the prompt."
800 (defcustom tramp-wrong-passwd-regexp
802 ;; These strings should be on the last line
803 (regexp-opt '("Permission denied."
809 "Name or service not known"
810 "Host key verification failed.") t)
814 ;; Here comes a list of regexes, separated by \\|
815 "Received signal [0-9]+"
817 "*Regexp matching a `login failed' message.
818 The regexp should match at end of buffer."
822 (defcustom tramp-yesno-prompt-regexp
824 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
826 "Regular expression matching all yes/no queries which need to be confirmed.
827 The confirmation should be done with yes or no.
828 The regexp should match at end of buffer.
829 See also `tramp-yn-prompt-regexp'."
833 (defcustom tramp-yn-prompt-regexp
834 (concat (regexp-opt '("Store key in cache? (y/n)") t)
836 "Regular expression matching all y/n queries which need to be confirmed.
837 The confirmation should be done with y or n.
838 The regexp should match at end of buffer.
839 See also `tramp-yesno-prompt-regexp'."
844 (defcustom tramp-temp-name-prefix "tramp."
845 "*Prefix to use for temporary files.
846 If this is a relative file name (such as \"tramp.\"), it is considered
847 relative to the directory name returned by the function
848 `tramp-temporary-file-directory' (which see). It may also be an
849 absolute file name; don't forget to include a prefix for the filename
854 (defcustom tramp-discard-garbage nil
855 "*If non-nil, try to discard garbage sent by remote shell.
856 Some shells send such garbage upon connection setup."
860 (defcustom tramp-sh-extra-args '(("/bash\\'" . "--norc"))
861 "*Alist specifying extra arguments to pass to the remote shell.
862 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
863 matching the shell file name and ARGS is a string specifying the
866 This variable is only used when Tramp needs to start up another shell
867 for tilde expansion. The extra arguments should typically prevent the
868 shell from reading its init file."
870 :type '(alist :key-type string :value-type string))
872 (defcustom tramp-prefix-format
873 (if tramp-unified-filenames "/" "/[")
874 "*String matching the very beginning of tramp file names.
875 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
879 (defcustom tramp-prefix-regexp
880 (concat "^" (regexp-quote tramp-prefix-format))
881 "*Regexp matching the very beginning of tramp file names.
882 Should always start with \"^\". Derived from `tramp-prefix-format'."
886 (defcustom tramp-method-regexp
888 "*Regexp matching methods identifiers."
892 ;; It is a little bit annoying that in XEmacs case this delimeter is different
893 ;; for single-hop and multi-hop cases.
894 (defcustom tramp-postfix-single-method-format
895 (if tramp-unified-filenames ":" "/")
896 "*String matching delimeter between method and user or host names.
897 Applicable for single-hop methods.
898 Used in `tramp-make-tramp-file-name'."
902 (defcustom tramp-postfix-single-method-regexp
903 (regexp-quote tramp-postfix-single-method-format)
904 "*Regexp matching delimeter between method and user or host names.
905 Applicable for single-hop methods.
906 Derived from `tramp-postfix-single-method-format'."
910 (defcustom tramp-postfix-multi-method-format
912 "*String matching delimeter between method and user or host names.
913 Applicable for multi-hop methods.
914 Used in `tramp-make-tramp-multi-file-name'."
918 (defcustom tramp-postfix-multi-method-regexp
919 (regexp-quote tramp-postfix-multi-method-format)
920 "*Regexp matching delimeter between method and user or host names.
921 Applicable for multi-hop methods.
922 Derived from `tramp-postfix-multi-method-format'."
926 (defcustom tramp-postfix-multi-hop-format
927 (if tramp-unified-filenames ":" "/")
928 "*String matching delimeter between path and next method.
929 Applicable for multi-hop methods.
930 Used in `tramp-make-tramp-multi-file-name'."
934 (defcustom tramp-postfix-multi-hop-regexp
935 (regexp-quote tramp-postfix-multi-hop-format)
936 "*Regexp matching delimeter between path and next method.
937 Applicable for multi-hop methods.
938 Derived from `tramp-postfix-multi-hop-format'."
942 (defcustom tramp-user-regexp
944 "*Regexp matching user names."
948 (defcustom tramp-postfix-user-format
950 "*String matching delimeter between user and host names.
951 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
955 (defcustom tramp-postfix-user-regexp
956 (regexp-quote tramp-postfix-user-format)
957 "*Regexp matching delimeter between user and host names.
958 Derived from `tramp-postfix-user-format'."
962 (defcustom tramp-host-regexp
964 "*Regexp matching host names."
968 (defcustom tramp-postfix-host-format
969 (if tramp-unified-filenames ":" "]")
970 "*String matching delimeter between host names and paths.
971 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
975 (defcustom tramp-postfix-host-regexp
976 (regexp-quote tramp-postfix-host-format)
977 "*Regexp matching delimeter between host names and paths.
978 Derived from `tramp-postfix-host-format'."
982 (defcustom tramp-path-regexp
984 "*Regexp matching paths."
990 (defcustom tramp-file-name-structure
994 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
995 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
996 "\\(" tramp-host-regexp "\\)" tramp-postfix-host-regexp
997 "\\(" tramp-path-regexp "\\)")
1000 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1001 the tramp file name structure.
1003 The first element REGEXP is a regular expression matching a tramp file
1004 name. The regex should contain parentheses around the method name,
1005 the user name, the host name, and the file name parts.
1007 The second element METHOD is a number, saying which pair of
1008 parentheses matches the method name. The third element USER is
1009 similar, but for the user name. The fourth element HOST is similar,
1010 but for the host name. The fifth element FILE is for the file name.
1011 These numbers are passed directly to `match-string', which see. That
1012 means the opening parentheses are counted to identify the pair.
1014 See also `tramp-file-name-regexp'."
1016 :type '(list (regexp :tag "File name regexp")
1017 (integer :tag "Paren pair for method name")
1018 (integer :tag "Paren pair for user name ")
1019 (integer :tag "Paren pair for host name ")
1020 (integer :tag "Paren pair for file name ")))
1023 (defconst tramp-file-name-regexp-unified
1025 "Value for `tramp-file-name-regexp' for unified remoting.
1026 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1027 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1030 (defconst tramp-file-name-regexp-separate
1032 "Value for `tramp-file-name-regexp' for separate remoting.
1033 XEmacs uses a separate filename syntax for Tramp and EFS.
1034 See `tramp-file-name-structure-separate' for more explanations.")
1037 (defcustom tramp-file-name-regexp
1038 (if tramp-unified-filenames
1039 tramp-file-name-regexp-unified
1040 tramp-file-name-regexp-separate)
1041 "*Regular expression matching file names handled by tramp.
1042 This regexp should match tramp file names but no other file names.
1043 \(When tramp.el is loaded, this regular expression is prepended to
1044 `file-name-handler-alist', and that is searched sequentially. Thus,
1045 if the tramp entry appears rather early in the `file-name-handler-alist'
1046 and is a bit too general, then some files might be considered tramp
1047 files which are not really tramp files.
1049 Please note that the entry in `file-name-handler-alist' is made when
1050 this file (tramp.el) is loaded. This means that this variable must be set
1051 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1052 updated after changing this variable.
1054 Also see `tramp-file-name-structure'."
1059 (defconst tramp-completion-file-name-regexp-unified
1061 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1062 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1063 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1066 (defconst tramp-completion-file-name-regexp-separate
1067 "^/\\([[][^]]*\\)?$"
1068 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1069 XEmacs uses a separate filename syntax for Tramp and EFS.
1070 See `tramp-file-name-structure-separate' for more explanations.")
1073 (defcustom tramp-completion-file-name-regexp
1074 (if tramp-unified-filenames
1075 tramp-completion-file-name-regexp-unified
1076 tramp-completion-file-name-regexp-separate)
1077 "*Regular expression matching file names handled by tramp completion.
1078 This regexp should match partial tramp file names only.
1080 Please note that the entry in `file-name-handler-alist' is made when
1081 this file (tramp.el) is loaded. This means that this variable must be set
1082 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1083 updated after changing this variable.
1085 Also see `tramp-file-name-structure'."
1089 (defcustom tramp-multi-file-name-structure
1093 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1094 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1095 tramp-postfix-host-regexp "\\(" tramp-path-regexp "\\)")
1097 "*Describes the file name structure of `multi' files.
1098 Multi files allow you to contact a remote host in several hops.
1099 This is a list of four elements (REGEXP METHOD HOP PATH).
1101 The first element, REGEXP, gives a regular expression to match against
1102 the file name. In this regular expression, `%s' is replaced with the
1103 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1104 allow multiple hops, you normally want to use something like
1105 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1106 of parentheses is used for the HOP element, see below.)
1108 All remaining elements are numbers. METHOD gives the number of the
1109 paren pair which matches the method name. HOP gives the number of the
1110 paren pair which matches the hop sequence. PATH gives the number of
1111 the paren pair which matches the path name on the remote host.
1113 PATH can also be negative, which means to count from the end. Ie, a
1114 value of -1 means the last paren pair.
1116 I think it would be good if the regexp matches the whole of the
1117 string, but I haven't actually tried what happens if it doesn't..."
1119 :type '(list (regexp :tag "File name regexp")
1120 (integer :tag "Paren pair for method name")
1121 (integer :tag "Paren pair for hops")
1122 (integer :tag "Paren pair to match path")))
1124 (defcustom tramp-multi-file-name-hop-structure
1127 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1128 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1129 "\\(" tramp-host-regexp "\\)")
1131 "*Describes the structure of a hop in multi files.
1132 This is a list of four elements (REGEXP METHOD USER HOST). First
1133 element REGEXP is used to match against the hop. Pair number METHOD
1134 matches the method of one hop, pair number USER matches the user of
1135 one hop, pair number HOST matches the host of one hop.
1137 This regular expression should match exactly all of one hop."
1139 :type '(list (regexp :tag "Hop regexp")
1140 (integer :tag "Paren pair for method name")
1141 (integer :tag "Paren pair for user name")
1142 (integer :tag "Paren pair for host name")))
1144 (defcustom tramp-make-multi-tramp-file-format
1146 (concat tramp-prefix-format "%m")
1147 (concat tramp-postfix-multi-hop-format
1148 "%m" tramp-postfix-multi-method-format
1149 "%u" tramp-postfix-user-format
1151 (concat tramp-postfix-host-format "%p"))
1152 "*Describes how to construct a `multi' file name.
1153 This is a list of three elements PREFIX, HOP and PATH.
1155 The first element PREFIX says how to construct the prefix, the second
1156 element HOP specifies what each hop looks like, and the final element
1157 PATH says how to construct the path name.
1159 In PREFIX, `%%' means `%' and `%m' means the method name.
1161 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1162 user and hop host, respectively.
1164 In PATH, `%%' means `%' and `%p' means the path name.
1166 The resulting file name always contains one copy of PREFIX and one
1167 copy of PATH, but there is one copy of HOP for each hop in the file
1170 Note: the current implementation requires the prefix to contain the
1171 method name, followed by all the hops, and the path name must come
1174 :type '(list string string string))
1176 (defcustom tramp-terminal-type "dumb"
1177 "*Value of TERM environment variable for logging in to remote host.
1178 Because Tramp wants to parse the output of the remote shell, it is easily
1179 confused by ANSI color escape sequences and suchlike. Often, shell init
1180 files conditionalize this setup based on the TERM environment variable."
1184 (defcustom tramp-completion-without-shell-p nil
1185 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1186 Using shell wildcards for completions has the advantage that it can be
1187 fast even in large directories, but completion is always
1188 case-sensitive. Relying on Lisp only means that case-insensitive
1189 completion is possible (subject to the variable `completion-ignore-case'),
1190 but it might be slow on large directories."
1194 (defcustom tramp-actions-before-shell
1195 '((tramp-password-prompt-regexp tramp-action-password)
1196 (tramp-login-prompt-regexp tramp-action-login)
1197 (shell-prompt-pattern tramp-action-succeed)
1198 (tramp-shell-prompt-pattern tramp-action-succeed)
1199 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1200 (tramp-yesno-prompt-regexp tramp-action-yesno)
1201 (tramp-yn-prompt-regexp tramp-action-yn))
1202 "List of pattern/action pairs.
1203 Whenever a pattern matches, the corresponding action is performed.
1204 Each item looks like (PATTERN ACTION).
1206 The PATTERN should be a symbol, a variable. The value of this
1207 variable gives the regular expression to search for. Note that the
1208 regexp must match at the end of the buffer, \"\\'\" is implicitly
1211 The ACTION should also be a symbol, but a function. When the
1212 corresponding PATTERN matches, the ACTION function is called."
1214 :type '(repeat (list variable function)))
1216 (defcustom tramp-multi-actions
1217 '((tramp-password-prompt-regexp tramp-multi-action-password)
1218 (tramp-login-prompt-regexp tramp-multi-action-login)
1219 (shell-prompt-pattern tramp-multi-action-succeed)
1220 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1221 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied))
1222 "List of pattern/action pairs.
1223 This list is used for each hop in multi-hop connections.
1224 See `tramp-actions-before-shell' for more info."
1226 :type '(repeat (list variable function)))
1228 ;;; Internal Variables:
1230 (defvar tramp-buffer-file-attributes nil
1231 "Holds the `ls -ild' output for the current buffer.
1232 This variable is local to each buffer. It is not used if the remote
1233 machine groks Perl. If it is used, it's used as an emulation for
1234 the visited file modtime.")
1235 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1237 (defvar tramp-end-of-output "/////"
1238 "String used to recognize end of output.")
1240 (defvar tramp-connection-function nil
1241 "This internal variable holds a parameter for `tramp-methods'.
1242 In the connection buffer, this variable has the value of the like-named
1243 method parameter, as specified in `tramp-methods' (which see).")
1245 (defvar tramp-remote-sh nil
1246 "This internal variable holds a parameter for `tramp-methods'.
1247 In the connection buffer, this variable has the value of the like-named
1248 method parameter, as specified in `tramp-methods' (which see).")
1250 (defvar tramp-rsh-program nil
1251 "This internal variable holds a parameter for `tramp-methods'.
1252 In the connection buffer, this variable has the value of the like-named
1253 method parameter, as specified in `tramp-methods' (which see).")
1255 (defvar tramp-rsh-args nil
1256 "This internal variable holds a parameter for `tramp-methods'.
1257 In the connection buffer, this variable has the value of the like-named
1258 method parameter, as specified in `tramp-methods' (which see).")
1260 (defvar tramp-rcp-program nil
1261 "This internal variable holds a parameter for `tramp-methods'.
1262 In the connection buffer, this variable has the value of the like-named
1263 method parameter, as specified in `tramp-methods' (which see).")
1265 (defvar tramp-rcp-args nil
1266 "This internal variable holds a parameter for `tramp-methods'.
1267 In the connection buffer, this variable has the value of the like-named
1268 method parameter, as specified in `tramp-methods' (which see).")
1270 (defvar tramp-rcp-keep-date-arg nil
1271 "This internal variable holds a parameter for `tramp-methods'.
1272 In the connection buffer, this variable has the value of the like-named
1273 method parameter, as specified in `tramp-methods' (which see).")
1275 (defvar tramp-encoding-command nil
1276 "This internal variable holds a parameter for `tramp-methods'.
1277 In the connection buffer, this variable has the value of the like-named
1278 method parameter, as specified in `tramp-methods' (which see).")
1280 (defvar tramp-decoding-command nil
1281 "This internal variable holds a parameter for `tramp-methods'.
1282 In the connection buffer, this variable has the value of the like-named
1283 method parameter, as specified in `tramp-methods' (which see).")
1285 (defvar tramp-encoding-function nil
1286 "This internal variable holds a parameter for `tramp-methods'.
1287 In the connection buffer, this variable has the value of the like-named
1288 method parameter, as specified in `tramp-methods' (which see).")
1290 (defvar tramp-decoding-function nil
1291 "This internal variable holds a parameter for `tramp-methods'.
1292 In the connection buffer, this variable has the value of the like-named
1293 method parameter, as specified in `tramp-methods' (which see).")
1295 (defvar tramp-telnet-program nil
1296 "This internal variable holds a parameter for `tramp-methods'.
1297 In the connection buffer, this variable has the value of the like-named
1298 method parameter, as specified in `tramp-methods' (which see).")
1300 (defvar tramp-telnet-args nil
1301 "This internal variable holds a parameter for `tramp-methods'.
1302 In the connection buffer, this variable has the value of the like-named
1303 method parameter, as specified in `tramp-methods' (which see).")
1305 (defvar tramp-su-program nil
1306 "This internal variable holds a parameter for `tramp-methods'.
1307 In the connection buffer, this variable has the value of the like-named
1308 method parameter, as specified in `tramp-methods' (which see).")
1310 ;; CCC `local in each buffer'?
1311 (defvar tramp-ls-command nil
1312 "This command is used to get a long listing with numeric user and group ids.
1313 This variable is automatically made buffer-local to each rsh process buffer
1314 upon opening the connection.")
1316 (defvar tramp-current-multi-method nil
1317 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1318 This variable is automatically made buffer-local to each rsh process buffer
1319 upon opening the connection.")
1321 (defvar tramp-current-method nil
1322 "Connection method for this *tramp* buffer.
1323 This variable is automatically made buffer-local to each rsh process buffer
1324 upon opening the connection.")
1326 (defvar tramp-current-user nil
1327 "Remote login name for this *tramp* buffer.
1328 This variable is automatically made buffer-local to each rsh process buffer
1329 upon opening the connection.")
1331 (defvar tramp-current-host nil
1332 "Remote host for this *tramp* buffer.
1333 This variable is automatically made buffer-local to each rsh process buffer
1334 upon opening the connection.")
1336 (defvar tramp-test-groks-nt nil
1337 "Whether the `test' command groks the `-nt' switch.
1338 \(`test A -nt B' tests if file A is newer than file B.)
1339 This variable is automatically made buffer-local to each rsh process buffer
1340 upon opening the connection.")
1342 (defvar tramp-file-exists-command nil
1343 "Command to use for checking if a file exists.
1344 This variable is automatically made buffer-local to each rsh process buffer
1345 upon opening the connection.")
1347 (defconst tramp-uudecode "\
1349 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1353 "Shell function to implement `uudecode' to standard output.
1354 Many systems support `uudecode -o -' for this or `uudecode -p', but
1355 some systems don't, and for them we have this shell function.")
1357 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1358 ;; output. If you are hacking on this, note that you get *no* output
1359 ;; unless this spits out a complete line, including the '\n' at the
1361 (defconst tramp-perl-file-attributes "\
1364 if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; }
1365 elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; }
1366 else { $l = \"nil\" };
1367 printf(\"(%s %u %d %d (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1368 $l, $s[3], $s[4], $s[5], $s[8] >> 16 & 0xffff, $s[8] & 0xffff,
1369 $s[9] >> 16 & 0xffff, $s[9] & 0xffff, $s[10] >> 16 & 0xffff, $s[10] & 0xffff,
1370 $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff, $s[0] >> 16 & 0xffff, $s[0] & 0xffff);"
1371 "Perl script to produce output suitable for use with `file-attributes'
1372 on the remote file system.")
1374 ;; ;; These two use uu encoding.
1375 ;; (defvar tramp-perl-encode "%s -e'\
1376 ;; print qq(begin 644 xxx\n);
1379 ;; while (read(STDIN, $s, 45)) {
1380 ;; print pack(q(u), $s);
1385 ;; "Perl program to use for encoding a file.
1386 ;; Escape sequence %s is replaced with name of Perl binary.")
1388 ;; (defvar tramp-perl-decode "%s -ne '
1389 ;; print unpack q(u), $_;
1391 ;; "Perl program to use for decoding a file.
1392 ;; Escape sequence %s is replaced with name of Perl binary.")
1394 ;; These two use base64 encoding.
1395 (defvar tramp-perl-encode-with-module
1396 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1397 "Perl program to use for encoding a file.
1398 Escape sequence %s is replaced with name of Perl binary.
1399 This string is passed to `format', so percent characters need to be doubled.
1400 This implementation requires the MIME::Base64 Perl module to be installed
1401 on the remote host.")
1403 (defvar tramp-perl-decode-with-module
1404 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1405 "Perl program to use for decoding a file.
1406 Escape sequence %s is replaced with name of Perl binary.
1407 This string is passed to `format', so percent characters need to be doubled.
1408 This implementation requires the MIME::Base64 Perl module to be installed
1409 on the remote host.")
1411 (defvar tramp-perl-encode
1413 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1414 # Copyright (C) 2002 Free Software Foundation, Inc.
1419 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1420 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1425 # We read in chunks of 54 bytes, to generate output lines
1426 # of 72 chars (plus end of line)
1429 while (my $data = <STDIN>) {
1432 # Only for the last chunk, and only if did not fill the last three-byte packet
1434 my $mod = length($data) %% 3;
1435 $pad = q(=) x (3 - $mod) if $mod;
1438 # Not the fastest method, but it is simple: unpack to binary string, split
1439 # by groups of 6 bits and convert back from binary to byte; then map into
1440 # the translation table
1444 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1449 "Perl program to use for encoding a file.
1450 Escape sequence %s is replaced with name of Perl binary.
1451 This string is passed to `format', so percent characters need to be doubled.")
1453 (defvar tramp-perl-decode
1455 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1456 # Copyright (C) 2002 Free Software Foundation, Inc.
1461 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1462 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1465 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1469 # We are going to accumulate into $pending to accept any line length
1470 # (we do not check they are <= 76 chars as the RFC says)
1473 while (my $data = <STDIN>) {
1476 # If we find one or two =, we have reached the end and
1477 # any following data is to be discarded
1478 my $finished = $data =~ s/(==?).*/$1/;
1481 my $len = length($pending);
1482 my $chunk = substr($pending, 0, $len & ~3);
1484 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1485 # split in 8-bit chunks and convert back to char.
1488 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1493 "Perl program to use for decoding a file.
1494 Escape sequence %s is replaced with name of Perl binary.
1495 This string is passed to `format', so percent characters need to be doubled.")
1497 ; These values conform to `file-attributes' from XEmacs 21.2.
1498 ; GNU Emacs and other tools not checked.
1499 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1501 (2 . "c") ; character device
1502 (3 . "m") ; multiplexed character device (v7)
1503 (4 . "d") ; directory
1504 (5 . "?") ; Named special file (XENIX)
1505 (6 . "b") ; block device
1506 (7 . "?") ; multiplexed block device (v7)
1507 (8 . "-") ; regular file
1508 (9 . "n") ; network special file (HP-UX)
1509 (10 . "l") ; symlink
1510 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1512 (13 . "D") ; door special (Solaris)
1513 (14 . "w")) ; whiteout (BSD)
1514 "A list of file types returned from the `stat' system call.
1515 This is used to map a mode number to a permission string.")
1517 (defvar tramp-dos-coding-system
1518 (if (and (fboundp 'coding-system-p)
1519 (funcall 'coding-system-p '(dos)))
1522 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1524 (defvar tramp-last-cmd-time nil
1525 "Internal Tramp variable recording the time when the last cmd was sent.
1526 This variable is buffer-local in every buffer.")
1527 (make-variable-buffer-local 'tramp-last-cmd-time)
1529 ;; This variable does not have the right value in XEmacs. What should
1530 ;; I use instead of find-operation-coding-system in XEmacs?
1531 (defvar tramp-feature-write-region-fix
1532 (when (fboundp 'find-operation-coding-system)
1533 (let ((file-coding-system-alist '(("test" emacs-mule))))
1534 (find-operation-coding-system 'write-region 0 0 "" nil "test")))
1535 "Internal variable to say if `write-region' chooses the right coding.
1536 Older versions of Emacs chose the coding system for `write-region' based
1537 on the FILENAME argument, even if VISIT was a string.")
1539 ;; New handlers should be added here. The following operations can be
1540 ;; handled using the normal primitives: file-name-as-directory,
1541 ;; file-name-directory, file-name-nondirectory,
1542 ;; file-name-sans-versions, get-file-buffer.
1543 (defconst tramp-file-name-handler-alist
1545 (load . tramp-handle-load)
1546 (make-symbolic-link . tramp-handle-make-symbolic-link)
1547 (file-name-directory . tramp-handle-file-name-directory)
1548 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1549 (file-truename . tramp-handle-file-truename)
1550 (file-exists-p . tramp-handle-file-exists-p)
1551 (file-directory-p . tramp-handle-file-directory-p)
1552 (file-executable-p . tramp-handle-file-executable-p)
1553 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1554 (file-readable-p . tramp-handle-file-readable-p)
1555 (file-regular-p . tramp-handle-file-regular-p)
1556 (file-symlink-p . tramp-handle-file-symlink-p)
1557 (file-writable-p . tramp-handle-file-writable-p)
1558 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1559 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1560 (file-attributes . tramp-handle-file-attributes)
1561 (file-modes . tramp-handle-file-modes)
1562 (file-directory-files . tramp-handle-file-directory-files)
1563 (directory-files . tramp-handle-directory-files)
1564 (file-name-all-completions . tramp-handle-file-name-all-completions)
1565 (file-name-completion . tramp-handle-file-name-completion)
1566 (add-name-to-file . tramp-handle-add-name-to-file)
1567 (copy-file . tramp-handle-copy-file)
1568 (rename-file . tramp-handle-rename-file)
1569 (set-file-modes . tramp-handle-set-file-modes)
1570 (make-directory . tramp-handle-make-directory)
1571 (delete-directory . tramp-handle-delete-directory)
1572 (delete-file . tramp-handle-delete-file)
1573 (directory-file-name . tramp-handle-directory-file-name)
1574 (shell-command . tramp-handle-shell-command)
1575 (insert-directory . tramp-handle-insert-directory)
1576 (expand-file-name . tramp-handle-expand-file-name)
1577 (file-local-copy . tramp-handle-file-local-copy)
1578 (insert-file-contents . tramp-handle-insert-file-contents)
1579 (write-region . tramp-handle-write-region)
1580 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1581 (dired-call-process . tramp-handle-dired-call-process)
1582 (dired-recursive-delete-directory
1583 . tramp-handle-dired-recursive-delete-directory)
1584 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1585 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1586 "Alist of handler functions.
1587 Operations not mentioned here will be handled by the normal Emacs functions.")
1589 ;; Handlers for partial tramp file names. For GNU Emacs just
1590 ;; `file-name-all-completions' is needed. The other ones are necessary
1592 (defconst tramp-completion-file-name-handler-alist
1594 (file-name-directory . tramp-completion-handle-file-name-directory)
1595 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1596 (file-exists-p . tramp-completion-handle-file-exists-p)
1597 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1598 (file-name-completion . tramp-completion-handle-file-name-completion)
1599 (expand-file-name . tramp-completion-handle-expand-file-name))
1600 "Alist of completion handler functions.
1601 Used for file names matching `tramp-file-name-regexp'. Operations not
1602 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1603 normal Emacs functions.")
1605 ;;; Internal functions which must come first.
1607 (defsubst tramp-message (level fmt-string &rest args)
1608 "Emit a message depending on verbosity level.
1609 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1610 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1611 Calls function `message' with FMT-STRING as control string and the remaining
1612 ARGS to actually emit the message (if applicable).
1614 This function expects to be called from the tramp buffer only!"
1615 (when (<= level tramp-verbose)
1616 (apply #'message (concat "tramp: " fmt-string) args)
1617 (when tramp-debug-buffer
1620 (tramp-get-debug-buffer
1621 tramp-current-multi-method tramp-current-method
1622 tramp-current-user tramp-current-host))
1623 (goto-char (point-max))
1624 (tramp-insert-with-face
1626 (concat "# " (apply #'format fmt-string args) "\n"))))))
1628 (defun tramp-message-for-buffer
1629 (multi-method method user host level fmt-string &rest args)
1630 "Like `tramp-message' but temporarily switches to the tramp buffer.
1631 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1632 remaining args passed to `tramp-message'."
1634 (set-buffer (tramp-get-buffer multi-method method user host))
1635 (apply 'tramp-message level fmt-string args)))
1637 (defsubst tramp-line-end-position nil
1638 "Return point at end of line.
1639 Calls `line-end-position' or `point-at-eol' if defined, else
1640 own implementation."
1642 ((fboundp 'line-end-position) (funcall 'line-end-position))
1643 ((fboundp 'point-at-eol) (funcall 'point-at-eol))
1644 (t (save-excursion (end-of-line) (point)))))
1646 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1647 "Parse a Tramp filename and make components available in the body.
1649 First arg FILENAME is evaluated and dissected into its components.
1650 Second arg VAR is a symbol. It is used as a variable name to hold
1651 the filename structure. It is also used as a prefix for the variables
1652 holding the components. For example, if VAR is the symbol `foo', then
1653 `foo' will be bound to the whole structure, `foo-multi-method' will
1654 be bound to the multi-method component, and so on for `foo-method',
1655 `foo-user', `foo-host', `foo-path'.
1657 Remaining args are Lisp expressions to be evaluated (inside an implicit
1660 If VAR is nil, then we bind `v' to the structure and `multi-method',
1661 `method', `user', `host', `path' to the components."
1662 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1663 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1664 (tramp-file-name-multi-method ,(or var 'v)))
1665 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1666 (tramp-file-name-method ,(or var 'v)))
1667 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1668 (tramp-file-name-user ,(or var 'v)))
1669 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1670 (tramp-file-name-host ,(or var 'v)))
1671 (,(if var (intern (concat (symbol-name var) "-path")) 'path)
1672 (tramp-file-name-path ,(or var 'v))))
1675 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1677 ;;; Config Manipulation Functions:
1679 (defun tramp-set-completion-function (method function-list)
1680 "Sets the list of completion functions for METHOD.
1681 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1682 The FUNCTION is intended to parse FILE according its syntax.
1683 It might be a predefined FUNCTION, or a user defined FUNCTION.
1684 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
1685 `tramp-parse-hosts', and `tramp-parse-passwd'.
1688 (tramp-set-completion-function
1690 '((tramp-parse-shosts \"/etc/ssh_known_hosts\")
1691 (tramp-parse-shosts \"~/.ssh/known_hosts\")))"
1693 (let ((v (cdr (assoc method tramp-completion-function-alist))))
1694 (when v (setcdr v function-list))))
1696 (defun tramp-get-completion-function (method)
1697 "Returns list of completion functions for METHOD.
1698 For definition of that list see `tramp-set-completion-function'."
1699 (cdr (assoc method tramp-completion-function-alist)))
1701 ;;; File Name Handler Functions:
1703 (defun tramp-handle-make-symbolic-link
1704 (filename linkname &optional ok-if-already-exists)
1705 "Like `make-symbolic-link' for tramp files.
1706 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1707 the symlink. If LINKNAME is a Tramp file, only the path component is
1708 used as the target of the symlink.
1710 If LINKNAME is a Tramp file and the path component is relative, then
1711 it is expanded first, before the path component is taken. Note that
1712 this can give surprising results if the user/host for the source and
1713 target of the symlink differ."
1714 (with-parsed-tramp-file-name linkname l
1715 (when (tramp-ange-ftp-file-name-p l-multi-method l-method)
1716 (tramp-invoke-ange-ftp 'make-symbolic-link
1717 filename linkname ok-if-already-exists))
1718 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
1719 (cwd (file-name-directory l-path)))
1722 (list "Making a symbolic link."
1723 "ln(1) does not exist on the remote host.")))
1725 ;; Do the 'confirm if exists' thing.
1726 (when (file-exists-p linkname)
1728 (if (or (null ok-if-already-exists) ; not allowed to exist
1729 (and (numberp ok-if-already-exists)
1732 "File %s already exists; make it a link anyway? "
1734 (signal 'file-already-exists (list "File already exists" l-path))
1735 (delete-file linkname)))
1737 ;; If FILENAME is a Tramp name, use just the path component.
1738 (when (tramp-tramp-file-p filename)
1739 (setq filename (tramp-file-name-path
1740 (tramp-dissect-file-name
1741 (expand-file-name filename)))))
1743 ;; Right, they are on the same host, regardless of user, method, etc.
1744 ;; We now make the link on the remote machine. This will occur as the user
1745 ;; that FILENAME belongs to.
1747 (tramp-send-command-and-check
1748 l-multi-method l-method l-user l-host
1749 (format "cd %s && %s -sf %s %s"
1756 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
1757 "Like `load' for tramp files. Not implemented!"
1758 (unless (file-name-absolute-p file)
1759 (error "Tramp cannot `load' files without absolute path name"))
1760 (with-parsed-tramp-file-name file nil
1761 (when (tramp-ange-ftp-file-name-p multi-method method)
1762 (tramp-invoke-ange-ftp 'load
1763 file noerror nomessage nosuffix must-suffix))
1765 (cond ((file-exists-p (concat file ".elc"))
1766 (setq file (concat file ".elc")))
1767 ((file-exists-p (concat file ".el"))
1768 (setq file (concat file ".el")))))
1770 ;; The first condition is always true for absolute file names.
1771 ;; Included for safety's sake.
1772 (unless (or (file-name-directory file)
1773 (string-match "\\.elc?\\'" file))
1774 (error "File `%s' does not include a `.el' or `.elc' suffix"
1777 (when (not (file-exists-p file))
1778 (error "Cannot load nonexistant file `%s'" file)))
1779 (if (not (file-exists-p file))
1782 (message "Loading %s..." file))
1783 (let ((local-copy (file-local-copy file)))
1784 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
1785 (load local-copy noerror t t)
1786 (delete-file local-copy))
1788 (message "Loading %s...done" file))
1791 ;; Path manipulation functions that grok TRAMP paths...
1792 (defun tramp-handle-file-name-directory (file)
1793 "Like `file-name-directory' but aware of TRAMP files."
1794 ;; everything except the last filename thing is the directory
1795 (with-parsed-tramp-file-name file nil
1796 (when (tramp-ange-ftp-file-name-p multi-method method)
1797 (tramp-invoke-ange-ftp 'file-name-directory file))
1798 ;; For the following condition, two possibilities should be tried:
1799 ;; (1) (string= path "")
1800 ;; (2) (or (string= path "") (string= path "/"))
1801 ;; The second variant fails when completing a "/" directory on
1802 ;; the remote host, that is a filename which looks like
1803 ;; "/user@host:/". But maybe wildcards fail with the first variant.
1804 ;; We should do some investigation.
1805 (if (string= path "")
1806 ;; For a filename like "/[foo]", we return "/". The `else'
1807 ;; case would return "/[foo]" unchanged. But if we do that,
1808 ;; then `file-expand-wildcards' ceases to work. It's not
1809 ;; quite clear to me what's the intuition that tells that this
1810 ;; behavior is the right behavior, but oh, well.
1812 ;; run the command on the path portion only
1813 ;; CCC: This should take into account the remote machine type, no?
1814 ;; --daniel <daniel@danann.net>
1815 (tramp-make-tramp-file-name multi-method method user host
1816 ;; This will not recurse...
1817 (or (file-name-directory path) "")))))
1819 (defun tramp-handle-file-name-nondirectory (file)
1820 "Like `file-name-nondirectory' but aware of TRAMP files."
1821 (with-parsed-tramp-file-name file nil
1822 (when (tramp-ange-ftp-file-name-p multi-method method)
1823 (tramp-invoke-ange-ftp 'file-name-nondirectory file))
1824 (file-name-nondirectory path)))
1826 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
1827 "Like `file-truename' for tramp files."
1828 (with-parsed-tramp-file-name filename nil
1829 ;; Ange-FTP does not support truename processing, but for
1830 ;; convenience we pretend it did and forward the call to Ange-FTP
1831 ;; anyway. Ange-FTP then just invokes `identity'.
1832 (when (tramp-ange-ftp-file-name-p multi-method method)
1833 (tramp-invoke-ange-ftp 'file-truename filename))
1834 (let* ((steps (tramp-split-string path "/"))
1835 (pathdir (let ((directory-sep-char ?/))
1836 (file-name-as-directory path)))
1837 (is-dir (string= path pathdir))
1840 ;; Don't make the following value larger than necessary.
1841 ;; People expect an error message in a timely fashion when
1842 ;; something is wrong; otherwise they might think that Emacs
1843 ;; is hung. Of course, correctness has to come first.
1845 (result nil) ;result steps in reverse order
1848 (tramp-message-for-buffer
1849 multi-method method user host
1850 10 "Finding true name for `%s'" filename)
1851 (while (and steps (< numchase numchase-limit))
1852 (setq thisstep (pop steps))
1853 (tramp-message-for-buffer
1854 multi-method method user host
1856 (mapconcat 'identity
1857 (append '("") (reverse result) (list thisstep))
1859 (setq symlink-target
1860 (nth 0 (tramp-handle-file-attributes
1861 (tramp-make-tramp-file-name
1862 multi-method method user host
1863 (mapconcat 'identity
1868 (cond ((string= "." thisstep)
1869 (tramp-message-for-buffer multi-method method user host
1870 10 "Ignoring step `.'"))
1871 ((string= ".." thisstep)
1872 (tramp-message-for-buffer multi-method method user host
1873 10 "Processing step `..'")
1875 ((stringp symlink-target)
1876 ;; It's a symlink, follow it.
1877 (tramp-message-for-buffer
1878 multi-method method user host
1879 10 "Follow symlink to %s" symlink-target)
1880 (setq numchase (1+ numchase))
1881 (when (file-name-absolute-p symlink-target)
1884 (append (tramp-split-string symlink-target "/") steps)))
1887 (setq result (cons thisstep result)))))
1888 (when (>= numchase numchase-limit)
1889 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
1890 (setq result (reverse result))
1891 (tramp-message-for-buffer
1892 multi-method method user host
1893 10 "True name of `%s' is `%s'"
1894 filename (mapconcat 'identity (cons "" result) "/"))
1895 (tramp-make-tramp-file-name
1896 multi-method method user host
1897 (concat (mapconcat 'identity (cons "" result) "/")
1898 (if is-dir "/" ""))))))
1902 (defun tramp-handle-file-exists-p (filename)
1903 "Like `file-exists-p' for tramp files."
1904 (with-parsed-tramp-file-name filename nil
1905 (when (tramp-ange-ftp-file-name-p multi-method method)
1906 (tramp-invoke-ange-ftp 'file-exists-p filename))
1908 (zerop (tramp-send-command-and-check
1909 multi-method method user host
1911 (tramp-get-file-exists-command multi-method method user host)
1912 (tramp-shell-quote-argument path)))))))
1914 ;; CCC: This should check for an error condition and signal failure
1915 ;; when something goes wrong.
1916 ;; Daniel Pittman <daniel@danann.net>
1917 (defun tramp-handle-file-attributes (filename &optional nonnumeric)
1918 "Like `file-attributes' for tramp files.
1919 Optional argument NONNUMERIC means return user and group name
1920 rather than as numbers."
1922 (with-parsed-tramp-file-name filename nil
1923 (when (tramp-ange-ftp-file-name-p multi-method method)
1924 (tramp-invoke-ange-ftp 'file-attributes filename))
1925 (when (tramp-handle-file-exists-p filename)
1926 ;; file exists, find out stuff
1928 (if (tramp-get-remote-perl multi-method method user host)
1930 (tramp-handle-file-attributes-with-perl
1931 multi-method method user host path nonnumeric))
1933 (tramp-handle-file-attributes-with-ls
1934 multi-method method user host path nonnumeric))))))
1938 (defun tramp-handle-file-attributes-with-ls
1939 (multi-method method user host path &optional nonnumeric)
1940 "Implement `file-attributes' for tramp files using the ls(1) command."
1942 res-inode res-filemodes res-numlinks
1943 res-uid res-gid res-size res-symlink-target)
1944 (tramp-message-for-buffer multi-method method user host 10
1945 "file attributes with ls: %s"
1946 (tramp-make-tramp-file-name
1947 multi-method method user host path))
1949 multi-method method user host
1951 (tramp-get-ls-command multi-method method user host)
1952 (if nonnumeric "-ild" "-ildn")
1953 (tramp-shell-quote-argument path)))
1954 (tramp-wait-for-output)
1955 ;; parse `ls -l' output ...
1959 (read (current-buffer))
1960 (invalid-read-syntax
1961 (when (and (equal (cadr err)
1962 "Integer constant overflow in reader")
1964 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1966 (let* ((big (read (substring (caddr err) 0
1967 (match-beginning 1))))
1968 (small (read (match-string 1 (caddr err))))
1969 (twiddle (/ small 65536)))
1970 (cons (+ big twiddle)
1971 (- small (* twiddle 65536))))))))
1972 ;; ... file mode flags
1973 (setq res-filemodes (symbol-name (read (current-buffer))))
1975 (setq res-numlinks (read (current-buffer)))
1977 (setq res-uid (read (current-buffer)))
1978 (setq res-gid (read (current-buffer)))
1980 (unless (numberp res-uid) (setq res-uid -1))
1981 (unless (numberp res-gid) (setq res-gid -1)))
1983 (setq res-size (read (current-buffer)))
1984 ;; From the file modes, figure out other stuff.
1985 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1986 (setq dirp (eq ?d (aref res-filemodes 0)))
1987 ;; if symlink, find out file name pointed to
1989 (search-forward "-> ")
1990 (setq res-symlink-target
1991 (buffer-substring (point)
1992 (tramp-line-end-position))))
1993 ;; return data gathered
1995 ;; 0. t for directory, string (name linked to) for symbolic
1997 (or dirp res-symlink-target nil)
1998 ;; 1. Number of links to file.
2004 ;; 4. Last access time, as a list of two integers. First
2005 ;; integer has high-order 16 bits of time, second has low 16
2007 ;; 5. Last modification time, likewise.
2008 ;; 6. Last status change time, likewise.
2009 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2010 ;; 7. Size in bytes (-1, if number is out of range).
2012 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2014 ;; 9. t iff file's gid would change if file were deleted and
2017 ;; 10. inode number.
2019 ;; 11. Device number.
2023 (defun tramp-handle-file-attributes-with-perl
2024 (multi-method method user host path &optional nonnumeric)
2025 "Implement `file-attributes' for tramp files using a Perl script.
2027 The Perl command is sent to the remote machine when the connection
2028 is initially created and is kept cached by the remote shell."
2029 (tramp-message-for-buffer multi-method method user host 10
2030 "file attributes with perl: %s"
2031 (tramp-make-tramp-file-name
2032 multi-method method user host path))
2034 multi-method method user host
2035 (format "tramp_file_attributes %s"
2036 (tramp-shell-quote-argument path)))
2037 (tramp-wait-for-output)
2038 (let ((result (read (current-buffer))))
2039 (setcar (nthcdr 8 result)
2040 (tramp-file-mode-from-int (nth 8 result)))
2043 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2044 "Like `set-visited-file-modtime' for tramp files."
2045 (unless (buffer-file-name)
2046 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2049 (tramp-run-real-handler 'set-visited-file-modtime (list time-list)))
2050 (let ((f (buffer-file-name))
2051 (coding-system-used nil))
2052 (with-parsed-tramp-file-name f nil
2053 ;; This operation is not handled by Ange-FTP! Compare this
2054 ;; behavior with `file-truename' which Ange-FTP does not really
2055 ;; handle, either, but at least it pretends to. I wonder if
2056 ;; Ange-FTP should also pretend to grok
2057 ;; `set-visited-file-modtime', for consistency?
2058 (when (tramp-ange-ftp-file-name-p multi-method method)
2059 (throw 'tramp-forward-to-ange-ftp
2060 (tramp-run-real-handler 'set-visited-file-modtime
2062 (let* ((attr (file-attributes f))
2063 (modtime (nth 5 attr)))
2064 ;; We use '(0 0) as a don't-know value. See also
2065 ;; `tramp-handle-file-attributes-with-ls'.
2066 (when (boundp 'last-coding-system-used)
2067 (setq coding-system-used last-coding-system-used))
2068 (if (not (equal modtime '(0 0)))
2069 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2072 multi-method method user host
2073 (format "%s -ild %s"
2074 (tramp-get-ls-command multi-method method user host)
2075 (tramp-shell-quote-argument path)))
2076 (tramp-wait-for-output)
2077 (setq attr (buffer-substring (point)
2078 (progn (end-of-line) (point)))))
2079 (setq tramp-buffer-file-attributes attr))
2080 (when (boundp 'last-coding-system-used)
2081 (setq last-coding-system-used coding-system-used))
2084 ;; CCC continue here
2086 ;; This function makes the same assumption as
2087 ;; `tramp-handle-set-visited-file-modtime'.
2088 (defun tramp-handle-verify-visited-file-modtime (buf)
2089 "Like `verify-visited-file-modtime' for tramp files."
2090 (with-current-buffer buf
2091 (let ((f (buffer-file-name)))
2092 (with-parsed-tramp-file-name f nil
2093 (when (tramp-ange-ftp-file-name-p multi-method method)
2094 ;; This one requires a hack since the file name is not passed
2096 (let ((buffer-file-name (tramp-make-ange-ftp-file-name
2098 (tramp-invoke-ange-ftp 'verify-visited-file-modtime buf)))
2099 (let* ((attr (file-attributes f))
2100 (modtime (nth 5 attr)))
2101 (cond ((and attr (not (equal modtime '(0 0))))
2102 ;; Why does `file-attributes' return a list (HIGH
2103 ;; LOW), but `visited-file-modtime' returns a cons
2105 (let ((mt (visited-file-modtime)))
2106 (< (abs (tramp-time-diff
2107 modtime (list (car mt) (cdr mt)))) 2)))
2111 multi-method method user host
2112 (format "%s -ild %s"
2113 (tramp-get-ls-command multi-method method
2115 (tramp-shell-quote-argument path)))
2116 (tramp-wait-for-output)
2117 (setq attr (buffer-substring
2118 (point) (progn (end-of-line) (point)))))
2119 (equal tramp-buffer-file-attributes attr))
2120 ;; If file does not exist, say it is not modified.
2123 (defadvice clear-visited-file-modtime (after tramp activate)
2124 "Set `tramp-buffer-file-attributes' back to nil.
2125 Tramp uses this variable as an emulation for the actual modtime of the file,
2126 if the remote host can't provide the modtime."
2127 (setq tramp-buffer-file-attributes nil))
2129 (defun tramp-handle-set-file-modes (filename mode)
2130 "Like `set-file-modes' for tramp files."
2131 (with-parsed-tramp-file-name filename nil
2132 (when (tramp-ange-ftp-file-name-p multi-method method)
2133 (tramp-invoke-ange-ftp 'set-file-modes mode filename))
2135 (unless (zerop (tramp-send-command-and-check
2136 multi-method method user host
2137 (format "chmod %s %s"
2138 (tramp-decimal-to-octal mode)
2139 (tramp-shell-quote-argument path))))
2142 ;; FIXME: extract the proper text from chmod's stderr.
2143 "error while changing file's mode"
2146 ;; Simple functions using the `test' command.
2148 (defun tramp-handle-file-executable-p (filename)
2149 "Like `file-executable-p' for tramp files."
2150 (with-parsed-tramp-file-name filename nil
2151 (when (tramp-ange-ftp-file-name-p multi-method method)
2152 (tramp-invoke-ange-ftp 'file-executable-p filename))
2153 (zerop (tramp-run-test "-x" filename))))
2155 (defun tramp-handle-file-readable-p (filename)
2156 "Like `file-readable-p' for tramp files."
2157 (with-parsed-tramp-file-name filename nil
2158 (when (tramp-ange-ftp-file-name-p multi-method method)
2159 (tramp-invoke-ange-ftp 'file-readable-p filename))
2160 (zerop (tramp-run-test "-r" filename))))
2162 (defun tramp-handle-file-accessible-directory-p (filename)
2163 "Like `file-accessible-directory-p' for tramp files."
2164 (with-parsed-tramp-file-name filename nil
2165 (when (tramp-ange-ftp-file-name-p multi-method method)
2166 (tramp-invoke-ange-ftp 'file-accessible-directory-p filename))
2167 (and (zerop (tramp-run-test "-d" filename))
2168 (zerop (tramp-run-test "-r" filename))
2169 (zerop (tramp-run-test "-x" filename)))))
2171 ;; When the remote shell is started, it looks for a shell which groks
2172 ;; tilde expansion. Here, we assume that all shells which grok tilde
2173 ;; expansion will also provide a `test' command which groks `-nt' (for
2174 ;; newer than). If this breaks, tell me about it and I'll try to do
2175 ;; something smarter about it.
2176 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2177 "Like `file-newer-than-file-p' for tramp files."
2178 (cond ((not (file-exists-p file1))
2180 ((not (file-exists-p file2))
2182 ;; We are sure both files exist at this point.
2185 ;; We try to get the mtime of both files. If they are not
2186 ;; equal to the "dont-know" value, then we subtract the times
2187 ;; and obtain the result.
2188 (let ((fa1 (file-attributes file1))
2189 (fa2 (file-attributes file2)))
2190 (if (and (not (equal (nth 5 fa1) '(0 0)))
2191 (not (equal (nth 5 fa2) '(0 0))))
2192 (> 0 (car (subtract-time (nth 5 fa1) (nth 5 fa2))))
2193 ;; If one of them is the dont-know value, then we can
2194 ;; still try to run a shell command on the remote host.
2195 ;; However, this only works if both files are Tramp
2196 ;; files and both have the same method, same user, same
2198 (unless (and (tramp-tramp-file-p file1)
2199 (tramp-tramp-file-p file2))
2203 "Cannot check if Tramp file is newer than non-Tramp file"
2205 (with-parsed-tramp-file-name file1 v1
2206 (with-parsed-tramp-file-name file2 v2
2207 (when (and (tramp-ange-ftp-file-name-p
2208 v1-multi-method v1-method)
2209 (tramp-ange-ftp-file-name-p
2210 v2-multi-method v2-method))
2211 (tramp-invoke-ange-ftp 'file-newer-than-file-p
2213 (unless (and (equal v1-multi-method v2-multi-method)
2214 (equal v1-method v2-method)
2215 (equal v1-user v2-user)
2216 (equal v1-host v2-host))
2218 (list "Files must have same method, user, host"
2220 (unless (and (tramp-tramp-file-p file1)
2221 (tramp-tramp-file-p file2))
2223 (list "Files must be tramp files on same host"
2225 (if (tramp-get-test-groks-nt
2226 v1-multi-method v1-method v1-user v1-host)
2227 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2228 (zerop (tramp-run-test2
2229 "tramp_test_nt" file1 file2)))))))))))
2231 ;; Functions implemented using the basic functions above.
2233 (defun tramp-handle-file-modes (filename)
2234 "Like `file-modes' for tramp files."
2235 (with-parsed-tramp-file-name filename nil
2236 (when (tramp-ange-ftp-file-name-p multi-method method)
2237 (tramp-invoke-ange-ftp 'file-modes filename))
2238 (when (file-exists-p filename)
2239 (tramp-mode-string-to-int
2240 (nth 8 (tramp-handle-file-attributes filename))))))
2242 (defun tramp-handle-file-directory-p (filename)
2243 "Like `file-directory-p' for tramp files."
2244 ;; Care must be taken that this function returns `t' for symlinks
2245 ;; pointing to directories. Surely the most obvious implementation
2246 ;; would be `test -d', but that returns false for such symlinks.
2247 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2248 ;; I now think he's right. So we could be using `test -d', couldn't
2251 ;; Alternatives: `cd %s', `test -d %s'
2252 (with-parsed-tramp-file-name filename nil
2253 (when (tramp-ange-ftp-file-name-p multi-method method)
2254 (tramp-invoke-ange-ftp 'file-directory-p filename))
2257 (tramp-send-command-and-check
2258 multi-method method user host
2259 (format "test -d %s"
2260 (tramp-shell-quote-argument path))
2261 t))))) ;run command in subshell
2263 (defun tramp-handle-file-regular-p (filename)
2264 "Like `file-regular-p' for tramp files."
2265 (with-parsed-tramp-file-name filename nil
2266 (when (tramp-ange-ftp-file-name-p multi-method method)
2267 (tramp-invoke-ange-ftp 'file-regular-p filename))
2268 (and (tramp-handle-file-exists-p filename)
2269 (eq ?- (aref (nth 8 (tramp-handle-file-attributes filename)) 0)))))
2271 (defun tramp-handle-file-symlink-p (filename)
2272 "Like `file-symlink-p' for tramp files."
2273 (with-parsed-tramp-file-name filename nil
2274 (when (tramp-ange-ftp-file-name-p multi-method method)
2275 (tramp-invoke-ange-ftp 'file-symlink-p filename))
2276 (let ((x (car (tramp-handle-file-attributes filename))))
2277 (when (stringp x) x))))
2279 (defun tramp-handle-file-writable-p (filename)
2280 "Like `file-writable-p' for tramp files."
2281 (with-parsed-tramp-file-name filename nil
2282 (when (tramp-ange-ftp-file-name-p multi-method method)
2283 (tramp-invoke-ange-ftp 'file-writable-p filename))
2284 (if (tramp-handle-file-exists-p filename)
2285 ;; Existing files must be writable.
2286 (zerop (tramp-run-test "-w" filename))
2287 ;; If file doesn't exist, check if directory is writable.
2288 (and (zerop (tramp-run-test
2289 "-d" (tramp-handle-file-name-directory filename)))
2290 (zerop (tramp-run-test
2291 "-w" (tramp-handle-file-name-directory filename)))))))
2293 (defun tramp-handle-file-ownership-preserved-p (filename)
2294 "Like `file-ownership-preserved-p' for tramp files."
2295 (with-parsed-tramp-file-name filename nil
2296 (when (tramp-ange-ftp-file-name-p multi-method method)
2297 (tramp-invoke-ange-ftp 'file-ownership-preserved-p filename))
2298 (or (not (tramp-handle-file-exists-p filename))
2299 ;; Existing files must be writable.
2300 (zerop (tramp-run-test "-O" filename)))))
2302 ;; Other file name ops.
2304 ;; ;; Matthias Köppe <mkoeppe@mail.math.uni-magdeburg.de>
2305 ;; (defun tramp-handle-directory-file-name (directory)
2306 ;; "Like `directory-file-name' for tramp files."
2307 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2308 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2309 ;; (substring directory 0 (- (length directory) 1))
2312 ;; Philippe Troin <phil@fifi.org>
2313 (defun tramp-handle-directory-file-name (directory)
2314 "Like `directory-file-name' for tramp files."
2315 (with-parsed-tramp-file-name directory nil
2316 (when (tramp-ange-ftp-file-name-p multi-method method)
2317 (tramp-invoke-ange-ftp 'directory-file-name directory))
2318 (let ((directory-length-1 (1- (length directory))))
2320 (if (and (eq (aref directory directory-length-1) ?/)
2321 (eq (string-match tramp-file-name-regexp directory) 0)
2322 (/= (match-end 0) directory-length-1))
2323 (substring directory 0 directory-length-1)
2326 ;; Directory listings.
2328 (defun tramp-handle-directory-files (directory
2329 &optional full match nosort files-only)
2330 "Like `directory-files' for tramp files."
2331 (with-parsed-tramp-file-name directory nil
2332 (when (tramp-ange-ftp-file-name-p multi-method method)
2333 (tramp-invoke-ange-ftp 'directory-files
2334 directory full match nosort files-only))
2337 (tramp-barf-unless-okay
2338 multi-method method user host
2339 (concat "cd " (tramp-shell-quote-argument path))
2342 "tramp-handle-directory-files: couldn't `cd %s'"
2343 (tramp-shell-quote-argument path))
2345 multi-method method user host
2346 (concat (tramp-get-ls-command multi-method method user host)
2348 (tramp-wait-for-output)
2349 (goto-char (point-max))
2350 (while (zerop (forward-line -1))
2351 (setq x (buffer-substring (point)
2352 (tramp-line-end-position)))
2353 (when (or (not match) (string-match match x))
2355 (push (concat (file-name-as-directory directory)
2359 (tramp-send-command multi-method method user host "cd")
2360 (tramp-wait-for-output)
2361 ;; Remove non-files or non-directories if necessary. Using
2362 ;; the remote shell for this would probably be way faster.
2363 ;; Maybe something could be adapted from
2364 ;; tramp-handle-file-name-all-completions.
2366 (let ((temp (nreverse result))
2369 (if (equal files-only t)
2372 (setq item (pop temp))
2373 (when (file-regular-p item)
2374 (push item result)))
2377 (setq item (pop temp))
2378 (when (file-directory-p item)
2379 (push item result)))))))
2382 ;; This function should return "foo/" for directories and "bar" for
2383 ;; files. We use `ls -ad' to get a list of files (including
2384 ;; directories), and `find . -type d \! -name . -prune' to get a list
2386 (defun tramp-handle-file-name-all-completions (filename directory)
2387 "Like `file-name-all-completions' for tramp files."
2388 (with-parsed-tramp-file-name directory nil
2389 (when (tramp-ange-ftp-file-name-p multi-method method)
2390 (tramp-invoke-ange-ftp 'file-name-all-completions
2391 filename directory))
2392 (unless (save-match-data (string-match "/" filename))
2393 (let* ((nowild tramp-completion-without-shell-p)
2396 (tramp-barf-unless-okay
2397 multi-method method user host
2398 (format "cd %s" (tramp-shell-quote-argument path))
2400 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2401 (tramp-shell-quote-argument path))
2403 ;; Get a list of directories and files, including reliably
2404 ;; tagging the directories with a trailing '/'. Because I
2405 ;; rock. --daniel@danann.net
2407 multi-method method user host
2408 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2409 "if test -d \"$f\" 2>/dev/null; "
2410 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2411 (tramp-get-ls-command multi-method method user host)
2412 (if (or nowild (zerop (length filename)))
2415 (tramp-shell-quote-argument filename)))))
2417 ;; Now grab the output.
2418 (tramp-wait-for-output)
2419 (goto-char (point-max))
2420 (while (zerop (forward-line -1))
2421 (push (buffer-substring (point)
2422 (tramp-line-end-position))
2425 (tramp-send-command multi-method method user host "cd")
2426 (tramp-wait-for-output)
2430 (all-completions filename (mapcar 'list result))
2434 ;; The following isn't needed for Emacs 20 but for 19.34?
2435 (defun tramp-handle-file-name-completion (filename directory)
2436 "Like `file-name-completion' for tramp files."
2437 (unless (tramp-tramp-file-p directory)
2439 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2441 (with-parsed-tramp-file-name directory nil
2442 (when (tramp-ange-ftp-file-name-p multi-method method)
2443 (tramp-invoke-ange-ftp 'file-name-completion
2444 filename directory))
2447 (mapcar (lambda (x) (cons x nil))
2448 (tramp-handle-file-name-all-completions filename directory)))))
2452 (defun tramp-handle-add-name-to-file
2453 (filename newname &optional ok-if-already-exists)
2454 "Like `add-name-to-file' for tramp files."
2455 (with-parsed-tramp-file-name filename v1
2456 (with-parsed-tramp-file-name newname v2
2457 (let ((ln (when v1 (tramp-get-remote-ln
2458 v1-multi-method v1-method v1-user v1-host))))
2459 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2460 (equal v1-multi-method v2-multi-method)
2461 (equal v1-method v2-method)
2462 (equal v1-user v2-user)
2463 (equal v1-host v2-host))
2464 (error "add-name-to-file: %s"
2465 "only implemented for same method, same user, same host"))
2466 (when (and (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2467 (tramp-ange-ftp-file-name-p v2-multi-method v2-method))
2468 (tramp-invoke-ange-ftp 'add-name-to-file
2469 filename newname ok-if-already-exists))
2470 (when (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2471 (tramp-invoke-ange-ftp 'add-name-to-file
2472 filename newname ok-if-already-exists))
2473 (when (tramp-ange-ftp-file-name-p v2-multi-method v2-method)
2474 (tramp-invoke-ange-ftp 'add-name-to-file
2475 filename newname ok-if-already-exists))
2476 (when (and (not ok-if-already-exists)
2477 (file-exists-p newname)
2478 (not (numberp ok-if-already-exists))
2481 "File %s already exists; make it a new name anyway? "
2483 (error "add-name-to-file: file %s already exists" newname))
2484 (tramp-barf-unless-okay
2485 v1-multi-method v1-method v1-user v1-host
2486 (format "%s %s %s" ln (tramp-shell-quote-argument v1-path)
2487 (tramp-shell-quote-argument v2-path))
2489 "error with add-name-to-file, see buffer `%s' for details"
2492 (defun tramp-handle-copy-file
2493 (filename newname &optional ok-if-already-exists keep-date)
2494 "Like `copy-file' for tramp files."
2495 ;; Check if both files are local -- invoke normal copy-file.
2496 ;; Otherwise, use tramp from local system.
2497 (setq filename (expand-file-name filename))
2498 (setq newname (expand-file-name newname))
2499 ;; At least one file a tramp file?
2500 (if (or (tramp-tramp-file-p filename)
2501 (tramp-tramp-file-p newname))
2502 (tramp-do-copy-or-rename-file
2503 'copy filename newname ok-if-already-exists keep-date)
2504 (tramp-run-real-handler
2506 (list filename newname ok-if-already-exists keep-date))))
2508 (defun tramp-handle-rename-file
2509 (filename newname &optional ok-if-already-exists)
2510 "Like `rename-file' for tramp files."
2511 ;; Check if both files are local -- invoke normal rename-file.
2512 ;; Otherwise, use tramp from local system.
2513 (setq filename (expand-file-name filename))
2514 (setq newname (expand-file-name newname))
2515 ;; At least one file a tramp file?
2516 (if (or (tramp-tramp-file-p filename)
2517 (tramp-tramp-file-p newname))
2518 (tramp-do-copy-or-rename-file
2519 'rename filename newname ok-if-already-exists)
2520 (tramp-run-real-handler 'rename-file
2521 (list filename newname ok-if-already-exists))))
2523 (defun tramp-do-copy-or-rename-file
2524 (op filename newname &optional ok-if-already-exists keep-date)
2525 "Copy or rename a remote file.
2526 OP must be `copy' or `rename' and indicates the operation to perform.
2527 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2528 the new file (for copy) or the new name of the file (for rename).
2529 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2530 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2533 This function is invoked by `tramp-handle-copy-file' and
2534 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2535 and `rename'. FILENAME and NEWNAME must be absolute file names."
2536 (unless (memq op '(copy rename))
2537 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2538 (unless ok-if-already-exists
2539 (when (file-exists-p newname)
2540 (signal 'file-already-exists
2542 (let ((t1 (tramp-tramp-file-p filename))
2543 (t2 (tramp-tramp-file-p newname)))
2544 ;; Check which ones of source and target are Tramp files.
2547 ;; Both are Tramp files.
2548 (with-parsed-tramp-file-name filename v1
2549 (with-parsed-tramp-file-name newname v2
2550 ;; Possibly invoke Ange-FTP.
2551 (when (and (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2552 (tramp-ange-ftp-file-name-p v2-multi-method v2-method))
2554 (tramp-invoke-ange-ftp
2555 'copy-file filename newname ok-if-already-exists keep-date)
2556 (tramp-invoke-ange-ftp
2557 'rename-file filename newname ok-if-already-exists)))
2558 ;; Check if we can use a shortcut.
2559 (if (and (equal v1-multi-method v2-multi-method)
2560 (equal v1-method v2-method)
2561 (equal v1-host v2-host)
2562 (equal v1-user v2-user))
2563 ;; Shortcut: if method, host, user are the same for both
2564 ;; files, we invoke `cp' or `mv' on the remote host
2566 (tramp-do-copy-or-rename-file-directly
2567 op v1-multi-method v1-method v1-user v1-host
2568 v1-path v2-path keep-date)
2569 ;; The shortcut was not possible. So we copy the
2570 ;; file first. If the operation was `rename', we go
2571 ;; back and delete the original file (if the copy was
2572 ;; successful). The approach is simple-minded: we
2573 ;; create a new buffer, insert the contents of the
2574 ;; source file into it, then write out the buffer to
2575 ;; the target file. The advantage is that it doesn't
2576 ;; matter which filename handlers are used for the
2577 ;; source and target file.
2579 ;; CCC: If both source and target are Tramp files,
2580 ;; and both are using the same rcp-program, then we
2581 ;; can invoke rcp directly. Note that
2582 ;; default-directory should point to a local
2583 ;; directory if we want to invoke rcp.
2584 (tramp-do-copy-or-rename-via-buffer
2585 op filename newname keep-date)))))
2587 ;; Use the generic method via a Tramp buffer.
2588 (tramp-do-copy-or-rename-via-buffer op filename newname keep-date))
2590 ;; One of them must be a Tramp file.
2591 (error "Tramp implementation says this cannot happen")))))
2593 (defun tramp-do-copy-or-rename-via-buffer (op filename newname keep-date)
2594 "Use an Emacs buffer to copy or rename a file.
2595 First arg OP is either `copy' or `rename' and indicates the operation.
2596 FILENAME is the source file, NEWNAME the target file.
2597 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2598 (let ((trampbuf (get-buffer-create "*tramp output*")))
2601 1 (concat "Warning: cannot preserve file time stamp"
2602 " with inline copying across machines")))
2604 (set-buffer trampbuf) (erase-buffer)
2605 (insert-file-contents-literally filename)
2606 (let ((coding-system-for-write 'no-conversion))
2607 (write-region (point-min) (point-max) newname)))
2608 ;; If the operation was `rename', delete the original file.
2609 (unless (eq op 'copy)
2610 (delete-file filename))))
2612 (defun tramp-do-copy-or-rename-file-directly
2613 (op multi-method method user host path1 path2 keep-date)
2614 "Invokes `cp' or `mv' on the remote system.
2615 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2616 respectively. METHOD, USER, and HOST specify the connection.
2617 PATH1 and PATH2 specify the two arguments of `cp' or `mv'.
2618 If KEEP-DATE is non-nil, preserve the time stamp when copying."
2619 ;; CCC: What happens to the timestamp when renaming?
2620 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
2621 ((eq op 'copy) "cp -f")
2622 ((eq op 'rename) "mv -f")
2624 "Unknown operation `%s', must be `copy' or `rename'"
2627 (tramp-barf-unless-okay
2628 multi-method method user host
2631 (tramp-shell-quote-argument path1)
2632 (tramp-shell-quote-argument path2))
2634 "Copying directly failed, see buffer `%s' for details."
2638 (defun tramp-handle-make-directory (dir &optional parents)
2639 "Like `make-directory' for tramp files."
2640 (setq dir (expand-file-name dir))
2641 (with-parsed-tramp-file-name dir nil
2642 (when (tramp-ange-ftp-file-name-p multi-method method)
2643 (tramp-invoke-ange-ftp 'make-directory dir parents))
2645 (tramp-barf-unless-okay
2646 multi-method method user host
2648 (if parents "mkdir -p" "mkdir")
2649 (tramp-shell-quote-argument path))
2651 "Couldn't make directory %s" dir))))
2653 ;; CCC error checking?
2654 (defun tramp-handle-delete-directory (directory)
2655 "Like `delete-directory' for tramp files."
2656 (setq directory (expand-file-name directory))
2657 (with-parsed-tramp-file-name directory nil
2658 (when (tramp-ange-ftp-file-name-p multi-method method)
2659 (tramp-invoke-ange-ftp 'delete-directory directory))
2662 multi-method method user host
2663 (format "rmdir %s ; echo ok"
2664 (tramp-shell-quote-argument path)))
2665 (tramp-wait-for-output))))
2667 (defun tramp-handle-delete-file (filename)
2668 "Like `delete-file' for tramp files."
2669 (setq filename (expand-file-name filename))
2670 (with-parsed-tramp-file-name filename nil
2671 (when (tramp-ange-ftp-file-name-p multi-method method)
2672 (tramp-invoke-ange-ftp 'delete-file filename))
2674 (unless (zerop (tramp-send-command-and-check
2675 multi-method method user host
2677 (tramp-shell-quote-argument path))))
2678 (signal 'file-error "Couldn't delete Tramp file")))))
2682 ;; CCC: This does not seem to be enough. Something dies when
2683 ;; we try and delete two directories under TRAMP :/
2684 (defun tramp-handle-dired-recursive-delete-directory (filename)
2685 "Recursively delete the directory given.
2686 This is like `dired-recursive-delete-directory' for tramp files."
2687 (with-parsed-tramp-file-name filename nil
2688 (when (tramp-ange-ftp-file-name-p multi-method method)
2689 (tramp-invoke-ange-ftp 'dired-recursive-delete-directory
2691 ;; run a shell command 'rm -r <path>'
2692 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
2693 (or (tramp-handle-file-exists-p filename)
2696 (list "Removing old file name" "no such directory" filename)))
2697 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
2698 (tramp-send-command multi-method method user host
2699 (format "rm -r %s" (tramp-shell-quote-argument path)))
2700 ;; Wait for the remote system to return to us...
2701 ;; This might take a while, allow it plenty of time.
2702 (tramp-wait-for-output 120)
2703 ;; Make sure that it worked...
2704 (and (tramp-handle-file-exists-p filename)
2705 (error "Failed to recusively delete %s" filename))))
2708 (defun tramp-handle-dired-call-process (program discard &rest arguments)
2709 "Like `dired-call-process' for tramp files."
2710 (with-parsed-tramp-file-name default-directory nil
2711 (when (tramp-ange-ftp-file-name-p multi-method method)
2712 (let ((default-directory
2713 (tramp-make-ange-ftp-file-name user host path)))
2714 (tramp-invoke-ange-ftp 'dired-call-process
2715 program discard arguments)))
2717 (tramp-barf-unless-okay
2718 multi-method method user host
2719 (format "cd %s" (tramp-shell-quote-argument path))
2721 "tramp-handle-dired-call-process: Couldn't `cd %s'"
2722 (tramp-shell-quote-argument path))
2724 multi-method method user host
2725 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
2726 (tramp-wait-for-output))
2728 (insert-buffer (tramp-get-buffer multi-method method user host)))
2731 (tramp-send-command-and-check multi-method method user host nil)
2732 (tramp-send-command multi-method method user host "cd")
2733 (tramp-wait-for-output)))))
2735 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
2736 ;; not sure at all that this is the right way to do it, but let's hope
2737 ;; it works for now, and wait for a guru to point out the Right Way to
2739 ;;(eval-when-compile
2740 ;; (unless (fboundp 'dired-insert-set-properties)
2741 ;; (fset 'dired-insert-set-properties 'ignore)))
2742 ;; Gerd suggests this:
2743 (eval-when-compile (require 'dired))
2744 ;; Note that dired is required at run-time, too, when it is needed.
2745 ;; It is only needed on XEmacs for the function
2746 ;; `dired-insert-set-properties'.
2748 (defun tramp-handle-insert-directory
2749 (filename switches &optional wildcard full-directory-p)
2750 "Like `insert-directory' for tramp files."
2751 ;; For the moment, we assume that the remote "ls" program does not
2752 ;; grok "--dired". In the future, we should detect this on
2753 ;; connection setup.
2754 (when (string-match "^--dired\\s-+" switches)
2755 (setq switches (replace-match "" nil t switches)))
2756 (setq filename (expand-file-name filename))
2757 (with-parsed-tramp-file-name filename nil
2758 (when (tramp-ange-ftp-file-name-p multi-method method)
2759 (tramp-invoke-ange-ftp 'insert-directory
2760 filename switches wildcard full-directory-p))
2761 (tramp-message-for-buffer
2762 multi-method method user host 10
2763 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2764 switches filename (if wildcard "yes" "no")
2765 (if full-directory-p "yes" "no"))
2767 (setq wildcard (file-name-nondirectory path))
2768 (setq path (file-name-directory path)))
2769 (when (listp switches)
2770 (setq switches (mapconcat 'identity switches " ")))
2771 (unless full-directory-p
2772 (setq switches (concat "-d " switches)))
2774 (setq switches (concat switches " " wildcard)))
2776 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2777 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2778 (if full-directory-p
2780 multi-method method user host
2782 (tramp-get-ls-command multi-method method user host)
2786 (tramp-shell-quote-argument (concat path ".")))))
2787 (tramp-barf-unless-okay
2788 multi-method method user host
2789 (format "cd %s" (tramp-shell-quote-argument
2790 (file-name-directory path)))
2793 (tramp-shell-quote-argument (file-name-directory path)))
2795 multi-method method user host
2797 (tramp-get-ls-command multi-method method user host)
2799 (if full-directory-p
2800 ;; Add "/." to make sure we got complete dir
2801 ;; listing for symlinks, too.
2802 (concat (file-name-as-directory
2803 (file-name-nondirectory path)) ".")
2804 (file-name-nondirectory path)))))
2805 (sit-for 1) ;needed for rsh but not ssh?
2806 (tramp-wait-for-output))
2807 (let ((old-pos (point)))
2808 (insert-buffer-substring
2809 (tramp-get-buffer multi-method method user host))
2810 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
2811 ;; that doesn't exist on Emacs, so we use this workaround instead.
2812 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
2813 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
2814 ;; (let ((zmacs-region-stays t))
2815 ;; (exchange-point-and-mark))
2817 (tramp-send-command multi-method method user host "cd")
2818 (tramp-wait-for-output))
2819 ;; Another XEmacs specialty follows. What's the right way to do
2821 (when (and (featurep 'xemacs)
2822 (eq major-mode 'dired-mode))
2825 (dired-insert-set-properties old-pos (point)))))))
2827 ;; Continuation of kluge to pacify byte-compiler.
2828 ;;(eval-when-compile
2829 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
2830 ;; (fmakunbound 'dired-insert-set-properties)))
2832 ;; CCC is this the right thing to do?
2833 (defun tramp-handle-unhandled-file-name-directory (filename)
2834 "Like `unhandled-file-name-directory' for tramp files."
2835 (with-parsed-tramp-file-name filename nil
2836 (when (tramp-ange-ftp-file-name-p multi-method method)
2837 (tramp-invoke-ange-ftp 'unhandled-file-name-directory
2839 (expand-file-name "~/")))
2841 ;; Canonicalization of file names.
2843 (defun tramp-drop-volume-letter (name)
2844 "Cut off unnecessary drive letter from file NAME.
2845 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2846 locally on a remote file name. When the local system is a W32 system
2847 but the remote system is Unix, this introduces a superfluous drive
2848 letter into the file name. This function removes it.
2850 Doesn't do anything if the NAME does not start with a drive letter."
2851 (if (and (> (length name) 1)
2852 (char-equal (aref name 1) ?:)
2853 (let ((c1 (aref name 0)))
2854 (or (and (>= c1 ?A) (<= c1 ?Z))
2855 (and (>= c1 ?a) (<= c1 ?z)))))
2859 (defun tramp-handle-expand-file-name (name &optional dir)
2860 "Like `expand-file-name' for tramp files."
2861 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2862 (setq dir (or dir default-directory "/"))
2863 ;; Unless NAME is absolute, concat DIR and NAME.
2864 (unless (file-name-absolute-p name)
2865 (setq name (concat (file-name-as-directory dir) name)))
2866 ;; If NAME is not a tramp file, run the real handler
2867 (if (not (tramp-tramp-file-p name))
2868 (tramp-run-real-handler 'expand-file-name
2871 (with-parsed-tramp-file-name name nil
2872 (when (tramp-ange-ftp-file-name-p multi-method method)
2873 (tramp-invoke-ange-ftp 'expand-file-name name nil))
2874 (unless (file-name-absolute-p path)
2875 (setq path (concat "~/" path)))
2877 ;; Tilde expansion if necessary. This needs a shell which
2878 ;; groks tilde expansion! The function `tramp-find-shell' is
2879 ;; supposed to find such a shell on the remote host. Please
2880 ;; tell me about it when this doesn't work on your system.
2881 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" path)
2882 (let ((uname (match-string 1 path))
2883 (fname (match-string 2 path)))
2884 ;; CCC fanatic error checking?
2885 (set-buffer (tramp-get-buffer multi-method method user host))
2888 multi-method method user host
2889 (format "cd %s; pwd" uname)
2891 (tramp-wait-for-output)
2892 (goto-char (point-min))
2893 (setq uname (buffer-substring (point) (tramp-line-end-position)))
2894 (setq path (concat uname fname))
2896 ;; No tilde characters in file name, do normal
2897 ;; expand-file-name (this does "/./" and "/../"). We bind
2898 ;; directory-sep-char here for XEmacs on Windows, which would
2899 ;; otherwise use backslash.
2900 (let ((directory-sep-char ?/))
2901 (tramp-make-tramp-file-name
2902 multi-method method user host
2903 (tramp-drop-volume-letter
2904 (tramp-run-real-handler 'expand-file-name (list path)))))))))
2908 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
2909 "Like `shell-command' for tramp files.
2910 This will break if COMMAND prints a newline, followed by the value of
2911 `tramp-end-of-output', followed by another newline."
2912 (when (tramp-tramp-file-p default-directory)
2913 (with-parsed-tramp-file-name default-directory nil
2914 (when (tramp-ange-ftp-file-name-p multi-method method)
2915 (let ((default-directory (tramp-make-ange-ftp-file-name
2917 (tramp-invoke-ange-ftp 'shell-command
2918 command output-buffer error-buffer)))
2920 (when (string-match "&[ \t]*\\'" command)
2921 (error "Tramp doesn't grok asynchronous shell commands, yet"))
2923 (error "Tramp doesn't grok optional third arg ERROR-BUFFER, yet"))
2925 (tramp-barf-unless-okay
2926 multi-method method user host
2927 (format "cd %s" (tramp-shell-quote-argument path))
2929 "tramp-handle-shell-command: Couldn't `cd %s'"
2930 (tramp-shell-quote-argument path))
2931 (tramp-send-command multi-method method user host
2932 (concat command "; tramp_old_status=$?"))
2933 ;; This will break if the shell command prints "/////"
2934 ;; somewhere. Let's just hope for the best...
2935 (tramp-wait-for-output))
2936 (unless output-buffer
2937 (setq output-buffer (get-buffer-create "*Shell Command Output*"))
2938 (set-buffer output-buffer)
2940 (unless (bufferp output-buffer)
2941 (setq output-buffer (current-buffer)))
2942 (set-buffer output-buffer)
2943 (insert-buffer (tramp-get-buffer multi-method method user host))
2945 (tramp-send-command multi-method method user host "cd")
2946 (tramp-wait-for-output)
2948 multi-method method user host
2949 (concat "tramp_set_exit_status $tramp_old_status;"
2950 " echo tramp_exit_status $?"))
2951 (tramp-wait-for-output)
2952 (goto-char (point-max))
2953 (unless (search-backward "tramp_exit_status " nil t)
2954 (error "Couldn't find exit status of `%s'" command))
2955 (skip-chars-forward "^ ")
2956 (setq status (read (current-buffer))))
2957 (unless (zerop (buffer-size))
2958 (pop-to-buffer output-buffer))
2960 ;; The following is only executed if something strange was
2961 ;; happening. Emit a helpful message and do it anyway.
2962 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
2964 (tramp-run-real-handler 'shell-command
2965 (list command output-buffer error-buffer)))
2969 (defsubst tramp-make-temp-file ()
2970 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
2971 (expand-file-name tramp-temp-name-prefix
2972 (tramp-temporary-file-directory))))
2974 (defun tramp-handle-file-local-copy (filename)
2975 "Like `file-local-copy' for tramp files."
2976 (with-parsed-tramp-file-name filename nil
2977 (when (tramp-ange-ftp-file-name-p multi-method method)
2978 (tramp-invoke-ange-ftp 'file-local-copy filename))
2979 (let ((trampbuf (get-buffer-create "*tramp output*"))
2980 (rcp-program (tramp-get-rcp-program
2982 (tramp-find-method multi-method method user host)
2984 (rcp-args (tramp-get-rcp-args
2986 (tramp-find-method multi-method method user host)
2989 (unless (file-exists-p filename)
2990 (error "Cannot make local copy of non-existing file `%s'"
2992 (setq tmpfil (tramp-make-temp-file))
2994 ;; Use rcp-like program for file transfer.
2995 (tramp-message-for-buffer
2996 multi-method method user host
2997 5 "Fetching %s to tmp file %s..." filename tmpfil)
2998 (save-excursion (set-buffer trampbuf) (erase-buffer))
3001 (apply #'call-process
3006 (tramp-make-rcp-program-file-name
3008 (tramp-shell-quote-argument path))
3010 (pop-to-buffer trampbuf)
3012 (concat "tramp-handle-file-local-copy: `%s' didn't work, "
3013 "see buffer `%s' for details")
3014 rcp-program trampbuf))
3015 (tramp-message-for-buffer
3016 multi-method method user host
3017 5 "Fetching %s to tmp file %s...done" filename tmpfil))
3018 ((and (tramp-get-remote-encoding multi-method method user host)
3019 (tramp-get-remote-decoding multi-method method user host))
3020 ;; Use inline encoding for file transfer.
3022 ;; Following line for setting tramp-current-method,
3023 ;; tramp-current-user, tramp-current-host.
3024 (set-buffer (tramp-get-buffer multi-method method user host))
3025 (tramp-message 5 "Encoding remote file %s..." filename)
3026 (tramp-barf-unless-okay
3027 multi-method method user host
3028 (concat (tramp-get-remote-encoding
3029 multi-method method user host)
3030 " < " (tramp-shell-quote-argument path))
3032 "Encoding remote file failed, see buffer `%s' for details"
3033 (tramp-get-buffer multi-method method user host))
3034 ;; Remove trailing status code
3035 (goto-char (point-max))
3036 (delete-region (point) (progn (forward-line -1) (point)))
3038 (tramp-message 5 "Decoding remote file %s..." filename)
3040 (let ((loc-enc (tramp-get-local-encoding
3041 multi-method method user host))
3042 (loc-dec (tramp-get-local-decoding
3043 multi-method method user host)))
3044 (if (and (symbolp loc-dec) (fboundp loc-dec))
3045 ;; If local decoding is a function, we call it.
3046 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3049 (insert-buffer (tramp-get-buffer multi-method method
3051 (tramp-message-for-buffer
3052 multi-method method user host
3053 6 "Decoding remote file %s with function %s..."
3056 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3057 ;; says that we need to strip tramp_exit_status
3058 ;; line from the output here. Go to point-max,
3059 ;; search backward for tramp_exit_status, delete
3060 ;; between point and point-max if found.
3061 (let ((coding-system-for-write 'no-conversion))
3062 (funcall loc-dec (point-min) (point-max))
3063 (write-region (point-min) (point-max) tmpfil))
3064 (kill-buffer tmpbuf))
3065 ;; If tramp-decoding-function is not defined for this
3066 ;; method, we invoke tramp-decoding-command instead.
3067 (let ((tmpfil2 (tramp-make-temp-file)))
3068 (write-region (point-min) (point-max) tmpfil2)
3070 6 "Decoding remote file %s with command %s..."
3072 (tramp-call-local-coding-command
3073 loc-dec tmpfil2 tmpfil)
3074 (delete-file tmpfil2)))
3075 (tramp-message-for-buffer
3076 multi-method method user host
3077 5 "Decoding remote file %s...done" filename))))
3079 (t (error "Wrong method specification for `%s'" method)))
3083 (defun tramp-handle-insert-file-contents
3084 (filename &optional visit beg end replace)
3085 "Like `insert-file-contents' for tramp files."
3086 (barf-if-buffer-read-only)
3087 (setq filename (expand-file-name filename))
3088 (with-parsed-tramp-file-name filename nil
3089 (when (tramp-ange-ftp-file-name-p multi-method method)
3090 (tramp-invoke-ange-ftp 'insert-file-contents
3091 filename visit beg end replace))
3092 (if (not (tramp-handle-file-exists-p filename))
3095 (setq buffer-file-name filename)
3096 (set-visited-file-modtime)
3097 (set-buffer-modified-p nil))
3099 (format "File `%s' not found on remote host" filename))
3100 (list (tramp-handle-expand-file-name filename) 0))
3101 (let ((local-copy (tramp-handle-file-local-copy filename))
3102 (coding-system-used nil)
3105 (setq buffer-file-name filename)
3106 (set-visited-file-modtime)
3107 (set-buffer-modified-p nil))
3108 (tramp-message-for-buffer
3109 multi-method method user host
3110 9 "Inserting local temp file `%s'..." local-copy)
3112 (tramp-run-real-handler 'insert-file-contents
3113 (list local-copy nil beg end replace)))
3114 ;; Now `last-coding-system-used' has right value. Remember it.
3115 (when (boundp 'last-coding-system-used)
3116 (setq coding-system-used last-coding-system-used))
3117 (tramp-message-for-buffer
3118 multi-method method user host
3119 9 "Inserting local temp file `%s'...done" local-copy)
3120 (delete-file local-copy)
3121 (when (boundp 'last-coding-system-used)
3122 (setq last-coding-system-used coding-system-used))
3123 (list (expand-file-name filename)
3124 (second result))))))
3126 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3127 (defun tramp-handle-write-region
3128 (start end filename &optional append visit lockname confirm)
3129 "Like `write-region' for tramp files."
3130 (unless (eq append nil)
3131 (error "Cannot append to file using tramp (`%s')" filename))
3132 (setq filename (expand-file-name filename))
3133 ;; Following part commented out because we don't know what to do about
3134 ;; file locking, and it does not appear to be a problem to ignore it.
3135 ;; Ange-ftp ignores it, too.
3136 ;; (when (and lockname (stringp lockname))
3137 ;; (setq lockname (expand-file-name lockname)))
3138 ;; (unless (or (eq lockname nil)
3139 ;; (string= lockname filename))
3141 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3142 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
3143 (when (and (not (featurep 'xemacs))
3144 confirm (file-exists-p filename))
3145 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3147 (error "File not overwritten")))
3148 (with-parsed-tramp-file-name filename nil
3149 (when (tramp-ange-ftp-file-name-p multi-method method)
3150 (tramp-invoke-ange-ftp 'write-region
3151 start end filename append visit))
3152 (let ((curbuf (current-buffer))
3153 (rcp-program (tramp-get-rcp-program
3154 multi-method (tramp-find-method multi-method method user host)
3156 (rcp-args (tramp-get-rcp-args
3158 (tramp-find-method multi-method method user host)
3160 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3161 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3162 (loc-enc (tramp-get-local-encoding multi-method method user host))
3163 (loc-dec (tramp-get-local-decoding multi-method method user host))
3164 (trampbuf (get-buffer-create "*tramp output*"))
3165 ;; We use this to save the value of `last-coding-system-used'
3166 ;; after writing the tmp file. At the end of the function,
3167 ;; we set `last-coding-system-used' to this saved value.
3168 ;; This way, any intermediary coding systems used while
3169 ;; talking to the remote shell or suchlike won't hose this
3170 ;; variable. This approach was snarfed from ange-ftp.el.
3173 ;; Write region into a tmp file. This isn't really needed if we
3174 ;; use an encoding function, but currently we use it always
3175 ;; because this makes the logic simpler.
3176 (setq tmpfil (tramp-make-temp-file))
3177 ;; We say `no-message' here because we don't want the visited file
3178 ;; modtime data to be clobbered from the temp file. We call
3179 ;; `set-visited-file-modtime' ourselves later on.
3180 (tramp-run-real-handler
3182 (if confirm ; don't pass this arg unless defined for backward compat.
3183 (list start end tmpfil append 'no-message lockname confirm)
3184 (list start end tmpfil append 'no-message lockname)))
3185 ;; Now, `last-coding-system-used' has the right value. Remember it.
3186 (when (boundp 'last-coding-system-used)
3187 (setq coding-system-used last-coding-system-used))
3188 ;; This is a bit lengthy due to the different methods possible for
3189 ;; file transfer. First, we check whether the method uses an rcp
3190 ;; program. If so, we call it. Otherwise, both encoding and
3191 ;; decoding command must be specified. However, if the method
3192 ;; _also_ specifies an encoding function, then that is used for
3193 ;; encoding the contents of the tmp file.
3195 ;; use rcp-like program for file transfer
3196 (let ((argl (append rcp-args
3199 (tramp-make-rcp-program-file-name
3201 (tramp-shell-quote-argument path))))))
3202 (tramp-message-for-buffer
3203 multi-method method user host
3204 6 "Writing tmp file using `%s'..." rcp-program)
3205 (save-excursion (set-buffer trampbuf) (erase-buffer))
3206 (when tramp-debug-buffer
3208 (set-buffer (tramp-get-debug-buffer multi-method
3210 (goto-char (point-max))
3211 (tramp-insert-with-face
3212 'bold (format "$ %s %s\n" rcp-program
3213 (mapconcat 'identity argl " ")))))
3215 (apply #'call-process
3216 rcp-program nil trampbuf nil argl))
3217 (pop-to-buffer trampbuf)
3219 "Cannot write region to file `%s', command `%s' failed"
3220 filename rcp-program))
3221 (tramp-message-for-buffer
3222 multi-method method user host
3223 6 "Transferring file using `%s'...done"
3225 ((and rem-enc rem-dec)
3226 ;; Use inline file transfer
3227 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
3229 ;; Encode tmpfil into tmpbuf
3230 (tramp-message-for-buffer multi-method method user host
3231 5 "Encoding region...")
3234 ;; Use encoding function or command.
3235 (if (and (symbolp loc-enc) (fboundp loc-enc))
3237 (tramp-message-for-buffer
3238 multi-method method user host
3239 6 "Encoding region using function...")
3240 (insert-file-contents-literally tmpfil)
3241 ;; CCC. The following `let' is a workaround for
3242 ;; the base64.el that comes with pgnus-0.84. If
3243 ;; both of the following conditions are
3244 ;; satisfied, it tries to write to a local file
3245 ;; in default-directory, but at this point,
3246 ;; default-directory is remote.
3247 ;; (CALL-PROCESS-REGION can't write to remote
3248 ;; files, it seems.) The file in question is a
3250 (let ((default-directory
3251 (tramp-temporary-file-directory)))
3252 (funcall loc-enc (point-min) (point-max)))
3253 (goto-char (point-max))
3256 (tramp-message-for-buffer
3257 multi-method method user host
3258 6 "Encoding region using command `%s'..." loc-enc)
3259 (unless (equal 0 (tramp-call-local-coding-command
3261 (pop-to-buffer trampbuf)
3262 (error (concat "Cannot write to `%s', local encoding"
3263 " command `%s' failed")
3265 ;; Send tmpbuf into remote decoding command which
3266 ;; writes to remote file. Because this happens on the
3267 ;; remote host, we cannot use the function.
3268 (tramp-message-for-buffer
3269 multi-method method user host
3270 5 "Decoding region into remote file %s..." filename)
3272 multi-method method user host
3273 (format "%s >%s <<'EOF'"
3275 (tramp-shell-quote-argument path)))
3277 (tramp-message-for-buffer
3278 multi-method method user host
3279 6 "Sending data to remote host...")
3280 (tramp-send-region multi-method method user host
3281 (point-min) (point-max))
3282 ;; wait for remote decoding to complete
3283 (tramp-message-for-buffer
3284 multi-method method user host
3285 6 "Sending end of data token...")
3287 multi-method method user host "EOF" nil t)
3288 (tramp-message-for-buffer
3289 multi-method method user host 6
3290 "Waiting for remote host to process data...")
3291 (set-buffer (tramp-get-buffer multi-method method user host))
3292 (tramp-wait-for-output)
3293 (tramp-barf-unless-okay
3294 multi-method method user host nil nil 'file-error
3295 (concat "Couldn't write region to `%s',"
3296 " decode using `%s' failed")
3298 (tramp-message 5 "Decoding region into remote file %s...done"
3300 (kill-buffer tmpbuf))))
3303 (concat "Method `%s' should specify both encoding and "
3304 "decoding command or an rcp program")
3306 (delete-file tmpfil)
3307 (unless (equal curbuf (current-buffer))
3308 (error "Buffer has changed from `%s' to `%s'"
3309 curbuf (current-buffer)))
3311 (set-visited-file-modtime))
3312 ;; Make `last-coding-system-used' have the right value.
3313 (when (boundp 'last-coding-system-used)
3314 (setq last-coding-system-used coding-system-used))
3315 (when (or (eq visit t)
3318 (message "Wrote %s" filename)))))
3320 ;; Call down to the real handler.
3321 ;; Because EFS does not play nicely with TRAMP (both systems match an
3322 ;; TRAMP path) it is needed to disable efs as well as tramp for the
3325 ;; Other than that, this is the canon file-handler code that the doco
3326 ;; says should be used here. Which is nice.
3328 ;; Under XEmacs current, EFS also hooks in as
3329 ;; efs-sifn-handler-function to handle any path with environment
3330 ;; variables. This has two implications:
3331 ;; 1) That EFS may not be completely dead (yet) for TRAMP paths
3332 ;; 2) That TRAMP might want to do the same thing.
3333 ;; Details as they come in.
3335 ;; Daniel Pittman <daniel@danann.net>
3337 ;; (defun tramp-run-real-handler (operation args)
3338 ;; "Invoke normal file name handler for OPERATION.
3339 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
3340 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
3342 ;; (let ((inhibit-file-name-handlers
3343 ;; (list 'tramp-file-name-handler
3344 ;; 'efs-file-handler-function
3345 ;; 'ange-ftp-hook-function
3346 ;; (and (eq inhibit-file-name-operation operation)
3347 ;; inhibit-file-name-handlers)))
3348 ;; (inhibit-file-name-operation operation))
3349 ;; (apply operation args)))
3351 (defun tramp-run-real-handler (operation args)
3352 "Invoke normal file name handler for OPERATION.
3353 First arg specifies the OPERATION, second arg is a list of arguments to
3354 pass to the OPERATION."
3355 (let* ((op (if (eq operation 'ange-ftp-hook-function)
3358 (inhibit-file-name-handlers
3359 (list 'tramp-file-name-handler
3360 'tramp-completion-file-name-handler
3361 (and (eq inhibit-file-name-operation op)
3362 inhibit-file-name-handlers)))
3363 (inhibit-file-name-operation op))
3364 (apply operation args)))
3366 ;; This function is used from `tramp-completion-file-name-handler' functions
3367 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
3368 ;; because the check is based on a full filename, not available for all
3369 ;; basic I/O operations.
3370 (defun tramp-completion-run-real-handler (operation args)
3371 "Invoke `tramp-file-name-handler' for OPERATION.
3372 First arg specifies the OPERATION, second arg is a list of arguments to
3373 pass to the OPERATION."
3374 (let* ((op (if (eq operation 'ange-ftp-hook-function)
3377 (inhibit-file-name-handlers
3378 (list 'tramp-completion-file-name-handler
3379 (and (eq inhibit-file-name-operation op)
3380 inhibit-file-name-handlers)))
3381 (inhibit-file-name-operation op))
3382 (apply operation args)))
3386 (defun tramp-file-name-handler (operation &rest args)
3387 "Invoke tramp file name handler.
3388 Falls back to normal file name handler if no tramp file name handler exists."
3389 (let ((fn (assoc operation tramp-file-name-handler-alist)))
3391 (catch 'tramp-forward-to-ange-ftp
3392 (save-match-data (apply (cdr fn) args)))
3393 (tramp-run-real-handler operation args))))
3395 (put 'tramp-file-name-handler 'file-remote-p t) ;for file-remote-p
3398 (defun tramp-completion-file-name-handler (operation &rest args)
3399 "Invoke tramp file name completion handler.
3400 Falls back to normal file name handler if no tramp file name handler exists."
3401 ;; (setq tramp-debug-buffer t)
3402 ;; (tramp-message 1 "%s %s" operation args)
3403 ;; (tramp-message 1 "%s %s\n%s"
3404 ;; operation args (with-output-to-string (backtrace)))
3405 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
3407 (catch 'tramp-forward-to-ange-ftp
3408 (save-match-data (apply (cdr fn) args)))
3409 (tramp-completion-run-real-handler operation args))))
3411 ;; Register in file name handler alist
3413 (add-to-list 'file-name-handler-alist
3414 (cons tramp-file-name-regexp 'tramp-file-name-handler))
3415 (add-to-list 'file-name-handler-alist
3416 (cons tramp-completion-file-name-regexp
3417 'tramp-completion-file-name-handler))
3419 ;; To handle EFS, the following functions need to be dealt with:
3421 ;; * dired-before-readin-hook contains efs-dired-before-readin
3422 ;; * file-name-handler-alist contains efs-file-handler-function
3423 ;; and efs-root-handler-function and efs-sifn-handler-function
3424 ;; * find-file-hooks contains efs-set-buffer-mode
3426 ;; But it won't happen for EFS since the XEmacs maintainers
3427 ;; don't want to use a unified filename syntax.
3428 (defun tramp-disable-ange-ftp ()
3430 This is useful for unified remoting. See
3431 `tramp-file-name-structure-unified' and
3432 `tramp-file-name-structure-separate' for details. Requests suitable
3433 for Ange-FTP will be forwarded to Ange-FTP. Also see the variables
3434 `tramp-ftp-method', `tramp-default-method', and
3435 `tramp-default-method-alist'.
3437 This function is not needed in Emacsen which include Tramp, but is
3438 present for backward compatibility."
3439 (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist))
3440 (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist)))
3441 (setq file-name-handler-alist
3442 (delete a1 (delete a2 file-name-handler-alist)))))
3443 (tramp-disable-ange-ftp)
3445 (defun tramp-repair-jka-compr ()
3446 "If jka-compr is already loaded, move it to the front of
3447 `file-name-handler-alist'. On Emacs 21.4 or so this will not be
3449 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
3451 (setq file-name-handler-alist
3452 (cons jka (delete jka file-name-handler-alist))))))
3453 (tramp-repair-jka-compr)
3455 (defun tramp-flatten-list (arg)
3456 "Expands all lists inside ARG to a sequential list.
3457 Return (nil) if arg is nil."
3458 (let ((car (car arg))
3461 ((eq arg nil) '(nil))
3464 (tramp-flatten-list car)
3465 (append (tramp-flatten-list car) (tramp-flatten-list cdr))))
3466 ((null cdr) (list car))
3467 (t (cons car (tramp-flatten-list cdr))))))
3469 (defun tramp-invoke-ange-ftp (operation &rest args)
3470 "Invoke the Ange-FTP handler function and throw."
3471 (or (boundp 'ange-ftp-name-format)
3472 (and (require 'ange-ftp)
3473 (tramp-disable-ange-ftp)))
3474 (let ((ange-ftp-name-format
3475 (list (nth 0 tramp-file-name-structure)
3476 (nth 3 tramp-file-name-structure)
3477 (nth 2 tramp-file-name-structure)
3478 (nth 4 tramp-file-name-structure))))
3479 (throw 'tramp-forward-to-ange-ftp
3480 (tramp-run-real-handler 'ange-ftp-hook-function
3481 (cons operation args)))))
3483 (defun tramp-ange-ftp-file-name-p (multi-method method)
3484 "Check if it's a filename that should be forwarded to Ange-FTP."
3485 (and tramp-unified-filenames
3487 (string= method tramp-ftp-method)))
3490 ;;; Interactions with other packages:
3492 ;; -- complete.el --
3494 ;; This function contributed by Ed Sabol
3495 (defun tramp-handle-expand-many-files (name)
3496 "Like `PC-expand-many-files' for tramp files."
3497 (with-parsed-tramp-file-name name nil
3498 (when (tramp-ange-ftp-file-name-p multi-method method)
3499 (tramp-invoke-ange-ftp 'expand-many-files name))
3501 (if (or (string-match "\\*" name)
3502 (string-match "\\?" name)
3503 (string-match "\\[.*\\]" name))
3506 ;; CCC: To do it right, we should quote certain characters
3507 ;; in the file name, but since the echo command is going to
3508 ;; break anyway when there are spaces in the file names, we
3510 ;;-(let ((comint-file-name-quote-list
3511 ;;- (set-difference tramp-file-name-quote-list
3512 ;;- '(?\* ?\? ?[ ?]))))
3513 ;;- (tramp-send-command
3514 ;;- multi-method method user host
3515 ;;- (format "echo %s" (comint-quote-filename path)))
3516 ;;- (tramp-wait-for-output))
3517 (tramp-send-command multi-method method user host
3518 (format "echo %s" path))
3519 (tramp-wait-for-output)
3520 (setq bufstr (buffer-substring (point-min)
3521 (tramp-line-end-position)))
3522 (goto-char (point-min))
3523 (if (string-equal path bufstr)
3526 (while (search-forward " " nil t)
3527 (delete-backward-char 1)
3529 (goto-char (point-max))
3530 (delete-backward-char 1)
3532 (goto-char (point-min))
3534 (function (lambda (x)
3535 (tramp-make-tramp-file-name multi-method method
3537 (read (current-buffer))))))
3538 (list (tramp-handle-expand-file-name name))))))
3540 ;; Check for complete.el and override PC-expand-many-files if appropriate.
3542 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
3544 (defun tramp-setup-complete ()
3545 (fset 'tramp-save-PC-expand-many-files
3546 (symbol-function 'PC-expand-many-files))
3547 (defun PC-expand-many-files (name)
3548 (if (tramp-tramp-file-p name)
3549 (tramp-handle-expand-many-files name)
3550 (tramp-save-PC-expand-many-files name))))
3552 ;; Why isn't eval-after-load sufficient?
3553 (if (fboundp 'PC-expand-many-files)
3554 (tramp-setup-complete)
3555 (eval-after-load "complete" '(tramp-setup-complete)))
3557 ;;; File name handler functions for completion mode
3559 ;; Necessary because `tramp-file-name-regexp-unified' and
3560 ;; `tramp-completion-file-name-regexp-unified' aren't different.
3561 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
3562 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
3563 ;; Using `last-input-event' is a little bit risky, because completing a file
3564 ;; might require loading other files, like "~/.netrc", and for them it
3565 ;; shouldn't be decided based on that variable. On the other hand, those files
3566 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
3567 ;; be introduced overwriting this check in such cases. Or we change tramp
3568 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
3569 ;; In case of XEmacs it can be always true (and wouldn't be necessary).
3570 (defun tramp-completion-mode (file)
3571 "Checks whether method / user name / host name completion is active."
3573 ((featurep 'xemacs) t)
3574 ((string-match "^/.*:.*:$" file) nil)
3578 tramp-method-regexp tramp-postfix-single-method-regexp "$")
3580 (member (match-string 1 file)
3581 (cons tramp-ftp-method (mapcar 'car tramp-methods))))
3582 ((or (equal last-input-event 'tab)
3583 (and (not (event-modifiers last-input-event))
3584 (integerp last-input-event)
3585 (or (char-equal last-input-event ?\?)
3586 (char-equal last-input-event ?\t) ; handled by 'tab already?
3587 (char-equal last-input-event ?\ ))))
3590 (defun tramp-completion-handle-file-exists-p (filename)
3591 "Like `file-exists-p' for tramp files."
3592 (if (tramp-completion-mode filename)
3593 (tramp-run-real-handler
3594 'file-exists-p (list filename))
3595 (tramp-completion-run-real-handler
3596 'file-exists-p (list filename))))
3598 ;; Path manipulation in case of partial TRAMP file names.
3599 (defun tramp-completion-handle-file-name-directory (file)
3600 "Like `file-name-directory' but aware of TRAMP files."
3601 (if (tramp-completion-mode file)
3603 (tramp-completion-run-real-handler
3604 'file-name-directory (list file))))
3606 ;; Path manipulation in case of partial TRAMP file names.
3607 (defun tramp-completion-handle-file-name-nondirectory (file)
3608 "Like `file-name-nondirectory' but aware of TRAMP files."
3610 file (length (tramp-completion-handle-file-name-directory file))))
3612 ;; Method, host name and user name completion.
3613 ;; `tramp-completion-dissect-file-name' returns a list of
3614 ;; tramp-file-name structures. For all of them we return possible completions.
3615 (defun tramp-completion-handle-file-name-all-completions (filename directory)
3616 "Like `file-name-all-completions' for partial tramp files."
3619 ((fullname (concat directory filename))
3622 (if (tramp-completion-mode fullname)
3623 (tramp-run-real-handler
3624 'file-name-all-completions (list filename directory))
3625 (tramp-completion-run-real-handler
3626 'file-name-all-completions (list filename directory))))
3627 ;; possible completion structures
3628 (v (tramp-completion-dissect-file-name fullname)))
3631 (let* ((car (car v))
3632 (multi-method (tramp-file-name-multi-method car))
3633 (method (tramp-file-name-method car))
3634 (user (tramp-file-name-user car))
3635 (host (tramp-file-name-host car))
3636 (path (tramp-file-name-path car))
3637 (m (tramp-find-method multi-method method user host))
3640 (unless (or multi-method ;; Not handled (yet).
3641 path) ;; Nothing to complete
3645 ;; Method dependent user / host combinations
3649 (setq all-user-hosts
3650 (append all-user-hosts
3651 (funcall (nth 0 x) (nth 1 x)))))
3652 (tramp-get-completion-function m))
3654 (setq result (append result
3657 (tramp-get-completion-user-host
3658 method user host (nth 0 x) (nth 1 x)))
3659 (delq nil all-user-hosts)))))
3663 (append result (tramp-get-completion-methods m)))))
3665 (setq v (delq car v))))
3667 ;;; unify list, remove nil elements
3670 (let ((car (car result)))
3671 (when car (add-to-list 'result1 car))
3672 (setq result (delq car result))))
3676 ;; Method, host name and user name completion for a file.
3677 (defun tramp-completion-handle-file-name-completion (filename directory)
3678 "Like `file-name-completion' for tramp files."
3679 (try-completion filename
3680 (mapcar 'list (file-name-all-completions filename directory))))
3682 ;; I misuse a little bit the tramp-file-name structure in order to handle
3683 ;; completion possibilities for partial methods / user names / host names.
3684 ;; Return value is a list of tramp-file-name structures according to possible
3685 ;; completions. If "multi-method" or "path" is non-nil it means there
3686 ;; shouldn't be a completion anymore.
3688 ;; Expected results:
3690 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
3691 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
3692 ;; [nil nil "x" nil nil]
3693 ;; [nil "x" nil nil nil]
3695 ;; "/x:" "/x:y" "/x:y:"
3696 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
3698 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
3699 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
3701 ;; "/x:y@" "/x:y@z" "/x:y@z:"
3702 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
3703 ;; "/[x/y@" "/[x/y@z"
3704 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
3705 (defun tramp-completion-dissect-file-name (name)
3706 "Returns a list of `tramp-file-name' structures.
3707 They are collected by `tramp-completion-dissect-file-name1'."
3710 (x-nil "\\|\\(\\)"))
3712 ;; "/method" "/[method"
3713 (defconst tramp-completion-file-name-structure1
3714 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
3717 (defconst tramp-completion-file-name-structure2
3718 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
3721 (defconst tramp-completion-file-name-structure3
3722 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
3724 ;; "/user@host" "/[user@host"
3725 (defconst tramp-completion-file-name-structure4
3726 (list (concat tramp-prefix-regexp
3727 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
3728 "\\(" tramp-host-regexp x-nil "\\)$")
3730 ;; "/method:user" "/[method/user"
3731 (defconst tramp-completion-file-name-structure5
3732 (list (concat tramp-prefix-regexp
3733 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
3734 "\\(" tramp-user-regexp x-nil "\\)$")
3736 ;; "/method:host" "/[method/host"
3737 (defconst tramp-completion-file-name-structure6
3738 (list (concat tramp-prefix-regexp
3739 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
3740 "\\(" tramp-host-regexp x-nil "\\)$")
3742 ;; "/method:user@host" "/[method/user@host"
3743 (defconst tramp-completion-file-name-structure7
3744 (list (concat tramp-prefix-regexp
3745 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
3746 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
3747 "\\(" tramp-host-regexp x-nil "\\)$")
3750 (mapcar '(lambda (regexp)
3751 (add-to-list 'result
3752 (tramp-completion-dissect-file-name1 regexp name)))
3754 tramp-completion-file-name-structure1
3755 tramp-completion-file-name-structure2
3756 tramp-completion-file-name-structure3
3757 tramp-completion-file-name-structure4
3758 tramp-completion-file-name-structure5
3759 tramp-completion-file-name-structure6
3760 tramp-completion-file-name-structure7
3761 tramp-file-name-structure))
3765 (defun tramp-completion-dissect-file-name1 (structure name)
3766 "Returns a `tramp-file-name' structure matching STRUCTURE.
3767 The structure consists of multi-method, remote method, remote user,
3768 remote host and remote path name."
3772 (when (string-match (nth 0 structure) name)
3773 (setq method (match-string (nth 1 structure) name))
3774 (if (and method (member method tramp-multi-methods))
3775 ;; Not handled (yet).
3776 (make-tramp-file-name
3777 :multi-method method
3782 (let ((user (match-string (nth 2 structure) name))
3783 (host (match-string (nth 3 structure) name))
3784 (path (match-string (nth 4 structure) name)))
3785 (make-tramp-file-name
3792 ;; This function returns all possible method completions, adding the
3793 ;; trailing method delimeter.
3794 ;; In case of Emacs, `tramp-ftp-method' is handled as well because it doesn't
3795 ;; belong to `tramp-methods'.
3796 (defun tramp-get-completion-methods (partial-method)
3797 "Returns all method completions for PARTIAL-METHOD."
3798 (let ((all-methods (delete "multi" (mapcar 'car tramp-methods))))
3803 (string-match (concat "^" (regexp-quote partial-method)) method)
3804 ;; we must remove leading "/".
3805 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
3807 (add-to-list 'all-methods
3808 (when tramp-unified-filenames tramp-ftp-method)))))
3810 ;; Compares partial user and host names with possible completions.
3811 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
3812 "Returns the most expanded string for user and host name completion.
3813 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
3816 ((and partial-user partial-host)
3818 (string-match (concat "^" (regexp-quote partial-host)) host)
3819 (string-equal partial-user (or user partial-user)))
3820 (setq user partial-user)
3827 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
3833 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
3839 (unless (zerop (+ (length user) (length host)))
3840 ;; we must remove leading "/".
3841 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
3843 (defun tramp-parse-rhosts (filename)
3844 "Return a list of (user host) tuples allowed to access.
3845 Either user or host may be nil."
3848 (when (file-exists-p filename)
3850 (insert-file-contents filename)
3851 (goto-char (point-min))
3853 (push (tramp-parse-rhosts-group) res))))
3856 ;; Taken from gnus/netrc.el
3858 (defalias 'tramp-point-at-eol
3859 (if (fboundp 'point-at-eol)
3861 'line-end-position)))
3863 (defun tramp-parse-rhosts-group ()
3864 "Return a (user host) tuple allowed to access.
3865 Either user or host may be nil."
3870 "^\\(" tramp-host-regexp "\\)"
3871 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
3873 (narrow-to-region (point) (tramp-point-at-eol))
3874 (when (re-search-forward regexp nil t)
3875 (setq result (append (list (match-string 3) (match-string 1)))))
3880 (defun tramp-parse-shosts (filename)
3881 "Return a list of (user host) tuples allowed to access.
3882 User is always nil."
3885 (when (file-exists-p filename)
3887 (insert-file-contents filename)
3888 (goto-char (point-min))
3890 (push (tramp-parse-shosts-group) res))))
3893 (defun tramp-parse-shosts-group ()
3894 "Return a (user host) tuple allowed to access.
3895 User is always nil."
3898 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
3900 (narrow-to-region (point) (tramp-point-at-eol))
3901 (when (re-search-forward regexp nil t)
3902 (setq result (list nil (match-string 1))))
3905 (> (skip-chars-forward ",") 0)
3909 (defun tramp-parse-hosts (filename)
3910 "Return a list of (user host) tuples allowed to access.
3911 User is always nil."
3914 (when (file-exists-p filename)
3916 (insert-file-contents filename)
3917 (goto-char (point-min))
3919 (push (tramp-parse-hosts-group) res))))
3922 (defun tramp-parse-hosts-group ()
3923 "Return a (user host) tuple allowed to access.
3924 User is always nil."
3927 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
3929 (narrow-to-region (point) (tramp-point-at-eol))
3930 (when (re-search-forward regexp nil t)
3931 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
3932 (setq result (list nil (match-string 1)))))
3935 (> (skip-chars-forward " \t") 0)
3939 (defun tramp-parse-passwd (filename)
3940 "Return a list of (user host) tuples allowed to access.
3941 Host is always \"localhost\"."
3944 (if (and (symbolp 'user) (zerop (length user)))
3946 (when (file-exists-p filename)
3948 (insert-file-contents filename)
3949 (goto-char (point-min))
3951 (push (tramp-parse-passwd-group) res))))
3954 (defun tramp-parse-passwd-group ()
3955 "Return a (user host) tuple allowed to access.
3956 Host is always \"localhost\"."
3959 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
3961 (narrow-to-region (point) (tramp-point-at-eol))
3962 (when (re-search-forward regexp nil t)
3963 (setq result (list (match-string 1) "localhost")))
3968 (defun tramp-parse-netrc (filename)
3969 "Return a list of (user host) tuples allowed to access.
3973 (when (file-exists-p filename)
3975 (insert-file-contents filename)
3976 (goto-char (point-min))
3978 (push (tramp-parse-netrc-group) res))))
3981 (defun tramp-parse-netrc-group ()
3982 "Return a (user host) tuple allowed to access.
3988 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
3989 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
3991 (narrow-to-region (point) (tramp-point-at-eol))
3992 (when (re-search-forward regexp nil t)
3993 (setq result (list (match-string 3) (match-string 1))))
3998 (defun tramp-completion-handle-expand-file-name (name &optional dir)
3999 "Like `expand-file-name' for tramp files."
4000 (let ((fullname (concat (or dir default-directory) name)))
4001 (if (tramp-completion-mode fullname)
4002 (tramp-run-real-handler
4003 'expand-file-name (list name dir))
4004 (tramp-completion-run-real-handler
4005 'expand-file-name (list name dir)))))
4007 ;;; Internal Functions:
4009 (defun tramp-set-auto-save ()
4010 (when (and (buffer-file-name)
4011 (tramp-tramp-file-p (buffer-file-name))
4013 (auto-save-mode 1)))
4014 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4016 (defun tramp-run-test (switch filename)
4017 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4018 Returns the exit code of the `test' program."
4019 (let ((v (tramp-dissect-file-name filename)))
4021 (tramp-send-command-and-check
4022 (tramp-file-name-multi-method v) (tramp-file-name-method v)
4023 (tramp-file-name-user v) (tramp-file-name-host v)
4024 (format "test %s %s" switch
4025 (tramp-shell-quote-argument (tramp-file-name-path v)))))))
4027 (defun tramp-run-test2 (program file1 file2 &optional switch)
4028 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4029 The optional SWITCH is inserted between the two files.
4030 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4031 hosts, or files, disagree."
4032 (let* ((v1 (tramp-dissect-file-name file1))
4033 (v2 (tramp-dissect-file-name file2))
4034 (mmethod1 (tramp-file-name-multi-method v1))
4035 (mmethod2 (tramp-file-name-multi-method v2))
4036 (method1 (tramp-file-name-method v1))
4037 (method2 (tramp-file-name-method v2))
4038 (user1 (tramp-file-name-user v1))
4039 (user2 (tramp-file-name-user v2))
4040 (host1 (tramp-file-name-host v1))
4041 (host2 (tramp-file-name-host v2))
4042 (path1 (tramp-file-name-path v1))
4043 (path2 (tramp-file-name-path v2)))
4044 (unless (and method1 method2 host1 host2
4045 (equal mmethod1 mmethod2)
4046 (equal method1 method2)
4048 (equal host1 host2))
4049 (error "tramp-run-test2: %s"
4050 "only implemented for same method, same user, same host"))
4052 (tramp-send-command-and-check
4053 mmethod1 method1 user1 host1
4054 (format "%s %s %s %s"
4056 (tramp-shell-quote-argument path1)
4058 (tramp-shell-quote-argument path2))))))
4060 (defun tramp-buffer-name (multi-method method user host)
4061 "A name for the connection buffer for USER at HOST using METHOD."
4063 (tramp-buffer-name-multi-method "tramp" multi-method method user host))
4065 (format "*tramp/%s %s@%s*" method user host))
4067 (format "*tramp/%s %s*" method host))))
4069 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
4070 "A name for the multi method connection buffer.
4071 MULTI-METHOD gives the multi method, METHOD the array of methods,
4072 USER the array of user names, HOST the array of host names."
4073 (unless (and (= (length method) (length user))
4074 (= (length method) (length host)))
4075 (error "Syntax error in multi method (implementation error)"))
4076 (let ((len (length method))
4081 (cons (if (aref user i)
4082 (format "%s#%s@%s:" (aref method i)
4083 (aref user i) (aref host i))
4084 (format "%s@%s:" (aref method i) (aref host i)))
4087 (format "*%s/%s %s*"
4089 (apply 'concat (reverse string-list)))))
4091 (defun tramp-get-buffer (multi-method method user host)
4092 "Get the connection buffer to be used for USER at HOST using METHOD."
4093 (get-buffer-create (tramp-buffer-name multi-method method user host)))
4095 (defun tramp-debug-buffer-name (multi-method method user host)
4096 "A name for the debug buffer for USER at HOST using METHOD."
4098 (tramp-buffer-name-multi-method "debug tramp"
4099 multi-method method user host))
4101 (format "*debug tramp/%s %s@%s*" method user host))
4103 (format "*debug tramp/%s %s*" method host))))
4105 (defun tramp-get-debug-buffer (multi-method method user host)
4106 "Get the debug buffer for USER at HOST using METHOD."
4107 (get-buffer-create (tramp-debug-buffer-name multi-method method user host)))
4109 (defun tramp-find-executable (multi-method method user host
4110 progname dirlist ignore-tilde)
4111 "Searches for PROGNAME in all directories mentioned in DIRLIST.
4112 First args METHOD, USER and HOST specify the connection, PROGNAME
4113 is the program to search for, and DIRLIST gives the list of directories
4114 to search. If IGNORE-TILDE is non-nil, directory names starting
4115 with `~' will be ignored.
4117 Returns the full path name of PROGNAME, if found, and nil otherwise.
4119 This function expects to be in the right *tramp* buffer."
4122 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
4123 ;; `remove' is in CL, and we want to avoid CL dependencies.
4126 (setq d (car dirlist))
4127 (setq dirlist (cdr dirlist))
4128 (unless (char-equal ?~ (aref d 0))
4129 (setq newdl (cons d newdl))))
4130 (setq dirlist (nreverse newdl))))
4132 multi-method method user host
4133 (format (concat "while read d; "
4134 "do if test -x $d/%s -a -f $d/%s; "
4135 "then echo tramp_executable $d/%s; "
4136 "break; fi; done <<'EOF'")
4137 progname progname progname))
4139 (tramp-send-command multi-method method user host d))
4141 (tramp-send-command multi-method method user host "EOF")
4142 (tramp-wait-for-output)
4143 (goto-char (point-max))
4144 (when (search-backward "tramp_executable " nil t)
4145 (skip-chars-forward "^ ")
4146 (skip-chars-forward " ")
4147 (buffer-substring (point) (tramp-line-end-position)))))
4149 (defun tramp-set-remote-path (multi-method method user host var dirlist)
4150 "Sets the remote environment VAR to existing directories from DIRLIST.
4151 I.e., for each directory in DIRLIST, it is tested whether it exists and if
4152 so, it is added to the environment variable VAR."
4153 (let ((existing-dirs
4158 (tramp-make-tramp-file-name multi-method method user host x))
4160 (tramp-make-tramp-file-name multi-method method user host x)))
4164 multi-method method user host
4166 (mapconcat 'identity (delq nil existing-dirs) ":")
4168 (tramp-wait-for-output)))
4170 ;; -- communication with external shell --
4172 (defun tramp-find-file-exists-command (multi-method method user host)
4173 "Find a command on the remote host for checking if a file exists.
4174 Here, we are looking for a command which has zero exit status if the
4175 file exists and nonzero exit status otherwise."
4176 (make-local-variable 'tramp-file-exists-command)
4177 (tramp-message 9 "Finding command to check if file exists")
4179 (tramp-make-tramp-file-name
4180 multi-method method user host
4181 "/")) ;assume this file always exists
4183 (tramp-make-tramp-file-name
4184 multi-method method user host
4185 "/ this file does not exist "))) ;assume this never exists
4186 ;; The algorithm is as follows: we try a list of several commands.
4187 ;; For each command, we first run `$cmd /' -- this should return
4188 ;; true, as the root directory always exists. And then we run
4189 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
4190 ;; does not exist. This should return false. We use the first
4191 ;; command we find that seems to work.
4192 ;; The list of commands to try is as follows:
4193 ;; `ls -d' This works on most systems, but NetBSD 1.4
4194 ;; has a bug: `ls' always returns zero exit
4195 ;; status, even for files which don't exist.
4196 ;; `test -e' Some Bourne shells have a `test' builtin
4197 ;; which does not know the `-e' option.
4198 ;; `/bin/test -e' For those, the `test' binary on disk normally
4199 ;; provides the option. Alas, the binary
4200 ;; is sometimes `/bin/test' and sometimes it's
4202 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
4204 (and (setq tramp-file-exists-command "test -e %s")
4205 (tramp-handle-file-exists-p existing)
4206 (not (tramp-handle-file-exists-p nonexisting)))
4207 (and (setq tramp-file-exists-command "/bin/test -e %s")
4208 (tramp-handle-file-exists-p existing)
4209 (not (tramp-handle-file-exists-p nonexisting)))
4210 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
4211 (tramp-handle-file-exists-p existing)
4212 (not (tramp-handle-file-exists-p nonexisting)))
4213 (and (setq tramp-file-exists-command "ls -d %s")
4214 (tramp-handle-file-exists-p existing)
4215 (not (tramp-handle-file-exists-p nonexisting))))
4216 (error "Couldn't find command to check if file exists."))))
4219 ;; CCC test ksh or bash found for tilde expansion?
4220 (defun tramp-find-shell (multi-method method user host)
4221 "Find a shell on the remote host which groks tilde expansion."
4223 (tramp-send-command multi-method method user host "echo ~root")
4224 (tramp-wait-for-output)
4226 ((string-match "^~root$" (buffer-string))
4228 (or (tramp-find-executable multi-method method user host
4229 "bash" tramp-remote-path t)
4230 (tramp-find-executable multi-method method user host
4231 "ksh" tramp-remote-path t)))
4233 (error "Couldn't find a shell which groks tilde expansion"))
4234 ;; Find arguments for this shell.
4235 (let ((alist tramp-sh-extra-args)
4237 (while (and alist (null extra-args))
4238 (setq item (pop alist))
4239 (when (string-match (car item) shell)
4240 (setq extra-args (cdr item))))
4241 (when extra-args (setq shell (concat shell " " extra-args))))
4243 5 "Starting remote shell `%s' for tilde expansion..." shell)
4245 multi-method method user host
4246 (concat "PS1='$ ' exec " shell)) ;
4247 (unless (tramp-wait-for-regexp
4248 (get-buffer-process (current-buffer))
4249 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'"
4250 tramp-shell-prompt-pattern shell-prompt-pattern))
4251 (pop-to-buffer (buffer-name))
4252 (error "Couldn't find remote `%s' prompt." shell))
4254 9 "Setting remote shell prompt...")
4255 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
4256 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
4257 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
4259 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
4260 tramp-rsh-end-of-line
4262 tramp-rsh-end-of-line
4263 tramp-rsh-end-of-line))
4264 (tramp-wait-for-output)
4266 9 "Setting remote shell prompt...done")
4268 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
4269 (tramp-get-remote-sh multi-method method user host))))))
4271 (defun tramp-check-ls-command (multi-method method user host cmd)
4272 "Checks whether the given `ls' executable groks `-n'.
4273 METHOD, USER and HOST specify the connection, CMD (the full path name of)
4274 the `ls' executable. Returns t if CMD supports the `-n' option, nil
4276 (tramp-message 9 "Checking remote `%s' command for `-n' option"
4278 (when (tramp-handle-file-executable-p
4279 (tramp-make-tramp-file-name multi-method method user host cmd))
4281 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
4283 (tramp-send-command-and-check
4284 multi-method method user host
4285 (format "%s -lnd / >/dev/null"
4287 (tramp-message 7 "Testing remote command `%s' for -n...%s"
4289 (if (zerop result) "okay" "failed"))
4292 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
4293 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
4294 Returns nil if none was found, else the command is returned."
4297 (directory-sep-char ?/)) ;for XEmacs
4298 ;; It would be better to use the CL function `find', but
4299 ;; we don't want run-time dependencies on CL.
4300 (while (and dl (not result))
4301 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
4302 (when (tramp-check-ls-command multi-method method user host x)
4307 (defun tramp-find-ls-command (multi-method method user host)
4308 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
4309 \(This option prints numeric user and group ids in a long listing.)"
4310 (tramp-message 9 "Finding a suitable `ls' command")
4312 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
4313 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
4314 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
4316 ;; ------------------------------------------------------------
4317 ;; -- Functions for establishing connection --
4318 ;; ------------------------------------------------------------
4320 ;; The following functions are actions to be taken when seeing certain
4321 ;; prompts from the remote host. See the variable
4322 ;; `tramp-actions-before-shell' for usage of these functions.
4324 (defun tramp-action-login (p multi-method method user host)
4325 "Send the login name."
4326 (tramp-message 9 "Sending login name `%s'"
4327 (or user (user-login-name)))
4329 (process-send-string nil (concat (or user (user-login-name))
4330 tramp-rsh-end-of-line)))
4332 (defun tramp-action-password (p multi-method method user host)
4333 "Query the user for a password."
4334 (when (tramp-method-out-of-band-p multi-method method user host)
4335 (kill-process (get-buffer-process (current-buffer)))
4336 (error (concat "Out of band method `%s' not applicable "
4337 "for remote shell asking for a password")
4339 (tramp-message 9 "Sending password")
4340 (tramp-enter-password p (match-string 0)))
4342 (defun tramp-action-succeed (p multi-method method user host)
4343 "Signal success in finding shell prompt."
4344 (tramp-message 9 "Found remote shell prompt.")
4346 (throw 'tramp-action 'ok))
4348 (defun tramp-action-permission-denied (p multi-method method user host)
4349 "Signal permission denied."
4350 (pop-to-buffer (tramp-get-buffer multi-method method user host))
4351 (tramp-message 9 "Permission denied by remote host.")
4353 (throw 'tramp-action 'permission-denied))
4355 (defun tramp-action-yesno (p multi-method method user host)
4356 "Ask the user for confirmation using `yes-or-no-p'.
4357 Send \"yes\" to remote process on confirmation, abort otherwise.
4358 See also `tramp-action-yn'."
4359 (save-window-excursion
4360 (pop-to-buffer (tramp-get-buffer multi-method method user host))
4361 (unless (yes-or-no-p (match-string 0))
4364 (throw 'tramp-action 'permission-denied))
4365 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
4368 (defun tramp-action-yn (p multi-method method user host)
4369 "Ask the user for confirmation using `y-or-n-p'.
4370 Send \"y\" to remote process on confirmation, abort otherwise.
4371 See also `tramp-action-yesno'."
4372 (save-window-excursion
4373 (pop-to-buffer (tramp-get-buffer multi-method method user host))
4374 (unless (y-or-n-p (match-string 0))
4377 (throw 'tramp-action 'permission-denied))
4378 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
4380 ;; The following functions are specifically for multi connections.
4382 (defun tramp-multi-action-login (p method user host)
4383 "Send the login name."
4384 (tramp-message 9 "Sending login name `%s'" user)
4386 (process-send-string p (concat user tramp-rsh-end-of-line)))
4388 (defun tramp-multi-action-password (p method user host)
4389 "Query the user for a password."
4390 (tramp-message 9 "Sending password")
4391 (tramp-enter-password p (match-string 0)))
4393 (defun tramp-multi-action-succeed (p method user host)
4394 "Signal success in finding shell prompt."
4395 (tramp-message 9 "Found shell prompt on `%s'" host)
4397 (throw 'tramp-action 'ok))
4399 (defun tramp-multi-action-permission-denied (p method user host)
4400 "Signal permission denied."
4401 (tramp-message 9 "Permission denied by remote host `%s'" host)
4404 (throw 'tramp-action 'permission-denied))
4406 ;; Functions for processing the actions.
4408 (defun tramp-process-one-action (p multi-method method user host actions)
4409 "Wait for output from the shell and perform one action."
4410 (let (found item pattern action todo)
4412 (tramp-message 9 "Waiting 60s for prompt from remote shell")
4413 (with-timeout (60 (throw 'tramp-action 'timeout))
4415 (accept-process-output p 1)
4416 (goto-char (point-min))
4419 (goto-char (point-min))
4420 (setq item (pop todo))
4421 (setq pattern (symbol-value (nth 0 item)))
4422 (setq action (nth 1 item))
4423 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
4425 (when (re-search-forward (concat pattern "\\'") nil t)
4426 (setq found (funcall action p multi-method method user host)))))
4429 (defun tramp-process-actions (p multi-method method user host actions)
4430 "Perform actions until success."
4433 (tramp-message 9 "Waiting for prompts from remote shell")
4435 (catch 'tramp-action
4436 (tramp-process-one-action
4437 p multi-method method user host actions)
4439 (unless (eq exit 'ok)
4440 (error "Login failed"))))
4442 ;; For multi-actions.
4444 (defun tramp-process-one-multi-action (p method user host actions)
4445 "Wait for output from the shell and perform one action."
4446 (let (found item pattern action todo)
4448 (tramp-message 9 "Waiting 60s for prompt from remote shell")
4449 (with-timeout (60 (throw 'tramp-action 'timeout))
4451 (accept-process-output p 1)
4453 (goto-char (point-min))
4455 (goto-char (point-min))
4456 (setq item (pop todo))
4457 (setq pattern (symbol-value (nth 0 item)))
4458 (setq action (nth 1 item))
4459 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
4461 (when (re-search-forward (concat pattern "\\'") nil t)
4462 (setq found (funcall action p method user host)))))
4465 (defun tramp-process-multi-actions (p method user host actions)
4466 "Perform actions until success."
4469 (tramp-message 9 "Waiting for prompts from remote shell")
4471 (catch 'tramp-action
4472 (tramp-process-one-multi-action p method user host actions)
4474 (unless (eq exit 'ok)
4475 (error "Login failed"))))
4477 ;; The actual functions for opening connections.
4479 (defun tramp-open-connection-telnet (multi-method method user host)
4480 "Open a connection using a telnet METHOD.
4481 This starts the command `telnet HOST ARGS'[*], then waits for a remote
4482 login prompt, then sends the user name USER, then waits for a remote
4483 password prompt. It queries the user for the password, then sends the
4484 password to the remote host.
4486 If USER is nil, uses value returned by `(user-login-name)' instead.
4488 Recognition of the remote shell prompt is based on the variables
4489 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4492 Please note that it is NOT possible to use this connection method
4493 together with an out-of-band transfer method! You must use an inline
4496 Maybe the different regular expressions need to be tuned.
4498 * Actually, the telnet program as well as the args to be used can be
4499 specified in the method parameters, see the variable `tramp-methods'."
4501 (when (tramp-method-out-of-band-p multi-method method user host)
4502 (error "Cannot use out-of-band method `%s' with telnet connection method"
4505 (error "Cannot multi-connect using telnet connection method"))
4506 (tramp-pre-connection multi-method method user host)
4507 (tramp-message 7 "Opening connection for %s@%s using %s..."
4508 (or user (user-login-name)) host method)
4509 (let ((process-environment (copy-sequence process-environment)))
4510 (setenv "TERM" tramp-terminal-type)
4511 (let* ((default-directory (tramp-temporary-file-directory))
4512 ;; If we omit the conditional here, then we would use
4513 ;; `undecided-dos' in some cases. With the conditional,
4514 ;; we use nil in these cases. Which one is right?
4515 (coding-system-for-read (unless (and (not (featurep 'xemacs))
4516 (> emacs-major-version 20))
4517 tramp-dos-coding-system))
4518 (p (apply 'start-process
4519 (tramp-buffer-name multi-method method user host)
4520 (tramp-get-buffer multi-method method user host)
4521 (tramp-get-telnet-program
4523 (tramp-find-method multi-method method user host)
4526 (tramp-get-telnet-args
4528 (tramp-find-method multi-method method user host)
4532 (process-kill-without-query p)
4533 (set-buffer (tramp-get-buffer multi-method method user host))
4535 (tramp-process-actions p multi-method method user host
4536 tramp-actions-before-shell)
4537 (tramp-open-connection-setup-interactive-shell
4538 p multi-method method user host)
4539 (tramp-post-connection multi-method method user host)))))
4542 (defun tramp-open-connection-rsh (multi-method method user host)
4543 "Open a connection using an rsh METHOD.
4544 This starts the command `rsh HOST -l USER'[*], then waits for a remote
4545 password or shell prompt. If a password prompt is seen, the user is
4546 queried for a password, this function sends the password to the remote
4547 host and waits for a shell prompt.
4549 If USER is nil, start the command `rsh HOST'[*] instead
4551 Recognition of the remote shell prompt is based on the variables
4552 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4555 Please note that it is NOT possible to use this connection method with
4556 an out-of-band transfer method if this function asks the user for a
4557 password! You must use an inline transfer method in this case.
4558 Sadly, the transfer method cannot be switched on the fly, instead you
4559 must specify the right method in the file name.
4561 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
4562 be a port number for ssh, and \"-p yy\" will be added to the list of
4563 arguments, and xx will be used as the host name to connect to.
4565 * Actually, the rsh program to be used can be specified in the
4566 method parameters, see the variable `tramp-methods'."
4569 (error "Cannot multi-connect using rsh connection method"))
4570 (tramp-pre-connection multi-method method user host)
4571 (if (and user (not (string= user "")))
4572 (tramp-message 7 "Opening connection for %s@%s using %s..."
4574 (tramp-message 7 "Opening connection at %s using %s..." host method))
4575 (let ((process-environment (copy-sequence process-environment))
4576 (bufnam (tramp-buffer-name multi-method method user host))
4577 (buf (tramp-get-buffer multi-method method user host))
4578 (rsh-program (tramp-get-rsh-program
4580 (tramp-find-method multi-method method user host)
4582 (rsh-args (tramp-get-rsh-args
4584 (tramp-find-method multi-method method user host)
4586 ;; The following should be changed. We need a more general
4587 ;; mechanism to parse extra host args.
4588 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
4589 (setq rsh-args (cons "-p" (cons (match-string 2 host) rsh-args)))
4590 (setq host (match-string 1 host)))
4591 (setenv "TERM" tramp-terminal-type)
4592 (let* ((default-directory (tramp-temporary-file-directory))
4593 ;; If we omit the conditional, we would use
4594 ;; `undecided-dos' in some cases. With the conditional,
4595 ;; we use nil in these cases. Which one is right?
4596 (coding-system-for-read (unless (and (not (featurep 'xemacs))
4597 (> emacs-major-version 20))
4598 tramp-dos-coding-system))
4599 (p (if (and user (not (string= user "")))
4600 (apply #'start-process bufnam buf rsh-program
4601 host "-l" user rsh-args)
4602 (apply #'start-process bufnam buf rsh-program
4605 (process-kill-without-query p)
4608 (tramp-process-actions p multi-method method user host
4609 tramp-actions-before-shell)
4610 (tramp-message 7 "Initializing remote shell")
4611 (tramp-open-connection-setup-interactive-shell
4612 p multi-method method user host)
4613 (tramp-post-connection multi-method method user host)))))
4615 (defun tramp-open-connection-su (multi-method method user host)
4616 "Open a connection using the `su' program with METHOD.
4617 This starts `su - USER', then waits for a password prompt. The HOST
4618 name must be equal to the local host name or to `localhost'.
4620 If USER is nil, uses value returned by user-login-name instead.
4622 Recognition of the remote shell prompt is based on the variables
4623 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4624 set up correctly. Note that the other user may have a different shell
4625 prompt than you do, so it is not at all unlikely that the variable
4626 `shell-prompt-pattern' is set up wrongly!"
4628 (when (tramp-method-out-of-band-p multi-method method user host)
4629 (error "Cannot use out-of-band method `%s' with `su' connection method"
4631 (unless (or (string-match (concat "^" (regexp-quote host))
4633 (string= "localhost" host)
4636 "Cannot connect to different host `%s' with `su' connection method"
4638 (tramp-pre-connection multi-method method user host)
4639 (tramp-message 7 "Opening connection for `%s' using `%s'..."
4640 (or user "<root>") method)
4641 (let ((process-environment (copy-sequence process-environment)))
4642 (setenv "TERM" tramp-terminal-type)
4643 (let* ((default-directory (tramp-temporary-file-directory))
4644 ;; If we omit the conditional, we use `undecided-dos' in
4645 ;; some cases. With the conditional, we use nil in these
4646 ;; cases. What's the difference? Which one is right?
4647 (coding-system-for-read (unless (and (not (featurep 'xemacs))
4648 (> emacs-major-version 20))
4649 tramp-dos-coding-system))
4650 (p (apply 'start-process
4651 (tramp-buffer-name multi-method method user host)
4652 (tramp-get-buffer multi-method method user host)
4653 (tramp-get-su-program
4655 (tramp-find-method multi-method method user host)
4659 (format-spec x `((?u . ,(or user "root")))))
4662 (tramp-find-method multi-method method user host)
4666 (process-kill-without-query p)
4667 (set-buffer (tramp-get-buffer multi-method method user host))
4668 (tramp-process-actions p multi-method method user host
4669 tramp-actions-before-shell)
4670 (tramp-open-connection-setup-interactive-shell
4671 p multi-method method user host)
4672 (tramp-post-connection multi-method method
4675 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
4676 ;; handle not giving a user name in the "file name".
4678 ;; This is more difficult than for the single-hop method. In the
4679 ;; multi-hop-method, the desired behaviour should be that the
4680 ;; user must specify names for the telnet hops of which the user
4681 ;; name is different than the "original" name (or different from
4682 ;; the previous hop.
4683 (defun tramp-open-connection-multi (multi-method method user host)
4684 "Open a multi-hop connection using METHOD.
4685 This uses a slightly changed file name syntax. The idea is to say
4686 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
4687 This will use telnet to log in as u1 to h1, then use rsh from there to
4688 log in as u2 to h2."
4690 (unless multi-method
4691 (error "Multi-hop open connection function called on non-multi method"))
4692 (when (tramp-method-out-of-band-p multi-method method user host)
4693 (error "No out of band multi-hop connections"))
4694 (unless (and (arrayp method) (not (stringp method)))
4695 (error "METHOD must be an array of strings for multi methods"))
4696 (unless (and (arrayp user) (not (stringp user)))
4697 (error "USER must be an array of strings for multi methods"))
4698 (unless (and (arrayp host) (not (stringp host)))
4699 (error "HOST must be an array of strings for multi methods"))
4700 (unless (and (= (length method) (length user))
4701 (= (length method) (length host)))
4702 (error "Arrays METHOD, USER, HOST must have equal length"))
4703 (tramp-pre-connection multi-method method user host)
4704 (tramp-message 7 "Opening `%s' connection..." multi-method)
4705 (let ((process-environment (copy-sequence process-environment)))
4706 (setenv "TERM" tramp-terminal-type)
4707 (let* ((default-directory (tramp-temporary-file-directory))
4708 ;; If we omit the conditional, we use `undecided-dos' in
4709 ;; some cases. With the conditional, we use nil in these
4710 ;; cases. What's the difference? Which one is right?
4711 (coding-system-for-read (unless (and (not (featurep 'xemacs))
4712 (> emacs-major-version 20))
4713 tramp-dos-coding-system))
4714 (p (start-process (tramp-buffer-name multi-method method user host)
4715 (tramp-get-buffer multi-method method user host)
4716 tramp-multi-sh-program))
4717 (num-hops (length method))
4719 (process-kill-without-query p)
4720 (tramp-message 9 "Waiting 60s for local shell to come up...")
4721 (unless (tramp-wait-for-regexp
4722 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
4723 shell-prompt-pattern tramp-shell-prompt-pattern))
4724 (pop-to-buffer (buffer-name))
4726 (error "Couldn't find local shell prompt"))
4727 ;; Now do all the connections as specified.
4728 (while (< i num-hops)
4729 (let* ((m (aref method i))
4732 (entry (assoc m tramp-multi-connection-function-alist))
4733 (multi-func (nth 1 entry))
4734 (command (nth 2 entry)))
4735 ;; The multi-funcs don't need to do save-match-data, as that
4737 (funcall multi-func p m u h command)
4740 (tramp-open-connection-setup-interactive-shell
4741 p multi-method method user host)
4742 (tramp-post-connection multi-method method user host)))))
4744 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
4745 ;; of no user name provided. Hack to make it work as it did before:
4746 ;; changed `user' to `(or user (user-login-name))' in the places where
4747 ;; the value is actually used.
4748 (defun tramp-multi-connect-telnet (p method user host command)
4749 "Issue `telnet' command.
4750 Uses shell COMMAND to issue a `telnet' command to log in as USER to
4751 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
4752 the host name, and `%n' is replaced with an end of line character, as
4753 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
4756 If USER is nil, uses the return value of (user-login-name) instead."
4757 (let ((cmd (format-spec command
4758 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
4759 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
4762 (tramp-message 9 "Sending telnet command `%s'" cmd1)
4763 (process-send-string p cmd)
4764 (tramp-process-multi-actions p method user host
4765 tramp-multi-actions)))
4767 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
4768 ;; of no user name provided. Hack to make it work as it did before:
4769 ;; changed `user' to `(or user (user-login-name))' in the places where
4770 ;; the value is actually used.
4771 (defun tramp-multi-connect-rlogin (p method user host command)
4772 "Issue `rlogin' command.
4773 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
4774 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
4775 with the user name, `%h' will be replaced with the host name, and `%n'
4776 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
4777 `%%' if you want to use a literal percent character.
4779 If USER is nil, uses the return value of (user-login-name) instead."
4780 (let ((cmd (format-spec command `((?h . ,host)
4781 (?u . ,(or user (user-login-name)))
4782 (?n . ,tramp-rsh-end-of-line))))
4783 (cmd1 (format-spec command `((?h . ,host)
4784 (?u . ,(or user (user-login-name)))
4788 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
4789 (process-send-string p cmd)
4790 (tramp-process-multi-actions p method user host
4791 tramp-multi-actions)))
4793 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
4794 ;; of no user name provided. Hack to make it work as it did before:
4795 ;; changed `user' to `(or user (user-login-name))' in the places where
4796 ;; the value is actually used.
4797 (defun tramp-multi-connect-su (p method user host command)
4798 "Issue `su' command.
4799 Uses shell COMMAND to issue a `su' command to log in as USER on
4800 HOST. The HOST name is ignored, this just changes the user id on the
4801 host currently logged in to.
4803 If USER is nil, uses the return value of (user-login-name) instead.
4805 You can use percent escapes in the COMMAND. `%u' is replaced with the
4806 user name, and `%n' is replaced with the value of
4807 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
4809 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
4810 (?n . ,tramp-rsh-end-of-line))))
4811 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
4815 (tramp-message 9 "Sending su command `%s'" cmd1)
4816 (process-send-string p cmd)
4817 (tramp-process-multi-actions p method user host
4818 tramp-multi-actions)))
4820 ;; Utility functions.
4822 (defun tramp-wait-for-regexp (proc timeout regexp)
4823 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
4824 Expects the output of PROC to be sent to the current buffer. Returns
4825 the string that matched, or nil. Waits indefinitely if TIMEOUT is
4828 (start-time (current-time)))
4830 ;; Work around a bug in XEmacs 21, where the timeout
4831 ;; expires faster than it should. This degenerates
4832 ;; to polling for buggy XEmacsen, but oh, well.
4833 (while (and (not found)
4834 (< (tramp-time-diff (current-time) start-time)
4836 (with-timeout (timeout)
4838 (accept-process-output proc 1)
4839 (goto-char (point-min))
4840 (setq found (when (re-search-forward regexp nil t)
4841 (tramp-match-string-list)))))))
4844 (accept-process-output proc 1)
4845 (goto-char (point-min))
4846 (setq found (when (re-search-forward regexp nil t)
4847 (tramp-match-string-list))))))
4848 (when tramp-debug-buffer
4850 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
4851 tramp-current-user tramp-current-host)
4852 (point-min) (point-max))
4856 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
4857 tramp-current-user tramp-current-host))
4858 (goto-char (point-max))
4859 (insert "[[Regexp `" regexp "' not found"
4860 (if timeout (format " in %d secs" timeout) "")
4864 (defun tramp-enter-password (p prompt)
4865 "Prompt for a password and send it to the remote end.
4866 Uses PROMPT as a prompt and sends the password to process P."
4867 (let ((pw (tramp-read-passwd prompt)))
4869 (process-send-string p (concat pw tramp-password-end-of-line))))
4871 ;; HHH: Not Changed. This might handle the case where USER is not
4872 ;; given in the "File name" very poorly. Then, the local
4873 ;; variable tramp-current user will be set to nil.
4874 (defun tramp-pre-connection (multi-method method user host)
4875 "Do some setup before actually logging in.
4876 METHOD, USER and HOST specify the connection."
4877 (set-buffer (tramp-get-buffer multi-method method user host))
4878 (set (make-local-variable 'tramp-current-multi-method) multi-method)
4879 (set (make-local-variable 'tramp-current-method) method)
4880 (set (make-local-variable 'tramp-current-user) user)
4881 (set (make-local-variable 'tramp-current-host) host)
4882 (set (make-local-variable 'inhibit-eol-conversion) nil)
4885 (defun tramp-open-connection-setup-interactive-shell
4886 (p multi-method method user host)
4887 "Set up an interactive shell.
4888 Mainly sets the prompt and the echo correctly. P is the shell process
4889 to set up. METHOD, USER and HOST specify the connection."
4890 ;; Wait a bit in case the remote end feels like sending a little
4891 ;; junk first. It seems that fencepost.gnu.org does this when doing
4892 ;; a Kerberos login.
4894 (tramp-discard-garbage-erase-buffer p multi-method method user host)
4895 ;; It is useful to set the prompt in the following command because
4896 ;; some people have a setting for $PS1 which /bin/sh doesn't know
4897 ;; about and thus /bin/sh will display a strange prompt. For
4898 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
4899 ;; the current working directory but /bin/sh will display a dollar
4900 ;; sign. The following command line sets $PS1 to a sane value, and
4901 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
4902 ;; Pittman reports that the unusual positioning of the single quotes
4903 ;; makes it work under `rc', too.
4904 (process-send-string nil (format "exec env 'PS1=$ ' %s%s"
4905 (tramp-get-remote-sh
4906 multi-method method user host)
4907 tramp-rsh-end-of-line))
4908 (when tramp-debug-buffer
4910 (set-buffer (tramp-get-debug-buffer multi-method method user host))
4911 (goto-char (point-max))
4912 (tramp-insert-with-face
4913 'bold (format "$ exec env PS1='$ ' %s\n"
4914 (tramp-get-remote-sh multi-method method user host)))))
4915 (tramp-message 9 "Waiting 30s for remote `%s' to come up..."
4916 (tramp-get-remote-sh multi-method method user host))
4917 (unless (tramp-wait-for-regexp
4918 p 30 (format "\\(%s\\|%s\\)\\'"
4919 shell-prompt-pattern tramp-shell-prompt-pattern))
4920 (pop-to-buffer (buffer-name))
4921 (error "Remote `%s' didn't come up. See buffer `%s' for details"
4922 (tramp-get-remote-sh multi-method method user host)
4924 (tramp-message 9 "Setting up remote shell environment")
4925 (tramp-discard-garbage-erase-buffer p multi-method method user host)
4926 (process-send-string
4927 nil (format "stty -inlcr -echo kill '^U'%s" tramp-rsh-end-of-line))
4928 (unless (tramp-wait-for-regexp
4929 p 30 (format "\\(%s\\|%s\\)\\'"
4930 shell-prompt-pattern tramp-shell-prompt-pattern))
4931 (pop-to-buffer (buffer-name))
4932 (error "Couldn't `stty -echo', see buffer `%s'" (buffer-name)))
4934 (process-send-string nil (format "TERM=dumb; export TERM%s"
4935 tramp-rsh-end-of-line))
4936 (unless (tramp-wait-for-regexp
4937 p 30 (format "\\(%s\\|%s\\)\\'"
4938 shell-prompt-pattern tramp-shell-prompt-pattern))
4939 (pop-to-buffer (buffer-name))
4940 (error "Couldn't `TERM=dumb; export TERM', see buffer `%s'" (buffer-name)))
4941 ;; Try to set up the coding system correctly.
4942 ;; CCC this can't be the right way to do it. Hm.
4945 (tramp-message 9 "Determining coding system")
4946 (process-send-string nil (format "echo foo ; echo bar %s"
4947 tramp-rsh-end-of-line))
4948 (unless (tramp-wait-for-regexp
4949 p 30 (format "\\(%s\\|%s\\)\\'"
4950 shell-prompt-pattern tramp-shell-prompt-pattern))
4951 (pop-to-buffer (buffer-name))
4952 (error "Couldn't `echo foo; echo bar' to determine line endings'"))
4953 (goto-char (point-min))
4954 (if (featurep 'mule)
4955 ;; Use MULE to select the right EOL convention for communicating
4956 ;; with the process.
4957 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
4958 cs-decode cs-encode)
4959 (when (symbolp cs) (setq cs (cons cs cs)))
4960 (setq cs-decode (car cs))
4961 (setq cs-encode (cdr cs))
4962 (unless cs-decode (setq cs-decode 'undecided))
4963 (unless cs-encode (setq cs-encode 'undecided))
4964 (setq cs-encode (tramp-coding-system-change-eol-conversion
4966 (when (search-forward "\r" nil t)
4967 (setq cs-decode (tramp-coding-system-change-eol-conversion
4969 (set-buffer-process-coding-system cs-decode cs-encode))
4970 ;; Look for ^M and do something useful if found.
4971 (when (search-forward "\r" nil t)
4972 ;; We have found a ^M but cannot frob the process coding system
4973 ;; because we're running on a non-MULE Emacs. Let's try
4975 (tramp-message 9 "Trying `stty -onlcr'")
4976 (process-send-string nil (format "stty -onlcr%s" tramp-rsh-end-of-line))
4977 (unless (tramp-wait-for-regexp
4978 p 30 (format "\\(%s\\|%s\\)\\'"
4979 shell-prompt-pattern tramp-shell-prompt-pattern))
4980 (pop-to-buffer (buffer-name))
4981 (error "Couldn't `stty -onlcr', see buffer `%s'" (buffer-name))))))
4984 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1'")
4985 (process-send-string
4986 nil (format "HISTFILE=$HOME/.tramp_history; HISTSIZE=1%s"
4987 tramp-rsh-end-of-line))
4988 (unless (tramp-wait-for-regexp
4989 p 30 (format "\\(%s\\|%s\\)\\'"
4990 shell-prompt-pattern tramp-shell-prompt-pattern))
4991 (pop-to-buffer (buffer-name))
4992 (error (concat "Couldn't `HISTFILE=$HOME/.tramp_history; "
4993 "HISTSIZE=1', see buffer `%s'")
4996 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
4997 (process-send-string
4998 nil (format "set +o vi +o emacs%s" ;mustn't `>/dev/null' with AIX?
4999 tramp-rsh-end-of-line))
5000 (unless (tramp-wait-for-regexp
5001 p 30 (format "\\(%s\\|%s\\)\\'"
5002 shell-prompt-pattern tramp-shell-prompt-pattern))
5003 (pop-to-buffer (buffer-name))
5004 (error "Couldn't `set +o vi +o emacs', see buffer `%s'"
5007 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
5008 (process-send-string
5009 nil (format "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null%s"
5010 tramp-rsh-end-of-line))
5011 (unless (tramp-wait-for-regexp
5012 p 30 (format "\\(%s\\|%s\\)\\'"
5013 shell-prompt-pattern tramp-shell-prompt-pattern))
5014 (pop-to-buffer (buffer-name))
5015 (error "Couldn't `unset MAIL MAILCHECK MAILPATH', see buffer `%s'"
5018 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
5019 (process-send-string
5020 nil (format "unset CDPATH%s" tramp-rsh-end-of-line))
5021 (unless (tramp-wait-for-regexp
5022 p 30 (format "\\(%s\\|%s\\)\\'"
5023 shell-prompt-pattern tramp-shell-prompt-pattern))
5024 (pop-to-buffer (buffer-name))
5025 (error "Couldn't `unset CDPATH', see buffer `%s'"
5028 (tramp-message 9 "Setting shell prompt")
5029 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
5030 ;; use "\n" here, not tramp-rsh-end-of-line.
5032 multi-method method user host
5033 (format "PS1='%s%s%s'; PS2=''; PS3=''"
5034 tramp-rsh-end-of-line
5036 tramp-rsh-end-of-line))
5037 (tramp-wait-for-output))
5039 (defun tramp-post-connection (multi-method method user host)
5040 "Prepare a remote shell before being able to work on it.
5041 METHOD, USER and HOST specify the connection.
5042 Among other things, this finds a shell which groks tilde expansion,
5043 tries to find an `ls' command which groks the `-n' option, sets the
5044 locale to C and sets up the remote shell search path."
5045 ;; Search for a good shell before searching for a command which
5046 ;; checks if a file exists. This is done because Tramp wants to use
5047 ;; "test foo; echo $?" to check if various conditions hold, and
5048 ;; there are buggy /bin/sh implementations which don't execute the
5049 ;; "echo $?" part if the "test" part has an error. In particular,
5050 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
5051 ;; with buggy /bin/sh implementations will have a working bash or
5053 (tramp-find-shell multi-method method user host)
5054 ;; Without (sit-for 0.1) at least, my machine will almost always blow
5055 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
5056 ;; output of (tramp-find-shell) to show up along with the output of
5057 ;; (tramp-find-ls-command) testing.
5059 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
5060 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
5062 ;; After much debugging I couldn't find any problem with the implementation
5063 ;; of that function though. The workaround stays for me at least. :/
5065 ;; Daniel Pittman <daniel@danann.net>
5068 (tramp-find-file-exists-command multi-method method user host)
5069 (make-local-variable 'tramp-ls-command)
5070 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
5071 (unless tramp-ls-command
5074 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
5075 (setq tramp-ls-command
5076 (tramp-find-executable multi-method method user host
5077 "ls" tramp-remote-path nil)))
5078 (unless tramp-ls-command
5079 (error "Fatal error: Couldn't find remote executable `ls'"))
5080 (tramp-message 5 "Using remote command `%s' for getting directory listings"
5082 (tramp-send-command multi-method method user host
5083 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
5084 "return $1" tramp-rsh-end-of-line
5086 (tramp-wait-for-output)
5087 ;; Set remote PATH variable.
5088 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
5089 ;; Tell remote shell to use standard time format, needed for
5090 ;; parsing `ls -l' output.
5091 (tramp-send-command multi-method method user host
5092 "LC_TIME=C; export LC_TIME; echo huhu")
5093 (tramp-wait-for-output)
5094 (tramp-send-command multi-method method user host
5095 "mesg n; echo huhu")
5096 (tramp-wait-for-output)
5097 (tramp-send-command multi-method method user host
5098 "biff n ; echo huhu")
5099 (tramp-wait-for-output)
5100 ;; Unalias ls(1) to work around issues with those silly people who make it
5101 ;; spit out ANSI escapes or whatever.
5102 (tramp-send-command multi-method method user host
5103 "unalias ls; echo huhu")
5104 (tramp-wait-for-output)
5105 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5106 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5107 ;; for otherwise the shell crashes.
5109 (make-local-variable 'tramp-test-groks-nt)
5110 (tramp-send-command multi-method method user host
5112 (tramp-wait-for-output)
5113 (goto-char (point-min))
5114 (setq tramp-test-groks-nt
5115 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
5116 (unless tramp-test-groks-nt
5118 multi-method method user host
5119 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
5120 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
5122 (tramp-wait-for-output)
5123 ;; Send the fallback `uudecode' script.
5125 (tramp-send-linewise multi-method method user host tramp-uudecode)
5126 (tramp-wait-for-output)
5129 (let ((tramp-remote-perl
5130 (or (tramp-find-executable multi-method method user host
5131 "perl5" tramp-remote-path nil)
5132 (tramp-find-executable multi-method method user host
5133 "perl" tramp-remote-path nil))))
5134 (when tramp-remote-perl
5135 (tramp-set-connection-property "perl" tramp-remote-perl
5136 multi-method method user host)
5137 ;; Set up stat in Perl if we can.
5138 (when tramp-remote-perl
5139 (tramp-message 5 "Sending the Perl `file-attributes' implementation.")
5140 (tramp-send-linewise
5141 multi-method method user host
5142 (concat "tramp_file_attributes () {\n"
5144 " -e '" tramp-perl-file-attributes "' $1 2>/dev/null\n"
5146 (tramp-wait-for-output)
5147 (unless (tramp-get-rcp-program
5149 (tramp-find-method multi-method method user host)
5151 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
5152 (tramp-send-linewise
5153 multi-method method user host
5154 (concat "tramp_encode () {\n"
5155 (format tramp-perl-encode tramp-remote-perl)
5158 (tramp-wait-for-output)
5159 (tramp-send-linewise
5160 multi-method method user host
5161 (concat "tramp_encode_with_module () {\n"
5162 (format tramp-perl-encode-with-module tramp-remote-perl)
5165 (tramp-wait-for-output)
5166 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
5167 (tramp-send-linewise
5168 multi-method method user host
5169 (concat "tramp_decode () {\n"
5170 (format tramp-perl-decode tramp-remote-perl)
5173 (tramp-wait-for-output)
5174 (tramp-send-linewise
5175 multi-method method user host
5176 (concat "tramp_decode_with_module () {\n"
5177 (format tramp-perl-decode-with-module tramp-remote-perl)
5180 (tramp-wait-for-output)))))
5183 (let ((ln (tramp-find-executable multi-method method user host
5184 "ln" tramp-remote-path nil)))
5186 (tramp-set-connection-property "ln" ln multi-method method user host)))
5188 ;; Find the right encoding/decoding commands to use.
5189 (unless (tramp-get-rcp-program
5191 (tramp-find-method multi-method method user host)
5193 (tramp-find-inline-encoding multi-method method user host))
5194 ;; If encoding/decoding command are given, test to see if they work.
5195 ;; CCC: Maybe it would be useful to run the encoder both locally and
5196 ;; remotely to see if they produce the same result.
5197 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
5198 (rem-dec (tramp-get-remote-decoding multi-method method user host))
5199 (magic-string "xyzzy"))
5200 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
5201 (tramp-kill-process multi-method method user host)
5202 ;; Improve error message and/or error check.
5204 "Must give both decoding and encoding command in method definition"))
5205 (when (and rem-enc rem-dec)
5208 "Checking to see if encoding/decoding commands work on remote host...")
5210 multi-method method user host
5211 (format "echo %s | %s | %s"
5212 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
5213 (tramp-wait-for-output)
5214 (unless (looking-at (regexp-quote magic-string))
5215 (tramp-kill-process multi-method method user host)
5216 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
5220 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
5222 ;; CCC: We should either implement a Perl version of base64 encoding
5223 ;; and decoding. Then we just use that in the last item. The other
5224 ;; alternative is to use the Perl version of UU encoding. But then
5225 ;; we need a Lisp version of uuencode.
5227 ;; Old text from documentation of tramp-methods:
5228 ;; Using a uuencode/uudecode inline method is discouraged, please use one
5229 ;; of the base64 methods instead since base64 encoding is much more
5230 ;; reliable and the commands are more standardized between the different
5231 ;; Unix versions. But if you can't use base64 for some reason, please
5232 ;; note that the default uudecode command does not work well for some
5233 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
5234 ;; the following command for uudecode:
5236 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
5238 ;; For Irix, no solution is known yet.
5240 (defvar tramp-coding-commands
5241 '(("mimencode -b" "mimencode -u -b"
5242 base64-encode-region base64-decode-region)
5243 ("mmencode -b" "mmencode -u -b"
5244 base64-encode-region base64-decode-region)
5245 ("recode data..base64" "recode base64..data"
5246 base64-encode-region base64-decode-region)
5247 ("uuencode xxx" "uudecode -o -"
5248 tramp-uuencode-region uudecode-decode-region)
5249 ("uuencode xxx" "uudecode -p"
5250 tramp-uuencode-region uudecode-decode-region)
5251 ("uuencode xxx" "tramp_uudecode"
5252 tramp-uuencode-region uudecode-decode-region)
5253 ("tramp_encode_with_module" "tramp_decode_with_module"
5254 base64-encode-region base64-decode-region)
5255 ("tramp_encode" "tramp_decode"
5256 base64-encode-region base64-decode-region))
5257 "List of coding commands for inline transfer.
5258 Each item is a list that looks like this:
5260 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
5262 The REMOTE-ENCODING should be a string, giving a command accepting a
5263 plain file on standard input and writing the encoded file to standard
5264 output. The REMOTE-DECODING should also be a string, giving a command
5265 accepting an encoded file on standard input and writing the decoded
5266 file to standard output.
5268 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
5269 symbols, giving functions. If they are strings, then they can contain
5270 the \"%s\" format specifier. If that specifier is present, the input
5271 filename will be put into the command line at that spot. If the
5272 specifier is not present, the input should be read from standard
5275 If they are functions, they will be called with two arguments, start
5276 and end of region, and are expected to replace the region contents
5277 with the encoded or decoded results, respectively.")
5279 (defun tramp-find-inline-encoding (multi-method method user host)
5280 "Find an inline transfer encoding that works.
5281 Goes through the list `tramp-coding-commands'."
5282 (let ((commands tramp-coding-commands)
5284 (while (and commands (null found))
5285 (setq item (pop commands))
5287 (let ((rem-enc (nth 0 item))
5288 (rem-dec (nth 1 item))
5289 (loc-enc (nth 2 item))
5290 (loc-dec (nth 3 item)))
5291 ;; Check if remote encoding and decoding commands can be
5292 ;; called remotely with null input and output. This makes
5293 ;; sure there are no syntax errors and the command is really
5295 (tramp-message-for-buffer
5296 multi-method method user host 9
5297 "Checking remote encoding command `%s' for sanity" rem-enc)
5298 (unless (zerop (tramp-send-command-and-check
5299 multi-method method user host
5300 (format "%s </dev/null >/dev/null" rem-enc) t))
5301 (throw 'wont-work nil))
5302 (tramp-message-for-buffer
5303 multi-method method user host 9
5304 "Checking remote decoding command `%s' for sanity" rem-dec)
5305 (unless (zerop (tramp-send-command-and-check
5306 multi-method method user host
5307 (format "echo xyzzy | %s | %s >/dev/null"
5308 rem-enc rem-dec) t))
5309 (throw 'wont-work nil))
5310 ;; If the local encoder or decoder is a string, the
5311 ;; corresponding command has to work locally.
5312 (when (stringp loc-enc)
5313 (tramp-message-for-buffer
5314 multi-method method user host 9
5315 "Checking local encoding command `%s' for sanity" loc-enc)
5316 (unless (zerop (tramp-call-local-coding-command
5318 (throw 'wont-work nil)))
5319 (when (stringp loc-dec)
5320 (tramp-message-for-buffer
5321 multi-method method user host 9
5322 "Checking local decoding command `%s' for sanity" loc-dec)
5323 (unless (zerop (tramp-call-local-coding-command
5325 (throw 'wont-work nil)))
5326 ;; CCC: At this point, maybe we should check that the output
5327 ;; of the commands is correct. But for the moment we will
5328 ;; assume that commands working on empty input will also
5329 ;; work in practice.
5330 (setq found item))))
5331 ;; Did we find something? If not, issue error. If so,
5332 ;; set connection properties.
5334 (error "Couldn't find an inline transfer encoding"))
5335 (let ((rem-enc (nth 0 found))
5336 (rem-dec (nth 1 found))
5337 (loc-enc (nth 2 found))
5338 (loc-dec (nth 3 found)))
5339 (tramp-message 10 "Using remote encoding %s" rem-enc)
5340 (tramp-set-remote-encoding multi-method method user host rem-enc)
5341 (tramp-message 10 "Using remote decoding %s" rem-dec)
5342 (tramp-set-remote-decoding multi-method method user host rem-dec)
5343 (tramp-message 10 "Using local encoding %s" loc-enc)
5344 (tramp-set-local-encoding multi-method method user host loc-enc)
5345 (tramp-message 10 "Using local decoding %s" loc-dec)
5346 (tramp-set-local-decoding multi-method method user host loc-dec))))
5348 (defun tramp-call-local-coding-command (cmd input output)
5349 "Call the local encoding or decoding command.
5350 If CMD contains \"%s\", provide input file INPUT there in command.
5351 Otherwise, INPUT is passed via standard input.
5352 INPUT can also be nil which means `/dev/null'.
5353 OUTPUT can be a string (which specifies a filename), or t (which
5354 means standard output and thus the current buffer), or nil (which
5357 tramp-encoding-shell ;program
5358 (when (and input (not (string-match "%s" cmd)))
5360 (if (eq output t) t nil) ;output
5362 tramp-encoding-command-switch
5363 ;; actual shell command
5365 (if (string-match "%s" cmd) (format cmd input) cmd)
5366 (if (stringp output) (concat "> " output) ""))))
5368 (defun tramp-maybe-open-connection (multi-method method user host)
5369 "Maybe open a connection to HOST, logging in as USER, using METHOD.
5370 Does not do anything if a connection is already open, but re-opens the
5371 connection if a previous connection has died for some reason."
5372 (let ((p (get-buffer-process
5373 (tramp-get-buffer multi-method method user host)))
5375 ;; If too much time has passed since last command was sent, look
5376 ;; whether process is still alive. If it isn't, kill it. When
5377 ;; using ssh, it can sometimes happen that the remote end has hung
5378 ;; up but the local ssh client doesn't recognize this until it
5379 ;; tries to send some data to the remote end. So that's why we
5380 ;; try to send a command from time to time, then look again
5381 ;; whether the process is really alive.
5383 (set-buffer (tramp-get-buffer multi-method method user host))
5384 (when (and tramp-last-cmd-time
5385 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60))
5387 multi-method method user host "echo are you awake" nil t)
5388 (unless (tramp-wait-for-output 10)
5392 (unless (and p (processp p) (memq (process-status p) '(run open)))
5393 (when (and p (processp p))
5395 (funcall (tramp-get-connection-function
5397 (tramp-find-method multi-method method user host)
5399 multi-method method user host))))
5401 (defun tramp-send-command
5402 (multi-method method user host command &optional noerase neveropen)
5403 "Send the COMMAND to USER at HOST (logged in using METHOD).
5404 Erases temporary buffer before sending the command (unless NOERASE
5406 If optional seventh arg NEVEROPEN is non-nil, never try to open the
5407 connection. This is meant to be used from
5408 `tramp-maybe-open-connection' only."
5410 (tramp-maybe-open-connection multi-method method user host))
5411 (setq tramp-last-cmd-time (current-time))
5412 (when tramp-debug-buffer
5414 (set-buffer (tramp-get-debug-buffer multi-method method user host))
5415 (goto-char (point-max))
5416 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
5418 (set-buffer (tramp-get-buffer multi-method method user host))
5419 (unless noerase (erase-buffer))
5420 (setq proc (get-buffer-process (current-buffer)))
5421 (process-send-string proc
5422 (concat command tramp-rsh-end-of-line))))
5424 ;; It seems that Tru64 Unix does not like it if long strings are sent
5425 ;; to it in one go. (This happens when sending the Perl
5426 ;; `file-attributes' implementation, for instance.) Therefore, we
5427 ;; have this function which waits a bit at each line.
5428 (defun tramp-send-linewise
5429 (multi-method method user host string &optional noerase)
5430 "Send the STRING to USER at HOST linewise.
5431 Erases temporary buffer before sending the STRING (unless NOERASE
5434 The STRING is expected to use Unix line-endings, but the lines sent to
5435 the remote host use line-endings as defined in the variable
5436 `tramp-rsh-end-of-line'."
5437 (tramp-maybe-open-connection multi-method method user host)
5438 (when tramp-debug-buffer
5440 (set-buffer (tramp-get-debug-buffer multi-method method user host))
5441 (goto-char (point-max))
5442 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
5444 (lines (split-string string "\n")))
5445 (set-buffer (tramp-get-buffer multi-method method user host))
5446 (unless noerase (erase-buffer))
5447 (setq proc (get-buffer-process (current-buffer)))
5450 (process-send-string proc
5451 (concat x tramp-rsh-end-of-line)))
5454 (defun tramp-wait-for-output (&optional timeout)
5455 "Wait for output from remote rsh command."
5456 (let ((proc (get-buffer-process (current-buffer)))
5458 (start-time (current-time))
5459 (end-of-output (concat "^"
5460 (regexp-quote tramp-end-of-output)
5462 ;; Algorithm: get waiting output. See if last line contains
5463 ;; end-of-output sentinel. If not, wait a bit and again get
5464 ;; waiting output. Repeat until timeout expires or end-of-output
5465 ;; sentinel is seen. Will hang if timeout is nil and
5466 ;; end-of-output sentinel never appears.
5469 ;; Work around an XEmacs bug, where the timeout expires
5470 ;; faster than it should. This degenerates into polling
5471 ;; for buggy XEmacsen, but oh, well.
5472 (while (and (not found)
5473 (< (tramp-time-diff (current-time) start-time)
5475 (with-timeout (timeout)
5477 (accept-process-output proc 1)
5478 (goto-char (point-max))
5480 (setq found (looking-at end-of-output))))))
5483 (accept-process-output proc 1)
5484 (goto-char (point-max))
5486 (setq found (looking-at end-of-output))))))
5487 ;; At this point, either the timeout has expired or we have found
5488 ;; the end-of-output sentinel.
5490 (goto-char (point-max))
5492 (delete-region (point) (point-max)))
5493 ;; Add output to debug buffer if appropriate.
5494 (when tramp-debug-buffer
5496 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5497 tramp-current-user tramp-current-host)
5498 (point-min) (point-max))
5502 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5503 tramp-current-user tramp-current-host))
5504 (goto-char (point-max))
5505 (insert "[[Remote prompt `" end-of-output "' not found"
5506 (if timeout (format " in %d secs" timeout) "")
5508 (goto-char (point-min))
5509 ;; Return value is whether end-of-output sentinel was found.
5512 (defun tramp-match-string-list (&optional string)
5513 "Returns list of all match strings.
5514 That is, (list (match-string 0) (match-string 1) ...), according to the
5516 (let* ((nmatches (/ (length (match-data)) 2))
5520 (setq res (cons (match-string i string) res))
5524 (defun tramp-send-command-and-check (multi-method method user host command
5526 "Run COMMAND and check its exit status.
5527 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
5528 Sends `echo $?' along with the COMMAND for checking the exit status. If
5529 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
5531 If the optional argument SUBSHELL is non-nil, the command is executed in
5532 a subshell, ie surrounded by parentheses."
5533 (tramp-send-command multi-method method user host
5534 (concat (if subshell "( " "")
5536 (if command " 2>/dev/null; " "")
5537 "echo tramp_exit_status $?"
5538 (if subshell " )" " ")))
5539 (tramp-wait-for-output)
5540 (goto-char (point-max))
5541 (unless (search-backward "tramp_exit_status " nil t)
5542 (error "Couldn't find exit status of `%s'" command))
5543 (skip-chars-forward "^ ")
5544 (read (current-buffer)))
5546 (defun tramp-barf-unless-okay (multi-method method user host command subshell
5547 signal fmt &rest args)
5548 "Run COMMAND, check exit status, throw error if exit status not okay.
5549 Similar to `tramp-send-command-and-check' but accepts two more arguments
5550 FMT and ARGS which are passed to `error'."
5551 (unless (zerop (tramp-send-command-and-check
5552 multi-method method user host command subshell))
5553 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
5555 (pop-to-buffer (current-buffer))
5556 (funcall 'signal signal (apply 'format fmt args))))
5558 ;; Chunked sending kluge.
5559 (defvar tramp-chunksize nil
5560 "If non-nil, chunksize for sending things to remote host.")
5562 (defun tramp-send-region (multi-method method user host start end)
5563 "Send the region from START to END to remote command
5564 running as USER on HOST using METHOD."
5565 (let ((proc (get-buffer-process
5566 (tramp-get-buffer multi-method method user host))))
5568 (error "Can't send region to remote host -- not logged in"))
5572 (tramp-message-for-buffer
5573 multi-method method user host 10
5574 "Sending chunk from %s to %s" pos end)
5575 (process-send-region proc
5577 (min (+ pos tramp-chunksize)
5579 (setq pos (+ pos tramp-chunksize))
5581 (process-send-region proc start end))
5582 (when tramp-debug-buffer
5584 (tramp-get-debug-buffer multi-method method user host)
5587 (defun tramp-send-eof (multi-method method user host)
5588 "Send EOF to the remote end.
5589 METHOD, HOST and USER specify the connection."
5590 (let ((proc (get-buffer-process
5591 (tramp-get-buffer multi-method method user host))))
5593 (error "Can't send EOF to remote host -- not logged in"))
5594 (process-send-eof proc)))
5595 ; (process-send-string proc "\^D")))
5597 (defun tramp-kill-process (multi-method method user host)
5598 "Kill the connection process used by Tramp.
5599 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
5600 (let ((proc (get-buffer-process
5601 (tramp-get-buffer multi-method method user host))))
5602 (kill-process proc)))
5604 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
5605 "Erase buffer, then discard subsequent garbage.
5606 If `tramp-discard-garbage' is nil, just erase buffer."
5607 (if (not tramp-discard-garbage)
5609 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
5610 (when tramp-debug-buffer
5612 (set-buffer (tramp-get-debug-buffer multi-method method user host))
5613 (goto-char (point-max))
5614 (tramp-insert-with-face
5615 'bold (format "Additional characters detected\n")))))))
5617 (defun tramp-mode-string-to-int (mode-string)
5618 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
5619 (let* ((mode-chars (string-to-vector mode-string))
5620 (owner-read (aref mode-chars 1))
5621 (owner-write (aref mode-chars 2))
5622 (owner-execute-or-setid (aref mode-chars 3))
5623 (group-read (aref mode-chars 4))
5624 (group-write (aref mode-chars 5))
5625 (group-execute-or-setid (aref mode-chars 6))
5626 (other-read (aref mode-chars 7))
5627 (other-write (aref mode-chars 8))
5628 (other-execute-or-sticky (aref mode-chars 9)))
5632 (?r (tramp-octal-to-decimal "00400")) (?- 0)
5633 (t (error "Second char `%c' must be one of `r-'" owner-read)))
5635 (?w (tramp-octal-to-decimal "00200")) (?- 0)
5636 (t (error "Third char `%c' must be one of `w-'" owner-write)))
5637 (case owner-execute-or-setid
5638 (?x (tramp-octal-to-decimal "00100"))
5639 (?S (tramp-octal-to-decimal "04000"))
5640 (?s (tramp-octal-to-decimal "04100"))
5642 (t (error "Fourth char `%c' must be one of `xsS-'"
5643 owner-execute-or-setid)))
5645 (?r (tramp-octal-to-decimal "00040")) (?- 0)
5646 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
5648 (?w (tramp-octal-to-decimal "00020")) (?- 0)
5649 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
5650 (case group-execute-or-setid
5651 (?x (tramp-octal-to-decimal "00010"))
5652 (?S (tramp-octal-to-decimal "02000"))
5653 (?s (tramp-octal-to-decimal "02010"))
5655 (t (error "Seventh char `%c' must be one of `xsS-'"
5656 group-execute-or-setid)))
5658 (?r (tramp-octal-to-decimal "00004")) (?- 0)
5659 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
5661 (?w (tramp-octal-to-decimal "00002")) (?- 0)
5662 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
5663 (case other-execute-or-sticky
5664 (?x (tramp-octal-to-decimal "00001"))
5665 (?T (tramp-octal-to-decimal "01000"))
5666 (?t (tramp-octal-to-decimal "01001"))
5668 (t (error "Tenth char `%c' must be one of `xtT-'"
5669 other-execute-or-sticky)))))))
5672 (defun tramp-file-mode-from-int (mode)
5673 "Turn an integer representing a file mode into an ls(1)-like string."
5674 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
5675 (user (logand (lsh mode -6) 7))
5676 (group (logand (lsh mode -3) 7))
5677 (other (logand (lsh mode -0) 7))
5678 (suid (> (logand (lsh mode -9) 4) 0))
5679 (sgid (> (logand (lsh mode -9) 2) 0))
5680 (sticky (> (logand (lsh mode -9) 1) 0)))
5681 (setq user (tramp-file-mode-permissions user suid "s"))
5682 (setq group (tramp-file-mode-permissions group sgid "s"))
5683 (setq other (tramp-file-mode-permissions other sticky "t"))
5684 (concat type user group other)))
5687 (defun tramp-file-mode-permissions (perm suid suid-text)
5688 "Convert a permission bitset into a string.
5689 This is used internally by `tramp-file-mode-from-int'."
5690 (let ((r (> (logand perm 4) 0))
5691 (w (> (logand perm 2) 0))
5692 (x (> (logand perm 1) 0)))
5693 (concat (or (and r "r") "-")
5694 (or (and w "w") "-")
5695 (or (and suid x suid-text) ; suid, execute
5696 (and suid (upcase suid-text)) ; suid, !execute
5697 (and x "x") "-")))) ; !suid
5700 (defun tramp-decimal-to-octal (i)
5701 "Return a string consisting of the octal digits of I.
5702 Not actually used. Use `(format \"%o\" i)' instead?"
5703 (cond ((< i 0) (error "Cannot convert negative number to octal"))
5704 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
5706 (t (concat (tramp-decimal-to-octal (/ i 8))
5707 (number-to-string (% i 8))))))
5710 ;;(defun tramp-octal-to-decimal (ostr)
5711 ;; "Given a string of octal digits, return a decimal number."
5712 ;; (cond ((null ostr) 0)
5713 ;; ((string= "" ostr) 0)
5714 ;; (t (let ((last (aref ostr (1- (length ostr))))
5715 ;; (rest (substring ostr 0 (1- (length ostr)))))
5716 ;; (unless (and (>= last ?0)
5718 ;; (error "Not an octal digit: %c" last))
5719 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
5720 ;; Kudos to Gerd Moellmann for this suggestion.
5721 (defun tramp-octal-to-decimal (ostr)
5722 "Given a string of octal digits, return a decimal number."
5723 (let ((x (or ostr "")))
5724 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
5725 (unless (string-match "\\`[0-7]*\\'" x)
5726 (error "Non-octal junk in string `%s'" x))
5727 (string-to-number ostr 8)))
5729 (defun tramp-shell-case-fold (string)
5730 "Converts STRING to shell glob pattern which ignores case."
5733 (if (equal (downcase c) (upcase c))
5735 (format "[%c%c]" (downcase c) (upcase c))))
5740 ;; ------------------------------------------------------------
5741 ;; -- TRAMP file names --
5742 ;; ------------------------------------------------------------
5743 ;; Conversion functions between external representation and
5744 ;; internal data structure. Convenience functions for internal
5747 (defstruct tramp-file-name multi-method method user host path)
5749 (defun tramp-tramp-file-p (name)
5750 "Return t iff NAME is a tramp file."
5752 (string-match tramp-file-name-regexp name)))
5754 ;; HHH: Changed. Used to assign the return value of (user-login-name)
5755 ;; to the `user' part of the structure if a user name was not
5756 ;; provided, now it assigns nil.
5757 (defun tramp-dissect-file-name (name)
5758 "Return an `tramp-file-name' structure.
5759 The structure consists of remote method, remote user, remote host and
5763 (unless (string-match (nth 0 tramp-file-name-structure) name)
5764 (error "Not a tramp file name: %s" name))
5765 (setq method (match-string (nth 1 tramp-file-name-structure) name))
5766 (if (and method (member method tramp-multi-methods))
5767 ;; If it's a multi method, the file name structure contains
5768 ;; arrays of method, user and host.
5769 (tramp-dissect-multi-file-name name)
5770 ;; Normal method. First, find out default method.
5771 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
5772 (host (match-string (nth 3 tramp-file-name-structure) name))
5773 (path (match-string (nth 4 tramp-file-name-structure) name)))
5774 (make-tramp-file-name
5781 (defun tramp-find-default-method (user host)
5782 "Look up the right method to use in `tramp-default-method-alist'."
5783 (let ((choices tramp-default-method-alist)
5784 (method tramp-default-method)
5787 (setq item (pop choices))
5788 (when (and (string-match (nth 0 item) (or host ""))
5789 (string-match (nth 1 item) (or user "")))
5790 (setq method (nth 2 item))
5791 (setq choices nil)))
5794 (defun tramp-find-method (multi-method method user host)
5795 "Return the right method string to use.
5796 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
5797 If both MULTI-METHOD and METHOD are nil, do a lookup in
5798 `tramp-default-method-alist'."
5799 (or multi-method method (tramp-find-default-method user host)))
5801 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
5802 ;; a user name is not provided in the "file name" very well.
5803 (defun tramp-dissect-multi-file-name (name)
5804 "Not implemented yet."
5805 (let ((regexp (nth 0 tramp-multi-file-name-structure))
5806 (method-index (nth 1 tramp-multi-file-name-structure))
5807 (hops-index (nth 2 tramp-multi-file-name-structure))
5808 (path-index (nth 3 tramp-multi-file-name-structure))
5809 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
5810 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
5811 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
5812 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
5813 method hops len hop-methods hop-users hop-hosts path)
5814 (unless (string-match (format regexp hop-regexp) name)
5815 (error "Not a multi tramp file name: %s" name))
5816 (setq method (match-string method-index name))
5817 (setq hops (match-string hops-index name))
5818 (setq len (/ (length (match-data t)) 2))
5819 (when (< path-index 0) (incf path-index len))
5820 (setq path (match-string path-index name))
5822 (while (string-match hop-regexp hops index)
5823 (setq index (match-end 0))
5825 (cons (match-string hop-method-index hops) hop-methods))
5827 (cons (match-string hop-user-index hops) hop-users))
5829 (cons (match-string hop-host-index hops) hop-hosts))))
5830 (make-tramp-file-name
5831 :multi-method method
5832 :method (apply 'vector (reverse hop-methods))
5833 :user (apply 'vector (reverse hop-users))
5834 :host (apply 'vector (reverse hop-hosts))
5837 (defun tramp-make-tramp-file-name (multi-method method user host path)
5838 "Constructs a tramp file name from METHOD, USER, HOST and PATH."
5840 (tramp-make-tramp-multi-file-name multi-method method user host path)
5842 (concat tramp-prefix-format
5843 (when method (concat "%m" tramp-postfix-single-method-format))
5844 (when user (concat "%u" tramp-postfix-user-format))
5845 (when host (concat "%h" tramp-postfix-host-format))
5846 (when path (concat "%p")))
5847 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,path)))))
5849 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
5850 ;; with this when USER is nil?
5851 (defun tramp-make-tramp-multi-file-name (multi-method method user host path)
5852 "Constructs a tramp file name for a multi-hop method."
5853 (unless tramp-make-multi-tramp-file-format
5854 (error "`tramp-make-multi-tramp-file-format' is nil"))
5855 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
5856 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
5857 (path-format (nth 2 tramp-make-multi-tramp-file-format))
5858 (prefix (format-spec prefix-format `((?m . ,multi-method))))
5860 (path (format-spec path-format `((?p . ,path))))
5862 (len (length method)))
5864 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
5865 (setq hops (concat hops (format-spec hop-format
5866 `((?m . ,m) (?u . ,u) (?h . ,h)))))
5868 (concat prefix hops path)))
5870 (defun tramp-make-rcp-program-file-name (user host path)
5871 "Create a file name suitable to be passed to `rcp'."
5873 (format "%s@%s:%s" user host path)
5874 (format "%s:%s" host path)))
5876 (defun tramp-make-ange-ftp-file-name (user host path)
5877 "Given user, host, and path, return an Ange-FTP filename."
5879 (format "/%s@%s:%s" user host path)
5880 (format "/%s:%s" host path)))
5882 (defun tramp-method-out-of-band-p (multi-method method user host)
5883 "Return t if this is an out-of-band method, nil otherwise.
5884 It is important to check for this condition, since it is not possible
5885 to enter a password for the `tramp-rcp-program'."
5886 (tramp-get-rcp-program
5888 (tramp-find-method multi-method method user host)
5891 ;; Variables local to connection.
5893 (defun tramp-get-ls-command (multi-method method user host)
5895 (tramp-maybe-open-connection multi-method method user host)
5896 (set-buffer (tramp-get-buffer multi-method method user host))
5899 (defun tramp-get-test-groks-nt (multi-method method user host)
5901 (tramp-maybe-open-connection multi-method method user host)
5902 (set-buffer (tramp-get-buffer multi-method method user host))
5903 tramp-test-groks-nt))
5905 (defun tramp-get-file-exists-command (multi-method method user host)
5907 (tramp-maybe-open-connection multi-method method user host)
5908 (set-buffer (tramp-get-buffer multi-method method user host))
5909 tramp-file-exists-command))
5911 (defun tramp-get-remote-perl (multi-method method user host)
5912 (tramp-get-connection-property "perl" nil multi-method method user host))
5914 (defun tramp-get-remote-ln (multi-method method user host)
5915 (tramp-get-connection-property "ln" nil multi-method method user host))
5917 ;; Get a property of a TRAMP connection.
5918 (defun tramp-get-connection-property
5919 (property default multi-method method user host)
5920 "Get the named property for the connection.
5921 If the value is not set for the connection, return `default'"
5922 (tramp-maybe-open-connection multi-method method user host)
5923 (with-current-buffer (tramp-get-buffer multi-method method user host)
5926 (symbol-value (intern (concat "tramp-connection-property-" property)))
5929 ;; Set a property of a TRAMP connection.
5930 (defun tramp-set-connection-property
5931 (property value multi-method method user host)
5932 "Set the named property of a TRAMP connection."
5933 (tramp-maybe-open-connection multi-method method user host)
5934 (with-current-buffer (tramp-get-buffer multi-method method user host)
5935 (set (make-local-variable
5936 (intern (concat "tramp-connection-property-" property)))
5939 ;; Some predefined connection properties.
5940 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
5941 (tramp-set-connection-property "remote-encoding" rem-enc
5942 multi-method method user host))
5943 (defun tramp-get-remote-encoding (multi-method method user host)
5944 (tramp-get-connection-property "remote-encoding" nil
5945 multi-method method user host))
5947 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
5948 (tramp-set-connection-property "remote-decoding" rem-dec
5949 multi-method method user host))
5950 (defun tramp-get-remote-decoding (multi-method method user host)
5951 (tramp-get-connection-property "remote-decoding" nil
5952 multi-method method user host))
5954 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
5955 (tramp-set-connection-property "local-encoding" loc-enc
5956 multi-method method user host))
5957 (defun tramp-get-local-encoding (multi-method method user host)
5958 (tramp-get-connection-property "local-encoding" nil
5959 multi-method method user host))
5961 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
5962 (tramp-set-connection-property "local-decoding" loc-dec
5963 multi-method method user host))
5964 (defun tramp-get-local-decoding (multi-method method user host)
5965 (tramp-get-connection-property "local-decoding" nil
5966 multi-method method user host))
5970 (defun tramp-get-connection-function (multi-method method user host)
5971 (second (or (assoc 'tramp-connection-function
5972 (assoc (tramp-find-method multi-method method user host)
5974 (error "Method `%s' didn't specify a connection function"
5975 (or multi-method method)))))
5977 (defun tramp-get-remote-sh (multi-method method user host)
5978 (second (or (assoc 'tramp-remote-sh
5979 (assoc (tramp-find-method multi-method method user host)
5981 (error "Method `%s' didn't specify a remote shell"
5982 (or multi-method method)))))
5984 (defun tramp-get-rsh-program (multi-method method user host)
5985 (second (or (assoc 'tramp-rsh-program
5986 (assoc (tramp-find-method multi-method method user host)
5988 (error "Method `%s' didn't specify an rsh program"
5989 (or multi-method method)))))
5991 (defun tramp-get-rsh-args (multi-method method user host)
5992 (second (or (assoc 'tramp-rsh-args
5993 (assoc (tramp-find-method multi-method method user host)
5995 (error "Method `%s' didn't specify rsh args"
5996 (or multi-method method)))))
5998 (defun tramp-get-rcp-program (multi-method method user host)
5999 (second (or (assoc 'tramp-rcp-program
6000 (assoc (tramp-find-method multi-method method user host)
6002 (error "Method `%s' didn't specify an rcp program"
6003 (or multi-method method)))))
6005 (defun tramp-get-rcp-args (multi-method method user host)
6006 (second (or (assoc 'tramp-rcp-args
6007 (assoc (tramp-find-method multi-method method user host)
6009 (error "Method `%s' didn't specify rcp args"
6010 (or multi-method method)))))
6012 (defun tramp-get-rcp-keep-date-arg (multi-method method user host)
6013 (second (or (assoc 'tramp-rcp-keep-date-arg
6014 (assoc (tramp-find-method multi-method method user host)
6016 (error "Method `%s' didn't specify `keep-date' arg for tramp"
6017 (or multi-method method)))))
6019 (defun tramp-get-su-program (multi-method method user host)
6020 (second (or (assoc 'tramp-su-program
6021 (assoc (tramp-find-method multi-method method user host)
6023 (error "Method `%s' didn't specify a su program"
6024 (or multi-method method)))))
6026 (defun tramp-get-su-args (multi-method method user host)
6027 (second (or (assoc 'tramp-su-args
6028 (assoc (tramp-find-method multi-method method user host)
6030 (error "Method `%s' didn't specify su args"
6031 (or multi-method method)))))
6033 (defun tramp-get-telnet-program (multi-method method user host)
6034 (second (or (assoc 'tramp-telnet-program
6035 (assoc (tramp-find-method multi-method method user host)
6037 (error "Method `%s' didn't specify a telnet program"
6038 (or multi-method method)))))
6040 (defun tramp-get-telnet-args (multi-method method user host)
6041 (second (or (assoc 'tramp-telnet-args
6042 (assoc (tramp-find-method multi-method method user host)
6044 (error "Method `%s' didn't specify telnet args"
6045 (or multi-method method)))))
6048 ;; Auto saving to a special directory.
6050 (defun tramp-make-auto-save-file-name (fn)
6051 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
6052 (when tramp-auto-save-directory
6053 (unless (file-exists-p tramp-auto-save-directory)
6054 (make-directory tramp-auto-save-directory t)))
6055 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
6056 ;; file name we make sure that jka-compr isn't used for the
6058 (let ((buffer-file-name (expand-file-name
6059 (tramp-subst-strs-in-string '(("_" . "|")
6066 tramp-auto-save-directory)))
6067 (make-auto-save-file-name)))
6069 (defadvice make-auto-save-file-name
6070 (around tramp-advice-make-auto-save-file-name () activate)
6071 "Invoke `tramp-make-auto-save-file-name' for tramp files."
6072 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
6073 tramp-auto-save-directory)
6074 (setq ad-return-value
6075 (tramp-make-auto-save-file-name (buffer-file-name)))
6078 (defun tramp-subst-strs-in-string (alist string)
6079 "Replace all occurrences of the string FROM with TO in STRING.
6080 ALIST is of the form ((FROM . TO) ...)."
6083 (let* ((pr (car alist))
6086 (while (string-match (regexp-quote from) string)
6087 (setq string (replace-match to t t string)))
6088 (setq alist (cdr alist))))
6091 (defun tramp-insert-with-face (face string)
6092 "Insert text with a specific face."
6093 (let ((start (point)))
6095 (add-text-properties start (point) (list 'face face))))
6097 ;; ------------------------------------------------------------
6098 ;; -- Compatibility functions section --
6099 ;; ------------------------------------------------------------
6101 (defun tramp-temporary-file-directory ()
6102 "Return name of directory for temporary files (compat function).
6103 For Emacs, this is the variable `temporary-file-directory', for XEmacs
6104 this is the function `temp-directory'."
6105 (cond ((boundp 'temporary-file-directory)
6106 (symbol-value 'temporary-file-directory))
6107 ((fboundp 'temp-directory)
6108 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
6109 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
6110 (file-name-as-directory (getenv "TEMP")))
6111 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
6112 (file-name-as-directory (getenv "TMP")))
6113 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
6114 (file-name-as-directory (getenv "TMPDIR")))
6115 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
6116 (t (message (concat "Neither `temporary-file-directory' nor "
6117 "`temp-directory' is defined -- using /tmp."))
6118 (file-name-as-directory "/tmp"))))
6120 (defun tramp-read-passwd (prompt)
6121 "Read a password from user (compat function).
6122 Invokes `read-passwd' if that is defined, else `ange-ftp-read-passwd'."
6124 (if (fboundp 'read-passwd) #'read-passwd #'ange-ftp-read-passwd)
6127 (defun tramp-time-diff (t1 t2)
6128 "Return the difference between the two times, in seconds.
6129 T1 and T2 are time values (as returned by `current-time' for example).
6131 NOTE: This function will fail if the time difference is too large to
6133 ;; Pacify byte-compiler with `symbol-function'.
6134 (cond ((fboundp 'subtract-time)
6135 (cadr (funcall (symbol-function 'subtract-time) t1 t2)))
6136 ((fboundp 'itimer-time-difference)
6138 (symbol-function 'itimer-time-difference)
6139 (if (< (length t1) 3) (append t1 '(0)) t1)
6140 (if (< (length t2) 3) (append t2 '(0)) t2))))
6142 ;; snarfed from Emacs 21 time-date.el
6143 (cadr (let ((borrow (< (cadr t1) (cadr t2))))
6144 (list (- (car t1) (car t2) (if borrow 1 0))
6145 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))))
6147 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
6148 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
6149 EOL-TYPE can be one of `dos', `unix', or `mac'."
6150 (cond ((fboundp 'coding-system-change-eol-conversion)
6151 (apply #'coding-system-change-eol-conversion
6152 (list coding-system eol-type)))
6153 ((fboundp 'subsidiary-coding-system)
6155 #'subsidiary-coding-system
6157 (cond ((eq eol-type 'dos) 'crlf)
6158 ((eq eol-type 'unix) 'lf)
6159 ((eq eol-type 'mac) 'cr)
6161 (error "Unknown EOL-TYPE `%s', must be %s"
6163 "`dos', `unix', or `mac'"))))))
6164 (t (error "Can't change EOL conversion -- is MULE missing?"))))
6166 (defun tramp-split-string (string pattern)
6167 "Like `split-string' but omit empty strings.
6168 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
6169 This is, the first, empty, element is omitted. In XEmacs, the first
6170 element is not omitted.
6172 Note: this function has been written for `tramp-handle-file-truename'.
6173 If you want to use it for something else, you'll have to check whether
6174 it does the right thing."
6175 (delete "" (split-string string pattern)))
6177 ;; ------------------------------------------------------------
6178 ;; -- Kludges section --
6179 ;; ------------------------------------------------------------
6181 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
6182 ;; does not deal well with newline characters. Newline is replaced by
6183 ;; backslash newline. But if, say, the string `a backslash newline b'
6184 ;; is passed to a shell, the shell will expand this into "ab",
6185 ;; completely omitting the newline. This is not what was intended.
6186 ;; It does not appear to be possible to make the function
6187 ;; `shell-quote-argument' work with newlines without making it
6188 ;; dependent on the shell used. But within this package, we know that
6189 ;; we will always use a Bourne-like shell, so we use an approach which
6192 ;; The approach is simple: we call `shell-quote-argument', then
6193 ;; massage the newline part of the result.
6195 ;; This function should produce a string which is grokked by a Unix
6196 ;; shell, even if the Emacs is running on Windows. Since this is the
6197 ;; kludges section, we bind `system-type' in such a way that
6198 ;; `shell-quote-arguments' behaves as if on Unix.
6200 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
6201 ;; function to work with Bourne-like shells.
6203 ;; CCC: This function should be rewritten so that
6204 ;; `shell-quote-argument' is not used. This way, we are safe from
6205 ;; changes in `shell-quote-argument'.
6206 (defun tramp-shell-quote-argument (s)
6207 "Similar to `shell-quote-argument', but groks newlines.
6208 Only works for Bourne-like shells."
6209 (let ((system-type 'not-windows))
6211 (let ((result (shell-quote-argument s))
6212 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
6213 (when (and (>= (length result) 2)
6214 (string= (substring result 0 2) "\\~"))
6215 (setq result (substring result 1)))
6216 (while (string-match nl result)
6217 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
6221 ;; ;; EFS hooks itself into the file name handling stuff in more places
6222 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
6223 ;; ;; away from tramp.el paths.
6225 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
6226 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
6227 ;; ;; connection to help it's dired process. Not that I have any real
6228 ;; ;; idea *why* this is helpful to dired.
6230 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
6232 ;; ;; Daniel Pittman <daniel@danann.net>
6234 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
6235 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
6236 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
6237 ;; ;; EFS might nicht be loaded yet.
6238 ;; (defadvice efs-ftp-path (around dont-match-tramp-path activate protect)
6239 ;; "Cause efs-ftp-path to fail when the path is a TRAMP path."
6240 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
6244 ;; We currently (sometimes) use "[" and "]" in the filename format.
6245 ;; This means that Emacs wants to expand wildcards if
6246 ;; `find-file-wildcards' is non-nil, and then barfs because no
6247 ;; expansion could be found. We detect this situation and do
6248 ;; something really awful: we have `file-expand-wildcards' return the
6249 ;; original filename if it can't expand anything. Let's just hope
6250 ;; that this doesn't break anything else.
6251 ;; CCC: This check is now also really awful; we should search all
6252 ;; of the filename format, not just the prefix.
6253 (when (string-match "\\[" tramp-prefix-format)
6254 (defadvice file-expand-wildcards (around tramp-fix activate)
6255 (let ((name (ad-get-arg 0)))
6256 (if (tramp-tramp-file-p name)
6257 ;; If it's a Tramp file, dissect it and look if wildcards
6258 ;; need to be expanded at all.
6259 (let ((v (tramp-dissect-file-name name)))
6260 (if (string-match "[[*?]" (tramp-file-name-path v))
6261 (let ((res ad-do-it))
6262 (setq ad-return-value (or res (list name))))
6263 (setq ad-return-value (list name))))
6264 ;; If it is not a Tramp file, just run the original function.
6265 (let ((res ad-do-it))
6266 (setq ad-return-value (or res (list name)))))))
6269 ;; Tramp version is useful in a number of situations.
6271 (defun tramp-version (arg)
6272 "Print version number of tramp.el in minibuffer or current buffer."
6274 (if arg (insert tramp-version) (message tramp-version)))
6276 ;; Make the `reporter` functionality available for making bug reports about
6277 ;; the package. A most useful piece of code.
6279 (unless (fboundp 'reporter-submit-bug-report)
6280 (autoload 'reporter-submit-bug-report "reporter"))
6283 "Submit a bug report to the TRAMP developers."
6286 (let ((reporter-prompt-for-summary-p t))
6287 (reporter-submit-bug-report
6288 tramp-bug-report-address ; to-address
6289 (format "tramp (%s)" tramp-version) ; package name and version
6293 tramp-file-exists-command
6294 tramp-current-multi-method
6295 tramp-current-method
6300 tramp-auto-save-directory ; vars to dump
6301 tramp-default-method
6302 tramp-rsh-end-of-line
6303 tramp-password-end-of-line
6305 tramp-login-prompt-regexp
6306 tramp-password-prompt-regexp
6307 tramp-wrong-passwd-regexp
6308 tramp-yesno-prompt-regexp
6309 tramp-yn-prompt-regexp
6310 tramp-temp-name-prefix
6311 tramp-file-name-structure
6312 tramp-file-name-regexp
6313 tramp-multi-file-name-structure
6314 tramp-multi-file-name-hop-structure
6316 tramp-multi-connection-function-alist
6319 tramp-coding-commands
6320 tramp-actions-before-shell
6323 tramp-shell-prompt-pattern
6325 ;; Non-tramp variables of interest
6326 shell-prompt-pattern
6328 backup-by-copying-when-linked
6329 backup-by-copying-when-mismatch
6330 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
6331 'backup-by-copying-when-privileged-mismatch)
6332 file-name-handler-alist)
6336 Enter your bug report in this message, including as much detail as you
6337 possibly can about the problem, what you did to cause it and what the
6338 local and remote machines are.
6340 If you can give a simple set of instructions to make this bug happen
6341 reliably, please include those. Thank you for helping kill bugs in
6344 Another useful thing to do is to put (setq tramp-debug-buffer t) in
6345 the ~/.emacs file and to repeat the bug. Then, include the contents
6346 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
6349 --bug report follows this line--
6352 (defalias 'tramp-submit-bug 'tramp-bug)
6356 ;; Make sure that we get integration with the VC package.
6357 ;; When it is loaded, we need to pull in the integration module.
6358 ;; This must come after (provide 'tramp) because tramp-vc.el
6360 (eval-after-load "vc"
6361 '(require 'tramp-vc))
6365 ;; * Autodetect if remote `ls' groks the "--dired" switch.
6366 ;; * Add fallback for inline encodings. This should be used
6367 ;; if the remote end doesn't support mimencode or a similar program.
6368 ;; For reading files from the remote host, we can just parse the output
6369 ;; of `od -b'. For writing files to the remote host, we construct
6370 ;; a shell program which contains only "safe" ascii characters
6371 ;; and which writes the right bytes to the file. We can use printf(1)
6372 ;; or "echo -e" or the printf function in awk and use octal escapes
6373 ;; for the "dangerous" characters. The null byte might be a problem.
6374 ;; On some systems, the octal escape doesn't work. So we try the following
6375 ;; two commands to write a null byte:
6376 ;; dd if=/dev/zero bs=1 count=1
6377 ;; echo | tr '\n' '\000'
6378 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
6379 ;; the two via a format which can be `uu' or `b64'. Then we can search
6380 ;; for the right local commands and the right remote commands separately.
6381 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
6382 ;; work for remote files.
6383 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
6384 ;; `shell-quote-argument'.
6385 ;; * Completion gets confused when you leave out the method name.
6386 ;; * Support `dired-compress-file' filename handler.
6387 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
6388 ;; by the files in that directory. Add this here.
6389 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
6390 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
6391 ;; * When logging in, keep looking for questions according to an alist
6392 ;; and then invoke the right function.
6393 ;; * Case-insensitive filename completion. (Norbert Goevert.)
6394 ;; * Running CVS remotely doesn't appear to work right. It thinks
6395 ;; files are locked by somebody else even if I'm the locking user.
6396 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
6398 ;; * Don't use globbing for directories with many files, as this is
6399 ;; likely to produce long command lines, and some shells choke on
6400 ;; long command lines.
6401 ;; * Find out about the new auto-save mechanism in Emacs 21 and
6402 ;; do the right thing.
6403 ;; * `vc-directory' does not work. It never displays any files, even
6404 ;; if it does show files when run locally.
6405 ;; * Allow correction of passwords, if the remote end allows this.
6406 ;; (Mark Hershberger)
6407 ;; * Make sure permissions of tmp file are good.
6408 ;; (Nelson Minar <nelson@media.mit.edu>)
6409 ;; * Grok passwd prompts with scp? (David Winter
6410 ;; <winter@nevis1.nevis.columbia.edu>). Maybe just do `ssh -l user
6411 ;; host', then wait a while for the passwd or passphrase prompt. If
6412 ;; there is one, remember the passwd/phrase.
6413 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
6414 ;; * Do asynchronous `shell-command's.
6415 ;; * Grok `append' parameter for `write-region'.
6416 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
6417 ;; * abbreviate-file-name
6418 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
6419 ;; * `C' in dired gives error `not tramp file name'.
6420 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
6421 ;; what the user names should default to, though.
6422 ;; * better error checking. At least whenever we see something
6423 ;; strange when doing zerop, we should kill the process and start
6424 ;; again. (Greg Stark)
6425 ;; * Add caching for filename completion. (Greg Stark)
6426 ;; Of course, this has issues with usability (stale cache bites)
6427 ;; -- <daniel@danann.net>
6428 ;; * Provide a local cache of old versions of remote files for the rsync
6429 ;; transfer method to use. (Greg Stark)
6430 ;; * Remove unneeded parameters from methods.
6431 ;; * Invoke rsync once for copying a whole directory hierarchy.
6432 ;; (Francesco Potortì)
6433 ;; * Should we set PATH ourselves or should we rely on the remote end
6435 ;; * Do the autoconf thing.
6436 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
6437 ;; * Allow non-Unix remote systems. (More a long-term thing.)
6438 ;; * Make it work for different encodings, and for different file name
6439 ;; encodings, too. (Daniel Pittman)
6440 ;; * Change applicable functions to pass a struct tramp-file-name rather
6441 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, PATH.
6442 ;; * Implement asynchronous shell commands.
6443 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
6444 ;; * Progress reports while copying files. (Michael Kifer)
6445 ;; * `Smart' connection method that uses inline for small and out of
6446 ;; band for large files. (Michael Kifer)
6447 ;; * Don't search for perl5 and perl. Instead, only search for perl and
6448 ;; then look if it's the right version (with `perl -v').
6449 ;; * When editing a remote CVS controlled file as a different user, VC
6450 ;; gets confused about the file locking status. Try to find out why
6451 ;; the workaround doesn't work.
6452 ;; * When user is running ssh-agent, it would be useful to add the
6453 ;; passwords typed by the user to that agent. This way, the next time
6454 ;; round, the users don't have to type all this in again.
6455 ;; This would be especially useful for start-process, I think.
6456 ;; An easy way to implement start-process is to open a second shell
6457 ;; connection which is inconvenient if the user has to reenter
6459 ;; * Change `copy-file' to grok the case where the filename handler
6460 ;; for the source and the target file are different. Right now,
6461 ;; it looks at the source file and then calls that handler, if
6462 ;; there is one. But since ange-ftp, for instance, does not know
6463 ;; about Tramp, it does not do the right thing if the target file
6464 ;; name is a Tramp name.
6465 ;; * Username and hostname completion.
6466 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
6467 ;; connect to host "blabla" already if that host is unique. No idea
6468 ;; how to suppress. Maybe not an essential problem.
6469 ;; ** For "/ssh1-old:", `(file-name-all-completions "ssh1" "/")' is called
6470 ;; only. Likely due to word delimeter property of "-". Maybe we can remove
6471 ;; all the "*-old" methods? Or rename them to "*_old"?
6472 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
6473 ;; ** Handle quoted file names, starting with "/:". Problem is that
6474 ;; `file-name-non-special' calls later on `file-name-all-completions'
6475 ;; without ":". Hmm. Worth a bug report?
6476 ;; ** Acknowledge port numbers.
6477 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
6478 ;; ** Unify `tramp-parse-{rhosts,shosts,hosts,passwd,netrc}'.
6479 ;; Code is nearly identical.
6480 ;; ** Decide whiche files to take for searching user/host names depending on
6481 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
6482 ;; ** Enhance variables for debug.
6483 ;; ** Implement "/multi:" completion.
6484 ;; ** Add a learning mode for completion. Make results persistent.
6486 ;; Functions for file-name-handler-alist:
6487 ;; diff-latest-backup-file -- in diff.el
6488 ;; dired-compress-file
6489 ;; dired-uncache -- this will be needed when we do insert-directory caching
6490 ;; file-name-as-directory -- use primitive?
6491 ;; file-name-directory -- use primitive?
6492 ;; file-name-nondirectory -- use primitive?
6493 ;; file-name-sans-versions -- use primitive?
6494 ;; file-newer-than-file-p
6495 ;; find-backup-file-name
6496 ;; get-file-buffer -- use primitive
6498 ;; unhandled-file-name-directory
6501 ;;; tramp.el ends here