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