(insert-for-yank-1): If last inserted char has properties,
[emacs.git] / lisp / net / tramp.el
blob97b08e7e704ae8ef79e7813fbf8ae87873898714
1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
4 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006 Free Software Foundation, Inc.
7 ;; Author: Kai Gro\e,A_\e(Bjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This package provides remote file editing, similar to ange-ftp.
31 ;; The difference is that ange-ftp uses FTP to transfer files between
32 ;; the local and the remote host, whereas tramp.el uses a combination
33 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
35 ;; For more detailed instructions, please see the info file.
37 ;; Notes:
38 ;; -----
40 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
41 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
42 ;; 19 is reported to have other problems. For XEmacs 21, you need the
43 ;; package `fsf-compat' for the `with-timeout' macro.)
45 ;; This version might not work with pre-Emacs 21 VC unless VC is
46 ;; loaded before tramp.el. Could you please test this and tell me about
47 ;; the result? Thanks.
49 ;; Also see the todo list at the bottom of this file.
51 ;; The current version of Tramp can be retrieved from the following URL:
52 ;; http://ftp.gnu.org/gnu/tramp/
54 ;; There's a mailing list for this, as well. Its name is:
55 ;; tramp-devel@gnu.org
56 ;; You can use the Web to subscribe, under the following URL:
57 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
59 ;; For the adventurous, the current development sources are available
60 ;; via CVS. You can find instructions about this at the following URL:
61 ;; http://savannah.gnu.org/projects/tramp/
62 ;; Click on "CVS" in the navigation bar near the top.
64 ;; Don't forget to put on your asbestos longjohns, first!
66 ;;; Code:
68 ;; The Tramp version number and bug report address, as prepared by configure.
69 (require 'trampver)
70 (add-hook 'tramp-unload-hook
71 '(lambda ()
72 (when (featurep 'trampver)
73 (unload-feature 'trampver 'force))))
75 (if (featurep 'xemacs)
76 (require 'timer-funcs)
77 (require 'timer))
79 (require 'format-spec) ;from Gnus 5.8, also in tar ball
80 ;; As long as password.el is not part of (X)Emacs, it shouldn't
81 ;; be mandatory
82 (if (featurep 'xemacs)
83 (load "password" 'noerror)
84 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
86 ;; The explicit check is not necessary in Emacs, which provides the
87 ;; feature even if implemented in C, but it appears to be necessary
88 ;; in XEmacs.
89 (unless (and (fboundp 'base64-encode-region)
90 (fboundp 'base64-decode-region))
91 (require 'base64)) ;for the mimencode methods
92 (require 'shell)
93 (require 'advice)
95 (autoload 'tramp-uuencode-region "tramp-uu"
96 "Implementation of `uuencode' in Lisp.")
97 (add-hook 'tramp-unload-hook
98 '(lambda ()
99 (when (featurep 'tramp-uu)
100 (unload-feature 'tramp-uu 'force))))
102 (unless (fboundp 'uudecode-decode-region)
103 (autoload 'uudecode-decode-region "uudecode"))
105 ;; XEmacs is distributed with few Lisp packages. Further packages are
106 ;; installed using EFS. If we use a unified filename format, then
107 ;; Tramp is required in addition to EFS. (But why can't Tramp just
108 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
109 ;; just like before.) Another reason for using a separate filename
110 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
111 ;; Tramp only knows how to deal with `file-name-handler-alist', not
112 ;; the other places.
113 ;;;###autoload
114 (defvar tramp-unified-filenames (not (featurep 'xemacs))
115 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
116 Nil means to use a separate filename syntax for Tramp.")
118 ;; Load foreign methods. Because they do require Tramp internally, this
119 ;; must be done with the `eval-after-load' trick.
121 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
122 (unless (featurep 'xemacs)
123 (eval-after-load "tramp"
124 '(progn
125 (require 'tramp-ftp)
126 (add-hook 'tramp-unload-hook
127 '(lambda ()
128 (when (featurep 'tramp-ftp)
129 (unload-feature 'tramp-ftp 'force)))))))
130 (when (and tramp-unified-filenames (featurep 'xemacs))
131 (eval-after-load "tramp"
132 '(progn
133 (require 'tramp-efs)
134 (add-hook 'tramp-unload-hook
135 '(lambda ()
136 (when (featurep 'tramp-efs)
137 (unload-feature 'tramp-efs 'force)))))))
139 ;; tramp-smb uses "smbclient" from Samba.
140 ;; Not available under Cygwin and Windows, because they don't offer
141 ;; "smbclient". And even not necessary there, because Emacs supports
142 ;; UNC file names like "//host/share/localname".
143 (unless (memq system-type '(cygwin windows-nt))
144 (eval-after-load "tramp"
145 '(progn
146 (require 'tramp-smb)
147 (add-hook 'tramp-unload-hook
148 '(lambda ()
149 (when (featurep 'tramp-smb)
150 (unload-feature 'tramp-smb 'force)))))))
152 (eval-when-compile
153 (require 'cl)
154 (require 'custom)
155 ;; Emacs 19.34 compatibility hack -- is this needed?
156 (or (>= emacs-major-version 20)
157 (load "cl-seq")))
159 (unless (boundp 'custom-print-functions)
160 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
162 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
163 ;; Currently, XEmacs supports this.
164 (eval-when-compile
165 (when (featurep 'xemacs)
166 (let (unused-vars) ; Pacify Emacs byte-compiler
167 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
168 (byte-compiler-options (warnings (- unused-vars))))))
170 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
171 ;; used in XEmacs, so we set it here and there. The following is needed
172 ;; to pacify Emacs byte-compiler.
173 (eval-when-compile
174 (when (boundp 'byte-compile-not-obsolete-var)
175 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
177 ;;; User Customizable Internal Variables:
179 (defgroup tramp nil
180 "Edit remote files with a combination of rsh and rcp or similar programs."
181 :group 'files
182 :version "22.1")
184 (defcustom tramp-verbose 9
185 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
186 :group 'tramp
187 :type 'integer)
189 (defcustom tramp-debug-buffer nil
190 "*Whether to send all commands and responses to a debug buffer."
191 :group 'tramp
192 :type 'boolean)
194 ;; Emacs case
195 (eval-and-compile
196 (when (boundp 'backup-directory-alist)
197 (defcustom tramp-backup-directory-alist nil
198 "Alist of filename patterns and backup directory names.
199 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
200 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
201 is a local file name, the backup directory is prepended with Tramp file
202 name prefix \(multi-method, method, user, host\) of file.
204 \(setq tramp-backup-directory-alist backup-directory-alist\)
206 gives the same backup policy for Tramp files on their hosts like the
207 policy for local files."
208 :group 'tramp
209 :type '(repeat (cons (regexp :tag "Regexp matching filename")
210 (directory :tag "Backup directory name"))))))
212 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
213 ;; the package "backup-dir" might not be loaded yet.
214 (eval-and-compile
215 (when (featurep 'xemacs)
216 (defcustom tramp-bkup-backup-directory-info nil
217 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
218 It has the same meaning like `bkup-backup-directory-info' from package
219 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
220 file name, the backup directory is prepended with Tramp file name prefix
221 \(multi-method, method, user, host\) of file.
223 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
225 gives the same backup policy for Tramp files on their hosts like the
226 policy for local files."
227 :type '(repeat
228 (list (regexp :tag "File regexp")
229 (string :tag "Backup Dir")
230 (set :inline t
231 (const ok-create)
232 (const full-path)
233 (const prepend-name)
234 (const search-upward))))
235 :group 'tramp)))
237 (defcustom tramp-auto-save-directory nil
238 "*Put auto-save files in this directory, if set.
239 The idea is to use a local directory so that auto-saving is faster."
240 :group 'tramp
241 :type '(choice (const nil)
242 string))
244 (defcustom tramp-encoding-shell
245 (if (memq system-type '(windows-nt))
246 (getenv "COMSPEC")
247 "/bin/sh")
248 "*Use this program for encoding and decoding commands on the local host.
249 This shell is used to execute the encoding and decoding command on the
250 local host, so if you want to use `~' in those commands, you should
251 choose a shell here which groks tilde expansion. `/bin/sh' normally
252 does not understand tilde expansion.
254 For encoding and deocding, commands like the following are executed:
256 /bin/sh -c COMMAND < INPUT > OUTPUT
258 This variable can be used to change the \"/bin/sh\" part. See the
259 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
260 variable `tramp-encoding-reads-stdin' to specify whether the commands read
261 standard input or a file.
263 Note that this variable is not used for remote commands. There are
264 mechanisms in tramp.el which automatically determine the right shell to
265 use for the remote host."
266 :group 'tramp
267 :type '(file :must-match t))
269 (defcustom tramp-encoding-command-switch
270 (if (string-match "cmd\\.exe" tramp-encoding-shell)
271 "/c"
272 "-c")
273 "*Use this switch together with `tramp-encoding-shell' for local commands.
274 See the variable `tramp-encoding-shell' for more information."
275 :group 'tramp
276 :type 'string)
278 (defcustom tramp-encoding-reads-stdin t
279 "*If non-nil, encoding commands read from standard input.
280 If nil, the filename is the last argument.
282 Note that the commands always must write to standard output."
283 :group 'tramp
284 :type 'boolean)
286 (defcustom tramp-multi-sh-program
287 tramp-encoding-shell
288 "*Use this program for bootstrapping multi-hop connections.
289 This variable is similar to `tramp-encoding-shell', but it is only used
290 when initializing a multi-hop connection. Therefore, the set of
291 commands sent to this shell is quite restricted, and if you are
292 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
293 shell which does not normally exist on Windows anyway).
295 To use multi-hop methods from Windows, you also need suitable entries
296 in `tramp-multi-connection-function-alist' for the first hop.
298 This variable defaults to the value of `tramp-encoding-shell'."
299 :group 'tramp
300 :type '(file :must-match t))
302 ;; CCC I have changed all occurrences of comint-quote-filename with
303 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
304 ;; There, comint-quote-filename was removed altogether. If it turns
305 ;; out to be necessary there, something will need to be done.
306 ;;-(defcustom tramp-file-name-quote-list
307 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
308 ;;- "*Protect these characters from the remote shell.
309 ;;-Any character in this list is quoted (preceded with a backslash)
310 ;;-because it means something special to the shell. This takes effect
311 ;;-when sending file and directory names to the remote shell.
313 ;;-See `comint-file-name-quote-list' for details."
314 ;;- :group 'tramp
315 ;;- :type '(repeat character))
317 (defcustom tramp-methods
318 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
319 (tramp-login-program "rsh")
320 (tramp-copy-program "rcp")
321 (tramp-remote-sh "/bin/sh")
322 (tramp-login-args nil)
323 (tramp-copy-args nil)
324 (tramp-copy-keep-date-arg "-p")
325 (tramp-password-end-of-line nil))
326 ("scp" (tramp-connection-function tramp-open-connection-rsh)
327 (tramp-login-program "ssh")
328 (tramp-copy-program "scp")
329 (tramp-remote-sh "/bin/sh")
330 (tramp-login-args ("-e" "none"))
331 (tramp-copy-args nil)
332 (tramp-copy-keep-date-arg "-p")
333 (tramp-password-end-of-line nil))
334 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
335 (tramp-login-program "ssh")
336 (tramp-copy-program "scp")
337 (tramp-remote-sh "/bin/sh")
338 (tramp-login-args ("-1" "-e" "none"))
339 (tramp-copy-args ("-1"))
340 (tramp-copy-keep-date-arg "-p")
341 (tramp-password-end-of-line nil))
342 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
343 (tramp-login-program "ssh")
344 (tramp-copy-program "scp")
345 (tramp-remote-sh "/bin/sh")
346 (tramp-login-args ("-2" "-e" "none"))
347 (tramp-copy-args ("-2"))
348 (tramp-copy-keep-date-arg "-p")
349 (tramp-password-end-of-line nil))
350 ("scp1_old"
351 (tramp-connection-function tramp-open-connection-rsh)
352 (tramp-login-program "ssh1")
353 (tramp-copy-program "scp1")
354 (tramp-remote-sh "/bin/sh")
355 (tramp-login-args ("-e" "none"))
356 (tramp-copy-args nil)
357 (tramp-copy-keep-date-arg "-p")
358 (tramp-password-end-of-line nil))
359 ("scp2_old"
360 (tramp-connection-function tramp-open-connection-rsh)
361 (tramp-login-program "ssh2")
362 (tramp-copy-program "scp2")
363 (tramp-remote-sh "/bin/sh")
364 (tramp-login-args ("-e" "none"))
365 (tramp-copy-args nil)
366 (tramp-copy-keep-date-arg "-p")
367 (tramp-password-end-of-line nil))
368 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
369 (tramp-login-program "ssh")
370 (tramp-copy-program "rsync")
371 (tramp-remote-sh "/bin/sh")
372 (tramp-login-args ("-e" "none"))
373 (tramp-copy-args ("-e" "ssh"))
374 (tramp-copy-keep-date-arg "-t")
375 (tramp-password-end-of-line nil))
376 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
377 (tramp-login-program "remsh")
378 (tramp-copy-program "rcp")
379 (tramp-remote-sh "/bin/sh")
380 (tramp-login-args nil)
381 (tramp-copy-args nil)
382 (tramp-copy-keep-date-arg "-p")
383 (tramp-password-end-of-line nil))
384 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
385 (tramp-login-program "rsh")
386 (tramp-copy-program nil)
387 (tramp-remote-sh "/bin/sh")
388 (tramp-login-args nil)
389 (tramp-copy-args nil)
390 (tramp-copy-keep-date-arg nil)
391 (tramp-password-end-of-line nil))
392 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
393 (tramp-login-program "ssh")
394 (tramp-copy-program nil)
395 (tramp-remote-sh "/bin/sh")
396 (tramp-login-args ("-e" "none"))
397 (tramp-copy-args nil)
398 (tramp-copy-keep-date-arg nil)
399 (tramp-password-end-of-line nil))
400 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
401 (tramp-login-program "ssh")
402 (tramp-copy-program nil)
403 (tramp-remote-sh "/bin/sh")
404 (tramp-login-args ("-1" "-e" "none"))
405 (tramp-copy-args ("-1"))
406 (tramp-copy-keep-date-arg nil)
407 (tramp-password-end-of-line nil))
408 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
409 (tramp-login-program "ssh")
410 (tramp-copy-program nil)
411 (tramp-remote-sh "/bin/sh")
412 (tramp-login-args ("-2" "-e" "none"))
413 (tramp-copy-args ("-2"))
414 (tramp-copy-keep-date-arg nil)
415 (tramp-password-end-of-line nil))
416 ("ssh1_old"
417 (tramp-connection-function tramp-open-connection-rsh)
418 (tramp-login-program "ssh1")
419 (tramp-copy-program nil)
420 (tramp-remote-sh "/bin/sh")
421 (tramp-login-args ("-e" "none"))
422 (tramp-copy-args nil)
423 (tramp-copy-keep-date-arg nil)
424 (tramp-password-end-of-line nil))
425 ("ssh2_old"
426 (tramp-connection-function tramp-open-connection-rsh)
427 (tramp-login-program "ssh2")
428 (tramp-copy-program nil)
429 (tramp-remote-sh "/bin/sh")
430 (tramp-login-args ("-e" "none"))
431 (tramp-copy-args nil)
432 (tramp-copy-keep-date-arg nil)
433 (tramp-password-end-of-line nil))
434 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
435 (tramp-login-program "remsh")
436 (tramp-copy-program nil)
437 (tramp-remote-sh "/bin/sh")
438 (tramp-login-args nil)
439 (tramp-copy-args nil)
440 (tramp-copy-keep-date-arg nil)
441 (tramp-password-end-of-line nil))
442 ("telnet"
443 (tramp-connection-function tramp-open-connection-telnet)
444 (tramp-login-program "telnet")
445 (tramp-copy-program nil)
446 (tramp-remote-sh "/bin/sh")
447 (tramp-login-args nil)
448 (tramp-copy-args nil)
449 (tramp-copy-keep-date-arg nil)
450 (tramp-password-end-of-line nil))
451 ("su" (tramp-connection-function tramp-open-connection-su)
452 (tramp-login-program "su")
453 (tramp-copy-program nil)
454 (tramp-remote-sh "/bin/sh")
455 (tramp-login-args ("-" "%u"))
456 (tramp-copy-args nil)
457 (tramp-copy-keep-date-arg nil)
458 (tramp-password-end-of-line nil))
459 ("sudo" (tramp-connection-function tramp-open-connection-su)
460 (tramp-login-program "sudo")
461 (tramp-copy-program nil)
462 (tramp-remote-sh "/bin/sh")
463 (tramp-login-args ("-u" "%u" "-s"
464 "-p" "Password:"))
465 (tramp-copy-args nil)
466 (tramp-copy-keep-date-arg nil)
467 (tramp-password-end-of-line nil))
468 ("multi" (tramp-connection-function tramp-open-connection-multi)
469 (tramp-login-program nil)
470 (tramp-copy-program nil)
471 (tramp-remote-sh "/bin/sh")
472 (tramp-login-args nil)
473 (tramp-copy-args nil)
474 (tramp-copy-keep-date-arg nil)
475 (tramp-password-end-of-line nil))
476 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
477 (tramp-login-program "ssh")
478 (tramp-copy-program "scp")
479 (tramp-remote-sh "/bin/sh")
480 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
481 (tramp-copy-args nil)
482 (tramp-copy-keep-date-arg "-p")
483 (tramp-password-end-of-line nil))
484 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
485 (tramp-login-program "ssh")
486 (tramp-copy-program nil)
487 (tramp-remote-sh "/bin/sh")
488 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
489 (tramp-copy-args nil)
490 (tramp-copy-keep-date-arg nil)
491 (tramp-password-end-of-line nil))
492 ("krlogin"
493 (tramp-connection-function tramp-open-connection-rsh)
494 (tramp-login-program "krlogin")
495 (tramp-copy-program nil)
496 (tramp-remote-sh "/bin/sh")
497 (tramp-login-args ("-x"))
498 (tramp-copy-args nil)
499 (tramp-copy-keep-date-arg nil)
500 (tramp-password-end-of-line nil))
501 ("plink"
502 (tramp-connection-function tramp-open-connection-rsh)
503 (tramp-login-program "plink")
504 (tramp-copy-program nil)
505 (tramp-remote-sh "/bin/sh")
506 (tramp-login-args ("-ssh")) ;optionally add "-v"
507 (tramp-copy-args nil)
508 (tramp-copy-keep-date-arg nil)
509 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
510 ("plink1"
511 (tramp-connection-function tramp-open-connection-rsh)
512 (tramp-login-program "plink")
513 (tramp-copy-program nil)
514 (tramp-remote-sh "/bin/sh")
515 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
516 (tramp-copy-args nil)
517 (tramp-copy-keep-date-arg nil)
518 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
519 ("pscp"
520 (tramp-connection-function tramp-open-connection-rsh)
521 (tramp-login-program "plink")
522 (tramp-copy-program "pscp")
523 (tramp-remote-sh "/bin/sh")
524 (tramp-login-args ("-ssh"))
525 (tramp-copy-args nil)
526 (tramp-copy-keep-date-arg "-p")
527 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
528 ("fcp"
529 (tramp-connection-function tramp-open-connection-rsh)
530 (tramp-login-program "fsh")
531 (tramp-copy-program "fcp")
532 (tramp-remote-sh "/bin/sh -i")
533 (tramp-login-args ("sh" "-i"))
534 (tramp-copy-args nil)
535 (tramp-copy-keep-date-arg "-p")
536 (tramp-password-end-of-line nil))
538 "*Alist of methods for remote files.
539 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
540 Each NAME stands for a remote access method. Each PARAM is a
541 pair of the form (KEY VALUE). The following KEYs are defined:
542 * `tramp-connection-function'
543 This specifies the function to use to connect to the remote host.
544 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
545 and `tramp-open-connection-su' are defined. See the documentation
546 of these functions for more details.
547 * `tramp-remote-sh'
548 This specifies the Bourne shell to use on the remote host. This
549 MUST be a Bourne-like shell. It is normally not necessary to set
550 this to any value other than \"/bin/sh\": tramp wants to use a shell
551 which groks tilde expansion, but it can search for it. Also note
552 that \"/bin/sh\" exists on all Unixen, this might not be true for
553 the value that you decide to use. You Have Been Warned.
554 * `tramp-login-program'
555 This specifies the name of the program to use for logging in to the
556 remote host. Depending on `tramp-connection-function', this may be
557 the name of rsh or a workalike program (when
558 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
559 name of telnet or a workalike (for `tramp-open-connection-telnet'),
560 or the name of su or a workalike (for `tramp-open-connection-su').
561 * `tramp-login-args'
562 This specifies the list of arguments to pass to the above
563 mentioned program. Please note that this is a list of arguments,
564 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
565 here. Instead, you want two list elements, one for \"-a\" and one
566 for \"-b\", or one for \"-f\" and one for \"foo\".
567 If `tramp-connection-function' is `tramp-open-connection-su', then
568 \"%u\" in this list is replaced by the user name, and \"%%\" can
569 be used to obtain a literal percent character.
570 * `tramp-copy-program'
571 This specifies the name of the program to use for remotely copying
572 the file; this might be the absolute filename of rcp or the name of
573 a workalike program.
574 * `tramp-copy-args'
575 This specifies the list of parameters to pass to the above mentioned
576 program, the hints for `tramp-login-args' also apply here.
577 * `tramp-copy-keep-date-arg'
578 This specifies the parameter to use for the copying program when the
579 timestamp of the original file should be kept. For `rcp', use `-p', for
580 `rsync', use `-t'.
581 * `tramp-password-end-of-line'
582 This specifies the string to use for terminating the line after
583 submitting the password. If this method parameter is nil, then the
584 value of the normal variable `tramp-default-password-end-of-line'
585 is used. This parameter is necessary because the \"plink\" program
586 requires any two characters after sending the password. These do
587 not have to be newline or carriage return characters. Other login
588 programs are happy with just one character, the newline character.
589 We use \"xy\" as the value for methods using \"plink\".
591 What does all this mean? Well, you should specify `tramp-login-program'
592 for all methods; this program is used to log in to the remote site. Then,
593 there are two ways to actually transfer the files between the local and the
594 remote side. One way is using an additional rcp-like program. If you want
595 to do this, set `tramp-copy-program' in the method.
597 Another possibility for file transfer is inline transfer, i.e. the
598 file is passed through the same buffer used by `tramp-login-program'. In
599 this case, the file contents need to be protected since the
600 `tramp-login-program' might use escape codes or the connection might not
601 be eight-bit clean. Therefore, file contents are encoded for transit.
602 See the variable `tramp-coding-commands' for details.
604 So, to summarize: if the method is an out-of-band method, then you
605 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
606 inline method, then these two parameters should be nil. Every method,
607 inline or out of band, must specify `tramp-connection-function' plus
608 the associated arguments (for example, the login program if you chose
609 `tramp-open-connection-telnet').
611 Notes:
613 When using `tramp-open-connection-su' the phrase `open connection to a
614 remote host' sounds strange, but it is used nevertheless, for
615 consistency. No connection is opened to a remote host, but `su' is
616 started on the local host. You are not allowed to specify a remote
617 host other than `localhost' or the name of the local host."
618 :group 'tramp
619 :type '(repeat
620 (cons string
621 (set (list (const tramp-connection-function) function)
622 (list (const tramp-login-program)
623 (choice (const nil) string))
624 (list (const tramp-copy-program)
625 (choice (const nil) string))
626 (list (const tramp-remote-sh)
627 (choice (const nil) string))
628 (list (const tramp-login-args) (repeat string))
629 (list (const tramp-copy-args) (repeat string))
630 (list (const tramp-copy-keep-date-arg)
631 (choice (const nil) string))
632 (list (const tramp-encoding-command)
633 (choice (const nil) string))
634 (list (const tramp-decoding-command)
635 (choice (const nil) string))
636 (list (const tramp-encoding-function)
637 (choice (const nil) function))
638 (list (const tramp-decoding-function)
639 (choice (const nil) function))
640 (list (const tramp-password-end-of-line)
641 (choice (const nil) string))))))
643 (defcustom tramp-multi-methods '("multi" "multiu")
644 "*List of multi-hop methods.
645 Each entry in this list should be a method name as mentioned in the
646 variable `tramp-methods'."
647 :group 'tramp
648 :type '(repeat string))
650 (defcustom tramp-multi-connection-function-alist
651 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
652 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
653 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
654 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
655 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
656 ("su" tramp-multi-connect-su "su - %u%n")
657 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
658 "*List of connection functions for multi-hop methods.
659 Each list item is a list of three items (METHOD FUNCTION COMMAND),
660 where METHOD is the name as used in the file name, FUNCTION is the
661 function to be executed, and COMMAND is the shell command used for
662 connecting.
664 COMMAND may contain percent escapes. `%u' will be replaced with the
665 user name, `%h' will be replaced with the host name, and `%n' will be
666 replaced with an end-of-line character, as specified in the variable
667 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
668 Note that the interpretation of the percent escapes also depends on
669 the FUNCTION. For example, the `%u' escape is forbidden with the
670 function `tramp-multi-connect-telnet'. See the documentation of the
671 various functions for details."
672 :group 'tramp
673 :type '(repeat (list string function string)))
675 (defcustom tramp-default-method
676 (if (and (fboundp 'executable-find)
677 (executable-find "plink"))
678 "plink"
679 "ssh")
680 "*Default method to use for transferring files.
681 See `tramp-methods' for possibilities.
682 Also see `tramp-default-method-alist'."
683 :group 'tramp
684 :type 'string)
686 (defcustom tramp-default-method-alist
687 '(("\\`localhost\\'" "\\`root\\'" "su"))
688 "*Default method to use for specific user/host pairs.
689 This is an alist of items (HOST USER METHOD). The first matching item
690 specifies the method to use for a file name which does not specify a
691 method. HOST and USER are regular expressions or nil, which is
692 interpreted as a regular expression which always matches. If no entry
693 matches, the variable `tramp-default-method' takes effect.
695 If the file name does not specify the user, lookup is done using the
696 empty string for the user name.
698 See `tramp-methods' for a list of possibilities for METHOD."
699 :group 'tramp
700 :type '(repeat (list (regexp :tag "Host regexp")
701 (regexp :tag "User regexp")
702 (string :tag "Method"))))
704 ;; Default values for non-Unices seeked
705 (defconst tramp-completion-function-alist-rsh
706 (unless (memq system-type '(windows-nt))
707 '((tramp-parse-rhosts "/etc/hosts.equiv")
708 (tramp-parse-rhosts "~/.rhosts")))
709 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
711 ;; Default values for non-Unices seeked
712 (defconst tramp-completion-function-alist-ssh
713 (unless (memq system-type '(windows-nt))
714 '((tramp-parse-rhosts "/etc/hosts.equiv")
715 (tramp-parse-rhosts "/etc/shosts.equiv")
716 (tramp-parse-shosts "/etc/ssh_known_hosts")
717 (tramp-parse-sconfig "/etc/ssh_config")
718 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
719 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
720 (tramp-parse-rhosts "~/.rhosts")
721 (tramp-parse-rhosts "~/.shosts")
722 (tramp-parse-shosts "~/.ssh/known_hosts")
723 (tramp-parse-sconfig "~/.ssh/config")
724 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
725 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
726 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
728 ;; Default values for non-Unices seeked
729 (defconst tramp-completion-function-alist-telnet
730 (unless (memq system-type '(windows-nt))
731 '((tramp-parse-hosts "/etc/hosts")))
732 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
734 ;; Default values for non-Unices seeked
735 (defconst tramp-completion-function-alist-su
736 (unless (memq system-type '(windows-nt))
737 '((tramp-parse-passwd "/etc/passwd")))
738 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
740 (defvar tramp-completion-function-alist nil
741 "*Alist of methods for remote files.
742 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
743 Each NAME stands for a remote access method. Each PAIR is of the form
744 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
745 names from FILE for completion. The following predefined FUNCTIONs exists:
747 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
748 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
749 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
750 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
751 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
752 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
753 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
754 * `tramp-parse-netrc' for \"~/.netrc\" like files.
756 FUNCTION can also be a customer defined function. For more details see
757 the info pages.")
759 (eval-after-load "tramp"
760 '(progn
761 (tramp-set-completion-function
762 "rcp" tramp-completion-function-alist-rsh)
763 (tramp-set-completion-function
764 "scp" tramp-completion-function-alist-ssh)
765 (tramp-set-completion-function
766 "scp1" tramp-completion-function-alist-ssh)
767 (tramp-set-completion-function
768 "scp2" tramp-completion-function-alist-ssh)
769 (tramp-set-completion-function
770 "scp1_old" tramp-completion-function-alist-ssh)
771 (tramp-set-completion-function
772 "scp2_old" tramp-completion-function-alist-ssh)
773 (tramp-set-completion-function
774 "rsync" tramp-completion-function-alist-rsh)
775 (tramp-set-completion-function
776 "remcp" tramp-completion-function-alist-rsh)
777 (tramp-set-completion-function
778 "rsh" tramp-completion-function-alist-rsh)
779 (tramp-set-completion-function
780 "ssh" tramp-completion-function-alist-ssh)
781 (tramp-set-completion-function
782 "ssh1" tramp-completion-function-alist-ssh)
783 (tramp-set-completion-function
784 "ssh2" tramp-completion-function-alist-ssh)
785 (tramp-set-completion-function
786 "ssh1_old" tramp-completion-function-alist-ssh)
787 (tramp-set-completion-function
788 "ssh2_old" tramp-completion-function-alist-ssh)
789 (tramp-set-completion-function
790 "remsh" tramp-completion-function-alist-rsh)
791 (tramp-set-completion-function
792 "telnet" tramp-completion-function-alist-telnet)
793 (tramp-set-completion-function
794 "su" tramp-completion-function-alist-su)
795 (tramp-set-completion-function
796 "sudo" tramp-completion-function-alist-su)
797 (tramp-set-completion-function
798 "multi" nil)
799 (tramp-set-completion-function
800 "scpx" tramp-completion-function-alist-ssh)
801 (tramp-set-completion-function
802 "sshx" tramp-completion-function-alist-ssh)
803 (tramp-set-completion-function
804 "krlogin" tramp-completion-function-alist-rsh)
805 (tramp-set-completion-function
806 "plink" tramp-completion-function-alist-ssh)
807 (tramp-set-completion-function
808 "plink1" tramp-completion-function-alist-ssh)
809 (tramp-set-completion-function
810 "pscp" tramp-completion-function-alist-ssh)
811 (tramp-set-completion-function
812 "fcp" tramp-completion-function-alist-ssh)))
814 (defcustom tramp-rsh-end-of-line "\n"
815 "*String used for end of line in rsh connections.
816 I don't think this ever needs to be changed, so please tell me about it
817 if you need to change this.
818 Also see the method parameter `tramp-password-end-of-line' and the normal
819 variable `tramp-default-password-end-of-line'."
820 :group 'tramp
821 :type 'string)
823 (defcustom tramp-default-password-end-of-line
824 tramp-rsh-end-of-line
825 "*String used for end of line after sending a password.
826 This variable provides the default value for the method parameter
827 `tramp-password-end-of-line', see `tramp-methods' for more details.
829 It seems that people using plink under Windows need to send
830 \"\\r\\n\" (carriage-return, then newline) after a password, but just
831 \"\\n\" after all other lines. This variable can be used for the
832 password, see `tramp-rsh-end-of-line' for the other cases.
834 The default value is to use the same value as `tramp-rsh-end-of-line'."
835 :group 'tramp
836 :type 'string)
838 (defcustom tramp-remote-path
839 ;; "/usr/xpg4/bin" has been placed first, because on Solaris a POSIX
840 ;; compatible "id" is needed.
841 '("/usr/xpg4/bin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
842 "/usr/ccs/bin" "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
843 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
844 "*List of directories to search for executables on remote host.
845 Please notify me about other semi-standard directories to include here.
847 You can use `~' in this list, but when searching for a shell which groks
848 tilde expansion, all directory names starting with `~' will be ignored."
849 :group 'tramp
850 :type '(repeat string))
852 (defcustom tramp-login-prompt-regexp
853 ".*ogin\\( .*\\)?: *"
854 "*Regexp matching login-like prompts.
855 The regexp should match at end of buffer.
857 Sometimes the prompt is reported to look like \"login as:\"."
858 :group 'tramp
859 :type 'regexp)
861 (defcustom tramp-shell-prompt-pattern
862 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
863 "Regexp to match prompts from remote shell.
864 Normally, Tramp expects you to configure `shell-prompt-pattern'
865 correctly, but sometimes it happens that you are connecting to a
866 remote host which sends a different kind of shell prompt. Therefore,
867 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
868 and also things matched by this variable. The default value of this
869 variable is similar to the default value of `shell-prompt-pattern',
870 which should work well in many cases."
871 :group 'tramp
872 :type 'regexp)
874 (defcustom tramp-password-prompt-regexp
875 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
876 "*Regexp matching password-like prompts.
877 The regexp should match at end of buffer.
879 The `sudo' program appears to insert a `^@' character into the prompt."
880 :group 'tramp
881 :type 'regexp)
883 (defcustom tramp-wrong-passwd-regexp
884 (concat "^.*"
885 ;; These strings should be on the last line
886 (regexp-opt '("Permission denied."
887 "Login incorrect"
888 "Login Incorrect"
889 "Connection refused"
890 "Connection closed"
891 "Sorry, try again."
892 "Name or service not known"
893 "Host key verification failed.") t)
894 ".*"
895 "\\|"
896 "^.*\\("
897 ;; Here comes a list of regexes, separated by \\|
898 "Received signal [0-9]+"
899 "\\).*")
900 "*Regexp matching a `login failed' message.
901 The regexp should match at end of buffer."
902 :group 'tramp
903 :type 'regexp)
905 (defcustom tramp-yesno-prompt-regexp
906 (concat
907 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
908 "\\s-*")
909 "Regular expression matching all yes/no queries which need to be confirmed.
910 The confirmation should be done with yes or no.
911 The regexp should match at end of buffer.
912 See also `tramp-yn-prompt-regexp'."
913 :group 'tramp
914 :type 'regexp)
916 (defcustom tramp-yn-prompt-regexp
917 (concat (regexp-opt '("Store key in cache? (y/n)") t)
918 "\\s-*")
919 "Regular expression matching all y/n queries which need to be confirmed.
920 The confirmation should be done with y or n.
921 The regexp should match at end of buffer.
922 See also `tramp-yesno-prompt-regexp'."
923 :group 'tramp
924 :type 'regexp)
926 (defcustom tramp-terminal-prompt-regexp
927 (concat "\\("
928 "TERM = (.*)"
929 "\\|"
930 "Terminal type\\? \\[.*\\]"
931 "\\)\\s-*")
932 "Regular expression matching all terminal setting prompts.
933 The regexp should match at end of buffer.
934 The answer will be provided by `tramp-action-terminal', which see."
935 :group 'tramp
936 :type 'regexp)
938 (defcustom tramp-operation-not-permitted-regexp
939 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
940 (regexp-opt '("Operation not permitted") t))
941 "Regular expression matching keep-date problems in (s)cp operations.
942 Copying has been performed successfully already, so this message can
943 be ignored safely."
944 :group 'tramp
945 :type 'regexp)
947 (defcustom tramp-process-alive-regexp
949 "Regular expression indicating a process has finished.
950 In fact this expression is empty by intention, it will be used only to
951 check regularly the status of the associated process.
952 The answer will be provided by `tramp-action-process-alive',
953 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
954 :group 'tramp
955 :type 'regexp)
957 (defcustom tramp-temp-name-prefix "tramp."
958 "*Prefix to use for temporary files.
959 If this is a relative file name (such as \"tramp.\"), it is considered
960 relative to the directory name returned by the function
961 `tramp-temporary-file-directory' (which see). It may also be an
962 absolute file name; don't forget to include a prefix for the filename
963 part, though."
964 :group 'tramp
965 :type 'string)
967 (defcustom tramp-discard-garbage nil
968 "*If non-nil, try to discard garbage sent by remote shell.
969 Some shells send such garbage upon connection setup."
970 :group 'tramp
971 :type 'boolean)
973 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
974 "*Alist specifying extra arguments to pass to the remote shell.
975 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
976 matching the shell file name and ARGS is a string specifying the
977 arguments.
979 This variable is only used when Tramp needs to start up another shell
980 for tilde expansion. The extra arguments should typically prevent the
981 shell from reading its init file."
982 :group 'tramp
983 ;; This might be the wrong way to test whether the widget type
984 ;; `alist' is available. Who knows the right way to test it?
985 :type (if (get 'alist 'widget-type)
986 '(alist :key-type string :value-type string)
987 '(repeat (cons string string))))
989 (defcustom tramp-prefix-format
990 (if tramp-unified-filenames "/" "/[")
991 "*String matching the very beginning of tramp file names.
992 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
993 :group 'tramp
994 :type 'string)
996 (defcustom tramp-prefix-regexp
997 (concat "^" (regexp-quote tramp-prefix-format))
998 "*Regexp matching the very beginning of tramp file names.
999 Should always start with \"^\". Derived from `tramp-prefix-format'."
1000 :group 'tramp
1001 :type 'regexp)
1003 (defcustom tramp-method-regexp
1004 "[a-zA-Z_0-9-]+"
1005 "*Regexp matching methods identifiers."
1006 :group 'tramp
1007 :type 'regexp)
1009 ;; It is a little bit annoying that in XEmacs case this delimeter is different
1010 ;; for single-hop and multi-hop cases.
1011 (defcustom tramp-postfix-single-method-format
1012 (if tramp-unified-filenames ":" "/")
1013 "*String matching delimeter between method and user or host names.
1014 Applicable for single-hop methods.
1015 Used in `tramp-make-tramp-file-name'."
1016 :group 'tramp
1017 :type 'string)
1019 (defcustom tramp-postfix-single-method-regexp
1020 (regexp-quote tramp-postfix-single-method-format)
1021 "*Regexp matching delimeter between method and user or host names.
1022 Applicable for single-hop methods.
1023 Derived from `tramp-postfix-single-method-format'."
1024 :group 'tramp
1025 :type 'regexp)
1027 (defcustom tramp-postfix-multi-method-format
1029 "*String matching delimeter between method and user or host names.
1030 Applicable for multi-hop methods.
1031 Used in `tramp-make-tramp-multi-file-name'."
1032 :group 'tramp
1033 :type 'string)
1035 (defcustom tramp-postfix-multi-method-regexp
1036 (regexp-quote tramp-postfix-multi-method-format)
1037 "*Regexp matching delimeter between method and user or host names.
1038 Applicable for multi-hop methods.
1039 Derived from `tramp-postfix-multi-method-format'."
1040 :group 'tramp
1041 :type 'regexp)
1043 (defcustom tramp-postfix-multi-hop-format
1044 (if tramp-unified-filenames ":" "/")
1045 "*String matching delimeter between host and next method.
1046 Applicable for multi-hop methods.
1047 Used in `tramp-make-tramp-multi-file-name'."
1048 :group 'tramp
1049 :type 'string)
1051 (defcustom tramp-postfix-multi-hop-regexp
1052 (regexp-quote tramp-postfix-multi-hop-format)
1053 "*Regexp matching delimeter between host and next method.
1054 Applicable for multi-hop methods.
1055 Derived from `tramp-postfix-multi-hop-format'."
1056 :group 'tramp
1057 :type 'regexp)
1059 (defcustom tramp-user-regexp
1060 "[^:/ \t]*"
1061 "*Regexp matching user names."
1062 :group 'tramp
1063 :type 'regexp)
1065 (defcustom tramp-postfix-user-format
1067 "*String matching delimeter between user and host names.
1068 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1069 :group 'tramp
1070 :type 'string)
1072 (defcustom tramp-postfix-user-regexp
1073 (regexp-quote tramp-postfix-user-format)
1074 "*Regexp matching delimeter between user and host names.
1075 Derived from `tramp-postfix-user-format'."
1076 :group 'tramp
1077 :type 'regexp)
1079 (defcustom tramp-host-regexp
1080 "[a-zA-Z0-9_.-]*"
1081 "*Regexp matching host names."
1082 :group 'tramp
1083 :type 'regexp)
1085 (defcustom tramp-host-with-port-regexp
1086 "[a-zA-Z0-9_.#-]*"
1087 "*Regexp matching host names."
1088 :group 'tramp
1089 :type 'regexp)
1091 (defcustom tramp-postfix-host-format
1092 (if tramp-unified-filenames ":" "]")
1093 "*String matching delimeter between host names and localnames.
1094 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1095 :group 'tramp
1096 :type 'string)
1098 (defcustom tramp-postfix-host-regexp
1099 (regexp-quote tramp-postfix-host-format)
1100 "*Regexp matching delimeter between host names and localnames.
1101 Derived from `tramp-postfix-host-format'."
1102 :group 'tramp
1103 :type 'regexp)
1105 (defcustom tramp-localname-regexp
1106 ".*$"
1107 "*Regexp matching localnames."
1108 :group 'tramp
1109 :type 'regexp)
1111 ;; File name format.
1113 (defcustom tramp-file-name-structure
1114 (list
1115 (concat
1116 tramp-prefix-regexp
1117 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1118 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1119 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1120 "\\(" tramp-localname-regexp "\\)")
1121 2 4 5 6)
1123 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1124 the tramp file name structure.
1126 The first element REGEXP is a regular expression matching a tramp file
1127 name. The regex should contain parentheses around the method name,
1128 the user name, the host name, and the file name parts.
1130 The second element METHOD is a number, saying which pair of
1131 parentheses matches the method name. The third element USER is
1132 similar, but for the user name. The fourth element HOST is similar,
1133 but for the host name. The fifth element FILE is for the file name.
1134 These numbers are passed directly to `match-string', which see. That
1135 means the opening parentheses are counted to identify the pair.
1137 See also `tramp-file-name-regexp'."
1138 :group 'tramp
1139 :type '(list (regexp :tag "File name 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 ")
1143 (integer :tag "Paren pair for file name ")))
1145 ;;;###autoload
1146 (defconst tramp-file-name-regexp-unified
1147 "\\`/[^/:]+:"
1148 "Value for `tramp-file-name-regexp' for unified remoting.
1149 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1150 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1152 ;;;###autoload
1153 (defconst tramp-file-name-regexp-separate
1154 "\\`/\\[.*\\]"
1155 "Value for `tramp-file-name-regexp' for separate remoting.
1156 XEmacs uses a separate filename syntax for Tramp and EFS.
1157 See `tramp-file-name-structure-separate' for more explanations.")
1159 ;;;###autoload
1160 (defcustom tramp-file-name-regexp
1161 (if tramp-unified-filenames
1162 tramp-file-name-regexp-unified
1163 tramp-file-name-regexp-separate)
1164 "*Regular expression matching file names handled by tramp.
1165 This regexp should match tramp file names but no other file names.
1166 \(When tramp.el is loaded, this regular expression is prepended to
1167 `file-name-handler-alist', and that is searched sequentially. Thus,
1168 if the tramp entry appears rather early in the `file-name-handler-alist'
1169 and is a bit too general, then some files might be considered tramp
1170 files which are not really tramp files.
1172 Please note that the entry in `file-name-handler-alist' is made when
1173 this file (tramp.el) is loaded. This means that this variable must be set
1174 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1175 updated after changing this variable.
1177 Also see `tramp-file-name-structure'."
1178 :group 'tramp
1179 :type 'regexp)
1181 ;;;###autoload
1182 (defconst tramp-completion-file-name-regexp-unified
1183 "^/$\\|^/[^/:][^/]*$"
1184 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1185 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1186 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1188 ;;;###autoload
1189 (defconst tramp-completion-file-name-regexp-separate
1190 "^/\\([[][^]]*\\)?$"
1191 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1192 XEmacs uses a separate filename syntax for Tramp and EFS.
1193 See `tramp-file-name-structure-separate' for more explanations.")
1195 ;;;###autoload
1196 (defcustom tramp-completion-file-name-regexp
1197 (if tramp-unified-filenames
1198 tramp-completion-file-name-regexp-unified
1199 tramp-completion-file-name-regexp-separate)
1200 "*Regular expression matching file names handled by tramp completion.
1201 This regexp should match partial tramp file names only.
1203 Please note that the entry in `file-name-handler-alist' is made when
1204 this file (tramp.el) is loaded. This means that this variable must be set
1205 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1206 updated after changing this variable.
1208 Also see `tramp-file-name-structure'."
1209 :group 'tramp
1210 :type 'regexp)
1212 (defcustom tramp-multi-file-name-structure
1213 (list
1214 (concat
1215 tramp-prefix-regexp
1216 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1217 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1218 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1219 2 3 -1)
1220 "*Describes the file name structure of `multi' files.
1221 Multi files allow you to contact a remote host in several hops.
1222 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1224 The first element, REGEXP, gives a regular expression to match against
1225 the file name. In this regular expression, `%s' is replaced with the
1226 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1227 allow multiple hops, you normally want to use something like
1228 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1229 of parentheses is used for the HOP element, see below.)
1231 All remaining elements are numbers. METHOD gives the number of the
1232 paren pair which matches the method name. HOP gives the number of the
1233 paren pair which matches the hop sequence. LOCALNAME gives the number of
1234 the paren pair which matches the localname (pathname) on the remote host.
1236 LOCALNAME can also be negative, which means to count from the end. Ie, a
1237 value of -1 means the last paren pair.
1239 I think it would be good if the regexp matches the whole of the
1240 string, but I haven't actually tried what happens if it doesn't..."
1241 :group 'tramp
1242 :type '(list (regexp :tag "File name regexp")
1243 (integer :tag "Paren pair for method name")
1244 (integer :tag "Paren pair for hops")
1245 (integer :tag "Paren pair to match localname")))
1247 (defcustom tramp-multi-file-name-hop-structure
1248 (list
1249 (concat
1250 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1251 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1252 "\\(" tramp-host-with-port-regexp "\\)")
1253 1 2 3)
1254 "*Describes the structure of a hop in multi files.
1255 This is a list of four elements (REGEXP METHOD USER HOST). First
1256 element REGEXP is used to match against the hop. Pair number METHOD
1257 matches the method of one hop, pair number USER matches the user of
1258 one hop, pair number HOST matches the host of one hop.
1260 This regular expression should match exactly all of one hop."
1261 :group 'tramp
1262 :type '(list (regexp :tag "Hop regexp")
1263 (integer :tag "Paren pair for method name")
1264 (integer :tag "Paren pair for user name")
1265 (integer :tag "Paren pair for host name")))
1267 (defcustom tramp-make-multi-tramp-file-format
1268 (list
1269 (concat tramp-prefix-format "%m")
1270 (concat tramp-postfix-multi-hop-format
1271 "%m" tramp-postfix-multi-method-format
1272 "%u" tramp-postfix-user-format
1273 "%h")
1274 (concat tramp-postfix-host-format "%p"))
1275 "*Describes how to construct a `multi' file name.
1276 This is a list of three elements PREFIX, HOP and LOCALNAME.
1278 The first element PREFIX says how to construct the prefix, the second
1279 element HOP specifies what each hop looks like, and the final element
1280 LOCALNAME says how to construct the localname (pathname).
1282 In PREFIX, `%%' means `%' and `%m' means the method name.
1284 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1285 user and hop host, respectively.
1287 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1289 The resulting file name always contains one copy of PREFIX and one
1290 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1291 name.
1293 Note: the current implementation requires the prefix to contain the
1294 method name, followed by all the hops, and the localname must come
1295 last."
1296 :group 'tramp
1297 :type '(list string string string))
1299 (defcustom tramp-terminal-type "dumb"
1300 "*Value of TERM environment variable for logging in to remote host.
1301 Because Tramp wants to parse the output of the remote shell, it is easily
1302 confused by ANSI color escape sequences and suchlike. Often, shell init
1303 files conditionalize this setup based on the TERM environment variable."
1304 :group 'tramp
1305 :type 'string)
1307 (defcustom tramp-completion-without-shell-p nil
1308 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1309 Using shell wildcards for completions has the advantage that it can be
1310 fast even in large directories, but completion is always
1311 case-sensitive. Relying on Lisp only means that case-insensitive
1312 completion is possible (subject to the variable `completion-ignore-case'),
1313 but it might be slow on large directories."
1314 :group 'tramp
1315 :type 'boolean)
1317 (defcustom tramp-actions-before-shell
1318 '((tramp-password-prompt-regexp tramp-action-password)
1319 (tramp-login-prompt-regexp tramp-action-login)
1320 (shell-prompt-pattern tramp-action-succeed)
1321 (tramp-shell-prompt-pattern tramp-action-succeed)
1322 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1323 (tramp-yesno-prompt-regexp tramp-action-yesno)
1324 (tramp-yn-prompt-regexp tramp-action-yn)
1325 (tramp-terminal-prompt-regexp tramp-action-terminal)
1326 (tramp-process-alive-regexp tramp-action-process-alive))
1327 "List of pattern/action pairs.
1328 Whenever a pattern matches, the corresponding action is performed.
1329 Each item looks like (PATTERN ACTION).
1331 The PATTERN should be a symbol, a variable. The value of this
1332 variable gives the regular expression to search for. Note that the
1333 regexp must match at the end of the buffer, \"\\'\" is implicitly
1334 appended to it.
1336 The ACTION should also be a symbol, but a function. When the
1337 corresponding PATTERN matches, the ACTION function is called."
1338 :group 'tramp
1339 :type '(repeat (list variable function)))
1341 (defcustom tramp-actions-copy-out-of-band
1342 '((tramp-password-prompt-regexp tramp-action-password)
1343 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1344 (tramp-process-alive-regexp tramp-action-out-of-band))
1345 "List of pattern/action pairs.
1346 This list is used for copying/renaming with out-of-band methods.
1347 See `tramp-actions-before-shell' for more info."
1348 :group 'tramp
1349 :type '(repeat (list variable function)))
1351 (defcustom tramp-multi-actions
1352 '((tramp-password-prompt-regexp tramp-multi-action-password)
1353 (tramp-login-prompt-regexp tramp-multi-action-login)
1354 (shell-prompt-pattern tramp-multi-action-succeed)
1355 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1356 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1357 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1358 "List of pattern/action pairs.
1359 This list is used for each hop in multi-hop connections.
1360 See `tramp-actions-before-shell' for more info."
1361 :group 'tramp
1362 :type '(repeat (list variable function)))
1364 (defcustom tramp-initial-commands
1365 '("unset HISTORY"
1366 "unset correct"
1367 "unset autocorrect")
1368 "List of commands to send to the first remote shell that we see.
1369 These commands will be sent to any shell, and thus they should be
1370 designed to work in such circumstances. Also, restrict the commands
1371 to the bare necessity for getting the remote shell into a state
1372 where it is possible to execute the Bourne-ish shell.
1374 At the moment, the command to execute the Bourne-ish shell uses strange
1375 quoting which `tcsh' tries to correct, so we send the command \"unset
1376 autocorrect\" to the remote host."
1377 :group 'tramp
1378 :type '(repeat string))
1380 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1381 ;; known to have a bug in `process-send-string'; some ssh connections appear
1382 ;; to drop bytes when data is sent too quickly. There is also a connection
1383 ;; buffer local variable, which is computed depending on remote host properties
1384 ;; when `tramp-chunksize' is zero or nil.
1385 (defcustom tramp-chunksize
1386 (when (and (not (featurep 'xemacs))
1387 (memq system-type '(hpux)))
1388 500)
1389 ;; Parentheses in docstring starting at beginning of line are escaped.
1390 ;; Fontification is messed up when
1391 ;; `open-paren-in-column-0-is-defun-start' set to t.
1392 "*If non-nil, chunksize for sending input to local process.
1393 It is necessary only on systems which have a buggy `process-send-string'
1394 implementation. The necessity, whether this variable must be set, can be
1395 checked via the following code:
1397 (with-temp-buffer
1398 (let* ((user \"xxx\") (host \"yyy\")
1399 (init 0) (step 50)
1400 (sent init) (received init))
1401 (while (= sent received)
1402 (setq sent (+ sent step))
1403 (erase-buffer)
1404 (let ((proc (start-process (buffer-name) (current-buffer)
1405 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1406 (when (memq (process-status proc) '(run open))
1407 (process-send-string proc (make-string sent ?\\ ))
1408 (process-send-eof proc)
1409 (process-send-eof proc))
1410 (while (not (progn (goto-char (point-min))
1411 (re-search-forward \"\\\\w+\" (point-max) t)))
1412 (accept-process-output proc 1))
1413 (when (memq (process-status proc) '(run open))
1414 (setq received (string-to-number (match-string 0)))
1415 (delete-process proc)
1416 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1417 (sit-for 0))))
1418 (if (> sent (+ init step))
1419 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1420 (- sent step))
1421 (message \"Test does not work\")
1422 (display-buffer (current-buffer))
1423 (sit-for 30))))
1425 In the Emacs normally running Tramp, evaluate the above code
1426 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1427 respectively). You can do this, for example, by pasting it into
1428 the `*scratch*' buffer and then hitting C-j with the cursor after the
1429 last closing parenthesis. Note that it works only if you have configured
1430 \"ssh\" to run without password query, see ssh-agent(1).
1432 You will see the number of bytes sent successfully to the remote host.
1433 If that number exceeds 1000, you can stop the execution by hitting
1434 C-g, because your Emacs is likely clean.
1436 If your Emacs is buggy, the code stops and gives you an indication
1437 about the value `tramp-chunksize' should be set. Maybe you could just
1438 experiment a bit, e.g. changing the values of `init' and `step'
1439 in the third line of the code.
1441 When it is necessary to set `tramp-chunksize', you might consider to
1442 use an out-of-the-band method (like \"scp\") instead of an internal one
1443 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1444 performance.
1446 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1447 this variable to be set as well."
1448 :group 'tramp
1449 :type '(choice (const nil) integer))
1451 ;; Logging in to a remote host normally requires obtaining a pty. But
1452 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1453 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1454 ;; for an override of the system default.
1455 (defcustom tramp-process-connection-type t
1456 "Overrides `process-connection-type' for connections from Tramp.
1457 Tramp binds process-connection-type to the value given here before
1458 opening a connection to a remote host."
1459 :group 'tramp
1460 :type '(choice (const nil) (const t) (const pty)))
1462 ;;; Internal Variables:
1464 (defvar tramp-buffer-file-attributes nil
1465 "Holds the `ls -ild' output for the current buffer.
1466 This variable is local to each buffer. It is not used if the remote
1467 machine groks Perl. If it is used, it's used as an emulation for
1468 the visited file modtime.")
1469 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1471 (defvar tramp-md5-function
1472 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1473 ((fboundp 'md5-encode)
1474 (lambda (x) (base64-encode-string
1475 (funcall (symbol-function 'md5-encode) x))))
1476 (t (error "Couldn't find an `md5' function")))
1477 "Function to call for running the MD5 algorithm.")
1479 (defvar tramp-end-of-output
1480 (concat "///"
1481 (funcall tramp-md5-function
1482 (concat
1483 (prin1-to-string process-environment)
1484 (current-time-string)
1485 ;; (prin1-to-string
1486 ;; (if (fboundp 'directory-files-and-attributes)
1487 ;; (funcall 'directory-files-and-attributes
1488 ;; (or (getenv "HOME")
1489 ;; (tramp-temporary-file-directory)))
1490 ;; (mapcar
1491 ;; (lambda (x)
1492 ;; (cons x (file-attributes x)))
1493 ;; (directory-files (or (getenv "HOME")
1494 ;; (tramp-temporary-file-directory))
1495 ;; t))))
1497 "String used to recognize end of output.")
1499 (defvar tramp-connection-function nil
1500 "This internal variable holds a parameter for `tramp-methods'.
1501 In the connection buffer, this variable has the value of the like-named
1502 method parameter, as specified in `tramp-methods' (which see).")
1504 (defvar tramp-remote-sh nil
1505 "This internal variable holds a parameter for `tramp-methods'.
1506 In the connection buffer, this variable has the value of the like-named
1507 method parameter, as specified in `tramp-methods' (which see).")
1509 (defvar tramp-login-program nil
1510 "This internal variable holds a parameter for `tramp-methods'.
1511 In the connection buffer, this variable has the value of the like-named
1512 method parameter, as specified in `tramp-methods' (which see).")
1514 (defvar tramp-login-args nil
1515 "This internal variable holds a parameter for `tramp-methods'.
1516 In the connection buffer, this variable has the value of the like-named
1517 method parameter, as specified in `tramp-methods' (which see).")
1519 (defvar tramp-copy-program nil
1520 "This internal variable holds a parameter for `tramp-methods'.
1521 In the connection buffer, this variable has the value of the like-named
1522 method parameter, as specified in `tramp-methods' (which see).")
1524 (defvar tramp-copy-args nil
1525 "This internal variable holds a parameter for `tramp-methods'.
1526 In the connection buffer, this variable has the value of the like-named
1527 method parameter, as specified in `tramp-methods' (which see).")
1529 (defvar tramp-copy-keep-date-arg nil
1530 "This internal variable holds a parameter for `tramp-methods'.
1531 In the connection buffer, this variable has the value of the like-named
1532 method parameter, as specified in `tramp-methods' (which see).")
1534 (defvar tramp-encoding-command nil
1535 "This internal variable holds a parameter for `tramp-methods'.
1536 In the connection buffer, this variable has the value of the like-named
1537 method parameter, as specified in `tramp-methods' (which see).")
1539 (defvar tramp-decoding-command nil
1540 "This internal variable holds a parameter for `tramp-methods'.
1541 In the connection buffer, this variable has the value of the like-named
1542 method parameter, as specified in `tramp-methods' (which see).")
1544 (defvar tramp-encoding-function nil
1545 "This internal variable holds a parameter for `tramp-methods'.
1546 In the connection buffer, this variable has the value of the like-named
1547 method parameter, as specified in `tramp-methods' (which see).")
1549 (defvar tramp-decoding-function nil
1550 "This internal variable holds a parameter for `tramp-methods'.
1551 In the connection buffer, this variable has the value of the like-named
1552 method parameter, as specified in `tramp-methods' (which see).")
1554 (defvar tramp-password-end-of-line nil
1555 "This internal variable holds a parameter for `tramp-methods'.
1556 In the connection buffer, this variable has the value of the like-named
1557 method parameter, as specified in `tramp-methods' (which see).")
1559 ;; CCC `local in each buffer'?
1560 (defvar tramp-ls-command nil
1561 "This command is used to get a long listing with numeric user and group ids.
1562 This variable is automatically made buffer-local to each rsh process buffer
1563 upon opening the connection.")
1565 (defvar tramp-current-multi-method nil
1566 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1567 This variable is automatically made buffer-local to each rsh process buffer
1568 upon opening the connection.")
1570 (defvar tramp-current-method nil
1571 "Connection method for this *tramp* buffer.
1572 This variable is automatically made buffer-local to each rsh process buffer
1573 upon opening the connection.")
1575 (defvar tramp-current-user nil
1576 "Remote login name for this *tramp* buffer.
1577 This variable is automatically made buffer-local to each rsh process buffer
1578 upon opening the connection.")
1580 (defvar tramp-current-host nil
1581 "Remote host for this *tramp* buffer.
1582 This variable is automatically made buffer-local to each rsh process buffer
1583 upon opening the connection.")
1585 (defvar tramp-test-groks-nt nil
1586 "Whether the `test' command groks the `-nt' switch.
1587 \(`test A -nt B' tests if file A is newer than file B.)
1588 This variable is automatically made buffer-local to each rsh process buffer
1589 upon opening the connection.")
1591 (defvar tramp-file-exists-command nil
1592 "Command to use for checking if a file exists.
1593 This variable is automatically made buffer-local to each rsh process buffer
1594 upon opening the connection.")
1596 (defconst tramp-uudecode "\
1597 tramp_uudecode () {
1598 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1599 cat /tmp/tramp.$$
1600 rm -f /tmp/tramp.$$
1602 "Shell function to implement `uudecode' to standard output.
1603 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1604 for this or `uudecode -p', but some systems don't, and for them
1605 we have this shell function.")
1607 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1608 ;; output. If you are hacking on this, note that you get *no* output
1609 ;; unless this spits out a complete line, including the '\n' at the
1610 ;; end.
1611 ;; The device number is returned as "-1", because there will be a virtual
1612 ;; device number set in `tramp-handle-file-attributes'
1613 (defconst tramp-perl-file-attributes "\
1614 @stat = lstat($ARGV[0]);
1615 if (($stat[2] & 0170000) == 0120000)
1617 $type = readlink($ARGV[0]);
1618 $type = \"\\\"$type\\\"\";
1620 elsif (($stat[2] & 0170000) == 040000)
1622 $type = \"t\";
1624 else
1626 $type = \"nil\"
1628 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1629 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1630 printf(
1631 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1632 $type,
1633 $stat[3],
1634 $uid,
1635 $gid,
1636 $stat[8] >> 16 & 0xffff,
1637 $stat[8] & 0xffff,
1638 $stat[9] >> 16 & 0xffff,
1639 $stat[9] & 0xffff,
1640 $stat[10] >> 16 & 0xffff,
1641 $stat[10] & 0xffff,
1642 $stat[7],
1643 $stat[2],
1644 $stat[1] >> 16 & 0xffff,
1645 $stat[1] & 0xffff
1647 "Perl script to produce output suitable for use with `file-attributes'
1648 on the remote file system.")
1650 (defconst tramp-perl-directory-files-and-attributes "\
1651 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1652 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1653 @list = readdir(DIR);
1654 closedir(DIR);
1655 $n = scalar(@list);
1656 printf(\"(\\n\");
1657 for($i = 0; $i < $n; $i++)
1659 $filename = $list[$i];
1660 @stat = lstat($filename);
1661 if (($stat[2] & 0170000) == 0120000)
1663 $type = readlink($filename);
1664 $type = \"\\\"$type\\\"\";
1666 elsif (($stat[2] & 0170000) == 040000)
1668 $type = \"t\";
1670 else
1672 $type = \"nil\"
1674 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1675 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1676 printf(
1677 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1678 $filename,
1679 $type,
1680 $stat[3],
1681 $uid,
1682 $gid,
1683 $stat[8] >> 16 & 0xffff,
1684 $stat[8] & 0xffff,
1685 $stat[9] >> 16 & 0xffff,
1686 $stat[9] & 0xffff,
1687 $stat[10] >> 16 & 0xffff,
1688 $stat[10] & 0xffff,
1689 $stat[7],
1690 $stat[2],
1691 $stat[1] >> 16 & 0xffff,
1692 $stat[1] & 0xffff,
1693 $stat[0] >> 16 & 0xffff,
1694 $stat[0] & 0xffff);
1696 printf(\")\\n\");"
1697 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1698 output.")
1700 ;; ;; These two use uu encoding.
1701 ;; (defvar tramp-perl-encode "%s -e'\
1702 ;; print qq(begin 644 xxx\n);
1703 ;; my $s = q();
1704 ;; my $res = q();
1705 ;; while (read(STDIN, $s, 45)) {
1706 ;; print pack(q(u), $s);
1707 ;; }
1708 ;; print qq(`\n);
1709 ;; print qq(end\n);
1710 ;; '"
1711 ;; "Perl program to use for encoding a file.
1712 ;; Escape sequence %s is replaced with name of Perl binary.")
1714 ;; (defvar tramp-perl-decode "%s -ne '
1715 ;; print unpack q(u), $_;
1716 ;; '"
1717 ;; "Perl program to use for decoding a file.
1718 ;; Escape sequence %s is replaced with name of Perl binary.")
1720 ;; These two use base64 encoding.
1721 (defvar tramp-perl-encode-with-module
1722 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1723 "Perl program to use for encoding a file.
1724 Escape sequence %s is replaced with name of Perl binary.
1725 This string is passed to `format', so percent characters need to be doubled.
1726 This implementation requires the MIME::Base64 Perl module to be installed
1727 on the remote host.")
1729 (defvar tramp-perl-decode-with-module
1730 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1731 "Perl program to use for decoding a file.
1732 Escape sequence %s is replaced with name of Perl binary.
1733 This string is passed to `format', so percent characters need to be doubled.
1734 This implementation requires the MIME::Base64 Perl module to be installed
1735 on the remote host.")
1737 (defvar tramp-perl-encode
1738 "%s -e '
1739 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1740 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1741 use strict;
1743 my %%trans = do {
1744 my $i = 0;
1745 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1746 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1749 binmode(\\*STDIN);
1751 # We read in chunks of 54 bytes, to generate output lines
1752 # of 72 chars (plus end of line)
1753 $/ = \\54;
1755 while (my $data = <STDIN>) {
1756 my $pad = q();
1758 # Only for the last chunk, and only if did not fill the last three-byte packet
1759 if (eof) {
1760 my $mod = length($data) %% 3;
1761 $pad = q(=) x (3 - $mod) if $mod;
1764 # Not the fastest method, but it is simple: unpack to binary string, split
1765 # by groups of 6 bits and convert back from binary to byte; then map into
1766 # the translation table
1767 print
1768 join q(),
1769 map($trans{$_},
1770 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1771 $pad,
1772 qq(\\n);
1775 "Perl program to use for encoding a file.
1776 Escape sequence %s is replaced with name of Perl binary.
1777 This string is passed to `format', so percent characters need to be doubled.")
1779 (defvar tramp-perl-decode
1780 "%s -e '
1781 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1782 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1783 use strict;
1785 my %%trans = do {
1786 my $i = 0;
1787 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1788 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1791 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1793 binmode(\\*STDOUT);
1795 # We are going to accumulate into $pending to accept any line length
1796 # (we do not check they are <= 76 chars as the RFC says)
1797 my $pending = q();
1799 while (my $data = <STDIN>) {
1800 chomp $data;
1802 # If we find one or two =, we have reached the end and
1803 # any following data is to be discarded
1804 my $finished = $data =~ s/(==?).*/$1/;
1805 $pending .= $data;
1807 my $len = length($pending);
1808 my $chunk = substr($pending, 0, $len & ~3);
1809 $pending = substr($pending, $len & ~3 + 1);
1811 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1812 # split in 8-bit chunks and convert back to char.
1813 print join q(),
1814 map $bytes{$_},
1815 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1817 last if $finished;
1820 "Perl program to use for decoding a file.
1821 Escape sequence %s is replaced with name of Perl binary.
1822 This string is passed to `format', so percent characters need to be doubled.")
1824 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1825 (1 . "p") ; fifo
1826 (2 . "c") ; character device
1827 (3 . "m") ; multiplexed character device (v7)
1828 (4 . "d") ; directory
1829 (5 . "?") ; Named special file (XENIX)
1830 (6 . "b") ; block device
1831 (7 . "?") ; multiplexed block device (v7)
1832 (8 . "-") ; regular file
1833 (9 . "n") ; network special file (HP-UX)
1834 (10 . "l") ; symlink
1835 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1836 (12 . "s") ; socket
1837 (13 . "D") ; door special (Solaris)
1838 (14 . "w")) ; whiteout (BSD)
1839 "A list of file types returned from the `stat' system call.
1840 This is used to map a mode number to a permission string.")
1842 (defvar tramp-dos-coding-system
1843 (if (and (fboundp 'coding-system-p)
1844 (funcall 'coding-system-p '(dos)))
1845 'dos
1846 'undecided-dos)
1847 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1849 (defvar tramp-last-cmd nil
1850 "Internal Tramp variable recording the last command sent.
1851 This variable is buffer-local in every buffer.")
1852 (make-variable-buffer-local 'tramp-last-cmd)
1854 (defvar tramp-process-echoes nil
1855 "Whether to process echoes from the remote shell.")
1857 (defvar tramp-last-cmd-time nil
1858 "Internal Tramp variable recording the time when the last cmd was sent.
1859 This variable is buffer-local in every buffer.")
1860 (make-variable-buffer-local 'tramp-last-cmd-time)
1862 ;; This variable does not have the right value in XEmacs. What should
1863 ;; I use instead of find-operation-coding-system in XEmacs?
1864 (defvar tramp-feature-write-region-fix
1865 (when (fboundp 'find-operation-coding-system)
1866 (let ((file-coding-system-alist '(("test" emacs-mule))))
1867 (funcall (symbol-function 'find-operation-coding-system)
1868 'write-region 0 0 "" nil "test")))
1869 "Internal variable to say if `write-region' chooses the right coding.
1870 Older versions of Emacs chose the coding system for `write-region' based
1871 on the FILENAME argument, even if VISIT was a string.")
1873 ;; New handlers should be added here. The following operations can be
1874 ;; handled using the normal primitives: file-name-as-directory,
1875 ;; file-name-directory, file-name-nondirectory,
1876 ;; file-name-sans-versions, get-file-buffer.
1877 (defconst tramp-file-name-handler-alist
1879 (load . tramp-handle-load)
1880 (make-symbolic-link . tramp-handle-make-symbolic-link)
1881 (file-name-directory . tramp-handle-file-name-directory)
1882 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1883 (file-truename . tramp-handle-file-truename)
1884 (file-exists-p . tramp-handle-file-exists-p)
1885 (file-directory-p . tramp-handle-file-directory-p)
1886 (file-executable-p . tramp-handle-file-executable-p)
1887 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1888 (file-readable-p . tramp-handle-file-readable-p)
1889 (file-regular-p . tramp-handle-file-regular-p)
1890 (file-symlink-p . tramp-handle-file-symlink-p)
1891 (file-writable-p . tramp-handle-file-writable-p)
1892 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1893 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1894 (file-attributes . tramp-handle-file-attributes)
1895 (file-modes . tramp-handle-file-modes)
1896 (directory-files . tramp-handle-directory-files)
1897 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1898 (file-name-all-completions . tramp-handle-file-name-all-completions)
1899 (file-name-completion . tramp-handle-file-name-completion)
1900 (add-name-to-file . tramp-handle-add-name-to-file)
1901 (copy-file . tramp-handle-copy-file)
1902 (rename-file . tramp-handle-rename-file)
1903 (set-file-modes . tramp-handle-set-file-modes)
1904 (make-directory . tramp-handle-make-directory)
1905 (delete-directory . tramp-handle-delete-directory)
1906 (delete-file . tramp-handle-delete-file)
1907 (directory-file-name . tramp-handle-directory-file-name)
1908 (shell-command . tramp-handle-shell-command)
1909 (process-file . tramp-handle-process-file)
1910 (insert-directory . tramp-handle-insert-directory)
1911 (expand-file-name . tramp-handle-expand-file-name)
1912 (file-local-copy . tramp-handle-file-local-copy)
1913 (file-remote-p . tramp-handle-file-remote-p)
1914 (insert-file-contents . tramp-handle-insert-file-contents)
1915 (write-region . tramp-handle-write-region)
1916 (find-backup-file-name . tramp-handle-find-backup-file-name)
1917 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1918 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1919 (dired-compress-file . tramp-handle-dired-compress-file)
1920 (dired-call-process . tramp-handle-dired-call-process)
1921 (dired-recursive-delete-directory
1922 . tramp-handle-dired-recursive-delete-directory)
1923 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1924 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1925 "Alist of handler functions.
1926 Operations not mentioned here will be handled by the normal Emacs functions.")
1928 ;; Handlers for partial tramp file names. For Emacs just
1929 ;; `file-name-all-completions' is needed.
1930 ;;;###autoload
1931 (defconst tramp-completion-file-name-handler-alist
1932 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1933 (file-name-completion . tramp-completion-handle-file-name-completion))
1934 "Alist of completion handler functions.
1935 Used for file names matching `tramp-file-name-regexp'. Operations not
1936 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1937 normal Emacs functions.")
1939 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1940 (defvar tramp-foreign-file-name-handler-alist
1941 ;; (identity . tramp-sh-file-name-handler) should always be the last
1942 ;; entry, since `identity' always matches.
1943 '((identity . tramp-sh-file-name-handler))
1944 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1945 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1946 calling HANDLER.")
1948 ;;; Internal functions which must come first.
1950 (defsubst tramp-message (level fmt-string &rest args)
1951 "Emit a message depending on verbosity level.
1952 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1953 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1954 Calls function `message' with FMT-STRING as control string and the remaining
1955 ARGS to actually emit the message (if applicable).
1957 This function expects to be called from the tramp buffer only!"
1958 (when (<= level tramp-verbose)
1959 (apply #'message (concat "tramp: " fmt-string) args)
1960 (when tramp-debug-buffer
1961 (save-excursion
1962 (set-buffer
1963 (tramp-get-debug-buffer
1964 tramp-current-multi-method tramp-current-method
1965 tramp-current-user tramp-current-host))
1966 (goto-char (point-max))
1967 (unless (bolp)
1968 (insert "\n"))
1969 (tramp-insert-with-face
1970 'italic
1971 (concat "# " (apply #'format fmt-string args) "\n"))))))
1973 (defun tramp-message-for-buffer
1974 (multi-method method user host level fmt-string &rest args)
1975 "Like `tramp-message' but temporarily switches to the tramp buffer.
1976 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1977 remaining args passed to `tramp-message'."
1978 (save-excursion
1979 (set-buffer (tramp-get-buffer multi-method method user host))
1980 (apply 'tramp-message level fmt-string args)))
1982 (defsubst tramp-line-end-position nil
1983 "Return point at end of line.
1984 Calls `line-end-position' or `point-at-eol' if defined, else
1985 own implementation."
1986 (cond
1987 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1988 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1989 (t (save-excursion (end-of-line) (point)))))
1991 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1992 "Parse a Tramp filename and make components available in the body.
1994 First arg FILENAME is evaluated and dissected into its components.
1995 Second arg VAR is a symbol. It is used as a variable name to hold
1996 the filename structure. It is also used as a prefix for the variables
1997 holding the components. For example, if VAR is the symbol `foo', then
1998 `foo' will be bound to the whole structure, `foo-multi-method' will
1999 be bound to the multi-method component, and so on for `foo-method',
2000 `foo-user', `foo-host', `foo-localname'.
2002 Remaining args are Lisp expressions to be evaluated (inside an implicit
2003 `progn').
2005 If VAR is nil, then we bind `v' to the structure and `multi-method',
2006 `method', `user', `host', `localname' to the components."
2007 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2008 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2009 (tramp-file-name-multi-method ,(or var 'v)))
2010 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2011 (tramp-file-name-method ,(or var 'v)))
2012 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2013 (tramp-file-name-user ,(or var 'v)))
2014 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2015 (tramp-file-name-host ,(or var 'v)))
2016 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2017 (tramp-file-name-localname ,(or var 'v))))
2018 ,@body))
2020 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2021 ;; To be activated for debugging containing this macro
2022 ;; It works only when VAR is nil. Otherwise, it can be deactivated by
2023 ;; (put 'with-parsed-tramp-file-name 'edebug-form-spec 0)
2024 ;; I'm too stupid to write a precise SPEC for it.
2025 (put 'with-parsed-tramp-file-name 'edebug-form-spec t)
2027 (defmacro tramp-let-maybe (variable value &rest body)
2028 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2029 BODY is executed whether or not the variable is obsolete.
2030 The intent is to protect against `obsolete variable' warnings."
2031 `(if (get ',variable 'byte-obsolete-variable)
2032 (progn ,@body)
2033 (let ((,variable ,value))
2034 ,@body)))
2035 (put 'tramp-let-maybe 'lisp-indent-function 2)
2036 (put 'tramp-let-maybe 'edebug-form-spec t)
2038 ;;; Config Manipulation Functions:
2040 (defun tramp-set-completion-function (method function-list)
2041 "Sets the list of completion functions for METHOD.
2042 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2043 The FUNCTION is intended to parse FILE according its syntax.
2044 It might be a predefined FUNCTION, or a user defined FUNCTION.
2045 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2046 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2047 and `tramp-parse-netrc'.
2049 Example:
2051 (tramp-set-completion-function
2052 \"ssh\"
2053 '((tramp-parse-sconfig \"/etc/ssh_config\")
2054 (tramp-parse-sconfig \"~/.ssh/config\")))"
2056 (let ((r function-list)
2057 (v function-list))
2058 (setq tramp-completion-function-alist
2059 (delete (assoc method tramp-completion-function-alist)
2060 tramp-completion-function-alist))
2062 (while v
2063 ;; Remove double entries
2064 (when (member (car v) (cdr v))
2065 (setcdr v (delete (car v) (cdr v))))
2066 ;; Check for function and file
2067 (unless (and (functionp (nth 0 (car v)))
2068 (file-exists-p (nth 1 (car v))))
2069 (setq r (delete (car v) r)))
2070 (setq v (cdr v)))
2072 (when r
2073 (add-to-list 'tramp-completion-function-alist
2074 (cons method r)))))
2076 (defun tramp-get-completion-function (method)
2077 "Returns list of completion functions for METHOD.
2078 For definition of that list see `tramp-set-completion-function'."
2079 (cdr (assoc method tramp-completion-function-alist)))
2081 ;;; File Name Handler Functions:
2083 (defun tramp-handle-make-symbolic-link
2084 (filename linkname &optional ok-if-already-exists)
2085 "Like `make-symbolic-link' for tramp files.
2086 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2087 the symlink. If LINKNAME is a Tramp file, only the localname component is
2088 used as the target of the symlink.
2090 If LINKNAME is a Tramp file and the localname component is relative, then
2091 it is expanded first, before the localname component is taken. Note that
2092 this can give surprising results if the user/host for the source and
2093 target of the symlink differ."
2094 (with-parsed-tramp-file-name linkname l
2095 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2096 (cwd (file-name-directory l-localname)))
2097 (unless ln
2098 (signal 'file-error
2099 (list "Making a symbolic link."
2100 "ln(1) does not exist on the remote host.")))
2102 ;; Do the 'confirm if exists' thing.
2103 (when (file-exists-p linkname)
2104 ;; What to do?
2105 (if (or (null ok-if-already-exists) ; not allowed to exist
2106 (and (numberp ok-if-already-exists)
2107 (not (yes-or-no-p
2108 (format
2109 "File %s already exists; make it a link anyway? "
2110 l-localname)))))
2111 (signal 'file-already-exists (list "File already exists" l-localname))
2112 (delete-file linkname)))
2114 ;; If FILENAME is a Tramp name, use just the localname component.
2115 (when (tramp-tramp-file-p filename)
2116 (setq filename (tramp-file-name-localname
2117 (tramp-dissect-file-name
2118 (expand-file-name filename)))))
2120 ;; Right, they are on the same host, regardless of user, method, etc.
2121 ;; We now make the link on the remote machine. This will occur as the user
2122 ;; that FILENAME belongs to.
2123 (zerop
2124 (tramp-send-command-and-check
2125 l-multi-method l-method l-user l-host
2126 (format "cd %s && %s -sf %s %s"
2127 cwd ln
2128 filename
2129 l-localname)
2130 t)))))
2133 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2134 "Like `load' for tramp files. Not implemented!"
2135 (unless (file-name-absolute-p file)
2136 (error "Tramp cannot `load' files without absolute file name"))
2137 (with-parsed-tramp-file-name file nil
2138 (unless nosuffix
2139 (cond ((file-exists-p (concat file ".elc"))
2140 (setq file (concat file ".elc")))
2141 ((file-exists-p (concat file ".el"))
2142 (setq file (concat file ".el")))))
2143 (when must-suffix
2144 ;; The first condition is always true for absolute file names.
2145 ;; Included for safety's sake.
2146 (unless (or (file-name-directory file)
2147 (string-match "\\.elc?\\'" file))
2148 (error "File `%s' does not include a `.el' or `.elc' suffix"
2149 file)))
2150 (unless noerror
2151 (when (not (file-exists-p file))
2152 (error "Cannot load nonexistent file `%s'" file)))
2153 (if (not (file-exists-p file))
2155 (unless nomessage
2156 (message "Loading %s..." file))
2157 (let ((local-copy (file-local-copy file)))
2158 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2159 (load local-copy noerror t t)
2160 (delete-file local-copy))
2161 (unless nomessage
2162 (message "Loading %s...done" file))
2163 t)))
2165 ;; Localname manipulation functions that grok TRAMP localnames...
2166 (defun tramp-handle-file-name-directory (file)
2167 "Like `file-name-directory' but aware of TRAMP files."
2168 ;; Everything except the last filename thing is the directory.
2169 (with-parsed-tramp-file-name file nil
2170 ;; Run the command on the localname portion only.
2171 (tramp-make-tramp-file-name
2172 multi-method method user host (file-name-directory (or localname "")))))
2174 (defun tramp-handle-file-name-nondirectory (file)
2175 "Like `file-name-nondirectory' but aware of TRAMP files."
2176 (with-parsed-tramp-file-name file nil
2177 (file-name-nondirectory localname)))
2179 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2180 "Like `file-truename' for tramp files."
2181 (with-parsed-tramp-file-name (expand-file-name filename) nil
2182 (let* ((steps (tramp-split-string localname "/"))
2183 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2184 (file-name-as-directory localname)))
2185 (is-dir (string= localname localnamedir))
2186 (thisstep nil)
2187 (numchase 0)
2188 ;; Don't make the following value larger than necessary.
2189 ;; People expect an error message in a timely fashion when
2190 ;; something is wrong; otherwise they might think that Emacs
2191 ;; is hung. Of course, correctness has to come first.
2192 (numchase-limit 20)
2193 (result nil) ;result steps in reverse order
2194 symlink-target)
2195 (tramp-message-for-buffer
2196 multi-method method user host
2197 10 "Finding true name for `%s'" filename)
2198 (while (and steps (< numchase numchase-limit))
2199 (setq thisstep (pop steps))
2200 (tramp-message-for-buffer
2201 multi-method method user host
2202 10 "Check %s"
2203 (mapconcat 'identity
2204 (append '("") (reverse result) (list thisstep))
2205 "/"))
2206 (setq symlink-target
2207 (nth 0 (file-attributes
2208 (tramp-make-tramp-file-name
2209 multi-method method user host
2210 (mapconcat 'identity
2211 (append '("")
2212 (reverse result)
2213 (list thisstep))
2214 "/")))))
2215 (cond ((string= "." thisstep)
2216 (tramp-message-for-buffer multi-method method user host
2217 10 "Ignoring step `.'"))
2218 ((string= ".." thisstep)
2219 (tramp-message-for-buffer multi-method method user host
2220 10 "Processing step `..'")
2221 (pop result))
2222 ((stringp symlink-target)
2223 ;; It's a symlink, follow it.
2224 (tramp-message-for-buffer
2225 multi-method method user host
2226 10 "Follow symlink to %s" symlink-target)
2227 (setq numchase (1+ numchase))
2228 (when (file-name-absolute-p symlink-target)
2229 (setq result nil))
2230 ;; If the symlink was absolute, we'll get a string like
2231 ;; "/user@host:/some/target"; extract the
2232 ;; "/some/target" part from it.
2233 (when (tramp-tramp-file-p symlink-target)
2234 (with-parsed-tramp-file-name symlink-target sym
2235 (unless (equal (list multi-method method user host)
2236 (list sym-multi-method sym-method
2237 sym-user sym-host))
2238 (error "Symlink target `%s' on wrong host"
2239 symlink-target))
2240 (setq symlink-target localname)))
2241 (setq steps
2242 (append (tramp-split-string symlink-target "/") steps)))
2244 ;; It's a file.
2245 (setq result (cons thisstep result)))))
2246 (when (>= numchase numchase-limit)
2247 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2248 (setq result (reverse result))
2249 ;; Combine list to form string.
2250 (setq result
2251 (if result
2252 (mapconcat 'identity (cons "" result) "/")
2253 "/"))
2254 (when (and is-dir (or (string= "" result)
2255 (not (string= (substring result -1) "/"))))
2256 (setq result (concat result "/")))
2257 (tramp-message-for-buffer
2258 multi-method method user host
2259 10 "True name of `%s' is `%s'" filename result)
2260 (tramp-make-tramp-file-name
2261 multi-method method user host result))))
2263 ;; Basic functions.
2265 (defun tramp-handle-file-exists-p (filename)
2266 "Like `file-exists-p' for tramp files."
2267 (with-parsed-tramp-file-name filename nil
2268 (save-excursion
2269 (zerop (tramp-send-command-and-check
2270 multi-method method user host
2271 (format
2272 (tramp-get-file-exists-command multi-method method user host)
2273 (tramp-shell-quote-argument localname)))))))
2275 ;; Devices must distinguish physical file systems. The device numbers
2276 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2277 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2278 ;; EFS use device number "-1". In order to be different, we use device number
2279 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2280 (defvar tramp-devices nil
2281 "Keeps virtual device numbers.")
2283 ;; CCC: This should check for an error condition and signal failure
2284 ;; when something goes wrong.
2285 ;; Daniel Pittman <daniel@danann.net>
2286 (defun tramp-handle-file-attributes (filename &optional id-format)
2287 "Like `file-attributes' for tramp files."
2288 (when (file-exists-p filename)
2289 ;; file exists, find out stuff
2290 (unless id-format (setq id-format 'integer))
2291 (with-parsed-tramp-file-name filename nil
2292 (save-excursion
2293 (tramp-convert-file-attributes
2294 multi-method method user host
2295 (if (tramp-get-remote-perl multi-method method user host)
2296 (tramp-handle-file-attributes-with-perl multi-method method user host
2297 localname id-format)
2298 (tramp-handle-file-attributes-with-ls multi-method method user host
2299 localname id-format)))))))
2301 (defun tramp-handle-file-attributes-with-ls
2302 (multi-method method user host localname &optional id-format)
2303 "Implement `file-attributes' for tramp files using the ls(1) command."
2304 (let (symlinkp dirp
2305 res-inode res-filemodes res-numlinks
2306 res-uid res-gid res-size res-symlink-target)
2307 (tramp-message-for-buffer multi-method method user host 10
2308 "file attributes with ls: %s"
2309 (tramp-make-tramp-file-name
2310 multi-method method user host localname))
2311 (tramp-send-command
2312 multi-method method user host
2313 (format "%s %s %s"
2314 (tramp-get-ls-command multi-method method user host)
2315 (if (eq id-format 'integer) "-ildn" "-ild")
2316 (tramp-shell-quote-argument localname)))
2317 (tramp-wait-for-output)
2318 ;; parse `ls -l' output ...
2319 ;; ... inode
2320 (setq res-inode
2321 (condition-case err
2322 (read (current-buffer))
2323 (invalid-read-syntax
2324 (when (and (equal (cadr err)
2325 "Integer constant overflow in reader")
2326 (string-match
2327 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2328 (caddr err)))
2329 (let* ((big (read (substring (caddr err) 0
2330 (match-beginning 1))))
2331 (small (read (match-string 1 (caddr err))))
2332 (twiddle (/ small 65536)))
2333 (cons (+ big twiddle)
2334 (- small (* twiddle 65536))))))))
2335 ;; ... file mode flags
2336 (setq res-filemodes (symbol-name (read (current-buffer))))
2337 ;; ... number links
2338 (setq res-numlinks (read (current-buffer)))
2339 ;; ... uid and gid
2340 (setq res-uid (read (current-buffer)))
2341 (setq res-gid (read (current-buffer)))
2342 (when (eq id-format 'integer)
2343 (unless (numberp res-uid) (setq res-uid -1))
2344 (unless (numberp res-gid) (setq res-gid -1)))
2345 ;; ... size
2346 (setq res-size (read (current-buffer)))
2347 ;; From the file modes, figure out other stuff.
2348 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2349 (setq dirp (eq ?d (aref res-filemodes 0)))
2350 ;; if symlink, find out file name pointed to
2351 (when symlinkp
2352 (search-forward "-> ")
2353 (setq res-symlink-target
2354 (buffer-substring (point)
2355 (tramp-line-end-position))))
2356 ;; return data gathered
2357 (list
2358 ;; 0. t for directory, string (name linked to) for symbolic
2359 ;; link, or nil.
2360 (or dirp res-symlink-target nil)
2361 ;; 1. Number of links to file.
2362 res-numlinks
2363 ;; 2. File uid.
2364 res-uid
2365 ;; 3. File gid.
2366 res-gid
2367 ;; 4. Last access time, as a list of two integers. First
2368 ;; integer has high-order 16 bits of time, second has low 16
2369 ;; bits.
2370 ;; 5. Last modification time, likewise.
2371 ;; 6. Last status change time, likewise.
2372 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2373 ;; 7. Size in bytes (-1, if number is out of range).
2374 res-size
2375 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2376 res-filemodes
2377 ;; 9. t iff file's gid would change if file were deleted and
2378 ;; recreated. Will be set in `tramp-convert-file-attributes'
2380 ;; 10. inode number.
2381 res-inode
2382 ;; 11. Device number. Will be replaced by a virtual device number.
2386 (defun tramp-handle-file-attributes-with-perl
2387 (multi-method method user host localname &optional id-format)
2388 "Implement `file-attributes' for tramp files using a Perl script."
2389 (tramp-message-for-buffer multi-method method user host 10
2390 "file attributes with perl: %s"
2391 (tramp-make-tramp-file-name
2392 multi-method method user host localname))
2393 (tramp-maybe-send-perl-script multi-method method user host
2394 tramp-perl-file-attributes
2395 "tramp_file_attributes")
2396 (tramp-send-command multi-method method user host
2397 (format "tramp_file_attributes %s %s"
2398 (tramp-shell-quote-argument localname) id-format))
2399 (tramp-wait-for-output)
2400 (read (current-buffer)))
2402 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2403 "Like `set-visited-file-modtime' for tramp files."
2404 (unless (buffer-file-name)
2405 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2406 (buffer-name)))
2407 (if time-list
2408 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2409 (let ((f (buffer-file-name))
2410 coding-system-used)
2411 (with-parsed-tramp-file-name f nil
2412 (let* ((attr (file-attributes f))
2413 ;; '(-1 65535) means file doesn't exists yet.
2414 (modtime (or (nth 5 attr) '(-1 65535))))
2415 (when (boundp 'last-coding-system-used)
2416 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2417 ;; We use '(0 0) as a don't-know value. See also
2418 ;; `tramp-handle-file-attributes-with-ls'.
2419 (if (not (equal modtime '(0 0)))
2420 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2421 (save-excursion
2422 (tramp-send-command
2423 multi-method method user host
2424 (format "%s -ild %s"
2425 (tramp-get-ls-command multi-method method user host)
2426 (tramp-shell-quote-argument localname)))
2427 (tramp-wait-for-output)
2428 (setq attr (buffer-substring (point)
2429 (progn (end-of-line) (point)))))
2430 (setq tramp-buffer-file-attributes attr))
2431 (when (boundp 'last-coding-system-used)
2432 (set 'last-coding-system-used coding-system-used))
2433 nil)))))
2435 ;; CCC continue here
2437 ;; This function makes the same assumption as
2438 ;; `tramp-handle-set-visited-file-modtime'.
2439 (defun tramp-handle-verify-visited-file-modtime (buf)
2440 "Like `verify-visited-file-modtime' for tramp files.
2441 At the time `verify-visited-file-modtime' calls this function, we
2442 already know that the buffer is visiting a file and that
2443 `visited-file-modtime' does not return 0. Do not call this
2444 function directly, unless those two cases are already taken care
2445 of."
2446 (with-current-buffer buf
2447 ;; There is no file visiting the buffer, or the buffer has no
2448 ;; recorded last modification time.
2449 (if (or (not (buffer-file-name))
2450 (eq (visited-file-modtime) 0))
2452 (let ((f (buffer-file-name)))
2453 (with-parsed-tramp-file-name f nil
2454 (let* ((attr (file-attributes f))
2455 (modtime (nth 5 attr))
2456 (mt (visited-file-modtime)))
2458 (cond
2459 ;; file exists, and has a known modtime.
2460 ((and attr (not (equal modtime '(0 0))))
2461 (< (abs (tramp-time-diff
2462 modtime
2463 ;; For compatibility, deal with both the old
2464 ;; (HIGH . LOW) and the new (HIGH LOW)
2465 ;; return values of `visited-file-modtime'.
2466 (if (atom (cdr mt))
2467 (list (car mt) (cdr mt))
2468 mt)))
2470 ;; modtime has the don't know value.
2471 (attr
2472 (save-excursion
2473 (tramp-send-command
2474 multi-method method user host
2475 (format "%s -ild %s"
2476 (tramp-get-ls-command multi-method method user host)
2477 (tramp-shell-quote-argument localname)))
2478 (tramp-wait-for-output)
2479 (setq attr (buffer-substring
2480 (point) (progn (end-of-line) (point)))))
2481 (equal tramp-buffer-file-attributes attr))
2482 ;; If file does not exist, say it is not modified
2483 ;; if and only if that agrees with the buffer's record.
2484 (t (equal mt '(-1 65535))))))))))
2486 (defun tramp-handle-set-file-modes (filename mode)
2487 "Like `set-file-modes' for tramp files."
2488 (with-parsed-tramp-file-name filename nil
2489 (save-excursion
2490 (unless (zerop (tramp-send-command-and-check
2491 multi-method method user host
2492 (format "chmod %s %s"
2493 (tramp-decimal-to-octal mode)
2494 (tramp-shell-quote-argument localname))))
2495 (signal 'file-error
2496 (list "Doing chmod"
2497 ;; FIXME: extract the proper text from chmod's stderr.
2498 "error while changing file's mode"
2499 filename))))))
2501 ;; Simple functions using the `test' command.
2503 (defun tramp-handle-file-executable-p (filename)
2504 "Like `file-executable-p' for tramp files."
2505 (with-parsed-tramp-file-name filename nil
2506 (zerop (tramp-run-test "-x" filename))))
2508 (defun tramp-handle-file-readable-p (filename)
2509 "Like `file-readable-p' for tramp files."
2510 (with-parsed-tramp-file-name filename nil
2511 (zerop (tramp-run-test "-r" filename))))
2513 (defun tramp-handle-file-accessible-directory-p (filename)
2514 "Like `file-accessible-directory-p' for tramp files."
2515 (with-parsed-tramp-file-name filename nil
2516 (and (zerop (tramp-run-test "-d" filename))
2517 (zerop (tramp-run-test "-r" filename))
2518 (zerop (tramp-run-test "-x" filename)))))
2520 ;; When the remote shell is started, it looks for a shell which groks
2521 ;; tilde expansion. Here, we assume that all shells which grok tilde
2522 ;; expansion will also provide a `test' command which groks `-nt' (for
2523 ;; newer than). If this breaks, tell me about it and I'll try to do
2524 ;; something smarter about it.
2525 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2526 "Like `file-newer-than-file-p' for tramp files."
2527 (cond ((not (file-exists-p file1))
2528 nil)
2529 ((not (file-exists-p file2))
2531 ;; We are sure both files exist at this point.
2533 (save-excursion
2534 ;; We try to get the mtime of both files. If they are not
2535 ;; equal to the "dont-know" value, then we subtract the times
2536 ;; and obtain the result.
2537 (let ((fa1 (file-attributes file1))
2538 (fa2 (file-attributes file2)))
2539 (if (and (not (equal (nth 5 fa1) '(0 0)))
2540 (not (equal (nth 5 fa2) '(0 0))))
2541 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2542 ;; If one of them is the dont-know value, then we can
2543 ;; still try to run a shell command on the remote host.
2544 ;; However, this only works if both files are Tramp
2545 ;; files and both have the same method, same user, same
2546 ;; host.
2547 (unless (and (tramp-tramp-file-p file1)
2548 (tramp-tramp-file-p file2))
2549 (signal
2550 'file-error
2551 (list
2552 "Cannot check if Tramp file is newer than non-Tramp file"
2553 file1 file2)))
2554 (with-parsed-tramp-file-name file1 v1
2555 (with-parsed-tramp-file-name file2 v2
2556 (unless (and (equal v1-multi-method v2-multi-method)
2557 (equal v1-method v2-method)
2558 (equal v1-user v2-user)
2559 (equal v1-host v2-host))
2560 (signal 'file-error
2561 (list "Files must have same method, user, host"
2562 file1 file2)))
2563 (unless (and (tramp-tramp-file-p file1)
2564 (tramp-tramp-file-p file2))
2565 (signal 'file-error
2566 (list "Files must be tramp files on same host"
2567 file1 file2)))
2568 (if (tramp-get-test-groks-nt
2569 v1-multi-method v1-method v1-user v1-host)
2570 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2571 (zerop (tramp-run-test2
2572 "tramp_test_nt" file1 file2)))))))))))
2574 ;; Functions implemented using the basic functions above.
2576 (defun tramp-handle-file-modes (filename)
2577 "Like `file-modes' for tramp files."
2578 (with-parsed-tramp-file-name filename nil
2579 (when (file-exists-p filename)
2580 (tramp-mode-string-to-int
2581 (nth 8 (file-attributes filename))))))
2583 (defun tramp-handle-file-directory-p (filename)
2584 "Like `file-directory-p' for tramp files."
2585 ;; Care must be taken that this function returns `t' for symlinks
2586 ;; pointing to directories. Surely the most obvious implementation
2587 ;; would be `test -d', but that returns false for such symlinks.
2588 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2589 ;; I now think he's right. So we could be using `test -d', couldn't
2590 ;; we?
2592 ;; Alternatives: `cd %s', `test -d %s'
2593 (with-parsed-tramp-file-name filename nil
2594 (save-excursion
2595 (zerop
2596 (tramp-send-command-and-check
2597 multi-method method user host
2598 (format "test -d %s"
2599 (tramp-shell-quote-argument localname))
2600 t))))) ;run command in subshell
2602 (defun tramp-handle-file-regular-p (filename)
2603 "Like `file-regular-p' for tramp files."
2604 (with-parsed-tramp-file-name filename nil
2605 (and (file-exists-p filename)
2606 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2608 (defun tramp-handle-file-symlink-p (filename)
2609 "Like `file-symlink-p' for tramp files."
2610 (with-parsed-tramp-file-name filename nil
2611 (let ((x (car (file-attributes filename))))
2612 (when (stringp x)
2613 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2614 ;; might do weird things.
2615 (if (file-name-absolute-p x)
2616 (tramp-make-tramp-file-name
2617 multi-method method user host x)
2618 x)))))
2620 (defun tramp-handle-file-writable-p (filename)
2621 "Like `file-writable-p' for tramp files."
2622 (with-parsed-tramp-file-name filename nil
2623 (if (file-exists-p filename)
2624 ;; Existing files must be writable.
2625 (zerop (tramp-run-test "-w" filename))
2626 ;; If file doesn't exist, check if directory is writable.
2627 (and (zerop (tramp-run-test
2628 "-d" (file-name-directory filename)))
2629 (zerop (tramp-run-test
2630 "-w" (file-name-directory filename)))))))
2632 (defun tramp-handle-file-ownership-preserved-p (filename)
2633 "Like `file-ownership-preserved-p' for tramp files."
2634 (with-parsed-tramp-file-name filename nil
2635 (let ((attributes (file-attributes filename)))
2636 ;; Return t if the file doesn't exist, since it's true that no
2637 ;; information would be lost by an (attempted) delete and create.
2638 (or (null attributes)
2639 (= (nth 2 attributes)
2640 (tramp-get-remote-uid multi-method method user host))))))
2642 ;; Other file name ops.
2644 ;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2645 ;; (defun tramp-handle-directory-file-name (directory)
2646 ;; "Like `directory-file-name' for tramp files."
2647 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2648 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2649 ;; (substring directory 0 (- (length directory) 1))
2650 ;; directory))
2652 ;; ;; Philippe Troin <phil@fifi.org>
2653 ;; (defun tramp-handle-directory-file-name (directory)
2654 ;; "Like `directory-file-name' for tramp files."
2655 ;; (with-parsed-tramp-file-name directory nil
2656 ;; (let ((directory-length-1 (1- (length directory))))
2657 ;; (save-match-data
2658 ;; (if (and (eq (aref directory directory-length-1) ?/)
2659 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2660 ;; (/= (match-end 0) directory-length-1))
2661 ;; (substring directory 0 directory-length-1)
2662 ;; directory)))))
2664 (defun tramp-handle-directory-file-name (directory)
2665 "Like `directory-file-name' for tramp files."
2666 ;; If localname component of filename is "/", leave it unchanged.
2667 ;; Otherwise, remove any trailing slash from localname component.
2668 ;; Method, host, etc, are unchanged. Does it make sense to try
2669 ;; to avoid parsing the filename?
2670 (with-parsed-tramp-file-name directory nil
2671 (if (and (not (zerop (length localname)))
2672 (eq (aref localname (1- (length localname))) ?/)
2673 (not (string= localname "/")))
2674 (substring directory 0 -1)
2675 directory)))
2677 ;; Directory listings.
2679 (defun tramp-handle-directory-files (directory
2680 &optional full match nosort files-only)
2681 "Like `directory-files' for tramp files."
2682 (with-parsed-tramp-file-name directory nil
2683 (let (result x)
2684 (save-excursion
2685 (tramp-barf-unless-okay
2686 multi-method method user host
2687 (concat "cd " (tramp-shell-quote-argument localname))
2689 'file-error
2690 "tramp-handle-directory-files: couldn't `cd %s'"
2691 (tramp-shell-quote-argument localname))
2692 (tramp-send-command
2693 multi-method method user host
2694 (concat (tramp-get-ls-command multi-method method user host)
2695 " -a | cat"))
2696 (tramp-wait-for-output)
2697 (goto-char (point-max))
2698 (while (zerop (forward-line -1))
2699 (setq x (buffer-substring (point)
2700 (tramp-line-end-position)))
2701 (when (or (not match) (string-match match x))
2702 (if full
2703 (push (concat (file-name-as-directory directory)
2705 result)
2706 (push x result))))
2707 (tramp-send-command multi-method method user host "cd")
2708 (tramp-wait-for-output)
2709 ;; Remove non-files or non-directories if necessary. Using
2710 ;; the remote shell for this would probably be way faster.
2711 ;; Maybe something could be adapted from
2712 ;; tramp-handle-file-name-all-completions.
2713 (when files-only
2714 (let ((temp (nreverse result))
2715 item)
2716 (setq result nil)
2717 (if (equal files-only t)
2718 ;; files only
2719 (while temp
2720 (setq item (pop temp))
2721 (when (file-regular-p item)
2722 (push item result)))
2723 ;; directories only
2724 (while temp
2725 (setq item (pop temp))
2726 (when (file-directory-p item)
2727 (push item result)))))))
2728 result)))
2730 (defun tramp-handle-directory-files-and-attributes
2731 (directory &optional full match nosort id-format)
2732 "Like `directory-files-and-attributes' for tramp files."
2733 (when (tramp-handle-file-exists-p directory)
2734 (save-excursion
2735 (setq directory (tramp-handle-expand-file-name directory))
2736 (with-parsed-tramp-file-name directory nil
2737 (tramp-maybe-send-perl-script multi-method method user host
2738 tramp-perl-directory-files-and-attributes
2739 "tramp_directory_files_and_attributes")
2740 (tramp-send-command multi-method method user host
2741 (format "tramp_directory_files_and_attributes %s %s"
2742 (tramp-shell-quote-argument localname)
2743 (or id-format 'integer)))
2744 (tramp-wait-for-output)
2745 (let* ((root (cons nil (let ((object (read (current-buffer))))
2746 (when (stringp object)
2747 (error object))
2748 object)))
2749 (cell root))
2750 (while (cdr cell)
2751 (if (and match (not (string-match match (caadr cell))))
2752 ;; Remove from list
2753 (setcdr cell (cddr cell))
2754 ;; Include in list
2755 (setq cell (cdr cell))
2756 (let ((l (car cell)))
2757 (tramp-convert-file-attributes multi-method method user host
2758 (cdr l))
2759 ;; If FULL, make file name absolute
2760 (when full (setcar l (concat directory "/" (car l)))))))
2761 (if nosort
2762 (cdr root)
2763 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2765 ;; This function should return "foo/" for directories and "bar" for
2766 ;; files. We use `ls -ad' to get a list of files (including
2767 ;; directories), and `find . -type d \! -name . -prune' to get a list
2768 ;; of directories.
2769 (defun tramp-handle-file-name-all-completions (filename directory)
2770 "Like `file-name-all-completions' for tramp files."
2771 (with-parsed-tramp-file-name directory nil
2772 (unless (save-match-data (string-match "/" filename))
2773 (let* ((nowild tramp-completion-without-shell-p)
2774 result)
2775 (save-excursion
2776 (tramp-barf-unless-okay
2777 multi-method method user host
2778 (format "cd %s" (tramp-shell-quote-argument localname))
2779 nil 'file-error
2780 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2781 (tramp-shell-quote-argument localname))
2783 ;; Get a list of directories and files, including reliably
2784 ;; tagging the directories with a trailing '/'. Because I
2785 ;; rock. --daniel@danann.net
2786 (tramp-send-command
2787 multi-method method user host
2788 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2789 "if test -d \"$f\" 2>/dev/null; "
2790 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2791 (tramp-get-ls-command multi-method method user host)
2792 (if (or nowild (zerop (length filename)))
2794 (format "-d %s*"
2795 (tramp-shell-quote-argument filename)))))
2797 ;; Now grab the output.
2798 (tramp-wait-for-output)
2799 (goto-char (point-max))
2800 (while (zerop (forward-line -1))
2801 (push (buffer-substring (point)
2802 (tramp-line-end-position))
2803 result))
2805 (tramp-send-command multi-method method user host "cd")
2806 (tramp-wait-for-output)
2808 ;; Return the list.
2809 (if nowild
2810 (all-completions filename (mapcar 'list result))
2811 result))))))
2814 ;; The following isn't needed for Emacs 20 but for 19.34?
2815 (defun tramp-handle-file-name-completion (filename directory)
2816 "Like `file-name-completion' for tramp files."
2817 (unless (tramp-tramp-file-p directory)
2818 (error
2819 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2820 directory))
2821 (with-parsed-tramp-file-name directory nil
2822 (try-completion
2823 filename
2824 (mapcar (lambda (x) (cons x nil))
2825 (file-name-all-completions filename directory)))))
2827 ;; cp, mv and ln
2829 (defun tramp-handle-add-name-to-file
2830 (filename newname &optional ok-if-already-exists)
2831 "Like `add-name-to-file' for tramp files."
2832 (with-parsed-tramp-file-name filename v1
2833 (with-parsed-tramp-file-name newname v2
2834 (let ((ln (when v1 (tramp-get-remote-ln
2835 v1-multi-method v1-method v1-user v1-host))))
2836 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2837 (equal v1-multi-method v2-multi-method)
2838 (equal v1-method v2-method)
2839 (equal v1-user v2-user)
2840 (equal v1-host v2-host))
2841 (error "add-name-to-file: %s"
2842 "only implemented for same method, same user, same host"))
2843 (when (and (not ok-if-already-exists)
2844 (file-exists-p newname)
2845 (not (numberp ok-if-already-exists))
2846 (y-or-n-p
2847 (format
2848 "File %s already exists; make it a new name anyway? "
2849 newname)))
2850 (error "add-name-to-file: file %s already exists" newname))
2851 (tramp-barf-unless-okay
2852 v1-multi-method v1-method v1-user v1-host
2853 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2854 (tramp-shell-quote-argument v2-localname))
2855 nil 'file-error
2856 "error with add-name-to-file, see buffer `%s' for details"
2857 (buffer-name))))))
2859 (defun tramp-handle-copy-file
2860 (filename newname &optional ok-if-already-exists keep-date)
2861 "Like `copy-file' for tramp files."
2862 ;; Check if both files are local -- invoke normal copy-file.
2863 ;; Otherwise, use tramp from local system.
2864 (setq filename (expand-file-name filename))
2865 (setq newname (expand-file-name newname))
2866 ;; At least one file a tramp file?
2867 (if (or (tramp-tramp-file-p filename)
2868 (tramp-tramp-file-p newname))
2869 (tramp-do-copy-or-rename-file
2870 'copy filename newname ok-if-already-exists keep-date)
2871 (tramp-run-real-handler
2872 'copy-file
2873 (list filename newname ok-if-already-exists keep-date))))
2875 (defun tramp-handle-rename-file
2876 (filename newname &optional ok-if-already-exists)
2877 "Like `rename-file' for tramp files."
2878 ;; Check if both files are local -- invoke normal rename-file.
2879 ;; Otherwise, use tramp from local system.
2880 (setq filename (expand-file-name filename))
2881 (setq newname (expand-file-name newname))
2882 ;; At least one file a tramp file?
2883 (if (or (tramp-tramp-file-p filename)
2884 (tramp-tramp-file-p newname))
2885 (tramp-do-copy-or-rename-file
2886 'rename filename newname ok-if-already-exists)
2887 (tramp-run-real-handler 'rename-file
2888 (list filename newname ok-if-already-exists))))
2890 (defun tramp-do-copy-or-rename-file
2891 (op filename newname &optional ok-if-already-exists keep-date)
2892 "Copy or rename a remote file.
2893 OP must be `copy' or `rename' and indicates the operation to perform.
2894 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2895 the new file (for copy) or the new name of the file (for rename).
2896 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2897 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2898 as FILENAME.
2900 This function is invoked by `tramp-handle-copy-file' and
2901 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2902 and `rename'. FILENAME and NEWNAME must be absolute file names."
2903 (unless (memq op '(copy rename))
2904 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2905 (unless ok-if-already-exists
2906 (when (file-exists-p newname)
2907 (signal 'file-already-exists
2908 (list newname))))
2909 (let ((t1 (tramp-tramp-file-p filename))
2910 (t2 (tramp-tramp-file-p newname))
2911 v1-multi-method v1-method v1-user v1-host v1-localname
2912 v2-multi-method v2-method v2-user v2-host v2-localname)
2914 ;; Check which ones of source and target are Tramp files.
2915 ;; We cannot invoke `with-parsed-tramp-file-name';
2916 ;; it fails if the file isn't a Tramp file name.
2917 (if t1
2918 (with-parsed-tramp-file-name filename l
2919 (setq v1-multi-method l-multi-method
2920 v1-method l-method
2921 v1-user l-user
2922 v1-host l-host
2923 v1-localname l-localname))
2924 (setq v1-localname filename))
2925 (if t2
2926 (with-parsed-tramp-file-name newname l
2927 (setq v2-multi-method l-multi-method
2928 v2-method l-method
2929 v2-user l-user
2930 v2-host l-host
2931 v2-localname l-localname))
2932 (setq v2-localname newname))
2934 (cond
2935 ;; Both are Tramp files.
2936 ((and t1 t2)
2937 (cond
2938 ;; Shortcut: if method, host, user are the same for both
2939 ;; files, we invoke `cp' or `mv' on the remote host
2940 ;; directly.
2941 ((and (equal v1-multi-method v2-multi-method)
2942 (equal v1-method v2-method)
2943 (equal v1-user v2-user)
2944 (equal v1-host v2-host))
2945 (tramp-do-copy-or-rename-file-directly
2946 op v1-multi-method v1-method v1-user v1-host
2947 v1-localname v2-localname keep-date))
2948 ;; If both source and target are Tramp files,
2949 ;; both are using the same copy-program, then we
2950 ;; can invoke rcp directly. Note that
2951 ;; default-directory should point to a local
2952 ;; directory if we want to invoke rcp.
2953 ((and (not v1-multi-method)
2954 (not v2-multi-method)
2955 (equal v1-method v2-method)
2956 (tramp-method-out-of-band-p
2957 v1-multi-method v1-method v1-user v1-host)
2958 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2959 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2960 (tramp-do-copy-or-rename-file-out-of-band
2961 op filename newname keep-date))
2962 ;; No shortcut was possible. So we copy the
2963 ;; file first. If the operation was `rename', we go
2964 ;; back and delete the original file (if the copy was
2965 ;; successful). The approach is simple-minded: we
2966 ;; create a new buffer, insert the contents of the
2967 ;; source file into it, then write out the buffer to
2968 ;; the target file. The advantage is that it doesn't
2969 ;; matter which filename handlers are used for the
2970 ;; source and target file.
2972 (tramp-do-copy-or-rename-file-via-buffer
2973 op filename newname keep-date))))
2975 ;; One file is a Tramp file, the other one is local.
2976 ((or t1 t2)
2977 ;; If the Tramp file has an out-of-band method, the corresponding
2978 ;; copy-program can be invoked.
2979 (if (and (not v1-multi-method)
2980 (not v2-multi-method)
2981 (or (tramp-method-out-of-band-p
2982 v1-multi-method v1-method v1-user v1-host)
2983 (tramp-method-out-of-band-p
2984 v2-multi-method v2-method v2-user v2-host)))
2985 (tramp-do-copy-or-rename-file-out-of-band
2986 op filename newname keep-date)
2987 ;; Use the generic method via a Tramp buffer.
2988 (tramp-do-copy-or-rename-file-via-buffer
2989 op filename newname keep-date)))
2992 ;; One of them must be a Tramp file.
2993 (error "Tramp implementation says this cannot happen")))))
2995 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2996 "Use an Emacs buffer to copy or rename a file.
2997 First arg OP is either `copy' or `rename' and indicates the operation.
2998 FILENAME is the source file, NEWNAME the target file.
2999 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3000 (let ((trampbuf (get-buffer-create "*tramp output*"))
3001 (modtime (nth 5 (file-attributes filename))))
3002 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
3003 (tramp-message
3004 1 (concat "Warning: cannot preserve file time stamp"
3005 " with inline copying across machines")))
3006 (save-excursion
3007 (set-buffer trampbuf) (erase-buffer)
3008 (insert-file-contents-literally filename)
3009 ;; We don't want the target file to be compressed, so we let-bind
3010 ;; `jka-compr-inhibit' to t.
3011 (let ((coding-system-for-write 'binary)
3012 (jka-compr-inhibit t))
3013 (write-region (point-min) (point-max) newname))
3014 ;; KEEP-DATE handling.
3015 (when keep-date
3016 (when (and (not (null modtime))
3017 (not (equal modtime '(0 0))))
3018 (tramp-touch newname modtime)))
3019 ;; Set the mode.
3020 (set-file-modes newname (file-modes filename)))
3021 ;; If the operation was `rename', delete the original file.
3022 (unless (eq op 'copy)
3023 (delete-file filename))))
3025 (defun tramp-do-copy-or-rename-file-directly
3026 (op multi-method method user host localname1 localname2 keep-date)
3027 "Invokes `cp' or `mv' on the remote system.
3028 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3029 respectively. METHOD, USER, and HOST specify the connection.
3030 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3031 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3032 ;; CCC: What happens to the timestamp when renaming?
3033 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3034 ((eq op 'copy) "cp -f")
3035 ((eq op 'rename) "mv -f")
3036 (t (error
3037 "Unknown operation `%s', must be `copy' or `rename'"
3038 op)))))
3039 (save-excursion
3040 (tramp-send-command
3041 multi-method method user host
3042 (format "%s %s %s"
3044 (tramp-shell-quote-argument localname1)
3045 (tramp-shell-quote-argument localname2)))
3046 (tramp-wait-for-output)
3047 (goto-char (point-min))
3048 (unless
3050 (and (eq op 'copy) keep-date
3051 ;; Mask cp -f error.
3052 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3053 (zerop (tramp-send-command-and-check
3054 multi-method method user host nil nil)))
3055 (pop-to-buffer (current-buffer))
3056 (signal 'file-error
3057 (format "Copying directly failed, see buffer `%s' for details."
3058 (buffer-name)))))
3059 ;; Set the mode.
3060 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3061 ;; where available?
3062 (unless (or (eq op 'rename) keep-date)
3063 (set-file-modes
3064 (tramp-make-tramp-file-name multi-method method user host localname2)
3065 (file-modes
3066 (tramp-make-tramp-file-name
3067 multi-method method user host localname1))))))
3069 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3070 "Invoke rcp program to copy.
3071 One of FILENAME and NEWNAME must be a Tramp name, the other must
3072 be a local filename. The method used must be an out-of-band method."
3073 (let ((t1 (tramp-tramp-file-p filename))
3074 (t2 (tramp-tramp-file-p newname))
3075 v1-multi-method v1-method v1-user v1-host v1-localname
3076 v2-multi-method v2-method v2-user v2-host v2-localname
3077 multi-method method user host copy-program copy-args
3078 source target trampbuf)
3080 ;; Check which ones of source and target are Tramp files.
3081 ;; We cannot invoke `with-parsed-tramp-file-name';
3082 ;; it fails if the file isn't a Tramp file name.
3083 (if t1
3084 (with-parsed-tramp-file-name filename l
3085 (setq v1-multi-method l-multi-method
3086 v1-method l-method
3087 v1-user l-user
3088 v1-host l-host
3089 v1-localname l-localname
3090 multi-method l-multi-method
3091 method (tramp-find-method
3092 v1-multi-method v1-method v1-user v1-host)
3093 user l-user
3094 host l-host
3095 copy-program (tramp-get-method-parameter
3096 v1-multi-method method
3097 v1-user v1-host 'tramp-copy-program)
3098 copy-args (tramp-get-method-parameter
3099 v1-multi-method method
3100 v1-user v1-host 'tramp-copy-args)))
3101 (setq v1-localname filename))
3103 (if t2
3104 (with-parsed-tramp-file-name newname l
3105 (setq v2-multi-method l-multi-method
3106 v2-method l-method
3107 v2-user l-user
3108 v2-host l-host
3109 v2-localname l-localname
3110 multi-method l-multi-method
3111 method (tramp-find-method
3112 v2-multi-method v2-method v2-user v2-host)
3113 user l-user
3114 host l-host
3115 copy-program (tramp-get-method-parameter
3116 v2-multi-method method
3117 v2-user v2-host 'tramp-copy-program)
3118 copy-args (tramp-get-method-parameter
3119 v2-multi-method method
3120 v2-user v2-host 'tramp-copy-args)))
3121 (setq v2-localname newname))
3123 ;; The following should be changed. We need a more general
3124 ;; mechanism to parse extra host args.
3125 (if (not t1)
3126 (setq source v1-localname)
3127 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3128 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3129 (setq v1-host (match-string 1 v1-host)))
3130 (setq source
3131 (tramp-make-copy-program-file-name
3132 v1-user v1-host
3133 (tramp-shell-quote-argument v1-localname))))
3135 (if (not t2)
3136 (setq target v2-localname)
3137 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3138 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3139 (setq v2-host (match-string 1 v2-host)))
3140 (setq target
3141 (tramp-make-copy-program-file-name
3142 v2-user v2-host
3143 (tramp-shell-quote-argument v2-localname))))
3145 ;; Handle keep-date argument
3146 (when keep-date
3147 (if t1
3148 (setq copy-args
3149 (cons (tramp-get-method-parameter
3150 v1-multi-method method
3151 v1-user v1-host 'tramp-copy-keep-date-arg)
3152 copy-args))
3153 (setq copy-args
3154 (cons (tramp-get-method-parameter
3155 v2-multi-method method
3156 v2-user v2-host 'tramp-copy-keep-date-arg)
3157 copy-args))))
3159 (setq copy-args (append copy-args (list source target))
3160 trampbuf (generate-new-buffer
3161 (tramp-buffer-name multi-method method user host)))
3163 ;; Use an asynchronous process. By this, password can be handled.
3164 (save-excursion
3166 ;; Check for program.
3167 (when (and (fboundp 'executable-find)
3168 (not (executable-find copy-program)))
3169 (error "Cannot find copy program: %s" copy-program))
3171 (set-buffer trampbuf)
3172 (setq tramp-current-multi-method multi-method
3173 tramp-current-method method
3174 tramp-current-user user
3175 tramp-current-host host)
3176 (message "Transferring %s to %s..." filename newname)
3178 ;; Use rcp-like program for file transfer.
3179 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3180 copy-program copy-args)))
3181 (tramp-set-process-query-on-exit-flag p nil)
3182 (tramp-process-actions p multi-method method user host
3183 tramp-actions-copy-out-of-band))
3184 (kill-buffer trampbuf)
3185 (message "Transferring %s to %s...done" filename newname)
3187 ;; Set the mode.
3188 (unless keep-date
3189 (set-file-modes newname (file-modes filename))))
3191 ;; If the operation was `rename', delete the original file.
3192 (unless (eq op 'copy)
3193 (delete-file filename))))
3195 ;; mkdir
3196 (defun tramp-handle-make-directory (dir &optional parents)
3197 "Like `make-directory' for tramp files."
3198 (setq dir (expand-file-name dir))
3199 (with-parsed-tramp-file-name dir nil
3200 (save-excursion
3201 (tramp-barf-unless-okay
3202 multi-method method user host
3203 (format " %s %s"
3204 (if parents "mkdir -p" "mkdir")
3205 (tramp-shell-quote-argument localname))
3206 nil 'file-error
3207 "Couldn't make directory %s" dir))))
3209 ;; CCC error checking?
3210 (defun tramp-handle-delete-directory (directory)
3211 "Like `delete-directory' for tramp files."
3212 (setq directory (expand-file-name directory))
3213 (with-parsed-tramp-file-name directory nil
3214 (save-excursion
3215 (tramp-send-command
3216 multi-method method user host
3217 (format "rmdir %s ; echo ok"
3218 (tramp-shell-quote-argument localname)))
3219 (tramp-wait-for-output))))
3221 (defun tramp-handle-delete-file (filename)
3222 "Like `delete-file' for tramp files."
3223 (setq filename (expand-file-name filename))
3224 (with-parsed-tramp-file-name filename nil
3225 (save-excursion
3226 (unless (zerop (tramp-send-command-and-check
3227 multi-method method user host
3228 (format "rm -f %s"
3229 (tramp-shell-quote-argument localname))))
3230 (signal 'file-error "Couldn't delete Tramp file")))))
3232 ;; Dired.
3234 ;; CCC: This does not seem to be enough. Something dies when
3235 ;; we try and delete two directories under TRAMP :/
3236 (defun tramp-handle-dired-recursive-delete-directory (filename)
3237 "Recursively delete the directory given.
3238 This is like `dired-recursive-delete-directory' for tramp files."
3239 (with-parsed-tramp-file-name filename nil
3240 ;; run a shell command 'rm -r <localname>'
3241 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3242 (or (file-exists-p filename)
3243 (signal
3244 'file-error
3245 (list "Removing old file name" "no such directory" filename)))
3246 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3247 (tramp-send-command multi-method method user host
3248 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3249 ;; Wait for the remote system to return to us...
3250 ;; This might take a while, allow it plenty of time.
3251 (tramp-wait-for-output 120)
3252 ;; Make sure that it worked...
3253 (and (file-exists-p filename)
3254 (error "Failed to recursively delete %s" filename))))
3256 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3257 "Like `dired-call-process' for tramp files."
3258 (with-parsed-tramp-file-name default-directory nil
3259 (save-excursion
3260 (tramp-barf-unless-okay
3261 multi-method method user host
3262 (format "cd %s" (tramp-shell-quote-argument localname))
3263 nil 'file-error
3264 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3265 (tramp-shell-quote-argument localname))
3266 (tramp-send-command
3267 multi-method method user host
3268 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3269 (tramp-wait-for-output))
3270 (unless discard
3271 ;; We cannot use `insert-buffer' because the tramp buffer
3272 ;; changes its contents before insertion due to calling
3273 ;; `expand-file' and alike.
3274 (insert
3275 (with-current-buffer
3276 (tramp-get-buffer multi-method method user host)
3277 (buffer-string))))
3278 (save-excursion
3279 (prog1
3280 (tramp-send-command-and-check multi-method method user host nil)
3281 (tramp-send-command multi-method method user host "cd")
3282 (tramp-wait-for-output)))))
3284 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3285 "Like `dired-compress-file' for tramp files."
3286 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3287 ;; Code stolen mainly from dired-aux.el.
3288 (with-parsed-tramp-file-name file nil
3289 (save-excursion
3290 (let ((suffixes
3291 (if (not (featurep 'xemacs))
3292 ;; Emacs case
3293 (symbol-value 'dired-compress-file-suffixes)
3294 ;; XEmacs has `dired-compression-method-alist', which is
3295 ;; transformed into `dired-compress-file-suffixes' structure.
3296 (mapcar
3297 '(lambda (x)
3298 (list (concat (regexp-quote (nth 1 x)) "\\'")
3300 (mapconcat 'identity (nth 3 x) " ")))
3301 (symbol-value 'dired-compression-method-alist))))
3302 suffix)
3303 ;; See if any suffix rule matches this file name.
3304 (while suffixes
3305 (let (case-fold-search)
3306 (if (string-match (car (car suffixes)) localname)
3307 (setq suffix (car suffixes) suffixes nil))
3308 (setq suffixes (cdr suffixes))))
3310 (cond ((file-symlink-p file)
3311 nil)
3312 ((and suffix (nth 2 suffix))
3313 ;; We found an uncompression rule.
3314 (message "Uncompressing %s..." file)
3315 (when (zerop (tramp-send-command-and-check
3316 multi-method method user host
3317 (concat (nth 2 suffix) " " localname)))
3318 (message "Uncompressing %s...done" file)
3319 ;; `dired-remove-file' is not defined in XEmacs
3320 (funcall (symbol-function 'dired-remove-file) file)
3321 (string-match (car suffix) file)
3322 (concat (substring file 0 (match-beginning 0)))))
3324 ;; We don't recognize the file as compressed, so compress it.
3325 ;; Try gzip.
3326 (message "Compressing %s..." file)
3327 (when (zerop (tramp-send-command-and-check
3328 multi-method method user host
3329 (concat "gzip -f " localname)))
3330 (message "Compressing %s...done" file)
3331 ;; `dired-remove-file' is not defined in XEmacs
3332 (funcall (symbol-function 'dired-remove-file) file)
3333 (cond ((file-exists-p (concat file ".gz"))
3334 (concat file ".gz"))
3335 ((file-exists-p (concat file ".z"))
3336 (concat file ".z"))
3337 (t nil)))))))))
3339 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3340 ;; not sure at all that this is the right way to do it, but let's hope
3341 ;; it works for now, and wait for a guru to point out the Right Way to
3342 ;; achieve this.
3343 ;;(eval-when-compile
3344 ;; (unless (fboundp 'dired-insert-set-properties)
3345 ;; (fset 'dired-insert-set-properties 'ignore)))
3346 ;; Gerd suggests this:
3347 (eval-when-compile (require 'dired))
3348 ;; Note that dired is required at run-time, too, when it is needed.
3349 ;; It is only needed on XEmacs for the function
3350 ;; `dired-insert-set-properties'.
3352 (defun tramp-handle-insert-directory
3353 (filename switches &optional wildcard full-directory-p)
3354 "Like `insert-directory' for tramp files."
3355 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3356 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3357 (tramp-run-real-handler 'insert-directory
3358 (list filename switches wildcard full-directory-p))
3359 ;; For the moment, we assume that the remote "ls" program does not
3360 ;; grok "--dired". In the future, we should detect this on
3361 ;; connection setup.
3362 (when (string-match "^--dired\\s-+" switches)
3363 (setq switches (replace-match "" nil t switches)))
3364 (setq filename (expand-file-name filename))
3365 (with-parsed-tramp-file-name filename nil
3366 (tramp-message-for-buffer
3367 multi-method method user host 10
3368 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3369 switches filename (if wildcard "yes" "no")
3370 (if full-directory-p "yes" "no"))
3371 (when wildcard
3372 (setq wildcard (file-name-nondirectory localname))
3373 (setq localname (file-name-directory localname)))
3374 (when (listp switches)
3375 (setq switches (mapconcat 'identity switches " ")))
3376 (unless full-directory-p
3377 (setq switches (concat "-d " switches)))
3378 (when wildcard
3379 (setq switches (concat switches " " wildcard)))
3380 (save-excursion
3381 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3382 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3383 (if full-directory-p
3384 (tramp-send-command
3385 multi-method method user host
3386 (format "%s %s %s"
3387 (tramp-get-ls-command multi-method method user host)
3388 switches
3389 (if wildcard
3390 localname
3391 (tramp-shell-quote-argument (concat localname ".")))))
3392 (tramp-barf-unless-okay
3393 multi-method method user host
3394 (format "cd %s" (tramp-shell-quote-argument
3395 (file-name-directory localname)))
3396 nil 'file-error
3397 "Couldn't `cd %s'"
3398 (tramp-shell-quote-argument (file-name-directory localname)))
3399 (tramp-send-command
3400 multi-method method user host
3401 (format "%s %s %s"
3402 (tramp-get-ls-command multi-method method user host)
3403 switches
3404 (if wildcard
3405 localname
3406 (if (zerop (length (file-name-nondirectory localname)))
3408 (tramp-shell-quote-argument
3409 (file-name-nondirectory localname)))))))
3410 (sit-for 1) ;needed for rsh but not ssh?
3411 (tramp-wait-for-output))
3412 ;; The following let-binding is used by code that's commented
3413 ;; out. Let's leave the let-binding in for a while to see
3414 ;; that the commented-out code is really not needed. Commenting-out
3415 ;; happened on 2003-03-13.
3416 (let ((old-pos (point)))
3417 ;; We cannot use `insert-buffer' because the tramp buffer
3418 ;; changes its contents before insertion due to calling
3419 ;; `expand-file' and alike.
3420 (insert
3421 (with-current-buffer
3422 (tramp-get-buffer multi-method method user host)
3423 (buffer-string)))
3424 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3425 ;; that doesn't exist on Emacs, so we use this workaround instead.
3426 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3427 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3428 ;; (let ((zmacs-region-stays t))
3429 ;; (exchange-point-and-mark))
3430 (save-excursion
3431 (tramp-send-command multi-method method user host "cd")
3432 (tramp-wait-for-output))
3433 ;; For the time being, the XEmacs kludge is commented out.
3434 ;; Please test it on various XEmacs versions to see if it works.
3435 ;; ;; Another XEmacs specialty follows. What's the right way to do
3436 ;; ;; it?
3437 ;; (when (and (featurep 'xemacs)
3438 ;; (eq major-mode 'dired-mode))
3439 ;; (save-excursion
3440 ;; (require 'dired)
3441 ;; (dired-insert-set-properties old-pos (point))))
3442 ))))
3444 ;; Continuation of kluge to pacify byte-compiler.
3445 ;;(eval-when-compile
3446 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3447 ;; (fmakunbound 'dired-insert-set-properties)))
3449 ;; CCC is this the right thing to do?
3450 (defun tramp-handle-unhandled-file-name-directory (filename)
3451 "Like `unhandled-file-name-directory' for tramp files."
3452 (with-parsed-tramp-file-name filename nil
3453 (expand-file-name "~/")))
3455 ;; Canonicalization of file names.
3457 (defun tramp-drop-volume-letter (name)
3458 "Cut off unnecessary drive letter from file NAME.
3459 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3460 locally on a remote file name. When the local system is a W32 system
3461 but the remote system is Unix, this introduces a superfluous drive
3462 letter into the file name. This function removes it.
3464 Doesn't do anything if the NAME does not start with a drive letter."
3465 (if (and (> (length name) 1)
3466 (char-equal (aref name 1) ?:)
3467 (let ((c1 (aref name 0)))
3468 (or (and (>= c1 ?A) (<= c1 ?Z))
3469 (and (>= c1 ?a) (<= c1 ?z)))))
3470 (substring name 2)
3471 name))
3473 (defun tramp-handle-expand-file-name (name &optional dir)
3474 "Like `expand-file-name' for tramp files.
3475 If the localname part of the given filename starts with \"/../\" then
3476 the result will be a local, non-Tramp, filename."
3477 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3478 (setq dir (or dir default-directory "/"))
3479 ;; Unless NAME is absolute, concat DIR and NAME.
3480 (unless (file-name-absolute-p name)
3481 (setq name (concat (file-name-as-directory dir) name)))
3482 ;; If NAME is not a tramp file, run the real handler
3483 (if (not (tramp-tramp-file-p name))
3484 (tramp-run-real-handler 'expand-file-name
3485 (list name nil))
3486 ;; Dissect NAME.
3487 (with-parsed-tramp-file-name name nil
3488 (unless (file-name-absolute-p localname)
3489 (setq localname (concat "~/" localname)))
3490 (save-excursion
3491 ;; Tilde expansion if necessary. This needs a shell which
3492 ;; groks tilde expansion! The function `tramp-find-shell' is
3493 ;; supposed to find such a shell on the remote host. Please
3494 ;; tell me about it when this doesn't work on your system.
3495 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3496 (let ((uname (match-string 1 localname))
3497 (fname (match-string 2 localname)))
3498 ;; We cannot simply apply "~/", because under sudo "~/" is
3499 ;; expanded to the local user home directory but to the
3500 ;; root home directory. On the other hand, using always
3501 ;; the default user name for tilde expansion is not
3502 ;; appropriate either, because ssh and companions might
3503 ;; use a user name from the config file.
3504 (when (and (string-equal uname "~")
3505 (string-match
3506 "\\`su\\(do\\)?\\'"
3507 (tramp-find-method multi-method method user host)))
3508 (setq uname (concat uname (or user "root"))))
3509 ;; CCC fanatic error checking?
3510 (set-buffer (tramp-get-buffer multi-method method user host))
3511 (erase-buffer)
3512 (tramp-send-command
3513 multi-method method user host
3514 (format "cd %s; pwd" uname)
3516 (tramp-wait-for-output)
3517 (goto-char (point-min))
3518 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3519 (setq localname (concat uname fname))
3520 (erase-buffer)))
3521 ;; There might be a double slash, for example when "~/"
3522 ;; expands to "/". Remove this.
3523 (while (string-match "//" localname)
3524 (setq localname (replace-match "/" t t localname)))
3525 ;; No tilde characters in file name, do normal
3526 ;; expand-file-name (this does "/./" and "/../"). We bind
3527 ;; directory-sep-char here for XEmacs on Windows, which would
3528 ;; otherwise use backslash. `default-directory' is bound to
3529 ;; "/", because on Windows there would be problems with UNC
3530 ;; shares or Cygwin mounts.
3531 (tramp-let-maybe directory-sep-char ?/
3532 (let ((default-directory "/"))
3533 (tramp-make-tramp-file-name
3534 multi-method (or method (tramp-find-default-method user host))
3535 user host
3536 (tramp-drop-volume-letter
3537 (tramp-run-real-handler 'expand-file-name
3538 (list localname))))))))))
3540 ;; old version follows. it uses ".." to cross file handler
3541 ;; boundaries.
3542 ;; ;; Look if localname starts with "/../" construct. If this is
3543 ;; ;; the case, then we return a local name instead of a remote name.
3544 ;; (if (string-match "^/\\.\\./" localname)
3545 ;; (expand-file-name (substring localname 3))
3546 ;; ;; No tilde characters in file name, do normal
3547 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3548 ;; ;; directory-sep-char here for XEmacs on Windows, which
3549 ;; ;; would otherwise use backslash.
3550 ;; (let ((directory-sep-char ?/))
3551 ;; (tramp-make-tramp-file-name
3552 ;; multi-method method user host
3553 ;; (tramp-drop-volume-letter
3554 ;; (tramp-run-real-handler 'expand-file-name
3555 ;; (list localname))))))))))
3557 ;; Remote commands.
3559 (defvar tramp-async-proc nil
3560 "Global variable keeping asynchronous process object.
3561 Used in `tramp-handle-shell-command'")
3563 (defvar tramp-display-shell-command-buffer t
3564 "Whether to display output buffer of `shell-command'.
3565 This is necessary for handling DISPLAY of `process-file'.")
3567 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3568 "Like `shell-command' for tramp files.
3569 This will break if COMMAND prints a newline, followed by the value of
3570 `tramp-end-of-output', followed by another newline."
3571 ;; Asynchronous processes are far from being perfect. But it works at least
3572 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3573 (if (tramp-tramp-file-p default-directory)
3574 (with-parsed-tramp-file-name default-directory nil
3575 (let ((curbuf (current-buffer))
3576 (asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3577 status)
3578 (unless output-buffer
3579 (setq output-buffer
3580 (get-buffer-create
3581 (if asynchronous
3582 "*Async Shell Command*"
3583 "*Shell Command Output*")))
3584 (set-buffer output-buffer)
3585 (erase-buffer))
3586 (unless (bufferp output-buffer)
3587 (setq output-buffer (current-buffer)))
3588 (set-buffer output-buffer)
3589 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3590 ;; process. Instead of, another asynchronous process is opened
3591 ;; which gets the output of the (synchronous) Tramp process
3592 ;; via process-filter. ERROR-BUFFER is disabled.
3593 (when asynchronous
3594 (setq command (substring command 0 (match-beginning 0))
3595 error-buffer nil
3596 tramp-async-proc (start-process (buffer-name output-buffer)
3597 output-buffer "cat")))
3598 (save-excursion
3599 (tramp-barf-unless-okay
3600 multi-method method user host
3601 (format "cd %s" (tramp-shell-quote-argument localname))
3602 nil 'file-error
3603 "tramp-handle-shell-command: Couldn't `cd %s'"
3604 (tramp-shell-quote-argument localname))
3605 ;; Define the process filter
3606 (when asynchronous
3607 (set-process-filter
3608 (get-buffer-process
3609 (tramp-get-buffer multi-method method user host))
3610 '(lambda (process string)
3611 ;; Write the output into the Tramp Process
3612 (save-current-buffer
3613 (set-buffer (process-buffer process))
3614 (goto-char (point-max))
3615 (insert string))
3616 ;; Hand-over output to asynchronous process.
3617 (let ((end
3618 (string-match
3619 (regexp-quote tramp-end-of-output) string)))
3620 (when end
3621 (setq string
3622 (substring string 0 (1- (match-beginning 0)))))
3623 (process-send-string tramp-async-proc string)
3624 (when end
3625 (set-process-filter process nil)
3626 (process-send-eof tramp-async-proc))))))
3627 ;; Send the command
3628 (tramp-send-command
3629 multi-method method user host
3630 (if error-buffer
3631 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3632 command)
3633 (format "%s; tramp_old_status=$?" command)))
3634 (unless asynchronous
3635 (tramp-wait-for-output)))
3636 (unless asynchronous
3637 ;; We cannot use `insert-buffer' because the tramp buffer
3638 ;; changes its contents before insertion due to calling
3639 ;; `expand-file' and alike.
3640 (insert
3641 (with-current-buffer
3642 (tramp-get-buffer multi-method method user host)
3643 (buffer-string))))
3644 (when error-buffer
3645 (save-excursion
3646 (unless (bufferp error-buffer)
3647 (setq error-buffer (get-buffer-create error-buffer)))
3648 (tramp-send-command
3649 multi-method method user host
3650 "cat /tmp/tramp.$$.err")
3651 (tramp-wait-for-output)
3652 (set-buffer error-buffer)
3653 ;; Same comment as above
3654 (insert
3655 (with-current-buffer
3656 (tramp-get-buffer multi-method method user host)
3657 (buffer-string)))
3658 (tramp-send-command-and-check
3659 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3660 (save-excursion
3661 (tramp-send-command multi-method method user host "cd")
3662 (unless asynchronous
3663 (tramp-wait-for-output))
3664 (tramp-send-command
3665 multi-method method user host
3666 (concat "tramp_set_exit_status $tramp_old_status;"
3667 " echo tramp_exit_status $?"))
3668 (unless asynchronous
3669 (tramp-wait-for-output)
3670 (goto-char (point-max))
3671 (unless (search-backward "tramp_exit_status " nil t)
3672 (error "Couldn't find exit status of `%s'" command))
3673 (skip-chars-forward "^ ")
3674 (setq status (read (current-buffer)))))
3675 (unless (zerop (buffer-size))
3676 (when tramp-display-shell-command-buffer
3677 (display-buffer output-buffer)))
3678 (set-buffer curbuf)
3679 status))
3680 ;; The following is only executed if something strange was
3681 ;; happening. Emit a helpful message and do it anyway.
3682 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3683 default-directory)
3684 (tramp-run-real-handler 'shell-command
3685 (list command output-buffer error-buffer))))
3687 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3688 "Like `process-file' for Tramp files."
3689 (when infile (error "Implementation does not handle input from file"))
3690 (when (and (numberp buffer) (zerop buffer))
3691 (error "Implementation does not handle immediate return"))
3692 (when (consp buffer) (error "Implementation does not handle error files"))
3693 (let ((tramp-display-shell-command-buffer display))
3694 (shell-command
3695 (mapconcat 'tramp-shell-quote-argument (cons program args) " ")
3696 buffer)))
3698 ;; File Editing.
3700 (defsubst tramp-make-temp-file ()
3701 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3702 (expand-file-name tramp-temp-name-prefix
3703 (tramp-temporary-file-directory))))
3705 (defun tramp-handle-file-local-copy (filename)
3706 "Like `file-local-copy' for tramp files."
3707 (with-parsed-tramp-file-name filename nil
3708 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3709 ;; We used to bind the following as late as possible.
3710 ;; loc-enc and loc-dec were bound directly before the if
3711 ;; statement that checks them. But the functions
3712 ;; tramp-get-* might invoke the "are you awake" check in
3713 ;; tramp-maybe-open-connection, which is an unfortunate time
3714 ;; since we rely on the buffer contents at that spot.
3715 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3716 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3717 (loc-enc (tramp-get-local-encoding multi-method method user host))
3718 (loc-dec (tramp-get-local-decoding multi-method method user host))
3719 tmpfil)
3720 (unless (file-exists-p filename)
3721 (error "Cannot make local copy of non-existing file `%s'"
3722 filename))
3723 (setq tmpfil (tramp-make-temp-file))
3725 (cond ((tramp-method-out-of-band-p multi-method method user host)
3726 ;; `copy-file' handles out-of-band methods
3727 (copy-file filename tmpfil t t))
3729 ((and rem-enc rem-dec)
3730 ;; Use inline encoding for file transfer.
3731 (save-excursion
3732 ;; Following line for setting tramp-current-method,
3733 ;; tramp-current-user, tramp-current-host.
3734 (set-buffer tramp-buf)
3735 (tramp-message 5 "Encoding remote file %s..." filename)
3736 (tramp-barf-unless-okay
3737 multi-method method user host
3738 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3739 nil 'file-error
3740 "Encoding remote file failed, see buffer `%s' for details"
3741 tramp-buf)
3742 ;; Remove trailing status code
3743 (goto-char (point-max))
3744 (delete-region (point) (progn (forward-line -1) (point)))
3746 (tramp-message 5 "Decoding remote file %s..." filename)
3748 ;; Here is where loc-enc and loc-dec used to be let-bound.
3749 (if (and (symbolp loc-dec) (fboundp loc-dec))
3750 ;; If local decoding is a function, we call it.
3751 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3752 (set-buffer tmpbuf)
3753 (erase-buffer)
3754 (insert-buffer-substring tramp-buf)
3755 (tramp-message-for-buffer
3756 multi-method method user host
3757 6 "Decoding remote file %s with function %s..."
3758 filename loc-dec)
3759 (set-buffer tmpbuf)
3760 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3761 ;; says that we need to strip tramp_exit_status
3762 ;; line from the output here. Go to point-max,
3763 ;; search backward for tramp_exit_status, delete
3764 ;; between point and point-max if found.
3765 (let ((coding-system-for-write 'binary))
3766 (funcall loc-dec (point-min) (point-max))
3767 (write-region (point-min) (point-max) tmpfil))
3768 (kill-buffer tmpbuf))
3769 ;; If tramp-decoding-function is not defined for this
3770 ;; method, we invoke tramp-decoding-command instead.
3771 (let ((tmpfil2 (tramp-make-temp-file)))
3772 (write-region (point-min) (point-max) tmpfil2)
3773 (tramp-message
3774 6 "Decoding remote file %s with command %s..."
3775 filename loc-dec)
3776 (tramp-call-local-coding-command
3777 loc-dec tmpfil2 tmpfil)
3778 (delete-file tmpfil2)))
3779 (tramp-message-for-buffer
3780 multi-method method user host
3781 5 "Decoding remote file %s...done" filename)
3782 ;; Set proper permissions.
3783 (set-file-modes tmpfil (file-modes filename))))
3785 (t (error "Wrong method specification for `%s'" method)))
3786 tmpfil)))
3788 (defun tramp-handle-file-remote-p (filename)
3789 "Like `file-remote-p' for tramp files."
3790 (when (tramp-tramp-file-p filename)
3791 (with-parsed-tramp-file-name filename nil
3792 (make-tramp-file-name
3793 :multi-method multi-method
3794 :method method
3795 :user user
3796 :host host
3797 :localname ""))))
3799 (defun tramp-handle-insert-file-contents
3800 (filename &optional visit beg end replace)
3801 "Like `insert-file-contents' for tramp files."
3802 (barf-if-buffer-read-only)
3803 (setq filename (expand-file-name filename))
3804 (with-parsed-tramp-file-name filename nil
3805 (if (not (file-exists-p filename))
3806 (progn
3807 (when visit
3808 (setq buffer-file-name filename)
3809 (set-visited-file-modtime)
3810 (set-buffer-modified-p nil))
3811 (signal 'file-error
3812 (format "File `%s' not found on remote host" filename))
3813 (list (expand-file-name filename) 0))
3814 ;; `insert-file-contents-literally' takes care to avoid calling
3815 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3816 ;; propagate that care to the file-local-copy operation.
3817 (let ((local-copy
3818 (let ((inhibit-file-name-operation
3819 (when (eq inhibit-file-name-operation
3820 'insert-file-contents)
3821 'file-local-copy)))
3822 (file-local-copy filename)))
3823 coding-system-used result)
3824 (when visit
3825 (setq buffer-file-name filename)
3826 (set-visited-file-modtime)
3827 (set-buffer-modified-p nil))
3828 (tramp-message-for-buffer
3829 multi-method method user host
3830 9 "Inserting local temp file `%s'..." local-copy)
3831 (setq result (insert-file-contents local-copy nil beg end replace))
3832 ;; Now `last-coding-system-used' has right value. Remember it.
3833 (when (boundp 'last-coding-system-used)
3834 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3835 (tramp-message-for-buffer
3836 multi-method method user host
3837 9 "Inserting local temp file `%s'...done" local-copy)
3838 (delete-file local-copy)
3839 (when (boundp 'last-coding-system-used)
3840 (set 'last-coding-system-used coding-system-used))
3841 (list (expand-file-name filename)
3842 (second result))))))
3845 (defun tramp-handle-find-backup-file-name (filename)
3846 "Like `find-backup-file-name' for tramp files."
3847 (with-parsed-tramp-file-name filename nil
3848 ;; We set both variables. It doesn't matter whether it is
3849 ;; Emacs or XEmacs
3850 (let ((backup-directory-alist
3851 ;; Emacs case
3852 (when (boundp 'backup-directory-alist)
3853 (if (boundp 'tramp-backup-directory-alist)
3854 (mapcar
3855 '(lambda (x)
3856 (cons
3857 (car x)
3858 (if (and (stringp (cdr x))
3859 (file-name-absolute-p (cdr x))
3860 (not (tramp-file-name-p (cdr x))))
3861 (tramp-make-tramp-file-name
3862 multi-method method user host (cdr x))
3863 (cdr x))))
3864 (symbol-value 'tramp-backup-directory-alist))
3865 (symbol-value 'backup-directory-alist))))
3867 (bkup-backup-directory-info
3868 ;; XEmacs case
3869 (when (boundp 'bkup-backup-directory-info)
3870 (if (boundp 'tramp-bkup-backup-directory-info)
3871 (mapcar
3872 '(lambda (x)
3873 (nconc
3874 (list (car x))
3875 (list
3876 (if (and (stringp (car (cdr x)))
3877 (file-name-absolute-p (car (cdr x)))
3878 (not (tramp-file-name-p (car (cdr x)))))
3879 (tramp-make-tramp-file-name
3880 multi-method method user host (car (cdr x)))
3881 (car (cdr x))))
3882 (cdr (cdr x))))
3883 (symbol-value 'tramp-bkup-backup-directory-info))
3884 (symbol-value 'bkup-backup-directory-info)))))
3886 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3888 (defun tramp-handle-make-auto-save-file-name ()
3889 "Like `make-auto-save-file-name' for tramp files.
3890 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3891 (let ((tramp-auto-save-directory tramp-auto-save-directory))
3892 ;; File name must be unique. This is ensured with Emacs 22 (see
3893 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3894 ;; all other cases we must do it ourselves.
3895 (when (boundp 'auto-save-file-name-transforms)
3896 (mapcar
3897 '(lambda (x)
3898 (when (and (string-match (car x) buffer-file-name)
3899 (not (car (cddr x))))
3900 (setq tramp-auto-save-directory
3901 (or tramp-auto-save-directory temporary-file-directory))))
3902 (symbol-value 'auto-save-file-name-transforms)))
3903 ;; Create directory.
3904 (when tramp-auto-save-directory
3905 (unless (file-exists-p tramp-auto-save-directory)
3906 (make-directory tramp-auto-save-directory t)))
3907 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3908 ;; file name we make sure that jka-compr isn't used for the
3909 ;; auto-save file.
3910 (let ((buffer-file-name
3911 (if tramp-auto-save-directory
3912 (expand-file-name
3913 (tramp-subst-strs-in-string
3914 '(("_" . "|")
3915 ("/" . "_a")
3916 (":" . "_b")
3917 ("|" . "__")
3918 ("[" . "_l")
3919 ("]" . "_r"))
3920 (buffer-file-name))
3921 tramp-auto-save-directory)
3922 (buffer-file-name))))
3923 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3924 ;; it is not a magic file name operation (since Emacs 22).
3925 ;; We must deactivate it temporarily.
3926 (if (not (ad-is-active 'make-auto-save-file-name))
3927 (tramp-run-real-handler
3928 'make-auto-save-file-name nil)
3929 ;; else
3930 (ad-deactivate 'make-auto-save-file-name)
3931 (prog1
3932 (tramp-run-real-handler
3933 'make-auto-save-file-name nil)
3934 (ad-activate 'make-auto-save-file-name))))))
3937 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3938 (defun tramp-handle-write-region
3939 (start end filename &optional append visit lockname confirm)
3940 "Like `write-region' for tramp files."
3941 (unless (eq append nil)
3942 (error "Cannot append to file using tramp (`%s')" filename))
3943 (setq filename (expand-file-name filename))
3944 ;; Following part commented out because we don't know what to do about
3945 ;; file locking, and it does not appear to be a problem to ignore it.
3946 ;; Ange-ftp ignores it, too.
3947 ;; (when (and lockname (stringp lockname))
3948 ;; (setq lockname (expand-file-name lockname)))
3949 ;; (unless (or (eq lockname nil)
3950 ;; (string= lockname filename))
3951 ;; (error
3952 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3953 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
3954 (when (and (not (featurep 'xemacs))
3955 confirm (file-exists-p filename))
3956 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3957 filename))
3958 (error "File not overwritten")))
3959 (with-parsed-tramp-file-name filename nil
3960 (let ((curbuf (current-buffer))
3961 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3962 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3963 (loc-enc (tramp-get-local-encoding multi-method method user host))
3964 (loc-dec (tramp-get-local-decoding multi-method method user host))
3965 (trampbuf (get-buffer-create "*tramp output*"))
3966 (modes (file-modes filename))
3967 ;; We use this to save the value of `last-coding-system-used'
3968 ;; after writing the tmp file. At the end of the function,
3969 ;; we set `last-coding-system-used' to this saved value.
3970 ;; This way, any intermediary coding systems used while
3971 ;; talking to the remote shell or suchlike won't hose this
3972 ;; variable. This approach was snarfed from ange-ftp.el.
3973 coding-system-used
3974 tmpfil)
3975 ;; Write region into a tmp file. This isn't really needed if we
3976 ;; use an encoding function, but currently we use it always
3977 ;; because this makes the logic simpler.
3978 (setq tmpfil (tramp-make-temp-file))
3979 ;; Set current buffer. If connection wasn't open, `file-modes' has
3980 ;; changed it accidently.
3981 (set-buffer curbuf)
3982 ;; We say `no-message' here because we don't want the visited file
3983 ;; modtime data to be clobbered from the temp file. We call
3984 ;; `set-visited-file-modtime' ourselves later on.
3985 (tramp-run-real-handler
3986 'write-region
3987 (if confirm ; don't pass this arg unless defined for backward compat.
3988 (list start end tmpfil append 'no-message lockname confirm)
3989 (list start end tmpfil append 'no-message lockname)))
3990 ;; Now, `last-coding-system-used' has the right value. Remember it.
3991 (when (boundp 'last-coding-system-used)
3992 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3993 ;; The permissions of the temporary file should be set. If
3994 ;; filename does not exist (eq modes nil) it has been renamed to
3995 ;; the backup file. This case `save-buffer' handles
3996 ;; permissions.
3997 (when modes (set-file-modes tmpfil modes))
3998 ;; This is a bit lengthy due to the different methods possible for
3999 ;; file transfer. First, we check whether the method uses an rcp
4000 ;; program. If so, we call it. Otherwise, both encoding and
4001 ;; decoding command must be specified. However, if the method
4002 ;; _also_ specifies an encoding function, then that is used for
4003 ;; encoding the contents of the tmp file.
4004 (cond ((tramp-method-out-of-band-p multi-method method user host)
4005 ;; `copy-file' handles out-of-band methods
4006 (copy-file tmpfil filename t t))
4008 ((and rem-enc rem-dec)
4009 ;; Use inline file transfer
4010 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4011 (save-excursion
4012 ;; Encode tmpfil into tmpbuf
4013 (tramp-message-for-buffer multi-method method user host
4014 5 "Encoding region...")
4015 (set-buffer tmpbuf)
4016 (erase-buffer)
4017 ;; Use encoding function or command.
4018 (if (and (symbolp loc-enc) (fboundp loc-enc))
4019 (progn
4020 (tramp-message-for-buffer
4021 multi-method method user host
4022 6 "Encoding region using function `%s'..."
4023 (symbol-name loc-enc))
4024 (insert-file-contents-literally tmpfil)
4025 ;; CCC. The following `let' is a workaround for
4026 ;; the base64.el that comes with pgnus-0.84. If
4027 ;; both of the following conditions are
4028 ;; satisfied, it tries to write to a local file
4029 ;; in default-directory, but at this point,
4030 ;; default-directory is remote.
4031 ;; (CALL-PROCESS-REGION can't write to remote
4032 ;; files, it seems.) The file in question is a
4033 ;; tmp file anyway.
4034 (let ((default-directory
4035 (tramp-temporary-file-directory)))
4036 (funcall loc-enc (point-min) (point-max)))
4037 (goto-char (point-max))
4038 (unless (bolp)
4039 (newline)))
4040 (tramp-message-for-buffer
4041 multi-method method user host
4042 6 "Encoding region using command `%s'..." loc-enc)
4043 (unless (equal 0 (tramp-call-local-coding-command
4044 loc-enc tmpfil t))
4045 (pop-to-buffer trampbuf)
4046 (error (concat "Cannot write to `%s', local encoding"
4047 " command `%s' failed")
4048 filename loc-enc)))
4049 ;; Send tmpbuf into remote decoding command which
4050 ;; writes to remote file. Because this happens on the
4051 ;; remote host, we cannot use the function.
4052 (tramp-message-for-buffer
4053 multi-method method user host
4054 5 "Decoding region into remote file %s..." filename)
4055 (tramp-send-command
4056 multi-method method user host
4057 (format "%s >%s <<'EOF'"
4058 rem-dec
4059 (tramp-shell-quote-argument localname)))
4060 (set-buffer tmpbuf)
4061 (tramp-message-for-buffer
4062 multi-method method user host
4063 6 "Sending data to remote host...")
4064 (tramp-send-string multi-method method user host
4065 (buffer-string))
4066 ;; wait for remote decoding to complete
4067 (tramp-message-for-buffer
4068 multi-method method user host
4069 6 "Sending end of data token...")
4070 (tramp-send-command
4071 multi-method method user host "EOF" nil t)
4072 (tramp-message-for-buffer
4073 multi-method method user host 6
4074 "Waiting for remote host to process data...")
4075 (set-buffer (tramp-get-buffer multi-method method user host))
4076 (tramp-wait-for-output)
4077 (tramp-barf-unless-okay
4078 multi-method method user host nil nil 'file-error
4079 (concat "Couldn't write region to `%s',"
4080 " decode using `%s' failed")
4081 filename rem-dec)
4082 (tramp-message 5 "Decoding region into remote file %s...done"
4083 filename)
4084 (kill-buffer tmpbuf))))
4086 (error
4087 (concat "Method `%s' should specify both encoding and "
4088 "decoding command or an rcp program")
4089 method)))
4090 (delete-file tmpfil)
4091 (unless (equal curbuf (current-buffer))
4092 (error "Buffer has changed from `%s' to `%s'"
4093 curbuf (current-buffer)))
4094 (when (or (eq visit t) (stringp visit))
4095 (set-visited-file-modtime
4096 ;; We must pass modtime explicitely, because filename can be different
4097 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4098 (nth 5 (file-attributes filename))))
4099 ;; Make `last-coding-system-used' have the right value.
4100 (when (boundp 'last-coding-system-used)
4101 (set 'last-coding-system-used coding-system-used))
4102 (when (or (eq visit t)
4103 (eq visit nil)
4104 (stringp visit))
4105 (message "Wrote %s" filename)))))
4107 ;; Call down to the real handler.
4108 ;; Because EFS does not play nicely with TRAMP (both systems match a
4109 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4110 ;; operation.
4112 ;; Other than that, this is the canon file-handler code that the doco
4113 ;; says should be used here. Which is nice.
4115 ;; Under XEmacs current, EFS also hooks in as
4116 ;; efs-sifn-handler-function to handle any filename with environment
4117 ;; variables. This has two implications:
4118 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4119 ;; 2) That TRAMP might want to do the same thing.
4120 ;; Details as they come in.
4122 ;; Daniel Pittman <daniel@danann.net>
4124 ;; (defun tramp-run-real-handler (operation args)
4125 ;; "Invoke normal file name handler for OPERATION.
4126 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4127 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4128 ;; OPERATION."
4129 ;; (let ((inhibit-file-name-handlers
4130 ;; (list 'tramp-file-name-handler
4131 ;; 'efs-file-handler-function
4132 ;; 'ange-ftp-hook-function
4133 ;; (and (eq inhibit-file-name-operation operation)
4134 ;; inhibit-file-name-handlers)))
4135 ;; (inhibit-file-name-operation operation))
4136 ;; (apply operation args)))
4138 ;;;###autoload
4139 (progn (defun tramp-run-real-handler (operation args)
4140 "Invoke normal file name handler for OPERATION.
4141 First arg specifies the OPERATION, second arg is a list of arguments to
4142 pass to the OPERATION."
4143 (let* ((inhibit-file-name-handlers
4144 `(tramp-file-name-handler
4145 tramp-completion-file-name-handler
4146 cygwin-mount-name-hook-function
4147 cygwin-mount-map-drive-hook-function
4149 ,(and (eq inhibit-file-name-operation operation)
4150 inhibit-file-name-handlers)))
4151 (inhibit-file-name-operation operation))
4152 (apply operation args))))
4154 ;; This function is used from `tramp-completion-file-name-handler' functions
4155 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4156 ;; because the check is based on a full filename, not available for all
4157 ;; basic I/O operations.
4158 ;;;###autoload
4159 (progn (defun tramp-completion-run-real-handler (operation args)
4160 "Invoke `tramp-file-name-handler' for OPERATION.
4161 First arg specifies the OPERATION, second arg is a list of arguments to
4162 pass to the OPERATION."
4163 (let* ((inhibit-file-name-handlers
4164 `(tramp-completion-file-name-handler
4165 cygwin-mount-name-hook-function
4166 cygwin-mount-map-drive-hook-function
4168 ,(and (eq inhibit-file-name-operation operation)
4169 inhibit-file-name-handlers)))
4170 (inhibit-file-name-operation operation))
4171 (apply operation args))))
4173 ;; We handle here all file primitives. Most of them have the file
4174 ;; name as first parameter; nevertheless we check for them explicitly
4175 ;; in order to be signalled if a new primitive appears. This
4176 ;; scenario is needed because there isn't a way to decide by
4177 ;; syntactical means whether a foreign method must be called. It would
4178 ;; ease the life if `file-name-handler-alist' would support a decision
4179 ;; function as well but regexp only.
4180 (defun tramp-file-name-for-operation (operation &rest args)
4181 "Return file name related to OPERATION file primitive.
4182 ARGS are the arguments OPERATION has been called with."
4183 (cond
4184 ; FILE resp DIRECTORY
4185 ((member operation
4186 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4187 'delete-file 'diff-latest-backup-file 'directory-file-name
4188 'directory-files 'directory-files-and-attributes
4189 'dired-compress-file 'dired-uncache
4190 'file-accessible-directory-p 'file-attributes
4191 'file-directory-p 'file-executable-p 'file-exists-p
4192 'file-local-copy 'file-remote-p 'file-modes
4193 'file-name-as-directory 'file-name-directory
4194 'file-name-nondirectory 'file-name-sans-versions
4195 'file-ownership-preserved-p 'file-readable-p
4196 'file-regular-p 'file-symlink-p 'file-truename
4197 'file-writable-p 'find-backup-file-name 'find-file-noselect
4198 'get-file-buffer 'insert-directory 'insert-file-contents
4199 'load 'make-directory 'make-directory-internal
4200 'set-file-modes 'substitute-in-file-name
4201 'unhandled-file-name-directory 'vc-registered
4202 ; XEmacs only
4203 'abbreviate-file-name 'create-file-buffer
4204 'dired-file-modtime 'dired-make-compressed-filename
4205 'dired-recursive-delete-directory 'dired-set-file-modtime
4206 'dired-shell-unhandle-file-name 'dired-uucode-file
4207 'insert-file-contents-literally 'recover-file
4208 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4209 (if (file-name-absolute-p (nth 0 args))
4210 (nth 0 args)
4211 (expand-file-name (nth 0 args))))
4212 ; FILE DIRECTORY resp FILE1 FILE2
4213 ((member operation
4214 (list 'add-name-to-file 'copy-file 'expand-file-name
4215 'file-name-all-completions 'file-name-completion
4216 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4217 ; XEmacs only
4218 'dired-make-relative-symlink
4219 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4220 (save-match-data
4221 (cond
4222 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4223 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4224 (t (buffer-file-name (current-buffer))))))
4225 ; START END FILE
4226 ((eq operation 'write-region)
4227 (nth 2 args))
4228 ; BUF
4229 ((member operation
4230 (list 'make-auto-save-file-name
4231 'set-visited-file-modtime 'verify-visited-file-modtime
4232 ; XEmacs only
4233 'backup-buffer))
4234 (buffer-file-name
4235 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4236 ; COMMAND
4237 ((member operation
4238 (list 'dired-call-process
4239 ; Emacs only
4240 'shell-command
4241 ; Emacs 22 only
4242 'process-file
4243 ; XEmacs only
4244 'dired-print-file 'dired-shell-call-process))
4245 default-directory)
4246 ; unknown file primitive
4247 (t (error "unknown file I/O primitive: %s" operation))))
4249 (defun tramp-find-foreign-file-name-handler (filename)
4250 "Return foreign file name handler if exists."
4251 (when (tramp-tramp-file-p filename)
4252 (let (elt
4254 (handler-alist tramp-foreign-file-name-handler-alist))
4255 (while handler-alist
4256 (setq elt (car handler-alist)
4257 handler-alist (cdr handler-alist))
4258 (when (funcall (car elt) filename)
4259 (setq handler-alist nil)
4260 (setq res (cdr elt))))
4261 res)))
4263 ;; Main function.
4264 ;;;###autoload
4265 (defun tramp-file-name-handler (operation &rest args)
4266 "Invoke Tramp file name handler.
4267 Falls back to normal file name handler if no tramp file name handler exists."
4268 ;; (setq edebug-trace t)
4269 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4270 (save-match-data
4271 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4272 (completion (tramp-completion-mode filename))
4273 (foreign (tramp-find-foreign-file-name-handler filename)))
4274 (with-parsed-tramp-file-name filename nil
4275 (cond
4276 ;; When we are in completion mode, some operations shouldn' be
4277 ;; handled by backend.
4278 ((and completion (memq operation '(expand-file-name)))
4279 (tramp-run-real-handler operation args))
4280 ((and completion (zerop (length localname))
4281 (memq operation '(file-exists-p file-directory-p)))
4283 ;; Call the backend function.
4284 (foreign (apply foreign operation args))
4285 ;; Nothing to do for us.
4286 (t (tramp-run-real-handler operation args)))))))
4289 ;; In Emacs, there is some concurrency due to timers. If a timer
4290 ;; interrupts Tramp and wishes to use the same connection buffer as
4291 ;; the "main" Emacs, then garbage might occur in the connection
4292 ;; buffer. Therefore, we need to make sure that a timer does not use
4293 ;; the same connection buffer as the "main" Emacs. We implement a
4294 ;; cheap global lock, instead of locking each connection buffer
4295 ;; separately. The global lock is based on two variables,
4296 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4297 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4298 ;; processing of a single file operation, so we need to allow
4299 ;; recursive calls. That's where the `tramp-locker' variable comes in
4300 ;; -- it is let-bound to t during the execution of the current
4301 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4302 ;; then we should just proceed because we have been called
4303 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4304 ;; interrupting the "main" Emacs, and then we signal an error.
4306 (defvar tramp-locked nil
4307 "If non-nil, then Tramp is currently busy.
4308 Together with `tramp-locker', this implements a locking mechanism
4309 preventing reentrant calls of Tramp.")
4311 (defvar tramp-locker nil
4312 "If non-nil, then a caller has locked Tramp.
4313 Together with `tramp-locked', this implements a locking mechanism
4314 preventing reentrant calls of Tramp.")
4316 (defun tramp-sh-file-name-handler (operation &rest args)
4317 "Invoke remote-shell Tramp file name handler.
4318 Fall back to normal file name handler if no Tramp handler exists."
4319 (when (and tramp-locked (not tramp-locker))
4320 (signal 'file-error "Forbidden reentrant call of Tramp"))
4321 (let ((tl tramp-locked))
4322 (unwind-protect
4323 (progn
4324 (setq tramp-locked t)
4325 (let ((tramp-locker t))
4326 (save-match-data
4327 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4328 (if fn
4329 (apply (cdr fn) args)
4330 (tramp-run-real-handler operation args))))))
4331 (setq tramp-locked tl))))
4333 ;;;###autoload
4334 (progn (defun tramp-completion-file-name-handler (operation &rest args)
4335 "Invoke tramp file name completion handler.
4336 Falls back to normal file name handler if no tramp file name handler exists."
4337 ;; (setq edebug-trace t)
4338 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4339 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4340 (if fn
4341 (save-match-data (apply (cdr fn) args))
4342 (tramp-completion-run-real-handler operation args)))))
4344 ;;;###autoload
4345 (defsubst tramp-register-file-name-handlers ()
4346 "Add tramp file name handlers to `file-name-handler-alist'."
4347 (add-to-list 'file-name-handler-alist
4348 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4349 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4350 ;; load it unconditionally there. In the GNU Emacs case, method/
4351 ;; user/host name completion shall be bound to `partial-completion-mode'.
4352 (when (or (not (boundp 'partial-completion-mode))
4353 (symbol-value 'partial-completion-mode)
4354 (featurep 'ido))
4355 (add-to-list 'file-name-handler-alist
4356 (cons tramp-completion-file-name-regexp
4357 'tramp-completion-file-name-handler))
4358 (put 'tramp-completion-file-name-handler 'safe-magic t))
4359 ;; If jka-compr is already loaded, move it to the front of
4360 ;; `file-name-handler-alist'.
4361 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4362 (when jka
4363 (setq file-name-handler-alist
4364 (cons jka (delete jka file-name-handler-alist))))))
4366 ;; During autoload, it shall be checked whether
4367 ;; `partial-completion-mode' is active. Therefore registering will be
4368 ;; delayed.
4369 ;;;###autoload(add-hook
4370 ;;;###autoload 'after-init-hook
4371 ;;;###autoload '(lambda () (tramp-register-file-name-handlers)))
4372 (tramp-register-file-name-handlers)
4374 ;;;###autoload
4375 (defun tramp-unload-file-name-handlers ()
4376 (setq file-name-handler-alist
4377 (delete (rassoc 'tramp-file-name-handler
4378 file-name-handler-alist)
4379 (delete (rassoc 'tramp-completion-file-name-handler
4380 file-name-handler-alist)
4381 file-name-handler-alist))))
4383 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
4386 ;;; Interactions with other packages:
4388 ;; -- complete.el --
4390 ;; This function contributed by Ed Sabol
4391 (defun tramp-handle-expand-many-files (name)
4392 "Like `PC-expand-many-files' for tramp files."
4393 (with-parsed-tramp-file-name name nil
4394 (save-match-data
4395 (if (or (string-match "\\*" name)
4396 (string-match "\\?" name)
4397 (string-match "\\[.*\\]" name))
4398 (save-excursion
4399 (let (bufstr)
4400 ;; CCC: To do it right, we should quote certain characters
4401 ;; in the file name, but since the echo command is going to
4402 ;; break anyway when there are spaces in the file names, we
4403 ;; don't bother.
4404 ;;-(let ((comint-file-name-quote-list
4405 ;;- (set-difference tramp-file-name-quote-list
4406 ;;- '(?\* ?\? ?[ ?]))))
4407 ;;- (tramp-send-command
4408 ;;- multi-method method user host
4409 ;;- (format "echo %s" (comint-quote-filename localname)))
4410 ;;- (tramp-wait-for-output))
4411 (tramp-send-command multi-method method user host
4412 (format "echo %s" localname))
4413 (tramp-wait-for-output)
4414 (setq bufstr (buffer-substring (point-min)
4415 (tramp-line-end-position)))
4416 (goto-char (point-min))
4417 (if (string-equal localname bufstr)
4419 (insert "(\"")
4420 (while (search-forward " " nil t)
4421 (delete-backward-char 1)
4422 (insert "\" \""))
4423 (goto-char (point-max))
4424 (delete-backward-char 1)
4425 (insert "\")")
4426 (goto-char (point-min))
4427 (mapcar
4428 (function (lambda (x)
4429 (tramp-make-tramp-file-name multi-method method
4430 user host x)))
4431 (read (current-buffer))))))
4432 (list (expand-file-name name))))))
4434 (eval-after-load "complete"
4435 '(progn
4436 (defadvice PC-expand-many-files
4437 (around tramp-advice-PC-expand-many-files (name) activate)
4438 "Invoke `tramp-handle-expand-many-files' for tramp files."
4439 (if (tramp-tramp-file-p name)
4440 (setq ad-return-value (tramp-handle-expand-many-files name))
4441 ad-do-it))
4442 (add-hook 'tramp-unload-hook
4443 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
4445 ;;; File name handler functions for completion mode
4447 (defvar tramp-completion-mode nil
4448 "If non-nil, we are in file name completion mode.")
4450 ;; Necessary because `tramp-file-name-regexp-unified' and
4451 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4452 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4453 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4454 ;; Using `last-input-event' is a little bit risky, because completing a file
4455 ;; might require loading other files, like "~/.netrc", and for them it
4456 ;; shouldn't be decided based on that variable. On the other hand, those files
4457 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4458 ;; be introduced overwriting this check in such cases. Or we change tramp
4459 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4460 ;; In case of non unified file names it can be always true (and wouldn't be
4461 ;; necessary, because there are different regexp).
4462 (defun tramp-completion-mode (file)
4463 "Checks whether method / user name / host name completion is active."
4464 (cond
4465 (tramp-completion-mode t)
4466 ((not tramp-unified-filenames) t)
4467 ((string-match "^/.*:.*:$" file) nil)
4468 ((string-match
4469 (concat tramp-prefix-regexp
4470 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4471 file)
4472 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4473 ((or (equal last-input-event 'tab)
4474 ;; Emacs
4475 (and (integerp last-input-event)
4477 ;; ?\t has event-modifier 'control
4478 (char-equal last-input-event ?\t)
4479 (and (not (event-modifiers last-input-event))
4480 (or (char-equal last-input-event ?\?)
4481 (char-equal last-input-event ?\ )))))
4482 ;; XEmacs
4483 (and (featurep 'xemacs)
4485 ;; ?\t has event-modifier 'control
4486 (char-equal
4487 (funcall (symbol-function 'event-to-character)
4488 last-input-event) ?\t)
4489 (and (not (event-modifiers last-input-event))
4490 (or (char-equal
4491 (funcall (symbol-function 'event-to-character)
4492 last-input-event) ?\?)
4493 (char-equal
4494 (funcall (symbol-function 'event-to-character)
4495 last-input-event) ?\ ))))))
4496 t)))
4498 ;; Method, host name and user name completion.
4499 ;; `tramp-completion-dissect-file-name' returns a list of
4500 ;; tramp-file-name structures. For all of them we return possible completions.
4501 ;;;###autoload
4502 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4503 "Like `file-name-all-completions' for partial tramp files."
4505 (unwind-protect
4506 ;; We need to reset `tramp-completion-mode'.
4507 (progn
4508 (setq tramp-completion-mode t)
4509 (let*
4510 ((fullname (concat directory filename))
4511 ;; possible completion structures
4512 (v (tramp-completion-dissect-file-name fullname))
4513 result result1)
4515 (while v
4516 (let* ((car (car v))
4517 (multi-method (tramp-file-name-multi-method car))
4518 (method (tramp-file-name-method car))
4519 (user (tramp-file-name-user car))
4520 (host (tramp-file-name-host car))
4521 (localname (tramp-file-name-localname car))
4522 (m (tramp-find-method multi-method method user host))
4523 (tramp-current-user user) ; see `tramp-parse-passwd'
4524 all-user-hosts)
4526 (unless (or multi-method ;; Not handled (yet).
4527 localname) ;; Nothing to complete
4529 (if (or user host)
4531 ;; Method dependent user / host combinations
4532 (progn
4533 (mapcar
4534 (lambda (x)
4535 (setq all-user-hosts
4536 (append all-user-hosts
4537 (funcall (nth 0 x) (nth 1 x)))))
4538 (tramp-get-completion-function m))
4540 (setq result (append result
4541 (mapcar
4542 (lambda (x)
4543 (tramp-get-completion-user-host
4544 method user host (nth 0 x) (nth 1 x)))
4545 (delq nil all-user-hosts)))))
4547 ;; Possible methods
4548 (setq result
4549 (append result (tramp-get-completion-methods m)))))
4551 (setq v (cdr v))))
4553 ;; unify list, remove nil elements
4554 (while result
4555 (let ((car (car result)))
4556 (when car (add-to-list
4557 'result1 (substring car (length directory))))
4558 (setq result (cdr result))))
4560 ;; Complete local parts
4561 (append
4562 result1
4563 (condition-case nil
4564 (if result1
4565 ;; "/ssh:" does not need to be expanded as hostname.
4566 (tramp-run-real-handler
4567 'file-name-all-completions (list filename directory))
4568 ;; No method/user/host found to be expanded.
4569 (tramp-completion-run-real-handler
4570 'file-name-all-completions (list filename directory)))
4571 (error nil)))))
4572 ;; unwindform
4573 (setq tramp-completion-mode nil)))
4575 ;; Method, host name and user name completion for a file.
4576 ;;;###autoload
4577 (defun tramp-completion-handle-file-name-completion (filename directory)
4578 "Like `file-name-completion' for tramp files."
4579 (try-completion filename
4580 (mapcar 'list (file-name-all-completions filename directory))))
4582 ;; I misuse a little bit the tramp-file-name structure in order to handle
4583 ;; completion possibilities for partial methods / user names / host names.
4584 ;; Return value is a list of tramp-file-name structures according to possible
4585 ;; completions. If "multi-method" or "localname" is non-nil it means there
4586 ;; shouldn't be a completion anymore.
4588 ;; Expected results:
4590 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4591 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4592 ;; [nil nil "x" nil nil]
4593 ;; [nil "x" nil nil nil]
4595 ;; "/x:" "/x:y" "/x:y:"
4596 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4597 ;; "/[x/" "/[x/y"
4598 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4599 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4601 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4602 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4603 ;; "/[x/y@" "/[x/y@z"
4604 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4605 (defun tramp-completion-dissect-file-name (name)
4606 "Returns a list of `tramp-file-name' structures.
4607 They are collected by `tramp-completion-dissect-file-name1'."
4609 (let* ((result)
4610 (x-nil "\\|\\(\\)")
4611 ;; "/method" "/[method"
4612 (tramp-completion-file-name-structure1
4613 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4614 1 nil nil nil))
4615 ;; "/user" "/[user"
4616 (tramp-completion-file-name-structure2
4617 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4618 nil 1 nil nil))
4619 ;; "/host" "/[host"
4620 (tramp-completion-file-name-structure3
4621 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4622 nil nil 1 nil))
4623 ;; "/user@host" "/[user@host"
4624 (tramp-completion-file-name-structure4
4625 (list (concat tramp-prefix-regexp
4626 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4627 "\\(" tramp-host-regexp x-nil "\\)$")
4628 nil 1 2 nil))
4629 ;; "/method:user" "/[method/user"
4630 (tramp-completion-file-name-structure5
4631 (list (concat tramp-prefix-regexp
4632 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4633 "\\(" tramp-user-regexp x-nil "\\)$")
4634 1 2 nil nil))
4635 ;; "/method:host" "/[method/host"
4636 (tramp-completion-file-name-structure6
4637 (list (concat tramp-prefix-regexp
4638 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4639 "\\(" tramp-host-regexp x-nil "\\)$")
4640 1 nil 2 nil))
4641 ;; "/method:user@host" "/[method/user@host"
4642 (tramp-completion-file-name-structure7
4643 (list (concat tramp-prefix-regexp
4644 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4645 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4646 "\\(" tramp-host-regexp x-nil "\\)$")
4647 1 2 3 nil)))
4649 (mapcar (lambda (regexp)
4650 (add-to-list 'result
4651 (tramp-completion-dissect-file-name1 regexp name)))
4652 (list
4653 tramp-completion-file-name-structure1
4654 tramp-completion-file-name-structure2
4655 tramp-completion-file-name-structure3
4656 tramp-completion-file-name-structure4
4657 tramp-completion-file-name-structure5
4658 tramp-completion-file-name-structure6
4659 tramp-completion-file-name-structure7
4660 tramp-file-name-structure))
4662 (delq nil result)))
4664 (defun tramp-completion-dissect-file-name1 (structure name)
4665 "Returns a `tramp-file-name' structure matching STRUCTURE.
4666 The structure consists of multi-method, remote method, remote user,
4667 remote host and localname (filename on remote host)."
4669 (let (method)
4670 (save-match-data
4671 (when (string-match (nth 0 structure) name)
4672 (setq method (and (nth 1 structure)
4673 (match-string (nth 1 structure) name)))
4674 (if (and method (member method tramp-multi-methods))
4675 ;; Not handled (yet).
4676 (make-tramp-file-name
4677 :multi-method method
4678 :method nil
4679 :user nil
4680 :host nil
4681 :localname nil)
4682 (let ((user (and (nth 2 structure)
4683 (match-string (nth 2 structure) name)))
4684 (host (and (nth 3 structure)
4685 (match-string (nth 3 structure) name)))
4686 (localname (and (nth 4 structure)
4687 (match-string (nth 4 structure) name))))
4688 (make-tramp-file-name
4689 :multi-method nil
4690 :method method
4691 :user user
4692 :host host
4693 :localname localname)))))))
4695 ;; This function returns all possible method completions, adding the
4696 ;; trailing method delimeter.
4697 (defun tramp-get-completion-methods (partial-method)
4698 "Returns all method completions for PARTIAL-METHOD."
4699 (mapcar
4700 (lambda (method)
4701 (and method
4702 (string-match (concat "^" (regexp-quote partial-method)) method)
4703 (tramp-make-tramp-file-name nil method nil nil nil)))
4704 (delete "multi" (mapcar 'car tramp-methods))))
4706 ;; Compares partial user and host names with possible completions.
4707 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4708 "Returns the most expanded string for user and host name completion.
4709 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4710 (cond
4712 ((and partial-user partial-host)
4713 (if (and host
4714 (string-match (concat "^" (regexp-quote partial-host)) host)
4715 (string-equal partial-user (or user partial-user)))
4716 (setq user partial-user)
4717 (setq user nil
4718 host nil)))
4720 (partial-user
4721 (setq host nil)
4722 (unless
4723 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4724 (setq user nil)))
4726 (partial-host
4727 (setq user nil)
4728 (unless
4729 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4730 (setq host nil)))
4732 (t (setq user nil
4733 host nil)))
4735 (unless (zerop (+ (length user) (length host)))
4736 (tramp-make-tramp-file-name nil method user host nil)))
4738 (defun tramp-parse-rhosts (filename)
4739 "Return a list of (user host) tuples allowed to access.
4740 Either user or host may be nil."
4742 (let (res)
4743 (when (file-readable-p filename)
4744 (with-temp-buffer
4745 (insert-file-contents filename)
4746 (goto-char (point-min))
4747 (while (not (eobp))
4748 (push (tramp-parse-rhosts-group) res))))
4749 res))
4751 ;; Taken from gnus/netrc.el
4752 (eval-and-compile
4753 (defalias 'tramp-point-at-eol
4754 (if (fboundp 'point-at-eol)
4755 'point-at-eol
4756 'line-end-position)))
4758 (defun tramp-parse-rhosts-group ()
4759 "Return a (user host) tuple allowed to access.
4760 Either user or host may be nil."
4762 (let ((result)
4763 (regexp
4764 (concat
4765 "^\\(" tramp-host-regexp "\\)"
4766 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4768 (narrow-to-region (point) (tramp-point-at-eol))
4769 (when (re-search-forward regexp nil t)
4770 (setq result (append (list (match-string 3) (match-string 1)))))
4771 (widen)
4772 (forward-line 1)
4773 result))
4775 (defun tramp-parse-shosts (filename)
4776 "Return a list of (user host) tuples allowed to access.
4777 User is always nil."
4779 (let (res)
4780 (when (file-readable-p filename)
4781 (with-temp-buffer
4782 (insert-file-contents filename)
4783 (goto-char (point-min))
4784 (while (not (eobp))
4785 (push (tramp-parse-shosts-group) res))))
4786 res))
4788 (defun tramp-parse-shosts-group ()
4789 "Return a (user host) tuple allowed to access.
4790 User is always nil."
4792 (let ((result)
4793 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4795 (narrow-to-region (point) (tramp-point-at-eol))
4796 (when (re-search-forward regexp nil t)
4797 (setq result (list nil (match-string 1))))
4798 (widen)
4800 (> (skip-chars-forward ",") 0)
4801 (forward-line 1))
4802 result))
4804 (defun tramp-parse-sconfig (filename)
4805 "Return a list of (user host) tuples allowed to access.
4806 User is always nil."
4808 (let (res)
4809 (when (file-readable-p filename)
4810 (with-temp-buffer
4811 (insert-file-contents filename)
4812 (goto-char (point-min))
4813 (while (not (eobp))
4814 (push (tramp-parse-sconfig-group) res))))
4815 res))
4817 (defun tramp-parse-sconfig-group ()
4818 "Return a (user host) tuple allowed to access.
4819 User is always nil."
4821 (let ((result)
4822 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4824 (narrow-to-region (point) (tramp-point-at-eol))
4825 (when (re-search-forward regexp nil t)
4826 (setq result (list nil (match-string 1))))
4827 (widen)
4829 (> (skip-chars-forward ",") 0)
4830 (forward-line 1))
4831 result))
4833 (defun tramp-parse-shostkeys (dirname)
4834 "Return a list of (user host) tuples allowed to access.
4835 User is always nil."
4837 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4838 (files (when (file-directory-p dirname) (directory-files dirname)))
4839 result)
4841 (while files
4842 (when (string-match regexp (car files))
4843 (push (list nil (match-string 1 (car files))) result))
4844 (setq files (cdr files)))
4845 result))
4847 (defun tramp-parse-sknownhosts (dirname)
4848 "Return a list of (user host) tuples allowed to access.
4849 User is always nil."
4851 (let ((regexp (concat "^\\(" tramp-host-regexp
4852 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4853 (files (when (file-directory-p dirname) (directory-files dirname)))
4854 result)
4856 (while files
4857 (when (string-match regexp (car files))
4858 (push (list nil (match-string 1 (car files))) result))
4859 (setq files (cdr files)))
4860 result))
4862 (defun tramp-parse-hosts (filename)
4863 "Return a list of (user host) tuples allowed to access.
4864 User is always nil."
4866 (let (res)
4867 (when (file-readable-p filename)
4868 (with-temp-buffer
4869 (insert-file-contents filename)
4870 (goto-char (point-min))
4871 (while (not (eobp))
4872 (push (tramp-parse-hosts-group) res))))
4873 res))
4875 (defun tramp-parse-hosts-group ()
4876 "Return a (user host) tuple allowed to access.
4877 User is always nil."
4879 (let ((result)
4880 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4882 (narrow-to-region (point) (tramp-point-at-eol))
4883 (when (re-search-forward regexp nil t)
4884 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4885 (setq result (list nil (match-string 1)))))
4886 (widen)
4888 (> (skip-chars-forward " \t") 0)
4889 (forward-line 1))
4890 result))
4892 ;; For su-alike methods it would be desirable to return "root@localhost"
4893 ;; as default. Unfortunately, we have no information whether any user name
4894 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4895 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4896 (defun tramp-parse-passwd (filename)
4897 "Return a list of (user host) tuples allowed to access.
4898 Host is always \"localhost\"."
4900 (let (res)
4901 (if (zerop (length tramp-current-user))
4902 '(("root" nil))
4903 (when (file-readable-p filename)
4904 (with-temp-buffer
4905 (insert-file-contents filename)
4906 (goto-char (point-min))
4907 (while (not (eobp))
4908 (push (tramp-parse-passwd-group) res))))
4909 res)))
4911 (defun tramp-parse-passwd-group ()
4912 "Return a (user host) tuple allowed to access.
4913 Host is always \"localhost\"."
4915 (let ((result)
4916 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4918 (narrow-to-region (point) (tramp-point-at-eol))
4919 (when (re-search-forward regexp nil t)
4920 (setq result (list (match-string 1) "localhost")))
4921 (widen)
4922 (forward-line 1)
4923 result))
4925 (defun tramp-parse-netrc (filename)
4926 "Return a list of (user host) tuples allowed to access.
4927 User may be nil."
4929 (let (res)
4930 (when (file-readable-p filename)
4931 (with-temp-buffer
4932 (insert-file-contents filename)
4933 (goto-char (point-min))
4934 (while (not (eobp))
4935 (push (tramp-parse-netrc-group) res))))
4936 res))
4938 (defun tramp-parse-netrc-group ()
4939 "Return a (user host) tuple allowed to access.
4940 User may be nil."
4942 (let ((result)
4943 (regexp
4944 (concat
4945 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4946 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4948 (narrow-to-region (point) (tramp-point-at-eol))
4949 (when (re-search-forward regexp nil t)
4950 (setq result (list (match-string 3) (match-string 1))))
4951 (widen)
4952 (forward-line 1)
4953 result))
4955 ;;; Internal Functions:
4957 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
4958 "Define in remote shell function NAME implemented as perl SCRIPT.
4959 Only send the definition if it has not already been done.
4960 Function may have 0-3 parameters."
4961 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4962 (unless remote-perl (error "No remote perl"))
4963 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4964 multi-method method user host)))
4965 (unless (memq name perl-scripts)
4966 (with-current-buffer (tramp-get-buffer multi-method method user host)
4967 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4968 (tramp-send-string multi-method method user host
4969 (concat name
4970 " () {\n"
4971 remote-perl
4972 " -e '"
4973 script
4974 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4975 (tramp-wait-for-output)
4976 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4977 multi-method method user host)
4978 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4980 (defun tramp-set-auto-save ()
4981 (when (and (buffer-file-name)
4982 (tramp-tramp-file-p (buffer-file-name))
4983 ;; ange-ftp has its own auto-save mechanism
4984 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
4985 'tramp-sh-file-name-handler)
4986 auto-save-default)
4987 (auto-save-mode 1)))
4988 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4989 (add-hook 'tramp-unload-hook
4990 '(lambda ()
4991 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
4993 (defun tramp-run-test (switch filename)
4994 "Run `test' on the remote system, given a SWITCH and a FILENAME.
4995 Returns the exit code of the `test' program."
4996 (let ((v (tramp-dissect-file-name filename)))
4997 (save-excursion
4998 (tramp-send-command-and-check
4999 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5000 (tramp-file-name-user v) (tramp-file-name-host v)
5001 (format "test %s %s" switch
5002 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
5004 (defun tramp-run-test2 (program file1 file2 &optional switch)
5005 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5006 The optional SWITCH is inserted between the two files.
5007 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5008 hosts, or files, disagree."
5009 (let* ((v1 (tramp-dissect-file-name file1))
5010 (v2 (tramp-dissect-file-name file2))
5011 (mmethod1 (tramp-file-name-multi-method v1))
5012 (mmethod2 (tramp-file-name-multi-method v2))
5013 (method1 (tramp-file-name-method v1))
5014 (method2 (tramp-file-name-method v2))
5015 (user1 (tramp-file-name-user v1))
5016 (user2 (tramp-file-name-user v2))
5017 (host1 (tramp-file-name-host v1))
5018 (host2 (tramp-file-name-host v2))
5019 (localname1 (tramp-file-name-localname v1))
5020 (localname2 (tramp-file-name-localname v2)))
5021 (unless (and method1 method2 host1 host2
5022 (equal mmethod1 mmethod2)
5023 (equal method1 method2)
5024 (equal user1 user2)
5025 (equal host1 host2))
5026 (error "tramp-run-test2: %s"
5027 "only implemented for same method, same user, same host"))
5028 (save-excursion
5029 (tramp-send-command-and-check
5030 mmethod1 method1 user1 host1
5031 (format "%s %s %s %s"
5032 program
5033 (tramp-shell-quote-argument localname1)
5034 (or switch "")
5035 (tramp-shell-quote-argument localname2))))))
5037 (defun tramp-touch (file time)
5038 "Set the last-modified timestamp of the given file.
5039 TIME is an Emacs internal time value as returned by `current-time'."
5040 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time t)))
5041 (if (tramp-tramp-file-p file)
5042 (with-parsed-tramp-file-name file nil
5043 (let ((buf (tramp-get-buffer multi-method method user host)))
5044 (unless (zerop (tramp-send-command-and-check
5045 multi-method method user host
5046 (format "TZ=UTC; export TZ; touch -t %s %s"
5047 touch-time
5048 localname)
5050 (pop-to-buffer buf)
5051 (error "tramp-touch: touch failed, see buffer `%s' for details"
5052 buf))))
5053 ;; It's a local file
5054 (with-temp-buffer
5055 (unless (zerop (call-process
5056 "touch" nil (current-buffer) nil "-t" touch-time file))
5057 (pop-to-buffer (current-buffer))
5058 (error "tramp-touch: touch failed"))))))
5060 (defun tramp-buffer-name (multi-method method user host)
5061 "A name for the connection buffer for USER at HOST using METHOD."
5062 (if multi-method
5063 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5064 (let ((method (tramp-find-method multi-method method user host)))
5065 (if user
5066 (format "*tramp/%s %s@%s*" method user host)
5067 (format "*tramp/%s %s*" method host)))))
5069 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5070 "A name for the multi method connection buffer.
5071 MULTI-METHOD gives the multi method, METHOD the array of methods,
5072 USER the array of user names, HOST the array of host names."
5073 (unless (and (= (length method) (length user))
5074 (= (length method) (length host)))
5075 (error "Syntax error in multi method (implementation error)"))
5076 (let ((len (length method))
5077 (i 0)
5078 string-list)
5079 (while (< i len)
5080 (setq string-list
5081 (cons (if (aref user i)
5082 (format "%s#%s@%s:" (aref method i)
5083 (aref user i) (aref host i))
5084 (format "%s@%s:" (aref method i) (aref host i)))
5085 string-list))
5086 (incf i))
5087 (format "*%s/%s %s*"
5088 prefix multi-method
5089 (apply 'concat (reverse string-list)))))
5091 (defun tramp-get-buffer (multi-method method user host)
5092 "Get the connection buffer to be used for USER at HOST using METHOD."
5093 (with-current-buffer
5094 (get-buffer-create (tramp-buffer-name multi-method method user host))
5095 (setq buffer-undo-list t)
5096 (current-buffer)))
5098 (defun tramp-debug-buffer-name (multi-method method user host)
5099 "A name for the debug buffer for USER at HOST using METHOD."
5100 (if multi-method
5101 (tramp-buffer-name-multi-method "debug tramp"
5102 multi-method method user host)
5103 (let ((method (tramp-find-method multi-method method user host)))
5104 (if user
5105 (format "*debug tramp/%s %s@%s*" method user host)
5106 (format "*debug tramp/%s %s*" method host)))))
5108 (defun tramp-get-debug-buffer (multi-method method user host)
5109 "Get the debug buffer for USER at HOST using METHOD."
5110 (with-current-buffer
5111 (get-buffer-create
5112 (tramp-debug-buffer-name multi-method method user host))
5113 (setq buffer-undo-list t)
5114 (current-buffer)))
5116 (defun tramp-find-executable (multi-method method user host
5117 progname dirlist ignore-tilde)
5118 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5119 First args METHOD, USER and HOST specify the connection, PROGNAME
5120 is the program to search for, and DIRLIST gives the list of directories
5121 to search. If IGNORE-TILDE is non-nil, directory names starting
5122 with `~' will be ignored.
5124 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5126 This function expects to be in the right *tramp* buffer."
5127 (let (result)
5128 (when ignore-tilde
5129 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5130 ;; `remove' is in CL, and we want to avoid CL dependencies.
5131 (let (newdl d)
5132 (while dirlist
5133 (setq d (car dirlist))
5134 (setq dirlist (cdr dirlist))
5135 (unless (char-equal ?~ (aref d 0))
5136 (setq newdl (cons d newdl))))
5137 (setq dirlist (nreverse newdl))))
5138 (tramp-send-command
5139 multi-method method user host
5140 (format (concat "while read d; "
5141 "do if test -x $d/%s -a -f $d/%s; "
5142 "then echo tramp_executable $d/%s; "
5143 "break; fi; done <<'EOF'")
5144 progname progname progname))
5145 (mapcar (lambda (d)
5146 (tramp-send-command multi-method method user host d))
5147 dirlist)
5148 (tramp-send-command multi-method method user host "EOF")
5149 (tramp-wait-for-output)
5150 (goto-char (point-max))
5151 (when (search-backward "tramp_executable " nil t)
5152 (skip-chars-forward "^ ")
5153 (skip-chars-forward " ")
5154 (buffer-substring (point) (tramp-line-end-position)))))
5156 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5157 "Sets the remote environment VAR to existing directories from DIRLIST.
5158 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5159 so, it is added to the environment variable VAR."
5160 (let ((existing-dirs
5161 (mapcar
5162 (lambda (x)
5163 (when (and
5164 (file-exists-p
5165 (tramp-make-tramp-file-name multi-method method user host x))
5166 (file-directory-p
5167 (tramp-make-tramp-file-name multi-method method user host x)))
5169 dirlist)))
5170 (tramp-send-command
5171 multi-method method user host
5172 (concat var "="
5173 (mapconcat 'identity (delq nil existing-dirs) ":")
5174 "; export " var))
5175 (tramp-wait-for-output)))
5177 ;; -- communication with external shell --
5179 (defun tramp-find-file-exists-command (multi-method method user host)
5180 "Find a command on the remote host for checking if a file exists.
5181 Here, we are looking for a command which has zero exit status if the
5182 file exists and nonzero exit status otherwise."
5183 (make-local-variable 'tramp-file-exists-command)
5184 (tramp-message 9 "Finding command to check if file exists")
5185 (let ((existing
5186 (tramp-make-tramp-file-name
5187 multi-method method user host
5188 "/")) ;assume this file always exists
5189 (nonexisting
5190 (tramp-make-tramp-file-name
5191 multi-method method user host
5192 "/ this file does not exist "))) ;assume this never exists
5193 ;; The algorithm is as follows: we try a list of several commands.
5194 ;; For each command, we first run `$cmd /' -- this should return
5195 ;; true, as the root directory always exists. And then we run
5196 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5197 ;; does not exist. This should return false. We use the first
5198 ;; command we find that seems to work.
5199 ;; The list of commands to try is as follows:
5200 ;; `ls -d' This works on most systems, but NetBSD 1.4
5201 ;; has a bug: `ls' always returns zero exit
5202 ;; status, even for files which don't exist.
5203 ;; `test -e' Some Bourne shells have a `test' builtin
5204 ;; which does not know the `-e' option.
5205 ;; `/bin/test -e' For those, the `test' binary on disk normally
5206 ;; provides the option. Alas, the binary
5207 ;; is sometimes `/bin/test' and sometimes it's
5208 ;; `/usr/bin/test'.
5209 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5210 (unless (or
5211 (and (setq tramp-file-exists-command "test -e %s")
5212 (file-exists-p existing)
5213 (not (file-exists-p nonexisting)))
5214 (and (setq tramp-file-exists-command "/bin/test -e %s")
5215 (file-exists-p existing)
5216 (not (file-exists-p nonexisting)))
5217 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5218 (file-exists-p existing)
5219 (not (file-exists-p nonexisting)))
5220 (and (setq tramp-file-exists-command "ls -d %s")
5221 (file-exists-p existing)
5222 (not (file-exists-p nonexisting))))
5223 (error "Couldn't find command to check if file exists"))))
5226 ;; CCC test ksh or bash found for tilde expansion?
5227 (defun tramp-find-shell (multi-method method user host)
5228 "Find a shell on the remote host which groks tilde expansion."
5229 (let ((shell nil))
5230 (tramp-send-command multi-method method user host "echo ~root")
5231 (tramp-wait-for-output)
5232 (cond
5233 ((string-match "^~root$" (buffer-string))
5234 (setq shell
5235 (or (tramp-find-executable multi-method method user host
5236 "bash" tramp-remote-path t)
5237 (tramp-find-executable multi-method method user host
5238 "ksh" tramp-remote-path t)))
5239 (unless shell
5240 (error "Couldn't find a shell which groks tilde expansion"))
5241 ;; Find arguments for this shell.
5242 (let ((alist tramp-sh-extra-args)
5243 item extra-args)
5244 (while (and alist (null extra-args))
5245 (setq item (pop alist))
5246 (when (string-match (car item) shell)
5247 (setq extra-args (cdr item))))
5248 (when extra-args (setq shell (concat shell " " extra-args))))
5249 (tramp-message
5250 5 "Starting remote shell `%s' for tilde expansion..." shell)
5251 (tramp-send-command
5252 multi-method method user host
5253 (concat "PS1='$ ' exec " shell)) ;
5254 (tramp-barf-if-no-shell-prompt
5255 (get-buffer-process (current-buffer))
5256 60 "Couldn't find remote `%s' prompt" shell)
5257 (tramp-message
5258 9 "Setting remote shell prompt...")
5259 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5260 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5261 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5262 ;; as well.
5263 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5264 tramp-rsh-end-of-line
5265 tramp-end-of-output
5266 tramp-rsh-end-of-line
5267 tramp-rsh-end-of-line))
5268 (tramp-wait-for-output)
5269 (tramp-message
5270 9 "Setting remote shell prompt...done")
5272 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5273 (tramp-get-method-parameter
5274 multi-method method user host 'tramp-remote-sh))))))
5276 (defun tramp-check-ls-command (multi-method method user host cmd)
5277 "Checks whether the given `ls' executable groks `-n'.
5278 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5279 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5280 otherwise."
5281 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5282 (when (file-executable-p
5283 (tramp-make-tramp-file-name multi-method method user host cmd))
5284 (let ((result nil))
5285 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5286 (setq result
5287 (tramp-send-command-and-check
5288 multi-method method user host
5289 (format "%s -lnd / >/dev/null"
5290 cmd)))
5291 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5293 (if (zerop result) "okay" "failed"))
5294 (zerop result))))
5296 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5297 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5298 Returns nil if none was found, else the command is returned."
5299 (let ((dl dirlist)
5300 (result nil))
5301 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5302 ;; It would be better to use the CL function `find', but
5303 ;; we don't want run-time dependencies on CL.
5304 (while (and dl (not result))
5305 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5306 (when (tramp-check-ls-command multi-method method user host x)
5307 (setq result x)))
5308 (setq dl (cdr dl)))
5309 result)))
5311 (defun tramp-find-ls-command (multi-method method user host)
5312 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5313 \(This option prints numeric user and group ids in a long listing.)"
5314 (tramp-message 9 "Finding a suitable `ls' command")
5316 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5317 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5318 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5320 ;; ------------------------------------------------------------
5321 ;; -- Functions for establishing connection --
5322 ;; ------------------------------------------------------------
5324 ;; The following functions are actions to be taken when seeing certain
5325 ;; prompts from the remote host. See the variable
5326 ;; `tramp-actions-before-shell' for usage of these functions.
5328 (defun tramp-action-login (p multi-method method user host)
5329 "Send the login name."
5330 (tramp-message 9 "Sending login name `%s'"
5331 (or user (user-login-name)))
5332 (erase-buffer)
5333 (process-send-string nil (concat (or user (user-login-name))
5334 tramp-rsh-end-of-line)))
5336 (defun tramp-action-password (p multi-method method user host)
5337 "Query the user for a password."
5338 (let ((pw-prompt
5339 (format "Password for %s "
5340 (tramp-make-tramp-file-name
5341 nil method user host ""))))
5342 (tramp-message 9 "Sending password")
5343 (tramp-enter-password p pw-prompt user host)))
5345 (defun tramp-action-succeed (p multi-method method user host)
5346 "Signal success in finding shell prompt."
5347 (tramp-message 9 "Found remote shell prompt.")
5348 (erase-buffer)
5349 (throw 'tramp-action 'ok))
5351 (defun tramp-action-permission-denied (p multi-method method user host)
5352 "Signal permission denied."
5353 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5354 (tramp-message 9 "Permission denied by remote host.")
5355 (kill-process p)
5356 (throw 'tramp-action 'permission-denied))
5358 (defun tramp-action-yesno (p multi-method method user host)
5359 "Ask the user for confirmation using `yes-or-no-p'.
5360 Send \"yes\" to remote process on confirmation, abort otherwise.
5361 See also `tramp-action-yn'."
5362 (save-window-excursion
5363 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5364 (unless (yes-or-no-p (match-string 0))
5365 (kill-process p)
5366 (erase-buffer)
5367 (throw 'tramp-action 'permission-denied))
5368 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5369 (erase-buffer)))
5371 (defun tramp-action-yn (p multi-method method user host)
5372 "Ask the user for confirmation using `y-or-n-p'.
5373 Send \"y\" to remote process on confirmation, abort otherwise.
5374 See also `tramp-action-yesno'."
5375 (save-window-excursion
5376 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5377 (unless (y-or-n-p (match-string 0))
5378 (kill-process p)
5379 (throw 'tramp-action 'permission-denied))
5380 (erase-buffer)
5381 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5383 (defun tramp-action-terminal (p multi-method method user host)
5384 "Tell the remote host which terminal type to use.
5385 The terminal type can be configured with `tramp-terminal-type'."
5386 (tramp-message 9 "Setting `%s' as terminal type."
5387 tramp-terminal-type)
5388 (erase-buffer)
5389 (process-send-string nil (concat tramp-terminal-type
5390 tramp-rsh-end-of-line)))
5392 (defun tramp-action-process-alive (p multi-method method user host)
5393 "Check whether a process has finished."
5394 (unless (memq (process-status p) '(run open))
5395 (throw 'tramp-action 'process-died)))
5397 (defun tramp-action-out-of-band (p multi-method method user host)
5398 "Check whether an out-of-band copy has finished."
5399 (cond ((and (memq (process-status p) '(stop exit))
5400 (zerop (process-exit-status p)))
5401 (tramp-message 9 "Process has finished.")
5402 (throw 'tramp-action 'ok))
5403 ((or (and (memq (process-status p) '(stop exit))
5404 (not (zerop (process-exit-status p))))
5405 (memq (process-status p) '(signal)))
5406 ;; `scp' could have copied correctly, but set modes could have failed.
5407 ;; This can be ignored.
5408 (goto-char (point-min))
5409 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5410 (progn
5411 (tramp-message 10 "'set mode' error ignored.")
5412 (tramp-message 9 "Process has finished.")
5413 (throw 'tramp-action 'ok))
5414 (goto-char (point-min))
5415 (when (re-search-forward "^.cp.?: \\(.+: Permission denied.?\\)$" nil t)
5416 (error "Remote host: %s" (match-string 1)))
5417 (tramp-message 9 "Process has died.")
5418 (throw 'tramp-action 'process-died)))
5419 (t nil)))
5421 ;; The following functions are specifically for multi connections.
5423 (defun tramp-multi-action-login (p method user host)
5424 "Send the login name."
5425 (tramp-message 9 "Sending login name `%s'" user)
5426 (erase-buffer)
5427 (process-send-string p (concat user tramp-rsh-end-of-line)))
5429 (defun tramp-multi-action-password (p method user host)
5430 "Query the user for a password."
5431 (let ((pw-prompt
5432 (format "Password for %s "
5433 (tramp-make-tramp-file-name
5434 nil method user host ""))))
5435 (tramp-message 9 "Sending password")
5436 (tramp-enter-password p pw-prompt user host)))
5438 (defun tramp-multi-action-succeed (p method user host)
5439 "Signal success in finding shell prompt."
5440 (tramp-message 9 "Found shell prompt on `%s'" host)
5441 (erase-buffer)
5442 (throw 'tramp-action 'ok))
5444 (defun tramp-multi-action-permission-denied (p method user host)
5445 "Signal permission denied."
5446 (tramp-message 9 "Permission denied by remote host `%s'" host)
5447 (kill-process p)
5448 (erase-buffer)
5449 (throw 'tramp-action 'permission-denied))
5451 (defun tramp-multi-action-process-alive (p method user host)
5452 "Check whether a process has finished."
5453 (unless (memq (process-status p) '(run open))
5454 (throw 'tramp-action 'process-died)))
5456 ;; Functions for processing the actions.
5458 (defun tramp-process-one-action (p multi-method method user host actions)
5459 "Wait for output from the shell and perform one action."
5460 (let (found item pattern action todo)
5461 (erase-buffer)
5462 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5463 (with-timeout (60 (throw 'tramp-action 'timeout))
5464 (while (not found)
5465 (tramp-accept-process-output p 1)
5466 (goto-char (point-min))
5467 (setq todo actions)
5468 (while todo
5469 (goto-char (point-min))
5470 (setq item (pop todo))
5471 (setq pattern (symbol-value (nth 0 item)))
5472 (setq action (nth 1 item))
5473 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5474 pattern)
5475 (when (re-search-forward (concat pattern "\\'") nil t)
5476 (setq found (funcall action p multi-method method user host)))))
5477 found)))
5479 (defun tramp-process-actions (p multi-method method user host actions)
5480 "Perform actions until success."
5481 (let (exit)
5482 (while (not exit)
5483 (tramp-message 9 "Waiting for prompts from remote shell")
5484 (setq exit
5485 (catch 'tramp-action
5486 (tramp-process-one-action
5487 p multi-method method user host actions)
5488 nil)))
5489 (unless (eq exit 'ok)
5490 (tramp-clear-passwd user host)
5491 (error "Login failed"))))
5493 ;; For multi-actions.
5495 (defun tramp-process-one-multi-action (p method user host actions)
5496 "Wait for output from the shell and perform one action."
5497 (let (found item pattern action todo)
5498 (erase-buffer)
5499 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5500 (with-timeout (60 (throw 'tramp-action 'timeout))
5501 (while (not found)
5502 (tramp-accept-process-output p 1)
5503 (setq todo actions)
5504 (goto-char (point-min))
5505 (while todo
5506 (goto-char (point-min))
5507 (setq item (pop todo))
5508 (setq pattern (symbol-value (nth 0 item)))
5509 (setq action (nth 1 item))
5510 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5511 pattern)
5512 (when (re-search-forward (concat pattern "\\'") nil t)
5513 (setq found (funcall action p method user host)))))
5514 found)))
5516 (defun tramp-process-multi-actions (p method user host actions)
5517 "Perform actions until success."
5518 (let (exit)
5519 (while (not exit)
5520 (tramp-message 9 "Waiting for prompts from remote shell")
5521 (setq exit
5522 (catch 'tramp-action
5523 (tramp-process-one-multi-action p method user host actions)
5524 nil)))
5525 (unless (eq exit 'ok)
5526 (tramp-clear-passwd user host)
5527 (error "Login failed"))))
5529 ;; Functions to execute when we have seen the remote shell prompt but
5530 ;; before we exec the Bourne-ish shell. Note that these commands
5531 ;; might be sent to any shell, not just a Bourne-ish shell. This
5532 ;; means that the commands need to work in all shells. (It is also
5533 ;; okay for some commands to just fail with an error message, but
5534 ;; please make sure that they at least don't crash the odd shell people
5535 ;; might be running...)
5536 (defun tramp-process-initial-commands (p
5537 multi-method method user host
5538 commands)
5539 "Send list of commands to remote host, in order."
5540 (let (cmd)
5541 (while commands
5542 (setq cmd (pop commands))
5543 (erase-buffer)
5544 (tramp-message 10 "Sending command to remote shell: %s"
5545 cmd)
5546 (tramp-send-command multi-method method user host cmd nil t)
5547 (tramp-barf-if-no-shell-prompt
5548 p 60 "Remote shell command failed: %s" cmd))
5549 (erase-buffer)))
5551 ;; The actual functions for opening connections.
5553 (defun tramp-open-connection-telnet (multi-method method user host)
5554 "Open a connection using a telnet METHOD.
5555 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5556 login prompt, then sends the user name USER, then waits for a remote
5557 password prompt. It queries the user for the password, then sends the
5558 password to the remote host.
5560 If USER is nil, uses value returned by `(user-login-name)' instead.
5562 Recognition of the remote shell prompt is based on the variables
5563 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5564 set up correctly.
5566 Please note that it is NOT possible to use this connection method
5567 together with an out-of-band transfer method! You must use an inline
5568 transfer method.
5570 Maybe the different regular expressions need to be tuned.
5572 * Actually, the telnet program as well as the args to be used can be
5573 specified in the method parameters, see the variable `tramp-methods'."
5574 (save-match-data
5575 (when (tramp-method-out-of-band-p multi-method method user host)
5576 (error "Cannot use out-of-band method `%s' with telnet connection method"
5577 method))
5578 (when multi-method
5579 (error "Cannot multi-connect using telnet connection method"))
5580 (tramp-pre-connection multi-method method user host tramp-chunksize)
5581 (tramp-message 7 "Opening connection for %s@%s using %s..."
5582 (or user (user-login-name)) host method)
5583 (let ((process-environment (copy-sequence process-environment)))
5584 (setenv "TERM" tramp-terminal-type)
5585 (setenv "PS1" "$ ")
5586 (let* ((default-directory (tramp-temporary-file-directory))
5587 ;; If we omit the conditional here, then we would use
5588 ;; `undecided-dos' in some cases. With the conditional,
5589 ;; we use nil in these cases. Which one is right?
5590 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5591 (> emacs-major-version 20))
5592 tramp-dos-coding-system))
5593 (p (apply 'start-process
5594 (tramp-buffer-name multi-method method user host)
5595 (tramp-get-buffer multi-method method user host)
5596 (tramp-get-method-parameter
5597 multi-method
5598 (tramp-find-method multi-method method user host)
5599 user host 'tramp-login-program)
5600 host
5601 (tramp-get-method-parameter
5602 multi-method
5603 (tramp-find-method multi-method method user host)
5604 user host 'tramp-login-args)))
5605 (found nil)
5606 (pw nil))
5607 (tramp-set-process-query-on-exit-flag p nil)
5608 (set-buffer (tramp-get-buffer multi-method method user host))
5609 (erase-buffer)
5610 (tramp-process-actions p multi-method method user host
5611 tramp-actions-before-shell)
5612 (tramp-open-connection-setup-interactive-shell
5613 p multi-method method user host)
5614 (tramp-post-connection multi-method method user host)))))
5617 (defun tramp-open-connection-rsh (multi-method method user host)
5618 "Open a connection using an rsh METHOD.
5619 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5620 password or shell prompt. If a password prompt is seen, the user is
5621 queried for a password, this function sends the password to the remote
5622 host and waits for a shell prompt.
5624 If USER is nil, start the command `rsh HOST'[*] instead
5626 Recognition of the remote shell prompt is based on the variables
5627 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5628 set up correctly.
5630 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5631 be a port number for ssh, and \"-p yy\" will be added to the list of
5632 arguments, and xx will be used as the host name to connect to.
5634 * Actually, the rsh program to be used can be specified in the
5635 method parameters, see the variable `tramp-methods'."
5636 (save-match-data
5637 (when multi-method
5638 (error "Cannot multi-connect using rsh connection method"))
5639 (tramp-pre-connection multi-method method user host tramp-chunksize)
5640 (if (and user (not (string= user "")))
5641 (tramp-message 7 "Opening connection for %s@%s using %s..."
5642 user host method)
5643 (tramp-message 7 "Opening connection at %s using %s..." host method))
5644 (let ((process-environment (copy-sequence process-environment))
5645 (bufnam (tramp-buffer-name multi-method method user host))
5646 (buf (tramp-get-buffer multi-method method user host))
5647 (login-program (tramp-get-method-parameter
5648 multi-method
5649 (tramp-find-method multi-method method user host)
5650 user host 'tramp-login-program))
5651 (login-args (tramp-get-method-parameter
5652 multi-method
5653 (tramp-find-method multi-method method user host)
5654 user host 'tramp-login-args))
5655 (real-host host))
5656 ;; The following should be changed. We need a more general
5657 ;; mechanism to parse extra host args.
5658 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5659 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5660 (setq real-host (match-string 1 host)))
5661 (setenv "TERM" tramp-terminal-type)
5662 (setenv "PS1" "$ ")
5663 (let* ((default-directory (tramp-temporary-file-directory))
5664 ;; If we omit the conditional, we would use
5665 ;; `undecided-dos' in some cases. With the conditional,
5666 ;; we use nil in these cases. Which one is right?
5667 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5668 (> emacs-major-version 20))
5669 tramp-dos-coding-system))
5670 (p (if (and user (not (string= user "")))
5671 (apply #'start-process bufnam buf login-program
5672 real-host "-l" user login-args)
5673 (apply #'start-process bufnam buf login-program
5674 real-host login-args)))
5675 (found nil))
5676 (tramp-set-process-query-on-exit-flag p nil)
5678 (set-buffer buf)
5679 (tramp-process-actions p multi-method method user host
5680 tramp-actions-before-shell)
5681 (tramp-message 7 "Initializing remote shell")
5682 (tramp-open-connection-setup-interactive-shell
5683 p multi-method method user host)
5684 (tramp-post-connection multi-method method user host)))))
5686 (defun tramp-open-connection-su (multi-method method user host)
5687 "Open a connection using the `su' program with METHOD.
5688 This starts `su - USER', then waits for a password prompt. The HOST
5689 name must be equal to the local host name or to `localhost'.
5691 If USER is nil, uses value returned by user-login-name instead.
5693 Recognition of the remote shell prompt is based on the variables
5694 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5695 set up correctly. Note that the other user may have a different shell
5696 prompt than you do, so it is not at all unlikely that the variable
5697 `shell-prompt-pattern' is set up wrongly!"
5698 (save-match-data
5699 (when (tramp-method-out-of-band-p multi-method method user host)
5700 (error "Cannot use out-of-band method `%s' with `su' connection method"
5701 method))
5702 (unless (or (string-match (concat "^" (regexp-quote host))
5703 (system-name))
5704 (string= "localhost" host)
5705 (string= "" host))
5706 (error
5707 "Cannot connect to different host `%s' with `su' connection method"
5708 host))
5709 (tramp-pre-connection multi-method method user host tramp-chunksize)
5710 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5711 (or user "<root>") method)
5712 (let ((process-environment (copy-sequence process-environment)))
5713 (setenv "TERM" tramp-terminal-type)
5714 (setenv "PS1" "$ ")
5715 (let* ((default-directory (tramp-temporary-file-directory))
5716 ;; If we omit the conditional, we use `undecided-dos' in
5717 ;; some cases. With the conditional, we use nil in these
5718 ;; cases. What's the difference? Which one is right?
5719 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5720 (> emacs-major-version 20))
5721 tramp-dos-coding-system))
5722 (p (apply 'start-process
5723 (tramp-buffer-name multi-method method user host)
5724 (tramp-get-buffer multi-method method user host)
5725 (tramp-get-method-parameter
5726 multi-method
5727 (tramp-find-method multi-method method user host)
5728 user host 'tramp-login-program)
5729 (mapcar
5730 (lambda (x)
5731 (format-spec x `((?u . ,(or user "root")))))
5732 (tramp-get-method-parameter
5733 multi-method
5734 (tramp-find-method multi-method method user host)
5735 user host 'tramp-login-args))))
5736 (found nil)
5737 (pw nil))
5738 (tramp-set-process-query-on-exit-flag p nil)
5739 (set-buffer (tramp-get-buffer multi-method method user host))
5740 (tramp-process-actions p multi-method method user host
5741 tramp-actions-before-shell)
5742 (tramp-open-connection-setup-interactive-shell
5743 p multi-method method user host)
5744 (tramp-post-connection multi-method method
5745 user host)))))
5747 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5748 ;; handle not giving a user name in the "file name".
5750 ;; This is more difficult than for the single-hop method. In the
5751 ;; multi-hop-method, the desired behaviour should be that the
5752 ;; user must specify names for the telnet hops of which the user
5753 ;; name is different than the "original" name (or different from
5754 ;; the previous hop.
5755 (defun tramp-open-connection-multi (multi-method method user host)
5756 "Open a multi-hop connection using METHOD.
5757 This uses a slightly changed file name syntax. The idea is to say
5758 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5759 This will use telnet to log in as u1 to h1, then use rsh from there to
5760 log in as u2 to h2."
5761 (save-match-data
5762 (unless multi-method
5763 (error "Multi-hop open connection function called on non-multi method"))
5764 (when (tramp-method-out-of-band-p multi-method method user host)
5765 (error "No out of band multi-hop connections"))
5766 (unless (and (arrayp method) (not (stringp method)))
5767 (error "METHOD must be an array of strings for multi methods"))
5768 (unless (and (arrayp user) (not (stringp user)))
5769 (error "USER must be an array of strings for multi methods"))
5770 (unless (and (arrayp host) (not (stringp host)))
5771 (error "HOST must be an array of strings for multi methods"))
5772 (unless (and (= (length method) (length user))
5773 (= (length method) (length host)))
5774 (error "Arrays METHOD, USER, HOST must have equal length"))
5775 (tramp-pre-connection multi-method method user host tramp-chunksize)
5776 (tramp-message 7 "Opening `%s' connection..." multi-method)
5777 (let ((process-environment (copy-sequence process-environment)))
5778 (setenv "TERM" tramp-terminal-type)
5779 (setenv "PS1" "$ ")
5780 (let* ((default-directory (tramp-temporary-file-directory))
5781 ;; If we omit the conditional, we use `undecided-dos' in
5782 ;; some cases. With the conditional, we use nil in these
5783 ;; cases. What's the difference? Which one is right?
5784 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5785 (> emacs-major-version 20))
5786 tramp-dos-coding-system))
5787 (p (start-process (tramp-buffer-name multi-method method user host)
5788 (tramp-get-buffer multi-method method user host)
5789 tramp-multi-sh-program))
5790 (num-hops (length method))
5791 (i 0))
5792 (tramp-set-process-query-on-exit-flag p nil)
5793 (tramp-message 9 "Waiting 60s for local shell to come up...")
5794 (unless (tramp-wait-for-regexp
5795 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5796 shell-prompt-pattern tramp-shell-prompt-pattern))
5797 (pop-to-buffer (buffer-name))
5798 (kill-process p)
5799 (error "Couldn't find local shell prompt"))
5800 ;; Now do all the connections as specified.
5801 (while (< i num-hops)
5802 (let* ((m (aref method i))
5803 (u (aref user i))
5804 (h (aref host i))
5805 (entry (assoc m tramp-multi-connection-function-alist))
5806 (multi-func (nth 1 entry))
5807 (command (nth 2 entry)))
5808 ;; The multi-funcs don't need to do save-match-data, as that
5809 ;; is done here.
5810 (funcall multi-func p m u h command)
5811 (erase-buffer)
5812 (incf i)))
5813 (tramp-open-connection-setup-interactive-shell
5814 p multi-method method user host)
5815 (tramp-post-connection multi-method method user host)))))
5817 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5818 ;; of no user name provided. Hack to make it work as it did before:
5819 ;; changed `user' to `(or user (user-login-name))' in the places where
5820 ;; the value is actually used.
5821 (defun tramp-multi-connect-telnet (p method user host command)
5822 "Issue `telnet' command.
5823 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5824 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5825 the host name, and `%n' is replaced with an end of line character, as
5826 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5827 character.
5829 If USER is nil, uses the return value of (user-login-name) instead."
5830 (let ((cmd (format-spec command
5831 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5832 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5833 found pw)
5834 (erase-buffer)
5835 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5836 (process-send-string p cmd)
5837 (tramp-process-multi-actions p method user host
5838 tramp-multi-actions)))
5840 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5841 ;; of no user name provided. Hack to make it work as it did before:
5842 ;; changed `user' to `(or user (user-login-name))' in the places where
5843 ;; the value is actually used.
5844 (defun tramp-multi-connect-rlogin (p method user host command)
5845 "Issue `rlogin' command.
5846 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5847 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5848 with the user name, `%h' will be replaced with the host name, and `%n'
5849 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5850 `%%' if you want to use a literal percent character.
5852 If USER is nil, uses the return value of (user-login-name) instead."
5853 (let ((cmd (format-spec command `((?h . ,host)
5854 (?u . ,(or user (user-login-name)))
5855 (?n . ,tramp-rsh-end-of-line))))
5856 (cmd1 (format-spec command `((?h . ,host)
5857 (?u . ,(or user (user-login-name)))
5858 (?n . ""))))
5859 found)
5860 (erase-buffer)
5861 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5862 (process-send-string p cmd)
5863 (tramp-process-multi-actions p method user host
5864 tramp-multi-actions)))
5866 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5867 ;; of no user name provided. Hack to make it work as it did before:
5868 ;; changed `user' to `(or user (user-login-name))' in the places where
5869 ;; the value is actually used.
5870 (defun tramp-multi-connect-su (p method user host command)
5871 "Issue `su' command.
5872 Uses shell COMMAND to issue a `su' command to log in as USER on
5873 HOST. The HOST name is ignored, this just changes the user id on the
5874 host currently logged in to.
5876 If USER is nil, uses the return value of (user-login-name) instead.
5878 You can use percent escapes in the COMMAND. `%u' is replaced with the
5879 user name, and `%n' is replaced with the value of
5880 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5881 character."
5882 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5883 (?n . ,tramp-rsh-end-of-line))))
5884 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5885 (?n . ""))))
5886 found)
5887 (erase-buffer)
5888 (tramp-message 9 "Sending su command `%s'" cmd1)
5889 (process-send-string p cmd)
5890 (tramp-process-multi-actions p method user host
5891 tramp-multi-actions)))
5893 ;; Utility functions.
5895 (defun tramp-accept-process-output
5896 (&optional process timeout timeout-msecs)
5897 "Like `accept-process-output' for Tramp processes.
5898 This is needed in order to hide `last-coding-system-used', which is set
5899 for process communication also."
5900 (let (last-coding-system-used)
5901 (accept-process-output process timeout timeout-msecs)))
5903 (defun tramp-wait-for-regexp (proc timeout regexp)
5904 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5905 Expects the output of PROC to be sent to the current buffer. Returns
5906 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5907 nil."
5908 (let ((found nil)
5909 (start-time (current-time)))
5910 (cond (timeout
5911 ;; Work around a bug in XEmacs 21, where the timeout
5912 ;; expires faster than it should. This degenerates
5913 ;; to polling for buggy XEmacsen, but oh, well.
5914 (while (and (not found)
5915 (< (tramp-time-diff (current-time) start-time)
5916 timeout))
5917 (with-timeout (timeout)
5918 (while (not found)
5919 (tramp-accept-process-output proc 1)
5920 (unless (memq (process-status proc) '(run open))
5921 (error "Process has died"))
5922 (goto-char (point-min))
5923 (setq found (re-search-forward regexp nil t))))))
5925 (while (not found)
5926 (tramp-accept-process-output proc 1)
5927 (unless (memq (process-status proc) '(run open))
5928 (error "Process has died"))
5929 (goto-char (point-min))
5930 (setq found (re-search-forward regexp nil t)))))
5931 (when tramp-debug-buffer
5932 (append-to-buffer
5933 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5934 tramp-current-user tramp-current-host)
5935 (point-min) (point-max))
5936 (when (not found)
5937 (save-excursion
5938 (set-buffer
5939 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5940 tramp-current-user tramp-current-host))
5941 (goto-char (point-max))
5942 (insert "[[Regexp `" regexp "' not found"
5943 (if timeout (format " in %d secs" timeout) "")
5944 "]]"))))
5945 found))
5947 (defun tramp-wait-for-shell-prompt (proc timeout)
5948 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5949 See `tramp-wait-for-regexp' for more details.
5950 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5951 and `tramp-shell-prompt-pattern'."
5952 (tramp-wait-for-regexp
5953 proc timeout
5954 (format "\\(%s\\|%s\\)\\'"
5955 shell-prompt-pattern tramp-shell-prompt-pattern)))
5957 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5958 "Wait for shell prompt and barf if none appears.
5959 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5960 seconds. If not, it produces an error message with the given ERROR-ARGS."
5961 (unless (tramp-wait-for-shell-prompt proc timeout)
5962 (pop-to-buffer (buffer-name))
5963 (apply 'error error-args)))
5965 (defun tramp-enter-password (p prompt user host)
5966 "Prompt for a password and send it to the remote end.
5967 Uses PROMPT as a prompt and sends the password to process P."
5968 (let ((pw (tramp-read-passwd user host prompt)))
5969 (erase-buffer)
5970 (process-send-string
5971 p (concat pw
5972 (or (tramp-get-method-parameter
5973 tramp-current-multi-method
5974 tramp-current-method
5975 tramp-current-user
5976 tramp-current-host
5977 'tramp-password-end-of-line)
5978 tramp-default-password-end-of-line)))))
5980 ;; HHH: Not Changed. This might handle the case where USER is not
5981 ;; given in the "File name" very poorly. Then, the local
5982 ;; variable tramp-current-user will be set to nil.
5983 (defun tramp-pre-connection (multi-method method user host chunksize)
5984 "Do some setup before actually logging in.
5985 METHOD, USER and HOST specify the connection."
5986 (set-buffer (tramp-get-buffer multi-method method user host))
5987 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5988 (set (make-local-variable 'tramp-current-method) method)
5989 (set (make-local-variable 'tramp-current-user) user)
5990 (set (make-local-variable 'tramp-current-host) host)
5991 (set (make-local-variable 'tramp-chunksize) chunksize)
5992 (set (make-local-variable 'inhibit-eol-conversion) nil)
5993 (erase-buffer))
5995 (defun tramp-open-connection-setup-interactive-shell
5996 (p multi-method method user host)
5997 "Set up an interactive shell.
5998 Mainly sets the prompt and the echo correctly. P is the shell process
5999 to set up. METHOD, USER and HOST specify the connection."
6000 ;; Wait a bit in case the remote end feels like sending a little
6001 ;; junk first. It seems that fencepost.gnu.org does this when doing
6002 ;; a Kerberos login.
6003 (sit-for 1)
6004 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6005 (tramp-process-initial-commands p multi-method method user host
6006 tramp-initial-commands)
6007 ;; It is useful to set the prompt in the following command because
6008 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6009 ;; about and thus /bin/sh will display a strange prompt. For
6010 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6011 ;; the current working directory but /bin/sh will display a dollar
6012 ;; sign. The following command line sets $PS1 to a sane value, and
6013 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6014 ;; Pittman reports that the unusual positioning of the single quotes
6015 ;; makes it work under `rc', too. We also unset the variable $ENV
6016 ;; because that is read by some sh implementations (eg, bash when
6017 ;; called as sh) on startup; this way, we avoid the startup file
6018 ;; clobbering $PS1.
6019 (tramp-send-command-internal
6020 multi-method method user host
6021 (format "exec env 'ENV=' 'PS1=$ ' %s"
6022 (tramp-get-method-parameter
6023 multi-method method user host 'tramp-remote-sh))
6024 (format "remote `%s' to come up"
6025 (tramp-get-method-parameter
6026 multi-method method user host 'tramp-remote-sh)))
6027 (tramp-barf-if-no-shell-prompt
6028 p 30
6029 "Remote `%s' didn't come up. See buffer `%s' for details"
6030 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
6031 (buffer-name))
6032 (tramp-message 8 "Setting up remote shell environment")
6033 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6034 (tramp-send-command-internal multi-method method user host
6035 "stty -inlcr -echo kill '^U'")
6036 (erase-buffer)
6037 ;; Ignore garbage after stty command.
6038 (tramp-send-command-internal multi-method method user host
6039 "echo foo")
6040 (erase-buffer)
6041 (tramp-send-command-internal multi-method method user host
6042 "TERM=dumb; export TERM")
6043 (erase-buffer)
6044 ;; Check whether the remote host suffers from buggy `send-process-string'.
6045 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6046 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6047 ;; Emacs makes a hack when this host type is detected locally. It cannot
6048 ;; handle remote hosts, though.
6049 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6050 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6051 (tramp-send-command-internal multi-method method user host
6052 "(uname -sr) 2>/dev/null")
6053 (goto-char (point-min))
6054 (when (looking-at "FreeBSD")
6055 (setq tramp-chunksize 500)))
6057 ;; Try to set up the coding system correctly.
6058 ;; CCC this can't be the right way to do it. Hm.
6059 (save-excursion
6060 (erase-buffer)
6061 (tramp-message 9 "Determining coding system")
6062 (tramp-send-command-internal multi-method method user host
6063 "echo foo ; echo bar")
6064 (goto-char (point-min))
6065 (if (featurep 'mule)
6066 ;; Use MULE to select the right EOL convention for communicating
6067 ;; with the process.
6068 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6069 cs-decode cs-encode)
6070 (when (symbolp cs) (setq cs (cons cs cs)))
6071 (setq cs-decode (car cs))
6072 (setq cs-encode (cdr cs))
6073 (unless cs-decode (setq cs-decode 'undecided))
6074 (unless cs-encode (setq cs-encode 'undecided))
6075 (setq cs-encode (tramp-coding-system-change-eol-conversion
6076 cs-encode 'unix))
6077 (when (search-forward "\r" nil t)
6078 (setq cs-decode (tramp-coding-system-change-eol-conversion
6079 cs-decode 'dos)))
6080 (set-buffer-process-coding-system cs-decode cs-encode))
6081 ;; Look for ^M and do something useful if found.
6082 (when (search-forward "\r" nil t)
6083 ;; We have found a ^M but cannot frob the process coding system
6084 ;; because we're running on a non-MULE Emacs. Let's try
6085 ;; stty, instead.
6086 (erase-buffer)
6087 (tramp-message 9 "Trying `stty -onlcr'")
6088 (tramp-send-command-internal multi-method method user host
6089 "stty -onlcr"))))
6090 (erase-buffer)
6091 (tramp-message
6092 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6093 (tramp-send-command-internal
6094 multi-method method user host
6095 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6096 (erase-buffer)
6097 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6098 (tramp-send-command-internal multi-method method user host
6099 "set +o vi +o emacs")
6100 (erase-buffer)
6101 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6102 (tramp-send-command-internal
6103 multi-method method user host
6104 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6105 (erase-buffer)
6106 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6107 (tramp-send-command-internal multi-method method user host
6108 "unset CDPATH")
6109 (erase-buffer)
6110 (tramp-message 9 "Setting shell prompt")
6111 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6112 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6113 ;; the last time we sent a command, to avoid tramp-send-command to send
6114 ;; "echo are you awake".
6115 (setq tramp-last-cmd-time (current-time))
6116 (tramp-send-command
6117 multi-method method user host
6118 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6119 tramp-rsh-end-of-line
6120 tramp-end-of-output
6121 tramp-rsh-end-of-line))
6122 (tramp-wait-for-output))
6124 (defun tramp-post-connection (multi-method method user host)
6125 "Prepare a remote shell before being able to work on it.
6126 METHOD, USER and HOST specify the connection.
6127 Among other things, this finds a shell which groks tilde expansion,
6128 tries to find an `ls' command which groks the `-n' option, sets the
6129 locale to C and sets up the remote shell search path."
6130 ;; Search for a good shell before searching for a command which
6131 ;; checks if a file exists. This is done because Tramp wants to use
6132 ;; "test foo; echo $?" to check if various conditions hold, and
6133 ;; there are buggy /bin/sh implementations which don't execute the
6134 ;; "echo $?" part if the "test" part has an error. In particular,
6135 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6136 ;; with buggy /bin/sh implementations will have a working bash or
6137 ;; ksh. Whee...
6138 (tramp-find-shell multi-method method user host)
6139 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6140 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6141 ;; output of (tramp-find-shell) to show up along with the output of
6142 ;; (tramp-find-ls-command) testing.
6144 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6145 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6147 ;; After much debugging I couldn't find any problem with the implementation
6148 ;; of that function though. The workaround stays for me at least. :/
6150 ;; Daniel Pittman <daniel@danann.net>
6151 (sleep-for 1)
6152 (erase-buffer)
6153 (tramp-find-file-exists-command multi-method method user host)
6154 (make-local-variable 'tramp-ls-command)
6155 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6156 (unless tramp-ls-command
6157 (tramp-message
6159 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6160 (setq tramp-ls-command
6161 (tramp-find-executable multi-method method user host
6162 "ls" tramp-remote-path nil)))
6163 (unless tramp-ls-command
6164 (error "Fatal error: Couldn't find remote executable `ls'"))
6165 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6166 tramp-ls-command)
6167 (tramp-send-command multi-method method user host
6168 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6169 "return $1" tramp-rsh-end-of-line
6170 "}"))
6171 (tramp-wait-for-output)
6172 ;; Set remote PATH variable.
6173 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6174 ;; Tell remote shell to use standard time format, needed for
6175 ;; parsing `ls -l' output.
6176 (tramp-send-command multi-method method user host
6177 "LC_TIME=C; export LC_TIME; echo huhu")
6178 (tramp-wait-for-output)
6179 (tramp-send-command multi-method method user host
6180 "mesg n; echo huhu")
6181 (tramp-wait-for-output)
6182 (tramp-send-command multi-method method user host
6183 "biff n ; echo huhu")
6184 (tramp-wait-for-output)
6185 ;; Unalias ls(1) to work around issues with those silly people who make it
6186 ;; spit out ANSI escapes or whatever.
6187 (tramp-send-command multi-method method user host
6188 "unalias ls; echo huhu")
6189 (tramp-wait-for-output)
6190 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6191 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6192 ;; for otherwise the shell crashes.
6193 (erase-buffer)
6194 (make-local-variable 'tramp-test-groks-nt)
6195 (tramp-send-command multi-method method user host
6196 "( test / -nt / )")
6197 (tramp-wait-for-output)
6198 (goto-char (point-min))
6199 (setq tramp-test-groks-nt
6200 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6201 (unless tramp-test-groks-nt
6202 (tramp-send-command
6203 multi-method method user host
6204 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6205 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6206 "}")))
6207 (tramp-wait-for-output)
6208 ;; Send the fallback `uudecode' script.
6209 (erase-buffer)
6210 (tramp-send-string multi-method method user host tramp-uudecode)
6211 (tramp-wait-for-output)
6212 ;; Find a `perl'.
6213 (erase-buffer)
6214 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6215 (let ((tramp-remote-perl
6216 (or (tramp-find-executable multi-method method user host
6217 "perl5" tramp-remote-path nil)
6218 (tramp-find-executable multi-method method user host
6219 "perl" tramp-remote-path nil))))
6220 (when tramp-remote-perl
6221 (tramp-set-connection-property "perl" tramp-remote-perl
6222 multi-method method user host)
6223 (unless (tramp-method-out-of-band-p multi-method method user host)
6224 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6225 (tramp-send-string
6226 multi-method method user host
6227 (concat "tramp_encode () {\n"
6228 (format tramp-perl-encode tramp-remote-perl)
6229 " 2>/dev/null"
6230 "\n}"))
6231 (tramp-wait-for-output)
6232 (tramp-send-string
6233 multi-method method user host
6234 (concat "tramp_encode_with_module () {\n"
6235 (format tramp-perl-encode-with-module tramp-remote-perl)
6236 " 2>/dev/null"
6237 "\n}"))
6238 (tramp-wait-for-output)
6239 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6240 (tramp-send-string
6241 multi-method method user host
6242 (concat "tramp_decode () {\n"
6243 (format tramp-perl-decode tramp-remote-perl)
6244 " 2>/dev/null"
6245 "\n}"))
6246 (tramp-wait-for-output)
6247 (tramp-send-string
6248 multi-method method user host
6249 (concat "tramp_decode_with_module () {\n"
6250 (format tramp-perl-decode-with-module tramp-remote-perl)
6251 " 2>/dev/null"
6252 "\n}"))
6253 (tramp-wait-for-output))))
6254 ;; Find ln(1)
6255 (erase-buffer)
6256 (let ((ln (tramp-find-executable multi-method method user host
6257 "ln" tramp-remote-path nil)))
6258 (when ln
6259 (tramp-set-connection-property "ln" ln multi-method method user host)))
6260 ;; Set uid and gid.
6261 (erase-buffer)
6262 (tramp-send-command multi-method method user host "id -u; id -g")
6263 (tramp-wait-for-output)
6264 (goto-char (point-min))
6265 (tramp-set-connection-property
6266 "uid" (read (current-buffer)) multi-method method user host)
6267 (tramp-set-connection-property
6268 "gid" (read (current-buffer)) multi-method method user host)
6269 ;; Find the right encoding/decoding commands to use.
6270 (erase-buffer)
6271 (unless (tramp-method-out-of-band-p multi-method method user host)
6272 (tramp-find-inline-encoding multi-method method user host))
6273 ;; If encoding/decoding command are given, test to see if they work.
6274 ;; CCC: Maybe it would be useful to run the encoder both locally and
6275 ;; remotely to see if they produce the same result.
6276 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6277 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6278 (magic-string "xyzzy"))
6279 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6280 (tramp-kill-process multi-method method user host)
6281 ;; Improve error message and/or error check.
6282 (error
6283 "Must give both decoding and encoding command in method definition"))
6284 (when (and rem-enc rem-dec)
6285 (tramp-message
6287 "Checking to see if encoding/decoding commands work on remote host...")
6288 (tramp-send-command
6289 multi-method method user host
6290 (format "echo %s | %s | %s"
6291 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6292 (tramp-wait-for-output)
6293 (unless (looking-at (regexp-quote magic-string))
6294 (tramp-kill-process multi-method method user host)
6295 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6296 (buffer-name)))
6297 (erase-buffer)
6298 (tramp-message
6299 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6301 ;; CCC: We should either implement a Perl version of base64 encoding
6302 ;; and decoding. Then we just use that in the last item. The other
6303 ;; alternative is to use the Perl version of UU encoding. But then
6304 ;; we need a Lisp version of uuencode.
6306 ;; Old text from documentation of tramp-methods:
6307 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6308 ;; of the base64 methods instead since base64 encoding is much more
6309 ;; reliable and the commands are more standardized between the different
6310 ;; Unix versions. But if you can't use base64 for some reason, please
6311 ;; note that the default uudecode command does not work well for some
6312 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6313 ;; the following command for uudecode:
6315 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6317 ;; For Irix, no solution is known yet.
6319 (defvar tramp-coding-commands
6320 '(("mimencode -b" "mimencode -u -b"
6321 base64-encode-region base64-decode-region)
6322 ("mmencode -b" "mmencode -u -b"
6323 base64-encode-region base64-decode-region)
6324 ("recode data..base64" "recode base64..data"
6325 base64-encode-region base64-decode-region)
6326 ("uuencode xxx" "uudecode -o /dev/stdout"
6327 tramp-uuencode-region uudecode-decode-region)
6328 ("uuencode xxx" "uudecode -o -"
6329 tramp-uuencode-region uudecode-decode-region)
6330 ("uuencode xxx" "uudecode -p"
6331 tramp-uuencode-region uudecode-decode-region)
6332 ("uuencode xxx" "tramp_uudecode"
6333 tramp-uuencode-region uudecode-decode-region)
6334 ("tramp_encode_with_module" "tramp_decode_with_module"
6335 base64-encode-region base64-decode-region)
6336 ("tramp_encode" "tramp_decode"
6337 base64-encode-region base64-decode-region))
6338 "List of coding commands for inline transfer.
6339 Each item is a list that looks like this:
6341 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6343 The REMOTE-ENCODING should be a string, giving a command accepting a
6344 plain file on standard input and writing the encoded file to standard
6345 output. The REMOTE-DECODING should also be a string, giving a command
6346 accepting an encoded file on standard input and writing the decoded
6347 file to standard output.
6349 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6350 symbols, giving functions. If they are strings, then they can contain
6351 the \"%s\" format specifier. If that specifier is present, the input
6352 filename will be put into the command line at that spot. If the
6353 specifier is not present, the input should be read from standard
6354 input.
6356 If they are functions, they will be called with two arguments, start
6357 and end of region, and are expected to replace the region contents
6358 with the encoded or decoded results, respectively.")
6360 (defun tramp-find-inline-encoding (multi-method method user host)
6361 "Find an inline transfer encoding that works.
6362 Goes through the list `tramp-coding-commands'."
6363 (let ((commands tramp-coding-commands)
6364 (magic "xyzzy")
6365 item found)
6366 (while (and commands (null found))
6367 (setq item (pop commands))
6368 (catch 'wont-work
6369 (let ((rem-enc (nth 0 item))
6370 (rem-dec (nth 1 item))
6371 (loc-enc (nth 2 item))
6372 (loc-dec (nth 3 item)))
6373 ;; Check if remote encoding and decoding commands can be
6374 ;; called remotely with null input and output. This makes
6375 ;; sure there are no syntax errors and the command is really
6376 ;; found. Note that we do not redirect stdout to /dev/null,
6377 ;; for two reaons: when checking the decoding command, we
6378 ;; actually check the output it gives. And also, when
6379 ;; redirecting "mimencode" output to /dev/null, then as root
6380 ;; it might change the permissions of /dev/null!
6381 (tramp-message-for-buffer
6382 multi-method method user host 9
6383 "Checking remote encoding command `%s' for sanity" rem-enc)
6384 (unless (zerop (tramp-send-command-and-check
6385 multi-method method user host
6386 (format "%s </dev/null" rem-enc) t))
6387 (throw 'wont-work nil))
6388 (tramp-message-for-buffer
6389 multi-method method user host 9
6390 "Checking remote decoding command `%s' for sanity" rem-dec)
6391 (unless (zerop (tramp-send-command-and-check
6392 multi-method method user host
6393 (format "echo %s | %s | %s"
6394 magic rem-enc rem-dec) t))
6395 (throw 'wont-work nil))
6396 (save-excursion
6397 (goto-char (point-min))
6398 (unless (looking-at (regexp-quote magic))
6399 (throw 'wont-work nil)))
6400 ;; If the local encoder or decoder is a string, the
6401 ;; corresponding command has to work locally.
6402 (when (stringp loc-enc)
6403 (tramp-message-for-buffer
6404 multi-method method user host 9
6405 "Checking local encoding command `%s' for sanity" loc-enc)
6406 (unless (zerop (tramp-call-local-coding-command
6407 loc-enc nil nil))
6408 (throw 'wont-work nil)))
6409 (when (stringp loc-dec)
6410 (tramp-message-for-buffer
6411 multi-method method user host 9
6412 "Checking local decoding command `%s' for sanity" loc-dec)
6413 (unless (zerop (tramp-call-local-coding-command
6414 loc-dec nil nil))
6415 (throw 'wont-work nil)))
6416 ;; CCC: At this point, maybe we should check that the output
6417 ;; of the commands is correct. But for the moment we will
6418 ;; assume that commands working on empty input will also
6419 ;; work in practice.
6420 (setq found item))))
6421 ;; Did we find something? If not, issue error. If so,
6422 ;; set connection properties.
6423 (unless found
6424 (error "Couldn't find an inline transfer encoding"))
6425 (let ((rem-enc (nth 0 found))
6426 (rem-dec (nth 1 found))
6427 (loc-enc (nth 2 found))
6428 (loc-dec (nth 3 found)))
6429 (tramp-message 10 "Using remote encoding %s" rem-enc)
6430 (tramp-set-remote-encoding multi-method method user host rem-enc)
6431 (tramp-message 10 "Using remote decoding %s" rem-dec)
6432 (tramp-set-remote-decoding multi-method method user host rem-dec)
6433 (tramp-message 10 "Using local encoding %s" loc-enc)
6434 (tramp-set-local-encoding multi-method method user host loc-enc)
6435 (tramp-message 10 "Using local decoding %s" loc-dec)
6436 (tramp-set-local-decoding multi-method method user host loc-dec))))
6438 (defun tramp-call-local-coding-command (cmd input output)
6439 "Call the local encoding or decoding command.
6440 If CMD contains \"%s\", provide input file INPUT there in command.
6441 Otherwise, INPUT is passed via standard input.
6442 INPUT can also be nil which means `/dev/null'.
6443 OUTPUT can be a string (which specifies a filename), or t (which
6444 means standard output and thus the current buffer), or nil (which
6445 means discard it)."
6446 (call-process
6447 tramp-encoding-shell ;program
6448 (when (and input (not (string-match "%s" cmd)))
6449 input) ;input
6450 (if (eq output t) t nil) ;output
6451 nil ;redisplay
6452 tramp-encoding-command-switch
6453 ;; actual shell command
6454 (concat
6455 (if (string-match "%s" cmd) (format cmd input) cmd)
6456 (if (stringp output) (concat "> " output) ""))))
6458 (defun tramp-maybe-open-connection (multi-method method user host)
6459 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6460 Does not do anything if a connection is already open, but re-opens the
6461 connection if a previous connection has died for some reason."
6462 (let ((p (get-buffer-process
6463 (tramp-get-buffer multi-method method user host)))
6464 last-cmd-time)
6465 ;; If too much time has passed since last command was sent, look
6466 ;; whether process is still alive. If it isn't, kill it. When
6467 ;; using ssh, it can sometimes happen that the remote end has hung
6468 ;; up but the local ssh client doesn't recognize this until it
6469 ;; tries to send some data to the remote end. So that's why we
6470 ;; try to send a command from time to time, then look again
6471 ;; whether the process is really alive.
6472 (save-excursion
6473 (set-buffer (tramp-get-buffer multi-method method user host))
6474 (when (and tramp-last-cmd-time
6475 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6476 p (processp p) (memq (process-status p) '(run open)))
6477 (tramp-send-command
6478 multi-method method user host "echo are you awake" nil t)
6479 (unless (and (memq (process-status p) '(run open))
6480 (tramp-wait-for-output 10))
6481 (delete-process p)
6482 (setq p nil))
6483 (erase-buffer)))
6484 (unless (and p (processp p) (memq (process-status p) '(run open)))
6485 (when (and p (processp p))
6486 (delete-process p))
6487 (let ((process-connection-type tramp-process-connection-type))
6488 (funcall (tramp-get-method-parameter
6489 multi-method
6490 (tramp-find-method multi-method method user host)
6491 user host 'tramp-connection-function)
6492 multi-method method user host)))))
6494 (defun tramp-send-command
6495 (multi-method method user host command &optional noerase neveropen)
6496 "Send the COMMAND to USER at HOST (logged in using METHOD).
6497 Erases temporary buffer before sending the command (unless NOERASE
6498 is true).
6499 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6500 connection. This is meant to be used from
6501 `tramp-maybe-open-connection' only."
6502 (or neveropen
6503 (tramp-maybe-open-connection multi-method method user host))
6504 (setq tramp-last-cmd-time (current-time))
6505 (setq tramp-last-cmd command)
6506 (when tramp-debug-buffer
6507 (save-excursion
6508 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6509 (goto-char (point-max))
6510 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6511 (let ((proc nil))
6512 (set-buffer (tramp-get-buffer multi-method method user host))
6513 (unless noerase (erase-buffer))
6514 (setq proc (get-buffer-process (current-buffer)))
6515 (process-send-string proc
6516 (concat command tramp-rsh-end-of-line))))
6518 (defun tramp-send-command-internal
6519 (multi-method method user host command &optional msg)
6520 "Send command to remote host and wait for success.
6521 Sends COMMAND, then waits 30 seconds for shell prompt."
6522 (tramp-send-command multi-method method user host command t t)
6523 (when msg
6524 (tramp-message 9 "Waiting 30s for %s..." msg))
6525 (tramp-barf-if-no-shell-prompt
6526 nil 30
6527 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6529 (defun tramp-wait-for-output (&optional timeout)
6530 "Wait for output from remote rsh command."
6531 (let ((proc (get-buffer-process (current-buffer)))
6532 (found nil)
6533 (start-time (current-time))
6534 (start-point (point))
6535 (end-of-output (concat "^"
6536 (regexp-quote tramp-end-of-output)
6537 "\r?$")))
6538 ;; Algorithm: get waiting output. See if last line contains
6539 ;; end-of-output sentinel. If not, wait a bit and again get
6540 ;; waiting output. Repeat until timeout expires or end-of-output
6541 ;; sentinel is seen. Will hang if timeout is nil and
6542 ;; end-of-output sentinel never appears.
6543 (save-match-data
6544 (cond (timeout
6545 ;; Work around an XEmacs bug, where the timeout expires
6546 ;; faster than it should. This degenerates into polling
6547 ;; for buggy XEmacsen, but oh, well.
6548 (while (and (not found)
6549 (< (tramp-time-diff (current-time) start-time)
6550 timeout))
6551 (with-timeout (timeout)
6552 (while (not found)
6553 (tramp-accept-process-output proc 1)
6554 (unless (memq (process-status proc) '(run open))
6555 (error "Process has died"))
6556 (goto-char (point-max))
6557 (forward-line -1)
6558 (setq found (looking-at end-of-output))))))
6560 (while (not found)
6561 (tramp-accept-process-output proc 1)
6562 (unless (memq (process-status proc) '(run open))
6563 (error "Process has died"))
6564 (goto-char (point-max))
6565 (forward-line -1)
6566 (setq found (looking-at end-of-output))))))
6567 ;; At this point, either the timeout has expired or we have found
6568 ;; the end-of-output sentinel.
6569 (when found
6570 (goto-char (point-max))
6571 (forward-line -2)
6572 (delete-region (point) (point-max)))
6573 ;; If processing echoes, look for it in the first line and delete.
6574 (when tramp-process-echoes
6575 (save-excursion
6576 (goto-char start-point)
6577 (when (looking-at (regexp-quote tramp-last-cmd))
6578 (delete-region (point) (progn (forward-line 1) (point))))))
6579 ;; Add output to debug buffer if appropriate.
6580 (when tramp-debug-buffer
6581 (append-to-buffer
6582 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6583 tramp-current-user tramp-current-host)
6584 (point-min) (point-max))
6585 (when (not found)
6586 (save-excursion
6587 (set-buffer
6588 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6589 tramp-current-user tramp-current-host))
6590 (goto-char (point-max))
6591 (insert "[[Remote prompt `" end-of-output "' not found"
6592 (if timeout (format " in %d secs" timeout) "")
6593 "]]"))))
6594 (goto-char (point-min))
6595 ;; Return value is whether end-of-output sentinel was found.
6596 found))
6598 (defun tramp-send-command-and-check (multi-method method user host command
6599 &optional subshell)
6600 "Run COMMAND and check its exit status.
6601 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6602 Sends `echo $?' along with the COMMAND for checking the exit status. If
6603 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6605 If the optional argument SUBSHELL is non-nil, the command is executed in
6606 a subshell, ie surrounded by parentheses."
6607 (tramp-send-command multi-method method user host
6608 (concat (if subshell "( " "")
6609 command
6610 (if command " 2>/dev/null; " "")
6611 "echo tramp_exit_status $?"
6612 (if subshell " )" " ")))
6613 (tramp-wait-for-output)
6614 (goto-char (point-max))
6615 (unless (search-backward "tramp_exit_status " nil t)
6616 (error "Couldn't find exit status of `%s'" command))
6617 (skip-chars-forward "^ ")
6618 (read (current-buffer)))
6620 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6621 signal fmt &rest args)
6622 "Run COMMAND, check exit status, throw error if exit status not okay.
6623 Similar to `tramp-send-command-and-check' but accepts two more arguments
6624 FMT and ARGS which are passed to `error'."
6625 (unless (zerop (tramp-send-command-and-check
6626 multi-method method user host command subshell))
6627 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6628 ;; silent.
6629 (pop-to-buffer (current-buffer))
6630 (funcall 'signal signal (apply 'format fmt args))))
6632 ;; It seems that Tru64 Unix does not like it if long strings are sent
6633 ;; to it in one go. (This happens when sending the Perl
6634 ;; `file-attributes' implementation, for instance.) Therefore, we
6635 ;; have this function which waits a bit at each line.
6636 (defun tramp-send-string
6637 (multi-method method user host string)
6638 "Send the STRING to USER at HOST using METHOD.
6640 The STRING is expected to use Unix line-endings, but the lines sent to
6641 the remote host use line-endings as defined in the variable
6642 `tramp-rsh-end-of-line'."
6643 (let ((proc (get-buffer-process
6644 (tramp-get-buffer multi-method method user host))))
6645 (unless proc
6646 (error "Can't send string to remote host -- not logged in"))
6647 ;; debug message
6648 (when tramp-debug-buffer
6649 (save-excursion
6650 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6651 (goto-char (point-max))
6652 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6653 ;; replace "\n" by `tramp-rsh-end-of-line'
6654 (setq string
6655 (mapconcat 'identity
6656 (split-string string "\n")
6657 tramp-rsh-end-of-line))
6658 (unless (or (string= string "")
6659 (string-equal (substring string -1) tramp-rsh-end-of-line))
6660 (setq string (concat string tramp-rsh-end-of-line)))
6661 ;; send the string
6662 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6663 (let ((pos 0)
6664 (end (length string)))
6665 (while (< pos end)
6666 (tramp-message-for-buffer
6667 multi-method method user host 10
6668 "Sending chunk from %s to %s"
6669 pos (min (+ pos tramp-chunksize) end))
6670 (process-send-string
6671 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6672 (setq pos (+ pos tramp-chunksize))
6673 (sleep-for 0.1)))
6674 (process-send-string proc string))))
6676 (defun tramp-send-eof (multi-method method user host)
6677 "Send EOF to the remote end.
6678 METHOD, HOST and USER specify the connection."
6679 (let ((proc (get-buffer-process
6680 (tramp-get-buffer multi-method method user host))))
6681 (unless proc
6682 (error "Can't send EOF to remote host -- not logged in"))
6683 (process-send-eof proc)))
6684 ; (process-send-string proc "\^D")))
6686 (defun tramp-kill-process (multi-method method user host)
6687 "Kill the connection process used by Tramp.
6688 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6689 (let ((proc (get-buffer-process
6690 (tramp-get-buffer multi-method method user host))))
6691 (kill-process proc)))
6693 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6694 "Erase buffer, then discard subsequent garbage.
6695 If `tramp-discard-garbage' is nil, just erase buffer."
6696 (if (not tramp-discard-garbage)
6697 (erase-buffer)
6698 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6699 (when tramp-debug-buffer
6700 (save-excursion
6701 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6702 (goto-char (point-max))
6703 (tramp-insert-with-face
6704 'bold (format "Additional characters detected\n")))))))
6706 (defun tramp-mode-string-to-int (mode-string)
6707 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6708 (let* ((mode-chars (string-to-vector mode-string))
6709 (owner-read (aref mode-chars 1))
6710 (owner-write (aref mode-chars 2))
6711 (owner-execute-or-setid (aref mode-chars 3))
6712 (group-read (aref mode-chars 4))
6713 (group-write (aref mode-chars 5))
6714 (group-execute-or-setid (aref mode-chars 6))
6715 (other-read (aref mode-chars 7))
6716 (other-write (aref mode-chars 8))
6717 (other-execute-or-sticky (aref mode-chars 9)))
6718 (save-match-data
6719 (logior
6720 (case owner-read
6721 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6722 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6723 (case owner-write
6724 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6725 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6726 (case owner-execute-or-setid
6727 (?x (tramp-octal-to-decimal "00100"))
6728 (?S (tramp-octal-to-decimal "04000"))
6729 (?s (tramp-octal-to-decimal "04100"))
6730 (?- 0)
6731 (t (error "Fourth char `%c' must be one of `xsS-'"
6732 owner-execute-or-setid)))
6733 (case group-read
6734 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6735 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6736 (case group-write
6737 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6738 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6739 (case group-execute-or-setid
6740 (?x (tramp-octal-to-decimal "00010"))
6741 (?S (tramp-octal-to-decimal "02000"))
6742 (?s (tramp-octal-to-decimal "02010"))
6743 (?- 0)
6744 (t (error "Seventh char `%c' must be one of `xsS-'"
6745 group-execute-or-setid)))
6746 (case other-read
6747 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6748 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6749 (case other-write
6750 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6751 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6752 (case other-execute-or-sticky
6753 (?x (tramp-octal-to-decimal "00001"))
6754 (?T (tramp-octal-to-decimal "01000"))
6755 (?t (tramp-octal-to-decimal "01001"))
6756 (?- 0)
6757 (t (error "Tenth char `%c' must be one of `xtT-'"
6758 other-execute-or-sticky)))))))
6760 (defun tramp-convert-file-attributes (multi-method method user host attr)
6761 "Convert file-attributes ATTR generated by perl script or ls.
6762 Convert file mode bits to string and set virtual device number.
6763 Return ATTR."
6764 ;; Convert file mode bits to string.
6765 (unless (stringp (nth 8 attr))
6766 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6767 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6768 (when (numberp (nth 3 attr))
6769 (setcar (nthcdr 9 attr)
6770 (not (eql (nth 3 attr)
6771 (tramp-get-remote-gid multi-method method user host)))))
6772 ;; Set virtual device number.
6773 (setcar (nthcdr 11 attr)
6774 (tramp-get-device multi-method method user host))
6775 attr)
6777 (defun tramp-get-device (multi-method method user host)
6778 "Returns the virtual device number.
6779 If it doesn't exist, generate a new one."
6780 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6781 (unless (assoc string tramp-devices)
6782 (add-to-list 'tramp-devices
6783 (list string (length tramp-devices))))
6784 (list -1 (nth 1 (assoc string tramp-devices)))))
6786 (defun tramp-file-mode-from-int (mode)
6787 "Turn an integer representing a file mode into an ls(1)-like string."
6788 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6789 (user (logand (lsh mode -6) 7))
6790 (group (logand (lsh mode -3) 7))
6791 (other (logand (lsh mode -0) 7))
6792 (suid (> (logand (lsh mode -9) 4) 0))
6793 (sgid (> (logand (lsh mode -9) 2) 0))
6794 (sticky (> (logand (lsh mode -9) 1) 0)))
6795 (setq user (tramp-file-mode-permissions user suid "s"))
6796 (setq group (tramp-file-mode-permissions group sgid "s"))
6797 (setq other (tramp-file-mode-permissions other sticky "t"))
6798 (concat type user group other)))
6801 (defun tramp-file-mode-permissions (perm suid suid-text)
6802 "Convert a permission bitset into a string.
6803 This is used internally by `tramp-file-mode-from-int'."
6804 (let ((r (> (logand perm 4) 0))
6805 (w (> (logand perm 2) 0))
6806 (x (> (logand perm 1) 0)))
6807 (concat (or (and r "r") "-")
6808 (or (and w "w") "-")
6809 (or (and suid x suid-text) ; suid, execute
6810 (and suid (upcase suid-text)) ; suid, !execute
6811 (and x "x") "-")))) ; !suid
6814 (defun tramp-decimal-to-octal (i)
6815 "Return a string consisting of the octal digits of I.
6816 Not actually used. Use `(format \"%o\" i)' instead?"
6817 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6818 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6819 ((zerop i) "0")
6820 (t (concat (tramp-decimal-to-octal (/ i 8))
6821 (number-to-string (% i 8))))))
6824 ;;(defun tramp-octal-to-decimal (ostr)
6825 ;; "Given a string of octal digits, return a decimal number."
6826 ;; (cond ((null ostr) 0)
6827 ;; ((string= "" ostr) 0)
6828 ;; (t (let ((last (aref ostr (1- (length ostr))))
6829 ;; (rest (substring ostr 0 (1- (length ostr)))))
6830 ;; (unless (and (>= last ?0)
6831 ;; (<= last ?7))
6832 ;; (error "Not an octal digit: %c" last))
6833 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6834 ;; Kudos to Gerd Moellmann for this suggestion.
6835 (defun tramp-octal-to-decimal (ostr)
6836 "Given a string of octal digits, return a decimal number."
6837 (let ((x (or ostr "")))
6838 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6839 (unless (string-match "\\`[0-7]*\\'" x)
6840 (error "Non-octal junk in string `%s'" x))
6841 (string-to-number ostr 8)))
6843 (defun tramp-shell-case-fold (string)
6844 "Converts STRING to shell glob pattern which ignores case."
6845 (mapconcat
6846 (lambda (c)
6847 (if (equal (downcase c) (upcase c))
6848 (vector c)
6849 (format "[%c%c]" (downcase c) (upcase c))))
6850 string
6851 ""))
6854 ;; ------------------------------------------------------------
6855 ;; -- TRAMP file names --
6856 ;; ------------------------------------------------------------
6857 ;; Conversion functions between external representation and
6858 ;; internal data structure. Convenience functions for internal
6859 ;; data structure.
6861 (defstruct tramp-file-name multi-method method user host localname)
6863 (defun tramp-tramp-file-p (name)
6864 "Return t iff NAME is a tramp file."
6865 (save-match-data
6866 (string-match tramp-file-name-regexp name)))
6868 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6869 ;; to the `user' part of the structure if a user name was not
6870 ;; provided, now it assigns nil.
6871 (defun tramp-dissect-file-name (name)
6872 "Return an `tramp-file-name' structure.
6873 The structure consists of remote method, remote user, remote host and
6874 localname (file name on remote host)."
6875 (save-match-data
6876 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6877 (method
6878 ; single-hop
6879 (if match (match-string (nth 1 tramp-file-name-structure) name)
6880 ; maybe multi-hop
6881 (string-match
6882 (format (nth 0 tramp-multi-file-name-structure)
6883 (nth 0 tramp-multi-file-name-hop-structure)) name)
6884 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6885 (if (and method (member method tramp-multi-methods))
6886 ;; If it's a multi method, the file name structure contains
6887 ;; arrays of method, user and host.
6888 (tramp-dissect-multi-file-name name)
6889 ;; Normal method. First, find out default method.
6890 (unless match (error "Not a tramp file name: %s" name))
6891 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6892 (host (match-string (nth 3 tramp-file-name-structure) name))
6893 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6894 (make-tramp-file-name
6895 :multi-method nil
6896 :method method
6897 :user (or user nil)
6898 :host host
6899 :localname localname))))))
6901 (defun tramp-find-default-method (user host)
6902 "Look up the right method to use in `tramp-default-method-alist'."
6903 (let ((choices tramp-default-method-alist)
6904 (method tramp-default-method)
6905 item)
6906 (while choices
6907 (setq item (pop choices))
6908 (when (and (string-match (nth 0 item) (or host ""))
6909 (string-match (nth 1 item) (or user "")))
6910 (setq method (nth 2 item))
6911 (setq choices nil)))
6912 method))
6914 (defun tramp-find-method (multi-method method user host)
6915 "Return the right method string to use.
6916 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6917 If both MULTI-METHOD and METHOD are nil, do a lookup in
6918 `tramp-default-method-alist'."
6919 (or multi-method method (tramp-find-default-method user host)))
6921 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6922 ;; a user name is not provided in the "file name" very well.
6923 (defun tramp-dissect-multi-file-name (name)
6924 "Not implemented yet."
6925 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6926 (method-index (nth 1 tramp-multi-file-name-structure))
6927 (hops-index (nth 2 tramp-multi-file-name-structure))
6928 (localname-index (nth 3 tramp-multi-file-name-structure))
6929 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6930 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6931 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6932 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6933 method hops len hop-methods hop-users hop-hosts localname)
6934 (unless (string-match (format regexp hop-regexp) name)
6935 (error "Not a multi tramp file name: %s" name))
6936 (setq method (match-string method-index name))
6937 (setq hops (match-string hops-index name))
6938 (setq len (/ (length (match-data t)) 2))
6939 (when (< localname-index 0) (incf localname-index len))
6940 (setq localname (match-string localname-index name))
6941 (let ((index 0))
6942 (while (string-match hop-regexp hops index)
6943 (setq index (match-end 0))
6944 (setq hop-methods
6945 (cons (match-string hop-method-index hops) hop-methods))
6946 (setq hop-users
6947 (cons (match-string hop-user-index hops) hop-users))
6948 (setq hop-hosts
6949 (cons (match-string hop-host-index hops) hop-hosts))))
6950 (make-tramp-file-name
6951 :multi-method method
6952 :method (apply 'vector (reverse hop-methods))
6953 :user (apply 'vector (reverse hop-users))
6954 :host (apply 'vector (reverse hop-hosts))
6955 :localname localname)))
6957 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6958 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6959 (if multi-method
6960 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6961 (format-spec
6962 (concat tramp-prefix-format
6963 (when method (concat "%m" tramp-postfix-single-method-format))
6964 (when user (concat "%u" tramp-postfix-user-format))
6965 (when host (concat "%h" tramp-postfix-host-format))
6966 (when localname (concat "%p")))
6967 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6969 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6970 ;; with this when USER is nil?
6971 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6972 "Constructs a tramp file name for a multi-hop method."
6973 (unless tramp-make-multi-tramp-file-format
6974 (error "`tramp-make-multi-tramp-file-format' is nil"))
6975 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6976 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6977 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6978 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6979 (hops "")
6980 (localname (format-spec localname-format `((?p . ,localname))))
6981 (i 0)
6982 (len (length method)))
6983 (while (< i len)
6984 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6985 (setq hops (concat hops (format-spec hop-format
6986 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6987 (incf i)))
6988 (concat prefix hops localname)))
6990 (defun tramp-make-copy-program-file-name (user host localname)
6991 "Create a file name suitable to be passed to `rcp' and workalikes."
6992 (if user
6993 (format "%s@%s:%s" user host localname)
6994 (format "%s:%s" host localname)))
6996 (defun tramp-method-out-of-band-p (multi-method method user host)
6997 "Return t if this is an out-of-band method, nil otherwise."
6998 (tramp-get-method-parameter
6999 multi-method
7000 (tramp-find-method multi-method method user host)
7001 user host 'tramp-copy-program))
7003 ;; Variables local to connection.
7005 (defun tramp-get-ls-command (multi-method method user host)
7006 (save-excursion
7007 (tramp-maybe-open-connection multi-method method user host)
7008 (set-buffer (tramp-get-buffer multi-method method user host))
7009 tramp-ls-command))
7011 (defun tramp-get-test-groks-nt (multi-method method user host)
7012 (save-excursion
7013 (tramp-maybe-open-connection multi-method method user host)
7014 (set-buffer (tramp-get-buffer multi-method method user host))
7015 tramp-test-groks-nt))
7017 (defun tramp-get-file-exists-command (multi-method method user host)
7018 (save-excursion
7019 (tramp-maybe-open-connection multi-method method user host)
7020 (set-buffer (tramp-get-buffer multi-method method user host))
7021 tramp-file-exists-command))
7023 (defun tramp-get-remote-perl (multi-method method user host)
7024 (tramp-get-connection-property "perl" nil multi-method method user host))
7026 (defun tramp-get-remote-ln (multi-method method user host)
7027 (tramp-get-connection-property "ln" nil multi-method method user host))
7029 (defun tramp-get-remote-uid (multi-method method user host)
7030 (tramp-get-connection-property "uid" nil multi-method method user host))
7032 (defun tramp-get-remote-gid (multi-method method user host)
7033 (tramp-get-connection-property "gid" nil multi-method method user host))
7035 ;; Get a property of a TRAMP connection.
7036 (defun tramp-get-connection-property
7037 (property default multi-method method user host)
7038 "Get the named property for the connection.
7039 If the value is not set for the connection, return `default'"
7040 (tramp-maybe-open-connection multi-method method user host)
7041 (with-current-buffer (tramp-get-buffer multi-method method user host)
7042 (let (error)
7043 (condition-case nil
7044 (symbol-value (intern (concat "tramp-connection-property-" property)))
7045 (error default)))))
7047 ;; Set a property of a TRAMP connection.
7048 (defun tramp-set-connection-property
7049 (property value multi-method method user host)
7050 "Set the named property of a TRAMP connection."
7051 (tramp-maybe-open-connection multi-method method user host)
7052 (with-current-buffer (tramp-get-buffer multi-method method user host)
7053 (set (make-local-variable
7054 (intern (concat "tramp-connection-property-" property)))
7055 value)))
7057 ;; Some predefined connection properties.
7058 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7059 (tramp-set-connection-property "remote-encoding" rem-enc
7060 multi-method method user host))
7061 (defun tramp-get-remote-encoding (multi-method method user host)
7062 (tramp-get-connection-property "remote-encoding" nil
7063 multi-method method user host))
7065 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7066 (tramp-set-connection-property "remote-decoding" rem-dec
7067 multi-method method user host))
7068 (defun tramp-get-remote-decoding (multi-method method user host)
7069 (tramp-get-connection-property "remote-decoding" nil
7070 multi-method method user host))
7072 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
7073 (tramp-set-connection-property "local-encoding" loc-enc
7074 multi-method method user host))
7075 (defun tramp-get-local-encoding (multi-method method user host)
7076 (tramp-get-connection-property "local-encoding" nil
7077 multi-method method user host))
7079 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
7080 (tramp-set-connection-property "local-decoding" loc-dec
7081 multi-method method user host))
7082 (defun tramp-get-local-decoding (multi-method method user host)
7083 (tramp-get-connection-property "local-decoding" nil
7084 multi-method method user host))
7086 (defun tramp-get-method-parameter (multi-method method user host param)
7087 "Return the method parameter PARAM.
7088 If the `tramp-methods' entry does not exist, use the variable PARAM
7089 as default."
7090 (unless (boundp param)
7091 (error "Non-existing method parameter `%s'" param))
7092 (let ((entry (assoc param
7093 (assoc (tramp-find-method multi-method method user host)
7094 tramp-methods))))
7095 (if entry
7096 (second entry)
7097 (symbol-value param))))
7100 ;; Auto saving to a special directory.
7102 (defun tramp-exists-file-name-handler (operation &rest args)
7103 (let ((buffer-file-name "/")
7104 (fnha file-name-handler-alist)
7105 (check-file-name-operation operation)
7106 (file-name-handler-alist
7107 (list
7108 (cons "/"
7109 '(lambda (operation &rest args)
7110 "Returns OPERATION if it is the one to be checked"
7111 (if (equal check-file-name-operation operation)
7112 operation
7113 (let ((file-name-handler-alist fnha))
7114 (apply operation args))))))))
7115 (eq (apply operation args) operation)))
7117 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7118 (defadvice make-auto-save-file-name
7119 (around tramp-advice-make-auto-save-file-name () activate)
7120 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7121 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7122 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7123 ad-do-it))
7124 (add-hook 'tramp-unload-hook
7125 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
7127 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7128 ;; permission 0666 minus umask. This is a security threat.
7130 (defun tramp-set-auto-save-file-modes ()
7131 "Set permissions of autosaved remote files to the original permissions."
7132 (let ((bfn (buffer-file-name)))
7133 (when (and (stringp bfn)
7134 (tramp-tramp-file-p bfn)
7135 (stringp buffer-auto-save-file-name)
7136 (not (equal bfn buffer-auto-save-file-name)))
7137 (unless (file-exists-p buffer-auto-save-file-name)
7138 (write-region "" nil buffer-auto-save-file-name))
7139 ;; Permissions should be set always, because there might be an old
7140 ;; auto-saved file belonging to another original file. This could
7141 ;; be a security threat.
7142 (set-file-modes buffer-auto-save-file-name
7143 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7145 (unless (or (> emacs-major-version 21)
7146 (and (featurep 'xemacs)
7147 (= emacs-major-version 21)
7148 (> emacs-minor-version 4)))
7149 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7150 (add-hook 'tramp-unload-hook
7151 '(lambda ()
7152 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
7154 (defun tramp-subst-strs-in-string (alist string)
7155 "Replace all occurrences of the string FROM with TO in STRING.
7156 ALIST is of the form ((FROM . TO) ...)."
7157 (save-match-data
7158 (while alist
7159 (let* ((pr (car alist))
7160 (from (car pr))
7161 (to (cdr pr)))
7162 (while (string-match (regexp-quote from) string)
7163 (setq string (replace-match to t t string)))
7164 (setq alist (cdr alist))))
7165 string))
7167 (defun tramp-insert-with-face (face string)
7168 "Insert text with a specific face."
7169 (let ((start (point)))
7170 (insert string)
7171 (add-text-properties start (point) (list 'face face))))
7173 ;; ------------------------------------------------------------
7174 ;; -- Compatibility functions section --
7175 ;; ------------------------------------------------------------
7177 (defun tramp-temporary-file-directory ()
7178 "Return name of directory for temporary files (compat function).
7179 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7180 this is the function `temp-directory'."
7181 (cond ((boundp 'temporary-file-directory)
7182 (symbol-value 'temporary-file-directory))
7183 ((fboundp 'temp-directory)
7184 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7185 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7186 (file-name-as-directory (getenv "TEMP")))
7187 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7188 (file-name-as-directory (getenv "TMP")))
7189 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7190 (file-name-as-directory (getenv "TMPDIR")))
7191 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7192 (t (message (concat "Neither `temporary-file-directory' nor "
7193 "`temp-directory' is defined -- using /tmp."))
7194 (file-name-as-directory "/tmp"))))
7196 (defun tramp-read-passwd (user host prompt)
7197 "Read a password from user (compat function).
7198 Invokes `password-read' if available, `read-passwd' else."
7199 (if (functionp 'password-read)
7200 (let* ((key (concat (or user (user-login-name)) "@" host))
7201 (password (apply #'password-read (list prompt key))))
7202 (apply #'password-cache-add (list key password))
7203 password)
7204 (read-passwd prompt)))
7206 (defun tramp-clear-passwd (&optional user host)
7207 "Clear password cache for connection related to current-buffer."
7208 (interactive)
7209 (let ((filename (or buffer-file-name list-buffers-directory "")))
7210 (when (and (functionp 'password-cache-remove)
7211 (or (and user host) (tramp-tramp-file-p filename)))
7212 (let* ((v (when (tramp-tramp-file-p filename)
7213 (tramp-dissect-file-name filename)))
7214 (luser (or user (tramp-file-name-user v) (user-login-name)))
7215 (lhost (or host (tramp-file-name-host v) (system-name)))
7216 (key (concat luser "@" lhost)))
7217 (apply #'password-cache-remove (list key))))))
7219 (defun tramp-time-diff (t1 t2)
7220 "Return the difference between the two times, in seconds.
7221 T1 and T2 are time values (as returned by `current-time' for example)."
7222 ;; Pacify byte-compiler with `symbol-function'.
7223 (cond ((and (fboundp 'subtract-time)
7224 (fboundp 'float-time))
7225 (funcall (symbol-function 'float-time)
7226 (funcall (symbol-function 'subtract-time) t1 t2)))
7227 ((and (fboundp 'subtract-time)
7228 (fboundp 'time-to-seconds))
7229 (funcall (symbol-function 'time-to-seconds)
7230 (funcall (symbol-function 'subtract-time) t1 t2)))
7231 ((fboundp 'itimer-time-difference)
7232 (funcall (symbol-function 'itimer-time-difference)
7233 (if (< (length t1) 3) (append t1 '(0)) t1)
7234 (if (< (length t2) 3) (append t2 '(0)) t2)))
7236 ;; snarfed from Emacs 21 time-date.el; combining
7237 ;; time-to-seconds and subtract-time
7238 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7239 (list (- (car t1) (car t2) (if borrow 1 0))
7240 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7241 (+ (* (car time) 65536.0)
7242 (cadr time)
7243 (/ (or (nth 2 time) 0) 1000000.0))))))
7245 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7246 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7247 EOL-TYPE can be one of `dos', `unix', or `mac'."
7248 (cond ((fboundp 'coding-system-change-eol-conversion)
7249 (apply #'coding-system-change-eol-conversion
7250 (list coding-system eol-type)))
7251 ((fboundp 'subsidiary-coding-system)
7252 (apply
7253 #'subsidiary-coding-system
7254 (list coding-system
7255 (cond ((eq eol-type 'dos) 'crlf)
7256 ((eq eol-type 'unix) 'lf)
7257 ((eq eol-type 'mac) 'cr)
7259 (error "Unknown EOL-TYPE `%s', must be %s"
7260 eol-type
7261 "`dos', `unix', or `mac'"))))))
7262 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7264 (defun tramp-split-string (string pattern)
7265 "Like `split-string' but omit empty strings.
7266 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7267 This is, the first, empty, element is omitted. In XEmacs, the first
7268 element is not omitted.
7270 Note: this function has been written for `tramp-handle-file-truename'.
7271 If you want to use it for something else, you'll have to check whether
7272 it does the right thing."
7273 (delete "" (split-string string pattern)))
7275 (defun tramp-set-process-query-on-exit-flag (process flag)
7276 "Specify if query is needed for process when Emacs is exited.
7277 If the second argument flag is non-nil, Emacs will query the user before
7278 exiting if process is running."
7279 (funcall
7280 (if (fboundp 'set-process-query-on-exit-flag)
7281 (symbol-function 'set-process-query-on-exit-flag)
7282 (symbol-function 'process-kill-without-query))
7283 process flag))
7286 ;; ------------------------------------------------------------
7287 ;; -- Kludges section --
7288 ;; ------------------------------------------------------------
7290 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7291 ;; does not deal well with newline characters. Newline is replaced by
7292 ;; backslash newline. But if, say, the string `a backslash newline b'
7293 ;; is passed to a shell, the shell will expand this into "ab",
7294 ;; completely omitting the newline. This is not what was intended.
7295 ;; It does not appear to be possible to make the function
7296 ;; `shell-quote-argument' work with newlines without making it
7297 ;; dependent on the shell used. But within this package, we know that
7298 ;; we will always use a Bourne-like shell, so we use an approach which
7299 ;; groks newlines.
7301 ;; The approach is simple: we call `shell-quote-argument', then
7302 ;; massage the newline part of the result.
7304 ;; This function should produce a string which is grokked by a Unix
7305 ;; shell, even if the Emacs is running on Windows. Since this is the
7306 ;; kludges section, we bind `system-type' in such a way that
7307 ;; `shell-quote-arguments' behaves as if on Unix.
7309 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7310 ;; function to work with Bourne-like shells.
7312 ;; CCC: This function should be rewritten so that
7313 ;; `shell-quote-argument' is not used. This way, we are safe from
7314 ;; changes in `shell-quote-argument'.
7315 (defun tramp-shell-quote-argument (s)
7316 "Similar to `shell-quote-argument', but groks newlines.
7317 Only works for Bourne-like shells."
7318 (let ((system-type 'not-windows))
7319 (save-match-data
7320 (let ((result (shell-quote-argument s))
7321 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7322 (when (and (>= (length result) 2)
7323 (string= (substring result 0 2) "\\~"))
7324 (setq result (substring result 1)))
7325 (while (string-match nl result)
7326 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7327 t t result)))
7328 result))))
7330 ;; ;; EFS hooks itself into the file name handling stuff in more places
7331 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7332 ;; ;; away from tramp.el file names.
7333 ;; ;;
7334 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7335 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7336 ;; ;; connection to help it's dired process. Not that I have any real
7337 ;; ;; idea *why* this is helpful to dired.
7338 ;; ;;
7339 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7340 ;; ;;
7341 ;; ;; Daniel Pittman <daniel@danann.net>
7342 ;; ;;
7343 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7344 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7345 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7346 ;; ;; EFS might nicht be loaded yet.
7347 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7348 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7349 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7350 ;; nil
7351 ;; ad-do-it))
7353 ;; We currently (sometimes) use "[" and "]" in the filename format.
7354 ;; This means that Emacs wants to expand wildcards if
7355 ;; `find-file-wildcards' is non-nil, and then barfs because no
7356 ;; expansion could be found. We detect this situation and do
7357 ;; something really awful: we have `file-expand-wildcards' return the
7358 ;; original filename if it can't expand anything. Let's just hope
7359 ;; that this doesn't break anything else.
7360 ;; CCC: This check is now also really awful; we should search all
7361 ;; of the filename format, not just the prefix.
7362 (when (string-match "\\[" tramp-prefix-format)
7363 (defadvice file-expand-wildcards (around tramp-fix activate)
7364 (let ((name (ad-get-arg 0)))
7365 (if (tramp-tramp-file-p name)
7366 ;; If it's a Tramp file, dissect it and look if wildcards
7367 ;; need to be expanded at all.
7368 (let ((v (tramp-dissect-file-name name)))
7369 (if (string-match "[[*?]" (tramp-file-name-localname v))
7370 (let ((res ad-do-it))
7371 (setq ad-return-value (or res (list name))))
7372 (setq ad-return-value (list name))))
7373 ;; If it is not a Tramp file, just run the original function.
7374 (let ((res ad-do-it))
7375 (setq ad-return-value (or res (list name)))))))
7376 (add-hook 'tramp-unload-hook
7377 '(lambda () (ad-unadvise 'file-expand-wildcards))))
7379 ;; Tramp version is useful in a number of situations.
7381 (defun tramp-version (arg)
7382 "Print version number of tramp.el in minibuffer or current buffer."
7383 (interactive "P")
7384 (if arg (insert tramp-version) (message tramp-version)))
7386 ;; Make the `reporter` functionality available for making bug reports about
7387 ;; the package. A most useful piece of code.
7389 (unless (fboundp 'reporter-submit-bug-report)
7390 (autoload 'reporter-submit-bug-report "reporter"))
7392 (defun tramp-bug ()
7393 "Submit a bug report to the TRAMP developers."
7394 (interactive)
7395 (require 'reporter)
7396 (catch 'dont-send
7397 (let ((reporter-prompt-for-summary-p t))
7398 (reporter-submit-bug-report
7399 tramp-bug-report-address ; to-address
7400 (format "tramp (%s)" tramp-version) ; package name and version
7401 (delq nil
7402 `(;; Current state
7403 tramp-ls-command
7404 tramp-test-groks-nt
7405 tramp-file-exists-command
7406 tramp-current-multi-method
7407 tramp-current-method
7408 tramp-current-user
7409 tramp-current-host
7411 ;; System defaults
7412 tramp-auto-save-directory ; vars to dump
7413 tramp-default-method
7414 tramp-rsh-end-of-line
7415 tramp-default-password-end-of-line
7416 tramp-remote-path
7417 tramp-login-prompt-regexp
7418 ;; Mask non-7bit characters
7419 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7420 tramp-wrong-passwd-regexp
7421 tramp-yesno-prompt-regexp
7422 tramp-yn-prompt-regexp
7423 tramp-terminal-prompt-regexp
7424 tramp-temp-name-prefix
7425 tramp-file-name-structure
7426 tramp-file-name-regexp
7427 tramp-multi-file-name-structure
7428 tramp-multi-file-name-hop-structure
7429 tramp-multi-methods
7430 tramp-multi-connection-function-alist
7431 tramp-methods
7432 tramp-end-of-output
7433 tramp-coding-commands
7434 tramp-actions-before-shell
7435 tramp-actions-copy-out-of-band
7436 tramp-multi-actions
7437 tramp-terminal-type
7438 ;; Mask non-7bit characters
7439 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7440 tramp-chunksize
7441 ,(when (boundp 'tramp-backup-directory-alist)
7442 'tramp-backup-directory-alist)
7443 ,(when (boundp 'tramp-bkup-backup-directory-info)
7444 'tramp-bkup-backup-directory-info)
7446 ;; Non-tramp variables of interest
7447 ;; Mask non-7bit characters
7448 (shell-prompt-pattern . tramp-reporter-dump-variable)
7449 backup-by-copying
7450 backup-by-copying-when-linked
7451 backup-by-copying-when-mismatch
7452 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7453 'backup-by-copying-when-privileged-mismatch)
7454 ,(when (boundp 'password-cache)
7455 'password-cache)
7456 ,(when (boundp 'password-cache-expiry)
7457 'password-cache-expiry)
7458 ,(when (boundp 'backup-directory-alist)
7459 'backup-directory-alist)
7460 ,(when (boundp 'bkup-backup-directory-info)
7461 'bkup-backup-directory-info)
7462 file-name-handler-alist))
7464 'tramp-load-report-modules ; pre-hook
7465 'tramp-append-tramp-buffers ; post-hook
7467 Enter your bug report in this message, including as much detail as you
7468 possibly can about the problem, what you did to cause it and what the
7469 local and remote machines are.
7471 If you can give a simple set of instructions to make this bug happen
7472 reliably, please include those. Thank you for helping kill bugs in
7473 TRAMP.
7475 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7476 the ~/.emacs file and to repeat the bug. Then, include the contents
7477 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7478 report.
7480 --bug report follows this line--
7481 "))))
7483 (defun tramp-reporter-dump-variable (varsym mailbuf)
7484 "Pretty-print the value of the variable in symbol VARSYM.
7485 Used for non-7bit chars in strings."
7486 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7487 (val (with-current-buffer reporter-eval-buffer
7488 (symbol-value varsym))))
7490 ;; There are characters to be masked.
7491 (when (and (boundp 'mm-7bit-chars)
7492 (string-match
7493 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7494 (with-current-buffer reporter-eval-buffer
7495 (set varsym (concat "(base64-decode-string \""
7496 (base64-encode-string val)
7497 "\")"))))
7499 ;; Dump variable.
7500 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7502 ;; Remove string quotation.
7503 (forward-line -1)
7504 (when (looking-at
7505 (concat "\\(^.*\\)" "\"" ;; \1 "
7506 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7507 "\\(\".*\\)" "\\\\" ;; \3 \
7508 "\\(\")\\)" "\"$")) ;; \4 "
7509 (replace-match "\\1\\2\\3\\4")
7510 (beginning-of-line)
7511 (insert " ;; variable encoded due to non-printable characters\n"))
7512 (forward-line 1)
7514 ;; Reset VARSYM to old value.
7515 (with-current-buffer reporter-eval-buffer
7516 (set varsym val))))
7518 (defun tramp-load-report-modules ()
7519 "Load needed modules for reporting."
7521 ;; We load message.el and mml.el from Gnus.
7522 (if (featurep 'xemacs)
7523 (progn
7524 (load "message" 'noerror)
7525 (load "mml" 'noerror))
7526 (require 'message nil 'noerror)
7527 (require 'mml nil 'noerror))
7528 (when (functionp 'message-mode)
7529 (funcall (symbol-function 'message-mode)))
7530 (when (functionp 'mml-mode)
7531 (funcall (symbol-function 'mml-mode) t)))
7533 (defun tramp-append-tramp-buffers ()
7534 "Append Tramp buffers into the bug report."
7536 (when (and
7537 (eq major-mode 'message-mode)
7538 (boundp 'mml-mode)
7539 (symbol-value 'mml-mode))
7541 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7542 (buffer-list
7543 (delq nil
7544 (mapcar '(lambda (b)
7545 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7546 (buffer-list))))
7547 (curbuf (current-buffer)))
7549 ;; There is at least one Tramp buffer.
7550 (when buffer-list
7551 (switch-to-buffer (list-buffers-noselect nil))
7552 (delete-other-windows)
7553 (setq buffer-read-only nil)
7554 (goto-char (point-min))
7555 (while (not (eobp))
7556 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7557 (forward-line 1)
7558 (forward-line 0)
7559 (let ((start (point)))
7560 (forward-line 1)
7561 (kill-region start (point)))))
7562 (insert "
7563 The buffer(s) above will be appended to this message. If you don't want
7564 to append a buffer because it contains sensible data, or because the buffer
7565 is too large, you should delete the respective buffer. The buffer(s) will
7566 contain user and host names. Passwords will never be included there.")
7568 (when (and tramp-debug-buffer (> tramp-verbose 9))
7569 (insert "\n\n")
7570 (let ((start (point)))
7571 (insert "\
7572 Please note that you have set `tramp-verbose' to a value greater than 9.
7573 Therefore, the contents of files might be included in the debug buffer(s).")
7574 (add-text-properties start (point) (list 'face 'italic))))
7576 (set-buffer-modified-p nil)
7577 (setq buffer-read-only t)
7578 (goto-char (point-min))
7580 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7581 ;; OK, let's send. First we delete the buffer list.
7582 (progn
7583 (kill-buffer nil)
7584 (switch-to-buffer curbuf)
7585 (goto-char (point-max))
7586 (insert "\n\n")
7587 (dolist (buffer buffer-list)
7588 (funcall (symbol-function 'mml-insert-empty-tag)
7589 'part 'type "text/plain" 'encoding "base64"
7590 'disposition "attachment" 'buffer (buffer-name buffer)
7591 'description (buffer-name buffer)))
7592 (set-buffer-modified-p nil))
7594 ;; Don't send. Delete the message buffer.
7595 (set-buffer curbuf)
7596 (set-buffer-modified-p nil)
7597 (kill-buffer nil)
7598 (throw 'dont-send nil))))))
7600 (defalias 'tramp-submit-bug 'tramp-bug)
7602 ;; Checklist for `tramp-unload-hook'
7603 ;; - Unload all `tramp-*' packages
7604 ;; - Reset `file-name-handler-alist'
7605 ;; - Cleanup hooks where Tramp functions are in
7606 ;; - Cleanup advised functions
7607 ;; - Cleanup autoloads
7608 ;;;###autoload
7609 (defun tramp-unload-tramp ()
7610 "Discard Tramp from loading remote files."
7611 (interactive)
7612 ;; When Tramp is not loaded yet, its autoloads are still active.
7613 (tramp-unload-file-name-handlers)
7614 ;; ange-ftp settings must be enabled.
7615 (when (functionp 'tramp-ftp-enable-ange-ftp)
7616 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7617 ;; `tramp-util' unloads also `tramp'.
7618 (condition-case nil ;; maybe its not loaded yet.
7619 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7620 (error nil)))
7622 (provide 'tramp)
7624 ;; Make sure that we get integration with the VC package.
7625 ;; When it is loaded, we need to pull in the integration module.
7626 ;; This must come after (provide 'tramp) because tramp-vc.el
7627 ;; requires tramp.
7628 (eval-after-load "vc"
7629 '(progn
7630 (require 'tramp-vc)
7631 (add-hook 'tramp-unload-hook
7632 '(lambda ()
7633 (when (featurep 'tramp-vc)
7634 (unload-feature 'tramp-vc 'force))))))
7636 ;;; TODO:
7638 ;; * Allow putting passwords in the filename.
7639 ;; This should be implemented via a general mechanism to add
7640 ;; parameters in filenames. There is currently a kludge for
7641 ;; putting the port number into the filename for ssh and ftp
7642 ;; files. This could be subsumed by the new mechanism as well.
7643 ;; Another approach is to read a netrc file like ~/.authinfo
7644 ;; from Gnus.
7645 ;; * Handle nonlocal exits such as C-g.
7646 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7647 ;; * Add fallback for inline encodings. This should be used
7648 ;; if the remote end doesn't support mimencode or a similar program.
7649 ;; For reading files from the remote host, we can just parse the output
7650 ;; of `od -b'. For writing files to the remote host, we construct
7651 ;; a shell program which contains only "safe" ascii characters
7652 ;; and which writes the right bytes to the file. We can use printf(1)
7653 ;; or "echo -e" or the printf function in awk and use octal escapes
7654 ;; for the "dangerous" characters. The null byte might be a problem.
7655 ;; On some systems, the octal escape doesn't work. So we try the following
7656 ;; two commands to write a null byte:
7657 ;; dd if=/dev/zero bs=1 count=1
7658 ;; echo | tr '\n' '\000'
7659 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7660 ;; the two via a format which can be `uu' or `b64'. Then we can search
7661 ;; for the right local commands and the right remote commands separately.
7662 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7663 ;; work for remote files.
7664 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7665 ;; `shell-quote-argument'.
7666 ;; * Completion gets confused when you leave out the method name.
7667 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7668 ;; by the files in that directory. Add this here.
7669 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7670 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7671 ;; * When logging in, keep looking for questions according to an alist
7672 ;; and then invoke the right function.
7673 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7674 ;; * Running CVS remotely doesn't appear to work right. It thinks
7675 ;; files are locked by somebody else even if I'm the locking user.
7676 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7677 ;; (Skip Montanaro)
7678 ;; * Don't use globbing for directories with many files, as this is
7679 ;; likely to produce long command lines, and some shells choke on
7680 ;; long command lines.
7681 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7682 ;; do the right thing.
7683 ;; * `vc-directory' does not work. It never displays any files, even
7684 ;; if it does show files when run locally.
7685 ;; * Allow correction of passwords, if the remote end allows this.
7686 ;; (Mark Hershberger)
7687 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7688 ;; * Do asynchronous `shell-command's.
7689 ;; * Grok `append' parameter for `write-region'.
7690 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7691 ;; * abbreviate-file-name
7692 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7693 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7694 ;; what the user names should default to, though.
7695 ;; * better error checking. At least whenever we see something
7696 ;; strange when doing zerop, we should kill the process and start
7697 ;; again. (Greg Stark)
7698 ;; * Add caching for filename completion. (Greg Stark)
7699 ;; Of course, this has issues with usability (stale cache bites)
7700 ;; -- <daniel@danann.net>
7701 ;; * Provide a local cache of old versions of remote files for the rsync
7702 ;; transfer method to use. (Greg Stark)
7703 ;; * Remove unneeded parameters from methods.
7704 ;; * Invoke rsync once for copying a whole directory hierarchy.
7705 ;; (Francesco Potort\e,Al\e(B)
7706 ;; * Should we set PATH ourselves or should we rely on the remote end
7707 ;; to do it?
7708 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7709 ;; * Make it work for different encodings, and for different file name
7710 ;; encodings, too. (Daniel Pittman)
7711 ;; * Change applicable functions to pass a struct tramp-file-name rather
7712 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7713 ;; * Implement asynchronous shell commands.
7714 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7715 ;; * Progress reports while copying files. (Michael Kifer)
7716 ;; * `Smart' connection method that uses inline for small and out of
7717 ;; band for large files. (Michael Kifer)
7718 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7719 ;; then look if it's the right version (with `perl -v').
7720 ;; * When editing a remote CVS controlled file as a different user, VC
7721 ;; gets confused about the file locking status. Try to find out why
7722 ;; the workaround doesn't work.
7723 ;; * Change `copy-file' to grok the case where the filename handler
7724 ;; for the source and the target file are different. Right now,
7725 ;; it looks at the source file and then calls that handler, if
7726 ;; there is one. But since ange-ftp, for instance, does not know
7727 ;; about Tramp, it does not do the right thing if the target file
7728 ;; name is a Tramp name.
7729 ;; * Username and hostname completion.
7730 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7731 ;; connect to host "blabla" already if that host is unique. No idea
7732 ;; how to suppress. Maybe not an essential problem.
7733 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7734 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7735 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7736 ;; Code is nearly identical.
7737 ;; ** Decide whiche files to take for searching user/host names depending on
7738 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7739 ;; ** Enhance variables for debug.
7740 ;; ** Implement "/multi:" completion.
7741 ;; ** Add a learning mode for completion. Make results persistent.
7742 ;; * Allow out-of-band methods as _last_ multi-hop.
7744 ;; Functions for file-name-handler-alist:
7745 ;; diff-latest-backup-file -- in diff.el
7746 ;; dired-uncache -- this will be needed when we do insert-directory caching
7747 ;; file-name-as-directory -- use primitive?
7748 ;; file-name-sans-versions -- use primitive?
7749 ;; get-file-buffer -- use primitive
7750 ;; vc-registered
7752 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7753 ;;; tramp.el ends here