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