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