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