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