(Easy Customization, Customization Groups)
[emacs.git] / lisp / net / tramp.el
blob10a5dd9eedf82a2a7302cc9d61f26144db6dbf2b
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 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
840 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
841 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
842 "*List of directories to search for executables on remote host.
843 Please notify me about other semi-standard directories to include here.
845 You can use `~' in this list, but when searching for a shell which groks
846 tilde expansion, all directory names starting with `~' will be ignored."
847 :group 'tramp
848 :type '(repeat string))
850 (defcustom tramp-login-prompt-regexp
851 ".*ogin\\( .*\\)?: *"
852 "*Regexp matching login-like prompts.
853 The regexp should match at end of buffer.
855 Sometimes the prompt is reported to look like \"login as:\"."
856 :group 'tramp
857 :type 'regexp)
859 (defcustom tramp-shell-prompt-pattern
860 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
861 "Regexp to match prompts from remote shell.
862 Normally, Tramp expects you to configure `shell-prompt-pattern'
863 correctly, but sometimes it happens that you are connecting to a
864 remote host which sends a different kind of shell prompt. Therefore,
865 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
866 and also things matched by this variable. The default value of this
867 variable is similar to the default value of `shell-prompt-pattern',
868 which should work well in many cases."
869 :group 'tramp
870 :type 'regexp)
872 (defcustom tramp-password-prompt-regexp
873 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
874 "*Regexp matching password-like prompts.
875 The regexp should match at end of buffer.
877 The `sudo' program appears to insert a `^@' character into the prompt."
878 :group 'tramp
879 :type 'regexp)
881 (defcustom tramp-wrong-passwd-regexp
882 (concat "^.*"
883 ;; These strings should be on the last line
884 (regexp-opt '("Permission denied."
885 "Login incorrect"
886 "Login Incorrect"
887 "Connection refused"
888 "Connection closed"
889 "Sorry, try again."
890 "Name or service not known"
891 "Host key verification failed.") t)
892 ".*"
893 "\\|"
894 "^.*\\("
895 ;; Here comes a list of regexes, separated by \\|
896 "Received signal [0-9]+"
897 "\\).*")
898 "*Regexp matching a `login failed' message.
899 The regexp should match at end of buffer."
900 :group 'tramp
901 :type 'regexp)
903 (defcustom tramp-yesno-prompt-regexp
904 (concat
905 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
906 "\\s-*")
907 "Regular expression matching all yes/no queries which need to be confirmed.
908 The confirmation should be done with yes or no.
909 The regexp should match at end of buffer.
910 See also `tramp-yn-prompt-regexp'."
911 :group 'tramp
912 :type 'regexp)
914 (defcustom tramp-yn-prompt-regexp
915 (concat (regexp-opt '("Store key in cache? (y/n)") t)
916 "\\s-*")
917 "Regular expression matching all y/n queries which need to be confirmed.
918 The confirmation should be done with y or n.
919 The regexp should match at end of buffer.
920 See also `tramp-yesno-prompt-regexp'."
921 :group 'tramp
922 :type 'regexp)
924 (defcustom tramp-terminal-prompt-regexp
925 (concat "\\("
926 "TERM = (.*)"
927 "\\|"
928 "Terminal type\\? \\[.*\\]"
929 "\\)\\s-*")
930 "Regular expression matching all terminal setting prompts.
931 The regexp should match at end of buffer.
932 The answer will be provided by `tramp-action-terminal', which see."
933 :group 'tramp
934 :type 'regexp)
936 (defcustom tramp-operation-not-permitted-regexp
937 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
938 (regexp-opt '("Operation not permitted") t))
939 "Regular expression matching keep-date problems in (s)cp operations.
940 Copying has been performed successfully already, so this message can
941 be ignored safely."
942 :group 'tramp
943 :type 'regexp)
945 (defcustom tramp-process-alive-regexp
947 "Regular expression indicating a process has finished.
948 In fact this expression is empty by intention, it will be used only to
949 check regularly the status of the associated process.
950 The answer will be provided by `tramp-action-process-alive',
951 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
952 :group 'tramp
953 :type 'regexp)
955 (defcustom tramp-temp-name-prefix "tramp."
956 "*Prefix to use for temporary files.
957 If this is a relative file name (such as \"tramp.\"), it is considered
958 relative to the directory name returned by the function
959 `tramp-temporary-file-directory' (which see). It may also be an
960 absolute file name; don't forget to include a prefix for the filename
961 part, though."
962 :group 'tramp
963 :type 'string)
965 (defcustom tramp-discard-garbage nil
966 "*If non-nil, try to discard garbage sent by remote shell.
967 Some shells send such garbage upon connection setup."
968 :group 'tramp
969 :type 'boolean)
971 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
972 "*Alist specifying extra arguments to pass to the remote shell.
973 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
974 matching the shell file name and ARGS is a string specifying the
975 arguments.
977 This variable is only used when Tramp needs to start up another shell
978 for tilde expansion. The extra arguments should typically prevent the
979 shell from reading its init file."
980 :group 'tramp
981 ;; This might be the wrong way to test whether the widget type
982 ;; `alist' is available. Who knows the right way to test it?
983 :type (if (get 'alist 'widget-type)
984 '(alist :key-type string :value-type string)
985 '(repeat (cons string string))))
987 (defcustom tramp-prefix-format
988 (if tramp-unified-filenames "/" "/[")
989 "*String matching the very beginning of tramp file names.
990 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
991 :group 'tramp
992 :type 'string)
994 (defcustom tramp-prefix-regexp
995 (concat "^" (regexp-quote tramp-prefix-format))
996 "*Regexp matching the very beginning of tramp file names.
997 Should always start with \"^\". Derived from `tramp-prefix-format'."
998 :group 'tramp
999 :type 'regexp)
1001 (defcustom tramp-method-regexp
1002 "[a-zA-Z_0-9-]+"
1003 "*Regexp matching methods identifiers."
1004 :group 'tramp
1005 :type 'regexp)
1007 ;; It is a little bit annoying that in XEmacs case this delimeter is different
1008 ;; for single-hop and multi-hop cases.
1009 (defcustom tramp-postfix-single-method-format
1010 (if tramp-unified-filenames ":" "/")
1011 "*String matching delimeter between method and user or host names.
1012 Applicable for single-hop methods.
1013 Used in `tramp-make-tramp-file-name'."
1014 :group 'tramp
1015 :type 'string)
1017 (defcustom tramp-postfix-single-method-regexp
1018 (regexp-quote tramp-postfix-single-method-format)
1019 "*Regexp matching delimeter between method and user or host names.
1020 Applicable for single-hop methods.
1021 Derived from `tramp-postfix-single-method-format'."
1022 :group 'tramp
1023 :type 'regexp)
1025 (defcustom tramp-postfix-multi-method-format
1027 "*String matching delimeter between method and user or host names.
1028 Applicable for multi-hop methods.
1029 Used in `tramp-make-tramp-multi-file-name'."
1030 :group 'tramp
1031 :type 'string)
1033 (defcustom tramp-postfix-multi-method-regexp
1034 (regexp-quote tramp-postfix-multi-method-format)
1035 "*Regexp matching delimeter between method and user or host names.
1036 Applicable for multi-hop methods.
1037 Derived from `tramp-postfix-multi-method-format'."
1038 :group 'tramp
1039 :type 'regexp)
1041 (defcustom tramp-postfix-multi-hop-format
1042 (if tramp-unified-filenames ":" "/")
1043 "*String matching delimeter between host and next method.
1044 Applicable for multi-hop methods.
1045 Used in `tramp-make-tramp-multi-file-name'."
1046 :group 'tramp
1047 :type 'string)
1049 (defcustom tramp-postfix-multi-hop-regexp
1050 (regexp-quote tramp-postfix-multi-hop-format)
1051 "*Regexp matching delimeter between host and next method.
1052 Applicable for multi-hop methods.
1053 Derived from `tramp-postfix-multi-hop-format'."
1054 :group 'tramp
1055 :type 'regexp)
1057 (defcustom tramp-user-regexp
1058 "[^:/ \t]*"
1059 "*Regexp matching user names."
1060 :group 'tramp
1061 :type 'regexp)
1063 (defcustom tramp-postfix-user-format
1065 "*String matching delimeter between user and host names.
1066 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1067 :group 'tramp
1068 :type 'string)
1070 (defcustom tramp-postfix-user-regexp
1071 (regexp-quote tramp-postfix-user-format)
1072 "*Regexp matching delimeter between user and host names.
1073 Derived from `tramp-postfix-user-format'."
1074 :group 'tramp
1075 :type 'regexp)
1077 (defcustom tramp-host-regexp
1078 "[a-zA-Z0-9_.-]*"
1079 "*Regexp matching host names."
1080 :group 'tramp
1081 :type 'regexp)
1083 (defcustom tramp-host-with-port-regexp
1084 "[a-zA-Z0-9_.#-]*"
1085 "*Regexp matching host names."
1086 :group 'tramp
1087 :type 'regexp)
1089 (defcustom tramp-postfix-host-format
1090 (if tramp-unified-filenames ":" "]")
1091 "*String matching delimeter between host names and localnames.
1092 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1093 :group 'tramp
1094 :type 'string)
1096 (defcustom tramp-postfix-host-regexp
1097 (regexp-quote tramp-postfix-host-format)
1098 "*Regexp matching delimeter between host names and localnames.
1099 Derived from `tramp-postfix-host-format'."
1100 :group 'tramp
1101 :type 'regexp)
1103 (defcustom tramp-localname-regexp
1104 ".*$"
1105 "*Regexp matching localnames."
1106 :group 'tramp
1107 :type 'regexp)
1109 ;; File name format.
1111 (defcustom tramp-file-name-structure
1112 (list
1113 (concat
1114 tramp-prefix-regexp
1115 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1116 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1117 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1118 "\\(" tramp-localname-regexp "\\)")
1119 2 4 5 6)
1121 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1122 the tramp file name structure.
1124 The first element REGEXP is a regular expression matching a tramp file
1125 name. The regex should contain parentheses around the method name,
1126 the user name, the host name, and the file name parts.
1128 The second element METHOD is a number, saying which pair of
1129 parentheses matches the method name. The third element USER is
1130 similar, but for the user name. The fourth element HOST is similar,
1131 but for the host name. The fifth element FILE is for the file name.
1132 These numbers are passed directly to `match-string', which see. That
1133 means the opening parentheses are counted to identify the pair.
1135 See also `tramp-file-name-regexp'."
1136 :group 'tramp
1137 :type '(list (regexp :tag "File name regexp")
1138 (integer :tag "Paren pair for method name")
1139 (integer :tag "Paren pair for user name ")
1140 (integer :tag "Paren pair for host name ")
1141 (integer :tag "Paren pair for file name ")))
1143 ;;;###autoload
1144 (defconst tramp-file-name-regexp-unified
1145 "\\`/[^/:]+:"
1146 "Value for `tramp-file-name-regexp' for unified remoting.
1147 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1148 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1150 ;;;###autoload
1151 (defconst tramp-file-name-regexp-separate
1152 "\\`/\\[.*\\]"
1153 "Value for `tramp-file-name-regexp' for separate remoting.
1154 XEmacs uses a separate filename syntax for Tramp and EFS.
1155 See `tramp-file-name-structure-separate' for more explanations.")
1157 ;;;###autoload
1158 (defcustom tramp-file-name-regexp
1159 (if tramp-unified-filenames
1160 tramp-file-name-regexp-unified
1161 tramp-file-name-regexp-separate)
1162 "*Regular expression matching file names handled by tramp.
1163 This regexp should match tramp file names but no other file names.
1164 \(When tramp.el is loaded, this regular expression is prepended to
1165 `file-name-handler-alist', and that is searched sequentially. Thus,
1166 if the tramp entry appears rather early in the `file-name-handler-alist'
1167 and is a bit too general, then some files might be considered tramp
1168 files which are not really tramp files.
1170 Please note that the entry in `file-name-handler-alist' is made when
1171 this file (tramp.el) is loaded. This means that this variable must be set
1172 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1173 updated after changing this variable.
1175 Also see `tramp-file-name-structure'."
1176 :group 'tramp
1177 :type 'regexp)
1179 ;;;###autoload
1180 (defconst tramp-completion-file-name-regexp-unified
1181 "^/$\\|^/[^/:][^/]*$"
1182 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1183 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1184 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1186 ;;;###autoload
1187 (defconst tramp-completion-file-name-regexp-separate
1188 "^/\\([[][^]]*\\)?$"
1189 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1190 XEmacs uses a separate filename syntax for Tramp and EFS.
1191 See `tramp-file-name-structure-separate' for more explanations.")
1193 ;;;###autoload
1194 (defcustom tramp-completion-file-name-regexp
1195 (if tramp-unified-filenames
1196 tramp-completion-file-name-regexp-unified
1197 tramp-completion-file-name-regexp-separate)
1198 "*Regular expression matching file names handled by tramp completion.
1199 This regexp should match partial tramp file names only.
1201 Please note that the entry in `file-name-handler-alist' is made when
1202 this file (tramp.el) is loaded. This means that this variable must be set
1203 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1204 updated after changing this variable.
1206 Also see `tramp-file-name-structure'."
1207 :group 'tramp
1208 :type 'regexp)
1210 (defcustom tramp-multi-file-name-structure
1211 (list
1212 (concat
1213 tramp-prefix-regexp
1214 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1215 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1216 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1217 2 3 -1)
1218 "*Describes the file name structure of `multi' files.
1219 Multi files allow you to contact a remote host in several hops.
1220 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1222 The first element, REGEXP, gives a regular expression to match against
1223 the file name. In this regular expression, `%s' is replaced with the
1224 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1225 allow multiple hops, you normally want to use something like
1226 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1227 of parentheses is used for the HOP element, see below.)
1229 All remaining elements are numbers. METHOD gives the number of the
1230 paren pair which matches the method name. HOP gives the number of the
1231 paren pair which matches the hop sequence. LOCALNAME gives the number of
1232 the paren pair which matches the localname (pathname) on the remote host.
1234 LOCALNAME can also be negative, which means to count from the end. Ie, a
1235 value of -1 means the last paren pair.
1237 I think it would be good if the regexp matches the whole of the
1238 string, but I haven't actually tried what happens if it doesn't..."
1239 :group 'tramp
1240 :type '(list (regexp :tag "File name regexp")
1241 (integer :tag "Paren pair for method name")
1242 (integer :tag "Paren pair for hops")
1243 (integer :tag "Paren pair to match localname")))
1245 (defcustom tramp-multi-file-name-hop-structure
1246 (list
1247 (concat
1248 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1249 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1250 "\\(" tramp-host-with-port-regexp "\\)")
1251 1 2 3)
1252 "*Describes the structure of a hop in multi files.
1253 This is a list of four elements (REGEXP METHOD USER HOST). First
1254 element REGEXP is used to match against the hop. Pair number METHOD
1255 matches the method of one hop, pair number USER matches the user of
1256 one hop, pair number HOST matches the host of one hop.
1258 This regular expression should match exactly all of one hop."
1259 :group 'tramp
1260 :type '(list (regexp :tag "Hop regexp")
1261 (integer :tag "Paren pair for method name")
1262 (integer :tag "Paren pair for user name")
1263 (integer :tag "Paren pair for host name")))
1265 (defcustom tramp-make-multi-tramp-file-format
1266 (list
1267 (concat tramp-prefix-format "%m")
1268 (concat tramp-postfix-multi-hop-format
1269 "%m" tramp-postfix-multi-method-format
1270 "%u" tramp-postfix-user-format
1271 "%h")
1272 (concat tramp-postfix-host-format "%p"))
1273 "*Describes how to construct a `multi' file name.
1274 This is a list of three elements PREFIX, HOP and LOCALNAME.
1276 The first element PREFIX says how to construct the prefix, the second
1277 element HOP specifies what each hop looks like, and the final element
1278 LOCALNAME says how to construct the localname (pathname).
1280 In PREFIX, `%%' means `%' and `%m' means the method name.
1282 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1283 user and hop host, respectively.
1285 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1287 The resulting file name always contains one copy of PREFIX and one
1288 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1289 name.
1291 Note: the current implementation requires the prefix to contain the
1292 method name, followed by all the hops, and the localname must come
1293 last."
1294 :group 'tramp
1295 :type '(list string string string))
1297 (defcustom tramp-terminal-type "dumb"
1298 "*Value of TERM environment variable for logging in to remote host.
1299 Because Tramp wants to parse the output of the remote shell, it is easily
1300 confused by ANSI color escape sequences and suchlike. Often, shell init
1301 files conditionalize this setup based on the TERM environment variable."
1302 :group 'tramp
1303 :type 'string)
1305 (defcustom tramp-completion-without-shell-p nil
1306 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1307 Using shell wildcards for completions has the advantage that it can be
1308 fast even in large directories, but completion is always
1309 case-sensitive. Relying on Lisp only means that case-insensitive
1310 completion is possible (subject to the variable `completion-ignore-case'),
1311 but it might be slow on large directories."
1312 :group 'tramp
1313 :type 'boolean)
1315 (defcustom tramp-actions-before-shell
1316 '((tramp-password-prompt-regexp tramp-action-password)
1317 (tramp-login-prompt-regexp tramp-action-login)
1318 (shell-prompt-pattern tramp-action-succeed)
1319 (tramp-shell-prompt-pattern tramp-action-succeed)
1320 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1321 (tramp-yesno-prompt-regexp tramp-action-yesno)
1322 (tramp-yn-prompt-regexp tramp-action-yn)
1323 (tramp-terminal-prompt-regexp tramp-action-terminal)
1324 (tramp-process-alive-regexp tramp-action-process-alive))
1325 "List of pattern/action pairs.
1326 Whenever a pattern matches, the corresponding action is performed.
1327 Each item looks like (PATTERN ACTION).
1329 The PATTERN should be a symbol, a variable. The value of this
1330 variable gives the regular expression to search for. Note that the
1331 regexp must match at the end of the buffer, \"\\'\" is implicitly
1332 appended to it.
1334 The ACTION should also be a symbol, but a function. When the
1335 corresponding PATTERN matches, the ACTION function is called."
1336 :group 'tramp
1337 :type '(repeat (list variable function)))
1339 (defcustom tramp-actions-copy-out-of-band
1340 '((tramp-password-prompt-regexp tramp-action-password)
1341 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1342 (tramp-process-alive-regexp tramp-action-out-of-band))
1343 "List of pattern/action pairs.
1344 This list is used for copying/renaming with out-of-band methods.
1345 See `tramp-actions-before-shell' for more info."
1346 :group 'tramp
1347 :type '(repeat (list variable function)))
1349 (defcustom tramp-multi-actions
1350 '((tramp-password-prompt-regexp tramp-multi-action-password)
1351 (tramp-login-prompt-regexp tramp-multi-action-login)
1352 (shell-prompt-pattern tramp-multi-action-succeed)
1353 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1354 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1355 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1356 "List of pattern/action pairs.
1357 This list is used for each hop in multi-hop connections.
1358 See `tramp-actions-before-shell' for more info."
1359 :group 'tramp
1360 :type '(repeat (list variable function)))
1362 (defcustom tramp-initial-commands
1363 '("unset HISTORY"
1364 "unset correct"
1365 "unset autocorrect")
1366 "List of commands to send to the first remote shell that we see.
1367 These commands will be sent to any shell, and thus they should be
1368 designed to work in such circumstances. Also, restrict the commands
1369 to the bare necessity for getting the remote shell into a state
1370 where it is possible to execute the Bourne-ish shell.
1372 At the moment, the command to execute the Bourne-ish shell uses strange
1373 quoting which `tcsh' tries to correct, so we send the command \"unset
1374 autocorrect\" to the remote host."
1375 :group 'tramp
1376 :type '(repeat string))
1378 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1379 ;; known to have a bug in `process-send-string'; some ssh connections appear
1380 ;; to drop bytes when data is sent too quickly. There is also a connection
1381 ;; buffer local variable, which is computed depending on remote host properties
1382 ;; when `tramp-chunksize' is zero or nil.
1383 (defcustom tramp-chunksize
1384 (when (and (not (featurep 'xemacs))
1385 (memq system-type '(hpux)))
1386 500)
1387 ;; Parentheses in docstring starting at beginning of line are escaped.
1388 ;; Fontification is messed up when
1389 ;; `open-paren-in-column-0-is-defun-start' set to t.
1390 "*If non-nil, chunksize for sending input to local process.
1391 It is necessary only on systems which have a buggy `process-send-string'
1392 implementation. The necessity, whether this variable must be set, can be
1393 checked via the following code:
1395 (with-temp-buffer
1396 (let* ((user \"xxx\") (host \"yyy\")
1397 (init 0) (step 50)
1398 (sent init) (received init))
1399 (while (= sent received)
1400 (setq sent (+ sent step))
1401 (erase-buffer)
1402 (let ((proc (start-process (buffer-name) (current-buffer)
1403 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1404 (when (memq (process-status proc) '(run open))
1405 (process-send-string proc (make-string sent ?\\ ))
1406 (process-send-eof proc)
1407 (process-send-eof proc))
1408 (while (not (progn (goto-char (point-min))
1409 (re-search-forward \"\\\\w+\" (point-max) t)))
1410 (accept-process-output proc 1))
1411 (when (memq (process-status proc) '(run open))
1412 (setq received (string-to-number (match-string 0)))
1413 (delete-process proc)
1414 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1415 (sit-for 0))))
1416 (if (> sent (+ init step))
1417 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1418 (- sent step))
1419 (message \"Test does not work\")
1420 (display-buffer (current-buffer))
1421 (sit-for 30))))
1423 In the Emacs normally running Tramp, evaluate the above code
1424 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1425 respectively). You can do this, for example, by pasting it into
1426 the `*scratch*' buffer and then hitting C-j with the cursor after the
1427 last closing parenthesis. Note that it works only if you have configured
1428 \"ssh\" to run without password query, see ssh-agent(1).
1430 You will see the number of bytes sent successfully to the remote host.
1431 If that number exceeds 1000, you can stop the execution by hitting
1432 C-g, because your Emacs is likely clean.
1434 If your Emacs is buggy, the code stops and gives you an indication
1435 about the value `tramp-chunksize' should be set. Maybe you could just
1436 experiment a bit, e.g. changing the values of `init' and `step'
1437 in the third line of the code.
1439 When it is necessary to set `tramp-chunksize', you might consider to
1440 use an out-of-the-band method (like \"scp\") instead of an internal one
1441 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1442 performance.
1444 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1445 this variable to be set as well."
1446 :group 'tramp
1447 :type '(choice (const nil) integer))
1449 ;; Logging in to a remote host normally requires obtaining a pty. But
1450 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1451 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1452 ;; for an override of the system default.
1453 (defcustom tramp-process-connection-type t
1454 "Overrides `process-connection-type' for connections from Tramp.
1455 Tramp binds process-connection-type to the value given here before
1456 opening a connection to a remote host."
1457 :group 'tramp
1458 :type '(choice (const nil) (const t) (const pty)))
1460 ;;; Internal Variables:
1462 (defvar tramp-buffer-file-attributes nil
1463 "Holds the `ls -ild' output for the current buffer.
1464 This variable is local to each buffer. It is not used if the remote
1465 machine groks Perl. If it is used, it's used as an emulation for
1466 the visited file modtime.")
1467 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1469 (defvar tramp-md5-function
1470 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1471 ((fboundp 'md5-encode)
1472 (lambda (x) (base64-encode-string
1473 (funcall (symbol-function 'md5-encode) x))))
1474 (t (error "Couldn't find an `md5' function")))
1475 "Function to call for running the MD5 algorithm.")
1477 (defvar tramp-end-of-output
1478 (concat "///"
1479 (funcall tramp-md5-function
1480 (concat
1481 (prin1-to-string process-environment)
1482 (current-time-string)
1483 ;; (prin1-to-string
1484 ;; (if (fboundp 'directory-files-and-attributes)
1485 ;; (funcall 'directory-files-and-attributes
1486 ;; (or (getenv "HOME")
1487 ;; (tramp-temporary-file-directory)))
1488 ;; (mapcar
1489 ;; (lambda (x)
1490 ;; (cons x (file-attributes x)))
1491 ;; (directory-files (or (getenv "HOME")
1492 ;; (tramp-temporary-file-directory))
1493 ;; t))))
1495 "String used to recognize end of output.")
1497 (defvar tramp-connection-function nil
1498 "This internal variable holds a parameter for `tramp-methods'.
1499 In the connection buffer, this variable has the value of the like-named
1500 method parameter, as specified in `tramp-methods' (which see).")
1502 (defvar tramp-remote-sh nil
1503 "This internal variable holds a parameter for `tramp-methods'.
1504 In the connection buffer, this variable has the value of the like-named
1505 method parameter, as specified in `tramp-methods' (which see).")
1507 (defvar tramp-login-program nil
1508 "This internal variable holds a parameter for `tramp-methods'.
1509 In the connection buffer, this variable has the value of the like-named
1510 method parameter, as specified in `tramp-methods' (which see).")
1512 (defvar tramp-login-args nil
1513 "This internal variable holds a parameter for `tramp-methods'.
1514 In the connection buffer, this variable has the value of the like-named
1515 method parameter, as specified in `tramp-methods' (which see).")
1517 (defvar tramp-copy-program nil
1518 "This internal variable holds a parameter for `tramp-methods'.
1519 In the connection buffer, this variable has the value of the like-named
1520 method parameter, as specified in `tramp-methods' (which see).")
1522 (defvar tramp-copy-args nil
1523 "This internal variable holds a parameter for `tramp-methods'.
1524 In the connection buffer, this variable has the value of the like-named
1525 method parameter, as specified in `tramp-methods' (which see).")
1527 (defvar tramp-copy-keep-date-arg nil
1528 "This internal variable holds a parameter for `tramp-methods'.
1529 In the connection buffer, this variable has the value of the like-named
1530 method parameter, as specified in `tramp-methods' (which see).")
1532 (defvar tramp-encoding-command nil
1533 "This internal variable holds a parameter for `tramp-methods'.
1534 In the connection buffer, this variable has the value of the like-named
1535 method parameter, as specified in `tramp-methods' (which see).")
1537 (defvar tramp-decoding-command nil
1538 "This internal variable holds a parameter for `tramp-methods'.
1539 In the connection buffer, this variable has the value of the like-named
1540 method parameter, as specified in `tramp-methods' (which see).")
1542 (defvar tramp-encoding-function nil
1543 "This internal variable holds a parameter for `tramp-methods'.
1544 In the connection buffer, this variable has the value of the like-named
1545 method parameter, as specified in `tramp-methods' (which see).")
1547 (defvar tramp-decoding-function nil
1548 "This internal variable holds a parameter for `tramp-methods'.
1549 In the connection buffer, this variable has the value of the like-named
1550 method parameter, as specified in `tramp-methods' (which see).")
1552 (defvar tramp-password-end-of-line nil
1553 "This internal variable holds a parameter for `tramp-methods'.
1554 In the connection buffer, this variable has the value of the like-named
1555 method parameter, as specified in `tramp-methods' (which see).")
1557 ;; CCC `local in each buffer'?
1558 (defvar tramp-ls-command nil
1559 "This command is used to get a long listing with numeric user and group ids.
1560 This variable is automatically made buffer-local to each rsh process buffer
1561 upon opening the connection.")
1563 (defvar tramp-current-multi-method nil
1564 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1565 This variable is automatically made buffer-local to each rsh process buffer
1566 upon opening the connection.")
1568 (defvar tramp-current-method nil
1569 "Connection method for this *tramp* buffer.
1570 This variable is automatically made buffer-local to each rsh process buffer
1571 upon opening the connection.")
1573 (defvar tramp-current-user nil
1574 "Remote login name for this *tramp* buffer.
1575 This variable is automatically made buffer-local to each rsh process buffer
1576 upon opening the connection.")
1578 (defvar tramp-current-host nil
1579 "Remote host for this *tramp* buffer.
1580 This variable is automatically made buffer-local to each rsh process buffer
1581 upon opening the connection.")
1583 (defvar tramp-test-groks-nt nil
1584 "Whether the `test' command groks the `-nt' switch.
1585 \(`test A -nt B' tests if file A is newer than file B.)
1586 This variable is automatically made buffer-local to each rsh process buffer
1587 upon opening the connection.")
1589 (defvar tramp-file-exists-command nil
1590 "Command to use for checking if a file exists.
1591 This variable is automatically made buffer-local to each rsh process buffer
1592 upon opening the connection.")
1594 (defconst tramp-uudecode "\
1595 tramp_uudecode () {
1596 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1597 cat /tmp/tramp.$$
1598 rm -f /tmp/tramp.$$
1600 "Shell function to implement `uudecode' to standard output.
1601 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1602 for this or `uudecode -p', but some systems don't, and for them
1603 we have this shell function.")
1605 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1606 ;; output. If you are hacking on this, note that you get *no* output
1607 ;; unless this spits out a complete line, including the '\n' at the
1608 ;; end.
1609 ;; The device number is returned as "-1", because there will be a virtual
1610 ;; device number set in `tramp-handle-file-attributes'
1611 (defconst tramp-perl-file-attributes "\
1612 @stat = lstat($ARGV[0]);
1613 if (($stat[2] & 0170000) == 0120000)
1615 $type = readlink($ARGV[0]);
1616 $type = \"\\\"$type\\\"\";
1618 elsif (($stat[2] & 0170000) == 040000)
1620 $type = \"t\";
1622 else
1624 $type = \"nil\"
1626 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1627 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1628 printf(
1629 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1630 $type,
1631 $stat[3],
1632 $uid,
1633 $gid,
1634 $stat[8] >> 16 & 0xffff,
1635 $stat[8] & 0xffff,
1636 $stat[9] >> 16 & 0xffff,
1637 $stat[9] & 0xffff,
1638 $stat[10] >> 16 & 0xffff,
1639 $stat[10] & 0xffff,
1640 $stat[7],
1641 $stat[2],
1642 $stat[1] >> 16 & 0xffff,
1643 $stat[1] & 0xffff
1645 "Perl script to produce output suitable for use with `file-attributes'
1646 on the remote file system.")
1648 (defconst tramp-perl-directory-files-and-attributes "\
1649 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1650 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1651 @list = readdir(DIR);
1652 closedir(DIR);
1653 $n = scalar(@list);
1654 printf(\"(\\n\");
1655 for($i = 0; $i < $n; $i++)
1657 $filename = $list[$i];
1658 @stat = lstat($filename);
1659 if (($stat[2] & 0170000) == 0120000)
1661 $type = readlink($filename);
1662 $type = \"\\\"$type\\\"\";
1664 elsif (($stat[2] & 0170000) == 040000)
1666 $type = \"t\";
1668 else
1670 $type = \"nil\"
1672 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1673 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1674 printf(
1675 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1676 $filename,
1677 $type,
1678 $stat[3],
1679 $uid,
1680 $gid,
1681 $stat[8] >> 16 & 0xffff,
1682 $stat[8] & 0xffff,
1683 $stat[9] >> 16 & 0xffff,
1684 $stat[9] & 0xffff,
1685 $stat[10] >> 16 & 0xffff,
1686 $stat[10] & 0xffff,
1687 $stat[7],
1688 $stat[2],
1689 $stat[1] >> 16 & 0xffff,
1690 $stat[1] & 0xffff,
1691 $stat[0] >> 16 & 0xffff,
1692 $stat[0] & 0xffff);
1694 printf(\")\\n\");"
1695 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1696 output.")
1698 ;; ;; These two use uu encoding.
1699 ;; (defvar tramp-perl-encode "%s -e'\
1700 ;; print qq(begin 644 xxx\n);
1701 ;; my $s = q();
1702 ;; my $res = q();
1703 ;; while (read(STDIN, $s, 45)) {
1704 ;; print pack(q(u), $s);
1705 ;; }
1706 ;; print qq(`\n);
1707 ;; print qq(end\n);
1708 ;; '"
1709 ;; "Perl program to use for encoding a file.
1710 ;; Escape sequence %s is replaced with name of Perl binary.")
1712 ;; (defvar tramp-perl-decode "%s -ne '
1713 ;; print unpack q(u), $_;
1714 ;; '"
1715 ;; "Perl program to use for decoding a file.
1716 ;; Escape sequence %s is replaced with name of Perl binary.")
1718 ;; These two use base64 encoding.
1719 (defvar tramp-perl-encode-with-module
1720 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1721 "Perl program to use for encoding a file.
1722 Escape sequence %s is replaced with name of Perl binary.
1723 This string is passed to `format', so percent characters need to be doubled.
1724 This implementation requires the MIME::Base64 Perl module to be installed
1725 on the remote host.")
1727 (defvar tramp-perl-decode-with-module
1728 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1729 "Perl program to use for decoding a file.
1730 Escape sequence %s is replaced with name of Perl binary.
1731 This string is passed to `format', so percent characters need to be doubled.
1732 This implementation requires the MIME::Base64 Perl module to be installed
1733 on the remote host.")
1735 (defvar tramp-perl-encode
1736 "%s -e '
1737 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1738 # Copyright (C) 2002 Free Software Foundation, Inc.
1739 use strict;
1741 my %%trans = do {
1742 my $i = 0;
1743 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1744 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1747 binmode(\\*STDIN);
1749 # We read in chunks of 54 bytes, to generate output lines
1750 # of 72 chars (plus end of line)
1751 $/ = \\54;
1753 while (my $data = <STDIN>) {
1754 my $pad = q();
1756 # Only for the last chunk, and only if did not fill the last three-byte packet
1757 if (eof) {
1758 my $mod = length($data) %% 3;
1759 $pad = q(=) x (3 - $mod) if $mod;
1762 # Not the fastest method, but it is simple: unpack to binary string, split
1763 # by groups of 6 bits and convert back from binary to byte; then map into
1764 # the translation table
1765 print
1766 join q(),
1767 map($trans{$_},
1768 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1769 $pad,
1770 qq(\\n);
1773 "Perl program to use for encoding a file.
1774 Escape sequence %s is replaced with name of Perl binary.
1775 This string is passed to `format', so percent characters need to be doubled.")
1777 (defvar tramp-perl-decode
1778 "%s -e '
1779 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1780 # Copyright (C) 2002 Free Software Foundation, Inc.
1781 use strict;
1783 my %%trans = do {
1784 my $i = 0;
1785 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1786 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1789 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1791 binmode(\\*STDOUT);
1793 # We are going to accumulate into $pending to accept any line length
1794 # (we do not check they are <= 76 chars as the RFC says)
1795 my $pending = q();
1797 while (my $data = <STDIN>) {
1798 chomp $data;
1800 # If we find one or two =, we have reached the end and
1801 # any following data is to be discarded
1802 my $finished = $data =~ s/(==?).*/$1/;
1803 $pending .= $data;
1805 my $len = length($pending);
1806 my $chunk = substr($pending, 0, $len & ~3);
1807 $pending = substr($pending, $len & ~3 + 1);
1809 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1810 # split in 8-bit chunks and convert back to char.
1811 print join q(),
1812 map $bytes{$_},
1813 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1815 last if $finished;
1818 "Perl program to use for decoding a file.
1819 Escape sequence %s is replaced with name of Perl binary.
1820 This string is passed to `format', so percent characters need to be doubled.")
1822 ; These values conform to `file-attributes' from XEmacs 21.2.
1823 ; GNU Emacs and other tools not checked.
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 GNU Emacs just
1929 ;; `file-name-all-completions' is needed. The other ones are necessary
1930 ;; for XEmacs.
1931 (defconst tramp-completion-file-name-handler-alist
1933 (file-name-directory . tramp-completion-handle-file-name-directory)
1934 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1935 (file-exists-p . tramp-completion-handle-file-exists-p)
1936 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1937 (file-name-completion . tramp-completion-handle-file-name-completion)
1938 (expand-file-name . tramp-completion-handle-expand-file-name))
1939 "Alist of completion handler functions.
1940 Used for file names matching `tramp-file-name-regexp'. Operations not
1941 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1942 normal Emacs functions.")
1944 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1945 (defvar tramp-foreign-file-name-handler-alist
1946 ;; (identity . tramp-sh-file-name-handler) should always be the last
1947 ;; entry, since `identity' always matches.
1948 '((identity . tramp-sh-file-name-handler))
1949 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1950 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1951 calling HANDLER.")
1953 ;;; Internal functions which must come first.
1955 (defsubst tramp-message (level fmt-string &rest args)
1956 "Emit a message depending on verbosity level.
1957 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1958 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1959 Calls function `message' with FMT-STRING as control string and the remaining
1960 ARGS to actually emit the message (if applicable).
1962 This function expects to be called from the tramp buffer only!"
1963 (when (<= level tramp-verbose)
1964 (apply #'message (concat "tramp: " fmt-string) args)
1965 (when tramp-debug-buffer
1966 (save-excursion
1967 (set-buffer
1968 (tramp-get-debug-buffer
1969 tramp-current-multi-method tramp-current-method
1970 tramp-current-user tramp-current-host))
1971 (goto-char (point-max))
1972 (unless (bolp)
1973 (insert "\n"))
1974 (tramp-insert-with-face
1975 'italic
1976 (concat "# " (apply #'format fmt-string args) "\n"))))))
1978 (defun tramp-message-for-buffer
1979 (multi-method method user host level fmt-string &rest args)
1980 "Like `tramp-message' but temporarily switches to the tramp buffer.
1981 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1982 remaining args passed to `tramp-message'."
1983 (save-excursion
1984 (set-buffer (tramp-get-buffer multi-method method user host))
1985 (apply 'tramp-message level fmt-string args)))
1987 (defsubst tramp-line-end-position nil
1988 "Return point at end of line.
1989 Calls `line-end-position' or `point-at-eol' if defined, else
1990 own implementation."
1991 (cond
1992 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1993 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
1994 (t (save-excursion (end-of-line) (point)))))
1996 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1997 "Parse a Tramp filename and make components available in the body.
1999 First arg FILENAME is evaluated and dissected into its components.
2000 Second arg VAR is a symbol. It is used as a variable name to hold
2001 the filename structure. It is also used as a prefix for the variables
2002 holding the components. For example, if VAR is the symbol `foo', then
2003 `foo' will be bound to the whole structure, `foo-multi-method' will
2004 be bound to the multi-method component, and so on for `foo-method',
2005 `foo-user', `foo-host', `foo-localname'.
2007 Remaining args are Lisp expressions to be evaluated (inside an implicit
2008 `progn').
2010 If VAR is nil, then we bind `v' to the structure and `multi-method',
2011 `method', `user', `host', `localname' to the components."
2012 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2013 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2014 (tramp-file-name-multi-method ,(or var 'v)))
2015 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2016 (tramp-file-name-method ,(or var 'v)))
2017 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2018 (tramp-file-name-user ,(or var 'v)))
2019 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2020 (tramp-file-name-host ,(or var 'v)))
2021 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2022 (tramp-file-name-localname ,(or var 'v))))
2023 ,@body))
2025 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2026 ;; To be activated for debugging containing this macro
2027 ;; It works only when VAR is nil. Otherwise, it can be deactivated by
2028 ;; (put 'with-parsed-tramp-file-name 'edebug-form-spec 0)
2029 ;; I'm too stupid to write a precise SPEC for it.
2030 (put 'with-parsed-tramp-file-name 'edebug-form-spec t)
2032 (defmacro tramp-let-maybe (variable value &rest body)
2033 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2034 BODY is executed whether or not the variable is obsolete.
2035 The intent is to protect against `obsolete variable' warnings."
2036 `(if (get ',variable 'byte-obsolete-variable)
2037 (progn ,@body)
2038 (let ((,variable ,value))
2039 ,@body)))
2040 (put 'tramp-let-maybe 'lisp-indent-function 2)
2041 (put 'tramp-let-maybe 'edebug-form-spec t)
2043 ;;; Config Manipulation Functions:
2045 (defun tramp-set-completion-function (method function-list)
2046 "Sets the list of completion functions for METHOD.
2047 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2048 The FUNCTION is intended to parse FILE according its syntax.
2049 It might be a predefined FUNCTION, or a user defined FUNCTION.
2050 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2051 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2052 and `tramp-parse-netrc'.
2054 Example:
2056 (tramp-set-completion-function
2057 \"ssh\"
2058 '((tramp-parse-sconfig \"/etc/ssh_config\")
2059 (tramp-parse-sconfig \"~/.ssh/config\")))"
2061 (let ((r function-list)
2062 (v function-list))
2063 (setq tramp-completion-function-alist
2064 (delete (assoc method tramp-completion-function-alist)
2065 tramp-completion-function-alist))
2067 (while v
2068 ;; Remove double entries
2069 (when (member (car v) (cdr v))
2070 (setcdr v (delete (car v) (cdr v))))
2071 ;; Check for function and file
2072 (unless (and (functionp (nth 0 (car v)))
2073 (file-exists-p (nth 1 (car v))))
2074 (setq r (delete (car v) r)))
2075 (setq v (cdr v)))
2077 (when r
2078 (add-to-list 'tramp-completion-function-alist
2079 (cons method r)))))
2081 (defun tramp-get-completion-function (method)
2082 "Returns list of completion functions for METHOD.
2083 For definition of that list see `tramp-set-completion-function'."
2084 (cdr (assoc method tramp-completion-function-alist)))
2086 ;;; File Name Handler Functions:
2088 (defun tramp-handle-make-symbolic-link
2089 (filename linkname &optional ok-if-already-exists)
2090 "Like `make-symbolic-link' for tramp files.
2091 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2092 the symlink. If LINKNAME is a Tramp file, only the localname component is
2093 used as the target of the symlink.
2095 If LINKNAME is a Tramp file and the localname component is relative, then
2096 it is expanded first, before the localname component is taken. Note that
2097 this can give surprising results if the user/host for the source and
2098 target of the symlink differ."
2099 (with-parsed-tramp-file-name linkname l
2100 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2101 (cwd (file-name-directory l-localname)))
2102 (unless ln
2103 (signal 'file-error
2104 (list "Making a symbolic link."
2105 "ln(1) does not exist on the remote host.")))
2107 ;; Do the 'confirm if exists' thing.
2108 (when (file-exists-p linkname)
2109 ;; What to do?
2110 (if (or (null ok-if-already-exists) ; not allowed to exist
2111 (and (numberp ok-if-already-exists)
2112 (not (yes-or-no-p
2113 (format
2114 "File %s already exists; make it a link anyway? "
2115 l-localname)))))
2116 (signal 'file-already-exists (list "File already exists" l-localname))
2117 (delete-file linkname)))
2119 ;; If FILENAME is a Tramp name, use just the localname component.
2120 (when (tramp-tramp-file-p filename)
2121 (setq filename (tramp-file-name-localname
2122 (tramp-dissect-file-name
2123 (expand-file-name filename)))))
2125 ;; Right, they are on the same host, regardless of user, method, etc.
2126 ;; We now make the link on the remote machine. This will occur as the user
2127 ;; that FILENAME belongs to.
2128 (zerop
2129 (tramp-send-command-and-check
2130 l-multi-method l-method l-user l-host
2131 (format "cd %s && %s -sf %s %s"
2132 cwd ln
2133 filename
2134 l-localname)
2135 t)))))
2138 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2139 "Like `load' for tramp files. Not implemented!"
2140 (unless (file-name-absolute-p file)
2141 (error "Tramp cannot `load' files without absolute file name"))
2142 (with-parsed-tramp-file-name file nil
2143 (unless nosuffix
2144 (cond ((file-exists-p (concat file ".elc"))
2145 (setq file (concat file ".elc")))
2146 ((file-exists-p (concat file ".el"))
2147 (setq file (concat file ".el")))))
2148 (when must-suffix
2149 ;; The first condition is always true for absolute file names.
2150 ;; Included for safety's sake.
2151 (unless (or (file-name-directory file)
2152 (string-match "\\.elc?\\'" file))
2153 (error "File `%s' does not include a `.el' or `.elc' suffix"
2154 file)))
2155 (unless noerror
2156 (when (not (file-exists-p file))
2157 (error "Cannot load nonexistent file `%s'" file)))
2158 (if (not (file-exists-p file))
2160 (unless nomessage
2161 (message "Loading %s..." file))
2162 (let ((local-copy (file-local-copy file)))
2163 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2164 (load local-copy noerror t t)
2165 (delete-file local-copy))
2166 (unless nomessage
2167 (message "Loading %s...done" file))
2168 t)))
2170 ;; Localname manipulation functions that grok TRAMP localnames...
2171 (defun tramp-handle-file-name-directory (file)
2172 "Like `file-name-directory' but aware of TRAMP files."
2173 ;; everything except the last filename thing is the directory
2174 (with-parsed-tramp-file-name file nil
2175 ;; For the following condition, two possibilities should be tried:
2176 ;; (1) (string= localname "")
2177 ;; (2) (or (string= localname "") (string= localname "/"))
2178 ;; The second variant fails when completing a "/" directory on
2179 ;; the remote host, that is a filename which looks like
2180 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2181 ;; We should do some investigation.
2182 (if (string= localname "")
2183 ;; For a filename like "/[foo]", we return "/". The `else'
2184 ;; case would return "/[foo]" unchanged. But if we do that,
2185 ;; then `file-expand-wildcards' ceases to work. It's not
2186 ;; quite clear to me what's the intuition that tells that this
2187 ;; behavior is the right behavior, but oh, well.
2189 ;; run the command on the localname portion only
2190 ;; CCC: This should take into account the remote machine type, no?
2191 ;; --daniel <daniel@danann.net>
2192 (tramp-make-tramp-file-name multi-method method user host
2193 ;; This will not recurse...
2194 (or (file-name-directory localname) "")))))
2196 (defun tramp-handle-file-name-nondirectory (file)
2197 "Like `file-name-nondirectory' but aware of TRAMP files."
2198 (with-parsed-tramp-file-name file nil
2199 (file-name-nondirectory localname)))
2201 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2202 "Like `file-truename' for tramp files."
2203 (with-parsed-tramp-file-name (expand-file-name filename) nil
2204 (let* ((steps (tramp-split-string localname "/"))
2205 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2206 (file-name-as-directory localname)))
2207 (is-dir (string= localname localnamedir))
2208 (thisstep nil)
2209 (numchase 0)
2210 ;; Don't make the following value larger than necessary.
2211 ;; People expect an error message in a timely fashion when
2212 ;; something is wrong; otherwise they might think that Emacs
2213 ;; is hung. Of course, correctness has to come first.
2214 (numchase-limit 20)
2215 (result nil) ;result steps in reverse order
2216 symlink-target)
2217 (tramp-message-for-buffer
2218 multi-method method user host
2219 10 "Finding true name for `%s'" filename)
2220 (while (and steps (< numchase numchase-limit))
2221 (setq thisstep (pop steps))
2222 (tramp-message-for-buffer
2223 multi-method method user host
2224 10 "Check %s"
2225 (mapconcat 'identity
2226 (append '("") (reverse result) (list thisstep))
2227 "/"))
2228 (setq symlink-target
2229 (nth 0 (file-attributes
2230 (tramp-make-tramp-file-name
2231 multi-method method user host
2232 (mapconcat 'identity
2233 (append '("")
2234 (reverse result)
2235 (list thisstep))
2236 "/")))))
2237 (cond ((string= "." thisstep)
2238 (tramp-message-for-buffer multi-method method user host
2239 10 "Ignoring step `.'"))
2240 ((string= ".." thisstep)
2241 (tramp-message-for-buffer multi-method method user host
2242 10 "Processing step `..'")
2243 (pop result))
2244 ((stringp symlink-target)
2245 ;; It's a symlink, follow it.
2246 (tramp-message-for-buffer
2247 multi-method method user host
2248 10 "Follow symlink to %s" symlink-target)
2249 (setq numchase (1+ numchase))
2250 (when (file-name-absolute-p symlink-target)
2251 (setq result nil))
2252 ;; If the symlink was absolute, we'll get a string like
2253 ;; "/user@host:/some/target"; extract the
2254 ;; "/some/target" part from it.
2255 (when (tramp-tramp-file-p symlink-target)
2256 (with-parsed-tramp-file-name symlink-target sym
2257 (unless (equal (list multi-method method user host)
2258 (list sym-multi-method sym-method
2259 sym-user sym-host))
2260 (error "Symlink target `%s' on wrong host"
2261 symlink-target))
2262 (setq symlink-target localname)))
2263 (setq steps
2264 (append (tramp-split-string symlink-target "/") steps)))
2266 ;; It's a file.
2267 (setq result (cons thisstep result)))))
2268 (when (>= numchase numchase-limit)
2269 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2270 (setq result (reverse result))
2271 ;; Combine list to form string.
2272 (setq result
2273 (if result
2274 (mapconcat 'identity (cons "" result) "/")
2275 "/"))
2276 (when (and is-dir (or (string= "" result)
2277 (not (string= (substring result -1) "/"))))
2278 (setq result (concat result "/")))
2279 (tramp-message-for-buffer
2280 multi-method method user host
2281 10 "True name of `%s' is `%s'" filename result)
2282 (tramp-make-tramp-file-name
2283 multi-method method user host result))))
2285 ;; Basic functions.
2287 (defun tramp-handle-file-exists-p (filename)
2288 "Like `file-exists-p' for tramp files."
2289 (with-parsed-tramp-file-name filename nil
2290 (save-excursion
2291 (zerop (tramp-send-command-and-check
2292 multi-method method user host
2293 (format
2294 (tramp-get-file-exists-command multi-method method user host)
2295 (tramp-shell-quote-argument localname)))))))
2297 ;; Devices must distinguish physical file systems. The device numbers
2298 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2299 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2300 ;; EFS use device number "-1". In order to be different, we use device number
2301 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2302 (defvar tramp-devices nil
2303 "Keeps virtual device numbers.")
2305 ;; CCC: This should check for an error condition and signal failure
2306 ;; when something goes wrong.
2307 ;; Daniel Pittman <daniel@danann.net>
2308 (defun tramp-handle-file-attributes (filename &optional id-format)
2309 "Like `file-attributes' for tramp files."
2310 (when (file-exists-p filename)
2311 ;; file exists, find out stuff
2312 (unless id-format (setq id-format 'integer))
2313 (with-parsed-tramp-file-name filename nil
2314 (save-excursion
2315 (tramp-convert-file-attributes
2316 multi-method method user host
2317 (if (tramp-get-remote-perl multi-method method user host)
2318 (tramp-handle-file-attributes-with-perl multi-method method user host
2319 localname id-format)
2320 (tramp-handle-file-attributes-with-ls multi-method method user host
2321 localname id-format)))))))
2323 (defun tramp-handle-file-attributes-with-ls
2324 (multi-method method user host localname &optional id-format)
2325 "Implement `file-attributes' for tramp files using the ls(1) command."
2326 (let (symlinkp dirp
2327 res-inode res-filemodes res-numlinks
2328 res-uid res-gid res-size res-symlink-target)
2329 (tramp-message-for-buffer multi-method method user host 10
2330 "file attributes with ls: %s"
2331 (tramp-make-tramp-file-name
2332 multi-method method user host localname))
2333 (tramp-send-command
2334 multi-method method user host
2335 (format "%s %s %s"
2336 (tramp-get-ls-command multi-method method user host)
2337 (if (eq id-format 'integer) "-ildn" "-ild")
2338 (tramp-shell-quote-argument localname)))
2339 (tramp-wait-for-output)
2340 ;; parse `ls -l' output ...
2341 ;; ... inode
2342 (setq res-inode
2343 (condition-case err
2344 (read (current-buffer))
2345 (invalid-read-syntax
2346 (when (and (equal (cadr err)
2347 "Integer constant overflow in reader")
2348 (string-match
2349 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2350 (caddr err)))
2351 (let* ((big (read (substring (caddr err) 0
2352 (match-beginning 1))))
2353 (small (read (match-string 1 (caddr err))))
2354 (twiddle (/ small 65536)))
2355 (cons (+ big twiddle)
2356 (- small (* twiddle 65536))))))))
2357 ;; ... file mode flags
2358 (setq res-filemodes (symbol-name (read (current-buffer))))
2359 ;; ... number links
2360 (setq res-numlinks (read (current-buffer)))
2361 ;; ... uid and gid
2362 (setq res-uid (read (current-buffer)))
2363 (setq res-gid (read (current-buffer)))
2364 (when (eq id-format 'integer)
2365 (unless (numberp res-uid) (setq res-uid -1))
2366 (unless (numberp res-gid) (setq res-gid -1)))
2367 ;; ... size
2368 (setq res-size (read (current-buffer)))
2369 ;; From the file modes, figure out other stuff.
2370 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2371 (setq dirp (eq ?d (aref res-filemodes 0)))
2372 ;; if symlink, find out file name pointed to
2373 (when symlinkp
2374 (search-forward "-> ")
2375 (setq res-symlink-target
2376 (buffer-substring (point)
2377 (tramp-line-end-position))))
2378 ;; return data gathered
2379 (list
2380 ;; 0. t for directory, string (name linked to) for symbolic
2381 ;; link, or nil.
2382 (or dirp res-symlink-target nil)
2383 ;; 1. Number of links to file.
2384 res-numlinks
2385 ;; 2. File uid.
2386 res-uid
2387 ;; 3. File gid.
2388 res-gid
2389 ;; 4. Last access time, as a list of two integers. First
2390 ;; integer has high-order 16 bits of time, second has low 16
2391 ;; bits.
2392 ;; 5. Last modification time, likewise.
2393 ;; 6. Last status change time, likewise.
2394 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2395 ;; 7. Size in bytes (-1, if number is out of range).
2396 res-size
2397 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2398 res-filemodes
2399 ;; 9. t iff file's gid would change if file were deleted and
2400 ;; recreated. Will be set in `tramp-convert-file-attributes'
2402 ;; 10. inode number.
2403 res-inode
2404 ;; 11. Device number. Will be replaced by a virtual device number.
2408 (defun tramp-handle-file-attributes-with-perl
2409 (multi-method method user host localname &optional id-format)
2410 "Implement `file-attributes' for tramp files using a Perl script."
2411 (tramp-message-for-buffer multi-method method user host 10
2412 "file attributes with perl: %s"
2413 (tramp-make-tramp-file-name
2414 multi-method method user host localname))
2415 (tramp-maybe-send-perl-script multi-method method user host
2416 tramp-perl-file-attributes
2417 "tramp_file_attributes")
2418 (tramp-send-command multi-method method user host
2419 (format "tramp_file_attributes %s %s"
2420 (tramp-shell-quote-argument localname) id-format))
2421 (tramp-wait-for-output)
2422 (read (current-buffer)))
2424 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2425 "Like `set-visited-file-modtime' for tramp files."
2426 (unless (buffer-file-name)
2427 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2428 (buffer-name)))
2429 (if time-list
2430 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2431 (let ((f (buffer-file-name))
2432 coding-system-used)
2433 (with-parsed-tramp-file-name f nil
2434 (let* ((attr (file-attributes f))
2435 ;; '(-1 65535) means file doesn't exists yet.
2436 (modtime (or (nth 5 attr) '(-1 65535))))
2437 (when (boundp 'last-coding-system-used)
2438 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2439 ;; We use '(0 0) as a don't-know value. See also
2440 ;; `tramp-handle-file-attributes-with-ls'.
2441 (if (not (equal modtime '(0 0)))
2442 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2443 (save-excursion
2444 (tramp-send-command
2445 multi-method method user host
2446 (format "%s -ild %s"
2447 (tramp-get-ls-command multi-method method user host)
2448 (tramp-shell-quote-argument localname)))
2449 (tramp-wait-for-output)
2450 (setq attr (buffer-substring (point)
2451 (progn (end-of-line) (point)))))
2452 (setq tramp-buffer-file-attributes attr))
2453 (when (boundp 'last-coding-system-used)
2454 (set 'last-coding-system-used coding-system-used))
2455 nil)))))
2457 ;; CCC continue here
2459 ;; This function makes the same assumption as
2460 ;; `tramp-handle-set-visited-file-modtime'.
2461 (defun tramp-handle-verify-visited-file-modtime (buf)
2462 "Like `verify-visited-file-modtime' for tramp files.
2463 At the time `verify-visited-file-modtime' calls this function, we
2464 already know that the buffer is visiting a file and that
2465 `visited-file-modtime' does not return 0. Do not call this
2466 function directly, unless those two cases are already taken care
2467 of."
2468 (with-current-buffer buf
2469 ;; There is no file visiting the buffer, or the buffer has no
2470 ;; recorded last modification time.
2471 (if (or (not (buffer-file-name))
2472 (eq (visited-file-modtime) 0))
2474 (let ((f (buffer-file-name)))
2475 (with-parsed-tramp-file-name f nil
2476 (let* ((attr (file-attributes f))
2477 (modtime (nth 5 attr))
2478 (mt (visited-file-modtime)))
2480 (cond
2481 ;; file exists, and has a known modtime.
2482 ((and attr (not (equal modtime '(0 0))))
2483 (< (abs (tramp-time-diff
2484 modtime
2485 ;; For compatibility, deal with both the old
2486 ;; (HIGH . LOW) and the new (HIGH LOW)
2487 ;; return values of `visited-file-modtime'.
2488 (if (atom (cdr mt))
2489 (list (car mt) (cdr mt))
2490 mt)))
2492 ;; modtime has the don't know value.
2493 (attr
2494 (save-excursion
2495 (tramp-send-command
2496 multi-method method user host
2497 (format "%s -ild %s"
2498 (tramp-get-ls-command multi-method method user host)
2499 (tramp-shell-quote-argument localname)))
2500 (tramp-wait-for-output)
2501 (setq attr (buffer-substring
2502 (point) (progn (end-of-line) (point)))))
2503 (equal tramp-buffer-file-attributes attr))
2504 ;; If file does not exist, say it is not modified
2505 ;; if and only if that agrees with the buffer's record.
2506 (t (equal mt '(-1 65535))))))))))
2508 (defun tramp-handle-set-file-modes (filename mode)
2509 "Like `set-file-modes' for tramp files."
2510 (with-parsed-tramp-file-name filename nil
2511 (save-excursion
2512 (unless (zerop (tramp-send-command-and-check
2513 multi-method method user host
2514 (format "chmod %s %s"
2515 (tramp-decimal-to-octal mode)
2516 (tramp-shell-quote-argument localname))))
2517 (signal 'file-error
2518 (list "Doing chmod"
2519 ;; FIXME: extract the proper text from chmod's stderr.
2520 "error while changing file's mode"
2521 filename))))))
2523 ;; Simple functions using the `test' command.
2525 (defun tramp-handle-file-executable-p (filename)
2526 "Like `file-executable-p' for tramp files."
2527 (with-parsed-tramp-file-name filename nil
2528 (zerop (tramp-run-test "-x" filename))))
2530 (defun tramp-handle-file-readable-p (filename)
2531 "Like `file-readable-p' for tramp files."
2532 (with-parsed-tramp-file-name filename nil
2533 (zerop (tramp-run-test "-r" filename))))
2535 (defun tramp-handle-file-accessible-directory-p (filename)
2536 "Like `file-accessible-directory-p' for tramp files."
2537 (with-parsed-tramp-file-name filename nil
2538 (and (zerop (tramp-run-test "-d" filename))
2539 (zerop (tramp-run-test "-r" filename))
2540 (zerop (tramp-run-test "-x" filename)))))
2542 ;; When the remote shell is started, it looks for a shell which groks
2543 ;; tilde expansion. Here, we assume that all shells which grok tilde
2544 ;; expansion will also provide a `test' command which groks `-nt' (for
2545 ;; newer than). If this breaks, tell me about it and I'll try to do
2546 ;; something smarter about it.
2547 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2548 "Like `file-newer-than-file-p' for tramp files."
2549 (cond ((not (file-exists-p file1))
2550 nil)
2551 ((not (file-exists-p file2))
2553 ;; We are sure both files exist at this point.
2555 (save-excursion
2556 ;; We try to get the mtime of both files. If they are not
2557 ;; equal to the "dont-know" value, then we subtract the times
2558 ;; and obtain the result.
2559 (let ((fa1 (file-attributes file1))
2560 (fa2 (file-attributes file2)))
2561 (if (and (not (equal (nth 5 fa1) '(0 0)))
2562 (not (equal (nth 5 fa2) '(0 0))))
2563 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2564 ;; If one of them is the dont-know value, then we can
2565 ;; still try to run a shell command on the remote host.
2566 ;; However, this only works if both files are Tramp
2567 ;; files and both have the same method, same user, same
2568 ;; host.
2569 (unless (and (tramp-tramp-file-p file1)
2570 (tramp-tramp-file-p file2))
2571 (signal
2572 'file-error
2573 (list
2574 "Cannot check if Tramp file is newer than non-Tramp file"
2575 file1 file2)))
2576 (with-parsed-tramp-file-name file1 v1
2577 (with-parsed-tramp-file-name file2 v2
2578 (unless (and (equal v1-multi-method v2-multi-method)
2579 (equal v1-method v2-method)
2580 (equal v1-user v2-user)
2581 (equal v1-host v2-host))
2582 (signal 'file-error
2583 (list "Files must have same method, user, host"
2584 file1 file2)))
2585 (unless (and (tramp-tramp-file-p file1)
2586 (tramp-tramp-file-p file2))
2587 (signal 'file-error
2588 (list "Files must be tramp files on same host"
2589 file1 file2)))
2590 (if (tramp-get-test-groks-nt
2591 v1-multi-method v1-method v1-user v1-host)
2592 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2593 (zerop (tramp-run-test2
2594 "tramp_test_nt" file1 file2)))))))))))
2596 ;; Functions implemented using the basic functions above.
2598 (defun tramp-handle-file-modes (filename)
2599 "Like `file-modes' for tramp files."
2600 (with-parsed-tramp-file-name filename nil
2601 (when (file-exists-p filename)
2602 (tramp-mode-string-to-int
2603 (nth 8 (file-attributes filename))))))
2605 (defun tramp-handle-file-directory-p (filename)
2606 "Like `file-directory-p' for tramp files."
2607 ;; Care must be taken that this function returns `t' for symlinks
2608 ;; pointing to directories. Surely the most obvious implementation
2609 ;; would be `test -d', but that returns false for such symlinks.
2610 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2611 ;; I now think he's right. So we could be using `test -d', couldn't
2612 ;; we?
2614 ;; Alternatives: `cd %s', `test -d %s'
2615 (with-parsed-tramp-file-name filename nil
2616 (save-excursion
2617 (zerop
2618 (tramp-send-command-and-check
2619 multi-method method user host
2620 (format "test -d %s"
2621 (tramp-shell-quote-argument localname))
2622 t))))) ;run command in subshell
2624 (defun tramp-handle-file-regular-p (filename)
2625 "Like `file-regular-p' for tramp files."
2626 (with-parsed-tramp-file-name filename nil
2627 (and (file-exists-p filename)
2628 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2630 (defun tramp-handle-file-symlink-p (filename)
2631 "Like `file-symlink-p' for tramp files."
2632 (with-parsed-tramp-file-name filename nil
2633 (let ((x (car (file-attributes filename))))
2634 (when (stringp x)
2635 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2636 ;; might do weird things.
2637 (if (file-name-absolute-p x)
2638 (tramp-make-tramp-file-name
2639 multi-method method user host x)
2640 x)))))
2642 (defun tramp-handle-file-writable-p (filename)
2643 "Like `file-writable-p' for tramp files."
2644 (with-parsed-tramp-file-name filename nil
2645 (if (file-exists-p filename)
2646 ;; Existing files must be writable.
2647 (zerop (tramp-run-test "-w" filename))
2648 ;; If file doesn't exist, check if directory is writable.
2649 (and (zerop (tramp-run-test
2650 "-d" (file-name-directory filename)))
2651 (zerop (tramp-run-test
2652 "-w" (file-name-directory filename)))))))
2654 (defun tramp-handle-file-ownership-preserved-p (filename)
2655 "Like `file-ownership-preserved-p' for tramp files."
2656 (with-parsed-tramp-file-name filename nil
2657 (let ((attributes (file-attributes filename)))
2658 ;; Return t if the file doesn't exist, since it's true that no
2659 ;; information would be lost by an (attempted) delete and create.
2660 (or (null attributes)
2661 (= (nth 2 attributes)
2662 (tramp-get-remote-uid multi-method method user host))))))
2664 ;; Other file name ops.
2666 ;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2667 ;; (defun tramp-handle-directory-file-name (directory)
2668 ;; "Like `directory-file-name' for tramp files."
2669 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2670 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2671 ;; (substring directory 0 (- (length directory) 1))
2672 ;; directory))
2674 ;; ;; Philippe Troin <phil@fifi.org>
2675 ;; (defun tramp-handle-directory-file-name (directory)
2676 ;; "Like `directory-file-name' for tramp files."
2677 ;; (with-parsed-tramp-file-name directory nil
2678 ;; (let ((directory-length-1 (1- (length directory))))
2679 ;; (save-match-data
2680 ;; (if (and (eq (aref directory directory-length-1) ?/)
2681 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2682 ;; (/= (match-end 0) directory-length-1))
2683 ;; (substring directory 0 directory-length-1)
2684 ;; directory)))))
2686 (defun tramp-handle-directory-file-name (directory)
2687 "Like `directory-file-name' for tramp files."
2688 ;; If localname component of filename is "/", leave it unchanged.
2689 ;; Otherwise, remove any trailing slash from localname component.
2690 ;; Method, host, etc, are unchanged. Does it make sense to try
2691 ;; to avoid parsing the filename?
2692 (with-parsed-tramp-file-name directory nil
2693 (if (and (not (zerop (length localname)))
2694 (eq (aref localname (1- (length localname))) ?/)
2695 (not (string= localname "/")))
2696 (substring directory 0 -1)
2697 directory)))
2699 ;; Directory listings.
2701 (defun tramp-handle-directory-files (directory
2702 &optional full match nosort files-only)
2703 "Like `directory-files' for tramp files."
2704 (with-parsed-tramp-file-name directory nil
2705 (let (result x)
2706 (save-excursion
2707 (tramp-barf-unless-okay
2708 multi-method method user host
2709 (concat "cd " (tramp-shell-quote-argument localname))
2711 'file-error
2712 "tramp-handle-directory-files: couldn't `cd %s'"
2713 (tramp-shell-quote-argument localname))
2714 (tramp-send-command
2715 multi-method method user host
2716 (concat (tramp-get-ls-command multi-method method user host)
2717 " -a | cat"))
2718 (tramp-wait-for-output)
2719 (goto-char (point-max))
2720 (while (zerop (forward-line -1))
2721 (setq x (buffer-substring (point)
2722 (tramp-line-end-position)))
2723 (when (or (not match) (string-match match x))
2724 (if full
2725 (push (concat (file-name-as-directory directory)
2727 result)
2728 (push x result))))
2729 (tramp-send-command multi-method method user host "cd")
2730 (tramp-wait-for-output)
2731 ;; Remove non-files or non-directories if necessary. Using
2732 ;; the remote shell for this would probably be way faster.
2733 ;; Maybe something could be adapted from
2734 ;; tramp-handle-file-name-all-completions.
2735 (when files-only
2736 (let ((temp (nreverse result))
2737 item)
2738 (setq result nil)
2739 (if (equal files-only t)
2740 ;; files only
2741 (while temp
2742 (setq item (pop temp))
2743 (when (file-regular-p item)
2744 (push item result)))
2745 ;; directories only
2746 (while temp
2747 (setq item (pop temp))
2748 (when (file-directory-p item)
2749 (push item result)))))))
2750 result)))
2752 (defun tramp-handle-directory-files-and-attributes
2753 (directory &optional full match nosort id-format)
2754 "Like `directory-files-and-attributes' for tramp files."
2755 (when (tramp-handle-file-exists-p directory)
2756 (save-excursion
2757 (setq directory (tramp-handle-expand-file-name directory))
2758 (with-parsed-tramp-file-name directory nil
2759 (tramp-maybe-send-perl-script multi-method method user host
2760 tramp-perl-directory-files-and-attributes
2761 "tramp_directory_files_and_attributes")
2762 (tramp-send-command multi-method method user host
2763 (format "tramp_directory_files_and_attributes %s %s"
2764 (tramp-shell-quote-argument localname)
2765 (or id-format 'integer)))
2766 (tramp-wait-for-output)
2767 (let* ((root (cons nil (let ((object (read (current-buffer))))
2768 (when (stringp object)
2769 (error object))
2770 object)))
2771 (cell root))
2772 (while (cdr cell)
2773 (if (and match (not (string-match match (caadr cell))))
2774 ;; Remove from list
2775 (setcdr cell (cddr cell))
2776 ;; Include in list
2777 (setq cell (cdr cell))
2778 (let ((l (car cell)))
2779 (tramp-convert-file-attributes multi-method method user host
2780 (cdr l))
2781 ;; If FULL, make file name absolute
2782 (when full (setcar l (concat directory "/" (car l)))))))
2783 (if nosort
2784 (cdr root)
2785 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2787 ;; This function should return "foo/" for directories and "bar" for
2788 ;; files. We use `ls -ad' to get a list of files (including
2789 ;; directories), and `find . -type d \! -name . -prune' to get a list
2790 ;; of directories.
2791 (defun tramp-handle-file-name-all-completions (filename directory)
2792 "Like `file-name-all-completions' for tramp files."
2793 (with-parsed-tramp-file-name directory nil
2794 (unless (save-match-data (string-match "/" filename))
2795 (let* ((nowild tramp-completion-without-shell-p)
2796 result)
2797 (save-excursion
2798 (tramp-barf-unless-okay
2799 multi-method method user host
2800 (format "cd %s" (tramp-shell-quote-argument localname))
2801 nil 'file-error
2802 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2803 (tramp-shell-quote-argument localname))
2805 ;; Get a list of directories and files, including reliably
2806 ;; tagging the directories with a trailing '/'. Because I
2807 ;; rock. --daniel@danann.net
2808 (tramp-send-command
2809 multi-method method user host
2810 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2811 "if test -d \"$f\" 2>/dev/null; "
2812 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2813 (tramp-get-ls-command multi-method method user host)
2814 (if (or nowild (zerop (length filename)))
2816 (format "-d %s*"
2817 (tramp-shell-quote-argument filename)))))
2819 ;; Now grab the output.
2820 (tramp-wait-for-output)
2821 (goto-char (point-max))
2822 (while (zerop (forward-line -1))
2823 (push (buffer-substring (point)
2824 (tramp-line-end-position))
2825 result))
2827 (tramp-send-command multi-method method user host "cd")
2828 (tramp-wait-for-output)
2830 ;; Return the list.
2831 (if nowild
2832 (all-completions filename (mapcar 'list result))
2833 result))))))
2836 ;; The following isn't needed for Emacs 20 but for 19.34?
2837 (defun tramp-handle-file-name-completion (filename directory)
2838 "Like `file-name-completion' for tramp files."
2839 (unless (tramp-tramp-file-p directory)
2840 (error
2841 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2842 directory))
2843 (with-parsed-tramp-file-name directory nil
2844 (try-completion
2845 filename
2846 (mapcar (lambda (x) (cons x nil))
2847 (file-name-all-completions filename directory)))))
2849 ;; cp, mv and ln
2851 (defun tramp-handle-add-name-to-file
2852 (filename newname &optional ok-if-already-exists)
2853 "Like `add-name-to-file' for tramp files."
2854 (with-parsed-tramp-file-name filename v1
2855 (with-parsed-tramp-file-name newname v2
2856 (let ((ln (when v1 (tramp-get-remote-ln
2857 v1-multi-method v1-method v1-user v1-host))))
2858 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2859 (equal v1-multi-method v2-multi-method)
2860 (equal v1-method v2-method)
2861 (equal v1-user v2-user)
2862 (equal v1-host v2-host))
2863 (error "add-name-to-file: %s"
2864 "only implemented for same method, same user, same host"))
2865 (when (and (not ok-if-already-exists)
2866 (file-exists-p newname)
2867 (not (numberp ok-if-already-exists))
2868 (y-or-n-p
2869 (format
2870 "File %s already exists; make it a new name anyway? "
2871 newname)))
2872 (error "add-name-to-file: file %s already exists" newname))
2873 (tramp-barf-unless-okay
2874 v1-multi-method v1-method v1-user v1-host
2875 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2876 (tramp-shell-quote-argument v2-localname))
2877 nil 'file-error
2878 "error with add-name-to-file, see buffer `%s' for details"
2879 (buffer-name))))))
2881 (defun tramp-handle-copy-file
2882 (filename newname &optional ok-if-already-exists keep-date)
2883 "Like `copy-file' for tramp files."
2884 ;; Check if both files are local -- invoke normal copy-file.
2885 ;; Otherwise, use tramp from local system.
2886 (setq filename (expand-file-name filename))
2887 (setq newname (expand-file-name newname))
2888 ;; At least one file a tramp file?
2889 (if (or (tramp-tramp-file-p filename)
2890 (tramp-tramp-file-p newname))
2891 (tramp-do-copy-or-rename-file
2892 'copy filename newname ok-if-already-exists keep-date)
2893 (tramp-run-real-handler
2894 'copy-file
2895 (list filename newname ok-if-already-exists keep-date))))
2897 (defun tramp-handle-rename-file
2898 (filename newname &optional ok-if-already-exists)
2899 "Like `rename-file' for tramp files."
2900 ;; Check if both files are local -- invoke normal rename-file.
2901 ;; Otherwise, use tramp from local system.
2902 (setq filename (expand-file-name filename))
2903 (setq newname (expand-file-name newname))
2904 ;; At least one file a tramp file?
2905 (if (or (tramp-tramp-file-p filename)
2906 (tramp-tramp-file-p newname))
2907 (tramp-do-copy-or-rename-file
2908 'rename filename newname ok-if-already-exists)
2909 (tramp-run-real-handler 'rename-file
2910 (list filename newname ok-if-already-exists))))
2912 (defun tramp-do-copy-or-rename-file
2913 (op filename newname &optional ok-if-already-exists keep-date)
2914 "Copy or rename a remote file.
2915 OP must be `copy' or `rename' and indicates the operation to perform.
2916 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2917 the new file (for copy) or the new name of the file (for rename).
2918 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2919 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2920 as FILENAME.
2922 This function is invoked by `tramp-handle-copy-file' and
2923 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2924 and `rename'. FILENAME and NEWNAME must be absolute file names."
2925 (unless (memq op '(copy rename))
2926 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2927 (unless ok-if-already-exists
2928 (when (file-exists-p newname)
2929 (signal 'file-already-exists
2930 (list newname))))
2931 (let ((t1 (tramp-tramp-file-p filename))
2932 (t2 (tramp-tramp-file-p newname))
2933 v1-multi-method v1-method v1-user v1-host v1-localname
2934 v2-multi-method v2-method v2-user v2-host v2-localname)
2936 ;; Check which ones of source and target are Tramp files.
2937 ;; We cannot invoke `with-parsed-tramp-file-name';
2938 ;; it fails if the file isn't a Tramp file name.
2939 (if t1
2940 (with-parsed-tramp-file-name filename l
2941 (setq v1-multi-method l-multi-method
2942 v1-method l-method
2943 v1-user l-user
2944 v1-host l-host
2945 v1-localname l-localname))
2946 (setq v1-localname filename))
2947 (if t2
2948 (with-parsed-tramp-file-name newname l
2949 (setq v2-multi-method l-multi-method
2950 v2-method l-method
2951 v2-user l-user
2952 v2-host l-host
2953 v2-localname l-localname))
2954 (setq v2-localname newname))
2956 (cond
2957 ;; Both are Tramp files.
2958 ((and t1 t2)
2959 (cond
2960 ;; Shortcut: if method, host, user are the same for both
2961 ;; files, we invoke `cp' or `mv' on the remote host
2962 ;; directly.
2963 ((and (equal v1-multi-method v2-multi-method)
2964 (equal v1-method v2-method)
2965 (equal v1-user v2-user)
2966 (equal v1-host v2-host))
2967 (tramp-do-copy-or-rename-file-directly
2968 op v1-multi-method v1-method v1-user v1-host
2969 v1-localname v2-localname keep-date))
2970 ;; If both source and target are Tramp files,
2971 ;; both are using the same copy-program, then we
2972 ;; can invoke rcp directly. Note that
2973 ;; default-directory should point to a local
2974 ;; directory if we want to invoke rcp.
2975 ((and (not v1-multi-method)
2976 (not v2-multi-method)
2977 (equal v1-method v2-method)
2978 (tramp-method-out-of-band-p
2979 v1-multi-method v1-method v1-user v1-host)
2980 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2981 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2982 (tramp-do-copy-or-rename-file-out-of-band
2983 op filename newname keep-date))
2984 ;; No shortcut was possible. So we copy the
2985 ;; file first. If the operation was `rename', we go
2986 ;; back and delete the original file (if the copy was
2987 ;; successful). The approach is simple-minded: we
2988 ;; create a new buffer, insert the contents of the
2989 ;; source file into it, then write out the buffer to
2990 ;; the target file. The advantage is that it doesn't
2991 ;; matter which filename handlers are used for the
2992 ;; source and target file.
2994 (tramp-do-copy-or-rename-file-via-buffer
2995 op filename newname keep-date))))
2997 ;; One file is a Tramp file, the other one is local.
2998 ((or t1 t2)
2999 ;; If the Tramp file has an out-of-band method, the corresponding
3000 ;; copy-program can be invoked.
3001 (if (and (not v1-multi-method)
3002 (not v2-multi-method)
3003 (or (tramp-method-out-of-band-p
3004 v1-multi-method v1-method v1-user v1-host)
3005 (tramp-method-out-of-band-p
3006 v2-multi-method v2-method v2-user v2-host)))
3007 (tramp-do-copy-or-rename-file-out-of-band
3008 op filename newname keep-date)
3009 ;; Use the generic method via a Tramp buffer.
3010 (tramp-do-copy-or-rename-file-via-buffer
3011 op filename newname keep-date)))
3014 ;; One of them must be a Tramp file.
3015 (error "Tramp implementation says this cannot happen")))))
3017 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3018 "Use an Emacs buffer to copy or rename a file.
3019 First arg OP is either `copy' or `rename' and indicates the operation.
3020 FILENAME is the source file, NEWNAME the target file.
3021 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3022 (let ((trampbuf (get-buffer-create "*tramp output*"))
3023 (modtime (nth 5 (file-attributes filename))))
3024 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
3025 (tramp-message
3026 1 (concat "Warning: cannot preserve file time stamp"
3027 " with inline copying across machines")))
3028 (save-excursion
3029 (set-buffer trampbuf) (erase-buffer)
3030 (insert-file-contents-literally filename)
3031 ;; We don't want the target file to be compressed, so we let-bind
3032 ;; `jka-compr-inhibit' to t.
3033 (let ((coding-system-for-write 'binary)
3034 (jka-compr-inhibit t))
3035 (write-region (point-min) (point-max) newname))
3036 ;; KEEP-DATE handling.
3037 (when keep-date
3038 (when (and (not (null modtime))
3039 (not (equal modtime '(0 0))))
3040 (tramp-touch newname modtime)))
3041 ;; Set the mode.
3042 (set-file-modes newname (file-modes filename)))
3043 ;; If the operation was `rename', delete the original file.
3044 (unless (eq op 'copy)
3045 (delete-file filename))))
3047 (defun tramp-do-copy-or-rename-file-directly
3048 (op multi-method method user host localname1 localname2 keep-date)
3049 "Invokes `cp' or `mv' on the remote system.
3050 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3051 respectively. METHOD, USER, and HOST specify the connection.
3052 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3053 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3054 ;; CCC: What happens to the timestamp when renaming?
3055 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3056 ((eq op 'copy) "cp -f")
3057 ((eq op 'rename) "mv -f")
3058 (t (error
3059 "Unknown operation `%s', must be `copy' or `rename'"
3060 op)))))
3061 (save-excursion
3062 (tramp-send-command
3063 multi-method method user host
3064 (format "%s %s %s"
3066 (tramp-shell-quote-argument localname1)
3067 (tramp-shell-quote-argument localname2)))
3068 (tramp-wait-for-output)
3069 (goto-char (point-min))
3070 (unless
3072 (and (eq op 'copy) keep-date
3073 ;; Mask cp -f error.
3074 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3075 (zerop (tramp-send-command-and-check
3076 multi-method method user host nil nil)))
3077 (pop-to-buffer (current-buffer))
3078 (signal 'file-error
3079 (format "Copying directly failed, see buffer `%s' for details."
3080 (buffer-name)))))
3081 ;; Set the mode.
3082 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3083 ;; where available?
3084 (unless (or (eq op 'rename) keep-date)
3085 (set-file-modes
3086 (tramp-make-tramp-file-name multi-method method user host localname2)
3087 (file-modes
3088 (tramp-make-tramp-file-name
3089 multi-method method user host localname1))))))
3091 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3092 "Invoke rcp program to copy.
3093 One of FILENAME and NEWNAME must be a Tramp name, the other must
3094 be a local filename. The method used must be an out-of-band method."
3095 (let ((t1 (tramp-tramp-file-p filename))
3096 (t2 (tramp-tramp-file-p newname))
3097 v1-multi-method v1-method v1-user v1-host v1-localname
3098 v2-multi-method v2-method v2-user v2-host v2-localname
3099 multi-method method user host copy-program copy-args
3100 source target trampbuf)
3102 ;; Check which ones of source and target are Tramp files.
3103 ;; We cannot invoke `with-parsed-tramp-file-name';
3104 ;; it fails if the file isn't a Tramp file name.
3105 (if t1
3106 (with-parsed-tramp-file-name filename l
3107 (setq v1-multi-method l-multi-method
3108 v1-method l-method
3109 v1-user l-user
3110 v1-host l-host
3111 v1-localname l-localname
3112 multi-method l-multi-method
3113 method (tramp-find-method
3114 v1-multi-method v1-method v1-user v1-host)
3115 user l-user
3116 host l-host
3117 copy-program (tramp-get-method-parameter
3118 v1-multi-method method
3119 v1-user v1-host 'tramp-copy-program)
3120 copy-args (tramp-get-method-parameter
3121 v1-multi-method method
3122 v1-user v1-host 'tramp-copy-args)))
3123 (setq v1-localname filename))
3125 (if t2
3126 (with-parsed-tramp-file-name newname l
3127 (setq v2-multi-method l-multi-method
3128 v2-method l-method
3129 v2-user l-user
3130 v2-host l-host
3131 v2-localname l-localname
3132 multi-method l-multi-method
3133 method (tramp-find-method
3134 v2-multi-method v2-method v2-user v2-host)
3135 user l-user
3136 host l-host
3137 copy-program (tramp-get-method-parameter
3138 v2-multi-method method
3139 v2-user v2-host 'tramp-copy-program)
3140 copy-args (tramp-get-method-parameter
3141 v2-multi-method method
3142 v2-user v2-host 'tramp-copy-args)))
3143 (setq v2-localname newname))
3145 ;; The following should be changed. We need a more general
3146 ;; mechanism to parse extra host args.
3147 (if (not t1)
3148 (setq source v1-localname)
3149 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3150 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3151 (setq v1-host (match-string 1 v1-host)))
3152 (setq source
3153 (tramp-make-copy-program-file-name
3154 v1-user v1-host
3155 (tramp-shell-quote-argument v1-localname))))
3157 (if (not t2)
3158 (setq target v2-localname)
3159 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3160 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3161 (setq v2-host (match-string 1 v2-host)))
3162 (setq target
3163 (tramp-make-copy-program-file-name
3164 v2-user v2-host
3165 (tramp-shell-quote-argument v2-localname))))
3167 ;; Handle keep-date argument
3168 (when keep-date
3169 (if t1
3170 (setq copy-args
3171 (cons (tramp-get-method-parameter
3172 v1-multi-method method
3173 v1-user v1-host 'tramp-copy-keep-date-arg)
3174 copy-args))
3175 (setq copy-args
3176 (cons (tramp-get-method-parameter
3177 v2-multi-method method
3178 v2-user v2-host 'tramp-copy-keep-date-arg)
3179 copy-args))))
3181 (setq copy-args (append copy-args (list source target))
3182 trampbuf (generate-new-buffer
3183 (tramp-buffer-name multi-method method user host)))
3185 ;; Use an asynchronous process. By this, password can be handled.
3186 (save-excursion
3188 ;; Check for program.
3189 (when (and (fboundp 'executable-find)
3190 (not (executable-find copy-program)))
3191 (error "Cannot find copy program: %s" copy-program))
3193 (set-buffer trampbuf)
3194 (setq tramp-current-multi-method multi-method
3195 tramp-current-method method
3196 tramp-current-user user
3197 tramp-current-host host)
3198 (message "Transferring %s to %s..." filename newname)
3200 ;; Use rcp-like program for file transfer.
3201 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3202 copy-program copy-args)))
3203 (tramp-set-process-query-on-exit-flag p nil)
3204 (tramp-process-actions p multi-method method user host
3205 tramp-actions-copy-out-of-band))
3206 (kill-buffer trampbuf)
3207 (message "Transferring %s to %s...done" filename newname)
3209 ;; Set the mode.
3210 (unless keep-date
3211 (set-file-modes newname (file-modes filename))))
3213 ;; If the operation was `rename', delete the original file.
3214 (unless (eq op 'copy)
3215 (delete-file filename))))
3217 ;; mkdir
3218 (defun tramp-handle-make-directory (dir &optional parents)
3219 "Like `make-directory' for tramp files."
3220 (setq dir (expand-file-name dir))
3221 (with-parsed-tramp-file-name dir nil
3222 (save-excursion
3223 (tramp-barf-unless-okay
3224 multi-method method user host
3225 (format " %s %s"
3226 (if parents "mkdir -p" "mkdir")
3227 (tramp-shell-quote-argument localname))
3228 nil 'file-error
3229 "Couldn't make directory %s" dir))))
3231 ;; CCC error checking?
3232 (defun tramp-handle-delete-directory (directory)
3233 "Like `delete-directory' for tramp files."
3234 (setq directory (expand-file-name directory))
3235 (with-parsed-tramp-file-name directory nil
3236 (save-excursion
3237 (tramp-send-command
3238 multi-method method user host
3239 (format "rmdir %s ; echo ok"
3240 (tramp-shell-quote-argument localname)))
3241 (tramp-wait-for-output))))
3243 (defun tramp-handle-delete-file (filename)
3244 "Like `delete-file' for tramp files."
3245 (setq filename (expand-file-name filename))
3246 (with-parsed-tramp-file-name filename nil
3247 (save-excursion
3248 (unless (zerop (tramp-send-command-and-check
3249 multi-method method user host
3250 (format "rm -f %s"
3251 (tramp-shell-quote-argument localname))))
3252 (signal 'file-error "Couldn't delete Tramp file")))))
3254 ;; Dired.
3256 ;; CCC: This does not seem to be enough. Something dies when
3257 ;; we try and delete two directories under TRAMP :/
3258 (defun tramp-handle-dired-recursive-delete-directory (filename)
3259 "Recursively delete the directory given.
3260 This is like `dired-recursive-delete-directory' for tramp files."
3261 (with-parsed-tramp-file-name filename nil
3262 ;; run a shell command 'rm -r <localname>'
3263 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3264 (or (file-exists-p filename)
3265 (signal
3266 'file-error
3267 (list "Removing old file name" "no such directory" filename)))
3268 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3269 (tramp-send-command multi-method method user host
3270 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3271 ;; Wait for the remote system to return to us...
3272 ;; This might take a while, allow it plenty of time.
3273 (tramp-wait-for-output 120)
3274 ;; Make sure that it worked...
3275 (and (file-exists-p filename)
3276 (error "Failed to recursively delete %s" filename))))
3278 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3279 "Like `dired-call-process' for tramp files."
3280 (with-parsed-tramp-file-name default-directory nil
3281 (save-excursion
3282 (tramp-barf-unless-okay
3283 multi-method method user host
3284 (format "cd %s" (tramp-shell-quote-argument localname))
3285 nil 'file-error
3286 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3287 (tramp-shell-quote-argument localname))
3288 (tramp-send-command
3289 multi-method method user host
3290 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3291 (tramp-wait-for-output))
3292 (unless discard
3293 ;; We cannot use `insert-buffer' because the tramp buffer
3294 ;; changes its contents before insertion due to calling
3295 ;; `expand-file' and alike.
3296 (insert
3297 (with-current-buffer
3298 (tramp-get-buffer multi-method method user host)
3299 (buffer-string))))
3300 (save-excursion
3301 (prog1
3302 (tramp-send-command-and-check multi-method method user host nil)
3303 (tramp-send-command multi-method method user host "cd")
3304 (tramp-wait-for-output)))))
3306 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3307 "Like `dired-compress-file' for tramp files."
3308 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3309 ;; Code stolen mainly from dired-aux.el.
3310 (with-parsed-tramp-file-name file nil
3311 (save-excursion
3312 (let ((suffixes
3313 (if (not (featurep 'xemacs))
3314 ;; Emacs case
3315 (symbol-value 'dired-compress-file-suffixes)
3316 ;; XEmacs has `dired-compression-method-alist', which is
3317 ;; transformed into `dired-compress-file-suffixes' structure.
3318 (mapcar
3319 '(lambda (x)
3320 (list (concat (regexp-quote (nth 1 x)) "\\'")
3322 (mapconcat 'identity (nth 3 x) " ")))
3323 (symbol-value 'dired-compression-method-alist))))
3324 suffix)
3325 ;; See if any suffix rule matches this file name.
3326 (while suffixes
3327 (let (case-fold-search)
3328 (if (string-match (car (car suffixes)) localname)
3329 (setq suffix (car suffixes) suffixes nil))
3330 (setq suffixes (cdr suffixes))))
3332 (cond ((file-symlink-p file)
3333 nil)
3334 ((and suffix (nth 2 suffix))
3335 ;; We found an uncompression rule.
3336 (message "Uncompressing %s..." file)
3337 (when (zerop (tramp-send-command-and-check
3338 multi-method method user host
3339 (concat (nth 2 suffix) " " localname)))
3340 (message "Uncompressing %s...done" file)
3341 ;; `dired-remove-file' is not defined in XEmacs
3342 (funcall (symbol-function 'dired-remove-file) file)
3343 (string-match (car suffix) file)
3344 (concat (substring file 0 (match-beginning 0)))))
3346 ;; We don't recognize the file as compressed, so compress it.
3347 ;; Try gzip.
3348 (message "Compressing %s..." file)
3349 (when (zerop (tramp-send-command-and-check
3350 multi-method method user host
3351 (concat "gzip -f " localname)))
3352 (message "Compressing %s...done" file)
3353 ;; `dired-remove-file' is not defined in XEmacs
3354 (funcall (symbol-function 'dired-remove-file) file)
3355 (cond ((file-exists-p (concat file ".gz"))
3356 (concat file ".gz"))
3357 ((file-exists-p (concat file ".z"))
3358 (concat file ".z"))
3359 (t nil)))))))))
3361 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3362 ;; not sure at all that this is the right way to do it, but let's hope
3363 ;; it works for now, and wait for a guru to point out the Right Way to
3364 ;; achieve this.
3365 ;;(eval-when-compile
3366 ;; (unless (fboundp 'dired-insert-set-properties)
3367 ;; (fset 'dired-insert-set-properties 'ignore)))
3368 ;; Gerd suggests this:
3369 (eval-when-compile (require 'dired))
3370 ;; Note that dired is required at run-time, too, when it is needed.
3371 ;; It is only needed on XEmacs for the function
3372 ;; `dired-insert-set-properties'.
3374 (defun tramp-handle-insert-directory
3375 (filename switches &optional wildcard full-directory-p)
3376 "Like `insert-directory' for tramp files."
3377 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3378 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3379 (tramp-run-real-handler 'insert-directory
3380 (list filename switches wildcard full-directory-p))
3381 ;; For the moment, we assume that the remote "ls" program does not
3382 ;; grok "--dired". In the future, we should detect this on
3383 ;; connection setup.
3384 (when (string-match "^--dired\\s-+" switches)
3385 (setq switches (replace-match "" nil t switches)))
3386 (setq filename (expand-file-name filename))
3387 (with-parsed-tramp-file-name filename nil
3388 (tramp-message-for-buffer
3389 multi-method method user host 10
3390 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3391 switches filename (if wildcard "yes" "no")
3392 (if full-directory-p "yes" "no"))
3393 (when wildcard
3394 (setq wildcard (file-name-nondirectory localname))
3395 (setq localname (file-name-directory localname)))
3396 (when (listp switches)
3397 (setq switches (mapconcat 'identity switches " ")))
3398 (unless full-directory-p
3399 (setq switches (concat "-d " switches)))
3400 (when wildcard
3401 (setq switches (concat switches " " wildcard)))
3402 (save-excursion
3403 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3404 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3405 (if full-directory-p
3406 (tramp-send-command
3407 multi-method method user host
3408 (format "%s %s %s"
3409 (tramp-get-ls-command multi-method method user host)
3410 switches
3411 (if wildcard
3412 localname
3413 (tramp-shell-quote-argument (concat localname ".")))))
3414 (tramp-barf-unless-okay
3415 multi-method method user host
3416 (format "cd %s" (tramp-shell-quote-argument
3417 (file-name-directory localname)))
3418 nil 'file-error
3419 "Couldn't `cd %s'"
3420 (tramp-shell-quote-argument (file-name-directory localname)))
3421 (tramp-send-command
3422 multi-method method user host
3423 (format "%s %s %s"
3424 (tramp-get-ls-command multi-method method user host)
3425 switches
3426 (if wildcard
3427 localname
3428 (if (zerop (length (file-name-nondirectory localname)))
3430 (tramp-shell-quote-argument
3431 (file-name-nondirectory localname)))))))
3432 (sit-for 1) ;needed for rsh but not ssh?
3433 (tramp-wait-for-output))
3434 ;; The following let-binding is used by code that's commented
3435 ;; out. Let's leave the let-binding in for a while to see
3436 ;; that the commented-out code is really not needed. Commenting-out
3437 ;; happened on 2003-03-13.
3438 (let ((old-pos (point)))
3439 ;; We cannot use `insert-buffer' because the tramp buffer
3440 ;; changes its contents before insertion due to calling
3441 ;; `expand-file' and alike.
3442 (insert
3443 (with-current-buffer
3444 (tramp-get-buffer multi-method method user host)
3445 (buffer-string)))
3446 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3447 ;; that doesn't exist on Emacs, so we use this workaround instead.
3448 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3449 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3450 ;; (let ((zmacs-region-stays t))
3451 ;; (exchange-point-and-mark))
3452 (save-excursion
3453 (tramp-send-command multi-method method user host "cd")
3454 (tramp-wait-for-output))
3455 ;; For the time being, the XEmacs kludge is commented out.
3456 ;; Please test it on various XEmacs versions to see if it works.
3457 ;; ;; Another XEmacs specialty follows. What's the right way to do
3458 ;; ;; it?
3459 ;; (when (and (featurep 'xemacs)
3460 ;; (eq major-mode 'dired-mode))
3461 ;; (save-excursion
3462 ;; (require 'dired)
3463 ;; (dired-insert-set-properties old-pos (point))))
3464 ))))
3466 ;; Continuation of kluge to pacify byte-compiler.
3467 ;;(eval-when-compile
3468 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3469 ;; (fmakunbound 'dired-insert-set-properties)))
3471 ;; CCC is this the right thing to do?
3472 (defun tramp-handle-unhandled-file-name-directory (filename)
3473 "Like `unhandled-file-name-directory' for tramp files."
3474 (with-parsed-tramp-file-name filename nil
3475 (expand-file-name "~/")))
3477 ;; Canonicalization of file names.
3479 (defun tramp-drop-volume-letter (name)
3480 "Cut off unnecessary drive letter from file NAME.
3481 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3482 locally on a remote file name. When the local system is a W32 system
3483 but the remote system is Unix, this introduces a superfluous drive
3484 letter into the file name. This function removes it.
3486 Doesn't do anything if the NAME does not start with a drive letter."
3487 (if (and (> (length name) 1)
3488 (char-equal (aref name 1) ?:)
3489 (let ((c1 (aref name 0)))
3490 (or (and (>= c1 ?A) (<= c1 ?Z))
3491 (and (>= c1 ?a) (<= c1 ?z)))))
3492 (substring name 2)
3493 name))
3495 (defun tramp-handle-expand-file-name (name &optional dir)
3496 "Like `expand-file-name' for tramp files.
3497 If the localname part of the given filename starts with \"/../\" then
3498 the result will be a local, non-Tramp, filename."
3499 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3500 (setq dir (or dir default-directory "/"))
3501 ;; Unless NAME is absolute, concat DIR and NAME.
3502 (unless (file-name-absolute-p name)
3503 (setq name (concat (file-name-as-directory dir) name)))
3504 ;; If NAME is not a tramp file, run the real handler
3505 (if (not (tramp-tramp-file-p name))
3506 (tramp-run-real-handler 'expand-file-name
3507 (list name nil))
3508 ;; Dissect NAME.
3509 (with-parsed-tramp-file-name name nil
3510 (unless (file-name-absolute-p localname)
3511 (setq localname (concat "~/" localname)))
3512 (save-excursion
3513 ;; Tilde expansion if necessary. This needs a shell which
3514 ;; groks tilde expansion! The function `tramp-find-shell' is
3515 ;; supposed to find such a shell on the remote host. Please
3516 ;; tell me about it when this doesn't work on your system.
3517 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3518 (let ((uname (match-string 1 localname))
3519 (fname (match-string 2 localname)))
3520 ;; We cannot simply apply "~/", because under sudo "~/" is
3521 ;; expanded to the local user home directory but to the
3522 ;; root home directory. On the other hand, using always
3523 ;; the default user name for tilde expansion is not
3524 ;; appropriate either, because ssh and companions might
3525 ;; use a user name from the config file.
3526 (when (and (string-equal uname "~")
3527 (string-match
3528 "\\`su\\(do\\)?\\'"
3529 (tramp-find-method multi-method method user host)))
3530 (setq uname (concat uname (or user "root"))))
3531 ;; CCC fanatic error checking?
3532 (set-buffer (tramp-get-buffer multi-method method user host))
3533 (erase-buffer)
3534 (tramp-send-command
3535 multi-method method user host
3536 (format "cd %s; pwd" uname)
3538 (tramp-wait-for-output)
3539 (goto-char (point-min))
3540 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3541 (setq localname (concat uname fname))
3542 (erase-buffer)))
3543 ;; There might be a double slash, for example when "~/"
3544 ;; expands to "/". Remove this.
3545 (while (string-match "//" localname)
3546 (setq localname (replace-match "/" t t localname)))
3547 ;; No tilde characters in file name, do normal
3548 ;; expand-file-name (this does "/./" and "/../"). We bind
3549 ;; directory-sep-char here for XEmacs on Windows, which would
3550 ;; otherwise use backslash. `default-directory' is bound to
3551 ;; "/", because on Windows there would be problems with UNC
3552 ;; shares or Cygwin mounts.
3553 (tramp-let-maybe directory-sep-char ?/
3554 (let ((default-directory "/"))
3555 (tramp-make-tramp-file-name
3556 multi-method (or method (tramp-find-default-method user host))
3557 user host
3558 (tramp-drop-volume-letter
3559 (tramp-run-real-handler 'expand-file-name
3560 (list localname))))))))))
3562 ;; old version follows. it uses ".." to cross file handler
3563 ;; boundaries.
3564 ;; ;; Look if localname starts with "/../" construct. If this is
3565 ;; ;; the case, then we return a local name instead of a remote name.
3566 ;; (if (string-match "^/\\.\\./" localname)
3567 ;; (expand-file-name (substring localname 3))
3568 ;; ;; No tilde characters in file name, do normal
3569 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3570 ;; ;; directory-sep-char here for XEmacs on Windows, which
3571 ;; ;; would otherwise use backslash.
3572 ;; (let ((directory-sep-char ?/))
3573 ;; (tramp-make-tramp-file-name
3574 ;; multi-method method user host
3575 ;; (tramp-drop-volume-letter
3576 ;; (tramp-run-real-handler 'expand-file-name
3577 ;; (list localname))))))))))
3579 ;; Remote commands.
3581 (defvar tramp-async-proc nil
3582 "Global variable keeping asynchronous process object.
3583 Used in `tramp-handle-shell-command'")
3585 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3586 "Like `shell-command' for tramp files.
3587 This will break if COMMAND prints a newline, followed by the value of
3588 `tramp-end-of-output', followed by another newline."
3589 ;; Asynchronous processes are far from being perfect. But it works at least
3590 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3591 (if (tramp-tramp-file-p default-directory)
3592 (with-parsed-tramp-file-name default-directory nil
3593 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3594 status)
3595 (unless output-buffer
3596 (setq output-buffer
3597 (get-buffer-create
3598 (if asynchronous
3599 "*Async Shell Command*"
3600 "*Shell Command Output*")))
3601 (set-buffer output-buffer)
3602 (erase-buffer))
3603 (unless (bufferp output-buffer)
3604 (setq output-buffer (current-buffer)))
3605 (set-buffer output-buffer)
3606 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3607 ;; process. Instead of, another asynchronous process is opened
3608 ;; which gets the output of the (synchronous) Tramp process
3609 ;; via process-filter. ERROR-BUFFER is disabled.
3610 (when asynchronous
3611 (setq command (substring command 0 (match-beginning 0))
3612 error-buffer nil
3613 tramp-async-proc (start-process (buffer-name output-buffer)
3614 output-buffer "cat")))
3615 (save-excursion
3616 (tramp-barf-unless-okay
3617 multi-method method user host
3618 (format "cd %s" (tramp-shell-quote-argument localname))
3619 nil 'file-error
3620 "tramp-handle-shell-command: Couldn't `cd %s'"
3621 (tramp-shell-quote-argument localname))
3622 ;; Define the process filter
3623 (when asynchronous
3624 (set-process-filter
3625 (get-buffer-process
3626 (tramp-get-buffer multi-method method user host))
3627 '(lambda (process string)
3628 ;; Write the output into the Tramp Process
3629 (save-current-buffer
3630 (set-buffer (process-buffer process))
3631 (goto-char (point-max))
3632 (insert string))
3633 ;; Hand-over output to asynchronous process.
3634 (let ((end
3635 (string-match
3636 (regexp-quote tramp-end-of-output) string)))
3637 (when end
3638 (setq string
3639 (substring string 0 (1- (match-beginning 0)))))
3640 (process-send-string tramp-async-proc string)
3641 (when end
3642 (set-process-filter process nil)
3643 (process-send-eof tramp-async-proc))))))
3644 ;; Send the command
3645 (tramp-send-command
3646 multi-method method user host
3647 (if error-buffer
3648 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3649 command)
3650 (format "%s; tramp_old_status=$?" command)))
3651 (unless asynchronous
3652 (tramp-wait-for-output)))
3653 (unless asynchronous
3654 ;; We cannot use `insert-buffer' because the tramp buffer
3655 ;; changes its contents before insertion due to calling
3656 ;; `expand-file' and alike.
3657 (insert
3658 (with-current-buffer
3659 (tramp-get-buffer multi-method method user host)
3660 (buffer-string))))
3661 (when error-buffer
3662 (save-excursion
3663 (unless (bufferp error-buffer)
3664 (setq error-buffer (get-buffer-create error-buffer)))
3665 (tramp-send-command
3666 multi-method method user host
3667 "cat /tmp/tramp.$$.err")
3668 (tramp-wait-for-output)
3669 (set-buffer error-buffer)
3670 ;; Same comment as above
3671 (insert
3672 (with-current-buffer
3673 (tramp-get-buffer multi-method method user host)
3674 (buffer-string)))
3675 (tramp-send-command-and-check
3676 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3677 (save-excursion
3678 (tramp-send-command multi-method method user host "cd")
3679 (unless asynchronous
3680 (tramp-wait-for-output))
3681 (tramp-send-command
3682 multi-method method user host
3683 (concat "tramp_set_exit_status $tramp_old_status;"
3684 " echo tramp_exit_status $?"))
3685 (unless asynchronous
3686 (tramp-wait-for-output)
3687 (goto-char (point-max))
3688 (unless (search-backward "tramp_exit_status " nil t)
3689 (error "Couldn't find exit status of `%s'" command))
3690 (skip-chars-forward "^ ")
3691 (setq status (read (current-buffer)))))
3692 (unless (zerop (buffer-size))
3693 (display-buffer output-buffer))
3694 status))
3695 ;; The following is only executed if something strange was
3696 ;; happening. Emit a helpful message and do it anyway.
3697 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3698 default-directory)
3699 (tramp-run-real-handler 'shell-command
3700 (list command output-buffer error-buffer))))
3702 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3703 "Like `process-file' for Tramp files."
3704 (when infile (error "Implementation does not handle input from file"))
3705 (when (and (numberp buffer) (zerop buffer))
3706 (error "Implementation does not handle immediate return"))
3707 (when (consp buffer) (error "Implementation does not handle error files"))
3708 (shell-command
3709 (mapconcat 'tramp-shell-quote-argument
3710 (cons program args)
3711 " ")
3712 buffer))
3714 ;; File Editing.
3716 (defsubst tramp-make-temp-file ()
3717 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3718 (expand-file-name tramp-temp-name-prefix
3719 (tramp-temporary-file-directory))))
3721 (defun tramp-handle-file-local-copy (filename)
3722 "Like `file-local-copy' for tramp files."
3723 (with-parsed-tramp-file-name filename nil
3724 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3725 ;; We used to bind the following as late as possible.
3726 ;; loc-enc and loc-dec were bound directly before the if
3727 ;; statement that checks them. But the functions
3728 ;; tramp-get-* might invoke the "are you awake" check in
3729 ;; tramp-maybe-open-connection, which is an unfortunate time
3730 ;; since we rely on the buffer contents at that spot.
3731 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3732 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3733 (loc-enc (tramp-get-local-encoding multi-method method user host))
3734 (loc-dec (tramp-get-local-decoding multi-method method user host))
3735 tmpfil)
3736 (unless (file-exists-p filename)
3737 (error "Cannot make local copy of non-existing file `%s'"
3738 filename))
3739 (setq tmpfil (tramp-make-temp-file))
3741 (cond ((tramp-method-out-of-band-p multi-method method user host)
3742 ;; `copy-file' handles out-of-band methods
3743 (copy-file filename tmpfil t t))
3745 ((and rem-enc rem-dec)
3746 ;; Use inline encoding for file transfer.
3747 (save-excursion
3748 ;; Following line for setting tramp-current-method,
3749 ;; tramp-current-user, tramp-current-host.
3750 (set-buffer tramp-buf)
3751 (tramp-message 5 "Encoding remote file %s..." filename)
3752 (tramp-barf-unless-okay
3753 multi-method method user host
3754 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3755 nil 'file-error
3756 "Encoding remote file failed, see buffer `%s' for details"
3757 tramp-buf)
3758 ;; Remove trailing status code
3759 (goto-char (point-max))
3760 (delete-region (point) (progn (forward-line -1) (point)))
3762 (tramp-message 5 "Decoding remote file %s..." filename)
3764 ;; Here is where loc-enc and loc-dec used to be let-bound.
3765 (if (and (symbolp loc-dec) (fboundp loc-dec))
3766 ;; If local decoding is a function, we call it.
3767 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3768 (set-buffer tmpbuf)
3769 (erase-buffer)
3770 (insert-buffer-substring tramp-buf)
3771 (tramp-message-for-buffer
3772 multi-method method user host
3773 6 "Decoding remote file %s with function %s..."
3774 filename loc-dec)
3775 (set-buffer tmpbuf)
3776 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3777 ;; says that we need to strip tramp_exit_status
3778 ;; line from the output here. Go to point-max,
3779 ;; search backward for tramp_exit_status, delete
3780 ;; between point and point-max if found.
3781 (let ((coding-system-for-write 'binary))
3782 (funcall loc-dec (point-min) (point-max))
3783 (write-region (point-min) (point-max) tmpfil))
3784 (kill-buffer tmpbuf))
3785 ;; If tramp-decoding-function is not defined for this
3786 ;; method, we invoke tramp-decoding-command instead.
3787 (let ((tmpfil2 (tramp-make-temp-file)))
3788 (write-region (point-min) (point-max) tmpfil2)
3789 (tramp-message
3790 6 "Decoding remote file %s with command %s..."
3791 filename loc-dec)
3792 (tramp-call-local-coding-command
3793 loc-dec tmpfil2 tmpfil)
3794 (delete-file tmpfil2)))
3795 (tramp-message-for-buffer
3796 multi-method method user host
3797 5 "Decoding remote file %s...done" filename)
3798 ;; Set proper permissions.
3799 (set-file-modes tmpfil (file-modes filename))))
3801 (t (error "Wrong method specification for `%s'" method)))
3802 tmpfil)))
3804 (defun tramp-handle-file-remote-p (filename)
3805 "Like `file-remote-p' for tramp files."
3806 (when (tramp-tramp-file-p filename)
3807 (with-parsed-tramp-file-name filename nil
3808 (make-tramp-file-name
3809 :multi-method multi-method
3810 :method method
3811 :user user
3812 :host host
3813 :localname ""))))
3815 (defun tramp-handle-insert-file-contents
3816 (filename &optional visit beg end replace)
3817 "Like `insert-file-contents' for tramp files."
3818 (barf-if-buffer-read-only)
3819 (setq filename (expand-file-name filename))
3820 (with-parsed-tramp-file-name filename nil
3821 (if (not (file-exists-p filename))
3822 (progn
3823 (when visit
3824 (setq buffer-file-name filename)
3825 (set-visited-file-modtime)
3826 (set-buffer-modified-p nil))
3827 (signal 'file-error
3828 (format "File `%s' not found on remote host" filename))
3829 (list (expand-file-name filename) 0))
3830 ;; `insert-file-contents-literally' takes care to avoid calling
3831 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3832 ;; propagate that care to the file-local-copy operation.
3833 (let ((local-copy
3834 (let ((inhibit-file-name-operation
3835 (when (eq inhibit-file-name-operation
3836 'insert-file-contents)
3837 'file-local-copy)))
3838 (file-local-copy filename)))
3839 coding-system-used result)
3840 (when visit
3841 (setq buffer-file-name filename)
3842 (set-visited-file-modtime)
3843 (set-buffer-modified-p nil))
3844 (tramp-message-for-buffer
3845 multi-method method user host
3846 9 "Inserting local temp file `%s'..." local-copy)
3847 (setq result (insert-file-contents local-copy nil beg end replace))
3848 ;; Now `last-coding-system-used' has right value. Remember it.
3849 (when (boundp 'last-coding-system-used)
3850 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3851 (tramp-message-for-buffer
3852 multi-method method user host
3853 9 "Inserting local temp file `%s'...done" local-copy)
3854 (delete-file local-copy)
3855 (when (boundp 'last-coding-system-used)
3856 (set 'last-coding-system-used coding-system-used))
3857 (list (expand-file-name filename)
3858 (second result))))))
3861 (defun tramp-handle-find-backup-file-name (filename)
3862 "Like `find-backup-file-name' for tramp files."
3863 (with-parsed-tramp-file-name filename nil
3864 ;; We set both variables. It doesn't matter whether it is
3865 ;; Emacs or XEmacs
3866 (let ((backup-directory-alist
3867 ;; Emacs case
3868 (when (boundp 'backup-directory-alist)
3869 (if (boundp 'tramp-backup-directory-alist)
3870 (mapcar
3871 '(lambda (x)
3872 (cons
3873 (car x)
3874 (if (and (stringp (cdr x))
3875 (file-name-absolute-p (cdr x))
3876 (not (tramp-file-name-p (cdr x))))
3877 (tramp-make-tramp-file-name
3878 multi-method method user host (cdr x))
3879 (cdr x))))
3880 (symbol-value 'tramp-backup-directory-alist))
3881 (symbol-value 'backup-directory-alist))))
3883 (bkup-backup-directory-info
3884 ;; XEmacs case
3885 (when (boundp 'bkup-backup-directory-info)
3886 (if (boundp 'tramp-bkup-backup-directory-info)
3887 (mapcar
3888 '(lambda (x)
3889 (nconc
3890 (list (car x))
3891 (list
3892 (if (and (stringp (car (cdr x)))
3893 (file-name-absolute-p (car (cdr x)))
3894 (not (tramp-file-name-p (car (cdr x)))))
3895 (tramp-make-tramp-file-name
3896 multi-method method user host (car (cdr x)))
3897 (car (cdr x))))
3898 (cdr (cdr x))))
3899 (symbol-value 'tramp-bkup-backup-directory-info))
3900 (symbol-value 'bkup-backup-directory-info)))))
3902 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3904 (defun tramp-handle-make-auto-save-file-name ()
3905 "Like `make-auto-save-file-name' for tramp files.
3906 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3907 (when tramp-auto-save-directory
3908 (unless (file-exists-p tramp-auto-save-directory)
3909 (make-directory tramp-auto-save-directory t)))
3910 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3911 ;; file name we make sure that jka-compr isn't used for the
3912 ;; auto-save file.
3913 (let ((buffer-file-name
3914 (if tramp-auto-save-directory
3915 (expand-file-name
3916 (tramp-subst-strs-in-string
3917 '(("_" . "|")
3918 ("/" . "_a")
3919 (":" . "_b")
3920 ("|" . "__")
3921 ("[" . "_l")
3922 ("]" . "_r"))
3923 (buffer-file-name))
3924 tramp-auto-save-directory)
3925 (buffer-file-name))))
3926 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3927 ;; it is not a magic file name operation (since Emacs 22).
3928 ;; We must deactivate it temporarily.
3929 (if (not (ad-is-active 'make-auto-save-file-name))
3930 (tramp-run-real-handler
3931 'make-auto-save-file-name nil)
3932 ;; else
3933 (ad-deactivate 'make-auto-save-file-name)
3934 (prog1
3935 (tramp-run-real-handler
3936 'make-auto-save-file-name nil)
3937 (ad-activate 'make-auto-save-file-name)))))
3940 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3941 (defun tramp-handle-write-region
3942 (start end filename &optional append visit lockname confirm)
3943 "Like `write-region' for tramp files."
3944 (unless (eq append nil)
3945 (error "Cannot append to file using tramp (`%s')" filename))
3946 (setq filename (expand-file-name filename))
3947 ;; Following part commented out because we don't know what to do about
3948 ;; file locking, and it does not appear to be a problem to ignore it.
3949 ;; Ange-ftp ignores it, too.
3950 ;; (when (and lockname (stringp lockname))
3951 ;; (setq lockname (expand-file-name lockname)))
3952 ;; (unless (or (eq lockname nil)
3953 ;; (string= lockname filename))
3954 ;; (error
3955 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3956 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
3957 (when (and (not (featurep 'xemacs))
3958 confirm (file-exists-p filename))
3959 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3960 filename))
3961 (error "File not overwritten")))
3962 (with-parsed-tramp-file-name filename nil
3963 (let ((curbuf (current-buffer))
3964 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3965 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3966 (loc-enc (tramp-get-local-encoding multi-method method user host))
3967 (loc-dec (tramp-get-local-decoding multi-method method user host))
3968 (trampbuf (get-buffer-create "*tramp output*"))
3969 (modes (file-modes filename))
3970 ;; We use this to save the value of `last-coding-system-used'
3971 ;; after writing the tmp file. At the end of the function,
3972 ;; we set `last-coding-system-used' to this saved value.
3973 ;; This way, any intermediary coding systems used while
3974 ;; talking to the remote shell or suchlike won't hose this
3975 ;; variable. This approach was snarfed from ange-ftp.el.
3976 coding-system-used
3977 tmpfil)
3978 ;; Write region into a tmp file. This isn't really needed if we
3979 ;; use an encoding function, but currently we use it always
3980 ;; because this makes the logic simpler.
3981 (setq tmpfil (tramp-make-temp-file))
3982 ;; Set current buffer. If connection wasn't open, `file-modes' has
3983 ;; changed it accidently.
3984 (set-buffer curbuf)
3985 ;; We say `no-message' here because we don't want the visited file
3986 ;; modtime data to be clobbered from the temp file. We call
3987 ;; `set-visited-file-modtime' ourselves later on.
3988 (tramp-run-real-handler
3989 'write-region
3990 (if confirm ; don't pass this arg unless defined for backward compat.
3991 (list start end tmpfil append 'no-message lockname confirm)
3992 (list start end tmpfil append 'no-message lockname)))
3993 ;; Now, `last-coding-system-used' has the right value. Remember it.
3994 (when (boundp 'last-coding-system-used)
3995 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3996 ;; The permissions of the temporary file should be set. If
3997 ;; filename does not exist (eq modes nil) it has been renamed to
3998 ;; the backup file. This case `save-buffer' handles
3999 ;; permissions.
4000 (when modes (set-file-modes tmpfil modes))
4001 ;; This is a bit lengthy due to the different methods possible for
4002 ;; file transfer. First, we check whether the method uses an rcp
4003 ;; program. If so, we call it. Otherwise, both encoding and
4004 ;; decoding command must be specified. However, if the method
4005 ;; _also_ specifies an encoding function, then that is used for
4006 ;; encoding the contents of the tmp file.
4007 (cond ((tramp-method-out-of-band-p multi-method method user host)
4008 ;; `copy-file' handles out-of-band methods
4009 (copy-file tmpfil filename t t))
4011 ((and rem-enc rem-dec)
4012 ;; Use inline file transfer
4013 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4014 (save-excursion
4015 ;; Encode tmpfil into tmpbuf
4016 (tramp-message-for-buffer multi-method method user host
4017 5 "Encoding region...")
4018 (set-buffer tmpbuf)
4019 (erase-buffer)
4020 ;; Use encoding function or command.
4021 (if (and (symbolp loc-enc) (fboundp loc-enc))
4022 (progn
4023 (tramp-message-for-buffer
4024 multi-method method user host
4025 6 "Encoding region using function `%s'..."
4026 (symbol-name loc-enc))
4027 (insert-file-contents-literally tmpfil)
4028 ;; CCC. The following `let' is a workaround for
4029 ;; the base64.el that comes with pgnus-0.84. If
4030 ;; both of the following conditions are
4031 ;; satisfied, it tries to write to a local file
4032 ;; in default-directory, but at this point,
4033 ;; default-directory is remote.
4034 ;; (CALL-PROCESS-REGION can't write to remote
4035 ;; files, it seems.) The file in question is a
4036 ;; tmp file anyway.
4037 (let ((default-directory
4038 (tramp-temporary-file-directory)))
4039 (funcall loc-enc (point-min) (point-max)))
4040 (goto-char (point-max))
4041 (unless (bolp)
4042 (newline)))
4043 (tramp-message-for-buffer
4044 multi-method method user host
4045 6 "Encoding region using command `%s'..." loc-enc)
4046 (unless (equal 0 (tramp-call-local-coding-command
4047 loc-enc tmpfil t))
4048 (pop-to-buffer trampbuf)
4049 (error (concat "Cannot write to `%s', local encoding"
4050 " command `%s' failed")
4051 filename loc-enc)))
4052 ;; Send tmpbuf into remote decoding command which
4053 ;; writes to remote file. Because this happens on the
4054 ;; remote host, we cannot use the function.
4055 (tramp-message-for-buffer
4056 multi-method method user host
4057 5 "Decoding region into remote file %s..." filename)
4058 (tramp-send-command
4059 multi-method method user host
4060 (format "%s >%s <<'EOF'"
4061 rem-dec
4062 (tramp-shell-quote-argument localname)))
4063 (set-buffer tmpbuf)
4064 (tramp-message-for-buffer
4065 multi-method method user host
4066 6 "Sending data to remote host...")
4067 (tramp-send-string multi-method method user host
4068 (buffer-string))
4069 ;; wait for remote decoding to complete
4070 (tramp-message-for-buffer
4071 multi-method method user host
4072 6 "Sending end of data token...")
4073 (tramp-send-command
4074 multi-method method user host "EOF" nil t)
4075 (tramp-message-for-buffer
4076 multi-method method user host 6
4077 "Waiting for remote host to process data...")
4078 (set-buffer (tramp-get-buffer multi-method method user host))
4079 (tramp-wait-for-output)
4080 (tramp-barf-unless-okay
4081 multi-method method user host nil nil 'file-error
4082 (concat "Couldn't write region to `%s',"
4083 " decode using `%s' failed")
4084 filename rem-dec)
4085 (tramp-message 5 "Decoding region into remote file %s...done"
4086 filename)
4087 (kill-buffer tmpbuf))))
4089 (error
4090 (concat "Method `%s' should specify both encoding and "
4091 "decoding command or an rcp program")
4092 method)))
4093 (delete-file tmpfil)
4094 (unless (equal curbuf (current-buffer))
4095 (error "Buffer has changed from `%s' to `%s'"
4096 curbuf (current-buffer)))
4097 (when (or (eq visit t) (stringp visit))
4098 (set-visited-file-modtime
4099 ;; We must pass modtime explicitely, because filename can be different
4100 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4101 (nth 5 (file-attributes filename))))
4102 ;; Make `last-coding-system-used' have the right value.
4103 (when (boundp 'last-coding-system-used)
4104 (set 'last-coding-system-used coding-system-used))
4105 (when (or (eq visit t)
4106 (eq visit nil)
4107 (stringp visit))
4108 (message "Wrote %s" filename)))))
4110 ;; Call down to the real handler.
4111 ;; Because EFS does not play nicely with TRAMP (both systems match a
4112 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4113 ;; operation.
4115 ;; Other than that, this is the canon file-handler code that the doco
4116 ;; says should be used here. Which is nice.
4118 ;; Under XEmacs current, EFS also hooks in as
4119 ;; efs-sifn-handler-function to handle any filename with environment
4120 ;; variables. This has two implications:
4121 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4122 ;; 2) That TRAMP might want to do the same thing.
4123 ;; Details as they come in.
4125 ;; Daniel Pittman <daniel@danann.net>
4127 ;; (defun tramp-run-real-handler (operation args)
4128 ;; "Invoke normal file name handler for OPERATION.
4129 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4130 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4131 ;; OPERATION."
4132 ;; (let ((inhibit-file-name-handlers
4133 ;; (list 'tramp-file-name-handler
4134 ;; 'efs-file-handler-function
4135 ;; 'ange-ftp-hook-function
4136 ;; (and (eq inhibit-file-name-operation operation)
4137 ;; inhibit-file-name-handlers)))
4138 ;; (inhibit-file-name-operation operation))
4139 ;; (apply operation args)))
4141 (defun tramp-run-real-handler (operation args)
4142 "Invoke normal file name handler for OPERATION.
4143 First arg specifies the OPERATION, second arg is a list of arguments to
4144 pass to the OPERATION."
4145 (let* ((inhibit-file-name-handlers
4146 `(tramp-file-name-handler
4147 tramp-completion-file-name-handler
4148 cygwin-mount-name-hook-function
4149 cygwin-mount-map-drive-hook-function
4151 ,(and (eq inhibit-file-name-operation operation)
4152 inhibit-file-name-handlers)))
4153 (inhibit-file-name-operation operation))
4154 (apply operation args)))
4156 ;; This function is used from `tramp-completion-file-name-handler' functions
4157 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4158 ;; because the check is based on a full filename, not available for all
4159 ;; basic I/O operations.
4160 (defun tramp-completion-run-real-handler (operation args)
4161 "Invoke `tramp-file-name-handler' for OPERATION.
4162 First arg specifies the OPERATION, second arg is a list of arguments to
4163 pass to the OPERATION."
4164 (let* ((inhibit-file-name-handlers
4165 `(tramp-completion-file-name-handler
4166 cygwin-mount-name-hook-function
4167 cygwin-mount-map-drive-hook-function
4169 ,(and (eq inhibit-file-name-operation operation)
4170 inhibit-file-name-handlers)))
4171 (inhibit-file-name-operation operation))
4172 (apply operation args)))
4174 ;; We handle here all file primitives. Most of them have the file
4175 ;; name as first parameter; nevertheless we check for them explicitly
4176 ;; in order to be signalled if a new primitive appears. This
4177 ;; scenario is needed because there isn't a way to decide by
4178 ;; syntactical means whether a foreign method must be called. It would
4179 ;; ease the life if `file-name-handler-alist' would support a decision
4180 ;; function as well but regexp only.
4181 (defun tramp-file-name-for-operation (operation &rest args)
4182 "Return file name related to OPERATION file primitive.
4183 ARGS are the arguments OPERATION has been called with."
4184 (cond
4185 ; FILE resp DIRECTORY
4186 ((member operation
4187 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4188 'delete-file 'diff-latest-backup-file 'directory-file-name
4189 'directory-files 'directory-files-and-attributes
4190 'dired-compress-file 'dired-uncache
4191 'file-accessible-directory-p 'file-attributes
4192 'file-directory-p 'file-executable-p 'file-exists-p
4193 'file-local-copy 'file-remote-p 'file-modes
4194 'file-name-as-directory 'file-name-directory
4195 'file-name-nondirectory 'file-name-sans-versions
4196 'file-ownership-preserved-p 'file-readable-p
4197 'file-regular-p 'file-symlink-p 'file-truename
4198 'file-writable-p 'find-backup-file-name 'find-file-noselect
4199 'get-file-buffer 'insert-directory 'insert-file-contents
4200 'load 'make-directory 'make-directory-internal
4201 'set-file-modes 'substitute-in-file-name
4202 'unhandled-file-name-directory 'vc-registered
4203 ; XEmacs only
4204 'abbreviate-file-name 'create-file-buffer
4205 'dired-file-modtime 'dired-make-compressed-filename
4206 'dired-recursive-delete-directory 'dired-set-file-modtime
4207 'dired-shell-unhandle-file-name 'dired-uucode-file
4208 'insert-file-contents-literally 'recover-file
4209 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4210 (if (file-name-absolute-p (nth 0 args))
4211 (nth 0 args)
4212 (expand-file-name (nth 0 args))))
4213 ; FILE DIRECTORY resp FILE1 FILE2
4214 ((member operation
4215 (list 'add-name-to-file 'copy-file 'expand-file-name
4216 'file-name-all-completions 'file-name-completion
4217 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4218 ; XEmacs only
4219 'dired-make-relative-symlink
4220 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4221 (save-match-data
4222 (cond
4223 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4224 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4225 (t (buffer-file-name (current-buffer))))))
4226 ; START END FILE
4227 ((eq operation 'write-region)
4228 (nth 2 args))
4229 ; BUF
4230 ((member operation
4231 (list 'make-auto-save-file-name
4232 'set-visited-file-modtime 'verify-visited-file-modtime
4233 ; XEmacs only
4234 'backup-buffer))
4235 (buffer-file-name
4236 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4237 ; COMMAND
4238 ((member operation
4239 (list 'dired-call-process
4240 ; Emacs only
4241 'shell-command
4242 ; Emacs 22 only
4243 'process-file
4244 ; XEmacs only
4245 'dired-print-file 'dired-shell-call-process))
4246 default-directory)
4247 ; unknown file primitive
4248 (t (error "unknown file I/O primitive: %s" operation))))
4250 (defun tramp-find-foreign-file-name-handler (filename)
4251 "Return foreign file name handler if exists."
4252 (when (tramp-tramp-file-p filename)
4253 (let (elt
4255 (handler-alist tramp-foreign-file-name-handler-alist))
4256 (while handler-alist
4257 (setq elt (car handler-alist)
4258 handler-alist (cdr handler-alist))
4259 (when (funcall (car elt) filename)
4260 (setq handler-alist nil)
4261 (setq res (cdr elt))))
4262 res)))
4264 ;; Main function.
4265 ;;;###autoload
4266 (defun tramp-file-name-handler (operation &rest args)
4267 "Invoke Tramp file name handler.
4268 Falls back to normal file name handler if no tramp file name handler exists."
4269 (save-match-data
4270 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4271 (foreign (tramp-find-foreign-file-name-handler filename)))
4272 (cond
4273 (foreign (apply foreign operation args))
4274 (t (tramp-run-real-handler operation args))))))
4277 ;; In Emacs, there is some concurrency due to timers. If a timer
4278 ;; interrupts Tramp and wishes to use the same connection buffer as
4279 ;; the "main" Emacs, then garbage might occur in the connection
4280 ;; buffer. Therefore, we need to make sure that a timer does not use
4281 ;; the same connection buffer as the "main" Emacs. We implement a
4282 ;; cheap global lock, instead of locking each connection buffer
4283 ;; separately. The global lock is based on two variables,
4284 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4285 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4286 ;; processing of a single file operation, so we need to allow
4287 ;; recursive calls. That's where the `tramp-locker' variable comes in
4288 ;; -- it is let-bound to t during the execution of the current
4289 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4290 ;; then we should just proceed because we have been called
4291 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4292 ;; interrupting the "main" Emacs, and then we signal an error.
4294 (defvar tramp-locked nil
4295 "If non-nil, then Tramp is currently busy.
4296 Together with `tramp-locker', this implements a locking mechanism
4297 preventing reentrant calls of Tramp.")
4299 (defvar tramp-locker nil
4300 "If non-nil, then a caller has locked Tramp.
4301 Together with `tramp-locked', this implements a locking mechanism
4302 preventing reentrant calls of Tramp.")
4304 (defun tramp-sh-file-name-handler (operation &rest args)
4305 "Invoke remote-shell Tramp file name handler.
4306 Fall back to normal file name handler if no Tramp handler exists."
4307 (when (and tramp-locked (not tramp-locker))
4308 (signal 'file-error "Forbidden reentrant call of Tramp"))
4309 (let ((tl tramp-locked))
4310 (unwind-protect
4311 (progn
4312 (setq tramp-locked t)
4313 (let ((tramp-locker t))
4314 (save-match-data
4315 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4316 (if fn
4317 (apply (cdr fn) args)
4318 (tramp-run-real-handler operation args))))))
4319 (setq tramp-locked tl))))
4321 ;;;###autoload
4322 (defun tramp-completion-file-name-handler (operation &rest args)
4323 "Invoke tramp file name completion handler.
4324 Falls back to normal file name handler if no tramp file name handler exists."
4325 ;; (setq tramp-debug-buffer t)
4326 ;; (tramp-message 1 "%s %s" operation args)
4327 ;; (tramp-message 1 "%s %s\n%s"
4328 ;; operation args (with-output-to-string (backtrace)))
4329 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4330 (if fn
4331 (save-match-data (apply (cdr fn) args))
4332 (tramp-completion-run-real-handler operation args))))
4334 ;;;###autoload
4335 (put 'tramp-completion-file-name-handler 'safe-magic t)
4337 ;; Register in file name handler alist
4338 ;;;###autoload
4339 (add-to-list 'file-name-handler-alist
4340 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4341 (add-to-list 'file-name-handler-alist
4342 (cons tramp-completion-file-name-regexp
4343 'tramp-completion-file-name-handler))
4345 ;;;###autoload
4346 (defun tramp-unload-file-name-handler-alist ()
4347 (setq file-name-handler-alist
4348 (delete (rassoc 'tramp-file-name-handler
4349 file-name-handler-alist)
4350 (delete (rassoc 'tramp-completion-file-name-handler
4351 file-name-handler-alist)
4352 file-name-handler-alist))))
4354 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handler-alist)
4356 (defun tramp-repair-jka-compr ()
4357 "If jka-compr is already loaded, move it to the front of
4358 `file-name-handler-alist'. On Emacs 22 or so this will not be
4359 necessary anymore."
4360 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4361 (when jka
4362 (setq file-name-handler-alist
4363 (cons jka (delete jka file-name-handler-alist))))))
4364 (tramp-repair-jka-compr)
4367 ;;; Interactions with other packages:
4369 ;; -- complete.el --
4371 ;; This function contributed by Ed Sabol
4372 (defun tramp-handle-expand-many-files (name)
4373 "Like `PC-expand-many-files' for tramp files."
4374 (with-parsed-tramp-file-name name nil
4375 (save-match-data
4376 (if (or (string-match "\\*" name)
4377 (string-match "\\?" name)
4378 (string-match "\\[.*\\]" name))
4379 (save-excursion
4380 (let (bufstr)
4381 ;; CCC: To do it right, we should quote certain characters
4382 ;; in the file name, but since the echo command is going to
4383 ;; break anyway when there are spaces in the file names, we
4384 ;; don't bother.
4385 ;;-(let ((comint-file-name-quote-list
4386 ;;- (set-difference tramp-file-name-quote-list
4387 ;;- '(?\* ?\? ?[ ?]))))
4388 ;;- (tramp-send-command
4389 ;;- multi-method method user host
4390 ;;- (format "echo %s" (comint-quote-filename localname)))
4391 ;;- (tramp-wait-for-output))
4392 (tramp-send-command multi-method method user host
4393 (format "echo %s" localname))
4394 (tramp-wait-for-output)
4395 (setq bufstr (buffer-substring (point-min)
4396 (tramp-line-end-position)))
4397 (goto-char (point-min))
4398 (if (string-equal localname bufstr)
4400 (insert "(\"")
4401 (while (search-forward " " nil t)
4402 (delete-backward-char 1)
4403 (insert "\" \""))
4404 (goto-char (point-max))
4405 (delete-backward-char 1)
4406 (insert "\")")
4407 (goto-char (point-min))
4408 (mapcar
4409 (function (lambda (x)
4410 (tramp-make-tramp-file-name multi-method method
4411 user host x)))
4412 (read (current-buffer))))))
4413 (list (expand-file-name name))))))
4415 (eval-after-load "complete"
4416 '(progn
4417 (defadvice PC-expand-many-files
4418 (around tramp-advice-PC-expand-many-files (name) activate)
4419 "Invoke `tramp-handle-expand-many-files' for tramp files."
4420 (if (tramp-tramp-file-p name)
4421 (setq ad-return-value (tramp-handle-expand-many-files name))
4422 ad-do-it))
4423 (add-hook 'tramp-unload-hook
4424 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
4426 ;;; File name handler functions for completion mode
4428 (defvar tramp-completion-mode nil
4429 "If non-nil, we are in file name completion mode.")
4431 ;; Necessary because `tramp-file-name-regexp-unified' and
4432 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4433 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4434 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4435 ;; Using `last-input-event' is a little bit risky, because completing a file
4436 ;; might require loading other files, like "~/.netrc", and for them it
4437 ;; shouldn't be decided based on that variable. On the other hand, those files
4438 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4439 ;; be introduced overwriting this check in such cases. Or we change tramp
4440 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4441 ;; In case of non unified file names it can be always true (and wouldn't be
4442 ;; necessary, because there are different regexp).
4443 (defun tramp-completion-mode (file)
4444 "Checks whether method / user name / host name completion is active."
4445 (cond
4446 (tramp-completion-mode t)
4447 ((not tramp-unified-filenames) t)
4448 ((string-match "^/.*:.*:$" file) nil)
4449 ((string-match
4450 (concat tramp-prefix-regexp
4451 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4452 file)
4453 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4454 ((or (equal last-input-event 'tab)
4455 ;; Emacs
4456 (and (integerp last-input-event)
4457 (not (event-modifiers last-input-event))
4458 (or (char-equal last-input-event ?\?)
4459 (char-equal last-input-event ?\t) ; handled by 'tab already?
4460 (char-equal last-input-event ?\ )))
4461 ;; XEmacs
4462 (and (featurep 'xemacs)
4463 (not (event-modifiers last-input-event))
4464 (or (char-equal
4465 (funcall (symbol-function 'event-to-character)
4466 last-input-event) ?\?)
4467 (char-equal
4468 (funcall (symbol-function 'event-to-character)
4469 last-input-event) ?\t)
4470 (char-equal
4471 (funcall (symbol-function 'event-to-character)
4472 last-input-event) ?\ ))))
4473 t)))
4475 (defun tramp-completion-handle-file-exists-p (filename)
4476 "Like `file-exists-p' for tramp files."
4477 (if (tramp-completion-mode filename)
4478 (tramp-run-real-handler
4479 'file-exists-p (list filename))
4480 (tramp-completion-run-real-handler
4481 'file-exists-p (list filename))))
4483 ;; Localname manipulation in case of partial TRAMP file names.
4484 (defun tramp-completion-handle-file-name-directory (file)
4485 "Like `file-name-directory' but aware of TRAMP files."
4486 (if (tramp-completion-mode file)
4488 (tramp-completion-run-real-handler
4489 'file-name-directory (list file))))
4491 ;; Localname manipulation in case of partial TRAMP file names.
4492 (defun tramp-completion-handle-file-name-nondirectory (file)
4493 "Like `file-name-nondirectory' but aware of TRAMP files."
4494 (substring
4495 file (length (tramp-completion-handle-file-name-directory file))))
4497 ;; Method, host name and user name completion.
4498 ;; `tramp-completion-dissect-file-name' returns a list of
4499 ;; tramp-file-name structures. For all of them we return possible completions.
4500 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4501 "Like `file-name-all-completions' for partial tramp files."
4503 (unwind-protect
4504 ;; We need to reset `tramp-completion-mode'.
4505 (progn
4506 (setq tramp-completion-mode t)
4507 (let*
4508 ((fullname (concat directory filename))
4509 ;; possible completion structures
4510 (v (tramp-completion-dissect-file-name fullname))
4511 result result1)
4513 (while v
4514 (let* ((car (car v))
4515 (multi-method (tramp-file-name-multi-method car))
4516 (method (tramp-file-name-method car))
4517 (user (tramp-file-name-user car))
4518 (host (tramp-file-name-host car))
4519 (localname (tramp-file-name-localname car))
4520 (m (tramp-find-method multi-method method user host))
4521 (tramp-current-user user) ; see `tramp-parse-passwd'
4522 all-user-hosts)
4524 (unless (or multi-method ;; Not handled (yet).
4525 localname) ;; Nothing to complete
4527 (if (or user host)
4529 ;; Method dependent user / host combinations
4530 (progn
4531 (mapcar
4532 (lambda (x)
4533 (setq all-user-hosts
4534 (append all-user-hosts
4535 (funcall (nth 0 x) (nth 1 x)))))
4536 (tramp-get-completion-function m))
4538 (setq result (append result
4539 (mapcar
4540 (lambda (x)
4541 (tramp-get-completion-user-host
4542 method user host (nth 0 x) (nth 1 x)))
4543 (delq nil all-user-hosts)))))
4545 ;; Possible methods
4546 (setq result
4547 (append result (tramp-get-completion-methods m)))))
4549 (setq v (cdr v))))
4551 ;; unify list, remove nil elements
4552 (while result
4553 (let ((car (car result)))
4554 (when car (add-to-list 'result1 car))
4555 (setq result (cdr result))))
4557 ;; Complete local parts
4558 (append
4559 result1
4560 (condition-case nil
4561 (if result1
4562 ;; "/ssh:" does not need to be expanded as hostname.
4563 (tramp-run-real-handler
4564 'file-name-all-completions (list filename directory))
4565 ;; No method/user/host found to be expanded.
4566 (tramp-completion-run-real-handler
4567 'file-name-all-completions (list filename directory)))
4568 (error nil)))))
4569 ;; unwindform
4570 (setq tramp-completion-mode nil)))
4572 ;; Method, host name and user name completion for a file.
4573 (defun tramp-completion-handle-file-name-completion (filename directory)
4574 "Like `file-name-completion' for tramp files."
4575 (try-completion filename
4576 (mapcar 'list (file-name-all-completions filename directory))))
4578 ;; I misuse a little bit the tramp-file-name structure in order to handle
4579 ;; completion possibilities for partial methods / user names / host names.
4580 ;; Return value is a list of tramp-file-name structures according to possible
4581 ;; completions. If "multi-method" or "localname" is non-nil it means there
4582 ;; shouldn't be a completion anymore.
4584 ;; Expected results:
4586 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4587 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4588 ;; [nil nil "x" nil nil]
4589 ;; [nil "x" nil nil nil]
4591 ;; "/x:" "/x:y" "/x:y:"
4592 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4593 ;; "/[x/" "/[x/y"
4594 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4595 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4597 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4598 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4599 ;; "/[x/y@" "/[x/y@z"
4600 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4601 (defun tramp-completion-dissect-file-name (name)
4602 "Returns a list of `tramp-file-name' structures.
4603 They are collected by `tramp-completion-dissect-file-name1'."
4605 (let* ((result)
4606 (x-nil "\\|\\(\\)")
4607 ;; "/method" "/[method"
4608 (tramp-completion-file-name-structure1
4609 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4610 1 nil nil nil))
4611 ;; "/user" "/[user"
4612 (tramp-completion-file-name-structure2
4613 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4614 nil 1 nil nil))
4615 ;; "/host" "/[host"
4616 (tramp-completion-file-name-structure3
4617 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4618 nil nil 1 nil))
4619 ;; "/user@host" "/[user@host"
4620 (tramp-completion-file-name-structure4
4621 (list (concat tramp-prefix-regexp
4622 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4623 "\\(" tramp-host-regexp x-nil "\\)$")
4624 nil 1 2 nil))
4625 ;; "/method:user" "/[method/user"
4626 (tramp-completion-file-name-structure5
4627 (list (concat tramp-prefix-regexp
4628 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4629 "\\(" tramp-user-regexp x-nil "\\)$")
4630 1 2 nil nil))
4631 ;; "/method:host" "/[method/host"
4632 (tramp-completion-file-name-structure6
4633 (list (concat tramp-prefix-regexp
4634 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4635 "\\(" tramp-host-regexp x-nil "\\)$")
4636 1 nil 2 nil))
4637 ;; "/method:user@host" "/[method/user@host"
4638 (tramp-completion-file-name-structure7
4639 (list (concat tramp-prefix-regexp
4640 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4641 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4642 "\\(" tramp-host-regexp x-nil "\\)$")
4643 1 2 3 nil)))
4645 (mapcar (lambda (regexp)
4646 (add-to-list 'result
4647 (tramp-completion-dissect-file-name1 regexp name)))
4648 (list
4649 tramp-completion-file-name-structure1
4650 tramp-completion-file-name-structure2
4651 tramp-completion-file-name-structure3
4652 tramp-completion-file-name-structure4
4653 tramp-completion-file-name-structure5
4654 tramp-completion-file-name-structure6
4655 tramp-completion-file-name-structure7
4656 tramp-file-name-structure))
4658 (delq nil result)))
4660 (defun tramp-completion-dissect-file-name1 (structure name)
4661 "Returns a `tramp-file-name' structure matching STRUCTURE.
4662 The structure consists of multi-method, remote method, remote user,
4663 remote host and localname (filename on remote host)."
4665 (let (method)
4666 (save-match-data
4667 (when (string-match (nth 0 structure) name)
4668 (setq method (and (nth 1 structure)
4669 (match-string (nth 1 structure) name)))
4670 (if (and method (member method tramp-multi-methods))
4671 ;; Not handled (yet).
4672 (make-tramp-file-name
4673 :multi-method method
4674 :method nil
4675 :user nil
4676 :host nil
4677 :localname nil)
4678 (let ((user (and (nth 2 structure)
4679 (match-string (nth 2 structure) name)))
4680 (host (and (nth 3 structure)
4681 (match-string (nth 3 structure) name)))
4682 (localname (and (nth 4 structure)
4683 (match-string (nth 4 structure) name))))
4684 (make-tramp-file-name
4685 :multi-method nil
4686 :method method
4687 :user user
4688 :host host
4689 :localname localname)))))))
4691 ;; This function returns all possible method completions, adding the
4692 ;; trailing method delimeter.
4693 (defun tramp-get-completion-methods (partial-method)
4694 "Returns all method completions for PARTIAL-METHOD."
4695 (mapcar
4696 (lambda (method)
4697 (and method
4698 (string-match (concat "^" (regexp-quote partial-method)) method)
4699 ;; we must remove leading "/".
4700 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4701 (delete "multi" (mapcar 'car tramp-methods))))
4703 ;; Compares partial user and host names with possible completions.
4704 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4705 "Returns the most expanded string for user and host name completion.
4706 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4707 (cond
4709 ((and partial-user partial-host)
4710 (if (and host
4711 (string-match (concat "^" (regexp-quote partial-host)) host)
4712 (string-equal partial-user (or user partial-user)))
4713 (setq user partial-user)
4714 (setq user nil
4715 host nil)))
4717 (partial-user
4718 (setq host nil)
4719 (unless
4720 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4721 (setq user nil)))
4723 (partial-host
4724 (setq user nil)
4725 (unless
4726 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4727 (setq host nil)))
4729 (t (setq user nil
4730 host nil)))
4732 (unless (zerop (+ (length user) (length host)))
4733 ;; we must remove leading "/".
4734 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4736 (defun tramp-parse-rhosts (filename)
4737 "Return a list of (user host) tuples allowed to access.
4738 Either user or host may be nil."
4740 (let (res)
4741 (when (file-readable-p filename)
4742 (with-temp-buffer
4743 (insert-file-contents filename)
4744 (goto-char (point-min))
4745 (while (not (eobp))
4746 (push (tramp-parse-rhosts-group) res))))
4747 res))
4749 ;; Taken from gnus/netrc.el
4750 (eval-and-compile
4751 (defalias 'tramp-point-at-eol
4752 (if (fboundp 'point-at-eol)
4753 'point-at-eol
4754 'line-end-position)))
4756 (defun tramp-parse-rhosts-group ()
4757 "Return a (user host) tuple allowed to access.
4758 Either user or host may be nil."
4760 (let ((result)
4761 (regexp
4762 (concat
4763 "^\\(" tramp-host-regexp "\\)"
4764 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4766 (narrow-to-region (point) (tramp-point-at-eol))
4767 (when (re-search-forward regexp nil t)
4768 (setq result (append (list (match-string 3) (match-string 1)))))
4769 (widen)
4770 (forward-line 1)
4771 result))
4773 (defun tramp-parse-shosts (filename)
4774 "Return a list of (user host) tuples allowed to access.
4775 User is always nil."
4777 (let (res)
4778 (when (file-readable-p filename)
4779 (with-temp-buffer
4780 (insert-file-contents filename)
4781 (goto-char (point-min))
4782 (while (not (eobp))
4783 (push (tramp-parse-shosts-group) res))))
4784 res))
4786 (defun tramp-parse-shosts-group ()
4787 "Return a (user host) tuple allowed to access.
4788 User is always nil."
4790 (let ((result)
4791 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4793 (narrow-to-region (point) (tramp-point-at-eol))
4794 (when (re-search-forward regexp nil t)
4795 (setq result (list nil (match-string 1))))
4796 (widen)
4798 (> (skip-chars-forward ",") 0)
4799 (forward-line 1))
4800 result))
4802 (defun tramp-parse-sconfig (filename)
4803 "Return a list of (user host) tuples allowed to access.
4804 User is always nil."
4806 (let (res)
4807 (when (file-readable-p filename)
4808 (with-temp-buffer
4809 (insert-file-contents filename)
4810 (goto-char (point-min))
4811 (while (not (eobp))
4812 (push (tramp-parse-sconfig-group) res))))
4813 res))
4815 (defun tramp-parse-sconfig-group ()
4816 "Return a (user host) tuple allowed to access.
4817 User is always nil."
4819 (let ((result)
4820 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4822 (narrow-to-region (point) (tramp-point-at-eol))
4823 (when (re-search-forward regexp nil t)
4824 (setq result (list nil (match-string 1))))
4825 (widen)
4827 (> (skip-chars-forward ",") 0)
4828 (forward-line 1))
4829 result))
4831 (defun tramp-parse-shostkeys (dirname)
4832 "Return a list of (user host) tuples allowed to access.
4833 User is always nil."
4835 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4836 (files (when (file-directory-p dirname) (directory-files dirname)))
4837 result)
4839 (while files
4840 (when (string-match regexp (car files))
4841 (push (list nil (match-string 1 (car files))) result))
4842 (setq files (cdr files)))
4843 result))
4845 (defun tramp-parse-sknownhosts (dirname)
4846 "Return a list of (user host) tuples allowed to access.
4847 User is always nil."
4849 (let ((regexp (concat "^\\(" tramp-host-regexp
4850 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4851 (files (when (file-directory-p dirname) (directory-files dirname)))
4852 result)
4854 (while files
4855 (when (string-match regexp (car files))
4856 (push (list nil (match-string 1 (car files))) result))
4857 (setq files (cdr files)))
4858 result))
4860 (defun tramp-parse-hosts (filename)
4861 "Return a list of (user host) tuples allowed to access.
4862 User is always nil."
4864 (let (res)
4865 (when (file-readable-p filename)
4866 (with-temp-buffer
4867 (insert-file-contents filename)
4868 (goto-char (point-min))
4869 (while (not (eobp))
4870 (push (tramp-parse-hosts-group) res))))
4871 res))
4873 (defun tramp-parse-hosts-group ()
4874 "Return a (user host) tuple allowed to access.
4875 User is always nil."
4877 (let ((result)
4878 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4880 (narrow-to-region (point) (tramp-point-at-eol))
4881 (when (re-search-forward regexp nil t)
4882 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4883 (setq result (list nil (match-string 1)))))
4884 (widen)
4886 (> (skip-chars-forward " \t") 0)
4887 (forward-line 1))
4888 result))
4890 ;; For su-alike methods it would be desirable to return "root@localhost"
4891 ;; as default. Unfortunately, we have no information whether any user name
4892 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4893 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4894 (defun tramp-parse-passwd (filename)
4895 "Return a list of (user host) tuples allowed to access.
4896 Host is always \"localhost\"."
4898 (let (res)
4899 (if (zerop (length tramp-current-user))
4900 '(("root" nil))
4901 (when (file-readable-p filename)
4902 (with-temp-buffer
4903 (insert-file-contents filename)
4904 (goto-char (point-min))
4905 (while (not (eobp))
4906 (push (tramp-parse-passwd-group) res))))
4907 res)))
4909 (defun tramp-parse-passwd-group ()
4910 "Return a (user host) tuple allowed to access.
4911 Host is always \"localhost\"."
4913 (let ((result)
4914 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4916 (narrow-to-region (point) (tramp-point-at-eol))
4917 (when (re-search-forward regexp nil t)
4918 (setq result (list (match-string 1) "localhost")))
4919 (widen)
4920 (forward-line 1)
4921 result))
4923 (defun tramp-parse-netrc (filename)
4924 "Return a list of (user host) tuples allowed to access.
4925 User may be nil."
4927 (let (res)
4928 (when (file-readable-p filename)
4929 (with-temp-buffer
4930 (insert-file-contents filename)
4931 (goto-char (point-min))
4932 (while (not (eobp))
4933 (push (tramp-parse-netrc-group) res))))
4934 res))
4936 (defun tramp-parse-netrc-group ()
4937 "Return a (user host) tuple allowed to access.
4938 User may be nil."
4940 (let ((result)
4941 (regexp
4942 (concat
4943 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4944 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4946 (narrow-to-region (point) (tramp-point-at-eol))
4947 (when (re-search-forward regexp nil t)
4948 (setq result (list (match-string 3) (match-string 1))))
4949 (widen)
4950 (forward-line 1)
4951 result))
4953 (defun tramp-completion-handle-expand-file-name (name &optional dir)
4954 "Like `expand-file-name' for tramp files."
4955 (let ((fullname (concat (or dir default-directory) name)))
4956 (if (tramp-completion-mode fullname)
4957 (tramp-run-real-handler
4958 'expand-file-name (list name dir))
4959 (tramp-completion-run-real-handler
4960 'expand-file-name (list name dir)))))
4962 ;;; Internal Functions:
4964 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
4965 "Define in remote shell function NAME implemented as perl SCRIPT.
4966 Only send the definition if it has not already been done.
4967 Function may have 0-3 parameters."
4968 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4969 (unless remote-perl (error "No remote perl"))
4970 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4971 multi-method method user host)))
4972 (unless (memq name perl-scripts)
4973 (with-current-buffer (tramp-get-buffer multi-method method user host)
4974 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4975 (tramp-send-string multi-method method user host
4976 (concat name
4977 " () {\n"
4978 remote-perl
4979 " -e '"
4980 script
4981 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4982 (tramp-wait-for-output)
4983 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4984 multi-method method user host)
4985 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4987 (defun tramp-set-auto-save ()
4988 (when (and (buffer-file-name)
4989 (tramp-tramp-file-p (buffer-file-name))
4990 ;; ange-ftp has its own auto-save mechanism
4991 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
4992 'tramp-sh-file-name-handler)
4993 auto-save-default)
4994 (auto-save-mode 1)))
4995 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
4996 (add-hook 'tramp-unload-hook
4997 '(lambda ()
4998 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
5000 (defun tramp-run-test (switch filename)
5001 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5002 Returns the exit code of the `test' program."
5003 (let ((v (tramp-dissect-file-name filename)))
5004 (save-excursion
5005 (tramp-send-command-and-check
5006 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5007 (tramp-file-name-user v) (tramp-file-name-host v)
5008 (format "test %s %s" switch
5009 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
5011 (defun tramp-run-test2 (program file1 file2 &optional switch)
5012 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5013 The optional SWITCH is inserted between the two files.
5014 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5015 hosts, or files, disagree."
5016 (let* ((v1 (tramp-dissect-file-name file1))
5017 (v2 (tramp-dissect-file-name file2))
5018 (mmethod1 (tramp-file-name-multi-method v1))
5019 (mmethod2 (tramp-file-name-multi-method v2))
5020 (method1 (tramp-file-name-method v1))
5021 (method2 (tramp-file-name-method v2))
5022 (user1 (tramp-file-name-user v1))
5023 (user2 (tramp-file-name-user v2))
5024 (host1 (tramp-file-name-host v1))
5025 (host2 (tramp-file-name-host v2))
5026 (localname1 (tramp-file-name-localname v1))
5027 (localname2 (tramp-file-name-localname v2)))
5028 (unless (and method1 method2 host1 host2
5029 (equal mmethod1 mmethod2)
5030 (equal method1 method2)
5031 (equal user1 user2)
5032 (equal host1 host2))
5033 (error "tramp-run-test2: %s"
5034 "only implemented for same method, same user, same host"))
5035 (save-excursion
5036 (tramp-send-command-and-check
5037 mmethod1 method1 user1 host1
5038 (format "%s %s %s %s"
5039 program
5040 (tramp-shell-quote-argument localname1)
5041 (or switch "")
5042 (tramp-shell-quote-argument localname2))))))
5044 (defun tramp-touch (file time)
5045 "Set the last-modified timestamp of the given file.
5046 TIME is an Emacs internal time value as returned by `current-time'."
5047 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
5048 (if (tramp-tramp-file-p file)
5049 (with-parsed-tramp-file-name file nil
5050 (let ((buf (tramp-get-buffer multi-method method user host)))
5051 (unless (zerop (tramp-send-command-and-check
5052 multi-method method user host
5053 (format "touch -t %s %s"
5054 touch-time
5055 localname)))
5056 (pop-to-buffer buf)
5057 (error "tramp-touch: touch failed, see buffer `%s' for details"
5058 buf))))
5059 ;; It's a local file
5060 (with-temp-buffer
5061 (unless (zerop (call-process
5062 "touch" nil (current-buffer) nil "-t" touch-time file))
5063 (pop-to-buffer (current-buffer))
5064 (error "tramp-touch: touch failed"))))))
5066 (defun tramp-buffer-name (multi-method method user host)
5067 "A name for the connection buffer for USER at HOST using METHOD."
5068 (if multi-method
5069 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5070 (let ((method (tramp-find-method multi-method method user host)))
5071 (if user
5072 (format "*tramp/%s %s@%s*" method user host)
5073 (format "*tramp/%s %s*" method host)))))
5075 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5076 "A name for the multi method connection buffer.
5077 MULTI-METHOD gives the multi method, METHOD the array of methods,
5078 USER the array of user names, HOST the array of host names."
5079 (unless (and (= (length method) (length user))
5080 (= (length method) (length host)))
5081 (error "Syntax error in multi method (implementation error)"))
5082 (let ((len (length method))
5083 (i 0)
5084 string-list)
5085 (while (< i len)
5086 (setq string-list
5087 (cons (if (aref user i)
5088 (format "%s#%s@%s:" (aref method i)
5089 (aref user i) (aref host i))
5090 (format "%s@%s:" (aref method i) (aref host i)))
5091 string-list))
5092 (incf i))
5093 (format "*%s/%s %s*"
5094 prefix multi-method
5095 (apply 'concat (reverse string-list)))))
5097 (defun tramp-get-buffer (multi-method method user host)
5098 "Get the connection buffer to be used for USER at HOST using METHOD."
5099 (with-current-buffer
5100 (get-buffer-create (tramp-buffer-name multi-method method user host))
5101 (setq buffer-undo-list t)
5102 (current-buffer)))
5104 (defun tramp-debug-buffer-name (multi-method method user host)
5105 "A name for the debug buffer for USER at HOST using METHOD."
5106 (if multi-method
5107 (tramp-buffer-name-multi-method "debug tramp"
5108 multi-method method user host)
5109 (let ((method (tramp-find-method multi-method method user host)))
5110 (if user
5111 (format "*debug tramp/%s %s@%s*" method user host)
5112 (format "*debug tramp/%s %s*" method host)))))
5114 (defun tramp-get-debug-buffer (multi-method method user host)
5115 "Get the debug buffer for USER at HOST using METHOD."
5116 (with-current-buffer
5117 (get-buffer-create
5118 (tramp-debug-buffer-name multi-method method user host))
5119 (setq buffer-undo-list t)
5120 (current-buffer)))
5122 (defun tramp-find-executable (multi-method method user host
5123 progname dirlist ignore-tilde)
5124 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5125 First args METHOD, USER and HOST specify the connection, PROGNAME
5126 is the program to search for, and DIRLIST gives the list of directories
5127 to search. If IGNORE-TILDE is non-nil, directory names starting
5128 with `~' will be ignored.
5130 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5132 This function expects to be in the right *tramp* buffer."
5133 (let (result)
5134 (when ignore-tilde
5135 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5136 ;; `remove' is in CL, and we want to avoid CL dependencies.
5137 (let (newdl d)
5138 (while dirlist
5139 (setq d (car dirlist))
5140 (setq dirlist (cdr dirlist))
5141 (unless (char-equal ?~ (aref d 0))
5142 (setq newdl (cons d newdl))))
5143 (setq dirlist (nreverse newdl))))
5144 (tramp-send-command
5145 multi-method method user host
5146 (format (concat "while read d; "
5147 "do if test -x $d/%s -a -f $d/%s; "
5148 "then echo tramp_executable $d/%s; "
5149 "break; fi; done <<'EOF'")
5150 progname progname progname))
5151 (mapcar (lambda (d)
5152 (tramp-send-command multi-method method user host d))
5153 dirlist)
5154 (tramp-send-command multi-method method user host "EOF")
5155 (tramp-wait-for-output)
5156 (goto-char (point-max))
5157 (when (search-backward "tramp_executable " nil t)
5158 (skip-chars-forward "^ ")
5159 (skip-chars-forward " ")
5160 (buffer-substring (point) (tramp-line-end-position)))))
5162 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5163 "Sets the remote environment VAR to existing directories from DIRLIST.
5164 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5165 so, it is added to the environment variable VAR."
5166 (let ((existing-dirs
5167 (mapcar
5168 (lambda (x)
5169 (when (and
5170 (file-exists-p
5171 (tramp-make-tramp-file-name multi-method method user host x))
5172 (file-directory-p
5173 (tramp-make-tramp-file-name multi-method method user host x)))
5175 dirlist)))
5176 (tramp-send-command
5177 multi-method method user host
5178 (concat var "="
5179 (mapconcat 'identity (delq nil existing-dirs) ":")
5180 "; export " var))
5181 (tramp-wait-for-output)))
5183 ;; -- communication with external shell --
5185 (defun tramp-find-file-exists-command (multi-method method user host)
5186 "Find a command on the remote host for checking if a file exists.
5187 Here, we are looking for a command which has zero exit status if the
5188 file exists and nonzero exit status otherwise."
5189 (make-local-variable 'tramp-file-exists-command)
5190 (tramp-message 9 "Finding command to check if file exists")
5191 (let ((existing
5192 (tramp-make-tramp-file-name
5193 multi-method method user host
5194 "/")) ;assume this file always exists
5195 (nonexisting
5196 (tramp-make-tramp-file-name
5197 multi-method method user host
5198 "/ this file does not exist "))) ;assume this never exists
5199 ;; The algorithm is as follows: we try a list of several commands.
5200 ;; For each command, we first run `$cmd /' -- this should return
5201 ;; true, as the root directory always exists. And then we run
5202 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5203 ;; does not exist. This should return false. We use the first
5204 ;; command we find that seems to work.
5205 ;; The list of commands to try is as follows:
5206 ;; `ls -d' This works on most systems, but NetBSD 1.4
5207 ;; has a bug: `ls' always returns zero exit
5208 ;; status, even for files which don't exist.
5209 ;; `test -e' Some Bourne shells have a `test' builtin
5210 ;; which does not know the `-e' option.
5211 ;; `/bin/test -e' For those, the `test' binary on disk normally
5212 ;; provides the option. Alas, the binary
5213 ;; is sometimes `/bin/test' and sometimes it's
5214 ;; `/usr/bin/test'.
5215 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5216 (unless (or
5217 (and (setq tramp-file-exists-command "test -e %s")
5218 (file-exists-p existing)
5219 (not (file-exists-p nonexisting)))
5220 (and (setq tramp-file-exists-command "/bin/test -e %s")
5221 (file-exists-p existing)
5222 (not (file-exists-p nonexisting)))
5223 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5224 (file-exists-p existing)
5225 (not (file-exists-p nonexisting)))
5226 (and (setq tramp-file-exists-command "ls -d %s")
5227 (file-exists-p existing)
5228 (not (file-exists-p nonexisting))))
5229 (error "Couldn't find command to check if file exists"))))
5232 ;; CCC test ksh or bash found for tilde expansion?
5233 (defun tramp-find-shell (multi-method method user host)
5234 "Find a shell on the remote host which groks tilde expansion."
5235 (let ((shell nil))
5236 (tramp-send-command multi-method method user host "echo ~root")
5237 (tramp-wait-for-output)
5238 (cond
5239 ((string-match "^~root$" (buffer-string))
5240 (setq shell
5241 (or (tramp-find-executable multi-method method user host
5242 "bash" tramp-remote-path t)
5243 (tramp-find-executable multi-method method user host
5244 "ksh" tramp-remote-path t)))
5245 (unless shell
5246 (error "Couldn't find a shell which groks tilde expansion"))
5247 ;; Find arguments for this shell.
5248 (let ((alist tramp-sh-extra-args)
5249 item extra-args)
5250 (while (and alist (null extra-args))
5251 (setq item (pop alist))
5252 (when (string-match (car item) shell)
5253 (setq extra-args (cdr item))))
5254 (when extra-args (setq shell (concat shell " " extra-args))))
5255 (tramp-message
5256 5 "Starting remote shell `%s' for tilde expansion..." shell)
5257 (tramp-send-command
5258 multi-method method user host
5259 (concat "PS1='$ ' exec " shell)) ;
5260 (tramp-barf-if-no-shell-prompt
5261 (get-buffer-process (current-buffer))
5262 60 "Couldn't find remote `%s' prompt" shell)
5263 (tramp-message
5264 9 "Setting remote shell prompt...")
5265 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5266 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5267 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5268 ;; as well.
5269 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5270 tramp-rsh-end-of-line
5271 tramp-end-of-output
5272 tramp-rsh-end-of-line
5273 tramp-rsh-end-of-line))
5274 (tramp-wait-for-output)
5275 (tramp-message
5276 9 "Setting remote shell prompt...done")
5278 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5279 (tramp-get-method-parameter
5280 multi-method method user host 'tramp-remote-sh))))))
5282 (defun tramp-check-ls-command (multi-method method user host cmd)
5283 "Checks whether the given `ls' executable groks `-n'.
5284 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5285 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5286 otherwise."
5287 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5288 (when (file-executable-p
5289 (tramp-make-tramp-file-name multi-method method user host cmd))
5290 (let ((result nil))
5291 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5292 (setq result
5293 (tramp-send-command-and-check
5294 multi-method method user host
5295 (format "%s -lnd / >/dev/null"
5296 cmd)))
5297 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5299 (if (zerop result) "okay" "failed"))
5300 (zerop result))))
5302 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5303 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5304 Returns nil if none was found, else the command is returned."
5305 (let ((dl dirlist)
5306 (result nil))
5307 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5308 ;; It would be better to use the CL function `find', but
5309 ;; we don't want run-time dependencies on CL.
5310 (while (and dl (not result))
5311 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5312 (when (tramp-check-ls-command multi-method method user host x)
5313 (setq result x)))
5314 (setq dl (cdr dl)))
5315 result)))
5317 (defun tramp-find-ls-command (multi-method method user host)
5318 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5319 \(This option prints numeric user and group ids in a long listing.)"
5320 (tramp-message 9 "Finding a suitable `ls' command")
5322 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5323 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5324 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5326 ;; ------------------------------------------------------------
5327 ;; -- Functions for establishing connection --
5328 ;; ------------------------------------------------------------
5330 ;; The following functions are actions to be taken when seeing certain
5331 ;; prompts from the remote host. See the variable
5332 ;; `tramp-actions-before-shell' for usage of these functions.
5334 (defun tramp-action-login (p multi-method method user host)
5335 "Send the login name."
5336 (tramp-message 9 "Sending login name `%s'"
5337 (or user (user-login-name)))
5338 (erase-buffer)
5339 (process-send-string nil (concat (or user (user-login-name))
5340 tramp-rsh-end-of-line)))
5342 (defun tramp-action-password (p multi-method method user host)
5343 "Query the user for a password."
5344 (let ((pw-prompt
5345 (format "Password for %s "
5346 (tramp-make-tramp-file-name
5347 nil method user host ""))))
5348 (tramp-message 9 "Sending password")
5349 (tramp-enter-password p pw-prompt user host)))
5351 (defun tramp-action-succeed (p multi-method method user host)
5352 "Signal success in finding shell prompt."
5353 (tramp-message 9 "Found remote shell prompt.")
5354 (erase-buffer)
5355 (throw 'tramp-action 'ok))
5357 (defun tramp-action-permission-denied (p multi-method method user host)
5358 "Signal permission denied."
5359 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5360 (tramp-message 9 "Permission denied by remote host.")
5361 (kill-process p)
5362 (throw 'tramp-action 'permission-denied))
5364 (defun tramp-action-yesno (p multi-method method user host)
5365 "Ask the user for confirmation using `yes-or-no-p'.
5366 Send \"yes\" to remote process on confirmation, abort otherwise.
5367 See also `tramp-action-yn'."
5368 (save-window-excursion
5369 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5370 (unless (yes-or-no-p (match-string 0))
5371 (kill-process p)
5372 (erase-buffer)
5373 (throw 'tramp-action 'permission-denied))
5374 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5375 (erase-buffer)))
5377 (defun tramp-action-yn (p multi-method method user host)
5378 "Ask the user for confirmation using `y-or-n-p'.
5379 Send \"y\" to remote process on confirmation, abort otherwise.
5380 See also `tramp-action-yesno'."
5381 (save-window-excursion
5382 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5383 (unless (y-or-n-p (match-string 0))
5384 (kill-process p)
5385 (throw 'tramp-action 'permission-denied))
5386 (erase-buffer)
5387 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5389 (defun tramp-action-terminal (p multi-method method user host)
5390 "Tell the remote host which terminal type to use.
5391 The terminal type can be configured with `tramp-terminal-type'."
5392 (tramp-message 9 "Setting `%s' as terminal type."
5393 tramp-terminal-type)
5394 (erase-buffer)
5395 (process-send-string nil (concat tramp-terminal-type
5396 tramp-rsh-end-of-line)))
5398 (defun tramp-action-process-alive (p multi-method method user host)
5399 "Check whether a process has finished."
5400 (unless (memq (process-status p) '(run open))
5401 (throw 'tramp-action 'process-died)))
5403 (defun tramp-action-out-of-band (p multi-method method user host)
5404 "Check whether an out-of-band copy has finished."
5405 (cond ((and (memq (process-status p) '(stop exit))
5406 (zerop (process-exit-status p)))
5407 (tramp-message 9 "Process has finished.")
5408 (throw 'tramp-action 'ok))
5409 ((or (and (memq (process-status p) '(stop exit))
5410 (not (zerop (process-exit-status p))))
5411 (memq (process-status p) '(signal)))
5412 ;; `scp' could have copied correctly, but set modes could have failed.
5413 ;; This can be ignored.
5414 (goto-char (point-min))
5415 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5416 (progn
5417 (tramp-message 10 "'set mode' error ignored.")
5418 (tramp-message 9 "Process has finished.")
5419 (throw 'tramp-action 'ok))
5420 (goto-char (point-min))
5421 (when (re-search-forward "^.cp.?: \\(.+: Permission denied.?\\)$" nil t)
5422 (error "Remote host: %s" (match-string 1)))
5423 (tramp-message 9 "Process has died.")
5424 (throw 'tramp-action 'process-died)))
5425 (t nil)))
5427 ;; The following functions are specifically for multi connections.
5429 (defun tramp-multi-action-login (p method user host)
5430 "Send the login name."
5431 (tramp-message 9 "Sending login name `%s'" user)
5432 (erase-buffer)
5433 (process-send-string p (concat user tramp-rsh-end-of-line)))
5435 (defun tramp-multi-action-password (p method user host)
5436 "Query the user for a password."
5437 (let ((pw-prompt
5438 (format "Password for %s "
5439 (tramp-make-tramp-file-name
5440 nil method user host ""))))
5441 (tramp-message 9 "Sending password")
5442 (tramp-enter-password p pw-prompt user host)))
5444 (defun tramp-multi-action-succeed (p method user host)
5445 "Signal success in finding shell prompt."
5446 (tramp-message 9 "Found shell prompt on `%s'" host)
5447 (erase-buffer)
5448 (throw 'tramp-action 'ok))
5450 (defun tramp-multi-action-permission-denied (p method user host)
5451 "Signal permission denied."
5452 (tramp-message 9 "Permission denied by remote host `%s'" host)
5453 (kill-process p)
5454 (erase-buffer)
5455 (throw 'tramp-action 'permission-denied))
5457 (defun tramp-multi-action-process-alive (p method user host)
5458 "Check whether a process has finished."
5459 (unless (memq (process-status p) '(run open))
5460 (throw 'tramp-action 'process-died)))
5462 ;; Functions for processing the actions.
5464 (defun tramp-process-one-action (p multi-method method user host actions)
5465 "Wait for output from the shell and perform one action."
5466 (let (found item pattern action todo)
5467 (erase-buffer)
5468 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5469 (with-timeout (60 (throw 'tramp-action 'timeout))
5470 (while (not found)
5471 (tramp-accept-process-output p 1)
5472 (goto-char (point-min))
5473 (setq todo actions)
5474 (while todo
5475 (goto-char (point-min))
5476 (setq item (pop todo))
5477 (setq pattern (symbol-value (nth 0 item)))
5478 (setq action (nth 1 item))
5479 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5480 pattern)
5481 (when (re-search-forward (concat pattern "\\'") nil t)
5482 (setq found (funcall action p multi-method method user host)))))
5483 found)))
5485 (defun tramp-process-actions (p multi-method method user host actions)
5486 "Perform actions until success."
5487 (let (exit)
5488 (while (not exit)
5489 (tramp-message 9 "Waiting for prompts from remote shell")
5490 (setq exit
5491 (catch 'tramp-action
5492 (tramp-process-one-action
5493 p multi-method method user host actions)
5494 nil)))
5495 (unless (eq exit 'ok)
5496 (tramp-clear-passwd user host)
5497 (error "Login failed"))))
5499 ;; For multi-actions.
5501 (defun tramp-process-one-multi-action (p method user host actions)
5502 "Wait for output from the shell and perform one action."
5503 (let (found item pattern action todo)
5504 (erase-buffer)
5505 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5506 (with-timeout (60 (throw 'tramp-action 'timeout))
5507 (while (not found)
5508 (tramp-accept-process-output p 1)
5509 (setq todo actions)
5510 (goto-char (point-min))
5511 (while todo
5512 (goto-char (point-min))
5513 (setq item (pop todo))
5514 (setq pattern (symbol-value (nth 0 item)))
5515 (setq action (nth 1 item))
5516 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5517 pattern)
5518 (when (re-search-forward (concat pattern "\\'") nil t)
5519 (setq found (funcall action p method user host)))))
5520 found)))
5522 (defun tramp-process-multi-actions (p method user host actions)
5523 "Perform actions until success."
5524 (let (exit)
5525 (while (not exit)
5526 (tramp-message 9 "Waiting for prompts from remote shell")
5527 (setq exit
5528 (catch 'tramp-action
5529 (tramp-process-one-multi-action p method user host actions)
5530 nil)))
5531 (unless (eq exit 'ok)
5532 (tramp-clear-passwd user host)
5533 (error "Login failed"))))
5535 ;; Functions to execute when we have seen the remote shell prompt but
5536 ;; before we exec the Bourne-ish shell. Note that these commands
5537 ;; might be sent to any shell, not just a Bourne-ish shell. This
5538 ;; means that the commands need to work in all shells. (It is also
5539 ;; okay for some commands to just fail with an error message, but
5540 ;; please make sure that they at least don't crash the odd shell people
5541 ;; might be running...)
5542 (defun tramp-process-initial-commands (p
5543 multi-method method user host
5544 commands)
5545 "Send list of commands to remote host, in order."
5546 (let (cmd)
5547 (while commands
5548 (setq cmd (pop commands))
5549 (erase-buffer)
5550 (tramp-message 10 "Sending command to remote shell: %s"
5551 cmd)
5552 (tramp-send-command multi-method method user host cmd nil t)
5553 (tramp-barf-if-no-shell-prompt
5554 p 60 "Remote shell command failed: %s" cmd))
5555 (erase-buffer)))
5557 ;; The actual functions for opening connections.
5559 (defun tramp-open-connection-telnet (multi-method method user host)
5560 "Open a connection using a telnet METHOD.
5561 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5562 login prompt, then sends the user name USER, then waits for a remote
5563 password prompt. It queries the user for the password, then sends the
5564 password to the remote host.
5566 If USER is nil, uses value returned by `(user-login-name)' instead.
5568 Recognition of the remote shell prompt is based on the variables
5569 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5570 set up correctly.
5572 Please note that it is NOT possible to use this connection method
5573 together with an out-of-band transfer method! You must use an inline
5574 transfer method.
5576 Maybe the different regular expressions need to be tuned.
5578 * Actually, the telnet program as well as the args to be used can be
5579 specified in the method parameters, see the variable `tramp-methods'."
5580 (save-match-data
5581 (when (tramp-method-out-of-band-p multi-method method user host)
5582 (error "Cannot use out-of-band method `%s' with telnet connection method"
5583 method))
5584 (when multi-method
5585 (error "Cannot multi-connect using telnet connection method"))
5586 (tramp-pre-connection multi-method method user host tramp-chunksize)
5587 (tramp-message 7 "Opening connection for %s@%s using %s..."
5588 (or user (user-login-name)) host method)
5589 (let ((process-environment (copy-sequence process-environment)))
5590 (setenv "TERM" tramp-terminal-type)
5591 (setenv "PS1" "$ ")
5592 (let* ((default-directory (tramp-temporary-file-directory))
5593 ;; If we omit the conditional here, then we would use
5594 ;; `undecided-dos' in some cases. With the conditional,
5595 ;; we use nil in these cases. Which one is right?
5596 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5597 (> emacs-major-version 20))
5598 tramp-dos-coding-system))
5599 (p (apply 'start-process
5600 (tramp-buffer-name multi-method method user host)
5601 (tramp-get-buffer multi-method method user host)
5602 (tramp-get-method-parameter
5603 multi-method
5604 (tramp-find-method multi-method method user host)
5605 user host 'tramp-login-program)
5606 host
5607 (tramp-get-method-parameter
5608 multi-method
5609 (tramp-find-method multi-method method user host)
5610 user host 'tramp-login-args)))
5611 (found nil)
5612 (pw nil))
5613 (tramp-set-process-query-on-exit-flag p nil)
5614 (set-buffer (tramp-get-buffer multi-method method user host))
5615 (erase-buffer)
5616 (tramp-process-actions p multi-method method user host
5617 tramp-actions-before-shell)
5618 (tramp-open-connection-setup-interactive-shell
5619 p multi-method method user host)
5620 (tramp-post-connection multi-method method user host)))))
5623 (defun tramp-open-connection-rsh (multi-method method user host)
5624 "Open a connection using an rsh METHOD.
5625 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5626 password or shell prompt. If a password prompt is seen, the user is
5627 queried for a password, this function sends the password to the remote
5628 host and waits for a shell prompt.
5630 If USER is nil, start the command `rsh HOST'[*] instead
5632 Recognition of the remote shell prompt is based on the variables
5633 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5634 set up correctly.
5636 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5637 be a port number for ssh, and \"-p yy\" will be added to the list of
5638 arguments, and xx will be used as the host name to connect to.
5640 * Actually, the rsh program to be used can be specified in the
5641 method parameters, see the variable `tramp-methods'."
5642 (save-match-data
5643 (when multi-method
5644 (error "Cannot multi-connect using rsh connection method"))
5645 (tramp-pre-connection multi-method method user host tramp-chunksize)
5646 (if (and user (not (string= user "")))
5647 (tramp-message 7 "Opening connection for %s@%s using %s..."
5648 user host method)
5649 (tramp-message 7 "Opening connection at %s using %s..." host method))
5650 (let ((process-environment (copy-sequence process-environment))
5651 (bufnam (tramp-buffer-name multi-method method user host))
5652 (buf (tramp-get-buffer multi-method method user host))
5653 (login-program (tramp-get-method-parameter
5654 multi-method
5655 (tramp-find-method multi-method method user host)
5656 user host 'tramp-login-program))
5657 (login-args (tramp-get-method-parameter
5658 multi-method
5659 (tramp-find-method multi-method method user host)
5660 user host 'tramp-login-args))
5661 (real-host host))
5662 ;; The following should be changed. We need a more general
5663 ;; mechanism to parse extra host args.
5664 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5665 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5666 (setq real-host (match-string 1 host)))
5667 (setenv "TERM" tramp-terminal-type)
5668 (setenv "PS1" "$ ")
5669 (let* ((default-directory (tramp-temporary-file-directory))
5670 ;; If we omit the conditional, we would use
5671 ;; `undecided-dos' in some cases. With the conditional,
5672 ;; we use nil in these cases. Which one is right?
5673 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5674 (> emacs-major-version 20))
5675 tramp-dos-coding-system))
5676 (p (if (and user (not (string= user "")))
5677 (apply #'start-process bufnam buf login-program
5678 real-host "-l" user login-args)
5679 (apply #'start-process bufnam buf login-program
5680 real-host login-args)))
5681 (found nil))
5682 (tramp-set-process-query-on-exit-flag p nil)
5684 (set-buffer buf)
5685 (tramp-process-actions p multi-method method user host
5686 tramp-actions-before-shell)
5687 (tramp-message 7 "Initializing remote shell")
5688 (tramp-open-connection-setup-interactive-shell
5689 p multi-method method user host)
5690 (tramp-post-connection multi-method method user host)))))
5692 (defun tramp-open-connection-su (multi-method method user host)
5693 "Open a connection using the `su' program with METHOD.
5694 This starts `su - USER', then waits for a password prompt. The HOST
5695 name must be equal to the local host name or to `localhost'.
5697 If USER is nil, uses value returned by user-login-name instead.
5699 Recognition of the remote shell prompt is based on the variables
5700 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5701 set up correctly. Note that the other user may have a different shell
5702 prompt than you do, so it is not at all unlikely that the variable
5703 `shell-prompt-pattern' is set up wrongly!"
5704 (save-match-data
5705 (when (tramp-method-out-of-band-p multi-method method user host)
5706 (error "Cannot use out-of-band method `%s' with `su' connection method"
5707 method))
5708 (unless (or (string-match (concat "^" (regexp-quote host))
5709 (system-name))
5710 (string= "localhost" host)
5711 (string= "" host))
5712 (error
5713 "Cannot connect to different host `%s' with `su' connection method"
5714 host))
5715 (tramp-pre-connection multi-method method user host tramp-chunksize)
5716 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5717 (or user "<root>") method)
5718 (let ((process-environment (copy-sequence process-environment)))
5719 (setenv "TERM" tramp-terminal-type)
5720 (setenv "PS1" "$ ")
5721 (let* ((default-directory (tramp-temporary-file-directory))
5722 ;; If we omit the conditional, we use `undecided-dos' in
5723 ;; some cases. With the conditional, we use nil in these
5724 ;; cases. What's the difference? Which one is right?
5725 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5726 (> emacs-major-version 20))
5727 tramp-dos-coding-system))
5728 (p (apply 'start-process
5729 (tramp-buffer-name multi-method method user host)
5730 (tramp-get-buffer multi-method method user host)
5731 (tramp-get-method-parameter
5732 multi-method
5733 (tramp-find-method multi-method method user host)
5734 user host 'tramp-login-program)
5735 (mapcar
5736 (lambda (x)
5737 (format-spec x `((?u . ,(or user "root")))))
5738 (tramp-get-method-parameter
5739 multi-method
5740 (tramp-find-method multi-method method user host)
5741 user host 'tramp-login-args))))
5742 (found nil)
5743 (pw nil))
5744 (tramp-set-process-query-on-exit-flag p nil)
5745 (set-buffer (tramp-get-buffer multi-method method user host))
5746 (tramp-process-actions p multi-method method user host
5747 tramp-actions-before-shell)
5748 (tramp-open-connection-setup-interactive-shell
5749 p multi-method method user host)
5750 (tramp-post-connection multi-method method
5751 user host)))))
5753 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5754 ;; handle not giving a user name in the "file name".
5756 ;; This is more difficult than for the single-hop method. In the
5757 ;; multi-hop-method, the desired behaviour should be that the
5758 ;; user must specify names for the telnet hops of which the user
5759 ;; name is different than the "original" name (or different from
5760 ;; the previous hop.
5761 (defun tramp-open-connection-multi (multi-method method user host)
5762 "Open a multi-hop connection using METHOD.
5763 This uses a slightly changed file name syntax. The idea is to say
5764 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5765 This will use telnet to log in as u1 to h1, then use rsh from there to
5766 log in as u2 to h2."
5767 (save-match-data
5768 (unless multi-method
5769 (error "Multi-hop open connection function called on non-multi method"))
5770 (when (tramp-method-out-of-band-p multi-method method user host)
5771 (error "No out of band multi-hop connections"))
5772 (unless (and (arrayp method) (not (stringp method)))
5773 (error "METHOD must be an array of strings for multi methods"))
5774 (unless (and (arrayp user) (not (stringp user)))
5775 (error "USER must be an array of strings for multi methods"))
5776 (unless (and (arrayp host) (not (stringp host)))
5777 (error "HOST must be an array of strings for multi methods"))
5778 (unless (and (= (length method) (length user))
5779 (= (length method) (length host)))
5780 (error "Arrays METHOD, USER, HOST must have equal length"))
5781 (tramp-pre-connection multi-method method user host tramp-chunksize)
5782 (tramp-message 7 "Opening `%s' connection..." multi-method)
5783 (let ((process-environment (copy-sequence process-environment)))
5784 (setenv "TERM" tramp-terminal-type)
5785 (setenv "PS1" "$ ")
5786 (let* ((default-directory (tramp-temporary-file-directory))
5787 ;; If we omit the conditional, we use `undecided-dos' in
5788 ;; some cases. With the conditional, we use nil in these
5789 ;; cases. What's the difference? Which one is right?
5790 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5791 (> emacs-major-version 20))
5792 tramp-dos-coding-system))
5793 (p (start-process (tramp-buffer-name multi-method method user host)
5794 (tramp-get-buffer multi-method method user host)
5795 tramp-multi-sh-program))
5796 (num-hops (length method))
5797 (i 0))
5798 (tramp-set-process-query-on-exit-flag p nil)
5799 (tramp-message 9 "Waiting 60s for local shell to come up...")
5800 (unless (tramp-wait-for-regexp
5801 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5802 shell-prompt-pattern tramp-shell-prompt-pattern))
5803 (pop-to-buffer (buffer-name))
5804 (kill-process p)
5805 (error "Couldn't find local shell prompt"))
5806 ;; Now do all the connections as specified.
5807 (while (< i num-hops)
5808 (let* ((m (aref method i))
5809 (u (aref user i))
5810 (h (aref host i))
5811 (entry (assoc m tramp-multi-connection-function-alist))
5812 (multi-func (nth 1 entry))
5813 (command (nth 2 entry)))
5814 ;; The multi-funcs don't need to do save-match-data, as that
5815 ;; is done here.
5816 (funcall multi-func p m u h command)
5817 (erase-buffer)
5818 (incf i)))
5819 (tramp-open-connection-setup-interactive-shell
5820 p multi-method method user host)
5821 (tramp-post-connection multi-method method user host)))))
5823 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5824 ;; of no user name provided. Hack to make it work as it did before:
5825 ;; changed `user' to `(or user (user-login-name))' in the places where
5826 ;; the value is actually used.
5827 (defun tramp-multi-connect-telnet (p method user host command)
5828 "Issue `telnet' command.
5829 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5830 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5831 the host name, and `%n' is replaced with an end of line character, as
5832 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5833 character.
5835 If USER is nil, uses the return value of (user-login-name) instead."
5836 (let ((cmd (format-spec command
5837 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5838 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5839 found pw)
5840 (erase-buffer)
5841 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5842 (process-send-string p cmd)
5843 (tramp-process-multi-actions p method user host
5844 tramp-multi-actions)))
5846 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5847 ;; of no user name provided. Hack to make it work as it did before:
5848 ;; changed `user' to `(or user (user-login-name))' in the places where
5849 ;; the value is actually used.
5850 (defun tramp-multi-connect-rlogin (p method user host command)
5851 "Issue `rlogin' command.
5852 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5853 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5854 with the user name, `%h' will be replaced with the host name, and `%n'
5855 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5856 `%%' if you want to use a literal percent character.
5858 If USER is nil, uses the return value of (user-login-name) instead."
5859 (let ((cmd (format-spec command `((?h . ,host)
5860 (?u . ,(or user (user-login-name)))
5861 (?n . ,tramp-rsh-end-of-line))))
5862 (cmd1 (format-spec command `((?h . ,host)
5863 (?u . ,(or user (user-login-name)))
5864 (?n . ""))))
5865 found)
5866 (erase-buffer)
5867 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5868 (process-send-string p cmd)
5869 (tramp-process-multi-actions p method user host
5870 tramp-multi-actions)))
5872 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5873 ;; of no user name provided. Hack to make it work as it did before:
5874 ;; changed `user' to `(or user (user-login-name))' in the places where
5875 ;; the value is actually used.
5876 (defun tramp-multi-connect-su (p method user host command)
5877 "Issue `su' command.
5878 Uses shell COMMAND to issue a `su' command to log in as USER on
5879 HOST. The HOST name is ignored, this just changes the user id on the
5880 host currently logged in to.
5882 If USER is nil, uses the return value of (user-login-name) instead.
5884 You can use percent escapes in the COMMAND. `%u' is replaced with the
5885 user name, and `%n' is replaced with the value of
5886 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5887 character."
5888 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5889 (?n . ,tramp-rsh-end-of-line))))
5890 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5891 (?n . ""))))
5892 found)
5893 (erase-buffer)
5894 (tramp-message 9 "Sending su command `%s'" cmd1)
5895 (process-send-string p cmd)
5896 (tramp-process-multi-actions p method user host
5897 tramp-multi-actions)))
5899 ;; Utility functions.
5901 (defun tramp-accept-process-output
5902 (&optional process timeout timeout-msecs)
5903 "Like `accept-process-output' for Tramp processes.
5904 This is needed in order to hide `last-coding-system-used', which is set
5905 for process communication also."
5906 (let (last-coding-system-used)
5907 (accept-process-output process timeout timeout-msecs)))
5909 (defun tramp-wait-for-regexp (proc timeout regexp)
5910 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5911 Expects the output of PROC to be sent to the current buffer. Returns
5912 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5913 nil."
5914 (let ((found nil)
5915 (start-time (current-time)))
5916 (cond (timeout
5917 ;; Work around a bug in XEmacs 21, where the timeout
5918 ;; expires faster than it should. This degenerates
5919 ;; to polling for buggy XEmacsen, but oh, well.
5920 (while (and (not found)
5921 (< (tramp-time-diff (current-time) start-time)
5922 timeout))
5923 (with-timeout (timeout)
5924 (while (not found)
5925 (tramp-accept-process-output proc 1)
5926 (unless (memq (process-status proc) '(run open))
5927 (error "Process has died"))
5928 (goto-char (point-min))
5929 (setq found (re-search-forward regexp nil t))))))
5931 (while (not found)
5932 (tramp-accept-process-output proc 1)
5933 (unless (memq (process-status proc) '(run open))
5934 (error "Process has died"))
5935 (goto-char (point-min))
5936 (setq found (re-search-forward regexp nil t)))))
5937 (when tramp-debug-buffer
5938 (append-to-buffer
5939 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5940 tramp-current-user tramp-current-host)
5941 (point-min) (point-max))
5942 (when (not found)
5943 (save-excursion
5944 (set-buffer
5945 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5946 tramp-current-user tramp-current-host))
5947 (goto-char (point-max))
5948 (insert "[[Regexp `" regexp "' not found"
5949 (if timeout (format " in %d secs" timeout) "")
5950 "]]"))))
5951 found))
5953 (defun tramp-wait-for-shell-prompt (proc timeout)
5954 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5955 See `tramp-wait-for-regexp' for more details.
5956 Shell prompt pattern is determined by variables `shell-prompt-pattern'
5957 and `tramp-shell-prompt-pattern'."
5958 (tramp-wait-for-regexp
5959 proc timeout
5960 (format "\\(%s\\|%s\\)\\'"
5961 shell-prompt-pattern tramp-shell-prompt-pattern)))
5963 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5964 "Wait for shell prompt and barf if none appears.
5965 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5966 seconds. If not, it produces an error message with the given ERROR-ARGS."
5967 (unless (tramp-wait-for-shell-prompt proc timeout)
5968 (pop-to-buffer (buffer-name))
5969 (apply 'error error-args)))
5971 (defun tramp-enter-password (p prompt user host)
5972 "Prompt for a password and send it to the remote end.
5973 Uses PROMPT as a prompt and sends the password to process P."
5974 (let ((pw (tramp-read-passwd user host prompt)))
5975 (erase-buffer)
5976 (process-send-string
5977 p (concat pw
5978 (or (tramp-get-method-parameter
5979 tramp-current-multi-method
5980 tramp-current-method
5981 tramp-current-user
5982 tramp-current-host
5983 'tramp-password-end-of-line)
5984 tramp-default-password-end-of-line)))))
5986 ;; HHH: Not Changed. This might handle the case where USER is not
5987 ;; given in the "File name" very poorly. Then, the local
5988 ;; variable tramp-current-user will be set to nil.
5989 (defun tramp-pre-connection (multi-method method user host chunksize)
5990 "Do some setup before actually logging in.
5991 METHOD, USER and HOST specify the connection."
5992 (set-buffer (tramp-get-buffer multi-method method user host))
5993 (set (make-local-variable 'tramp-current-multi-method) multi-method)
5994 (set (make-local-variable 'tramp-current-method) method)
5995 (set (make-local-variable 'tramp-current-user) user)
5996 (set (make-local-variable 'tramp-current-host) host)
5997 (set (make-local-variable 'tramp-chunksize) chunksize)
5998 (set (make-local-variable 'inhibit-eol-conversion) nil)
5999 (erase-buffer))
6001 (defun tramp-open-connection-setup-interactive-shell
6002 (p multi-method method user host)
6003 "Set up an interactive shell.
6004 Mainly sets the prompt and the echo correctly. P is the shell process
6005 to set up. METHOD, USER and HOST specify the connection."
6006 ;; Wait a bit in case the remote end feels like sending a little
6007 ;; junk first. It seems that fencepost.gnu.org does this when doing
6008 ;; a Kerberos login.
6009 (sit-for 1)
6010 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6011 (tramp-process-initial-commands p multi-method method user host
6012 tramp-initial-commands)
6013 ;; It is useful to set the prompt in the following command because
6014 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6015 ;; about and thus /bin/sh will display a strange prompt. For
6016 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6017 ;; the current working directory but /bin/sh will display a dollar
6018 ;; sign. The following command line sets $PS1 to a sane value, and
6019 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6020 ;; Pittman reports that the unusual positioning of the single quotes
6021 ;; makes it work under `rc', too. We also unset the variable $ENV
6022 ;; because that is read by some sh implementations (eg, bash when
6023 ;; called as sh) on startup; this way, we avoid the startup file
6024 ;; clobbering $PS1.
6025 (tramp-send-command-internal
6026 multi-method method user host
6027 (format "exec env 'ENV=' 'PS1=$ ' %s"
6028 (tramp-get-method-parameter
6029 multi-method method user host 'tramp-remote-sh))
6030 (format "remote `%s' to come up"
6031 (tramp-get-method-parameter
6032 multi-method method user host 'tramp-remote-sh)))
6033 (tramp-barf-if-no-shell-prompt
6034 p 30
6035 "Remote `%s' didn't come up. See buffer `%s' for details"
6036 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
6037 (buffer-name))
6038 (tramp-message 8 "Setting up remote shell environment")
6039 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6040 (tramp-send-command-internal multi-method method user host
6041 "stty -inlcr -echo kill '^U'")
6042 (erase-buffer)
6043 ;; Ignore garbage after stty command.
6044 (tramp-send-command-internal multi-method method user host
6045 "echo foo")
6046 (erase-buffer)
6047 (tramp-send-command-internal multi-method method user host
6048 "TERM=dumb; export TERM")
6049 (erase-buffer)
6050 ;; Check whether the remote host suffers from buggy `send-process-string'.
6051 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6052 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6053 ;; Emacs makes a hack when this host type is detected locally. It cannot
6054 ;; handle remote hosts, though.
6055 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6056 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6057 (tramp-send-command-internal multi-method method user host
6058 "(uname -sr) 2>/dev/null")
6059 (goto-char (point-min))
6060 (when (looking-at "FreeBSD")
6061 (setq tramp-chunksize 500)))
6063 ;; Try to set up the coding system correctly.
6064 ;; CCC this can't be the right way to do it. Hm.
6065 (save-excursion
6066 (erase-buffer)
6067 (tramp-message 9 "Determining coding system")
6068 (tramp-send-command-internal multi-method method user host
6069 "echo foo ; echo bar")
6070 (goto-char (point-min))
6071 (if (featurep 'mule)
6072 ;; Use MULE to select the right EOL convention for communicating
6073 ;; with the process.
6074 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6075 cs-decode cs-encode)
6076 (when (symbolp cs) (setq cs (cons cs cs)))
6077 (setq cs-decode (car cs))
6078 (setq cs-encode (cdr cs))
6079 (unless cs-decode (setq cs-decode 'undecided))
6080 (unless cs-encode (setq cs-encode 'undecided))
6081 (setq cs-encode (tramp-coding-system-change-eol-conversion
6082 cs-encode 'unix))
6083 (when (search-forward "\r" nil t)
6084 (setq cs-decode (tramp-coding-system-change-eol-conversion
6085 cs-decode 'dos)))
6086 (set-buffer-process-coding-system cs-decode cs-encode))
6087 ;; Look for ^M and do something useful if found.
6088 (when (search-forward "\r" nil t)
6089 ;; We have found a ^M but cannot frob the process coding system
6090 ;; because we're running on a non-MULE Emacs. Let's try
6091 ;; stty, instead.
6092 (erase-buffer)
6093 (tramp-message 9 "Trying `stty -onlcr'")
6094 (tramp-send-command-internal multi-method method user host
6095 "stty -onlcr"))))
6096 (erase-buffer)
6097 (tramp-message
6098 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6099 (tramp-send-command-internal
6100 multi-method method user host
6101 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6102 (erase-buffer)
6103 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6104 (tramp-send-command-internal multi-method method user host
6105 "set +o vi +o emacs")
6106 (erase-buffer)
6107 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6108 (tramp-send-command-internal
6109 multi-method method user host
6110 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6111 (erase-buffer)
6112 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6113 (tramp-send-command-internal multi-method method user host
6114 "unset CDPATH")
6115 (erase-buffer)
6116 (tramp-message 9 "Setting shell prompt")
6117 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6118 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6119 ;; the last time we sent a command, to avoid tramp-send-command to send
6120 ;; "echo are you awake".
6121 (setq tramp-last-cmd-time (current-time))
6122 (tramp-send-command
6123 multi-method method user host
6124 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6125 tramp-rsh-end-of-line
6126 tramp-end-of-output
6127 tramp-rsh-end-of-line))
6128 (tramp-wait-for-output))
6130 (defun tramp-post-connection (multi-method method user host)
6131 "Prepare a remote shell before being able to work on it.
6132 METHOD, USER and HOST specify the connection.
6133 Among other things, this finds a shell which groks tilde expansion,
6134 tries to find an `ls' command which groks the `-n' option, sets the
6135 locale to C and sets up the remote shell search path."
6136 ;; Search for a good shell before searching for a command which
6137 ;; checks if a file exists. This is done because Tramp wants to use
6138 ;; "test foo; echo $?" to check if various conditions hold, and
6139 ;; there are buggy /bin/sh implementations which don't execute the
6140 ;; "echo $?" part if the "test" part has an error. In particular,
6141 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6142 ;; with buggy /bin/sh implementations will have a working bash or
6143 ;; ksh. Whee...
6144 (tramp-find-shell multi-method method user host)
6145 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6146 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6147 ;; output of (tramp-find-shell) to show up along with the output of
6148 ;; (tramp-find-ls-command) testing.
6150 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6151 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6153 ;; After much debugging I couldn't find any problem with the implementation
6154 ;; of that function though. The workaround stays for me at least. :/
6156 ;; Daniel Pittman <daniel@danann.net>
6157 (sleep-for 1)
6158 (erase-buffer)
6159 (tramp-find-file-exists-command multi-method method user host)
6160 (make-local-variable 'tramp-ls-command)
6161 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6162 (unless tramp-ls-command
6163 (tramp-message
6165 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6166 (setq tramp-ls-command
6167 (tramp-find-executable multi-method method user host
6168 "ls" tramp-remote-path nil)))
6169 (unless tramp-ls-command
6170 (error "Fatal error: Couldn't find remote executable `ls'"))
6171 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6172 tramp-ls-command)
6173 (tramp-send-command multi-method method user host
6174 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6175 "return $1" tramp-rsh-end-of-line
6176 "}"))
6177 (tramp-wait-for-output)
6178 ;; Set remote PATH variable.
6179 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6180 ;; Tell remote shell to use standard time format, needed for
6181 ;; parsing `ls -l' output.
6182 (tramp-send-command multi-method method user host
6183 "LC_TIME=C; export LC_TIME; echo huhu")
6184 (tramp-wait-for-output)
6185 (tramp-send-command multi-method method user host
6186 "mesg n; echo huhu")
6187 (tramp-wait-for-output)
6188 (tramp-send-command multi-method method user host
6189 "biff n ; echo huhu")
6190 (tramp-wait-for-output)
6191 ;; Unalias ls(1) to work around issues with those silly people who make it
6192 ;; spit out ANSI escapes or whatever.
6193 (tramp-send-command multi-method method user host
6194 "unalias ls; echo huhu")
6195 (tramp-wait-for-output)
6196 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6197 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6198 ;; for otherwise the shell crashes.
6199 (erase-buffer)
6200 (make-local-variable 'tramp-test-groks-nt)
6201 (tramp-send-command multi-method method user host
6202 "( test / -nt / )")
6203 (tramp-wait-for-output)
6204 (goto-char (point-min))
6205 (setq tramp-test-groks-nt
6206 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6207 (unless tramp-test-groks-nt
6208 (tramp-send-command
6209 multi-method method user host
6210 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6211 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6212 "}")))
6213 (tramp-wait-for-output)
6214 ;; Send the fallback `uudecode' script.
6215 (erase-buffer)
6216 (tramp-send-string multi-method method user host tramp-uudecode)
6217 (tramp-wait-for-output)
6218 ;; Find a `perl'.
6219 (erase-buffer)
6220 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6221 (let ((tramp-remote-perl
6222 (or (tramp-find-executable multi-method method user host
6223 "perl5" tramp-remote-path nil)
6224 (tramp-find-executable multi-method method user host
6225 "perl" tramp-remote-path nil))))
6226 (when tramp-remote-perl
6227 (tramp-set-connection-property "perl" tramp-remote-perl
6228 multi-method method user host)
6229 (unless (tramp-method-out-of-band-p multi-method method user host)
6230 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6231 (tramp-send-string
6232 multi-method method user host
6233 (concat "tramp_encode () {\n"
6234 (format tramp-perl-encode tramp-remote-perl)
6235 " 2>/dev/null"
6236 "\n}"))
6237 (tramp-wait-for-output)
6238 (tramp-send-string
6239 multi-method method user host
6240 (concat "tramp_encode_with_module () {\n"
6241 (format tramp-perl-encode-with-module tramp-remote-perl)
6242 " 2>/dev/null"
6243 "\n}"))
6244 (tramp-wait-for-output)
6245 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6246 (tramp-send-string
6247 multi-method method user host
6248 (concat "tramp_decode () {\n"
6249 (format tramp-perl-decode tramp-remote-perl)
6250 " 2>/dev/null"
6251 "\n}"))
6252 (tramp-wait-for-output)
6253 (tramp-send-string
6254 multi-method method user host
6255 (concat "tramp_decode_with_module () {\n"
6256 (format tramp-perl-decode-with-module tramp-remote-perl)
6257 " 2>/dev/null"
6258 "\n}"))
6259 (tramp-wait-for-output))))
6260 ;; Find ln(1)
6261 (erase-buffer)
6262 (let ((ln (tramp-find-executable multi-method method user host
6263 "ln" tramp-remote-path nil)))
6264 (when ln
6265 (tramp-set-connection-property "ln" ln multi-method method user host)))
6266 ;; Set uid and gid.
6267 (erase-buffer)
6268 (tramp-send-command multi-method method user host "id -u; id -g")
6269 (tramp-wait-for-output)
6270 (goto-char (point-min))
6271 (tramp-set-connection-property
6272 "uid" (read (current-buffer)) multi-method method user host)
6273 (tramp-set-connection-property
6274 "gid" (read (current-buffer)) multi-method method user host)
6275 ;; Find the right encoding/decoding commands to use.
6276 (erase-buffer)
6277 (unless (tramp-method-out-of-band-p multi-method method user host)
6278 (tramp-find-inline-encoding multi-method method user host))
6279 ;; If encoding/decoding command are given, test to see if they work.
6280 ;; CCC: Maybe it would be useful to run the encoder both locally and
6281 ;; remotely to see if they produce the same result.
6282 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6283 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6284 (magic-string "xyzzy"))
6285 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6286 (tramp-kill-process multi-method method user host)
6287 ;; Improve error message and/or error check.
6288 (error
6289 "Must give both decoding and encoding command in method definition"))
6290 (when (and rem-enc rem-dec)
6291 (tramp-message
6293 "Checking to see if encoding/decoding commands work on remote host...")
6294 (tramp-send-command
6295 multi-method method user host
6296 (format "echo %s | %s | %s"
6297 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6298 (tramp-wait-for-output)
6299 (unless (looking-at (regexp-quote magic-string))
6300 (tramp-kill-process multi-method method user host)
6301 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6302 (buffer-name)))
6303 (erase-buffer)
6304 (tramp-message
6305 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6307 ;; CCC: We should either implement a Perl version of base64 encoding
6308 ;; and decoding. Then we just use that in the last item. The other
6309 ;; alternative is to use the Perl version of UU encoding. But then
6310 ;; we need a Lisp version of uuencode.
6312 ;; Old text from documentation of tramp-methods:
6313 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6314 ;; of the base64 methods instead since base64 encoding is much more
6315 ;; reliable and the commands are more standardized between the different
6316 ;; Unix versions. But if you can't use base64 for some reason, please
6317 ;; note that the default uudecode command does not work well for some
6318 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6319 ;; the following command for uudecode:
6321 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6323 ;; For Irix, no solution is known yet.
6325 (defvar tramp-coding-commands
6326 '(("mimencode -b" "mimencode -u -b"
6327 base64-encode-region base64-decode-region)
6328 ("mmencode -b" "mmencode -u -b"
6329 base64-encode-region base64-decode-region)
6330 ("recode data..base64" "recode base64..data"
6331 base64-encode-region base64-decode-region)
6332 ("uuencode xxx" "uudecode -o /dev/stdout"
6333 tramp-uuencode-region uudecode-decode-region)
6334 ("uuencode xxx" "uudecode -o -"
6335 tramp-uuencode-region uudecode-decode-region)
6336 ("uuencode xxx" "uudecode -p"
6337 tramp-uuencode-region uudecode-decode-region)
6338 ("uuencode xxx" "tramp_uudecode"
6339 tramp-uuencode-region uudecode-decode-region)
6340 ("tramp_encode_with_module" "tramp_decode_with_module"
6341 base64-encode-region base64-decode-region)
6342 ("tramp_encode" "tramp_decode"
6343 base64-encode-region base64-decode-region))
6344 "List of coding commands for inline transfer.
6345 Each item is a list that looks like this:
6347 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6349 The REMOTE-ENCODING should be a string, giving a command accepting a
6350 plain file on standard input and writing the encoded file to standard
6351 output. The REMOTE-DECODING should also be a string, giving a command
6352 accepting an encoded file on standard input and writing the decoded
6353 file to standard output.
6355 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6356 symbols, giving functions. If they are strings, then they can contain
6357 the \"%s\" format specifier. If that specifier is present, the input
6358 filename will be put into the command line at that spot. If the
6359 specifier is not present, the input should be read from standard
6360 input.
6362 If they are functions, they will be called with two arguments, start
6363 and end of region, and are expected to replace the region contents
6364 with the encoded or decoded results, respectively.")
6366 (defun tramp-find-inline-encoding (multi-method method user host)
6367 "Find an inline transfer encoding that works.
6368 Goes through the list `tramp-coding-commands'."
6369 (let ((commands tramp-coding-commands)
6370 (magic "xyzzy")
6371 item found)
6372 (while (and commands (null found))
6373 (setq item (pop commands))
6374 (catch 'wont-work
6375 (let ((rem-enc (nth 0 item))
6376 (rem-dec (nth 1 item))
6377 (loc-enc (nth 2 item))
6378 (loc-dec (nth 3 item)))
6379 ;; Check if remote encoding and decoding commands can be
6380 ;; called remotely with null input and output. This makes
6381 ;; sure there are no syntax errors and the command is really
6382 ;; found. Note that we do not redirect stdout to /dev/null,
6383 ;; for two reaons: when checking the decoding command, we
6384 ;; actually check the output it gives. And also, when
6385 ;; redirecting "mimencode" output to /dev/null, then as root
6386 ;; it might change the permissions of /dev/null!
6387 (tramp-message-for-buffer
6388 multi-method method user host 9
6389 "Checking remote encoding command `%s' for sanity" rem-enc)
6390 (unless (zerop (tramp-send-command-and-check
6391 multi-method method user host
6392 (format "%s </dev/null" rem-enc) t))
6393 (throw 'wont-work nil))
6394 (tramp-message-for-buffer
6395 multi-method method user host 9
6396 "Checking remote decoding command `%s' for sanity" rem-dec)
6397 (unless (zerop (tramp-send-command-and-check
6398 multi-method method user host
6399 (format "echo %s | %s | %s"
6400 magic rem-enc rem-dec) t))
6401 (throw 'wont-work nil))
6402 (save-excursion
6403 (goto-char (point-min))
6404 (unless (looking-at (regexp-quote magic))
6405 (throw 'wont-work nil)))
6406 ;; If the local encoder or decoder is a string, the
6407 ;; corresponding command has to work locally.
6408 (when (stringp loc-enc)
6409 (tramp-message-for-buffer
6410 multi-method method user host 9
6411 "Checking local encoding command `%s' for sanity" loc-enc)
6412 (unless (zerop (tramp-call-local-coding-command
6413 loc-enc nil nil))
6414 (throw 'wont-work nil)))
6415 (when (stringp loc-dec)
6416 (tramp-message-for-buffer
6417 multi-method method user host 9
6418 "Checking local decoding command `%s' for sanity" loc-dec)
6419 (unless (zerop (tramp-call-local-coding-command
6420 loc-dec nil nil))
6421 (throw 'wont-work nil)))
6422 ;; CCC: At this point, maybe we should check that the output
6423 ;; of the commands is correct. But for the moment we will
6424 ;; assume that commands working on empty input will also
6425 ;; work in practice.
6426 (setq found item))))
6427 ;; Did we find something? If not, issue error. If so,
6428 ;; set connection properties.
6429 (unless found
6430 (error "Couldn't find an inline transfer encoding"))
6431 (let ((rem-enc (nth 0 found))
6432 (rem-dec (nth 1 found))
6433 (loc-enc (nth 2 found))
6434 (loc-dec (nth 3 found)))
6435 (tramp-message 10 "Using remote encoding %s" rem-enc)
6436 (tramp-set-remote-encoding multi-method method user host rem-enc)
6437 (tramp-message 10 "Using remote decoding %s" rem-dec)
6438 (tramp-set-remote-decoding multi-method method user host rem-dec)
6439 (tramp-message 10 "Using local encoding %s" loc-enc)
6440 (tramp-set-local-encoding multi-method method user host loc-enc)
6441 (tramp-message 10 "Using local decoding %s" loc-dec)
6442 (tramp-set-local-decoding multi-method method user host loc-dec))))
6444 (defun tramp-call-local-coding-command (cmd input output)
6445 "Call the local encoding or decoding command.
6446 If CMD contains \"%s\", provide input file INPUT there in command.
6447 Otherwise, INPUT is passed via standard input.
6448 INPUT can also be nil which means `/dev/null'.
6449 OUTPUT can be a string (which specifies a filename), or t (which
6450 means standard output and thus the current buffer), or nil (which
6451 means discard it)."
6452 (call-process
6453 tramp-encoding-shell ;program
6454 (when (and input (not (string-match "%s" cmd)))
6455 input) ;input
6456 (if (eq output t) t nil) ;output
6457 nil ;redisplay
6458 tramp-encoding-command-switch
6459 ;; actual shell command
6460 (concat
6461 (if (string-match "%s" cmd) (format cmd input) cmd)
6462 (if (stringp output) (concat "> " output) ""))))
6464 (defun tramp-maybe-open-connection (multi-method method user host)
6465 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6466 Does not do anything if a connection is already open, but re-opens the
6467 connection if a previous connection has died for some reason."
6468 (let ((p (get-buffer-process
6469 (tramp-get-buffer multi-method method user host)))
6470 last-cmd-time)
6471 ;; If too much time has passed since last command was sent, look
6472 ;; whether process is still alive. If it isn't, kill it. When
6473 ;; using ssh, it can sometimes happen that the remote end has hung
6474 ;; up but the local ssh client doesn't recognize this until it
6475 ;; tries to send some data to the remote end. So that's why we
6476 ;; try to send a command from time to time, then look again
6477 ;; whether the process is really alive.
6478 (save-excursion
6479 (set-buffer (tramp-get-buffer multi-method method user host))
6480 (when (and tramp-last-cmd-time
6481 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6482 p (processp p) (memq (process-status p) '(run open)))
6483 (tramp-send-command
6484 multi-method method user host "echo are you awake" nil t)
6485 (unless (tramp-wait-for-output 10)
6486 (delete-process p)
6487 (setq p nil))
6488 (erase-buffer)))
6489 (unless (and p (processp p) (memq (process-status p) '(run open)))
6490 (when (and p (processp p))
6491 (delete-process p))
6492 (let ((process-connection-type tramp-process-connection-type))
6493 (funcall (tramp-get-method-parameter
6494 multi-method
6495 (tramp-find-method multi-method method user host)
6496 user host 'tramp-connection-function)
6497 multi-method method user host)))))
6499 (defun tramp-send-command
6500 (multi-method method user host command &optional noerase neveropen)
6501 "Send the COMMAND to USER at HOST (logged in using METHOD).
6502 Erases temporary buffer before sending the command (unless NOERASE
6503 is true).
6504 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6505 connection. This is meant to be used from
6506 `tramp-maybe-open-connection' only."
6507 (or neveropen
6508 (tramp-maybe-open-connection multi-method method user host))
6509 (setq tramp-last-cmd-time (current-time))
6510 (setq tramp-last-cmd command)
6511 (when tramp-debug-buffer
6512 (save-excursion
6513 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6514 (goto-char (point-max))
6515 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6516 (let ((proc nil))
6517 (set-buffer (tramp-get-buffer multi-method method user host))
6518 (unless noerase (erase-buffer))
6519 (setq proc (get-buffer-process (current-buffer)))
6520 (process-send-string proc
6521 (concat command tramp-rsh-end-of-line))))
6523 (defun tramp-send-command-internal
6524 (multi-method method user host command &optional msg)
6525 "Send command to remote host and wait for success.
6526 Sends COMMAND, then waits 30 seconds for shell prompt."
6527 (tramp-send-command multi-method method user host command t t)
6528 (when msg
6529 (tramp-message 9 "Waiting 30s for %s..." msg))
6530 (tramp-barf-if-no-shell-prompt
6531 nil 30
6532 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6534 (defun tramp-wait-for-output (&optional timeout)
6535 "Wait for output from remote rsh command."
6536 (let ((proc (get-buffer-process (current-buffer)))
6537 (found nil)
6538 (start-time (current-time))
6539 (start-point (point))
6540 (end-of-output (concat "^"
6541 (regexp-quote tramp-end-of-output)
6542 "\r?$")))
6543 ;; Algorithm: get waiting output. See if last line contains
6544 ;; end-of-output sentinel. If not, wait a bit and again get
6545 ;; waiting output. Repeat until timeout expires or end-of-output
6546 ;; sentinel is seen. Will hang if timeout is nil and
6547 ;; end-of-output sentinel never appears.
6548 (save-match-data
6549 (cond (timeout
6550 ;; Work around an XEmacs bug, where the timeout expires
6551 ;; faster than it should. This degenerates into polling
6552 ;; for buggy XEmacsen, but oh, well.
6553 (while (and (not found)
6554 (< (tramp-time-diff (current-time) start-time)
6555 timeout))
6556 (with-timeout (timeout)
6557 (while (not found)
6558 (tramp-accept-process-output proc 1)
6559 (unless (memq (process-status proc) '(run open))
6560 (error "Process has died"))
6561 (goto-char (point-max))
6562 (forward-line -1)
6563 (setq found (looking-at end-of-output))))))
6565 (while (not found)
6566 (tramp-accept-process-output proc 1)
6567 (unless (memq (process-status proc) '(run open))
6568 (error "Process has died"))
6569 (goto-char (point-max))
6570 (forward-line -1)
6571 (setq found (looking-at end-of-output))))))
6572 ;; At this point, either the timeout has expired or we have found
6573 ;; the end-of-output sentinel.
6574 (when found
6575 (goto-char (point-max))
6576 (forward-line -2)
6577 (delete-region (point) (point-max)))
6578 ;; If processing echoes, look for it in the first line and delete.
6579 (when tramp-process-echoes
6580 (save-excursion
6581 (goto-char start-point)
6582 (when (looking-at (regexp-quote tramp-last-cmd))
6583 (delete-region (point) (progn (forward-line 1) (point))))))
6584 ;; Add output to debug buffer if appropriate.
6585 (when tramp-debug-buffer
6586 (append-to-buffer
6587 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6588 tramp-current-user tramp-current-host)
6589 (point-min) (point-max))
6590 (when (not found)
6591 (save-excursion
6592 (set-buffer
6593 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6594 tramp-current-user tramp-current-host))
6595 (goto-char (point-max))
6596 (insert "[[Remote prompt `" end-of-output "' not found"
6597 (if timeout (format " in %d secs" timeout) "")
6598 "]]"))))
6599 (goto-char (point-min))
6600 ;; Return value is whether end-of-output sentinel was found.
6601 found))
6603 (defun tramp-send-command-and-check (multi-method method user host command
6604 &optional subshell)
6605 "Run COMMAND and check its exit status.
6606 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6607 Sends `echo $?' along with the COMMAND for checking the exit status. If
6608 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6610 If the optional argument SUBSHELL is non-nil, the command is executed in
6611 a subshell, ie surrounded by parentheses."
6612 (tramp-send-command multi-method method user host
6613 (concat (if subshell "( " "")
6614 command
6615 (if command " 2>/dev/null; " "")
6616 "echo tramp_exit_status $?"
6617 (if subshell " )" " ")))
6618 (tramp-wait-for-output)
6619 (goto-char (point-max))
6620 (unless (search-backward "tramp_exit_status " nil t)
6621 (error "Couldn't find exit status of `%s'" command))
6622 (skip-chars-forward "^ ")
6623 (read (current-buffer)))
6625 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6626 signal fmt &rest args)
6627 "Run COMMAND, check exit status, throw error if exit status not okay.
6628 Similar to `tramp-send-command-and-check' but accepts two more arguments
6629 FMT and ARGS which are passed to `error'."
6630 (unless (zerop (tramp-send-command-and-check
6631 multi-method method user host command subshell))
6632 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6633 ;; silent.
6634 (pop-to-buffer (current-buffer))
6635 (funcall 'signal signal (apply 'format fmt args))))
6637 ;; It seems that Tru64 Unix does not like it if long strings are sent
6638 ;; to it in one go. (This happens when sending the Perl
6639 ;; `file-attributes' implementation, for instance.) Therefore, we
6640 ;; have this function which waits a bit at each line.
6641 (defun tramp-send-string
6642 (multi-method method user host string)
6643 "Send the STRING to USER at HOST using METHOD.
6645 The STRING is expected to use Unix line-endings, but the lines sent to
6646 the remote host use line-endings as defined in the variable
6647 `tramp-rsh-end-of-line'."
6648 (let ((proc (get-buffer-process
6649 (tramp-get-buffer multi-method method user host))))
6650 (unless proc
6651 (error "Can't send string to remote host -- not logged in"))
6652 ;; debug message
6653 (when tramp-debug-buffer
6654 (save-excursion
6655 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6656 (goto-char (point-max))
6657 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6658 ;; replace "\n" by `tramp-rsh-end-of-line'
6659 (setq string
6660 (mapconcat 'identity
6661 (split-string string "\n")
6662 tramp-rsh-end-of-line))
6663 (unless (or (string= string "")
6664 (string-equal (substring string -1) tramp-rsh-end-of-line))
6665 (setq string (concat string tramp-rsh-end-of-line)))
6666 ;; send the string
6667 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6668 (let ((pos 0)
6669 (end (length string)))
6670 (while (< pos end)
6671 (tramp-message-for-buffer
6672 multi-method method user host 10
6673 "Sending chunk from %s to %s"
6674 pos (min (+ pos tramp-chunksize) end))
6675 (process-send-string
6676 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6677 (setq pos (+ pos tramp-chunksize))
6678 (sleep-for 0.1)))
6679 (process-send-string proc string))))
6681 (defun tramp-send-eof (multi-method method user host)
6682 "Send EOF to the remote end.
6683 METHOD, HOST and USER specify the connection."
6684 (let ((proc (get-buffer-process
6685 (tramp-get-buffer multi-method method user host))))
6686 (unless proc
6687 (error "Can't send EOF to remote host -- not logged in"))
6688 (process-send-eof proc)))
6689 ; (process-send-string proc "\^D")))
6691 (defun tramp-kill-process (multi-method method user host)
6692 "Kill the connection process used by Tramp.
6693 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6694 (let ((proc (get-buffer-process
6695 (tramp-get-buffer multi-method method user host))))
6696 (kill-process proc)))
6698 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6699 "Erase buffer, then discard subsequent garbage.
6700 If `tramp-discard-garbage' is nil, just erase buffer."
6701 (if (not tramp-discard-garbage)
6702 (erase-buffer)
6703 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6704 (when tramp-debug-buffer
6705 (save-excursion
6706 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6707 (goto-char (point-max))
6708 (tramp-insert-with-face
6709 'bold (format "Additional characters detected\n")))))))
6711 (defun tramp-mode-string-to-int (mode-string)
6712 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6713 (let* ((mode-chars (string-to-vector mode-string))
6714 (owner-read (aref mode-chars 1))
6715 (owner-write (aref mode-chars 2))
6716 (owner-execute-or-setid (aref mode-chars 3))
6717 (group-read (aref mode-chars 4))
6718 (group-write (aref mode-chars 5))
6719 (group-execute-or-setid (aref mode-chars 6))
6720 (other-read (aref mode-chars 7))
6721 (other-write (aref mode-chars 8))
6722 (other-execute-or-sticky (aref mode-chars 9)))
6723 (save-match-data
6724 (logior
6725 (case owner-read
6726 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6727 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6728 (case owner-write
6729 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6730 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6731 (case owner-execute-or-setid
6732 (?x (tramp-octal-to-decimal "00100"))
6733 (?S (tramp-octal-to-decimal "04000"))
6734 (?s (tramp-octal-to-decimal "04100"))
6735 (?- 0)
6736 (t (error "Fourth char `%c' must be one of `xsS-'"
6737 owner-execute-or-setid)))
6738 (case group-read
6739 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6740 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6741 (case group-write
6742 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6743 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6744 (case group-execute-or-setid
6745 (?x (tramp-octal-to-decimal "00010"))
6746 (?S (tramp-octal-to-decimal "02000"))
6747 (?s (tramp-octal-to-decimal "02010"))
6748 (?- 0)
6749 (t (error "Seventh char `%c' must be one of `xsS-'"
6750 group-execute-or-setid)))
6751 (case other-read
6752 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6753 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6754 (case other-write
6755 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6756 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6757 (case other-execute-or-sticky
6758 (?x (tramp-octal-to-decimal "00001"))
6759 (?T (tramp-octal-to-decimal "01000"))
6760 (?t (tramp-octal-to-decimal "01001"))
6761 (?- 0)
6762 (t (error "Tenth char `%c' must be one of `xtT-'"
6763 other-execute-or-sticky)))))))
6765 (defun tramp-convert-file-attributes (multi-method method user host attr)
6766 "Convert file-attributes ATTR generated by perl script or ls.
6767 Convert file mode bits to string and set virtual device number.
6768 Return ATTR."
6769 (unless (stringp (nth 8 attr))
6770 ;; Convert file mode bits to string.
6771 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6772 ;; Set file's gid change bit.
6773 (setcar (nthcdr 9 attr)
6774 (not (= (nth 3 attr)
6775 (tramp-get-remote-gid multi-method method user host))))
6776 ;; Set virtual device number.
6777 (setcar (nthcdr 11 attr)
6778 (tramp-get-device multi-method method user host))
6779 attr)
6781 (defun tramp-get-device (multi-method method user host)
6782 "Returns the virtual device number.
6783 If it doesn't exist, generate a new one."
6784 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6785 (unless (assoc string tramp-devices)
6786 (add-to-list 'tramp-devices
6787 (list string (length tramp-devices))))
6788 (list -1 (nth 1 (assoc string tramp-devices)))))
6790 (defun tramp-file-mode-from-int (mode)
6791 "Turn an integer representing a file mode into an ls(1)-like string."
6792 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6793 (user (logand (lsh mode -6) 7))
6794 (group (logand (lsh mode -3) 7))
6795 (other (logand (lsh mode -0) 7))
6796 (suid (> (logand (lsh mode -9) 4) 0))
6797 (sgid (> (logand (lsh mode -9) 2) 0))
6798 (sticky (> (logand (lsh mode -9) 1) 0)))
6799 (setq user (tramp-file-mode-permissions user suid "s"))
6800 (setq group (tramp-file-mode-permissions group sgid "s"))
6801 (setq other (tramp-file-mode-permissions other sticky "t"))
6802 (concat type user group other)))
6805 (defun tramp-file-mode-permissions (perm suid suid-text)
6806 "Convert a permission bitset into a string.
6807 This is used internally by `tramp-file-mode-from-int'."
6808 (let ((r (> (logand perm 4) 0))
6809 (w (> (logand perm 2) 0))
6810 (x (> (logand perm 1) 0)))
6811 (concat (or (and r "r") "-")
6812 (or (and w "w") "-")
6813 (or (and suid x suid-text) ; suid, execute
6814 (and suid (upcase suid-text)) ; suid, !execute
6815 (and x "x") "-")))) ; !suid
6818 (defun tramp-decimal-to-octal (i)
6819 "Return a string consisting of the octal digits of I.
6820 Not actually used. Use `(format \"%o\" i)' instead?"
6821 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6822 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6823 ((zerop i) "0")
6824 (t (concat (tramp-decimal-to-octal (/ i 8))
6825 (number-to-string (% i 8))))))
6828 ;;(defun tramp-octal-to-decimal (ostr)
6829 ;; "Given a string of octal digits, return a decimal number."
6830 ;; (cond ((null ostr) 0)
6831 ;; ((string= "" ostr) 0)
6832 ;; (t (let ((last (aref ostr (1- (length ostr))))
6833 ;; (rest (substring ostr 0 (1- (length ostr)))))
6834 ;; (unless (and (>= last ?0)
6835 ;; (<= last ?7))
6836 ;; (error "Not an octal digit: %c" last))
6837 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6838 ;; Kudos to Gerd Moellmann for this suggestion.
6839 (defun tramp-octal-to-decimal (ostr)
6840 "Given a string of octal digits, return a decimal number."
6841 (let ((x (or ostr "")))
6842 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6843 (unless (string-match "\\`[0-7]*\\'" x)
6844 (error "Non-octal junk in string `%s'" x))
6845 (string-to-number ostr 8)))
6847 (defun tramp-shell-case-fold (string)
6848 "Converts STRING to shell glob pattern which ignores case."
6849 (mapconcat
6850 (lambda (c)
6851 (if (equal (downcase c) (upcase c))
6852 (vector c)
6853 (format "[%c%c]" (downcase c) (upcase c))))
6854 string
6855 ""))
6858 ;; ------------------------------------------------------------
6859 ;; -- TRAMP file names --
6860 ;; ------------------------------------------------------------
6861 ;; Conversion functions between external representation and
6862 ;; internal data structure. Convenience functions for internal
6863 ;; data structure.
6865 (defstruct tramp-file-name multi-method method user host localname)
6867 (defun tramp-tramp-file-p (name)
6868 "Return t iff NAME is a tramp file."
6869 (save-match-data
6870 (string-match tramp-file-name-regexp name)))
6872 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6873 ;; to the `user' part of the structure if a user name was not
6874 ;; provided, now it assigns nil.
6875 (defun tramp-dissect-file-name (name)
6876 "Return an `tramp-file-name' structure.
6877 The structure consists of remote method, remote user, remote host and
6878 localname (file name on remote host)."
6879 (save-match-data
6880 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6881 (method
6882 ; single-hop
6883 (if match (match-string (nth 1 tramp-file-name-structure) name)
6884 ; maybe multi-hop
6885 (string-match
6886 (format (nth 0 tramp-multi-file-name-structure)
6887 (nth 0 tramp-multi-file-name-hop-structure)) name)
6888 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6889 (if (and method (member method tramp-multi-methods))
6890 ;; If it's a multi method, the file name structure contains
6891 ;; arrays of method, user and host.
6892 (tramp-dissect-multi-file-name name)
6893 ;; Normal method. First, find out default method.
6894 (unless match (error "Not a tramp file name: %s" name))
6895 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6896 (host (match-string (nth 3 tramp-file-name-structure) name))
6897 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6898 (make-tramp-file-name
6899 :multi-method nil
6900 :method method
6901 :user (or user nil)
6902 :host host
6903 :localname localname))))))
6905 (defun tramp-find-default-method (user host)
6906 "Look up the right method to use in `tramp-default-method-alist'."
6907 (let ((choices tramp-default-method-alist)
6908 (method tramp-default-method)
6909 item)
6910 (while choices
6911 (setq item (pop choices))
6912 (when (and (string-match (nth 0 item) (or host ""))
6913 (string-match (nth 1 item) (or user "")))
6914 (setq method (nth 2 item))
6915 (setq choices nil)))
6916 method))
6918 (defun tramp-find-method (multi-method method user host)
6919 "Return the right method string to use.
6920 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6921 If both MULTI-METHOD and METHOD are nil, do a lookup in
6922 `tramp-default-method-alist'."
6923 (or multi-method method (tramp-find-default-method user host)))
6925 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6926 ;; a user name is not provided in the "file name" very well.
6927 (defun tramp-dissect-multi-file-name (name)
6928 "Not implemented yet."
6929 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6930 (method-index (nth 1 tramp-multi-file-name-structure))
6931 (hops-index (nth 2 tramp-multi-file-name-structure))
6932 (localname-index (nth 3 tramp-multi-file-name-structure))
6933 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6934 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6935 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6936 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
6937 method hops len hop-methods hop-users hop-hosts localname)
6938 (unless (string-match (format regexp hop-regexp) name)
6939 (error "Not a multi tramp file name: %s" name))
6940 (setq method (match-string method-index name))
6941 (setq hops (match-string hops-index name))
6942 (setq len (/ (length (match-data t)) 2))
6943 (when (< localname-index 0) (incf localname-index len))
6944 (setq localname (match-string localname-index name))
6945 (let ((index 0))
6946 (while (string-match hop-regexp hops index)
6947 (setq index (match-end 0))
6948 (setq hop-methods
6949 (cons (match-string hop-method-index hops) hop-methods))
6950 (setq hop-users
6951 (cons (match-string hop-user-index hops) hop-users))
6952 (setq hop-hosts
6953 (cons (match-string hop-host-index hops) hop-hosts))))
6954 (make-tramp-file-name
6955 :multi-method method
6956 :method (apply 'vector (reverse hop-methods))
6957 :user (apply 'vector (reverse hop-users))
6958 :host (apply 'vector (reverse hop-hosts))
6959 :localname localname)))
6961 (defun tramp-make-tramp-file-name (multi-method method user host localname)
6962 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
6963 (if multi-method
6964 (tramp-make-tramp-multi-file-name multi-method method user host localname)
6965 (format-spec
6966 (concat tramp-prefix-format
6967 (when method (concat "%m" tramp-postfix-single-method-format))
6968 (when user (concat "%u" tramp-postfix-user-format))
6969 (when host (concat "%h" tramp-postfix-host-format))
6970 (when localname (concat "%p")))
6971 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
6973 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6974 ;; with this when USER is nil?
6975 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
6976 "Constructs a tramp file name for a multi-hop method."
6977 (unless tramp-make-multi-tramp-file-format
6978 (error "`tramp-make-multi-tramp-file-format' is nil"))
6979 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6980 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
6981 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
6982 (prefix (format-spec prefix-format `((?m . ,multi-method))))
6983 (hops "")
6984 (localname (format-spec localname-format `((?p . ,localname))))
6985 (i 0)
6986 (len (length method)))
6987 (while (< i len)
6988 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
6989 (setq hops (concat hops (format-spec hop-format
6990 `((?m . ,m) (?u . ,u) (?h . ,h)))))
6991 (incf i)))
6992 (concat prefix hops localname)))
6994 (defun tramp-make-copy-program-file-name (user host localname)
6995 "Create a file name suitable to be passed to `rcp' and workalikes."
6996 (if user
6997 (format "%s@%s:%s" user host localname)
6998 (format "%s:%s" host localname)))
7000 (defun tramp-method-out-of-band-p (multi-method method user host)
7001 "Return t if this is an out-of-band method, nil otherwise."
7002 (tramp-get-method-parameter
7003 multi-method
7004 (tramp-find-method multi-method method user host)
7005 user host 'tramp-copy-program))
7007 ;; Variables local to connection.
7009 (defun tramp-get-ls-command (multi-method method user host)
7010 (save-excursion
7011 (tramp-maybe-open-connection multi-method method user host)
7012 (set-buffer (tramp-get-buffer multi-method method user host))
7013 tramp-ls-command))
7015 (defun tramp-get-test-groks-nt (multi-method method user host)
7016 (save-excursion
7017 (tramp-maybe-open-connection multi-method method user host)
7018 (set-buffer (tramp-get-buffer multi-method method user host))
7019 tramp-test-groks-nt))
7021 (defun tramp-get-file-exists-command (multi-method method user host)
7022 (save-excursion
7023 (tramp-maybe-open-connection multi-method method user host)
7024 (set-buffer (tramp-get-buffer multi-method method user host))
7025 tramp-file-exists-command))
7027 (defun tramp-get-remote-perl (multi-method method user host)
7028 (tramp-get-connection-property "perl" nil multi-method method user host))
7030 (defun tramp-get-remote-ln (multi-method method user host)
7031 (tramp-get-connection-property "ln" nil multi-method method user host))
7033 (defun tramp-get-remote-uid (multi-method method user host)
7034 (tramp-get-connection-property "uid" nil multi-method method user host))
7036 (defun tramp-get-remote-gid (multi-method method user host)
7037 (tramp-get-connection-property "gid" nil multi-method method user host))
7039 ;; Get a property of a TRAMP connection.
7040 (defun tramp-get-connection-property
7041 (property default multi-method method user host)
7042 "Get the named property for the connection.
7043 If the value is not set for the connection, return `default'"
7044 (tramp-maybe-open-connection multi-method method user host)
7045 (with-current-buffer (tramp-get-buffer multi-method method user host)
7046 (let (error)
7047 (condition-case nil
7048 (symbol-value (intern (concat "tramp-connection-property-" property)))
7049 (error default)))))
7051 ;; Set a property of a TRAMP connection.
7052 (defun tramp-set-connection-property
7053 (property value multi-method method user host)
7054 "Set the named property of a TRAMP connection."
7055 (tramp-maybe-open-connection multi-method method user host)
7056 (with-current-buffer (tramp-get-buffer multi-method method user host)
7057 (set (make-local-variable
7058 (intern (concat "tramp-connection-property-" property)))
7059 value)))
7061 ;; Some predefined connection properties.
7062 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7063 (tramp-set-connection-property "remote-encoding" rem-enc
7064 multi-method method user host))
7065 (defun tramp-get-remote-encoding (multi-method method user host)
7066 (tramp-get-connection-property "remote-encoding" nil
7067 multi-method method user host))
7069 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7070 (tramp-set-connection-property "remote-decoding" rem-dec
7071 multi-method method user host))
7072 (defun tramp-get-remote-decoding (multi-method method user host)
7073 (tramp-get-connection-property "remote-decoding" nil
7074 multi-method method user host))
7076 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
7077 (tramp-set-connection-property "local-encoding" loc-enc
7078 multi-method method user host))
7079 (defun tramp-get-local-encoding (multi-method method user host)
7080 (tramp-get-connection-property "local-encoding" nil
7081 multi-method method user host))
7083 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
7084 (tramp-set-connection-property "local-decoding" loc-dec
7085 multi-method method user host))
7086 (defun tramp-get-local-decoding (multi-method method user host)
7087 (tramp-get-connection-property "local-decoding" nil
7088 multi-method method user host))
7090 (defun tramp-get-method-parameter (multi-method method user host param)
7091 "Return the method parameter PARAM.
7092 If the `tramp-methods' entry does not exist, use the variable PARAM
7093 as default."
7094 (unless (boundp param)
7095 (error "Non-existing method parameter `%s'" param))
7096 (let ((entry (assoc param
7097 (assoc (tramp-find-method multi-method method user host)
7098 tramp-methods))))
7099 (if entry
7100 (second entry)
7101 (symbol-value param))))
7104 ;; Auto saving to a special directory.
7106 (defun tramp-exists-file-name-handler (operation &rest args)
7107 (let ((buffer-file-name "/")
7108 (fnha file-name-handler-alist)
7109 (check-file-name-operation operation)
7110 (file-name-handler-alist
7111 (list
7112 (cons "/"
7113 '(lambda (operation &rest args)
7114 "Returns OPERATION if it is the one to be checked"
7115 (if (equal check-file-name-operation operation)
7116 operation
7117 (let ((file-name-handler-alist fnha))
7118 (apply operation args))))))))
7119 (eq (apply operation args) operation)))
7121 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7122 (defadvice make-auto-save-file-name
7123 (around tramp-advice-make-auto-save-file-name () activate)
7124 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7125 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7126 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7127 ad-do-it))
7128 (add-hook 'tramp-unload-hook
7129 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
7131 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7132 ;; permission 0666 minus umask. This is a security threat.
7134 (defun tramp-set-auto-save-file-modes ()
7135 "Set permissions of autosaved remote files to the original permissions."
7136 (let ((bfn (buffer-file-name)))
7137 (when (and (stringp bfn)
7138 (tramp-tramp-file-p bfn)
7139 (stringp buffer-auto-save-file-name)
7140 (not (equal bfn buffer-auto-save-file-name)))
7141 (unless (file-exists-p buffer-auto-save-file-name)
7142 (write-region "" nil buffer-auto-save-file-name))
7143 ;; Permissions should be set always, because there might be an old
7144 ;; auto-saved file belonging to another original file. This could
7145 ;; be a security threat.
7146 (set-file-modes buffer-auto-save-file-name
7147 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7149 (unless (or (> emacs-major-version 21)
7150 (and (featurep 'xemacs)
7151 (= emacs-major-version 21)
7152 (> emacs-minor-version 4)))
7153 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7154 (add-hook 'tramp-unload-hook
7155 '(lambda ()
7156 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
7158 (defun tramp-subst-strs-in-string (alist string)
7159 "Replace all occurrences of the string FROM with TO in STRING.
7160 ALIST is of the form ((FROM . TO) ...)."
7161 (save-match-data
7162 (while alist
7163 (let* ((pr (car alist))
7164 (from (car pr))
7165 (to (cdr pr)))
7166 (while (string-match (regexp-quote from) string)
7167 (setq string (replace-match to t t string)))
7168 (setq alist (cdr alist))))
7169 string))
7171 (defun tramp-insert-with-face (face string)
7172 "Insert text with a specific face."
7173 (let ((start (point)))
7174 (insert string)
7175 (add-text-properties start (point) (list 'face face))))
7177 ;; ------------------------------------------------------------
7178 ;; -- Compatibility functions section --
7179 ;; ------------------------------------------------------------
7181 (defun tramp-temporary-file-directory ()
7182 "Return name of directory for temporary files (compat function).
7183 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7184 this is the function `temp-directory'."
7185 (cond ((boundp 'temporary-file-directory)
7186 (symbol-value 'temporary-file-directory))
7187 ((fboundp 'temp-directory)
7188 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7189 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7190 (file-name-as-directory (getenv "TEMP")))
7191 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7192 (file-name-as-directory (getenv "TMP")))
7193 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7194 (file-name-as-directory (getenv "TMPDIR")))
7195 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7196 (t (message (concat "Neither `temporary-file-directory' nor "
7197 "`temp-directory' is defined -- using /tmp."))
7198 (file-name-as-directory "/tmp"))))
7200 (defun tramp-read-passwd (user host prompt)
7201 "Read a password from user (compat function).
7202 Invokes `password-read' if available, `read-passwd' else."
7203 (if (functionp 'password-read)
7204 (let* ((key (concat (or user (user-login-name)) "@" host))
7205 (password (apply #'password-read (list prompt key))))
7206 (apply #'password-cache-add (list key password))
7207 password)
7208 (read-passwd prompt)))
7210 (defun tramp-clear-passwd (&optional user host)
7211 "Clear password cache for connection related to current-buffer."
7212 (interactive)
7213 (let ((filename (or buffer-file-name list-buffers-directory "")))
7214 (when (and (functionp 'password-cache-remove)
7215 (or (and user host) (tramp-tramp-file-p filename)))
7216 (let* ((v (when (tramp-tramp-file-p filename)
7217 (tramp-dissect-file-name filename)))
7218 (luser (or user (tramp-file-name-user v) (user-login-name)))
7219 (lhost (or host (tramp-file-name-host v) (system-name)))
7220 (key (concat luser "@" lhost)))
7221 (apply #'password-cache-remove (list key))))))
7223 (defun tramp-time-diff (t1 t2)
7224 "Return the difference between the two times, in seconds.
7225 T1 and T2 are time values (as returned by `current-time' for example).
7227 NOTE: This function will fail if the time difference is too large to
7228 fit in an integer."
7229 ;; Pacify byte-compiler with `symbol-function'.
7230 (cond ((and (fboundp 'subtract-time)
7231 (fboundp 'float-time))
7232 (funcall (symbol-function 'float-time)
7233 (funcall (symbol-function 'subtract-time) t1 t2)))
7234 ((and (fboundp 'subtract-time)
7235 (fboundp 'time-to-seconds))
7236 (funcall (symbol-function 'time-to-seconds)
7237 (funcall (symbol-function 'subtract-time) t1 t2)))
7238 ((fboundp 'itimer-time-difference)
7239 (floor (funcall
7240 (symbol-function 'itimer-time-difference)
7241 (if (< (length t1) 3) (append t1 '(0)) t1)
7242 (if (< (length t2) 3) (append t2 '(0)) t2))))
7244 ;; snarfed from Emacs 21 time-date.el; combining
7245 ;; time-to-seconds and subtract-time
7246 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7247 (list (- (car t1) (car t2) (if borrow 1 0))
7248 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7249 (+ (* (car time) 65536.0)
7250 (cadr time)
7251 (/ (or (nth 2 time) 0) 1000000.0))))))
7253 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7254 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7255 EOL-TYPE can be one of `dos', `unix', or `mac'."
7256 (cond ((fboundp 'coding-system-change-eol-conversion)
7257 (apply #'coding-system-change-eol-conversion
7258 (list coding-system eol-type)))
7259 ((fboundp 'subsidiary-coding-system)
7260 (apply
7261 #'subsidiary-coding-system
7262 (list coding-system
7263 (cond ((eq eol-type 'dos) 'crlf)
7264 ((eq eol-type 'unix) 'lf)
7265 ((eq eol-type 'mac) 'cr)
7267 (error "Unknown EOL-TYPE `%s', must be %s"
7268 eol-type
7269 "`dos', `unix', or `mac'"))))))
7270 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7272 (defun tramp-split-string (string pattern)
7273 "Like `split-string' but omit empty strings.
7274 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7275 This is, the first, empty, element is omitted. In XEmacs, the first
7276 element is not omitted.
7278 Note: this function has been written for `tramp-handle-file-truename'.
7279 If you want to use it for something else, you'll have to check whether
7280 it does the right thing."
7281 (delete "" (split-string string pattern)))
7283 (defun tramp-set-process-query-on-exit-flag (process flag)
7284 "Specify if query is needed for process when Emacs is exited.
7285 If the second argument flag is non-nil, Emacs will query the user before
7286 exiting if process is running."
7287 (funcall
7288 (if (fboundp 'set-process-query-on-exit-flag)
7289 (symbol-function 'set-process-query-on-exit-flag)
7290 (symbol-function 'process-kill-without-query))
7291 process flag))
7294 ;; ------------------------------------------------------------
7295 ;; -- Kludges section --
7296 ;; ------------------------------------------------------------
7298 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7299 ;; does not deal well with newline characters. Newline is replaced by
7300 ;; backslash newline. But if, say, the string `a backslash newline b'
7301 ;; is passed to a shell, the shell will expand this into "ab",
7302 ;; completely omitting the newline. This is not what was intended.
7303 ;; It does not appear to be possible to make the function
7304 ;; `shell-quote-argument' work with newlines without making it
7305 ;; dependent on the shell used. But within this package, we know that
7306 ;; we will always use a Bourne-like shell, so we use an approach which
7307 ;; groks newlines.
7309 ;; The approach is simple: we call `shell-quote-argument', then
7310 ;; massage the newline part of the result.
7312 ;; This function should produce a string which is grokked by a Unix
7313 ;; shell, even if the Emacs is running on Windows. Since this is the
7314 ;; kludges section, we bind `system-type' in such a way that
7315 ;; `shell-quote-arguments' behaves as if on Unix.
7317 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7318 ;; function to work with Bourne-like shells.
7320 ;; CCC: This function should be rewritten so that
7321 ;; `shell-quote-argument' is not used. This way, we are safe from
7322 ;; changes in `shell-quote-argument'.
7323 (defun tramp-shell-quote-argument (s)
7324 "Similar to `shell-quote-argument', but groks newlines.
7325 Only works for Bourne-like shells."
7326 (let ((system-type 'not-windows))
7327 (save-match-data
7328 (let ((result (shell-quote-argument s))
7329 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7330 (when (and (>= (length result) 2)
7331 (string= (substring result 0 2) "\\~"))
7332 (setq result (substring result 1)))
7333 (while (string-match nl result)
7334 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7335 t t result)))
7336 result))))
7338 ;; ;; EFS hooks itself into the file name handling stuff in more places
7339 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7340 ;; ;; away from tramp.el file names.
7341 ;; ;;
7342 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7343 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7344 ;; ;; connection to help it's dired process. Not that I have any real
7345 ;; ;; idea *why* this is helpful to dired.
7346 ;; ;;
7347 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7348 ;; ;;
7349 ;; ;; Daniel Pittman <daniel@danann.net>
7350 ;; ;;
7351 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7352 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7353 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7354 ;; ;; EFS might nicht be loaded yet.
7355 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7356 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7357 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7358 ;; nil
7359 ;; ad-do-it))
7361 ;; We currently (sometimes) use "[" and "]" in the filename format.
7362 ;; This means that Emacs wants to expand wildcards if
7363 ;; `find-file-wildcards' is non-nil, and then barfs because no
7364 ;; expansion could be found. We detect this situation and do
7365 ;; something really awful: we have `file-expand-wildcards' return the
7366 ;; original filename if it can't expand anything. Let's just hope
7367 ;; that this doesn't break anything else.
7368 ;; CCC: This check is now also really awful; we should search all
7369 ;; of the filename format, not just the prefix.
7370 (when (string-match "\\[" tramp-prefix-format)
7371 (defadvice file-expand-wildcards (around tramp-fix activate)
7372 (let ((name (ad-get-arg 0)))
7373 (if (tramp-tramp-file-p name)
7374 ;; If it's a Tramp file, dissect it and look if wildcards
7375 ;; need to be expanded at all.
7376 (let ((v (tramp-dissect-file-name name)))
7377 (if (string-match "[[*?]" (tramp-file-name-localname v))
7378 (let ((res ad-do-it))
7379 (setq ad-return-value (or res (list name))))
7380 (setq ad-return-value (list name))))
7381 ;; If it is not a Tramp file, just run the original function.
7382 (let ((res ad-do-it))
7383 (setq ad-return-value (or res (list name)))))))
7384 (add-hook 'tramp-unload-hook
7385 '(lambda () (ad-unadvise 'file-expand-wildcards))))
7387 ;; Tramp version is useful in a number of situations.
7389 (defun tramp-version (arg)
7390 "Print version number of tramp.el in minibuffer or current buffer."
7391 (interactive "P")
7392 (if arg (insert tramp-version) (message tramp-version)))
7394 ;; Make the `reporter` functionality available for making bug reports about
7395 ;; the package. A most useful piece of code.
7397 (unless (fboundp 'reporter-submit-bug-report)
7398 (autoload 'reporter-submit-bug-report "reporter"))
7400 (defun tramp-bug ()
7401 "Submit a bug report to the TRAMP developers."
7402 (interactive)
7403 (require 'reporter)
7404 (catch 'dont-send
7405 (let ((reporter-prompt-for-summary-p t))
7406 (reporter-submit-bug-report
7407 tramp-bug-report-address ; to-address
7408 (format "tramp (%s)" tramp-version) ; package name and version
7409 (delq nil
7410 `(;; Current state
7411 tramp-ls-command
7412 tramp-test-groks-nt
7413 tramp-file-exists-command
7414 tramp-current-multi-method
7415 tramp-current-method
7416 tramp-current-user
7417 tramp-current-host
7419 ;; System defaults
7420 tramp-auto-save-directory ; vars to dump
7421 tramp-default-method
7422 tramp-rsh-end-of-line
7423 tramp-default-password-end-of-line
7424 tramp-remote-path
7425 tramp-login-prompt-regexp
7426 ;; Mask non-7bit characters
7427 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7428 tramp-wrong-passwd-regexp
7429 tramp-yesno-prompt-regexp
7430 tramp-yn-prompt-regexp
7431 tramp-terminal-prompt-regexp
7432 tramp-temp-name-prefix
7433 tramp-file-name-structure
7434 tramp-file-name-regexp
7435 tramp-multi-file-name-structure
7436 tramp-multi-file-name-hop-structure
7437 tramp-multi-methods
7438 tramp-multi-connection-function-alist
7439 tramp-methods
7440 tramp-end-of-output
7441 tramp-coding-commands
7442 tramp-actions-before-shell
7443 tramp-actions-copy-out-of-band
7444 tramp-multi-actions
7445 tramp-terminal-type
7446 ;; Mask non-7bit characters
7447 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7448 tramp-chunksize
7449 ,(when (boundp 'tramp-backup-directory-alist)
7450 'tramp-backup-directory-alist)
7451 ,(when (boundp 'tramp-bkup-backup-directory-info)
7452 'tramp-bkup-backup-directory-info)
7454 ;; Non-tramp variables of interest
7455 ;; Mask non-7bit characters
7456 (shell-prompt-pattern . tramp-reporter-dump-variable)
7457 backup-by-copying
7458 backup-by-copying-when-linked
7459 backup-by-copying-when-mismatch
7460 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7461 'backup-by-copying-when-privileged-mismatch)
7462 ,(when (boundp 'password-cache)
7463 'password-cache)
7464 ,(when (boundp 'password-cache-expiry)
7465 'password-cache-expiry)
7466 ,(when (boundp 'backup-directory-alist)
7467 'backup-directory-alist)
7468 ,(when (boundp 'bkup-backup-directory-info)
7469 'bkup-backup-directory-info)
7470 file-name-handler-alist))
7472 'tramp-load-report-modules ; pre-hook
7473 'tramp-append-tramp-buffers ; post-hook
7475 Enter your bug report in this message, including as much detail as you
7476 possibly can about the problem, what you did to cause it and what the
7477 local and remote machines are.
7479 If you can give a simple set of instructions to make this bug happen
7480 reliably, please include those. Thank you for helping kill bugs in
7481 TRAMP.
7483 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7484 the ~/.emacs file and to repeat the bug. Then, include the contents
7485 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7486 report.
7488 --bug report follows this line--
7489 "))))
7491 (defun tramp-reporter-dump-variable (varsym mailbuf)
7492 "Pretty-print the value of the variable in symbol VARSYM.
7493 Used for non-7bit chars in strings."
7494 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7495 (val (with-current-buffer reporter-eval-buffer
7496 (symbol-value varsym))))
7498 ;; There are characters to be masked.
7499 (when (and (boundp 'mm-7bit-chars)
7500 (string-match
7501 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7502 (with-current-buffer reporter-eval-buffer
7503 (set varsym (concat "(base64-decode-string \""
7504 (base64-encode-string val)
7505 "\")"))))
7507 ;; Dump variable.
7508 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7510 ;; Remove string quotation.
7511 (forward-line -1)
7512 (when (looking-at
7513 (concat "\\(^.*\\)" "\"" ;; \1 "
7514 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7515 "\\(\".*\\)" "\\\\" ;; \3 \
7516 "\\(\")\\)" "\"$")) ;; \4 "
7517 (replace-match "\\1\\2\\3\\4")
7518 (beginning-of-line)
7519 (insert " ;; variable encoded due to non-printable characters\n"))
7520 (forward-line 1)
7522 ;; Reset VARSYM to old value.
7523 (with-current-buffer reporter-eval-buffer
7524 (set varsym val))))
7526 (defun tramp-load-report-modules ()
7527 "Load needed modules for reporting."
7529 ;; We load message.el and mml.el from Gnus.
7530 (if (featurep 'xemacs)
7531 (progn
7532 (load "message" 'noerror)
7533 (load "mml" 'noerror))
7534 (require 'message nil 'noerror)
7535 (require 'mml nil 'noerror))
7536 (when (functionp 'message-mode)
7537 (funcall (symbol-function 'message-mode)))
7538 (when (functionp 'mml-mode)
7539 (funcall (symbol-function 'mml-mode) t)))
7541 (defun tramp-append-tramp-buffers ()
7542 "Append Tramp buffers into the bug report."
7544 (when (and
7545 (eq major-mode 'message-mode)
7546 (boundp 'mml-mode)
7547 (symbol-value 'mml-mode))
7549 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7550 (buffer-list
7551 (delq nil
7552 (mapcar '(lambda (b)
7553 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7554 (buffer-list))))
7555 (curbuf (current-buffer)))
7557 ;; There is at least one Tramp buffer.
7558 (when buffer-list
7559 (switch-to-buffer (list-buffers-noselect nil))
7560 (delete-other-windows)
7561 (setq buffer-read-only nil)
7562 (goto-char (point-min))
7563 (while (not (eobp))
7564 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7565 (forward-line 1)
7566 (forward-line 0)
7567 (let ((start (point)))
7568 (forward-line 1)
7569 (kill-region start (point)))))
7570 (insert "
7571 The buffer(s) above will be appended to this message. If you don't want
7572 to append a buffer because it contains sensible data, or because the buffer
7573 is too large, you should delete the respective buffer. The buffer(s) will
7574 contain user and host names. Passwords will never be included there.")
7576 (when (and tramp-debug-buffer (> tramp-verbose 9))
7577 (insert "\n\n")
7578 (let ((start (point)))
7579 (insert "\
7580 Please note that you have set `tramp-verbose' to a value greater than 9.
7581 Therefore, the contents of files might be included in the debug buffer(s).")
7582 (add-text-properties start (point) (list 'face 'italic))))
7584 (set-buffer-modified-p nil)
7585 (setq buffer-read-only t)
7586 (goto-char (point-min))
7588 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7589 ;; OK, let's send. First we delete the buffer list.
7590 (progn
7591 (kill-buffer nil)
7592 (switch-to-buffer curbuf)
7593 (goto-char (point-max))
7594 (insert "\n\n")
7595 (dolist (buffer buffer-list)
7596 (funcall (symbol-function 'mml-insert-empty-tag)
7597 'part 'type "text/plain" 'encoding "base64"
7598 'disposition "attachment" 'buffer (buffer-name buffer)
7599 'description (buffer-name buffer)))
7600 (set-buffer-modified-p nil))
7602 ;; Don't send. Delete the message buffer.
7603 (set-buffer curbuf)
7604 (set-buffer-modified-p nil)
7605 (kill-buffer nil)
7606 (throw 'dont-send nil))))))
7608 (defalias 'tramp-submit-bug 'tramp-bug)
7610 ;; Checklist for `tramp-unload-hook'
7611 ;; - Unload all `tramp-*' packages
7612 ;; - Reset `file-name-handler-alist'
7613 ;; - Cleanup hooks where Tramp functions are in
7614 ;; - Cleanup advised functions
7615 ;; - Cleanup autoloads
7616 ;;;###autoload
7617 (defun tramp-unload-tramp ()
7618 (interactive)
7619 ;; When Tramp is not loaded yet, its autoloads are still active.
7620 (tramp-unload-file-name-handler-alist)
7621 ;; ange-ftp settings must be enabled.
7622 (when (functionp 'tramp-ftp-enable-ange-ftp)
7623 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7624 ;; `tramp-util' unloads also `tramp'.
7625 (condition-case nil ;; maybe its not loaded yet.
7626 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7627 (error nil)))
7629 (provide 'tramp)
7631 ;; Make sure that we get integration with the VC package.
7632 ;; When it is loaded, we need to pull in the integration module.
7633 ;; This must come after (provide 'tramp) because tramp-vc.el
7634 ;; requires tramp.
7635 (eval-after-load "vc"
7636 '(progn
7637 (require 'tramp-vc)
7638 (add-hook 'tramp-unload-hook
7639 '(lambda ()
7640 (when (featurep 'tramp-vc)
7641 (unload-feature 'tramp-vc 'force))))))
7643 ;;; TODO:
7645 ;; * Allow putting passwords in the filename.
7646 ;; This should be implemented via a general mechanism to add
7647 ;; parameters in filenames. There is currently a kludge for
7648 ;; putting the port number into the filename for ssh and ftp
7649 ;; files. This could be subsumed by the new mechanism as well.
7650 ;; Another approach is to read a netrc file like ~/.authinfo
7651 ;; from Gnus.
7652 ;; * Handle nonlocal exits such as C-g.
7653 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7654 ;; * Add fallback for inline encodings. This should be used
7655 ;; if the remote end doesn't support mimencode or a similar program.
7656 ;; For reading files from the remote host, we can just parse the output
7657 ;; of `od -b'. For writing files to the remote host, we construct
7658 ;; a shell program which contains only "safe" ascii characters
7659 ;; and which writes the right bytes to the file. We can use printf(1)
7660 ;; or "echo -e" or the printf function in awk and use octal escapes
7661 ;; for the "dangerous" characters. The null byte might be a problem.
7662 ;; On some systems, the octal escape doesn't work. So we try the following
7663 ;; two commands to write a null byte:
7664 ;; dd if=/dev/zero bs=1 count=1
7665 ;; echo | tr '\n' '\000'
7666 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7667 ;; the two via a format which can be `uu' or `b64'. Then we can search
7668 ;; for the right local commands and the right remote commands separately.
7669 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7670 ;; work for remote files.
7671 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7672 ;; `shell-quote-argument'.
7673 ;; * Completion gets confused when you leave out the method name.
7674 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7675 ;; by the files in that directory. Add this here.
7676 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7677 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7678 ;; * When logging in, keep looking for questions according to an alist
7679 ;; and then invoke the right function.
7680 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7681 ;; * Running CVS remotely doesn't appear to work right. It thinks
7682 ;; files are locked by somebody else even if I'm the locking user.
7683 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7684 ;; (Skip Montanaro)
7685 ;; * Don't use globbing for directories with many files, as this is
7686 ;; likely to produce long command lines, and some shells choke on
7687 ;; long command lines.
7688 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7689 ;; do the right thing.
7690 ;; * `vc-directory' does not work. It never displays any files, even
7691 ;; if it does show files when run locally.
7692 ;; * Allow correction of passwords, if the remote end allows this.
7693 ;; (Mark Hershberger)
7694 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7695 ;; * Do asynchronous `shell-command's.
7696 ;; * Grok `append' parameter for `write-region'.
7697 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7698 ;; * abbreviate-file-name
7699 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7700 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7701 ;; what the user names should default to, though.
7702 ;; * better error checking. At least whenever we see something
7703 ;; strange when doing zerop, we should kill the process and start
7704 ;; again. (Greg Stark)
7705 ;; * Add caching for filename completion. (Greg Stark)
7706 ;; Of course, this has issues with usability (stale cache bites)
7707 ;; -- <daniel@danann.net>
7708 ;; * Provide a local cache of old versions of remote files for the rsync
7709 ;; transfer method to use. (Greg Stark)
7710 ;; * Remove unneeded parameters from methods.
7711 ;; * Invoke rsync once for copying a whole directory hierarchy.
7712 ;; (Francesco Potort\e,Al\e(B)
7713 ;; * Should we set PATH ourselves or should we rely on the remote end
7714 ;; to do it?
7715 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7716 ;; * Make it work for different encodings, and for different file name
7717 ;; encodings, too. (Daniel Pittman)
7718 ;; * Change applicable functions to pass a struct tramp-file-name rather
7719 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7720 ;; * Implement asynchronous shell commands.
7721 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7722 ;; * Progress reports while copying files. (Michael Kifer)
7723 ;; * `Smart' connection method that uses inline for small and out of
7724 ;; band for large files. (Michael Kifer)
7725 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7726 ;; then look if it's the right version (with `perl -v').
7727 ;; * When editing a remote CVS controlled file as a different user, VC
7728 ;; gets confused about the file locking status. Try to find out why
7729 ;; the workaround doesn't work.
7730 ;; * Change `copy-file' to grok the case where the filename handler
7731 ;; for the source and the target file are different. Right now,
7732 ;; it looks at the source file and then calls that handler, if
7733 ;; there is one. But since ange-ftp, for instance, does not know
7734 ;; about Tramp, it does not do the right thing if the target file
7735 ;; name is a Tramp name.
7736 ;; * Username and hostname completion.
7737 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7738 ;; connect to host "blabla" already if that host is unique. No idea
7739 ;; how to suppress. Maybe not an essential problem.
7740 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7741 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7742 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7743 ;; Code is nearly identical.
7744 ;; ** Decide whiche files to take for searching user/host names depending on
7745 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7746 ;; ** Enhance variables for debug.
7747 ;; ** Implement "/multi:" completion.
7748 ;; ** Add a learning mode for completion. Make results persistent.
7749 ;; * Allow out-of-band methods as _last_ multi-hop.
7751 ;; Functions for file-name-handler-alist:
7752 ;; diff-latest-backup-file -- in diff.el
7753 ;; dired-uncache -- this will be needed when we do insert-directory caching
7754 ;; file-name-as-directory -- use primitive?
7755 ;; file-name-sans-versions -- use primitive?
7756 ;; get-file-buffer -- use primitive
7757 ;; vc-registered
7759 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7760 ;;; tramp.el ends here