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