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