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