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