*** empty log message ***
[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
KG
1823
1824; These values conform to `file-attributes' from XEmacs 21.2.
1825; GNU Emacs and other tools not checked.
1826(defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1827 (1 . "p") ; fifo
1828 (2 . "c") ; character device
1829 (3 . "m") ; multiplexed character device (v7)
1830 (4 . "d") ; directory
1831 (5 . "?") ; Named special file (XENIX)
1832 (6 . "b") ; block device
1833 (7 . "?") ; multiplexed block device (v7)
1834 (8 . "-") ; regular file
1835 (9 . "n") ; network special file (HP-UX)
1836 (10 . "l") ; symlink
1837 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1838 (12 . "s") ; socket
1839 (13 . "D") ; door special (Solaris)
1840 (14 . "w")) ; whiteout (BSD)
1841 "A list of file types returned from the `stat' system call.
1842This is used to map a mode number to a permission string.")
1843
1844(defvar tramp-dos-coding-system
1845 (if (and (fboundp 'coding-system-p)
1846 (funcall 'coding-system-p '(dos)))
1847 'dos
1848 'undecided-dos)
1849 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1850
38c65fca
KG
1851(defvar tramp-last-cmd nil
1852 "Internal Tramp variable recording the last command sent.
1853This variable is buffer-local in every buffer.")
1854(make-variable-buffer-local 'tramp-last-cmd)
1855
1856(defvar tramp-process-echoes nil
1857 "Whether to process echoes from the remote shell.")
1858
ac474af1
KG
1859(defvar tramp-last-cmd-time nil
1860 "Internal Tramp variable recording the time when the last cmd was sent.
1861This variable is buffer-local in every buffer.")
1862(make-variable-buffer-local 'tramp-last-cmd-time)
fb7933a3 1863
dba28077
KG
1864;; This variable does not have the right value in XEmacs. What should
1865;; I use instead of find-operation-coding-system in XEmacs?
3cdaec13 1866(defvar tramp-feature-write-region-fix
16674e4f 1867 (when (fboundp 'find-operation-coding-system)
dba28077 1868 (let ((file-coding-system-alist '(("test" emacs-mule))))
38c65fca
KG
1869 (funcall (symbol-function 'find-operation-coding-system)
1870 'write-region 0 0 "" nil "test")))
dba28077 1871 "Internal variable to say if `write-region' chooses the right coding.
3cdaec13
KG
1872Older versions of Emacs chose the coding system for `write-region' based
1873on the FILENAME argument, even if VISIT was a string.")
1874
fb7933a3
KG
1875;; New handlers should be added here. The following operations can be
1876;; handled using the normal primitives: file-name-as-directory,
1877;; file-name-directory, file-name-nondirectory,
1878;; file-name-sans-versions, get-file-buffer.
1879(defconst tramp-file-name-handler-alist
1880 '(
1881 (load . tramp-handle-load)
1882 (make-symbolic-link . tramp-handle-make-symbolic-link)
1883 (file-name-directory . tramp-handle-file-name-directory)
1884 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1885 (file-truename . tramp-handle-file-truename)
1886 (file-exists-p . tramp-handle-file-exists-p)
1887 (file-directory-p . tramp-handle-file-directory-p)
1888 (file-executable-p . tramp-handle-file-executable-p)
1889 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1890 (file-readable-p . tramp-handle-file-readable-p)
1891 (file-regular-p . tramp-handle-file-regular-p)
1892 (file-symlink-p . tramp-handle-file-symlink-p)
1893 (file-writable-p . tramp-handle-file-writable-p)
1894 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1895 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1896 (file-attributes . tramp-handle-file-attributes)
1897 (file-modes . tramp-handle-file-modes)
fb7933a3 1898 (directory-files . tramp-handle-directory-files)
c82c5727 1899 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
fb7933a3
KG
1900 (file-name-all-completions . tramp-handle-file-name-all-completions)
1901 (file-name-completion . tramp-handle-file-name-completion)
1902 (add-name-to-file . tramp-handle-add-name-to-file)
1903 (copy-file . tramp-handle-copy-file)
1904 (rename-file . tramp-handle-rename-file)
1905 (set-file-modes . tramp-handle-set-file-modes)
1906 (make-directory . tramp-handle-make-directory)
1907 (delete-directory . tramp-handle-delete-directory)
1908 (delete-file . tramp-handle-delete-file)
1909 (directory-file-name . tramp-handle-directory-file-name)
1910 (shell-command . tramp-handle-shell-command)
0457dd55 1911 (process-file . tramp-handle-process-file)
fb7933a3
KG
1912 (insert-directory . tramp-handle-insert-directory)
1913 (expand-file-name . tramp-handle-expand-file-name)
1914 (file-local-copy . tramp-handle-file-local-copy)
19a87064 1915 (file-remote-p . tramp-handle-file-remote-p)
fb7933a3
KG
1916 (insert-file-contents . tramp-handle-insert-file-contents)
1917 (write-region . tramp-handle-write-region)
38c65fca 1918 (find-backup-file-name . tramp-handle-find-backup-file-name)
c1105d05 1919 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
fb7933a3 1920 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
5ec2cc41 1921 (dired-compress-file . tramp-handle-dired-compress-file)
fb7933a3
KG
1922 (dired-call-process . tramp-handle-dired-call-process)
1923 (dired-recursive-delete-directory
1924 . tramp-handle-dired-recursive-delete-directory)
1925 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1926 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
c1105d05 1927 "Alist of handler functions.
fb7933a3
KG
1928Operations not mentioned here will be handled by the normal Emacs functions.")
1929
16674e4f
KG
1930;; Handlers for partial tramp file names. For GNU Emacs just
1931;; `file-name-all-completions' is needed. The other ones are necessary
1932;; for XEmacs.
1933(defconst tramp-completion-file-name-handler-alist
1934 '(
1935 (file-name-directory . tramp-completion-handle-file-name-directory)
1936 (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory)
1937 (file-exists-p . tramp-completion-handle-file-exists-p)
1938 (file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1939 (file-name-completion . tramp-completion-handle-file-name-completion)
1940 (expand-file-name . tramp-completion-handle-expand-file-name))
1941 "Alist of completion handler functions.
1942Used for file names matching `tramp-file-name-regexp'. Operations not
1943mentioned here will be handled by `tramp-file-name-handler-alist' or the
1944normal Emacs functions.")
1945
4007ba5b 1946;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
ea9d1443
KG
1947(defvar tramp-foreign-file-name-handler-alist
1948 ;; (identity . tramp-sh-file-name-handler) should always be the last
1949 ;; entry, since `identity' always matches.
1950 '((identity . tramp-sh-file-name-handler))
4007ba5b
KG
1951 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1952If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1953calling HANDLER.")
1954
fb7933a3
KG
1955;;; Internal functions which must come first.
1956
1957(defsubst tramp-message (level fmt-string &rest args)
1958 "Emit a message depending on verbosity level.
1959First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1960message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1961Calls function `message' with FMT-STRING as control string and the remaining
1962ARGS to actually emit the message (if applicable).
1963
1964This function expects to be called from the tramp buffer only!"
1965 (when (<= level tramp-verbose)
1966 (apply #'message (concat "tramp: " fmt-string) args)
1967 (when tramp-debug-buffer
1968 (save-excursion
1969 (set-buffer
1970 (tramp-get-debug-buffer
1971 tramp-current-multi-method tramp-current-method
1972 tramp-current-user tramp-current-host))
1973 (goto-char (point-max))
d2a2c17f
MA
1974 (unless (bolp)
1975 (insert "\n"))
1976 (tramp-insert-with-face
fb7933a3
KG
1977 'italic
1978 (concat "# " (apply #'format fmt-string args) "\n"))))))
1979
1980(defun tramp-message-for-buffer
1981 (multi-method method user host level fmt-string &rest args)
1982 "Like `tramp-message' but temporarily switches to the tramp buffer.
1983First three args METHOD, USER, and HOST identify the tramp buffer to use,
1984remaining args passed to `tramp-message'."
1985 (save-excursion
1986 (set-buffer (tramp-get-buffer multi-method method user host))
1987 (apply 'tramp-message level fmt-string args)))
1988
1989(defsubst tramp-line-end-position nil
1990 "Return point at end of line.
1991Calls `line-end-position' or `point-at-eol' if defined, else
1992own implementation."
1993 (cond
38c65fca
KG
1994 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
1995 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
fb7933a3
KG
1996 (t (save-excursion (end-of-line) (point)))))
1997
c62c9d08
KG
1998(defmacro with-parsed-tramp-file-name (filename var &rest body)
1999 "Parse a Tramp filename and make components available in the body.
2000
2001First arg FILENAME is evaluated and dissected into its components.
2002Second arg VAR is a symbol. It is used as a variable name to hold
2003the filename structure. It is also used as a prefix for the variables
2004holding the components. For example, if VAR is the symbol `foo', then
2005`foo' will be bound to the whole structure, `foo-multi-method' will
2006be bound to the multi-method component, and so on for `foo-method',
7432277c 2007`foo-user', `foo-host', `foo-localname'.
c62c9d08
KG
2008
2009Remaining args are Lisp expressions to be evaluated (inside an implicit
2010`progn').
2011
2012If VAR is nil, then we bind `v' to the structure and `multi-method',
7432277c 2013`method', `user', `host', `localname' to the components."
c62c9d08
KG
2014 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2015 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2016 (tramp-file-name-multi-method ,(or var 'v)))
2017 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2018 (tramp-file-name-method ,(or var 'v)))
2019 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2020 (tramp-file-name-user ,(or var 'v)))
2021 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2022 (tramp-file-name-host ,(or var 'v)))
7432277c
KG
2023 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2024 (tramp-file-name-localname ,(or var 'v))))
c62c9d08
KG
2025 ,@body))
2026
2027(put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
38c65fca 2028;; To be activated for debugging containing this macro
414da5ab 2029;; It works only when VAR is nil. Otherwise, it can be deactivated by
c08e6004 2030;; (put 'with-parsed-tramp-file-name 'edebug-form-spec 0)
414da5ab 2031;; I'm too stupid to write a precise SPEC for it.
c08e6004 2032(put 'with-parsed-tramp-file-name 'edebug-form-spec t)
c62c9d08 2033
2fcaee47
KG
2034(defmacro tramp-let-maybe (variable value &rest body)
2035 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2036BODY is executed whether or not the variable is obsolete.
2037The intent is to protect against `obsolete variable' warnings."
2038 `(if (get ',variable 'byte-obsolete-variable)
2039 (progn ,@body)
2040 (let ((,variable ,value))
2041 ,@body)))
2042(put 'tramp-let-maybe 'lisp-indent-function 2)
a69c01a0 2043(put 'tramp-let-maybe 'edebug-form-spec t)
2fcaee47 2044
16674e4f
KG
2045;;; Config Manipulation Functions:
2046
2047(defun tramp-set-completion-function (method function-list)
2048 "Sets the list of completion functions for METHOD.
2049FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2050The FUNCTION is intended to parse FILE according its syntax.
2051It might be a predefined FUNCTION, or a user defined FUNCTION.
2052Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
8daea7fc
KG
2053`tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2054and `tramp-parse-netrc'.
2055
16674e4f
KG
2056Example:
2057
2058 (tramp-set-completion-function
2059 \"ssh\"
8daea7fc
KG
2060 '((tramp-parse-sconfig \"/etc/ssh_config\")
2061 (tramp-parse-sconfig \"~/.ssh/config\")))"
16674e4f 2062
5ec2cc41
KG
2063 (let ((r function-list)
2064 (v function-list))
2065 (setq tramp-completion-function-alist
2066 (delete (assoc method tramp-completion-function-alist)
2067 tramp-completion-function-alist))
2068
2069 (while v
2070 ;; Remove double entries
2071 (when (member (car v) (cdr v))
2072 (setcdr v (delete (car v) (cdr v))))
2073 ;; Check for function and file
2074 (unless (and (functionp (nth 0 (car v)))
2075 (file-exists-p (nth 1 (car v))))
2076 (setq r (delete (car v) r)))
2077 (setq v (cdr v)))
2078
2079 (when r
4007ba5b 2080 (add-to-list 'tramp-completion-function-alist
5ec2cc41 2081 (cons method r)))))
16674e4f
KG
2082
2083(defun tramp-get-completion-function (method)
2084 "Returns list of completion functions for METHOD.
2085For definition of that list see `tramp-set-completion-function'."
5ec2cc41 2086 (cdr (assoc method tramp-completion-function-alist)))
16674e4f 2087
fb7933a3
KG
2088;;; File Name Handler Functions:
2089
fb7933a3
KG
2090(defun tramp-handle-make-symbolic-link
2091 (filename linkname &optional ok-if-already-exists)
2092 "Like `make-symbolic-link' for tramp files.
cebb4ec6 2093If LINKNAME is a non-Tramp file, it is used verbatim as the target of
7432277c 2094the symlink. If LINKNAME is a Tramp file, only the localname component is
cebb4ec6
KG
2095used as the target of the symlink.
2096
7432277c
KG
2097If LINKNAME is a Tramp file and the localname component is relative, then
2098it is expanded first, before the localname component is taken. Note that
cebb4ec6
KG
2099this can give surprising results if the user/host for the source and
2100target of the symlink differ."
c62c9d08 2101 (with-parsed-tramp-file-name linkname l
487fa986 2102 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
7432277c 2103 (cwd (file-name-directory l-localname)))
c62c9d08
KG
2104 (unless ln
2105 (signal 'file-error
2106 (list "Making a symbolic link."
2107 "ln(1) does not exist on the remote host.")))
2108
2109 ;; Do the 'confirm if exists' thing.
cebb4ec6 2110 (when (file-exists-p linkname)
c62c9d08
KG
2111 ;; What to do?
2112 (if (or (null ok-if-already-exists) ; not allowed to exist
2113 (and (numberp ok-if-already-exists)
2114 (not (yes-or-no-p
2115 (format
2116 "File %s already exists; make it a link anyway? "
7432277c
KG
2117 l-localname)))))
2118 (signal 'file-already-exists (list "File already exists" l-localname))
cebb4ec6
KG
2119 (delete-file linkname)))
2120
7432277c 2121 ;; If FILENAME is a Tramp name, use just the localname component.
cebb4ec6 2122 (when (tramp-tramp-file-p filename)
7432277c 2123 (setq filename (tramp-file-name-localname
cebb4ec6
KG
2124 (tramp-dissect-file-name
2125 (expand-file-name filename)))))
bf247b6e 2126
c62c9d08
KG
2127 ;; Right, they are on the same host, regardless of user, method, etc.
2128 ;; We now make the link on the remote machine. This will occur as the user
2129 ;; that FILENAME belongs to.
2130 (zerop
2131 (tramp-send-command-and-check
487fa986 2132 l-multi-method l-method l-user l-host
c62c9d08
KG
2133 (format "cd %s && %s -sf %s %s"
2134 cwd ln
bf247b6e 2135 filename
7432277c 2136 l-localname)
c62c9d08 2137 t)))))
fb7933a3
KG
2138
2139
2140(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2141 "Like `load' for tramp files. Not implemented!"
2142 (unless (file-name-absolute-p file)
7432277c 2143 (error "Tramp cannot `load' files without absolute file name"))
c62c9d08 2144 (with-parsed-tramp-file-name file nil
c62c9d08
KG
2145 (unless nosuffix
2146 (cond ((file-exists-p (concat file ".elc"))
2147 (setq file (concat file ".elc")))
2148 ((file-exists-p (concat file ".el"))
2149 (setq file (concat file ".el")))))
2150 (when must-suffix
2151 ;; The first condition is always true for absolute file names.
2152 ;; Included for safety's sake.
2153 (unless (or (file-name-directory file)
2154 (string-match "\\.elc?\\'" file))
2155 (error "File `%s' does not include a `.el' or `.elc' suffix"
2156 file)))
2157 (unless noerror
2158 (when (not (file-exists-p file))
540bd575 2159 (error "Cannot load nonexistent file `%s'" file)))
c62c9d08
KG
2160 (if (not (file-exists-p file))
2161 nil
2162 (unless nomessage
2163 (message "Loading %s..." file))
2164 (let ((local-copy (file-local-copy file)))
2165 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2166 (load local-copy noerror t t)
2167 (delete-file local-copy))
2168 (unless nomessage
2169 (message "Loading %s...done" file))
2170 t)))
fb7933a3 2171
7432277c 2172;; Localname manipulation functions that grok TRAMP localnames...
fb7933a3
KG
2173(defun tramp-handle-file-name-directory (file)
2174 "Like `file-name-directory' but aware of TRAMP files."
2175 ;; everything except the last filename thing is the directory
c62c9d08 2176 (with-parsed-tramp-file-name file nil
505edaeb 2177 ;; For the following condition, two possibilities should be tried:
7432277c
KG
2178 ;; (1) (string= localname "")
2179 ;; (2) (or (string= localname "") (string= localname "/"))
505edaeb
KG
2180 ;; The second variant fails when completing a "/" directory on
2181 ;; the remote host, that is a filename which looks like
2182 ;; "/user@host:/". But maybe wildcards fail with the first variant.
2183 ;; We should do some investigation.
7432277c 2184 (if (string= localname "")
fb7933a3
KG
2185 ;; For a filename like "/[foo]", we return "/". The `else'
2186 ;; case would return "/[foo]" unchanged. But if we do that,
2187 ;; then `file-expand-wildcards' ceases to work. It's not
2188 ;; quite clear to me what's the intuition that tells that this
2189 ;; behavior is the right behavior, but oh, well.
2190 "/"
7432277c 2191 ;; run the command on the localname portion only
fb7933a3
KG
2192 ;; CCC: This should take into account the remote machine type, no?
2193 ;; --daniel <daniel@danann.net>
2194 (tramp-make-tramp-file-name multi-method method user host
2195 ;; This will not recurse...
7432277c 2196 (or (file-name-directory localname) "")))))
fb7933a3
KG
2197
2198(defun tramp-handle-file-name-nondirectory (file)
2199 "Like `file-name-nondirectory' but aware of TRAMP files."
c62c9d08 2200 (with-parsed-tramp-file-name file nil
7432277c 2201 (file-name-nondirectory localname)))
fb7933a3
KG
2202
2203(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2204 "Like `file-truename' for tramp files."
48ddd622 2205 (with-parsed-tramp-file-name (expand-file-name filename) nil
7432277c 2206 (let* ((steps (tramp-split-string localname "/"))
19a87064
MA
2207 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2208 (file-name-as-directory localname)))
7432277c 2209 (is-dir (string= localname localnamedir))
c62c9d08
KG
2210 (thisstep nil)
2211 (numchase 0)
2212 ;; Don't make the following value larger than necessary.
2213 ;; People expect an error message in a timely fashion when
2214 ;; something is wrong; otherwise they might think that Emacs
2215 ;; is hung. Of course, correctness has to come first.
2216 (numchase-limit 20)
2217 (result nil) ;result steps in reverse order
c62c9d08 2218 symlink-target)
fb7933a3
KG
2219 (tramp-message-for-buffer
2220 multi-method method user host
c62c9d08
KG
2221 10 "Finding true name for `%s'" filename)
2222 (while (and steps (< numchase numchase-limit))
2223 (setq thisstep (pop steps))
2224 (tramp-message-for-buffer
2225 multi-method method user host
2226 10 "Check %s"
2227 (mapconcat 'identity
2228 (append '("") (reverse result) (list thisstep))
2229 "/"))
2230 (setq symlink-target
c951aecb 2231 (nth 0 (file-attributes
c62c9d08
KG
2232 (tramp-make-tramp-file-name
2233 multi-method method user host
2234 (mapconcat 'identity
b1d06e75
KG
2235 (append '("")
2236 (reverse result)
2237 (list thisstep))
c62c9d08
KG
2238 "/")))))
2239 (cond ((string= "." thisstep)
2240 (tramp-message-for-buffer multi-method method user host
2241 10 "Ignoring step `.'"))
2242 ((string= ".." thisstep)
2243 (tramp-message-for-buffer multi-method method user host
2244 10 "Processing step `..'")
2245 (pop result))
2246 ((stringp symlink-target)
2247 ;; It's a symlink, follow it.
2248 (tramp-message-for-buffer
2249 multi-method method user host
2250 10 "Follow symlink to %s" symlink-target)
2251 (setq numchase (1+ numchase))
2252 (when (file-name-absolute-p symlink-target)
2253 (setq result nil))
b25a52cc
KG
2254 ;; If the symlink was absolute, we'll get a string like
2255 ;; "/user@host:/some/target"; extract the
2256 ;; "/some/target" part from it.
2257 (when (tramp-tramp-file-p symlink-target)
2258 (with-parsed-tramp-file-name symlink-target sym
2259 (unless (equal (list multi-method method user host)
2260 (list sym-multi-method sym-method
2261 sym-user sym-host))
2262 (error "Symlink target `%s' on wrong host"
2263 symlink-target))
2264 (setq symlink-target localname)))
c62c9d08
KG
2265 (setq steps
2266 (append (tramp-split-string symlink-target "/") steps)))
2267 (t
2268 ;; It's a file.
2269 (setq result (cons thisstep result)))))
2270 (when (>= numchase numchase-limit)
2271 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2272 (setq result (reverse result))
487f4fb7
KG
2273 ;; Combine list to form string.
2274 (setq result
2275 (if result
2276 (mapconcat 'identity (cons "" result) "/")
2277 "/"))
2278 (when (and is-dir (or (string= "" result)
2279 (not (string= (substring result -1) "/"))))
2280 (setq result (concat result "/")))
c62c9d08
KG
2281 (tramp-message-for-buffer
2282 multi-method method user host
487f4fb7 2283 10 "True name of `%s' is `%s'" filename result)
c62c9d08 2284 (tramp-make-tramp-file-name
487f4fb7 2285 multi-method method user host result))))
fb7933a3
KG
2286
2287;; Basic functions.
2288
2289(defun tramp-handle-file-exists-p (filename)
2290 "Like `file-exists-p' for tramp files."
c62c9d08 2291 (with-parsed-tramp-file-name filename nil
fb7933a3
KG
2292 (save-excursion
2293 (zerop (tramp-send-command-and-check
2294 multi-method method user host
2295 (format
c62c9d08 2296 (tramp-get-file-exists-command multi-method method user host)
7432277c 2297 (tramp-shell-quote-argument localname)))))))
fb7933a3 2298
8daea7fc
KG
2299;; Devices must distinguish physical file systems. The device numbers
2300;; provided by "lstat" aren't unique, because we operate on different hosts.
2301;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2302;; EFS use device number "-1". In order to be different, we use device number
2303;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2304(defvar tramp-devices nil
2305 "Keeps virtual device numbers.")
2306
fb7933a3
KG
2307;; CCC: This should check for an error condition and signal failure
2308;; when something goes wrong.
2309;; Daniel Pittman <daniel@danann.net>
c951aecb
KG
2310(defun tramp-handle-file-attributes (filename &optional id-format)
2311 "Like `file-attributes' for tramp files."
c82c5727
LH
2312 (when (file-exists-p filename)
2313 ;; file exists, find out stuff
2314 (unless id-format (setq id-format 'integer))
ac474af1 2315 (with-parsed-tramp-file-name filename nil
c82c5727
LH
2316 (save-excursion
2317 (tramp-convert-file-attributes
2318 multi-method method user host
2319 (if (tramp-get-remote-perl multi-method method user host)
2320 (tramp-handle-file-attributes-with-perl multi-method method user host
2321 localname id-format)
2322 (tramp-handle-file-attributes-with-ls multi-method method user host
2323 localname id-format)))))))
fb7933a3 2324
fb7933a3 2325(defun tramp-handle-file-attributes-with-ls
c82c5727 2326 (multi-method method user host localname &optional id-format)
fb7933a3
KG
2327 "Implement `file-attributes' for tramp files using the ls(1) command."
2328 (let (symlinkp dirp
2329 res-inode res-filemodes res-numlinks
2330 res-uid res-gid res-size res-symlink-target)
ac474af1
KG
2331 (tramp-message-for-buffer multi-method method user host 10
2332 "file attributes with ls: %s"
2333 (tramp-make-tramp-file-name
7432277c 2334 multi-method method user host localname))
fb7933a3
KG
2335 (tramp-send-command
2336 multi-method method user host
2337 (format "%s %s %s"
2338 (tramp-get-ls-command multi-method method user host)
c82c5727 2339 (if (eq id-format 'integer) "-ildn" "-ild")
7432277c 2340 (tramp-shell-quote-argument localname)))
fb7933a3
KG
2341 (tramp-wait-for-output)
2342 ;; parse `ls -l' output ...
2343 ;; ... inode
2344 (setq res-inode
2345 (condition-case err
2346 (read (current-buffer))
2347 (invalid-read-syntax
2348 (when (and (equal (cadr err)
2349 "Integer constant overflow in reader")
2350 (string-match
2351 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2352 (caddr err)))
2353 (let* ((big (read (substring (caddr err) 0
2354 (match-beginning 1))))
2355 (small (read (match-string 1 (caddr err))))
2356 (twiddle (/ small 65536)))
2357 (cons (+ big twiddle)
2358 (- small (* twiddle 65536))))))))
2359 ;; ... file mode flags
2360 (setq res-filemodes (symbol-name (read (current-buffer))))
2361 ;; ... number links
2362 (setq res-numlinks (read (current-buffer)))
2363 ;; ... uid and gid
2364 (setq res-uid (read (current-buffer)))
2365 (setq res-gid (read (current-buffer)))
c82c5727 2366 (when (eq id-format 'integer)
fb7933a3
KG
2367 (unless (numberp res-uid) (setq res-uid -1))
2368 (unless (numberp res-gid) (setq res-gid -1)))
2369 ;; ... size
2370 (setq res-size (read (current-buffer)))
2371 ;; From the file modes, figure out other stuff.
2372 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2373 (setq dirp (eq ?d (aref res-filemodes 0)))
2374 ;; if symlink, find out file name pointed to
2375 (when symlinkp
2376 (search-forward "-> ")
2377 (setq res-symlink-target
2378 (buffer-substring (point)
2379 (tramp-line-end-position))))
2380 ;; return data gathered
2381 (list
2382 ;; 0. t for directory, string (name linked to) for symbolic
2383 ;; link, or nil.
2384 (or dirp res-symlink-target nil)
2385 ;; 1. Number of links to file.
2386 res-numlinks
2387 ;; 2. File uid.
2388 res-uid
2389 ;; 3. File gid.
2390 res-gid
2391 ;; 4. Last access time, as a list of two integers. First
2392 ;; integer has high-order 16 bits of time, second has low 16
2393 ;; bits.
2394 ;; 5. Last modification time, likewise.
2395 ;; 6. Last status change time, likewise.
2396 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2397 ;; 7. Size in bytes (-1, if number is out of range).
2398 res-size
2399 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2400 res-filemodes
2401 ;; 9. t iff file's gid would change if file were deleted and
a69c01a0
MA
2402 ;; recreated. Will be set in `tramp-convert-file-attributes'
2403 t
fb7933a3
KG
2404 ;; 10. inode number.
2405 res-inode
8daea7fc
KG
2406 ;; 11. Device number. Will be replaced by a virtual device number.
2407 -1
fb7933a3
KG
2408 )))
2409
2410(defun tramp-handle-file-attributes-with-perl
c82c5727
LH
2411 (multi-method method user host localname &optional id-format)
2412 "Implement `file-attributes' for tramp files using a Perl script."
ac474af1
KG
2413 (tramp-message-for-buffer multi-method method user host 10
2414 "file attributes with perl: %s"
2415 (tramp-make-tramp-file-name
7432277c 2416 multi-method method user host localname))
c08e6004
MA
2417 (tramp-maybe-send-perl-script multi-method method user host
2418 tramp-perl-file-attributes
2419 "tramp_file_attributes")
c82c5727
LH
2420 (tramp-send-command multi-method method user host
2421 (format "tramp_file_attributes %s %s"
2422 (tramp-shell-quote-argument localname) id-format))
fb7933a3 2423 (tramp-wait-for-output)
c82c5727 2424 (read (current-buffer)))
8daea7fc 2425
fb7933a3
KG
2426(defun tramp-handle-set-visited-file-modtime (&optional time-list)
2427 "Like `set-visited-file-modtime' for tramp files."
2428 (unless (buffer-file-name)
2429 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2430 (buffer-name)))
48ddd622
MA
2431 (if time-list
2432 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
11948172
MA
2433 (let ((f (buffer-file-name))
2434 coding-system-used)
48ddd622
MA
2435 (with-parsed-tramp-file-name f nil
2436 (let* ((attr (file-attributes f))
2437 ;; '(-1 65535) means file doesn't exists yet.
2438 (modtime (or (nth 5 attr) '(-1 65535))))
11948172
MA
2439 (when (boundp 'last-coding-system-used)
2440 (setq coding-system-used (symbol-value 'last-coding-system-used)))
48ddd622
MA
2441 ;; We use '(0 0) as a don't-know value. See also
2442 ;; `tramp-handle-file-attributes-with-ls'.
48ddd622
MA
2443 (if (not (equal modtime '(0 0)))
2444 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2445 (save-excursion
2446 (tramp-send-command
2447 multi-method method user host
2448 (format "%s -ild %s"
2449 (tramp-get-ls-command multi-method method user host)
2450 (tramp-shell-quote-argument localname)))
2451 (tramp-wait-for-output)
2452 (setq attr (buffer-substring (point)
2453 (progn (end-of-line) (point)))))
2454 (setq tramp-buffer-file-attributes attr))
11948172
MA
2455 (when (boundp 'last-coding-system-used)
2456 (set 'last-coding-system-used coding-system-used))
d2a2c17f 2457 nil)))))
fb7933a3 2458
c62c9d08
KG
2459;; CCC continue here
2460
2461;; This function makes the same assumption as
2462;; `tramp-handle-set-visited-file-modtime'.
2463(defun tramp-handle-verify-visited-file-modtime (buf)
c08e6004
MA
2464 "Like `verify-visited-file-modtime' for tramp files.
2465At the time `verify-visited-file-modtime' calls this function, we
2466already know that the buffer is visiting a file and that
2467`visited-file-modtime' does not return 0. Do not call this
2468function directly, unless those two cases are already taken care
2469of."
c62c9d08 2470 (with-current-buffer buf
b15d0c4c
MA
2471 ;; There is no file visiting the buffer, or the buffer has no
2472 ;; recorded last modification time.
2473 (if (or (not (buffer-file-name))
2474 (eq (visited-file-modtime) 0))
d2a2c17f 2475 t
b15d0c4c
MA
2476 (let ((f (buffer-file-name)))
2477 (with-parsed-tramp-file-name f nil
2478 (let* ((attr (file-attributes f))
2479 (modtime (nth 5 attr))
2480 (mt (visited-file-modtime)))
bf247b6e 2481
b15d0c4c
MA
2482 (cond
2483 ;; file exists, and has a known modtime.
2484 ((and attr (not (equal modtime '(0 0))))
2485 (< (abs (tramp-time-diff
2486 modtime
2487 ;; For compatibility, deal with both the old
2488 ;; (HIGH . LOW) and the new (HIGH LOW)
2489 ;; return values of `visited-file-modtime'.
2490 (if (atom (cdr mt))
2491 (list (car mt) (cdr mt))
2492 mt)))
2493 2))
2494 ;; modtime has the don't know value.
2495 (attr
2496 (save-excursion
2497 (tramp-send-command
2498 multi-method method user host
2499 (format "%s -ild %s"
2500 (tramp-get-ls-command multi-method method user host)
2501 (tramp-shell-quote-argument localname)))
2502 (tramp-wait-for-output)
2503 (setq attr (buffer-substring
2504 (point) (progn (end-of-line) (point)))))
2505 (equal tramp-buffer-file-attributes attr))
2506 ;; If file does not exist, say it is not modified
2507 ;; if and only if that agrees with the buffer's record.
2508 (t (equal mt '(-1 65535))))))))))
c62c9d08 2509
fb7933a3
KG
2510(defun tramp-handle-set-file-modes (filename mode)
2511 "Like `set-file-modes' for tramp files."
c62c9d08 2512 (with-parsed-tramp-file-name filename nil
fb7933a3
KG
2513 (save-excursion
2514 (unless (zerop (tramp-send-command-and-check
c62c9d08
KG
2515 multi-method method user host
2516 (format "chmod %s %s"
2517 (tramp-decimal-to-octal mode)
7432277c 2518 (tramp-shell-quote-argument localname))))
fb7933a3
KG
2519 (signal 'file-error
2520 (list "Doing chmod"
2521 ;; FIXME: extract the proper text from chmod's stderr.
2522 "error while changing file's mode"
2523 filename))))))
2524
2525;; Simple functions using the `test' command.
2526
2527(defun tramp-handle-file-executable-p (filename)
2528 "Like `file-executable-p' for tramp files."
c62c9d08 2529 (with-parsed-tramp-file-name filename nil
c62c9d08 2530 (zerop (tramp-run-test "-x" filename))))
fb7933a3
KG
2531
2532(defun tramp-handle-file-readable-p (filename)
2533 "Like `file-readable-p' for tramp files."
c62c9d08 2534 (with-parsed-tramp-file-name filename nil
c62c9d08 2535 (zerop (tramp-run-test "-r" filename))))
fb7933a3
KG
2536
2537(defun tramp-handle-file-accessible-directory-p (filename)
2538 "Like `file-accessible-directory-p' for tramp files."
c62c9d08 2539 (with-parsed-tramp-file-name filename nil
c62c9d08
KG
2540 (and (zerop (tramp-run-test "-d" filename))
2541 (zerop (tramp-run-test "-r" filename))
2542 (zerop (tramp-run-test "-x" filename)))))
fb7933a3
KG
2543
2544;; When the remote shell is started, it looks for a shell which groks
2545;; tilde expansion. Here, we assume that all shells which grok tilde
2546;; expansion will also provide a `test' command which groks `-nt' (for
2547;; newer than). If this breaks, tell me about it and I'll try to do
2548;; something smarter about it.
2549(defun tramp-handle-file-newer-than-file-p (file1 file2)
2550 "Like `file-newer-than-file-p' for tramp files."
2551 (cond ((not (file-exists-p file1))
2552 nil)
2553 ((not (file-exists-p file2))
2554 t)
91879624 2555 ;; We are sure both files exist at this point.
fb7933a3
KG
2556 (t
2557 (save-excursion
91879624
KG
2558 ;; We try to get the mtime of both files. If they are not
2559 ;; equal to the "dont-know" value, then we subtract the times
2560 ;; and obtain the result.
2561 (let ((fa1 (file-attributes file1))
2562 (fa2 (file-attributes file2)))
2563 (if (and (not (equal (nth 5 fa1) '(0 0)))
2564 (not (equal (nth 5 fa2) '(0 0))))
01917a18 2565 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
91879624
KG
2566 ;; If one of them is the dont-know value, then we can
2567 ;; still try to run a shell command on the remote host.
2568 ;; However, this only works if both files are Tramp
2569 ;; files and both have the same method, same user, same
2570 ;; host.
c62c9d08
KG
2571 (unless (and (tramp-tramp-file-p file1)
2572 (tramp-tramp-file-p file2))
91879624
KG
2573 (signal
2574 'file-error
2575 (list
2576 "Cannot check if Tramp file is newer than non-Tramp file"
2577 file1 file2)))
2578 (with-parsed-tramp-file-name file1 v1
2579 (with-parsed-tramp-file-name file2 v2
91879624
KG
2580 (unless (and (equal v1-multi-method v2-multi-method)
2581 (equal v1-method v2-method)
2582 (equal v1-user v2-user)
2583 (equal v1-host v2-host))
2584 (signal 'file-error
2585 (list "Files must have same method, user, host"
2586 file1 file2)))
2587 (unless (and (tramp-tramp-file-p file1)
2588 (tramp-tramp-file-p file2))
2589 (signal 'file-error
2590 (list "Files must be tramp files on same host"
2591 file1 file2)))
2592 (if (tramp-get-test-groks-nt
2593 v1-multi-method v1-method v1-user v1-host)
2594 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2595 (zerop (tramp-run-test2
2596 "tramp_test_nt" file1 file2)))))))))))
fb7933a3
KG
2597
2598;; Functions implemented using the basic functions above.
2599
2600(defun tramp-handle-file-modes (filename)
2601 "Like `file-modes' for tramp files."
c62c9d08 2602 (with-parsed-tramp-file-name filename nil
c62c9d08
KG
2603 (when (file-exists-p filename)
2604 (tramp-mode-string-to-int
4007ba5b 2605 (nth 8 (file-attributes filename))))))
fb7933a3
KG
2606
2607(defun tramp-handle-file-directory-p (filename)
2608 "Like `file-directory-p' for tramp files."
2609 ;; Care must be taken that this function returns `t' for symlinks
2610 ;; pointing to directories. Surely the most obvious implementation
2611 ;; would be `test -d', but that returns false for such symlinks.
2612 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2613 ;; I now think he's right. So we could be using `test -d', couldn't
2614 ;; we?
2615 ;;
2616 ;; Alternatives: `cd %s', `test -d %s'
c62c9d08 2617 (with-parsed-tramp-file-name filename nil
c62c9d08 2618 (save-excursion
fb7933a3
KG
2619 (zerop
2620 (tramp-send-command-and-check
c62c9d08
KG
2621 multi-method method user host
2622 (format "test -d %s"
7432277c 2623 (tramp-shell-quote-argument localname))
c62c9d08 2624 t))))) ;run command in subshell
fb7933a3
KG
2625
2626(defun tramp-handle-file-regular-p (filename)
2627 "Like `file-regular-p' for tramp files."
c62c9d08 2628 (with-parsed-tramp-file-name filename nil
8daea7fc
KG
2629 (and (file-exists-p filename)
2630 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
fb7933a3
KG
2631
2632(defun tramp-handle-file-symlink-p (filename)
2633 "Like `file-symlink-p' for tramp files."
c62c9d08 2634 (with-parsed-tramp-file-name filename nil
c951aecb 2635 (let ((x (car (file-attributes filename))))
b25a52cc
KG
2636 (when (stringp x)
2637 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2638 ;; might do weird things.
2639 (if (file-name-absolute-p x)
2640 (tramp-make-tramp-file-name
2641 multi-method method user host x)
2642 x)))))
fb7933a3
KG
2643
2644(defun tramp-handle-file-writable-p (filename)
2645 "Like `file-writable-p' for tramp files."
c62c9d08 2646 (with-parsed-tramp-file-name filename nil
07dfe738 2647 (if (file-exists-p filename)
c62c9d08
KG
2648 ;; Existing files must be writable.
2649 (zerop (tramp-run-test "-w" filename))
2650 ;; If file doesn't exist, check if directory is writable.
2651 (and (zerop (tramp-run-test
07dfe738 2652 "-d" (file-name-directory filename)))
c62c9d08 2653 (zerop (tramp-run-test
07dfe738 2654 "-w" (file-name-directory filename)))))))
fb7933a3
KG
2655
2656(defun tramp-handle-file-ownership-preserved-p (filename)
2657 "Like `file-ownership-preserved-p' for tramp files."
c62c9d08 2658 (with-parsed-tramp-file-name filename nil
a69c01a0
MA
2659 (let ((attributes (file-attributes filename)))
2660 ;; Return t if the file doesn't exist, since it's true that no
2661 ;; information would be lost by an (attempted) delete and create.
2662 (or (null attributes)
2663 (= (nth 2 attributes)
2664 (tramp-get-remote-uid multi-method method user host))))))
fb7933a3
KG
2665
2666;; Other file name ops.
2667
83bbd71b 2668;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
fb7933a3
KG
2669;; (defun tramp-handle-directory-file-name (directory)
2670;; "Like `directory-file-name' for tramp files."
2671;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2672;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2673;; (substring directory 0 (- (length directory) 1))
2674;; directory))
2675
8daea7fc
KG
2676;; ;; Philippe Troin <phil@fifi.org>
2677;; (defun tramp-handle-directory-file-name (directory)
2678;; "Like `directory-file-name' for tramp files."
2679;; (with-parsed-tramp-file-name directory nil
2680;; (let ((directory-length-1 (1- (length directory))))
2681;; (save-match-data
2682;; (if (and (eq (aref directory directory-length-1) ?/)
2683;; (eq (string-match tramp-file-name-regexp directory) 0)
2684;; (/= (match-end 0) directory-length-1))
2685;; (substring directory 0 directory-length-1)
2686;; directory)))))
2687
fb7933a3
KG
2688(defun tramp-handle-directory-file-name (directory)
2689 "Like `directory-file-name' for tramp files."
7432277c
KG
2690 ;; If localname component of filename is "/", leave it unchanged.
2691 ;; Otherwise, remove any trailing slash from localname component.
8daea7fc
KG
2692 ;; Method, host, etc, are unchanged. Does it make sense to try
2693 ;; to avoid parsing the filename?
c62c9d08 2694 (with-parsed-tramp-file-name directory nil
7432277c
KG
2695 (if (and (not (zerop (length localname)))
2696 (eq (aref localname (1- (length localname))) ?/)
2697 (not (string= localname "/")))
8daea7fc
KG
2698 (substring directory 0 -1)
2699 directory)))
fb7933a3
KG
2700
2701;; Directory listings.
2702
ac474af1
KG
2703(defun tramp-handle-directory-files (directory
2704 &optional full match nosort files-only)
fb7933a3 2705 "Like `directory-files' for tramp files."
c62c9d08 2706 (with-parsed-tramp-file-name directory nil
c62c9d08 2707 (let (result x)
fb7933a3
KG
2708 (save-excursion
2709 (tramp-barf-unless-okay
2710 multi-method method user host
7432277c 2711 (concat "cd " (tramp-shell-quote-argument localname))
c62c9d08
KG
2712 nil
2713 'file-error
2714 "tramp-handle-directory-files: couldn't `cd %s'"
7432277c 2715 (tramp-shell-quote-argument localname))
fb7933a3
KG
2716 (tramp-send-command
2717 multi-method method user host
c62c9d08
KG
2718 (concat (tramp-get-ls-command multi-method method user host)
2719 " -a | cat"))
fb7933a3
KG
2720 (tramp-wait-for-output)
2721 (goto-char (point-max))
2722 (while (zerop (forward-line -1))
c62c9d08
KG
2723 (setq x (buffer-substring (point)
2724 (tramp-line-end-position)))
2725 (when (or (not match) (string-match match x))
2726 (if full
2727 (push (concat (file-name-as-directory directory)
2728 x)
2729 result)
2730 (push x result))))
fb7933a3 2731 (tramp-send-command multi-method method user host "cd")
ac474af1
KG
2732 (tramp-wait-for-output)
2733 ;; Remove non-files or non-directories if necessary. Using
2734 ;; the remote shell for this would probably be way faster.
2735 ;; Maybe something could be adapted from
2736 ;; tramp-handle-file-name-all-completions.
2737 (when files-only
2738 (let ((temp (nreverse result))
2739 item)
2740 (setq result nil)
2741 (if (equal files-only t)
2742 ;; files only
2743 (while temp
2744 (setq item (pop temp))
2745 (when (file-regular-p item)
2746 (push item result)))
2747 ;; directories only
2748 (while temp
2749 (setq item (pop temp))
2750 (when (file-directory-p item)
2751 (push item result)))))))
c62c9d08
KG
2752 result)))
2753
c82c5727
LH
2754(defun tramp-handle-directory-files-and-attributes
2755 (directory &optional full match nosort id-format)
2756 "Like `directory-files-and-attributes' for tramp files."
2757 (when (tramp-handle-file-exists-p directory)
2758 (save-excursion
2759 (setq directory (tramp-handle-expand-file-name directory))
2760 (with-parsed-tramp-file-name directory nil
c08e6004
MA
2761 (tramp-maybe-send-perl-script multi-method method user host
2762 tramp-perl-directory-files-and-attributes
2763 "tramp_directory_files_and_attributes")
c82c5727
LH
2764 (tramp-send-command multi-method method user host
2765 (format "tramp_directory_files_and_attributes %s %s"
2766 (tramp-shell-quote-argument localname)
2767 (or id-format 'integer)))
2768 (tramp-wait-for-output)
8cb0a559
LH
2769 (let* ((root (cons nil (let ((object (read (current-buffer))))
2770 (when (stringp object)
2771 (error object))
2772 object)))
c82c5727
LH
2773 (cell root))
2774 (while (cdr cell)
2775 (if (and match (not (string-match match (caadr cell))))
2776 ;; Remove from list
2777 (setcdr cell (cddr cell))
2778 ;; Include in list
2779 (setq cell (cdr cell))
2780 (let ((l (car cell)))
2781 (tramp-convert-file-attributes multi-method method user host
2782 (cdr l))
2783 ;; If FULL, make file name absolute
2784 (when full (setcar l (concat directory "/" (car l)))))))
2785 (if nosort
2786 (cdr root)
2787 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2788
c62c9d08
KG
2789;; This function should return "foo/" for directories and "bar" for
2790;; files. We use `ls -ad' to get a list of files (including
2791;; directories), and `find . -type d \! -name . -prune' to get a list
2792;; of directories.
2793(defun tramp-handle-file-name-all-completions (filename directory)
2794 "Like `file-name-all-completions' for tramp files."
2795 (with-parsed-tramp-file-name directory nil
c62c9d08
KG
2796 (unless (save-match-data (string-match "/" filename))
2797 (let* ((nowild tramp-completion-without-shell-p)
2798 result)
2799 (save-excursion
2800 (tramp-barf-unless-okay
2801 multi-method method user host
7432277c 2802 (format "cd %s" (tramp-shell-quote-argument localname))
c62c9d08
KG
2803 nil 'file-error
2804 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
7432277c 2805 (tramp-shell-quote-argument localname))
c62c9d08
KG
2806
2807 ;; Get a list of directories and files, including reliably
2808 ;; tagging the directories with a trailing '/'. Because I
2809 ;; rock. --daniel@danann.net
2810 (tramp-send-command
2811 multi-method method user host
2812 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2813 "if test -d \"$f\" 2>/dev/null; "
2814 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2815 (tramp-get-ls-command multi-method method user host)
2816 (if (or nowild (zerop (length filename)))
2817 ""
2818 (format "-d %s*"
2819 (tramp-shell-quote-argument filename)))))
2820
2821 ;; Now grab the output.
2822 (tramp-wait-for-output)
2823 (goto-char (point-max))
2824 (while (zerop (forward-line -1))
2825 (push (buffer-substring (point)
2826 (tramp-line-end-position))
2827 result))
bf247b6e 2828
c62c9d08
KG
2829 (tramp-send-command multi-method method user host "cd")
2830 (tramp-wait-for-output)
fb7933a3 2831
c62c9d08
KG
2832 ;; Return the list.
2833 (if nowild
2834 (all-completions filename (mapcar 'list result))
2835 result))))))
fb7933a3
KG
2836
2837
2838;; The following isn't needed for Emacs 20 but for 19.34?
2839(defun tramp-handle-file-name-completion (filename directory)
2840 "Like `file-name-completion' for tramp files."
2841 (unless (tramp-tramp-file-p directory)
2842 (error
2843 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2844 directory))
c62c9d08 2845 (with-parsed-tramp-file-name directory nil
c62c9d08
KG
2846 (try-completion
2847 filename
2848 (mapcar (lambda (x) (cons x nil))
4007ba5b 2849 (file-name-all-completions filename directory)))))
fb7933a3
KG
2850
2851;; cp, mv and ln
2852
2853(defun tramp-handle-add-name-to-file
2854 (filename newname &optional ok-if-already-exists)
2855 "Like `add-name-to-file' for tramp files."
c62c9d08
KG
2856 (with-parsed-tramp-file-name filename v1
2857 (with-parsed-tramp-file-name newname v2
2858 (let ((ln (when v1 (tramp-get-remote-ln
2859 v1-multi-method v1-method v1-user v1-host))))
2860 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2861 (equal v1-multi-method v2-multi-method)
2862 (equal v1-method v2-method)
2863 (equal v1-user v2-user)
2864 (equal v1-host v2-host))
2865 (error "add-name-to-file: %s"
2866 "only implemented for same method, same user, same host"))
c62c9d08
KG
2867 (when (and (not ok-if-already-exists)
2868 (file-exists-p newname)
2869 (not (numberp ok-if-already-exists))
2870 (y-or-n-p
2871 (format
2872 "File %s already exists; make it a new name anyway? "
2873 newname)))
2874 (error "add-name-to-file: file %s already exists" newname))
2875 (tramp-barf-unless-okay
2876 v1-multi-method v1-method v1-user v1-host
7432277c
KG
2877 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2878 (tramp-shell-quote-argument v2-localname))
c62c9d08
KG
2879 nil 'file-error
2880 "error with add-name-to-file, see buffer `%s' for details"
2881 (buffer-name))))))
fb7933a3
KG
2882
2883(defun tramp-handle-copy-file
2884 (filename newname &optional ok-if-already-exists keep-date)
2885 "Like `copy-file' for tramp files."
2886 ;; Check if both files are local -- invoke normal copy-file.
2887 ;; Otherwise, use tramp from local system.
2888 (setq filename (expand-file-name filename))
2889 (setq newname (expand-file-name newname))
2890 ;; At least one file a tramp file?
2891 (if (or (tramp-tramp-file-p filename)
2892 (tramp-tramp-file-p newname))
01917a18
MA
2893 (tramp-do-copy-or-rename-file
2894 'copy filename newname ok-if-already-exists keep-date)
fb7933a3
KG
2895 (tramp-run-real-handler
2896 'copy-file
2897 (list filename newname ok-if-already-exists keep-date))))
2898
2899(defun tramp-handle-rename-file
2900 (filename newname &optional ok-if-already-exists)
2901 "Like `rename-file' for tramp files."
2902 ;; Check if both files are local -- invoke normal rename-file.
2903 ;; Otherwise, use tramp from local system.
2904 (setq filename (expand-file-name filename))
2905 (setq newname (expand-file-name newname))
2906 ;; At least one file a tramp file?
2907 (if (or (tramp-tramp-file-p filename)
2908 (tramp-tramp-file-p newname))
2909 (tramp-do-copy-or-rename-file
2910 'rename filename newname ok-if-already-exists)
2911 (tramp-run-real-handler 'rename-file
2912 (list filename newname ok-if-already-exists))))
2913
2914(defun tramp-do-copy-or-rename-file
2915 (op filename newname &optional ok-if-already-exists keep-date)
2916 "Copy or rename a remote file.
2917OP must be `copy' or `rename' and indicates the operation to perform.
2918FILENAME specifies the file to copy or rename, NEWNAME is the name of
2919the new file (for copy) or the new name of the file (for rename).
2920OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2921KEEP-DATE means to make sure that NEWNAME has the same timestamp
2922as FILENAME.
2923
2924This function is invoked by `tramp-handle-copy-file' and
2925`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2926and `rename'. FILENAME and NEWNAME must be absolute file names."
2927 (unless (memq op '(copy rename))
2928 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2929 (unless ok-if-already-exists
2930 (when (file-exists-p newname)
2931 (signal 'file-already-exists
2932 (list newname))))
90dc758d 2933 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41
KG
2934 (t2 (tramp-tramp-file-p newname))
2935 v1-multi-method v1-method v1-user v1-host v1-localname
2936 v2-multi-method v2-method v2-user v2-host v2-localname)
2937
90dc758d 2938 ;; Check which ones of source and target are Tramp files.
5ec2cc41
KG
2939 ;; We cannot invoke `with-parsed-tramp-file-name';
2940 ;; it fails if the file isn't a Tramp file name.
2941 (if t1
2942 (with-parsed-tramp-file-name filename l
2943 (setq v1-multi-method l-multi-method
2944 v1-method l-method
2945 v1-user l-user
2946 v1-host l-host
2947 v1-localname l-localname))
2948 (setq v1-localname filename))
2949 (if t2
2950 (with-parsed-tramp-file-name newname l
2951 (setq v2-multi-method l-multi-method
2952 v2-method l-method
2953 v2-user l-user
2954 v2-host l-host
2955 v2-localname l-localname))
2956 (setq v2-localname newname))
2957
90dc758d 2958 (cond
5ec2cc41 2959 ;; Both are Tramp files.
90dc758d 2960 ((and t1 t2)
5ec2cc41
KG
2961 (cond
2962 ;; Shortcut: if method, host, user are the same for both
2963 ;; files, we invoke `cp' or `mv' on the remote host
2964 ;; directly.
2965 ((and (equal v1-multi-method v2-multi-method)
2966 (equal v1-method v2-method)
2967 (equal v1-user v2-user)
2968 (equal v1-host v2-host))
2969 (tramp-do-copy-or-rename-file-directly
2970 op v1-multi-method v1-method v1-user v1-host
2971 v1-localname v2-localname keep-date))
2972 ;; If both source and target are Tramp files,
2973 ;; both are using the same copy-program, then we
2974 ;; can invoke rcp directly. Note that
2975 ;; default-directory should point to a local
2976 ;; directory if we want to invoke rcp.
2977 ((and (not v1-multi-method)
2978 (not v2-multi-method)
2979 (equal v1-method v2-method)
2980 (tramp-method-out-of-band-p
2981 v1-multi-method v1-method v1-user v1-host)
2982 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
2983 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
2984 (tramp-do-copy-or-rename-file-out-of-band
2985 op filename newname keep-date))
2986 ;; No shortcut was possible. So we copy the
2987 ;; file first. If the operation was `rename', we go
2988 ;; back and delete the original file (if the copy was
2989 ;; successful). The approach is simple-minded: we
2990 ;; create a new buffer, insert the contents of the
2991 ;; source file into it, then write out the buffer to
2992 ;; the target file. The advantage is that it doesn't
2993 ;; matter which filename handlers are used for the
2994 ;; source and target file.
2995 (t
38c65fca 2996 (tramp-do-copy-or-rename-file-via-buffer
5ec2cc41
KG
2997 op filename newname keep-date))))
2998
2999 ;; One file is a Tramp file, the other one is local.
7432277c 3000 ((or t1 t2)
5ec2cc41
KG
3001 ;; If the Tramp file has an out-of-band method, the corresponding
3002 ;; copy-program can be invoked.
3003 (if (and (not v1-multi-method)
3004 (not v2-multi-method)
3005 (or (tramp-method-out-of-band-p
3006 v1-multi-method v1-method v1-user v1-host)
3007 (tramp-method-out-of-band-p
3008 v2-multi-method v2-method v2-user v2-host)))
3009 (tramp-do-copy-or-rename-file-out-of-band
3010 op filename newname keep-date)
3011 ;; Use the generic method via a Tramp buffer.
38c65fca
KG
3012 (tramp-do-copy-or-rename-file-via-buffer
3013 op filename newname keep-date)))
5ec2cc41 3014
7432277c
KG
3015 (t
3016 ;; One of them must be a Tramp file.
3017 (error "Tramp implementation says this cannot happen")))))
3018
38c65fca 3019(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
90dc758d
KG
3020 "Use an Emacs buffer to copy or rename a file.
3021First arg OP is either `copy' or `rename' and indicates the operation.
3022FILENAME is the source file, NEWNAME the target file.
3023KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
5ec2cc41
KG
3024 (let ((trampbuf (get-buffer-create "*tramp output*"))
3025 (modtime (nth 5 (file-attributes filename))))
3026 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
90dc758d
KG
3027 (tramp-message
3028 1 (concat "Warning: cannot preserve file time stamp"
3029 " with inline copying across machines")))
3030 (save-excursion
3031 (set-buffer trampbuf) (erase-buffer)
3032 (insert-file-contents-literally filename)
ea9d1443
KG
3033 ;; We don't want the target file to be compressed, so we let-bind
3034 ;; `jka-compr-inhibit' to t.
3035 (let ((coding-system-for-write 'binary)
3036 (jka-compr-inhibit t))
5ec2cc41
KG
3037 (write-region (point-min) (point-max) newname))
3038 ;; KEEP-DATE handling.
38c65fca
KG
3039 (when keep-date
3040 (when (and (not (null modtime))
3041 (not (equal modtime '(0 0))))
01917a18
MA
3042 (tramp-touch newname modtime)))
3043 ;; Set the mode.
3044 (set-file-modes newname (file-modes filename)))
90dc758d
KG
3045 ;; If the operation was `rename', delete the original file.
3046 (unless (eq op 'copy)
3047 (delete-file filename))))
fb7933a3
KG
3048
3049(defun tramp-do-copy-or-rename-file-directly
7432277c 3050 (op multi-method method user host localname1 localname2 keep-date)
fb7933a3
KG
3051 "Invokes `cp' or `mv' on the remote system.
3052OP must be one of `copy' or `rename', indicating `cp' or `mv',
3053respectively. METHOD, USER, and HOST specify the connection.
7432277c 3054LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
fb7933a3
KG
3055If KEEP-DATE is non-nil, preserve the time stamp when copying."
3056 ;; CCC: What happens to the timestamp when renaming?
3057 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3058 ((eq op 'copy) "cp -f")
3059 ((eq op 'rename) "mv -f")
3060 (t (error
3061 "Unknown operation `%s', must be `copy' or `rename'"
3062 op)))))
3063 (save-excursion
01917a18 3064 (tramp-send-command
fb7933a3
KG
3065 multi-method method user host
3066 (format "%s %s %s"
3067 cmd
7432277c 3068 (tramp-shell-quote-argument localname1)
01917a18
MA
3069 (tramp-shell-quote-argument localname2)))
3070 (tramp-wait-for-output)
3071 (goto-char (point-min))
3072 (unless
3073 (or
3074 (and (eq op 'copy) keep-date
3075 ;; Mask cp -f error.
3076 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3077 (zerop (tramp-send-command-and-check
3078 multi-method method user host nil nil)))
3079 (pop-to-buffer (current-buffer))
3080 (signal 'file-error
3081 (format "Copying directly failed, see buffer `%s' for details."
3082 (buffer-name)))))
3083 ;; Set the mode.
3084 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3085 ;; where available?
3086 (unless (or (eq op 'rename) keep-date)
3087 (set-file-modes
3088 (tramp-make-tramp-file-name multi-method method user host localname2)
3089 (file-modes
3090 (tramp-make-tramp-file-name
3091 multi-method method user host localname1))))))
fb7933a3 3092
5ec2cc41 3093(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
7432277c
KG
3094 "Invoke rcp program to copy.
3095One of FILENAME and NEWNAME must be a Tramp name, the other must
3096be a local filename. The method used must be an out-of-band method."
38c65fca 3097 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41
KG
3098 (t2 (tramp-tramp-file-p newname))
3099 v1-multi-method v1-method v1-user v1-host v1-localname
3100 v2-multi-method v2-method v2-user v2-host v2-localname
38c65fca
KG
3101 multi-method method user host copy-program copy-args
3102 source target trampbuf)
5ec2cc41
KG
3103
3104 ;; Check which ones of source and target are Tramp files.
3105 ;; We cannot invoke `with-parsed-tramp-file-name';
3106 ;; it fails if the file isn't a Tramp file name.
3107 (if t1
3108 (with-parsed-tramp-file-name filename l
3109 (setq v1-multi-method l-multi-method
3110 v1-method l-method
3111 v1-user l-user
3112 v1-host l-host
3113 v1-localname l-localname
38c65fca 3114 multi-method l-multi-method
5ec2cc41
KG
3115 method (tramp-find-method
3116 v1-multi-method v1-method v1-user v1-host)
38c65fca
KG
3117 user l-user
3118 host l-host
5ec2cc41
KG
3119 copy-program (tramp-get-method-parameter
3120 v1-multi-method method
3121 v1-user v1-host 'tramp-copy-program)
3122 copy-args (tramp-get-method-parameter
3123 v1-multi-method method
3124 v1-user v1-host 'tramp-copy-args)))
3125 (setq v1-localname filename))
3126
3127 (if t2
3128 (with-parsed-tramp-file-name newname l
3129 (setq v2-multi-method l-multi-method
3130 v2-method l-method
3131 v2-user l-user
3132 v2-host l-host
3133 v2-localname l-localname
38c65fca 3134 multi-method l-multi-method
5ec2cc41
KG
3135 method (tramp-find-method
3136 v2-multi-method v2-method v2-user v2-host)
38c65fca
KG
3137 user l-user
3138 host l-host
5ec2cc41
KG
3139 copy-program (tramp-get-method-parameter
3140 v2-multi-method method
3141 v2-user v2-host 'tramp-copy-program)
3142 copy-args (tramp-get-method-parameter
3143 v2-multi-method method
3144 v2-user v2-host 'tramp-copy-args)))
3145 (setq v2-localname newname))
3146
3147 ;; The following should be changed. We need a more general
3148 ;; mechanism to parse extra host args.
3149 (if (not t1)
3150 (setq source v1-localname)
3151 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3152 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3153 (setq v1-host (match-string 1 v1-host)))
3154 (setq source
3155 (tramp-make-copy-program-file-name
3156 v1-user v1-host
3157 (tramp-shell-quote-argument v1-localname))))
3158
3159 (if (not t2)
3160 (setq target v2-localname)
3161 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3162 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3163 (setq v2-host (match-string 1 v2-host)))
3164 (setq target
3165 (tramp-make-copy-program-file-name
3166 v2-user v2-host
3167 (tramp-shell-quote-argument v2-localname))))
3168
3169 ;; Handle keep-date argument
3170 (when keep-date
3171 (if t1
3172 (setq copy-args
3173 (cons (tramp-get-method-parameter
3174 v1-multi-method method
3175 v1-user v1-host 'tramp-copy-keep-date-arg)
3176 copy-args))
3177 (setq copy-args
3178 (cons (tramp-get-method-parameter
3179 v2-multi-method method
3180 v2-user v2-host 'tramp-copy-keep-date-arg)
3181 copy-args))))
3182
38c65fca
KG
3183 (setq copy-args (append copy-args (list source target))
3184 trampbuf (generate-new-buffer
3185 (tramp-buffer-name multi-method method user host)))
5ec2cc41 3186
38c65fca
KG
3187 ;; Use an asynchronous process. By this, password can be handled.
3188 (save-excursion
c08e6004
MA
3189
3190 ;; Check for program.
3191 (when (and (fboundp 'executable-find)
3192 (not (executable-find copy-program)))
3193 (error "Cannot find copy program: %s" copy-program))
3194
38c65fca
KG
3195 (set-buffer trampbuf)
3196 (setq tramp-current-multi-method multi-method
3197 tramp-current-method method
3198 tramp-current-user user
3199 tramp-current-host host)
d2a2c17f 3200 (message "Transferring %s to %s..." filename newname)
38c65fca
KG
3201
3202 ;; Use rcp-like program for file transfer.
3203 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3204 copy-program copy-args)))
19a87064 3205 (tramp-set-process-query-on-exit-flag p nil)
38c65fca
KG
3206 (tramp-process-actions p multi-method method user host
3207 tramp-actions-copy-out-of-band))
3208 (kill-buffer trampbuf)
d2a2c17f 3209 (message "Transferring %s to %s...done" filename newname)
01917a18
MA
3210
3211 ;; Set the mode.
3212 (unless keep-date
3213 (set-file-modes newname (file-modes filename))))
5ec2cc41
KG
3214
3215 ;; If the operation was `rename', delete the original file.
3216 (unless (eq op 'copy)
3217 (delete-file filename))))
7432277c 3218
fb7933a3
KG
3219;; mkdir
3220(defun tramp-handle-make-directory (dir &optional parents)
3221 "Like `make-directory' for tramp files."
ac474af1 3222 (setq dir (expand-file-name dir))
c62c9d08 3223 (with-parsed-tramp-file-name dir nil
b1d06e75
KG
3224 (save-excursion
3225 (tramp-barf-unless-okay
3226 multi-method method user host
3227 (format " %s %s"
3228 (if parents "mkdir -p" "mkdir")
7432277c 3229 (tramp-shell-quote-argument localname))
b1d06e75
KG
3230 nil 'file-error
3231 "Couldn't make directory %s" dir))))
fb7933a3
KG
3232
3233;; CCC error checking?
3234(defun tramp-handle-delete-directory (directory)
3235 "Like `delete-directory' for tramp files."
ac474af1 3236 (setq directory (expand-file-name directory))
c62c9d08 3237 (with-parsed-tramp-file-name directory nil
fb7933a3
KG
3238 (save-excursion
3239 (tramp-send-command
c62c9d08 3240 multi-method method user host
fb7933a3 3241 (format "rmdir %s ; echo ok"
7432277c 3242 (tramp-shell-quote-argument localname)))
fb7933a3
KG
3243 (tramp-wait-for-output))))
3244
3245(defun tramp-handle-delete-file (filename)
3246 "Like `delete-file' for tramp files."
ac474af1 3247 (setq filename (expand-file-name filename))
c62c9d08 3248 (with-parsed-tramp-file-name filename nil
487fa986
KG
3249 (save-excursion
3250 (unless (zerop (tramp-send-command-and-check
3251 multi-method method user host
3252 (format "rm -f %s"
7432277c 3253 (tramp-shell-quote-argument localname))))
487fa986 3254 (signal 'file-error "Couldn't delete Tramp file")))))
fb7933a3
KG
3255
3256;; Dired.
3257
3258;; CCC: This does not seem to be enough. Something dies when
3259;; we try and delete two directories under TRAMP :/
3260(defun tramp-handle-dired-recursive-delete-directory (filename)
3261 "Recursively delete the directory given.
3262This is like `dired-recursive-delete-directory' for tramp files."
c62c9d08 3263 (with-parsed-tramp-file-name filename nil
7432277c 3264 ;; run a shell command 'rm -r <localname>'
fb7933a3 3265 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
07dfe738 3266 (or (file-exists-p filename)
fb7933a3
KG
3267 (signal
3268 'file-error
3269 (list "Removing old file name" "no such directory" filename)))
3270 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
bf247b6e 3271 (tramp-send-command multi-method method user host
7432277c 3272 (format "rm -r %s" (tramp-shell-quote-argument localname)))
fb7933a3
KG
3273 ;; Wait for the remote system to return to us...
3274 ;; This might take a while, allow it plenty of time.
3275 (tramp-wait-for-output 120)
3276 ;; Make sure that it worked...
07dfe738 3277 (and (file-exists-p filename)
c08e6004 3278 (error "Failed to recursively delete %s" filename))))
bf247b6e 3279
fb7933a3
KG
3280(defun tramp-handle-dired-call-process (program discard &rest arguments)
3281 "Like `dired-call-process' for tramp files."
c62c9d08 3282 (with-parsed-tramp-file-name default-directory nil
fb7933a3
KG
3283 (save-excursion
3284 (tramp-barf-unless-okay
3285 multi-method method user host
7432277c 3286 (format "cd %s" (tramp-shell-quote-argument localname))
fb7933a3
KG
3287 nil 'file-error
3288 "tramp-handle-dired-call-process: Couldn't `cd %s'"
7432277c 3289 (tramp-shell-quote-argument localname))
fb7933a3
KG
3290 (tramp-send-command
3291 multi-method method user host
3292 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3293 (tramp-wait-for-output))
3294 (unless discard
7177e2a3
MA
3295 ;; We cannot use `insert-buffer' because the tramp buffer
3296 ;; changes its contents before insertion due to calling
3297 ;; `expand-file' and alike.
3298 (insert
3299 (with-current-buffer
3300 (tramp-get-buffer multi-method method user host)
3301 (buffer-string))))
fb7933a3
KG
3302 (save-excursion
3303 (prog1
3304 (tramp-send-command-and-check multi-method method user host nil)
3305 (tramp-send-command multi-method method user host "cd")
3306 (tramp-wait-for-output)))))
bf247b6e 3307
5ec2cc41
KG
3308(defun tramp-handle-dired-compress-file (file &rest ok-flag)
3309 "Like `dired-compress-file' for tramp files."
3310 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3311 ;; Code stolen mainly from dired-aux.el.
3312 (with-parsed-tramp-file-name file nil
3313 (save-excursion
3314 (let ((suffixes
3315 (if (not (featurep 'xemacs))
3316 ;; Emacs case
3317 (symbol-value 'dired-compress-file-suffixes)
3318 ;; XEmacs has `dired-compression-method-alist', which is
3319 ;; transformed into `dired-compress-file-suffixes' structure.
3320 (mapcar
3321 '(lambda (x)
3322 (list (concat (regexp-quote (nth 1 x)) "\\'")
3323 nil
3324 (mapconcat 'identity (nth 3 x) " ")))
3325 (symbol-value 'dired-compression-method-alist))))
3326 suffix)
3327 ;; See if any suffix rule matches this file name.
3328 (while suffixes
3329 (let (case-fold-search)
3330 (if (string-match (car (car suffixes)) localname)
3331 (setq suffix (car suffixes) suffixes nil))
3332 (setq suffixes (cdr suffixes))))
3333
3334 (cond ((file-symlink-p file)
3335 nil)
3336 ((and suffix (nth 2 suffix))
3337 ;; We found an uncompression rule.
3338 (message "Uncompressing %s..." file)
3339 (when (zerop (tramp-send-command-and-check
3340 multi-method method user host
3341 (concat (nth 2 suffix) " " localname)))
3342 (message "Uncompressing %s...done" file)
38c65fca
KG
3343 ;; `dired-remove-file' is not defined in XEmacs
3344 (funcall (symbol-function 'dired-remove-file) file)
5ec2cc41
KG
3345 (string-match (car suffix) file)
3346 (concat (substring file 0 (match-beginning 0)))))
3347 (t
3348 ;; We don't recognize the file as compressed, so compress it.
3349 ;; Try gzip.
3350 (message "Compressing %s..." file)
3351 (when (zerop (tramp-send-command-and-check
3352 multi-method method user host
3353 (concat "gzip -f " localname)))
3354 (message "Compressing %s...done" file)
38c65fca
KG
3355 ;; `dired-remove-file' is not defined in XEmacs
3356 (funcall (symbol-function 'dired-remove-file) file)
5ec2cc41
KG
3357 (cond ((file-exists-p (concat file ".gz"))
3358 (concat file ".gz"))
3359 ((file-exists-p (concat file ".z"))
3360 (concat file ".z"))
3361 (t nil)))))))))
fb7933a3
KG
3362
3363;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3364;; not sure at all that this is the right way to do it, but let's hope
3365;; it works for now, and wait for a guru to point out the Right Way to
3366;; achieve this.
3367;;(eval-when-compile
3368;; (unless (fboundp 'dired-insert-set-properties)
3369;; (fset 'dired-insert-set-properties 'ignore)))
3370;; Gerd suggests this:
3371(eval-when-compile (require 'dired))
3372;; Note that dired is required at run-time, too, when it is needed.
3373;; It is only needed on XEmacs for the function
3374;; `dired-insert-set-properties'.
3375
3376(defun tramp-handle-insert-directory
3377 (filename switches &optional wildcard full-directory-p)
3378 "Like `insert-directory' for tramp files."
c82c5727 3379 (if (and (boundp 'ls-lisp-use-insert-directory-program)
d2a2c17f 3380 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
c82c5727
LH
3381 (tramp-run-real-handler 'insert-directory
3382 (list filename switches wildcard full-directory-p))
3383 ;; For the moment, we assume that the remote "ls" program does not
3384 ;; grok "--dired". In the future, we should detect this on
3385 ;; connection setup.
3386 (when (string-match "^--dired\\s-+" switches)
3387 (setq switches (replace-match "" nil t switches)))
3388 (setq filename (expand-file-name filename))
3389 (with-parsed-tramp-file-name filename nil
3390 (tramp-message-for-buffer
3391 multi-method method user host 10
3392 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3393 switches filename (if wildcard "yes" "no")
3394 (if full-directory-p "yes" "no"))
3395 (when wildcard
3396 (setq wildcard (file-name-nondirectory localname))
3397 (setq localname (file-name-directory localname)))
3398 (when (listp switches)
3399 (setq switches (mapconcat 'identity switches " ")))
3400 (unless full-directory-p
3401 (setq switches (concat "-d " switches)))
3402 (when wildcard
3403 (setq switches (concat switches " " wildcard)))
fb7933a3 3404 (save-excursion
c82c5727
LH
3405 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3406 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3407 (if full-directory-p
3408 (tramp-send-command
3409 multi-method method user host
3410 (format "%s %s %s"
3411 (tramp-get-ls-command multi-method method user host)
3412 switches
3413 (if wildcard
3414 localname
3415 (tramp-shell-quote-argument (concat localname ".")))))
3416 (tramp-barf-unless-okay
3417 multi-method method user host
3418 (format "cd %s" (tramp-shell-quote-argument
3419 (file-name-directory localname)))
3420 nil 'file-error
3421 "Couldn't `cd %s'"
3422 (tramp-shell-quote-argument (file-name-directory localname)))
3423 (tramp-send-command
3424 multi-method method user host
3425 (format "%s %s %s"
3426 (tramp-get-ls-command multi-method method user host)
3427 switches
3428 (if wildcard
3429 localname
7177e2a3
MA
3430 (if (zerop (length (file-name-nondirectory localname)))
3431 ""
3432 (tramp-shell-quote-argument
3433 (file-name-nondirectory localname)))))))
c82c5727
LH
3434 (sit-for 1) ;needed for rsh but not ssh?
3435 (tramp-wait-for-output))
3436 ;; The following let-binding is used by code that's commented
3437 ;; out. Let's leave the let-binding in for a while to see
3438 ;; that the commented-out code is really not needed. Commenting-out
3439 ;; happened on 2003-03-13.
3440 (let ((old-pos (point)))
7177e2a3
MA
3441 ;; We cannot use `insert-buffer' because the tramp buffer
3442 ;; changes its contents before insertion due to calling
3443 ;; `expand-file' and alike.
3444 (insert
3445 (with-current-buffer
3446 (tramp-get-buffer multi-method method user host)
3447 (buffer-string)))
c82c5727
LH
3448 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3449 ;; that doesn't exist on Emacs, so we use this workaround instead.
3450 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3451 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3452 ;; (let ((zmacs-region-stays t))
3453 ;; (exchange-point-and-mark))
3454 (save-excursion
3455 (tramp-send-command multi-method method user host "cd")
3456 (tramp-wait-for-output))
3457 ;; For the time being, the XEmacs kludge is commented out.
3458 ;; Please test it on various XEmacs versions to see if it works.
3459 ;; ;; Another XEmacs specialty follows. What's the right way to do
3460 ;; ;; it?
3461 ;; (when (and (featurep 'xemacs)
3462 ;; (eq major-mode 'dired-mode))
3463 ;; (save-excursion
3464 ;; (require 'dired)
3465 ;; (dired-insert-set-properties old-pos (point))))
3466 ))))
fb7933a3
KG
3467
3468;; Continuation of kluge to pacify byte-compiler.
3469;;(eval-when-compile
3470;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3471;; (fmakunbound 'dired-insert-set-properties)))
3472
3473;; CCC is this the right thing to do?
3474(defun tramp-handle-unhandled-file-name-directory (filename)
3475 "Like `unhandled-file-name-directory' for tramp files."
c62c9d08 3476 (with-parsed-tramp-file-name filename nil
c62c9d08 3477 (expand-file-name "~/")))
fb7933a3
KG
3478
3479;; Canonicalization of file names.
3480
3481(defun tramp-drop-volume-letter (name)
3482 "Cut off unnecessary drive letter from file NAME.
3483The function `tramp-handle-expand-file-name' calls `expand-file-name'
3484locally on a remote file name. When the local system is a W32 system
3485but the remote system is Unix, this introduces a superfluous drive
3486letter into the file name. This function removes it.
3487
3488Doesn't do anything if the NAME does not start with a drive letter."
3489 (if (and (> (length name) 1)
3490 (char-equal (aref name 1) ?:)
3491 (let ((c1 (aref name 0)))
3492 (or (and (>= c1 ?A) (<= c1 ?Z))
3493 (and (>= c1 ?a) (<= c1 ?z)))))
3494 (substring name 2)
3495 name))
3496
3497(defun tramp-handle-expand-file-name (name &optional dir)
7432277c
KG
3498 "Like `expand-file-name' for tramp files.
3499If the localname part of the given filename starts with \"/../\" then
3500the result will be a local, non-Tramp, filename."
fb7933a3
KG
3501 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3502 (setq dir (or dir default-directory "/"))
3503 ;; Unless NAME is absolute, concat DIR and NAME.
3504 (unless (file-name-absolute-p name)
3505 (setq name (concat (file-name-as-directory dir) name)))
3506 ;; If NAME is not a tramp file, run the real handler
3507 (if (not (tramp-tramp-file-p name))
3508 (tramp-run-real-handler 'expand-file-name
3509 (list name nil))
3510 ;; Dissect NAME.
c62c9d08 3511 (with-parsed-tramp-file-name name nil
7432277c
KG
3512 (unless (file-name-absolute-p localname)
3513 (setq localname (concat "~/" localname)))
fb7933a3
KG
3514 (save-excursion
3515 ;; Tilde expansion if necessary. This needs a shell which
3516 ;; groks tilde expansion! The function `tramp-find-shell' is
3517 ;; supposed to find such a shell on the remote host. Please
3518 ;; tell me about it when this doesn't work on your system.
7432277c
KG
3519 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3520 (let ((uname (match-string 1 localname))
3521 (fname (match-string 2 localname)))
a69c01a0
MA
3522 ;; We cannot simply apply "~/", because under sudo "~/" is
3523 ;; expanded to the local user home directory but to the
3524 ;; root home directory. On the other hand, using always
3525 ;; the default user name for tilde expansion is not
3526 ;; appropriate either, because ssh and companions might
3527 ;; use a user name from the config file.
3528 (when (and (string-equal uname "~")
3529 (string-match
3530 "\\`su\\(do\\)?\\'"
3531 (tramp-find-method multi-method method user host)))
3532 (setq uname (concat uname (or user "root"))))
fb7933a3
KG
3533 ;; CCC fanatic error checking?
3534 (set-buffer (tramp-get-buffer multi-method method user host))
3535 (erase-buffer)
3536 (tramp-send-command
3537 multi-method method user host
3538 (format "cd %s; pwd" uname)
3539 t)
3540 (tramp-wait-for-output)
3541 (goto-char (point-min))
3542 (setq uname (buffer-substring (point) (tramp-line-end-position)))
7432277c 3543 (setq localname (concat uname fname))
fb7933a3 3544 (erase-buffer)))
a69c01a0
MA
3545 ;; There might be a double slash, for example when "~/"
3546 ;; expands to "/". Remove this.
3547 (while (string-match "//" localname)
3548 (setq localname (replace-match "/" t t localname)))
b1a2b924
KG
3549 ;; No tilde characters in file name, do normal
3550 ;; expand-file-name (this does "/./" and "/../"). We bind
a69c01a0
MA
3551 ;; directory-sep-char here for XEmacs on Windows, which would
3552 ;; otherwise use backslash. `default-directory' is bound to
3553 ;; "/", because on Windows there would be problems with UNC
3554 ;; shares or Cygwin mounts.
19a87064 3555 (tramp-let-maybe directory-sep-char ?/
a69c01a0
MA
3556 (let ((default-directory "/"))
3557 (tramp-make-tramp-file-name
3558 multi-method (or method (tramp-find-default-method user host))
3559 user host
3560 (tramp-drop-volume-letter
3561 (tramp-run-real-handler 'expand-file-name
3562 (list localname))))))))))
b1a2b924
KG
3563
3564;; old version follows. it uses ".." to cross file handler
3565;; boundaries.
3566;; ;; Look if localname starts with "/../" construct. If this is
3567;; ;; the case, then we return a local name instead of a remote name.
3568;; (if (string-match "^/\\.\\./" localname)
3569;; (expand-file-name (substring localname 3))
3570;; ;; No tilde characters in file name, do normal
3571;; ;; expand-file-name (this does "/./" and "/../"). We bind
3572;; ;; directory-sep-char here for XEmacs on Windows, which
3573;; ;; would otherwise use backslash.
3574;; (let ((directory-sep-char ?/))
3575;; (tramp-make-tramp-file-name
3576;; multi-method method user host
3577;; (tramp-drop-volume-letter
3578;; (tramp-run-real-handler 'expand-file-name
3579;; (list localname))))))))))
fb7933a3
KG
3580
3581;; Remote commands.
3582
5ec2cc41 3583(defvar tramp-async-proc nil
d2a2c17f 3584 "Global variable keeping asynchronous process object.
5ec2cc41
KG
3585Used in `tramp-handle-shell-command'")
3586
32f9593c
MA
3587(defvar tramp-display-shell-command-buffer t
3588 "Whether to display output buffer of `shell-command'.
3589This is necessary for handling DISPLAY of `process-file'.")
3590
fb7933a3
KG
3591(defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3592 "Like `shell-command' for tramp files.
3593This will break if COMMAND prints a newline, followed by the value of
3594`tramp-end-of-output', followed by another newline."
5ec2cc41 3595 ;; Asynchronous processes are far from being perfect. But it works at least
340b8d4f 3596 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
487f4fb7
KG
3597 (if (tramp-tramp-file-p default-directory)
3598 (with-parsed-tramp-file-name default-directory nil
5ec2cc41
KG
3599 (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3600 status)
3601 (unless output-buffer
3602 (setq output-buffer
3603 (get-buffer-create
3604 (if asynchronous
3605 "*Async Shell Command*"
3606 "*Shell Command Output*")))
3607 (set-buffer output-buffer)
3608 (erase-buffer))
3609 (unless (bufferp output-buffer)
3610 (setq output-buffer (current-buffer)))
3611 (set-buffer output-buffer)
3612 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3613 ;; process. Instead of, another asynchronous process is opened
3614 ;; which gets the output of the (synchronous) Tramp process
3615 ;; via process-filter. ERROR-BUFFER is disabled.
3616 (when asynchronous
3617 (setq command (substring command 0 (match-beginning 0))
3618 error-buffer nil
3619 tramp-async-proc (start-process (buffer-name output-buffer)
3620 output-buffer "cat")))
487f4fb7
KG
3621 (save-excursion
3622 (tramp-barf-unless-okay
3623 multi-method method user host
7432277c 3624 (format "cd %s" (tramp-shell-quote-argument localname))
487f4fb7
KG
3625 nil 'file-error
3626 "tramp-handle-shell-command: Couldn't `cd %s'"
7432277c 3627 (tramp-shell-quote-argument localname))
5ec2cc41
KG
3628 ;; Define the process filter
3629 (when asynchronous
3630 (set-process-filter
3631 (get-buffer-process
3632 (tramp-get-buffer multi-method method user host))
3633 '(lambda (process string)
3634 ;; Write the output into the Tramp Process
3635 (save-current-buffer
3636 (set-buffer (process-buffer process))
3637 (goto-char (point-max))
3638 (insert string))
3639 ;; Hand-over output to asynchronous process.
3640 (let ((end
3641 (string-match
3642 (regexp-quote tramp-end-of-output) string)))
3643 (when end
3644 (setq string
3645 (substring string 0 (1- (match-beginning 0)))))
3646 (process-send-string tramp-async-proc string)
3647 (when end
3648 (set-process-filter process nil)
3649 (process-send-eof tramp-async-proc))))))
3650 ;; Send the command
90f8dc03
KG
3651 (tramp-send-command
3652 multi-method method user host
3653 (if error-buffer
3654 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3655 command)
5ec2cc41
KG
3656 (format "%s; tramp_old_status=$?" command)))
3657 (unless asynchronous
3658 (tramp-wait-for-output)))
3659 (unless asynchronous
7177e2a3
MA
3660 ;; We cannot use `insert-buffer' because the tramp buffer
3661 ;; changes its contents before insertion due to calling
3662 ;; `expand-file' and alike.
3663 (insert
3664 (with-current-buffer
3665 (tramp-get-buffer multi-method method user host)
3666 (buffer-string))))
90f8dc03
KG
3667 (when error-buffer
3668 (save-excursion
3669 (unless (bufferp error-buffer)
3670 (setq error-buffer (get-buffer-create error-buffer)))
3671 (tramp-send-command
3672 multi-method method user host
3673 "cat /tmp/tramp.$$.err")
3674 (tramp-wait-for-output)
3675 (set-buffer error-buffer)
7177e2a3
MA
3676 ;; Same comment as above
3677 (insert
3678 (with-current-buffer
3679 (tramp-get-buffer multi-method method user host)
3680 (buffer-string)))
90f8dc03
KG
3681 (tramp-send-command-and-check
3682 multi-method method user host "rm -f /tmp/tramp.$$.err")))
487f4fb7
KG
3683 (save-excursion
3684 (tramp-send-command multi-method method user host "cd")
5ec2cc41
KG
3685 (unless asynchronous
3686 (tramp-wait-for-output))
487f4fb7
KG
3687 (tramp-send-command
3688 multi-method method user host
3689 (concat "tramp_set_exit_status $tramp_old_status;"
3690 " echo tramp_exit_status $?"))
5ec2cc41
KG
3691 (unless asynchronous
3692 (tramp-wait-for-output)
3693 (goto-char (point-max))
3694 (unless (search-backward "tramp_exit_status " nil t)
3695 (error "Couldn't find exit status of `%s'" command))
3696 (skip-chars-forward "^ ")
3697 (setq status (read (current-buffer)))))
487f4fb7 3698 (unless (zerop (buffer-size))
32f9593c
MA
3699 (when tramp-display-shell-command-buffer
3700 (display-buffer output-buffer)))
487f4fb7
KG
3701 status))
3702 ;; The following is only executed if something strange was
3703 ;; happening. Emit a helpful message and do it anyway.
3704 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3705 default-directory)
3706 (tramp-run-real-handler 'shell-command
3707 (list command output-buffer error-buffer))))
fb7933a3 3708
0457dd55
KG
3709(defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3710 "Like `process-file' for Tramp files."
3711 (when infile (error "Implementation does not handle input from file"))
3712 (when (and (numberp buffer) (zerop buffer))
3713 (error "Implementation does not handle immediate return"))
3714 (when (consp buffer) (error "Implementation does not handle error files"))
32f9593c
MA
3715 (let ((tramp-display-shell-command-buffer display))
3716 (shell-command
3717 (mapconcat 'tramp-shell-quote-argument (cons program args) " ")
3718 buffer)))
0457dd55 3719
fb7933a3
KG
3720;; File Editing.
3721
3722(defsubst tramp-make-temp-file ()
3723 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3724 (expand-file-name tramp-temp-name-prefix
3725 (tramp-temporary-file-directory))))
3726
3727(defun tramp-handle-file-local-copy (filename)
3728 "Like `file-local-copy' for tramp files."
c62c9d08 3729 (with-parsed-tramp-file-name filename nil
5ec2cc41 3730 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3b89d388
KG
3731 ;; We used to bind the following as late as possible.
3732 ;; loc-enc and loc-dec were bound directly before the if
3733 ;; statement that checks them. But the functions
3734 ;; tramp-get-* might invoke the "are you awake" check in
3735 ;; tramp-maybe-open-connection, which is an unfortunate time
3736 ;; since we rely on the buffer contents at that spot.
3737 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3738 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3739 (loc-enc (tramp-get-local-encoding multi-method method user host))
3740 (loc-dec (tramp-get-local-decoding multi-method method user host))
c62c9d08
KG
3741 tmpfil)
3742 (unless (file-exists-p filename)
3743 (error "Cannot make local copy of non-existing file `%s'"
3744 filename))
3745 (setq tmpfil (tramp-make-temp-file))
5ec2cc41 3746
5ec2cc41
KG
3747 (cond ((tramp-method-out-of-band-p multi-method method user host)
3748 ;; `copy-file' handles out-of-band methods
3749 (copy-file filename tmpfil t t))
3750
3b89d388 3751 ((and rem-enc rem-dec)
c62c9d08
KG
3752 ;; Use inline encoding for file transfer.
3753 (save-excursion
3754 ;; Following line for setting tramp-current-method,
3755 ;; tramp-current-user, tramp-current-host.
3b89d388 3756 (set-buffer tramp-buf)
c62c9d08
KG
3757 (tramp-message 5 "Encoding remote file %s..." filename)
3758 (tramp-barf-unless-okay
3759 multi-method method user host
7432277c 3760 (concat rem-enc " < " (tramp-shell-quote-argument localname))
c62c9d08
KG
3761 nil 'file-error
3762 "Encoding remote file failed, see buffer `%s' for details"
3b89d388 3763 tramp-buf)
c62c9d08
KG
3764 ;; Remove trailing status code
3765 (goto-char (point-max))
3766 (delete-region (point) (progn (forward-line -1) (point)))
3767
3768 (tramp-message 5 "Decoding remote file %s..." filename)
16674e4f 3769
3b89d388 3770 ;; Here is where loc-enc and loc-dec used to be let-bound.
16674e4f
KG
3771 (if (and (symbolp loc-dec) (fboundp loc-dec))
3772 ;; If local decoding is a function, we call it.
c62c9d08
KG
3773 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3774 (set-buffer tmpbuf)
3775 (erase-buffer)
f9f51bbf 3776 (insert-buffer-substring tramp-buf)
c62c9d08
KG
3777 (tramp-message-for-buffer
3778 multi-method method user host
3779 6 "Decoding remote file %s with function %s..."
16674e4f 3780 filename loc-dec)
c62c9d08 3781 (set-buffer tmpbuf)
16674e4f
KG
3782 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3783 ;; says that we need to strip tramp_exit_status
3784 ;; line from the output here. Go to point-max,
3785 ;; search backward for tramp_exit_status, delete
3786 ;; between point and point-max if found.
ea9d1443 3787 (let ((coding-system-for-write 'binary))
16674e4f 3788 (funcall loc-dec (point-min) (point-max))
c62c9d08
KG
3789 (write-region (point-min) (point-max) tmpfil))
3790 (kill-buffer tmpbuf))
3791 ;; If tramp-decoding-function is not defined for this
3792 ;; method, we invoke tramp-decoding-command instead.
3793 (let ((tmpfil2 (tramp-make-temp-file)))
3794 (write-region (point-min) (point-max) tmpfil2)
3795 (tramp-message
3796 6 "Decoding remote file %s with command %s..."
16674e4f
KG
3797 filename loc-dec)
3798 (tramp-call-local-coding-command
3799 loc-dec tmpfil2 tmpfil)
c62c9d08
KG
3800 (delete-file tmpfil2)))
3801 (tramp-message-for-buffer
3802 multi-method method user host
38c65fca
KG
3803 5 "Decoding remote file %s...done" filename)
3804 ;; Set proper permissions.
3805 (set-file-modes tmpfil (file-modes filename))))
c62c9d08
KG
3806
3807 (t (error "Wrong method specification for `%s'" method)))
3808 tmpfil)))
fb7933a3 3809
19a87064
MA
3810(defun tramp-handle-file-remote-p (filename)
3811 "Like `file-remote-p' for tramp files."
15cc764c
KG
3812 (when (tramp-tramp-file-p filename)
3813 (with-parsed-tramp-file-name filename nil
3814 (make-tramp-file-name
3815 :multi-method multi-method
3816 :method method
3817 :user user
3818 :host host
3819 :localname ""))))
fb7933a3
KG
3820
3821(defun tramp-handle-insert-file-contents
3822 (filename &optional visit beg end replace)
3823 "Like `insert-file-contents' for tramp files."
3824 (barf-if-buffer-read-only)
3825 (setq filename (expand-file-name filename))
c62c9d08 3826 (with-parsed-tramp-file-name filename nil
4007ba5b 3827 (if (not (file-exists-p filename))
fb7933a3
KG
3828 (progn
3829 (when visit
3830 (setq buffer-file-name filename)
3831 (set-visited-file-modtime)
3832 (set-buffer-modified-p nil))
3833 (signal 'file-error
3834 (format "File `%s' not found on remote host" filename))
4007ba5b 3835 (list (expand-file-name filename) 0))
ea9d1443
KG
3836 ;; `insert-file-contents-literally' takes care to avoid calling
3837 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3838 ;; propagate that care to the file-local-copy operation.
3839 (let ((local-copy
90f8dc03
KG
3840 (let ((inhibit-file-name-operation
3841 (when (eq inhibit-file-name-operation
3842 'insert-file-contents)
3843 'file-local-copy)))
ea9d1443 3844 (file-local-copy filename)))
11948172 3845 coding-system-used result)
fb7933a3
KG
3846 (when visit
3847 (setq buffer-file-name filename)
3848 (set-visited-file-modtime)
3849 (set-buffer-modified-p nil))
3850 (tramp-message-for-buffer
3851 multi-method method user host
3852 9 "Inserting local temp file `%s'..." local-copy)
4007ba5b 3853 (setq result (insert-file-contents local-copy nil beg end replace))
11948172
MA
3854 ;; Now `last-coding-system-used' has right value. Remember it.
3855 (when (boundp 'last-coding-system-used)
3856 (setq coding-system-used (symbol-value 'last-coding-system-used)))
16674e4f
KG
3857 (tramp-message-for-buffer
3858 multi-method method user host
3859 9 "Inserting local temp file `%s'...done" local-copy)
fb7933a3 3860 (delete-file local-copy)
11948172
MA
3861 (when (boundp 'last-coding-system-used)
3862 (set 'last-coding-system-used coding-system-used))
fb7933a3
KG
3863 (list (expand-file-name filename)
3864 (second result))))))
3865
38c65fca
KG
3866
3867(defun tramp-handle-find-backup-file-name (filename)
3868 "Like `find-backup-file-name' for tramp files."
07dfe738
KG
3869 (with-parsed-tramp-file-name filename nil
3870 ;; We set both variables. It doesn't matter whether it is
3871 ;; Emacs or XEmacs
3872 (let ((backup-directory-alist
3873 ;; Emacs case
3874 (when (boundp 'backup-directory-alist)
3875 (if (boundp 'tramp-backup-directory-alist)
3876 (mapcar
3877 '(lambda (x)
3878 (cons
3879 (car x)
3880 (if (and (stringp (cdr x))
3881 (file-name-absolute-p (cdr x))
3882 (not (tramp-file-name-p (cdr x))))
3883 (tramp-make-tramp-file-name
3884 multi-method method user host (cdr x))
3885 (cdr x))))
3886 (symbol-value 'tramp-backup-directory-alist))
3887 (symbol-value 'backup-directory-alist))))
3888
3889 (bkup-backup-directory-info
3890 ;; XEmacs case
3891 (when (boundp 'bkup-backup-directory-info)
3892 (if (boundp 'tramp-bkup-backup-directory-info)
3893 (mapcar
3894 '(lambda (x)
3895 (nconc
3896 (list (car x))
3897 (list
3898 (if (and (stringp (car (cdr x)))
3899 (file-name-absolute-p (car (cdr x)))
3900 (not (tramp-file-name-p (car (cdr x)))))
3901 (tramp-make-tramp-file-name
3902 multi-method method user host (car (cdr x)))
3903 (car (cdr x))))
3904 (cdr (cdr x))))
3905 (symbol-value 'tramp-bkup-backup-directory-info))
3906 (symbol-value 'bkup-backup-directory-info)))))
3907
3908 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
38c65fca 3909
c1105d05
MA
3910(defun tramp-handle-make-auto-save-file-name ()
3911 "Like `make-auto-save-file-name' for tramp files.
3912Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3913 (when tramp-auto-save-directory
3914 (unless (file-exists-p tramp-auto-save-directory)
3915 (make-directory tramp-auto-save-directory t)))
3916 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3917 ;; file name we make sure that jka-compr isn't used for the
3918 ;; auto-save file.
3919 (let ((buffer-file-name
3920 (if tramp-auto-save-directory
3921 (expand-file-name
3922 (tramp-subst-strs-in-string
3923 '(("_" . "|")
3924 ("/" . "_a")
3925 (":" . "_b")
3926 ("|" . "__")
3927 ("[" . "_l")
3928 ("]" . "_r"))
3929 (buffer-file-name))
3930 tramp-auto-save-directory)
00cec167
MA
3931 (buffer-file-name))))
3932 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3933 ;; it is not a magic file name operation (since Emacs 22).
3934 ;; We must deactivate it temporarily.
3935 (if (not (ad-is-active 'make-auto-save-file-name))
3936 (tramp-run-real-handler
3937 'make-auto-save-file-name nil)
3938 ;; else
3939 (ad-deactivate 'make-auto-save-file-name)
3940 (prog1
3941 (tramp-run-real-handler
3942 'make-auto-save-file-name nil)
3943 (ad-activate 'make-auto-save-file-name)))))
c1105d05 3944
38c65fca 3945
fb7933a3
KG
3946;; CCC grok APPEND, LOCKNAME, CONFIRM
3947(defun tramp-handle-write-region
3948 (start end filename &optional append visit lockname confirm)
3949 "Like `write-region' for tramp files."
3950 (unless (eq append nil)
3951 (error "Cannot append to file using tramp (`%s')" filename))
3952 (setq filename (expand-file-name filename))
c62c9d08
KG
3953 ;; Following part commented out because we don't know what to do about
3954 ;; file locking, and it does not appear to be a problem to ignore it.
3955 ;; Ange-ftp ignores it, too.
3956 ;; (when (and lockname (stringp lockname))
3957 ;; (setq lockname (expand-file-name lockname)))
3958 ;; (unless (or (eq lockname nil)
3959 ;; (string= lockname filename))
3960 ;; (error
3961 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
d2a2c17f 3962 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
fb7933a3 3963 (when (and (not (featurep 'xemacs))
c62c9d08 3964 confirm (file-exists-p filename))
fb7933a3
KG
3965 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3966 filename))
3967 (error "File not overwritten")))
c62c9d08 3968 (with-parsed-tramp-file-name filename nil
c62c9d08 3969 (let ((curbuf (current-buffer))
16674e4f
KG
3970 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3971 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3972 (loc-enc (tramp-get-local-encoding multi-method method user host))
3973 (loc-dec (tramp-get-local-decoding multi-method method user host))
c62c9d08 3974 (trampbuf (get-buffer-create "*tramp output*"))
38c65fca 3975 (modes (file-modes filename))
11948172
MA
3976 ;; We use this to save the value of `last-coding-system-used'
3977 ;; after writing the tmp file. At the end of the function,
3978 ;; we set `last-coding-system-used' to this saved value.
3979 ;; This way, any intermediary coding systems used while
3980 ;; talking to the remote shell or suchlike won't hose this
3981 ;; variable. This approach was snarfed from ange-ftp.el.
3982 coding-system-used
c62c9d08
KG
3983 tmpfil)
3984 ;; Write region into a tmp file. This isn't really needed if we
3985 ;; use an encoding function, but currently we use it always
3986 ;; because this makes the logic simpler.
3987 (setq tmpfil (tramp-make-temp-file))
07dfe738
KG
3988 ;; Set current buffer. If connection wasn't open, `file-modes' has
3989 ;; changed it accidently.
3990 (set-buffer curbuf)
c62c9d08
KG
3991 ;; We say `no-message' here because we don't want the visited file
3992 ;; modtime data to be clobbered from the temp file. We call
3993 ;; `set-visited-file-modtime' ourselves later on.
3994 (tramp-run-real-handler
3995 'write-region
3996 (if confirm ; don't pass this arg unless defined for backward compat.
3997 (list start end tmpfil append 'no-message lockname confirm)
3998 (list start end tmpfil append 'no-message lockname)))
11948172
MA
3999 ;; Now, `last-coding-system-used' has the right value. Remember it.
4000 (when (boundp 'last-coding-system-used)
4001 (setq coding-system-used (symbol-value 'last-coding-system-used)))
38c65fca
KG
4002 ;; The permissions of the temporary file should be set. If
4003 ;; filename does not exist (eq modes nil) it has been renamed to
4004 ;; the backup file. This case `save-buffer' handles
4005 ;; permissions.
4006 (when modes (set-file-modes tmpfil modes))
c62c9d08
KG
4007 ;; This is a bit lengthy due to the different methods possible for
4008 ;; file transfer. First, we check whether the method uses an rcp
4009 ;; program. If so, we call it. Otherwise, both encoding and
4010 ;; decoding command must be specified. However, if the method
4011 ;; _also_ specifies an encoding function, then that is used for
4012 ;; encoding the contents of the tmp file.
5ec2cc41
KG
4013 (cond ((tramp-method-out-of-band-p multi-method method user host)
4014 ;; `copy-file' handles out-of-band methods
4015 (copy-file tmpfil filename t t))
4016
16674e4f 4017 ((and rem-enc rem-dec)
c62c9d08
KG
4018 ;; Use inline file transfer
4019 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4020 (save-excursion
4021 ;; Encode tmpfil into tmpbuf
4022 (tramp-message-for-buffer multi-method method user host
4023 5 "Encoding region...")
4024 (set-buffer tmpbuf)
4025 (erase-buffer)
4026 ;; Use encoding function or command.
16674e4f 4027 (if (and (symbolp loc-enc) (fboundp loc-enc))
c62c9d08
KG
4028 (progn
4029 (tramp-message-for-buffer
4030 multi-method method user host
5ec2cc41
KG
4031 6 "Encoding region using function `%s'..."
4032 (symbol-name loc-enc))
c62c9d08
KG
4033 (insert-file-contents-literally tmpfil)
4034 ;; CCC. The following `let' is a workaround for
4035 ;; the base64.el that comes with pgnus-0.84. If
4036 ;; both of the following conditions are
4037 ;; satisfied, it tries to write to a local file
4038 ;; in default-directory, but at this point,
4039 ;; default-directory is remote.
4040 ;; (CALL-PROCESS-REGION can't write to remote
4041 ;; files, it seems.) The file in question is a
4042 ;; tmp file anyway.
4043 (let ((default-directory
4044 (tramp-temporary-file-directory)))
16674e4f 4045 (funcall loc-enc (point-min) (point-max)))
c62c9d08
KG
4046 (goto-char (point-max))
4047 (unless (bolp)
4048 (newline)))
4049 (tramp-message-for-buffer
4050 multi-method method user host
16674e4f
KG
4051 6 "Encoding region using command `%s'..." loc-enc)
4052 (unless (equal 0 (tramp-call-local-coding-command
4053 loc-enc tmpfil t))
c62c9d08
KG
4054 (pop-to-buffer trampbuf)
4055 (error (concat "Cannot write to `%s', local encoding"
4056 " command `%s' failed")
16674e4f 4057 filename loc-enc)))
c62c9d08
KG
4058 ;; Send tmpbuf into remote decoding command which
4059 ;; writes to remote file. Because this happens on the
4060 ;; remote host, we cannot use the function.
4061 (tramp-message-for-buffer
4062 multi-method method user host
4063 5 "Decoding region into remote file %s..." filename)
4064 (tramp-send-command
4065 multi-method method user host
4066 (format "%s >%s <<'EOF'"
16674e4f 4067 rem-dec
7432277c 4068 (tramp-shell-quote-argument localname)))
c62c9d08
KG
4069 (set-buffer tmpbuf)
4070 (tramp-message-for-buffer
4071 multi-method method user host
4072 6 "Sending data to remote host...")
7432277c
KG
4073 (tramp-send-string multi-method method user host
4074 (buffer-string))
c62c9d08
KG
4075 ;; wait for remote decoding to complete
4076 (tramp-message-for-buffer
4077 multi-method method user host
4078 6 "Sending end of data token...")
4079 (tramp-send-command
3cdaec13 4080 multi-method method user host "EOF" nil t)
c62c9d08
KG
4081 (tramp-message-for-buffer
4082 multi-method method user host 6
4083 "Waiting for remote host to process data...")
4084 (set-buffer (tramp-get-buffer multi-method method user host))
4085 (tramp-wait-for-output)
4086 (tramp-barf-unless-okay
4087 multi-method method user host nil nil 'file-error
4088 (concat "Couldn't write region to `%s',"
4089 " decode using `%s' failed")
16674e4f 4090 filename rem-dec)
c62c9d08
KG
4091 (tramp-message 5 "Decoding region into remote file %s...done"
4092 filename)
4093 (kill-buffer tmpbuf))))
4094 (t
4095 (error
4096 (concat "Method `%s' should specify both encoding and "
4097 "decoding command or an rcp program")
4098 method)))
4099 (delete-file tmpfil)
4100 (unless (equal curbuf (current-buffer))
4101 (error "Buffer has changed from `%s' to `%s'"
4102 curbuf (current-buffer)))
48ddd622
MA
4103 (when (or (eq visit t) (stringp visit))
4104 (set-visited-file-modtime
4105 ;; We must pass modtime explicitely, because filename can be different
4106 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4107 (nth 5 (file-attributes filename))))
11948172
MA
4108 ;; Make `last-coding-system-used' have the right value.
4109 (when (boundp 'last-coding-system-used)
4110 (set 'last-coding-system-used coding-system-used))
c62c9d08
KG
4111 (when (or (eq visit t)
4112 (eq visit nil)
4113 (stringp visit))
4114 (message "Wrote %s" filename)))))
fb7933a3
KG
4115
4116;; Call down to the real handler.
7432277c
KG
4117;; Because EFS does not play nicely with TRAMP (both systems match a
4118;; TRAMP file name) it is needed to disable efs as well as tramp for the
fb7933a3
KG
4119;; operation.
4120;;
4121;; Other than that, this is the canon file-handler code that the doco
4122;; says should be used here. Which is nice.
4123;;
4124;; Under XEmacs current, EFS also hooks in as
7432277c 4125;; efs-sifn-handler-function to handle any filename with environment
fb7933a3 4126;; variables. This has two implications:
7432277c 4127;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
fb7933a3
KG
4128;; 2) That TRAMP might want to do the same thing.
4129;; Details as they come in.
4130;;
4131;; Daniel Pittman <daniel@danann.net>
4132
4133;; (defun tramp-run-real-handler (operation args)
4134;; "Invoke normal file name handler for OPERATION.
4135;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4136;; First arg specifies the OPERATION, remaining ARGS are passed to the
4137;; OPERATION."
4138;; (let ((inhibit-file-name-handlers
4139;; (list 'tramp-file-name-handler
4140;; 'efs-file-handler-function
4141;; 'ange-ftp-hook-function
4142;; (and (eq inhibit-file-name-operation operation)
4143;; inhibit-file-name-handlers)))
4144;; (inhibit-file-name-operation operation))
4145;; (apply operation args)))
4146
4147(defun tramp-run-real-handler (operation args)
4148 "Invoke normal file name handler for OPERATION.
c62c9d08
KG
4149First arg specifies the OPERATION, second arg is a list of arguments to
4150pass to the OPERATION."
4007ba5b
KG
4151 (let* ((inhibit-file-name-handlers
4152 `(tramp-file-name-handler
4153 tramp-completion-file-name-handler
4154 cygwin-mount-name-hook-function
4155 cygwin-mount-map-drive-hook-function
4156 .
4157 ,(and (eq inhibit-file-name-operation operation)
4158 inhibit-file-name-handlers)))
4159 (inhibit-file-name-operation operation))
16674e4f
KG
4160 (apply operation args)))
4161
4162;; This function is used from `tramp-completion-file-name-handler' functions
4163;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4164;; because the check is based on a full filename, not available for all
4165;; basic I/O operations.
4166(defun tramp-completion-run-real-handler (operation args)
4167 "Invoke `tramp-file-name-handler' for OPERATION.
4168First arg specifies the OPERATION, second arg is a list of arguments to
4169pass to the OPERATION."
4007ba5b
KG
4170 (let* ((inhibit-file-name-handlers
4171 `(tramp-completion-file-name-handler
4172 cygwin-mount-name-hook-function
4173 cygwin-mount-map-drive-hook-function
4174 .
4175 ,(and (eq inhibit-file-name-operation operation)
4176 inhibit-file-name-handlers)))
4177 (inhibit-file-name-operation operation))
fb7933a3
KG
4178 (apply operation args)))
4179
4007ba5b
KG
4180;; We handle here all file primitives. Most of them have the file
4181;; name as first parameter; nevertheless we check for them explicitly
19a87064 4182;; in order to be signalled if a new primitive appears. This
4007ba5b
KG
4183;; scenario is needed because there isn't a way to decide by
4184;; syntactical means whether a foreign method must be called. It would
19a87064 4185;; ease the life if `file-name-handler-alist' would support a decision
4007ba5b
KG
4186;; function as well but regexp only.
4187(defun tramp-file-name-for-operation (operation &rest args)
4188 "Return file name related to OPERATION file primitive.
4189ARGS are the arguments OPERATION has been called with."
4190 (cond
4191 ; FILE resp DIRECTORY
4192 ((member operation
4193 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4194 'delete-file 'diff-latest-backup-file 'directory-file-name
4195 'directory-files 'directory-files-and-attributes
4196 'dired-compress-file 'dired-uncache
4197 'file-accessible-directory-p 'file-attributes
4198 'file-directory-p 'file-executable-p 'file-exists-p
19a87064
MA
4199 'file-local-copy 'file-remote-p 'file-modes
4200 'file-name-as-directory 'file-name-directory
4201 'file-name-nondirectory 'file-name-sans-versions
4202 'file-ownership-preserved-p 'file-readable-p
4203 'file-regular-p 'file-symlink-p 'file-truename
4204 'file-writable-p 'find-backup-file-name 'find-file-noselect
4205 'get-file-buffer 'insert-directory 'insert-file-contents
4206 'load 'make-directory 'make-directory-internal
4207 'set-file-modes 'substitute-in-file-name
4208 'unhandled-file-name-directory 'vc-registered
4007ba5b
KG
4209 ; XEmacs only
4210 'abbreviate-file-name 'create-file-buffer
4211 'dired-file-modtime 'dired-make-compressed-filename
4212 'dired-recursive-delete-directory 'dired-set-file-modtime
4213 'dired-shell-unhandle-file-name 'dired-uucode-file
4214 'insert-file-contents-literally 'recover-file
4215 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
8daea7fc
KG
4216 (if (file-name-absolute-p (nth 0 args))
4217 (nth 0 args)
4218 (expand-file-name (nth 0 args))))
4007ba5b
KG
4219 ; FILE DIRECTORY resp FILE1 FILE2
4220 ((member operation
4221 (list 'add-name-to-file 'copy-file 'expand-file-name
4222 'file-name-all-completions 'file-name-completion
4223 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4224 ; XEmacs only
4225 'dired-make-relative-symlink
4226 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4227 (save-match-data
4228 (cond
4229 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4230 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4231 (t (buffer-file-name (current-buffer))))))
4232 ; START END FILE
4233 ((eq operation 'write-region)
4234 (nth 2 args))
4235 ; BUF
4236 ((member operation
c1105d05
MA
4237 (list 'make-auto-save-file-name
4238 'set-visited-file-modtime 'verify-visited-file-modtime
4239 ; XEmacs only
4007ba5b
KG
4240 'backup-buffer))
4241 (buffer-file-name
4242 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4243 ; COMMAND
4244 ((member operation
74f69b93 4245 (list 'dired-call-process
01917a18 4246 ; Emacs only
b71c9e75 4247 'shell-command
340b8d4f 4248 ; Emacs 22 only
0457dd55 4249 'process-file
4007ba5b
KG
4250 ; XEmacs only
4251 'dired-print-file 'dired-shell-call-process))
4252 default-directory)
4253 ; unknown file primitive
4254 (t (error "unknown file I/O primitive: %s" operation))))
4255
4256(defun tramp-find-foreign-file-name-handler (filename)
4257 "Return foreign file name handler if exists."
8daea7fc 4258 (when (tramp-tramp-file-p filename)
ea9d1443
KG
4259 (let (elt
4260 res
4261 (handler-alist tramp-foreign-file-name-handler-alist))
4262 (while handler-alist
4263 (setq elt (car handler-alist)
4264 handler-alist (cdr handler-alist))
8daea7fc 4265 (when (funcall (car elt) filename)
ea9d1443 4266 (setq handler-alist nil)
8daea7fc
KG
4267 (setq res (cdr elt))))
4268 res)))
4007ba5b 4269
fb7933a3
KG
4270;; Main function.
4271;;;###autoload
4272(defun tramp-file-name-handler (operation &rest args)
ea9d1443 4273 "Invoke Tramp file name handler.
fb7933a3 4274Falls back to normal file name handler if no tramp file name handler exists."
4007ba5b 4275 (save-match-data
ea9d1443 4276 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4007ba5b
KG
4277 (foreign (tramp-find-foreign-file-name-handler filename)))
4278 (cond
4279 (foreign (apply foreign operation args))
4007ba5b 4280 (t (tramp-run-real-handler operation args))))))
fb7933a3 4281
07dfe738
KG
4282
4283;; In Emacs, there is some concurrency due to timers. If a timer
4284;; interrupts Tramp and wishes to use the same connection buffer as
4285;; the "main" Emacs, then garbage might occur in the connection
4286;; buffer. Therefore, we need to make sure that a timer does not use
4287;; the same connection buffer as the "main" Emacs. We implement a
4288;; cheap global lock, instead of locking each connection buffer
4289;; separately. The global lock is based on two variables,
4290;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4291;; (with setq) to indicate a lock. But Tramp also calls itself during
4292;; processing of a single file operation, so we need to allow
4293;; recursive calls. That's where the `tramp-locker' variable comes in
4294;; -- it is let-bound to t during the execution of the current
4295;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4296;; then we should just proceed because we have been called
4297;; recursively. But if `tramp-locker' is nil, then we are a timer
4298;; interrupting the "main" Emacs, and then we signal an error.
4299
4300(defvar tramp-locked nil
4301 "If non-nil, then Tramp is currently busy.
4302Together with `tramp-locker', this implements a locking mechanism
4303preventing reentrant calls of Tramp.")
4304
4305(defvar tramp-locker nil
4306 "If non-nil, then a caller has locked Tramp.
4307Together with `tramp-locked', this implements a locking mechanism
4308preventing reentrant calls of Tramp.")
4309
ea9d1443
KG
4310(defun tramp-sh-file-name-handler (operation &rest args)
4311 "Invoke remote-shell Tramp file name handler.
4312Fall back to normal file name handler if no Tramp handler exists."
07dfe738
KG
4313 (when (and tramp-locked (not tramp-locker))
4314 (signal 'file-error "Forbidden reentrant call of Tramp"))
4315 (let ((tl tramp-locked))
4316 (unwind-protect
4317 (progn
4318 (setq tramp-locked t)
4319 (let ((tramp-locker t))
4320 (save-match-data
4321 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4322 (if fn
4323 (apply (cdr fn) args)
4324 (tramp-run-real-handler operation args))))))
4325 (setq tramp-locked tl))))
ea9d1443 4326
16674e4f
KG
4327;;;###autoload
4328(defun tramp-completion-file-name-handler (operation &rest args)
4329 "Invoke tramp file name completion handler.
4330Falls back to normal file name handler if no tramp file name handler exists."
4331;; (setq tramp-debug-buffer t)
4332;; (tramp-message 1 "%s %s" operation args)
4333;; (tramp-message 1 "%s %s\n%s"
4334;; operation args (with-output-to-string (backtrace)))
4335 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4336 (if fn
4007ba5b 4337 (save-match-data (apply (cdr fn) args))
16674e4f
KG
4338 (tramp-completion-run-real-handler operation args))))
4339
b25a52cc
KG
4340;;;###autoload
4341(put 'tramp-completion-file-name-handler 'safe-magic t)
4342
fb7933a3
KG
4343;; Register in file name handler alist
4344;;;###autoload
4345(add-to-list 'file-name-handler-alist
4346 (cons tramp-file-name-regexp 'tramp-file-name-handler))
16674e4f
KG
4347(add-to-list 'file-name-handler-alist
4348 (cons tramp-completion-file-name-regexp
4349 'tramp-completion-file-name-handler))
fb7933a3 4350
a69c01a0
MA
4351;;;###autoload
4352(defun tramp-unload-file-name-handler-alist ()
4353 (setq file-name-handler-alist
4354 (delete (rassoc 'tramp-file-name-handler
4355 file-name-handler-alist)
4356 (delete (rassoc 'tramp-completion-file-name-handler
4357 file-name-handler-alist)
4358 file-name-handler-alist))))
4359
4360(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handler-alist)
4361
487fa986
KG
4362(defun tramp-repair-jka-compr ()
4363 "If jka-compr is already loaded, move it to the front of
340b8d4f 4364`file-name-handler-alist'. On Emacs 22 or so this will not be
487fa986
KG
4365necessary anymore."
4366 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4367 (when jka
4368 (setq file-name-handler-alist
4369 (cons jka (delete jka file-name-handler-alist))))))
4370(tramp-repair-jka-compr)
fb7933a3 4371
c62c9d08 4372
fb7933a3
KG
4373;;; Interactions with other packages:
4374
4375;; -- complete.el --
4376
4377;; This function contributed by Ed Sabol
4378(defun tramp-handle-expand-many-files (name)
4379 "Like `PC-expand-many-files' for tramp files."
c62c9d08 4380 (with-parsed-tramp-file-name name nil
c62c9d08
KG
4381 (save-match-data
4382 (if (or (string-match "\\*" name)
4383 (string-match "\\?" name)
4384 (string-match "\\[.*\\]" name))
4385 (save-excursion
c62c9d08 4386 (let (bufstr)
c62c9d08
KG
4387 ;; CCC: To do it right, we should quote certain characters
4388 ;; in the file name, but since the echo command is going to
4389 ;; break anyway when there are spaces in the file names, we
4390 ;; don't bother.
4391 ;;-(let ((comint-file-name-quote-list
4392 ;;- (set-difference tramp-file-name-quote-list
4393 ;;- '(?\* ?\? ?[ ?]))))
4394 ;;- (tramp-send-command
4395 ;;- multi-method method user host
7432277c 4396 ;;- (format "echo %s" (comint-quote-filename localname)))
c62c9d08
KG
4397 ;;- (tramp-wait-for-output))
4398 (tramp-send-command multi-method method user host
7432277c 4399 (format "echo %s" localname))
c62c9d08
KG
4400 (tramp-wait-for-output)
4401 (setq bufstr (buffer-substring (point-min)
4402 (tramp-line-end-position)))
4403 (goto-char (point-min))
7432277c 4404 (if (string-equal localname bufstr)
c62c9d08
KG
4405 nil
4406 (insert "(\"")
4407 (while (search-forward " " nil t)
4408 (delete-backward-char 1)
4409 (insert "\" \""))
4410 (goto-char (point-max))
4411 (delete-backward-char 1)
4412 (insert "\")")
4413 (goto-char (point-min))
4414 (mapcar
4415 (function (lambda (x)
4416 (tramp-make-tramp-file-name multi-method method
4417 user host x)))
4418 (read (current-buffer))))))
07dfe738 4419 (list (expand-file-name name))))))
fb7933a3 4420
a69c01a0
MA
4421(eval-after-load "complete"
4422 '(progn
4423 (defadvice PC-expand-many-files
4424 (around tramp-advice-PC-expand-many-files (name) activate)
4425 "Invoke `tramp-handle-expand-many-files' for tramp files."
4426 (if (tramp-tramp-file-p name)
4427 (setq ad-return-value (tramp-handle-expand-many-files name))
4428 ad-do-it))
4429 (add-hook 'tramp-unload-hook
4430 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
fb7933a3 4431
16674e4f
KG
4432;;; File name handler functions for completion mode
4433
8cee983d
RS
4434(defvar tramp-completion-mode nil
4435 "If non-nil, we are in file name completion mode.")
4436
16674e4f
KG
4437;; Necessary because `tramp-file-name-regexp-unified' and
4438;; `tramp-completion-file-name-regexp-unified' aren't different.
4439;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4440;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
bf247b6e 4441;; Using `last-input-event' is a little bit risky, because completing a file
16674e4f
KG
4442;; might require loading other files, like "~/.netrc", and for them it
4443;; shouldn't be decided based on that variable. On the other hand, those files
4444;; shouldn't have partial tramp file name syntax. Maybe another variable should
4445;; be introduced overwriting this check in such cases. Or we change tramp
4446;; file name syntax in order to avoid ambiguities, like in XEmacs ...
5ec2cc41
KG
4447;; In case of non unified file names it can be always true (and wouldn't be
4448;; necessary, because there are different regexp).
16674e4f
KG
4449(defun tramp-completion-mode (file)
4450 "Checks whether method / user name / host name completion is active."
4451 (cond
c1253aad 4452 (tramp-completion-mode t)
5ec2cc41 4453 ((not tramp-unified-filenames) t)
16674e4f
KG
4454 ((string-match "^/.*:.*:$" file) nil)
4455 ((string-match
487f4fb7
KG
4456 (concat tramp-prefix-regexp
4457 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
16674e4f 4458 file)
4007ba5b 4459 (member (match-string 1 file) (mapcar 'car tramp-methods)))
16674e4f 4460 ((or (equal last-input-event 'tab)
85806390
MA
4461 ;; Emacs
4462 (and (integerp last-input-event)
4463 (or
4464 ;; ?\t has event-modifier 'control
4465 (char-equal last-input-event ?\t)
4466 (and (not (event-modifiers last-input-event))
4467 (or (char-equal last-input-event ?\?)
4468 (char-equal last-input-event ?\ )))))
5ec2cc41
KG
4469 ;; XEmacs
4470 (and (featurep 'xemacs)
85806390
MA
4471 (or
4472 ;; ?\t has event-modifier 'control
4473 (char-equal
4474 (funcall (symbol-function 'event-to-character)
4475 last-input-event) ?\t)
4476 (and (not (event-modifiers last-input-event))
4477 (or (char-equal
4478 (funcall (symbol-function 'event-to-character)
4479 last-input-event) ?\?)
4480 (char-equal
4481 (funcall (symbol-function 'event-to-character)
4482 last-input-event) ?\ ))))))
16674e4f
KG
4483 t)))
4484
4485(defun tramp-completion-handle-file-exists-p (filename)
4486 "Like `file-exists-p' for tramp files."
4487 (if (tramp-completion-mode filename)
4488 (tramp-run-real-handler
4489 'file-exists-p (list filename))
4490 (tramp-completion-run-real-handler
4491 'file-exists-p (list filename))))
4492
7432277c 4493;; Localname manipulation in case of partial TRAMP file names.
16674e4f
KG
4494(defun tramp-completion-handle-file-name-directory (file)
4495 "Like `file-name-directory' but aware of TRAMP files."
4496 (if (tramp-completion-mode file)
4497 "/"
4498 (tramp-completion-run-real-handler
4499 'file-name-directory (list file))))
fb7933a3 4500
7432277c 4501;; Localname manipulation in case of partial TRAMP file names.
16674e4f
KG
4502(defun tramp-completion-handle-file-name-nondirectory (file)
4503 "Like `file-name-nondirectory' but aware of TRAMP files."
4504 (substring
4505 file (length (tramp-completion-handle-file-name-directory file))))
4506
4507;; Method, host name and user name completion.
4508;; `tramp-completion-dissect-file-name' returns a list of
4509;; tramp-file-name structures. For all of them we return possible completions.
4510(defun tramp-completion-handle-file-name-all-completions (filename directory)
4511 "Like `file-name-all-completions' for partial tramp files."
4512
c1253aad
MA
4513 (unwind-protect
4514 ;; We need to reset `tramp-completion-mode'.
4515 (progn
4516 (setq tramp-completion-mode t)
4517 (let*
4518 ((fullname (concat directory filename))
4519 ;; possible completion structures
4520 (v (tramp-completion-dissect-file-name fullname))
4521 result result1)
4522
4523 (while v
4524 (let* ((car (car v))
4525 (multi-method (tramp-file-name-multi-method car))
4526 (method (tramp-file-name-method car))
4527 (user (tramp-file-name-user car))
4528 (host (tramp-file-name-host car))
4529 (localname (tramp-file-name-localname car))
4530 (m (tramp-find-method multi-method method user host))
4531 (tramp-current-user user) ; see `tramp-parse-passwd'
4532 all-user-hosts)
4533
4534 (unless (or multi-method ;; Not handled (yet).
4535 localname) ;; Nothing to complete
4536
4537 (if (or user host)
4538
4539 ;; Method dependent user / host combinations
4540 (progn
4541 (mapcar
4542 (lambda (x)
4543 (setq all-user-hosts
4544 (append all-user-hosts
4545 (funcall (nth 0 x) (nth 1 x)))))
4546 (tramp-get-completion-function m))
4547
4548 (setq result (append result
4549 (mapcar
4550 (lambda (x)
4551 (tramp-get-completion-user-host
4552 method user host (nth 0 x) (nth 1 x)))
4553 (delq nil all-user-hosts)))))
4554
4555 ;; Possible methods
4556 (setq result
4557 (append result (tramp-get-completion-methods m)))))
4558
4559 (setq v (cdr v))))
4560
4561 ;; unify list, remove nil elements
4562 (while result
4563 (let ((car (car result)))
4564 (when car (add-to-list 'result1 car))
4565 (setq result (cdr result))))
4566
4567 ;; Complete local parts
4568 (append
4569 result1
4570 (condition-case nil
4571 (if result1
4572 ;; "/ssh:" does not need to be expanded as hostname.
4573 (tramp-run-real-handler
4574 'file-name-all-completions (list filename directory))
4575 ;; No method/user/host found to be expanded.
4576 (tramp-completion-run-real-handler
4577 'file-name-all-completions (list filename directory)))
4578 (error nil)))))
4579 ;; unwindform
4580 (setq tramp-completion-mode nil)))
16674e4f
KG
4581
4582;; Method, host name and user name completion for a file.
4583(defun tramp-completion-handle-file-name-completion (filename directory)
4584 "Like `file-name-completion' for tramp files."
4585 (try-completion filename
4586 (mapcar 'list (file-name-all-completions filename directory))))
4587
4588;; I misuse a little bit the tramp-file-name structure in order to handle
4589;; completion possibilities for partial methods / user names / host names.
4590;; Return value is a list of tramp-file-name structures according to possible
7432277c 4591;; completions. If "multi-method" or "localname" is non-nil it means there
16674e4f
KG
4592;; shouldn't be a completion anymore.
4593
4594;; Expected results:
4595
4596;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4597;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4598;; [nil nil "x" nil nil]
4599;; [nil "x" nil nil nil]
4600
bf247b6e 4601;; "/x:" "/x:y" "/x:y:"
16674e4f
KG
4602;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4603;; "/[x/" "/[x/y"
4604;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4605;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4606
4607;; "/x:y@" "/x:y@z" "/x:y@z:"
4608;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4609;; "/[x/y@" "/[x/y@z"
4610;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4611(defun tramp-completion-dissect-file-name (name)
4612 "Returns a list of `tramp-file-name' structures.
4613They are collected by `tramp-completion-dissect-file-name1'."
4614
4615 (let* ((result)
4007ba5b
KG
4616 (x-nil "\\|\\(\\)")
4617 ;; "/method" "/[method"
4618 (tramp-completion-file-name-structure1
4619 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4620 1 nil nil nil))
4621 ;; "/user" "/[user"
4622 (tramp-completion-file-name-structure2
4623 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4624 nil 1 nil nil))
4625 ;; "/host" "/[host"
4626 (tramp-completion-file-name-structure3
4627 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4628 nil nil 1 nil))
4629 ;; "/user@host" "/[user@host"
4630 (tramp-completion-file-name-structure4
4631 (list (concat tramp-prefix-regexp
4632 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4633 "\\(" tramp-host-regexp x-nil "\\)$")
4634 nil 1 2 nil))
4635 ;; "/method:user" "/[method/user"
4636 (tramp-completion-file-name-structure5
4637 (list (concat tramp-prefix-regexp
4638 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4639 "\\(" tramp-user-regexp x-nil "\\)$")
4640 1 2 nil nil))
4641 ;; "/method:host" "/[method/host"
4642 (tramp-completion-file-name-structure6
4643 (list (concat tramp-prefix-regexp
4644 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4645 "\\(" tramp-host-regexp x-nil "\\)$")
4646 1 nil 2 nil))
4647 ;; "/method:user@host" "/[method/user@host"
4648 (tramp-completion-file-name-structure7
4649 (list (concat tramp-prefix-regexp
4650 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4651 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4652 "\\(" tramp-host-regexp x-nil "\\)$")
4653 1 2 3 nil)))
4654
4655 (mapcar (lambda (regexp)
16674e4f
KG
4656 (add-to-list 'result
4657 (tramp-completion-dissect-file-name1 regexp name)))
4658 (list
4659 tramp-completion-file-name-structure1
4660 tramp-completion-file-name-structure2
4661 tramp-completion-file-name-structure3
4662 tramp-completion-file-name-structure4
4663 tramp-completion-file-name-structure5
4664 tramp-completion-file-name-structure6
4665 tramp-completion-file-name-structure7
4666 tramp-file-name-structure))
4667
4668 (delq nil result)))
4669
4670(defun tramp-completion-dissect-file-name1 (structure name)
4671 "Returns a `tramp-file-name' structure matching STRUCTURE.
4672The structure consists of multi-method, remote method, remote user,
7432277c 4673remote host and localname (filename on remote host)."
fb7933a3 4674
16674e4f
KG
4675 (let (method)
4676 (save-match-data
4677 (when (string-match (nth 0 structure) name)
4007ba5b
KG
4678 (setq method (and (nth 1 structure)
4679 (match-string (nth 1 structure) name)))
16674e4f
KG
4680 (if (and method (member method tramp-multi-methods))
4681 ;; Not handled (yet).
4682 (make-tramp-file-name
4683 :multi-method method
4684 :method nil
4685 :user nil
4686 :host nil
7432277c 4687 :localname nil)
4007ba5b
KG
4688 (let ((user (and (nth 2 structure)
4689 (match-string (nth 2 structure) name)))
4690 (host (and (nth 3 structure)
4691 (match-string (nth 3 structure) name)))
7432277c 4692 (localname (and (nth 4 structure)
4007ba5b 4693 (match-string (nth 4 structure) name))))
16674e4f
KG
4694 (make-tramp-file-name
4695 :multi-method nil
4696 :method method
4697 :user user
4698 :host host
7432277c 4699 :localname localname)))))))
16674e4f
KG
4700
4701;; This function returns all possible method completions, adding the
4702;; trailing method delimeter.
16674e4f
KG
4703(defun tramp-get-completion-methods (partial-method)
4704 "Returns all method completions for PARTIAL-METHOD."
4007ba5b
KG
4705 (mapcar
4706 (lambda (method)
4707 (and method
4708 (string-match (concat "^" (regexp-quote partial-method)) method)
4709 ;; we must remove leading "/".
4710 (substring (tramp-make-tramp-file-name nil method nil nil nil) 1)))
4711 (delete "multi" (mapcar 'car tramp-methods))))
16674e4f
KG
4712
4713;; Compares partial user and host names with possible completions.
4714(defun tramp-get-completion-user-host (method partial-user partial-host user host)
4715 "Returns the most expanded string for user and host name completion.
4716PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4717 (cond
4718
4719 ((and partial-user partial-host)
4720 (if (and host
4721 (string-match (concat "^" (regexp-quote partial-host)) host)
4722 (string-equal partial-user (or user partial-user)))
4723 (setq user partial-user)
4724 (setq user nil
4725 host nil)))
4726
4727 (partial-user
4728 (setq host nil)
4729 (unless
4730 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4731 (setq user nil)))
4732
4733 (partial-host
4734 (setq user nil)
4735 (unless
4736 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4737 (setq host nil)))
4738
4739 (t (setq user nil
4740 host nil)))
4741
292ffc15 4742 (unless (zerop (+ (length user) (length host)))
16674e4f
KG
4743 ;; we must remove leading "/".
4744 (substring (tramp-make-tramp-file-name nil method user host nil) 1)))
4745
4746(defun tramp-parse-rhosts (filename)
4747 "Return a list of (user host) tuples allowed to access.
292ffc15 4748Either user or host may be nil."
16674e4f
KG
4749
4750 (let (res)
8daea7fc 4751 (when (file-readable-p filename)
16674e4f
KG
4752 (with-temp-buffer
4753 (insert-file-contents filename)
4754 (goto-char (point-min))
4755 (while (not (eobp))
292ffc15 4756 (push (tramp-parse-rhosts-group) res))))
16674e4f
KG
4757 res))
4758
4759;; Taken from gnus/netrc.el
4760(eval-and-compile
4761 (defalias 'tramp-point-at-eol
4762 (if (fboundp 'point-at-eol)
4763 'point-at-eol
4764 'line-end-position)))
4765
4766(defun tramp-parse-rhosts-group ()
4767 "Return a (user host) tuple allowed to access.
292ffc15 4768Either user or host may be nil."
16674e4f
KG
4769
4770 (let ((result)
4771 (regexp
4772 (concat
4773 "^\\(" tramp-host-regexp "\\)"
4774 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4775
4776 (narrow-to-region (point) (tramp-point-at-eol))
4777 (when (re-search-forward regexp nil t)
4778 (setq result (append (list (match-string 3) (match-string 1)))))
4779 (widen)
4780 (forward-line 1)
4781 result))
4782
4783(defun tramp-parse-shosts (filename)
4784 "Return a list of (user host) tuples allowed to access.
4785User is always nil."
4786
4787 (let (res)
8daea7fc 4788 (when (file-readable-p filename)
16674e4f
KG
4789 (with-temp-buffer
4790 (insert-file-contents filename)
4791 (goto-char (point-min))
4792 (while (not (eobp))
292ffc15 4793 (push (tramp-parse-shosts-group) res))))
16674e4f
KG
4794 res))
4795
4796(defun tramp-parse-shosts-group ()
4797 "Return a (user host) tuple allowed to access.
4798User is always nil."
4799
4800 (let ((result)
4801 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4802
4803 (narrow-to-region (point) (tramp-point-at-eol))
4804 (when (re-search-forward regexp nil t)
4805 (setq result (list nil (match-string 1))))
4806 (widen)
4807 (or
4808 (> (skip-chars-forward ",") 0)
4809 (forward-line 1))
4810 result))
4811
8daea7fc
KG
4812(defun tramp-parse-sconfig (filename)
4813 "Return a list of (user host) tuples allowed to access.
4814User is always nil."
4815
4816 (let (res)
4817 (when (file-readable-p filename)
4818 (with-temp-buffer
4819 (insert-file-contents filename)
4820 (goto-char (point-min))
4821 (while (not (eobp))
4822 (push (tramp-parse-sconfig-group) res))))
4823 res))
4824
4825(defun tramp-parse-sconfig-group ()
4826 "Return a (user host) tuple allowed to access.
4827User is always nil."
4828
4829 (let ((result)
4830 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4831
4832 (narrow-to-region (point) (tramp-point-at-eol))
4833 (when (re-search-forward regexp nil t)
4834 (setq result (list nil (match-string 1))))
4835 (widen)
4836 (or
4837 (> (skip-chars-forward ",") 0)
4838 (forward-line 1))
4839 result))
4840
5ec2cc41
KG
4841(defun tramp-parse-shostkeys (dirname)
4842 "Return a list of (user host) tuples allowed to access.
4843User is always nil."
4844
4845 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4846 (files (when (file-directory-p dirname) (directory-files dirname)))
4847 result)
4848
4849 (while files
4850 (when (string-match regexp (car files))
4851 (push (list nil (match-string 1 (car files))) result))
4852 (setq files (cdr files)))
4853 result))
4854
4855(defun tramp-parse-sknownhosts (dirname)
4856 "Return a list of (user host) tuples allowed to access.
4857User is always nil."
4858
4859 (let ((regexp (concat "^\\(" tramp-host-regexp
4860 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4861 (files (when (file-directory-p dirname) (directory-files dirname)))
4862 result)
4863
4864 (while files
4865 (when (string-match regexp (car files))
4866 (push (list nil (match-string 1 (car files))) result))
4867 (setq files (cdr files)))
4868 result))
4869
16674e4f
KG
4870(defun tramp-parse-hosts (filename)
4871 "Return a list of (user host) tuples allowed to access.
4872User is always nil."
4873
4874 (let (res)
8daea7fc 4875 (when (file-readable-p filename)
16674e4f
KG
4876 (with-temp-buffer
4877 (insert-file-contents filename)
4878 (goto-char (point-min))
4879 (while (not (eobp))
292ffc15 4880 (push (tramp-parse-hosts-group) res))))
16674e4f
KG
4881 res))
4882
4883(defun tramp-parse-hosts-group ()
4884 "Return a (user host) tuple allowed to access.
4885User is always nil."
4886
4887 (let ((result)
4888 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4889
4890 (narrow-to-region (point) (tramp-point-at-eol))
4891 (when (re-search-forward regexp nil t)
4892 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4893 (setq result (list nil (match-string 1)))))
4894 (widen)
4895 (or
4896 (> (skip-chars-forward " \t") 0)
4897 (forward-line 1))
4898 result))
4899
8daea7fc
KG
4900;; For su-alike methods it would be desirable to return "root@localhost"
4901;; as default. Unfortunately, we have no information whether any user name
4902;; has been typed already. So we (mis-)use tramp-current-user as indication,
4903;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
16674e4f
KG
4904(defun tramp-parse-passwd (filename)
4905 "Return a list of (user host) tuples allowed to access.
4906Host is always \"localhost\"."
4907
4908 (let (res)
8daea7fc 4909 (if (zerop (length tramp-current-user))
16674e4f 4910 '(("root" nil))
8daea7fc 4911 (when (file-readable-p filename)
16674e4f
KG
4912 (with-temp-buffer
4913 (insert-file-contents filename)
4914 (goto-char (point-min))
4915 (while (not (eobp))
292ffc15 4916 (push (tramp-parse-passwd-group) res))))
16674e4f
KG
4917 res)))
4918
4919(defun tramp-parse-passwd-group ()
4920 "Return a (user host) tuple allowed to access.
292ffc15 4921Host is always \"localhost\"."
16674e4f
KG
4922
4923 (let ((result)
4924 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4925
4926 (narrow-to-region (point) (tramp-point-at-eol))
4927 (when (re-search-forward regexp nil t)
4928 (setq result (list (match-string 1) "localhost")))
4929 (widen)
4930 (forward-line 1)
4931 result))
4932
292ffc15
KG
4933(defun tramp-parse-netrc (filename)
4934 "Return a list of (user host) tuples allowed to access.
4935User may be nil."
4936
4937 (let (res)
8daea7fc 4938 (when (file-readable-p filename)
292ffc15
KG
4939 (with-temp-buffer
4940 (insert-file-contents filename)
4941 (goto-char (point-min))
4942 (while (not (eobp))
4943 (push (tramp-parse-netrc-group) res))))
4944 res))
4945
4946(defun tramp-parse-netrc-group ()
4947 "Return a (user host) tuple allowed to access.
4948User may be nil."
4949
4950 (let ((result)
4951 (regexp
4952 (concat
4953 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
4954 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4955
4956 (narrow-to-region (point) (tramp-point-at-eol))
4957 (when (re-search-forward regexp nil t)
4958 (setq result (list (match-string 3) (match-string 1))))
4959 (widen)
4960 (forward-line 1)
4961 result))
4962
16674e4f
KG
4963(defun tramp-completion-handle-expand-file-name (name &optional dir)
4964 "Like `expand-file-name' for tramp files."
4965 (let ((fullname (concat (or dir default-directory) name)))
d2a2c17f
MA
4966 (if (tramp-completion-mode fullname)
4967 (tramp-run-real-handler
4968 'expand-file-name (list name dir))
4969 (tramp-completion-run-real-handler
4970 'expand-file-name (list name dir)))))
fb7933a3
KG
4971
4972;;; Internal Functions:
4973
c08e6004 4974(defun tramp-maybe-send-perl-script (multi-method method user host script name)
c82c5727
LH
4975 "Define in remote shell function NAME implemented as perl SCRIPT.
4976Only send the definition if it has not already been done.
4977Function may have 0-3 parameters."
4978 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
4979 (unless remote-perl (error "No remote perl"))
4980 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
4981 multi-method method user host)))
4982 (unless (memq name perl-scripts)
4983 (with-current-buffer (tramp-get-buffer multi-method method user host)
4984 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
4985 (tramp-send-string multi-method method user host
4986 (concat name
4987 " () {\n"
4988 remote-perl
4989 " -e '"
4990 script
4991 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
4992 (tramp-wait-for-output)
4993 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
4994 multi-method method user host)
4995 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
4996
fb7933a3
KG
4997(defun tramp-set-auto-save ()
4998 (when (and (buffer-file-name)
4999 (tramp-tramp-file-p (buffer-file-name))
7177e2a3
MA
5000 ;; ange-ftp has its own auto-save mechanism
5001 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5002 'tramp-sh-file-name-handler)
fb7933a3
KG
5003 auto-save-default)
5004 (auto-save-mode 1)))
5005(add-hook 'find-file-hooks 'tramp-set-auto-save t)
a69c01a0
MA
5006(add-hook 'tramp-unload-hook
5007 '(lambda ()
5008 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
fb7933a3
KG
5009
5010(defun tramp-run-test (switch filename)
5011 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5012Returns the exit code of the `test' program."
5013 (let ((v (tramp-dissect-file-name filename)))
5014 (save-excursion
5015 (tramp-send-command-and-check
5016 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5017 (tramp-file-name-user v) (tramp-file-name-host v)
5018 (format "test %s %s" switch
7432277c 5019 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
fb7933a3
KG
5020
5021(defun tramp-run-test2 (program file1 file2 &optional switch)
5022 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5023The optional SWITCH is inserted between the two files.
5024Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5025hosts, or files, disagree."
5026 (let* ((v1 (tramp-dissect-file-name file1))
5027 (v2 (tramp-dissect-file-name file2))
5028 (mmethod1 (tramp-file-name-multi-method v1))
5029 (mmethod2 (tramp-file-name-multi-method v2))
5030 (method1 (tramp-file-name-method v1))
5031 (method2 (tramp-file-name-method v2))
5032 (user1 (tramp-file-name-user v1))
5033 (user2 (tramp-file-name-user v2))
5034 (host1 (tramp-file-name-host v1))
5035 (host2 (tramp-file-name-host v2))
7432277c
KG
5036 (localname1 (tramp-file-name-localname v1))
5037 (localname2 (tramp-file-name-localname v2)))
fb7933a3
KG
5038 (unless (and method1 method2 host1 host2
5039 (equal mmethod1 mmethod2)
5040 (equal method1 method2)
5041 (equal user1 user2)
5042 (equal host1 host2))
5043 (error "tramp-run-test2: %s"
5044 "only implemented for same method, same user, same host"))
5045 (save-excursion
5046 (tramp-send-command-and-check
5047 mmethod1 method1 user1 host1
5048 (format "%s %s %s %s"
5049 program
7432277c 5050 (tramp-shell-quote-argument localname1)
fb7933a3 5051 (or switch "")
7432277c 5052 (tramp-shell-quote-argument localname2))))))
fb7933a3 5053
5ec2cc41
KG
5054(defun tramp-touch (file time)
5055 "Set the last-modified timestamp of the given file.
5056TIME is an Emacs internal time value as returned by `current-time'."
5057 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time)))
38c65fca
KG
5058 (if (tramp-tramp-file-p file)
5059 (with-parsed-tramp-file-name file nil
5060 (let ((buf (tramp-get-buffer multi-method method user host)))
5061 (unless (zerop (tramp-send-command-and-check
5062 multi-method method user host
5063 (format "touch -t %s %s"
5064 touch-time
5065 localname)))
5066 (pop-to-buffer buf)
5067 (error "tramp-touch: touch failed, see buffer `%s' for details"
5068 buf))))
5069 ;; It's a local file
5070 (with-temp-buffer
5071 (unless (zerop (call-process
5072 "touch" nil (current-buffer) nil "-t" touch-time file))
5073 (pop-to-buffer (current-buffer))
5074 (error "tramp-touch: touch failed"))))))
bf247b6e 5075
fb7933a3
KG
5076(defun tramp-buffer-name (multi-method method user host)
5077 "A name for the connection buffer for USER at HOST using METHOD."
90f8dc03
KG
5078 (if multi-method
5079 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5080 (let ((method (tramp-find-method multi-method method user host)))
5081 (if user
5ec2cc41
KG
5082 (format "*tramp/%s %s@%s*" method user host)
5083 (format "*tramp/%s %s*" method host)))))
fb7933a3
KG
5084
5085(defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5086 "A name for the multi method connection buffer.
5087MULTI-METHOD gives the multi method, METHOD the array of methods,
5088USER the array of user names, HOST the array of host names."
5089 (unless (and (= (length method) (length user))
5090 (= (length method) (length host)))
5091 (error "Syntax error in multi method (implementation error)"))
5092 (let ((len (length method))
5093 (i 0)
5094 string-list)
5095 (while (< i len)
5096 (setq string-list
5097 (cons (if (aref user i)
5098 (format "%s#%s@%s:" (aref method i)
5099 (aref user i) (aref host i))
5100 (format "%s@%s:" (aref method i) (aref host i)))
5101 string-list))
5102 (incf i))
5103 (format "*%s/%s %s*"
5104 prefix multi-method
5105 (apply 'concat (reverse string-list)))))
5106
5107(defun tramp-get-buffer (multi-method method user host)
5108 "Get the connection buffer to be used for USER at HOST using METHOD."
01917a18
MA
5109 (with-current-buffer
5110 (get-buffer-create (tramp-buffer-name multi-method method user host))
5111 (setq buffer-undo-list t)
5112 (current-buffer)))
fb7933a3
KG
5113
5114(defun tramp-debug-buffer-name (multi-method method user host)
5115 "A name for the debug buffer for USER at HOST using METHOD."
90f8dc03
KG
5116 (if multi-method
5117 (tramp-buffer-name-multi-method "debug tramp"
5118 multi-method method user host)
5119 (let ((method (tramp-find-method multi-method method user host)))
5120 (if user
5121 (format "*debug tramp/%s %s@%s*" method user host)
5122 (format "*debug tramp/%s %s*" method host)))))
fb7933a3
KG
5123
5124(defun tramp-get-debug-buffer (multi-method method user host)
5125 "Get the debug buffer for USER at HOST using METHOD."
01917a18
MA
5126 (with-current-buffer
5127 (get-buffer-create
5128 (tramp-debug-buffer-name multi-method method user host))
5129 (setq buffer-undo-list t)
5130 (current-buffer)))
fb7933a3
KG
5131
5132(defun tramp-find-executable (multi-method method user host
5133 progname dirlist ignore-tilde)
5134 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5135First args METHOD, USER and HOST specify the connection, PROGNAME
5136is the program to search for, and DIRLIST gives the list of directories
5137to search. If IGNORE-TILDE is non-nil, directory names starting
5138with `~' will be ignored.
5139
7432277c 5140Returns the absolute file name of PROGNAME, if found, and nil otherwise.
fb7933a3
KG
5141
5142This function expects to be in the right *tramp* buffer."
5143 (let (result)
5144 (when ignore-tilde
5145 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5146 ;; `remove' is in CL, and we want to avoid CL dependencies.
5147 (let (newdl d)
5148 (while dirlist
5149 (setq d (car dirlist))
5150 (setq dirlist (cdr dirlist))
5151 (unless (char-equal ?~ (aref d 0))
5152 (setq newdl (cons d newdl))))
5153 (setq dirlist (nreverse newdl))))
5154 (tramp-send-command
5155 multi-method method user host
5156 (format (concat "while read d; "
5157 "do if test -x $d/%s -a -f $d/%s; "
5158 "then echo tramp_executable $d/%s; "
5159 "break; fi; done <<'EOF'")
5160 progname progname progname))
5161 (mapcar (lambda (d)
5162 (tramp-send-command multi-method method user host d))
5163 dirlist)
5164 (tramp-send-command multi-method method user host "EOF")
5165 (tramp-wait-for-output)
5166 (goto-char (point-max))
5167 (when (search-backward "tramp_executable " nil t)
5168 (skip-chars-forward "^ ")
5169 (skip-chars-forward " ")
5170 (buffer-substring (point) (tramp-line-end-position)))))
5171
5172(defun tramp-set-remote-path (multi-method method user host var dirlist)
5173 "Sets the remote environment VAR to existing directories from DIRLIST.
5174I.e., for each directory in DIRLIST, it is tested whether it exists and if
5175so, it is added to the environment variable VAR."
5176 (let ((existing-dirs
5177 (mapcar
5178 (lambda (x)
5179 (when (and
5180 (file-exists-p
5181 (tramp-make-tramp-file-name multi-method method user host x))
5182 (file-directory-p
5183 (tramp-make-tramp-file-name multi-method method user host x)))
5184 x))
5185 dirlist)))
5186 (tramp-send-command
5187 multi-method method user host
5188 (concat var "="
5189 (mapconcat 'identity (delq nil existing-dirs) ":")
5190 "; export " var))
5191 (tramp-wait-for-output)))
5192
5193;; -- communication with external shell --
5194
5195(defun tramp-find-file-exists-command (multi-method method user host)
5196 "Find a command on the remote host for checking if a file exists.
5197Here, we are looking for a command which has zero exit status if the
5198file exists and nonzero exit status otherwise."
5199 (make-local-variable 'tramp-file-exists-command)
821e6e36 5200 (tramp-message 9 "Finding command to check if file exists")
fb7933a3
KG
5201 (let ((existing
5202 (tramp-make-tramp-file-name
5203 multi-method method user host
5204 "/")) ;assume this file always exists
5205 (nonexisting
5206 (tramp-make-tramp-file-name
5207 multi-method method user host
5208 "/ this file does not exist "))) ;assume this never exists
5209 ;; The algorithm is as follows: we try a list of several commands.
5210 ;; For each command, we first run `$cmd /' -- this should return
5211 ;; true, as the root directory always exists. And then we run
5212 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5213 ;; does not exist. This should return false. We use the first
5214 ;; command we find that seems to work.
5215 ;; The list of commands to try is as follows:
5216 ;; `ls -d' This works on most systems, but NetBSD 1.4
5217 ;; has a bug: `ls' always returns zero exit
5218 ;; status, even for files which don't exist.
5219 ;; `test -e' Some Bourne shells have a `test' builtin
5220 ;; which does not know the `-e' option.
5221 ;; `/bin/test -e' For those, the `test' binary on disk normally
5222 ;; provides the option. Alas, the binary
5223 ;; is sometimes `/bin/test' and sometimes it's
5224 ;; `/usr/bin/test'.
5225 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5226 (unless (or
fb7933a3 5227 (and (setq tramp-file-exists-command "test -e %s")
07dfe738
KG
5228 (file-exists-p existing)
5229 (not (file-exists-p nonexisting)))
fb7933a3 5230 (and (setq tramp-file-exists-command "/bin/test -e %s")
07dfe738
KG
5231 (file-exists-p existing)
5232 (not (file-exists-p nonexisting)))
fb7933a3 5233 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
07dfe738
KG
5234 (file-exists-p existing)
5235 (not (file-exists-p nonexisting)))
5beaf831 5236 (and (setq tramp-file-exists-command "ls -d %s")
07dfe738
KG
5237 (file-exists-p existing)
5238 (not (file-exists-p nonexisting))))
ff12e78d 5239 (error "Couldn't find command to check if file exists"))))
bf247b6e 5240
fb7933a3
KG
5241
5242;; CCC test ksh or bash found for tilde expansion?
5243(defun tramp-find-shell (multi-method method user host)
5244 "Find a shell on the remote host which groks tilde expansion."
5245 (let ((shell nil))
5246 (tramp-send-command multi-method method user host "echo ~root")
5247 (tramp-wait-for-output)
5248 (cond
5249 ((string-match "^~root$" (buffer-string))
5250 (setq shell
5251 (or (tramp-find-executable multi-method method user host
c62c9d08 5252 "bash" tramp-remote-path t)
fb7933a3 5253 (tramp-find-executable multi-method method user host
c62c9d08 5254 "ksh" tramp-remote-path t)))
fb7933a3
KG
5255 (unless shell
5256 (error "Couldn't find a shell which groks tilde expansion"))
c62c9d08
KG
5257 ;; Find arguments for this shell.
5258 (let ((alist tramp-sh-extra-args)
5259 item extra-args)
5260 (while (and alist (null extra-args))
5261 (setq item (pop alist))
5262 (when (string-match (car item) shell)
5263 (setq extra-args (cdr item))))
5264 (when extra-args (setq shell (concat shell " " extra-args))))
fb7933a3
KG
5265 (tramp-message
5266 5 "Starting remote shell `%s' for tilde expansion..." shell)
5267 (tramp-send-command
5268 multi-method method user host
89509ea0 5269 (concat "PS1='$ ' exec " shell)) ;
d2a2c17f
MA
5270 (tramp-barf-if-no-shell-prompt
5271 (get-buffer-process (current-buffer))
5272 60 "Couldn't find remote `%s' prompt" shell)
89509ea0 5273 (tramp-message
821e6e36 5274 9 "Setting remote shell prompt...")
16674e4f
KG
5275 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5276 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5277 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5278 ;; as well.
fb7933a3 5279 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
16674e4f 5280 tramp-rsh-end-of-line
fb7933a3 5281 tramp-end-of-output
16674e4f 5282 tramp-rsh-end-of-line
fb7933a3
KG
5283 tramp-rsh-end-of-line))
5284 (tramp-wait-for-output)
89509ea0 5285 (tramp-message
821e6e36 5286 9 "Setting remote shell prompt...done")
685f5858 5287 )
fb7933a3 5288 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
c951aecb
KG
5289 (tramp-get-method-parameter
5290 multi-method method user host 'tramp-remote-sh))))))
fb7933a3
KG
5291
5292(defun tramp-check-ls-command (multi-method method user host cmd)
5293 "Checks whether the given `ls' executable groks `-n'.
7432277c 5294METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
fb7933a3
KG
5295the `ls' executable. Returns t if CMD supports the `-n' option, nil
5296otherwise."
07dfe738
KG
5297 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5298 (when (file-executable-p
fb7933a3
KG
5299 (tramp-make-tramp-file-name multi-method method user host cmd))
5300 (let ((result nil))
5301 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5302 (setq result
5303 (tramp-send-command-and-check
5304 multi-method method user host
5305 (format "%s -lnd / >/dev/null"
5306 cmd)))
5307 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5308 cmd
5309 (if (zerop result) "okay" "failed"))
5310 (zerop result))))
5311
5312(defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5313 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5314Returns nil if none was found, else the command is returned."
5315 (let ((dl dirlist)
19a87064
MA
5316 (result nil))
5317 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5318 ;; It would be better to use the CL function `find', but
5319 ;; we don't want run-time dependencies on CL.
5320 (while (and dl (not result))
5321 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5322 (when (tramp-check-ls-command multi-method method user host x)
5323 (setq result x)))
5324 (setq dl (cdr dl)))
5325 result)))
fb7933a3
KG
5326
5327(defun tramp-find-ls-command (multi-method method user host)
5328 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5329\(This option prints numeric user and group ids in a long listing.)"
5330 (tramp-message 9 "Finding a suitable `ls' command")
5331 (or
5332 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5333 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5334 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5335
bf247b6e
KS
5336;; ------------------------------------------------------------
5337;; -- Functions for establishing connection --
5338;; ------------------------------------------------------------
fb7933a3 5339
ac474af1
KG
5340;; The following functions are actions to be taken when seeing certain
5341;; prompts from the remote host. See the variable
5342;; `tramp-actions-before-shell' for usage of these functions.
5343
5344(defun tramp-action-login (p multi-method method user host)
5345 "Send the login name."
5346 (tramp-message 9 "Sending login name `%s'"
5347 (or user (user-login-name)))
5348 (erase-buffer)
5349 (process-send-string nil (concat (or user (user-login-name))
5350 tramp-rsh-end-of-line)))
5351
5352(defun tramp-action-password (p multi-method method user host)
5353 "Query the user for a password."
553f03bc
MA
5354 (let ((pw-prompt
5355 (format "Password for %s "
5356 (tramp-make-tramp-file-name
5357 nil method user host ""))))
3b89d388 5358 (tramp-message 9 "Sending password")
07dfe738 5359 (tramp-enter-password p pw-prompt user host)))
ac474af1
KG
5360
5361(defun tramp-action-succeed (p multi-method method user host)
5362 "Signal success in finding shell prompt."
5363 (tramp-message 9 "Found remote shell prompt.")
5364 (erase-buffer)
5365 (throw 'tramp-action 'ok))
5366
5367(defun tramp-action-permission-denied (p multi-method method user host)
5368 "Signal permission denied."
b1d06e75 5369 (pop-to-buffer (tramp-get-buffer multi-method method user host))
ac474af1
KG
5370 (tramp-message 9 "Permission denied by remote host.")
5371 (kill-process p)
ac474af1
KG
5372 (throw 'tramp-action 'permission-denied))
5373
5374(defun tramp-action-yesno (p multi-method method user host)
3cdaec13
KG
5375 "Ask the user for confirmation using `yes-or-no-p'.
5376Send \"yes\" to remote process on confirmation, abort otherwise.
5377See also `tramp-action-yn'."
ac474af1
KG
5378 (save-window-excursion
5379 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5380 (unless (yes-or-no-p (match-string 0))
5381 (kill-process p)
5382 (erase-buffer)
5383 (throw 'tramp-action 'permission-denied))
5384 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5385 (erase-buffer)))
5386
3cdaec13
KG
5387(defun tramp-action-yn (p multi-method method user host)
5388 "Ask the user for confirmation using `y-or-n-p'.
5389Send \"y\" to remote process on confirmation, abort otherwise.
5390See also `tramp-action-yesno'."
5391 (save-window-excursion
5392 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5393 (unless (y-or-n-p (match-string 0))
5394 (kill-process p)
3cdaec13 5395 (throw 'tramp-action 'permission-denied))
4007ba5b 5396 (erase-buffer)
3cdaec13
KG
5397 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5398
487f4fb7
KG
5399(defun tramp-action-terminal (p multi-method method user host)
5400 "Tell the remote host which terminal type to use.
5401The terminal type can be configured with `tramp-terminal-type'."
5402 (tramp-message 9 "Setting `%s' as terminal type."
5403 tramp-terminal-type)
5404 (erase-buffer)
5405 (process-send-string nil (concat tramp-terminal-type
5406 tramp-rsh-end-of-line)))
5407
19a87064
MA
5408(defun tramp-action-process-alive (p multi-method method user host)
5409 "Check whether a process has finished."
5410 (unless (memq (process-status p) '(run open))
5411 (throw 'tramp-action 'process-died)))
5412
38c65fca
KG
5413(defun tramp-action-out-of-band (p multi-method method user host)
5414 "Check whether an out-of-band copy has finished."
5415 (cond ((and (memq (process-status p) '(stop exit))
5416 (zerop (process-exit-status p)))
5417 (tramp-message 9 "Process has finished.")
5418 (throw 'tramp-action 'ok))
5419 ((or (and (memq (process-status p) '(stop exit))
5420 (not (zerop (process-exit-status p))))
5421 (memq (process-status p) '(signal)))
01917a18
MA
5422 ;; `scp' could have copied correctly, but set modes could have failed.
5423 ;; This can be ignored.
5424 (goto-char (point-min))
5425 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5426 (progn
5427 (tramp-message 10 "'set mode' error ignored.")
5428 (tramp-message 9 "Process has finished.")
5429 (throw 'tramp-action 'ok))
e89cd3e6
LH
5430 (goto-char (point-min))
5431 (when (re-search-forward "^.cp.?: \\(.+: Permission denied.?\\)$" nil t)
5432 (error "Remote host: %s" (match-string 1)))
01917a18
MA
5433 (tramp-message 9 "Process has died.")
5434 (throw 'tramp-action 'process-died)))
38c65fca
KG
5435 (t nil)))
5436
ac474af1
KG
5437;; The following functions are specifically for multi connections.
5438
5439(defun tramp-multi-action-login (p method user host)
5440 "Send the login name."
5441 (tramp-message 9 "Sending login name `%s'" user)
5442 (erase-buffer)
5443 (process-send-string p (concat user tramp-rsh-end-of-line)))
5444
5445(defun tramp-multi-action-password (p method user host)
5446 "Query the user for a password."
553f03bc
MA
5447 (let ((pw-prompt
5448 (format "Password for %s "
5449 (tramp-make-tramp-file-name
5450 nil method user host ""))))
5451 (tramp-message 9 "Sending password")
5452 (tramp-enter-password p pw-prompt user host)))
ac474af1
KG
5453
5454(defun tramp-multi-action-succeed (p method user host)
5455 "Signal success in finding shell prompt."
5456 (tramp-message 9 "Found shell prompt on `%s'" host)
5457 (erase-buffer)
5458 (throw 'tramp-action 'ok))
5459
5460(defun tramp-multi-action-permission-denied (p method user host)
5461 "Signal permission denied."
5462 (tramp-message 9 "Permission denied by remote host `%s'" host)
5463 (kill-process p)
5464 (erase-buffer)
5465 (throw 'tramp-action 'permission-denied))
5466
07dfe738
KG
5467(defun tramp-multi-action-process-alive (p method user host)
5468 "Check whether a process has finished."
5469 (unless (memq (process-status p) '(run open))
5470 (throw 'tramp-action 'process-died)))
5471
ac474af1
KG
5472;; Functions for processing the actions.
5473
5474(defun tramp-process-one-action (p multi-method method user host actions)
5475 "Wait for output from the shell and perform one action."
5476 (let (found item pattern action todo)
5477 (erase-buffer)
5478 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5479 (with-timeout (60 (throw 'tramp-action 'timeout))
5480 (while (not found)
d2a2c17f 5481 (tramp-accept-process-output p 1)
ac474af1
KG
5482 (goto-char (point-min))
5483 (setq todo actions)
5484 (while todo
5485 (goto-char (point-min))
5486 (setq item (pop todo))
5487 (setq pattern (symbol-value (nth 0 item)))
5488 (setq action (nth 1 item))
821e6e36
KG
5489 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5490 pattern)
ac474af1
KG
5491 (when (re-search-forward (concat pattern "\\'") nil t)
5492 (setq found (funcall action p multi-method method user host)))))
5493 found)))
5494
5495(defun tramp-process-actions (p multi-method method user host actions)
5496 "Perform actions until success."
5497 (let (exit)
5498 (while (not exit)
821e6e36 5499 (tramp-message 9 "Waiting for prompts from remote shell")
ac474af1
KG
5500 (setq exit
5501 (catch 'tramp-action
5502 (tramp-process-one-action
5503 p multi-method 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
5509;; For multi-actions.
5510
5511(defun tramp-process-one-multi-action (p method user host actions)
5512 "Wait for output from the shell and perform one action."
5513 (let (found item pattern action todo)
5514 (erase-buffer)
5515 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5516 (with-timeout (60 (throw 'tramp-action 'timeout))
5517 (while (not found)
d2a2c17f 5518 (tramp-accept-process-output p 1)
ac474af1
KG
5519 (setq todo actions)
5520 (goto-char (point-min))
5521 (while todo
5522 (goto-char (point-min))
5523 (setq item (pop todo))
5524 (setq pattern (symbol-value (nth 0 item)))
5525 (setq action (nth 1 item))
821e6e36
KG
5526 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5527 pattern)
ac474af1
KG
5528 (when (re-search-forward (concat pattern "\\'") nil t)
5529 (setq found (funcall action p method user host)))))
5530 found)))
5531
5532(defun tramp-process-multi-actions (p method user host actions)
5533 "Perform actions until success."
5534 (let (exit)
5535 (while (not exit)
821e6e36 5536 (tramp-message 9 "Waiting for prompts from remote shell")
ac474af1
KG
5537 (setq exit
5538 (catch 'tramp-action
5539 (tramp-process-one-multi-action p method user host actions)
5540 nil)))
5541 (unless (eq exit 'ok)
5ec2cc41 5542 (tramp-clear-passwd user host)
ac474af1
KG
5543 (error "Login failed"))))
5544
90f8dc03
KG
5545;; Functions to execute when we have seen the remote shell prompt but
5546;; before we exec the Bourne-ish shell. Note that these commands
5547;; might be sent to any shell, not just a Bourne-ish shell. This
5548;; means that the commands need to work in all shells. (It is also
5549;; okay for some commands to just fail with an error message, but
5550;; please make sure that they at least don't crash the odd shell people
5551;; might be running...)
5552(defun tramp-process-initial-commands (p
5553 multi-method method user host
5554 commands)
5555 "Send list of commands to remote host, in order."
5556 (let (cmd)
5557 (while commands
5558 (setq cmd (pop commands))
5559 (erase-buffer)
5560 (tramp-message 10 "Sending command to remote shell: %s"
5561 cmd)
38c65fca 5562 (tramp-send-command multi-method method user host cmd nil t)
90f8dc03
KG
5563 (tramp-barf-if-no-shell-prompt
5564 p 60 "Remote shell command failed: %s" cmd))
5565 (erase-buffer)))
5566
ac474af1 5567;; The actual functions for opening connections.
fb7933a3
KG
5568
5569(defun tramp-open-connection-telnet (multi-method method user host)
5570 "Open a connection using a telnet METHOD.
5571This starts the command `telnet HOST ARGS'[*], then waits for a remote
5572login prompt, then sends the user name USER, then waits for a remote
5573password prompt. It queries the user for the password, then sends the
5574password to the remote host.
5575
5576If USER is nil, uses value returned by `(user-login-name)' instead.
5577
821e6e36
KG
5578Recognition of the remote shell prompt is based on the variables
5579`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5580set up correctly.
fb7933a3
KG
5581
5582Please note that it is NOT possible to use this connection method
5583together with an out-of-band transfer method! You must use an inline
5584transfer method.
5585
5586Maybe the different regular expressions need to be tuned.
5587
5588* Actually, the telnet program as well as the args to be used can be
5589 specified in the method parameters, see the variable `tramp-methods'."
5590 (save-match-data
16674e4f 5591 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5592 (error "Cannot use out-of-band method `%s' with telnet connection method"
5593 method))
5594 (when multi-method
5595 (error "Cannot multi-connect using telnet connection method"))
7177e2a3 5596 (tramp-pre-connection multi-method method user host tramp-chunksize)
bf247b6e 5597 (tramp-message 7 "Opening connection for %s@%s using %s..."
fb7933a3
KG
5598 (or user (user-login-name)) host method)
5599 (let ((process-environment (copy-sequence process-environment)))
5600 (setenv "TERM" tramp-terminal-type)
a69c01a0 5601 (setenv "PS1" "$ ")
fb7933a3 5602 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5603 ;; If we omit the conditional here, then we would use
5604 ;; `undecided-dos' in some cases. With the conditional,
5605 ;; we use nil in these cases. Which one is right?
fb7933a3
KG
5606 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5607 (> emacs-major-version 20))
5608 tramp-dos-coding-system))
5609 (p (apply 'start-process
5610 (tramp-buffer-name multi-method method user host)
5611 (tramp-get-buffer multi-method method user host)
c951aecb 5612 (tramp-get-method-parameter
16674e4f 5613 multi-method
91879624 5614 (tramp-find-method multi-method method user host)
c951aecb 5615 user host 'tramp-login-program)
fb7933a3 5616 host
c951aecb 5617 (tramp-get-method-parameter
16674e4f 5618 multi-method
91879624 5619 (tramp-find-method multi-method method user host)
c951aecb 5620 user host 'tramp-login-args)))
fb7933a3
KG
5621 (found nil)
5622 (pw nil))
19a87064 5623 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5624 (set-buffer (tramp-get-buffer multi-method method user host))
5625 (erase-buffer)
5626 (tramp-process-actions p multi-method method user host
5627 tramp-actions-before-shell)
fb7933a3
KG
5628 (tramp-open-connection-setup-interactive-shell
5629 p multi-method method user host)
5630 (tramp-post-connection multi-method method user host)))))
5631
bf247b6e 5632
fb7933a3
KG
5633(defun tramp-open-connection-rsh (multi-method method user host)
5634 "Open a connection using an rsh METHOD.
5635This starts the command `rsh HOST -l USER'[*], then waits for a remote
5636password or shell prompt. If a password prompt is seen, the user is
5637queried for a password, this function sends the password to the remote
5638host and waits for a shell prompt.
5639
5640If USER is nil, start the command `rsh HOST'[*] instead
5641
821e6e36
KG
5642Recognition of the remote shell prompt is based on the variables
5643`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5644set up correctly.
fb7933a3 5645
8e3a1104
KG
5646Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5647be a port number for ssh, and \"-p yy\" will be added to the list of
5648arguments, and xx will be used as the host name to connect to.
5649
fb7933a3
KG
5650* Actually, the rsh program to be used can be specified in the
5651 method parameters, see the variable `tramp-methods'."
5652 (save-match-data
5653 (when multi-method
5654 (error "Cannot multi-connect using rsh connection method"))
7177e2a3 5655 (tramp-pre-connection multi-method method user host tramp-chunksize)
16674e4f 5656 (if (and user (not (string= user "")))
bf247b6e 5657 (tramp-message 7 "Opening connection for %s@%s using %s..."
fb7933a3
KG
5658 user host method)
5659 (tramp-message 7 "Opening connection at %s using %s..." host method))
8e3a1104
KG
5660 (let ((process-environment (copy-sequence process-environment))
5661 (bufnam (tramp-buffer-name multi-method method user host))
5662 (buf (tramp-get-buffer multi-method method user host))
c951aecb 5663 (login-program (tramp-get-method-parameter
16674e4f 5664 multi-method
91879624 5665 (tramp-find-method multi-method method user host)
c951aecb
KG
5666 user host 'tramp-login-program))
5667 (login-args (tramp-get-method-parameter
16674e4f 5668 multi-method
91879624 5669 (tramp-find-method multi-method method user host)
07dfe738
KG
5670 user host 'tramp-login-args))
5671 (real-host host))
8e3a1104
KG
5672 ;; The following should be changed. We need a more general
5673 ;; mechanism to parse extra host args.
5674 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5ec2cc41 5675 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
07dfe738 5676 (setq real-host (match-string 1 host)))
fb7933a3 5677 (setenv "TERM" tramp-terminal-type)
a69c01a0 5678 (setenv "PS1" "$ ")
fb7933a3 5679 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5680 ;; If we omit the conditional, we would use
5681 ;; `undecided-dos' in some cases. With the conditional,
5682 ;; we use nil in these cases. Which one is right?
fb7933a3
KG
5683 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5684 (> emacs-major-version 20))
5685 tramp-dos-coding-system))
16674e4f 5686 (p (if (and user (not (string= user "")))
bf247b6e 5687 (apply #'start-process bufnam buf login-program
07dfe738 5688 real-host "-l" user login-args)
bf247b6e 5689 (apply #'start-process bufnam buf login-program
07dfe738 5690 real-host login-args)))
fb7933a3 5691 (found nil))
19a87064 5692 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5693
5694 (set-buffer buf)
5695 (tramp-process-actions p multi-method method user host
5696 tramp-actions-before-shell)
fb7933a3
KG
5697 (tramp-message 7 "Initializing remote shell")
5698 (tramp-open-connection-setup-interactive-shell
5699 p multi-method method user host)
5700 (tramp-post-connection multi-method method user host)))))
5701
fb7933a3
KG
5702(defun tramp-open-connection-su (multi-method method user host)
5703 "Open a connection using the `su' program with METHOD.
5704This starts `su - USER', then waits for a password prompt. The HOST
5705name must be equal to the local host name or to `localhost'.
5706
5707If USER is nil, uses value returned by user-login-name instead.
5708
821e6e36
KG
5709Recognition of the remote shell prompt is based on the variables
5710`shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5711set up correctly. Note that the other user may have a different shell
5712prompt than you do, so it is not at all unlikely that the variable
5713`shell-prompt-pattern' is set up wrongly!"
fb7933a3 5714 (save-match-data
16674e4f 5715 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5716 (error "Cannot use out-of-band method `%s' with `su' connection method"
5717 method))
5718 (unless (or (string-match (concat "^" (regexp-quote host))
5719 (system-name))
16674e4f
KG
5720 (string= "localhost" host)
5721 (string= "" host))
fb7933a3
KG
5722 (error
5723 "Cannot connect to different host `%s' with `su' connection method"
5724 host))
7177e2a3 5725 (tramp-pre-connection multi-method method user host tramp-chunksize)
16674e4f
KG
5726 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5727 (or user "<root>") method)
fb7933a3
KG
5728 (let ((process-environment (copy-sequence process-environment)))
5729 (setenv "TERM" tramp-terminal-type)
a69c01a0 5730 (setenv "PS1" "$ ")
fb7933a3 5731 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5732 ;; If we omit the conditional, we use `undecided-dos' in
5733 ;; some cases. With the conditional, we use nil in these
5734 ;; cases. What's the difference? Which one is right?
fb7933a3
KG
5735 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5736 (> emacs-major-version 20))
5737 tramp-dos-coding-system))
5738 (p (apply 'start-process
ac474af1
KG
5739 (tramp-buffer-name multi-method method user host)
5740 (tramp-get-buffer multi-method method user host)
c951aecb 5741 (tramp-get-method-parameter
16674e4f 5742 multi-method
91879624 5743 (tramp-find-method multi-method method user host)
c951aecb 5744 user host 'tramp-login-program)
fb7933a3 5745 (mapcar
4007ba5b
KG
5746 (lambda (x)
5747 (format-spec x `((?u . ,(or user "root")))))
c951aecb 5748 (tramp-get-method-parameter
16674e4f 5749 multi-method
91879624 5750 (tramp-find-method multi-method method user host)
c951aecb 5751 user host 'tramp-login-args))))
fb7933a3
KG
5752 (found nil)
5753 (pw nil))
19a87064 5754 (tramp-set-process-query-on-exit-flag p nil)
ac474af1
KG
5755 (set-buffer (tramp-get-buffer multi-method method user host))
5756 (tramp-process-actions p multi-method method user host
5757 tramp-actions-before-shell)
fb7933a3
KG
5758 (tramp-open-connection-setup-interactive-shell
5759 p multi-method method user host)
bf247b6e 5760 (tramp-post-connection multi-method method
fb7933a3
KG
5761 user host)))))
5762
bf247b6e 5763;; HHH: Not Changed. Multi method. It is not clear to me how this can
fb7933a3
KG
5764;; handle not giving a user name in the "file name".
5765;;
5766;; This is more difficult than for the single-hop method. In the
5767;; multi-hop-method, the desired behaviour should be that the
5768;; user must specify names for the telnet hops of which the user
5769;; name is different than the "original" name (or different from
5770;; the previous hop.
5771(defun tramp-open-connection-multi (multi-method method user host)
5772 "Open a multi-hop connection using METHOD.
5773This uses a slightly changed file name syntax. The idea is to say
5774 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5775This will use telnet to log in as u1 to h1, then use rsh from there to
5776log in as u2 to h2."
5777 (save-match-data
5778 (unless multi-method
5779 (error "Multi-hop open connection function called on non-multi method"))
16674e4f 5780 (when (tramp-method-out-of-band-p multi-method method user host)
fb7933a3
KG
5781 (error "No out of band multi-hop connections"))
5782 (unless (and (arrayp method) (not (stringp method)))
5783 (error "METHOD must be an array of strings for multi methods"))
5784 (unless (and (arrayp user) (not (stringp user)))
5785 (error "USER must be an array of strings for multi methods"))
5786 (unless (and (arrayp host) (not (stringp host)))
5787 (error "HOST must be an array of strings for multi methods"))
5788 (unless (and (= (length method) (length user))
5789 (= (length method) (length host)))
5790 (error "Arrays METHOD, USER, HOST must have equal length"))
7177e2a3 5791 (tramp-pre-connection multi-method method user host tramp-chunksize)
fb7933a3
KG
5792 (tramp-message 7 "Opening `%s' connection..." multi-method)
5793 (let ((process-environment (copy-sequence process-environment)))
5794 (setenv "TERM" tramp-terminal-type)
a69c01a0 5795 (setenv "PS1" "$ ")
fb7933a3 5796 (let* ((default-directory (tramp-temporary-file-directory))
16674e4f
KG
5797 ;; If we omit the conditional, we use `undecided-dos' in
5798 ;; some cases. With the conditional, we use nil in these
5799 ;; cases. What's the difference? Which one is right?
fb7933a3
KG
5800 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5801 (> emacs-major-version 20))
5802 tramp-dos-coding-system))
5803 (p (start-process (tramp-buffer-name multi-method method user host)
5804 (tramp-get-buffer multi-method method user host)
90dc758d 5805 tramp-multi-sh-program))
fb7933a3
KG
5806 (num-hops (length method))
5807 (i 0))
19a87064 5808 (tramp-set-process-query-on-exit-flag p nil)
fb7933a3
KG
5809 (tramp-message 9 "Waiting 60s for local shell to come up...")
5810 (unless (tramp-wait-for-regexp
821e6e36
KG
5811 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5812 shell-prompt-pattern tramp-shell-prompt-pattern))
fb7933a3
KG
5813 (pop-to-buffer (buffer-name))
5814 (kill-process p)
5815 (error "Couldn't find local shell prompt"))
5816 ;; Now do all the connections as specified.
5817 (while (< i num-hops)
5818 (let* ((m (aref method i))
5819 (u (aref user i))
5820 (h (aref host i))
5821 (entry (assoc m tramp-multi-connection-function-alist))
5822 (multi-func (nth 1 entry))
5823 (command (nth 2 entry)))
dba28077 5824 ;; The multi-funcs don't need to do save-match-data, as that
fb7933a3
KG
5825 ;; is done here.
5826 (funcall multi-func p m u h command)
5827 (erase-buffer)
5828 (incf i)))
5829 (tramp-open-connection-setup-interactive-shell
5830 p multi-method method user host)
5831 (tramp-post-connection multi-method method user host)))))
5832
5833;; HHH: Changed. Multi method. Don't know how to handle this in the case
bf247b6e 5834;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5835;; changed `user' to `(or user (user-login-name))' in the places where
5836;; the value is actually used.
5837(defun tramp-multi-connect-telnet (p method user host command)
5838 "Issue `telnet' command.
5839Uses shell COMMAND to issue a `telnet' command to log in as USER to
5840HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5841the host name, and `%n' is replaced with an end of line character, as
5842set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5843character.
5844
5845If USER is nil, uses the return value of (user-login-name) instead."
ac474af1
KG
5846 (let ((cmd (format-spec command
5847 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5848 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
fb7933a3
KG
5849 found pw)
5850 (erase-buffer)
5851 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5852 (process-send-string p cmd)
ac474af1 5853 (tramp-process-multi-actions p method user host
dba28077 5854 tramp-multi-actions)))
fb7933a3 5855
bf247b6e
KS
5856;; HHH: Changed. Multi method. Don't know how to handle this in the case
5857;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5858;; changed `user' to `(or user (user-login-name))' in the places where
5859;; the value is actually used.
5860(defun tramp-multi-connect-rlogin (p method user host command)
5861 "Issue `rlogin' command.
5862Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5863HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5864with the user name, `%h' will be replaced with the host name, and `%n'
5865will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5866`%%' if you want to use a literal percent character.
5867
5868If USER is nil, uses the return value of (user-login-name) instead."
ac474af1
KG
5869 (let ((cmd (format-spec command `((?h . ,host)
5870 (?u . ,(or user (user-login-name)))
5871 (?n . ,tramp-rsh-end-of-line))))
5872 (cmd1 (format-spec command `((?h . ,host)
5873 (?u . ,(or user (user-login-name)))
5874 (?n . ""))))
fb7933a3
KG
5875 found)
5876 (erase-buffer)
5877 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5878 (process-send-string p cmd)
ac474af1 5879 (tramp-process-multi-actions p method user host
dba28077 5880 tramp-multi-actions)))
fb7933a3 5881
bf247b6e
KS
5882;; HHH: Changed. Multi method. Don't know how to handle this in the case
5883;; of no user name provided. Hack to make it work as it did before:
fb7933a3
KG
5884;; changed `user' to `(or user (user-login-name))' in the places where
5885;; the value is actually used.
5886(defun tramp-multi-connect-su (p method user host command)
5887 "Issue `su' command.
5888Uses shell COMMAND to issue a `su' command to log in as USER on
5889HOST. The HOST name is ignored, this just changes the user id on the
5890host currently logged in to.
5891
5892If USER is nil, uses the return value of (user-login-name) instead.
5893
5894You can use percent escapes in the COMMAND. `%u' is replaced with the
5895user name, and `%n' is replaced with the value of
5896`tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5897character."
ac474af1
KG
5898 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5899 (?n . ,tramp-rsh-end-of-line))))
5900 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5901 (?n . ""))))
fb7933a3
KG
5902 found)
5903 (erase-buffer)
5904 (tramp-message 9 "Sending su command `%s'" cmd1)
5905 (process-send-string p cmd)
ac474af1 5906 (tramp-process-multi-actions p method user host
dba28077 5907 tramp-multi-actions)))
fb7933a3
KG
5908
5909;; Utility functions.
5910
d2a2c17f
MA
5911(defun tramp-accept-process-output
5912 (&optional process timeout timeout-msecs)
5913 "Like `accept-process-output' for Tramp processes.
5914This is needed in order to hide `last-coding-system-used', which is set
5915for process communication also."
5916 (let (last-coding-system-used)
5917 (accept-process-output process timeout timeout-msecs)))
5918
fb7933a3
KG
5919(defun tramp-wait-for-regexp (proc timeout regexp)
5920 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5921Expects the output of PROC to be sent to the current buffer. Returns
5922the string that matched, or nil. Waits indefinitely if TIMEOUT is
5923nil."
5924 (let ((found nil)
5925 (start-time (current-time)))
5926 (cond (timeout
5927 ;; Work around a bug in XEmacs 21, where the timeout
5928 ;; expires faster than it should. This degenerates
5929 ;; to polling for buggy XEmacsen, but oh, well.
5930 (while (and (not found)
5931 (< (tramp-time-diff (current-time) start-time)
5932 timeout))
5933 (with-timeout (timeout)
5934 (while (not found)
d2a2c17f 5935 (tramp-accept-process-output proc 1)
19a87064
MA
5936 (unless (memq (process-status proc) '(run open))
5937 (error "Process has died"))
fb7933a3 5938 (goto-char (point-min))
d2a2c17f 5939 (setq found (re-search-forward regexp nil t))))))
fb7933a3
KG
5940 (t
5941 (while (not found)
d2a2c17f 5942 (tramp-accept-process-output proc 1)
19a87064
MA
5943 (unless (memq (process-status proc) '(run open))
5944 (error "Process has died"))
fb7933a3 5945 (goto-char (point-min))
d2a2c17f 5946 (setq found (re-search-forward regexp nil t)))))
fb7933a3
KG
5947 (when tramp-debug-buffer
5948 (append-to-buffer
5949 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5950 tramp-current-user tramp-current-host)
5951 (point-min) (point-max))
5952 (when (not found)
5953 (save-excursion
5954 (set-buffer
5955 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5956 tramp-current-user tramp-current-host))
5957 (goto-char (point-max))
5958 (insert "[[Regexp `" regexp "' not found"
487fa986 5959 (if timeout (format " in %d secs" timeout) "")
fb7933a3
KG
5960 "]]"))))
5961 found))
5962
b25a52cc
KG
5963(defun tramp-wait-for-shell-prompt (proc timeout)
5964 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
5965See `tramp-wait-for-regexp' for more details.
5966Shell prompt pattern is determined by variables `shell-prompt-pattern'
5967and `tramp-shell-prompt-pattern'."
5968 (tramp-wait-for-regexp
5969 proc timeout
5970 (format "\\(%s\\|%s\\)\\'"
5971 shell-prompt-pattern tramp-shell-prompt-pattern)))
5972
5973(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
5974 "Wait for shell prompt and barf if none appears.
5975Looks at process PROC to see if a shell prompt appears in TIMEOUT
5976seconds. If not, it produces an error message with the given ERROR-ARGS."
5977 (unless (tramp-wait-for-shell-prompt proc timeout)
5978 (pop-to-buffer (buffer-name))
5979 (apply 'error error-args)))
5980
07dfe738 5981(defun tramp-enter-password (p prompt user host)
fb7933a3
KG
5982 "Prompt for a password and send it to the remote end.
5983Uses PROMPT as a prompt and sends the password to process P."
07dfe738 5984 (let ((pw (tramp-read-passwd user host prompt)))
ac474af1 5985 (erase-buffer)
90f8dc03
KG
5986 (process-send-string
5987 p (concat pw
c951aecb
KG
5988 (or (tramp-get-method-parameter
5989 tramp-current-multi-method
5990 tramp-current-method
5991 tramp-current-user
5992 tramp-current-host
5993 'tramp-password-end-of-line)
5994 tramp-default-password-end-of-line)))))
fb7933a3
KG
5995
5996;; HHH: Not Changed. This might handle the case where USER is not
5997;; given in the "File name" very poorly. Then, the local
19a87064 5998;; variable tramp-current-user will be set to nil.
7177e2a3 5999(defun tramp-pre-connection (multi-method method user host chunksize)
fb7933a3
KG
6000 "Do some setup before actually logging in.
6001METHOD, USER and HOST specify the connection."
6002 (set-buffer (tramp-get-buffer multi-method method user host))
6003 (set (make-local-variable 'tramp-current-multi-method) multi-method)
6004 (set (make-local-variable 'tramp-current-method) method)
6005 (set (make-local-variable 'tramp-current-user) user)
6006 (set (make-local-variable 'tramp-current-host) host)
7177e2a3 6007 (set (make-local-variable 'tramp-chunksize) chunksize)
fb7933a3
KG
6008 (set (make-local-variable 'inhibit-eol-conversion) nil)
6009 (erase-buffer))
6010
6011(defun tramp-open-connection-setup-interactive-shell
6012 (p multi-method method user host)
6013 "Set up an interactive shell.
6014Mainly sets the prompt and the echo correctly. P is the shell process
6015to set up. METHOD, USER and HOST specify the connection."
6016 ;; Wait a bit in case the remote end feels like sending a little
6017 ;; junk first. It seems that fencepost.gnu.org does this when doing
6018 ;; a Kerberos login.
6019 (sit-for 1)
6020 (tramp-discard-garbage-erase-buffer p multi-method method user host)
90f8dc03
KG
6021 (tramp-process-initial-commands p multi-method method user host
6022 tramp-initial-commands)
16674e4f
KG
6023 ;; It is useful to set the prompt in the following command because
6024 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6025 ;; about and thus /bin/sh will display a strange prompt. For
6026 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6027 ;; the current working directory but /bin/sh will display a dollar
6028 ;; sign. The following command line sets $PS1 to a sane value, and
6029 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6030 ;; Pittman reports that the unusual positioning of the single quotes
7432277c
KG
6031 ;; makes it work under `rc', too. We also unset the variable $ENV
6032 ;; because that is read by some sh implementations (eg, bash when
6033 ;; called as sh) on startup; this way, we avoid the startup file
6034 ;; clobbering $PS1.
b25a52cc
KG
6035 (tramp-send-command-internal
6036 multi-method method user host
6037 (format "exec env 'ENV=' 'PS1=$ ' %s"
c951aecb
KG
6038 (tramp-get-method-parameter
6039 multi-method method user host 'tramp-remote-sh))
b25a52cc 6040 (format "remote `%s' to come up"
c951aecb
KG
6041 (tramp-get-method-parameter
6042 multi-method method user host 'tramp-remote-sh)))
b25a52cc
KG
6043 (tramp-barf-if-no-shell-prompt
6044 p 30
6045 "Remote `%s' didn't come up. See buffer `%s' for details"
c951aecb 6046 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
b25a52cc
KG
6047 (buffer-name))
6048 (tramp-message 8 "Setting up remote shell environment")
fb7933a3 6049 (tramp-discard-garbage-erase-buffer p multi-method method user host)
b25a52cc
KG
6050 (tramp-send-command-internal multi-method method user host
6051 "stty -inlcr -echo kill '^U'")
fb7933a3 6052 (erase-buffer)
38c65fca
KG
6053 ;; Ignore garbage after stty command.
6054 (tramp-send-command-internal multi-method method user host
6055 "echo foo")
6056 (erase-buffer)
b25a52cc
KG
6057 (tramp-send-command-internal multi-method method user host
6058 "TERM=dumb; export TERM")
7177e2a3
MA
6059 (erase-buffer)
6060 ;; Check whether the remote host suffers from buggy `send-process-string'.
6061 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6062 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6063 ;; Emacs makes a hack when this host type is detected locally. It cannot
6064 ;; handle remote hosts, though.
6065 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6066 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6067 (tramp-send-command-internal multi-method method user host
6068 "(uname -sr) 2>/dev/null")
6069 (goto-char (point-min))
6070 (when (looking-at "FreeBSD")
6071 (setq tramp-chunksize 500)))
6072
fb7933a3
KG
6073 ;; Try to set up the coding system correctly.
6074 ;; CCC this can't be the right way to do it. Hm.
6075 (save-excursion
6076 (erase-buffer)
6077 (tramp-message 9 "Determining coding system")
b25a52cc
KG
6078 (tramp-send-command-internal multi-method method user host
6079 "echo foo ; echo bar")
fb7933a3
KG
6080 (goto-char (point-min))
6081 (if (featurep 'mule)
6082 ;; Use MULE to select the right EOL convention for communicating
6083 ;; with the process.
6084 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6085 cs-decode cs-encode)
6086 (when (symbolp cs) (setq cs (cons cs cs)))
6087 (setq cs-decode (car cs))
6088 (setq cs-encode (cdr cs))
6089 (unless cs-decode (setq cs-decode 'undecided))
6090 (unless cs-encode (setq cs-encode 'undecided))
6091 (setq cs-encode (tramp-coding-system-change-eol-conversion
6092 cs-encode 'unix))
6093 (when (search-forward "\r" nil t)
6094 (setq cs-decode (tramp-coding-system-change-eol-conversion
6095 cs-decode 'dos)))
6096 (set-buffer-process-coding-system cs-decode cs-encode))
6097 ;; Look for ^M and do something useful if found.
6098 (when (search-forward "\r" nil t)
6099 ;; We have found a ^M but cannot frob the process coding system
6100 ;; because we're running on a non-MULE Emacs. Let's try
6101 ;; stty, instead.
90f8dc03 6102 (erase-buffer)
fb7933a3 6103 (tramp-message 9 "Trying `stty -onlcr'")
b25a52cc
KG
6104 (tramp-send-command-internal multi-method method user host
6105 "stty -onlcr"))))
fb7933a3
KG
6106 (erase-buffer)
6107 (tramp-message
19a87064
MA
6108 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6109 (tramp-send-command-internal
6110 multi-method method user host
6111 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
fb7933a3
KG
6112 (erase-buffer)
6113 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
b25a52cc
KG
6114 (tramp-send-command-internal multi-method method user host
6115 "set +o vi +o emacs")
fb7933a3
KG
6116 (erase-buffer)
6117 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
b25a52cc
KG
6118 (tramp-send-command-internal
6119 multi-method method user host
6120 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
fb7933a3
KG
6121 (erase-buffer)
6122 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
b25a52cc
KG
6123 (tramp-send-command-internal multi-method method user host
6124 "unset CDPATH")
fb7933a3
KG
6125 (erase-buffer)
6126 (tramp-message 9 "Setting shell prompt")
16674e4f 6127 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
3b89d388
KG
6128 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6129 ;; the last time we sent a command, to avoid tramp-send-command to send
6130 ;; "echo are you awake".
6131 (setq tramp-last-cmd-time (current-time))
fb7933a3
KG
6132 (tramp-send-command
6133 multi-method method user host
6134 (format "PS1='%s%s%s'; PS2=''; PS3=''"
16674e4f 6135 tramp-rsh-end-of-line
fb7933a3 6136 tramp-end-of-output
16674e4f
KG
6137 tramp-rsh-end-of-line))
6138 (tramp-wait-for-output))
fb7933a3
KG
6139
6140(defun tramp-post-connection (multi-method method user host)
6141 "Prepare a remote shell before being able to work on it.
6142METHOD, USER and HOST specify the connection.
6143Among other things, this finds a shell which groks tilde expansion,
6144tries to find an `ls' command which groks the `-n' option, sets the
6145locale to C and sets up the remote shell search path."
6146 ;; Search for a good shell before searching for a command which
6147 ;; checks if a file exists. This is done because Tramp wants to use
6148 ;; "test foo; echo $?" to check if various conditions hold, and
6149 ;; there are buggy /bin/sh implementations which don't execute the
6150 ;; "echo $?" part if the "test" part has an error. In particular,
6151 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6152 ;; with buggy /bin/sh implementations will have a working bash or
6153 ;; ksh. Whee...
6154 (tramp-find-shell multi-method method user host)
fb7933a3
KG
6155 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6156 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6157 ;; output of (tramp-find-shell) to show up along with the output of
6158 ;; (tramp-find-ls-command) testing.
6159 ;;
6160 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6161 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6162 ;;
6163 ;; After much debugging I couldn't find any problem with the implementation
6164 ;; of that function though. The workaround stays for me at least. :/
6165 ;;
6166 ;; Daniel Pittman <daniel@danann.net>
fabf2143 6167 (sleep-for 1)
5beaf831 6168 (erase-buffer)
fabf2143 6169 (tramp-find-file-exists-command multi-method method user host)
fb7933a3
KG
6170 (make-local-variable 'tramp-ls-command)
6171 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6172 (unless tramp-ls-command
6173 (tramp-message
6174 1
6175 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6176 (setq tramp-ls-command
6177 (tramp-find-executable multi-method method user host
6178 "ls" tramp-remote-path nil)))
6179 (unless tramp-ls-command
6180 (error "Fatal error: Couldn't find remote executable `ls'"))
6181 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6182 tramp-ls-command)
6183 (tramp-send-command multi-method method user host
6184 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6185 "return $1" tramp-rsh-end-of-line
6186 "}"))
6187 (tramp-wait-for-output)
6188 ;; Set remote PATH variable.
6189 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6190 ;; Tell remote shell to use standard time format, needed for
6191 ;; parsing `ls -l' output.
6192 (tramp-send-command multi-method method user host
6193 "LC_TIME=C; export LC_TIME; echo huhu")
6194 (tramp-wait-for-output)
6195 (tramp-send-command multi-method method user host
6196 "mesg n; echo huhu")
6197 (tramp-wait-for-output)
6198 (tramp-send-command multi-method method user host
6199 "biff n ; echo huhu")
6200 (tramp-wait-for-output)
6201 ;; Unalias ls(1) to work around issues with those silly people who make it
6202 ;; spit out ANSI escapes or whatever.
6203 (tramp-send-command multi-method method user host
6204 "unalias ls; echo huhu")
6205 (tramp-wait-for-output)
6206 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6207 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6208 ;; for otherwise the shell crashes.
6209 (erase-buffer)
6210 (make-local-variable 'tramp-test-groks-nt)
6211 (tramp-send-command multi-method method user host
6212 "( test / -nt / )")
6213 (tramp-wait-for-output)
6214 (goto-char (point-min))
6215 (setq tramp-test-groks-nt
16674e4f 6216 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
fb7933a3
KG
6217 (unless tramp-test-groks-nt
6218 (tramp-send-command
6219 multi-method method user host
6220 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6221 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6222 "}")))
6223 (tramp-wait-for-output)
fabf2143
KG
6224 ;; Send the fallback `uudecode' script.
6225 (erase-buffer)
7432277c 6226 (tramp-send-string multi-method method user host tramp-uudecode)
fabf2143 6227 (tramp-wait-for-output)
fb7933a3
KG
6228 ;; Find a `perl'.
6229 (erase-buffer)
c82c5727 6230 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
fb7933a3
KG
6231 (let ((tramp-remote-perl
6232 (or (tramp-find-executable multi-method method user host
fabf2143 6233 "perl5" tramp-remote-path nil)
fb7933a3 6234 (tramp-find-executable multi-method method user host
fabf2143 6235 "perl" tramp-remote-path nil))))
fb7933a3 6236 (when tramp-remote-perl
fabf2143
KG
6237 (tramp-set-connection-property "perl" tramp-remote-perl
6238 multi-method method user host)
c82c5727
LH
6239 (unless (tramp-method-out-of-band-p multi-method method user host)
6240 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6241 (tramp-send-string
6242 multi-method method user host
6243 (concat "tramp_encode () {\n"
6244 (format tramp-perl-encode tramp-remote-perl)
6245 " 2>/dev/null"
6246 "\n}"))
6247 (tramp-wait-for-output)
6248 (tramp-send-string
6249 multi-method method user host
6250 (concat "tramp_encode_with_module () {\n"
6251 (format tramp-perl-encode-with-module tramp-remote-perl)
6252 " 2>/dev/null"
6253 "\n}"))
6254 (tramp-wait-for-output)
6255 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6256 (tramp-send-string
6257 multi-method method user host
6258 (concat "tramp_decode () {\n"
6259 (format tramp-perl-decode tramp-remote-perl)
6260 " 2>/dev/null"
6261 "\n}"))
6262 (tramp-wait-for-output)
6263 (tramp-send-string
6264 multi-method method user host
6265 (concat "tramp_decode_with_module () {\n"
6266 (format tramp-perl-decode-with-module tramp-remote-perl)
6267 " 2>/dev/null"
6268 "\n}"))
6269 (tramp-wait-for-output))))
fb7933a3
KG
6270 ;; Find ln(1)
6271 (erase-buffer)
6272 (let ((ln (tramp-find-executable multi-method method user host
6273 "ln" tramp-remote-path nil)))
6274 (when ln
6275 (tramp-set-connection-property "ln" ln multi-method method user host)))
a69c01a0 6276 ;; Set uid and gid.
fb7933a3 6277 (erase-buffer)
a69c01a0
MA
6278 (tramp-send-command multi-method method user host "id -u; id -g")
6279 (tramp-wait-for-output)
6280 (goto-char (point-min))
6281 (tramp-set-connection-property
6282 "uid" (read (current-buffer)) multi-method method user host)
6283 (tramp-set-connection-property
6284 "gid" (read (current-buffer)) multi-method method user host)
ac474af1 6285 ;; Find the right encoding/decoding commands to use.
a69c01a0 6286 (erase-buffer)
5ec2cc41 6287 (unless (tramp-method-out-of-band-p multi-method method user host)
ac474af1 6288 (tramp-find-inline-encoding multi-method method user host))
fb7933a3
KG
6289 ;; If encoding/decoding command are given, test to see if they work.
6290 ;; CCC: Maybe it would be useful to run the encoder both locally and
6291 ;; remotely to see if they produce the same result.
16674e4f
KG
6292 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6293 (rem-dec (tramp-get-remote-decoding multi-method method user host))
fb7933a3 6294 (magic-string "xyzzy"))
16674e4f 6295 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
fb7933a3 6296 (tramp-kill-process multi-method method user host)
16674e4f 6297 ;; Improve error message and/or error check.
fb7933a3
KG
6298 (error
6299 "Must give both decoding and encoding command in method definition"))
16674e4f 6300 (when (and rem-enc rem-dec)
fb7933a3
KG
6301 (tramp-message
6302 5
6303 "Checking to see if encoding/decoding commands work on remote host...")
6304 (tramp-send-command
6305 multi-method method user host
6306 (format "echo %s | %s | %s"
16674e4f 6307 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
fb7933a3
KG
6308 (tramp-wait-for-output)
6309 (unless (looking-at (regexp-quote magic-string))
6310 (tramp-kill-process multi-method method user host)
6311 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6312 (buffer-name)))
6313 (erase-buffer)
6314 (tramp-message
6315 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6316
ac474af1
KG
6317;; CCC: We should either implement a Perl version of base64 encoding
6318;; and decoding. Then we just use that in the last item. The other
6319;; alternative is to use the Perl version of UU encoding. But then
6320;; we need a Lisp version of uuencode.
16674e4f
KG
6321;;
6322;; Old text from documentation of tramp-methods:
6323;; Using a uuencode/uudecode inline method is discouraged, please use one
6324;; of the base64 methods instead since base64 encoding is much more
6325;; reliable and the commands are more standardized between the different
6326;; Unix versions. But if you can't use base64 for some reason, please
6327;; note that the default uudecode command does not work well for some
6328;; Unices, in particular AIX and Irix. For AIX, you might want to use
6329;; the following command for uudecode:
6330;;
6331;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6332;;
6333;; For Irix, no solution is known yet.
6334
ac474af1
KG
6335(defvar tramp-coding-commands
6336 '(("mimencode -b" "mimencode -u -b"
6337 base64-encode-region base64-decode-region)
6338 ("mmencode -b" "mmencode -u -b"
6339 base64-encode-region base64-decode-region)
6340 ("recode data..base64" "recode base64..data"
6341 base64-encode-region base64-decode-region)
e7b52b6d
KG
6342 ("uuencode xxx" "uudecode -o /dev/stdout"
6343 tramp-uuencode-region uudecode-decode-region)
ac474af1 6344 ("uuencode xxx" "uudecode -o -"
16674e4f 6345 tramp-uuencode-region uudecode-decode-region)
ac474af1 6346 ("uuencode xxx" "uudecode -p"
16674e4f 6347 tramp-uuencode-region uudecode-decode-region)
fabf2143 6348 ("uuencode xxx" "tramp_uudecode"
16674e4f 6349 tramp-uuencode-region uudecode-decode-region)
b1d06e75
KG
6350 ("tramp_encode_with_module" "tramp_decode_with_module"
6351 base64-encode-region base64-decode-region)
ac474af1
KG
6352 ("tramp_encode" "tramp_decode"
6353 base64-encode-region base64-decode-region))
6354 "List of coding commands for inline transfer.
16674e4f
KG
6355Each item is a list that looks like this:
6356
6357\(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
ac474af1 6358
16674e4f
KG
6359The REMOTE-ENCODING should be a string, giving a command accepting a
6360plain file on standard input and writing the encoded file to standard
6361output. The REMOTE-DECODING should also be a string, giving a command
6362accepting an encoded file on standard input and writing the decoded
6363file to standard output.
ac474af1 6364
16674e4f
KG
6365LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6366symbols, giving functions. If they are strings, then they can contain
6367the \"%s\" format specifier. If that specifier is present, the input
6368filename will be put into the command line at that spot. If the
6369specifier is not present, the input should be read from standard
6370input.
ac474af1 6371
16674e4f
KG
6372If they are functions, they will be called with two arguments, start
6373and end of region, and are expected to replace the region contents
6374with the encoded or decoded results, respectively.")
ac474af1
KG
6375
6376(defun tramp-find-inline-encoding (multi-method method user host)
6377 "Find an inline transfer encoding that works.
6378Goes through the list `tramp-coding-commands'."
6379 (let ((commands tramp-coding-commands)
3b89d388 6380 (magic "xyzzy")
ac474af1
KG
6381 item found)
6382 (while (and commands (null found))
6383 (setq item (pop commands))
6384 (catch 'wont-work
16674e4f
KG
6385 (let ((rem-enc (nth 0 item))
6386 (rem-dec (nth 1 item))
6387 (loc-enc (nth 2 item))
6388 (loc-dec (nth 3 item)))
6389 ;; Check if remote encoding and decoding commands can be
6390 ;; called remotely with null input and output. This makes
6391 ;; sure there are no syntax errors and the command is really
3b89d388
KG
6392 ;; found. Note that we do not redirect stdout to /dev/null,
6393 ;; for two reaons: when checking the decoding command, we
6394 ;; actually check the output it gives. And also, when
6395 ;; redirecting "mimencode" output to /dev/null, then as root
6396 ;; it might change the permissions of /dev/null!
ac474af1 6397 (tramp-message-for-buffer
821e6e36 6398 multi-method method user host 9
16674e4f 6399 "Checking remote encoding command `%s' for sanity" rem-enc)
ac474af1
KG
6400 (unless (zerop (tramp-send-command-and-check
6401 multi-method method user host
3b89d388 6402 (format "%s </dev/null" rem-enc) t))
ac474af1
KG
6403 (throw 'wont-work nil))
6404 (tramp-message-for-buffer
821e6e36 6405 multi-method method user host 9
16674e4f 6406 "Checking remote decoding command `%s' for sanity" rem-dec)
ac474af1
KG
6407 (unless (zerop (tramp-send-command-and-check
6408 multi-method method user host
3b89d388
KG
6409 (format "echo %s | %s | %s"
6410 magic rem-enc rem-dec) t))
ac474af1 6411 (throw 'wont-work nil))
3b89d388
KG
6412 (save-excursion
6413 (goto-char (point-min))
6414 (unless (looking-at (regexp-quote magic))
6415 (throw 'wont-work nil)))
16674e4f
KG
6416 ;; If the local encoder or decoder is a string, the
6417 ;; corresponding command has to work locally.
6418 (when (stringp loc-enc)
ac474af1 6419 (tramp-message-for-buffer
821e6e36 6420 multi-method method user host 9
16674e4f
KG
6421 "Checking local encoding command `%s' for sanity" loc-enc)
6422 (unless (zerop (tramp-call-local-coding-command
6423 loc-enc nil nil))
ac474af1 6424 (throw 'wont-work nil)))
16674e4f 6425 (when (stringp loc-dec)
ac474af1 6426 (tramp-message-for-buffer
821e6e36 6427 multi-method method user host 9
16674e4f
KG
6428 "Checking local decoding command `%s' for sanity" loc-dec)
6429 (unless (zerop (tramp-call-local-coding-command
6430 loc-dec nil nil))
ac474af1
KG
6431 (throw 'wont-work nil)))
6432 ;; CCC: At this point, maybe we should check that the output
6433 ;; of the commands is correct. But for the moment we will
6434 ;; assume that commands working on empty input will also
6435 ;; work in practice.
6436 (setq found item))))
6437 ;; Did we find something? If not, issue error. If so,
6438 ;; set connection properties.
6439 (unless found
6440 (error "Couldn't find an inline transfer encoding"))
16674e4f
KG
6441 (let ((rem-enc (nth 0 found))
6442 (rem-dec (nth 1 found))
6443 (loc-enc (nth 2 found))
6444 (loc-dec (nth 3 found)))
6445 (tramp-message 10 "Using remote encoding %s" rem-enc)
6446 (tramp-set-remote-encoding multi-method method user host rem-enc)
6447 (tramp-message 10 "Using remote decoding %s" rem-dec)
6448 (tramp-set-remote-decoding multi-method method user host rem-dec)
6449 (tramp-message 10 "Using local encoding %s" loc-enc)
6450 (tramp-set-local-encoding multi-method method user host loc-enc)
6451 (tramp-message 10 "Using local decoding %s" loc-dec)
6452 (tramp-set-local-decoding multi-method method user host loc-dec))))
6453
6454(defun tramp-call-local-coding-command (cmd input output)
6455 "Call the local encoding or decoding command.
6456If CMD contains \"%s\", provide input file INPUT there in command.
6457Otherwise, INPUT is passed via standard input.
6458INPUT can also be nil which means `/dev/null'.
6459OUTPUT can be a string (which specifies a filename), or t (which
6460means standard output and thus the current buffer), or nil (which
6461means discard it)."
6462 (call-process
6463 tramp-encoding-shell ;program
6464 (when (and input (not (string-match "%s" cmd)))
6465 input) ;input
6466 (if (eq output t) t nil) ;output
6467 nil ;redisplay
6468 tramp-encoding-command-switch
6469 ;; actual shell command
6470 (concat
6471 (if (string-match "%s" cmd) (format cmd input) cmd)
6472 (if (stringp output) (concat "> " output) ""))))
fb7933a3
KG
6473
6474(defun tramp-maybe-open-connection (multi-method method user host)
6475 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6476Does not do anything if a connection is already open, but re-opens the
6477connection if a previous connection has died for some reason."
16674e4f
KG
6478 (let ((p (get-buffer-process
6479 (tramp-get-buffer multi-method method user host)))
ac474af1
KG
6480 last-cmd-time)
6481 ;; If too much time has passed since last command was sent, look
6482 ;; whether process is still alive. If it isn't, kill it. When
6483 ;; using ssh, it can sometimes happen that the remote end has hung
6484 ;; up but the local ssh client doesn't recognize this until it
6485 ;; tries to send some data to the remote end. So that's why we
6486 ;; try to send a command from time to time, then look again
6487 ;; whether the process is really alive.
6488 (save-excursion
6489 (set-buffer (tramp-get-buffer multi-method method user host))
6490 (when (and tramp-last-cmd-time
3b89d388
KG
6491 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6492 p (processp p) (memq (process-status p) '(run open)))
685f5858
KG
6493 (tramp-send-command
6494 multi-method method user host "echo are you awake" nil t)
df2a9b12
SS
6495 (unless (and (memq (process-status p) '(run open))
6496 (tramp-wait-for-output 10))
ac474af1
KG
6497 (delete-process p)
6498 (setq p nil))
6499 (erase-buffer)))
6500 (unless (and p (processp p) (memq (process-status p) '(run open)))
fb7933a3
KG
6501 (when (and p (processp p))
6502 (delete-process p))
5ec2cc41
KG
6503 (let ((process-connection-type tramp-process-connection-type))
6504 (funcall (tramp-get-method-parameter
6505 multi-method
6506 (tramp-find-method multi-method method user host)
6507 user host 'tramp-connection-function)
6508 multi-method method user host)))))
fb7933a3
KG
6509
6510(defun tramp-send-command
685f5858 6511 (multi-method method user host command &optional noerase neveropen)
fb7933a3
KG
6512 "Send the COMMAND to USER at HOST (logged in using METHOD).
6513Erases temporary buffer before sending the command (unless NOERASE
685f5858
KG
6514is true).
6515If optional seventh arg NEVEROPEN is non-nil, never try to open the
6516connection. This is meant to be used from
6517`tramp-maybe-open-connection' only."
6518 (or neveropen
6519 (tramp-maybe-open-connection multi-method method user host))
ac474af1 6520 (setq tramp-last-cmd-time (current-time))
38c65fca 6521 (setq tramp-last-cmd command)
fb7933a3
KG
6522 (when tramp-debug-buffer
6523 (save-excursion
6524 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6525 (goto-char (point-max))
6526 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6527 (let ((proc nil))
6528 (set-buffer (tramp-get-buffer multi-method method user host))
6529 (unless noerase (erase-buffer))
6530 (setq proc (get-buffer-process (current-buffer)))
6531 (process-send-string proc
6532 (concat command tramp-rsh-end-of-line))))
6533
b25a52cc
KG
6534(defun tramp-send-command-internal
6535 (multi-method method user host command &optional msg)
6536 "Send command to remote host and wait for success.
6537Sends COMMAND, then waits 30 seconds for shell prompt."
6538 (tramp-send-command multi-method method user host command t t)
6539 (when msg
6540 (tramp-message 9 "Waiting 30s for %s..." msg))
6541 (tramp-barf-if-no-shell-prompt
6542 nil 30
6543 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
bf247b6e 6544
fb7933a3
KG
6545(defun tramp-wait-for-output (&optional timeout)
6546 "Wait for output from remote rsh command."
6547 (let ((proc (get-buffer-process (current-buffer)))
6548 (found nil)
6549 (start-time (current-time))
38c65fca 6550 (start-point (point))
fb7933a3
KG
6551 (end-of-output (concat "^"
6552 (regexp-quote tramp-end-of-output)
16674e4f 6553 "\r?$")))
fb7933a3
KG
6554 ;; Algorithm: get waiting output. See if last line contains
6555 ;; end-of-output sentinel. If not, wait a bit and again get
6556 ;; waiting output. Repeat until timeout expires or end-of-output
6557 ;; sentinel is seen. Will hang if timeout is nil and
6558 ;; end-of-output sentinel never appears.
6559 (save-match-data
6560 (cond (timeout
6561 ;; Work around an XEmacs bug, where the timeout expires
6562 ;; faster than it should. This degenerates into polling
6563 ;; for buggy XEmacsen, but oh, well.
6564 (while (and (not found)
6565 (< (tramp-time-diff (current-time) start-time)
6566 timeout))
6567 (with-timeout (timeout)
6568 (while (not found)
d2a2c17f 6569 (tramp-accept-process-output proc 1)
19a87064
MA
6570 (unless (memq (process-status proc) '(run open))
6571 (error "Process has died"))
fb7933a3
KG
6572 (goto-char (point-max))
6573 (forward-line -1)
6574 (setq found (looking-at end-of-output))))))
6575 (t
6576 (while (not found)
d2a2c17f 6577 (tramp-accept-process-output proc 1)
19a87064
MA
6578 (unless (memq (process-status proc) '(run open))
6579 (error "Process has died"))
fb7933a3
KG
6580 (goto-char (point-max))
6581 (forward-line -1)
6582 (setq found (looking-at end-of-output))))))
6583 ;; At this point, either the timeout has expired or we have found
6584 ;; the end-of-output sentinel.
6585 (when found
6586 (goto-char (point-max))
6587 (forward-line -2)
6588 (delete-region (point) (point-max)))
38c65fca
KG
6589 ;; If processing echoes, look for it in the first line and delete.
6590 (when tramp-process-echoes
6591 (save-excursion
6592 (goto-char start-point)
6593 (when (looking-at (regexp-quote tramp-last-cmd))
01917a18 6594 (delete-region (point) (progn (forward-line 1) (point))))))
fb7933a3
KG
6595 ;; Add output to debug buffer if appropriate.
6596 (when tramp-debug-buffer
6597 (append-to-buffer
6598 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6599 tramp-current-user tramp-current-host)
6600 (point-min) (point-max))
6601 (when (not found)
6602 (save-excursion
6603 (set-buffer
6604 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6605 tramp-current-user tramp-current-host))
6606 (goto-char (point-max))
6607 (insert "[[Remote prompt `" end-of-output "' not found"
487fa986 6608 (if timeout (format " in %d secs" timeout) "")
fb7933a3
KG
6609 "]]"))))
6610 (goto-char (point-min))
6611 ;; Return value is whether end-of-output sentinel was found.
6612 found))
6613
fb7933a3
KG
6614(defun tramp-send-command-and-check (multi-method method user host command
6615 &optional subshell)
6616 "Run COMMAND and check its exit status.
6617MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6618Sends `echo $?' along with the COMMAND for checking the exit status. If
6619COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6620
6621If the optional argument SUBSHELL is non-nil, the command is executed in
6622a subshell, ie surrounded by parentheses."
6623 (tramp-send-command multi-method method user host
6624 (concat (if subshell "( " "")
6625 command
6626 (if command " 2>/dev/null; " "")
6627 "echo tramp_exit_status $?"
6628 (if subshell " )" " ")))
6629 (tramp-wait-for-output)
6630 (goto-char (point-max))
6631 (unless (search-backward "tramp_exit_status " nil t)
6632 (error "Couldn't find exit status of `%s'" command))
6633 (skip-chars-forward "^ ")
6634 (read (current-buffer)))
6635
6636(defun tramp-barf-unless-okay (multi-method method user host command subshell
6637 signal fmt &rest args)
6638 "Run COMMAND, check exit status, throw error if exit status not okay.
6639Similar to `tramp-send-command-and-check' but accepts two more arguments
6640FMT and ARGS which are passed to `error'."
6641 (unless (zerop (tramp-send-command-and-check
6642 multi-method method user host command subshell))
6643 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6644 ;; silent.
6645 (pop-to-buffer (current-buffer))
6646 (funcall 'signal signal (apply 'format fmt args))))
6647
7432277c
KG
6648;; It seems that Tru64 Unix does not like it if long strings are sent
6649;; to it in one go. (This happens when sending the Perl
6650;; `file-attributes' implementation, for instance.) Therefore, we
6651;; have this function which waits a bit at each line.
6652(defun tramp-send-string
6653 (multi-method method user host string)
6654 "Send the STRING to USER at HOST using METHOD.
6655
6656The STRING is expected to use Unix line-endings, but the lines sent to
6657the remote host use line-endings as defined in the variable
6658`tramp-rsh-end-of-line'."
fb7933a3
KG
6659 (let ((proc (get-buffer-process
6660 (tramp-get-buffer multi-method method user host))))
6661 (unless proc
7432277c
KG
6662 (error "Can't send string to remote host -- not logged in"))
6663 ;; debug message
6664 (when tramp-debug-buffer
6665 (save-excursion
6666 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6667 (goto-char (point-max))
6668 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6669 ;; replace "\n" by `tramp-rsh-end-of-line'
6670 (setq string
6671 (mapconcat 'identity
6672 (split-string string "\n")
6673 tramp-rsh-end-of-line))
49914e04
AS
6674 (unless (or (string= string "")
6675 (string-equal (substring string -1) tramp-rsh-end-of-line))
7432277c
KG
6676 (setq string (concat string tramp-rsh-end-of-line)))
6677 ;; send the string
8daea7fc 6678 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
7432277c
KG
6679 (let ((pos 0)
6680 (end (length string)))
16674e4f
KG
6681 (while (< pos end)
6682 (tramp-message-for-buffer
6683 multi-method method user host 10
7432277c
KG
6684 "Sending chunk from %s to %s"
6685 pos (min (+ pos tramp-chunksize) end))
6686 (process-send-string
6687 proc (substring string pos (min (+ pos tramp-chunksize) end)))
16674e4f
KG
6688 (setq pos (+ pos tramp-chunksize))
6689 (sleep-for 0.1)))
7432277c 6690 (process-send-string proc string))))
fb7933a3
KG
6691
6692(defun tramp-send-eof (multi-method method user host)
6693 "Send EOF to the remote end.
25f78d18 6694METHOD, HOST and USER specify the connection."
fb7933a3
KG
6695 (let ((proc (get-buffer-process
6696 (tramp-get-buffer multi-method method user host))))
6697 (unless proc
6698 (error "Can't send EOF to remote host -- not logged in"))
6699 (process-send-eof proc)))
6700; (process-send-string proc "\^D")))
6701
6702(defun tramp-kill-process (multi-method method user host)
6703 "Kill the connection process used by Tramp.
25f78d18 6704MULTI-METHOD, METHOD, USER, and HOST specify the connection."
fb7933a3
KG
6705 (let ((proc (get-buffer-process
6706 (tramp-get-buffer multi-method method user host))))
6707 (kill-process proc)))
6708
6709(defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6710 "Erase buffer, then discard subsequent garbage.
6711If `tramp-discard-garbage' is nil, just erase buffer."
6712 (if (not tramp-discard-garbage)
6713 (erase-buffer)
d2a2c17f 6714 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
fb7933a3
KG
6715 (when tramp-debug-buffer
6716 (save-excursion
6717 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6718 (goto-char (point-max))
6719 (tramp-insert-with-face
6720 'bold (format "Additional characters detected\n")))))))
6721
6722(defun tramp-mode-string-to-int (mode-string)
6723 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6724 (let* ((mode-chars (string-to-vector mode-string))
6725 (owner-read (aref mode-chars 1))
6726 (owner-write (aref mode-chars 2))
6727 (owner-execute-or-setid (aref mode-chars 3))
6728 (group-read (aref mode-chars 4))
6729 (group-write (aref mode-chars 5))
6730 (group-execute-or-setid (aref mode-chars 6))
6731 (other-read (aref mode-chars 7))
6732 (other-write (aref mode-chars 8))
6733 (other-execute-or-sticky (aref mode-chars 9)))
6734 (save-match-data
6735 (logior
6736 (case owner-read
6737 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6738 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6739 (case owner-write
6740 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6741 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6742 (case owner-execute-or-setid
6743 (?x (tramp-octal-to-decimal "00100"))
6744 (?S (tramp-octal-to-decimal "04000"))
6745 (?s (tramp-octal-to-decimal "04100"))
6746 (?- 0)
6747 (t (error "Fourth char `%c' must be one of `xsS-'"
6748 owner-execute-or-setid)))
6749 (case group-read
6750 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6751 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6752 (case group-write
6753 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6754 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6755 (case group-execute-or-setid
6756 (?x (tramp-octal-to-decimal "00010"))
6757 (?S (tramp-octal-to-decimal "02000"))
6758 (?s (tramp-octal-to-decimal "02010"))
6759 (?- 0)
6760 (t (error "Seventh char `%c' must be one of `xsS-'"
6761 group-execute-or-setid)))
6762 (case other-read
6763 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6764 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6765 (case other-write
6766 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6767 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6768 (case other-execute-or-sticky
6769 (?x (tramp-octal-to-decimal "00001"))
6770 (?T (tramp-octal-to-decimal "01000"))
6771 (?t (tramp-octal-to-decimal "01001"))
6772 (?- 0)
6773 (t (error "Tenth char `%c' must be one of `xtT-'"
6774 other-execute-or-sticky)))))))
6775
c82c5727
LH
6776(defun tramp-convert-file-attributes (multi-method method user host attr)
6777 "Convert file-attributes ATTR generated by perl script or ls.
6778Convert file mode bits to string and set virtual device number.
6779Return ATTR."
ca637b2a 6780 ;; Convert file mode bits to string.
c82c5727 6781 (unless (stringp (nth 8 attr))
c82c5727 6782 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
ca637b2a
MA
6783 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6784 (when (numberp (nth 3 attr))
6785 (setcar (nthcdr 9 attr)
6786 (not (= (nth 3 attr)
6787 (tramp-get-remote-gid multi-method method user host)))))
c82c5727
LH
6788 ;; Set virtual device number.
6789 (setcar (nthcdr 11 attr)
6790 (tramp-get-device multi-method method user host))
6791 attr)
6792
6793(defun tramp-get-device (multi-method method user host)
6794 "Returns the virtual device number.
6795If it doesn't exist, generate a new one."
6796 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6797 (unless (assoc string tramp-devices)
6798 (add-to-list 'tramp-devices
6799 (list string (length tramp-devices))))
6800 (list -1 (nth 1 (assoc string tramp-devices)))))
fb7933a3
KG
6801
6802(defun tramp-file-mode-from-int (mode)
6803 "Turn an integer representing a file mode into an ls(1)-like string."
6804 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6805 (user (logand (lsh mode -6) 7))
6806 (group (logand (lsh mode -3) 7))
6807 (other (logand (lsh mode -0) 7))
6808 (suid (> (logand (lsh mode -9) 4) 0))
6809 (sgid (> (logand (lsh mode -9) 2) 0))
6810 (sticky (> (logand (lsh mode -9) 1) 0)))
6811 (setq user (tramp-file-mode-permissions user suid "s"))
6812 (setq group (tramp-file-mode-permissions group sgid "s"))
6813 (setq other (tramp-file-mode-permissions other sticky "t"))
6814 (concat type user group other)))
6815
6816
6817(defun tramp-file-mode-permissions (perm suid suid-text)
6818 "Convert a permission bitset into a string.
6819This is used internally by `tramp-file-mode-from-int'."
6820 (let ((r (> (logand perm 4) 0))
6821 (w (> (logand perm 2) 0))
6822 (x (> (logand perm 1) 0)))
6823 (concat (or (and r "r") "-")
6824 (or (and w "w") "-")
6825 (or (and suid x suid-text) ; suid, execute
6826 (and suid (upcase suid-text)) ; suid, !execute
6827 (and x "x") "-")))) ; !suid
6828
6829
6830(defun tramp-decimal-to-octal (i)
6831 "Return a string consisting of the octal digits of I.
6832Not actually used. Use `(format \"%o\" i)' instead?"
6833 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6834 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6835 ((zerop i) "0")
6836 (t (concat (tramp-decimal-to-octal (/ i 8))
6837 (number-to-string (% i 8))))))
6838
6839
6840;;(defun tramp-octal-to-decimal (ostr)
6841;; "Given a string of octal digits, return a decimal number."
6842;; (cond ((null ostr) 0)
6843;; ((string= "" ostr) 0)
6844;; (t (let ((last (aref ostr (1- (length ostr))))
6845;; (rest (substring ostr 0 (1- (length ostr)))))
6846;; (unless (and (>= last ?0)
6847;; (<= last ?7))
6848;; (error "Not an octal digit: %c" last))
6849;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6850;; Kudos to Gerd Moellmann for this suggestion.
6851(defun tramp-octal-to-decimal (ostr)
6852 "Given a string of octal digits, return a decimal number."
6853 (let ((x (or ostr "")))
6854 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6855 (unless (string-match "\\`[0-7]*\\'" x)
6856 (error "Non-octal junk in string `%s'" x))
6857 (string-to-number ostr 8)))
6858
6859(defun tramp-shell-case-fold (string)
6860 "Converts STRING to shell glob pattern which ignores case."
6861 (mapconcat
6862 (lambda (c)
6863 (if (equal (downcase c) (upcase c))
6864 (vector c)
6865 (format "[%c%c]" (downcase c) (upcase c))))
6866 string
6867 ""))
6868
6869
bf247b6e
KS
6870;; ------------------------------------------------------------
6871;; -- TRAMP file names --
6872;; ------------------------------------------------------------
fb7933a3
KG
6873;; Conversion functions between external representation and
6874;; internal data structure. Convenience functions for internal
6875;; data structure.
6876
7432277c 6877(defstruct tramp-file-name multi-method method user host localname)
fb7933a3
KG
6878
6879(defun tramp-tramp-file-p (name)
6880 "Return t iff NAME is a tramp file."
6881 (save-match-data
6882 (string-match tramp-file-name-regexp name)))
bf247b6e 6883
fb7933a3
KG
6884;; HHH: Changed. Used to assign the return value of (user-login-name)
6885;; to the `user' part of the structure if a user name was not
6886;; provided, now it assigns nil.
6887(defun tramp-dissect-file-name (name)
6888 "Return an `tramp-file-name' structure.
6889The structure consists of remote method, remote user, remote host and
7432277c 6890localname (file name on remote host)."
4007ba5b
KG
6891 (save-match-data
6892 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6893 (method
6894 ; single-hop
6895 (if match (match-string (nth 1 tramp-file-name-structure) name)
6896 ; maybe multi-hop
6897 (string-match
6898 (format (nth 0 tramp-multi-file-name-structure)
6899 (nth 0 tramp-multi-file-name-hop-structure)) name)
6900 (match-string (nth 1 tramp-multi-file-name-structure) name))))
c62c9d08 6901 (if (and method (member method tramp-multi-methods))
fb7933a3
KG
6902 ;; If it's a multi method, the file name structure contains
6903 ;; arrays of method, user and host.
6904 (tramp-dissect-multi-file-name name)
c62c9d08 6905 ;; Normal method. First, find out default method.
4007ba5b 6906 (unless match (error "Not a tramp file name: %s" name))
c62c9d08
KG
6907 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6908 (host (match-string (nth 3 tramp-file-name-structure) name))
7432277c 6909 (localname (match-string (nth 4 tramp-file-name-structure) name)))
c62c9d08
KG
6910 (make-tramp-file-name
6911 :multi-method nil
6912 :method method
6913 :user (or user nil)
6914 :host host
7432277c 6915 :localname localname))))))
c62c9d08
KG
6916
6917(defun tramp-find-default-method (user host)
6918 "Look up the right method to use in `tramp-default-method-alist'."
6919 (let ((choices tramp-default-method-alist)
6920 (method tramp-default-method)
6921 item)
6922 (while choices
6923 (setq item (pop choices))
16674e4f 6924 (when (and (string-match (nth 0 item) (or host ""))
c62c9d08
KG
6925 (string-match (nth 1 item) (or user "")))
6926 (setq method (nth 2 item))
6927 (setq choices nil)))
6928 method))
16674e4f
KG
6929
6930(defun tramp-find-method (multi-method method user host)
6931 "Return the right method string to use.
6932This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6933If both MULTI-METHOD and METHOD are nil, do a lookup in
6934`tramp-default-method-alist'."
6935 (or multi-method method (tramp-find-default-method user host)))
bf247b6e 6936
fb7933a3
KG
6937;; HHH: Not Changed. Multi method. Will probably not handle the case where
6938;; a user name is not provided in the "file name" very well.
6939(defun tramp-dissect-multi-file-name (name)
6940 "Not implemented yet."
6941 (let ((regexp (nth 0 tramp-multi-file-name-structure))
6942 (method-index (nth 1 tramp-multi-file-name-structure))
6943 (hops-index (nth 2 tramp-multi-file-name-structure))
7432277c 6944 (localname-index (nth 3 tramp-multi-file-name-structure))
fb7933a3
KG
6945 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
6946 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
6947 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
6948 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
7432277c 6949 method hops len hop-methods hop-users hop-hosts localname)
fb7933a3
KG
6950 (unless (string-match (format regexp hop-regexp) name)
6951 (error "Not a multi tramp file name: %s" name))
6952 (setq method (match-string method-index name))
6953 (setq hops (match-string hops-index name))
6954 (setq len (/ (length (match-data t)) 2))
7432277c
KG
6955 (when (< localname-index 0) (incf localname-index len))
6956 (setq localname (match-string localname-index name))
fb7933a3
KG
6957 (let ((index 0))
6958 (while (string-match hop-regexp hops index)
6959 (setq index (match-end 0))
6960 (setq hop-methods
6961 (cons (match-string hop-method-index hops) hop-methods))
6962 (setq hop-users
6963 (cons (match-string hop-user-index hops) hop-users))
6964 (setq hop-hosts
6965 (cons (match-string hop-host-index hops) hop-hosts))))
6966 (make-tramp-file-name
6967 :multi-method method
6968 :method (apply 'vector (reverse hop-methods))
6969 :user (apply 'vector (reverse hop-users))
6970 :host (apply 'vector (reverse hop-hosts))
7432277c 6971 :localname localname)))
fb7933a3 6972
7432277c
KG
6973(defun tramp-make-tramp-file-name (multi-method method user host localname)
6974 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
fb7933a3 6975 (if multi-method
7432277c 6976 (tramp-make-tramp-multi-file-name multi-method method user host localname)
16674e4f
KG
6977 (format-spec
6978 (concat tramp-prefix-format
6979 (when method (concat "%m" tramp-postfix-single-method-format))
6980 (when user (concat "%u" tramp-postfix-user-format))
6981 (when host (concat "%h" tramp-postfix-host-format))
7432277c
KG
6982 (when localname (concat "%p")))
6983 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
fb7933a3
KG
6984
6985;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
6986;; with this when USER is nil?
7432277c 6987(defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
fb7933a3
KG
6988 "Constructs a tramp file name for a multi-hop method."
6989 (unless tramp-make-multi-tramp-file-format
6990 (error "`tramp-make-multi-tramp-file-format' is nil"))
6991 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
6992 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
7432277c 6993 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
ac474af1 6994 (prefix (format-spec prefix-format `((?m . ,multi-method))))
fb7933a3 6995 (hops "")
7432277c 6996 (localname (format-spec localname-format `((?p . ,localname))))
fb7933a3
KG
6997 (i 0)
6998 (len (length method)))
6999 (while (< i len)
90dc758d
KG
7000 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
7001 (setq hops (concat hops (format-spec hop-format
ac474af1 7002 `((?m . ,m) (?u . ,u) (?h . ,h)))))
fb7933a3 7003 (incf i)))
7432277c 7004 (concat prefix hops localname)))
fb7933a3 7005
b25a52cc
KG
7006(defun tramp-make-copy-program-file-name (user host localname)
7007 "Create a file name suitable to be passed to `rcp' and workalikes."
fb7933a3 7008 (if user
7432277c
KG
7009 (format "%s@%s:%s" user host localname)
7010 (format "%s:%s" host localname)))
fb7933a3 7011
16674e4f 7012(defun tramp-method-out-of-band-p (multi-method method user host)
38c65fca 7013 "Return t if this is an out-of-band method, nil otherwise."
c951aecb 7014 (tramp-get-method-parameter
16674e4f 7015 multi-method
91879624 7016 (tramp-find-method multi-method method user host)
c951aecb 7017 user host 'tramp-copy-program))
fb7933a3
KG
7018
7019;; Variables local to connection.
7020
7021(defun tramp-get-ls-command (multi-method method user host)
7022 (save-excursion
7023 (tramp-maybe-open-connection multi-method method user host)
7024 (set-buffer (tramp-get-buffer multi-method method user host))
7025 tramp-ls-command))
7026
7027(defun tramp-get-test-groks-nt (multi-method method user host)
7028 (save-excursion
7029 (tramp-maybe-open-connection multi-method method user host)
7030 (set-buffer (tramp-get-buffer multi-method method user host))
7031 tramp-test-groks-nt))
7032
7033(defun tramp-get-file-exists-command (multi-method method user host)
7034 (save-excursion
7035 (tramp-maybe-open-connection multi-method method user host)
7036 (set-buffer (tramp-get-buffer multi-method method user host))
7037 tramp-file-exists-command))
7038
7039(defun tramp-get-remote-perl (multi-method method user host)
7040 (tramp-get-connection-property "perl" nil multi-method method user host))
7041
7042(defun tramp-get-remote-ln (multi-method method user host)
7043 (tramp-get-connection-property "ln" nil multi-method method user host))
7044
a69c01a0
MA
7045(defun tramp-get-remote-uid (multi-method method user host)
7046 (tramp-get-connection-property "uid" nil multi-method method user host))
7047
7048(defun tramp-get-remote-gid (multi-method method user host)
7049 (tramp-get-connection-property "gid" nil multi-method method user host))
7050
fb7933a3 7051;; Get a property of a TRAMP connection.
ac474af1
KG
7052(defun tramp-get-connection-property
7053 (property default multi-method method user host)
fb7933a3
KG
7054 "Get the named property for the connection.
7055If the value is not set for the connection, return `default'"
7056 (tramp-maybe-open-connection multi-method method user host)
7057 (with-current-buffer (tramp-get-buffer multi-method method user host)
7058 (let (error)
7059 (condition-case nil
7060 (symbol-value (intern (concat "tramp-connection-property-" property)))
a69c01a0 7061 (error default)))))
fb7933a3
KG
7062
7063;; Set a property of a TRAMP connection.
ac474af1
KG
7064(defun tramp-set-connection-property
7065 (property value multi-method method user host)
fb7933a3
KG
7066 "Set the named property of a TRAMP connection."
7067 (tramp-maybe-open-connection multi-method method user host)
7068 (with-current-buffer (tramp-get-buffer multi-method method user host)
7069 (set (make-local-variable
7070 (intern (concat "tramp-connection-property-" property)))
7071 value)))
7072
ac474af1 7073;; Some predefined connection properties.
16674e4f
KG
7074(defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7075 (tramp-set-connection-property "remote-encoding" rem-enc
ac474af1 7076 multi-method method user host))
16674e4f
KG
7077(defun tramp-get-remote-encoding (multi-method method user host)
7078 (tramp-get-connection-property "remote-encoding" nil
ac474af1 7079 multi-method method user host))
16674e4f
KG
7080
7081(defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7082 (tramp-set-connection-property "remote-decoding" rem-dec
ac474af1 7083 multi-method method user host))
16674e4f
KG
7084(defun tramp-get-remote-decoding (multi-method method user host)
7085 (tramp-get-connection-property "remote-decoding" nil
ac474af1 7086 multi-method method user host))
16674e4f
KG
7087
7088(defun tramp-set-local-encoding (multi-method method user host loc-enc)
7089 (tramp-set-connection-property "local-encoding" loc-enc
ac474af1 7090 multi-method method user host))
16674e4f
KG
7091(defun tramp-get-local-encoding (multi-method method user host)
7092 (tramp-get-connection-property "local-encoding" nil
ac474af1 7093 multi-method method user host))
16674e4f
KG
7094
7095(defun tramp-set-local-decoding (multi-method method user host loc-dec)
7096 (tramp-set-connection-property "local-decoding" loc-dec
ac474af1 7097 multi-method method user host))
16674e4f
KG
7098(defun tramp-get-local-decoding (multi-method method user host)
7099 (tramp-get-connection-property "local-decoding" nil
ac474af1 7100 multi-method method user host))
fb7933a3 7101
c951aecb
KG
7102(defun tramp-get-method-parameter (multi-method method user host param)
7103 "Return the method parameter PARAM.
7104If the `tramp-methods' entry does not exist, use the variable PARAM
7105as default."
7106 (unless (boundp param)
7107 (error "Non-existing method parameter `%s'" param))
7108 (let ((entry (assoc param
90f8dc03
KG
7109 (assoc (tramp-find-method multi-method method user host)
7110 tramp-methods))))
c951aecb
KG
7111 (if entry
7112 (second entry)
7113 (symbol-value param))))
bf247b6e 7114
90f8dc03 7115
fb7933a3
KG
7116;; Auto saving to a special directory.
7117
00cec167
MA
7118(defun tramp-exists-file-name-handler (operation &rest args)
7119 (let ((buffer-file-name "/")
7120 (fnha file-name-handler-alist)
7121 (check-file-name-operation operation)
7122 (file-name-handler-alist
7123 (list
7124 (cons "/"
7125 '(lambda (operation &rest args)
7126 "Returns OPERATION if it is the one to be checked"
7127 (if (equal check-file-name-operation operation)
7128 operation
7129 (let ((file-name-handler-alist fnha))
7130 (apply operation args))))))))
7131 (eq (apply operation args) operation)))
c1105d05
MA
7132
7133(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7134 (defadvice make-auto-save-file-name
7135 (around tramp-advice-make-auto-save-file-name () activate)
7136 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7137 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
00cec167 7138 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
a69c01a0
MA
7139 ad-do-it))
7140 (add-hook 'tramp-unload-hook
7141 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
fb7933a3 7142
340b8d4f
MA
7143;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7144;; permission 0666 minus umask. This is a security threat.
414da5ab
MA
7145
7146(defun tramp-set-auto-save-file-modes ()
7147 "Set permissions of autosaved remote files to the original permissions."
7148 (let ((bfn (buffer-file-name)))
7149 (when (and (stringp bfn)
7150 (tramp-tramp-file-p bfn)
7151 (stringp buffer-auto-save-file-name)
340b8d4f
MA
7152 (not (equal bfn buffer-auto-save-file-name)))
7153 (unless (file-exists-p buffer-auto-save-file-name)
7154 (write-region "" nil buffer-auto-save-file-name))
7155 ;; Permissions should be set always, because there might be an old
7156 ;; auto-saved file belonging to another original file. This could
7157 ;; be a security threat.
7177e2a3 7158 (set-file-modes buffer-auto-save-file-name
11948172 7159 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
414da5ab
MA
7160
7161(unless (or (> emacs-major-version 21)
7162 (and (featurep 'xemacs)
7163 (= emacs-major-version 21)
340b8d4f 7164 (> emacs-minor-version 4)))
a69c01a0
MA
7165 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7166 (add-hook 'tramp-unload-hook
7167 '(lambda ()
7168 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
414da5ab 7169
fb7933a3
KG
7170(defun tramp-subst-strs-in-string (alist string)
7171 "Replace all occurrences of the string FROM with TO in STRING.
7172ALIST is of the form ((FROM . TO) ...)."
7173 (save-match-data
7174 (while alist
7175 (let* ((pr (car alist))
7176 (from (car pr))
7177 (to (cdr pr)))
7178 (while (string-match (regexp-quote from) string)
7179 (setq string (replace-match to t t string)))
7180 (setq alist (cdr alist))))
7181 string))
7182
7183(defun tramp-insert-with-face (face string)
7184 "Insert text with a specific face."
7185 (let ((start (point)))
7186 (insert string)
7187 (add-text-properties start (point) (list 'face face))))
7188
7189;; ------------------------------------------------------------
7190;; -- Compatibility functions section --
7191;; ------------------------------------------------------------
7192
7193(defun tramp-temporary-file-directory ()
7194 "Return name of directory for temporary files (compat function).
7195For Emacs, this is the variable `temporary-file-directory', for XEmacs
7196this is the function `temp-directory'."
7197 (cond ((boundp 'temporary-file-directory)
7198 (symbol-value 'temporary-file-directory))
7199 ((fboundp 'temp-directory)
7200 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7201 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7202 (file-name-as-directory (getenv "TEMP")))
7203 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7204 (file-name-as-directory (getenv "TMP")))
7205 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7206 (file-name-as-directory (getenv "TMPDIR")))
7207 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7208 (t (message (concat "Neither `temporary-file-directory' nor "
7209 "`temp-directory' is defined -- using /tmp."))
7210 (file-name-as-directory "/tmp"))))
7211
07dfe738 7212(defun tramp-read-passwd (user host prompt)
fb7933a3 7213 "Read a password from user (compat function).
5ec2cc41
KG
7214Invokes `password-read' if available, `read-passwd' else."
7215 (if (functionp 'password-read)
07dfe738 7216 (let* ((key (concat (or user (user-login-name)) "@" host))
5ec2cc41
KG
7217 (password (apply #'password-read (list prompt key))))
7218 (apply #'password-cache-add (list key password))
7219 password)
7220 (read-passwd prompt)))
7221
7222(defun tramp-clear-passwd (&optional user host)
7223 "Clear password cache for connection related to current-buffer."
7224 (interactive)
7225 (let ((filename (or buffer-file-name list-buffers-directory "")))
7226 (when (and (functionp 'password-cache-remove)
7227 (or (and user host) (tramp-tramp-file-p filename)))
7228 (let* ((v (when (tramp-tramp-file-p filename)
7229 (tramp-dissect-file-name filename)))
7230 (luser (or user (tramp-file-name-user v) (user-login-name)))
7231 (lhost (or host (tramp-file-name-host v) (system-name)))
7232 (key (concat luser "@" lhost)))
7233 (apply #'password-cache-remove (list key))))))
fb7933a3
KG
7234
7235(defun tramp-time-diff (t1 t2)
7236 "Return the difference between the two times, in seconds.
7237T1 and T2 are time values (as returned by `current-time' for example).
7238
7239NOTE: This function will fail if the time difference is too large to
7240fit in an integer."
7241 ;; Pacify byte-compiler with `symbol-function'.
ea9d1443
KG
7242 (cond ((and (fboundp 'subtract-time)
7243 (fboundp 'float-time))
7244 (funcall (symbol-function 'float-time)
7245 (funcall (symbol-function 'subtract-time) t1 t2)))
7246 ((and (fboundp 'subtract-time)
7247 (fboundp 'time-to-seconds))
7248 (funcall (symbol-function 'time-to-seconds)
7249 (funcall (symbol-function 'subtract-time) t1 t2)))
fb7933a3
KG
7250 ((fboundp 'itimer-time-difference)
7251 (floor (funcall
7252 (symbol-function 'itimer-time-difference)
7253 (if (< (length t1) 3) (append t1 '(0)) t1)
7254 (if (< (length t2) 3) (append t2 '(0)) t2))))
7255 (t
ea9d1443
KG
7256 ;; snarfed from Emacs 21 time-date.el; combining
7257 ;; time-to-seconds and subtract-time
7258 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
fb7933a3 7259 (list (- (car t1) (car t2) (if borrow 1 0))
ea9d1443
KG
7260 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7261 (+ (* (car time) 65536.0)
7262 (cadr time)
7263 (/ (or (nth 2 time) 0) 1000000.0))))))
fb7933a3
KG
7264
7265(defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7266 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7267EOL-TYPE can be one of `dos', `unix', or `mac'."
7268 (cond ((fboundp 'coding-system-change-eol-conversion)
7269 (apply #'coding-system-change-eol-conversion
7270 (list coding-system eol-type)))
7271 ((fboundp 'subsidiary-coding-system)
7272 (apply
7273 #'subsidiary-coding-system
7274 (list coding-system
7275 (cond ((eq eol-type 'dos) 'crlf)
7276 ((eq eol-type 'unix) 'lf)
7277 ((eq eol-type 'mac) 'cr)
7278 (t
7279 (error "Unknown EOL-TYPE `%s', must be %s"
7280 eol-type
7281 "`dos', `unix', or `mac'"))))))
7282 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7283
7284(defun tramp-split-string (string pattern)
7285 "Like `split-string' but omit empty strings.
7286In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7287This is, the first, empty, element is omitted. In XEmacs, the first
7288element is not omitted.
7289
7290Note: this function has been written for `tramp-handle-file-truename'.
7291If you want to use it for something else, you'll have to check whether
7292it does the right thing."
7293 (delete "" (split-string string pattern)))
7294
19a87064
MA
7295(defun tramp-set-process-query-on-exit-flag (process flag)
7296 "Specify if query is needed for process when Emacs is exited.
7297If the second argument flag is non-nil, Emacs will query the user before
7298exiting if process is running."
d2a2c17f 7299 (funcall
19a87064 7300 (if (fboundp 'set-process-query-on-exit-flag)
d2a2c17f
MA
7301 (symbol-function 'set-process-query-on-exit-flag)
7302 (symbol-function 'process-kill-without-query))
7303 process flag))
19a87064 7304
19a87064 7305
bf247b6e
KS
7306;; ------------------------------------------------------------
7307;; -- Kludges section --
7308;; ------------------------------------------------------------
fb7933a3
KG
7309
7310;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7311;; does not deal well with newline characters. Newline is replaced by
7312;; backslash newline. But if, say, the string `a backslash newline b'
7313;; is passed to a shell, the shell will expand this into "ab",
7314;; completely omitting the newline. This is not what was intended.
7315;; It does not appear to be possible to make the function
7316;; `shell-quote-argument' work with newlines without making it
7317;; dependent on the shell used. But within this package, we know that
7318;; we will always use a Bourne-like shell, so we use an approach which
7319;; groks newlines.
7320;;
7321;; The approach is simple: we call `shell-quote-argument', then
7322;; massage the newline part of the result.
7323;;
7324;; This function should produce a string which is grokked by a Unix
7325;; shell, even if the Emacs is running on Windows. Since this is the
7326;; kludges section, we bind `system-type' in such a way that
7327;; `shell-quote-arguments' behaves as if on Unix.
7328;;
7329;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7330;; function to work with Bourne-like shells.
7331;;
7332;; CCC: This function should be rewritten so that
7333;; `shell-quote-argument' is not used. This way, we are safe from
7334;; changes in `shell-quote-argument'.
7335(defun tramp-shell-quote-argument (s)
7336 "Similar to `shell-quote-argument', but groks newlines.
7337Only works for Bourne-like shells."
7338 (let ((system-type 'not-windows))
7339 (save-match-data
7340 (let ((result (shell-quote-argument s))
7341 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7342 (when (and (>= (length result) 2)
7343 (string= (substring result 0 2) "\\~"))
7344 (setq result (substring result 1)))
7345 (while (string-match nl result)
7346 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7347 t t result)))
7348 result))))
7349
7350;; ;; EFS hooks itself into the file name handling stuff in more places
7351;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7432277c 7352;; ;; away from tramp.el file names.
fb7933a3
KG
7353;; ;;
7354;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7355;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7356;; ;; connection to help it's dired process. Not that I have any real
7357;; ;; idea *why* this is helpful to dired.
7358;; ;;
7359;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7360;; ;;
7361;; ;; Daniel Pittman <daniel@danann.net>
7362;; ;;
7363;; ;; CCC: when the other defadvice calls have disappeared, make sure
7364;; ;; not to call defadvice unless it's necessary. How do we find out whether
7365;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7366;; ;; EFS might nicht be loaded yet.
7432277c
KG
7367;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7368;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
fb7933a3
KG
7369;; (if (tramp-tramp-file-p (ad-get-arg 0))
7370;; nil
7371;; ad-do-it))
7372
16674e4f
KG
7373;; We currently (sometimes) use "[" and "]" in the filename format.
7374;; This means that Emacs wants to expand wildcards if
fb7933a3
KG
7375;; `find-file-wildcards' is non-nil, and then barfs because no
7376;; expansion could be found. We detect this situation and do
7377;; something really awful: we have `file-expand-wildcards' return the
7378;; original filename if it can't expand anything. Let's just hope
7379;; that this doesn't break anything else.
16674e4f
KG
7380;; CCC: This check is now also really awful; we should search all
7381;; of the filename format, not just the prefix.
7382(when (string-match "\\[" tramp-prefix-format)
d2a2c17f
MA
7383 (defadvice file-expand-wildcards (around tramp-fix activate)
7384 (let ((name (ad-get-arg 0)))
7385 (if (tramp-tramp-file-p name)
7386 ;; If it's a Tramp file, dissect it and look if wildcards
7387 ;; need to be expanded at all.
7388 (let ((v (tramp-dissect-file-name name)))
7389 (if (string-match "[[*?]" (tramp-file-name-localname v))
7390 (let ((res ad-do-it))
7391 (setq ad-return-value (or res (list name))))
7392 (setq ad-return-value (list name))))
7393 ;; If it is not a Tramp file, just run the original function.
7394 (let ((res ad-do-it))
a69c01a0
MA
7395 (setq ad-return-value (or res (list name)))))))
7396 (add-hook 'tramp-unload-hook
7397 '(lambda () (ad-unadvise 'file-expand-wildcards))))
fb7933a3 7398
ac474af1
KG
7399;; Tramp version is useful in a number of situations.
7400
7401(defun tramp-version (arg)
7402 "Print version number of tramp.el in minibuffer or current buffer."
7403 (interactive "P")
7404 (if arg (insert tramp-version) (message tramp-version)))
7405
fb7933a3
KG
7406;; Make the `reporter` functionality available for making bug reports about
7407;; the package. A most useful piece of code.
7408
7409(unless (fboundp 'reporter-submit-bug-report)
7410 (autoload 'reporter-submit-bug-report "reporter"))
7411
7412(defun tramp-bug ()
7413 "Submit a bug report to the TRAMP developers."
7414 (interactive)
7415 (require 'reporter)
340b8d4f 7416 (catch 'dont-send
d2a2c17f 7417 (let ((reporter-prompt-for-summary-p t))
340b8d4f
MA
7418 (reporter-submit-bug-report
7419 tramp-bug-report-address ; to-address
7420 (format "tramp (%s)" tramp-version) ; package name and version
d2a2c17f
MA
7421 (delq nil
7422 `(;; Current state
7423 tramp-ls-command
7424 tramp-test-groks-nt
7425 tramp-file-exists-command
7426 tramp-current-multi-method
7427 tramp-current-method
7428 tramp-current-user
7429 tramp-current-host
7430
7431 ;; System defaults
7432 tramp-auto-save-directory ; vars to dump
7433 tramp-default-method
7434 tramp-rsh-end-of-line
7435 tramp-default-password-end-of-line
7436 tramp-remote-path
7437 tramp-login-prompt-regexp
7438 ;; Mask non-7bit characters
7439 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7440 tramp-wrong-passwd-regexp
7441 tramp-yesno-prompt-regexp
7442 tramp-yn-prompt-regexp
7443 tramp-terminal-prompt-regexp
7444 tramp-temp-name-prefix
7445 tramp-file-name-structure
7446 tramp-file-name-regexp
7447 tramp-multi-file-name-structure
7448 tramp-multi-file-name-hop-structure
7449 tramp-multi-methods
7450 tramp-multi-connection-function-alist
7451 tramp-methods
7452 tramp-end-of-output
7453 tramp-coding-commands
7454 tramp-actions-before-shell
7455 tramp-actions-copy-out-of-band
7456 tramp-multi-actions
7457 tramp-terminal-type
7458 ;; Mask non-7bit characters
7459 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7460 tramp-chunksize
7461 ,(when (boundp 'tramp-backup-directory-alist)
7462 'tramp-backup-directory-alist)
7463 ,(when (boundp 'tramp-bkup-backup-directory-info)
7464 'tramp-bkup-backup-directory-info)
7465
7466 ;; Non-tramp variables of interest
7467 ;; Mask non-7bit characters
7468 (shell-prompt-pattern . tramp-reporter-dump-variable)
7469 backup-by-copying
7470 backup-by-copying-when-linked
7471 backup-by-copying-when-mismatch
7472 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7473 'backup-by-copying-when-privileged-mismatch)
7474 ,(when (boundp 'password-cache)
7475 'password-cache)
7476 ,(when (boundp 'password-cache-expiry)
7477 'password-cache-expiry)
7478 ,(when (boundp 'backup-directory-alist)
7479 'backup-directory-alist)
7480 ,(when (boundp 'bkup-backup-directory-info)
7481 'bkup-backup-directory-info)
7482 file-name-handler-alist))
7483
7484 'tramp-load-report-modules ; pre-hook
340b8d4f
MA
7485 'tramp-append-tramp-buffers ; post-hook
7486 "\
fb7933a3
KG
7487Enter your bug report in this message, including as much detail as you
7488possibly can about the problem, what you did to cause it and what the
7489local and remote machines are.
7490
7491If you can give a simple set of instructions to make this bug happen
7492reliably, please include those. Thank you for helping kill bugs in
7493TRAMP.
89509ea0
KG
7494
7495Another useful thing to do is to put (setq tramp-debug-buffer t) in
7496the ~/.emacs file and to repeat the bug. Then, include the contents
7497of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7498report.
7499
fabf2143 7500--bug report follows this line--
340b8d4f
MA
7501"))))
7502
d2a2c17f
MA
7503(defun tramp-reporter-dump-variable (varsym mailbuf)
7504 "Pretty-print the value of the variable in symbol VARSYM.
7505Used for non-7bit chars in strings."
7506 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7507 (val (with-current-buffer reporter-eval-buffer
7508 (symbol-value varsym))))
7509
7510 ;; There are characters to be masked.
7511 (when (and (boundp 'mm-7bit-chars)
7512 (string-match
7513 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7514 (with-current-buffer reporter-eval-buffer
7515 (set varsym (concat "(base64-decode-string \""
7516 (base64-encode-string val)
7517 "\")"))))
7518
7519 ;; Dump variable.
7520 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7521
7522 ;; Remove string quotation.
7523 (forward-line -1)
7524 (when (looking-at
7525 (concat "\\(^.*\\)" "\"" ;; \1 "
7526 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7527 "\\(\".*\\)" "\\\\" ;; \3 \
7528 "\\(\")\\)" "\"$")) ;; \4 "
7529 (replace-match "\\1\\2\\3\\4")
7530 (beginning-of-line)
7531 (insert " ;; variable encoded due to non-printable characters\n"))
7532 (forward-line 1)
7533
7534 ;; Reset VARSYM to old value.
7535 (with-current-buffer reporter-eval-buffer
7536 (set varsym val))))
7537
7538(defun tramp-load-report-modules ()
7539 "Load needed modules for reporting."
340b8d4f 7540
7177e2a3 7541 ;; We load message.el and mml.el from Gnus.
340b8d4f 7542 (if (featurep 'xemacs)
7177e2a3
MA
7543 (progn
7544 (load "message" 'noerror)
7545 (load "mml" 'noerror))
7546 (require 'message nil 'noerror)
340b8d4f 7547 (require 'mml nil 'noerror))
7177e2a3 7548 (when (functionp 'message-mode)
d2a2c17f 7549 (funcall (symbol-function 'message-mode)))
7177e2a3 7550 (when (functionp 'mml-mode)
d2a2c17f
MA
7551 (funcall (symbol-function 'mml-mode) t)))
7552
7553(defun tramp-append-tramp-buffers ()
7554 "Append Tramp buffers into the bug report."
340b8d4f
MA
7555
7556 (when (and
7177e2a3
MA
7557 (eq major-mode 'message-mode)
7558 (boundp 'mml-mode)
7559 (symbol-value 'mml-mode))
7560
7561 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7562 (buffer-list
7563 (delq nil
7564 (mapcar '(lambda (b)
7565 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7566 (buffer-list))))
7567 (curbuf (current-buffer)))
340b8d4f
MA
7568
7569 ;; There is at least one Tramp buffer.
7570 (when buffer-list
7177e2a3 7571 (switch-to-buffer (list-buffers-noselect nil))
340b8d4f
MA
7572 (delete-other-windows)
7573 (setq buffer-read-only nil)
7177e2a3
MA
7574 (goto-char (point-min))
7575 (while (not (eobp))
7576 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7577 (forward-line 1)
7578 (forward-line 0)
7579 (let ((start (point)))
7580 (forward-line 1)
7581 (kill-region start (point)))))
340b8d4f
MA
7582 (insert "
7583The buffer(s) above will be appended to this message. If you don't want
7584to append a buffer because it contains sensible data, or because the buffer
7585is too large, you should delete the respective buffer. The buffer(s) will
7586contain user and host names. Passwords will never be included there.")
7587
7588 (when (and tramp-debug-buffer (> tramp-verbose 9))
7589 (insert "\n\n")
7590 (let ((start (point)))
7591 (insert "\
7592Please note that you have set `tramp-verbose' to a value greater than 9.
7593Therefore, the contents of files might be included in the debug buffer(s).")
7594 (add-text-properties start (point) (list 'face 'italic))))
7595
7596 (set-buffer-modified-p nil)
7597 (setq buffer-read-only t)
7598 (goto-char (point-min))
7599
7600 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7601 ;; OK, let's send. First we delete the buffer list.
7602 (progn
7603 (kill-buffer nil)
7604 (switch-to-buffer curbuf)
7605 (goto-char (point-max))
7606 (insert "\n\n")
7607 (dolist (buffer buffer-list)
d2a2c17f
MA
7608 (funcall (symbol-function 'mml-insert-empty-tag)
7609 'part 'type "text/plain" 'encoding "base64"
7610 'disposition "attachment" 'buffer (buffer-name buffer)
7611 'description (buffer-name buffer)))
340b8d4f
MA
7612 (set-buffer-modified-p nil))
7613
7614 ;; Don't send. Delete the message buffer.
7615 (set-buffer curbuf)
7616 (set-buffer-modified-p nil)
7617 (kill-buffer nil)
7618 (throw 'dont-send nil))))))
fb7933a3
KG
7619
7620(defalias 'tramp-submit-bug 'tramp-bug)
7621
a69c01a0
MA
7622;; Checklist for `tramp-unload-hook'
7623;; - Unload all `tramp-*' packages
7624;; - Reset `file-name-handler-alist'
7625;; - Cleanup hooks where Tramp functions are in
7626;; - Cleanup advised functions
7627;; - Cleanup autoloads
7628;;;###autoload
7629(defun tramp-unload-tramp ()
7630 (interactive)
7631 ;; When Tramp is not loaded yet, its autoloads are still active.
7632 (tramp-unload-file-name-handler-alist)
7633 ;; ange-ftp settings must be enabled.
7634 (when (functionp 'tramp-ftp-enable-ange-ftp)
7635 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7636 ;; `tramp-util' unloads also `tramp'.
7637 (condition-case nil ;; maybe its not loaded yet.
7638 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7639 (error nil)))
7640
fb7933a3
KG
7641(provide 'tramp)
7642
7643;; Make sure that we get integration with the VC package.
7644;; When it is loaded, we need to pull in the integration module.
7645;; This must come after (provide 'tramp) because tramp-vc.el
7646;; requires tramp.
7647(eval-after-load "vc"
a69c01a0
MA
7648 '(progn
7649 (require 'tramp-vc)
7650 (add-hook 'tramp-unload-hook
7651 '(lambda ()
7652 (when (featurep 'tramp-vc)
7653 (unload-feature 'tramp-vc 'force))))))
fb7933a3
KG
7654
7655;;; TODO:
7656
4007ba5b
KG
7657;; * Allow putting passwords in the filename.
7658;; This should be implemented via a general mechanism to add
7659;; parameters in filenames. There is currently a kludge for
7660;; putting the port number into the filename for ssh and ftp
7661;; files. This could be subsumed by the new mechanism as well.
7662;; Another approach is to read a netrc file like ~/.authinfo
7663;; from Gnus.
7664;; * Handle nonlocal exits such as C-g.
16674e4f 7665;; * Autodetect if remote `ls' groks the "--dired" switch.
b1d06e75
KG
7666;; * Add fallback for inline encodings. This should be used
7667;; if the remote end doesn't support mimencode or a similar program.
7668;; For reading files from the remote host, we can just parse the output
7669;; of `od -b'. For writing files to the remote host, we construct
7670;; a shell program which contains only "safe" ascii characters
7671;; and which writes the right bytes to the file. We can use printf(1)
7672;; or "echo -e" or the printf function in awk and use octal escapes
7673;; for the "dangerous" characters. The null byte might be a problem.
7674;; On some systems, the octal escape doesn't work. So we try the following
7675;; two commands to write a null byte:
7676;; dd if=/dev/zero bs=1 count=1
7677;; echo | tr '\n' '\000'
16674e4f
KG
7678;; * Separate local `tramp-coding-commands' from remote ones. Connect
7679;; the two via a format which can be `uu' or `b64'. Then we can search
7680;; for the right local commands and the right remote commands separately.
fb7933a3
KG
7681;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7682;; work for remote files.
fb7933a3 7683;; * Rewrite `tramp-shell-quote-argument' to abstain from using
b1d06e75 7684;; `shell-quote-argument'.
fb7933a3 7685;; * Completion gets confused when you leave out the method name.
fb7933a3
KG
7686;; * In Emacs 21, `insert-directory' shows total number of bytes used
7687;; by the files in that directory. Add this here.
7688;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7689;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7690;; * When logging in, keep looking for questions according to an alist
7691;; and then invoke the right function.
7692;; * Case-insensitive filename completion. (Norbert Goevert.)
7693;; * Running CVS remotely doesn't appear to work right. It thinks
7694;; files are locked by somebody else even if I'm the locking user.
7695;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7696;; (Skip Montanaro)
7697;; * Don't use globbing for directories with many files, as this is
7698;; likely to produce long command lines, and some shells choke on
7699;; long command lines.
fb7933a3
KG
7700;; * Find out about the new auto-save mechanism in Emacs 21 and
7701;; do the right thing.
7702;; * `vc-directory' does not work. It never displays any files, even
7703;; if it does show files when run locally.
7704;; * Allow correction of passwords, if the remote end allows this.
7705;; (Mark Hershberger)
fb7933a3
KG
7706;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7707;; * Do asynchronous `shell-command's.
7708;; * Grok `append' parameter for `write-region'.
7709;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7710;; * abbreviate-file-name
7711;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
fb7933a3
KG
7712;; * Also allow to omit user names when doing multi-hop. Not sure yet
7713;; what the user names should default to, though.
7714;; * better error checking. At least whenever we see something
7715;; strange when doing zerop, we should kill the process and start
7716;; again. (Greg Stark)
7717;; * Add caching for filename completion. (Greg Stark)
bf247b6e 7718;; Of course, this has issues with usability (stale cache bites)
fb7933a3
KG
7719;; -- <daniel@danann.net>
7720;; * Provide a local cache of old versions of remote files for the rsync
7721;; transfer method to use. (Greg Stark)
7722;; * Remove unneeded parameters from methods.
7723;; * Invoke rsync once for copying a whole directory hierarchy.
83bbd71b 7724;; (Francesco Potort\e,Al\e(B)
fb7933a3
KG
7725;; * Should we set PATH ourselves or should we rely on the remote end
7726;; to do it?
fb7933a3 7727;; * Make it work for XEmacs 20, which is missing `with-timeout'.
fb7933a3
KG
7728;; * Make it work for different encodings, and for different file name
7729;; encodings, too. (Daniel Pittman)
7730;; * Change applicable functions to pass a struct tramp-file-name rather
7432277c 7731;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
fb7933a3
KG
7732;; * Implement asynchronous shell commands.
7733;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7734;; * Progress reports while copying files. (Michael Kifer)
7735;; * `Smart' connection method that uses inline for small and out of
7736;; band for large files. (Michael Kifer)
7737;; * Don't search for perl5 and perl. Instead, only search for perl and
7738;; then look if it's the right version (with `perl -v').
7739;; * When editing a remote CVS controlled file as a different user, VC
7740;; gets confused about the file locking status. Try to find out why
7741;; the workaround doesn't work.
fb7933a3
KG
7742;; * Change `copy-file' to grok the case where the filename handler
7743;; for the source and the target file are different. Right now,
7744;; it looks at the source file and then calls that handler, if
7745;; there is one. But since ange-ftp, for instance, does not know
7746;; about Tramp, it does not do the right thing if the target file
7747;; name is a Tramp name.
3cdaec13 7748;; * Username and hostname completion.
16674e4f
KG
7749;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7750;; connect to host "blabla" already if that host is unique. No idea
7751;; how to suppress. Maybe not an essential problem.
16674e4f 7752;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
16674e4f 7753;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
8daea7fc 7754;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
16674e4f
KG
7755;; Code is nearly identical.
7756;; ** Decide whiche files to take for searching user/host names depending on
7757;; operating system (windows-nt) in `tramp-completion-function-alist'.
7758;; ** Enhance variables for debug.
7759;; ** Implement "/multi:" completion.
7760;; ** Add a learning mode for completion. Make results persistent.
c951aecb 7761;; * Allow out-of-band methods as _last_ multi-hop.
fb7933a3
KG
7762
7763;; Functions for file-name-handler-alist:
7764;; diff-latest-backup-file -- in diff.el
fb7933a3
KG
7765;; dired-uncache -- this will be needed when we do insert-directory caching
7766;; file-name-as-directory -- use primitive?
fb7933a3 7767;; file-name-sans-versions -- use primitive?
fb7933a3 7768;; get-file-buffer -- use primitive
fb7933a3
KG
7769;; vc-registered
7770
ab5796a9 7771;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
fb7933a3 7772;;; tramp.el ends here