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