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