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