]> code.delx.au - gnu-emacs/blob - lisp/net/tramp.el
* tramp.el: Version 2.0.31 released.
[gnu-emacs] / lisp / net / tramp.el
1 ;;; -*- mode: Emacs-Lisp; coding: iso-8859-1; -*-
2 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
3
4 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 ;; Author: kai.grossjohann@gmx.net
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 ;;
33 ;; For more detailed instructions, please see the info file.
34 ;;
35 ;; Notes:
36 ;; -----
37 ;;
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 ;;
49 ;; The current version of Tramp can be retrieved from the following URL:
50 ;; http://savannah.nongnu.org/download/tramp/
51 ;;
52 ;; There's a mailing list for this, as well. Its name is:
53 ;; tramp-devel@mail.freesoftware.fsf.org
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:
57 ;; tramp-devel-request@mail.freesoftware.fsf.org
58 ;; You can also use the Web to subscribe, under the following URL:
59 ;; http://mail.freesoftware.fsf.org/mailman/listinfo/tramp-devel
60 ;;
61 ;; For the adventurous, the current development sources are available
62 ;; via CVS. You can find instructions about this at the following URL:
63 ;; http://savannah.gnu.org/projects/tramp/
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
70 ;; The Tramp version number and bug report address, as prepared by configure.
71 (require 'trampver)
72
73 (require 'timer)
74 (require 'format-spec) ;from Gnus 5.8, also in tar ball
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
81 (require 'shell)
82 (require 'advice)
83
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
90 ;; ;; It does not work to load EFS after loading TRAMP.
91 ;; (when (fboundp 'efs-file-handler-function)
92 ;; (require 'efs))
93
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
105 ;; UNC file names like "//host/share/localname".
106 (unless (memq system-type '(cygwin windows-nt))
107 (eval-after-load "tramp"
108 '(require 'tramp-smb)))
109
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
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
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.
134 ;;;###autoload
135 (defvar tramp-unified-filenames (not (featurep 'xemacs))
136 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
137 Nil means to use a separate filename syntax for Tramp.")
138
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
145 (defcustom tramp-verbose 9
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.
157 The idea is to use a local directory so that auto-saving is faster."
158 :group 'tramp
159 :type '(choice (const nil)
160 string))
161
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.
167 This shell is used to execute the encoding and decoding command on the
168 local host, so if you want to use `~' in those commands, you should
169 choose a shell here which groks tilde expansion. `/bin/sh' normally
170 does not understand tilde expansion.
171
172 For encoding and deocding, commands like the following are executed:
173
174 /bin/sh -c COMMAND < INPUT > OUTPUT
175
176 This variable can be used to change the \"/bin/sh\" part. See the
177 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
178 variable `tramp-encoding-reads-stdin' to specify whether the commands read
179 standard input or a file.
180
181 Note that this variable is not used for remote commands. There are
182 mechanisms in tramp.el which automatically determine the right shell to
183 use for the remote host."
184 :group 'tramp
185 :type '(file :must-match t))
186
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.
192 See 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.
198 If nil, the filename is the last argument.
199
200 Note that the commands always must write to standard output."
201 :group 'tramp
202 :type 'boolean)
203
204 (defcustom tramp-multi-sh-program
205 tramp-encoding-shell
206 "*Use this program for bootstrapping multi-hop connections.
207 This variable is similar to `tramp-encoding-shell', but it is only used
208 when initializing a multi-hop connection. Therefore, the set of
209 commands sent to this shell is quite restricted, and if you are
210 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
211 shell which does not normally exist on Windows anyway).
212
213 To use multi-hop methods from Windows, you also need suitable entries
214 in `tramp-multi-connection-function-alist' for the first hop.
215
216 This variable defaults to the value of `tramp-encoding-shell'."
217 :group 'tramp
218 :type '(file :must-match t))
219
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)
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)
256 (tramp-telnet-program nil)
257 (tramp-telnet-args nil))
258 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
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)
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)
278 (tramp-telnet-program nil)
279 (tramp-telnet-args nil))
280 ("scp1_old"
281 (tramp-connection-function tramp-open-connection-rsh)
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)
290 (tramp-telnet-program nil)
291 (tramp-telnet-args nil))
292 ("scp2_old"
293 (tramp-connection-function tramp-open-connection-rsh)
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)
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)
313 (tramp-telnet-program nil)
314 (tramp-telnet-args nil))
315 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
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)
324 (tramp-telnet-program nil)
325 (tramp-telnet-args nil))
326 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
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)
335 (tramp-telnet-program nil)
336 (tramp-telnet-args nil))
337 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
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)
346 (tramp-telnet-program nil)
347 (tramp-telnet-args nil))
348 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
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)
357 (tramp-telnet-program nil)
358 (tramp-telnet-args nil))
359 ("ssh1_old"
360 (tramp-connection-function tramp-open-connection-rsh)
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)
369 (tramp-telnet-program nil)
370 (tramp-telnet-args nil))
371 ("ssh2_old"
372 (tramp-connection-function tramp-open-connection-rsh)
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)
381 (tramp-telnet-program nil)
382 (tramp-telnet-args nil))
383 ("telnet"
384 (tramp-connection-function tramp-open-connection-telnet)
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)
393 (tramp-telnet-program "telnet")
394 (tramp-telnet-args nil))
395 ("su" (tramp-connection-function tramp-open-connection-su)
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"))
404 (tramp-telnet-program nil)
405 (tramp-telnet-args nil))
406 ("sudo" (tramp-connection-function tramp-open-connection-su)
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"))
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)
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")
435 (tramp-telnet-program nil)
436 (tramp-telnet-args nil))
437 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
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)
446 (tramp-telnet-program nil)
447 (tramp-telnet-args nil))
448 ("krlogin"
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)
458 (tramp-telnet-program nil)
459 (tramp-telnet-args nil))
460 ("plink"
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)
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)
482 (tramp-telnet-program nil)
483 (tramp-telnet-args nil))
484 ("fcp"
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)
494 (tramp-telnet-program nil)
495 (tramp-telnet-args nil))
496 )
497 "*Alist of methods for remote files.
498 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
499 Each NAME stands for a remote access method. Each PARAM is a
500 pair of the form (KEY VALUE). The following KEYs are defined:
501 * `tramp-connection-function'
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
515 the absolute filename of rsh or the name of a workalike program.
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
524 the absolute filename of rcp or the name of a workalike program.
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.
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
545 What does all this mean? Well, you should specify `tramp-rsh-program',
546 `tramp-telnet-program' or `tramp-su-program' for all methods; this program
547 is used to log in to the remote site. Then, there are two ways to
548 actually transfer the files between the local and the remote side.
549 One way is using an additional rcp-like program. If you want to do
550 this, set `tramp-rcp-program' in the method.
551
552 Another possibility for file transfer is inline transfer, i.e. the
553 file is passed through the same buffer used by `tramp-rsh-program'. In
554 this case, the file contents need to be protected since the
555 `tramp-rsh-program' might use escape codes or the connection might not
556 be eight-bit clean. Therefore, file contents are encoded for transit.
557 See the variable `tramp-coding-commands' for details.
558
559 So, to summarize: if the method is an out-of-band method, then you
560 must specify `tramp-rcp-program' and `tramp-rcp-args'. If it is an
561 inline method, then these two parameters should be nil. Every method,
562 inline or out of band, must specify `tramp-connection-function' plus
563 the associated arguments (for example, the telnet program if you chose
564 `tramp-open-connection-telnet').
565
566 Notes:
567
568 When using `tramp-open-connection-su' the phrase `open connection to a
569 remote host' sounds strange, but it is used nevertheless, for
570 consistency. No connection is opened to a remote host, but `su' is
571 started on the local host. You are not allowed to specify a remote
572 host other than `localhost' or the name of the local host."
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.
604 Each entry in this list should be a method name as mentioned in the
605 variable `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.
616 Each list item is a list of three items (METHOD FUNCTION COMMAND),
617 where METHOD is the name as used in the file name, FUNCTION is the
618 function to be executed, and COMMAND is the shell command used for
619 connecting.
620
621 COMMAND may contain percent escapes. `%u' will be replaced with the
622 user name, `%h' will be replaced with the host name, and `%n' will be
623 replaced with an end-of-line character, as specified in the variable
624 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
625 Note that the interpretation of the percent escapes also depends on
626 the FUNCTION. For example, the `%u' escape is forbidden with the
627 function `tramp-multi-connect-telnet'. See the documentation of the
628 various functions for details."
629 :group 'tramp
630 :type '(repeat (list string function string)))
631
632 (defcustom tramp-default-method "ssh"
633 "*Default method to use for transferring files.
634 See `tramp-methods' for possibilities.
635 Also see `tramp-default-method-alist'."
636 :group 'tramp
637 :type 'string)
638
639 (defcustom tramp-default-method-alist
640 '(("\\`localhost\\'" "\\`root\\'" "su"))
641 "*Default method to use for specific user/host pairs.
642 This is an alist of items (HOST USER METHOD). The first matching item
643 specifies the method to use for a file name which does not specify a
644 method. HOST and USER are regular expressions or nil, which is
645 interpreted as a regular expression which always matches. If no entry
646 matches, the variable `tramp-default-method' takes effect.
647
648 If the file name does not specify the user, lookup is done using the
649 empty string for the user name.
650
651 See `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
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")))
662 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods."
663 )
664
665 ;; Default values for non-Unices seeked
666 (defconst tramp-completion-function-alist-ssh
667 (unless (memq system-type '(windows-nt))
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")))
676 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods."
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")))
683 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods."
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")))
690 "Default list of (FUNCTION FILE) pairs to be examined for su methods."
691 )
692
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)
698 (cons "scp1_old" tramp-completion-function-alist-ssh)
699 (cons "scp2_old" tramp-completion-function-alist-ssh)
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)
705 (cons "ssh1_old" tramp-completion-function-alist-ssh)
706 (cons "ssh2_old" tramp-completion-function-alist-ssh)
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)
716 (cons "fcp" tramp-completion-function-alist-ssh)
717 )
718 "*Alist of methods for remote files.
719 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
720 Each 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
722 names from FILE for completion. The following predefined FUNCTIONs exists:
723
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.
730
731 FUNCTION can also see a customer defined function. For more details see
732 the info pages."
733 :group 'tramp
734 :type '(repeat
735 (cons string
736 (choice (const nil) (repeat (list function file))))))
737
738 (defcustom tramp-rsh-end-of-line "\n"
739 "*String used for end of line in rsh connections.
740 I don't think this ever needs to be changed, so please tell me about it
741 if you need to change this.
742 Also 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.
748 It 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
751 password, see `tramp-rsh-end-of-line' for the other cases.
752
753 The default value is to use the same value as `tramp-rsh-end-of-line'."
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.
762 Please notify me about other semi-standard directories to include here.
763
764 You can use `~' in this list, but when searching for a shell which groks
765 tilde expansion, all directory names starting with `~' will be ignored."
766 :group 'tramp
767 :type '(repeat string))
768
769 (defcustom tramp-login-prompt-regexp
770 ".*ogin: *"
771 "*Regexp matching login-like prompts.
772 The regexp should match at end of buffer."
773 :group 'tramp
774 :type 'regexp)
775
776 (defcustom tramp-shell-prompt-pattern
777 "^[^#$%>\n]*[#$%>] *"
778 "Regexp to match prompts from remote shell.
779 Normally, Tramp expects you to configure `shell-prompt-pattern'
780 correctly, but sometimes it happens that you are connecting to a
781 remote host which sends a different kind of shell prompt. Therefore,
782 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
783 and also things matched by this variable. The default value of this
784 variable is the same as the default value of `shell-prompt-pattern',
785 which should work well in many cases."
786 :group 'tramp
787 :type 'regexp)
788
789 (defcustom tramp-password-prompt-regexp
790 "^.*\\([pP]assword\\|passphrase.*\\):\^@? *"
791 "*Regexp matching password-like prompts.
792 The regexp should match at end of buffer.
793
794 The `sudo' program appears to insert a `^@' character into the prompt."
795 :group 'tramp
796 :type 'regexp)
797
798 (defcustom tramp-wrong-passwd-regexp
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 "\\).*")
815 "*Regexp matching a `login failed' message.
816 The regexp should match at end of buffer."
817 :group 'tramp
818 :type 'regexp)
819
820 (defcustom tramp-yesno-prompt-regexp
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.
825 The confirmation should be done with yes or no.
826 The regexp should match at end of buffer.
827 See also `tramp-yn-prompt-regexp'."
828 :group 'tramp
829 :type 'regexp)
830
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.
835 The confirmation should be done with y or n.
836 The regexp should match at end of buffer.
837 See also `tramp-yesno-prompt-regexp'."
838 :group 'tramp
839 :type 'regexp)
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.
848 The regexp should match at end of buffer.
849 The answer will be provided by `tramp-action-terminal', which see."
850 :group 'tramp
851 :type 'regexp)
852
853 (defcustom tramp-temp-name-prefix "tramp."
854 "*Prefix to use for temporary files.
855 If this is a relative file name (such as \"tramp.\"), it is considered
856 relative to the directory name returned by the function
857 `tramp-temporary-file-directory' (which see). It may also be an
858 absolute file name; don't forget to include a prefix for the filename
859 part, 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.
865 Some shells send such garbage upon connection setup."
866 :group 'tramp
867 :type 'boolean)
868
869 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
870 "*Alist specifying extra arguments to pass to the remote shell.
871 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
872 matching the shell file name and ARGS is a string specifying the
873 arguments.
874
875 This variable is only used when Tramp needs to start up another shell
876 for tilde expansion. The extra arguments should typically prevent the
877 shell from reading its init file."
878 :group 'tramp
879 :type '(alist :key-type string :value-type string))
880
881 (defcustom tramp-prefix-format
882 (if tramp-unified-filenames "/" "/[")
883 "*String matching the very beginning of tramp file names.
884 Used 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.
891 Should 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.
906 Applicable for single-hop methods.
907 Used 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.
914 Applicable for single-hop methods.
915 Derived 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.
922 Applicable for multi-hop methods.
923 Used 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.
930 Applicable for multi-hop methods.
931 Derived from `tramp-postfix-multi-method-format'."
932 :group 'tramp
933 :type 'regexp)
934
935 (defcustom tramp-postfix-multi-hop-format
936 (if tramp-unified-filenames ":" "/")
937 "*String matching delimeter between host and next method.
938 Applicable for multi-hop methods.
939 Used 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)
945 "*Regexp matching delimeter between host and next method.
946 Applicable for multi-hop methods.
947 Derived from `tramp-postfix-multi-hop-format'."
948 :group 'tramp
949 :type 'regexp)
950
951 (defcustom tramp-user-regexp
952 "[^:@/ \t]*"
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.
960 Used 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.
967 Derived from `tramp-postfix-user-format'."
968 :group 'tramp
969 :type 'regexp)
970
971 (defcustom tramp-host-regexp
972 "[a-zA-Z0-9_.-]*"
973 "*Regexp matching host names."
974 :group 'tramp
975 :type 'regexp)
976
977 (defcustom tramp-host-with-port-regexp
978 "[a-zA-Z0-9_.#-]*"
979 "*Regexp matching host names."
980 :group 'tramp
981 :type 'regexp)
982
983 (defcustom tramp-postfix-host-format
984 (if tramp-unified-filenames ":" "]")
985 "*String matching delimeter between host names and localnames.
986 Used 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)
992 "*Regexp matching delimeter between host names and localnames.
993 Derived from `tramp-postfix-host-format'."
994 :group 'tramp
995 :type 'regexp)
996
997 (defcustom tramp-localname-regexp
998 ".*$"
999 "*Regexp matching localnames."
1000 :group 'tramp
1001 :type 'regexp)
1002
1003 ;; File name format.
1004
1005 (defcustom tramp-file-name-structure
1006 (list
1007 (concat
1008 tramp-prefix-regexp
1009 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1010 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1011 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1012 "\\(" tramp-localname-regexp "\\)")
1013 2 4 5 6)
1014
1015 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1016 the tramp file name structure.
1017
1018 The first element REGEXP is a regular expression matching a tramp file
1019 name. The regex should contain parentheses around the method name,
1020 the user name, the host name, and the file name parts.
1021
1022 The second element METHOD is a number, saying which pair of
1023 parentheses matches the method name. The third element USER is
1024 similar, but for the user name. The fourth element HOST is similar,
1025 but for the host name. The fifth element FILE is for the file name.
1026 These numbers are passed directly to `match-string', which see. That
1027 means the opening parentheses are counted to identify the pair.
1028
1029 See also `tramp-file-name-regexp'."
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
1038 (defconst tramp-file-name-regexp-unified
1039 "\\`/[^/:]+:"
1040 "Value for `tramp-file-name-regexp' for unified remoting.
1041 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1042 Tramp. 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.
1048 XEmacs uses a separate filename syntax for Tramp and EFS.
1049 See `tramp-file-name-structure-separate' for more explanations.")
1050
1051 ;;;###autoload
1052 (defcustom tramp-file-name-regexp
1053 (if tramp-unified-filenames
1054 tramp-file-name-regexp-unified
1055 tramp-file-name-regexp-separate)
1056 "*Regular expression matching file names handled by tramp.
1057 This 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,
1060 if the tramp entry appears rather early in the `file-name-handler-alist'
1061 and is a bit too general, then some files might be considered tramp
1062 files which are not really tramp files.
1063
1064 Please note that the entry in `file-name-handler-alist' is made when
1065 this file (tramp.el) is loaded. This means that this variable must be set
1066 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1067 updated after changing this variable.
1068
1069 Also see `tramp-file-name-structure'."
1070 :group 'tramp
1071 :type 'regexp)
1072
1073 ;;;###autoload
1074 (defconst tramp-completion-file-name-regexp-unified
1075 "^/[^/]*$"
1076 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1077 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1078 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1079
1080 ;;;###autoload
1081 (defconst tramp-completion-file-name-regexp-separate
1082 "^/\\([[][^]]*\\)?$"
1083 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1084 XEmacs uses a separate filename syntax for Tramp and EFS.
1085 See `tramp-file-name-structure-separate' for more explanations.")
1086
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.
1093 This regexp should match partial tramp file names only.
1094
1095 Please note that the entry in `file-name-handler-alist' is made when
1096 this file (tramp.el) is loaded. This means that this variable must be set
1097 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1098 updated after changing this variable.
1099
1100 Also see `tramp-file-name-structure'."
1101 :group 'tramp
1102 :type 'regexp)
1103
1104 (defcustom tramp-multi-file-name-structure
1105 (list
1106 (concat
1107 tramp-prefix-regexp
1108 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1109 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1110 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1111 2 3 -1)
1112 "*Describes the file name structure of `multi' files.
1113 Multi files allow you to contact a remote host in several hops.
1114 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1115
1116 The first element, REGEXP, gives a regular expression to match against
1117 the file name. In this regular expression, `%s' is replaced with the
1118 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1119 allow multiple hops, you normally want to use something like
1120 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1121 of parentheses is used for the HOP element, see below.)
1122
1123 All remaining elements are numbers. METHOD gives the number of the
1124 paren pair which matches the method name. HOP gives the number of the
1125 paren pair which matches the hop sequence. LOCALNAME gives the number of
1126 the paren pair which matches the localname (pathname) on the remote host.
1127
1128 LOCALNAME can also be negative, which means to count from the end. Ie, a
1129 value of -1 means the last paren pair.
1130
1131 I think it would be good if the regexp matches the whole of the
1132 string, 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")
1137 (integer :tag "Paren pair to match localname")))
1138
1139 (defcustom tramp-multi-file-name-hop-structure
1140 (list
1141 (concat
1142 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1143 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1144 "\\(" tramp-host-with-port-regexp "\\)")
1145 1 2 3)
1146 "*Describes the structure of a hop in multi files.
1147 This is a list of four elements (REGEXP METHOD USER HOST). First
1148 element REGEXP is used to match against the hop. Pair number METHOD
1149 matches the method of one hop, pair number USER matches the user of
1150 one hop, pair number HOST matches the host of one hop.
1151
1152 This 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
1159 (defcustom tramp-make-multi-tramp-file-format
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"))
1167 "*Describes how to construct a `multi' file name.
1168 This is a list of three elements PREFIX, HOP and LOCALNAME.
1169
1170 The first element PREFIX says how to construct the prefix, the second
1171 element HOP specifies what each hop looks like, and the final element
1172 LOCALNAME says how to construct the localname (pathname).
1173
1174 In PREFIX, `%%' means `%' and `%m' means the method name.
1175
1176 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1177 user and hop host, respectively.
1178
1179 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1180
1181 The resulting file name always contains one copy of PREFIX and one
1182 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1183 name.
1184
1185 Note: the current implementation requires the prefix to contain the
1186 method name, followed by all the hops, and the localname must come
1187 last."
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.
1193 Because Tramp wants to parse the output of the remote shell, it is easily
1194 confused by ANSI color escape sequences and suchlike. Often, shell init
1195 files 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.
1201 Using shell wildcards for completions has the advantage that it can be
1202 fast even in large directories, but completion is always
1203 case-sensitive. Relying on Lisp only means that case-insensitive
1204 completion is possible (subject to the variable `completion-ignore-case'),
1205 but it might be slow on large directories."
1206 :group 'tramp
1207 :type 'boolean)
1208
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)
1213 (tramp-shell-prompt-pattern tramp-action-succeed)
1214 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1215 (tramp-yesno-prompt-regexp tramp-action-yesno)
1216 (tramp-yn-prompt-regexp tramp-action-yn)
1217 (tramp-terminal-prompt-regexp tramp-action-terminal))
1218 "List of pattern/action pairs.
1219 Whenever a pattern matches, the corresponding action is performed.
1220 Each item looks like (PATTERN ACTION).
1221
1222 The PATTERN should be a symbol, a variable. The value of this
1223 variable gives the regular expression to search for. Note that the
1224 regexp must match at the end of the buffer, \"\\'\" is implicitly
1225 appended to it.
1226
1227 The ACTION should also be a symbol, but a function. When the
1228 corresponding 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)
1236 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1237 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied))
1238 "List of pattern/action pairs.
1239 This list is used for each hop in multi-hop connections.
1240 See `tramp-actions-before-shell' for more info."
1241 :group 'tramp
1242 :type '(repeat (list variable function)))
1243
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.
1252 It is necessary only on systems which have a buggy `process-send-string'
1253 implementation. The necessity, whether this variable must be set, can be
1254 checked 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
1267 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1268 this variable to be set as well."
1269 :group 'tramp
1270 :type '(choice (const nil) integer))
1271
1272 ;;; Internal Variables:
1273
1274 (defvar tramp-buffer-file-attributes nil
1275 "Holds the `ls -ild' output for the current buffer.
1276 This variable is local to each buffer. It is not used if the remote
1277 machine groks Perl. If it is used, it's used as an emulation for
1278 the visited file modtime.")
1279 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1280
1281 (defvar tramp-md5-function
1282 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1283 ((fboundp 'md5-encode)
1284 (lambda (x) (base64-encode-string (md5-encode x))))
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 )))
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'.
1310 In the connection buffer, this variable has the value of the like-named
1311 method 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'.
1315 In the connection buffer, this variable has the value of the like-named
1316 method 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'.
1320 In the connection buffer, this variable has the value of the like-named
1321 method 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'.
1325 In the connection buffer, this variable has the value of the like-named
1326 method 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'.
1330 In the connection buffer, this variable has the value of the like-named
1331 method 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'.
1335 In the connection buffer, this variable has the value of the like-named
1336 method 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'.
1340 In the connection buffer, this variable has the value of the like-named
1341 method 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'.
1345 In the connection buffer, this variable has the value of the like-named
1346 method 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'.
1350 In the connection buffer, this variable has the value of the like-named
1351 method 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'.
1355 In the connection buffer, this variable has the value of the like-named
1356 method 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'.
1360 In the connection buffer, this variable has the value of the like-named
1361 method 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'.
1365 In the connection buffer, this variable has the value of the like-named
1366 method 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'.
1370 In the connection buffer, this variable has the value of the like-named
1371 method parameter, as specified in `tramp-methods' (which see).")
1372
1373 (defvar tramp-su-program nil
1374 "This internal variable holds a parameter for `tramp-methods'.
1375 In the connection buffer, this variable has the value of the like-named
1376 method parameter, as specified in `tramp-methods' (which see).")
1377
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.
1381 This variable is automatically made buffer-local to each rsh process buffer
1382 upon 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.
1386 This variable is automatically made buffer-local to each rsh process buffer
1387 upon opening the connection.")
1388
1389 (defvar tramp-current-method nil
1390 "Connection method for this *tramp* buffer.
1391 This variable is automatically made buffer-local to each rsh process buffer
1392 upon opening the connection.")
1393
1394 (defvar tramp-current-user nil
1395 "Remote login name for this *tramp* buffer.
1396 This variable is automatically made buffer-local to each rsh process buffer
1397 upon opening the connection.")
1398
1399 (defvar tramp-current-host nil
1400 "Remote host for this *tramp* buffer.
1401 This variable is automatically made buffer-local to each rsh process buffer
1402 upon 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.)
1407 This variable is automatically made buffer-local to each rsh process buffer
1408 upon opening the connection.")
1409
1410 (defvar tramp-file-exists-command nil
1411 "Command to use for checking if a file exists.
1412 This variable is automatically made buffer-local to each rsh process buffer
1413 upon opening the connection.")
1414
1415 (defconst tramp-uudecode "\
1416 tramp_uudecode () {
1417 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1418 cat /tmp/tramp.$$
1419 rm -f /tmp/tramp.$$
1420 }"
1421 "Shell function to implement `uudecode' to standard output.
1422 Many systems support `uudecode -o -' for this or `uudecode -p', but
1423 some 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.
1429 ;; The device number is returned as "-1", because there will be a virtual
1430 ;; device number set in `tramp-handle-file-attributes'
1431 (defconst tramp-perl-file-attributes "\
1432 $f = $ARGV[0];
1433 @s = lstat($f);
1434 if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; }
1435 elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; }
1436 else { $l = \"nil\" };
1437 printf(\"(%s %u %d %d (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
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,
1440 $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff);"
1441 "Perl script to produce output suitable for use with `file-attributes'
1442 on the remote file system.")
1443
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.
1465 (defvar tramp-perl-encode-with-module
1466 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1467 "Perl program to use for encoding a file.
1468 Escape sequence %s is replaced with name of Perl binary.
1469 This string is passed to `format', so percent characters need to be doubled.
1470 This implementation requires the MIME::Base64 Perl module to be installed
1471 on 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.
1476 Escape sequence %s is replaced with name of Perl binary.
1477 This string is passed to `format', so percent characters need to be doubled.
1478 This implementation requires the MIME::Base64 Perl module to be installed
1479 on 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.
1485 use strict;
1486
1487 my %%trans = do {
1488 my $i = 0;
1489 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1490 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1491 };
1492
1493 binmode(\\*STDIN);
1494
1495 # We read in chunks of 54 bytes, to generate output lines
1496 # of 72 chars (plus end of line)
1497 $/ = \\54;
1498
1499 while (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) {
1504 my $mod = length($data) %% 3;
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,
1516 qq(\\n);
1517 }
1518 '"
1519 "Perl program to use for encoding a file.
1520 Escape sequence %s is replaced with name of Perl binary.
1521 This string is passed to `format', so percent characters need to be doubled.")
1522
1523 (defvar tramp-perl-decode
1524 "%s -e '
1525 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1526 # Copyright (C) 2002 Free Software Foundation, Inc.
1527 use strict;
1528
1529 my %%trans = do {
1530 my $i = 0;
1531 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1532 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1533 };
1534
1535 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1536
1537 binmode(\\*STDOUT);
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)
1541 my $pending = q();
1542
1543 while (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);
1552 my $chunk = substr($pending, 0, $len & ~3);
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 '"
1563 "Perl program to use for decoding a file.
1564 Escape sequence %s is replaced with name of Perl binary.
1565 This string is passed to `format', so percent characters need to be doubled.")
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.
1585 This 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
1594 (defvar tramp-last-cmd-time nil
1595 "Internal Tramp variable recording the time when the last cmd was sent.
1596 This variable is buffer-local in every buffer.")
1597 (make-variable-buffer-local 'tramp-last-cmd-time)
1598
1599 ;; This variable does not have the right value in XEmacs. What should
1600 ;; I use instead of find-operation-coding-system in XEmacs?
1601 (defvar tramp-feature-write-region-fix
1602 (when (fboundp 'find-operation-coding-system)
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.
1606 Older versions of Emacs chose the coding system for `write-region' based
1607 on the FILENAME argument, even if VISIT was a string.")
1608
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.
1657 Operations not mentioned here will be handled by the normal Emacs functions.")
1658
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.
1671 Used for file names matching `tramp-file-name-regexp'. Operations not
1672 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1673 normal Emacs functions.")
1674
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.
1678 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1679 calling HANDLER.")
1680
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.
1685 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1686 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1687 Calls function `message' with FMT-STRING as control string and the remaining
1688 ARGS to actually emit the message (if applicable).
1689
1690 This 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.
1707 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1708 remaining 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.
1715 Calls `line-end-position' or `point-at-eol' if defined, else
1716 own 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
1722 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1723 "Parse a Tramp filename and make components available in the body.
1724
1725 First arg FILENAME is evaluated and dissected into its components.
1726 Second arg VAR is a symbol. It is used as a variable name to hold
1727 the filename structure. It is also used as a prefix for the variables
1728 holding the components. For example, if VAR is the symbol `foo', then
1729 `foo' will be bound to the whole structure, `foo-multi-method' will
1730 be bound to the multi-method component, and so on for `foo-method',
1731 `foo-user', `foo-host', `foo-localname'.
1732
1733 Remaining args are Lisp expressions to be evaluated (inside an implicit
1734 `progn').
1735
1736 If VAR is nil, then we bind `v' to the structure and `multi-method',
1737 `method', `user', `host', `localname' to the components."
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)))
1747 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1748 (tramp-file-name-localname ,(or var 'v))))
1749 ,@body))
1750
1751 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1752
1753 ;;; Config Manipulation Functions:
1754
1755 (defun tramp-set-completion-function (method function-list)
1756 "Sets the list of completion functions for METHOD.
1757 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1758 The FUNCTION is intended to parse FILE according its syntax.
1759 It might be a predefined FUNCTION, or a user defined FUNCTION.
1760 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
1761 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
1762 and `tramp-parse-netrc'.
1763
1764 Example:
1765
1766 (tramp-set-completion-function
1767 \"ssh\"
1768 '((tramp-parse-sconfig \"/etc/ssh_config\")
1769 (tramp-parse-sconfig \"~/.ssh/config\")))"
1770
1771 (let ((v (cdr (assoc method tramp-completion-function-alist))))
1772 (if v (setcdr v function-list)
1773 (add-to-list 'tramp-completion-function-alist
1774 (cons method function-list)))))
1775
1776 (defun tramp-get-completion-function (method)
1777 "Returns list of completion functions for METHOD.
1778 For definition of that list see `tramp-set-completion-function'."
1779 (cdr (assoc method tramp-completion-function-alist)))
1780
1781 ;;; File Name Handler Functions:
1782
1783 (defun tramp-handle-make-symbolic-link
1784 (filename linkname &optional ok-if-already-exists)
1785 "Like `make-symbolic-link' for tramp files.
1786 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1787 the symlink. If LINKNAME is a Tramp file, only the localname component is
1788 used as the target of the symlink.
1789
1790 If LINKNAME is a Tramp file and the localname component is relative, then
1791 it is expanded first, before the localname component is taken. Note that
1792 this can give surprising results if the user/host for the source and
1793 target of the symlink differ."
1794 (with-parsed-tramp-file-name linkname l
1795 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
1796 (cwd (file-name-directory l-localname)))
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.
1803 (when (file-exists-p linkname)
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? "
1810 l-localname)))))
1811 (signal 'file-already-exists (list "File already exists" l-localname))
1812 (delete-file linkname)))
1813
1814 ;; If FILENAME is a Tramp name, use just the localname component.
1815 (when (tramp-tramp-file-p filename)
1816 (setq filename (tramp-file-name-localname
1817 (tramp-dissect-file-name
1818 (expand-file-name filename)))))
1819
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
1825 l-multi-method l-method l-user l-host
1826 (format "cd %s && %s -sf %s %s"
1827 cwd ln
1828 filename
1829 l-localname)
1830 t)))))
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)
1836 (error "Tramp cannot `load' files without absolute file name"))
1837 (with-parsed-tramp-file-name file nil
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)))
1864
1865 ;; Localname manipulation functions that grok TRAMP localnames...
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
1869 (with-parsed-tramp-file-name file nil
1870 ;; For the following condition, two possibilities should be tried:
1871 ;; (1) (string= localname "")
1872 ;; (2) (or (string= localname "") (string= localname "/"))
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.
1877 (if (string= localname "")
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 "/"
1884 ;; run the command on the localname portion only
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...
1889 (or (file-name-directory localname) "")))))
1890
1891 (defun tramp-handle-file-name-nondirectory (file)
1892 "Like `file-name-nondirectory' but aware of TRAMP files."
1893 (with-parsed-tramp-file-name file nil
1894 (file-name-nondirectory localname)))
1895
1896 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
1897 "Like `file-truename' for tramp files."
1898 (with-parsed-tramp-file-name filename nil
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))
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
1911 symlink-target)
1912 (tramp-message-for-buffer
1913 multi-method method user host
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
1928 (append '("")
1929 (reverse result)
1930 (list thisstep))
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))
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 "/")))
1963 (tramp-message-for-buffer
1964 multi-method method user host
1965 10 "True name of `%s' is `%s'" filename result)
1966 (tramp-make-tramp-file-name
1967 multi-method method user host result))))
1968
1969 ;; Basic functions.
1970
1971 (defun tramp-handle-file-exists-p (filename)
1972 "Like `file-exists-p' for tramp files."
1973 (with-parsed-tramp-file-name filename nil
1974 (save-excursion
1975 (zerop (tramp-send-command-and-check
1976 multi-method method user host
1977 (format
1978 (tramp-get-file-exists-command multi-method method user host)
1979 (tramp-shell-quote-argument localname)))))))
1980
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
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.
1994 Optional argument NONNUMERIC means return user and group name
1995 rather than as numbers."
1996 (let (result)
1997 (with-parsed-tramp-file-name filename nil
1998 (when (tramp-handle-file-exists-p filename)
1999 ;; file exists, find out stuff
2000 (save-excursion
2001 (if (tramp-get-remote-perl multi-method method user host)
2002 (setq result
2003 (tramp-handle-file-attributes-with-perl
2004 multi-method method user host localname nonnumeric))
2005 (setq result
2006 (tramp-handle-file-attributes-with-ls
2007 multi-method method user host localname nonnumeric)))
2008 ;; set virtual device number
2009 (setcar (nthcdr 11 result)
2010 (tramp-get-device multi-method method user host)))))
2011 result))
2012
2013 (defun tramp-handle-file-attributes-with-ls
2014 (multi-method method user host localname &optional nonnumeric)
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)
2019 (tramp-message-for-buffer multi-method method user host 10
2020 "file attributes with ls: %s"
2021 (tramp-make-tramp-file-name
2022 multi-method method user host localname))
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")
2028 (tramp-shell-quote-argument localname)))
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
2094 ;; 11. Device number. Will be replaced by a virtual device number.
2095 -1
2096 )))
2097
2098 (defun tramp-handle-file-attributes-with-perl
2099 (multi-method method user host localname &optional nonnumeric)
2100 "Implement `file-attributes' for tramp files using a Perl script.
2101
2102 The Perl command is sent to the remote machine when the connection
2103 is initially created and is kept cached by the remote shell."
2104 (tramp-message-for-buffer multi-method method user host 10
2105 "file attributes with perl: %s"
2106 (tramp-make-tramp-file-name
2107 multi-method method user host localname))
2108 (tramp-send-command
2109 multi-method method user host
2110 (format "tramp_file_attributes %s"
2111 (tramp-shell-quote-argument localname)))
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
2118 (defun tramp-get-device (multi-method method user host)
2119 "Returns the virtual device number.
2120 If 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
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)))
2134 (let ((f (buffer-file-name))
2135 (coding-system-used nil))
2136 (with-parsed-tramp-file-name f nil
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))
2143 (if (not (equal modtime '(0 0)))
2144 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
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)
2150 (tramp-shell-quote-argument localname)))
2151 (tramp-wait-for-output)
2152 (setq attr (buffer-substring (point)
2153 (progn (end-of-line) (point)))))
2154 (setq tramp-buffer-file-attributes attr))
2155 (when (boundp 'last-coding-system-used)
2156 (setq last-coding-system-used coding-system-used))
2157 nil))))
2158
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
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)
2184 (tramp-shell-quote-argument localname)))
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.
2190 (t nil)))))))
2191
2192 (defadvice clear-visited-file-modtime (after tramp activate)
2193 "Set `tramp-buffer-file-attributes' back to nil.
2194 Tramp uses this variable as an emulation for the actual modtime of the file,
2195 if 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."
2200 (with-parsed-tramp-file-name filename nil
2201 (save-excursion
2202 (unless (zerop (tramp-send-command-and-check
2203 multi-method method user host
2204 (format "chmod %s %s"
2205 (tramp-decimal-to-octal mode)
2206 (tramp-shell-quote-argument localname))))
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."
2217 (with-parsed-tramp-file-name filename nil
2218 (zerop (tramp-run-test "-x" filename))))
2219
2220 (defun tramp-handle-file-readable-p (filename)
2221 "Like `file-readable-p' for tramp files."
2222 (with-parsed-tramp-file-name filename nil
2223 (zerop (tramp-run-test "-r" filename))))
2224
2225 (defun tramp-handle-file-accessible-directory-p (filename)
2226 "Like `file-accessible-directory-p' for tramp files."
2227 (with-parsed-tramp-file-name filename nil
2228 (and (zerop (tramp-run-test "-d" filename))
2229 (zerop (tramp-run-test "-r" filename))
2230 (zerop (tramp-run-test "-x" filename)))))
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)
2243 ;; We are sure both files exist at this point.
2244 (t
2245 (save-excursion
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))))
2253 (> 0 (car (tramp-time-diff (nth 5 fa1) (nth 5 fa2))))
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.
2259 (unless (and (tramp-tramp-file-p file1)
2260 (tramp-tramp-file-p file2))
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
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)))))))))))
2285
2286 ;; Functions implemented using the basic functions above.
2287
2288 (defun tramp-handle-file-modes (filename)
2289 "Like `file-modes' for tramp files."
2290 (with-parsed-tramp-file-name filename nil
2291 (when (file-exists-p filename)
2292 (tramp-mode-string-to-int
2293 (nth 8 (file-attributes filename))))))
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'
2305 (with-parsed-tramp-file-name filename nil
2306 (save-excursion
2307 (zerop
2308 (tramp-send-command-and-check
2309 multi-method method user host
2310 (format "test -d %s"
2311 (tramp-shell-quote-argument localname))
2312 t))))) ;run command in subshell
2313
2314 (defun tramp-handle-file-regular-p (filename)
2315 "Like `file-regular-p' for tramp files."
2316 (with-parsed-tramp-file-name filename nil
2317 (and (file-exists-p filename)
2318 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2319
2320 (defun tramp-handle-file-symlink-p (filename)
2321 "Like `file-symlink-p' for tramp files."
2322 (with-parsed-tramp-file-name filename nil
2323 (let ((x (car (tramp-handle-file-attributes filename))))
2324 (when (stringp x) x))))
2325
2326 (defun tramp-handle-file-writable-p (filename)
2327 "Like `file-writable-p' for tramp files."
2328 (with-parsed-tramp-file-name filename nil
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)))))))
2337
2338 (defun tramp-handle-file-ownership-preserved-p (filename)
2339 "Like `file-ownership-preserved-p' for tramp files."
2340 (with-parsed-tramp-file-name filename nil
2341 (or (not (tramp-handle-file-exists-p filename))
2342 ;; Existing files must be writable.
2343 (zerop (tramp-run-test "-O" filename)))))
2344
2345 ;; Other file name ops.
2346
2347 ;; ;; Matthias Köppe <mkoeppe@mail.math.uni-magdeburg.de>
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
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
2367 (defun tramp-handle-directory-file-name (directory)
2368 "Like `directory-file-name' for tramp files."
2369 ;; If localname component of filename is "/", leave it unchanged.
2370 ;; Otherwise, remove any trailing slash from localname component.
2371 ;; Method, host, etc, are unchanged. Does it make sense to try
2372 ;; to avoid parsing the filename?
2373 (with-parsed-tramp-file-name directory nil
2374 (if (and (not (zerop (length localname)))
2375 (eq (aref localname (1- (length localname))) ?/)
2376 (not (string= localname "/")))
2377 (substring directory 0 -1)
2378 directory)))
2379
2380 ;; Directory listings.
2381
2382 (defun tramp-handle-directory-files (directory
2383 &optional full match nosort files-only)
2384 "Like `directory-files' for tramp files."
2385 (with-parsed-tramp-file-name directory nil
2386 (let (result x)
2387 (save-excursion
2388 (tramp-barf-unless-okay
2389 multi-method method user host
2390 (concat "cd " (tramp-shell-quote-argument localname))
2391 nil
2392 'file-error
2393 "tramp-handle-directory-files: couldn't `cd %s'"
2394 (tramp-shell-quote-argument localname))
2395 (tramp-send-command
2396 multi-method method user host
2397 (concat (tramp-get-ls-command multi-method method user host)
2398 " -a | cat"))
2399 (tramp-wait-for-output)
2400 (goto-char (point-max))
2401 (while (zerop (forward-line -1))
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))))
2410 (tramp-send-command multi-method method user host "cd")
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)))))))
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
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
2446 (format "cd %s" (tramp-shell-quote-argument localname))
2447 nil 'file-error
2448 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2449 (tramp-shell-quote-argument localname))
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))
2472
2473 (tramp-send-command multi-method method user host "cd")
2474 (tramp-wait-for-output)
2475
2476 ;; Return the list.
2477 (if nowild
2478 (all-completions filename (mapcar 'list result))
2479 result))))))
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))
2489 (with-parsed-tramp-file-name directory nil
2490 (try-completion
2491 filename
2492 (mapcar (lambda (x) (cons x nil))
2493 (file-name-all-completions filename directory)))))
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."
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"))
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
2521 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2522 (tramp-shell-quote-argument v2-localname))
2523 nil 'file-error
2524 "error with add-name-to-file, see buffer `%s' for details"
2525 (buffer-name))))))
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.
2561 OP must be `copy' or `rename' and indicates the operation to perform.
2562 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2563 the new file (for copy) or the new name of the file (for rename).
2564 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2565 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2566 as FILENAME.
2567
2568 This function is invoked by `tramp-handle-copy-file' and
2569 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2570 and `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))))
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
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
2595 v1-localname v2-localname keep-date)
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)))))
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?
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.
2623 First arg OP is either `copy' or `rename' and indicates the operation.
2624 FILENAME is the source file, NEWNAME the target file.
2625 KEEP-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))))
2639
2640 (defun tramp-do-copy-or-rename-file-directly
2641 (op multi-method method user host localname1 localname2 keep-date)
2642 "Invokes `cp' or `mv' on the remote system.
2643 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2644 respectively. METHOD, USER, and HOST specify the connection.
2645 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
2646 If 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
2659 (tramp-shell-quote-argument localname1)
2660 (tramp-shell-quote-argument localname2))
2661 nil 'file-error
2662 "Copying directly failed, see buffer `%s' for details."
2663 (buffer-name)))))
2664
2665 (defun tramp-do-copy-or-rename-file-one-local
2666 (op filename newname keep-date)
2667 "Invoke rcp program to copy.
2668 One of FILENAME and NEWNAME must be a Tramp name, the other must
2669 be a local filename. The method used must be an out-of-band method."
2670 ;; CCC
2671 )
2672
2673 ;; mkdir
2674 (defun tramp-handle-make-directory (dir &optional parents)
2675 "Like `make-directory' for tramp files."
2676 (setq dir (expand-file-name dir))
2677 (with-parsed-tramp-file-name dir nil
2678 (save-excursion
2679 (tramp-barf-unless-okay
2680 multi-method method user host
2681 (format " %s %s"
2682 (if parents "mkdir -p" "mkdir")
2683 (tramp-shell-quote-argument localname))
2684 nil 'file-error
2685 "Couldn't make directory %s" dir))))
2686
2687 ;; CCC error checking?
2688 (defun tramp-handle-delete-directory (directory)
2689 "Like `delete-directory' for tramp files."
2690 (setq directory (expand-file-name directory))
2691 (with-parsed-tramp-file-name directory nil
2692 (save-excursion
2693 (tramp-send-command
2694 multi-method method user host
2695 (format "rmdir %s ; echo ok"
2696 (tramp-shell-quote-argument localname)))
2697 (tramp-wait-for-output))))
2698
2699 (defun tramp-handle-delete-file (filename)
2700 "Like `delete-file' for tramp files."
2701 (setq filename (expand-file-name filename))
2702 (with-parsed-tramp-file-name filename nil
2703 (save-excursion
2704 (unless (zerop (tramp-send-command-and-check
2705 multi-method method user host
2706 (format "rm -f %s"
2707 (tramp-shell-quote-argument localname))))
2708 (signal 'file-error "Couldn't delete Tramp file")))))
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.
2716 This is like `dired-recursive-delete-directory' for tramp files."
2717 (with-parsed-tramp-file-name filename nil
2718 ;; run a shell command 'rm -r <localname>'
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>)
2725 (tramp-send-command multi-method method user host
2726 (format "rm -r %s" (tramp-shell-quote-argument localname)))
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))))
2733
2734
2735 (defun tramp-handle-dired-call-process (program discard &rest arguments)
2736 "Like `dired-call-process' for tramp files."
2737 (with-parsed-tramp-file-name default-directory nil
2738 (save-excursion
2739 (tramp-barf-unless-okay
2740 multi-method method user host
2741 (format "cd %s" (tramp-shell-quote-argument localname))
2742 nil 'file-error
2743 "tramp-handle-dired-call-process: Couldn't `cd %s'"
2744 (tramp-shell-quote-argument localname))
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."
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)))
2778 (setq filename (expand-file-name filename))
2779 (with-parsed-tramp-file-name filename nil
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
2786 (setq wildcard (file-name-nondirectory localname))
2787 (setq localname (file-name-directory localname)))
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
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
2804 localname
2805 (tramp-shell-quote-argument (concat localname ".")))))
2806 (tramp-barf-unless-okay
2807 multi-method method user host
2808 (format "cd %s" (tramp-shell-quote-argument
2809 (file-name-directory localname)))
2810 nil 'file-error
2811 "Couldn't `cd %s'"
2812 (tramp-shell-quote-argument (file-name-directory localname)))
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
2822 (file-name-nondirectory localname)) ".")
2823 (file-name-nondirectory localname)))))
2824 (sit-for 1) ;needed for rsh but not ssh?
2825 (tramp-wait-for-output))
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.
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))
2839 (save-excursion
2840 (tramp-send-command multi-method method user host "cd")
2841 (tramp-wait-for-output))
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 )))
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."
2861 (with-parsed-tramp-file-name filename nil
2862 (expand-file-name "~/")))
2863
2864 ;; Canonicalization of file names.
2865
2866 (defun tramp-drop-volume-letter (name)
2867 "Cut off unnecessary drive letter from file NAME.
2868 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2869 locally on a remote file name. When the local system is a W32 system
2870 but the remote system is Unix, this introduces a superfluous drive
2871 letter into the file name. This function removes it.
2872
2873 Doesn'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)
2883 "Like `expand-file-name' for tramp files.
2884 If the localname part of the given filename starts with \"/../\" then
2885 the result will be a local, non-Tramp, filename."
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.
2896 (with-parsed-tramp-file-name name nil
2897 (unless (file-name-absolute-p localname)
2898 (setq localname (concat "~/" localname)))
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.
2904 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2905 (let ((uname (match-string 1 localname))
2906 (fname (match-string 2 localname)))
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)))
2917 (setq localname (concat uname fname))
2918 (erase-buffer)))
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))))))))))
2946
2947 ;; Remote commands.
2948
2949 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
2950 "Like `shell-command' for tramp files.
2951 This will break if COMMAND prints a newline, followed by the value of
2952 `tramp-end-of-output', followed by another newline."
2953 (if (tramp-tramp-file-p default-directory)
2954 (with-parsed-tramp-file-name default-directory nil
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
2963 (format "cd %s" (tramp-shell-quote-argument localname))
2964 nil 'file-error
2965 "tramp-handle-shell-command: Couldn't `cd %s'"
2966 (tramp-shell-quote-argument localname))
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)))
2978 (set-buffer output-buffer)
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))
2994 (display-buffer output-buffer))
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))))
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."
3012 (with-parsed-tramp-file-name filename nil
3013 (let ((output-buf (get-buffer-create "*tramp output*"))
3014 (tramp-buf (tramp-get-buffer multi-method method user host))
3015 (rcp-program (tramp-get-rcp-program
3016 multi-method
3017 (tramp-find-method multi-method method user host)
3018 user host))
3019 (rcp-args (tramp-get-rcp-args
3020 multi-method
3021 (tramp-find-method multi-method method user host)
3022 user host))
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))
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))
3038 (cond (rcp-program
3039 ;; Use rcp-like program for file transfer.
3040 (tramp-message-for-buffer
3041 multi-method method user host
3042 5 "Fetching %s to tmp file %s..." filename tmpfil)
3043 (save-excursion (set-buffer output-buf) (erase-buffer))
3044 (unless (equal
3045 0
3046 (apply #'call-process
3047 rcp-program
3048 nil output-buf nil
3049 (append rcp-args
3050 (list
3051 (tramp-make-rcp-program-file-name
3052 user host
3053 (tramp-shell-quote-argument localname))
3054 tmpfil))))
3055 (pop-to-buffer output-buf)
3056 (error
3057 (concat "tramp-handle-file-local-copy: `%s' didn't work, "
3058 "see buffer `%s' for details")
3059 rcp-program output-buf))
3060 (tramp-message-for-buffer
3061 multi-method method user host
3062 5 "Fetching %s to tmp file %s...done" filename tmpfil))
3063 ((and rem-enc rem-dec)
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.
3068 (set-buffer tramp-buf)
3069 (tramp-message 5 "Encoding remote file %s..." filename)
3070 (tramp-barf-unless-okay
3071 multi-method method user host
3072 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3073 nil 'file-error
3074 "Encoding remote file failed, see buffer `%s' for details"
3075 tramp-buf)
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)
3081
3082 ;; Here is where loc-enc and loc-dec used to be let-bound.
3083 (if (and (symbolp loc-dec) (fboundp loc-dec))
3084 ;; If local decoding is a function, we call it.
3085 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3086 (set-buffer tmpbuf)
3087 (erase-buffer)
3088 (insert-buffer tramp-buf)
3089 (tramp-message-for-buffer
3090 multi-method method user host
3091 6 "Decoding remote file %s with function %s..."
3092 filename loc-dec)
3093 (set-buffer tmpbuf)
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.
3099 (let ((coding-system-for-write 'no-conversion))
3100 (funcall loc-dec (point-min) (point-max))
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..."
3109 filename loc-dec)
3110 (tramp-call-local-coding-command
3111 loc-dec tmpfil2 tmpfil)
3112 (delete-file tmpfil2)))
3113 (tramp-message-for-buffer
3114 multi-method method user host
3115 5 "Decoding remote file %s...done" filename)))
3116
3117 (t (error "Wrong method specification for `%s'" method)))
3118 tmpfil)))
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))
3126 (with-parsed-tramp-file-name filename nil
3127 (if (not (file-exists-p filename))
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))
3135 (list (expand-file-name filename) 0))
3136 (let ((local-copy (file-local-copy filename))
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)
3146 (setq result (insert-file-contents local-copy nil beg end replace))
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))
3150 (tramp-message-for-buffer
3151 multi-method method user host
3152 9 "Inserting local temp file `%s'...done" local-copy)
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))
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"))
3175 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
3176 (when (and (not (featurep 'xemacs))
3177 confirm (file-exists-p filename))
3178 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
3179 filename))
3180 (error "File not overwritten")))
3181 (with-parsed-tramp-file-name filename nil
3182 (let ((curbuf (current-buffer))
3183 (rcp-program (tramp-get-rcp-program
3184 multi-method (tramp-find-method multi-method method user host)
3185 user host))
3186 (rcp-args (tramp-get-rcp-args
3187 multi-method
3188 (tramp-find-method multi-method method user host)
3189 user host))
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))
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
3231 (tramp-shell-quote-argument localname))))))
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)))
3255 ((and rem-enc rem-dec)
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.
3265 (if (and (symbolp loc-enc) (fboundp loc-enc))
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)))
3282 (funcall loc-enc (point-min) (point-max)))
3283 (goto-char (point-max))
3284 (unless (bolp)
3285 (newline)))
3286 (tramp-message-for-buffer
3287 multi-method method user host
3288 6 "Encoding region using command `%s'..." loc-enc)
3289 (unless (equal 0 (tramp-call-local-coding-command
3290 loc-enc tmpfil t))
3291 (pop-to-buffer trampbuf)
3292 (error (concat "Cannot write to `%s', local encoding"
3293 " command `%s' failed")
3294 filename loc-enc)))
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'"
3304 rem-dec
3305 (tramp-shell-quote-argument localname)))
3306 (set-buffer tmpbuf)
3307 (tramp-message-for-buffer
3308 multi-method method user host
3309 6 "Sending data to remote host...")
3310 (tramp-send-string multi-method method user host
3311 (buffer-string))
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
3317 multi-method method user host "EOF" nil t)
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")
3327 filename rem-dec)
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)))))
3349
3350 ;; Call down to the real handler.
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
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
3359 ;; efs-sifn-handler-function to handle any filename with environment
3360 ;; variables. This has two implications:
3361 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
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.
3383 First arg specifies the OPERATION, second arg is a list of arguments to
3384 pass to the OPERATION."
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))
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.
3402 First arg specifies the OPERATION, second arg is a list of arguments to
3403 pass to the OPERATION."
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))
3412 (apply operation args)))
3413
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.
3423 ARGS 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))
3450 (if (file-name-absolute-p (nth 0 args))
3451 (nth 0 args)
3452 (expand-file-name (nth 0 args))))
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."
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)))
3493
3494 ;; Main function.
3495 ;;;###autoload
3496 (defun tramp-file-name-handler (operation &rest args)
3497 "Invoke tramp file name handler.
3498 Falls back to normal file name handler if no tramp file name handler exists."
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))))))
3507
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.
3513 Falls 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
3520 (save-match-data (apply (cdr fn) args))
3521 (tramp-completion-run-real-handler operation args))))
3522
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))
3527 (add-to-list 'file-name-handler-alist
3528 (cons tramp-completion-file-name-regexp
3529 'tramp-completion-file-name-handler))
3530
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
3534 necessary 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)
3540
3541
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."
3549 (with-parsed-tramp-file-name name nil
3550 (save-match-data
3551 (if (or (string-match "\\*" name)
3552 (string-match "\\?" name)
3553 (string-match "\\[.*\\]" name))
3554 (save-excursion
3555 (let (bufstr)
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
3565 ;;- (format "echo %s" (comint-quote-filename localname)))
3566 ;;- (tramp-wait-for-output))
3567 (tramp-send-command multi-method method user host
3568 (format "echo %s" localname))
3569 (tramp-wait-for-output)
3570 (setq bufstr (buffer-substring (point-min)
3571 (tramp-line-end-position)))
3572 (goto-char (point-min))
3573 (if (string-equal localname bufstr)
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))))))
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
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'.
3613 ;; Using `last-input-event' is a little bit risky, because completing a file
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
3626 (concat tramp-prefix-regexp
3627 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
3628 file)
3629 (member (match-string 1 file) (mapcar 'car tramp-methods)))
3630 ((or (equal last-input-event 'tab)
3631 (and (integerp last-input-event)
3632 (not (event-modifiers last-input-event))
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
3646 ;; Localname manipulation in case of partial TRAMP file names.
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))))
3653
3654 ;; Localname manipulation in case of partial TRAMP file names.
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))
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))
3684 (localname (tramp-file-name-localname car))
3685 (m (tramp-find-method multi-method method user host))
3686 (tramp-current-user user) ; see `tramp-parse-passwd'
3687 all-user-hosts)
3688
3689 (unless (or multi-method ;; Not handled (yet).
3690 localname) ;; Nothing to complete
3691
3692 (if (or user host)
3693
3694 ;; Method dependent user / host combinations
3695 (progn
3696 (mapcar
3697 (lambda (x)
3698 (setq all-user-hosts
3699 (append all-user-hosts
3700 (funcall (nth 0 x) (nth 1 x)))))
3701 (tramp-get-completion-function m))
3702
3703 (setq result (append result
3704 (mapcar
3705 (lambda (x)
3706 (tramp-get-completion-user-host
3707 method user host (nth 0 x) (nth 1 x)))
3708 (delq nil all-user-hosts)))))
3709
3710 ;; Possible methods
3711 (setq result
3712 (append result (tramp-get-completion-methods m)))))
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
3734 ;; completions. If "multi-method" or "localname" is non-nil it means there
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
3744 ;; "/x:" "/x:y" "/x:y:"
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.
3756 They are collected by `tramp-completion-dissect-file-name1'."
3757
3758 (let* ((result)
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)
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.
3815 The structure consists of multi-method, remote method, remote user,
3816 remote host and localname (filename on remote host)."
3817
3818 (let (method)
3819 (save-match-data
3820 (when (string-match (nth 0 structure) name)
3821 (setq method (and (nth 1 structure)
3822 (match-string (nth 1 structure) name)))
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
3830 :localname nil)
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)))
3835 (localname (and (nth 4 structure)
3836 (match-string (nth 4 structure) name))))
3837 (make-tramp-file-name
3838 :multi-method nil
3839 :method method
3840 :user user
3841 :host host
3842 :localname localname)))))))
3843
3844 ;; This function returns all possible method completions, adding the
3845 ;; trailing method delimeter.
3846 (defun tramp-get-completion-methods (partial-method)
3847 "Returns all method completions for PARTIAL-METHOD."
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))))
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.
3859 PARTIAL-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
3885 (unless (zerop (+ (length user) (length host)))
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.
3891 Either user or host may be nil."
3892
3893 (let (res)
3894 (when (file-readable-p filename)
3895 (with-temp-buffer
3896 (insert-file-contents filename)
3897 (goto-char (point-min))
3898 (while (not (eobp))
3899 (push (tramp-parse-rhosts-group) res))))
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.
3911 Either user or host may be nil."
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.
3928 User is always nil."
3929
3930 (let (res)
3931 (when (file-readable-p filename)
3932 (with-temp-buffer
3933 (insert-file-contents filename)
3934 (goto-char (point-min))
3935 (while (not (eobp))
3936 (push (tramp-parse-shosts-group) res))))
3937 res))
3938
3939 (defun tramp-parse-shosts-group ()
3940 "Return a (user host) tuple allowed to access.
3941 User 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
3955 (defun tramp-parse-sconfig (filename)
3956 "Return a list of (user host) tuples allowed to access.
3957 User 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.
3970 User 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
3984 (defun tramp-parse-hosts (filename)
3985 "Return a list of (user host) tuples allowed to access.
3986 User is always nil."
3987
3988 (let (res)
3989 (when (file-readable-p filename)
3990 (with-temp-buffer
3991 (insert-file-contents filename)
3992 (goto-char (point-min))
3993 (while (not (eobp))
3994 (push (tramp-parse-hosts-group) res))))
3995 res))
3996
3997 (defun tramp-parse-hosts-group ()
3998 "Return a (user host) tuple allowed to access.
3999 User 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
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'.
4018 (defun tramp-parse-passwd (filename)
4019 "Return a list of (user host) tuples allowed to access.
4020 Host is always \"localhost\"."
4021
4022 (let (res)
4023 (if (zerop (length tramp-current-user))
4024 '(("root" nil))
4025 (when (file-readable-p filename)
4026 (with-temp-buffer
4027 (insert-file-contents filename)
4028 (goto-char (point-min))
4029 (while (not (eobp))
4030 (push (tramp-parse-passwd-group) res))))
4031 res)))
4032
4033 (defun tramp-parse-passwd-group ()
4034 "Return a (user host) tuple allowed to access.
4035 Host is always \"localhost\"."
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
4047 (defun tramp-parse-netrc (filename)
4048 "Return a list of (user host) tuples allowed to access.
4049 User may be nil."
4050
4051 (let (res)
4052 (when (file-readable-p filename)
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.
4062 User 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
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)))
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))))))
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.
4098 Returns 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
4105 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
4106
4107 (defun tramp-run-test2 (program file1 file2 &optional switch)
4108 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
4109 The optional SWITCH is inserted between the two files.
4110 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
4111 hosts, 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))
4122 (localname1 (tramp-file-name-localname v1))
4123 (localname2 (tramp-file-name-localname v2)))
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
4136 (tramp-shell-quote-argument localname1)
4137 (or switch "")
4138 (tramp-shell-quote-argument localname2))))))
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.
4151 MULTI-METHOD gives the multi method, METHOD the array of methods,
4152 USER 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.
4192 First args METHOD, USER and HOST specify the connection, PROGNAME
4193 is the program to search for, and DIRLIST gives the list of directories
4194 to search. If IGNORE-TILDE is non-nil, directory names starting
4195 with `~' will be ignored.
4196
4197 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
4198
4199 This 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.
4231 I.e., for each directory in DIRLIST, it is tested whether it exists and if
4232 so, 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.
4254 Here, we are looking for a command which has zero exit status if the
4255 file exists and nonzero exit status otherwise."
4256 (make-local-variable 'tramp-file-exists-command)
4257 (tramp-message 9 "Finding command to check if file exists")
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
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")
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")
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."))))
4297
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
4309 "bash" tramp-remote-path t)
4310 (tramp-find-executable multi-method method user host
4311 "ksh" tramp-remote-path t)))
4312 (unless shell
4313 (error "Couldn't find a shell which groks tilde expansion"))
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))))
4322 (tramp-message
4323 5 "Starting remote shell `%s' for tilde expansion..." shell)
4324 (tramp-send-command
4325 multi-method method user host
4326 (concat "PS1='$ ' exec " shell)) ;
4327 (unless (tramp-wait-for-regexp
4328 (get-buffer-process (current-buffer))
4329 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'"
4330 tramp-shell-prompt-pattern shell-prompt-pattern))
4331 (pop-to-buffer (buffer-name))
4332 (error "Couldn't find remote `%s' prompt." shell))
4333 (tramp-message
4334 9 "Setting remote shell prompt...")
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.
4339 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
4340 tramp-rsh-end-of-line
4341 tramp-end-of-output
4342 tramp-rsh-end-of-line
4343 tramp-rsh-end-of-line))
4344 (tramp-wait-for-output)
4345 (tramp-message
4346 9 "Setting remote shell prompt...done")
4347 )
4348 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
4349 (tramp-get-remote-sh multi-method method user host))))))
4350
4351 (defun tramp-check-ls-command (multi-method method user host cmd)
4352 "Checks whether the given `ls' executable groks `-n'.
4353 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
4354 the `ls' executable. Returns t if CMD supports the `-n' option, nil
4355 otherwise."
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'.
4374 Returns nil if none was found, else the command is returned."
4375 (let ((dl dirlist)
4376 (result nil)
4377 (directory-sep-char ?/)) ;for XEmacs
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
4396 ;; ------------------------------------------------------------
4397 ;; -- Functions for establishing connection --
4398 ;; ------------------------------------------------------------
4399
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."
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)))
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."
4431 (pop-to-buffer (tramp-get-buffer multi-method method user host))
4432 (tramp-message 9 "Permission denied by remote host.")
4433 (kill-process p)
4434 (throw 'tramp-action 'permission-denied))
4435
4436 (defun tramp-action-yesno (p multi-method method user host)
4437 "Ask the user for confirmation using `yes-or-no-p'.
4438 Send \"yes\" to remote process on confirmation, abort otherwise.
4439 See also `tramp-action-yn'."
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
4449 (defun tramp-action-yn (p multi-method method user host)
4450 "Ask the user for confirmation using `y-or-n-p'.
4451 Send \"y\" to remote process on confirmation, abort otherwise.
4452 See 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)
4457 (throw 'tramp-action 'permission-denied))
4458 (erase-buffer)
4459 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
4460
4461 (defun tramp-action-terminal (p multi-method method user host)
4462 "Tell the remote host which terminal type to use.
4463 The 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
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."
4480 (tramp-message 9 "Sending password")
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))
4513 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
4514 pattern)
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)
4523 (tramp-message 9 "Waiting for prompts from remote shell")
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))
4549 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
4550 pattern)
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)
4559 (tramp-message 9 "Waiting for prompts from remote shell")
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.
4568
4569 (defun tramp-open-connection-telnet (multi-method method user host)
4570 "Open a connection using a telnet METHOD.
4571 This starts the command `telnet HOST ARGS'[*], then waits for a remote
4572 login prompt, then sends the user name USER, then waits for a remote
4573 password prompt. It queries the user for the password, then sends the
4574 password to the remote host.
4575
4576 If USER is nil, uses value returned by `(user-login-name)' instead.
4577
4578 Recognition of the remote shell prompt is based on the variables
4579 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4580 set up correctly.
4581
4582 Please note that it is NOT possible to use this connection method
4583 together with an out-of-band transfer method! You must use an inline
4584 transfer method.
4585
4586 Maybe 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
4591 (when (tramp-method-out-of-band-p multi-method method user host)
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)
4597 (tramp-message 7 "Opening connection for %s@%s using %s..."
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))
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?
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)
4611 (tramp-get-telnet-program
4612 multi-method
4613 (tramp-find-method multi-method method user host)
4614 user host)
4615 host
4616 (tramp-get-telnet-args
4617 multi-method
4618 (tramp-find-method multi-method method user host)
4619 user host)))
4620 (found nil)
4621 (pw nil))
4622 (process-kill-without-query p)
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)
4627 (tramp-open-connection-setup-interactive-shell
4628 p multi-method method user host)
4629 (tramp-post-connection multi-method method user host)))))
4630
4631
4632 (defun tramp-open-connection-rsh (multi-method method user host)
4633 "Open a connection using an rsh METHOD.
4634 This starts the command `rsh HOST -l USER'[*], then waits for a remote
4635 password or shell prompt. If a password prompt is seen, the user is
4636 queried for a password, this function sends the password to the remote
4637 host and waits for a shell prompt.
4638
4639 If USER is nil, start the command `rsh HOST'[*] instead
4640
4641 Recognition of the remote shell prompt is based on the variables
4642 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4643 set up correctly.
4644
4645 Please note that it is NOT possible to use this connection method with
4646 an out-of-band transfer method if this function asks the user for a
4647 password! You must use an inline transfer method in this case.
4648 Sadly, the transfer method cannot be switched on the fly, instead you
4649 must specify the right method in the file name.
4650
4651 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
4652 be a port number for ssh, and \"-p yy\" will be added to the list of
4653 arguments, and xx will be used as the host name to connect to.
4654
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)
4661 (if (and user (not (string= user "")))
4662 (tramp-message 7 "Opening connection for %s@%s using %s..."
4663 user host method)
4664 (tramp-message 7 "Opening connection at %s using %s..." host method))
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))
4668 (rsh-program (tramp-get-rsh-program
4669 multi-method
4670 (tramp-find-method multi-method method user host)
4671 user host))
4672 (rsh-args (tramp-get-rsh-args
4673 multi-method
4674 (tramp-find-method multi-method method user host)
4675 user host)))
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)))
4681 (setenv "TERM" tramp-terminal-type)
4682 (let* ((default-directory (tramp-temporary-file-directory))
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?
4686 (coding-system-for-read (unless (and (not (featurep 'xemacs))
4687 (> emacs-major-version 20))
4688 tramp-dos-coding-system))
4689 (p (if (and user (not (string= user "")))
4690 (apply #'start-process bufnam buf rsh-program
4691 host "-l" user rsh-args)
4692 (apply #'start-process bufnam buf rsh-program
4693 host rsh-args)))
4694 (found nil))
4695 (process-kill-without-query p)
4696
4697 (set-buffer buf)
4698 (tramp-process-actions p multi-method method user host
4699 tramp-actions-before-shell)
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
4705 (defun tramp-open-connection-su (multi-method method user host)
4706 "Open a connection using the `su' program with METHOD.
4707 This starts `su - USER', then waits for a password prompt. The HOST
4708 name must be equal to the local host name or to `localhost'.
4709
4710 If USER is nil, uses value returned by user-login-name instead.
4711
4712 Recognition of the remote shell prompt is based on the variables
4713 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
4714 set up correctly. Note that the other user may have a different shell
4715 prompt than you do, so it is not at all unlikely that the variable
4716 `shell-prompt-pattern' is set up wrongly!"
4717 (save-match-data
4718 (when (tramp-method-out-of-band-p multi-method method user host)
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))
4723 (string= "localhost" host)
4724 (string= "" host))
4725 (error
4726 "Cannot connect to different host `%s' with `su' connection method"
4727 host))
4728 (tramp-pre-connection multi-method method user host)
4729 (tramp-message 7 "Opening connection for `%s' using `%s'..."
4730 (or user "<root>") method)
4731 (let ((process-environment (copy-sequence process-environment)))
4732 (setenv "TERM" tramp-terminal-type)
4733 (let* ((default-directory (tramp-temporary-file-directory))
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?
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
4741 (tramp-buffer-name multi-method method user host)
4742 (tramp-get-buffer multi-method method user host)
4743 (tramp-get-su-program
4744 multi-method
4745 (tramp-find-method multi-method method user host)
4746 user host)
4747 (mapcar
4748 (lambda (x)
4749 (format-spec x `((?u . ,(or user "root")))))
4750 (tramp-get-su-args
4751 multi-method
4752 (tramp-find-method multi-method method user host)
4753 user host))))
4754 (found nil)
4755 (pw nil))
4756 (process-kill-without-query p)
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)
4760 (tramp-open-connection-setup-interactive-shell
4761 p multi-method method user host)
4762 (tramp-post-connection multi-method method
4763 user host)))))
4764
4765 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
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.
4775 This uses a slightly changed file name syntax. The idea is to say
4776 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
4777 This will use telnet to log in as u1 to h1, then use rsh from there to
4778 log 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"))
4782 (when (tramp-method-out-of-band-p multi-method method user host)
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))
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?
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)
4806 tramp-multi-sh-program))
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
4812 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
4813 shell-prompt-pattern tramp-shell-prompt-pattern))
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)))
4825 ;; The multi-funcs don't need to do save-match-data, as that
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
4835 ;; of no user name provided. Hack to make it work as it did before:
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.
4840 Uses shell COMMAND to issue a `telnet' command to log in as USER to
4841 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
4842 the host name, and `%n' is replaced with an end of line character, as
4843 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
4844 character.
4845
4846 If USER is nil, uses the return value of (user-login-name) instead."
4847 (let ((cmd (format-spec command
4848 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
4849 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
4850 found pw)
4851 (erase-buffer)
4852 (tramp-message 9 "Sending telnet command `%s'" cmd1)
4853 (process-send-string p cmd)
4854 (tramp-process-multi-actions p method user host
4855 tramp-multi-actions)))
4856
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:
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.
4863 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
4864 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
4865 with the user name, `%h' will be replaced with the host name, and `%n'
4866 will 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
4869 If USER is nil, uses the return value of (user-login-name) instead."
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 . ""))))
4876 found)
4877 (erase-buffer)
4878 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
4879 (process-send-string p cmd)
4880 (tramp-process-multi-actions p method user host
4881 tramp-multi-actions)))
4882
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:
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.
4889 Uses shell COMMAND to issue a `su' command to log in as USER on
4890 HOST. The HOST name is ignored, this just changes the user id on the
4891 host currently logged in to.
4892
4893 If USER is nil, uses the return value of (user-login-name) instead.
4894
4895 You can use percent escapes in the COMMAND. `%u' is replaced with the
4896 user name, and `%n' is replaced with the value of
4897 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
4898 character."
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 . ""))))
4903 found)
4904 (erase-buffer)
4905 (tramp-message 9 "Sending su command `%s'" cmd1)
4906 (process-send-string p cmd)
4907 (tramp-process-multi-actions p method user host
4908 tramp-multi-actions)))
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.
4914 Expects the output of PROC to be sent to the current buffer. Returns
4915 the string that matched, or nil. Waits indefinitely if TIMEOUT is
4916 nil."
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"
4950 (if timeout (format " in %d secs" timeout) "")
4951 "]]"))))
4952 found))
4953
4954 (defun tramp-enter-password (p prompt)
4955 "Prompt for a password and send it to the remote end.
4956 Uses PROMPT as a prompt and sends the password to process P."
4957 (let ((pw (tramp-read-passwd prompt)))
4958 (erase-buffer)
4959 (process-send-string p (concat pw tramp-password-end-of-line))))
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.
4966 METHOD, 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.
4978 Mainly sets the prompt and the echo correctly. P is the shell process
4979 to 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)
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
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"
4998 (tramp-get-remote-sh
4999 multi-method method user host)
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
5006 'bold (format "$ exec env PS1='$ ' %s\n"
5007 (tramp-get-remote-sh multi-method method user host)))))
5008 (tramp-message 9 "Waiting 30s for remote `%s' to come up..."
5009 (tramp-get-remote-sh multi-method method user host))
5010 (unless (tramp-wait-for-regexp
5011 p 30 (format "\\(%s\\|%s\\)\\'"
5012 shell-prompt-pattern tramp-shell-prompt-pattern))
5013 (pop-to-buffer (buffer-name))
5014 (error "Remote `%s' didn't come up. See buffer `%s' for details"
5015 (tramp-get-remote-sh multi-method method user host)
5016 (buffer-name)))
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
5022 p 30 (format "\\(%s\\|%s\\)\\'"
5023 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5030 p 30 (format "\\(%s\\|%s\\)\\'"
5031 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5042 p 30 (format "\\(%s\\|%s\\)\\'"
5043 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5071 p 30 (format "\\(%s\\|%s\\)\\'"
5072 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5082 p 30 (format "\\(%s\\|%s\\)\\'"
5083 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5094 p 30 (format "\\(%s\\|%s\\)\\'"
5095 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5105 p 30 (format "\\(%s\\|%s\\)\\'"
5106 shell-prompt-pattern tramp-shell-prompt-pattern))
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
5115 p 30 (format "\\(%s\\|%s\\)\\'"
5116 shell-prompt-pattern tramp-shell-prompt-pattern))
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")
5122 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
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))
5127 (tramp-send-command
5128 multi-method method user host
5129 (format "PS1='%s%s%s'; PS2=''; PS3=''"
5130 tramp-rsh-end-of-line
5131 tramp-end-of-output
5132 tramp-rsh-end-of-line))
5133 (tramp-wait-for-output))
5134
5135 (defun tramp-post-connection (multi-method method user host)
5136 "Prepare a remote shell before being able to work on it.
5137 METHOD, USER and HOST specify the connection.
5138 Among other things, this finds a shell which groks tilde expansion,
5139 tries to find an `ls' command which groks the `-n' option, sets the
5140 locale 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)
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>
5162 (sleep-for 1)
5163 (erase-buffer)
5164 (tramp-find-file-exists-command multi-method method user host)
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
5211 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
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)
5219 ;; Send the fallback `uudecode' script.
5220 (erase-buffer)
5221 (tramp-send-string multi-method method user host tramp-uudecode)
5222 (tramp-wait-for-output)
5223 ;; Find a `perl'.
5224 (erase-buffer)
5225 (let ((tramp-remote-perl
5226 (or (tramp-find-executable multi-method method user host
5227 "perl5" tramp-remote-path nil)
5228 (tramp-find-executable multi-method method user host
5229 "perl" tramp-remote-path nil))))
5230 (when tramp-remote-perl
5231 (tramp-set-connection-property "perl" tramp-remote-perl
5232 multi-method method user host)
5233 ;; Set up stat in Perl if we can.
5234 (when tramp-remote-perl
5235 (tramp-message 5 "Sending the Perl `file-attributes' implementation.")
5236 (tramp-send-string
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)
5243 (unless (tramp-get-rcp-program
5244 multi-method
5245 (tramp-find-method multi-method method user host)
5246 user host)
5247 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
5248 (tramp-send-string
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)
5255 (tramp-send-string
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.")
5263 (tramp-send-string
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)
5270 (tramp-send-string
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)))))
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)
5284 ;; Find the right encoding/decoding commands to use.
5285 (unless (tramp-get-rcp-program
5286 multi-method
5287 (tramp-find-method multi-method method user host)
5288 user host)
5289 (tramp-find-inline-encoding multi-method method user host))
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.
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))
5295 (magic-string "xyzzy"))
5296 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
5297 (tramp-kill-process multi-method method user host)
5298 ;; Improve error message and/or error check.
5299 (error
5300 "Must give both decoding and encoding command in method definition"))
5301 (when (and rem-enc rem-dec)
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"
5308 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
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
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.
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
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 -"
5344 tramp-uuencode-region uudecode-decode-region)
5345 ("uuencode xxx" "uudecode -p"
5346 tramp-uuencode-region uudecode-decode-region)
5347 ("uuencode xxx" "tramp_uudecode"
5348 tramp-uuencode-region uudecode-decode-region)
5349 ("tramp_encode_with_module" "tramp_decode_with_module"
5350 base64-encode-region base64-decode-region)
5351 ("tramp_encode" "tramp_decode"
5352 base64-encode-region base64-decode-region))
5353 "List of coding commands for inline transfer.
5354 Each item is a list that looks like this:
5355
5356 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
5357
5358 The REMOTE-ENCODING should be a string, giving a command accepting a
5359 plain file on standard input and writing the encoded file to standard
5360 output. The REMOTE-DECODING should also be a string, giving a command
5361 accepting an encoded file on standard input and writing the decoded
5362 file to standard output.
5363
5364 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
5365 symbols, giving functions. If they are strings, then they can contain
5366 the \"%s\" format specifier. If that specifier is present, the input
5367 filename will be put into the command line at that spot. If the
5368 specifier is not present, the input should be read from standard
5369 input.
5370
5371 If they are functions, they will be called with two arguments, start
5372 and end of region, and are expected to replace the region contents
5373 with the encoded or decoded results, respectively.")
5374
5375 (defun tramp-find-inline-encoding (multi-method method user host)
5376 "Find an inline transfer encoding that works.
5377 Goes through the list `tramp-coding-commands'."
5378 (let ((commands tramp-coding-commands)
5379 (magic "xyzzy")
5380 item found)
5381 (while (and commands (null found))
5382 (setq item (pop commands))
5383 (catch 'wont-work
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
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!
5396 (tramp-message-for-buffer
5397 multi-method method user host 9
5398 "Checking remote encoding command `%s' for sanity" rem-enc)
5399 (unless (zerop (tramp-send-command-and-check
5400 multi-method method user host
5401 (format "%s </dev/null" rem-enc) t))
5402 (throw 'wont-work nil))
5403 (tramp-message-for-buffer
5404 multi-method method user host 9
5405 "Checking remote decoding command `%s' for sanity" rem-dec)
5406 (unless (zerop (tramp-send-command-and-check
5407 multi-method method user host
5408 (format "echo %s | %s | %s"
5409 magic rem-enc rem-dec) t))
5410 (throw 'wont-work nil))
5411 (save-excursion
5412 (goto-char (point-min))
5413 (unless (looking-at (regexp-quote magic))
5414 (throw 'wont-work nil)))
5415 ;; If the local encoder or decoder is a string, the
5416 ;; corresponding command has to work locally.
5417 (when (stringp loc-enc)
5418 (tramp-message-for-buffer
5419 multi-method method user host 9
5420 "Checking local encoding command `%s' for sanity" loc-enc)
5421 (unless (zerop (tramp-call-local-coding-command
5422 loc-enc nil nil))
5423 (throw 'wont-work nil)))
5424 (when (stringp loc-dec)
5425 (tramp-message-for-buffer
5426 multi-method method user host 9
5427 "Checking local decoding command `%s' for sanity" loc-dec)
5428 (unless (zerop (tramp-call-local-coding-command
5429 loc-dec nil nil))
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"))
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.
5455 If CMD contains \"%s\", provide input file INPUT there in command.
5456 Otherwise, INPUT is passed via standard input.
5457 INPUT can also be nil which means `/dev/null'.
5458 OUTPUT can be a string (which specifies a filename), or t (which
5459 means standard output and thus the current buffer), or nil (which
5460 means 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) ""))))
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.
5475 Does not do anything if a connection is already open, but re-opens the
5476 connection if a previous connection has died for some reason."
5477 (let ((p (get-buffer-process
5478 (tramp-get-buffer multi-method method user host)))
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
5490 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
5491 p (processp p) (memq (process-status p) '(run open)))
5492 (tramp-send-command
5493 multi-method method user host "echo are you awake" nil t)
5494 (unless (tramp-wait-for-output 10)
5495 (delete-process p)
5496 (setq p nil))
5497 (erase-buffer)))
5498 (unless (and p (processp p) (memq (process-status p) '(run open)))
5499 (when (and p (processp p))
5500 (delete-process p))
5501 (funcall (tramp-get-connection-function
5502 multi-method
5503 (tramp-find-method multi-method method user host)
5504 user host)
5505 multi-method method user host))))
5506
5507 (defun tramp-send-command
5508 (multi-method method user host command &optional noerase neveropen)
5509 "Send the COMMAND to USER at HOST (logged in using METHOD).
5510 Erases temporary buffer before sending the command (unless NOERASE
5511 is true).
5512 If optional seventh arg NEVEROPEN is non-nil, never try to open the
5513 connection. 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))
5517 (setq tramp-last-cmd-time (current-time))
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
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)
5537 "\r?$")))
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"
5582 (if timeout (format " in %d secs" timeout) "")
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.
5590 That is, (list (match-string 0) (match-string 1) ...), according to the
5591 number 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.
5603 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
5604 Sends `echo $?' along with the COMMAND for checking the exit status. If
5605 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
5606
5607 If the optional argument SUBSHELL is non-nil, the command is executed in
5608 a 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.
5625 Similar to `tramp-send-command-and-check' but accepts two more arguments
5626 FMT 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
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
5642 The STRING is expected to use Unix line-endings, but the lines sent to
5643 the remote host use line-endings as defined in the variable
5644 `tramp-rsh-end-of-line'."
5645 (let ((proc (get-buffer-process
5646 (tramp-get-buffer multi-method method user host))))
5647 (unless proc
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))
5660 (unless (string-equal (substring string -1) tramp-rsh-end-of-line)
5661 (setq string (concat string tramp-rsh-end-of-line)))
5662 ;; send the string
5663 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
5664 (let ((pos 0)
5665 (end (length string)))
5666 (while (< pos end)
5667 (tramp-message-for-buffer
5668 multi-method method user host 10
5669 "Sending chunk from %s to %s"
5670 pos (min (+ pos tramp-chunksize) end))
5671 (process-send-string
5672 proc (substring string pos (min (+ pos tramp-chunksize) end)))
5673 (setq pos (+ pos tramp-chunksize))
5674 (sleep-for 0.1)))
5675 (process-send-string proc string))))
5676
5677 (defun tramp-send-eof (multi-method method user host)
5678 "Send EOF to the remote end.
5679 METHOD, HOST and USER specify the connection."
5680 (let ((proc (get-buffer-process
5681 (tramp-get-buffer multi-method method user host))))
5682 (unless proc
5683 (error "Can't send EOF to remote host -- not logged in"))
5684 (process-send-eof proc)))
5685 ; (process-send-string proc "\^D")))
5686
5687 (defun tramp-kill-process (multi-method method user host)
5688 "Kill the connection process used by Tramp.
5689 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
5690 (let ((proc (get-buffer-process
5691 (tramp-get-buffer multi-method method user host))))
5692 (kill-process proc)))
5693
5694 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
5695 "Erase buffer, then discard subsequent garbage.
5696 If `tramp-discard-garbage' is nil, just erase buffer."
5697 (if (not tramp-discard-garbage)
5698 (erase-buffer)
5699 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
5700 (when tramp-debug-buffer
5701 (save-excursion
5702 (set-buffer (tramp-get-debug-buffer multi-method method user host))
5703 (goto-char (point-max))
5704 (tramp-insert-with-face
5705 'bold (format "Additional characters detected\n")))))))
5706
5707 (defun tramp-mode-string-to-int (mode-string)
5708 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
5709 (let* ((mode-chars (string-to-vector mode-string))
5710 (owner-read (aref mode-chars 1))
5711 (owner-write (aref mode-chars 2))
5712 (owner-execute-or-setid (aref mode-chars 3))
5713 (group-read (aref mode-chars 4))
5714 (group-write (aref mode-chars 5))
5715 (group-execute-or-setid (aref mode-chars 6))
5716 (other-read (aref mode-chars 7))
5717 (other-write (aref mode-chars 8))
5718 (other-execute-or-sticky (aref mode-chars 9)))
5719 (save-match-data
5720 (logior
5721 (case owner-read
5722 (?r (tramp-octal-to-decimal "00400")) (?- 0)
5723 (t (error "Second char `%c' must be one of `r-'" owner-read)))
5724 (case owner-write
5725 (?w (tramp-octal-to-decimal "00200")) (?- 0)
5726 (t (error "Third char `%c' must be one of `w-'" owner-write)))
5727 (case owner-execute-or-setid
5728 (?x (tramp-octal-to-decimal "00100"))
5729 (?S (tramp-octal-to-decimal "04000"))
5730 (?s (tramp-octal-to-decimal "04100"))
5731 (?- 0)
5732 (t (error "Fourth char `%c' must be one of `xsS-'"
5733 owner-execute-or-setid)))
5734 (case group-read
5735 (?r (tramp-octal-to-decimal "00040")) (?- 0)
5736 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
5737 (case group-write
5738 (?w (tramp-octal-to-decimal "00020")) (?- 0)
5739 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
5740 (case group-execute-or-setid
5741 (?x (tramp-octal-to-decimal "00010"))
5742 (?S (tramp-octal-to-decimal "02000"))
5743 (?s (tramp-octal-to-decimal "02010"))
5744 (?- 0)
5745 (t (error "Seventh char `%c' must be one of `xsS-'"
5746 group-execute-or-setid)))
5747 (case other-read
5748 (?r (tramp-octal-to-decimal "00004")) (?- 0)
5749 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
5750 (case other-write
5751 (?w (tramp-octal-to-decimal "00002")) (?- 0)
5752 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
5753 (case other-execute-or-sticky
5754 (?x (tramp-octal-to-decimal "00001"))
5755 (?T (tramp-octal-to-decimal "01000"))
5756 (?t (tramp-octal-to-decimal "01001"))
5757 (?- 0)
5758 (t (error "Tenth char `%c' must be one of `xtT-'"
5759 other-execute-or-sticky)))))))
5760
5761
5762 (defun tramp-file-mode-from-int (mode)
5763 "Turn an integer representing a file mode into an ls(1)-like string."
5764 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
5765 (user (logand (lsh mode -6) 7))
5766 (group (logand (lsh mode -3) 7))
5767 (other (logand (lsh mode -0) 7))
5768 (suid (> (logand (lsh mode -9) 4) 0))
5769 (sgid (> (logand (lsh mode -9) 2) 0))
5770 (sticky (> (logand (lsh mode -9) 1) 0)))
5771 (setq user (tramp-file-mode-permissions user suid "s"))
5772 (setq group (tramp-file-mode-permissions group sgid "s"))
5773 (setq other (tramp-file-mode-permissions other sticky "t"))
5774 (concat type user group other)))
5775
5776
5777 (defun tramp-file-mode-permissions (perm suid suid-text)
5778 "Convert a permission bitset into a string.
5779 This is used internally by `tramp-file-mode-from-int'."
5780 (let ((r (> (logand perm 4) 0))
5781 (w (> (logand perm 2) 0))
5782 (x (> (logand perm 1) 0)))
5783 (concat (or (and r "r") "-")
5784 (or (and w "w") "-")
5785 (or (and suid x suid-text) ; suid, execute
5786 (and suid (upcase suid-text)) ; suid, !execute
5787 (and x "x") "-")))) ; !suid
5788
5789
5790 (defun tramp-decimal-to-octal (i)
5791 "Return a string consisting of the octal digits of I.
5792 Not actually used. Use `(format \"%o\" i)' instead?"
5793 (cond ((< i 0) (error "Cannot convert negative number to octal"))
5794 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
5795 ((zerop i) "0")
5796 (t (concat (tramp-decimal-to-octal (/ i 8))
5797 (number-to-string (% i 8))))))
5798
5799
5800 ;;(defun tramp-octal-to-decimal (ostr)
5801 ;; "Given a string of octal digits, return a decimal number."
5802 ;; (cond ((null ostr) 0)
5803 ;; ((string= "" ostr) 0)
5804 ;; (t (let ((last (aref ostr (1- (length ostr))))
5805 ;; (rest (substring ostr 0 (1- (length ostr)))))
5806 ;; (unless (and (>= last ?0)
5807 ;; (<= last ?7))
5808 ;; (error "Not an octal digit: %c" last))
5809 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
5810 ;; Kudos to Gerd Moellmann for this suggestion.
5811 (defun tramp-octal-to-decimal (ostr)
5812 "Given a string of octal digits, return a decimal number."
5813 (let ((x (or ostr "")))
5814 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
5815 (unless (string-match "\\`[0-7]*\\'" x)
5816 (error "Non-octal junk in string `%s'" x))
5817 (string-to-number ostr 8)))
5818
5819 (defun tramp-shell-case-fold (string)
5820 "Converts STRING to shell glob pattern which ignores case."
5821 (mapconcat
5822 (lambda (c)
5823 (if (equal (downcase c) (upcase c))
5824 (vector c)
5825 (format "[%c%c]" (downcase c) (upcase c))))
5826 string
5827 ""))
5828
5829
5830 ;; ------------------------------------------------------------
5831 ;; -- TRAMP file names --
5832 ;; ------------------------------------------------------------
5833 ;; Conversion functions between external representation and
5834 ;; internal data structure. Convenience functions for internal
5835 ;; data structure.
5836
5837 (defstruct tramp-file-name multi-method method user host localname)
5838
5839 (defun tramp-tramp-file-p (name)
5840 "Return t iff NAME is a tramp file."
5841 (save-match-data
5842 (string-match tramp-file-name-regexp name)))
5843
5844 ;; HHH: Changed. Used to assign the return value of (user-login-name)
5845 ;; to the `user' part of the structure if a user name was not
5846 ;; provided, now it assigns nil.
5847 (defun tramp-dissect-file-name (name)
5848 "Return an `tramp-file-name' structure.
5849 The structure consists of remote method, remote user, remote host and
5850 localname (file name on remote host)."
5851 (save-match-data
5852 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
5853 (method
5854 ; single-hop
5855 (if match (match-string (nth 1 tramp-file-name-structure) name)
5856 ; maybe multi-hop
5857 (string-match
5858 (format (nth 0 tramp-multi-file-name-structure)
5859 (nth 0 tramp-multi-file-name-hop-structure)) name)
5860 (match-string (nth 1 tramp-multi-file-name-structure) name))))
5861 (if (and method (member method tramp-multi-methods))
5862 ;; If it's a multi method, the file name structure contains
5863 ;; arrays of method, user and host.
5864 (tramp-dissect-multi-file-name name)
5865 ;; Normal method. First, find out default method.
5866 (unless match (error "Not a tramp file name: %s" name))
5867 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
5868 (host (match-string (nth 3 tramp-file-name-structure) name))
5869 (localname (match-string (nth 4 tramp-file-name-structure) name)))
5870 (make-tramp-file-name
5871 :multi-method nil
5872 :method method
5873 :user (or user nil)
5874 :host host
5875 :localname localname))))))
5876
5877 (defun tramp-find-default-method (user host)
5878 "Look up the right method to use in `tramp-default-method-alist'."
5879 (let ((choices tramp-default-method-alist)
5880 (method tramp-default-method)
5881 item)
5882 (while choices
5883 (setq item (pop choices))
5884 (when (and (string-match (nth 0 item) (or host ""))
5885 (string-match (nth 1 item) (or user "")))
5886 (setq method (nth 2 item))
5887 (setq choices nil)))
5888 method))
5889
5890 (defun tramp-find-method (multi-method method user host)
5891 "Return the right method string to use.
5892 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
5893 If both MULTI-METHOD and METHOD are nil, do a lookup in
5894 `tramp-default-method-alist'."
5895 (or multi-method method (tramp-find-default-method user host)))
5896
5897 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
5898 ;; a user name is not provided in the "file name" very well.
5899 (defun tramp-dissect-multi-file-name (name)
5900 "Not implemented yet."
5901 (let ((regexp (nth 0 tramp-multi-file-name-structure))
5902 (method-index (nth 1 tramp-multi-file-name-structure))
5903 (hops-index (nth 2 tramp-multi-file-name-structure))
5904 (localname-index (nth 3 tramp-multi-file-name-structure))
5905 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
5906 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
5907 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
5908 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
5909 method hops len hop-methods hop-users hop-hosts localname)
5910 (unless (string-match (format regexp hop-regexp) name)
5911 (error "Not a multi tramp file name: %s" name))
5912 (setq method (match-string method-index name))
5913 (setq hops (match-string hops-index name))
5914 (setq len (/ (length (match-data t)) 2))
5915 (when (< localname-index 0) (incf localname-index len))
5916 (setq localname (match-string localname-index name))
5917 (let ((index 0))
5918 (while (string-match hop-regexp hops index)
5919 (setq index (match-end 0))
5920 (setq hop-methods
5921 (cons (match-string hop-method-index hops) hop-methods))
5922 (setq hop-users
5923 (cons (match-string hop-user-index hops) hop-users))
5924 (setq hop-hosts
5925 (cons (match-string hop-host-index hops) hop-hosts))))
5926 (make-tramp-file-name
5927 :multi-method method
5928 :method (apply 'vector (reverse hop-methods))
5929 :user (apply 'vector (reverse hop-users))
5930 :host (apply 'vector (reverse hop-hosts))
5931 :localname localname)))
5932
5933 (defun tramp-make-tramp-file-name (multi-method method user host localname)
5934 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
5935 (if multi-method
5936 (tramp-make-tramp-multi-file-name multi-method method user host localname)
5937 (format-spec
5938 (concat tramp-prefix-format
5939 (when method (concat "%m" tramp-postfix-single-method-format))
5940 (when user (concat "%u" tramp-postfix-user-format))
5941 (when host (concat "%h" tramp-postfix-host-format))
5942 (when localname (concat "%p")))
5943 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
5944
5945 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
5946 ;; with this when USER is nil?
5947 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
5948 "Constructs a tramp file name for a multi-hop method."
5949 (unless tramp-make-multi-tramp-file-format
5950 (error "`tramp-make-multi-tramp-file-format' is nil"))
5951 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
5952 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
5953 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
5954 (prefix (format-spec prefix-format `((?m . ,multi-method))))
5955 (hops "")
5956 (localname (format-spec localname-format `((?p . ,localname))))
5957 (i 0)
5958 (len (length method)))
5959 (while (< i len)
5960 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
5961 (setq hops (concat hops (format-spec hop-format
5962 `((?m . ,m) (?u . ,u) (?h . ,h)))))
5963 (incf i)))
5964 (concat prefix hops localname)))
5965
5966 (defun tramp-make-rcp-program-file-name (user host localname)
5967 "Create a file name suitable to be passed to `rcp'."
5968 (if user
5969 (format "%s@%s:%s" user host localname)
5970 (format "%s:%s" host localname)))
5971
5972 (defun tramp-method-out-of-band-p (multi-method method user host)
5973 "Return t if this is an out-of-band method, nil otherwise.
5974 It is important to check for this condition, since it is not possible
5975 to enter a password for the `tramp-rcp-program'."
5976 (tramp-get-rcp-program
5977 multi-method
5978 (tramp-find-method multi-method method user host)
5979 user host))
5980
5981 ;; Variables local to connection.
5982
5983 (defun tramp-get-ls-command (multi-method method user host)
5984 (save-excursion
5985 (tramp-maybe-open-connection multi-method method user host)
5986 (set-buffer (tramp-get-buffer multi-method method user host))
5987 tramp-ls-command))
5988
5989 (defun tramp-get-test-groks-nt (multi-method method user host)
5990 (save-excursion
5991 (tramp-maybe-open-connection multi-method method user host)
5992 (set-buffer (tramp-get-buffer multi-method method user host))
5993 tramp-test-groks-nt))
5994
5995 (defun tramp-get-file-exists-command (multi-method method user host)
5996 (save-excursion
5997 (tramp-maybe-open-connection multi-method method user host)
5998 (set-buffer (tramp-get-buffer multi-method method user host))
5999 tramp-file-exists-command))
6000
6001 (defun tramp-get-remote-perl (multi-method method user host)
6002 (tramp-get-connection-property "perl" nil multi-method method user host))
6003
6004 (defun tramp-get-remote-ln (multi-method method user host)
6005 (tramp-get-connection-property "ln" nil multi-method method user host))
6006
6007 ;; Get a property of a TRAMP connection.
6008 (defun tramp-get-connection-property
6009 (property default multi-method method user host)
6010 "Get the named property for the connection.
6011 If the value is not set for the connection, return `default'"
6012 (tramp-maybe-open-connection multi-method method user host)
6013 (with-current-buffer (tramp-get-buffer multi-method method user host)
6014 (let (error)
6015 (condition-case nil
6016 (symbol-value (intern (concat "tramp-connection-property-" property)))
6017 (error default)))))
6018
6019 ;; Set a property of a TRAMP connection.
6020 (defun tramp-set-connection-property
6021 (property value multi-method method user host)
6022 "Set the named property of a TRAMP connection."
6023 (tramp-maybe-open-connection multi-method method user host)
6024 (with-current-buffer (tramp-get-buffer multi-method method user host)
6025 (set (make-local-variable
6026 (intern (concat "tramp-connection-property-" property)))
6027 value)))
6028
6029 ;; Some predefined connection properties.
6030 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
6031 (tramp-set-connection-property "remote-encoding" rem-enc
6032 multi-method method user host))
6033 (defun tramp-get-remote-encoding (multi-method method user host)
6034 (tramp-get-connection-property "remote-encoding" nil
6035 multi-method method user host))
6036
6037 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
6038 (tramp-set-connection-property "remote-decoding" rem-dec
6039 multi-method method user host))
6040 (defun tramp-get-remote-decoding (multi-method method user host)
6041 (tramp-get-connection-property "remote-decoding" nil
6042 multi-method method user host))
6043
6044 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
6045 (tramp-set-connection-property "local-encoding" loc-enc
6046 multi-method method user host))
6047 (defun tramp-get-local-encoding (multi-method method user host)
6048 (tramp-get-connection-property "local-encoding" nil
6049 multi-method method user host))
6050
6051 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
6052 (tramp-set-connection-property "local-decoding" loc-dec
6053 multi-method method user host))
6054 (defun tramp-get-local-decoding (multi-method method user host)
6055 (tramp-get-connection-property "local-decoding" nil
6056 multi-method method user host))
6057
6058
6059
6060 (defun tramp-get-connection-function (multi-method method user host)
6061 (second (or (assoc 'tramp-connection-function
6062 (assoc (tramp-find-method multi-method method user host)
6063 tramp-methods))
6064 (error "Method `%s' didn't specify a connection function"
6065 (or multi-method method)))))
6066
6067 (defun tramp-get-remote-sh (multi-method method user host)
6068 (second (or (assoc 'tramp-remote-sh
6069 (assoc (tramp-find-method multi-method method user host)
6070 tramp-methods))
6071 (error "Method `%s' didn't specify a remote shell"
6072 (or multi-method method)))))
6073
6074 (defun tramp-get-rsh-program (multi-method method user host)
6075 (second (or (assoc 'tramp-rsh-program
6076 (assoc (tramp-find-method multi-method method user host)
6077 tramp-methods))
6078 (error "Method `%s' didn't specify an rsh program"
6079 (or multi-method method)))))
6080
6081 (defun tramp-get-rsh-args (multi-method method user host)
6082 (second (or (assoc 'tramp-rsh-args
6083 (assoc (tramp-find-method multi-method method user host)
6084 tramp-methods))
6085 (error "Method `%s' didn't specify rsh args"
6086 (or multi-method method)))))
6087
6088 (defun tramp-get-rcp-program (multi-method method user host)
6089 (second (or (assoc 'tramp-rcp-program
6090 (assoc (tramp-find-method multi-method method user host)
6091 tramp-methods))
6092 (error "Method `%s' didn't specify an rcp program"
6093 (or multi-method method)))))
6094
6095 (defun tramp-get-rcp-args (multi-method method user host)
6096 (second (or (assoc 'tramp-rcp-args
6097 (assoc (tramp-find-method multi-method method user host)
6098 tramp-methods))
6099 (error "Method `%s' didn't specify rcp args"
6100 (or multi-method method)))))
6101
6102 (defun tramp-get-rcp-keep-date-arg (multi-method method user host)
6103 (second (or (assoc 'tramp-rcp-keep-date-arg
6104 (assoc (tramp-find-method multi-method method user host)
6105 tramp-methods))
6106 (error "Method `%s' didn't specify `keep-date' arg for tramp"
6107 (or multi-method method)))))
6108
6109 (defun tramp-get-su-program (multi-method method user host)
6110 (second (or (assoc 'tramp-su-program
6111 (assoc (tramp-find-method multi-method method user host)
6112 tramp-methods))
6113 (error "Method `%s' didn't specify a su program"
6114 (or multi-method method)))))
6115
6116 (defun tramp-get-su-args (multi-method method user host)
6117 (second (or (assoc 'tramp-su-args
6118 (assoc (tramp-find-method multi-method method user host)
6119 tramp-methods))
6120 (error "Method `%s' didn't specify su args"
6121 (or multi-method method)))))
6122
6123 (defun tramp-get-telnet-program (multi-method method user host)
6124 (second (or (assoc 'tramp-telnet-program
6125 (assoc (tramp-find-method multi-method method user host)
6126 tramp-methods))
6127 (error "Method `%s' didn't specify a telnet program"
6128 (or multi-method method)))))
6129
6130 (defun tramp-get-telnet-args (multi-method method user host)
6131 (second (or (assoc 'tramp-telnet-args
6132 (assoc (tramp-find-method multi-method method user host)
6133 tramp-methods))
6134 (error "Method `%s' didn't specify telnet args"
6135 (or multi-method method)))))
6136
6137
6138 ;; Auto saving to a special directory.
6139
6140 (defun tramp-make-auto-save-file-name (fn)
6141 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
6142 (when tramp-auto-save-directory
6143 (unless (file-exists-p tramp-auto-save-directory)
6144 (make-directory tramp-auto-save-directory t)))
6145 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
6146 ;; file name we make sure that jka-compr isn't used for the
6147 ;; auto-save file.
6148 (let ((buffer-file-name (expand-file-name
6149 (tramp-subst-strs-in-string '(("_" . "|")
6150 ("/" . "_a")
6151 (":" . "_b")
6152 ("|" . "__")
6153 ("[" . "_l")
6154 ("]" . "_r"))
6155 fn)
6156 tramp-auto-save-directory)))
6157 (make-auto-save-file-name)))
6158
6159 (defadvice make-auto-save-file-name
6160 (around tramp-advice-make-auto-save-file-name () activate)
6161 "Invoke `tramp-make-auto-save-file-name' for tramp files."
6162 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
6163 tramp-auto-save-directory)
6164 (setq ad-return-value
6165 (tramp-make-auto-save-file-name (buffer-file-name)))
6166 ad-do-it))
6167
6168 (defun tramp-subst-strs-in-string (alist string)
6169 "Replace all occurrences of the string FROM with TO in STRING.
6170 ALIST is of the form ((FROM . TO) ...)."
6171 (save-match-data
6172 (while alist
6173 (let* ((pr (car alist))
6174 (from (car pr))
6175 (to (cdr pr)))
6176 (while (string-match (regexp-quote from) string)
6177 (setq string (replace-match to t t string)))
6178 (setq alist (cdr alist))))
6179 string))
6180
6181 (defun tramp-insert-with-face (face string)
6182 "Insert text with a specific face."
6183 (let ((start (point)))
6184 (insert string)
6185 (add-text-properties start (point) (list 'face face))))
6186
6187 ;; ------------------------------------------------------------
6188 ;; -- Compatibility functions section --
6189 ;; ------------------------------------------------------------
6190
6191 (defun tramp-temporary-file-directory ()
6192 "Return name of directory for temporary files (compat function).
6193 For Emacs, this is the variable `temporary-file-directory', for XEmacs
6194 this is the function `temp-directory'."
6195 (cond ((boundp 'temporary-file-directory)
6196 (symbol-value 'temporary-file-directory))
6197 ((fboundp 'temp-directory)
6198 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
6199 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
6200 (file-name-as-directory (getenv "TEMP")))
6201 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
6202 (file-name-as-directory (getenv "TMP")))
6203 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
6204 (file-name-as-directory (getenv "TMPDIR")))
6205 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
6206 (t (message (concat "Neither `temporary-file-directory' nor "
6207 "`temp-directory' is defined -- using /tmp."))
6208 (file-name-as-directory "/tmp"))))
6209
6210 (defun tramp-read-passwd (prompt)
6211 "Read a password from user (compat function).
6212 Invokes `read-passwd' if that is defined, else `ange-ftp-read-passwd'."
6213 (apply
6214 (if (fboundp 'read-passwd) #'read-passwd #'ange-ftp-read-passwd)
6215 (list prompt)))
6216
6217 (defun tramp-time-diff (t1 t2)
6218 "Return the difference between the two times, in seconds.
6219 T1 and T2 are time values (as returned by `current-time' for example).
6220
6221 NOTE: This function will fail if the time difference is too large to
6222 fit in an integer."
6223 ;; Pacify byte-compiler with `symbol-function'.
6224 (cond ((fboundp 'subtract-time)
6225 (cadr (funcall (symbol-function 'subtract-time) t1 t2)))
6226 ((fboundp 'itimer-time-difference)
6227 (floor (funcall
6228 (symbol-function 'itimer-time-difference)
6229 (if (< (length t1) 3) (append t1 '(0)) t1)
6230 (if (< (length t2) 3) (append t2 '(0)) t2))))
6231 (t
6232 ;; snarfed from Emacs 21 time-date.el
6233 (cadr (let ((borrow (< (cadr t1) (cadr t2))))
6234 (list (- (car t1) (car t2) (if borrow 1 0))
6235 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))))
6236
6237 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
6238 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
6239 EOL-TYPE can be one of `dos', `unix', or `mac'."
6240 (cond ((fboundp 'coding-system-change-eol-conversion)
6241 (apply #'coding-system-change-eol-conversion
6242 (list coding-system eol-type)))
6243 ((fboundp 'subsidiary-coding-system)
6244 (apply
6245 #'subsidiary-coding-system
6246 (list coding-system
6247 (cond ((eq eol-type 'dos) 'crlf)
6248 ((eq eol-type 'unix) 'lf)
6249 ((eq eol-type 'mac) 'cr)
6250 (t
6251 (error "Unknown EOL-TYPE `%s', must be %s"
6252 eol-type
6253 "`dos', `unix', or `mac'"))))))
6254 (t (error "Can't change EOL conversion -- is MULE missing?"))))
6255
6256 (defun tramp-split-string (string pattern)
6257 "Like `split-string' but omit empty strings.
6258 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
6259 This is, the first, empty, element is omitted. In XEmacs, the first
6260 element is not omitted.
6261
6262 Note: this function has been written for `tramp-handle-file-truename'.
6263 If you want to use it for something else, you'll have to check whether
6264 it does the right thing."
6265 (delete "" (split-string string pattern)))
6266
6267 ;; ------------------------------------------------------------
6268 ;; -- Kludges section --
6269 ;; ------------------------------------------------------------
6270
6271 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
6272 ;; does not deal well with newline characters. Newline is replaced by
6273 ;; backslash newline. But if, say, the string `a backslash newline b'
6274 ;; is passed to a shell, the shell will expand this into "ab",
6275 ;; completely omitting the newline. This is not what was intended.
6276 ;; It does not appear to be possible to make the function
6277 ;; `shell-quote-argument' work with newlines without making it
6278 ;; dependent on the shell used. But within this package, we know that
6279 ;; we will always use a Bourne-like shell, so we use an approach which
6280 ;; groks newlines.
6281 ;;
6282 ;; The approach is simple: we call `shell-quote-argument', then
6283 ;; massage the newline part of the result.
6284 ;;
6285 ;; This function should produce a string which is grokked by a Unix
6286 ;; shell, even if the Emacs is running on Windows. Since this is the
6287 ;; kludges section, we bind `system-type' in such a way that
6288 ;; `shell-quote-arguments' behaves as if on Unix.
6289 ;;
6290 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
6291 ;; function to work with Bourne-like shells.
6292 ;;
6293 ;; CCC: This function should be rewritten so that
6294 ;; `shell-quote-argument' is not used. This way, we are safe from
6295 ;; changes in `shell-quote-argument'.
6296 (defun tramp-shell-quote-argument (s)
6297 "Similar to `shell-quote-argument', but groks newlines.
6298 Only works for Bourne-like shells."
6299 (let ((system-type 'not-windows))
6300 (save-match-data
6301 (let ((result (shell-quote-argument s))
6302 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
6303 (when (and (>= (length result) 2)
6304 (string= (substring result 0 2) "\\~"))
6305 (setq result (substring result 1)))
6306 (while (string-match nl result)
6307 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
6308 t t result)))
6309 result))))
6310
6311 ;; ;; EFS hooks itself into the file name handling stuff in more places
6312 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
6313 ;; ;; away from tramp.el file names.
6314 ;; ;;
6315 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
6316 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
6317 ;; ;; connection to help it's dired process. Not that I have any real
6318 ;; ;; idea *why* this is helpful to dired.
6319 ;; ;;
6320 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
6321 ;; ;;
6322 ;; ;; Daniel Pittman <daniel@danann.net>
6323 ;; ;;
6324 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
6325 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
6326 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
6327 ;; ;; EFS might nicht be loaded yet.
6328 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
6329 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
6330 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
6331 ;; nil
6332 ;; ad-do-it))
6333
6334 ;; We currently (sometimes) use "[" and "]" in the filename format.
6335 ;; This means that Emacs wants to expand wildcards if
6336 ;; `find-file-wildcards' is non-nil, and then barfs because no
6337 ;; expansion could be found. We detect this situation and do
6338 ;; something really awful: we have `file-expand-wildcards' return the
6339 ;; original filename if it can't expand anything. Let's just hope
6340 ;; that this doesn't break anything else.
6341 ;; CCC: This check is now also really awful; we should search all
6342 ;; of the filename format, not just the prefix.
6343 (when (string-match "\\[" tramp-prefix-format)
6344 (defadvice file-expand-wildcards (around tramp-fix activate)
6345 (let ((name (ad-get-arg 0)))
6346 (if (tramp-tramp-file-p name)
6347 ;; If it's a Tramp file, dissect it and look if wildcards
6348 ;; need to be expanded at all.
6349 (let ((v (tramp-dissect-file-name name)))
6350 (if (string-match "[[*?]" (tramp-file-name-localname v))
6351 (let ((res ad-do-it))
6352 (setq ad-return-value (or res (list name))))
6353 (setq ad-return-value (list name))))
6354 ;; If it is not a Tramp file, just run the original function.
6355 (let ((res ad-do-it))
6356 (setq ad-return-value (or res (list name)))))))
6357 )
6358
6359 ;; Tramp version is useful in a number of situations.
6360
6361 (defun tramp-version (arg)
6362 "Print version number of tramp.el in minibuffer or current buffer."
6363 (interactive "P")
6364 (if arg (insert tramp-version) (message tramp-version)))
6365
6366 ;; Make the `reporter` functionality available for making bug reports about
6367 ;; the package. A most useful piece of code.
6368
6369 (unless (fboundp 'reporter-submit-bug-report)
6370 (autoload 'reporter-submit-bug-report "reporter"))
6371
6372 (defun tramp-bug ()
6373 "Submit a bug report to the TRAMP developers."
6374 (interactive)
6375 (require 'reporter)
6376 (let ((reporter-prompt-for-summary-p t))
6377 (reporter-submit-bug-report
6378 tramp-bug-report-address ; to-address
6379 (format "tramp (%s)" tramp-version) ; package name and version
6380 `(;; Current state
6381 tramp-ls-command
6382 tramp-test-groks-nt
6383 tramp-file-exists-command
6384 tramp-current-multi-method
6385 tramp-current-method
6386 tramp-current-user
6387 tramp-current-host
6388
6389 ;; System defaults
6390 tramp-auto-save-directory ; vars to dump
6391 tramp-default-method
6392 tramp-rsh-end-of-line
6393 tramp-password-end-of-line
6394 tramp-remote-path
6395 tramp-login-prompt-regexp
6396 tramp-password-prompt-regexp
6397 tramp-wrong-passwd-regexp
6398 tramp-yesno-prompt-regexp
6399 tramp-yn-prompt-regexp
6400 tramp-temp-name-prefix
6401 tramp-file-name-structure
6402 tramp-file-name-regexp
6403 tramp-multi-file-name-structure
6404 tramp-multi-file-name-hop-structure
6405 tramp-multi-methods
6406 tramp-multi-connection-function-alist
6407 tramp-methods
6408 tramp-end-of-output
6409 tramp-coding-commands
6410 tramp-actions-before-shell
6411 tramp-multi-actions
6412 tramp-terminal-type
6413 tramp-shell-prompt-pattern
6414 tramp-chunksize
6415
6416 ;; Non-tramp variables of interest
6417 shell-prompt-pattern
6418 backup-by-copying
6419 backup-by-copying-when-linked
6420 backup-by-copying-when-mismatch
6421 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
6422 'backup-by-copying-when-privileged-mismatch)
6423 file-name-handler-alist)
6424 nil ; pre-hook
6425 nil ; post-hook
6426 "\
6427 Enter your bug report in this message, including as much detail as you
6428 possibly can about the problem, what you did to cause it and what the
6429 local and remote machines are.
6430
6431 If you can give a simple set of instructions to make this bug happen
6432 reliably, please include those. Thank you for helping kill bugs in
6433 TRAMP.
6434
6435 Another useful thing to do is to put (setq tramp-debug-buffer t) in
6436 the ~/.emacs file and to repeat the bug. Then, include the contents
6437 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
6438 report.
6439
6440 --bug report follows this line--
6441 ")))
6442
6443 (defalias 'tramp-submit-bug 'tramp-bug)
6444
6445 (provide 'tramp)
6446
6447 ;; Make sure that we get integration with the VC package.
6448 ;; When it is loaded, we need to pull in the integration module.
6449 ;; This must come after (provide 'tramp) because tramp-vc.el
6450 ;; requires tramp.
6451 (eval-after-load "vc"
6452 '(require 'tramp-vc))
6453
6454 ;;; TODO:
6455
6456 ;; * Allow putting passwords in the filename.
6457 ;; This should be implemented via a general mechanism to add
6458 ;; parameters in filenames. There is currently a kludge for
6459 ;; putting the port number into the filename for ssh and ftp
6460 ;; files. This could be subsumed by the new mechanism as well.
6461 ;; Another approach is to read a netrc file like ~/.authinfo
6462 ;; from Gnus.
6463 ;; * Handle nonlocal exits such as C-g.
6464 ;; * Autodetect if remote `ls' groks the "--dired" switch.
6465 ;; * Add fallback for inline encodings. This should be used
6466 ;; if the remote end doesn't support mimencode or a similar program.
6467 ;; For reading files from the remote host, we can just parse the output
6468 ;; of `od -b'. For writing files to the remote host, we construct
6469 ;; a shell program which contains only "safe" ascii characters
6470 ;; and which writes the right bytes to the file. We can use printf(1)
6471 ;; or "echo -e" or the printf function in awk and use octal escapes
6472 ;; for the "dangerous" characters. The null byte might be a problem.
6473 ;; On some systems, the octal escape doesn't work. So we try the following
6474 ;; two commands to write a null byte:
6475 ;; dd if=/dev/zero bs=1 count=1
6476 ;; echo | tr '\n' '\000'
6477 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
6478 ;; the two via a format which can be `uu' or `b64'. Then we can search
6479 ;; for the right local commands and the right remote commands separately.
6480 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
6481 ;; work for remote files.
6482 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
6483 ;; `shell-quote-argument'.
6484 ;; * Completion gets confused when you leave out the method name.
6485 ;; * Support `dired-compress-file' filename handler.
6486 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
6487 ;; by the files in that directory. Add this here.
6488 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
6489 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
6490 ;; * When logging in, keep looking for questions according to an alist
6491 ;; and then invoke the right function.
6492 ;; * Case-insensitive filename completion. (Norbert Goevert.)
6493 ;; * Running CVS remotely doesn't appear to work right. It thinks
6494 ;; files are locked by somebody else even if I'm the locking user.
6495 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
6496 ;; (Skip Montanaro)
6497 ;; * Don't use globbing for directories with many files, as this is
6498 ;; likely to produce long command lines, and some shells choke on
6499 ;; long command lines.
6500 ;; * Find out about the new auto-save mechanism in Emacs 21 and
6501 ;; do the right thing.
6502 ;; * `vc-directory' does not work. It never displays any files, even
6503 ;; if it does show files when run locally.
6504 ;; * Allow correction of passwords, if the remote end allows this.
6505 ;; (Mark Hershberger)
6506 ;; * Make sure permissions of tmp file are good.
6507 ;; (Nelson Minar <nelson@media.mit.edu>)
6508 ;; * Grok passwd prompts with scp? (David Winter
6509 ;; <winter@nevis1.nevis.columbia.edu>). Maybe just do `ssh -l user
6510 ;; host', then wait a while for the passwd or passphrase prompt. If
6511 ;; there is one, remember the passwd/phrase.
6512 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
6513 ;; * Do asynchronous `shell-command's.
6514 ;; * Grok `append' parameter for `write-region'.
6515 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
6516 ;; * abbreviate-file-name
6517 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
6518 ;; * `C' in dired gives error `not tramp file name'.
6519 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
6520 ;; what the user names should default to, though.
6521 ;; * better error checking. At least whenever we see something
6522 ;; strange when doing zerop, we should kill the process and start
6523 ;; again. (Greg Stark)
6524 ;; * Add caching for filename completion. (Greg Stark)
6525 ;; Of course, this has issues with usability (stale cache bites)
6526 ;; -- <daniel@danann.net>
6527 ;; * Provide a local cache of old versions of remote files for the rsync
6528 ;; transfer method to use. (Greg Stark)
6529 ;; * Remove unneeded parameters from methods.
6530 ;; * Invoke rsync once for copying a whole directory hierarchy.
6531 ;; (Francesco Potortì)
6532 ;; * Should we set PATH ourselves or should we rely on the remote end
6533 ;; to do it?
6534 ;; * Do the autoconf thing.
6535 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
6536 ;; * Allow non-Unix remote systems. (More a long-term thing.)
6537 ;; * Make it work for different encodings, and for different file name
6538 ;; encodings, too. (Daniel Pittman)
6539 ;; * Change applicable functions to pass a struct tramp-file-name rather
6540 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
6541 ;; * Implement asynchronous shell commands.
6542 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
6543 ;; * Progress reports while copying files. (Michael Kifer)
6544 ;; * `Smart' connection method that uses inline for small and out of
6545 ;; band for large files. (Michael Kifer)
6546 ;; * Don't search for perl5 and perl. Instead, only search for perl and
6547 ;; then look if it's the right version (with `perl -v').
6548 ;; * When editing a remote CVS controlled file as a different user, VC
6549 ;; gets confused about the file locking status. Try to find out why
6550 ;; the workaround doesn't work.
6551 ;; * When user is running ssh-agent, it would be useful to add the
6552 ;; passwords typed by the user to that agent. This way, the next time
6553 ;; round, the users don't have to type all this in again.
6554 ;; This would be especially useful for start-process, I think.
6555 ;; An easy way to implement start-process is to open a second shell
6556 ;; connection which is inconvenient if the user has to reenter
6557 ;; passwords.
6558 ;; * Change `copy-file' to grok the case where the filename handler
6559 ;; for the source and the target file are different. Right now,
6560 ;; it looks at the source file and then calls that handler, if
6561 ;; there is one. But since ange-ftp, for instance, does not know
6562 ;; about Tramp, it does not do the right thing if the target file
6563 ;; name is a Tramp name.
6564 ;; * Username and hostname completion.
6565 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
6566 ;; connect to host "blabla" already if that host is unique. No idea
6567 ;; how to suppress. Maybe not an essential problem.
6568 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
6569 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
6570 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
6571 ;; Code is nearly identical.
6572 ;; ** Decide whiche files to take for searching user/host names depending on
6573 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
6574 ;; ** Enhance variables for debug.
6575 ;; ** Implement "/multi:" completion.
6576 ;; ** Add a learning mode for completion. Make results persistent.
6577
6578 ;; Functions for file-name-handler-alist:
6579 ;; diff-latest-backup-file -- in diff.el
6580 ;; dired-compress-file
6581 ;; dired-uncache -- this will be needed when we do insert-directory caching
6582 ;; file-name-as-directory -- use primitive?
6583 ;; file-name-directory -- use primitive?
6584 ;; file-name-nondirectory -- use primitive?
6585 ;; file-name-sans-versions -- use primitive?
6586 ;; file-newer-than-file-p
6587 ;; find-backup-file-name
6588 ;; get-file-buffer -- use primitive
6589 ;; load
6590 ;; unhandled-file-name-directory
6591 ;; vc-registered
6592
6593 ;;; tramp.el ends here