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