]> code.delx.au - gnu-emacs/blob - lisp/net/tramp.el
Update copyright year to 2016
[gnu-emacs] / lisp / net / tramp.el
1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
2
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
4
5 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
8 ;; Package: tramp
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides remote file editing, similar to ange-ftp.
28 ;; The difference is that ange-ftp uses FTP to transfer files between
29 ;; the local and the remote host, whereas tramp.el uses a combination
30 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
31 ;;
32 ;; For more detailed instructions, please see the info file.
33 ;;
34 ;; Notes:
35 ;; -----
36 ;;
37 ;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
38 ;; and higher. For XEmacs 21, you need the package `fsf-compat' for
39 ;; the `with-timeout' macro.
40 ;;
41 ;; Also see the todo list at the bottom of this file.
42 ;;
43 ;; The current version of Tramp can be retrieved from the following URL:
44 ;; http://ftp.gnu.org/gnu/tramp/
45 ;;
46 ;; There's a mailing list for this, as well. Its name is:
47 ;; tramp-devel@gnu.org
48 ;; You can use the Web to subscribe, under the following URL:
49 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
50 ;;
51 ;; For the adventurous, the current development sources are available
52 ;; via Git. You can find instructions about this at the following URL:
53 ;; http://savannah.gnu.org/projects/tramp/
54 ;;
55 ;; Don't forget to put on your asbestos longjohns, first!
56
57 ;;; Code:
58
59 (require 'tramp-compat)
60
61 ;; Pacify byte-compiler.
62 (eval-when-compile
63 (require 'cl))
64 (defvar bkup-backup-directory-info)
65 (defvar directory-sep-char)
66 (defvar eshell-path-env)
67 (defvar ls-lisp-use-insert-directory-program)
68 (defvar outline-regexp)
69
70 ;;; User Customizable Internal Variables:
71
72 (defgroup tramp nil
73 "Edit remote files with a combination of ssh, scp, etc."
74 :group 'files
75 :group 'comm
76 :link '(custom-manual "(tramp)Top")
77 :version "22.1")
78
79 ;; Maybe we need once a real Tramp mode, with key bindings etc.
80 ;;;###autoload
81 (defcustom tramp-mode t
82 "Whether Tramp is enabled.
83 If it is set to nil, all remote file names are used literally."
84 :group 'tramp
85 :type 'boolean)
86
87 (defcustom tramp-verbose 3
88 "Verbosity level for Tramp messages.
89 Any level x includes messages for all levels 1 .. x-1. The levels are
90
91 0 silent (no tramp messages at all)
92 1 errors
93 2 warnings
94 3 connection to remote hosts (default level)
95 4 activities
96 5 internal
97 6 sent and received strings
98 7 file caching
99 8 connection properties
100 9 test commands
101 10 traces (huge)."
102 :group 'tramp
103 :type 'integer)
104
105 ;; Emacs case.
106 (eval-and-compile
107 (when (boundp 'backup-directory-alist)
108 (defcustom tramp-backup-directory-alist nil
109 "Alist of filename patterns and backup directory names.
110 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
111 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
112 is a local file name, the backup directory is prepended with Tramp file
113 name prefix \(method, user, host) of file.
114
115 \(setq tramp-backup-directory-alist backup-directory-alist)
116
117 gives the same backup policy for Tramp files on their hosts like the
118 policy for local files."
119 :group 'tramp
120 :type '(repeat (cons (regexp :tag "Regexp matching filename")
121 (directory :tag "Backup directory name"))))))
122
123 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
124 ;; the package "backup-dir" might not be loaded yet.
125 (eval-and-compile
126 (when (featurep 'xemacs)
127 (defcustom tramp-bkup-backup-directory-info nil
128 "Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
129 It has the same meaning like `bkup-backup-directory-info' from package
130 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
131 file name, the backup directory is prepended with Tramp file name prefix
132 \(method, user, host) of file.
133
134 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info)
135
136 gives the same backup policy for Tramp files on their hosts like the
137 policy for local files."
138 :type '(repeat
139 (list (regexp :tag "File regexp")
140 (string :tag "Backup Dir")
141 (set :inline t
142 (const ok-create)
143 (const full-path)
144 (const prepend-name)
145 (const search-upward))))
146 :group 'tramp)))
147
148 (defcustom tramp-auto-save-directory nil
149 "Put auto-save files in this directory, if set.
150 The idea is to use a local directory so that auto-saving is faster.
151 This setting has precedence over `auto-save-file-name-transforms'."
152 :group 'tramp
153 :type '(choice (const :tag "Use default" nil)
154 (directory :tag "Auto save directory name")))
155
156 (defcustom tramp-encoding-shell
157 (if (memq system-type '(windows-nt))
158 (getenv "COMSPEC")
159 "/bin/sh")
160 "Use this program for encoding and decoding commands on the local host.
161 This shell is used to execute the encoding and decoding command on the
162 local host, so if you want to use `~' in those commands, you should
163 choose a shell here which groks tilde expansion. `/bin/sh' normally
164 does not understand tilde expansion.
165
166 For encoding and decoding, commands like the following are executed:
167
168 /bin/sh -c COMMAND < INPUT > OUTPUT
169
170 This variable can be used to change the \"/bin/sh\" part. See the
171 variable `tramp-encoding-command-switch' for the \"-c\" part.
172
173 If the shell must be forced to be interactive, see
174 `tramp-encoding-command-interactive'.
175
176 Note that this variable is not used for remote commands. There are
177 mechanisms in tramp.el which automatically determine the right shell to
178 use for the remote host."
179 :group 'tramp
180 :type '(file :must-match t))
181
182 (defcustom tramp-encoding-command-switch
183 (if (string-match "cmd\\.exe" tramp-encoding-shell)
184 "/c"
185 "-c")
186 "Use this switch together with `tramp-encoding-shell' for local commands.
187 See the variable `tramp-encoding-shell' for more information."
188 :group 'tramp
189 :type 'string)
190
191 (defcustom tramp-encoding-command-interactive
192 (unless (string-match "cmd\\.exe" tramp-encoding-shell) "-i")
193 "Use this switch together with `tramp-encoding-shell' for interactive shells.
194 See the variable `tramp-encoding-shell' for more information."
195 :version "24.1"
196 :group 'tramp
197 :type '(choice (const nil) string))
198
199 ;;;###tramp-autoload
200 (defvar tramp-methods nil
201 "Alist of methods for remote files.
202 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
203 Each NAME stands for a remote access method. Each PARAM is a
204 pair of the form (KEY VALUE). The following KEYs are defined:
205 * `tramp-remote-shell'
206 This specifies the shell to use on the remote host. This
207 MUST be a Bourne-like shell. It is normally not necessary to
208 set this to any value other than \"/bin/sh\": Tramp wants to
209 use a shell which groks tilde expansion, but it can search
210 for it. Also note that \"/bin/sh\" exists on all Unixen,
211 this might not be true for the value that you decide to use.
212 You Have Been Warned.
213 * `tramp-remote-shell-login'
214 This specifies the arguments to let `tramp-remote-shell' run
215 as a login shell. It defaults to (\"-l\"), but some shells,
216 like ksh, require another argument. See
217 `tramp-connection-properties' for a way to overwrite the
218 default value.
219 * `tramp-remote-shell-args'
220 For implementation of `shell-command', this specifies the
221 arguments to let `tramp-remote-shell' run a single command.
222 * `tramp-login-program'
223 This specifies the name of the program to use for logging in to the
224 remote host. This may be the name of rsh or a workalike program,
225 or the name of telnet or a workalike, or the name of su or a workalike.
226 * `tramp-login-args'
227 This specifies the list of arguments to pass to the above
228 mentioned program. Please note that this is a list of list of arguments,
229 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
230 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
231 There are some patterns: \"%h\" in this list is replaced by the host
232 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
233 port number, and \"%%\" can be used to obtain a literal percent character.
234 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
235 expansion (i.e. no host or no user specified), this list is not used as
236 argument. By this, arguments like (\"-l\" \"%u\") are optional.
237 \"%t\" is replaced by the temporary file name produced with
238 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
239 parameter of a program, if exists. \"%c\" adds additional
240 `tramp-ssh-controlmaster-options' options for the first hop.
241 * `tramp-login-env'
242 A list of environment variables and their values, which will
243 be set when calling `tramp-login-program'.
244 * `tramp-async-args'
245 When an asynchronous process is started, we know already that
246 the connection works. Therefore, we can pass additional
247 parameters to suppress diagnostic messages, in order not to
248 tamper the process output.
249 * `tramp-copy-program'
250 This specifies the name of the program to use for remotely copying
251 the file; this might be the absolute filename of scp or the name of
252 a workalike program. It is always applied on the local host.
253 * `tramp-copy-args'
254 This specifies the list of parameters to pass to the above mentioned
255 program, the hints for `tramp-login-args' also apply here.
256 * `tramp-copy-env'
257 A list of environment variables and their values, which will
258 be set when calling `tramp-copy-program'.
259 * `tramp-remote-copy-program'
260 The listener program to be applied on remote side, if needed.
261 * `tramp-remote-copy-args'
262 The list of parameters to pass to the listener program, the hints
263 for `tramp-login-args' also apply here. Additionally, \"%r\" could
264 be used here and in `tramp-copy-args'. It denotes a randomly
265 chosen port for the remote listener.
266 * `tramp-copy-keep-date'
267 This specifies whether the copying program when the preserves the
268 timestamp of the original file.
269 * `tramp-copy-keep-tmpfile'
270 This specifies whether a temporary local file shall be kept
271 for optimization reasons (useful for \"rsync\" methods).
272 * `tramp-copy-recursive'
273 Whether the operation copies directories recursively.
274 * `tramp-default-port'
275 The default port of a method is needed in case of gateway connections.
276 Additionally, it is used as indication which method is prepared for
277 passing gateways.
278 * `tramp-gw-args'
279 As the attribute name says, additional arguments are specified here
280 when a method is applied via a gateway.
281 * `tramp-tmpdir'
282 A directory on the remote host for temporary files. If not
283 specified, \"/tmp\" is taken as default.
284 * `tramp-connection-timeout'
285 This is the maximum time to be spent for establishing a connection.
286 In general, the global default value shall be used, but for
287 some methods, like \"su\" or \"sudo\", a shorter timeout
288 might be desirable.
289
290 What does all this mean? Well, you should specify `tramp-login-program'
291 for all methods; this program is used to log in to the remote site. Then,
292 there are two ways to actually transfer the files between the local and the
293 remote side. One way is using an additional scp-like program. If you want
294 to do this, set `tramp-copy-program' in the method.
295
296 Another possibility for file transfer is inline transfer, i.e. the
297 file is passed through the same buffer used by `tramp-login-program'. In
298 this case, the file contents need to be protected since the
299 `tramp-login-program' might use escape codes or the connection might not
300 be eight-bit clean. Therefore, file contents are encoded for transit.
301 See the variables `tramp-local-coding-commands' and
302 `tramp-remote-coding-commands' for details.
303
304 So, to summarize: if the method is an out-of-band method, then you
305 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
306 inline method, then these two parameters should be nil. Methods which
307 are fit for gateways must have `tramp-default-port' at least.
308
309 Notes:
310
311 When using `su' or `sudo' the phrase \"open connection to a remote
312 host\" sounds strange, but it is used nevertheless, for consistency.
313 No connection is opened to a remote host, but `su' or `sudo' is
314 started on the local host. You should specify a remote host
315 `localhost' or the name of the local host. Another host name is
316 useful only in combination with `tramp-default-proxies-alist'.")
317
318 (defcustom tramp-default-method
319 ;; An external copy method seems to be preferred, because it performs
320 ;; much better for large files, and it hasn't too serious delays
321 ;; for small files. But it must be ensured that there aren't
322 ;; permanent password queries. Either a password agent like
323 ;; "ssh-agent" or "Pageant" shall run, or the optional
324 ;; password-cache.el or auth-sources.el packages shall be active for
325 ;; password caching. If we detect that the user is running OpenSSH
326 ;; 4.0 or newer, we could reuse the connection, which calls also for
327 ;; an external method.
328 (cond
329 ;; PuTTY is installed. We don't take it, if it is installed on a
330 ;; non-windows system, or pscp from the pssh (parallel ssh) package
331 ;; is found.
332 ((and (eq system-type 'windows-nt)
333 (executable-find "pscp"))
334 (if (or (fboundp 'password-read)
335 (fboundp 'auth-source-user-or-password)
336 (fboundp 'auth-source-search)
337 ;; Pageant is running.
338 (tramp-compat-process-running-p "Pageant"))
339 "pscp"
340 "plink"))
341 ;; There is an ssh installation.
342 ((executable-find "scp")
343 (if (or (fboundp 'password-read)
344 (fboundp 'auth-source-user-or-password)
345 (fboundp 'auth-source-search)
346 ;; ssh-agent is running.
347 (getenv "SSH_AUTH_SOCK")
348 (getenv "SSH_AGENT_PID"))
349 "scp"
350 "ssh"))
351 ;; Fallback.
352 (t "ftp"))
353 "Default method to use for transferring files.
354 See `tramp-methods' for possibilities.
355 Also see `tramp-default-method-alist'."
356 :group 'tramp
357 :type 'string)
358
359 ;;;###tramp-autoload
360 (defcustom tramp-default-method-alist nil
361 "Default method to use for specific host/user pairs.
362 This is an alist of items (HOST USER METHOD). The first matching item
363 specifies the method to use for a file name which does not specify a
364 method. HOST and USER are regular expressions or nil, which is
365 interpreted as a regular expression which always matches. If no entry
366 matches, the variable `tramp-default-method' takes effect.
367
368 If the file name does not specify the user, lookup is done using the
369 empty string for the user name.
370
371 See `tramp-methods' for a list of possibilities for METHOD."
372 :group 'tramp
373 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
374 (choice :tag "User regexp" regexp sexp)
375 (choice :tag "Method name" string (const nil)))))
376
377 (defcustom tramp-default-user nil
378 "Default user to use for transferring files.
379 It is nil by default; otherwise settings in configuration files like
380 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
381
382 This variable is regarded as obsolete, and will be removed soon."
383 :group 'tramp
384 :type '(choice (const nil) string))
385
386 ;;;###tramp-autoload
387 (defcustom tramp-default-user-alist nil
388 "Default user to use for specific method/host pairs.
389 This is an alist of items (METHOD HOST USER). The first matching item
390 specifies the user to use for a file name which does not specify a
391 user. METHOD and USER are regular expressions or nil, which is
392 interpreted as a regular expression which always matches. If no entry
393 matches, the variable `tramp-default-user' takes effect.
394
395 If the file name does not specify the method, lookup is done using the
396 empty string for the method name."
397 :group 'tramp
398 :type '(repeat (list (choice :tag "Method regexp" regexp sexp)
399 (choice :tag " Host regexp" regexp sexp)
400 (choice :tag " User name" string (const nil)))))
401
402 (defcustom tramp-default-host (system-name)
403 "Default host to use for transferring files.
404 Useful for su and sudo methods mostly."
405 :group 'tramp
406 :type 'string)
407
408 ;;;###tramp-autoload
409 (defcustom tramp-default-host-alist nil
410 "Default host to use for specific method/user pairs.
411 This is an alist of items (METHOD USER HOST). The first matching item
412 specifies the host to use for a file name which does not specify a
413 host. METHOD and HOST are regular expressions or nil, which is
414 interpreted as a regular expression which always matches. If no entry
415 matches, the variable `tramp-default-host' takes effect.
416
417 If the file name does not specify the method, lookup is done using the
418 empty string for the method name."
419 :group 'tramp
420 :version "24.4"
421 :type '(repeat (list (choice :tag "Method regexp" regexp sexp)
422 (choice :tag " User regexp" regexp sexp)
423 (choice :tag " Host name" string (const nil)))))
424
425 (defcustom tramp-default-proxies-alist nil
426 "Route to be followed for specific host/user pairs.
427 This is an alist of items (HOST USER PROXY). The first matching
428 item specifies the proxy to be passed for a file name located on
429 a remote target matching USER@HOST. HOST and USER are regular
430 expressions. PROXY must be a Tramp filename without a localname
431 part. Method and user name on PROXY are optional, which is
432 interpreted with the default values. PROXY can contain the
433 patterns %h and %u, which are replaced by the strings matching
434 HOST or USER, respectively.
435
436 HOST, USER or PROXY could also be Lisp forms, which will be
437 evaluated. The result must be a string or nil, which is
438 interpreted as a regular expression which always matches."
439 :group 'tramp
440 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
441 (choice :tag "User regexp" regexp sexp)
442 (choice :tag " Proxy name" string (const nil)))))
443
444 (defcustom tramp-save-ad-hoc-proxies nil
445 "Whether to save ad-hoc proxies persistently."
446 :group 'tramp
447 :version "24.3"
448 :type 'boolean)
449
450 (defcustom tramp-restricted-shell-hosts-alist
451 (when (memq system-type '(windows-nt))
452 (list (concat "\\`" (regexp-quote (system-name)) "\\'")))
453 "List of hosts, which run a restricted shell.
454 This is a list of regular expressions, which denote hosts running
455 a registered shell like \"rbash\". Those hosts can be used as
456 proxies only, see `tramp-default-proxies-alist'. If the local
457 host runs a registered shell, it shall be added to this list, too."
458 :version "24.3"
459 :group 'tramp
460 :type '(repeat (regexp :tag "Host regexp")))
461
462 ;;;###tramp-autoload
463 (defconst tramp-local-host-regexp
464 (concat
465 "\\`"
466 (regexp-opt
467 (list "localhost" "localhost6" (system-name) "127.0.0.1" "::1") t)
468 "\\'")
469 "Host names which are regarded as local host.")
470
471 (defvar tramp-completion-function-alist nil
472 "Alist of methods for remote files.
473 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...).
474 Each NAME stands for a remote access method. Each PAIR is of the form
475 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
476 names from FILE for completion. The following predefined FUNCTIONs exists:
477
478 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
479 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
480 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
481 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
482 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
483 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
484 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
485 * `tramp-parse-netrc' for \"~/.netrc\" like files.
486 * `tramp-parse-putty' for PuTTY registered sessions.
487
488 FUNCTION can also be a user defined function. For more details see
489 the info pages.")
490
491 (defconst tramp-echo-mark-marker "_echo"
492 "String marker to surround echoed commands.")
493
494 (defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
495 "String length of `tramp-echo-mark-marker'.")
496
497 (defconst tramp-echo-mark
498 (concat tramp-echo-mark-marker
499 (make-string tramp-echo-mark-marker-length ?\b))
500 "String mark to be transmitted around shell commands.
501 Used to separate their echo from the output they produce. This
502 will only be used if we cannot disable remote echo via stty.
503 This string must have no effect on the remote shell except for
504 producing some echo which can later be detected by
505 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
506 followed by an equal number of backspaces to erase them will
507 usually suffice.")
508
509 (defconst tramp-echoed-echo-mark-regexp
510 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
511 tramp-echo-mark-marker tramp-echo-mark-marker-length)
512 "Regexp which matches `tramp-echo-mark' as it gets echoed by
513 the remote shell.")
514
515 (defcustom tramp-local-end-of-line
516 (if (memq system-type '(windows-nt)) "\r\n" "\n")
517 "String used for end of line in local processes."
518 :version "24.1"
519 :group 'tramp
520 :type 'string)
521
522 (defcustom tramp-rsh-end-of-line "\n"
523 "String used for end of line in rsh connections.
524 I don't think this ever needs to be changed, so please tell me about it
525 if you need to change this."
526 :group 'tramp
527 :type 'string)
528
529 (defcustom tramp-login-prompt-regexp
530 ".*\\(user\\|login\\)\\( .*\\)?: *"
531 "Regexp matching login-like prompts.
532 The regexp should match at end of buffer.
533
534 Sometimes the prompt is reported to look like \"login as:\"."
535 :group 'tramp
536 :type 'regexp)
537
538 (defcustom tramp-shell-prompt-pattern
539 ;; Allow a prompt to start right after a ^M since it indeed would be
540 ;; displayed at the beginning of the line (and Zsh uses it). This
541 ;; regexp works only for GNU Emacs.
542 ;; Allow also [] style prompts. They can appear only during
543 ;; connection initialization; Tramp redefines the prompt afterwards.
544 (concat (if (featurep 'xemacs) "" "\\(?:^\\|\r\\)")
545 "[^]#$%>\n]*#?[]#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
546 "Regexp to match prompts from remote shell.
547 Normally, Tramp expects you to configure `shell-prompt-pattern'
548 correctly, but sometimes it happens that you are connecting to a
549 remote host which sends a different kind of shell prompt. Therefore,
550 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
551 and also things matched by this variable. The default value of this
552 variable is similar to the default value of `shell-prompt-pattern',
553 which should work well in many cases.
554
555 This regexp must match both `tramp-initial-end-of-output' and
556 `tramp-end-of-output'."
557 :group 'tramp
558 :type 'regexp)
559
560 (defcustom tramp-password-prompt-regexp
561 (format "^.*\\(%s\\).*:\^@? *"
562 (if (boundp 'password-word-equivalents)
563 (regexp-opt (symbol-value 'password-word-equivalents))
564 "password\\|passphrase"))
565 "Regexp matching password-like prompts.
566 The regexp should match at end of buffer.
567
568 The `sudo' program appears to insert a `^@' character into the prompt."
569 :version "24.4"
570 :group 'tramp
571 :type 'regexp)
572
573 (defcustom tramp-wrong-passwd-regexp
574 (concat "^.*"
575 ;; These strings should be on the last line
576 (regexp-opt '("Permission denied"
577 "Login incorrect"
578 "Login Incorrect"
579 "Connection refused"
580 "Connection closed"
581 "Timeout, server not responding."
582 "Sorry, try again."
583 "Name or service not known"
584 "Host key verification failed."
585 "No supported authentication methods left to try!") t)
586 ".*"
587 "\\|"
588 "^.*\\("
589 ;; Here comes a list of regexes, separated by \\|
590 "Received signal [0-9]+"
591 "\\).*")
592 "Regexp matching a `login failed' message.
593 The regexp should match at end of buffer."
594 :group 'tramp
595 :type 'regexp)
596
597 (defcustom tramp-yesno-prompt-regexp
598 (concat
599 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
600 "\\s-*")
601 "Regular expression matching all yes/no queries which need to be confirmed.
602 The confirmation should be done with yes or no.
603 The regexp should match at end of buffer.
604 See also `tramp-yn-prompt-regexp'."
605 :group 'tramp
606 :type 'regexp)
607
608 (defcustom tramp-yn-prompt-regexp
609 (concat
610 (regexp-opt '("Store key in cache? (y/n)"
611 "Update cached key? (y/n, Return cancels connection)") t)
612 "\\s-*")
613 "Regular expression matching all y/n queries which need to be confirmed.
614 The confirmation should be done with y or n.
615 The regexp should match at end of buffer.
616 See also `tramp-yesno-prompt-regexp'."
617 :group 'tramp
618 :type 'regexp)
619
620 (defcustom tramp-terminal-prompt-regexp
621 (concat "\\("
622 "TERM = (.*)"
623 "\\|"
624 "Terminal type\\? \\[.*\\]"
625 "\\)\\s-*")
626 "Regular expression matching all terminal setting prompts.
627 The regexp should match at end of buffer.
628 The answer will be provided by `tramp-action-terminal', which see."
629 :group 'tramp
630 :type 'regexp)
631
632 (defcustom tramp-operation-not-permitted-regexp
633 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
634 (regexp-opt '("Operation not permitted") t))
635 "Regular expression matching keep-date problems in (s)cp operations.
636 Copying has been performed successfully already, so this message can
637 be ignored safely."
638 :group 'tramp
639 :type 'regexp)
640
641 (defcustom tramp-copy-failed-regexp
642 (concat "\\(.+: "
643 (regexp-opt '("Permission denied"
644 "not a regular file"
645 "is a directory"
646 "No such file or directory") t)
647 "\\)\\s-*")
648 "Regular expression matching copy problems in (s)cp operations."
649 :group 'tramp
650 :type 'regexp)
651
652 (defcustom tramp-process-alive-regexp
653 ""
654 "Regular expression indicating a process has finished.
655 In fact this expression is empty by intention, it will be used only to
656 check regularly the status of the associated process.
657 The answer will be provided by `tramp-action-process-alive',
658 `tramp-action-out-of-band', which see."
659 :group 'tramp
660 :type 'regexp)
661
662 (defconst tramp-temp-name-prefix "tramp."
663 "Prefix to use for temporary files.
664 If this is a relative file name (such as \"tramp.\"), it is considered
665 relative to the directory name returned by the function
666 `tramp-compat-temporary-file-directory' (which see). It may also be an
667 absolute file name; don't forget to include a prefix for the filename
668 part, though.")
669
670 (defconst tramp-temp-buffer-name " *tramp temp*"
671 "Buffer name for a temporary buffer.
672 It shall be used in combination with `generate-new-buffer-name'.")
673
674 (defvar tramp-temp-buffer-file-name nil
675 "File name of a persistent local temporary file.
676 Useful for \"rsync\" like methods.")
677 (make-variable-buffer-local 'tramp-temp-buffer-file-name)
678 (put 'tramp-temp-buffer-file-name 'permanent-local t)
679
680 ;; XEmacs is distributed with few Lisp packages. Further packages are
681 ;; installed using EFS. If we use a unified filename format, then
682 ;; Tramp is required in addition to EFS. (But why can't Tramp just
683 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
684 ;; just like before.) Another reason for using a separate filename
685 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
686 ;; Tramp only knows how to deal with `file-name-handler-alist', not
687 ;; the other places.
688
689 ;; Currently, we have the choice between 'ftp and 'sep.
690 ;;;###autoload
691 (defcustom tramp-syntax
692 (if (featurep 'xemacs) 'sep 'ftp)
693 "Tramp filename syntax to be used.
694
695 It can have the following values:
696
697 `ftp' -- Ange-FTP respective EFS like syntax (GNU Emacs default)
698 `sep' -- Syntax as defined for XEmacs."
699 :group 'tramp
700 :version "24.4"
701 :type `(choice (const :tag ,(if (featurep 'xemacs) "EFS" "Ange-FTP") ftp)
702 (const :tag "XEmacs" sep)))
703
704 (defconst tramp-prefix-format
705 (cond ((equal tramp-syntax 'ftp) "/")
706 ((equal tramp-syntax 'sep) "/[")
707 (t (error "Wrong `tramp-syntax' defined")))
708 "String matching the very beginning of Tramp file names.
709 Used in `tramp-make-tramp-file-name'.")
710
711 (defconst tramp-prefix-regexp
712 (concat "^" (regexp-quote tramp-prefix-format))
713 "Regexp matching the very beginning of Tramp file names.
714 Should always start with \"^\". Derived from `tramp-prefix-format'.")
715
716 (defconst tramp-method-regexp
717 "[a-zA-Z_0-9-]+"
718 "Regexp matching methods identifiers.")
719
720 (defconst tramp-postfix-method-format
721 (cond ((equal tramp-syntax 'ftp) ":")
722 ((equal tramp-syntax 'sep) "/")
723 (t (error "Wrong `tramp-syntax' defined")))
724 "String matching delimiter between method and user or host names.
725 Used in `tramp-make-tramp-file-name'.")
726
727 (defconst tramp-postfix-method-regexp
728 (regexp-quote tramp-postfix-method-format)
729 "Regexp matching delimiter between method and user or host names.
730 Derived from `tramp-postfix-method-format'.")
731
732 (defconst tramp-user-regexp "[^/|: \t]+"
733 "Regexp matching user names.")
734
735 ;;;###tramp-autoload
736 (defconst tramp-prefix-domain-format "%"
737 "String matching delimiter between user and domain names.")
738
739 ;;;###tramp-autoload
740 (defconst tramp-prefix-domain-regexp
741 (regexp-quote tramp-prefix-domain-format)
742 "Regexp matching delimiter between user and domain names.
743 Derived from `tramp-prefix-domain-format'.")
744
745 (defconst tramp-domain-regexp "[-a-zA-Z0-9_.]+"
746 "Regexp matching domain names.")
747
748 (defconst tramp-user-with-domain-regexp
749 (concat "\\(" tramp-user-regexp "\\)"
750 tramp-prefix-domain-regexp
751 "\\(" tramp-domain-regexp "\\)")
752 "Regexp matching user names with domain names.")
753
754 (defconst tramp-postfix-user-format "@"
755 "String matching delimiter between user and host names.
756 Used in `tramp-make-tramp-file-name'.")
757
758 (defconst tramp-postfix-user-regexp
759 (regexp-quote tramp-postfix-user-format)
760 "Regexp matching delimiter between user and host names.
761 Derived from `tramp-postfix-user-format'.")
762
763 (defconst tramp-host-regexp "[a-zA-Z0-9_.-]+"
764 "Regexp matching host names.")
765
766 (defconst tramp-prefix-ipv6-format
767 (cond ((equal tramp-syntax 'ftp) "[")
768 ((equal tramp-syntax 'sep) "")
769 (t (error "Wrong `tramp-syntax' defined")))
770 "String matching left hand side of IPv6 addresses.
771 Used in `tramp-make-tramp-file-name'.")
772
773 (defconst tramp-prefix-ipv6-regexp
774 (regexp-quote tramp-prefix-ipv6-format)
775 "Regexp matching left hand side of IPv6 addresses.
776 Derived from `tramp-prefix-ipv6-format'.")
777
778 ;; The following regexp is a bit sloppy. But it shall serve our
779 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
780 ;; "::ffff:192.168.0.1".
781 (defconst tramp-ipv6-regexp
782 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
783 "Regexp matching IPv6 addresses.")
784
785 (defconst tramp-postfix-ipv6-format
786 (cond ((equal tramp-syntax 'ftp) "]")
787 ((equal tramp-syntax 'sep) "")
788 (t (error "Wrong `tramp-syntax' defined")))
789 "String matching right hand side of IPv6 addresses.
790 Used in `tramp-make-tramp-file-name'.")
791
792 (defconst tramp-postfix-ipv6-regexp
793 (regexp-quote tramp-postfix-ipv6-format)
794 "Regexp matching right hand side of IPv6 addresses.
795 Derived from `tramp-postfix-ipv6-format'.")
796
797 (defconst tramp-prefix-port-format
798 (cond ((equal tramp-syntax 'ftp) "#")
799 ((equal tramp-syntax 'sep) "#")
800 (t (error "Wrong `tramp-syntax' defined")))
801 "String matching delimiter between host names and port numbers.")
802
803 (defconst tramp-prefix-port-regexp
804 (regexp-quote tramp-prefix-port-format)
805 "Regexp matching delimiter between host names and port numbers.
806 Derived from `tramp-prefix-port-format'.")
807
808 (defconst tramp-port-regexp "[0-9]+"
809 "Regexp matching port numbers.")
810
811 (defconst tramp-host-with-port-regexp
812 (concat "\\(" tramp-host-regexp "\\)"
813 tramp-prefix-port-regexp
814 "\\(" tramp-port-regexp "\\)")
815 "Regexp matching host names with port numbers.")
816
817 (defconst tramp-postfix-hop-format "|"
818 "String matching delimiter after ad-hoc hop definitions.")
819
820 (defconst tramp-postfix-hop-regexp
821 (regexp-quote tramp-postfix-hop-format)
822 "Regexp matching delimiter after ad-hoc hop definitions.
823 Derived from `tramp-postfix-hop-format'.")
824
825 (defconst tramp-postfix-host-format
826 (cond ((equal tramp-syntax 'ftp) ":")
827 ((equal tramp-syntax 'sep) "]")
828 (t (error "Wrong `tramp-syntax' defined")))
829 "String matching delimiter between host names and localnames.
830 Used in `tramp-make-tramp-file-name'.")
831
832 (defconst tramp-postfix-host-regexp
833 (regexp-quote tramp-postfix-host-format)
834 "Regexp matching delimiter between host names and localnames.
835 Derived from `tramp-postfix-host-format'.")
836
837 (defconst tramp-localname-regexp ".*$"
838 "Regexp matching localnames.")
839
840 ;;; File name format:
841
842 (defconst tramp-remote-file-name-spec-regexp
843 (concat
844 "\\(?:" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
845 "\\(?:" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
846 "\\(" "\\(?:" tramp-host-regexp "\\|"
847 tramp-prefix-ipv6-regexp "\\(?:" tramp-ipv6-regexp "\\)?"
848 tramp-postfix-ipv6-regexp "\\)"
849 "\\(?:" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?")
850 "Regular expression matching a Tramp file name between prefix and postfix.")
851
852 (defconst tramp-file-name-structure
853 (list
854 (concat
855 tramp-prefix-regexp
856 "\\(" "\\(?:" tramp-remote-file-name-spec-regexp
857 tramp-postfix-hop-regexp "\\)+" "\\)?"
858 tramp-remote-file-name-spec-regexp tramp-postfix-host-regexp
859 "\\(" tramp-localname-regexp "\\)")
860 5 6 7 8 1)
861 "List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
862 the Tramp file name structure.
863
864 The first element REGEXP is a regular expression matching a Tramp file
865 name. The regex should contain parentheses around the method name,
866 the user name, the host name, and the file name parts.
867
868 The second element METHOD is a number, saying which pair of
869 parentheses matches the method name. The third element USER is
870 similar, but for the user name. The fourth element HOST is similar,
871 but for the host name. The fifth element FILE is for the file name.
872 The last element HOP is the ad-hoc hop definition, which could be a
873 cascade of several hops.
874
875 These numbers are passed directly to `match-string', which see. That
876 means the opening parentheses are counted to identify the pair.
877
878 See also `tramp-file-name-regexp'.")
879
880 ;;;###autoload
881 (defconst tramp-file-name-regexp-unified
882 (if (memq system-type '(cygwin windows-nt))
883 "\\`/\\(\\[.*\\]\\|[^/|:]\\{2,\\}[^/|]*\\):"
884 "\\`/[^/|:][^/|]*:")
885 "Value for `tramp-file-name-regexp' for unified remoting.
886 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
887 Tramp. See `tramp-file-name-structure' for more explanations.
888
889 On W32 systems, the volume letter must be ignored.")
890
891 ;;;###autoload
892 (defconst tramp-file-name-regexp-separate "\\`/\\[.*\\]"
893 "Value for `tramp-file-name-regexp' for separate remoting.
894 XEmacs uses a separate filename syntax for Tramp and EFS.
895 See `tramp-file-name-structure' for more explanations.")
896
897 ;;;###autoload
898 (defconst tramp-file-name-regexp
899 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
900 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
901 (t (error "Wrong `tramp-syntax' defined")))
902 "Regular expression matching file names handled by Tramp.
903 This regexp should match Tramp file names but no other file names.
904 When tramp.el is loaded, this regular expression is prepended to
905 `file-name-handler-alist', and that is searched sequentially. Thus,
906 if the Tramp entry appears rather early in the `file-name-handler-alist'
907 and is a bit too general, then some files might be considered Tramp
908 files which are not really Tramp files.
909
910 Please note that the entry in `file-name-handler-alist' is made when
911 this file \(tramp.el) is loaded. This means that this variable must be set
912 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
913 updated after changing this variable.
914
915 Also see `tramp-file-name-structure'.")
916
917 ;;;###autoload
918 (defconst tramp-completion-file-name-regexp-unified
919 (if (memq system-type '(cygwin windows-nt))
920 "\\`/[^/]\\{2,\\}\\'" "\\`/[^/]*\\'")
921 "Value for `tramp-completion-file-name-regexp' for unified remoting.
922 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
923 See `tramp-file-name-structure' for more explanations.
924
925 On W32 systems, the volume letter must be ignored.")
926
927 ;;;###autoload
928 (defconst tramp-completion-file-name-regexp-separate
929 "\\`/\\([[][^]]*\\)?\\'"
930 "Value for `tramp-completion-file-name-regexp' for separate remoting.
931 XEmacs uses a separate filename syntax for Tramp and EFS.
932 See `tramp-file-name-structure' for more explanations.")
933
934 ;;;###autoload
935 (defconst tramp-completion-file-name-regexp
936 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
937 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
938 (t (error "Wrong `tramp-syntax' defined")))
939 "Regular expression matching file names handled by Tramp completion.
940 This regexp should match partial Tramp file names only.
941
942 Please note that the entry in `file-name-handler-alist' is made when
943 this file \(tramp.el) is loaded. This means that this variable must be set
944 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
945 updated after changing this variable.
946
947 Also see `tramp-file-name-structure'.")
948
949 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
950 ;; known to have a bug in `process-send-string'; some ssh connections appear
951 ;; to drop bytes when data is sent too quickly. There is also a connection
952 ;; buffer local variable, which is computed depending on remote host properties
953 ;; when `tramp-chunksize' is zero or nil.
954 (defcustom tramp-chunksize
955 (when (and (not (featurep 'xemacs))
956 (memq system-type '(hpux)))
957 500)
958 ;; Parentheses in docstring starting at beginning of line are escaped.
959 ;; Fontification is messed up when
960 ;; `open-paren-in-column-0-is-defun-start' set to t.
961 "If non-nil, chunksize for sending input to local process.
962 It is necessary only on systems which have a buggy `process-send-string'
963 implementation. The necessity, whether this variable must be set, can be
964 checked via the following code:
965
966 (with-temp-buffer
967 (let* ((user \"xxx\") (host \"yyy\")
968 (init 0) (step 50)
969 (sent init) (received init))
970 (while (= sent received)
971 (setq sent (+ sent step))
972 (erase-buffer)
973 (let ((proc (start-process (buffer-name) (current-buffer)
974 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
975 (when (memq (process-status proc) \\='(run open))
976 (process-send-string proc (make-string sent ?\\ ))
977 (process-send-eof proc)
978 (process-send-eof proc))
979 (while (not (progn (goto-char (point-min))
980 (re-search-forward \"\\\\w+\" (point-max) t)))
981 (accept-process-output proc 1))
982 (when (memq (process-status proc) \\='(run open))
983 (setq received (string-to-number (match-string 0)))
984 (delete-process proc)
985 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
986 (sit-for 0))))
987 (if (> sent (+ init step))
988 (message \"You should set `tramp-chunksize' to a maximum of %s\"
989 (- sent step))
990 (message \"Test does not work\")
991 (display-buffer (current-buffer))
992 (sit-for 30))))
993
994 In the Emacs normally running Tramp, evaluate the above code
995 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
996 respectively). You can do this, for example, by pasting it into
997 the `*scratch*' buffer and then hitting C-j with the cursor after the
998 last closing parenthesis. Note that it works only if you have configured
999 \"ssh\" to run without password query, see ssh-agent(1).
1000
1001 You will see the number of bytes sent successfully to the remote host.
1002 If that number exceeds 1000, you can stop the execution by hitting
1003 C-g, because your Emacs is likely clean.
1004
1005 When it is necessary to set `tramp-chunksize', you might consider to
1006 use an out-of-the-band method \(like \"scp\") instead of an internal one
1007 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1008 performance.
1009
1010 If your Emacs is buggy, the code stops and gives you an indication
1011 about the value `tramp-chunksize' should be set. Maybe you could just
1012 experiment a bit, e.g. changing the values of `init' and `step'
1013 in the third line of the code.
1014
1015 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1016 this variable to be set as well."
1017 :group 'tramp
1018 :type '(choice (const nil) integer))
1019
1020 ;; Logging in to a remote host normally requires obtaining a pty. But
1021 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1022 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1023 ;; for an override of the system default.
1024 (defcustom tramp-process-connection-type t
1025 "Overrides `process-connection-type' for connections from Tramp.
1026 Tramp binds `process-connection-type' to the value given here before
1027 opening a connection to a remote host."
1028 :group 'tramp
1029 :type '(choice (const nil) (const t) (const pty)))
1030
1031 (defcustom tramp-connection-timeout 60
1032 "Defines the max time to wait for establishing a connection (in seconds).
1033 This can be overwritten for different connection types in `tramp-methods'.
1034
1035 The timeout does not include the time reading a password."
1036 :group 'tramp
1037 :version "24.4"
1038 :type 'integer)
1039
1040 (defcustom tramp-connection-min-time-diff 5
1041 "Defines seconds between two consecutive connection attempts.
1042 This is necessary as self defense mechanism, in order to avoid
1043 yo-yo connection attempts when the remote host is unavailable.
1044
1045 A value of 0 or nil suppresses this check. This might be
1046 necessary, when several out-of-order copy operations are
1047 performed, or when several asynchronous processes will be started
1048 in a short time frame. In those cases it is recommended to
1049 let-bind this variable."
1050 :group 'tramp
1051 :version "24.4"
1052 :type '(choice (const nil) integer))
1053
1054 (defcustom tramp-completion-reread-directory-timeout 10
1055 "Defines seconds since last remote command before rereading a directory.
1056 A remote directory might have changed its contents. In order to
1057 make it visible during file name completion in the minibuffer,
1058 Tramp flushes its cache and rereads the directory contents when
1059 more than `tramp-completion-reread-directory-timeout' seconds
1060 have been gone since last remote command execution. A value of t
1061 would require an immediate reread during filename completion, nil
1062 means to use always cached values for the directory contents."
1063 :group 'tramp
1064 :type '(choice (const nil) (const t) integer))
1065
1066 ;;; Internal Variables:
1067
1068 (defvar tramp-current-method nil
1069 "Connection method for this *tramp* buffer.")
1070
1071 (defvar tramp-current-user nil
1072 "Remote login name for this *tramp* buffer.")
1073
1074 (defvar tramp-current-host nil
1075 "Remote host for this *tramp* buffer.")
1076
1077 (defvar tramp-current-connection nil
1078 "Last connection timestamp.")
1079
1080 ;;;###autoload
1081 (defconst tramp-completion-file-name-handler-alist
1082 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1083 (file-name-completion . tramp-completion-handle-file-name-completion))
1084 "Alist of completion handler functions.
1085 Used for file names matching `tramp-file-name-regexp'. Operations
1086 not mentioned here will be handled by Tramp's file name handler
1087 functions, or the normal Emacs functions.")
1088
1089 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1090 ;;;###tramp-autoload
1091 (defvar tramp-foreign-file-name-handler-alist nil
1092 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1093 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1094 calling HANDLER.")
1095
1096 ;;; Internal functions which must come first:
1097
1098 (defsubst tramp-user-error (vec-or-proc format &rest args)
1099 "Signal a pilot error."
1100 (apply
1101 'tramp-error vec-or-proc
1102 (if (fboundp 'user-error) 'user-error 'error) format args))
1103
1104 ;; Conversion functions between external representation and
1105 ;; internal data structure. Convenience functions for internal
1106 ;; data structure.
1107
1108 (defun tramp-get-method-parameter (vec param)
1109 "Return the method parameter PARAM.
1110 If VEC is a vector, check first in connection properties.
1111 Afterwards, check in `tramp-methods'. If the `tramp-methods'
1112 entry does not exist, return nil."
1113 (let ((hash-entry
1114 (tramp-compat-replace-regexp-in-string
1115 "^tramp-" "" (symbol-name param))))
1116 (if (tramp-connection-property-p vec hash-entry)
1117 ;; We use the cached property.
1118 (tramp-get-connection-property vec hash-entry nil)
1119 ;; Use the static value from `tramp-methods'.
1120 (let ((methods-entry
1121 (assoc param (assoc (tramp-file-name-method vec) tramp-methods))))
1122 (when methods-entry (cadr methods-entry))))))
1123
1124 (defun tramp-file-name-p (vec)
1125 "Check, whether VEC is a Tramp object."
1126 (and (vectorp vec) (= 5 (length vec))))
1127
1128 (defun tramp-file-name-method (vec)
1129 "Return method component of VEC."
1130 (and (tramp-file-name-p vec) (aref vec 0)))
1131
1132 (defun tramp-file-name-user (vec)
1133 "Return user component of VEC."
1134 (and (tramp-file-name-p vec) (aref vec 1)))
1135
1136 (defun tramp-file-name-host (vec)
1137 "Return host component of VEC."
1138 (and (tramp-file-name-p vec) (aref vec 2)))
1139
1140 (defun tramp-file-name-localname (vec)
1141 "Return localname component of VEC."
1142 (and (tramp-file-name-p vec) (aref vec 3)))
1143
1144 (defun tramp-file-name-hop (vec)
1145 "Return hop component of VEC."
1146 (and (tramp-file-name-p vec) (aref vec 4)))
1147
1148 ;; The user part of a Tramp file name vector can be of kind
1149 ;; "user%domain". Sometimes, we must extract these parts.
1150 (defun tramp-file-name-real-user (vec)
1151 "Return the user name of VEC without domain."
1152 (save-match-data
1153 (let ((user (tramp-file-name-user vec)))
1154 (if (and (stringp user)
1155 (string-match tramp-user-with-domain-regexp user))
1156 (match-string 1 user)
1157 user))))
1158
1159 (defun tramp-file-name-domain (vec)
1160 "Return the domain name of VEC."
1161 (save-match-data
1162 (let ((user (tramp-file-name-user vec)))
1163 (and (stringp user)
1164 (string-match tramp-user-with-domain-regexp user)
1165 (match-string 2 user)))))
1166
1167 ;; The host part of a Tramp file name vector can be of kind
1168 ;; "host#port". Sometimes, we must extract these parts.
1169 (defun tramp-file-name-real-host (vec)
1170 "Return the host name of VEC without port."
1171 (save-match-data
1172 (let ((host (tramp-file-name-host vec)))
1173 (if (and (stringp host)
1174 (string-match tramp-host-with-port-regexp host))
1175 (match-string 1 host)
1176 host))))
1177
1178 (defun tramp-file-name-port (vec)
1179 "Return the port number of VEC."
1180 (save-match-data
1181 (let ((method (tramp-file-name-method vec))
1182 (host (tramp-file-name-host vec)))
1183 (or (and (stringp host)
1184 (string-match tramp-host-with-port-regexp host)
1185 (string-to-number (match-string 2 host)))
1186 (tramp-get-method-parameter vec 'tramp-default-port)))))
1187
1188 ;;;###tramp-autoload
1189 (defun tramp-tramp-file-p (name)
1190 "Return t if NAME is a string with Tramp file name syntax."
1191 (save-match-data
1192 (and (stringp name)
1193 (string-match tramp-file-name-regexp name))))
1194
1195 ;; Obsoleted with Tramp 2.2.7.
1196 (defconst tramp-obsolete-methods
1197 '("ssh1" "ssh2" "scp1" "scp2" "scpc" "rsyncc" "plink1")
1198 "Obsolete methods.")
1199
1200 (defvar tramp-warned-obsolete-methods nil
1201 "Which methods the user has been warned to be obsolete.")
1202
1203 (defun tramp-find-method (method user host)
1204 "Return the right method string to use.
1205 This is METHOD, if non-nil. Otherwise, do a lookup in
1206 `tramp-default-method-alist'. It maps also obsolete methods to
1207 their replacement."
1208 (let ((result
1209 (or method
1210 (let ((choices tramp-default-method-alist)
1211 lmethod item)
1212 (while choices
1213 (setq item (pop choices))
1214 (when (and (string-match (or (nth 0 item) "") (or host ""))
1215 (string-match (or (nth 1 item) "") (or user "")))
1216 (setq lmethod (nth 2 item))
1217 (setq choices nil)))
1218 lmethod)
1219 tramp-default-method)))
1220 ;; This is needed for a transition period only.
1221 (when (member result tramp-obsolete-methods)
1222 (unless (member result tramp-warned-obsolete-methods)
1223 (if noninteractive
1224 (warn "Method %s is obsolete, using %s"
1225 result (substring result 0 -1))
1226 (unless (y-or-n-p (format "Method \"%s\" is obsolete, use \"%s\"? "
1227 result (substring result 0 -1)))
1228 (tramp-user-error nil "Method \"%s\" not supported" result)))
1229 (add-to-list 'tramp-warned-obsolete-methods result))
1230 ;; This works with the current set of `tramp-obsolete-methods'.
1231 ;; Must be improved, if their are more sophisticated replacements.
1232 (setq result (substring result 0 -1)))
1233 ;; We must mark, whether a default value has been used. Not
1234 ;; applicable for XEmacs.
1235 (if (or method (null result) (null (functionp 'propertize)))
1236 result
1237 (tramp-compat-funcall 'propertize result 'tramp-default t))))
1238
1239 (defun tramp-find-user (method user host)
1240 "Return the right user string to use.
1241 This is USER, if non-nil. Otherwise, do a lookup in
1242 `tramp-default-user-alist'."
1243 (let ((result
1244 (or user
1245 (let ((choices tramp-default-user-alist)
1246 luser item)
1247 (while choices
1248 (setq item (pop choices))
1249 (when (and (string-match (or (nth 0 item) "") (or method ""))
1250 (string-match (or (nth 1 item) "") (or host "")))
1251 (setq luser (nth 2 item))
1252 (setq choices nil)))
1253 luser)
1254 tramp-default-user)))
1255 ;; We must mark, whether a default value has been used. Not
1256 ;; applicable for XEmacs.
1257 (if (or user (null result) (null (functionp 'propertize)))
1258 result
1259 (tramp-compat-funcall 'propertize result 'tramp-default t))))
1260
1261 (defun tramp-find-host (method user host)
1262 "Return the right host string to use.
1263 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1264 (or (and (> (length host) 0) host)
1265 (let ((choices tramp-default-host-alist)
1266 lhost item)
1267 (while choices
1268 (setq item (pop choices))
1269 (when (and (string-match (or (nth 0 item) "") (or method ""))
1270 (string-match (or (nth 1 item) "") (or user "")))
1271 (setq lhost (nth 2 item))
1272 (setq choices nil)))
1273 lhost)
1274 tramp-default-host))
1275
1276 (defun tramp-check-proper-method-and-host (vec)
1277 "Check method and host name of VEC."
1278 (let ((method (tramp-file-name-method vec))
1279 (user (tramp-file-name-user vec))
1280 (host (tramp-file-name-host vec))
1281 (methods (mapcar 'car tramp-methods)))
1282 (when (and method (not (member method methods)))
1283 (tramp-cleanup-connection vec)
1284 (tramp-user-error vec "Unknown method \"%s\"" method))
1285 (when (and (equal tramp-syntax 'ftp) host
1286 (or (null method) (get-text-property 0 'tramp-default method))
1287 (or (null user) (get-text-property 0 'tramp-default user))
1288 (member host methods))
1289 (tramp-cleanup-connection vec)
1290 (tramp-user-error vec "Host name must not match method \"%s\"" host))))
1291
1292 (defun tramp-dissect-file-name (name &optional nodefault)
1293 "Return a `tramp-file-name' structure.
1294 The structure consists of remote method, remote user, remote host,
1295 localname (file name on remote host) and hop. If NODEFAULT is
1296 non-nil, the file name parts are not expanded to their default
1297 values."
1298 (save-match-data
1299 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
1300 (unless match (tramp-user-error nil "Not a Tramp file name: \"%s\"" name))
1301 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
1302 (user (match-string (nth 2 tramp-file-name-structure) name))
1303 (host (match-string (nth 3 tramp-file-name-structure) name))
1304 (localname (match-string (nth 4 tramp-file-name-structure) name))
1305 (hop (match-string (nth 5 tramp-file-name-structure) name)))
1306 (when host
1307 (when (string-match tramp-prefix-ipv6-regexp host)
1308 (setq host (replace-match "" nil t host)))
1309 (when (string-match tramp-postfix-ipv6-regexp host)
1310 (setq host (replace-match "" nil t host))))
1311 (if nodefault
1312 (vector method user host localname hop)
1313 (vector
1314 (tramp-find-method method user host)
1315 (tramp-find-user method user host)
1316 (tramp-find-host method user host)
1317 localname hop))))))
1318
1319 (defun tramp-buffer-name (vec)
1320 "A name for the connection buffer VEC."
1321 ;; We must use `tramp-file-name-real-host', because for gateway
1322 ;; methods the default port will be expanded later on, which would
1323 ;; tamper the name.
1324 (let ((method (tramp-file-name-method vec))
1325 (user (tramp-file-name-user vec))
1326 (host (tramp-file-name-real-host vec)))
1327 (if (not (zerop (length user)))
1328 (format "*tramp/%s %s@%s*" method user host)
1329 (format "*tramp/%s %s*" method host))))
1330
1331 (defun tramp-make-tramp-file-name (method user host localname &optional hop)
1332 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1333 When not nil, an optional HOP is prepended."
1334 (concat tramp-prefix-format hop
1335 (when (not (zerop (length method)))
1336 (concat method tramp-postfix-method-format))
1337 (when (not (zerop (length user)))
1338 (concat user tramp-postfix-user-format))
1339 (when host
1340 (if (string-match tramp-ipv6-regexp host)
1341 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
1342 host))
1343 tramp-postfix-host-format
1344 (when localname localname)))
1345
1346 (defun tramp-completion-make-tramp-file-name (method user host localname)
1347 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1348 It must not be a complete Tramp file name, but as long as there are
1349 necessary only. This function will be used in file name completion."
1350 (concat tramp-prefix-format
1351 (when (not (zerop (length method)))
1352 (concat method tramp-postfix-method-format))
1353 (when (not (zerop (length user)))
1354 (concat user tramp-postfix-user-format))
1355 (when (not (zerop (length host)))
1356 (concat
1357 (if (string-match tramp-ipv6-regexp host)
1358 (concat
1359 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
1360 host)
1361 tramp-postfix-host-format))
1362 (when localname localname)))
1363
1364 (defun tramp-get-buffer (vec)
1365 "Get the connection buffer to be used for VEC."
1366 (or (get-buffer (tramp-buffer-name vec))
1367 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
1368 (setq buffer-undo-list t)
1369 (setq default-directory
1370 (tramp-make-tramp-file-name
1371 (tramp-file-name-method vec)
1372 (tramp-file-name-user vec)
1373 (tramp-file-name-host vec)
1374 "/"))
1375 (current-buffer))))
1376
1377 (defun tramp-get-connection-buffer (vec)
1378 "Get the connection buffer to be used for VEC.
1379 In case a second asynchronous communication has been started, it is different
1380 from `tramp-get-buffer'."
1381 (or (tramp-get-connection-property vec "process-buffer" nil)
1382 (tramp-get-buffer vec)))
1383
1384 (defun tramp-get-connection-name (vec)
1385 "Get the connection name to be used for VEC.
1386 In case a second asynchronous communication has been started, it is different
1387 from the default one."
1388 (or (tramp-get-connection-property vec "process-name" nil)
1389 (tramp-buffer-name vec)))
1390
1391 (defun tramp-get-connection-process (vec)
1392 "Get the connection process to be used for VEC.
1393 In case a second asynchronous communication has been started, it is different
1394 from the default one."
1395 (get-process (tramp-get-connection-name vec)))
1396
1397 (defun tramp-debug-buffer-name (vec)
1398 "A name for the debug buffer for VEC."
1399 ;; We must use `tramp-file-name-real-host', because for gateway
1400 ;; methods the default port will be expanded later on, which would
1401 ;; tamper the name.
1402 (let ((method (tramp-file-name-method vec))
1403 (user (tramp-file-name-user vec))
1404 (host (tramp-file-name-real-host vec)))
1405 (if (not (zerop (length user)))
1406 (format "*debug tramp/%s %s@%s*" method user host)
1407 (format "*debug tramp/%s %s*" method host))))
1408
1409 (defconst tramp-debug-outline-regexp
1410 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1411 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1412
1413 (defun tramp-debug-outline-level ()
1414 "Return the depth to which a statement is nested in the outline.
1415 Point must be at the beginning of a header line.
1416
1417 The outline level is equal to the verbosity of the Tramp message."
1418 (1+ (string-to-number (match-string 1))))
1419
1420 (defun tramp-get-debug-buffer (vec)
1421 "Get the debug buffer for VEC."
1422 (with-current-buffer
1423 (get-buffer-create (tramp-debug-buffer-name vec))
1424 (when (bobp)
1425 (setq buffer-undo-list t)
1426 ;; So it does not get loaded while `outline-regexp' is let-bound.
1427 (require 'outline)
1428 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1429 ;; `outline-mode-hook'. We must prevent that local processes
1430 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1431 ;; Furthermore, `outline-regexp' must have the correct value
1432 ;; already, because it is used by `font-lock-compile-keywords'.
1433 (let ((default-directory (tramp-compat-temporary-file-directory))
1434 (outline-regexp tramp-debug-outline-regexp))
1435 (outline-mode))
1436 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
1437 (set (make-local-variable 'outline-level) 'tramp-debug-outline-level))
1438 (current-buffer)))
1439
1440 (defsubst tramp-debug-message (vec fmt-string &rest arguments)
1441 "Append message to debug buffer.
1442 Message is formatted with FMT-STRING as control string and the remaining
1443 ARGUMENTS to actually emit the message (if applicable)."
1444 (with-current-buffer (tramp-get-debug-buffer vec)
1445 (goto-char (point-max))
1446 ;; Headline.
1447 (when (bobp)
1448 (insert
1449 (format
1450 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
1451 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
1452 emacs-version tramp-version))
1453 (when (>= tramp-verbose 10)
1454 (insert
1455 (format
1456 "\n;; Location: %s Git: %s"
1457 (locate-library "tramp") (tramp-repository-get-version)))))
1458 (unless (bolp)
1459 (insert "\n"))
1460 ;; Timestamp.
1461 (let ((now (current-time)))
1462 (insert (format-time-string "%T." now))
1463 (insert (format "%06d " (nth 2 now))))
1464 ;; Calling Tramp function. We suppress compat and trace functions
1465 ;; from being displayed.
1466 (let ((btn 1) btf fn)
1467 (while (not fn)
1468 (setq btf (nth 1 (backtrace-frame btn)))
1469 (if (not btf)
1470 (setq fn "")
1471 (when (symbolp btf)
1472 (setq fn (symbol-name btf))
1473 (unless
1474 (and
1475 (string-match "^tramp" fn)
1476 (not
1477 (string-match
1478 (concat
1479 "^"
1480 (regexp-opt
1481 '("tramp-backtrace"
1482 "tramp-compat-condition-case-unless-debug"
1483 "tramp-compat-funcall"
1484 "tramp-compat-with-temp-message"
1485 "tramp-condition-case-unless-debug"
1486 "tramp-debug-message"
1487 "tramp-error"
1488 "tramp-error-with-buffer"
1489 "tramp-message"
1490 "tramp-user-error")
1491 t)
1492 "$")
1493 fn)))
1494 (setq fn nil)))
1495 (setq btn (1+ btn))))
1496 ;; The following code inserts filename and line number. Should
1497 ;; be inactive by default, because it is time consuming.
1498 ; (let ((ffn (find-function-noselect (intern fn))))
1499 ; (insert
1500 ; (format
1501 ; "%s:%d: "
1502 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1503 ; (with-current-buffer (car ffn)
1504 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1505 (insert (format "%s " fn)))
1506 ;; The message.
1507 (insert (apply #'format-message fmt-string arguments))))
1508
1509 (defvar tramp-message-show-message t
1510 "Show Tramp message in the minibuffer.
1511 This variable is used to disable messages from `tramp-error'.
1512 The messages are visible anyway, because an error is raised.")
1513
1514 (defsubst tramp-message (vec-or-proc level fmt-string &rest arguments)
1515 "Emit a message depending on verbosity level.
1516 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1517 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1518 less than LEVEL. The message is emitted only if `tramp-verbose' is
1519 greater than or equal to LEVEL.
1520
1521 The message is also logged into the debug buffer when `tramp-verbose'
1522 is greater than or equal 4.
1523
1524 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1525 control string and the remaining ARGUMENTS to actually emit the message (if
1526 applicable)."
1527 (ignore-errors
1528 (when (<= level tramp-verbose)
1529 ;; Match data must be preserved!
1530 (save-match-data
1531 ;; Display only when there is a minimum level.
1532 (when (and tramp-message-show-message (<= level 3))
1533 (apply 'message
1534 (concat
1535 (cond
1536 ((= level 0) "")
1537 ((= level 1) "")
1538 ((= level 2) "Warning: ")
1539 (t "Tramp: "))
1540 fmt-string)
1541 arguments))
1542 ;; Log only when there is a minimum level.
1543 (when (>= tramp-verbose 4)
1544 ;; Translate proc to vec.
1545 (when (processp vec-or-proc)
1546 (let ((tramp-verbose 0))
1547 (setq vec-or-proc
1548 (tramp-get-connection-property vec-or-proc "vector" nil))))
1549 ;; Append connection buffer for error messages.
1550 (when (= level 1)
1551 (let ((tramp-verbose 0))
1552 (with-current-buffer (tramp-get-connection-buffer vec-or-proc)
1553 (setq fmt-string (concat fmt-string "\n%s")
1554 arguments (append arguments (list (buffer-string)))))))
1555 ;; Do it.
1556 (when (vectorp vec-or-proc)
1557 (apply 'tramp-debug-message
1558 vec-or-proc
1559 (concat (format "(%d) # " level) fmt-string)
1560 arguments)))))))
1561
1562 (defsubst tramp-backtrace (&optional vec-or-proc)
1563 "Dump a backtrace into the debug buffer.
1564 If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
1565 function is meant for debugging purposes."
1566 (if vec-or-proc
1567 (tramp-message vec-or-proc 10 "\n%s" (with-output-to-string (backtrace)))
1568 (if (>= tramp-verbose 10)
1569 (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
1570
1571 (defsubst tramp-error (vec-or-proc signal fmt-string &rest arguments)
1572 "Emit an error.
1573 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1574 signal identifier to be raised, remaining arguments passed to
1575 `tramp-message'. Finally, signal SIGNAL is raised."
1576 (let (tramp-message-show-message)
1577 (tramp-backtrace vec-or-proc)
1578 (when vec-or-proc
1579 (tramp-message
1580 vec-or-proc 1 "%s"
1581 (error-message-string
1582 (list signal
1583 (get signal 'error-message)
1584 (apply #'format-message fmt-string arguments)))))
1585 (signal signal (list (apply #'format-message fmt-string arguments)))))
1586
1587 (defsubst tramp-error-with-buffer
1588 (buf vec-or-proc signal fmt-string &rest arguments)
1589 "Emit an error, and show BUF.
1590 If BUF is nil, show the connection buf. Wait for 30\", or until
1591 an input event arrives. The other arguments are passed to `tramp-error'."
1592 (save-window-excursion
1593 (let* ((buf (or (and (bufferp buf) buf)
1594 (and (processp vec-or-proc) (process-buffer vec-or-proc))
1595 (and (vectorp vec-or-proc)
1596 (tramp-get-connection-buffer vec-or-proc))))
1597 (vec (or (and (vectorp vec-or-proc) vec-or-proc)
1598 (and buf (with-current-buffer buf
1599 (tramp-dissect-file-name default-directory))))))
1600 (unwind-protect
1601 (apply 'tramp-error vec-or-proc signal fmt-string arguments)
1602 ;; Save exit.
1603 (when (and buf
1604 tramp-message-show-message
1605 (not (zerop tramp-verbose))
1606 (not (tramp-completion-mode-p))
1607 ;; Show only when Emacs has started already.
1608 (current-message))
1609 (let ((enable-recursive-minibuffers t))
1610 ;; `tramp-error' does not show messages. So we must do it
1611 ;; ourselves.
1612 (apply 'message fmt-string arguments)
1613 ;; Show buffer.
1614 (pop-to-buffer buf)
1615 (discard-input)
1616 (sit-for 30)))
1617 ;; Reset timestamp. It would be wrong after waiting for a while.
1618 (when (equal (butlast (append vec nil) 2)
1619 (car tramp-current-connection))
1620 (setcdr tramp-current-connection (current-time)))))))
1621
1622 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1623 "Parse a Tramp filename and make components available in the body.
1624
1625 First arg FILENAME is evaluated and dissected into its components.
1626 Second arg VAR is a symbol. It is used as a variable name to hold
1627 the filename structure. It is also used as a prefix for the variables
1628 holding the components. For example, if VAR is the symbol `foo', then
1629 `foo' will be bound to the whole structure, `foo-method' will be bound to
1630 the method component, and so on for `foo-user', `foo-host', `foo-localname',
1631 `foo-hop'.
1632
1633 Remaining args are Lisp expressions to be evaluated (inside an implicit
1634 `progn').
1635
1636 If VAR is nil, then we bind `v' to the structure and `method', `user',
1637 `host', `localname', `hop' to the components."
1638 (let ((bindings
1639 (mapcar (lambda (elem)
1640 `(,(if var (intern (format "%s-%s" var elem)) elem)
1641 (,(intern (format "tramp-file-name-%s" elem))
1642 ,(or var 'v))))
1643 '(method user host localname hop))))
1644 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1645 ,@bindings)
1646 ;; We don't know which of those vars will be used, so we bind them all,
1647 ;; and then add here a dummy use of all those variables, so we don't get
1648 ;; flooded by warnings about those vars `body' didn't use.
1649 (ignore ,@(mapcar #'car bindings))
1650 ,@body)))
1651
1652 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1653 (put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
1654 (tramp-compat-font-lock-add-keywords
1655 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
1656
1657 (defun tramp-progress-reporter-update (reporter &optional value)
1658 (let* ((parameters (cdr reporter))
1659 (message (aref parameters 3)))
1660 (when (string-match message (or (current-message) ""))
1661 (tramp-compat-funcall 'progress-reporter-update reporter value))))
1662
1663 (defmacro with-tramp-progress-reporter (vec level message &rest body)
1664 "Executes BODY, spinning a progress reporter with MESSAGE.
1665 If LEVEL does not fit for visible messages, there are only traces
1666 without a visible progress reporter."
1667 (declare (indent 3) (debug t))
1668 `(progn
1669 (tramp-message ,vec ,level "%s..." ,message)
1670 (let ((cookie "failed")
1671 (tm
1672 ;; We start a pulsing progress reporter after 3 seconds. Feature
1673 ;; introduced in Emacs 24.1.
1674 (when (and tramp-message-show-message
1675 ;; Display only when there is a minimum level.
1676 (<= ,level (min tramp-verbose 3)))
1677 (ignore-errors
1678 (let ((pr (tramp-compat-funcall
1679 #'make-progress-reporter ,message)))
1680 (when pr
1681 (run-at-time 3 0.1
1682 #'tramp-progress-reporter-update pr)))))))
1683 (unwind-protect
1684 ;; Execute the body.
1685 (prog1 (progn ,@body) (setq cookie "done"))
1686 ;; Stop progress reporter.
1687 (if tm (tramp-compat-funcall 'cancel-timer tm))
1688 (tramp-message ,vec ,level "%s...%s" ,message cookie)))))
1689
1690 (tramp-compat-font-lock-add-keywords
1691 'emacs-lisp-mode '("\\<with-tramp-progress-reporter\\>"))
1692
1693 (defmacro with-tramp-file-property (vec file property &rest body)
1694 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
1695 FILE must be a local file name on a connection identified via VEC."
1696 `(if (file-name-absolute-p ,file)
1697 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
1698 (when (eq value 'undef)
1699 ;; We cannot pass @body as parameter to
1700 ;; `tramp-set-file-property' because it mangles our
1701 ;; debug messages.
1702 (setq value (progn ,@body))
1703 (tramp-set-file-property ,vec ,file ,property value))
1704 value)
1705 ,@body))
1706
1707 (put 'with-tramp-file-property 'lisp-indent-function 3)
1708 (put 'with-tramp-file-property 'edebug-form-spec t)
1709 (tramp-compat-font-lock-add-keywords
1710 'emacs-lisp-mode '("\\<with-tramp-file-property\\>"))
1711
1712 (defmacro with-tramp-connection-property (key property &rest body)
1713 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
1714 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
1715 (when (eq value 'undef)
1716 ;; We cannot pass ,@body as parameter to
1717 ;; `tramp-set-connection-property' because it mangles our debug
1718 ;; messages.
1719 (setq value (progn ,@body))
1720 (tramp-set-connection-property ,key ,property value))
1721 value))
1722
1723 (put 'with-tramp-connection-property 'lisp-indent-function 2)
1724 (put 'with-tramp-connection-property 'edebug-form-spec t)
1725 (tramp-compat-font-lock-add-keywords
1726 'emacs-lisp-mode '("\\<with-tramp-connection-property\\>"))
1727
1728 (defun tramp-drop-volume-letter (name)
1729 "Cut off unnecessary drive letter from file NAME.
1730 The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
1731 locally on a remote file name. When the local system is a W32 system
1732 but the remote system is Unix, this introduces a superfluous drive
1733 letter into the file name. This function removes it."
1734 (save-match-data
1735 (if (string-match "\\`[a-zA-Z]:/" name)
1736 (replace-match "/" nil t name)
1737 name)))
1738
1739 ;;; Config Manipulation Functions:
1740
1741 ;;;###tramp-autoload
1742 (defun tramp-set-completion-function (method function-list)
1743 "Sets the list of completion functions for METHOD.
1744 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1745 The FUNCTION is intended to parse FILE according its syntax.
1746 It might be a predefined FUNCTION, or a user defined FUNCTION.
1747 For the list of predefined FUNCTIONs see `tramp-completion-function-alist'.
1748
1749 Example:
1750
1751 (tramp-set-completion-function
1752 \"ssh\"
1753 \\='((tramp-parse-sconfig \"/etc/ssh_config\")
1754 (tramp-parse-sconfig \"~/.ssh/config\")))"
1755
1756 (let ((r function-list)
1757 (v function-list))
1758 (setq tramp-completion-function-alist
1759 (delete (assoc method tramp-completion-function-alist)
1760 tramp-completion-function-alist))
1761
1762 (while v
1763 ;; Remove double entries.
1764 (when (member (car v) (cdr v))
1765 (setcdr v (delete (car v) (cdr v))))
1766 ;; Check for function and file or registry key.
1767 (unless (and (functionp (nth 0 (car v)))
1768 (cond
1769 ;; Windows registry.
1770 ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
1771 (and (memq system-type '(cygwin windows-nt))
1772 (zerop
1773 (tramp-call-process
1774 v "reg" nil nil nil "query" (nth 1 (car v))))))
1775 ;; Zeroconf service type.
1776 ((string-match
1777 "^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v))))
1778 ;; Configuration file.
1779 (t (file-exists-p (nth 1 (car v))))))
1780 (setq r (delete (car v) r)))
1781 (setq v (cdr v)))
1782
1783 (when r
1784 (add-to-list 'tramp-completion-function-alist
1785 (cons method r)))))
1786
1787 (defun tramp-get-completion-function (method)
1788 "Returns a list of completion functions for METHOD.
1789 For definition of that list see `tramp-set-completion-function'."
1790 (cons
1791 ;; Hosts visited once shall be remembered.
1792 `(tramp-parse-connection-properties ,method)
1793 ;; The method related defaults.
1794 (cdr (assoc method tramp-completion-function-alist))))
1795
1796
1797 ;;; Fontification of `read-file-name':
1798
1799 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
1800 (defvar tramp-rfn-eshadow-overlay)
1801 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
1802
1803 (defun tramp-rfn-eshadow-setup-minibuffer ()
1804 "Set up a minibuffer for `file-name-shadow-mode'.
1805 Adds another overlay hiding filename parts according to Tramp's
1806 special handling of `substitute-in-file-name'."
1807 (when (symbol-value 'minibuffer-completing-file-name)
1808 (setq tramp-rfn-eshadow-overlay
1809 (tramp-compat-funcall
1810 'make-overlay
1811 (tramp-compat-funcall 'minibuffer-prompt-end)
1812 (tramp-compat-funcall 'minibuffer-prompt-end)))
1813 ;; Copy rfn-eshadow-overlay properties.
1814 (let ((props (tramp-compat-funcall
1815 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
1816 (while props
1817 ;; The `field' property prevents correct minibuffer
1818 ;; completion; we exclude it.
1819 (if (not (eq (car props) 'field))
1820 (tramp-compat-funcall
1821 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))
1822 (pop props) (pop props))))))
1823
1824 (when (boundp 'rfn-eshadow-setup-minibuffer-hook)
1825 (add-hook 'rfn-eshadow-setup-minibuffer-hook
1826 'tramp-rfn-eshadow-setup-minibuffer)
1827 (add-hook 'tramp-unload-hook
1828 (lambda ()
1829 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
1830 'tramp-rfn-eshadow-setup-minibuffer))))
1831
1832 (defconst tramp-rfn-eshadow-update-overlay-regexp
1833 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
1834
1835 (defun tramp-rfn-eshadow-update-overlay ()
1836 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
1837 This is intended to be used as a minibuffer `post-command-hook' for
1838 `file-name-shadow-mode'; the minibuffer should have already
1839 been set up by `rfn-eshadow-setup-minibuffer'."
1840 ;; In remote files name, there is a shadowing just for the local part.
1841 (ignore-errors
1842 (let ((end (or (tramp-compat-funcall
1843 'overlay-end (symbol-value 'rfn-eshadow-overlay))
1844 (tramp-compat-funcall 'minibuffer-prompt-end)))
1845 ;; We do not want to send any remote command.
1846 (non-essential t))
1847 (when
1848 (tramp-tramp-file-p
1849 (tramp-compat-funcall
1850 'buffer-substring-no-properties end (point-max)))
1851 (save-excursion
1852 (save-restriction
1853 (narrow-to-region
1854 (1+ (or (string-match
1855 tramp-rfn-eshadow-update-overlay-regexp
1856 (buffer-string) end)
1857 end))
1858 (point-max))
1859 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
1860 (rfn-eshadow-update-overlay-hook nil)
1861 file-name-handler-alist)
1862 (tramp-compat-funcall
1863 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
1864 (tramp-compat-funcall 'rfn-eshadow-update-overlay))))))))
1865
1866 (when (boundp 'rfn-eshadow-update-overlay-hook)
1867 (add-hook 'rfn-eshadow-update-overlay-hook
1868 'tramp-rfn-eshadow-update-overlay)
1869 (add-hook 'tramp-unload-hook
1870 (lambda ()
1871 (remove-hook 'rfn-eshadow-update-overlay-hook
1872 'tramp-rfn-eshadow-update-overlay))))
1873
1874 ;; Inodes don't exist for some file systems. Therefore we must
1875 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
1876 ;; applied might be not so efficient (Ange-FTP uses hashes). But
1877 ;; performance isn't the major issue given that file transfer will
1878 ;; take time.
1879 (defvar tramp-inodes 0
1880 "Keeps virtual inodes numbers.")
1881
1882 ;; Devices must distinguish physical file systems. The device numbers
1883 ;; provided by "lstat" aren't unique, because we operate on different hosts.
1884 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
1885 ;; EFS use device number "-1". In order to be different, we use device number
1886 ;; (-1 . x), whereby "x" is unique for a given (method user host).
1887 (defvar tramp-devices 0
1888 "Keeps virtual device numbers.")
1889
1890 (defun tramp-default-file-modes (filename)
1891 "Return file modes of FILENAME as integer.
1892 If the file modes of FILENAME cannot be determined, return the
1893 value of `default-file-modes', without execute permissions."
1894 (or (file-modes filename)
1895 (logand (default-file-modes) (tramp-compat-octal-to-decimal "0666"))))
1896
1897 (defun tramp-replace-environment-variables (filename)
1898 "Replace environment variables in FILENAME.
1899 Return the string with the replaced variables."
1900 (or (ignore-errors
1901 (tramp-compat-funcall 'substitute-env-vars filename 'only-defined))
1902 ;; We need an own implementation.
1903 (save-match-data
1904 (let ((idx (string-match "$\\(\\w+\\)" filename)))
1905 ;; `$' is coded as `$$'.
1906 (when (and idx
1907 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
1908 (getenv (match-string 1 filename)))
1909 (setq filename
1910 (replace-match
1911 (substitute-in-file-name (match-string 0 filename))
1912 t nil filename)))
1913 filename))))
1914
1915 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
1916 ;; which calls corresponding functions (see minibuf.el).
1917 (when (fboundp 'minibuffer-electric-separator)
1918 (mapc
1919 (lambda (x)
1920 (eval
1921 `(defadvice ,x
1922 (around ,(intern (format "tramp-advice-%s" x)) activate)
1923 "Invoke `substitute-in-file-name' for Tramp files."
1924 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
1925 (tramp-tramp-file-p (buffer-substring)))
1926 ;; We don't need to handle `last-input-event', because
1927 ;; due to the key map we know it must be ?/ or ?~.
1928 (let ((s (concat (buffer-substring (point-min) (point))
1929 (string last-command-char))))
1930 (delete-region (point-min) (point))
1931 (insert (substitute-in-file-name s))
1932 (setq ad-return-value last-command-char))
1933 ad-do-it)))
1934 (eval
1935 `(add-hook
1936 'tramp-unload-hook
1937 (lambda ()
1938 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
1939 (ad-activate ',x)))))
1940
1941 '(minibuffer-electric-separator
1942 minibuffer-electric-tilde)))
1943
1944 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
1945 "Like `find-operation-coding-system' for Tramp filenames.
1946 Tramp's `insert-file-contents' and `write-region' work over
1947 temporary file names. If `file-coding-system-alist' contains an
1948 expression, which matches more than the file name suffix, the
1949 coding system might not be determined. This function repairs it."
1950 (let (result)
1951 (dolist (elt file-coding-system-alist result)
1952 (when (and (consp elt) (string-match (car elt) filename))
1953 ;; We found a matching entry in `file-coding-system-alist'.
1954 ;; So we add a similar entry, but with the temporary file name
1955 ;; as regexp.
1956 (add-to-list
1957 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
1958
1959 (defun tramp-run-real-handler (operation args)
1960 "Invoke normal file name handler for OPERATION.
1961 First arg specifies the OPERATION, second arg is a list of arguments to
1962 pass to the OPERATION."
1963 (let* ((inhibit-file-name-handlers
1964 `(tramp-file-name-handler
1965 tramp-vc-file-name-handler
1966 tramp-completion-file-name-handler
1967 cygwin-mount-name-hook-function
1968 cygwin-mount-map-drive-hook-function
1969 .
1970 ,(and (eq inhibit-file-name-operation operation)
1971 inhibit-file-name-handlers)))
1972 (inhibit-file-name-operation operation))
1973 (apply operation args)))
1974
1975 ;;;###autoload
1976 (progn (defun tramp-completion-run-real-handler (operation args)
1977 "Invoke `tramp-file-name-handler' for OPERATION.
1978 First arg specifies the OPERATION, second arg is a list of arguments to
1979 pass to the OPERATION."
1980 (let* ((inhibit-file-name-handlers
1981 `(tramp-completion-file-name-handler
1982 cygwin-mount-name-hook-function
1983 cygwin-mount-map-drive-hook-function
1984 .
1985 ,(and (eq inhibit-file-name-operation operation)
1986 inhibit-file-name-handlers)))
1987 (inhibit-file-name-operation operation))
1988 (apply operation args))))
1989
1990 ;; We handle here all file primitives. Most of them have the file
1991 ;; name as first parameter; nevertheless we check for them explicitly
1992 ;; in order to be signaled if a new primitive appears. This
1993 ;; scenario is needed because there isn't a way to decide by
1994 ;; syntactical means whether a foreign method must be called. It would
1995 ;; ease the life if `file-name-handler-alist' would support a decision
1996 ;; function as well but regexp only.
1997 (defun tramp-file-name-for-operation (operation &rest args)
1998 "Return file name related to OPERATION file primitive.
1999 ARGS are the arguments OPERATION has been called with."
2000 (cond
2001 ;; FILE resp DIRECTORY.
2002 ((member operation
2003 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
2004 'delete-file 'diff-latest-backup-file 'directory-file-name
2005 'directory-files 'directory-files-and-attributes
2006 'dired-compress-file 'dired-uncache
2007 'file-accessible-directory-p 'file-attributes
2008 'file-directory-p 'file-executable-p 'file-exists-p
2009 'file-local-copy 'file-modes
2010 'file-name-as-directory 'file-name-directory
2011 'file-name-nondirectory 'file-name-sans-versions
2012 'file-ownership-preserved-p 'file-readable-p
2013 'file-regular-p 'file-remote-p 'file-symlink-p 'file-truename
2014 'file-writable-p 'find-backup-file-name 'find-file-noselect
2015 'get-file-buffer 'insert-directory 'insert-file-contents
2016 'load 'make-directory 'make-directory-internal
2017 'set-file-modes 'substitute-in-file-name
2018 'unhandled-file-name-directory 'vc-registered
2019 ;; Emacs 22+ only.
2020 'set-file-times
2021 ;; Emacs 24+ only.
2022 'file-acl 'file-notify-add-watch
2023 'file-selinux-context 'set-file-acl 'set-file-selinux-context
2024 ;; XEmacs only.
2025 'abbreviate-file-name 'create-file-buffer
2026 'dired-file-modtime 'dired-make-compressed-filename
2027 'dired-recursive-delete-directory 'dired-set-file-modtime
2028 'dired-shell-unhandle-file-name 'dired-uucode-file
2029 'insert-file-contents-literally 'make-temp-name 'recover-file
2030 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
2031 (if (file-name-absolute-p (nth 0 args))
2032 (nth 0 args)
2033 (expand-file-name (nth 0 args))))
2034 ;; FILE DIRECTORY resp FILE1 FILE2.
2035 ((member operation
2036 (list 'add-name-to-file 'copy-file 'expand-file-name
2037 'file-name-all-completions 'file-name-completion
2038 'file-newer-than-file-p 'make-symbolic-link 'rename-file
2039 ;; Emacs 23+ only.
2040 'copy-directory
2041 ;; Emacs 24+ only.
2042 'file-equal-p 'file-in-directory-p
2043 ;; XEmacs only.
2044 'dired-make-relative-symlink
2045 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
2046 (save-match-data
2047 (cond
2048 ((tramp-tramp-file-p (nth 0 args)) (nth 0 args))
2049 ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
2050 (t (buffer-file-name (current-buffer))))))
2051 ;; START END FILE.
2052 ((eq operation 'write-region)
2053 (nth 2 args))
2054 ;; BUFFER.
2055 ((member operation
2056 (list 'set-visited-file-modtime 'verify-visited-file-modtime
2057 ;; Emacs 22+ only.
2058 'make-auto-save-file-name
2059 ;; XEmacs only.
2060 'backup-buffer))
2061 (buffer-file-name
2062 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
2063 ;; COMMAND.
2064 ((member operation
2065 (list ;; not in Emacs 23+.
2066 'dired-call-process
2067 ;; Emacs only.
2068 'shell-command
2069 ;; Emacs 22+ only.
2070 'process-file
2071 ;; Emacs 23+ only.
2072 'start-file-process
2073 ;; XEmacs only.
2074 'dired-print-file 'dired-shell-call-process))
2075 default-directory)
2076 ;; PROC.
2077 ((member operation (list 'file-notify-rm-watch 'file-notify-valid-p))
2078 (when (processp (nth 0 args))
2079 (with-current-buffer (process-buffer (nth 0 args))
2080 default-directory)))
2081 ;; Unknown file primitive.
2082 (t (error "unknown file I/O primitive: %s" operation))))
2083
2084 (defun tramp-find-foreign-file-name-handler (filename)
2085 "Return foreign file name handler if exists."
2086 (when (tramp-tramp-file-p filename)
2087 (let ((v (tramp-dissect-file-name filename t))
2088 (handler tramp-foreign-file-name-handler-alist)
2089 elt res)
2090 ;; When we are not fully sure that filename completion is safe,
2091 ;; we should not return a handler.
2092 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
2093 (and (tramp-file-name-host v)
2094 (not (member (tramp-file-name-host v)
2095 (mapcar 'car tramp-methods))))
2096 (not (tramp-completion-mode-p)))
2097 (while handler
2098 (setq elt (car handler)
2099 handler (cdr handler))
2100 (when (funcall (car elt) filename)
2101 (setq handler nil
2102 res (cdr elt))))
2103 res))))
2104
2105 (defvar tramp-debug-on-error nil
2106 "Like `debug-on-error' but used Tramp internal.")
2107
2108 (defmacro tramp-condition-case-unless-debug
2109 (var bodyform &rest handlers)
2110 "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
2111 `(let ((debug-on-error tramp-debug-on-error))
2112 (tramp-compat-condition-case-unless-debug ,var ,bodyform ,@handlers)))
2113
2114 ;; Main function.
2115 (defun tramp-file-name-handler (operation &rest args)
2116 "Invoke Tramp file name handler.
2117 Falls back to normal file name handler if no Tramp file name handler exists."
2118 (if tramp-mode
2119 (save-match-data
2120 (let* ((filename
2121 (tramp-replace-environment-variables
2122 (apply 'tramp-file-name-for-operation operation args)))
2123 (completion (tramp-completion-mode-p))
2124 (foreign (tramp-find-foreign-file-name-handler filename)))
2125 (with-parsed-tramp-file-name filename nil
2126 ;; Call the backend function.
2127 (if foreign
2128 (tramp-condition-case-unless-debug err
2129 (let ((sf (symbol-function foreign))
2130 result)
2131 ;; Some packages set the default directory to a
2132 ;; remote path, before respective Tramp packages
2133 ;; are already loaded. This results in
2134 ;; recursive loading. Therefore, we load the
2135 ;; Tramp packages locally.
2136 (when (and (listp sf) (eq (car sf) 'autoload))
2137 (let ((default-directory
2138 (tramp-compat-temporary-file-directory)))
2139 (load (cadr sf) 'noerror 'nomessage)))
2140 ;; If `non-essential' is non-nil, Tramp shall
2141 ;; not open a new connection.
2142 ;; If Tramp detects that it shouldn't continue
2143 ;; to work, it throws the `suppress' event.
2144 ;; This could happen for example, when Tramp
2145 ;; tries to open the same connection twice in a
2146 ;; short time frame.
2147 ;; In both cases, we try the default handler then.
2148 (setq result
2149 (catch 'non-essential
2150 (catch 'suppress
2151 (apply foreign operation args))))
2152 (cond
2153 ((eq result 'non-essential)
2154 (tramp-message
2155 v 5 "Non-essential received in operation %s"
2156 (cons operation args))
2157 (tramp-run-real-handler operation args))
2158 ((eq result 'suppress)
2159 (let (tramp-message-show-message)
2160 (tramp-message
2161 v 1 "Suppress received in operation %s"
2162 (cons operation args))
2163 (tramp-cleanup-connection v t)
2164 (tramp-run-real-handler operation args)))
2165 (t result)))
2166
2167 ;; Trace that somebody has interrupted the operation.
2168 ((debug quit)
2169 (let (tramp-message-show-message)
2170 (tramp-message
2171 v 1 "Interrupt received in operation %s"
2172 (cons operation args)))
2173 ;; Propagate the quit signal.
2174 (signal (car err) (cdr err)))
2175
2176 ;; When we are in completion mode, some failed
2177 ;; operations shall return at least a default value
2178 ;; in order to give the user a chance to correct the
2179 ;; file name in the minibuffer.
2180 ;; In order to get a full backtrace, one could apply
2181 ;; (setq tramp-debug-on-error t)
2182 (error
2183 (cond
2184 ((and completion (zerop (length localname))
2185 (memq operation '(file-exists-p file-directory-p)))
2186 t)
2187 ((and completion (zerop (length localname))
2188 (memq operation
2189 '(expand-file-name file-name-as-directory)))
2190 filename)
2191 ;; Propagate the error.
2192 (t (signal (car err) (cdr err))))))
2193
2194 ;; Nothing to do for us.
2195 (tramp-run-real-handler operation args)))))
2196
2197 ;; When `tramp-mode' is not enabled, we don't do anything.
2198 (tramp-run-real-handler operation args)))
2199
2200 ;; In Emacs, there is some concurrency due to timers. If a timer
2201 ;; interrupts Tramp and wishes to use the same connection buffer as
2202 ;; the "main" Emacs, then garbage might occur in the connection
2203 ;; buffer. Therefore, we need to make sure that a timer does not use
2204 ;; the same connection buffer as the "main" Emacs. We implement a
2205 ;; cheap global lock, instead of locking each connection buffer
2206 ;; separately. The global lock is based on two variables,
2207 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
2208 ;; (with setq) to indicate a lock. But Tramp also calls itself during
2209 ;; processing of a single file operation, so we need to allow
2210 ;; recursive calls. That's where the `tramp-locker' variable comes in
2211 ;; -- it is let-bound to t during the execution of the current
2212 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
2213 ;; then we should just proceed because we have been called
2214 ;; recursively. But if `tramp-locker' is nil, then we are a timer
2215 ;; interrupting the "main" Emacs, and then we signal an error.
2216
2217 (defvar tramp-locked nil
2218 "If non-nil, then Tramp is currently busy.
2219 Together with `tramp-locker', this implements a locking mechanism
2220 preventing reentrant calls of Tramp.")
2221
2222 (defvar tramp-locker nil
2223 "If non-nil, then a caller has locked Tramp.
2224 Together with `tramp-locked', this implements a locking mechanism
2225 preventing reentrant calls of Tramp.")
2226
2227 ;;;###autoload
2228 (progn (defun tramp-completion-file-name-handler (operation &rest args)
2229 "Invoke Tramp file name completion handler.
2230 Falls back to normal file name handler if no Tramp file name handler exists."
2231 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
2232 ;; would otherwise use backslash.
2233 (let ((directory-sep-char ?/)
2234 (fn (assoc operation tramp-completion-file-name-handler-alist)))
2235 (if (and
2236 ;; When `tramp-mode' is not enabled, we don't do anything.
2237 fn tramp-mode
2238 ;; For other syntaxes than `sep', the regexp matches many common
2239 ;; situations where the user doesn't actually want to use Tramp.
2240 ;; So to avoid autoloading Tramp after typing just "/s", we
2241 ;; disable this part of the completion, unless the user implicitly
2242 ;; indicated his interest in using a fancier completion system.
2243 (or (eq tramp-syntax 'sep)
2244 (featurep 'tramp) ;; If it's loaded, we may as well use it.
2245 ;; `partial-completion-mode' does not exist in XEmacs.
2246 ;; It is obsoleted with Emacs 24.1.
2247 (and (boundp 'partial-completion-mode)
2248 (symbol-value 'partial-completion-mode))
2249 ;; FIXME: These may have been loaded even if the user never
2250 ;; intended to use them.
2251 (featurep 'ido)
2252 (featurep 'icicles)))
2253 (save-match-data (apply (cdr fn) args))
2254 (tramp-completion-run-real-handler operation args)))))
2255
2256 ;;;###autoload
2257 (progn (defun tramp-autoload-file-name-handler (operation &rest args)
2258 "Load Tramp file name handler, and perform OPERATION."
2259 ;; Avoid recursive loading of tramp.el. `temporary-file-directory'
2260 ;; does not exist in XEmacs, so we must use something else.
2261 (let ((default-directory "/"))
2262 (load "tramp" nil t))
2263 (apply operation args)))
2264
2265 ;; `tramp-autoload-file-name-handler' must be registered before
2266 ;; evaluation of site-start and init files, because there might exist
2267 ;; remote files already, f.e. files kept via recentf-mode. We cannot
2268 ;; autoload `tramp-file-name-handler', because it would result in
2269 ;; recursive loading of tramp.el when `default-directory' is set to
2270 ;; remote.
2271 ;;;###autoload
2272 (progn (defun tramp-register-autoload-file-name-handlers ()
2273 "Add Tramp file name handlers to `file-name-handler-alist' during autoload."
2274 (add-to-list 'file-name-handler-alist
2275 (cons tramp-file-name-regexp
2276 'tramp-autoload-file-name-handler))
2277 (put 'tramp-autoload-file-name-handler 'safe-magic t)
2278 (add-to-list 'file-name-handler-alist
2279 (cons tramp-completion-file-name-regexp
2280 'tramp-completion-file-name-handler))
2281 (put 'tramp-completion-file-name-handler 'safe-magic t)))
2282
2283 ;;;###autoload
2284 (tramp-register-autoload-file-name-handlers)
2285
2286 (defun tramp-register-file-name-handlers ()
2287 "Add Tramp file name handlers to `file-name-handler-alist'."
2288 ;; Remove autoloaded handlers from file name handler alist. Useful,
2289 ;; if `tramp-syntax' has been changed.
2290 (dolist (fnh '(tramp-file-name-handler
2291 tramp-completion-file-name-handler
2292 tramp-autoload-file-name-handler))
2293 (let ((a1 (rassq fnh file-name-handler-alist)))
2294 (setq file-name-handler-alist (delq a1 file-name-handler-alist))))
2295 ;; Add the handlers.
2296 (add-to-list 'file-name-handler-alist
2297 (cons tramp-file-name-regexp 'tramp-file-name-handler))
2298 (put 'tramp-file-name-handler 'safe-magic t)
2299 (add-to-list 'file-name-handler-alist
2300 (cons tramp-completion-file-name-regexp
2301 'tramp-completion-file-name-handler))
2302 (put 'tramp-completion-file-name-handler 'safe-magic t)
2303 ;; If jka-compr or epa-file are already loaded, move them to the
2304 ;; front of `file-name-handler-alist'.
2305 (dolist (fnh '(epa-file-handler jka-compr-handler))
2306 (let ((entry (rassoc fnh file-name-handler-alist)))
2307 (when entry
2308 (setq file-name-handler-alist
2309 (cons entry (delete entry file-name-handler-alist)))))))
2310
2311 (eval-after-load 'tramp (tramp-register-file-name-handlers))
2312
2313 (defun tramp-exists-file-name-handler (operation &rest args)
2314 "Check, whether OPERATION runs a file name handler."
2315 ;; The file name handler is determined on base of either an
2316 ;; argument, `buffer-file-name', or `default-directory'.
2317 (ignore-errors
2318 (let* ((buffer-file-name "/")
2319 (default-directory "/")
2320 (fnha file-name-handler-alist)
2321 (check-file-name-operation operation)
2322 (file-name-handler-alist
2323 (list
2324 (cons "/"
2325 (lambda (operation &rest args)
2326 "Returns OPERATION if it is the one to be checked."
2327 (if (equal check-file-name-operation operation)
2328 operation
2329 (let ((file-name-handler-alist fnha))
2330 (apply operation args))))))))
2331 (equal (apply operation args) operation))))
2332
2333 ;;;###autoload
2334 (defun tramp-unload-file-name-handlers ()
2335 (setq file-name-handler-alist
2336 (delete (rassoc 'tramp-file-name-handler
2337 file-name-handler-alist)
2338 (delete (rassoc 'tramp-completion-file-name-handler
2339 file-name-handler-alist)
2340 file-name-handler-alist))))
2341
2342 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
2343
2344 ;;; File name handler functions for completion mode:
2345
2346 (defvar tramp-completion-mode nil
2347 "If non-nil, external packages signal that they are in file name completion.
2348
2349 This is necessary, because Tramp uses a heuristic depending on last
2350 input event. This fails when external packages use other characters
2351 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
2352 should never be set globally, the intention is to let-bind it.")
2353
2354 ;; Necessary because `tramp-file-name-regexp-unified' and
2355 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
2356 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
2357 ;; to `tramp-file-name-handler'). Otherwise, it takes
2358 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
2359 ;; risky, because completing a file might require loading other files,
2360 ;; like "~/.netrc", and for them it shouldn't be decided based on that
2361 ;; variable. On the other hand, those files shouldn't have partial
2362 ;; Tramp file name syntax. Maybe another variable should be introduced
2363 ;; overwriting this check in such cases. Or we change Tramp file name
2364 ;; syntax in order to avoid ambiguities, like in XEmacs ...
2365 ;;;###tramp-autoload
2366 (defun tramp-completion-mode-p ()
2367 "Check, whether method / user name / host name completion is active."
2368 (or
2369 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
2370 (and (boundp 'non-essential) (symbol-value 'non-essential))
2371 tramp-completion-mode
2372 ;; Emacs.
2373 (equal last-input-event 'tab)
2374 (and (natnump last-input-event)
2375 (or
2376 ;; ?\t has event-modifier 'control.
2377 (equal last-input-event ?\t)
2378 (and (not (event-modifiers last-input-event))
2379 (or (equal last-input-event ?\?)
2380 (equal last-input-event ?\ )))))
2381 ;; XEmacs.
2382 (and (featurep 'xemacs)
2383 ;; `last-input-event' might be nil.
2384 (not (null last-input-event))
2385 ;; `last-input-event' may have no character approximation.
2386 (tramp-compat-funcall 'event-to-character last-input-event)
2387 (or
2388 ;; ?\t has event-modifier 'control.
2389 (equal
2390 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
2391 (and (not (event-modifiers last-input-event))
2392 (or (equal
2393 (tramp-compat-funcall 'event-to-character last-input-event)
2394 ?\?)
2395 (equal
2396 (tramp-compat-funcall 'event-to-character last-input-event)
2397 ?\ )))))))
2398
2399 (defun tramp-connectable-p (filename)
2400 "Check, whether it is possible to connect the remote host w/o side-effects.
2401 This is true, if either the remote host is already connected, or if we are
2402 not in completion mode."
2403 (and (tramp-tramp-file-p filename)
2404 (with-parsed-tramp-file-name filename nil
2405 (or (not (tramp-completion-mode-p))
2406 (let* ((tramp-verbose 0)
2407 (p (tramp-get-connection-process v)))
2408 (and p (processp p) (memq (process-status p) '(run open))))))))
2409
2410 ;; Method, host name and user name completion.
2411 ;; `tramp-completion-dissect-file-name' returns a list of
2412 ;; tramp-file-name structures. For all of them we return possible completions.
2413 ;;;###autoload
2414 (defun tramp-completion-handle-file-name-all-completions (filename directory)
2415 "Like `file-name-all-completions' for partial Tramp files."
2416
2417 (let ((fullname
2418 (tramp-drop-volume-letter (expand-file-name filename directory)))
2419 hop result result1)
2420
2421 ;; Suppress hop from completion.
2422 (when (string-match
2423 (concat
2424 tramp-prefix-regexp
2425 "\\(" "\\(" tramp-remote-file-name-spec-regexp
2426 tramp-postfix-hop-regexp
2427 "\\)+" "\\)")
2428 fullname)
2429 (setq hop (match-string 1 fullname)
2430 fullname (replace-match "" nil nil fullname 1)))
2431
2432 ;; Possible completion structures.
2433 (dolist (elt (tramp-completion-dissect-file-name fullname))
2434 (let* ((method (tramp-file-name-method elt))
2435 (user (tramp-file-name-user elt))
2436 (host (tramp-file-name-host elt))
2437 (localname (tramp-file-name-localname elt))
2438 (m (tramp-find-method method user host))
2439 (tramp-current-user user) ; see `tramp-parse-passwd'
2440 all-user-hosts)
2441
2442 (unless localname ;; Nothing to complete.
2443
2444 (if (or user host)
2445
2446 ;; Method dependent user / host combinations.
2447 (progn
2448 (mapc
2449 (lambda (x)
2450 (setq all-user-hosts
2451 (append all-user-hosts
2452 (funcall (nth 0 x) (nth 1 x)))))
2453 (tramp-get-completion-function m))
2454
2455 (setq result
2456 (append result
2457 (mapcar
2458 (lambda (x)
2459 (tramp-get-completion-user-host
2460 method user host (nth 0 x) (nth 1 x)))
2461 (delq nil all-user-hosts)))))
2462
2463 ;; Possible methods.
2464 (setq result
2465 (append result (tramp-get-completion-methods m)))))))
2466
2467 ;; Unify list, add hop, remove nil elements.
2468 (dolist (elt result)
2469 (when elt
2470 (string-match tramp-prefix-regexp elt)
2471 (setq elt (replace-match (concat tramp-prefix-format hop) nil nil elt))
2472 (add-to-list
2473 'result1
2474 (substring elt (length (tramp-drop-volume-letter directory))))))
2475
2476 ;; Complete local parts.
2477 (append
2478 result1
2479 (ignore-errors
2480 (apply (if (tramp-connectable-p fullname)
2481 'tramp-completion-run-real-handler
2482 'tramp-run-real-handler)
2483 'file-name-all-completions (list (list filename directory)))))))
2484
2485 ;; Method, host name and user name completion for a file.
2486 ;;;###autoload
2487 (defun tramp-completion-handle-file-name-completion
2488 (filename directory &optional predicate)
2489 "Like `file-name-completion' for Tramp files."
2490 (try-completion
2491 filename
2492 (mapcar 'list (file-name-all-completions filename directory))
2493 (when (and predicate
2494 (tramp-connectable-p (expand-file-name filename directory)))
2495 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2496
2497 ;; I misuse a little bit the tramp-file-name structure in order to handle
2498 ;; completion possibilities for partial methods / user names / host names.
2499 ;; Return value is a list of tramp-file-name structures according to possible
2500 ;; completions. If "localname" is non-nil it means there
2501 ;; shouldn't be a completion anymore.
2502
2503 ;; Expected results:
2504
2505 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
2506 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
2507 ;; [nil "x" nil nil]
2508 ;; ["x" nil nil nil]
2509
2510 ;; "/x:" "/x:y" "/x:y:"
2511 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
2512 ;; "/[x/" "/[x/y"
2513 ;; ["x" nil "" nil] ["x" nil "y" nil]
2514 ;; ["x" "" nil nil] ["x" "y" nil nil]
2515
2516 ;; "/x:y@" "/x:y@z" "/x:y@z:"
2517 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
2518 ;; "/[x/y@" "/[x/y@z"
2519 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
2520 (defun tramp-completion-dissect-file-name (name)
2521 "Returns a list of `tramp-file-name' structures.
2522 They are collected by `tramp-completion-dissect-file-name1'."
2523
2524 (let* ((result)
2525 (x-nil "\\|\\(\\)")
2526 (tramp-completion-ipv6-regexp
2527 (format
2528 "[^%s]*"
2529 (if (zerop (length tramp-postfix-ipv6-format))
2530 tramp-postfix-host-format
2531 tramp-postfix-ipv6-format)))
2532 ;; "/method" "/[method"
2533 (tramp-completion-file-name-structure1
2534 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
2535 1 nil nil nil))
2536 ;; "/user" "/[user"
2537 (tramp-completion-file-name-structure2
2538 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
2539 nil 1 nil nil))
2540 ;; "/host" "/[host"
2541 (tramp-completion-file-name-structure3
2542 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
2543 nil nil 1 nil))
2544 ;; "/[ipv6" "/[ipv6"
2545 (tramp-completion-file-name-structure4
2546 (list (concat tramp-prefix-regexp
2547 tramp-prefix-ipv6-regexp
2548 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2549 nil nil 1 nil))
2550 ;; "/user@host" "/[user@host"
2551 (tramp-completion-file-name-structure5
2552 (list (concat tramp-prefix-regexp
2553 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2554 "\\(" tramp-host-regexp x-nil "\\)$")
2555 nil 1 2 nil))
2556 ;; "/user@[ipv6" "/[user@ipv6"
2557 (tramp-completion-file-name-structure6
2558 (list (concat tramp-prefix-regexp
2559 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2560 tramp-prefix-ipv6-regexp
2561 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2562 nil 1 2 nil))
2563 ;; "/method:user" "/[method/user"
2564 (tramp-completion-file-name-structure7
2565 (list (concat tramp-prefix-regexp
2566 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2567 "\\(" tramp-user-regexp x-nil "\\)$")
2568 1 2 nil nil))
2569 ;; "/method:host" "/[method/host"
2570 (tramp-completion-file-name-structure8
2571 (list (concat tramp-prefix-regexp
2572 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2573 "\\(" tramp-host-regexp x-nil "\\)$")
2574 1 nil 2 nil))
2575 ;; "/method:[ipv6" "/[method/ipv6"
2576 (tramp-completion-file-name-structure9
2577 (list (concat tramp-prefix-regexp
2578 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2579 tramp-prefix-ipv6-regexp
2580 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2581 1 nil 2 nil))
2582 ;; "/method:user@host" "/[method/user@host"
2583 (tramp-completion-file-name-structure10
2584 (list (concat tramp-prefix-regexp
2585 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2586 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2587 "\\(" tramp-host-regexp x-nil "\\)$")
2588 1 2 3 nil))
2589 ;; "/method:user@[ipv6" "/[method/user@ipv6"
2590 (tramp-completion-file-name-structure11
2591 (list (concat tramp-prefix-regexp
2592 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2593 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2594 tramp-prefix-ipv6-regexp
2595 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2596 1 2 3 nil)))
2597
2598 (mapc (lambda (structure)
2599 (add-to-list 'result
2600 (tramp-completion-dissect-file-name1 structure name)))
2601 (list
2602 tramp-completion-file-name-structure1
2603 tramp-completion-file-name-structure2
2604 tramp-completion-file-name-structure3
2605 tramp-completion-file-name-structure4
2606 tramp-completion-file-name-structure5
2607 tramp-completion-file-name-structure6
2608 tramp-completion-file-name-structure7
2609 tramp-completion-file-name-structure8
2610 tramp-completion-file-name-structure9
2611 tramp-completion-file-name-structure10
2612 tramp-completion-file-name-structure11
2613 tramp-file-name-structure))
2614
2615 (delq nil result)))
2616
2617 (defun tramp-completion-dissect-file-name1 (structure name)
2618 "Returns a `tramp-file-name' structure matching STRUCTURE.
2619 The structure consists of remote method, remote user,
2620 remote host and localname (filename on remote host)."
2621
2622 (save-match-data
2623 (when (string-match (nth 0 structure) name)
2624 (let ((method (and (nth 1 structure)
2625 (match-string (nth 1 structure) name)))
2626 (user (and (nth 2 structure)
2627 (match-string (nth 2 structure) name)))
2628 (host (and (nth 3 structure)
2629 (match-string (nth 3 structure) name)))
2630 (localname (and (nth 4 structure)
2631 (match-string (nth 4 structure) name))))
2632 (vector method user host localname nil)))))
2633
2634 ;; This function returns all possible method completions, adding the
2635 ;; trailing method delimiter.
2636 (defun tramp-get-completion-methods (partial-method)
2637 "Returns all method completions for PARTIAL-METHOD."
2638 (mapcar
2639 (lambda (method)
2640 (and method
2641 (string-match (concat "^" (regexp-quote partial-method)) method)
2642 (tramp-completion-make-tramp-file-name method nil nil nil)))
2643 (mapcar 'car tramp-methods)))
2644
2645 ;; Compares partial user and host names with possible completions.
2646 (defun tramp-get-completion-user-host
2647 (method partial-user partial-host user host)
2648 "Returns the most expanded string for user and host name completion.
2649 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2650 (cond
2651
2652 ((and partial-user partial-host)
2653 (if (and host
2654 (string-match (concat "^" (regexp-quote partial-host)) host)
2655 (string-equal partial-user (or user partial-user)))
2656 (setq user partial-user)
2657 (setq user nil
2658 host nil)))
2659
2660 (partial-user
2661 (setq host nil)
2662 (unless
2663 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
2664 (setq user nil)))
2665
2666 (partial-host
2667 (setq user nil)
2668 (unless
2669 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
2670 (setq host nil)))
2671
2672 (t (setq user nil
2673 host nil)))
2674
2675 (unless (zerop (+ (length user) (length host)))
2676 (tramp-completion-make-tramp-file-name method user host nil)))
2677
2678 ;; Generic function.
2679 (defun tramp-parse-group (regexp match-level skip-regexp)
2680 "Return a (user host) tuple allowed to access.
2681 User is always nil."
2682 (let (result)
2683 (when (re-search-forward regexp (point-at-eol) t)
2684 (setq result (list nil (match-string match-level))))
2685 (or
2686 (> (skip-chars-forward skip-regexp) 0)
2687 (forward-line 1))
2688 result))
2689
2690 ;; Generic function.
2691 (defun tramp-parse-file (filename function)
2692 "Return a list of (user host) tuples allowed to access.
2693 User is always nil."
2694 ;; On Windows, there are problems in completion when
2695 ;; `default-directory' is remote.
2696 (let ((default-directory (tramp-compat-temporary-file-directory)))
2697 (when (file-readable-p filename)
2698 (with-temp-buffer
2699 (insert-file-contents filename)
2700 (goto-char (point-min))
2701 (loop while (not (eobp)) collect (funcall function))))))
2702
2703 ;;;###tramp-autoload
2704 (defun tramp-parse-rhosts (filename)
2705 "Return a list of (user host) tuples allowed to access.
2706 Either user or host may be nil."
2707 (tramp-parse-file filename 'tramp-parse-rhosts-group))
2708
2709 (defun tramp-parse-rhosts-group ()
2710 "Return a (user host) tuple allowed to access.
2711 Either user or host may be nil."
2712 (let ((result)
2713 (regexp
2714 (concat
2715 "^\\(" tramp-host-regexp "\\)"
2716 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
2717 (when (re-search-forward regexp (point-at-eol) t)
2718 (setq result (append (list (match-string 3) (match-string 1)))))
2719 (forward-line 1)
2720 result))
2721
2722 ;;;###tramp-autoload
2723 (defun tramp-parse-shosts (filename)
2724 "Return a list of (user host) tuples allowed to access.
2725 User is always nil."
2726 (tramp-parse-file filename 'tramp-parse-shosts-group))
2727
2728 (defun tramp-parse-shosts-group ()
2729 "Return a (user host) tuple allowed to access.
2730 User is always nil."
2731 (tramp-parse-group (concat "^\\(" tramp-host-regexp "\\)") 1 ","))
2732
2733 ;;;###tramp-autoload
2734 (defun tramp-parse-sconfig (filename)
2735 "Return a list of (user host) tuples allowed to access.
2736 User is always nil."
2737 (tramp-parse-file filename 'tramp-parse-sconfig-group))
2738
2739 (defun tramp-parse-sconfig-group ()
2740 "Return a (user host) tuple allowed to access.
2741 User is always nil."
2742 (tramp-parse-group
2743 (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)") 1 ","))
2744
2745 ;; Generic function.
2746 (defun tramp-parse-shostkeys-sknownhosts (dirname regexp)
2747 "Return a list of (user host) tuples allowed to access.
2748 User is always nil."
2749 ;; On Windows, there are problems in completion when
2750 ;; `default-directory' is remote.
2751 (let* ((default-directory (tramp-compat-temporary-file-directory))
2752 (files (and (file-directory-p dirname) (directory-files dirname))))
2753 (loop for f in files
2754 when (and (not (string-match "^\\.\\.?$" f)) (string-match regexp f))
2755 collect (list nil (match-string 1 f)))))
2756
2757 ;;;###tramp-autoload
2758 (defun tramp-parse-shostkeys (dirname)
2759 "Return a list of (user host) tuples allowed to access.
2760 User is always nil."
2761 (tramp-parse-shostkeys-sknownhosts
2762 dirname (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$")))
2763
2764 ;;;###tramp-autoload
2765 (defun tramp-parse-sknownhosts (dirname)
2766 "Return a list of (user host) tuples allowed to access.
2767 User is always nil."
2768 (tramp-parse-shostkeys-sknownhosts
2769 dirname
2770 (concat "^\\(" tramp-host-regexp "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
2771
2772 ;;;###tramp-autoload
2773 (defun tramp-parse-hosts (filename)
2774 "Return a list of (user host) tuples allowed to access.
2775 User is always nil."
2776 (tramp-parse-file filename 'tramp-parse-hosts-group))
2777
2778 (defun tramp-parse-hosts-group ()
2779 "Return a (user host) tuple allowed to access.
2780 User is always nil."
2781 (tramp-parse-group
2782 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)") 1 " \t"))
2783
2784 ;; For su-alike methods it would be desirable to return "root@localhost"
2785 ;; as default. Unfortunately, we have no information whether any user name
2786 ;; has been typed already. So we use `tramp-current-user' as indication,
2787 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
2788 ;;;###tramp-autoload
2789 (defun tramp-parse-passwd (filename)
2790 "Return a list of (user host) tuples allowed to access.
2791 Host is always \"localhost\"."
2792 (if (zerop (length tramp-current-user))
2793 '(("root" nil))
2794 (tramp-parse-file filename 'tramp-parse-passwd-group)))
2795
2796 (defun tramp-parse-passwd-group ()
2797 "Return a (user host) tuple allowed to access.
2798 Host is always \"localhost\"."
2799 (let ((result)
2800 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
2801 (when (re-search-forward regexp (point-at-eol) t)
2802 (setq result (list (match-string 1) "localhost")))
2803 (forward-line 1)
2804 result))
2805
2806 ;;;###tramp-autoload
2807 (defun tramp-parse-netrc (filename)
2808 "Return a list of (user host) tuples allowed to access.
2809 User may be nil."
2810 (tramp-parse-file filename 'tramp-parse-netrc-group))
2811
2812 (defun tramp-parse-netrc-group ()
2813 "Return a (user host) tuple allowed to access.
2814 User may be nil."
2815 (let ((result)
2816 (regexp
2817 (concat
2818 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
2819 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
2820 (when (re-search-forward regexp (point-at-eol) t)
2821 (setq result (list (match-string 3) (match-string 1))))
2822 (forward-line 1)
2823 result))
2824
2825 ;;;###tramp-autoload
2826 (defun tramp-parse-putty (registry-or-dirname)
2827 "Return a list of (user host) tuples allowed to access.
2828 User is always nil."
2829 (if (memq system-type '(windows-nt))
2830 (with-temp-buffer
2831 (when (zerop (tramp-call-process
2832 nil "reg" nil t nil "query" registry-or-dirname))
2833 (goto-char (point-min))
2834 (loop while (not (eobp)) collect
2835 (tramp-parse-putty-group registry-or-dirname))))
2836 ;; UNIX case.
2837 (tramp-parse-shostkeys-sknownhosts
2838 registry-or-dirname (concat "^\\(" tramp-host-regexp "\\)$"))))
2839
2840 (defun tramp-parse-putty-group (registry)
2841 "Return a (user host) tuple allowed to access.
2842 User is always nil."
2843 (let ((result)
2844 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
2845 (when (re-search-forward regexp (point-at-eol) t)
2846 (setq result (list nil (match-string 1))))
2847 (forward-line 1)
2848 result))
2849
2850 ;;; Common file name handler functions for different backends:
2851
2852 (defvar tramp-handle-file-local-copy-hook nil
2853 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2854
2855 (defvar tramp-handle-write-region-hook nil
2856 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2857
2858 (defun tramp-handle-directory-file-name (directory)
2859 "Like `directory-file-name' for Tramp files."
2860 ;; If localname component of filename is "/", leave it unchanged.
2861 ;; Otherwise, remove any trailing slash from localname component.
2862 ;; Method, host, etc, are unchanged. Does it make sense to try
2863 ;; to avoid parsing the filename?
2864 (with-parsed-tramp-file-name directory nil
2865 (if (and (not (zerop (length localname)))
2866 (eq (aref localname (1- (length localname))) ?/)
2867 (not (string= localname "/")))
2868 (substring directory 0 -1)
2869 directory)))
2870
2871 (defun tramp-handle-directory-files
2872 (directory &optional full match nosort files-only)
2873 "Like `directory-files' for Tramp files."
2874 ;; FILES-ONLY is valid for XEmacs only.
2875 (when (file-directory-p directory)
2876 (setq directory (file-name-as-directory (expand-file-name directory)))
2877 (let ((temp (nreverse (file-name-all-completions "" directory)))
2878 result item)
2879
2880 (while temp
2881 (setq item (directory-file-name (pop temp)))
2882 (when (and (or (null match) (string-match match item))
2883 (or (null files-only)
2884 ;; Files only.
2885 (and (equal files-only t) (file-regular-p item))
2886 ;; Directories only.
2887 (file-directory-p item)))
2888 (push (if full (concat directory item) item)
2889 result)))
2890 (if nosort result (sort result 'string<)))))
2891
2892 (defun tramp-handle-directory-files-and-attributes
2893 (directory &optional full match nosort id-format)
2894 "Like `directory-files-and-attributes' for Tramp files."
2895 (mapcar
2896 (lambda (x)
2897 (cons x (tramp-compat-file-attributes
2898 (if full x (expand-file-name x directory)) id-format)))
2899 (directory-files directory full match nosort)))
2900
2901 (defun tramp-handle-dired-uncache (dir &optional dir-p)
2902 "Like `dired-uncache' for Tramp files."
2903 ;; DIR-P is valid for XEmacs only.
2904 (with-parsed-tramp-file-name
2905 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
2906 (tramp-flush-directory-property v localname)))
2907
2908 (defun tramp-handle-file-accessible-directory-p (filename)
2909 "Like `file-accessible-directory-p' for Tramp files."
2910 (and (file-directory-p filename)
2911 (file-readable-p filename)))
2912
2913 (defun tramp-handle-file-equal-p (filename1 filename2)
2914 "Like `file-equalp-p' for Tramp files."
2915 ;; Native `file-equalp-p' calls `file-truename', which requires a
2916 ;; remote connection. This can be avoided, if FILENAME1 and
2917 ;; FILENAME2 are not located on the same remote host.
2918 (when (string-equal
2919 (file-remote-p (expand-file-name filename1))
2920 (file-remote-p (expand-file-name filename2)))
2921 (tramp-run-real-handler 'file-equal-p (list filename1 filename2))))
2922
2923 (defun tramp-handle-file-exists-p (filename)
2924 "Like `file-exists-p' for Tramp files."
2925 (not (null (file-attributes filename))))
2926
2927 (defun tramp-handle-file-in-directory-p (filename directory)
2928 "Like `file-in-directory-p' for Tramp files."
2929 ;; Native `file-in-directory-p' calls `file-truename', which
2930 ;; requires a remote connection. This can be avoided, if FILENAME
2931 ;; and DIRECTORY are not located on the same remote host.
2932 (when (string-equal
2933 (file-remote-p (expand-file-name filename))
2934 (file-remote-p (expand-file-name directory)))
2935 (tramp-run-real-handler 'file-in-directory-p (list filename directory))))
2936
2937 (defun tramp-handle-file-modes (filename)
2938 "Like `file-modes' for Tramp files."
2939 (let ((truename (or (file-truename filename) filename)))
2940 (when (file-exists-p truename)
2941 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
2942
2943 ;; Localname manipulation functions that grok Tramp localnames...
2944 (defun tramp-handle-file-name-as-directory (file)
2945 "Like `file-name-as-directory' but aware of Tramp files."
2946 ;; `file-name-as-directory' would be sufficient except localname is
2947 ;; the empty string.
2948 (let ((v (tramp-dissect-file-name file t)))
2949 ;; Run the command on the localname portion only.
2950 (tramp-make-tramp-file-name
2951 (tramp-file-name-method v)
2952 (tramp-file-name-user v)
2953 (tramp-file-name-host v)
2954 (tramp-run-real-handler
2955 'file-name-as-directory (list (or (tramp-file-name-localname v) "")))
2956 (tramp-file-name-hop v))))
2957
2958 (defun tramp-handle-file-name-completion
2959 (filename directory &optional predicate)
2960 "Like `file-name-completion' for Tramp files."
2961 (unless (tramp-tramp-file-p directory)
2962 (error
2963 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2964 directory))
2965 (try-completion
2966 filename
2967 (mapcar 'list (file-name-all-completions filename directory))
2968 (when predicate
2969 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2970
2971 (defun tramp-handle-file-name-directory (file)
2972 "Like `file-name-directory' but aware of Tramp files."
2973 ;; Everything except the last filename thing is the directory. We
2974 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2975 ;; the remote file name parts. This is a problem when we are in
2976 ;; file name completion.
2977 (let ((v (tramp-dissect-file-name file t)))
2978 ;; Run the command on the localname portion only.
2979 (tramp-make-tramp-file-name
2980 (tramp-file-name-method v)
2981 (tramp-file-name-user v)
2982 (tramp-file-name-host v)
2983 (tramp-run-real-handler
2984 'file-name-directory (list (or (tramp-file-name-localname v) "")))
2985 (tramp-file-name-hop v))))
2986
2987 (defun tramp-handle-file-name-nondirectory (file)
2988 "Like `file-name-nondirectory' but aware of Tramp files."
2989 (with-parsed-tramp-file-name file nil
2990 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2991
2992 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2993 "Like `file-newer-than-file-p' for Tramp files."
2994 (cond
2995 ((not (file-exists-p file1)) nil)
2996 ((not (file-exists-p file2)) t)
2997 (t (time-less-p (nth 5 (file-attributes file2))
2998 (nth 5 (file-attributes file1))))))
2999
3000 (defun tramp-handle-file-regular-p (filename)
3001 "Like `file-regular-p' for Tramp files."
3002 (and (file-exists-p filename)
3003 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3004
3005 (defun tramp-handle-file-remote-p (filename &optional identification connected)
3006 "Like `file-remote-p' for Tramp files."
3007 ;; We do not want traces in the debug buffer.
3008 (let ((tramp-verbose (min tramp-verbose 3)))
3009 (when (tramp-tramp-file-p filename)
3010 (let* ((v (tramp-dissect-file-name filename))
3011 (p (tramp-get-connection-process v))
3012 (c (and p (processp p) (memq (process-status p) '(run open)))))
3013 ;; We expand the file name only, if there is already a connection.
3014 (with-parsed-tramp-file-name
3015 (if c (expand-file-name filename) filename) nil
3016 (and (or (not connected) c)
3017 (cond
3018 ((eq identification 'method) method)
3019 ((eq identification 'user) user)
3020 ((eq identification 'host) host)
3021 ((eq identification 'localname) localname)
3022 ((eq identification 'hop) hop)
3023 (t (tramp-make-tramp-file-name method user host "" hop)))))))))
3024
3025 (defun tramp-handle-file-symlink-p (filename)
3026 "Like `file-symlink-p' for Tramp files."
3027 (with-parsed-tramp-file-name filename nil
3028 (let ((x (car (file-attributes filename))))
3029 (when (stringp x)
3030 (if (file-name-absolute-p x)
3031 (tramp-make-tramp-file-name method user host x)
3032 x)))))
3033
3034 (defun tramp-handle-find-backup-file-name (filename)
3035 "Like `find-backup-file-name' for Tramp files."
3036 (with-parsed-tramp-file-name filename nil
3037 ;; We set both variables. It doesn't matter whether it is
3038 ;; Emacs or XEmacs.
3039 (let ((backup-directory-alist
3040 ;; Emacs case.
3041 (when (boundp 'backup-directory-alist)
3042 (if (symbol-value 'tramp-backup-directory-alist)
3043 (mapcar
3044 (lambda (x)
3045 (cons
3046 (car x)
3047 (if (and (stringp (cdr x))
3048 (file-name-absolute-p (cdr x))
3049 (not (tramp-file-name-p (cdr x))))
3050 (tramp-make-tramp-file-name method user host (cdr x))
3051 (cdr x))))
3052 (symbol-value 'tramp-backup-directory-alist))
3053 (symbol-value 'backup-directory-alist))))
3054
3055 (bkup-backup-directory-info
3056 ;; XEmacs case.
3057 (when (boundp 'bkup-backup-directory-info)
3058 (if (symbol-value 'tramp-bkup-backup-directory-info)
3059 (mapcar
3060 (lambda (x)
3061 (nconc
3062 (list (car x))
3063 (list
3064 (if (and (stringp (car (cdr x)))
3065 (file-name-absolute-p (car (cdr x)))
3066 (not (tramp-file-name-p (car (cdr x)))))
3067 (tramp-make-tramp-file-name
3068 method user host (car (cdr x)))
3069 (car (cdr x))))
3070 (cdr (cdr x))))
3071 (symbol-value 'tramp-bkup-backup-directory-info))
3072 (symbol-value 'bkup-backup-directory-info)))))
3073
3074 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3075
3076 (defun tramp-handle-insert-directory
3077 (filename switches &optional wildcard full-directory-p)
3078 "Like `insert-directory' for Tramp files."
3079 (unless switches (setq switches ""))
3080 ;; Mark trailing "/".
3081 (when (and (zerop (length (file-name-nondirectory filename)))
3082 (not full-directory-p))
3083 (setq switches (concat switches "F")))
3084 (with-parsed-tramp-file-name (expand-file-name filename) nil
3085 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
3086 (require 'ls-lisp)
3087 (let (ls-lisp-use-insert-directory-program start)
3088 (tramp-run-real-handler
3089 'insert-directory
3090 (list filename switches wildcard full-directory-p))
3091 ;; `ls-lisp' always returns full listings. We must remove
3092 ;; superfluous parts.
3093 (unless (string-match "l" switches)
3094 (save-excursion
3095 (goto-char (point-min))
3096 (while (setq start
3097 (text-property-not-all
3098 (point) (point-at-eol) 'dired-filename t))
3099 (delete-region
3100 start
3101 (or (text-property-any start (point-at-eol) 'dired-filename t)
3102 (point-at-eol)))
3103 (if (= (point-at-bol) (point-at-eol))
3104 ;; Empty line.
3105 (delete-region (point) (progn (forward-line) (point)))
3106 (forward-line)))))))))
3107
3108 (defun tramp-handle-insert-file-contents
3109 (filename &optional visit beg end replace)
3110 "Like `insert-file-contents' for Tramp files."
3111 (barf-if-buffer-read-only)
3112 (setq filename (expand-file-name filename))
3113 (let (result local-copy remote-copy)
3114 (with-parsed-tramp-file-name filename nil
3115 (unwind-protect
3116 (if (not (file-exists-p filename))
3117 (tramp-error
3118 v 'file-error "File `%s' not found on remote host" filename)
3119
3120 (with-tramp-progress-reporter
3121 v 3 (format-message "Inserting `%s'" filename)
3122 (condition-case err
3123 (if (and (tramp-local-host-p v)
3124 (let (file-name-handler-alist)
3125 (file-readable-p localname)))
3126 ;; Short track: if we are on the local host, we can
3127 ;; run directly.
3128 (setq result
3129 (tramp-run-real-handler
3130 'insert-file-contents
3131 (list localname visit beg end replace)))
3132
3133 ;; When we shall insert only a part of the file, we
3134 ;; copy this part. This works only for the shell file
3135 ;; name handlers.
3136 (when (and (or beg end)
3137 (tramp-get-method-parameter
3138 v 'tramp-login-program))
3139 (setq remote-copy (tramp-make-tramp-temp-file v))
3140 ;; This is defined in tramp-sh.el. Let's assume
3141 ;; this is loaded already.
3142 (tramp-compat-funcall
3143 'tramp-send-command
3144 v
3145 (cond
3146 ((and beg end)
3147 (format "dd bs=1 skip=%d if=%s count=%d of=%s"
3148 beg (tramp-shell-quote-argument localname)
3149 (- end beg) remote-copy))
3150 (beg
3151 (format "dd bs=1 skip=%d if=%s of=%s"
3152 beg (tramp-shell-quote-argument localname)
3153 remote-copy))
3154 (end
3155 (format "dd bs=1 count=%d if=%s of=%s"
3156 end (tramp-shell-quote-argument localname)
3157 remote-copy))))
3158 (setq tramp-temp-buffer-file-name nil beg nil end nil))
3159
3160 ;; `insert-file-contents-literally' takes care to
3161 ;; avoid calling jka-compr.el and epa.el. By
3162 ;; let-binding `inhibit-file-name-operation', we
3163 ;; propagate that care to the `file-local-copy'
3164 ;; operation.
3165 (setq local-copy
3166 (let ((inhibit-file-name-operation
3167 (when (eq inhibit-file-name-operation
3168 'insert-file-contents)
3169 'file-local-copy)))
3170 (cond
3171 ((stringp remote-copy)
3172 (file-local-copy
3173 (tramp-make-tramp-file-name
3174 method user host remote-copy)))
3175 ((stringp tramp-temp-buffer-file-name)
3176 (copy-file
3177 filename tramp-temp-buffer-file-name 'ok)
3178 tramp-temp-buffer-file-name)
3179 (t (file-local-copy filename)))))
3180
3181 ;; When the file is not readable for the owner, it
3182 ;; cannot be inserted, even if it is readable for the
3183 ;; group or for everybody.
3184 (set-file-modes
3185 local-copy (tramp-compat-octal-to-decimal "0600"))
3186
3187 (when (and (null remote-copy)
3188 (tramp-get-method-parameter
3189 v 'tramp-copy-keep-tmpfile))
3190 ;; We keep the local file for performance reasons,
3191 ;; useful for "rsync".
3192 (setq tramp-temp-buffer-file-name local-copy))
3193
3194 ;; We must ensure that `file-coding-system-alist'
3195 ;; matches `local-copy'. We must also use `visit',
3196 ;; otherwise there might be an error in the
3197 ;; `revert-buffer' function under XEmacs.
3198 (let ((file-coding-system-alist
3199 (tramp-find-file-name-coding-system-alist
3200 filename local-copy)))
3201 (setq result
3202 (insert-file-contents
3203 local-copy visit beg end replace))))
3204 (error
3205 (add-hook 'find-file-not-found-functions
3206 `(lambda () (signal ',(car err) ',(cdr err)))
3207 nil t)
3208 (signal (car err) (cdr err))))))
3209
3210 ;; Save exit.
3211 (progn
3212 (when visit
3213 (setq buffer-file-name filename)
3214 (setq buffer-read-only (not (file-writable-p filename)))
3215 (set-visited-file-modtime)
3216 (set-buffer-modified-p nil))
3217 (when (and (stringp local-copy)
3218 (or remote-copy (null tramp-temp-buffer-file-name)))
3219 (delete-file local-copy))
3220 (when (stringp remote-copy)
3221 (delete-file
3222 (tramp-make-tramp-file-name method user host remote-copy)))))
3223
3224 ;; Result.
3225 (list (expand-file-name filename)
3226 (cadr result)))))
3227
3228 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
3229 "Like `load' for Tramp files."
3230 (with-parsed-tramp-file-name (expand-file-name file) nil
3231 (unless nosuffix
3232 (cond ((file-exists-p (concat file ".elc"))
3233 (setq file (concat file ".elc")))
3234 ((file-exists-p (concat file ".el"))
3235 (setq file (concat file ".el")))))
3236 (when must-suffix
3237 ;; The first condition is always true for absolute file names.
3238 ;; Included for safety's sake.
3239 (unless (or (file-name-directory file)
3240 (string-match "\\.elc?\\'" file))
3241 (tramp-error
3242 v 'file-error
3243 "File `%s' does not include a `.el' or `.elc' suffix" file)))
3244 (unless noerror
3245 (when (not (file-exists-p file))
3246 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
3247 (if (not (file-exists-p file))
3248 nil
3249 (let ((tramp-message-show-message (not nomessage)))
3250 (with-tramp-progress-reporter v 0 (format "Loading %s" file)
3251 (let ((local-copy (file-local-copy file)))
3252 (unwind-protect
3253 (tramp-compat-load local-copy noerror t nosuffix must-suffix)
3254 (delete-file local-copy)))))
3255 t)))
3256
3257 (defun tramp-handle-make-symbolic-link
3258 (filename linkname &optional _ok-if-already-exists)
3259 "Like `make-symbolic-link' for Tramp files."
3260 (with-parsed-tramp-file-name
3261 (if (tramp-tramp-file-p filename) filename linkname) nil
3262 (tramp-error v 'file-error "make-symbolic-link not supported")))
3263
3264 (defun tramp-handle-shell-command
3265 (command &optional output-buffer error-buffer)
3266 "Like `shell-command' for Tramp files."
3267 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3268 ;; We cannot use `shell-file-name' and `shell-command-switch',
3269 ;; they are variables of the local host.
3270 (args (append
3271 (cons
3272 (tramp-get-method-parameter
3273 (tramp-dissect-file-name default-directory)
3274 'tramp-remote-shell)
3275 (tramp-get-method-parameter
3276 (tramp-dissect-file-name default-directory)
3277 'tramp-remote-shell-args))
3278 (list (substring command 0 asynchronous))))
3279 current-buffer-p
3280 (output-buffer
3281 (cond
3282 ((bufferp output-buffer) output-buffer)
3283 ((stringp output-buffer) (get-buffer-create output-buffer))
3284 (output-buffer
3285 (setq current-buffer-p t)
3286 (current-buffer))
3287 (t (get-buffer-create
3288 (if asynchronous
3289 "*Async Shell Command*"
3290 "*Shell Command Output*")))))
3291 (error-buffer
3292 (cond
3293 ((bufferp error-buffer) error-buffer)
3294 ((stringp error-buffer) (get-buffer-create error-buffer))))
3295 (buffer
3296 (if (and (not asynchronous) error-buffer)
3297 (with-parsed-tramp-file-name default-directory nil
3298 (list output-buffer (tramp-make-tramp-temp-file v)))
3299 output-buffer))
3300 (p (get-buffer-process output-buffer)))
3301
3302 ;; Check whether there is another process running. Tramp does not
3303 ;; support 2 (asynchronous) processes in parallel.
3304 (when p
3305 (if (yes-or-no-p "A command is running. Kill it? ")
3306 (ignore-errors (kill-process p))
3307 (tramp-user-error p "Shell command in progress")))
3308
3309 (if current-buffer-p
3310 (progn
3311 (barf-if-buffer-read-only)
3312 (push-mark nil t))
3313 (with-current-buffer output-buffer
3314 (setq buffer-read-only nil)
3315 (erase-buffer)))
3316
3317 (if (and (not current-buffer-p) (integerp asynchronous))
3318 (prog1
3319 ;; Run the process.
3320 (setq p (apply 'start-file-process "*Async Shell*" buffer args))
3321 ;; Display output.
3322 (with-current-buffer output-buffer
3323 (display-buffer output-buffer '(nil (allow-no-window . t)))
3324 (setq mode-line-process '(":%s"))
3325 (shell-mode)
3326 (set-process-sentinel p 'shell-command-sentinel)
3327 (set-process-filter p 'comint-output-filter)))
3328
3329 (prog1
3330 ;; Run the process.
3331 (apply 'process-file (car args) nil buffer nil (cdr args))
3332 ;; Insert error messages if they were separated.
3333 (when (listp buffer)
3334 (with-current-buffer error-buffer
3335 (insert-file-contents (cadr buffer)))
3336 (delete-file (cadr buffer)))
3337 (if current-buffer-p
3338 ;; This is like exchange-point-and-mark, but doesn't
3339 ;; activate the mark. It is cleaner to avoid activation,
3340 ;; even though the command loop would deactivate the mark
3341 ;; because we inserted text.
3342 (goto-char (prog1 (mark t)
3343 (set-marker (mark-marker) (point)
3344 (current-buffer))))
3345 ;; There's some output, display it.
3346 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
3347 (if (functionp 'display-message-or-buffer)
3348 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
3349 (pop-to-buffer output-buffer))))))))
3350
3351 (defun tramp-handle-substitute-in-file-name (filename)
3352 "Like `substitute-in-file-name' for Tramp files.
3353 \"//\" and \"/~\" substitute only in the local filename part."
3354 ;; First, we must replace environment variables.
3355 (setq filename (tramp-replace-environment-variables filename))
3356 (with-parsed-tramp-file-name filename nil
3357 ;; Ignore in LOCALNAME everything before "//" or "/~".
3358 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
3359 (setq filename
3360 (concat (file-remote-p filename)
3361 (replace-match "\\1" nil nil localname)))
3362 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3363 (when (string-match "~$" filename)
3364 (setq filename (concat filename "/"))))
3365 ;; We do not want to replace environment variables, again.
3366 (let (process-environment)
3367 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
3368
3369 (defun tramp-handle-unhandled-file-name-directory (_filename)
3370 "Like `unhandled-file-name-directory' for Tramp files."
3371 ;; Starting with Emacs 23, we must simply return nil. But we must
3372 ;; keep backward compatibility, also with XEmacs. "~/" cannot be
3373 ;; returned, because there might be machines without a HOME
3374 ;; directory (like hydra).
3375 (and (< emacs-major-version 23) "/"))
3376
3377 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
3378 "Like `set-visited-file-modtime' for Tramp files."
3379 (unless (buffer-file-name)
3380 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3381 (buffer-name)))
3382 (unless time-list
3383 (let ((remote-file-name-inhibit-cache t))
3384 ;; '(-1 65535) means file doesn't exists yet.
3385 (setq time-list
3386 (or (nth 5 (file-attributes (buffer-file-name))) '(-1 65535)))))
3387 ;; We use '(0 0) as a don't-know value.
3388 (unless (equal time-list '(0 0))
3389 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))))
3390
3391 (defun tramp-handle-verify-visited-file-modtime (&optional buf)
3392 "Like `verify-visited-file-modtime' for Tramp files.
3393 At the time `verify-visited-file-modtime' calls this function, we
3394 already know that the buffer is visiting a file and that
3395 `visited-file-modtime' does not return 0. Do not call this
3396 function directly, unless those two cases are already taken care
3397 of."
3398 (with-current-buffer (or buf (current-buffer))
3399 (let ((f (buffer-file-name)))
3400 ;; There is no file visiting the buffer, or the buffer has no
3401 ;; recorded last modification time, or there is no established
3402 ;; connection.
3403 (if (or (not f)
3404 (eq (visited-file-modtime) 0)
3405 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
3406 t
3407 (with-parsed-tramp-file-name f nil
3408 (let* ((remote-file-name-inhibit-cache t)
3409 (attr (file-attributes f))
3410 (modtime (nth 5 attr))
3411 (mt (visited-file-modtime)))
3412
3413 (cond
3414 ;; File exists, and has a known modtime.
3415 ((and attr (not (equal modtime '(0 0))))
3416 (< (abs (tramp-time-diff
3417 modtime
3418 ;; For compatibility, deal with both the old
3419 ;; (HIGH . LOW) and the new (HIGH LOW) return
3420 ;; values of `visited-file-modtime'.
3421 (if (atom (cdr mt))
3422 (list (car mt) (cdr mt))
3423 mt)))
3424 2))
3425 ;; Modtime has the don't know value.
3426 (attr t)
3427 ;; If file does not exist, say it is not modified if and
3428 ;; only if that agrees with the buffer's record.
3429 (t (equal mt '(-1 65535))))))))))
3430
3431 (defun tramp-handle-file-notify-add-watch (filename _flags _callback)
3432 "Like `file-notify-add-watch' for Tramp files."
3433 ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
3434 ;; their own one.
3435 (setq filename (expand-file-name filename))
3436 (with-parsed-tramp-file-name filename nil
3437 (tramp-error
3438 v 'file-notify-error "File notification not supported for `%s'" filename)))
3439
3440 (defun tramp-handle-file-notify-rm-watch (proc)
3441 "Like `file-notify-rm-watch' for Tramp files."
3442 ;; The descriptor must be a process object.
3443 (unless (processp proc)
3444 (tramp-error proc 'file-notify-error "Not a valid descriptor %S" proc))
3445 (tramp-message proc 6 "Kill %S" proc)
3446 (delete-process proc))
3447
3448 (defun tramp-handle-file-notify-valid-p (proc)
3449 "Like `file-notify-valid-p' for Tramp files."
3450 (and proc (processp proc) (memq (process-status proc) '(run open))
3451 ;; Sometimes, the process is still in status `run' when the
3452 ;; file or directory to be watched is deleted already.
3453 (with-current-buffer (process-buffer proc)
3454 (file-exists-p
3455 (concat (file-remote-p default-directory)
3456 (tramp-compat-process-get proc 'watch-name))))))
3457
3458 ;;; Functions for establishing connection:
3459
3460 ;; The following functions are actions to be taken when seeing certain
3461 ;; prompts from the remote host. See the variable
3462 ;; `tramp-actions-before-shell' for usage of these functions.
3463
3464 (defun tramp-action-login (_proc vec)
3465 "Send the login name."
3466 (when (not (stringp tramp-current-user))
3467 (setq tramp-current-user
3468 (with-tramp-connection-property vec "login-as"
3469 (save-window-excursion
3470 (let ((enable-recursive-minibuffers t))
3471 (pop-to-buffer (tramp-get-connection-buffer vec))
3472 (read-string (match-string 0)))))))
3473 (with-current-buffer (tramp-get-connection-buffer vec)
3474 (tramp-message vec 6 "\n%s" (buffer-string)))
3475 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
3476 (tramp-send-string vec (concat tramp-current-user tramp-local-end-of-line)))
3477
3478 (defun tramp-action-password (proc vec)
3479 "Query the user for a password."
3480 (with-current-buffer (process-buffer proc)
3481 (let ((enable-recursive-minibuffers t)
3482 (case-fold-search t))
3483 ;; Let's check whether a wrong password has been sent already.
3484 ;; Sometimes, the process returns a new password request
3485 ;; immediately after rejecting the previous (wrong) one.
3486 (unless (tramp-get-connection-property vec "first-password-request" nil)
3487 (tramp-clear-passwd vec))
3488 (goto-char (point-min))
3489 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
3490 (tramp-message vec 3 "Sending %s" (match-string 1))
3491 ;; We don't call `tramp-send-string' in order to hide the
3492 ;; password from the debug buffer.
3493 (process-send-string
3494 proc (concat (tramp-read-passwd proc) tramp-local-end-of-line))
3495 ;; Hide password prompt.
3496 (narrow-to-region (point-max) (point-max)))))
3497
3498 (defun tramp-action-succeed (_proc _vec)
3499 "Signal success in finding shell prompt."
3500 (throw 'tramp-action 'ok))
3501
3502 (defun tramp-action-permission-denied (proc _vec)
3503 "Signal permission denied."
3504 (kill-process proc)
3505 (throw 'tramp-action 'permission-denied))
3506
3507 (defun tramp-action-yesno (proc vec)
3508 "Ask the user for confirmation using `yes-or-no-p'.
3509 Send \"yes\" to remote process on confirmation, abort otherwise.
3510 See also `tramp-action-yn'."
3511 (save-window-excursion
3512 (let ((enable-recursive-minibuffers t))
3513 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
3514 (unless (yes-or-no-p (match-string 0))
3515 (kill-process proc)
3516 (throw 'tramp-action 'permission-denied))
3517 (with-current-buffer (tramp-get-connection-buffer vec)
3518 (tramp-message vec 6 "\n%s" (buffer-string)))
3519 (tramp-send-string vec (concat "yes" tramp-local-end-of-line)))))
3520
3521 (defun tramp-action-yn (proc vec)
3522 "Ask the user for confirmation using `y-or-n-p'.
3523 Send \"y\" to remote process on confirmation, abort otherwise.
3524 See also `tramp-action-yesno'."
3525 (save-window-excursion
3526 (let ((enable-recursive-minibuffers t))
3527 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
3528 (unless (y-or-n-p (match-string 0))
3529 (kill-process proc)
3530 (throw 'tramp-action 'permission-denied))
3531 (with-current-buffer (tramp-get-connection-buffer vec)
3532 (tramp-message vec 6 "\n%s" (buffer-string)))
3533 (tramp-send-string vec (concat "y" tramp-local-end-of-line)))))
3534
3535 (defun tramp-action-terminal (_proc vec)
3536 "Tell the remote host which terminal type to use.
3537 The terminal type can be configured with `tramp-terminal-type'."
3538 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
3539 (with-current-buffer (tramp-get-connection-buffer vec)
3540 (tramp-message vec 6 "\n%s" (buffer-string)))
3541 (tramp-send-string vec (concat tramp-terminal-type tramp-local-end-of-line)))
3542
3543 (defun tramp-action-process-alive (proc _vec)
3544 "Check, whether a process has finished."
3545 (unless (memq (process-status proc) '(run open))
3546 (throw 'tramp-action 'process-died)))
3547
3548 (defun tramp-action-out-of-band (proc vec)
3549 "Check, whether an out-of-band copy has finished."
3550 ;; There might be pending output for the exit status.
3551 (tramp-accept-process-output proc 0.1)
3552 (cond ((and (memq (process-status proc) '(stop exit))
3553 (zerop (process-exit-status proc)))
3554 (tramp-message vec 3 "Process has finished.")
3555 (throw 'tramp-action 'ok))
3556 ((or (and (memq (process-status proc) '(stop exit))
3557 (not (zerop (process-exit-status proc))))
3558 (memq (process-status proc) '(signal)))
3559 ;; `scp' could have copied correctly, but set modes could have failed.
3560 ;; This can be ignored.
3561 (with-current-buffer (process-buffer proc)
3562 (goto-char (point-min))
3563 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
3564 (progn
3565 (tramp-message vec 5 "'set mode' error ignored.")
3566 (tramp-message vec 3 "Process has finished.")
3567 (throw 'tramp-action 'ok))
3568 (tramp-message vec 3 "Process has died.")
3569 (throw 'tramp-action 'process-died))))
3570 (t nil)))
3571
3572 ;;; Functions for processing the actions:
3573
3574 (defun tramp-process-one-action (proc vec actions)
3575 "Wait for output from the shell and perform one action."
3576 (let ((case-fold-search t)
3577 found todo item pattern action)
3578 (while (not found)
3579 ;; Reread output once all actions have been performed.
3580 ;; Obviously, the output was not complete.
3581 (tramp-accept-process-output proc 1)
3582 (setq todo actions)
3583 (while todo
3584 (setq item (pop todo))
3585 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
3586 (setq action (nth 1 item))
3587 (tramp-message
3588 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
3589 (when (tramp-check-for-regexp proc pattern)
3590 (tramp-message vec 5 "Call `%s'" (symbol-name action))
3591 (setq found (funcall action proc vec)))))
3592 found))
3593
3594 (defun tramp-process-actions (proc vec pos actions &optional timeout)
3595 "Perform ACTIONS until success or TIMEOUT.
3596 PROC and VEC indicate the remote connection to be used. POS, if
3597 set, is the starting point of the region to be deleted in the
3598 connection buffer."
3599 ;; Enable `auth-source'. We must use tramp-current-* variables in
3600 ;; case we have several hops.
3601 (tramp-set-connection-property
3602 (tramp-dissect-file-name
3603 (tramp-make-tramp-file-name
3604 tramp-current-method tramp-current-user tramp-current-host ""))
3605 "first-password-request" t)
3606 (save-restriction
3607 (with-tramp-progress-reporter
3608 proc 3 "Waiting for prompts from remote shell"
3609 (let (exit)
3610 (if timeout
3611 (with-timeout (timeout (setq exit 'timeout))
3612 (while (not exit)
3613 (setq exit
3614 (catch 'tramp-action
3615 (tramp-process-one-action proc vec actions)))))
3616 (while (not exit)
3617 (setq exit
3618 (catch 'tramp-action
3619 (tramp-process-one-action proc vec actions)))))
3620 (with-current-buffer (tramp-get-connection-buffer vec)
3621 (widen)
3622 (tramp-message vec 6 "\n%s" (buffer-string)))
3623 (unless (eq exit 'ok)
3624 (tramp-clear-passwd vec)
3625 (delete-process proc)
3626 (tramp-error-with-buffer
3627 (tramp-get-connection-buffer vec) vec 'file-error
3628 (cond
3629 ((eq exit 'permission-denied) "Permission denied")
3630 ((eq exit 'process-died)
3631 (substitute-command-keys
3632 (concat
3633 "Tramp failed to connect. If this happens repeatedly, try\n"
3634 " `\\[tramp-cleanup-this-connection]'")))
3635 ((eq exit 'timeout)
3636 (format-message
3637 "Timeout reached, see buffer `%s' for details"
3638 (tramp-get-connection-buffer vec)))
3639 (t "Login failed")))))
3640 (when (numberp pos)
3641 (with-current-buffer (tramp-get-connection-buffer vec)
3642 (let (buffer-read-only) (delete-region pos (point))))))))
3643
3644 :;; Utility functions:
3645
3646 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
3647 "Like `accept-process-output' for Tramp processes.
3648 This is needed in order to hide `last-coding-system-used', which is set
3649 for process communication also."
3650 (with-current-buffer (process-buffer proc)
3651 ;; FIXME: If there is a gateway process, we need communication
3652 ;; between several processes. Too complicate to implement, so we
3653 ;; read output from all processes.
3654 (let ((p (if (tramp-get-connection-property proc "gateway" nil) nil proc))
3655 buffer-read-only last-coding-system-used)
3656 ;; Under Windows XP, accept-process-output doesn't return
3657 ;; sometimes. So we add an additional timeout.
3658 (with-timeout ((or timeout 1))
3659 (if (featurep 'xemacs)
3660 (accept-process-output p timeout timeout-msecs)
3661 (accept-process-output p timeout timeout-msecs (and proc t))))
3662 (tramp-message proc 10 "%s %s %s\n%s"
3663 proc (process-status proc) p (buffer-string)))))
3664
3665 (defun tramp-check-for-regexp (proc regexp)
3666 "Check, whether REGEXP is contained in process buffer of PROC.
3667 Erase echoed commands if exists."
3668 (with-current-buffer (process-buffer proc)
3669 (goto-char (point-min))
3670
3671 ;; Check whether we need to remove echo output.
3672 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
3673 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
3674 (let ((begin (match-beginning 0)))
3675 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
3676 ;; Discard echo from remote output.
3677 (tramp-set-connection-property proc "check-remote-echo" nil)
3678 (tramp-message proc 5 "echo-mark found")
3679 (forward-line 1)
3680 (delete-region begin (point))
3681 (goto-char (point-min)))))
3682
3683 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
3684 ;; Sometimes, the echo string is suppressed on the remote side.
3685 (not (string-equal
3686 (tramp-compat-funcall
3687 'substring-no-properties tramp-echo-mark-marker
3688 0 (min tramp-echo-mark-marker-length (1- (point-max))))
3689 (tramp-compat-funcall
3690 'buffer-substring-no-properties
3691 (point-min)
3692 (min (+ (point-min) tramp-echo-mark-marker-length)
3693 (point-max))))))
3694 ;; No echo to be handled, now we can look for the regexp.
3695 ;; Sometimes, lines are much to long, and we run into a "Stack
3696 ;; overflow in regexp matcher". For example, //DIRED// lines of
3697 ;; directory listings with some thousand files. Therefore, we
3698 ;; look from the end.
3699 (goto-char (point-max))
3700 (ignore-errors (re-search-backward regexp nil t)))))
3701
3702 (defun tramp-wait-for-regexp (proc timeout regexp)
3703 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3704 Expects the output of PROC to be sent to the current buffer. Returns
3705 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3706 nil."
3707 (with-current-buffer (process-buffer proc)
3708 (let ((found (tramp-check-for-regexp proc regexp))
3709 (start-time (current-time)))
3710 (cond (timeout
3711 ;; Work around a bug in XEmacs 21, where the timeout
3712 ;; expires faster than it should. This degenerates
3713 ;; to polling for buggy XEmacsen, but oh, well.
3714 (while (and (not found)
3715 (< (tramp-time-diff (current-time) start-time)
3716 timeout))
3717 (with-timeout (timeout)
3718 (while (not found)
3719 (tramp-accept-process-output proc 1)
3720 (unless (memq (process-status proc) '(run open))
3721 (tramp-error-with-buffer
3722 nil proc 'file-error "Process has died"))
3723 (setq found (tramp-check-for-regexp proc regexp))))))
3724 (t
3725 (while (not found)
3726 (tramp-accept-process-output proc 1)
3727 (unless (memq (process-status proc) '(run open))
3728 (tramp-error-with-buffer
3729 nil proc 'file-error "Process has died"))
3730 (setq found (tramp-check-for-regexp proc regexp)))))
3731 (tramp-message proc 6 "\n%s" (buffer-string))
3732 (when (not found)
3733 (if timeout
3734 (tramp-error
3735 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
3736 regexp timeout)
3737 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
3738 found)))
3739
3740 ;; It seems that Tru64 Unix does not like it if long strings are sent
3741 ;; to it in one go. (This happens when sending the Perl
3742 ;; `file-attributes' implementation, for instance.) Therefore, we
3743 ;; have this function which sends the string in chunks.
3744 (defun tramp-send-string (vec string)
3745 "Send the STRING via connection VEC.
3746
3747 The STRING is expected to use Unix line-endings, but the lines sent to
3748 the remote host use line-endings as defined in the variable
3749 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3750 (let* ((p (tramp-get-connection-process vec))
3751 (chunksize (tramp-get-connection-property p "chunksize" nil)))
3752 (unless p
3753 (tramp-error
3754 vec 'file-error "Can't send string to remote host -- not logged in"))
3755 (tramp-set-connection-property p "last-cmd-time" (current-time))
3756 (tramp-message vec 10 "%s" string)
3757 (with-current-buffer (tramp-get-connection-buffer vec)
3758 ;; Clean up the buffer. We cannot call `erase-buffer' because
3759 ;; narrowing might be in effect.
3760 (let (buffer-read-only) (delete-region (point-min) (point-max)))
3761 ;; Replace "\n" by `tramp-rsh-end-of-line'.
3762 (setq string
3763 (mapconcat 'identity
3764 (tramp-compat-split-string string "\n")
3765 tramp-rsh-end-of-line))
3766 (unless (or (string= string "")
3767 (string-equal (substring string -1) tramp-rsh-end-of-line))
3768 (setq string (concat string tramp-rsh-end-of-line)))
3769 ;; Send the string.
3770 (if (and chunksize (not (zerop chunksize)))
3771 (let ((pos 0)
3772 (end (length string)))
3773 (while (< pos end)
3774 (tramp-message
3775 vec 10 "Sending chunk from %s to %s"
3776 pos (min (+ pos chunksize) end))
3777 (process-send-string
3778 p (substring string pos (min (+ pos chunksize) end)))
3779 (setq pos (+ pos chunksize))))
3780 (process-send-string p string)))))
3781
3782 (defun tramp-get-inode (vec)
3783 "Returns the virtual inode number.
3784 If it doesn't exist, generate a new one."
3785 (with-tramp-file-property vec (tramp-file-name-localname vec) "inode"
3786 (setq tramp-inodes (1+ tramp-inodes))))
3787
3788 (defun tramp-get-device (vec)
3789 "Returns the virtual device number.
3790 If it doesn't exist, generate a new one."
3791 (with-tramp-connection-property (tramp-get-connection-process vec) "device"
3792 (cons -1 (setq tramp-devices (1+ tramp-devices)))))
3793
3794 (defun tramp-equal-remote (file1 file2)
3795 "Check, whether the remote parts of FILE1 and FILE2 are identical.
3796 The check depends on method, user and host name of the files. If
3797 one of the components is missing, the default values are used.
3798 The local file name parts of FILE1 and FILE2 are not taken into
3799 account.
3800
3801 Example:
3802
3803 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
3804
3805 would yield t. On the other hand, the following check results in nil:
3806
3807 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
3808 (and (tramp-tramp-file-p file1)
3809 (tramp-tramp-file-p file2)
3810 (string-equal (file-remote-p file1) (file-remote-p file2))))
3811
3812 ;;;###tramp-autoload
3813 (defun tramp-mode-string-to-int (mode-string)
3814 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
3815 (let* (case-fold-search
3816 (mode-chars (string-to-vector mode-string))
3817 (owner-read (aref mode-chars 1))
3818 (owner-write (aref mode-chars 2))
3819 (owner-execute-or-setid (aref mode-chars 3))
3820 (group-read (aref mode-chars 4))
3821 (group-write (aref mode-chars 5))
3822 (group-execute-or-setid (aref mode-chars 6))
3823 (other-read (aref mode-chars 7))
3824 (other-write (aref mode-chars 8))
3825 (other-execute-or-sticky (aref mode-chars 9)))
3826 (save-match-data
3827 (logior
3828 (cond
3829 ((char-equal owner-read ?r) (tramp-compat-octal-to-decimal "00400"))
3830 ((char-equal owner-read ?-) 0)
3831 (t (error "Second char `%c' must be one of `r-'" owner-read)))
3832 (cond
3833 ((char-equal owner-write ?w) (tramp-compat-octal-to-decimal "00200"))
3834 ((char-equal owner-write ?-) 0)
3835 (t (error "Third char `%c' must be one of `w-'" owner-write)))
3836 (cond
3837 ((char-equal owner-execute-or-setid ?x)
3838 (tramp-compat-octal-to-decimal "00100"))
3839 ((char-equal owner-execute-or-setid ?S)
3840 (tramp-compat-octal-to-decimal "04000"))
3841 ((char-equal owner-execute-or-setid ?s)
3842 (tramp-compat-octal-to-decimal "04100"))
3843 ((char-equal owner-execute-or-setid ?-) 0)
3844 (t (error "Fourth char `%c' must be one of `xsS-'"
3845 owner-execute-or-setid)))
3846 (cond
3847 ((char-equal group-read ?r) (tramp-compat-octal-to-decimal "00040"))
3848 ((char-equal group-read ?-) 0)
3849 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
3850 (cond
3851 ((char-equal group-write ?w) (tramp-compat-octal-to-decimal "00020"))
3852 ((char-equal group-write ?-) 0)
3853 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
3854 (cond
3855 ((char-equal group-execute-or-setid ?x)
3856 (tramp-compat-octal-to-decimal "00010"))
3857 ((char-equal group-execute-or-setid ?S)
3858 (tramp-compat-octal-to-decimal "02000"))
3859 ((char-equal group-execute-or-setid ?s)
3860 (tramp-compat-octal-to-decimal "02010"))
3861 ((char-equal group-execute-or-setid ?-) 0)
3862 (t (error "Seventh char `%c' must be one of `xsS-'"
3863 group-execute-or-setid)))
3864 (cond
3865 ((char-equal other-read ?r)
3866 (tramp-compat-octal-to-decimal "00004"))
3867 ((char-equal other-read ?-) 0)
3868 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
3869 (cond
3870 ((char-equal other-write ?w) (tramp-compat-octal-to-decimal "00002"))
3871 ((char-equal other-write ?-) 0)
3872 (t (error "Ninth char `%c' must be one of `w-'" other-write)))
3873 (cond
3874 ((char-equal other-execute-or-sticky ?x)
3875 (tramp-compat-octal-to-decimal "00001"))
3876 ((char-equal other-execute-or-sticky ?T)
3877 (tramp-compat-octal-to-decimal "01000"))
3878 ((char-equal other-execute-or-sticky ?t)
3879 (tramp-compat-octal-to-decimal "01001"))
3880 ((char-equal other-execute-or-sticky ?-) 0)
3881 (t (error "Tenth char `%c' must be one of `xtT-'"
3882 other-execute-or-sticky)))))))
3883
3884 (defconst tramp-file-mode-type-map
3885 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
3886 (1 . "p") ; fifo
3887 (2 . "c") ; character device
3888 (3 . "m") ; multiplexed character device (v7)
3889 (4 . "d") ; directory
3890 (5 . "?") ; Named special file (XENIX)
3891 (6 . "b") ; block device
3892 (7 . "?") ; multiplexed block device (v7)
3893 (8 . "-") ; regular file
3894 (9 . "n") ; network special file (HP-UX)
3895 (10 . "l") ; symlink
3896 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
3897 (12 . "s") ; socket
3898 (13 . "D") ; door special (Solaris)
3899 (14 . "w")) ; whiteout (BSD)
3900 "A list of file types returned from the `stat' system call.
3901 This is used to map a mode number to a permission string.")
3902
3903 ;;;###tramp-autoload
3904 (defun tramp-file-mode-from-int (mode)
3905 "Turn an integer representing a file mode into an ls(1)-like string."
3906 (let ((type (cdr
3907 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
3908 (user (logand (lsh mode -6) 7))
3909 (group (logand (lsh mode -3) 7))
3910 (other (logand (lsh mode -0) 7))
3911 (suid (> (logand (lsh mode -9) 4) 0))
3912 (sgid (> (logand (lsh mode -9) 2) 0))
3913 (sticky (> (logand (lsh mode -9) 1) 0)))
3914 (setq user (tramp-file-mode-permissions user suid "s"))
3915 (setq group (tramp-file-mode-permissions group sgid "s"))
3916 (setq other (tramp-file-mode-permissions other sticky "t"))
3917 (concat type user group other)))
3918
3919 (defun tramp-file-mode-permissions (perm suid suid-text)
3920 "Convert a permission bitset into a string.
3921 This is used internally by `tramp-file-mode-from-int'."
3922 (let ((r (> (logand perm 4) 0))
3923 (w (> (logand perm 2) 0))
3924 (x (> (logand perm 1) 0)))
3925 (concat (or (and r "r") "-")
3926 (or (and w "w") "-")
3927 (or (and suid x suid-text) ; suid, execute
3928 (and suid (upcase suid-text)) ; suid, !execute
3929 (and x "x") "-")))) ; !suid
3930
3931 ;;;###tramp-autoload
3932 (defun tramp-get-local-uid (id-format)
3933 (if (equal id-format 'integer) (user-uid) (user-login-name)))
3934
3935 ;;;###tramp-autoload
3936 (defun tramp-get-local-gid (id-format)
3937 (if (and (fboundp 'group-gid) (equal id-format 'integer))
3938 (tramp-compat-funcall 'group-gid)
3939 (nth 3 (tramp-compat-file-attributes "~/" id-format))))
3940
3941 ;;;###tramp-autoload
3942 (defun tramp-check-cached-permissions (vec access)
3943 "Check `file-attributes' caches for VEC.
3944 Return t if according to the cache access type ACCESS is known to
3945 be granted."
3946 (let ((result nil)
3947 (offset (cond
3948 ((eq ?r access) 1)
3949 ((eq ?w access) 2)
3950 ((eq ?x access) 3))))
3951 (dolist (suffix '("string" "integer") result)
3952 (setq
3953 result
3954 (or
3955 result
3956 (let ((file-attr
3957 (or
3958 (tramp-get-file-property
3959 vec (tramp-file-name-localname vec)
3960 (concat "file-attributes-" suffix) nil)
3961 (tramp-compat-file-attributes
3962 (tramp-make-tramp-file-name
3963 (tramp-file-name-method vec)
3964 (tramp-file-name-user vec)
3965 (tramp-file-name-host vec)
3966 (tramp-file-name-localname vec)
3967 (tramp-file-name-hop vec))
3968 (intern suffix))))
3969 (remote-uid
3970 (tramp-get-connection-property
3971 vec (concat "uid-" suffix) nil))
3972 (remote-gid
3973 (tramp-get-connection-property
3974 vec (concat "gid-" suffix) nil)))
3975 (and
3976 file-attr
3977 (or
3978 ;; Not a symlink
3979 (eq t (car file-attr))
3980 (null (car file-attr)))
3981 (or
3982 ;; World accessible.
3983 (eq access (aref (nth 8 file-attr) (+ offset 6)))
3984 ;; User accessible and owned by user.
3985 (and
3986 (eq access (aref (nth 8 file-attr) offset))
3987 (equal remote-uid (nth 2 file-attr)))
3988 ;; Group accessible and owned by user's
3989 ;; principal group.
3990 (and
3991 (eq access (aref (nth 8 file-attr) (+ offset 3)))
3992 (equal remote-gid (nth 3 file-attr)))))))))))
3993
3994 ;;;###tramp-autoload
3995 (defun tramp-local-host-p (vec)
3996 "Return t if this points to the local host, nil otherwise."
3997 ;; We cannot use `tramp-file-name-real-host'. A port is an
3998 ;; indication for an ssh tunnel or alike.
3999 (let ((host (tramp-file-name-host vec)))
4000 (and
4001 (stringp host)
4002 (string-match tramp-local-host-regexp host)
4003 ;; The method shall be applied to one of the shell file name
4004 ;; handlers. `tramp-local-host-p' is also called for "smb" and
4005 ;; alike, where it must fail.
4006 (tramp-get-method-parameter vec 'tramp-login-program)
4007 ;; The local temp directory must be writable for the other user.
4008 (file-writable-p
4009 (tramp-make-tramp-file-name
4010 (tramp-file-name-method vec)
4011 (tramp-file-name-user vec)
4012 host
4013 (tramp-compat-temporary-file-directory)))
4014 ;; On some systems, chown runs only for root.
4015 (or (zerop (user-uid))
4016 ;; This is defined in tramp-sh.el. Let's assume this is
4017 ;; loaded already.
4018 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec 'integer))))))
4019
4020 (defun tramp-get-remote-tmpdir (vec)
4021 "Return directory for temporary files on the remote host identified by VEC."
4022 (when (file-remote-p (tramp-get-connection-property vec "tmpdir" ""))
4023 ;; Compatibility code: Cached value shall be the local path only.
4024 (tramp-set-connection-property vec "tmpdir" 'undef))
4025 (let ((dir (tramp-make-tramp-file-name
4026 (tramp-file-name-method vec)
4027 (tramp-file-name-user vec)
4028 (tramp-file-name-host vec)
4029 (or (tramp-get-method-parameter vec 'tramp-tmpdir) "/tmp"))))
4030 (with-tramp-connection-property vec "tmpdir"
4031 (or (and (file-directory-p dir) (file-writable-p dir)
4032 (tramp-file-name-handler 'file-remote-p dir 'localname))
4033 (tramp-error vec 'file-error "Directory %s not accessible" dir)))
4034 dir))
4035
4036 ;;;###tramp-autoload
4037 (defun tramp-make-tramp-temp-file (vec)
4038 "Create a temporary file on the remote host identified by VEC.
4039 Return the local name of the temporary file."
4040 (let ((prefix (expand-file-name
4041 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))
4042 result)
4043 (while (not result)
4044 ;; `make-temp-file' would be the natural choice for
4045 ;; implementation. But it calls `write-region' internally,
4046 ;; which also needs a temporary file - we would end in an
4047 ;; infinite loop.
4048 (setq result (make-temp-name prefix))
4049 (if (file-exists-p result)
4050 (setq result nil)
4051 ;; This creates the file by side effect.
4052 (set-file-times result)
4053 (set-file-modes result (tramp-compat-octal-to-decimal "0700"))))
4054
4055 ;; Return the local part.
4056 (with-parsed-tramp-file-name result nil localname)))
4057
4058 (defun tramp-delete-temp-file-function ()
4059 "Remove temporary files related to current buffer."
4060 (when (stringp tramp-temp-buffer-file-name)
4061 (ignore-errors (delete-file tramp-temp-buffer-file-name))))
4062
4063 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
4064 (add-hook 'tramp-unload-hook
4065 (lambda ()
4066 (remove-hook 'kill-buffer-hook
4067 'tramp-delete-temp-file-function)))
4068
4069 ;;; Auto saving to a special directory:
4070 (defvar auto-save-file-name-transforms)
4071
4072 (defun tramp-handle-make-auto-save-file-name ()
4073 "Like `make-auto-save-file-name' for Tramp files.
4074 Returns a file name in `tramp-auto-save-directory' for autosaving
4075 this file, if that variable is non-nil."
4076 (when (stringp tramp-auto-save-directory)
4077 (setq tramp-auto-save-directory
4078 (expand-file-name tramp-auto-save-directory)))
4079 ;; Create directory.
4080 (unless (or (null tramp-auto-save-directory)
4081 (file-exists-p tramp-auto-save-directory))
4082 (make-directory tramp-auto-save-directory t))
4083
4084 (let ((system-type 'not-windows)
4085 (auto-save-file-name-transforms
4086 (if (and (null tramp-auto-save-directory)
4087 (boundp 'auto-save-file-name-transforms))
4088 (symbol-value 'auto-save-file-name-transforms)))
4089 (buffer-file-name
4090 (if (null tramp-auto-save-directory)
4091 buffer-file-name
4092 (expand-file-name
4093 (tramp-subst-strs-in-string
4094 '(("_" . "|")
4095 ("/" . "_a")
4096 (":" . "_b")
4097 ("|" . "__")
4098 ("[" . "_l")
4099 ("]" . "_r"))
4100 (buffer-file-name))
4101 tramp-auto-save-directory))))
4102 ;; Run plain `make-auto-save-file-name'. There might be an advice when
4103 ;; it is not a magic file name operation (since Emacs 22).
4104 ;; We must deactivate it temporarily.
4105 (if (not (ad-is-active 'make-auto-save-file-name))
4106 (tramp-run-real-handler 'make-auto-save-file-name nil)
4107 ;; else
4108 (ad-deactivate 'make-auto-save-file-name)
4109 (prog1
4110 (tramp-run-real-handler 'make-auto-save-file-name nil)
4111 (ad-activate 'make-auto-save-file-name)))))
4112
4113 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
4114 (defadvice make-auto-save-file-name
4115 (around tramp-advice-make-auto-save-file-name () activate)
4116 "Invoke `tramp-*-handle-make-auto-save-file-name' for Tramp files."
4117 (if (tramp-tramp-file-p (buffer-file-name))
4118 ;; We cannot call `tramp-handle-make-auto-save-file-name'
4119 ;; directly, because this would bypass the locking mechanism.
4120 (setq ad-return-value
4121 (tramp-file-name-handler 'make-auto-save-file-name))
4122 ad-do-it))
4123 (add-hook
4124 'tramp-unload-hook
4125 (lambda ()
4126 (ad-remove-advice
4127 'make-auto-save-file-name
4128 'around 'tramp-advice-make-auto-save-file-name)
4129 (ad-activate 'make-auto-save-file-name))))
4130
4131 ;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
4132 ;; umask. This is a security threat.
4133
4134 (defun tramp-set-auto-save-file-modes ()
4135 "Set permissions of autosaved remote files to the original permissions."
4136 (let ((bfn (buffer-file-name)))
4137 (when (and (tramp-tramp-file-p bfn)
4138 (buffer-modified-p)
4139 (stringp buffer-auto-save-file-name)
4140 (not (equal bfn buffer-auto-save-file-name)))
4141 (unless (file-exists-p buffer-auto-save-file-name)
4142 (write-region "" nil buffer-auto-save-file-name))
4143 ;; Permissions should be set always, because there might be an old
4144 ;; auto-saved file belonging to another original file. This could
4145 ;; be a security threat.
4146 (set-file-modes
4147 buffer-auto-save-file-name
4148 (or (file-modes bfn) (tramp-compat-octal-to-decimal "0600"))))))
4149
4150 (unless (and (featurep 'xemacs)
4151 (= emacs-major-version 21)
4152 (> emacs-minor-version 4))
4153 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
4154 (add-hook 'tramp-unload-hook
4155 (lambda ()
4156 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
4157
4158 (defun tramp-subst-strs-in-string (alist string)
4159 "Replace all occurrences of the string FROM with TO in STRING.
4160 ALIST is of the form ((FROM . TO) ...)."
4161 (save-match-data
4162 (while alist
4163 (let* ((pr (car alist))
4164 (from (car pr))
4165 (to (cdr pr)))
4166 (while (string-match (regexp-quote from) string)
4167 (setq string (replace-match to t t string)))
4168 (setq alist (cdr alist))))
4169 string))
4170
4171 ;;; Compatibility functions section:
4172
4173 (defun tramp-call-process
4174 (vec program &optional infile destination display &rest args)
4175 "Calls `call-process' on the local host.
4176 It always returns a return code. The Lisp error raised when
4177 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4178 are written with verbosity of 6."
4179 (let ((v (or vec
4180 (vector tramp-current-method tramp-current-user
4181 tramp-current-host nil nil)))
4182 (destination (if (eq destination t) (current-buffer) destination))
4183 result)
4184 (tramp-message
4185 v 6 "`%s %s' %s %s"
4186 program (mapconcat 'identity args " ") infile destination)
4187 (condition-case err
4188 (with-temp-buffer
4189 (setq result
4190 (apply
4191 'call-process program infile (or destination t) display args))
4192 ;; `result' could also be an error string.
4193 (when (stringp result)
4194 (signal 'file-error (list result)))
4195 (with-current-buffer
4196 (if (bufferp destination) destination (current-buffer))
4197 (tramp-message v 6 "%d\n%s" result (buffer-string))))
4198 (error
4199 (setq result 1)
4200 (tramp-message v 6 "%d\n%s" result (error-message-string err))))
4201 result))
4202
4203 (defun tramp-call-process-region
4204 (vec start end program &optional delete buffer display &rest args)
4205 "Calls `call-process-region' on the local host.
4206 It always returns a return code. The Lisp error raised when
4207 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4208 are written with verbosity of 6."
4209 (let ((v (or vec
4210 (vector tramp-current-method tramp-current-user
4211 tramp-current-host nil nil)))
4212 (buffer (if (eq buffer t) (current-buffer) buffer))
4213 result)
4214 (tramp-message
4215 v 6 "`%s %s' %s %s %s %s"
4216 program (mapconcat 'identity args " ") start end delete buffer)
4217 (condition-case err
4218 (progn
4219 (setq result
4220 (apply
4221 'call-process-region
4222 start end program delete buffer display args))
4223 ;; `result' could also be an error string.
4224 (when (stringp result)
4225 (signal 'file-error (list result)))
4226 (with-current-buffer (if (bufferp buffer) buffer (current-buffer))
4227 (if (zerop result)
4228 (tramp-message v 6 "%d" result)
4229 (tramp-message v 6 "%d\n%s" result (buffer-string)))))
4230 (error
4231 (setq result 1)
4232 (tramp-message v 6 "%d\n%s" result (error-message-string err))))
4233 result))
4234
4235 ;;;###tramp-autoload
4236 (defun tramp-read-passwd (proc &optional prompt)
4237 "Read a password from user (compat function).
4238 Consults the auth-source package.
4239 Invokes `password-read' if available, `read-passwd' else."
4240 (let* ((case-fold-search t)
4241 (key (tramp-make-tramp-file-name
4242 tramp-current-method tramp-current-user
4243 tramp-current-host ""))
4244 (pw-prompt
4245 (or prompt
4246 (with-current-buffer (process-buffer proc)
4247 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
4248 (format "%s for %s " (capitalize (match-string 1)) key))))
4249 ;; We suspend the timers while reading the password.
4250 (stimers (and (functionp 'with-timeout-suspend)
4251 (tramp-compat-funcall 'with-timeout-suspend)))
4252 auth-info auth-passwd)
4253
4254 (unwind-protect
4255 (with-parsed-tramp-file-name key nil
4256 (prog1
4257 (or
4258 ;; See if auth-sources contains something useful, if
4259 ;; it's bound. `auth-source-user-or-password' is an
4260 ;; obsoleted function, it has been replaced by
4261 ;; `auth-source-search'.
4262 (ignore-errors
4263 (and (boundp 'auth-sources)
4264 (tramp-get-connection-property
4265 v "first-password-request" nil)
4266 ;; Try with Tramp's current method.
4267 (if (fboundp 'auth-source-search)
4268 (setq auth-info
4269 (tramp-compat-funcall
4270 'auth-source-search
4271 :max 1
4272 :user (or tramp-current-user t)
4273 :host tramp-current-host
4274 :port tramp-current-method)
4275 auth-passwd (plist-get
4276 (nth 0 auth-info) :secret)
4277 auth-passwd (if (functionp auth-passwd)
4278 (funcall auth-passwd)
4279 auth-passwd))
4280 (tramp-compat-funcall
4281 'auth-source-user-or-password
4282 "password" tramp-current-host tramp-current-method))))
4283 ;; Try the password cache.
4284 (when (functionp 'password-read)
4285 (let ((password
4286 (tramp-compat-funcall 'password-read pw-prompt key)))
4287 (tramp-compat-funcall 'password-cache-add key password)
4288 password))
4289 ;; Else, get the password interactively.
4290 (read-passwd pw-prompt))
4291 (tramp-set-connection-property v "first-password-request" nil)))
4292 ;; Reenable the timers.
4293 (and (functionp 'with-timeout-unsuspend)
4294 (tramp-compat-funcall 'with-timeout-unsuspend stimers)))))
4295
4296 ;;;###tramp-autoload
4297 (defun tramp-clear-passwd (vec)
4298 "Clear password cache for connection related to VEC."
4299 (let ((hop (tramp-file-name-hop vec)))
4300 (when hop
4301 ;; Clear also the passwords of the hops.
4302 (tramp-clear-passwd
4303 (tramp-dissect-file-name
4304 (concat
4305 tramp-prefix-format
4306 (tramp-compat-replace-regexp-in-string
4307 (concat tramp-postfix-hop-regexp "$")
4308 tramp-postfix-host-format hop))))))
4309 (tramp-compat-funcall
4310 'password-cache-remove
4311 (tramp-make-tramp-file-name
4312 (tramp-file-name-method vec)
4313 (tramp-file-name-user vec)
4314 (tramp-file-name-host vec)
4315 "")))
4316
4317 ;; Snarfed code from time-date.el and parse-time.el
4318
4319 (defconst tramp-half-a-year '(241 17024)
4320 "Evaluated by \"(days-to-time 183)\".")
4321
4322 (defconst tramp-parse-time-months
4323 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
4324 ("apr" . 4) ("may" . 5) ("jun" . 6)
4325 ("jul" . 7) ("aug" . 8) ("sep" . 9)
4326 ("oct" . 10) ("nov" . 11) ("dec" . 12))
4327 "Alist mapping month names to integers.")
4328
4329 ;;;###tramp-autoload
4330 (defun tramp-time-diff (t1 t2)
4331 "Return the difference between the two times, in seconds.
4332 T1 and T2 are time values (as returned by `current-time' for example)."
4333 (cond ((and (fboundp 'subtract-time)
4334 (fboundp 'float-time))
4335 (tramp-compat-funcall
4336 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
4337 ((and (fboundp 'subtract-time)
4338 (fboundp 'time-to-seconds))
4339 (tramp-compat-funcall
4340 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
4341 ((fboundp 'itimer-time-difference)
4342 (tramp-compat-funcall
4343 'itimer-time-difference
4344 (if (< (length t1) 3) (append t1 '(0)) t1)
4345 (if (< (length t2) 3) (append t2 '(0)) t2)))
4346 (t
4347 (let ((time (time-subtract t1 t2)))
4348 (+ (* (car time) 65536.0)
4349 (cadr time)
4350 (/ (or (nth 2 time) 0) 1000000.0))))))
4351
4352 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
4353 ;; does not deal well with newline characters. Newline is replaced by
4354 ;; backslash newline. But if, say, the string `a backslash newline b'
4355 ;; is passed to a shell, the shell will expand this into "ab",
4356 ;; completely omitting the newline. This is not what was intended.
4357 ;; It does not appear to be possible to make the function
4358 ;; `shell-quote-argument' work with newlines without making it
4359 ;; dependent on the shell used. But within this package, we know that
4360 ;; we will always use a Bourne-like shell, so we use an approach which
4361 ;; groks newlines.
4362 ;;
4363 ;; The approach is simple: we call `shell-quote-argument', then
4364 ;; massage the newline part of the result.
4365 ;;
4366 ;; This function should produce a string which is grokked by a Unix
4367 ;; shell, even if the Emacs is running on Windows. Since this is the
4368 ;; kludges section, we bind `system-type' in such a way that
4369 ;; `shell-quote-argument' behaves as if on Unix.
4370 ;;
4371 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
4372 ;; function to work with Bourne-like shells.
4373 ;;
4374 ;; CCC: This function should be rewritten so that
4375 ;; `shell-quote-argument' is not used. This way, we are safe from
4376 ;; changes in `shell-quote-argument'.
4377 ;;;###tramp-autoload
4378 (defun tramp-shell-quote-argument (s)
4379 "Similar to `shell-quote-argument', but groks newlines.
4380 Only works for Bourne-like shells."
4381 (let ((system-type 'not-windows))
4382 (save-match-data
4383 (let ((result (shell-quote-argument s))
4384 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
4385 (when (and (>= (length result) 2)
4386 (string= (substring result 0 2) "\\~"))
4387 (setq result (substring result 1)))
4388 (while (string-match nl result)
4389 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
4390 t t result)))
4391 result))))
4392
4393 ;;; Integration of eshell.el:
4394
4395 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4396 ;; when `default-directory' points to another host.
4397 (defun tramp-eshell-directory-change ()
4398 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4399 (setq eshell-path-env
4400 (if (tramp-tramp-file-p default-directory)
4401 (with-parsed-tramp-file-name default-directory nil
4402 (mapconcat
4403 'identity
4404 (or
4405 ;; When `tramp-own-remote-path' is in `tramp-remote-path',
4406 ;; the remote path is only set in the session cache.
4407 (tramp-get-connection-property
4408 (tramp-get-connection-process v) "remote-path" nil)
4409 (tramp-get-connection-property v "remote-path" nil))
4410 ":"))
4411 (getenv "PATH"))))
4412
4413 (eval-after-load "esh-util"
4414 '(progn
4415 (tramp-eshell-directory-change)
4416 (add-hook 'eshell-directory-change-hook
4417 'tramp-eshell-directory-change)
4418 (add-hook 'tramp-unload-hook
4419 (lambda ()
4420 (remove-hook 'eshell-directory-change-hook
4421 'tramp-eshell-directory-change)))))
4422
4423 ;; Checklist for `tramp-unload-hook'
4424 ;; - Unload all `tramp-*' packages
4425 ;; - Reset `file-name-handler-alist'
4426 ;; - Cleanup hooks where Tramp functions are in
4427 ;; - Cleanup advised functions
4428 ;; - Cleanup autoloads
4429 ;;;###autoload
4430 (defun tramp-unload-tramp ()
4431 "Discard Tramp from loading remote files."
4432 (interactive)
4433 ;; ange-ftp settings must be enabled.
4434 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
4435 ;; Maybe it's not loaded yet.
4436 (ignore-errors (unload-feature 'tramp 'force)))
4437
4438 (provide 'tramp)
4439
4440 ;;; TODO:
4441
4442 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
4443 ;; by the files in that directory. Add this here.
4444 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
4445 ;; * abbreviate-file-name
4446 ;; * Better error checking. At least whenever we see something
4447 ;; strange when doing zerop, we should kill the process and start
4448 ;; again. (Greg Stark)
4449 ;; * Username and hostname completion.
4450 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
4451 ;; * Make `tramp-default-user' obsolete.
4452 ;; * Implement a general server-local-variable mechanism, as there are
4453 ;; probably other variables that need different values for different
4454 ;; servers too. The user could then configure a variable (such as
4455 ;; tramp-server-local-variable-alist) to define any such variables
4456 ;; that they need to, which would then be let bound as appropriate
4457 ;; in tramp functions. (Jason Rumney)
4458 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
4459 ;; * I was wondering if it would be possible to use tramp even if I'm
4460 ;; actually using sshfs. But when I launch a command I would like
4461 ;; to get it executed on the remote machine where the files really
4462 ;; are. (Andrea Crotti)
4463 ;; * Run emerge on two remote files. Bug is described here:
4464 ;; <http://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
4465 ;; (Bug#6850)
4466 ;; * Use also port to distinguish connections. This is needed for
4467 ;; different hosts sitting behind a single router (distinguished by
4468 ;; different port numbers). (Tzvi Edelman)
4469
4470 ;;; tramp.el ends here
4471
4472 ;; Local Variables:
4473 ;; mode: Emacs-Lisp
4474 ;; coding: utf-8
4475 ;; End: