1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol -*- coding: iso-8859-1; -*-
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Kai.Grossjohann@CS.Uni-Dortmund.DE
6 ;; Keywords: comm, processes
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
32 ;; For more detailed instructions, please see the info file, which is
33 ;; included in the file `tramp.tar.gz' mentioned below.
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.)
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.
47 ;; Also see the todo list at the bottom of this file.
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.
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
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
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.
68 ;; Don't forget to put on your asbestos longjohns, first!
72 (defconst tramp-version "2.0.1"
73 "This version of tramp.")
74 (defconst tramp-bug-report-address "tramp-devel@mail.freesoftware.fsf.org"
75 "Email address to send bug reports to.")
78 (require 'format-spec) ;from Gnus 5.8, also in tar ball
79 (require 'base64) ;for the mimencode methods
83 ;; ;; It does not work to load EFS after loading TRAMP.
84 ;; (when (fboundp 'efs-file-handler-function)
90 ;; Emacs 19.34 compatibility hack -- is this needed?
91 (or (>= emacs-major-version 20)
94 (unless (boundp 'custom-print-functions)
95 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
97 ;;; User Customizable Internal Variables:
100 "Edit remote files with a combination of rsh and rcp or similar programs."
103 (defcustom tramp-verbose 10
104 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
108 (defcustom tramp-debug-buffer nil
109 "*Whether to send all commands and responses to a debug buffer."
113 (defcustom tramp-auto-save-directory nil
114 "*Put auto-save files in this directory, if set.
115 The idea is to use a local directory so that auto-saving is faster."
117 :type '(choice (const nil)
120 (defcustom tramp-sh-program "/bin/sh"
121 "*Use this program for shell commands on the local host.
122 This MUST be a Bourne-like shell. This shell is used to execute
123 the encoding and decoding command on the local host, so if you
124 want to use `~' in those commands, you should choose a shell here
125 which groks tilde expansion. `/bin/sh' normally does not
126 understand tilde expansion.
128 Note that this variable is not used for remote commands. There are
129 mechanisms in tramp.el which automatically determine the right shell to
130 use for the remote host."
132 :type '(file :must-match t))
134 ;; CCC I have changed all occurrences of comint-quote-filename with
135 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
136 ;; There, comint-quote-filename was removed altogether. If it turns
137 ;; out to be necessary there, something will need to be done.
138 ;;-(defcustom tramp-file-name-quote-list
139 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
140 ;;- "*Protect these characters from the remote shell.
141 ;;-Any character in this list is quoted (preceded with a backslash)
142 ;;-because it means something special to the shell. This takes effect
143 ;;-when sending file and directory names to the remote shell.
145 ;;-See `comint-file-name-quote-list' for details."
147 ;;- :type '(repeat character))
149 (defcustom tramp-methods
150 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
151 (tramp-rsh-program "rsh")
152 (tramp-rcp-program "rcp")
153 (tramp-remote-sh "/bin/sh")
156 (tramp-rcp-keep-date-arg "-p")
157 (tramp-su-program nil)
159 (tramp-encoding-command nil)
160 (tramp-decoding-command nil)
161 (tramp-encoding-function nil)
162 (tramp-decoding-function nil)
163 (tramp-telnet-program nil)
164 (tramp-telnet-args nil))
165 ("scp" (tramp-connection-function tramp-open-connection-rsh)
166 (tramp-rsh-program "ssh")
167 (tramp-rcp-program "scp")
168 (tramp-remote-sh "/bin/sh")
169 (tramp-rsh-args ("-e" "none"))
171 (tramp-rcp-keep-date-arg "-p")
172 (tramp-su-program nil)
174 (tramp-encoding-command nil)
175 (tramp-decoding-command nil)
176 (tramp-encoding-function nil)
177 (tramp-decoding-function nil)
178 (tramp-telnet-program nil)
179 (tramp-telnet-args nil))
180 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
181 (tramp-rsh-program "ssh1")
182 (tramp-rcp-program "scp1")
183 (tramp-remote-sh "/bin/sh")
184 (tramp-rsh-args ("-e" "none"))
186 (tramp-rcp-keep-date-arg "-p")
187 (tramp-su-program nil)
189 (tramp-encoding-command nil)
190 (tramp-decoding-command nil)
191 (tramp-encoding-function nil)
192 (tramp-decoding-function nil)
193 (tramp-telnet-program nil)
194 (tramp-telnet-args nil))
195 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
196 (tramp-rsh-program "ssh2")
197 (tramp-rcp-program "scp2")
198 (tramp-remote-sh "/bin/sh")
199 (tramp-rsh-args ("-e" "none"))
201 (tramp-rcp-keep-date-arg "-p")
202 (tramp-su-program nil)
204 (tramp-encoding-command nil)
205 (tramp-decoding-command nil)
206 (tramp-encoding-function nil)
207 (tramp-decoding-function nil)
208 (tramp-telnet-program nil)
209 (tramp-telnet-args nil))
210 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
211 (tramp-rsh-program "ssh")
212 (tramp-rcp-program "rsync")
213 (tramp-remote-sh "/bin/sh")
214 (tramp-rsh-args ("-e" "none"))
215 (tramp-rcp-args ("-e" "ssh"))
216 (tramp-rcp-keep-date-arg "-t")
217 (tramp-su-program nil)
219 (tramp-encoding-command nil)
220 (tramp-decoding-command nil)
221 (tramp-encoding-function nil)
222 (tramp-decoding-function nil)
223 (tramp-telnet-program nil)
224 (tramp-telnet-args nil))
225 ("ru" (tramp-connection-function tramp-open-connection-rsh)
226 (tramp-rsh-program "rsh")
227 (tramp-rcp-program nil)
228 (tramp-remote-sh "/bin/sh")
231 (tramp-rcp-keep-date-arg nil)
232 (tramp-su-program nil)
234 (tramp-encoding-command "uuencode xxx")
235 (tramp-decoding-command
236 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
237 (tramp-encoding-function nil)
238 (tramp-decoding-function uudecode-decode-region)
239 (tramp-telnet-program nil)
240 (tramp-telnet-args nil))
241 ("su" (tramp-connection-function tramp-open-connection-rsh)
242 (tramp-rsh-program "ssh")
243 (tramp-rcp-program nil)
244 (tramp-remote-sh "/bin/sh")
245 (tramp-rsh-args ("-e" "none"))
247 (tramp-rcp-keep-date-arg nil)
248 (tramp-su-program nil)
250 (tramp-encoding-command "uuencode xxx")
251 (tramp-decoding-command
252 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
253 (tramp-encoding-function nil)
254 (tramp-decoding-function uudecode-decode-region)
255 (tramp-telnet-program nil)
256 (tramp-telnet-args nil))
257 ("su1" (tramp-connection-function tramp-open-connection-rsh)
258 (tramp-rsh-program "ssh1")
259 (tramp-rcp-program nil)
260 (tramp-remote-sh "/bin/sh")
261 (tramp-rsh-args ("-e" "none"))
263 (tramp-rcp-keep-date-arg nil)
264 (tramp-su-program nil)
266 (tramp-encoding-command "uuencode xxx")
267 (tramp-decoding-command
268 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
269 (tramp-encoding-function nil)
270 (tramp-decoding-function uudecode-decode-region)
271 (tramp-telnet-program nil)
272 (tramp-telnet-args nil))
273 ("su2" (tramp-connection-function tramp-open-connection-rsh)
274 (tramp-rsh-program "ssh2")
275 (tramp-rcp-program nil)
276 (tramp-remote-sh "/bin/sh")
277 (tramp-rsh-args ("-e" "none"))
279 (tramp-rcp-keep-date-arg nil)
280 (tramp-su-program nil)
282 (tramp-encoding-command "uuencode xxx")
283 (tramp-decoding-command
284 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
285 (tramp-encoding-function nil)
286 (tramp-decoding-function uudecode-decode-region)
287 (tramp-telnet-program nil)
288 (tramp-telnet-args nil))
289 ("rm" (tramp-connection-function tramp-open-connection-rsh)
290 (tramp-rsh-program "rsh")
291 (tramp-rcp-program nil)
292 (tramp-remote-sh "/bin/sh")
295 (tramp-rcp-keep-date-arg nil)
296 (tramp-su-program nil)
298 (tramp-encoding-command "mimencode -b")
299 (tramp-decoding-command "mimencode -u -b")
300 (tramp-encoding-function base64-encode-region)
301 (tramp-decoding-function base64-decode-region)
302 (tramp-telnet-program nil)
303 (tramp-telnet-args nil))
304 ("sm" (tramp-connection-function tramp-open-connection-rsh)
305 (tramp-rsh-program "ssh")
306 (tramp-rcp-program nil)
307 (tramp-remote-sh "/bin/sh")
308 (tramp-rsh-args ("-e" "none"))
310 (tramp-rcp-keep-date-arg nil)
311 (tramp-su-program nil)
313 (tramp-encoding-command "mimencode -b")
314 (tramp-decoding-command "mimencode -u -b")
315 (tramp-encoding-function base64-encode-region)
316 (tramp-decoding-function base64-decode-region)
317 (tramp-telnet-program nil)
318 (tramp-telnet-args nil))
319 ("smp" (tramp-connection-function tramp-open-connection-rsh)
320 (tramp-rsh-program "ssh")
321 (tramp-rcp-program nil)
322 (tramp-remote-sh "/bin/sh")
323 (tramp-rsh-args ("-e" "none"))
325 (tramp-rcp-keep-date-arg nil)
326 (tramp-su-program nil)
328 (tramp-encoding-command "tramp_mimencode")
329 (tramp-decoding-command "tramp_mimedecode")
330 (tramp-encoding-function base64-encode-region)
331 (tramp-decoding-function base64-decode-region)
332 (tramp-telnet-program nil))
333 ("sm1" (tramp-connection-function tramp-open-connection-rsh)
334 (tramp-rsh-program "ssh1")
335 (tramp-rcp-program nil)
336 (tramp-remote-sh "/bin/sh")
337 (tramp-rsh-args ("-e" "none"))
339 (tramp-rcp-keep-date-arg nil)
340 (tramp-su-program nil)
342 (tramp-encoding-command "mimencode -b")
343 (tramp-decoding-command "mimencode -u -b")
344 (tramp-encoding-function base64-encode-region)
345 (tramp-decoding-function base64-decode-region)
346 (tramp-telnet-program nil)
347 (tramp-telnet-args nil))
348 ("sm2" (tramp-connection-function tramp-open-connection-rsh)
349 (tramp-rsh-program "ssh2")
350 (tramp-rcp-program nil)
351 (tramp-remote-sh "/bin/sh")
352 (tramp-rsh-args ("-e" "none"))
354 (tramp-rcp-keep-date-arg nil)
355 (tramp-su-program nil)
357 (tramp-encoding-command "mimencode -b")
358 (tramp-decoding-command "mimencode -u -b")
359 (tramp-encoding-function base64-encode-region)
360 (tramp-decoding-function base64-decode-region)
361 (tramp-telnet-program nil)
362 (tramp-telnet-args nil))
363 ("tm" (tramp-connection-function tramp-open-connection-telnet)
364 (tramp-rsh-program nil)
365 (tramp-rcp-program nil)
366 (tramp-remote-sh "/bin/sh")
369 (tramp-rcp-keep-date-arg nil)
370 (tramp-su-program nil)
372 (tramp-encoding-command "mimencode -b")
373 (tramp-decoding-command "mimencode -u -b")
374 (tramp-encoding-function base64-encode-region)
375 (tramp-decoding-function base64-decode-region)
376 (tramp-telnet-program "telnet")
377 (tramp-telnet-args nil))
378 ("tu" (tramp-connection-function tramp-open-connection-telnet)
379 (tramp-rsh-program nil)
380 (tramp-rcp-program nil)
381 (tramp-remote-sh "/bin/sh")
384 (tramp-rcp-keep-date-arg nil)
385 (tramp-su-program nil)
387 (tramp-encoding-command "uuencode xxx")
388 (tramp-decoding-command
389 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
390 (tramp-encoding-function nil)
391 (tramp-decoding-function uudecode-decode-region)
392 (tramp-telnet-program "telnet")
393 (tramp-telnet-args nil))
394 ("sum" (tramp-connection-function tramp-open-connection-su)
395 (tramp-rsh-program nil)
396 (tramp-rcp-program nil)
397 (tramp-remote-sh "/bin/sh")
400 (tramp-rcp-keep-date-arg nil)
401 (tramp-su-program "su")
402 (tramp-su-args ("-" "%u"))
403 (tramp-encoding-command "mimencode -b")
404 (tramp-decoding-command "mimencode -u -b")
405 (tramp-encoding-function base64-encode-region)
406 (tramp-decoding-function base64-decode-region)
407 (tramp-telnet-program nil)
408 (tramp-telnet-args nil))
409 ("suu" (tramp-connection-function tramp-open-connection-su)
410 (tramp-rsh-program nil)
411 (tramp-rcp-program nil)
412 (tramp-remote-sh "/bin/sh")
415 (tramp-rcp-keep-date-arg nil)
416 (tramp-su-program "su")
417 (tramp-su-args ("-" "%u"))
418 (tramp-encoding-command "uuencode xxx")
419 (tramp-decoding-command
420 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
421 (tramp-encoding-function nil)
422 (tramp-decoding-function uudecode-decode-region)
423 (tramp-telnet-program nil)
424 (tramp-telnet-args nil))
425 ("sudm" (tramp-connection-function tramp-open-connection-su)
426 (tramp-rsh-program nil)
427 (tramp-rcp-program nil)
428 (tramp-remote-sh "/bin/sh")
431 (tramp-rcp-keep-date-arg nil)
432 (tramp-su-program "sudo")
433 (tramp-su-args ("-u" "%u" "-s"))
434 (tramp-encoding-command "mimencode -b")
435 (tramp-decoding-command "mimencode -u -b")
436 (tramp-encoding-function base64-encode-region)
437 (tramp-decoding-function base64-decode-region)
438 (tramp-telnet-program nil)
439 (tramp-telnet-args nil))
440 ("sudu" (tramp-connection-function tramp-open-connection-su)
441 (tramp-rsh-program nil)
442 (tramp-rcp-program nil)
443 (tramp-remote-sh "/bin/sh")
446 (tramp-rcp-keep-date-arg nil)
447 (tramp-su-program "sudo")
448 (tramp-su-args ("-u" "%u" "-s"))
449 (tramp-encoding-command "uuencode xxx")
450 (tramp-decoding-command
451 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
452 (tramp-encoding-function nil)
453 (tramp-decoding-function uudecode-decode-region)
454 (tramp-telnet-program nil)
455 (tramp-telnet-args nil))
456 ("multi" (tramp-connection-function tramp-open-connection-multi)
457 (tramp-rsh-program nil)
458 (tramp-rcp-program nil)
459 (tramp-remote-sh "/bin/sh")
462 (tramp-rcp-keep-date-arg nil)
463 (tramp-su-program nil)
465 (tramp-encoding-command "mimencode -b")
466 (tramp-decoding-command "mimencode -u -b")
467 (tramp-encoding-function base64-encode-region)
468 (tramp-decoding-function base64-decode-region)
469 (tramp-telnet-program nil)
470 (tramp-telnet-args nil))
471 ("multiu" (tramp-connection-function tramp-open-connection-multi)
472 (tramp-rsh-program nil)
473 (tramp-rcp-program nil)
474 (tramp-remote-sh "/bin/sh")
477 (tramp-rcp-keep-date-arg nil)
478 (tramp-su-program nil)
480 (tramp-encoding-command "uuencode xxx")
481 (tramp-decoding-command
482 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
483 (tramp-encoding-function nil)
484 (tramp-decoding-function uudecode-decode-region)
485 (tramp-telnet-program nil)
486 (tramp-telnet-args nil))
487 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
488 (tramp-rsh-program "ssh")
489 (tramp-rcp-program "scp")
490 (tramp-remote-sh "/bin/sh")
491 (tramp-rsh-args ("-e" "none" "-t" "-t" "/bin/sh"))
493 (tramp-rcp-keep-date-arg "-p")
494 (tramp-encoding-command nil)
495 (tramp-decoding-command nil)
496 (tramp-encoding-function nil)
497 (tramp-decoding-function nil)
498 (tramp-telnet-program nil)
499 (tramp-telnet-args nil))
500 ("smx" (tramp-connection-function tramp-open-connection-rsh)
501 (tramp-rsh-program "ssh")
502 (tramp-rcp-program nil)
503 (tramp-remote-sh "/bin/sh")
504 (tramp-rsh-args ("-e" "none" "-t" "-t" "/bin/sh"))
506 (tramp-rcp-keep-date-arg nil)
507 (tramp-su-program nil)
509 (tramp-encoding-command "mimencode -b")
510 (tramp-decoding-command "mimencode -u -b")
511 (tramp-encoding-function base64-encode-region)
512 (tramp-decoding-function base64-decode-region)
513 (tramp-telnet-program nil)
514 (tramp-telnet-args nil))
516 (tramp-connection-function tramp-open-connection-rsh)
517 (tramp-rsh-program "krlogin")
518 (tramp-rcp-program nil)
519 (tramp-remote-sh "/bin/sh")
520 (tramp-rsh-args ("-x"))
522 (tramp-rcp-keep-date-arg nil)
523 (tramp-su-program nil)
525 (tramp-encoding-command "mimencode -b")
526 (tramp-decoding-command "mimencode -u -b")
527 (tramp-encoding-function base64-encode-region)
528 (tramp-decoding-function base64-decode-region)
529 (tramp-telnet-program nil)
530 (tramp-telnet-args nil))
532 (tramp-connection-function tramp-open-connection-rsh)
533 (tramp-rsh-program "plink")
534 (tramp-rcp-program nil)
535 (tramp-remote-sh "/bin/sh")
536 (tramp-rsh-args ("-ssh")) ;optionally add "-v"
538 (tramp-rcp-keep-date-arg nil)
539 (tramp-su-program nil)
541 (tramp-encoding-command "uuencode xxx")
542 (tramp-decoding-command
543 "( uudecode -o - 2>/dev/null || uudecode -p 2>/dev/null )")
544 (tramp-encoding-function nil)
545 (tramp-decoding-function uudecode-decode-region)
546 (tramp-telnet-program nil)
547 (tramp-telnet-args nil))
549 (tramp-connection-function tramp-open-connection-rsh)
550 (tramp-rsh-program "plink")
551 (tramp-rcp-program nil)
552 (tramp-remote-sh "/bin/sh")
553 (tramp-rsh-args ("-ssh")) ;optionally add "-v"
555 (tramp-rcp-keep-date-arg nil)
556 (tramp-su-program nil)
558 (tramp-encoding-command "mimencode -b")
559 (tramp-decoding-command "mimencode -u -b")
560 (tramp-encoding-function base64-encode-region)
561 (tramp-decoding-function base64-decode-region)
562 (tramp-telnet-program nil)
563 (tramp-telnet-args nil))
565 (tramp-connection-function tramp-open-connection-rsh)
566 (tramp-rsh-program "plink")
567 (tramp-rcp-program "pscp")
568 (tramp-remote-sh "/bin/sh")
569 (tramp-rsh-args ("-ssh"))
571 (tramp-rcp-keep-date-arg "-p")
572 (tramp-su-program nil)
574 (tramp-encoding-command nil)
575 (tramp-decoding-command nil)
576 (tramp-encoding-function nil)
577 (tramp-decoding-function nil)
578 (tramp-telnet-program nil)
579 (tramp-telnet-args nil))
581 (tramp-connection-function tramp-open-connection-rsh)
582 (tramp-rsh-program "fsh")
583 (tramp-rcp-program "fcp")
584 (tramp-remote-sh "/bin/sh -i")
585 (tramp-rsh-args ("sh" "-i"))
587 (tramp-rcp-keep-date-arg "-p")
588 (tramp-su-program nil)
590 (tramp-encoding-command nil)
591 (tramp-decoding-command nil)
592 (tramp-encoding-function nil)
593 (tramp-decoding-function nil)
594 (tramp-telnet-program nil)
595 (tramp-telnet-args nil))
597 "*Alist of methods for remote files.
598 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
599 Each NAME stands for a remote access method. Each PARAM is a
600 pair of the form (KEY VALUE). The following KEYs are defined:
601 * `tramp-connection-function'
602 This specifies the function to use to connect to the remote host.
603 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
604 and `tramp-open-connection-su' are defined. See the documentation
605 of these functions for more details.
607 This specifies the Bourne shell to use on the remote host. This
608 MUST be a Bourne-like shell. It is normally not necessary to set
609 this to any value other than \"/bin/sh\": tramp wants to use a shell
610 which groks tilde expansion, but it can search for it. Also note
611 that \"/bin/sh\" exists on all Unixen, this might not be true for
612 the value that you decide to use. You Have Been Warned.
613 * `tramp-rsh-program'
614 This specifies the name of the program to use for rsh; this might be
615 the full path to rsh or the name of a workalike program.
617 This specifies the list of arguments to pass to the above
618 mentioned program. Please note that this is a list of arguments,
619 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
620 here. Instead, you want two list elements, one for \"-a\" and one
621 for \"-b\", or one for \"-f\" and one for \"foo\".
622 * `tramp-rcp-program'
623 This specifies the name of the program to use for rcp; this might be
624 the full path to rcp or the name of a workalike program.
626 This specifies the list of parameters to pass to the above mentioned
627 program, the hints for `tramp-rsh-args' also apply here.
628 * `tramp-rcp-keep-date-arg'
629 This specifies the parameter to use for `rcp' when the timestamp
630 of the original file should be kept. For `rcp', use `-p', for
633 This specifies the name of the program to use for `su'.
635 This specifies the list of arguments to pass to `su'.
636 \"%u\" is replaced by the user name, use \"%%\" for a literal
638 * `tramp-encoding-command'
639 This specifies a command to use to encode the file contents for
640 transfer. The command should read the raw file contents from
641 standard input and write the encoded file contents to standard
642 output. In this string, the percent escape \"%f\" should be used
643 to indicate the file to convert. Use \"%%\" if you need a literal
644 percent character in your command.
645 * `tramp-decoding-command'
646 This specifies a command to use to decode file contents encoded
647 with `tramp-encoding-command'. The command should read from standard
648 input and write to standard output.
649 * `tramp-encoding-function'
650 This specifies a function to be called to encode the file contents
651 on the local side. This function should accept two arguments
652 START and END, the beginning and end of the region to encode. The
653 region should be replaced with the encoded contents.
654 * `tramp-decoding-function'
655 Same for decoding on the local side.
656 * `tramp-telnet-program'
657 Specifies the telnet program to use when using
658 `tramp-open-connection-telnet' to log in.
659 * `tramp-telnet-args'
660 Specifies list of arguments to pass to `telnet'. The hints for
661 `tramp-rsh-args' also apply here.
663 What does all this mean? Well, you should specify `tramp-rsh-program',
664 `tramp-telnet-program' or `tramp-su-program' for all methods; this program
665 is used to log in to the remote site. Then, there are two ways to
666 actually transfer the files between the local and the remote side.
667 One way is using an additional rcp-like program. If you want to do
668 this, set `tramp-rcp-program' in the method.
670 Another possibility for file transfer is inline transfer, i.e. the
671 file is passed through the same buffer used by `tramp-rsh-program'. In
672 this case, the file contents need to be protected since the
673 `tramp-rsh-program' might use escape codes or the connection might not
674 be eight-bit clean. Therefore, file contents are encoded for transit.
676 Two possibilities for encoding are uuencode/uudecode and mimencode.
677 For uuencode/uudecode you want to set `tramp-encoding-command' to
678 something like \"uuencode\" and `tramp-decoding-command' to \"uudecode
679 -p\". For mimencode you want to set `tramp-encoding-command' to
680 something like \"mimencode -b\" and `tramp-decoding-command' to
683 When using inline transfer, you can use a program or a Lisp function
684 on the local side to encode or decode the file contents. Set the
685 `tramp-encoding-function' and `tramp-decoding-function' parameters to nil
686 in order to use the commands or to the function to use. It is
687 possible to specify one function and the other parameter as nil.
689 So, to summarize: if the method is an inline method, you must specify
690 `tramp-encoding-command' and `tramp-decoding-command', and
691 `tramp-rcp-program' must be nil. If the method is out of band, then
692 you must specify `tramp-rcp-program' and `tramp-rcp-args' and
693 `tramp-encoding-command' and `tramp-decoding-command' must be nil.
694 Every method, inline or out of band, must specify
695 `tramp-connection-function' plus the associated arguments (for
696 example, the telnet program if you chose
697 `tramp-open-connection-telnet').
701 When using `tramp-open-connection-su' the phrase `open connection to a
702 remote host' sounds strange, but it is used nevertheless, for
703 consistency. No connection is opened to a remote host, but `su' is
704 started on the local host. You are not allowed to specify a remote
705 host other than `localhost' or the name of the local host.
707 Using a uuencode/uudecode inline method is discouraged, please use one
708 of the base64 methods instead since base64 encoding is much more
709 reliable and the commands are more standardized between the different
710 Unix versions. But if you can't use base64 for some reason, please
711 note that the default uudecode command does not work well for some
712 Unices, in particular AIX and Irix. For AIX, you might want to use
713 the following command for uudecode:
715 sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
717 For Irix, no solution is known yet."
721 (set (list (const tramp-connection-function) function)
722 (list (const tramp-rsh-program)
723 (choice (const nil) string))
724 (list (const tramp-rcp-program)
725 (choice (const nil) string))
726 (list (const tramp-remote-sh)
727 (choice (const nil) string))
728 (list (const tramp-rsh-args) (repeat string))
729 (list (const tramp-rcp-args) (repeat string))
730 (list (const tramp-rcp-keep-date-arg)
731 (choice (const nil) string))
732 (list (const tramp-su-program)
733 (choice (const nil) string))
734 (list (const tramp-su-args) (repeat string))
735 (list (const tramp-encoding-command)
736 (choice (const nil) string))
737 (list (const tramp-decoding-command)
738 (choice (const nil) string))
739 (list (const tramp-encoding-function)
740 (choice (const nil) function))
741 (list (const tramp-decoding-function)
742 (choice (const nil) function))
743 (list (const tramp-telnet-program)
744 (choice (const nil) string))
745 (list (const tramp-telnet-args) (repeat string))))))
747 (defcustom tramp-multi-methods '("multi" "multiu")
748 "*List of multi-hop methods.
749 Each entry in this list should be a method name as mentioned in the
750 variable `tramp-methods'."
752 :type '(repeat string))
754 (defcustom tramp-multi-connection-function-alist
755 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
756 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
757 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
758 ("su" tramp-multi-connect-su "su - %u%n")
759 ("sudo" tramp-multi-connect-su "sudo -u %u -s%n"))
760 "*List of connection functions for multi-hop methods.
761 Each list item is a list of three items (METHOD FUNCTION COMMAND),
762 where METHOD is the name as used in the file name, FUNCTION is the
763 function to be executed, and COMMAND is the shell command used for
766 COMMAND may contain percent escapes. `%u' will be replaced with the
767 user name, `%h' will be replaced with the host name, and `%n' will be
768 replaced with an end-of-line character, as specified in the variable
769 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
770 Note that the interpretation of the percent escapes also depends on
771 the FUNCTION. For example, the `%u' escape is forbidden with the
772 function `tramp-multi-connect-telnet'. See the documentation of the
773 various functions for details."
775 :type '(repeat (list string function string)))
777 (defcustom tramp-default-method "rcp"
778 "*Default method to use for transferring files.
779 See `tramp-methods' for possibilities.
780 Also see `tramp-default-method-alist'."
784 (defcustom tramp-default-method-alist nil
785 "*Default method to use for specific user/host pairs.
786 This is an alist of items (HOST USER METHOD). The first matching item
787 specifies the method to use for a file name which does not specify a
788 method. HOST and USER are regular expressions or nil, which is
789 interpreted as a regular expression which always matches. If no entry
790 matches, the variable `tramp-default-method' takes effect.
792 If the file name does not specify the user, lookup is done using the
793 empty string for the user name.
795 See `tramp-methods' for a list of possibilities for METHOD."
797 :type '(repeat (list (regexp :tag "Host regexp")
798 (regexp :tag "User regexp")
799 (string :tag "Method"))))
801 (defcustom tramp-ftp-method "ftp"
802 "*When this method name is used, forward all calls to Ange-FTP."
806 (defcustom tramp-rsh-end-of-line "\n"
807 "*String used for end of line in rsh connections.
808 I don't think this ever needs to be changed, so please tell me about it
809 if you need to change this."
813 (defcustom tramp-remote-path
814 '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin"
815 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
816 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
817 "*List of directories to search for executables on remote host.
818 Please notify me about other semi-standard directories to include here.
820 You can use `~' in this list, but when searching for a shell which groks
821 tilde expansion, all directory names starting with `~' will be ignored."
823 :type '(repeat string))
825 (defcustom tramp-login-prompt-regexp
827 "*Regexp matching login-like prompts.
828 The regexp should match the whole line."
832 (defcustom tramp-password-prompt-regexp
833 "^.*\\([pP]assword\\|passphrase.*\\):\^@? *$"
834 "*Regexp matching password-like prompts.
835 The regexp should match the whole line.
837 The `sudo' program appears to insert a `^@' character into the prompt."
841 (defcustom tramp-wrong-passwd-regexp
842 (concat "^.*\\(Permission denied.\\|Login [Ii]ncorrect\\|"
843 "Received signal [0-9]+\\|Connection \\(refused\\|closed\\)\\|"
844 "Sorry, try again.\\|Name or service not known\\).*$")
845 "*Regexp matching a `login failed' message.
846 The regexp should match the whole line."
850 (defcustom tramp-temp-name-prefix "tramp."
851 "*Prefix to use for temporary files.
852 If this is a relative file name (such as \"tramp.\"), it is considered
853 relative to the directory name returned by the function
854 `tramp-temporary-file-directory' (which see). It may also be an
855 absolute file name; don't forget to include a prefix for the filename
860 (defcustom tramp-discard-garbage nil
861 "*If non-nil, try to discard garbage sent by remote shell.
862 Some shells send such garbage upon connection setup."
866 (defcustom tramp-sh-extra-args '(("/bash\\'" . "--norc"))
867 "*Alist specifying extra arguments to pass to the remote shell.
868 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
869 matching the shell file name and ARGS is a string specifying the
872 This variable is only used when Tramp needs to start up another shell
873 for tilde expansion. The extra arguments should typically prevent the
874 shell from reading its init file."
876 :type '(alist :key-type string :value-type string))
880 (defcustom tramp-file-name-structure
881 (list (concat "\\`/\\[\\(\\([a-zA-Z0-9]+\\)/\\)?" ;method
882 "\\(\\([-a-zA-Z0-9_#/:]+\\)@\\)?" ;user
883 "\\([-a-zA-Z0-9_#/:@.]+\\)\\]" ;host
886 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
887 the tramp file name structure.
889 The first element REGEXP is a regular expression matching a tramp file
890 name. The regex should contain parentheses around the method name,
891 the user name, the host name, and the file name parts.
893 The second element METHOD is a number, saying which pair of
894 parentheses matches the method name. The third element USER is
895 similar, but for the user name. The fourth element HOST is similar,
896 but for the host name. The fifth element FILE is for the file name.
897 These numbers are passed directly to `match-string', which see. That
898 means the opening parentheses are counted to identify the pair.
900 See also `tramp-file-name-regexp' and `tramp-make-tramp-file-format'."
902 :type '(list (regexp :tag "File name regexp")
903 (integer :tag "Paren pair for method name")
904 (integer :tag "Paren pair for user name ")
905 (integer :tag "Paren pair for host name ")
906 (integer :tag "Paren pair for file name ")))
909 (defcustom tramp-file-name-regexp "\\`/\\[.*\\]"
910 "*Regular expression matching file names handled by tramp.
911 This regexp should match tramp file names but no other file names.
912 \(When tramp.el is loaded, this regular expression is prepended to
913 `file-name-handler-alist', and that is searched sequentially. Thus,
914 if the tramp entry appears rather early in the `file-name-handler-alist'
915 and is a bit too general, then some files might be considered tramp
916 files which are not really tramp files.
918 Please note that the entry in `file-name-handler-alist' is made when
919 this file (tramp.el) is loaded. This means that this variable must be set
920 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
921 updated after changing this variable.
923 Also see `tramp-file-name-structure' and `tramp-make-tramp-file-format'."
927 (defcustom tramp-make-tramp-file-format "/[%m/%u@%h]%p"
928 "*Format string saying how to construct tramp file name.
929 `%m' is replaced by the method name.
930 `%u' is replaced by the user name.
931 `%h' is replaced by the host name.
932 `%p' is replaced by the file name.
933 `%%' is replaced by %.
935 Also see `tramp-file-name-structure' and `tramp-file-name-regexp'."
939 (defcustom tramp-make-tramp-file-user-nil-format "/[%m/%h]%p"
940 "*Format string saying how to construct tramp file name when the user name is not known.
941 `%m' is replaced by the method name.
942 `%h' is replaced by the host name.
943 `%p' is replaced by the file name.
944 `%%' is replaced by %.
946 Also see `tramp-make-tramp-file-format', `tramp-file-name-structure', and `tramp-file-name-regexp'."
950 (defcustom tramp-multi-file-name-structure
953 "\\`/\\[\\(\\([a-z0-9]+\\)\\)?"
954 ;; regexp specifying the hops
958 2 ;number of pair to match method
959 3 ;number of pair to match hops
960 -1) ;number of pair to match path
961 "*Describes the file name structure of `multi' files.
962 Multi files allow you to contact a remote host in several hops.
963 This is a list of four elements (REGEXP METHOD HOP PATH).
965 The first element, REGEXP, gives a regular expression to match against
966 the file name. In this regular expression, `%s' is replaced with the
967 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
968 allow multiple hops, you normally want to use something like
969 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
970 of parentheses is used for the HOP element, see below.)
972 All remaining elements are numbers. METHOD gives the number of the
973 paren pair which matches the method name. HOP gives the number of the
974 paren pair which matches the hop sequence. PATH gives the number of
975 the paren pair which matches the path name on the remote host.
977 PATH can also be negative, which means to count from the end. Ie, a
978 value of -1 means the last paren pair.
980 I think it would be good if the regexp matches the whole of the
981 string, but I haven't actually tried what happens if it doesn't..."
983 :type '(list (regexp :tag "File name regexp")
984 (integer :tag "Paren pair for method name")
985 (integer :tag "Paren pair for hops")
986 (integer :tag "Paren pair to match path")))
988 (defcustom tramp-multi-file-name-hop-structure
989 (list (concat "/\\([a-z0-9_]+\\):" ;hop method
990 "\\([a-z0-9_]+\\)@" ;user
991 "\\([a-z0-9.-]+\\)") ;host
993 "*Describes the structure of a hop in multi files.
994 This is a list of four elements (REGEXP METHOD USER HOST). First
995 element REGEXP is used to match against the hop. Pair number METHOD
996 matches the method of one hop, pair number USER matches the user of
997 one hop, pair number HOST matches the host of one hop.
999 This regular expression should match exactly all of one hop."
1001 :type '(list (regexp :tag "Hop regexp")
1002 (integer :tag "Paren pair for method name")
1003 (integer :tag "Paren pair for user name")
1004 (integer :tag "Paren pair for host name")))
1006 (defcustom tramp-make-multi-tramp-file-format
1007 (list "/[%m" "/%m:%u@%h" "]%p")
1008 "*Describes how to construct a `multi' file name.
1009 This is a list of three elements PREFIX, HOP and PATH.
1011 The first element PREFIX says how to construct the prefix, the second
1012 element HOP specifies what each hop looks like, and the final element
1013 PATH says how to construct the path name.
1015 In PREFIX, `%%' means `%' and `%m' means the method name.
1017 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1018 user and hop host, respectively.
1020 In PATH, `%%' means `%' and `%p' means the path name.
1022 The resulting file name always contains one copy of PREFIX and one
1023 copy of PATH, but there is one copy of HOP for each hop in the file
1026 Note: the current implementation requires the prefix to contain the
1027 method name, followed by all the hops, and the path name must come
1030 :type '(list string string string))
1032 (defcustom tramp-terminal-type "dumb"
1033 "*Value of TERM environment variable for logging in to remote host.
1034 Because Tramp wants to parse the output of the remote shell, it is easily
1035 confused by ANSI color escape sequences and suchlike. Often, shell init
1036 files conditionalize this setup based on the TERM environment variable."
1040 (defcustom tramp-completion-without-shell-p nil
1041 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1042 Using shell wildcards for completions has the advantage that it can be
1043 fast even in large directories, but completion is always
1044 case-sensitive. Relying on Lisp only means that case-insensitive
1045 completion is possible (subject to the variable `completion-ignore-case'),
1046 but it might be slow on large directories."
1050 ;;; Internal Variables:
1052 (defvar tramp-buffer-file-attributes nil
1053 "Holds the `ls -ild' output for the current buffer.
1054 This variable is local to each buffer. It is not used if the remote
1055 machine groks Perl. If it is used, it's used as an emulation for
1056 the visited file modtime.")
1057 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1059 (defvar tramp-end-of-output "/////"
1060 "String used to recognize end of output.")
1062 (defvar tramp-connection-function nil
1063 "This internal variable holds a parameter for `tramp-methods'.
1064 In the connection buffer, this variable has the value of the like-named
1065 method parameter, as specified in `tramp-methods' (which see).")
1067 (defvar tramp-remote-sh nil
1068 "This internal variable holds a parameter for `tramp-methods'.
1069 In the connection buffer, this variable has the value of the like-named
1070 method parameter, as specified in `tramp-methods' (which see).")
1072 (defvar tramp-rsh-program nil
1073 "This internal variable holds a parameter for `tramp-methods'.
1074 In the connection buffer, this variable has the value of the like-named
1075 method parameter, as specified in `tramp-methods' (which see).")
1077 (defvar tramp-rsh-args nil
1078 "This internal variable holds a parameter for `tramp-methods'.
1079 In the connection buffer, this variable has the value of the like-named
1080 method parameter, as specified in `tramp-methods' (which see).")
1082 (defvar tramp-rcp-program nil
1083 "This internal variable holds a parameter for `tramp-methods'.
1084 In the connection buffer, this variable has the value of the like-named
1085 method parameter, as specified in `tramp-methods' (which see).")
1087 (defvar tramp-rcp-args nil
1088 "This internal variable holds a parameter for `tramp-methods'.
1089 In the connection buffer, this variable has the value of the like-named
1090 method parameter, as specified in `tramp-methods' (which see).")
1092 (defvar tramp-rcp-keep-date-arg nil
1093 "This internal variable holds a parameter for `tramp-methods'.
1094 In the connection buffer, this variable has the value of the like-named
1095 method parameter, as specified in `tramp-methods' (which see).")
1097 (defvar tramp-encoding-command nil
1098 "This internal variable holds a parameter for `tramp-methods'.
1099 In the connection buffer, this variable has the value of the like-named
1100 method parameter, as specified in `tramp-methods' (which see).")
1102 (defvar tramp-decoding-command nil
1103 "This internal variable holds a parameter for `tramp-methods'.
1104 In the connection buffer, this variable has the value of the like-named
1105 method parameter, as specified in `tramp-methods' (which see).")
1107 (defvar tramp-encoding-function nil
1108 "This internal variable holds a parameter for `tramp-methods'.
1109 In the connection buffer, this variable has the value of the like-named
1110 method parameter, as specified in `tramp-methods' (which see).")
1112 (defvar tramp-decoding-function nil
1113 "This internal variable holds a parameter for `tramp-methods'.
1114 In the connection buffer, this variable has the value of the like-named
1115 method parameter, as specified in `tramp-methods' (which see).")
1117 (defvar tramp-telnet-program nil
1118 "This internal variable holds a parameter for `tramp-methods'.
1119 In the connection buffer, this variable has the value of the like-named
1120 method parameter, as specified in `tramp-methods' (which see).")
1122 (defvar tramp-telnet-args nil
1123 "This internal variable holds a parameter for `tramp-methods'.
1124 In the connection buffer, this variable has the value of the like-named
1125 method parameter, as specified in `tramp-methods' (which see).")
1127 ;; CCC `local in each buffer'?
1128 (defvar tramp-ls-command nil
1129 "This command is used to get a long listing with numeric user and group ids.
1130 This variable is automatically made buffer-local to each rsh process buffer
1131 upon opening the connection.")
1133 (defvar tramp-current-multi-method nil
1134 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1135 This variable is automatically made buffer-local to each rsh process buffer
1136 upon opening the connection.")
1138 (defvar tramp-current-method nil
1139 "Connection method for this *tramp* buffer.
1140 This variable is automatically made buffer-local to each rsh process buffer
1141 upon opening the connection.")
1143 (defvar tramp-current-user nil
1144 "Remote login name for this *tramp* buffer.
1145 This variable is automatically made buffer-local to each rsh process buffer
1146 upon opening the connection.")
1148 (defvar tramp-current-host nil
1149 "Remote host for this *tramp* buffer.
1150 This variable is automatically made buffer-local to each rsh process buffer
1151 upon opening the connection.")
1153 (defvar tramp-test-groks-nt nil
1154 "Whether the `test' command groks the `-nt' switch.
1155 \(`test A -nt B' tests if file A is newer than file B.)
1156 This variable is automatically made buffer-local to each rsh process buffer
1157 upon opening the connection.")
1159 (defvar tramp-file-exists-command nil
1160 "Command to use for checking if a file exists.
1161 This variable is automatically made buffer-local to each rsh process buffer
1162 upon opening the connection.")
1164 ;; Perl script to implement `file-attributes' in a Lisp `read'able output.
1165 ;; If you are hacking on this, note that you get *no* output unless this
1166 ;; spits out a complete line, including the '\n' at the end.
1167 (defconst tramp-perl-file-attributes (concat
1170 if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; }
1171 elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; }
1172 else { $l = \"nil\" };
1173 printf(\"(%s %u %u %u (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1174 $l, $s[3], $s[4], $s[5], $s[8] >> 16 & 0xffff, $s[8] & 0xffff,
1175 $s[9] >> 16 & 0xffff, $s[9] & 0xffff, $s[10] >> 16 & 0xffff, $s[10] & 0xffff,
1176 $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff, $s[0] >> 16 & 0xffff, $s[0] & 0xffff);"
1178 "Perl script to produce output suitable for use with `file-attributes'
1179 on the remote file system.")
1181 ;; Perl script to implement `mime-encode'
1182 (defvar tramp-perl-mime-encode (concat
1183 "sub encode_base64 ($);
1185 while(read(STDIN, $buf, 60*57)) { print encode_base64($buf) }
1186 sub encode_base64 ($) {
1189 pos($_[0]) = 0; # ensure start at the beginning
1190 while ($_[0] =~ /(.{1,45})/gs) {
1191 $res .= substr(pack(\"u\", $1), 1);
1194 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
1195 # fix padding at the end
1196 my $padding = (3 - length($_[0]) % 3) % 3;
1197 $res =~ s/.{$padding}$/\"=\" x $padding/e if $padding;
1198 # break encoded string into lines of no more than 76 characters each
1200 $res =~ s/(.{1,76})/$1$eol/g;
1204 ;; Perl script to implement `mime-decode'
1205 (defvar tramp-perl-mime-decode (concat
1206 "sub decode_base64 ($);
1208 while(read(STDIN, $buf, 60*57)) { print decode_base64($buf) }
1209 sub decode_base64 ($) {
1210 local($^W) = 0; # unpack(\"u\",...) gives bogus warning in 5.00[123]
1215 $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
1216 if (length($str) % 4) {
1217 warn(\"Length of base64 data not a multiple of 4\")
1219 $str =~ s/=+$//; # remove padding
1220 $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
1221 while ($str =~ /(.{1,60})/gs) {
1222 my $len = chr(32 + length($1)*3/4); # compute length byte
1223 $res .= unpack(\"u\", $len . $1 ); # uudecode
1227 ; These values conform to `file-attributes' from XEmacs 21.2.
1228 ; GNU Emacs and other tools not checked.
1229 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1231 (2 . "c") ; character device
1232 (3 . "m") ; multiplexed character device (v7)
1233 (4 . "d") ; directory
1234 (5 . "?") ; Named special file (XENIX)
1235 (6 . "b") ; block device
1236 (7 . "?") ; multiplexed block device (v7)
1237 (8 . "-") ; regular file
1238 (9 . "n") ; network special file (HP-UX)
1239 (10 . "l") ; symlink
1240 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1242 (13 . "D") ; door special (Solaris)
1243 (14 . "w")) ; whiteout (BSD)
1244 "A list of file types returned from the `stat' system call.
1245 This is used to map a mode number to a permission string.")
1247 (defvar tramp-dos-coding-system
1248 (if (and (fboundp 'coding-system-p)
1249 (funcall 'coding-system-p '(dos)))
1252 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1255 ;; New handlers should be added here. The following operations can be
1256 ;; handled using the normal primitives: file-name-as-directory,
1257 ;; file-name-directory, file-name-nondirectory,
1258 ;; file-name-sans-versions, get-file-buffer.
1259 (defconst tramp-file-name-handler-alist
1261 (load . tramp-handle-load)
1262 (make-symbolic-link . tramp-handle-make-symbolic-link)
1263 (file-name-directory . tramp-handle-file-name-directory)
1264 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1265 (file-truename . tramp-handle-file-truename)
1266 (file-exists-p . tramp-handle-file-exists-p)
1267 (file-directory-p . tramp-handle-file-directory-p)
1268 (file-executable-p . tramp-handle-file-executable-p)
1269 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1270 (file-readable-p . tramp-handle-file-readable-p)
1271 (file-regular-p . tramp-handle-file-regular-p)
1272 (file-symlink-p . tramp-handle-file-symlink-p)
1273 (file-writable-p . tramp-handle-file-writable-p)
1274 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1275 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1276 (file-attributes . tramp-handle-file-attributes)
1277 (file-modes . tramp-handle-file-modes)
1278 (file-directory-files . tramp-handle-file-directory-files)
1279 (directory-files . tramp-handle-directory-files)
1280 (file-name-all-completions . tramp-handle-file-name-all-completions)
1281 (file-name-completion . tramp-handle-file-name-completion)
1282 (add-name-to-file . tramp-handle-add-name-to-file)
1283 (copy-file . tramp-handle-copy-file)
1284 (rename-file . tramp-handle-rename-file)
1285 (set-file-modes . tramp-handle-set-file-modes)
1286 (make-directory . tramp-handle-make-directory)
1287 (delete-directory . tramp-handle-delete-directory)
1288 (delete-file . tramp-handle-delete-file)
1289 (directory-file-name . tramp-handle-directory-file-name)
1290 (shell-command . tramp-handle-shell-command)
1291 (insert-directory . tramp-handle-insert-directory)
1292 (expand-file-name . tramp-handle-expand-file-name)
1293 (file-local-copy . tramp-handle-file-local-copy)
1294 (insert-file-contents . tramp-handle-insert-file-contents)
1295 (write-region . tramp-handle-write-region)
1296 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1297 (dired-call-process . tramp-handle-dired-call-process)
1298 (dired-recursive-delete-directory
1299 . tramp-handle-dired-recursive-delete-directory)
1300 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1301 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1302 "Alist of handler functions.
1303 Operations not mentioned here will be handled by the normal Emacs functions.")
1305 ;;; For better error reporting.
1307 (defun tramp-version (arg)
1308 "Print version number of tramp.el in minibuffer or current buffer."
1310 (if arg (insert tramp-version) (message tramp-version)))
1312 ;;; Internal functions which must come first.
1314 (defsubst tramp-message (level fmt-string &rest args)
1315 "Emit a message depending on verbosity level.
1316 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1317 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1318 Calls function `message' with FMT-STRING as control string and the remaining
1319 ARGS to actually emit the message (if applicable).
1321 This function expects to be called from the tramp buffer only!"
1322 (when (<= level tramp-verbose)
1323 (apply #'message (concat "tramp: " fmt-string) args)
1324 (when tramp-debug-buffer
1327 (tramp-get-debug-buffer
1328 tramp-current-multi-method tramp-current-method
1329 tramp-current-user tramp-current-host))
1330 (goto-char (point-max))
1331 (tramp-insert-with-face
1333 (concat "# " (apply #'format fmt-string args) "\n"))))))
1335 (defun tramp-message-for-buffer
1336 (multi-method method user host level fmt-string &rest args)
1337 "Like `tramp-message' but temporarily switches to the tramp buffer.
1338 First three args METHOD, USER, and HOST identify the tramp buffer to use,
1339 remaining args passed to `tramp-message'."
1341 (set-buffer (tramp-get-buffer multi-method method user host))
1342 (apply 'tramp-message level fmt-string args)))
1344 (defsubst tramp-line-end-position nil
1345 "Return point at end of line.
1346 Calls `line-end-position' or `point-at-eol' if defined, else
1347 own implementation."
1349 ((fboundp 'line-end-position) (funcall 'line-end-position))
1350 ((fboundp 'point-at-eol) (funcall 'point-at-eol))
1351 (t (save-excursion (end-of-line) (point)))))
1353 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1354 "Parse a Tramp filename and make components available in the body.
1356 First arg FILENAME is evaluated and dissected into its components.
1357 Second arg VAR is a symbol. It is used as a variable name to hold
1358 the filename structure. It is also used as a prefix for the variables
1359 holding the components. For example, if VAR is the symbol `foo', then
1360 `foo' will be bound to the whole structure, `foo-multi-method' will
1361 be bound to the multi-method component, and so on for `foo-method',
1362 `foo-user', `foo-host', `foo-path'.
1364 Remaining args are Lisp expressions to be evaluated (inside an implicit
1367 If VAR is nil, then we bind `v' to the structure and `multi-method',
1368 `method', `user', `host', `path' to the components."
1369 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1370 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
1371 (tramp-file-name-multi-method ,(or var 'v)))
1372 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1373 (tramp-file-name-method ,(or var 'v)))
1374 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1375 (tramp-file-name-user ,(or var 'v)))
1376 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1377 (tramp-file-name-host ,(or var 'v)))
1378 (,(if var (intern (concat (symbol-name var) "-path")) 'path)
1379 (tramp-file-name-path ,(or var 'v))))
1382 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1384 ;;; File Name Handler Functions:
1386 ;; The following file name handler ops are not implemented (yet?).
1388 (defun tramp-handle-make-symbolic-link
1389 (filename linkname &optional ok-if-already-exists)
1390 "Like `make-symbolic-link' for tramp files.
1391 The LINKNAME argument should look like \"/path/to/target\" or
1392 \"relative-name\",and not like a Tramp filename."
1393 (error "Not implemented yet")
1394 (with-parsed-tramp-file-name linkname l
1395 (when (tramp-ange-ftp-file-name-p l-multi-method l-method)
1396 (tramp-invoke-ange-ftp 'make-symbolic-link
1397 filename linkname ok-if-already-exists))
1398 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
1399 (cwd (file-name-directory l-path)))
1402 (list "Making a symbolic link."
1403 "ln(1) does not exist on the remote host.")))
1405 ;; Do the 'confirm if exists' thing.
1406 (when (file-exists-p (expand-file-name filename
1409 (if (or (null ok-if-already-exists) ; not allowed to exist
1410 (and (numberp ok-if-already-exists)
1413 "File %s already exists; make it a link anyway? "
1415 (signal 'file-already-exists (list "File already exists" l-path))))
1417 ;; Right, they are on the same host, regardless of user, method, etc.
1418 ;; We now make the link on the remote machine. This will occur as the user
1419 ;; that FILENAME belongs to.
1421 (tramp-send-command-and-check
1422 l-multi-method l-method l-user l-host
1423 (format "cd %s && %s -sf %s %s"
1430 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
1431 "Like `load' for tramp files. Not implemented!"
1432 (unless (file-name-absolute-p file)
1433 (error "Tramp cannot `load' files without absolute path name"))
1434 (with-parsed-tramp-file-name file nil
1435 (when (tramp-ange-ftp-file-name-p multi-method method)
1436 (tramp-invoke-ange-ftp 'load
1437 file noerror nomessage nosuffix must-suffix))
1439 (cond ((file-exists-p (concat file ".elc"))
1440 (setq file (concat file ".elc")))
1441 ((file-exists-p (concat file ".el"))
1442 (setq file (concat file ".el")))))
1444 ;; The first condition is always true for absolute file names.
1445 ;; Included for safety's sake.
1446 (unless (or (file-name-directory file)
1447 (string-match "\\.elc?\\'" file))
1448 (error "File `%s' does not include a `.el' or `.elc' suffix"
1451 (when (not (file-exists-p file))
1452 (error "Cannot load nonexistant file `%s'" file)))
1453 (if (not (file-exists-p file))
1456 (message "Loading %s..." file))
1457 (let ((local-copy (file-local-copy file)))
1458 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
1459 (load local-copy noerror t t)
1460 (delete-file local-copy))
1462 (message "Loading %s...done" file))
1465 ;; Path manipulation functions that grok TRAMP paths...
1466 (defun tramp-handle-file-name-directory (file)
1467 "Like `file-name-directory' but aware of TRAMP files."
1468 ;; everything except the last filename thing is the directory
1469 (with-parsed-tramp-file-name file nil
1470 (when (tramp-ange-ftp-file-name-p multi-method method)
1471 (tramp-invoke-ange-ftp 'file-name-directory file))
1472 (if (or (string= path "") (string= path "/"))
1473 ;; For a filename like "/[foo]", we return "/". The `else'
1474 ;; case would return "/[foo]" unchanged. But if we do that,
1475 ;; then `file-expand-wildcards' ceases to work. It's not
1476 ;; quite clear to me what's the intuition that tells that this
1477 ;; behavior is the right behavior, but oh, well.
1479 ;; run the command on the path portion only
1480 ;; CCC: This should take into account the remote machine type, no?
1481 ;; --daniel <daniel@danann.net>
1482 (tramp-make-tramp-file-name multi-method method user host
1483 ;; This will not recurse...
1484 (or (file-name-directory path) "")))))
1486 (defun tramp-handle-file-name-nondirectory (file)
1487 "Like `file-name-nondirectory' but aware of TRAMP files."
1488 (with-parsed-tramp-file-name file nil
1489 (when (tramp-ange-ftp-file-name-p multi-method method)
1490 (tramp-invoke-ange-ftp 'file-name-nondirectory file))
1491 (file-name-nondirectory path)))
1493 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
1494 "Like `file-truename' for tramp files."
1495 (with-parsed-tramp-file-name filename nil
1496 ;; Ange-FTP does not support truename processing. It returns the
1497 ;; file name as-is. So that's what we do, too.
1498 (when (tramp-ange-ftp-file-name-p multi-method method)
1500 (let* ((steps (tramp-split-string path "/"))
1501 (pathdir (let ((directory-sep-char ?/))
1502 (file-name-as-directory path)))
1503 (is-dir (string= path pathdir))
1506 ;; Don't make the following value larger than necessary.
1507 ;; People expect an error message in a timely fashion when
1508 ;; something is wrong; otherwise they might think that Emacs
1509 ;; is hung. Of course, correctness has to come first.
1511 (result nil) ;result steps in reverse order
1514 (tramp-message-for-buffer
1515 multi-method method user host
1516 10 "Finding true name for `%s'" filename)
1517 (while (and steps (< numchase numchase-limit))
1518 (setq thisstep (pop steps))
1519 (tramp-message-for-buffer
1520 multi-method method user host
1522 (mapconcat 'identity
1523 (append '("") (reverse result) (list thisstep))
1525 (setq symlink-target
1526 (nth 0 (tramp-handle-file-attributes
1527 (tramp-make-tramp-file-name
1528 multi-method method user host
1529 (mapconcat 'identity
1530 (append '("") (reverse result) (list thisstep))
1532 (cond ((string= "." thisstep)
1533 (tramp-message-for-buffer multi-method method user host
1534 10 "Ignoring step `.'"))
1535 ((string= ".." thisstep)
1536 (tramp-message-for-buffer multi-method method user host
1537 10 "Processing step `..'")
1539 ((stringp symlink-target)
1540 ;; It's a symlink, follow it.
1541 (tramp-message-for-buffer
1542 multi-method method user host
1543 10 "Follow symlink to %s" symlink-target)
1544 (setq numchase (1+ numchase))
1545 (when (file-name-absolute-p symlink-target)
1548 (append (tramp-split-string symlink-target "/") steps)))
1551 (setq result (cons thisstep result)))))
1552 (when (>= numchase numchase-limit)
1553 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
1554 (setq result (reverse result))
1555 (tramp-message-for-buffer
1556 multi-method method user host
1557 10 "True name of `%s' is `%s'"
1558 filename (mapconcat 'identity (cons "" result) "/"))
1559 (tramp-make-tramp-file-name
1560 multi-method method user host
1561 (concat (mapconcat 'identity (cons "" result) "/")
1562 (if is-dir "/" ""))))))
1566 (defun tramp-handle-file-exists-p (filename)
1567 "Like `file-exists-p' for tramp files."
1568 (with-parsed-tramp-file-name filename nil
1569 (when (tramp-ange-ftp-file-name-p multi-method method)
1570 (tramp-invoke-ange-ftp 'file-exists-p filename))
1572 (zerop (tramp-send-command-and-check
1573 multi-method method user host
1575 (tramp-get-file-exists-command multi-method method user host)
1576 (tramp-shell-quote-argument path)))))))
1578 ;; CCC: This should check for an error condition and signal failure
1579 ;; when something goes wrong.
1580 ;; Daniel Pittman <daniel@danann.net>
1581 (defun tramp-handle-file-attributes (filename &optional nonnumeric)
1582 "Like `file-attributes' for tramp files.
1583 Optional argument NONNUMERIC means return user and group name
1584 rather than as numbers."
1585 (if (tramp-handle-file-exists-p filename)
1586 ;; file exists, find out stuff
1588 (with-parsed-tramp-file-name filename nil
1589 (when (tramp-ange-ftp-file-name-p multi-method method)
1590 (tramp-invoke-ange-ftp 'file-attributes file))
1591 (if (tramp-get-remote-perl multi-method method user host)
1592 (tramp-handle-file-attributes-with-perl
1593 multi-method method user host path nonnumeric)
1594 (tramp-handle-file-attributes-with-ls
1595 multi-method method user host path nonnumeric))))
1599 (defun tramp-handle-file-attributes-with-ls
1600 (multi-method method user host path &optional nonnumeric)
1601 "Implement `file-attributes' for tramp files using the ls(1) command."
1603 res-inode res-filemodes res-numlinks
1604 res-uid res-gid res-size res-symlink-target)
1606 multi-method method user host
1608 (tramp-get-ls-command multi-method method user host)
1609 (if nonnumeric "-ild" "-ildn")
1610 (tramp-shell-quote-argument path)))
1611 (tramp-wait-for-output)
1612 ;; parse `ls -l' output ...
1616 (read (current-buffer))
1617 (invalid-read-syntax
1618 (when (and (equal (cadr err)
1619 "Integer constant overflow in reader")
1621 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1623 (let* ((big (read (substring (caddr err) 0
1624 (match-beginning 1))))
1625 (small (read (match-string 1 (caddr err))))
1626 (twiddle (/ small 65536)))
1627 (cons (+ big twiddle)
1628 (- small (* twiddle 65536))))))))
1629 ;; ... file mode flags
1630 (setq res-filemodes (symbol-name (read (current-buffer))))
1632 (setq res-numlinks (read (current-buffer)))
1634 (setq res-uid (read (current-buffer)))
1635 (setq res-gid (read (current-buffer)))
1637 (unless (numberp res-uid) (setq res-uid -1))
1638 (unless (numberp res-gid) (setq res-gid -1)))
1640 (setq res-size (read (current-buffer)))
1641 ;; From the file modes, figure out other stuff.
1642 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1643 (setq dirp (eq ?d (aref res-filemodes 0)))
1644 ;; if symlink, find out file name pointed to
1646 (search-forward "-> ")
1647 (setq res-symlink-target
1648 (buffer-substring (point)
1649 (tramp-line-end-position))))
1650 ;; return data gathered
1652 ;; 0. t for directory, string (name linked to) for symbolic
1654 (or dirp res-symlink-target nil)
1655 ;; 1. Number of links to file.
1661 ;; 4. Last access time, as a list of two integers. First
1662 ;; integer has high-order 16 bits of time, second has low 16
1664 ;; 5. Last modification time, likewise.
1665 ;; 6. Last status change time, likewise.
1666 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1667 ;; 7. Size in bytes (-1, if number is out of range).
1669 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1671 ;; 9. t iff file's gid would change if file were deleted and
1674 ;; 10. inode number.
1676 ;; 11. Device number.
1680 (defun tramp-handle-file-attributes-with-perl
1681 (multi-method method user host path &optional nonnumeric)
1682 "Implement `file-attributes' for tramp files using a Perl script.
1684 The Perl command is sent to the remote machine when the connection
1685 is initially created and is kept cached by the remote shell."
1687 multi-method method user host
1688 (format "tramp_file_attributes %s"
1689 (tramp-shell-quote-argument path)))
1690 (tramp-wait-for-output)
1691 (let ((result (read (current-buffer))))
1692 (setcar (nthcdr 8 result)
1693 (tramp-file-mode-from-int (nth 8 result)))
1696 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
1697 "Like `set-visited-file-modtime' for tramp files."
1698 (unless (buffer-file-name)
1699 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1702 (tramp-run-real-handler 'set-visited-file-modtime (list time-list)))
1703 (let ((f (buffer-file-name))
1704 (coding-system-used nil))
1705 (with-parsed-tramp-file-name f nil
1706 ;; This operation is not handled by Ange-FTP!
1707 (when (tramp-ange-ftp-file-name-p multi-method method)
1708 (throw 'tramp-forward-to-ange-ftp
1709 (tramp-run-real-handler 'set-visited-file-modtime
1711 (let* ((attr (file-attributes f))
1712 (modtime (nth 5 attr)))
1713 ;; We use '(0 0) as a don't-know value. See also
1714 ;; `tramp-handle-file-attributes-with-ls'.
1715 (when (boundp 'last-coding-system-used)
1716 (setq coding-system-used last-coding-system-used))
1717 (if (not (equal modtime '(0 0)))
1718 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1721 multi-method method user host
1722 (format "%s -ild %s"
1723 (tramp-get-ls-command multi-method method user host)
1724 (tramp-shell-quote-argument path)))
1725 (tramp-wait-for-output)
1726 (setq attr (buffer-substring (point)
1727 (progn (end-of-line) (point)))))
1728 (setq tramp-buffer-file-attributes attr))
1729 (when (boundp 'last-coding-system-used)
1730 (setq last-coding-system-used coding-system-used))
1733 ;; CCC continue here
1735 ;; This function makes the same assumption as
1736 ;; `tramp-handle-set-visited-file-modtime'.
1737 (defun tramp-handle-verify-visited-file-modtime (buf)
1738 "Like `verify-visited-file-modtime' for tramp files."
1739 (with-current-buffer buf
1740 (let ((f (buffer-file-name)))
1741 (with-parsed-tramp-file-name f nil
1742 (when (tramp-ange-ftp-file-name-p multi-method method)
1743 ;; This one requires a hack since the file name is not passed
1745 (let ((buffer-file-name (tramp-make-ange-ftp-file-name
1747 (tramp-invoke-ange-ftp 'verify-visited-file-modtime buf)))
1748 (let* ((attr (file-attributes f))
1749 (modtime (nth 5 attr)))
1750 (cond ((and attr (not (equal modtime '(0 0))))
1751 ;; Why does `file-attributes' return a list (HIGH
1752 ;; LOW), but `visited-file-modtime' returns a cons
1754 (let ((mt (visited-file-modtime)))
1755 (< (abs (tramp-time-diff
1756 modtime (list (car mt) (cdr mt)))) 2)))
1760 multi-method method user host
1761 (format "%s -ild %s"
1762 (tramp-get-ls-command multi-method method
1764 (tramp-shell-quote-argument path)))
1765 (tramp-wait-for-output)
1766 (setq attr (buffer-substring
1767 (point) (progn (end-of-line) (point)))))
1768 (equal tramp-buffer-file-attributes attr))
1769 ;; If file does not exist, say it is not modified.
1772 (defadvice clear-visited-file-modtime (after tramp activate)
1773 "Set `tramp-buffer-file-attributes' back to nil.
1774 Tramp uses this variable as an emulation for the actual modtime of the file,
1775 if the remote host can't provide the modtime."
1776 (setq tramp-buffer-file-attributes nil))
1778 (defun tramp-handle-set-file-modes (filename mode)
1779 "Like `set-file-modes' for tramp files."
1780 (with-parsed-tramp-file-name filename nil
1781 (when (tramp-ange-ftp-file-name-p multi-method method)
1782 (tramp-invoke-ange-ftp 'set-file-modes filename mode))
1784 (unless (zerop (tramp-send-command-and-check
1785 multi-method method user host
1786 (format "chmod %s %s"
1787 (tramp-decimal-to-octal mode)
1788 (tramp-shell-quote-argument path))))
1791 ;; FIXME: extract the proper text from chmod's stderr.
1792 "error while changing file's mode"
1795 ;; Simple functions using the `test' command.
1797 (defun tramp-handle-file-executable-p (filename)
1798 "Like `file-executable-p' for tramp files."
1799 (with-parsed-tramp-file-name filename nil
1800 (when (tramp-ange-ftp-file-name-p multi-method method)
1801 (tramp-invoke-ange-ftp 'file-executable-p filename))
1802 (zerop (tramp-run-test "-x" filename))))
1804 (defun tramp-handle-file-readable-p (filename)
1805 "Like `file-readable-p' for tramp files."
1806 (with-parsed-tramp-file-name filename nil
1807 (when (tramp-ange-ftp-file-name-p multi-method method)
1808 (tramp-invoke-ange-ftp 'file-readable-p filename))
1809 (zerop (tramp-run-test "-r" filename))))
1811 (defun tramp-handle-file-accessible-directory-p (filename)
1812 "Like `file-accessible-directory-p' for tramp files."
1813 (with-parsed-tramp-file-name filename nil
1814 (when (tramp-ange-ftp-file-name-p multi-method method)
1815 (tramp-invoke-ange-ftp 'file-accessible-directory-p filename))
1816 (and (zerop (tramp-run-test "-d" filename))
1817 (zerop (tramp-run-test "-r" filename))
1818 (zerop (tramp-run-test "-x" filename)))))
1820 ;; When the remote shell is started, it looks for a shell which groks
1821 ;; tilde expansion. Here, we assume that all shells which grok tilde
1822 ;; expansion will also provide a `test' command which groks `-nt' (for
1823 ;; newer than). If this breaks, tell me about it and I'll try to do
1824 ;; something smarter about it.
1825 (defun tramp-handle-file-newer-than-file-p (file1 file2)
1826 "Like `file-newer-than-file-p' for tramp files."
1827 (cond ((not (file-exists-p file1))
1829 ((not (file-exists-p file2))
1831 ;; We are sure both files exist at this point. We assume that
1832 ;; both files are Tramp files, otherwise we issue an error
1833 ;; message. Todo: make a better error message.
1836 (with-parsed-tramp-file-name file1 v1
1837 (with-parsed-tramp-file-name file2 v2
1838 (when (and (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
1839 (tramp-ange-ftp-file-name-p v2-multi-method v2-method))
1840 (tramp-invoke-ange-ftp 'file-newer-than-file-p
1842 (unless (and (equal v1-multi-method v2-multi-method)
1843 (equal v1-method v2-method)
1844 (equal v1-user v2-user)
1845 (equal v1-host v2-host))
1847 (list "Files must have same method, user, host"
1849 (unless (and (tramp-tramp-file-p file1)
1850 (tramp-tramp-file-p file2))
1852 (list "Files must be tramp files on same host"
1854 (if (tramp-get-test-groks-nt
1855 v1-multi-method v1-method v1-user v1-host)
1856 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
1857 (zerop (tramp-run-test2 "tramp_test_nt" file1 file2)))))))))
1859 ;; Functions implemented using the basic functions above.
1861 (defun tramp-handle-file-modes (filename)
1862 "Like `file-modes' for tramp files."
1863 (with-parsed-tramp-file-name filename nil
1864 (when (tramp-ange-ftp-file-name-p multi-method method)
1865 (tramp-invoke-ange-ftp 'file-modes filename))
1866 (when (file-exists-p filename)
1867 (tramp-mode-string-to-int
1868 (nth 8 (tramp-handle-file-attributes filename))))))
1870 (defun tramp-handle-file-directory-p (filename)
1871 "Like `file-directory-p' for tramp files."
1872 ;; Care must be taken that this function returns `t' for symlinks
1873 ;; pointing to directories. Surely the most obvious implementation
1874 ;; would be `test -d', but that returns false for such symlinks.
1875 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
1876 ;; I now think he's right. So we could be using `test -d', couldn't
1879 ;; Alternatives: `cd %s', `test -d %s'
1880 (with-parsed-tramp-file-name filename nil
1881 (when (tramp-ange-ftp-file-name-p multi-method method)
1882 (tramp-invoke-ange-ftp 'file-directory-p filename))
1885 (tramp-send-command-and-check
1886 multi-method method user host
1887 (format "test -d %s"
1888 (tramp-shell-quote-argument path))
1889 t))))) ;run command in subshell
1891 (defun tramp-handle-file-regular-p (filename)
1892 "Like `file-regular-p' for tramp files."
1893 (with-parsed-tramp-file-name filename nil
1894 (when (tramp-ange-ftp-file-name-p multi-method method)
1895 (tramp-invoke-ange-ftp 'file-regular-p filename))
1896 (and (tramp-handle-file-exists-p filename)
1897 (eq ?- (aref (nth 8 (tramp-handle-file-attributes filename)) 0)))))
1899 (defun tramp-handle-file-symlink-p (filename)
1900 "Like `file-symlink-p' for tramp files."
1901 (with-parsed-tramp-file-name filename nil
1902 (when (tramp-ange-ftp-file-name-p multi-method method)
1903 (tramp-invoke-ange-ftp 'file-symlink-p filename))
1904 (let ((x (car (tramp-handle-file-attributes filename))))
1905 (when (stringp x) x))))
1907 (defun tramp-handle-file-writable-p (filename)
1908 "Like `file-writable-p' for tramp files."
1909 (with-parsed-tramp-file-name filename nil
1910 (when (tramp-ange-ftp-file-name-p multi-method method)
1911 (tramp-invoke-ange-ftp 'file-writable-p filename))
1912 (if (tramp-handle-file-exists-p filename)
1913 ;; Existing files must be writable.
1914 (zerop (tramp-run-test "-w" filename))
1915 ;; If file doesn't exist, check if directory is writable.
1916 (and (zerop (tramp-run-test
1917 "-d" (tramp-handle-file-name-directory filename)))
1918 (zerop (tramp-run-test
1919 "-w" (tramp-handle-file-name-directory filename)))))))
1921 (defun tramp-handle-file-ownership-preserved-p (filename)
1922 "Like `file-ownership-preserved-p' for tramp files."
1923 (with-parsed-tramp-file-name filename nil
1924 (when (tramp-ange-ftp-file-name-p multi-method method)
1925 (tramp-invoke-ange-ftp 'file-ownership-preserved-p filename))
1926 (or (not (tramp-handle-file-exists-p filename))
1927 ;; Existing files must be writable.
1928 (zerop (tramp-run-test "-O" filename)))))
1930 ;; Other file name ops.
1932 ;; ;; Matthias Köppe <mkoeppe@mail.math.uni-magdeburg.de>
1933 ;; (defun tramp-handle-directory-file-name (directory)
1934 ;; "Like `directory-file-name' for tramp files."
1935 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
1936 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
1937 ;; (substring directory 0 (- (length directory) 1))
1940 ;; Philippe Troin <phil@fifi.org>
1941 (defun tramp-handle-directory-file-name (directory)
1942 "Like `directory-file-name' for tramp files."
1943 (with-parsed-tramp-file-name directory nil
1944 (when (tramp-ange-ftp-file-name-p multi-method method)
1945 (tramp-invoke-ange-ftp 'directory-file-name directory))
1946 (let ((directory-length-1 (1- (length directory))))
1948 (if (and (eq (aref directory directory-length-1) ?/)
1949 (eq (string-match tramp-file-name-regexp directory) 0)
1950 (/= (match-end 0) directory-length-1))
1951 (substring directory 0 directory-length-1)
1954 ;; Directory listings.
1956 (defun tramp-handle-directory-files (directory &optional full match nosort)
1957 "Like `directory-files' for tramp files."
1958 (with-parsed-tramp-file-name directory nil
1959 (when (tramp-ange-ftp-file-name-p multi-method method)
1960 (tramp-invoke-ange-ftp 'directory-files
1961 directory full match nosort))
1964 (tramp-barf-unless-okay
1965 multi-method method user host
1966 (concat "cd " (tramp-shell-quote-argument path))
1969 "tramp-handle-directory-files: couldn't `cd %s'"
1970 (tramp-shell-quote-argument path))
1972 multi-method method user host
1973 (concat (tramp-get-ls-command multi-method method user host)
1975 (tramp-wait-for-output)
1976 (goto-char (point-max))
1977 (while (zerop (forward-line -1))
1978 (setq x (buffer-substring (point)
1979 (tramp-line-end-position)))
1980 (when (or (not match) (string-match match x))
1982 (push (concat (file-name-as-directory directory)
1986 (tramp-send-command multi-method method user host "cd")
1987 (tramp-wait-for-output))
1990 ;; This function should return "foo/" for directories and "bar" for
1991 ;; files. We use `ls -ad' to get a list of files (including
1992 ;; directories), and `find . -type d \! -name . -prune' to get a list
1994 (defun tramp-handle-file-name-all-completions (filename directory)
1995 "Like `file-name-all-completions' for tramp files."
1996 (with-parsed-tramp-file-name directory nil
1997 (when (tramp-ange-ftp-file-name-p multi-method method)
1998 (tramp-invoke-ange-ftp 'file-name-all-completions
1999 filename directory))
2000 (unless (save-match-data (string-match "/" filename))
2001 (let* ((nowild tramp-completion-without-shell-p)
2004 (tramp-barf-unless-okay
2005 multi-method method user host
2006 (format "cd %s" (tramp-shell-quote-argument path))
2008 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2009 (tramp-shell-quote-argument path))
2011 ;; Get a list of directories and files, including reliably
2012 ;; tagging the directories with a trailing '/'. Because I
2013 ;; rock. --daniel@danann.net
2015 multi-method method user host
2016 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2017 "if test -d \"$f\" 2>/dev/null; "
2018 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2019 (tramp-get-ls-command multi-method method user host)
2020 (if (or nowild (zerop (length filename)))
2023 (tramp-shell-quote-argument filename)))))
2025 ;; Now grab the output.
2026 (tramp-wait-for-output)
2027 (goto-char (point-max))
2028 (while (zerop (forward-line -1))
2029 (push (buffer-substring (point)
2030 (tramp-line-end-position))
2033 (tramp-send-command multi-method method user host "cd")
2034 (tramp-wait-for-output)
2038 (all-completions filename (mapcar 'list result))
2042 ;; The following isn't needed for Emacs 20 but for 19.34?
2043 (defun tramp-handle-file-name-completion (filename directory)
2044 "Like `file-name-completion' for tramp files."
2045 (unless (tramp-tramp-file-p directory)
2047 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2049 (with-parsed-tramp-file-name directory nil
2050 (when (tramp-ange-ftp-file-name-p multi-method method)
2051 (tramp-invoke-ange-ftp 'file-name-completion
2052 filename directory))
2055 (mapcar (lambda (x) (cons x nil))
2056 (tramp-handle-file-name-all-completions filename directory)))))
2060 (defun tramp-handle-add-name-to-file
2061 (filename newname &optional ok-if-already-exists)
2062 "Like `add-name-to-file' for tramp files."
2063 (with-parsed-tramp-file-name filename v1
2064 (with-parsed-tramp-file-name newname v2
2065 (let ((ln (when v1 (tramp-get-remote-ln
2066 v1-multi-method v1-method v1-user v1-host))))
2067 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2068 (equal v1-multi-method v2-multi-method)
2069 (equal v1-method v2-method)
2070 (equal v1-user v2-user)
2071 (equal v1-host v2-host))
2072 (error "add-name-to-file: %s"
2073 "only implemented for same method, same user, same host"))
2074 (when (and (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2075 (tramp-ange-ftp-file-name-p v2-multi-method v2-method))
2076 (tramp-invoke-ange-ftp 'add-name-to-file
2077 filename newname ok-if-already-exists))
2078 (when (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2079 (tramp-invoke-ange-ftp 'add-name-to-file
2080 filename newname ok-if-already-exists))
2081 (when (tramp-ange-ftp-file-name-p v2-multi-method v2-method)
2082 (tramp-invoke-ange-ftp 'add-name-to-file
2083 filename newname ok-if-already-exists))
2084 (when (and (not ok-if-already-exists)
2085 (file-exists-p newname)
2086 (not (numberp ok-if-already-exists))
2089 "File %s already exists; make it a new name anyway? "
2091 (error "add-name-to-file: file %s already exists" newname))
2092 (tramp-barf-unless-okay
2093 v1-multi-method v1-method v1-user v1-host
2094 (format "%s %s %s" ln (tramp-shell-quote-argument v1-path)
2095 (tramp-shell-quote-argument v2-path))
2097 "error with add-name-to-file, see buffer `%s' for details"
2100 (defun tramp-handle-copy-file
2101 (filename newname &optional ok-if-already-exists keep-date)
2102 "Like `copy-file' for tramp files."
2103 ;; Check if both files are local -- invoke normal copy-file.
2104 ;; Otherwise, use tramp from local system.
2105 (setq filename (expand-file-name filename))
2106 (setq newname (expand-file-name newname))
2107 ;; At least one file a tramp file?
2108 (if (or (tramp-tramp-file-p filename)
2109 (tramp-tramp-file-p newname))
2110 (tramp-do-copy-or-rename-file
2111 'copy filename newname ok-if-already-exists keep-date)
2112 (tramp-run-real-handler
2114 (list filename newname ok-if-already-exists keep-date))))
2116 (defun tramp-handle-rename-file
2117 (filename newname &optional ok-if-already-exists)
2118 "Like `rename-file' for tramp files."
2119 ;; Check if both files are local -- invoke normal rename-file.
2120 ;; Otherwise, use tramp from local system.
2121 (setq filename (expand-file-name filename))
2122 (setq newname (expand-file-name newname))
2123 ;; At least one file a tramp file?
2124 (if (or (tramp-tramp-file-p filename)
2125 (tramp-tramp-file-p newname))
2126 (tramp-do-copy-or-rename-file
2127 'rename filename newname ok-if-already-exists)
2128 (tramp-run-real-handler 'rename-file
2129 (list filename newname ok-if-already-exists))))
2131 (defun tramp-do-copy-or-rename-file
2132 (op filename newname &optional ok-if-already-exists keep-date)
2133 "Copy or rename a remote file.
2134 OP must be `copy' or `rename' and indicates the operation to perform.
2135 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2136 the new file (for copy) or the new name of the file (for rename).
2137 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2138 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2141 This function is invoked by `tramp-handle-copy-file' and
2142 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2143 and `rename'. FILENAME and NEWNAME must be absolute file names."
2144 (unless (memq op '(copy rename))
2145 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2146 (unless ok-if-already-exists
2147 (when (file-exists-p newname)
2148 (signal 'file-already-exists
2150 (with-parsed-tramp-file-name filename v1
2151 (with-parsed-tramp-file-name newname v2
2152 (when (and (tramp-ange-ftp-file-name-p v1-multi-method v1-method)
2153 (tramp-ange-ftp-file-name-p v2-multi-method v2-method))
2154 (tramp-invoke-ange-ftp
2155 (if (eq op 'copy) 'copy-file 'rename-file)
2156 filename newname ok-if-already-exists keep-date))
2157 (let* ((mmeth (tramp-file-name-multi-method (or v1 v2)))
2158 (meth (tramp-file-name-method (or v1 v2)))
2159 (rcp-program (tramp-get-rcp-program mmeth meth))
2160 (rcp-args (tramp-get-rcp-args mmeth meth))
2161 (trampbuf (get-buffer-create "*tramp output*")))
2162 ;; Check if we can use a shortcut.
2163 (if (and v1-method v2-method
2164 (equal v1-multi-method v2-multi-method)
2165 (equal v1-method v2-method)
2166 (equal v1-host v2-host)
2167 (equal v1-user v2-user))
2168 ;; Shortcut: if method, host, user are the same for both
2169 ;; files, we invoke `cp' or `mv' on the remote host directly.
2170 (tramp-do-copy-or-rename-file-directly
2172 v1-multi-method v1-method v1-user v1-host v1-path v2-path
2174 ;; New algorithm: copy file first. Then, if operation is
2175 ;; `rename', go back and delete the original file if the copy
2178 ;; The following code uses a tramp program to copy the file.
2179 (let ((f1 (if (not v1)
2181 (tramp-make-rcp-program-file-name
2183 (tramp-shell-quote-argument v1-path))))
2186 (tramp-make-rcp-program-file-name
2188 (tramp-shell-quote-argument v2-path))))
2190 (if (tramp-tramp-file-p default-directory)
2191 (tramp-temporary-file-directory)
2192 default-directory)))
2194 (add-to-list 'rcp-args
2195 (tramp-get-rcp-keep-date-arg mmeth meth)))
2196 (save-excursion (set-buffer trampbuf) (erase-buffer))
2197 (unless (equal 0 (apply #'call-process
2198 (tramp-get-rcp-program mmeth meth)
2200 (append rcp-args (list f1 f2))))
2201 (pop-to-buffer trampbuf)
2202 (error (concat "tramp-do-copy-or-rename-file: %s"
2203 " didn't work, see buffer `%s' for details")
2204 (tramp-get-rcp-program mmeth meth) trampbuf)))
2205 ;; The following code uses an inline method for copying.
2206 ;; Let's start with a simple-minded approach: we create a new
2207 ;; buffer, insert the contents of the source file into it,
2208 ;; then write out the buffer. This should work fine, whether
2209 ;; the source or the target files are tramp files.
2210 ;; CCC TODO: error checking
2213 1 (concat "Warning: cannot preserve file time stamp"
2214 " with inline copying across machines")))
2216 (set-buffer trampbuf) (erase-buffer)
2217 (insert-file-contents-literally filename)
2218 (let ((coding-system-for-write 'no-conversion))
2219 (write-region (point-min) (point-max) newname))))
2221 ;; If the operation was `rename', delete the original file.
2222 (unless (eq op 'copy)
2223 (delete-file filename)))))))
2225 (defun tramp-do-copy-or-rename-file-directly
2226 (op multi-method method user host path1 path2 keep-date)
2227 "Invokes `cp' or `mv' on the remote system.
2228 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2229 respectively. METHOD, USER, and HOST specify the connection.
2230 PATH1 and PATH2 specify the two arguments of `cp' or `mv'.
2231 If KEEP-DATE is non-nil, preserve the time stamp when copying."
2232 ;; CCC: What happens to the timestamp when renaming?
2233 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
2234 ((eq op 'copy) "cp -f")
2235 ((eq op 'rename) "mv -f")
2237 "Unknown operation `%s', must be `copy' or `rename'"
2240 (tramp-barf-unless-okay
2241 multi-method method user host
2244 (tramp-shell-quote-argument path1)
2245 (tramp-shell-quote-argument path2))
2247 "Copying directly failed, see buffer `%s' for details."
2251 (defun tramp-handle-make-directory (dir &optional parents)
2252 "Like `make-directory' for tramp files."
2253 (with-parsed-tramp-file-name dir nil
2254 (when (tramp-ange-ftp-file-name-p multi-method method)
2255 (tramp-invoke-ange-ftp 'make-directory dir parents))
2256 (tramp-barf-unless-okay
2257 multi-method method user host
2259 (if parents "mkdir -p" "mkdir")
2260 (tramp-shell-quote-argument path))
2262 "Couldn't make directory %s" dir)))
2264 ;; CCC error checking?
2265 (defun tramp-handle-delete-directory (directory)
2266 "Like `delete-directory' for tramp files."
2267 (with-parsed-tramp-file-name directory nil
2268 (when (tramp-ange-ftp-file-name-p multi-method method)
2269 (tramp-invoke-ange-ftp 'delete-directory directory))
2272 multi-method method user host
2273 (format "rmdir %s ; echo ok"
2274 (tramp-shell-quote-argument path)))
2275 (tramp-wait-for-output))))
2277 (defun tramp-handle-delete-file (filename)
2278 "Like `delete-file' for tramp files."
2279 (with-parsed-tramp-file-name filename nil
2280 (when (tramp-ange-ftp-file-name-p multi-method method)
2281 (tramp-invoke-ange-ftp 'delete-file filename))
2283 (unless (zerop (tramp-send-command-and-check
2284 multi-method method user host
2286 (tramp-shell-quote-argument path))))
2287 (signal 'file-error "Couldn't delete Tramp file")))))
2291 ;; CCC: This does not seem to be enough. Something dies when
2292 ;; we try and delete two directories under TRAMP :/
2293 (defun tramp-handle-dired-recursive-delete-directory (filename)
2294 "Recursively delete the directory given.
2295 This is like `dired-recursive-delete-directory' for tramp files."
2296 (with-parsed-tramp-file-name filename nil
2297 (when (tramp-ange-ftp-file-name-p multi-method method)
2298 (tramp-invoke-ange-ftp 'dired-recursive-delete-directory
2300 ;; run a shell command 'rm -r <path>'
2301 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
2302 (or (tramp-handle-file-exists-p filename)
2305 (list "Removing old file name" "no such directory" filename)))
2306 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
2307 (tramp-send-command multi-method method user host
2308 (format "rm -r %s" (tramp-shell-quote-argument path)))
2309 ;; Wait for the remote system to return to us...
2310 ;; This might take a while, allow it plenty of time.
2311 (tramp-wait-for-output 120)
2312 ;; Make sure that it worked...
2313 (and (tramp-handle-file-exists-p filename)
2314 (error "Failed to recusively delete %s" filename))))
2317 (defun tramp-handle-dired-call-process (program discard &rest arguments)
2318 "Like `dired-call-process' for tramp files."
2319 (with-parsed-tramp-file-name default-directory nil
2320 (when (tramp-ange-ftp-file-name-p multi-method method)
2321 (let ((default-directory
2322 (tramp-make-ange-ftp-file-name user host path)))
2323 (tramp-invoke-ange-ftp 'dired-call-process
2324 program discard arguments)))
2326 (tramp-barf-unless-okay
2327 multi-method method user host
2328 (format "cd %s" (tramp-shell-quote-argument path))
2330 "tramp-handle-dired-call-process: Couldn't `cd %s'"
2331 (tramp-shell-quote-argument path))
2333 multi-method method user host
2334 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
2335 (tramp-wait-for-output))
2337 (insert-buffer (tramp-get-buffer multi-method method user host)))
2340 (tramp-send-command-and-check multi-method method user host nil)
2341 (tramp-send-command multi-method method user host "cd")
2342 (tramp-wait-for-output)))))
2344 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
2345 ;; not sure at all that this is the right way to do it, but let's hope
2346 ;; it works for now, and wait for a guru to point out the Right Way to
2348 ;;(eval-when-compile
2349 ;; (unless (fboundp 'dired-insert-set-properties)
2350 ;; (fset 'dired-insert-set-properties 'ignore)))
2351 ;; Gerd suggests this:
2352 (eval-when-compile (require 'dired))
2353 ;; Note that dired is required at run-time, too, when it is needed.
2354 ;; It is only needed on XEmacs for the function
2355 ;; `dired-insert-set-properties'.
2357 (defun tramp-handle-insert-directory
2358 (filename switches &optional wildcard full-directory-p)
2359 "Like `insert-directory' for tramp files."
2360 (with-parsed-tramp-file-name filename nil
2361 (when (tramp-ange-ftp-file-name-p multi-method method)
2362 (tramp-invoke-ange-ftp 'insert-directory
2363 filename switches wildcard full-directory-p))
2364 (tramp-message-for-buffer
2365 multi-method method user host 10
2366 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2367 switches filename (if wildcard "yes" "no")
2368 (if full-directory-p "yes" "no"))
2370 (setq wildcard (file-name-nondirectory path))
2371 (setq path (file-name-directory path)))
2372 (when (listp switches)
2373 (setq switches (mapconcat 'identity switches " ")))
2374 (unless full-directory-p
2375 (setq switches (concat "-d " switches)))
2377 (setq switches (concat switches " " wildcard)))
2379 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2380 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2381 (if full-directory-p
2383 multi-method method user host
2385 (tramp-get-ls-command multi-method method user host)
2389 (tramp-shell-quote-argument (concat path ".")))))
2390 (tramp-barf-unless-okay
2391 multi-method method user host
2392 (format "cd %s" (tramp-shell-quote-argument
2393 (file-name-directory path)))
2396 (tramp-shell-quote-argument (file-name-directory path)))
2398 multi-method method user host
2400 (tramp-get-ls-command multi-method method user host)
2402 (if full-directory-p
2403 ;; Add "/." to make sure we got complete dir
2404 ;; listing for symlinks, too.
2405 (concat (file-name-as-directory
2406 (file-name-nondirectory path)) ".")
2407 (file-name-nondirectory path)))))
2408 (sit-for 1) ;needed for rsh but not ssh?
2409 (tramp-wait-for-output))
2410 (insert-buffer (tramp-get-buffer multi-method method user host))
2411 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
2412 ;; that doesn't exist on Emacs, so we use this workaround instead.
2413 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
2414 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
2415 (let ((zmacs-region-stays t))
2416 (exchange-point-and-mark))
2418 (tramp-send-command multi-method method user host "cd")
2419 (tramp-wait-for-output))
2420 ;; Another XEmacs specialty follows. What's the right way to do
2422 (when (and (featurep 'xemacs)
2423 (eq major-mode 'dired-mode))
2426 (dired-insert-set-properties (point) (mark t))))))
2428 ;; Continuation of kluge to pacify byte-compiler.
2429 ;;(eval-when-compile
2430 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
2431 ;; (fmakunbound 'dired-insert-set-properties)))
2433 ;; CCC is this the right thing to do?
2434 (defun tramp-handle-unhandled-file-name-directory (filename)
2435 "Like `unhandled-file-name-directory' for tramp files."
2436 (with-parsed-tramp-file-name filename nil
2437 (when (tramp-ange-ftp-file-name-p multi-method method)
2438 (tramp-invoke-ange-ftp 'unhandled-file-name-directory
2440 (expand-file-name "~/")))
2442 ;; Canonicalization of file names.
2444 (defun tramp-drop-volume-letter (name)
2445 "Cut off unnecessary drive letter from file NAME.
2446 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2447 locally on a remote file name. When the local system is a W32 system
2448 but the remote system is Unix, this introduces a superfluous drive
2449 letter into the file name. This function removes it.
2451 Doesn't do anything if the NAME does not start with a drive letter."
2452 (if (and (> (length name) 1)
2453 (char-equal (aref name 1) ?:)
2454 (let ((c1 (aref name 0)))
2455 (or (and (>= c1 ?A) (<= c1 ?Z))
2456 (and (>= c1 ?a) (<= c1 ?z)))))
2460 (defun tramp-handle-expand-file-name (name &optional dir)
2461 "Like `expand-file-name' for tramp files."
2462 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2463 (setq dir (or dir default-directory "/"))
2464 ;; Unless NAME is absolute, concat DIR and NAME.
2465 (unless (file-name-absolute-p name)
2466 (setq name (concat (file-name-as-directory dir) name)))
2467 ;; If NAME is not a tramp file, run the real handler
2468 (if (not (tramp-tramp-file-p name))
2469 (tramp-run-real-handler 'expand-file-name
2472 (with-parsed-tramp-file-name name nil
2473 (when (tramp-ange-ftp-file-name-p multi-method method)
2474 (tramp-invoke-ange-ftp 'expand-file-name name nil))
2475 (unless (file-name-absolute-p path)
2476 (setq path (concat "~/" path)))
2478 ;; Tilde expansion if necessary. This needs a shell which
2479 ;; groks tilde expansion! The function `tramp-find-shell' is
2480 ;; supposed to find such a shell on the remote host. Please
2481 ;; tell me about it when this doesn't work on your system.
2482 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" path)
2483 (let ((uname (match-string 1 path))
2484 (fname (match-string 2 path)))
2485 ;; CCC fanatic error checking?
2486 (set-buffer (tramp-get-buffer multi-method method user host))
2489 multi-method method user host
2490 (format "cd %s; pwd" uname)
2492 (tramp-wait-for-output)
2493 (goto-char (point-min))
2494 (setq uname (buffer-substring (point) (tramp-line-end-position)))
2495 (setq path (concat uname fname))
2497 ;; No tilde characters in file name, do normal
2498 ;; expand-file-name (this does "/./" and "/../"). We bind
2499 ;; directory-sep-char here for XEmacs on Windows, which would
2500 ;; otherwise use backslash.
2501 (let ((directory-sep-char ?/))
2502 (tramp-make-tramp-file-name
2503 multi-method method user host
2504 (tramp-drop-volume-letter
2505 (tramp-run-real-handler 'expand-file-name (list path)))))))))
2509 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
2510 "Like `shell-command' for tramp files.
2511 This will break if COMMAND prints a newline, followed by the value of
2512 `tramp-end-of-output', followed by another newline."
2513 (if (tramp-tramp-file-p default-directory)
2514 (with-parsed-tramp-file-name default-directory nil
2515 (when (tramp-ange-ftp-file-name-p multi-method method)
2516 (let ((default-directory (tramp-make-ange-ftp-file-name
2518 (tramp-invoke-ange-ftp 'shell-command
2519 command output-buffer error-buffer)))
2521 (when (string-match "&[ \t]*\\'" command)
2522 (error "Tramp doesn't grok asynchronous shell commands, yet"))
2524 (error "Tramp doesn't grok optional third arg ERROR-BUFFER, yet"))
2526 (tramp-barf-unless-okay
2527 multi-method method user host
2528 (format "cd %s" (tramp-shell-quote-argument path))
2530 "tramp-handle-shell-command: Couldn't `cd %s'"
2531 (tramp-shell-quote-argument path))
2532 (tramp-send-command multi-method method user host
2533 (concat command "; tramp_old_status=$?"))
2534 ;; This will break if the shell command prints "/////"
2535 ;; somewhere. Let's just hope for the best...
2536 (tramp-wait-for-output))
2537 (unless output-buffer
2538 (setq output-buffer (get-buffer-create "*Shell Command Output*"))
2539 (set-buffer output-buffer)
2541 (unless (bufferp output-buffer)
2542 (setq output-buffer (current-buffer)))
2543 (set-buffer output-buffer)
2544 (insert-buffer (tramp-get-buffer multi-method method user host))
2546 (tramp-send-command multi-method method user host "cd")
2547 (tramp-wait-for-output)
2549 multi-method method user host
2550 (concat "tramp_set_exit_status $tramp_old_status;"
2551 " echo tramp_exit_status $?"))
2552 (tramp-wait-for-output)
2553 (goto-char (point-max))
2554 (unless (search-backward "tramp_exit_status " nil t)
2555 (error "Couldn't find exit status of `%s'" command))
2556 (skip-chars-forward "^ ")
2557 (setq status (read (current-buffer))))
2558 (unless (zerop (buffer-size))
2559 (pop-to-buffer output-buffer))
2561 ;; The following is only executed if something strange was
2562 ;; happening. Emit a helpful message and do it anyway.
2563 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
2565 (tramp-run-real-handler 'shell-command
2566 (list command output-buffer error-buffer)))
2570 (defsubst tramp-make-temp-file ()
2571 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
2572 (expand-file-name tramp-temp-name-prefix
2573 (tramp-temporary-file-directory))))
2575 (defun tramp-handle-file-local-copy (filename)
2576 "Like `file-local-copy' for tramp files."
2577 (with-parsed-tramp-file-name filename nil
2578 (when (tramp-ange-ftp-file-name-p multi-method method)
2579 (tramp-invoke-ange-ftp 'file-local-copy filename))
2580 (let ((trampbuf (get-buffer-create "*tramp output*"))
2582 (unless (file-exists-p filename)
2583 (error "Cannot make local copy of non-existing file `%s'"
2585 (setq tmpfil (tramp-make-temp-file))
2586 (cond ((tramp-get-rcp-program multi-method method)
2587 ;; Use tramp-like program for file transfer.
2588 (tramp-message-for-buffer
2589 multi-method method user host
2590 5 "Fetching %s to tmp file %s..." filename tmpfil)
2591 (save-excursion (set-buffer trampbuf) (erase-buffer))
2594 (apply #'call-process
2595 (tramp-get-rcp-program multi-method method)
2597 (append (tramp-get-rcp-args multi-method method)
2599 (tramp-make-rcp-program-file-name
2601 (tramp-shell-quote-argument path))
2603 (pop-to-buffer trampbuf)
2605 (concat "tramp-handle-file-local-copy: `%s' didn't work, "
2606 "see buffer `%s' for details")
2607 (tramp-get-rcp-program multi-method method) trampbuf))
2608 (tramp-message-for-buffer
2609 multi-method method user host
2610 5 "Fetching %s to tmp file %s...done" filename tmpfil))
2611 ((and (tramp-get-encoding-command multi-method method)
2612 (tramp-get-decoding-command multi-method method))
2613 ;; Use inline encoding for file transfer.
2615 ;; Following line for setting tramp-current-method,
2616 ;; tramp-current-user, tramp-current-host.
2617 (set-buffer (tramp-get-buffer multi-method method user host))
2618 (tramp-message 5 "Encoding remote file %s..." filename)
2619 (tramp-barf-unless-okay
2620 multi-method method user host
2621 (concat (tramp-get-encoding-command multi-method method)
2622 " < " (tramp-shell-quote-argument path))
2624 "Encoding remote file failed, see buffer `%s' for details"
2625 (tramp-get-buffer multi-method method user host))
2626 ;; Remove trailing status code
2627 (goto-char (point-max))
2628 (delete-region (point) (progn (forward-line -1) (point)))
2630 (tramp-message 5 "Decoding remote file %s..." filename)
2631 (if (and (tramp-get-decoding-function multi-method method)
2632 (fboundp (tramp-get-decoding-function
2633 multi-method method)))
2634 ;; If tramp-decoding-function is defined for this
2635 ;; method, we call it.
2636 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
2639 (insert-buffer (tramp-get-buffer multi-method method
2641 (tramp-message-for-buffer
2642 multi-method method user host
2643 6 "Decoding remote file %s with function %s..."
2645 (tramp-get-decoding-function multi-method method))
2647 (let ((coding-system-for-write 'no-conversion))
2648 (funcall (tramp-get-decoding-function
2649 multi-method method)
2652 (write-region (point-min) (point-max) tmpfil))
2653 (kill-buffer tmpbuf))
2654 ;; If tramp-decoding-function is not defined for this
2655 ;; method, we invoke tramp-decoding-command instead.
2656 (let ((tmpfil2 (tramp-make-temp-file)))
2657 (write-region (point-min) (point-max) tmpfil2)
2659 6 "Decoding remote file %s with command %s..."
2661 (tramp-get-decoding-command multi-method method))
2667 "-c" (concat (tramp-get-decoding-command
2668 multi-method method)
2670 (delete-file tmpfil2)))
2671 (tramp-message-for-buffer
2672 multi-method method user host
2673 5 "Decoding remote file %s...done" filename)))
2675 (t (error "Wrong method specification for `%s'" method)))
2679 (defun tramp-handle-insert-file-contents
2680 (filename &optional visit beg end replace)
2681 "Like `insert-file-contents' for tramp files."
2682 (barf-if-buffer-read-only)
2683 (setq filename (expand-file-name filename))
2684 (with-parsed-tramp-file-name filename nil
2685 (when (tramp-ange-ftp-file-name-p multi-method method)
2686 (tramp-invoke-ange-ftp 'insert-file-contents
2687 filename visit beg end replace))
2688 (if (not (tramp-handle-file-exists-p filename))
2691 (setq buffer-file-name filename)
2692 (set-visited-file-modtime)
2693 (set-buffer-modified-p nil))
2695 (format "File `%s' not found on remote host" filename))
2696 (list (tramp-handle-expand-file-name filename) 0))
2697 (let ((local-copy (tramp-handle-file-local-copy filename))
2698 (coding-system-used nil)
2701 (setq buffer-file-name filename)
2702 (set-visited-file-modtime)
2703 (set-buffer-modified-p nil))
2704 (tramp-message-for-buffer
2705 multi-method method user host
2706 9 "Inserting local temp file `%s'..." local-copy)
2708 (tramp-run-real-handler 'insert-file-contents
2709 (list local-copy nil beg end replace)))
2710 ;; Now `last-coding-system-used' has right value. Remember it.
2711 (when (boundp 'last-coding-system-used)
2712 (setq coding-system-used last-coding-system-used))
2713 (tramp-message 9 "Inserting local temp file `%s'...done" local-copy)
2714 (delete-file local-copy)
2715 (when (boundp 'last-coding-system-used)
2716 (setq last-coding-system-used coding-system-used))
2717 (list (expand-file-name filename)
2718 (second result))))))
2720 ;; CCC grok APPEND, LOCKNAME, CONFIRM
2721 (defun tramp-handle-write-region
2722 (start end filename &optional append visit lockname confirm)
2723 "Like `write-region' for tramp files."
2724 (unless (eq append nil)
2725 (error "Cannot append to file using tramp (`%s')" filename))
2726 (setq filename (expand-file-name filename))
2727 ;; Following part commented out because we don't know what to do about
2728 ;; file locking, and it does not appear to be a problem to ignore it.
2729 ;; Ange-ftp ignores it, too.
2730 ;; (when (and lockname (stringp lockname))
2731 ;; (setq lockname (expand-file-name lockname)))
2732 ;; (unless (or (eq lockname nil)
2733 ;; (string= lockname filename))
2735 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
2736 ;; XEmacs takes a coding system as the sevent argument, not `confirm'
2737 (when (and (not (featurep 'xemacs))
2738 confirm (file-exists-p filename))
2739 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
2741 (error "File not overwritten")))
2742 (with-parsed-tramp-file-name filename nil
2743 (when (tramp-ange-ftp-file-name-p multi-method method)
2744 (tramp-invoke-ange-ftp 'write-region
2745 start end filename append visit lockname confirm))
2746 (let ((curbuf (current-buffer))
2747 (rcp-program (tramp-get-rcp-program multi-method method))
2748 (rcp-args (tramp-get-rcp-args multi-method method))
2749 (encoding-command (tramp-get-encoding-command multi-method method))
2751 (tramp-get-encoding-function multi-method method))
2752 (decoding-command (tramp-get-decoding-command multi-method method))
2753 (trampbuf (get-buffer-create "*tramp output*"))
2754 ;; We use this to save the value of `last-coding-system-used'
2755 ;; after writing the tmp file. At the end of the function,
2756 ;; we set `last-coding-system-used' to this saved value.
2757 ;; This way, any intermediary coding systems used while
2758 ;; talking to the remote shell or suchlike won't hose this
2759 ;; variable. This approach was snarfed from ange-ftp.el.
2762 ;; Write region into a tmp file. This isn't really needed if we
2763 ;; use an encoding function, but currently we use it always
2764 ;; because this makes the logic simpler.
2765 (setq tmpfil (tramp-make-temp-file))
2766 ;; We say `no-message' here because we don't want the visited file
2767 ;; modtime data to be clobbered from the temp file. We call
2768 ;; `set-visited-file-modtime' ourselves later on.
2769 (tramp-run-real-handler
2771 (if confirm ; don't pass this arg unless defined for backward compat.
2772 (list start end tmpfil append 'no-message lockname confirm)
2773 (list start end tmpfil append 'no-message lockname)))
2774 ;; Now, `last-coding-system-used' has the right value. Remember it.
2775 (when (boundp 'last-coding-system-used)
2776 (setq coding-system-used last-coding-system-used))
2777 ;; This is a bit lengthy due to the different methods possible for
2778 ;; file transfer. First, we check whether the method uses an rcp
2779 ;; program. If so, we call it. Otherwise, both encoding and
2780 ;; decoding command must be specified. However, if the method
2781 ;; _also_ specifies an encoding function, then that is used for
2782 ;; encoding the contents of the tmp file.
2784 ;; use rcp-like program for file transfer
2785 (let ((argl (append rcp-args
2788 (tramp-make-rcp-program-file-name
2790 (tramp-shell-quote-argument path))))))
2791 (tramp-message-for-buffer
2792 multi-method method user host
2793 6 "Writing tmp file using `%s'..." rcp-program)
2794 (save-excursion (set-buffer trampbuf) (erase-buffer))
2795 (when tramp-debug-buffer
2797 (set-buffer (tramp-get-debug-buffer multi-method
2799 (goto-char (point-max))
2800 (tramp-insert-with-face
2801 'bold (format "$ %s %s\n" rcp-program
2802 (mapconcat 'identity argl " ")))))
2804 (apply #'call-process
2805 rcp-program nil trampbuf nil argl))
2806 (pop-to-buffer trampbuf)
2808 "Cannot write region to file `%s', command `%s' failed"
2809 filename rcp-program))
2810 (tramp-message-for-buffer
2811 multi-method method user host
2812 6 "Transferring file using `%s'...done"
2814 ((and encoding-command decoding-command)
2815 ;; Use inline file transfer
2816 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
2818 ;; Encode tmpfil into tmpbuf
2819 (tramp-message-for-buffer multi-method method user host
2820 5 "Encoding region...")
2823 ;; Use encoding function or command.
2824 (if (and encoding-function
2825 (fboundp encoding-function))
2827 (tramp-message-for-buffer
2828 multi-method method user host
2829 6 "Encoding region using function...")
2830 (insert-file-contents-literally tmpfil)
2831 ;; CCC. The following `let' is a workaround for
2832 ;; the base64.el that comes with pgnus-0.84. If
2833 ;; both of the following conditions are
2834 ;; satisfied, it tries to write to a local file
2835 ;; in default-directory, but at this point,
2836 ;; default-directory is remote.
2837 ;; (CALL-PROCESS-REGION can't write to remote
2838 ;; files, it seems.) The file in question is a
2840 (let ((default-directory
2841 (tramp-temporary-file-directory)))
2842 (funcall encoding-function (point-min) (point-max)))
2843 (goto-char (point-max))
2846 (tramp-message-for-buffer
2847 multi-method method user host
2848 6 "Encoding region using command...")
2852 tmpfil ;input = local tmp file
2853 t ;output is current buffer
2854 nil ;don't redisplay
2857 (pop-to-buffer trampbuf)
2858 (error (concat "Cannot write to `%s', local encoding"
2859 " command `%s' failed")
2860 filename encoding-command)))
2861 ;; Send tmpbuf into remote decoding command which
2862 ;; writes to remote file. Because this happens on the
2863 ;; remote host, we cannot use the function.
2864 (tramp-message-for-buffer
2865 multi-method method user host
2866 5 "Decoding region into remote file %s..." filename)
2868 multi-method method user host
2869 (format "%s >%s <<'EOF'"
2871 (tramp-shell-quote-argument path)))
2873 (tramp-message-for-buffer
2874 multi-method method user host
2875 6 "Sending data to remote host...")
2876 (tramp-send-region multi-method method user host
2877 (point-min) (point-max))
2878 ;; wait for remote decoding to complete
2879 (tramp-message-for-buffer
2880 multi-method method user host
2881 6 "Sending end of data token...")
2883 multi-method method user host "EOF")
2884 (tramp-message-for-buffer
2885 multi-method method user host 6
2886 "Waiting for remote host to process data...")
2887 (set-buffer (tramp-get-buffer multi-method method user host))
2888 (tramp-wait-for-output)
2889 (tramp-barf-unless-okay
2890 multi-method method user host nil nil 'file-error
2891 (concat "Couldn't write region to `%s',"
2892 " decode using `%s' failed")
2893 filename decoding-command)
2894 (tramp-message 5 "Decoding region into remote file %s...done"
2896 (kill-buffer tmpbuf))))
2899 (concat "Method `%s' should specify both encoding and "
2900 "decoding command or an rcp program")
2902 (delete-file tmpfil)
2903 (unless (equal curbuf (current-buffer))
2904 (error "Buffer has changed from `%s' to `%s'"
2905 curbuf (current-buffer)))
2907 (set-visited-file-modtime))
2908 ;; Make `last-coding-system-used' have the right value.
2909 (when (boundp 'last-coding-system-used)
2910 (setq last-coding-system-used coding-system-used))
2911 (when (or (eq visit t)
2914 (message "Wrote %s" filename)))))
2916 ;; Call down to the real handler.
2917 ;; Because EFS does not play nicely with TRAMP (both systems match an
2918 ;; TRAMP path) it is needed to disable efs as well as tramp for the
2921 ;; Other than that, this is the canon file-handler code that the doco
2922 ;; says should be used here. Which is nice.
2924 ;; Under XEmacs current, EFS also hooks in as
2925 ;; efs-sifn-handler-function to handle any path with environment
2926 ;; variables. This has two implications:
2927 ;; 1) That EFS may not be completely dead (yet) for TRAMP paths
2928 ;; 2) That TRAMP might want to do the same thing.
2929 ;; Details as they come in.
2931 ;; Daniel Pittman <daniel@danann.net>
2933 ;; (defun tramp-run-real-handler (operation args)
2934 ;; "Invoke normal file name handler for OPERATION.
2935 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
2936 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
2938 ;; (let ((inhibit-file-name-handlers
2939 ;; (list 'tramp-file-name-handler
2940 ;; 'efs-file-handler-function
2941 ;; 'ange-ftp-hook-function
2942 ;; (and (eq inhibit-file-name-operation operation)
2943 ;; inhibit-file-name-handlers)))
2944 ;; (inhibit-file-name-operation operation))
2945 ;; (apply operation args)))
2947 (defun tramp-run-real-handler (operation args)
2948 "Invoke normal file name handler for OPERATION.
2949 First arg specifies the OPERATION, second arg is a list of arguments to
2950 pass to the OPERATION."
2951 (let ((inhibit-file-name-handlers
2952 (list 'tramp-file-name-handler
2953 (and (eq inhibit-file-name-operation operation)
2954 inhibit-file-name-handlers)))
2955 (inhibit-file-name-operation operation))
2956 (apply operation args)))
2960 (defun tramp-file-name-handler (operation &rest args)
2961 "Invoke tramp file name handler.
2962 Falls back to normal file name handler if no tramp file name handler exists."
2963 (let ((fn (assoc operation tramp-file-name-handler-alist)))
2965 (catch 'tramp-forward-to-ange-ftp
2966 (save-match-data (apply (cdr fn) args)))
2967 (tramp-run-real-handler operation args))))
2969 ;; Register in file name handler alist
2971 (add-to-list 'file-name-handler-alist
2972 (cons tramp-file-name-regexp 'tramp-file-name-handler))
2975 (defun tramp-handle-ange-ftp ()
2976 "Turn Ange-FTP off and an Ange-FTP-like filename format.
2977 Requests suitable for Ange-FTP will be forwarded to Ange-FTP.
2978 Also see the variables `tramp-ftp-method', `tramp-default-method',
2979 and `tramp-default-method-alist'."
2981 (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist))
2982 (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist))
2983 (a3 (rassq 'tramp-file-name-handler file-name-handler-alist)))
2984 (setq file-name-handler-alist
2985 (delete a1 (delete a2 (delete a3 file-name-handler-alist)))))
2986 (setq tramp-file-name-structure
2987 (list (concat "\\`/\\(\\([a-zA-Z0-9]+\\)#\\)?" ;method
2988 "\\(\\([^:@/]+\\)@\\)?" ;user
2989 "\\([^:/]+\\):" ;host
2990 "\\(.*\\)\\'") ;path
2992 tramp-file-name-regexp "\\`/[^/:]+:"
2993 tramp-make-tramp-file-format "/%m#%u@%h:%p"
2994 tramp-make-tramp-file-user-nil-format "/%m#%h:%p"
2995 tramp-multi-file-name-structure
2996 (list (concat "\\`\\([a-zA-Z0-9]+\\)\\)?" ;method
2997 "\\(\\(%s\\)+\\)" ;hops
2998 ":\\(.*\\)\\'") ;path
3000 tramp-multi-file-name-hop-structure
3001 (list (concat ":\\([a-zA-z0-9_]+\\):" ;hop method
3002 "\\([^@:/]+\\)@" ;user
3003 "\\([^:/]+\\)") ;host
3005 tramp-make-multi-tramp-file-format
3006 (list "/%m" ":%m:%u@%h" ":%p"))
3007 (add-to-list 'file-name-handler-alist
3008 (cons tramp-file-name-regexp 'tramp-file-name-handler))
3009 (tramp-repair-jka-compr))
3011 (defun tramp-repair-jka-compr ()
3012 "If jka-compr is already loaded, move it to the front of
3013 `file-name-handler-alist'. On Emacs 21.4 or so this will not be
3015 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
3017 (setq file-name-handler-alist
3018 (cons jka (delete jka file-name-handler-alist))))))
3019 (tramp-repair-jka-compr)
3021 (defun tramp-invoke-ange-ftp (operation &rest args)
3022 "Invoke the Ange-FTP handler function and throw."
3023 (let ((ange-ftp-name-format
3024 (list (nth 0 tramp-file-name-structure)
3025 (nth 3 tramp-file-name-structure)
3026 (nth 2 tramp-file-name-structure)
3027 (nth 4 tramp-file-name-structure))))
3028 (throw 'tramp-forward-to-ange-ftp
3029 (apply 'ange-ftp-hook-function operation args))))
3031 (defun tramp-ange-ftp-file-name-p (multi-method method)
3032 "Check if it's a filename that should be forwarded to Ange-FTP."
3033 (and (null multi-method) (string= method tramp-ftp-method)))
3036 ;;; Interactions with other packages:
3038 ;; -- complete.el --
3040 ;; This function contributed by Ed Sabol
3041 (defun tramp-handle-expand-many-files (name)
3042 "Like `PC-expand-many-files' for tramp files."
3043 (with-parsed-tramp-file-name name nil
3044 (when (tramp-ange-ftp-file-name-p multi-method method)
3045 (tramp-invoke-ange-ftp 'expand-many-files name))
3047 (if (or (string-match "\\*" name)
3048 (string-match "\\?" name)
3049 (string-match "\\[.*\\]" name))
3053 ;; Perhaps invoke Ange-FTP.
3054 (when (string= method tramp-ftp-method)
3055 (signal 'tramp-run-ange-ftp (list 0)))
3056 ;; CCC: To do it right, we should quote certain characters
3057 ;; in the file name, but since the echo command is going to
3058 ;; break anyway when there are spaces in the file names, we
3060 ;;-(let ((comint-file-name-quote-list
3061 ;;- (set-difference tramp-file-name-quote-list
3062 ;;- '(?\* ?\? ?[ ?]))))
3063 ;;- (tramp-send-command
3064 ;;- multi-method method user host
3065 ;;- (format "echo %s" (comint-quote-filename path)))
3066 ;;- (tramp-wait-for-output))
3067 (tramp-send-command multi-method method user host
3068 (format "echo %s" path))
3069 (tramp-wait-for-output)
3070 (setq bufstr (buffer-substring (point-min)
3071 (tramp-line-end-position)))
3072 (goto-char (point-min))
3073 (if (string-equal path bufstr)
3076 (while (search-forward " " nil t)
3077 (delete-backward-char 1)
3079 (goto-char (point-max))
3080 (delete-backward-char 1)
3082 (goto-char (point-min))
3084 (function (lambda (x)
3085 (tramp-make-tramp-file-name multi-method method
3087 (read (current-buffer))))))
3088 (list (tramp-handle-expand-file-name name))))))
3090 ;; Check for complete.el and override PC-expand-many-files if appropriate.
3092 (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning
3094 (defun tramp-setup-complete ()
3095 (fset 'tramp-save-PC-expand-many-files
3096 (symbol-function 'PC-expand-many-files))
3097 (defun PC-expand-many-files (name)
3098 (if (tramp-tramp-file-p name)
3099 (tramp-handle-expand-many-files name)
3100 (tramp-save-PC-expand-many-files name))))
3102 ;; Why isn't eval-after-load sufficient?
3103 (if (fboundp 'PC-expand-many-files)
3104 (tramp-setup-complete)
3105 (eval-after-load "complete" '(tramp-setup-complete)))
3110 ;;; Internal Functions:
3112 (defun tramp-set-auto-save ()
3113 (when (and (buffer-file-name)
3114 (tramp-tramp-file-p (buffer-file-name))
3116 (auto-save-mode 1)))
3117 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3119 (defun tramp-run-test (switch filename)
3120 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3121 Returns the exit code of the `test' program."
3122 (let ((v (tramp-dissect-file-name filename)))
3124 (tramp-send-command-and-check
3125 (tramp-file-name-multi-method v) (tramp-file-name-method v)
3126 (tramp-file-name-user v) (tramp-file-name-host v)
3127 (format "test %s %s" switch
3128 (tramp-shell-quote-argument (tramp-file-name-path v)))))))
3130 (defun tramp-run-test2 (program file1 file2 &optional switch)
3131 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
3132 The optional SWITCH is inserted between the two files.
3133 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
3134 hosts, or files, disagree."
3135 (let* ((v1 (tramp-dissect-file-name file1))
3136 (v2 (tramp-dissect-file-name file2))
3137 (mmethod1 (tramp-file-name-multi-method v1))
3138 (mmethod2 (tramp-file-name-multi-method v2))
3139 (method1 (tramp-file-name-method v1))
3140 (method2 (tramp-file-name-method v2))
3141 (user1 (tramp-file-name-user v1))
3142 (user2 (tramp-file-name-user v2))
3143 (host1 (tramp-file-name-host v1))
3144 (host2 (tramp-file-name-host v2))
3145 (path1 (tramp-file-name-path v1))
3146 (path2 (tramp-file-name-path v2)))
3147 (unless (and method1 method2 host1 host2
3148 (equal mmethod1 mmethod2)
3149 (equal method1 method2)
3151 (equal host1 host2))
3152 (error "tramp-run-test2: %s"
3153 "only implemented for same method, same user, same host"))
3155 (tramp-send-command-and-check
3156 mmethod1 method1 user1 host1
3157 (format "%s %s %s %s"
3159 (tramp-shell-quote-argument path1)
3161 (tramp-shell-quote-argument path2))))))
3163 (defun tramp-buffer-name (multi-method method user host)
3164 "A name for the connection buffer for USER at HOST using METHOD."
3166 (tramp-buffer-name-multi-method "tramp" multi-method method user host))
3168 (format "*tramp/%s %s@%s*" method user host))
3170 (format "*tramp/%s %s*" method host))))
3172 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
3173 "A name for the multi method connection buffer.
3174 MULTI-METHOD gives the multi method, METHOD the array of methods,
3175 USER the array of user names, HOST the array of host names."
3176 (unless (and (= (length method) (length user))
3177 (= (length method) (length host)))
3178 (error "Syntax error in multi method (implementation error)"))
3179 (let ((len (length method))
3184 (cons (if (aref user i)
3185 (format "%s#%s@%s:" (aref method i)
3186 (aref user i) (aref host i))
3187 (format "%s@%s:" (aref method i) (aref host i)))
3190 (format "*%s/%s %s*"
3192 (apply 'concat (reverse string-list)))))
3194 (defun tramp-get-buffer (multi-method method user host)
3195 "Get the connection buffer to be used for USER at HOST using METHOD."
3196 (get-buffer-create (tramp-buffer-name multi-method method user host)))
3198 (defun tramp-debug-buffer-name (multi-method method user host)
3199 "A name for the debug buffer for USER at HOST using METHOD."
3201 (tramp-buffer-name-multi-method "debug tramp"
3202 multi-method method user host))
3204 (format "*debug tramp/%s %s@%s*" method user host))
3206 (format "*debug tramp/%s %s*" method host))))
3208 (defun tramp-get-debug-buffer (multi-method method user host)
3209 "Get the debug buffer for USER at HOST using METHOD."
3210 (get-buffer-create (tramp-debug-buffer-name multi-method method user host)))
3212 (defun tramp-find-executable (multi-method method user host
3213 progname dirlist ignore-tilde)
3214 "Searches for PROGNAME in all directories mentioned in DIRLIST.
3215 First args METHOD, USER and HOST specify the connection, PROGNAME
3216 is the program to search for, and DIRLIST gives the list of directories
3217 to search. If IGNORE-TILDE is non-nil, directory names starting
3218 with `~' will be ignored.
3220 Returns the full path name of PROGNAME, if found, and nil otherwise.
3222 This function expects to be in the right *tramp* buffer."
3225 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
3226 ;; `remove' is in CL, and we want to avoid CL dependencies.
3229 (setq d (car dirlist))
3230 (setq dirlist (cdr dirlist))
3231 (unless (char-equal ?~ (aref d 0))
3232 (setq newdl (cons d newdl))))
3233 (setq dirlist (nreverse newdl))))
3235 multi-method method user host
3236 (format (concat "while read d; "
3237 "do if test -x $d/%s -a -f $d/%s; "
3238 "then echo tramp_executable $d/%s; "
3239 "break; fi; done <<'EOF'")
3240 progname progname progname))
3242 (tramp-send-command multi-method method user host d))
3244 (tramp-send-command multi-method method user host "EOF")
3245 (tramp-wait-for-output)
3246 (goto-char (point-max))
3247 (when (search-backward "tramp_executable " nil t)
3248 (skip-chars-forward "^ ")
3249 (skip-chars-forward " ")
3250 (buffer-substring (point) (tramp-line-end-position)))))
3252 (defun tramp-set-remote-path (multi-method method user host var dirlist)
3253 "Sets the remote environment VAR to existing directories from DIRLIST.
3254 I.e., for each directory in DIRLIST, it is tested whether it exists and if
3255 so, it is added to the environment variable VAR."
3256 (let ((existing-dirs
3261 (tramp-make-tramp-file-name multi-method method user host x))
3263 (tramp-make-tramp-file-name multi-method method user host x)))
3267 multi-method method user host
3269 (mapconcat 'identity (delq nil existing-dirs) ":")
3271 (tramp-wait-for-output)))
3273 ;; -- communication with external shell --
3275 (defun tramp-find-file-exists-command (multi-method method user host)
3276 "Find a command on the remote host for checking if a file exists.
3277 Here, we are looking for a command which has zero exit status if the
3278 file exists and nonzero exit status otherwise."
3279 (make-local-variable 'tramp-file-exists-command)
3280 (tramp-message 10 "Finding command to check if file exists")
3282 (tramp-make-tramp-file-name
3283 multi-method method user host
3284 "/")) ;assume this file always exists
3286 (tramp-make-tramp-file-name
3287 multi-method method user host
3288 "/ this file does not exist "))) ;assume this never exists
3289 ;; The algorithm is as follows: we try a list of several commands.
3290 ;; For each command, we first run `$cmd /' -- this should return
3291 ;; true, as the root directory always exists. And then we run
3292 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
3293 ;; does not exist. This should return false. We use the first
3294 ;; command we find that seems to work.
3295 ;; The list of commands to try is as follows:
3296 ;; `ls -d' This works on most systems, but NetBSD 1.4
3297 ;; has a bug: `ls' always returns zero exit
3298 ;; status, even for files which don't exist.
3299 ;; `test -e' Some Bourne shells have a `test' builtin
3300 ;; which does not know the `-e' option.
3301 ;; `/bin/test -e' For those, the `test' binary on disk normally
3302 ;; provides the option. Alas, the binary
3303 ;; is sometimes `/bin/test' and sometimes it's
3305 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3307 (and (setq tramp-file-exists-command "ls -d %s")
3308 (tramp-handle-file-exists-p existing)
3309 (not (tramp-handle-file-exists-p nonexisting)))
3310 (and (setq tramp-file-exists-command "test -e %s")
3311 (tramp-handle-file-exists-p existing)
3312 (not (tramp-handle-file-exists-p nonexisting)))
3313 (and (setq tramp-file-exists-command "/bin/test -e %s")
3314 (tramp-handle-file-exists-p existing)
3315 (not (tramp-handle-file-exists-p nonexisting)))
3316 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
3317 (tramp-handle-file-exists-p existing)
3318 (not (tramp-handle-file-exists-p nonexisting))))
3319 (error "Couldn't find command to check if file exists."))))
3322 ;; CCC test ksh or bash found for tilde expansion?
3323 (defun tramp-find-shell (multi-method method user host)
3324 "Find a shell on the remote host which groks tilde expansion."
3326 (tramp-send-command multi-method method user host "echo ~root")
3327 (tramp-wait-for-output)
3329 ((string-match "^~root$" (buffer-string))
3331 (or (tramp-find-executable multi-method method user host
3332 "bash" tramp-remote-path t)
3333 (tramp-find-executable multi-method method user host
3334 "ksh" tramp-remote-path t)))
3336 (error "Couldn't find a shell which groks tilde expansion"))
3337 ;; Find arguments for this shell.
3338 (let ((alist tramp-sh-extra-args)
3340 (while (and alist (null extra-args))
3341 (setq item (pop alist))
3342 (when (string-match (car item) shell)
3343 (setq extra-args (cdr item))))
3344 (when extra-args (setq shell (concat shell " " extra-args))))
3346 5 "Starting remote shell `%s' for tilde expansion..." shell)
3348 multi-method method user host
3349 (concat "PS1='$ ' ; exec " shell)) ;
3350 (unless (tramp-wait-for-regexp
3351 (get-buffer-process (current-buffer))
3352 60 (format "\\(\\$ *\\|\\(%s\\)\\'\\)" shell-prompt-pattern))
3353 (pop-to-buffer (buffer-name))
3354 (error "Couldn't find remote `%s' prompt." shell))
3355 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
3356 tramp-rsh-end-of-line
3358 tramp-rsh-end-of-line
3359 tramp-rsh-end-of-line))
3360 (tramp-wait-for-output)
3361 (tramp-send-command multi-method method user host "echo hello")
3362 (tramp-message 5 "Waiting for remote `%s' to start up..." shell)
3363 (unless (tramp-wait-for-output 5)
3364 (unless (tramp-wait-for-output 5)
3365 (pop-to-buffer (buffer-name))
3366 (error "Couldn't start remote `%s', see buffer `%s' for details"
3367 shell (buffer-name))))
3368 (tramp-message 5 "Waiting for remote `%s' to start up...done" shell))
3369 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
3370 (tramp-get-remote-sh multi-method method))))))
3372 (defun tramp-check-ls-command (multi-method method user host cmd)
3373 "Checks whether the given `ls' executable groks `-n'.
3374 METHOD, USER and HOST specify the connection, CMD (the full path name of)
3375 the `ls' executable. Returns t if CMD supports the `-n' option, nil
3377 (tramp-message 9 "Checking remote `%s' command for `-n' option"
3379 (when (tramp-handle-file-executable-p
3380 (tramp-make-tramp-file-name multi-method method user host cmd))
3382 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
3384 (tramp-send-command-and-check
3385 multi-method method user host
3386 (format "%s -lnd / >/dev/null"
3388 (tramp-message 7 "Testing remote command `%s' for -n...%s"
3390 (if (zerop result) "okay" "failed"))
3393 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
3394 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
3395 Returns nil if none was found, else the command is returned."
3398 ;; It would be better to use the CL function `find', but
3399 ;; we don't want run-time dependencies on CL.
3400 (while (and dl (not result))
3401 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
3402 (when (tramp-check-ls-command multi-method method user host x)
3407 (defun tramp-find-ls-command (multi-method method user host)
3408 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
3409 \(This option prints numeric user and group ids in a long listing.)"
3410 (tramp-message 9 "Finding a suitable `ls' command")
3412 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
3413 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
3414 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
3416 ;; ------------------------------------------------------------
3417 ;; -- Functions for establishing connection --
3418 ;; ------------------------------------------------------------
3420 (defun tramp-process-actions
3421 (multi-method method user host actions &optional timeout)
3422 "Process given ACTIONS for login specified via first four args.
3423 ACTIONS is a list of items (REGEXP FUN), where REGEXP specifies what
3424 output from the remote end to look for, and FUN specifies the action
3425 to take when the regexp matches."
3428 (defun tramp-open-connection-telnet (multi-method method user host)
3429 "Open a connection using a telnet METHOD.
3430 This starts the command `telnet HOST ARGS'[*], then waits for a remote
3431 login prompt, then sends the user name USER, then waits for a remote
3432 password prompt. It queries the user for the password, then sends the
3433 password to the remote host.
3435 If USER is nil, uses value returned by `(user-login-name)' instead.
3437 Recognition of the remote shell prompt is based on the variable
3438 `shell-prompt-pattern' which must be set up correctly.
3440 Please note that it is NOT possible to use this connection method
3441 together with an out-of-band transfer method! You must use an inline
3444 Maybe the different regular expressions need to be tuned.
3446 * Actually, the telnet program as well as the args to be used can be
3447 specified in the method parameters, see the variable `tramp-methods'."
3449 (when (tramp-method-out-of-band-p multi-method method)
3450 (error "Cannot use out-of-band method `%s' with telnet connection method"
3453 (error "Cannot multi-connect using telnet connection method"))
3454 (tramp-pre-connection multi-method method user host)
3455 (tramp-message 7 "Opening connection for %s@%s using %s..."
3456 (or user (user-login-name)) host method)
3457 (let ((process-environment (copy-sequence process-environment)))
3458 (setenv "TERM" tramp-terminal-type)
3459 (let* ((default-directory (tramp-temporary-file-directory))
3460 (coding-system-for-read (unless (and (not (featurep 'xemacs))
3461 (> emacs-major-version 20))
3462 tramp-dos-coding-system))
3463 (p (apply 'start-process
3464 (tramp-buffer-name multi-method method user host)
3465 (tramp-get-buffer multi-method method user host)
3466 (tramp-get-telnet-program multi-method method)
3468 (tramp-get-telnet-args multi-method method)))
3471 (process-kill-without-query p)
3472 (tramp-message 9 "Waiting for login prompt...")
3473 (unless (tramp-wait-for-regexp p nil tramp-login-prompt-regexp)
3474 (pop-to-buffer (buffer-name))
3476 (error "Couldn't find remote login prompt"))
3478 ;; Remote login defaults to local one.
3479 (tramp-message 9 "Sending login name %s" (or user (user-login-name)))
3480 (process-send-string p (concat (or user (user-login-name))
3481 tramp-rsh-end-of-line))
3482 (tramp-message 9 "Waiting for password prompt...")
3483 (unless (setq found (tramp-wait-for-regexp
3484 p nil tramp-password-prompt-regexp))
3485 (pop-to-buffer (buffer-name))
3487 (error "Couldn't find remote password prompt"))
3489 (setq pw (tramp-read-passwd (car found)))
3490 (tramp-message 9 "Sending password")
3491 (process-send-string p (concat pw tramp-rsh-end-of-line))
3492 (tramp-message 9 "Waiting 30s for remote shell to come up...")
3494 (tramp-wait-for-regexp
3495 p 30 (format "\\(%s\\)\\|\\(%s\\)\\'"
3496 tramp-wrong-passwd-regexp
3497 shell-prompt-pattern)))
3498 (pop-to-buffer (buffer-name))
3500 (error "Couldn't find remote shell prompt"))
3502 (pop-to-buffer (buffer-name))
3504 (error "Login failed: %s" (nth 1 found)))
3505 (tramp-open-connection-setup-interactive-shell
3506 p multi-method method user host)
3507 (tramp-post-connection multi-method method user host)))))
3509 ;; HHH: Changed to handle the case when USER is nil.
3510 (defun tramp-open-connection-rsh (multi-method method user host)
3511 "Open a connection using an rsh METHOD.
3512 This starts the command `rsh HOST -l USER'[*], then waits for a remote
3513 password or shell prompt. If a password prompt is seen, the user is
3514 queried for a password, this function sends the password to the remote
3515 host and waits for a shell prompt.
3517 If USER is nil, start the command `rsh HOST'[*] instead
3519 Recognition of the remote shell prompt is based on the variable
3520 `shell-prompt-pattern' which must be set up correctly.
3522 Please note that it is NOT possible to use this connection method with
3523 an out-of-band transfer method if this function asks the user for a
3524 password! You must use an inline transfer method in this case.
3525 Sadly, the transfer method cannot be switched on the fly, instead you
3526 must specify the right method in the file name.
3528 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
3529 be a port number for ssh, and \"-p yy\" will be added to the list of
3530 arguments, and xx will be used as the host name to connect to.
3532 * Actually, the rsh program to be used can be specified in the
3533 method parameters, see the variable `tramp-methods'."
3536 (error "Cannot multi-connect using rsh connection method"))
3537 (tramp-pre-connection multi-method method user host)
3539 (tramp-message 7 "Opening connection for %s@%s using %s..."
3541 (tramp-message 7 "Opening connection at %s using %s..." host method))
3542 (let ((process-environment (copy-sequence process-environment))
3543 (bufnam (tramp-buffer-name multi-method method user host))
3544 (buf (tramp-get-buffer multi-method method user host))
3545 (rsh-program (tramp-get-rsh-program multi-method method))
3546 (rsh-args (tramp-get-rsh-args multi-method method)))
3547 ;; The following should be changed. We need a more general
3548 ;; mechanism to parse extra host args.
3549 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
3550 (setq rsh-args (cons "-p" (cons (match-string 2 host) rsh-args)))
3551 (setq host (match-string 1 host)))
3552 (setenv "TERM" tramp-terminal-type)
3553 (let* ((default-directory (tramp-temporary-file-directory))
3554 (coding-system-for-read (unless (and (not (featurep 'xemacs))
3555 (> emacs-major-version 20))
3556 tramp-dos-coding-system))
3558 (apply #'start-process bufnam buf rsh-program
3559 host "-l" user rsh-args)
3560 (apply #'start-process bufnam buf rsh-program
3563 (process-kill-without-query p)
3564 (tramp-message 9 "Waiting 60s for shell or passwd prompt from %s" host)
3566 (tramp-wait-for-regexp
3569 "\\(%s\\)\\|\\(%s\\)\\'"
3570 tramp-password-prompt-regexp
3571 shell-prompt-pattern)))
3573 (pop-to-buffer (buffer-name))
3575 (error "Couldn't find remote shell or passwd prompt"))
3577 (when (tramp-method-out-of-band-p multi-method method)
3578 (pop-to-buffer (buffer-name))
3580 (error (concat "Out of band method `%s' not applicable"
3581 " for remote shell asking for a password")
3584 (tramp-message 9 "Sending password...")
3585 (tramp-enter-password p (nth 1 found))
3586 (tramp-message 9 "Sent password, waiting 60s for remote shell prompt")
3587 (setq found (tramp-wait-for-regexp p 60
3588 (format "\\(%s\\)\\|\\(%s\\)\\'"
3589 tramp-wrong-passwd-regexp
3590 shell-prompt-pattern))))
3592 (pop-to-buffer (buffer-name))
3594 (error "Couldn't find remote shell prompt"))
3596 (pop-to-buffer (buffer-name))
3598 (error "Login failed: %s" (nth 1 found)))
3599 (tramp-message 7 "Initializing remote shell")
3600 (tramp-open-connection-setup-interactive-shell
3601 p multi-method method user host)
3602 (tramp-post-connection multi-method method user host)))))
3604 ;; HHH: Changed. Now utilizes (or user (user-login-name)) instead of USER.
3605 (defun tramp-open-connection-su (multi-method method user host)
3606 "Open a connection using the `su' program with METHOD.
3607 This starts `su - USER', then waits for a password prompt. The HOST
3608 name must be equal to the local host name or to `localhost'.
3610 If USER is nil, uses value returned by user-login-name instead.
3612 Recognition of the remote shell prompt is based on the variable
3613 `shell-prompt-pattern' which must be set up correctly. Note that the
3614 other user may have a different shell prompt than you do, so it is not
3615 at all unlikely that this variable is set up wrongly!"
3617 (when (tramp-method-out-of-band-p multi-method method)
3618 (error "Cannot use out-of-band method `%s' with `su' connection method"
3620 (unless (or (string-match (concat "^" (regexp-quote host))
3622 (string= "localhost" host))
3624 "Cannot connect to different host `%s' with `su' connection method"
3627 (error "Must give user name for `su' connection method"))
3628 (tramp-pre-connection multi-method method user host)
3629 (tramp-message 7 "Opening connection for `%s' using `%s'..."
3630 (or user (user-login-name)) method)
3631 (let ((process-environment (copy-sequence process-environment)))
3632 (setenv "TERM" tramp-terminal-type)
3633 (let* ((default-directory (tramp-temporary-file-directory))
3634 (coding-system-for-read (unless (and (not (featurep 'xemacs))
3635 (> emacs-major-version 20))
3636 tramp-dos-coding-system))
3637 (p (apply 'start-process
3638 (tramp-buffer-name multi-method method
3640 (tramp-get-buffer multi-method method
3642 (tramp-get-su-program multi-method method)
3646 x (list (cons ?u user))))
3647 (tramp-get-su-args multi-method method))))
3650 (process-kill-without-query p)
3651 (tramp-message 9 "Waiting 30s for shell or password prompt...")
3652 (unless (setq found (tramp-wait-for-regexp
3654 (format "\\(%s\\)\\|\\(%s\\)\\'"
3655 tramp-password-prompt-regexp
3656 shell-prompt-pattern)))
3657 (pop-to-buffer (buffer-name))
3659 (error "Couldn't find shell or password prompt"))
3662 (setq pw (tramp-read-passwd (car found)))
3663 (tramp-message 9 "Sending password")
3664 (process-send-string p (concat pw tramp-rsh-end-of-line))
3665 (tramp-message 9 "Waiting 30s for remote shell to come up...")
3667 (tramp-wait-for-regexp
3668 p 30 (format "\\(%s\\)\\|\\(%s\\)\\'"
3669 tramp-wrong-passwd-regexp
3670 shell-prompt-pattern)))
3671 (pop-to-buffer (buffer-name))
3673 (error "Couldn't find remote shell prompt"))
3675 (pop-to-buffer (buffer-name))
3677 (error "`su' failed: %s" (nth 1 found))))
3678 (tramp-open-connection-setup-interactive-shell
3679 p multi-method method user host)
3680 (tramp-post-connection multi-method method
3683 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
3684 ;; handle not giving a user name in the "file name".
3686 ;; This is more difficult than for the single-hop method. In the
3687 ;; multi-hop-method, the desired behaviour should be that the
3688 ;; user must specify names for the telnet hops of which the user
3689 ;; name is different than the "original" name (or different from
3690 ;; the previous hop.
3691 (defun tramp-open-connection-multi (multi-method method user host)
3692 "Open a multi-hop connection using METHOD.
3693 This uses a slightly changed file name syntax. The idea is to say
3694 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
3695 This will use telnet to log in as u1 to h1, then use rsh from there to
3696 log in as u2 to h2."
3698 (unless multi-method
3699 (error "Multi-hop open connection function called on non-multi method"))
3700 (when (tramp-method-out-of-band-p multi-method method)
3701 (error "No out of band multi-hop connections"))
3702 (unless (and (arrayp method) (not (stringp method)))
3703 (error "METHOD must be an array of strings for multi methods"))
3704 (unless (and (arrayp user) (not (stringp user)))
3705 (error "USER must be an array of strings for multi methods"))
3706 (unless (and (arrayp host) (not (stringp host)))
3707 (error "HOST must be an array of strings for multi methods"))
3708 (unless (and (= (length method) (length user))
3709 (= (length method) (length host)))
3710 (error "Arrays METHOD, USER, HOST must have equal length"))
3711 (tramp-pre-connection multi-method method user host)
3712 (tramp-message 7 "Opening `%s' connection..." multi-method)
3713 (let ((process-environment (copy-sequence process-environment)))
3714 (setenv "TERM" tramp-terminal-type)
3715 (let* ((default-directory (tramp-temporary-file-directory))
3716 (coding-system-for-read (unless (and (not (featurep 'xemacs))
3717 (> emacs-major-version 20))
3718 tramp-dos-coding-system))
3719 (p (start-process (tramp-buffer-name multi-method method user host)
3720 (tramp-get-buffer multi-method method user host)
3722 (num-hops (length method))
3724 (process-kill-without-query p)
3725 (tramp-message 9 "Waiting 60s for local shell to come up...")
3726 (unless (tramp-wait-for-regexp
3727 p 60 (format "%s\\'" shell-prompt-pattern))
3728 (pop-to-buffer (buffer-name))
3730 (error "Couldn't find local shell prompt"))
3731 ;; Now do all the connections as specified.
3732 (while (< i num-hops)
3733 (let* ((m (aref method i))
3736 (entry (assoc m tramp-multi-connection-function-alist))
3737 (multi-func (nth 1 entry))
3738 (command (nth 2 entry)))
3739 ;; The multi-funcs don't need to do save-match-data, as that
3741 (funcall multi-func p m u h command)
3744 (tramp-open-connection-setup-interactive-shell
3745 p multi-method method user host)
3746 (tramp-post-connection multi-method method user host)))))
3748 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
3749 ;; of no user name provided. Hack to make it work as it did before:
3750 ;; changed `user' to `(or user (user-login-name))' in the places where
3751 ;; the value is actually used.
3752 (defun tramp-multi-connect-telnet (p method user host command)
3753 "Issue `telnet' command.
3754 Uses shell COMMAND to issue a `telnet' command to log in as USER to
3755 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
3756 the host name, and `%n' is replaced with an end of line character, as
3757 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
3760 If USER is nil, uses the return value of (user-login-name) instead."
3761 (let ((cmd (format-spec command (list (cons ?h host)
3762 (cons ?n tramp-rsh-end-of-line))))
3763 (cmd1 (format-spec command (list (cons ?h host)
3767 (tramp-message 9 "Sending telnet command `%s'" cmd1)
3768 (process-send-string p cmd)
3769 (tramp-message 9 "Waiting 30s for login prompt from %s" host)
3770 (unless (tramp-wait-for-regexp p 30 tramp-login-prompt-regexp)
3771 (pop-to-buffer (buffer-name))
3773 (error "Couldn't find login prompt from host %s" host))
3775 (tramp-message 9 "Sending login name %s" (or user (user-login-name)))
3776 (process-send-string p (concat (or user (user-login-name)) tramp-rsh-end-of-line))
3777 (tramp-message 9 "Waiting for password prompt")
3778 (unless (setq found (tramp-wait-for-regexp p nil tramp-password-prompt-regexp))
3779 (pop-to-buffer (buffer-name))
3781 (error "Couldn't find password prompt from host %s" host))
3783 (setq pw (tramp-read-passwd
3784 (format "Password for %s@%s, %s" (or user (user-login-name)) host found)))
3785 (tramp-message 9 "Sending password")
3786 (process-send-string p (concat pw tramp-rsh-end-of-line))
3787 (tramp-message 9 "Waiting 60s for remote shell to come up...")
3788 (unless (setq found (tramp-wait-for-regexp
3789 p 60 (format "\\(%s\\)\\|\\(%s\\)\\'"
3790 tramp-wrong-passwd-regexp
3791 shell-prompt-pattern)))
3792 (pop-to-buffer (buffer-name))
3794 (error "Couldn't find shell prompt from host %s" host))
3796 (pop-to-buffer (buffer-name))
3798 (error "Login to %s failed: %s" (nth 2 found)))))
3800 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
3801 ;; of no user name provided. Hack to make it work as it did before:
3802 ;; changed `user' to `(or user (user-login-name))' in the places where
3803 ;; the value is actually used.
3804 (defun tramp-multi-connect-rlogin (p method user host command)
3805 "Issue `rlogin' command.
3806 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
3807 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
3808 with the user name, `%h' will be replaced with the host name, and `%n'
3809 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
3810 `%%' if you want to use a literal percent character.
3812 If USER is nil, uses the return value of (user-login-name) instead."
3813 (let ((cmd (format-spec command (list (cons ?h host)
3814 (cons ?u (or user (user-login-name)))
3815 (cons ?n tramp-rsh-end-of-line))))
3816 (cmd1 (format-spec command (list (cons ?h host)
3817 (cons ?u (or user (user-login-name)))
3821 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
3822 (process-send-string p cmd)
3823 (tramp-message 9 "Waiting 60s for shell or passwd prompt from %s" host)
3825 (tramp-wait-for-regexp p 60
3826 (format "\\(%s\\)\\|\\(%s\\)\\'"
3827 tramp-password-prompt-regexp
3828 shell-prompt-pattern)))
3829 (pop-to-buffer (buffer-name))
3831 (error "Couldn't find remote shell or passwd prompt"))
3834 (tramp-message 9 "Sending password...")
3835 (tramp-enter-password p (nth 1 found))
3836 (tramp-message 9 "Sent password, waiting 60s for remote shell prompt")
3837 (setq found (tramp-wait-for-regexp p 60
3838 (format "\\(%s\\)\\|\\(%s\\)\\'"
3839 tramp-wrong-passwd-regexp
3840 shell-prompt-pattern))))
3842 (pop-to-buffer (buffer-name))
3844 (error "Couldn't find remote shell prompt"))
3846 (pop-to-buffer (buffer-name))
3848 (error "Login failed: %s" (nth 1 found)))))
3850 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
3851 ;; of no user name provided. Hack to make it work as it did before:
3852 ;; changed `user' to `(or user (user-login-name))' in the places where
3853 ;; the value is actually used.
3854 (defun tramp-multi-connect-su (p method user host command)
3855 "Issue `su' command.
3856 Uses shell COMMAND to issue a `su' command to log in as USER on
3857 HOST. The HOST name is ignored, this just changes the user id on the
3858 host currently logged in to.
3860 If USER is nil, uses the return value of (user-login-name) instead.
3862 You can use percent escapes in the COMMAND. `%u' is replaced with the
3863 user name, and `%n' is replaced with the value of
3864 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
3866 (let ((cmd (format-spec command (list (cons ?u (or user (user-login-name)))
3867 (cons ?n tramp-rsh-end-of-line))))
3868 (cmd1 (format-spec command (list (cons ?u (or user (user-login-name)))
3872 (tramp-message 9 "Sending su command `%s'" cmd1)
3873 (process-send-string p cmd)
3874 (tramp-message 9 "Waiting 60s for shell or passwd prompt for %s" (or user (user-login-name)))
3875 (unless (setq found (tramp-wait-for-regexp
3876 p 60 (format "\\(%s\\)\\|\\(%s\\)\\'"
3877 tramp-password-prompt-regexp
3878 shell-prompt-pattern)))
3879 (pop-to-buffer (buffer-name))
3881 (error "Couldn't find shell or passwd prompt for %s"
3882 (or user (user-login-name))))
3884 (tramp-message 9 "Sending password...")
3885 (tramp-enter-password p (nth 1 found))
3887 (tramp-message 9 "Sent password, waiting 60s for remote shell prompt")
3888 (setq found (tramp-wait-for-regexp p 60
3889 (format "\\(%s\\)\\|\\(%s\\)\\'"
3890 tramp-wrong-passwd-regexp
3891 shell-prompt-pattern))))
3893 (pop-to-buffer (buffer-name))
3895 (error "Couldn't find remote shell prompt"))
3897 (pop-to-buffer (buffer-name))
3899 (error "Login failed: %s" (nth 1 found)))))
3901 ;; Utility functions.
3903 (defun tramp-wait-for-regexp (proc timeout regexp)
3904 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3905 Expects the output of PROC to be sent to the current buffer. Returns
3906 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3909 (start-time (current-time)))
3911 ;; Work around a bug in XEmacs 21, where the timeout
3912 ;; expires faster than it should. This degenerates
3913 ;; to polling for buggy XEmacsen, but oh, well.
3914 (while (and (not found)
3915 (< (tramp-time-diff (current-time) start-time)
3917 (with-timeout (timeout)
3919 (accept-process-output proc 1)
3920 (goto-char (point-min))
3921 (setq found (when (re-search-forward regexp nil t)
3922 (tramp-match-string-list)))))))
3925 (accept-process-output proc 1)
3926 (goto-char (point-min))
3927 (setq found (when (re-search-forward regexp nil t)
3928 (tramp-match-string-list))))))
3929 (when tramp-debug-buffer
3931 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
3932 tramp-current-user tramp-current-host)
3933 (point-min) (point-max))
3937 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
3938 tramp-current-user tramp-current-host))
3939 (goto-char (point-max))
3940 (insert "[[Regexp `" regexp "' not found"
3941 (if timeout (format " in %d secs" timeout) "")
3945 (defun tramp-enter-password (p prompt)
3946 "Prompt for a password and send it to the remote end.
3947 Uses PROMPT as a prompt and sends the password to process P."
3948 (let ((pw (tramp-read-passwd prompt)))
3949 (process-send-string p (concat pw tramp-rsh-end-of-line))))
3951 ;; HHH: Not Changed. This might handle the case where USER is not
3952 ;; given in the "File name" very poorly. Then, the local
3953 ;; variable tramp-current user will be set to nil.
3954 (defun tramp-pre-connection (multi-method method user host)
3955 "Do some setup before actually logging in.
3956 METHOD, USER and HOST specify the connection."
3957 (set-buffer (tramp-get-buffer multi-method method user host))
3958 (set (make-local-variable 'tramp-current-multi-method) multi-method)
3959 (set (make-local-variable 'tramp-current-method) method)
3960 (set (make-local-variable 'tramp-current-user) user)
3961 (set (make-local-variable 'tramp-current-host) host)
3962 (set (make-local-variable 'inhibit-eol-conversion) nil)
3965 (defun tramp-open-connection-setup-interactive-shell
3966 (p multi-method method user host)
3967 "Set up an interactive shell.
3968 Mainly sets the prompt and the echo correctly. P is the shell process
3969 to set up. METHOD, USER and HOST specify the connection."
3970 ;; Wait a bit in case the remote end feels like sending a little
3971 ;; junk first. It seems that fencepost.gnu.org does this when doing
3972 ;; a Kerberos login.
3974 (tramp-discard-garbage-erase-buffer p multi-method method user host)
3975 (process-send-string nil (format "exec %s%s"
3976 (tramp-get-remote-sh multi-method method)
3977 tramp-rsh-end-of-line))
3978 (when tramp-debug-buffer
3980 (set-buffer (tramp-get-debug-buffer multi-method method user host))
3981 (goto-char (point-max))
3982 (tramp-insert-with-face
3983 'bold (format "$ exec %s\n" (tramp-get-remote-sh multi-method method)))))
3984 (tramp-message 9 "Waiting 30s for remote `%s' to come up..."
3985 (tramp-get-remote-sh multi-method method))
3986 (unless (tramp-wait-for-regexp
3987 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
3988 (pop-to-buffer (buffer-name))
3989 (error "Remote `%s' didn't come up. See buffer `%s' for details"
3990 (tramp-get-remote-sh multi-method method) (buffer-name)))
3991 (tramp-message 9 "Setting up remote shell environment")
3992 (tramp-discard-garbage-erase-buffer p multi-method method user host)
3993 (process-send-string
3994 nil (format "stty -inlcr -echo kill '^U'%s" tramp-rsh-end-of-line))
3995 (unless (tramp-wait-for-regexp
3996 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
3997 (pop-to-buffer (buffer-name))
3998 (error "Couldn't `stty -echo', see buffer `%s'" (buffer-name)))
4000 (process-send-string nil (format "TERM=dumb; export TERM%s"
4001 tramp-rsh-end-of-line))
4002 (unless (tramp-wait-for-regexp
4003 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4004 (pop-to-buffer (buffer-name))
4005 (error "Couldn't `TERM=dumb; export TERM', see buffer `%s'" (buffer-name)))
4006 ;; Try to set up the coding system correctly.
4007 ;; CCC this can't be the right way to do it. Hm.
4010 (tramp-message 9 "Determining coding system")
4011 (process-send-string nil (format "echo foo ; echo bar %s"
4012 tramp-rsh-end-of-line))
4013 (unless (tramp-wait-for-regexp
4014 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4015 (pop-to-buffer (buffer-name))
4016 (error "Couldn't `echo foo; echo bar' to determine line endings'"))
4017 (goto-char (point-min))
4018 (if (featurep 'mule)
4019 ;; Use MULE to select the right EOL convention for communicating
4020 ;; with the process.
4021 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
4022 cs-decode cs-encode)
4023 (when (symbolp cs) (setq cs (cons cs cs)))
4024 (setq cs-decode (car cs))
4025 (setq cs-encode (cdr cs))
4026 (unless cs-decode (setq cs-decode 'undecided))
4027 (unless cs-encode (setq cs-encode 'undecided))
4028 (setq cs-encode (tramp-coding-system-change-eol-conversion
4030 (when (search-forward "\r" nil t)
4031 (setq cs-decode (tramp-coding-system-change-eol-conversion
4033 (set-buffer-process-coding-system cs-decode cs-encode))
4034 ;; Look for ^M and do something useful if found.
4035 (when (search-forward "\r" nil t)
4036 ;; We have found a ^M but cannot frob the process coding system
4037 ;; because we're running on a non-MULE Emacs. Let's try
4039 (tramp-message 9 "Trying `stty -onlcr'")
4040 (process-send-string nil (format "stty -onlcr%s" tramp-rsh-end-of-line))
4041 (unless (tramp-wait-for-regexp
4042 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4043 (pop-to-buffer (buffer-name))
4044 (error "Couldn't `stty -onlcr', see buffer `%s'" (buffer-name))))))
4047 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1'")
4048 (process-send-string
4049 nil (format "HISTFILE=$HOME/.tramp_history; HISTSIZE=1%s"
4050 tramp-rsh-end-of-line))
4051 (unless (tramp-wait-for-regexp
4052 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4053 (pop-to-buffer (buffer-name))
4054 (error (concat "Couldn't `HISTFILE=$HOME/.tramp_history; "
4055 "HISTSIZE=1', see buffer `%s'")
4058 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
4059 (process-send-string
4060 nil (format "set +o vi +o emacs%s" ;mustn't `>/dev/null' with AIX?
4061 tramp-rsh-end-of-line))
4062 (unless (tramp-wait-for-regexp
4063 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4064 (pop-to-buffer (buffer-name))
4065 (error "Couldn't `set +o vi +o emacs', see buffer `%s'"
4068 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
4069 (process-send-string
4070 nil (format "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null%s"
4071 tramp-rsh-end-of-line))
4072 (unless (tramp-wait-for-regexp
4073 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4074 (pop-to-buffer (buffer-name))
4075 (error "Couldn't `unset MAIL MAILCHECK MAILPATH', see buffer `%s'"
4078 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
4079 (process-send-string
4080 nil (format "unset CDPATH%s" tramp-rsh-end-of-line))
4081 (unless (tramp-wait-for-regexp
4082 p 30 (format "\\(\\$ *\\|%s\\)\\'" shell-prompt-pattern))
4083 (pop-to-buffer (buffer-name))
4084 (error "Couldn't `unset CDPATH', see buffer `%s'"
4087 (tramp-message 9 "Setting shell prompt")
4089 multi-method method user host
4090 (format "PS1='%s%s%s'; PS2=''; PS3=''"
4091 tramp-rsh-end-of-line
4093 tramp-rsh-end-of-line))
4094 (tramp-wait-for-output)
4095 (tramp-send-command multi-method method user host "echo hello")
4096 (tramp-message 9 "Waiting for remote `%s' to come up..."
4097 (tramp-get-remote-sh multi-method method))
4098 (unless (tramp-wait-for-output 5)
4099 (unless (tramp-wait-for-output 5)
4100 (pop-to-buffer (buffer-name))
4101 (error "Couldn't set remote shell prompt. See buffer `%s' for details"
4103 (tramp-message 7 "Waiting for remote `%s' to come up...done"
4104 (tramp-get-remote-sh multi-method method)))
4106 (defun tramp-post-connection (multi-method method user host)
4107 "Prepare a remote shell before being able to work on it.
4108 METHOD, USER and HOST specify the connection.
4109 Among other things, this finds a shell which groks tilde expansion,
4110 tries to find an `ls' command which groks the `-n' option, sets the
4111 locale to C and sets up the remote shell search path."
4112 ;; Search for a good shell before searching for a command which
4113 ;; checks if a file exists. This is done because Tramp wants to use
4114 ;; "test foo; echo $?" to check if various conditions hold, and
4115 ;; there are buggy /bin/sh implementations which don't execute the
4116 ;; "echo $?" part if the "test" part has an error. In particular,
4117 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
4118 ;; with buggy /bin/sh implementations will have a working bash or
4120 (tramp-find-shell multi-method method user host)
4121 (tramp-find-file-exists-command multi-method method user host)
4123 ;; Without (sit-for 0.1) at least, my machine will almost always blow
4124 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
4125 ;; output of (tramp-find-shell) to show up along with the output of
4126 ;; (tramp-find-ls-command) testing.
4128 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
4129 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
4131 ;; After much debugging I couldn't find any problem with the implementation
4132 ;; of that function though. The workaround stays for me at least. :/
4134 ;; Daniel Pittman <daniel@danann.net>
4135 (make-local-variable 'tramp-ls-command)
4136 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
4137 (unless tramp-ls-command
4140 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
4141 (setq tramp-ls-command
4142 (tramp-find-executable multi-method method user host
4143 "ls" tramp-remote-path nil)))
4144 (unless tramp-ls-command
4145 (error "Fatal error: Couldn't find remote executable `ls'"))
4146 (tramp-message 5 "Using remote command `%s' for getting directory listings"
4148 (tramp-send-command multi-method method user host
4149 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
4150 "return $1" tramp-rsh-end-of-line
4152 (tramp-wait-for-output)
4153 ;; Set remote PATH variable.
4154 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
4155 ;; Tell remote shell to use standard time format, needed for
4156 ;; parsing `ls -l' output.
4157 (tramp-send-command multi-method method user host
4158 "LC_TIME=C; export LC_TIME; echo huhu")
4159 (tramp-wait-for-output)
4160 (tramp-send-command multi-method method user host
4161 "mesg n; echo huhu")
4162 (tramp-wait-for-output)
4163 (tramp-send-command multi-method method user host
4164 "biff n ; echo huhu")
4165 (tramp-wait-for-output)
4166 ;; Unalias ls(1) to work around issues with those silly people who make it
4167 ;; spit out ANSI escapes or whatever.
4168 (tramp-send-command multi-method method user host
4169 "unalias ls; echo huhu")
4170 (tramp-wait-for-output)
4171 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4172 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4173 ;; for otherwise the shell crashes.
4175 (make-local-variable 'tramp-test-groks-nt)
4176 (tramp-send-command multi-method method user host
4178 (tramp-wait-for-output)
4179 (goto-char (point-min))
4180 (setq tramp-test-groks-nt
4181 (looking-at (format "\n%s\n" (regexp-quote tramp-end-of-output))))
4182 (unless tramp-test-groks-nt
4184 multi-method method user host
4185 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
4186 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
4188 (tramp-wait-for-output)
4191 (let ((tramp-remote-perl
4192 (or (tramp-find-executable multi-method method user host
4193 "perl5" tramp-remote-path nil)
4194 (tramp-find-executable multi-method method user host
4195 "perl" tramp-remote-path nil))))
4196 (when tramp-remote-perl
4197 (tramp-set-connection-property "perl" tramp-remote-perl multi-method method user host)
4198 ;; Set up stat in Perl if we can.
4199 (when tramp-remote-perl
4200 (tramp-message 5 "Sending the Perl `file-attributes' implementation.")
4201 (tramp-send-linewise
4202 multi-method method user host
4203 (concat "tramp_file_attributes () {\n"
4205 " -e '" tramp-perl-file-attributes "' $1 2>/dev/null\n"
4207 (tramp-wait-for-output)
4208 (when (string= (tramp-get-encoding-command multi-method method)
4210 (tramp-message 5 "Sending the Perl `mime-encode' implementation.")
4211 (tramp-send-linewise
4212 multi-method method user host
4213 (concat "tramp_mimencode () {\n"
4214 (if (tramp-find-executable multi-method method user host
4215 "mimencode" tramp-remote-path t)
4217 (concat tramp-remote-perl
4218 " -e '" tramp-perl-mime-encode "' $1 2>/dev/null"))
4220 (tramp-wait-for-output))
4221 (when (string= (tramp-get-decoding-command multi-method method)
4223 (tramp-message 5 "Sending the Perl `mime-decode' implementation.")
4224 (tramp-send-linewise
4225 multi-method method user host
4226 (concat "tramp_mimedecode () {\n"
4227 (if (tramp-find-executable multi-method method user host
4228 "mimencode" tramp-remote-path t)
4229 "mimencode -u -b $1"
4230 (concat tramp-remote-perl
4231 " -e '" tramp-perl-mime-decode "' $1 2>/dev/null"))
4233 (tramp-wait-for-output)))))
4236 (let ((ln (tramp-find-executable multi-method method user host
4237 "ln" tramp-remote-path nil)))
4239 (tramp-set-connection-property "ln" ln multi-method method user host)))
4241 ;; If encoding/decoding command are given, test to see if they work.
4242 ;; CCC: Maybe it would be useful to run the encoder both locally and
4243 ;; remotely to see if they produce the same result.
4244 (let ((decoding (tramp-get-decoding-command multi-method method))
4245 (encoding (tramp-get-encoding-command multi-method method))
4246 (magic-string "xyzzy"))
4247 (when (and (or decoding encoding) (not (and decoding encoding)))
4248 (tramp-kill-process multi-method method user host)
4250 "Must give both decoding and encoding command in method definition"))
4251 (when (and decoding encoding)
4254 "Checking to see if encoding/decoding commands work on remote host...")
4256 multi-method method user host
4257 (format "echo %s | %s | %s"
4258 (tramp-shell-quote-argument magic-string) encoding decoding))
4259 (tramp-wait-for-output)
4260 (unless (looking-at (regexp-quote magic-string))
4261 (tramp-kill-process multi-method method user host)
4262 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
4266 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
4269 (defun tramp-maybe-open-connection (multi-method method user host)
4270 "Maybe open a connection to HOST, logging in as USER, using METHOD.
4271 Does not do anything if a connection is already open, but re-opens the
4272 connection if a previous connection has died for some reason."
4273 (let ((p (get-buffer-process (tramp-get-buffer multi-method method user host))))
4276 (memq (process-status p) '(run open)))
4277 (when (and p (processp p))
4279 (funcall (tramp-get-connection-function multi-method method)
4280 multi-method method user host))))
4282 (defun tramp-send-command
4283 (multi-method method user host command &optional noerase)
4284 "Send the COMMAND to USER at HOST (logged in using METHOD).
4285 Erases temporary buffer before sending the command (unless NOERASE
4287 (tramp-maybe-open-connection multi-method method user host)
4288 (when tramp-debug-buffer
4290 (set-buffer (tramp-get-debug-buffer multi-method method user host))
4291 (goto-char (point-max))
4292 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
4294 (set-buffer (tramp-get-buffer multi-method method user host))
4295 (unless noerase (erase-buffer))
4296 (setq proc (get-buffer-process (current-buffer)))
4297 (process-send-string proc
4298 (concat command tramp-rsh-end-of-line))))
4300 ;; It seems that Tru64 Unix does not like it if long strings are sent
4301 ;; to it in one go. (This happens when sending the Perl
4302 ;; `file-attributes' implementation, for instance.) Therefore, we
4303 ;; have this function which waits a bit at each line.
4304 (defun tramp-send-linewise
4305 (multi-method method user host string &optional noerase)
4306 "Send the STRING to USER at HOST linewise.
4307 Erases temporary buffer before sending the STRING (unless NOERASE
4310 The STRING is expected to use Unix line-endings, but the lines sent to
4311 the remote host use line-endings as defined in the variable
4312 `tramp-rsh-end-of-line'."
4313 (tramp-maybe-open-connection multi-method method user host)
4314 (when tramp-debug-buffer
4316 (set-buffer (tramp-get-debug-buffer multi-method method user host))
4317 (goto-char (point-max))
4318 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
4320 (lines (split-string string "\n")))
4321 (set-buffer (tramp-get-buffer multi-method method user host))
4322 (unless noerase (erase-buffer))
4323 (setq proc (get-buffer-process (current-buffer)))
4326 (process-send-string proc
4327 (concat x tramp-rsh-end-of-line)))
4330 (defun tramp-wait-for-output (&optional timeout)
4331 "Wait for output from remote rsh command."
4332 (let ((proc (get-buffer-process (current-buffer)))
4334 (start-time (current-time))
4335 (end-of-output (concat "^"
4336 (regexp-quote tramp-end-of-output)
4338 ;; Algorithm: get waiting output. See if last line contains
4339 ;; end-of-output sentinel. If not, wait a bit and again get
4340 ;; waiting output. Repeat until timeout expires or end-of-output
4341 ;; sentinel is seen. Will hang if timeout is nil and
4342 ;; end-of-output sentinel never appears.
4345 ;; Work around an XEmacs bug, where the timeout expires
4346 ;; faster than it should. This degenerates into polling
4347 ;; for buggy XEmacsen, but oh, well.
4348 (while (and (not found)
4349 (< (tramp-time-diff (current-time) start-time)
4351 (with-timeout (timeout)
4353 (accept-process-output proc 1)
4354 (goto-char (point-max))
4356 (setq found (looking-at end-of-output))))))
4359 (accept-process-output proc 1)
4360 (goto-char (point-max))
4362 (setq found (looking-at end-of-output))))))
4363 ;; At this point, either the timeout has expired or we have found
4364 ;; the end-of-output sentinel.
4366 (goto-char (point-max))
4368 (delete-region (point) (point-max)))
4369 ;; Add output to debug buffer if appropriate.
4370 (when tramp-debug-buffer
4372 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
4373 tramp-current-user tramp-current-host)
4374 (point-min) (point-max))
4378 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
4379 tramp-current-user tramp-current-host))
4380 (goto-char (point-max))
4381 (insert "[[Remote prompt `" end-of-output "' not found"
4382 (if timeout (format " in %d secs" timeout) "")
4384 (goto-char (point-min))
4385 ;; Return value is whether end-of-output sentinel was found.
4388 (defun tramp-match-string-list (&optional string)
4389 "Returns list of all match strings.
4390 That is, (list (match-string 0) (match-string 1) ...), according to the
4392 (let* ((nmatches (/ (length (match-data)) 2))
4396 (setq res (cons (match-string i string) res))
4400 (defun tramp-send-command-and-check (multi-method method user host command
4402 "Run COMMAND and check its exit status.
4403 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
4404 Sends `echo $?' along with the COMMAND for checking the exit status. If
4405 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4407 If the optional argument SUBSHELL is non-nil, the command is executed in
4408 a subshell, ie surrounded by parentheses."
4409 (tramp-send-command multi-method method user host
4410 (concat (if subshell "( " "")
4412 (if command " 2>/dev/null; " "")
4413 "echo tramp_exit_status $?"
4414 (if subshell " )" " ")))
4415 (tramp-wait-for-output)
4416 (goto-char (point-max))
4417 (unless (search-backward "tramp_exit_status " nil t)
4418 (error "Couldn't find exit status of `%s'" command))
4419 (skip-chars-forward "^ ")
4420 (read (current-buffer)))
4422 (defun tramp-barf-unless-okay (multi-method method user host command subshell
4423 signal fmt &rest args)
4424 "Run COMMAND, check exit status, throw error if exit status not okay.
4425 Similar to `tramp-send-command-and-check' but accepts two more arguments
4426 FMT and ARGS which are passed to `error'."
4427 (unless (zerop (tramp-send-command-and-check
4428 multi-method method user host command subshell))
4429 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
4431 (pop-to-buffer (current-buffer))
4432 (funcall 'signal signal (apply 'format fmt args))))
4434 (defun tramp-send-region (multi-method method user host start end)
4435 "Send the region from START to END to remote command
4436 running as USER on HOST using METHOD."
4437 (let ((proc (get-buffer-process
4438 (tramp-get-buffer multi-method method user host))))
4440 (error "Can't send region to remote host -- not logged in"))
4441 (process-send-region proc start end)
4442 (when tramp-debug-buffer
4444 (tramp-get-debug-buffer multi-method method user host)
4447 (defun tramp-send-eof (multi-method method user host)
4448 "Send EOF to the remote end.
4449 METHOD, HOST and USER specify the connection."
4450 (let ((proc (get-buffer-process
4451 (tramp-get-buffer multi-method method user host))))
4453 (error "Can't send EOF to remote host -- not logged in"))
4454 (process-send-eof proc)))
4455 ; (process-send-string proc "\^D")))
4457 (defun tramp-kill-process (multi-method method user host)
4458 "Kill the connection process used by Tramp.
4459 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
4460 (let ((proc (get-buffer-process
4461 (tramp-get-buffer multi-method method user host))))
4462 (kill-process proc)))
4464 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
4465 "Erase buffer, then discard subsequent garbage.
4466 If `tramp-discard-garbage' is nil, just erase buffer."
4467 (if (not tramp-discard-garbage)
4469 (while (prog1 (erase-buffer) (accept-process-output p 0.25))
4470 (when tramp-debug-buffer
4472 (set-buffer (tramp-get-debug-buffer multi-method method user host))
4473 (goto-char (point-max))
4474 (tramp-insert-with-face
4475 'bold (format "Additional characters detected\n")))))))
4477 (defun tramp-mode-string-to-int (mode-string)
4478 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
4479 (let* ((mode-chars (string-to-vector mode-string))
4480 (owner-read (aref mode-chars 1))
4481 (owner-write (aref mode-chars 2))
4482 (owner-execute-or-setid (aref mode-chars 3))
4483 (group-read (aref mode-chars 4))
4484 (group-write (aref mode-chars 5))
4485 (group-execute-or-setid (aref mode-chars 6))
4486 (other-read (aref mode-chars 7))
4487 (other-write (aref mode-chars 8))
4488 (other-execute-or-sticky (aref mode-chars 9)))
4492 (?r (tramp-octal-to-decimal "00400")) (?- 0)
4493 (t (error "Second char `%c' must be one of `r-'" owner-read)))
4495 (?w (tramp-octal-to-decimal "00200")) (?- 0)
4496 (t (error "Third char `%c' must be one of `w-'" owner-write)))
4497 (case owner-execute-or-setid
4498 (?x (tramp-octal-to-decimal "00100"))
4499 (?S (tramp-octal-to-decimal "04000"))
4500 (?s (tramp-octal-to-decimal "04100"))
4502 (t (error "Fourth char `%c' must be one of `xsS-'"
4503 owner-execute-or-setid)))
4505 (?r (tramp-octal-to-decimal "00040")) (?- 0)
4506 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
4508 (?w (tramp-octal-to-decimal "00020")) (?- 0)
4509 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
4510 (case group-execute-or-setid
4511 (?x (tramp-octal-to-decimal "00010"))
4512 (?S (tramp-octal-to-decimal "02000"))
4513 (?s (tramp-octal-to-decimal "02010"))
4515 (t (error "Seventh char `%c' must be one of `xsS-'"
4516 group-execute-or-setid)))
4518 (?r (tramp-octal-to-decimal "00004")) (?- 0)
4519 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
4521 (?w (tramp-octal-to-decimal "00002")) (?- 0)
4522 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
4523 (case other-execute-or-sticky
4524 (?x (tramp-octal-to-decimal "00001"))
4525 (?T (tramp-octal-to-decimal "01000"))
4526 (?t (tramp-octal-to-decimal "01001"))
4528 (t (error "Tenth char `%c' must be one of `xtT-'"
4529 other-execute-or-sticky)))))))
4532 (defun tramp-file-mode-from-int (mode)
4533 "Turn an integer representing a file mode into an ls(1)-like string."
4534 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4535 (user (logand (lsh mode -6) 7))
4536 (group (logand (lsh mode -3) 7))
4537 (other (logand (lsh mode -0) 7))
4538 (suid (> (logand (lsh mode -9) 4) 0))
4539 (sgid (> (logand (lsh mode -9) 2) 0))
4540 (sticky (> (logand (lsh mode -9) 1) 0)))
4541 (setq user (tramp-file-mode-permissions user suid "s"))
4542 (setq group (tramp-file-mode-permissions group sgid "s"))
4543 (setq other (tramp-file-mode-permissions other sticky "t"))
4544 (concat type user group other)))
4547 (defun tramp-file-mode-permissions (perm suid suid-text)
4548 "Convert a permission bitset into a string.
4549 This is used internally by `tramp-file-mode-from-int'."
4550 (let ((r (> (logand perm 4) 0))
4551 (w (> (logand perm 2) 0))
4552 (x (> (logand perm 1) 0)))
4553 (concat (or (and r "r") "-")
4554 (or (and w "w") "-")
4555 (or (and suid x suid-text) ; suid, execute
4556 (and suid (upcase suid-text)) ; suid, !execute
4557 (and x "x") "-")))) ; !suid
4560 (defun tramp-decimal-to-octal (i)
4561 "Return a string consisting of the octal digits of I.
4562 Not actually used. Use `(format \"%o\" i)' instead?"
4563 (cond ((< i 0) (error "Cannot convert negative number to octal"))
4564 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
4566 (t (concat (tramp-decimal-to-octal (/ i 8))
4567 (number-to-string (% i 8))))))
4570 ;;(defun tramp-octal-to-decimal (ostr)
4571 ;; "Given a string of octal digits, return a decimal number."
4572 ;; (cond ((null ostr) 0)
4573 ;; ((string= "" ostr) 0)
4574 ;; (t (let ((last (aref ostr (1- (length ostr))))
4575 ;; (rest (substring ostr 0 (1- (length ostr)))))
4576 ;; (unless (and (>= last ?0)
4578 ;; (error "Not an octal digit: %c" last))
4579 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
4580 ;; Kudos to Gerd Moellmann for this suggestion.
4581 (defun tramp-octal-to-decimal (ostr)
4582 "Given a string of octal digits, return a decimal number."
4583 (let ((x (or ostr "")))
4584 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
4585 (unless (string-match "\\`[0-7]*\\'" x)
4586 (error "Non-octal junk in string `%s'" x))
4587 (string-to-number ostr 8)))
4589 (defun tramp-shell-case-fold (string)
4590 "Converts STRING to shell glob pattern which ignores case."
4593 (if (equal (downcase c) (upcase c))
4595 (format "[%c%c]" (downcase c) (upcase c))))
4600 ;; ------------------------------------------------------------
4601 ;; -- TRAMP file names --
4602 ;; ------------------------------------------------------------
4603 ;; Conversion functions between external representation and
4604 ;; internal data structure. Convenience functions for internal
4607 (defstruct tramp-file-name multi-method method user host path)
4609 (defun tramp-tramp-file-p (name)
4610 "Return t iff NAME is a tramp file."
4612 (string-match tramp-file-name-regexp name)))
4614 ;; HHH: Changed. Used to assign the return value of (user-login-name)
4615 ;; to the `user' part of the structure if a user name was not
4616 ;; provided, now it assigns nil.
4617 (defun tramp-dissect-file-name (name)
4618 "Return an `tramp-file-name' structure.
4619 The structure consists of remote method, remote user, remote host and
4623 (unless (string-match (nth 0 tramp-file-name-structure) name)
4624 (error "Not a tramp file name: %s" name))
4625 (setq method (match-string (nth 1 tramp-file-name-structure) name))
4626 (if (and method (member method tramp-multi-methods))
4627 ;; If it's a multi method, the file name structure contains
4628 ;; arrays of method, user and host.
4629 (tramp-dissect-multi-file-name name)
4630 ;; Normal method. First, find out default method.
4631 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
4632 (host (match-string (nth 3 tramp-file-name-structure) name))
4633 (path (match-string (nth 4 tramp-file-name-structure) name)))
4635 (setq method (tramp-find-default-method user host)))
4636 (make-tramp-file-name
4643 (defun tramp-find-default-method (user host)
4644 "Look up the right method to use in `tramp-default-method-alist'."
4645 (let ((choices tramp-default-method-alist)
4646 (method tramp-default-method)
4649 (setq item (pop choices))
4650 (when (and (string-match (nth 0 item) host)
4651 (string-match (nth 1 item) (or user "")))
4652 (setq method (nth 2 item))
4653 (setq choices nil)))
4656 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
4657 ;; a user name is not provided in the "file name" very well.
4658 (defun tramp-dissect-multi-file-name (name)
4659 "Not implemented yet."
4660 (let ((regexp (nth 0 tramp-multi-file-name-structure))
4661 (method-index (nth 1 tramp-multi-file-name-structure))
4662 (hops-index (nth 2 tramp-multi-file-name-structure))
4663 (path-index (nth 3 tramp-multi-file-name-structure))
4664 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
4665 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
4666 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
4667 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
4668 method hops len hop-methods hop-users hop-hosts path)
4669 (unless (string-match (format regexp hop-regexp) name)
4670 (error "Not a multi tramp file name: %s" name))
4671 (setq method (match-string method-index name))
4672 (setq hops (match-string hops-index name))
4673 (setq len (/ (length (match-data t)) 2))
4674 (when (< path-index 0) (incf path-index len))
4675 (setq path (match-string path-index name))
4677 (while (string-match hop-regexp hops index)
4678 (setq index (match-end 0))
4680 (cons (match-string hop-method-index hops) hop-methods))
4682 (cons (match-string hop-user-index hops) hop-users))
4684 (cons (match-string hop-host-index hops) hop-hosts))))
4685 (make-tramp-file-name
4686 :multi-method method
4687 :method (apply 'vector (reverse hop-methods))
4688 :user (apply 'vector (reverse hop-users))
4689 :host (apply 'vector (reverse hop-hosts))
4692 (defun tramp-make-tramp-file-name (multi-method method user host path)
4693 "Constructs a tramp file name from METHOD, USER, HOST and PATH."
4694 (unless tramp-make-tramp-file-format
4695 (error "`tramp-make-tramp-file-format' is nil"))
4697 (tramp-make-tramp-multi-file-name multi-method method user host path)
4699 (format-spec tramp-make-tramp-file-format
4700 (list (cons ?m method)
4704 (format-spec tramp-make-tramp-file-user-nil-format
4705 (list (cons ?m method)
4709 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
4710 ;; with this when USER is nil?
4711 (defun tramp-make-tramp-multi-file-name (multi-method method user host path)
4712 "Constructs a tramp file name for a multi-hop method."
4713 (unless tramp-make-multi-tramp-file-format
4714 (error "`tramp-make-multi-tramp-file-format' is nil"))
4715 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
4716 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
4717 (path-format (nth 2 tramp-make-multi-tramp-file-format))
4718 (prefix (format-spec prefix-format (list (cons ?m multi-method))))
4720 (path (format-spec path-format (list (cons ?p path))))
4722 (len (length method)))
4724 (let ((m (aref method i))
4727 (setq hops (concat hops
4734 (concat prefix hops path)))
4736 (defun tramp-make-rcp-program-file-name (user host path)
4737 "Create a file name suitable to be passed to `rcp'."
4739 (format "%s@%s:%s" user host path)
4740 (format "%s:%s" host path)))
4742 (defun tramp-make-ange-ftp-file-name (user host path)
4743 "Given user, host, and path, return an Ange-FTP filename."
4745 (format "/%s@%s:%s" user host path)
4746 (format "/%s:%s" host path)))
4748 (defun tramp-method-out-of-band-p (multi-method method)
4749 "Return t if this is an out-of-band method, nil otherwise.
4750 It is important to check for this condition, since it is not possible
4751 to enter a password for the `tramp-rcp-program'."
4752 (tramp-get-rcp-program multi-method method))
4754 ;; Variables local to connection.
4756 (defun tramp-get-ls-command (multi-method method user host)
4758 (tramp-maybe-open-connection multi-method method user host)
4759 (set-buffer (tramp-get-buffer multi-method method user host))
4762 (defun tramp-get-test-groks-nt (multi-method method user host)
4764 (tramp-maybe-open-connection multi-method method user host)
4765 (set-buffer (tramp-get-buffer multi-method method user host))
4766 tramp-test-groks-nt))
4768 (defun tramp-get-file-exists-command (multi-method method user host)
4770 (tramp-maybe-open-connection multi-method method user host)
4771 (set-buffer (tramp-get-buffer multi-method method user host))
4772 tramp-file-exists-command))
4774 (defun tramp-get-remote-perl (multi-method method user host)
4775 (tramp-get-connection-property "perl" nil multi-method method user host))
4777 (defun tramp-get-remote-ln (multi-method method user host)
4778 (tramp-get-connection-property "ln" nil multi-method method user host))
4780 ;; Get a property of a TRAMP connection.
4781 (defun tramp-get-connection-property (property default multi-method method user host)
4782 "Get the named property for the connection.
4783 If the value is not set for the connection, return `default'"
4784 (tramp-maybe-open-connection multi-method method user host)
4785 (with-current-buffer (tramp-get-buffer multi-method method user host)
4788 (symbol-value (intern (concat "tramp-connection-property-" property)))
4791 ;; Set a property of a TRAMP connection.
4792 (defun tramp-set-connection-property (property value multi-method method user host)
4793 "Set the named property of a TRAMP connection."
4794 (tramp-maybe-open-connection multi-method method user host)
4795 (with-current-buffer (tramp-get-buffer multi-method method user host)
4796 (set (make-local-variable
4797 (intern (concat "tramp-connection-property-" property)))
4802 (defun tramp-get-connection-function (multi-method method)
4803 (second (or (assoc 'tramp-connection-function
4804 (assoc (or multi-method method tramp-default-method)
4806 (error "Method `%s' didn't specify a connection function"
4807 (or multi-method method)))))
4809 (defun tramp-get-remote-sh (multi-method method)
4810 (second (or (assoc 'tramp-remote-sh
4811 (assoc (or multi-method method tramp-default-method)
4813 (error "Method `%s' didn't specify a remote shell"
4814 (or multi-method method)))))
4816 (defun tramp-get-rsh-program (multi-method method)
4817 (second (or (assoc 'tramp-rsh-program
4818 (assoc (or multi-method method tramp-default-method)
4820 (error "Method `%s' didn't specify an rsh program"
4821 (or multi-method method)))))
4823 (defun tramp-get-rsh-args (multi-method method)
4824 (second (or (assoc 'tramp-rsh-args
4825 (assoc (or multi-method method tramp-default-method)
4827 (error "Method `%s' didn't specify rsh args"
4828 (or multi-method method)))))
4830 (defun tramp-get-rcp-program (multi-method method)
4831 (second (or (assoc 'tramp-rcp-program
4832 (assoc (or multi-method method tramp-default-method)
4834 (error "Method `%s' didn't specify an rcp program"
4835 (or multi-method method)))))
4837 (defun tramp-get-rcp-args (multi-method method)
4838 (second (or (assoc 'tramp-rcp-args
4839 (assoc (or multi-method method tramp-default-method)
4841 (error "Method `%s' didn't specify rcp args"
4842 (or multi-method method)))))
4844 (defun tramp-get-rcp-keep-date-arg (multi-method method)
4845 (second (or (assoc 'tramp-rcp-keep-date-arg
4846 (assoc (or multi-method method tramp-default-method)
4848 (error "Method `%s' didn't specify `keep-date' arg for tramp"
4849 (or multi-method method)))))
4851 (defun tramp-get-su-program (multi-method method)
4852 (second (or (assoc 'tramp-su-program
4853 (assoc (or multi-method method tramp-default-method)
4855 (error "Method `%s' didn't specify a su program"
4856 (or multi-method method)))))
4858 (defun tramp-get-su-args (multi-method method)
4859 (second (or (assoc 'tramp-su-args
4860 (assoc (or multi-method method tramp-default-method)
4862 (error "Method `%s' didn't specify su args"
4863 (or multi-method method)))))
4865 (defun tramp-get-encoding-command (multi-method method)
4866 (second (or (assoc 'tramp-encoding-command
4867 (assoc (or multi-method method tramp-default-method)
4869 (error "Method `%s' didn't specify an encoding command"
4870 (or multi-method method)))))
4872 (defun tramp-get-decoding-command (multi-method method)
4873 (second (or (assoc 'tramp-decoding-command
4874 (assoc (or multi-method method tramp-default-method)
4876 (error "Method `%s' didn't specify a decoding command"
4877 (or multi-method method)))))
4879 (defun tramp-get-encoding-function (multi-method method)
4880 (second (or (assoc 'tramp-encoding-function
4881 (assoc (or multi-method method tramp-default-method)
4883 (error "Method `%s' didn't specify an encoding function"
4884 (or multi-method method)))))
4886 (defun tramp-get-decoding-function (multi-method method)
4887 (second (or (assoc 'tramp-decoding-function
4888 (assoc (or multi-method method tramp-default-method)
4890 (error "Method `%s' didn't specify a decoding function"
4891 (or multi-method method)))))
4893 (defun tramp-get-telnet-program (multi-method method)
4894 (second (or (assoc 'tramp-telnet-program
4895 (assoc (or multi-method method tramp-default-method)
4897 (error "Method `%s' didn't specify a telnet program"
4898 (or multi-method method)))))
4900 (defun tramp-get-telnet-args (multi-method method)
4901 (second (or (assoc 'tramp-telnet-args
4902 (assoc (or multi-method method tramp-default-method)
4904 (error "Method `%s' didn't specify telnet args"
4905 (or multi-method method)))))
4907 ;; Auto saving to a special directory.
4909 (defun tramp-make-auto-save-file-name (fn)
4910 "Returns a file name in `tramp-auto-save-directory' for autosaving this file."
4911 (when tramp-auto-save-directory
4912 (unless (file-exists-p tramp-auto-save-directory)
4913 (make-directory tramp-auto-save-directory t)))
4914 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
4915 ;; file name we make sure that jka-compr isn't used for the
4917 (let ((buffer-file-name (expand-file-name
4918 (tramp-subst-strs-in-string '(("_" . "|")
4925 tramp-auto-save-directory)))
4926 (make-auto-save-file-name)))
4928 (defadvice make-auto-save-file-name
4929 (around tramp-advice-make-auto-save-file-name () activate)
4930 "Invoke `tramp-make-auto-save-file-name' for tramp files."
4931 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name))
4932 tramp-auto-save-directory)
4933 (setq ad-return-value
4934 (tramp-make-auto-save-file-name (buffer-file-name)))
4937 (defun tramp-subst-strs-in-string (alist string)
4938 "Replace all occurrences of the string FROM with TO in STRING.
4939 ALIST is of the form ((FROM . TO) ...)."
4942 (let* ((pr (car alist))
4945 (while (string-match (regexp-quote from) string)
4946 (setq string (replace-match to t t string)))
4947 (setq alist (cdr alist))))
4950 (defun tramp-insert-with-face (face string)
4951 "Insert text with a specific face."
4952 (let ((start (point)))
4954 (add-text-properties start (point) (list 'face face))))
4956 ;; ------------------------------------------------------------
4957 ;; -- Compatibility functions section --
4958 ;; ------------------------------------------------------------
4960 (defun tramp-temporary-file-directory ()
4961 "Return name of directory for temporary files (compat function).
4962 For Emacs, this is the variable `temporary-file-directory', for XEmacs
4963 this is the function `temp-directory'."
4964 (cond ((boundp 'temporary-file-directory)
4965 (symbol-value 'temporary-file-directory))
4966 ((fboundp 'temp-directory)
4967 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
4968 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
4969 (file-name-as-directory (getenv "TEMP")))
4970 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
4971 (file-name-as-directory (getenv "TMP")))
4972 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
4973 (file-name-as-directory (getenv "TMPDIR")))
4974 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
4975 (t (message (concat "Neither `temporary-file-directory' nor "
4976 "`temp-directory' is defined -- using /tmp."))
4977 (file-name-as-directory "/tmp"))))
4979 (defun tramp-read-passwd (prompt)
4980 "Read a password from user (compat function).
4981 Invokes `read-passwd' if that is defined, else `ange-ftp-read-passwd'."
4983 (if (fboundp 'read-passwd) #'read-passwd #'ange-ftp-read-passwd)
4986 (defun tramp-time-diff (t1 t2)
4987 "Return the difference between the two times, in seconds.
4988 T1 and T2 are time values (as returned by `current-time' for example).
4990 NOTE: This function will fail if the time difference is too large to
4992 ;; Pacify byte-compiler with `symbol-function'.
4993 (cond ((fboundp 'subtract-time)
4994 (cadr (funcall (symbol-function 'subtract-time) t1 t2)))
4995 ((fboundp 'itimer-time-difference)
4997 (symbol-function 'itimer-time-difference)
4998 (if (< (length t1) 3) (append t1 '(0)) t1)
4999 (if (< (length t2) 3) (append t2 '(0)) t2))))
5001 ;; snarfed from Emacs 21 time-date.el
5002 (cadr (let ((borrow (< (cadr t1) (cadr t2))))
5003 (list (- (car t1) (car t2) (if borrow 1 0))
5004 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))))
5006 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
5007 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
5008 EOL-TYPE can be one of `dos', `unix', or `mac'."
5009 (cond ((fboundp 'coding-system-change-eol-conversion)
5010 (apply #'coding-system-change-eol-conversion
5011 (list coding-system eol-type)))
5012 ((fboundp 'subsidiary-coding-system)
5014 #'subsidiary-coding-system
5016 (cond ((eq eol-type 'dos) 'crlf)
5017 ((eq eol-type 'unix) 'lf)
5018 ((eq eol-type 'mac) 'cr)
5020 (error "Unknown EOL-TYPE `%s', must be %s"
5022 "`dos', `unix', or `mac'"))))))
5023 (t (error "Can't change EOL conversion -- is MULE missing?"))))
5025 (defun tramp-split-string (string pattern)
5026 "Like `split-string' but omit empty strings.
5027 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
5028 This is, the first, empty, element is omitted. In XEmacs, the first
5029 element is not omitted.
5031 Note: this function has been written for `tramp-handle-file-truename'.
5032 If you want to use it for something else, you'll have to check whether
5033 it does the right thing."
5034 (delete "" (split-string string pattern)))
5036 ;; ------------------------------------------------------------
5037 ;; -- Kludges section --
5038 ;; ------------------------------------------------------------
5040 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
5041 ;; does not deal well with newline characters. Newline is replaced by
5042 ;; backslash newline. But if, say, the string `a backslash newline b'
5043 ;; is passed to a shell, the shell will expand this into "ab",
5044 ;; completely omitting the newline. This is not what was intended.
5045 ;; It does not appear to be possible to make the function
5046 ;; `shell-quote-argument' work with newlines without making it
5047 ;; dependent on the shell used. But within this package, we know that
5048 ;; we will always use a Bourne-like shell, so we use an approach which
5051 ;; The approach is simple: we call `shell-quote-argument', then
5052 ;; massage the newline part of the result.
5054 ;; This function should produce a string which is grokked by a Unix
5055 ;; shell, even if the Emacs is running on Windows. Since this is the
5056 ;; kludges section, we bind `system-type' in such a way that
5057 ;; `shell-quote-arguments' behaves as if on Unix.
5059 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
5060 ;; function to work with Bourne-like shells.
5062 ;; CCC: This function should be rewritten so that
5063 ;; `shell-quote-argument' is not used. This way, we are safe from
5064 ;; changes in `shell-quote-argument'.
5065 (defun tramp-shell-quote-argument (s)
5066 "Similar to `shell-quote-argument', but groks newlines.
5067 Only works for Bourne-like shells."
5068 (let ((system-type 'not-windows))
5070 (let ((result (shell-quote-argument s))
5071 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
5072 (when (and (>= (length result) 2)
5073 (string= (substring result 0 2) "\\~"))
5074 (setq result (substring result 1)))
5075 (while (string-match nl result)
5076 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
5080 ;; ;; EFS hooks itself into the file name handling stuff in more places
5081 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
5082 ;; ;; away from tramp.el paths.
5084 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
5085 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
5086 ;; ;; connection to help it's dired process. Not that I have any real
5087 ;; ;; idea *why* this is helpful to dired.
5089 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
5091 ;; ;; Daniel Pittman <daniel@danann.net>
5093 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
5094 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
5095 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
5096 ;; ;; EFS might nicht be loaded yet.
5097 ;; (defadvice efs-ftp-path (around dont-match-tramp-path activate protect)
5098 ;; "Cause efs-ftp-path to fail when the path is a TRAMP path."
5099 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
5103 ;; We currently use "[" and "]" in the filename format. In Emacs
5104 ;; 20.x, this means that Emacs wants to expand wildcards if
5105 ;; `find-file-wildcards' is non-nil, and then barfs because no
5106 ;; expansion could be found. We detect this situation and do
5107 ;; something really awful: we have `file-expand-wildcards' return the
5108 ;; original filename if it can't expand anything. Let's just hope
5109 ;; that this doesn't break anything else.
5111 ;; Another problem is that the check is done by Emacs version, which
5112 ;; is really not what we want to do. Oh, well.
5114 ;;(when (and (not (featurep 'xemacs))
5115 ;; (= emacs-major-version 20))
5116 ;; It seems that this advice is needed in Emacs 21, too.
5117 (defadvice file-expand-wildcards (around tramp-fix activate)
5118 (let ((name (ad-get-arg 0)))
5119 (if (tramp-tramp-file-p name)
5120 ;; If it's a Tramp file, dissect it and look if wildcards
5121 ;; need to be expanded at all.
5122 (let ((v (tramp-dissect-file-name name)))
5123 (if (string-match "[[*?]" (tramp-file-name-path v))
5124 (let ((res ad-do-it))
5125 (setq ad-return-value (or res (list name))))
5126 (setq ad-return-value (list name))))
5127 ;; If it is not a Tramp file, just run the original function.
5128 (let ((res ad-do-it))
5129 (setq ad-return-value (or res (list name)))))))
5132 ;; Make the `reporter` functionality available for making bug reports about
5133 ;; the package. A most useful piece of code.
5135 (unless (fboundp 'reporter-submit-bug-report)
5136 (autoload 'reporter-submit-bug-report "reporter"))
5139 "Submit a bug report to the TRAMP developers."
5142 (let ((reporter-prompt-for-summary-p t))
5143 (reporter-submit-bug-report
5144 tramp-bug-report-address ; to-address
5145 (format "tramp (%s)" tramp-version) ; package name and version
5149 tramp-file-exists-command
5150 tramp-current-multi-method
5151 tramp-current-method
5156 tramp-auto-save-directory ; vars to dump
5157 tramp-default-method
5158 tramp-rsh-end-of-line
5160 tramp-login-prompt-regexp
5161 tramp-password-prompt-regexp
5162 tramp-wrong-passwd-regexp
5163 tramp-temp-name-prefix
5164 tramp-file-name-structure
5165 tramp-file-name-regexp
5166 tramp-multi-file-name-structure
5167 tramp-multi-file-name-hop-structure
5169 tramp-multi-connection-function-alist
5170 tramp-make-tramp-file-format
5173 ;; Non-tramp variables of interest
5174 shell-prompt-pattern
5176 backup-by-copying-when-linked
5177 backup-by-copying-when-mismatch
5178 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
5179 'backup-by-copying-when-privileged-mismatch)
5180 file-name-handler-alist)
5184 Enter your bug report in this message, including as much detail as you
5185 possibly can about the problem, what you did to cause it and what the
5186 local and remote machines are.
5188 If you can give a simple set of instructions to make this bug happen
5189 reliably, please include those. Thank you for helping kill bugs in
5191 --bug report follows this line--")))
5193 (defalias 'tramp-submit-bug 'tramp-bug)
5197 ;; Make sure that we get integration with the VC package.
5198 ;; When it is loaded, we need to pull in the integration module.
5199 ;; This must come after (provide 'tramp) because tramp-vc.el
5201 (eval-after-load "vc"
5202 '(require 'tramp-vc))
5206 ;; * Revise the comments near the beginning of the file.
5207 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
5208 ;; work for remote files.
5209 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
5210 ;; `shell-quote-argument'.
5211 ;; * Completion gets confused when you leave out the method name.
5212 ;; * Support `dired-compress-file' filename handler.
5213 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
5214 ;; by the files in that directory. Add this here.
5215 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
5216 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
5217 ;; * When logging in, keep looking for questions according to an alist
5218 ;; and then invoke the right function.
5219 ;; * Case-insensitive filename completion. (Norbert Goevert.)
5220 ;; * Running CVS remotely doesn't appear to work right. It thinks
5221 ;; files are locked by somebody else even if I'm the locking user.
5222 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
5224 ;; * Don't use globbing for directories with many files, as this is
5225 ;; likely to produce long command lines, and some shells choke on
5226 ;; long command lines.
5227 ;; * Find out about the new auto-save mechanism in Emacs 21 and
5228 ;; do the right thing.
5229 ;; * `vc-directory' does not work. It never displays any files, even
5230 ;; if it does show files when run locally.
5231 ;; * Allow correction of passwords, if the remote end allows this.
5232 ;; (Mark Hershberger)
5233 ;; * Make sure permissions of tmp file are good.
5234 ;; (Nelson Minar <nelson@media.mit.edu>)
5235 ;; * Grok passwd prompts with scp? (David Winter
5236 ;; <winter@nevis1.nevis.columbia.edu>). Maybe just do `ssh -l user
5237 ;; host', then wait a while for the passwd or passphrase prompt. If
5238 ;; there is one, remember the passwd/phrase.
5239 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
5240 ;; * Do asynchronous `shell-command's.
5241 ;; * Grok `append' parameter for `write-region'.
5242 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
5243 ;; * abbreviate-file-name
5244 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
5245 ;; * `C' in dired gives error `not tramp file name'.
5246 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
5247 ;; what the user names should default to, though.
5248 ;; * better error checking. At least whenever we see something
5249 ;; strange when doing zerop, we should kill the process and start
5250 ;; again. (Greg Stark)
5251 ;; * Add caching for filename completion. (Greg Stark)
5252 ;; Of course, this has issues with usability (stale cache bites)
5253 ;; -- <daniel@danann.net>
5254 ;; * Provide a local cache of old versions of remote files for the rsync
5255 ;; transfer method to use. (Greg Stark)
5256 ;; * Remove unneeded parameters from methods.
5257 ;; * Invoke rsync once for copying a whole directory hierarchy.
5258 ;; (Francesco Potortì)
5259 ;; * Should we set PATH ourselves or should we rely on the remote end
5261 ;; * Do the autoconf thing.
5262 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
5263 ;; * Allow non-Unix remote systems. (More a long-term thing.)
5264 ;; * Make it work for different encodings, and for different file name
5265 ;; encodings, too. (Daniel Pittman)
5266 ;; * Change applicable functions to pass a struct tramp-file-name rather
5267 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, PATH.
5268 ;; * Implement asynchronous shell commands.
5269 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
5270 ;; * Progress reports while copying files. (Michael Kifer)
5271 ;; * `Smart' connection method that uses inline for small and out of
5272 ;; band for large files. (Michael Kifer)
5273 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5274 ;; then look if it's the right version (with `perl -v').
5275 ;; * When editing a remote CVS controlled file as a different user, VC
5276 ;; gets confused about the file locking status. Try to find out why
5277 ;; the workaround doesn't work.
5278 ;; * When user is running ssh-agent, it would be useful to add the
5279 ;; passwords typed by the user to that agent. This way, the next time
5280 ;; round, the users don't have to type all this in again.
5281 ;; This would be especially useful for start-process, I think.
5282 ;; An easy way to implement start-process is to open a second shell
5283 ;; connection which is inconvenient if the user has to reenter
5285 ;; * Change `copy-file' to grok the case where the filename handler
5286 ;; for the source and the target file are different. Right now,
5287 ;; it looks at the source file and then calls that handler, if
5288 ;; there is one. But since ange-ftp, for instance, does not know
5289 ;; about Tramp, it does not do the right thing if the target file
5290 ;; name is a Tramp name.
5292 ;; Functions for file-name-handler-alist:
5293 ;; diff-latest-backup-file -- in diff.el
5294 ;; dired-compress-file
5295 ;; dired-uncache -- this will be needed when we do insert-directory caching
5296 ;; file-name-as-directory -- use primitive?
5297 ;; file-name-directory -- use primitive?
5298 ;; file-name-nondirectory -- use primitive?
5299 ;; file-name-sans-versions -- use primitive?
5300 ;; file-newer-than-file-p
5301 ;; find-backup-file-name
5302 ;; get-file-buffer -- use primitive
5304 ;; unhandled-file-name-directory
5307 ;;; tramp.el ends here