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