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