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