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