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