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