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