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