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