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