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