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