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