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