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