Commit | Line | Data |
---|---|---|
83bbd71b | 1 | ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*- |
b1a2b924 | 2 | ;;; tramp.el --- Transparent Remote Access, Multiple Protocol |
fb7933a3 | 3 | |
5ec2cc41 | 4 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
fb7933a3 | 5 | |
b1a2b924 | 6 | ;; Author: kai.grossjohann@gmx.net |
fb7933a3 KG |
7 | ;; Keywords: comm, processes |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; This package provides remote file editing, similar to ange-ftp. | |
29 | ;; The difference is that ange-ftp uses FTP to transfer files between | |
30 | ;; the local and the remote host, whereas tramp.el uses a combination | |
31 | ;; of rsh and rcp or other work-alike programs, such as ssh/scp. | |
32 | ;; | |
b1a2b924 | 33 | ;; For more detailed instructions, please see the info file. |
fb7933a3 KG |
34 | ;; |
35 | ;; Notes: | |
36 | ;; ----- | |
7432277c | 37 | ;; |
fb7933a3 KG |
38 | ;; This package only works for Emacs 20 and higher, and for XEmacs 21 |
39 | ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs | |
40 | ;; 19 is reported to have other problems. For XEmacs 21, you need the | |
41 | ;; package `fsf-compat' for the `with-timeout' macro.) | |
42 | ;; | |
43 | ;; This version might not work with pre-Emacs 21 VC unless VC is | |
44 | ;; loaded before tramp.el. Could you please test this and tell me about | |
45 | ;; the result? Thanks. | |
46 | ;; | |
47 | ;; Also see the todo list at the bottom of this file. | |
48 | ;; | |
b1a2b924 KG |
49 | ;; The current version of Tramp can be retrieved from the following URL: |
50 | ;; http://savannah.nongnu.org/download/tramp/ | |
fb7933a3 KG |
51 | ;; |
52 | ;; There's a mailing list for this, as well. Its name is: | |
b1a2b924 | 53 | ;; tramp-devel@mail.freesoftware.fsf.org |
fb7933a3 KG |
54 | ;; Send a mail with `help' in the subject (!) to the administration |
55 | ;; address for instructions on joining the list. The administration | |
56 | ;; address is: | |
c62c9d08 | 57 | ;; tramp-devel-request@mail.freesoftware.fsf.org |
fb7933a3 | 58 | ;; You can also use the Web to subscribe, under the following URL: |
c62c9d08 | 59 | ;; http://mail.freesoftware.fsf.org/mailman/listinfo/tramp-devel |
fb7933a3 KG |
60 | ;; |
61 | ;; For the adventurous, the current development sources are available | |
62 | ;; via CVS. You can find instructions about this at the following URL: | |
c62c9d08 | 63 | ;; http://savannah.gnu.org/projects/tramp/ |
fb7933a3 KG |
64 | ;; Click on "CVS" in the navigation bar near the top. |
65 | ;; | |
66 | ;; Don't forget to put on your asbestos longjohns, first! | |
67 | ||
68 | ;;; Code: | |
69 | ||
b1a2b924 KG |
70 | ;; The Tramp version number and bug report address, as prepared by configure. |
71 | (require 'trampver) | |
fb7933a3 KG |
72 | |
73 | (require 'timer) | |
74 | (require 'format-spec) ;from Gnus 5.8, also in tar ball | |
5ec2cc41 KG |
75 | ;; As long as password.el is not part of (X)Emacs, it shouldn't |
76 | ;; be mandatory | |
77 | (if (featurep 'xemacs) | |
78 | (load "password" 'noerror) | |
79 | (require 'password nil 'noerror)) ;from No Gnus, also in tar ball | |
80 | ||
dba28077 KG |
81 | ;; The explicit check is not necessary in Emacs, which provides the |
82 | ;; feature even if implemented in C, but it appears to be necessary | |
83 | ;; in XEmacs. | |
84 | (unless (and (fboundp 'base64-encode-region) | |
85 | (fboundp 'base64-decode-region)) | |
86 | (require 'base64)) ;for the mimencode methods | |
fb7933a3 KG |
87 | (require 'shell) |
88 | (require 'advice) | |
89 | ||
16674e4f KG |
90 | (autoload 'tramp-uuencode-region "tramp-uu" |
91 | "Implementation of `uuencode' in Lisp.") | |
92 | ||
93 | (unless (fboundp 'uudecode-decode-region) | |
94 | (autoload 'uudecode-decode-region "uudecode")) | |
95 | ||
b25a52cc KG |
96 | ;; XEmacs is distributed with few Lisp packages. Further packages are |
97 | ;; installed using EFS. If we use a unified filename format, then | |
98 | ;; Tramp is required in addition to EFS. (But why can't Tramp just | |
99 | ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS | |
100 | ;; just like before.) Another reason for using a separate filename | |
101 | ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but | |
102 | ;; Tramp only knows how to deal with `file-name-handler-alist', not | |
103 | ;; the other places. | |
104 | ;;;###autoload | |
105 | (defvar tramp-unified-filenames (not (featurep 'xemacs)) | |
106 | "Non-nil means to use unified Ange-FTP/Tramp filename syntax. | |
107 | Nil means to use a separate filename syntax for Tramp.") | |
fb7933a3 | 108 | |
4007ba5b KG |
109 | ;; Load foreign methods. Because they do require Tramp internally, this |
110 | ;; must be done with the `eval-after-load' trick. | |
111 | ||
112 | ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore. | |
113 | (unless (featurep 'xemacs) | |
114 | (eval-after-load "tramp" | |
115 | '(require 'tramp-ftp))) | |
b25a52cc KG |
116 | (when (and tramp-unified-filenames (featurep 'xemacs)) |
117 | (eval-after-load "tramp" | |
118 | '(require 'tramp-efs))) | |
4007ba5b KG |
119 | |
120 | ;; tramp-smb uses "smbclient" from Samba. | |
121 | ;; Not available under Cygwin and Windows, because they don't offer | |
122 | ;; "smbclient". And even not necessary there, because Emacs supports | |
7432277c | 123 | ;; UNC file names like "//host/share/localname". |
4007ba5b KG |
124 | (unless (memq system-type '(cygwin windows-nt)) |
125 | (eval-after-load "tramp" | |
126 | '(require 'tramp-smb))) | |
127 | ||
fb7933a3 KG |
128 | (eval-when-compile |
129 | (require 'cl) | |
130 | (require 'custom) | |
131 | ;; Emacs 19.34 compatibility hack -- is this needed? | |
132 | (or (>= emacs-major-version 20) | |
133 | (load "cl-seq"))) | |
134 | ||
135 | (unless (boundp 'custom-print-functions) | |
136 | (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4 | |
137 | ||
38c65fca | 138 | ;; Avoid byte-compiler warnings if the byte-compiler supports this. |
8daea7fc KG |
139 | ;; Currently, XEmacs supports this. |
140 | (eval-when-compile | |
141 | (when (fboundp 'byte-compiler-options) | |
38c65fca KG |
142 | (let (unused-vars) ; Pacify Emacs byte-compiler |
143 | (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler | |
144 | (byte-compiler-options (warnings (- unused-vars)))))) | |
145 | ||
146 | ;; `directory-sep-char' is an obsolete variable in Emacs. But it is | |
147 | ;; used in XEmacs, so we set it here and there. The following is needed | |
148 | ;; to pacify Emacs byte-compiler. | |
149 | (eval-when-compile | |
150 | (when (boundp 'byte-compile-not-obsolete-var) | |
151 | (setq byte-compile-not-obsolete-var 'directory-sep-char))) | |
152 | ||
153 | ;; XEmacs byte-compiler raises warning abouts `last-coding-system-used'. | |
154 | (eval-when-compile | |
155 | (unless (boundp 'last-coding-system-used) | |
156 | (defvar last-coding-system-used nil))) | |
8daea7fc | 157 | |
fb7933a3 KG |
158 | ;;; User Customizable Internal Variables: |
159 | ||
160 | (defgroup tramp nil | |
161 | "Edit remote files with a combination of rsh and rcp or similar programs." | |
162 | :group 'files) | |
163 | ||
821e6e36 | 164 | (defcustom tramp-verbose 9 |
fb7933a3 KG |
165 | "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose." |
166 | :group 'tramp | |
167 | :type 'integer) | |
168 | ||
169 | (defcustom tramp-debug-buffer nil | |
170 | "*Whether to send all commands and responses to a debug buffer." | |
171 | :group 'tramp | |
172 | :type 'boolean) | |
173 | ||
38c65fca KG |
174 | ;; Emacs case |
175 | (eval-and-compile | |
176 | (when (boundp 'backup-directory-alist) | |
177 | (defcustom tramp-backup-directory-alist nil | |
178 | "Alist of filename patterns and backup directory names. | |
179 | Each element looks like (REGEXP . DIRECTORY), with the same meaning like | |
180 | in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY | |
181 | is a local file name, the backup directory is prepended with Tramp file | |
182 | name prefix \(multi-method, method, user, host\) of file. | |
183 | ||
184 | \(setq tramp-backup-directory-alist backup-directory-alist\) | |
185 | ||
186 | gives the same backup policy for Tramp files on their hosts like the | |
187 | policy for local files." | |
188 | :group 'tramp | |
189 | :type '(repeat (cons (regexp :tag "Regexp matching filename") | |
190 | (directory :tag "Backup directory name")))))) | |
191 | ||
192 | ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because | |
193 | ;; the package "backup-dir" might not be loaded yet. | |
194 | (eval-and-compile | |
195 | (when (featurep 'xemacs) | |
196 | (defcustom tramp-bkup-backup-directory-info nil | |
197 | "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...)) | |
198 | It has the same meaning like `bkup-backup-directory-info' from package | |
199 | `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local | |
200 | file name, the backup directory is prepended with Tramp file name prefix | |
201 | \(multi-method, method, user, host\) of file. | |
202 | ||
203 | \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\) | |
204 | ||
205 | gives the same backup policy for Tramp files on their hosts like the | |
206 | policy for local files." | |
207 | :type '(repeat | |
208 | (list (regexp :tag "File regexp") | |
209 | (string :tag "Backup Dir") | |
210 | (set :inline t | |
211 | (const ok-create) | |
212 | (const full-path) | |
213 | (const prepend-name) | |
214 | (const search-upward)))) | |
215 | :group 'tramp))) | |
216 | ||
fb7933a3 KG |
217 | (defcustom tramp-auto-save-directory nil |
218 | "*Put auto-save files in this directory, if set. | |
219 | The idea is to use a local directory so that auto-saving is faster." | |
220 | :group 'tramp | |
221 | :type '(choice (const nil) | |
222 | string)) | |
223 | ||
16674e4f KG |
224 | (defcustom tramp-encoding-shell |
225 | (if (memq system-type '(windows-nt)) | |
226 | (getenv "COMSPEC") | |
227 | "/bin/sh") | |
228 | "*Use this program for encoding and decoding commands on the local host. | |
229 | This shell is used to execute the encoding and decoding command on the | |
230 | local host, so if you want to use `~' in those commands, you should | |
231 | choose a shell here which groks tilde expansion. `/bin/sh' normally | |
232 | does not understand tilde expansion. | |
233 | ||
234 | For encoding and deocding, commands like the following are executed: | |
235 | ||
236 | /bin/sh -c COMMAND < INPUT > OUTPUT | |
237 | ||
238 | This variable can be used to change the \"/bin/sh\" part. See the | |
239 | variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the | |
240 | variable `tramp-encoding-reads-stdin' to specify whether the commands read | |
241 | standard input or a file. | |
fb7933a3 KG |
242 | |
243 | Note that this variable is not used for remote commands. There are | |
244 | mechanisms in tramp.el which automatically determine the right shell to | |
245 | use for the remote host." | |
246 | :group 'tramp | |
247 | :type '(file :must-match t)) | |
248 | ||
16674e4f KG |
249 | (defcustom tramp-encoding-command-switch |
250 | (if (string-match "cmd\\.exe" tramp-encoding-shell) | |
251 | "/c" | |
252 | "-c") | |
253 | "*Use this switch together with `tramp-encoding-shell' for local commands. | |
254 | See the variable `tramp-encoding-shell' for more information." | |
255 | :group 'tramp | |
256 | :type 'string) | |
257 | ||
258 | (defcustom tramp-encoding-reads-stdin t | |
259 | "*If non-nil, encoding commands read from standard input. | |
260 | If nil, the filename is the last argument. | |
261 | ||
262 | Note that the commands always must write to standard output." | |
263 | :group 'tramp | |
264 | :type 'boolean) | |
265 | ||
90dc758d | 266 | (defcustom tramp-multi-sh-program |
16674e4f | 267 | tramp-encoding-shell |
90dc758d | 268 | "*Use this program for bootstrapping multi-hop connections. |
16674e4f | 269 | This variable is similar to `tramp-encoding-shell', but it is only used |
90dc758d KG |
270 | when initializing a multi-hop connection. Therefore, the set of |
271 | commands sent to this shell is quite restricted, and if you are | |
272 | careful it works to use CMD.EXE under Windows (instead of a Bourne-ish | |
273 | shell which does not normally exist on Windows anyway). | |
274 | ||
275 | To use multi-hop methods from Windows, you also need suitable entries | |
276 | in `tramp-multi-connection-function-alist' for the first hop. | |
277 | ||
16674e4f | 278 | This variable defaults to the value of `tramp-encoding-shell'." |
90dc758d KG |
279 | :group 'tramp |
280 | :type '(file :must-match t)) | |
281 | ||
fb7933a3 KG |
282 | ;; CCC I have changed all occurrences of comint-quote-filename with |
283 | ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files. | |
284 | ;; There, comint-quote-filename was removed altogether. If it turns | |
285 | ;; out to be necessary there, something will need to be done. | |
286 | ;;-(defcustom tramp-file-name-quote-list | |
287 | ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ ) | |
288 | ;;- "*Protect these characters from the remote shell. | |
289 | ;;-Any character in this list is quoted (preceded with a backslash) | |
290 | ;;-because it means something special to the shell. This takes effect | |
291 | ;;-when sending file and directory names to the remote shell. | |
292 | ;;- | |
293 | ;;-See `comint-file-name-quote-list' for details." | |
294 | ;;- :group 'tramp | |
295 | ;;- :type '(repeat character)) | |
296 | ||
297 | (defcustom tramp-methods | |
298 | '( ("rcp" (tramp-connection-function tramp-open-connection-rsh) | |
b25a52cc KG |
299 | (tramp-login-program "rsh") |
300 | (tramp-copy-program "rcp") | |
fb7933a3 | 301 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
302 | (tramp-login-args nil) |
303 | (tramp-copy-args nil) | |
90f8dc03 KG |
304 | (tramp-copy-keep-date-arg "-p") |
305 | (tramp-password-end-of-line nil)) | |
fb7933a3 | 306 | ("scp" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
307 | (tramp-login-program "ssh") |
308 | (tramp-copy-program "scp") | |
fb7933a3 | 309 | (tramp-remote-sh "/bin/sh") |
b25a52cc | 310 | (tramp-login-args ("-e" "none")) |
ea9d1443 | 311 | (tramp-copy-args nil) |
90f8dc03 KG |
312 | (tramp-copy-keep-date-arg "-p") |
313 | (tramp-password-end-of-line nil)) | |
fb7933a3 | 314 | ("scp1" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
315 | (tramp-login-program "ssh") |
316 | (tramp-copy-program "scp") | |
90dc758d | 317 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
318 | (tramp-login-args ("-1" "-e" "none")) |
319 | (tramp-copy-args ("-1")) | |
90f8dc03 KG |
320 | (tramp-copy-keep-date-arg "-p") |
321 | (tramp-password-end-of-line nil)) | |
90dc758d | 322 | ("scp2" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
323 | (tramp-login-program "ssh") |
324 | (tramp-copy-program "scp") | |
90dc758d | 325 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
326 | (tramp-login-args ("-2" "-e" "none")) |
327 | (tramp-copy-args ("-2")) | |
90f8dc03 KG |
328 | (tramp-copy-keep-date-arg "-p") |
329 | (tramp-password-end-of-line nil)) | |
3b89d388 | 330 | ("scp1_old" |
ac474af1 | 331 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
332 | (tramp-login-program "ssh1") |
333 | (tramp-copy-program "scp1") | |
fb7933a3 | 334 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
335 | (tramp-login-args ("-e" "none")) |
336 | (tramp-copy-args nil) | |
90f8dc03 KG |
337 | (tramp-copy-keep-date-arg "-p") |
338 | (tramp-password-end-of-line nil)) | |
3b89d388 | 339 | ("scp2_old" |
ac474af1 | 340 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
341 | (tramp-login-program "ssh2") |
342 | (tramp-copy-program "scp2") | |
fb7933a3 | 343 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
344 | (tramp-login-args ("-e" "none")) |
345 | (tramp-copy-args nil) | |
90f8dc03 KG |
346 | (tramp-copy-keep-date-arg "-p") |
347 | (tramp-password-end-of-line nil)) | |
fb7933a3 | 348 | ("rsync" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
349 | (tramp-login-program "ssh") |
350 | (tramp-copy-program "rsync") | |
351 | (tramp-remote-sh "/bin/sh") | |
352 | (tramp-login-args ("-e" "none")) | |
353 | (tramp-copy-args ("-e" "ssh")) | |
90f8dc03 KG |
354 | (tramp-copy-keep-date-arg "-t") |
355 | (tramp-password-end-of-line nil)) | |
b25a52cc KG |
356 | ("remcp" (tramp-connection-function tramp-open-connection-rsh) |
357 | (tramp-login-program "remsh") | |
358 | (tramp-copy-program "rcp") | |
fb7933a3 | 359 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
360 | (tramp-login-args nil) |
361 | (tramp-copy-args nil) | |
90f8dc03 KG |
362 | (tramp-copy-keep-date-arg "-p") |
363 | (tramp-password-end-of-line nil)) | |
ac474af1 | 364 | ("rsh" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
365 | (tramp-login-program "rsh") |
366 | (tramp-copy-program nil) | |
fb7933a3 | 367 | (tramp-remote-sh "/bin/sh") |
b25a52cc | 368 | (tramp-login-args nil) |
ea9d1443 | 369 | (tramp-copy-args nil) |
90f8dc03 KG |
370 | (tramp-copy-keep-date-arg nil) |
371 | (tramp-password-end-of-line nil)) | |
ac474af1 | 372 | ("ssh" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
373 | (tramp-login-program "ssh") |
374 | (tramp-copy-program nil) | |
fb7933a3 | 375 | (tramp-remote-sh "/bin/sh") |
b25a52cc | 376 | (tramp-login-args ("-e" "none")) |
ea9d1443 | 377 | (tramp-copy-args nil) |
90f8dc03 KG |
378 | (tramp-copy-keep-date-arg nil) |
379 | (tramp-password-end-of-line nil)) | |
ac474af1 | 380 | ("ssh1" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
381 | (tramp-login-program "ssh") |
382 | (tramp-copy-program nil) | |
90dc758d | 383 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
384 | (tramp-login-args ("-1" "-e" "none")) |
385 | (tramp-copy-args ("-1")) | |
90f8dc03 KG |
386 | (tramp-copy-keep-date-arg nil) |
387 | (tramp-password-end-of-line nil)) | |
ac474af1 | 388 | ("ssh2" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
389 | (tramp-login-program "ssh") |
390 | (tramp-copy-program nil) | |
90dc758d | 391 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
392 | (tramp-login-args ("-2" "-e" "none")) |
393 | (tramp-copy-args ("-2")) | |
90f8dc03 KG |
394 | (tramp-copy-keep-date-arg nil) |
395 | (tramp-password-end-of-line nil)) | |
3b89d388 | 396 | ("ssh1_old" |
ac474af1 | 397 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
398 | (tramp-login-program "ssh1") |
399 | (tramp-copy-program nil) | |
fb7933a3 | 400 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
401 | (tramp-login-args ("-e" "none")) |
402 | (tramp-copy-args nil) | |
90f8dc03 KG |
403 | (tramp-copy-keep-date-arg nil) |
404 | (tramp-password-end-of-line nil)) | |
3b89d388 | 405 | ("ssh2_old" |
ac474af1 | 406 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
407 | (tramp-login-program "ssh2") |
408 | (tramp-copy-program nil) | |
409 | (tramp-remote-sh "/bin/sh") | |
410 | (tramp-login-args ("-e" "none")) | |
411 | (tramp-copy-args nil) | |
90f8dc03 KG |
412 | (tramp-copy-keep-date-arg nil) |
413 | (tramp-password-end-of-line nil)) | |
b25a52cc KG |
414 | ("remsh" (tramp-connection-function tramp-open-connection-rsh) |
415 | (tramp-login-program "remsh") | |
416 | (tramp-copy-program nil) | |
fb7933a3 | 417 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
418 | (tramp-login-args nil) |
419 | (tramp-copy-args nil) | |
90f8dc03 KG |
420 | (tramp-copy-keep-date-arg nil) |
421 | (tramp-password-end-of-line nil)) | |
ac474af1 KG |
422 | ("telnet" |
423 | (tramp-connection-function tramp-open-connection-telnet) | |
b25a52cc KG |
424 | (tramp-login-program "telnet") |
425 | (tramp-copy-program nil) | |
fb7933a3 | 426 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
427 | (tramp-login-args nil) |
428 | (tramp-copy-args nil) | |
90f8dc03 KG |
429 | (tramp-copy-keep-date-arg nil) |
430 | (tramp-password-end-of-line nil)) | |
ac474af1 | 431 | ("su" (tramp-connection-function tramp-open-connection-su) |
b25a52cc KG |
432 | (tramp-login-program "su") |
433 | (tramp-copy-program nil) | |
fb7933a3 | 434 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
435 | (tramp-login-args ("-" "%u")) |
436 | (tramp-copy-args nil) | |
90f8dc03 KG |
437 | (tramp-copy-keep-date-arg nil) |
438 | (tramp-password-end-of-line nil)) | |
ac474af1 | 439 | ("sudo" (tramp-connection-function tramp-open-connection-su) |
b25a52cc KG |
440 | (tramp-login-program "sudo") |
441 | (tramp-copy-program nil) | |
fb7933a3 | 442 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
443 | (tramp-login-args ("-u" "%u" "-s" |
444 | "-p" "Password:")) | |
445 | (tramp-copy-args nil) | |
90f8dc03 KG |
446 | (tramp-copy-keep-date-arg nil) |
447 | (tramp-password-end-of-line nil)) | |
fb7933a3 | 448 | ("multi" (tramp-connection-function tramp-open-connection-multi) |
b25a52cc KG |
449 | (tramp-login-program nil) |
450 | (tramp-copy-program nil) | |
fb7933a3 | 451 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
452 | (tramp-login-args nil) |
453 | (tramp-copy-args nil) | |
90f8dc03 KG |
454 | (tramp-copy-keep-date-arg nil) |
455 | (tramp-password-end-of-line nil)) | |
fb7933a3 | 456 | ("scpx" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
457 | (tramp-login-program "ssh") |
458 | (tramp-copy-program "scp") | |
fb7933a3 | 459 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
460 | (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh")) |
461 | (tramp-copy-args nil) | |
90f8dc03 KG |
462 | (tramp-copy-keep-date-arg "-p") |
463 | (tramp-password-end-of-line nil)) | |
ac474af1 | 464 | ("sshx" (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
465 | (tramp-login-program "ssh") |
466 | (tramp-copy-program nil) | |
fb7933a3 | 467 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
468 | (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh")) |
469 | (tramp-copy-args nil) | |
90f8dc03 KG |
470 | (tramp-copy-keep-date-arg nil) |
471 | (tramp-password-end-of-line nil)) | |
ac474af1 | 472 | ("krlogin" |
fb7933a3 | 473 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
474 | (tramp-login-program "krlogin") |
475 | (tramp-copy-program nil) | |
fb7933a3 | 476 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
477 | (tramp-login-args ("-x")) |
478 | (tramp-copy-args nil) | |
90f8dc03 KG |
479 | (tramp-copy-keep-date-arg nil) |
480 | (tramp-password-end-of-line nil)) | |
ac474af1 | 481 | ("plink" |
fb7933a3 | 482 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
483 | (tramp-login-program "plink") |
484 | (tramp-copy-program nil) | |
fb7933a3 | 485 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
486 | (tramp-login-args ("-ssh")) ;optionally add "-v" |
487 | (tramp-copy-args nil) | |
90f8dc03 KG |
488 | (tramp-copy-keep-date-arg nil) |
489 | (tramp-password-end-of-line "xy")) ;see docstring for "xy" | |
b25a52cc KG |
490 | ("plink1" |
491 | (tramp-connection-function tramp-open-connection-rsh) | |
492 | (tramp-login-program "plink") | |
493 | (tramp-copy-program nil) | |
494 | (tramp-remote-sh "/bin/sh") | |
495 | (tramp-login-args ("-1" "-ssh")) ;optionally add "-v" | |
496 | (tramp-copy-args nil) | |
90f8dc03 KG |
497 | (tramp-copy-keep-date-arg nil) |
498 | (tramp-password-end-of-line "xy")) ;see docstring for "xy" | |
fb7933a3 KG |
499 | ("pscp" |
500 | (tramp-connection-function tramp-open-connection-rsh) | |
b25a52cc KG |
501 | (tramp-login-program "plink") |
502 | (tramp-copy-program "pscp") | |
fb7933a3 | 503 | (tramp-remote-sh "/bin/sh") |
b25a52cc KG |
504 | (tramp-login-args ("-ssh")) |
505 | (tramp-copy-args nil) | |
90f8dc03 KG |
506 | (tramp-copy-keep-date-arg "-p") |
507 | (tramp-password-end-of-line "xy")) ;see docstring for "xy" | |
7432277c | 508 | ("fcp" |
fb7933a3 | 509 | (tramp-connection-function tramp-open-connection-rsh) |
b25a52cc KG |
510 | (tramp-login-program "fsh") |
511 | (tramp-copy-program "fcp") | |
fb7933a3 | 512 | (tramp-remote-sh "/bin/sh -i") |
b25a52cc KG |
513 | (tramp-login-args ("sh" "-i")) |
514 | (tramp-copy-args nil) | |
90f8dc03 KG |
515 | (tramp-copy-keep-date-arg "-p") |
516 | (tramp-password-end-of-line nil)) | |
fb7933a3 KG |
517 | ) |
518 | "*Alist of methods for remote files. | |
519 | This is a list of entries of the form (NAME PARAM1 PARAM2 ...). | |
520 | Each NAME stands for a remote access method. Each PARAM is a | |
521 | pair of the form (KEY VALUE). The following KEYs are defined: | |
16674e4f | 522 | * `tramp-connection-function' |
fb7933a3 KG |
523 | This specifies the function to use to connect to the remote host. |
524 | Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet' | |
525 | and `tramp-open-connection-su' are defined. See the documentation | |
526 | of these functions for more details. | |
527 | * `tramp-remote-sh' | |
528 | This specifies the Bourne shell to use on the remote host. This | |
529 | MUST be a Bourne-like shell. It is normally not necessary to set | |
530 | this to any value other than \"/bin/sh\": tramp wants to use a shell | |
531 | which groks tilde expansion, but it can search for it. Also note | |
532 | that \"/bin/sh\" exists on all Unixen, this might not be true for | |
533 | the value that you decide to use. You Have Been Warned. | |
b25a52cc KG |
534 | * `tramp-login-program' |
535 | This specifies the name of the program to use for logging in to the | |
536 | remote host. Depending on `tramp-connection-function', this may be | |
537 | the name of rsh or a workalike program (when | |
538 | `tramp-connection-function' is `tramp-open-connection-rsh'), or the | |
539 | name of telnet or a workalike (for `tramp-open-connection-telnet'), | |
540 | or the name of su or a workalike (for `tramp-open-connection-su'). | |
541 | * `tramp-login-args' | |
fb7933a3 KG |
542 | This specifies the list of arguments to pass to the above |
543 | mentioned program. Please note that this is a list of arguments, | |
544 | that is, normally you don't want to put \"-a -b\" or \"-f foo\" | |
545 | here. Instead, you want two list elements, one for \"-a\" and one | |
546 | for \"-b\", or one for \"-f\" and one for \"foo\". | |
b25a52cc KG |
547 | If `tramp-connection-function' is `tramp-open-connection-su', then |
548 | \"%u\" in this list is replaced by the user name, and \"%%\" can | |
549 | be used to obtain a literal percent character. | |
550 | * `tramp-copy-program' | |
551 | This specifies the name of the program to use for remotely copying | |
552 | the file; this might be the absolute filename of rcp or the name of | |
553 | a workalike program. | |
554 | * `tramp-copy-args' | |
fb7933a3 | 555 | This specifies the list of parameters to pass to the above mentioned |
b25a52cc KG |
556 | program, the hints for `tramp-login-args' also apply here. |
557 | * `tramp-copy-keep-date-arg' | |
558 | This specifies the parameter to use for the copying program when the | |
559 | timestamp of the original file should be kept. For `rcp', use `-p', for | |
fb7933a3 | 560 | `rsync', use `-t'. |
90f8dc03 KG |
561 | * `tramp-password-end-of-line' |
562 | This specifies the string to use for terminating the line after | |
563 | submitting the password. If this method parameter is nil, then the | |
564 | value of the normal variable `tramp-default-password-end-of-line' | |
565 | is used. This parameter is necessary because the \"plink\" program | |
566 | requires any two characters after sending the password. These do | |
567 | not have to be newline or carriage return characters. Other login | |
568 | programs are happy with just one character, the newline character. | |
569 | We use \"xy\" as the value for methods using \"plink\". | |
b25a52cc KG |
570 | |
571 | What does all this mean? Well, you should specify `tramp-login-program' | |
572 | for all methods; this program is used to log in to the remote site. Then, | |
573 | there are two ways to actually transfer the files between the local and the | |
574 | remote side. One way is using an additional rcp-like program. If you want | |
575 | to do this, set `tramp-copy-program' in the method. | |
fb7933a3 KG |
576 | |
577 | Another possibility for file transfer is inline transfer, i.e. the | |
b25a52cc | 578 | file is passed through the same buffer used by `tramp-login-program'. In |
fb7933a3 | 579 | this case, the file contents need to be protected since the |
b25a52cc | 580 | `tramp-login-program' might use escape codes or the connection might not |
fb7933a3 | 581 | be eight-bit clean. Therefore, file contents are encoded for transit. |
16674e4f | 582 | See the variable `tramp-coding-commands' for details. |
fb7933a3 | 583 | |
16674e4f | 584 | So, to summarize: if the method is an out-of-band method, then you |
b25a52cc | 585 | must specify `tramp-copy-program' and `tramp-copy-args'. If it is an |
16674e4f KG |
586 | inline method, then these two parameters should be nil. Every method, |
587 | inline or out of band, must specify `tramp-connection-function' plus | |
b25a52cc | 588 | the associated arguments (for example, the login program if you chose |
fb7933a3 KG |
589 | `tramp-open-connection-telnet'). |
590 | ||
591 | Notes: | |
592 | ||
593 | When using `tramp-open-connection-su' the phrase `open connection to a | |
594 | remote host' sounds strange, but it is used nevertheless, for | |
595 | consistency. No connection is opened to a remote host, but `su' is | |
596 | started on the local host. You are not allowed to specify a remote | |
16674e4f | 597 | host other than `localhost' or the name of the local host." |
fb7933a3 KG |
598 | :group 'tramp |
599 | :type '(repeat | |
600 | (cons string | |
601 | (set (list (const tramp-connection-function) function) | |
b25a52cc | 602 | (list (const tramp-login-program) |
fb7933a3 | 603 | (choice (const nil) string)) |
b25a52cc | 604 | (list (const tramp-copy-program) |
fb7933a3 KG |
605 | (choice (const nil) string)) |
606 | (list (const tramp-remote-sh) | |
607 | (choice (const nil) string)) | |
b25a52cc KG |
608 | (list (const tramp-login-args) (repeat string)) |
609 | (list (const tramp-copy-args) (repeat string)) | |
610 | (list (const tramp-copy-keep-date-arg) | |
fb7933a3 | 611 | (choice (const nil) string)) |
fb7933a3 KG |
612 | (list (const tramp-encoding-command) |
613 | (choice (const nil) string)) | |
614 | (list (const tramp-decoding-command) | |
615 | (choice (const nil) string)) | |
616 | (list (const tramp-encoding-function) | |
617 | (choice (const nil) function)) | |
618 | (list (const tramp-decoding-function) | |
90f8dc03 KG |
619 | (choice (const nil) function)) |
620 | (list (const tramp-password-end-of-line) | |
621 | (choice (const nil) string)))))) | |
fb7933a3 KG |
622 | |
623 | (defcustom tramp-multi-methods '("multi" "multiu") | |
624 | "*List of multi-hop methods. | |
625 | Each entry in this list should be a method name as mentioned in the | |
626 | variable `tramp-methods'." | |
627 | :group 'tramp | |
628 | :type '(repeat string)) | |
629 | ||
630 | (defcustom tramp-multi-connection-function-alist | |
631 | '(("telnet" tramp-multi-connect-telnet "telnet %h%n") | |
632 | ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n") | |
b25a52cc | 633 | ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n") |
fb7933a3 | 634 | ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n") |
c951aecb | 635 | ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n") |
fb7933a3 | 636 | ("su" tramp-multi-connect-su "su - %u%n") |
b25a52cc | 637 | ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n")) |
fb7933a3 KG |
638 | "*List of connection functions for multi-hop methods. |
639 | Each list item is a list of three items (METHOD FUNCTION COMMAND), | |
640 | where METHOD is the name as used in the file name, FUNCTION is the | |
641 | function to be executed, and COMMAND is the shell command used for | |
642 | connecting. | |
643 | ||
644 | COMMAND may contain percent escapes. `%u' will be replaced with the | |
645 | user name, `%h' will be replaced with the host name, and `%n' will be | |
646 | replaced with an end-of-line character, as specified in the variable | |
647 | `tramp-rsh-end-of-line'. Use `%%' for a literal percent character. | |
648 | Note that the interpretation of the percent escapes also depends on | |
649 | the FUNCTION. For example, the `%u' escape is forbidden with the | |
650 | function `tramp-multi-connect-telnet'. See the documentation of the | |
651 | various functions for details." | |
652 | :group 'tramp | |
653 | :type '(repeat (list string function string))) | |
654 | ||
b25a52cc KG |
655 | (defcustom tramp-default-method |
656 | (if (and (fboundp 'executable-find) | |
657 | (executable-find "plink")) | |
658 | "plink" | |
659 | "ssh") | |
fb7933a3 | 660 | "*Default method to use for transferring files. |
c62c9d08 | 661 | See `tramp-methods' for possibilities. |
4007ba5b | 662 | Also see `tramp-default-method-alist'." |
c62c9d08 KG |
663 | :group 'tramp |
664 | :type 'string) | |
665 | ||
505edaeb | 666 | (defcustom tramp-default-method-alist |
4007ba5b | 667 | '(("\\`localhost\\'" "\\`root\\'" "su")) |
c62c9d08 KG |
668 | "*Default method to use for specific user/host pairs. |
669 | This is an alist of items (HOST USER METHOD). The first matching item | |
670 | specifies the method to use for a file name which does not specify a | |
671 | method. HOST and USER are regular expressions or nil, which is | |
672 | interpreted as a regular expression which always matches. If no entry | |
673 | matches, the variable `tramp-default-method' takes effect. | |
674 | ||
675 | If the file name does not specify the user, lookup is done using the | |
676 | empty string for the user name. | |
677 | ||
678 | See `tramp-methods' for a list of possibilities for METHOD." | |
679 | :group 'tramp | |
680 | :type '(repeat (list (regexp :tag "Host regexp") | |
681 | (regexp :tag "User regexp") | |
682 | (string :tag "Method")))) | |
683 | ||
16674e4f KG |
684 | ;; Default values for non-Unices seeked |
685 | (defconst tramp-completion-function-alist-rsh | |
686 | (unless (memq system-type '(windows-nt)) | |
687 | '((tramp-parse-rhosts "/etc/hosts.equiv") | |
688 | (tramp-parse-rhosts "~/.rhosts"))) | |
b25a52cc | 689 | "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.") |
16674e4f KG |
690 | |
691 | ;; Default values for non-Unices seeked | |
692 | (defconst tramp-completion-function-alist-ssh | |
693 | (unless (memq system-type '(windows-nt)) | |
5ec2cc41 KG |
694 | '((tramp-parse-rhosts "/etc/hosts.equiv") |
695 | (tramp-parse-rhosts "/etc/shosts.equiv") | |
696 | (tramp-parse-shosts "/etc/ssh_known_hosts") | |
697 | (tramp-parse-sconfig "/etc/ssh_config") | |
698 | (tramp-parse-shostkeys "/etc/ssh2/hostkeys") | |
699 | (tramp-parse-sknownhosts "/etc/ssh2/knownhosts") | |
700 | (tramp-parse-rhosts "~/.rhosts") | |
701 | (tramp-parse-rhosts "~/.shosts") | |
702 | (tramp-parse-shosts "~/.ssh/known_hosts") | |
703 | (tramp-parse-sconfig "~/.ssh/config") | |
704 | (tramp-parse-shostkeys "~/.ssh2/hostkeys") | |
705 | (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))) | |
b25a52cc | 706 | "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.") |
16674e4f KG |
707 | |
708 | ;; Default values for non-Unices seeked | |
709 | (defconst tramp-completion-function-alist-telnet | |
710 | (unless (memq system-type '(windows-nt)) | |
711 | '((tramp-parse-hosts "/etc/hosts"))) | |
b25a52cc | 712 | "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.") |
16674e4f KG |
713 | |
714 | ;; Default values for non-Unices seeked | |
715 | (defconst tramp-completion-function-alist-su | |
716 | (unless (memq system-type '(windows-nt)) | |
717 | '((tramp-parse-passwd "/etc/passwd"))) | |
b25a52cc | 718 | "Default list of (FUNCTION FILE) pairs to be examined for su methods.") |
292ffc15 | 719 | |
5ec2cc41 | 720 | (defvar tramp-completion-function-alist nil |
16674e4f KG |
721 | "*Alist of methods for remote files. |
722 | This is a list of entries of the form (NAME PAIR1 PAIR2 ...). | |
723 | Each NAME stands for a remote access method. Each PAIR is of the form | |
724 | \(FUNCTION FILE). FUNCTION is responsible to extract user names and host | |
725 | names from FILE for completion. The following predefined FUNCTIONs exists: | |
726 | ||
5ec2cc41 KG |
727 | * `tramp-parse-rhosts' for \"~/.rhosts\" like files, |
728 | * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files, | |
729 | * `tramp-parse-sconfig' for \"~/.ssh/config\" like files, | |
730 | * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files, | |
731 | * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files, | |
732 | * `tramp-parse-hosts' for \"/etc/hosts\" like files, | |
733 | * `tramp-parse-passwd' for \"/etc/passwd\" like files. | |
734 | * `tramp-parse-netrc' for \"~/.netrc\" like files. | |
735 | ||
736 | FUNCTION can also be a customer defined function. For more details see | |
737 | the info pages.") | |
738 | ||
739 | (eval-after-load "tramp" | |
740 | '(progn | |
741 | (tramp-set-completion-function | |
742 | "rcp" tramp-completion-function-alist-rsh) | |
743 | (tramp-set-completion-function | |
744 | "scp" tramp-completion-function-alist-ssh) | |
745 | (tramp-set-completion-function | |
746 | "scp1" tramp-completion-function-alist-ssh) | |
747 | (tramp-set-completion-function | |
748 | "scp2" tramp-completion-function-alist-ssh) | |
749 | (tramp-set-completion-function | |
750 | "scp1_old" tramp-completion-function-alist-ssh) | |
751 | (tramp-set-completion-function | |
752 | "scp2_old" tramp-completion-function-alist-ssh) | |
753 | (tramp-set-completion-function | |
754 | "rsync" tramp-completion-function-alist-rsh) | |
755 | (tramp-set-completion-function | |
756 | "remcp" tramp-completion-function-alist-rsh) | |
757 | (tramp-set-completion-function | |
758 | "rsh" tramp-completion-function-alist-rsh) | |
759 | (tramp-set-completion-function | |
760 | "ssh" tramp-completion-function-alist-ssh) | |
761 | (tramp-set-completion-function | |
762 | "ssh1" tramp-completion-function-alist-ssh) | |
763 | (tramp-set-completion-function | |
764 | "ssh2" tramp-completion-function-alist-ssh) | |
765 | (tramp-set-completion-function | |
766 | "ssh1_old" tramp-completion-function-alist-ssh) | |
767 | (tramp-set-completion-function | |
768 | "ssh2_old" tramp-completion-function-alist-ssh) | |
769 | (tramp-set-completion-function | |
770 | "remsh" tramp-completion-function-alist-rsh) | |
771 | (tramp-set-completion-function | |
772 | "telnet" tramp-completion-function-alist-telnet) | |
773 | (tramp-set-completion-function | |
774 | "su" tramp-completion-function-alist-su) | |
775 | (tramp-set-completion-function | |
776 | "sudo" tramp-completion-function-alist-su) | |
777 | (tramp-set-completion-function | |
778 | "multi" nil) | |
779 | (tramp-set-completion-function | |
780 | "scpx" tramp-completion-function-alist-ssh) | |
781 | (tramp-set-completion-function | |
782 | "sshx" tramp-completion-function-alist-ssh) | |
783 | (tramp-set-completion-function | |
784 | "krlogin" tramp-completion-function-alist-rsh) | |
785 | (tramp-set-completion-function | |
786 | "plink" tramp-completion-function-alist-ssh) | |
787 | (tramp-set-completion-function | |
788 | "plink1" tramp-completion-function-alist-ssh) | |
789 | (tramp-set-completion-function | |
790 | "pscp" tramp-completion-function-alist-ssh) | |
791 | (tramp-set-completion-function | |
792 | "fcp" tramp-completion-function-alist-ssh))) | |
16674e4f | 793 | |
fb7933a3 KG |
794 | (defcustom tramp-rsh-end-of-line "\n" |
795 | "*String used for end of line in rsh connections. | |
796 | I don't think this ever needs to be changed, so please tell me about it | |
16674e4f | 797 | if you need to change this. |
90f8dc03 KG |
798 | Also see the method parameter `tramp-password-end-of-line' and the normal |
799 | variable `tramp-default-password-end-of-line'." | |
16674e4f KG |
800 | :group 'tramp |
801 | :type 'string) | |
802 | ||
90f8dc03 KG |
803 | (defcustom tramp-default-password-end-of-line |
804 | tramp-rsh-end-of-line | |
16674e4f | 805 | "*String used for end of line after sending a password. |
90f8dc03 KG |
806 | This variable provides the default value for the method parameter |
807 | `tramp-password-end-of-line', see `tramp-methods' for more details. | |
808 | ||
16674e4f KG |
809 | It seems that people using plink under Windows need to send |
810 | \"\\r\\n\" (carriage-return, then newline) after a password, but just | |
811 | \"\\n\" after all other lines. This variable can be used for the | |
812 | password, see `tramp-rsh-end-of-line' for the other cases. | |
813 | ||
814 | The default value is to use the same value as `tramp-rsh-end-of-line'." | |
fb7933a3 KG |
815 | :group 'tramp |
816 | :type 'string) | |
817 | ||
818 | (defcustom tramp-remote-path | |
819 | '("/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/ccs/bin" | |
820 | "/local/bin" "/local/freeware/bin" "/local/gnu/bin" | |
821 | "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin") | |
822 | "*List of directories to search for executables on remote host. | |
823 | Please notify me about other semi-standard directories to include here. | |
824 | ||
825 | You can use `~' in this list, but when searching for a shell which groks | |
826 | tilde expansion, all directory names starting with `~' will be ignored." | |
827 | :group 'tramp | |
828 | :type '(repeat string)) | |
829 | ||
830 | (defcustom tramp-login-prompt-regexp | |
ac474af1 | 831 | ".*ogin: *" |
fb7933a3 | 832 | "*Regexp matching login-like prompts. |
ac474af1 | 833 | The regexp should match at end of buffer." |
fb7933a3 KG |
834 | :group 'tramp |
835 | :type 'regexp) | |
836 | ||
821e6e36 | 837 | (defcustom tramp-shell-prompt-pattern |
ea9d1443 | 838 | "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*" |
821e6e36 KG |
839 | "Regexp to match prompts from remote shell. |
840 | Normally, Tramp expects you to configure `shell-prompt-pattern' | |
841 | correctly, but sometimes it happens that you are connecting to a | |
842 | remote host which sends a different kind of shell prompt. Therefore, | |
843 | Tramp recognizes things matched by `shell-prompt-pattern' as prompt, | |
844 | and also things matched by this variable. The default value of this | |
b25a52cc | 845 | variable is similar to the default value of `shell-prompt-pattern', |
821e6e36 KG |
846 | which should work well in many cases." |
847 | :group 'tramp | |
848 | :type 'regexp) | |
849 | ||
fb7933a3 | 850 | (defcustom tramp-password-prompt-regexp |
ac474af1 | 851 | "^.*\\([pP]assword\\|passphrase.*\\):\^@? *" |
fb7933a3 | 852 | "*Regexp matching password-like prompts. |
ac474af1 | 853 | The regexp should match at end of buffer. |
fb7933a3 KG |
854 | |
855 | The `sudo' program appears to insert a `^@' character into the prompt." | |
856 | :group 'tramp | |
857 | :type 'regexp) | |
858 | ||
859 | (defcustom tramp-wrong-passwd-regexp | |
b1d06e75 KG |
860 | (concat "^.*" |
861 | ;; These strings should be on the last line | |
862 | (regexp-opt '("Permission denied." | |
863 | "Login incorrect" | |
864 | "Login Incorrect" | |
865 | "Connection refused" | |
866 | "Connection closed" | |
867 | "Sorry, try again." | |
868 | "Name or service not known" | |
869 | "Host key verification failed.") t) | |
870 | ".*" | |
871 | "\\|" | |
872 | "^.*\\(" | |
873 | ;; Here comes a list of regexes, separated by \\| | |
874 | "Received signal [0-9]+" | |
875 | "\\).*") | |
fb7933a3 | 876 | "*Regexp matching a `login failed' message. |
ac474af1 KG |
877 | The regexp should match at end of buffer." |
878 | :group 'tramp | |
879 | :type 'regexp) | |
880 | ||
881 | (defcustom tramp-yesno-prompt-regexp | |
3cdaec13 KG |
882 | (concat |
883 | (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t) | |
884 | "\\s-*") | |
885 | "Regular expression matching all yes/no queries which need to be confirmed. | |
ac474af1 | 886 | The confirmation should be done with yes or no. |
3cdaec13 KG |
887 | The regexp should match at end of buffer. |
888 | See also `tramp-yn-prompt-regexp'." | |
fb7933a3 KG |
889 | :group 'tramp |
890 | :type 'regexp) | |
891 | ||
3cdaec13 KG |
892 | (defcustom tramp-yn-prompt-regexp |
893 | (concat (regexp-opt '("Store key in cache? (y/n)") t) | |
894 | "\\s-*") | |
895 | "Regular expression matching all y/n queries which need to be confirmed. | |
896 | The confirmation should be done with y or n. | |
897 | The regexp should match at end of buffer. | |
898 | See also `tramp-yesno-prompt-regexp'." | |
899 | :group 'tramp | |
900 | :type 'regexp) | |
487f4fb7 KG |
901 | |
902 | (defcustom tramp-terminal-prompt-regexp | |
903 | (concat "\\(" | |
904 | "TERM = (.*)" | |
905 | "\\|" | |
906 | "Terminal type\\? \\[.*\\]" | |
907 | "\\)\\s-*") | |
908 | "Regular expression matching all terminal setting prompts. | |
909 | The regexp should match at end of buffer. | |
910 | The answer will be provided by `tramp-action-terminal', which see." | |
911 | :group 'tramp | |
912 | :type 'regexp) | |
3cdaec13 | 913 | |
19a87064 | 914 | (defcustom tramp-process-alive-regexp |
38c65fca | 915 | "" |
19a87064 | 916 | "Regular expression indicating a process has finished. |
38c65fca KG |
917 | In fact this expression is empty by intention, it will be used only to |
918 | check regularly the status of the associated process. | |
19a87064 MA |
919 | The answer will be provided by `tramp-action-process-alive' and |
920 | `tramp-action-out-of-band', which see." | |
38c65fca KG |
921 | :group 'tramp |
922 | :type 'regexp) | |
923 | ||
fb7933a3 KG |
924 | (defcustom tramp-temp-name-prefix "tramp." |
925 | "*Prefix to use for temporary files. | |
926 | If this is a relative file name (such as \"tramp.\"), it is considered | |
927 | relative to the directory name returned by the function | |
928 | `tramp-temporary-file-directory' (which see). It may also be an | |
929 | absolute file name; don't forget to include a prefix for the filename | |
930 | part, though." | |
931 | :group 'tramp | |
932 | :type 'string) | |
933 | ||
934 | (defcustom tramp-discard-garbage nil | |
935 | "*If non-nil, try to discard garbage sent by remote shell. | |
936 | Some shells send such garbage upon connection setup." | |
937 | :group 'tramp | |
938 | :type 'boolean) | |
939 | ||
4007ba5b | 940 | (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile")) |
c62c9d08 KG |
941 | "*Alist specifying extra arguments to pass to the remote shell. |
942 | Entries are (REGEXP . ARGS) where REGEXP is a regular expression | |
943 | matching the shell file name and ARGS is a string specifying the | |
944 | arguments. | |
945 | ||
946 | This variable is only used when Tramp needs to start up another shell | |
947 | for tilde expansion. The extra arguments should typically prevent the | |
948 | shell from reading its init file." | |
949 | :group 'tramp | |
90f8dc03 KG |
950 | ;; This might be the wrong way to test whether the widget type |
951 | ;; `alist' is available. Who knows the right way to test it? | |
952 | :type (if (get 'alist 'widget-type) | |
953 | '(alist :key-type string :value-type string) | |
954 | '(repeat (cons string string)))) | |
c62c9d08 | 955 | |
16674e4f KG |
956 | (defcustom tramp-prefix-format |
957 | (if tramp-unified-filenames "/" "/[") | |
958 | "*String matching the very beginning of tramp file names. | |
959 | Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'." | |
960 | :group 'tramp | |
961 | :type 'string) | |
962 | ||
963 | (defcustom tramp-prefix-regexp | |
964 | (concat "^" (regexp-quote tramp-prefix-format)) | |
965 | "*Regexp matching the very beginning of tramp file names. | |
966 | Should always start with \"^\". Derived from `tramp-prefix-format'." | |
967 | :group 'tramp | |
968 | :type 'regexp) | |
969 | ||
970 | (defcustom tramp-method-regexp | |
971 | "[a-zA-Z_0-9-]+" | |
972 | "*Regexp matching methods identifiers." | |
973 | :group 'tramp | |
974 | :type 'regexp) | |
975 | ||
976 | ;; It is a little bit annoying that in XEmacs case this delimeter is different | |
977 | ;; for single-hop and multi-hop cases. | |
978 | (defcustom tramp-postfix-single-method-format | |
979 | (if tramp-unified-filenames ":" "/") | |
980 | "*String matching delimeter between method and user or host names. | |
981 | Applicable for single-hop methods. | |
982 | Used in `tramp-make-tramp-file-name'." | |
983 | :group 'tramp | |
984 | :type 'string) | |
985 | ||
986 | (defcustom tramp-postfix-single-method-regexp | |
987 | (regexp-quote tramp-postfix-single-method-format) | |
988 | "*Regexp matching delimeter between method and user or host names. | |
989 | Applicable for single-hop methods. | |
990 | Derived from `tramp-postfix-single-method-format'." | |
991 | :group 'tramp | |
992 | :type 'regexp) | |
993 | ||
994 | (defcustom tramp-postfix-multi-method-format | |
995 | ":" | |
996 | "*String matching delimeter between method and user or host names. | |
997 | Applicable for multi-hop methods. | |
998 | Used in `tramp-make-tramp-multi-file-name'." | |
999 | :group 'tramp | |
1000 | :type 'string) | |
1001 | ||
1002 | (defcustom tramp-postfix-multi-method-regexp | |
1003 | (regexp-quote tramp-postfix-multi-method-format) | |
1004 | "*Regexp matching delimeter between method and user or host names. | |
1005 | Applicable for multi-hop methods. | |
1006 | Derived from `tramp-postfix-multi-method-format'." | |
1007 | :group 'tramp | |
1008 | :type 'regexp) | |
fb7933a3 | 1009 | |
16674e4f KG |
1010 | (defcustom tramp-postfix-multi-hop-format |
1011 | (if tramp-unified-filenames ":" "/") | |
7432277c | 1012 | "*String matching delimeter between host and next method. |
16674e4f KG |
1013 | Applicable for multi-hop methods. |
1014 | Used in `tramp-make-tramp-multi-file-name'." | |
1015 | :group 'tramp | |
1016 | :type 'string) | |
1017 | ||
1018 | (defcustom tramp-postfix-multi-hop-regexp | |
1019 | (regexp-quote tramp-postfix-multi-hop-format) | |
7432277c | 1020 | "*Regexp matching delimeter between host and next method. |
16674e4f KG |
1021 | Applicable for multi-hop methods. |
1022 | Derived from `tramp-postfix-multi-hop-format'." | |
1023 | :group 'tramp | |
1024 | :type 'regexp) | |
1025 | ||
1026 | (defcustom tramp-user-regexp | |
292ffc15 | 1027 | "[^:@/ \t]*" |
16674e4f KG |
1028 | "*Regexp matching user names." |
1029 | :group 'tramp | |
1030 | :type 'regexp) | |
1031 | ||
1032 | (defcustom tramp-postfix-user-format | |
1033 | "@" | |
1034 | "*String matching delimeter between user and host names. | |
1035 | Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'." | |
1036 | :group 'tramp | |
1037 | :type 'string) | |
1038 | ||
1039 | (defcustom tramp-postfix-user-regexp | |
1040 | (regexp-quote tramp-postfix-user-format) | |
1041 | "*Regexp matching delimeter between user and host names. | |
1042 | Derived from `tramp-postfix-user-format'." | |
1043 | :group 'tramp | |
1044 | :type 'regexp) | |
1045 | ||
1046 | (defcustom tramp-host-regexp | |
487f4fb7 KG |
1047 | "[a-zA-Z0-9_.-]*" |
1048 | "*Regexp matching host names." | |
1049 | :group 'tramp | |
1050 | :type 'regexp) | |
1051 | ||
1052 | (defcustom tramp-host-with-port-regexp | |
3b89d388 | 1053 | "[a-zA-Z0-9_.#-]*" |
16674e4f KG |
1054 | "*Regexp matching host names." |
1055 | :group 'tramp | |
1056 | :type 'regexp) | |
1057 | ||
1058 | (defcustom tramp-postfix-host-format | |
1059 | (if tramp-unified-filenames ":" "]") | |
7432277c | 1060 | "*String matching delimeter between host names and localnames. |
16674e4f KG |
1061 | Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'." |
1062 | :group 'tramp | |
1063 | :type 'string) | |
1064 | ||
1065 | (defcustom tramp-postfix-host-regexp | |
1066 | (regexp-quote tramp-postfix-host-format) | |
7432277c | 1067 | "*Regexp matching delimeter between host names and localnames. |
16674e4f KG |
1068 | Derived from `tramp-postfix-host-format'." |
1069 | :group 'tramp | |
1070 | :type 'regexp) | |
1071 | ||
7432277c | 1072 | (defcustom tramp-localname-regexp |
16674e4f | 1073 | ".*$" |
7432277c | 1074 | "*Regexp matching localnames." |
16674e4f KG |
1075 | :group 'tramp |
1076 | :type 'regexp) | |
1077 | ||
1078 | ;; File name format. | |
505edaeb KG |
1079 | |
1080 | (defcustom tramp-file-name-structure | |
16674e4f KG |
1081 | (list |
1082 | (concat | |
1083 | tramp-prefix-regexp | |
1084 | "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?" | |
487f4fb7 KG |
1085 | "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?" |
1086 | "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp | |
7432277c | 1087 | "\\(" tramp-localname-regexp "\\)") |
16674e4f KG |
1088 | 2 4 5 6) |
1089 | ||
fb7933a3 KG |
1090 | "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \ |
1091 | the tramp file name structure. | |
1092 | ||
1093 | The first element REGEXP is a regular expression matching a tramp file | |
1094 | name. The regex should contain parentheses around the method name, | |
1095 | the user name, the host name, and the file name parts. | |
1096 | ||
1097 | The second element METHOD is a number, saying which pair of | |
1098 | parentheses matches the method name. The third element USER is | |
1099 | similar, but for the user name. The fourth element HOST is similar, | |
1100 | but for the host name. The fifth element FILE is for the file name. | |
1101 | These numbers are passed directly to `match-string', which see. That | |
1102 | means the opening parentheses are counted to identify the pair. | |
1103 | ||
16674e4f | 1104 | See also `tramp-file-name-regexp'." |
fb7933a3 KG |
1105 | :group 'tramp |
1106 | :type '(list (regexp :tag "File name regexp") | |
1107 | (integer :tag "Paren pair for method name") | |
1108 | (integer :tag "Paren pair for user name ") | |
1109 | (integer :tag "Paren pair for host name ") | |
1110 | (integer :tag "Paren pair for file name "))) | |
1111 | ||
1112 | ;;;###autoload | |
505edaeb KG |
1113 | (defconst tramp-file-name-regexp-unified |
1114 | "\\`/[^/:]+:" | |
1115 | "Value for `tramp-file-name-regexp' for unified remoting. | |
1116 | Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and | |
1117 | Tramp. See `tramp-file-name-structure-unified' for more explanations.") | |
1118 | ||
1119 | ;;;###autoload | |
1120 | (defconst tramp-file-name-regexp-separate | |
1121 | "\\`/\\[.*\\]" | |
1122 | "Value for `tramp-file-name-regexp' for separate remoting. | |
1123 | XEmacs uses a separate filename syntax for Tramp and EFS. | |
1124 | See `tramp-file-name-structure-separate' for more explanations.") | |
1125 | ||
1126 | ;;;###autoload | |
1127 | (defcustom tramp-file-name-regexp | |
16674e4f KG |
1128 | (if tramp-unified-filenames |
1129 | tramp-file-name-regexp-unified | |
1130 | tramp-file-name-regexp-separate) | |
fb7933a3 KG |
1131 | "*Regular expression matching file names handled by tramp. |
1132 | This regexp should match tramp file names but no other file names. | |
1133 | \(When tramp.el is loaded, this regular expression is prepended to | |
1134 | `file-name-handler-alist', and that is searched sequentially. Thus, | |
1135 | if the tramp entry appears rather early in the `file-name-handler-alist' | |
1136 | and is a bit too general, then some files might be considered tramp | |
1137 | files which are not really tramp files. | |
1138 | ||
1139 | Please note that the entry in `file-name-handler-alist' is made when | |
1140 | this file (tramp.el) is loaded. This means that this variable must be set | |
1141 | before loading tramp.el. Alternatively, `file-name-handler-alist' can be | |
1142 | updated after changing this variable. | |
1143 | ||
16674e4f | 1144 | Also see `tramp-file-name-structure'." |
fb7933a3 KG |
1145 | :group 'tramp |
1146 | :type 'regexp) | |
1147 | ||
16674e4f KG |
1148 | ;;;###autoload |
1149 | (defconst tramp-completion-file-name-regexp-unified | |
19a87064 | 1150 | "^/$\\|^/[^/:][^/]*$" |
16674e4f KG |
1151 | "Value for `tramp-completion-file-name-regexp' for unified remoting. |
1152 | Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and | |
1153 | Tramp. See `tramp-file-name-structure-unified' for more explanations.") | |
fb7933a3 | 1154 | |
16674e4f KG |
1155 | ;;;###autoload |
1156 | (defconst tramp-completion-file-name-regexp-separate | |
1157 | "^/\\([[][^]]*\\)?$" | |
1158 | "Value for `tramp-completion-file-name-regexp' for separate remoting. | |
1159 | XEmacs uses a separate filename syntax for Tramp and EFS. | |
1160 | See `tramp-file-name-structure-separate' for more explanations.") | |
fb7933a3 | 1161 | |
16674e4f KG |
1162 | ;;;###autoload |
1163 | (defcustom tramp-completion-file-name-regexp | |
1164 | (if tramp-unified-filenames | |
1165 | tramp-completion-file-name-regexp-unified | |
1166 | tramp-completion-file-name-regexp-separate) | |
1167 | "*Regular expression matching file names handled by tramp completion. | |
1168 | This regexp should match partial tramp file names only. | |
1169 | ||
1170 | Please note that the entry in `file-name-handler-alist' is made when | |
1171 | this file (tramp.el) is loaded. This means that this variable must be set | |
1172 | before loading tramp.el. Alternatively, `file-name-handler-alist' can be | |
1173 | updated after changing this variable. | |
1174 | ||
1175 | Also see `tramp-file-name-structure'." | |
1176 | :group 'tramp | |
1177 | :type 'regexp) | |
505edaeb KG |
1178 | |
1179 | (defcustom tramp-multi-file-name-structure | |
16674e4f KG |
1180 | (list |
1181 | (concat | |
1182 | tramp-prefix-regexp | |
1183 | "\\(" "\\(" tramp-method-regexp "\\)" "\\)?" | |
1184 | "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?" | |
7432277c | 1185 | tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)") |
16674e4f | 1186 | 2 3 -1) |
fb7933a3 KG |
1187 | "*Describes the file name structure of `multi' files. |
1188 | Multi files allow you to contact a remote host in several hops. | |
7432277c | 1189 | This is a list of four elements (REGEXP METHOD HOP LOCALNAME). |
fb7933a3 KG |
1190 | |
1191 | The first element, REGEXP, gives a regular expression to match against | |
1192 | the file name. In this regular expression, `%s' is replaced with the | |
1193 | value of `tramp-multi-file-name-hop-structure'. (Note: in order to | |
1194 | allow multiple hops, you normally want to use something like | |
1195 | \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair | |
1196 | of parentheses is used for the HOP element, see below.) | |
1197 | ||
1198 | All remaining elements are numbers. METHOD gives the number of the | |
1199 | paren pair which matches the method name. HOP gives the number of the | |
7432277c KG |
1200 | paren pair which matches the hop sequence. LOCALNAME gives the number of |
1201 | the paren pair which matches the localname (pathname) on the remote host. | |
fb7933a3 | 1202 | |
7432277c | 1203 | LOCALNAME can also be negative, which means to count from the end. Ie, a |
fb7933a3 KG |
1204 | value of -1 means the last paren pair. |
1205 | ||
1206 | I think it would be good if the regexp matches the whole of the | |
1207 | string, but I haven't actually tried what happens if it doesn't..." | |
1208 | :group 'tramp | |
1209 | :type '(list (regexp :tag "File name regexp") | |
1210 | (integer :tag "Paren pair for method name") | |
1211 | (integer :tag "Paren pair for hops") | |
7432277c | 1212 | (integer :tag "Paren pair to match localname"))) |
fb7933a3 | 1213 | |
505edaeb | 1214 | (defcustom tramp-multi-file-name-hop-structure |
16674e4f KG |
1215 | (list |
1216 | (concat | |
1217 | "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp | |
487f4fb7 KG |
1218 | "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp |
1219 | "\\(" tramp-host-with-port-regexp "\\)") | |
16674e4f | 1220 | 1 2 3) |
fb7933a3 KG |
1221 | "*Describes the structure of a hop in multi files. |
1222 | This is a list of four elements (REGEXP METHOD USER HOST). First | |
1223 | element REGEXP is used to match against the hop. Pair number METHOD | |
1224 | matches the method of one hop, pair number USER matches the user of | |
1225 | one hop, pair number HOST matches the host of one hop. | |
1226 | ||
1227 | This regular expression should match exactly all of one hop." | |
1228 | :group 'tramp | |
1229 | :type '(list (regexp :tag "Hop regexp") | |
1230 | (integer :tag "Paren pair for method name") | |
1231 | (integer :tag "Paren pair for user name") | |
1232 | (integer :tag "Paren pair for host name"))) | |
1233 | ||
505edaeb | 1234 | (defcustom tramp-make-multi-tramp-file-format |
16674e4f KG |
1235 | (list |
1236 | (concat tramp-prefix-format "%m") | |
1237 | (concat tramp-postfix-multi-hop-format | |
1238 | "%m" tramp-postfix-multi-method-format | |
1239 | "%u" tramp-postfix-user-format | |
1240 | "%h") | |
1241 | (concat tramp-postfix-host-format "%p")) | |
fb7933a3 | 1242 | "*Describes how to construct a `multi' file name. |
7432277c | 1243 | This is a list of three elements PREFIX, HOP and LOCALNAME. |
fb7933a3 KG |
1244 | |
1245 | The first element PREFIX says how to construct the prefix, the second | |
1246 | element HOP specifies what each hop looks like, and the final element | |
7432277c | 1247 | LOCALNAME says how to construct the localname (pathname). |
fb7933a3 KG |
1248 | |
1249 | In PREFIX, `%%' means `%' and `%m' means the method name. | |
1250 | ||
1251 | In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop | |
1252 | user and hop host, respectively. | |
1253 | ||
7432277c | 1254 | In LOCALNAME, `%%' means `%' and `%p' means the localname. |
fb7933a3 KG |
1255 | |
1256 | The resulting file name always contains one copy of PREFIX and one | |
7432277c | 1257 | copy of LOCALNAME, but there is one copy of HOP for each hop in the file |
fb7933a3 KG |
1258 | name. |
1259 | ||
1260 | Note: the current implementation requires the prefix to contain the | |
7432277c | 1261 | method name, followed by all the hops, and the localname must come |
fb7933a3 KG |
1262 | last." |
1263 | :group 'tramp | |
1264 | :type '(list string string string)) | |
1265 | ||
1266 | (defcustom tramp-terminal-type "dumb" | |
1267 | "*Value of TERM environment variable for logging in to remote host. | |
1268 | Because Tramp wants to parse the output of the remote shell, it is easily | |
1269 | confused by ANSI color escape sequences and suchlike. Often, shell init | |
1270 | files conditionalize this setup based on the TERM environment variable." | |
1271 | :group 'tramp | |
1272 | :type 'string) | |
1273 | ||
1274 | (defcustom tramp-completion-without-shell-p nil | |
1275 | "*If nil, use shell wildcards for completion, else rely on Lisp only. | |
1276 | Using shell wildcards for completions has the advantage that it can be | |
1277 | fast even in large directories, but completion is always | |
1278 | case-sensitive. Relying on Lisp only means that case-insensitive | |
1279 | completion is possible (subject to the variable `completion-ignore-case'), | |
1280 | but it might be slow on large directories." | |
1281 | :group 'tramp | |
1282 | :type 'boolean) | |
1283 | ||
ac474af1 KG |
1284 | (defcustom tramp-actions-before-shell |
1285 | '((tramp-password-prompt-regexp tramp-action-password) | |
1286 | (tramp-login-prompt-regexp tramp-action-login) | |
1287 | (shell-prompt-pattern tramp-action-succeed) | |
821e6e36 | 1288 | (tramp-shell-prompt-pattern tramp-action-succeed) |
ac474af1 | 1289 | (tramp-wrong-passwd-regexp tramp-action-permission-denied) |
3cdaec13 | 1290 | (tramp-yesno-prompt-regexp tramp-action-yesno) |
487f4fb7 | 1291 | (tramp-yn-prompt-regexp tramp-action-yn) |
19a87064 MA |
1292 | (tramp-terminal-prompt-regexp tramp-action-terminal) |
1293 | (tramp-process-alive-regexp tramp-action-process-alive)) | |
ac474af1 KG |
1294 | "List of pattern/action pairs. |
1295 | Whenever a pattern matches, the corresponding action is performed. | |
1296 | Each item looks like (PATTERN ACTION). | |
1297 | ||
1298 | The PATTERN should be a symbol, a variable. The value of this | |
1299 | variable gives the regular expression to search for. Note that the | |
1300 | regexp must match at the end of the buffer, \"\\'\" is implicitly | |
1301 | appended to it. | |
1302 | ||
1303 | The ACTION should also be a symbol, but a function. When the | |
1304 | corresponding PATTERN matches, the ACTION function is called." | |
1305 | :group 'tramp | |
1306 | :type '(repeat (list variable function))) | |
1307 | ||
38c65fca KG |
1308 | (defcustom tramp-actions-copy-out-of-band |
1309 | '((tramp-password-prompt-regexp tramp-action-password) | |
1310 | (tramp-wrong-passwd-regexp tramp-action-permission-denied) | |
19a87064 | 1311 | (tramp-process-alive-regexp tramp-action-out-of-band)) |
38c65fca KG |
1312 | "List of pattern/action pairs. |
1313 | This list is used for copying/renaming with out-of-band methods. | |
1314 | See `tramp-actions-before-shell' for more info." | |
1315 | :group 'tramp | |
1316 | :type '(repeat (list variable function))) | |
1317 | ||
ac474af1 KG |
1318 | (defcustom tramp-multi-actions |
1319 | '((tramp-password-prompt-regexp tramp-multi-action-password) | |
1320 | (tramp-login-prompt-regexp tramp-multi-action-login) | |
1321 | (shell-prompt-pattern tramp-multi-action-succeed) | |
821e6e36 | 1322 | (tramp-shell-prompt-pattern tramp-multi-action-succeed) |
19a87064 MA |
1323 | (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied) |
1324 | (tramp-process-alive-regexp tramp-action-process-alive)) | |
ac474af1 KG |
1325 | "List of pattern/action pairs. |
1326 | This list is used for each hop in multi-hop connections. | |
1327 | See `tramp-actions-before-shell' for more info." | |
1328 | :group 'tramp | |
1329 | :type '(repeat (list variable function))) | |
1330 | ||
90f8dc03 | 1331 | (defcustom tramp-initial-commands |
19a87064 MA |
1332 | '("unset HISTORY" |
1333 | "unset correct" | |
90f8dc03 KG |
1334 | "unset autocorrect") |
1335 | "List of commands to send to the first remote shell that we see. | |
1336 | These commands will be sent to any shell, and thus they should be | |
1337 | designed to work in such circumstances. Also, restrict the commands | |
1338 | to the bare necessity for getting the remote shell into a state | |
1339 | where it is possible to execute the Bourne-ish shell. | |
1340 | ||
1341 | At the moment, the command to execute the Bourne-ish shell uses strange | |
1342 | quoting which `tcsh' tries to correct, so we send the command \"unset | |
1343 | autocorrect\" to the remote host." | |
1344 | :group 'tramp | |
1345 | :type '(repeat string)) | |
1346 | ||
7432277c KG |
1347 | ;; Chunked sending kluge. We set this to 500 for black-listed constellations |
1348 | ;; known to have a bug in `process-send-string'; some ssh connections appear | |
1349 | ;; to drop bytes when data is sent too quickly. | |
1350 | (defcustom tramp-chunksize | |
1351 | (when (and (not (featurep 'xemacs)) | |
1352 | (memq system-type '(hpux))) | |
1353 | 500) | |
1354 | "*If non-nil, chunksize for sending input to local process. | |
1355 | It is necessary only on systems which have a buggy `process-send-string' | |
1356 | implementation. The necessity, whether this variable must be set, can be | |
1357 | checked via the following code: | |
1358 | ||
1359 | (with-temp-buffer | |
1360 | (let ((bytes 1000) | |
1361 | (proc (start-process (buffer-name) (current-buffer) \"wc\" \"-c\"))) | |
1362 | (process-send-string proc (make-string bytes ?x)) | |
1363 | (process-send-eof proc) | |
1364 | (process-send-eof proc) | |
1365 | (accept-process-output proc 1) | |
1366 | (goto-char (point-min)) | |
1367 | (re-search-forward \"\\\\w+\") | |
1368 | (message \"Bytes sent: %s\\tBytes received: %s\" bytes (match-string 0)))) | |
1369 | ||
c951aecb KG |
1370 | In the Emacs normally running Tramp, evaluate the above code. |
1371 | You can do this, for example, by pasting it into the `*scratch*' | |
1372 | buffer and then hitting C-j with the cursor after the last | |
1373 | closing parenthesis. | |
1374 | ||
1375 | If your Emacs is buggy, the sent and received numbers will be | |
1376 | different. In that case, you'll want to set this variable to | |
1377 | some number. For those people who have needed it, the value 500 | |
1378 | seems to have worked well. There is no way to predict what value | |
1379 | you need; maybe you could just experiment a bit. | |
1380 | ||
7432277c KG |
1381 | Please raise a bug report via \"M-x tramp-bug\" if your system needs |
1382 | this variable to be set as well." | |
1383 | :group 'tramp | |
b1a2b924 | 1384 | :type '(choice (const nil) integer)) |
7432277c | 1385 | |
5ec2cc41 KG |
1386 | ;; Logging in to a remote host normally requires obtaining a pty. But |
1387 | ;; Emacs on MacOS X has process-connection-type set to nil by default, | |
1388 | ;; so on those systems Tramp doesn't obtain a pty. Here, we allow | |
1389 | ;; for an override of the system default. | |
1390 | (defcustom tramp-process-connection-type t | |
1391 | "Overrides `process-connection-type' for connections from Tramp. | |
1392 | Tramp binds process-connection-type to the value given here before | |
1393 | opening a connection to a remote host." | |
1394 | :group 'tramp | |
1395 | :type '(choice (const nil) (const t) (const pty))) | |
1396 | ||
fb7933a3 KG |
1397 | ;;; Internal Variables: |
1398 | ||
1399 | (defvar tramp-buffer-file-attributes nil | |
1400 | "Holds the `ls -ild' output for the current buffer. | |
1401 | This variable is local to each buffer. It is not used if the remote | |
1402 | machine groks Perl. If it is used, it's used as an emulation for | |
1403 | the visited file modtime.") | |
1404 | (make-variable-buffer-local 'tramp-buffer-file-attributes) | |
1405 | ||
4007ba5b | 1406 | (defvar tramp-md5-function |
8daea7fc KG |
1407 | (cond ((and (require 'md5) (fboundp 'md5)) 'md5) |
1408 | ((fboundp 'md5-encode) | |
38c65fca KG |
1409 | (lambda (x) (base64-encode-string |
1410 | (funcall (symbol-function 'md5-encode) x)))) | |
4007ba5b KG |
1411 | (t (error "Coulnd't find an `md5' function"))) |
1412 | "Function to call for running the MD5 algorithm.") | |
1413 | ||
1414 | (defvar tramp-end-of-output | |
1415 | (concat "///" | |
1416 | (funcall tramp-md5-function | |
1417 | (concat | |
1418 | (prin1-to-string process-environment) | |
1419 | (current-time-string) | |
1420 | ;; (prin1-to-string | |
1421 | ;; (if (fboundp 'directory-files-and-attributes) | |
1422 | ;; (funcall 'directory-files-and-attributes | |
1423 | ;; (or (getenv "HOME") | |
1424 | ;; (tramp-temporary-file-directory))) | |
1425 | ;; (mapcar | |
1426 | ;; (lambda (x) | |
1427 | ;; (cons x (file-attributes x))) | |
1428 | ;; (directory-files (or (getenv "HOME") | |
1429 | ;; (tramp-temporary-file-directory)) | |
1430 | ;; t)))) | |
1431 | ))) | |
fb7933a3 KG |
1432 | "String used to recognize end of output.") |
1433 | ||
1434 | (defvar tramp-connection-function nil | |
1435 | "This internal variable holds a parameter for `tramp-methods'. | |
1436 | In the connection buffer, this variable has the value of the like-named | |
1437 | method parameter, as specified in `tramp-methods' (which see).") | |
1438 | ||
1439 | (defvar tramp-remote-sh nil | |
1440 | "This internal variable holds a parameter for `tramp-methods'. | |
1441 | In the connection buffer, this variable has the value of the like-named | |
1442 | method parameter, as specified in `tramp-methods' (which see).") | |
1443 | ||
b25a52cc | 1444 | (defvar tramp-login-program nil |
fb7933a3 KG |
1445 | "This internal variable holds a parameter for `tramp-methods'. |
1446 | In the connection buffer, this variable has the value of the like-named | |
1447 | method parameter, as specified in `tramp-methods' (which see).") | |
1448 | ||
b25a52cc | 1449 | (defvar tramp-login-args nil |
fb7933a3 KG |
1450 | "This internal variable holds a parameter for `tramp-methods'. |
1451 | In the connection buffer, this variable has the value of the like-named | |
1452 | method parameter, as specified in `tramp-methods' (which see).") | |
1453 | ||
b25a52cc | 1454 | (defvar tramp-copy-program nil |
fb7933a3 KG |
1455 | "This internal variable holds a parameter for `tramp-methods'. |
1456 | In the connection buffer, this variable has the value of the like-named | |
1457 | method parameter, as specified in `tramp-methods' (which see).") | |
1458 | ||
b25a52cc | 1459 | (defvar tramp-copy-args nil |
fb7933a3 KG |
1460 | "This internal variable holds a parameter for `tramp-methods'. |
1461 | In the connection buffer, this variable has the value of the like-named | |
1462 | method parameter, as specified in `tramp-methods' (which see).") | |
1463 | ||
b25a52cc | 1464 | (defvar tramp-copy-keep-date-arg nil |
fb7933a3 KG |
1465 | "This internal variable holds a parameter for `tramp-methods'. |
1466 | In the connection buffer, this variable has the value of the like-named | |
1467 | method parameter, as specified in `tramp-methods' (which see).") | |
1468 | ||
1469 | (defvar tramp-encoding-command nil | |
1470 | "This internal variable holds a parameter for `tramp-methods'. | |
1471 | In the connection buffer, this variable has the value of the like-named | |
1472 | method parameter, as specified in `tramp-methods' (which see).") | |
1473 | ||
1474 | (defvar tramp-decoding-command nil | |
1475 | "This internal variable holds a parameter for `tramp-methods'. | |
1476 | In the connection buffer, this variable has the value of the like-named | |
1477 | method parameter, as specified in `tramp-methods' (which see).") | |
1478 | ||
1479 | (defvar tramp-encoding-function nil | |
1480 | "This internal variable holds a parameter for `tramp-methods'. | |
1481 | In the connection buffer, this variable has the value of the like-named | |
1482 | method parameter, as specified in `tramp-methods' (which see).") | |
1483 | ||
1484 | (defvar tramp-decoding-function nil | |
1485 | "This internal variable holds a parameter for `tramp-methods'. | |
1486 | In the connection buffer, this variable has the value of the like-named | |
1487 | method parameter, as specified in `tramp-methods' (which see).") | |
1488 | ||
90f8dc03 KG |
1489 | (defvar tramp-password-end-of-line nil |
1490 | "This internal variable holds a parameter for `tramp-methods'. | |
1491 | In the connection buffer, this variable has the value of the like-named | |
1492 | method parameter, as specified in `tramp-methods' (which see).") | |
1493 | ||
fb7933a3 KG |
1494 | ;; CCC `local in each buffer'? |
1495 | (defvar tramp-ls-command nil | |
1496 | "This command is used to get a long listing with numeric user and group ids. | |
1497 | This variable is automatically made buffer-local to each rsh process buffer | |
1498 | upon opening the connection.") | |
1499 | ||
1500 | (defvar tramp-current-multi-method nil | |
1501 | "Name of `multi' connection method for this *tramp* buffer, or nil if not multi. | |
1502 | This variable is automatically made buffer-local to each rsh process buffer | |
1503 | upon opening the connection.") | |
1504 | ||
1505 | (defvar tramp-current-method nil | |
1506 | "Connection method for this *tramp* buffer. | |
1507 | This variable is automatically made buffer-local to each rsh process buffer | |
1508 | upon opening the connection.") | |
1509 | ||
1510 | (defvar tramp-current-user nil | |
1511 | "Remote login name for this *tramp* buffer. | |
1512 | This variable is automatically made buffer-local to each rsh process buffer | |
1513 | upon opening the connection.") | |
1514 | ||
1515 | (defvar tramp-current-host nil | |
1516 | "Remote host for this *tramp* buffer. | |
1517 | This variable is automatically made buffer-local to each rsh process buffer | |
1518 | upon opening the connection.") | |
1519 | ||
1520 | (defvar tramp-test-groks-nt nil | |
1521 | "Whether the `test' command groks the `-nt' switch. | |
1522 | \(`test A -nt B' tests if file A is newer than file B.) | |
1523 | This variable is automatically made buffer-local to each rsh process buffer | |
1524 | upon opening the connection.") | |
1525 | ||
1526 | (defvar tramp-file-exists-command nil | |
1527 | "Command to use for checking if a file exists. | |
1528 | This variable is automatically made buffer-local to each rsh process buffer | |
1529 | upon opening the connection.") | |
1530 | ||
fabf2143 KG |
1531 | (defconst tramp-uudecode "\ |
1532 | tramp_uudecode () { | |
1533 | \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode | |
1534 | cat /tmp/tramp.$$ | |
1535 | rm -f /tmp/tramp.$$ | |
1536 | }" | |
1537 | "Shell function to implement `uudecode' to standard output. | |
1538 | Many systems support `uudecode -o -' for this or `uudecode -p', but | |
1539 | some systems don't, and for them we have this shell function.") | |
1540 | ||
1541 | ;; Perl script to implement `file-attributes' in a Lisp `read'able | |
1542 | ;; output. If you are hacking on this, note that you get *no* output | |
1543 | ;; unless this spits out a complete line, including the '\n' at the | |
1544 | ;; end. | |
8daea7fc KG |
1545 | ;; The device number is returned as "-1", because there will be a virtual |
1546 | ;; device number set in `tramp-handle-file-attributes' | |
5beaf831 | 1547 | (defconst tramp-perl-file-attributes "\ |
38c65fca | 1548 | \($f, $n) = @ARGV; |
fb7933a3 KG |
1549 | @s = lstat($f); |
1550 | if (($s[2] & 0170000) == 0120000) { $l = readlink($f); $l = \"\\\"$l\\\"\"; } | |
1551 | elsif (($s[2] & 0170000) == 040000) { $l = \"t\"; } | |
1552 | else { $l = \"nil\" }; | |
c951aecb KG |
1553 | $u = ($n eq \"nil\") ? $s[4] : getpwuid($s[4]); |
1554 | $g = ($n eq \"nil\") ? $s[5] : getgrgid($s[5]); | |
1555 | printf(\"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\", | |
1556 | $l, $s[3], $u, $g, $s[8] >> 16 & 0xffff, $s[8] & 0xffff, | |
fb7933a3 | 1557 | $s[9] >> 16 & 0xffff, $s[9] & 0xffff, $s[10] >> 16 & 0xffff, $s[10] & 0xffff, |
8daea7fc | 1558 | $s[7], $s[2], $s[1] >> 16 & 0xffff, $s[1] & 0xffff);" |
fb7933a3 KG |
1559 | "Perl script to produce output suitable for use with `file-attributes' |
1560 | on the remote file system.") | |
1561 | ||
ac474af1 KG |
1562 | ;; ;; These two use uu encoding. |
1563 | ;; (defvar tramp-perl-encode "%s -e'\ | |
1564 | ;; print qq(begin 644 xxx\n); | |
1565 | ;; my $s = q(); | |
1566 | ;; my $res = q(); | |
1567 | ;; while (read(STDIN, $s, 45)) { | |
1568 | ;; print pack(q(u), $s); | |
1569 | ;; } | |
1570 | ;; print qq(`\n); | |
1571 | ;; print qq(end\n); | |
1572 | ;; '" | |
1573 | ;; "Perl program to use for encoding a file. | |
1574 | ;; Escape sequence %s is replaced with name of Perl binary.") | |
1575 | ||
1576 | ;; (defvar tramp-perl-decode "%s -ne ' | |
1577 | ;; print unpack q(u), $_; | |
1578 | ;; '" | |
1579 | ;; "Perl program to use for decoding a file. | |
1580 | ;; Escape sequence %s is replaced with name of Perl binary.") | |
1581 | ||
1582 | ;; These two use base64 encoding. | |
b1d06e75 | 1583 | (defvar tramp-perl-encode-with-module |
ac474af1 KG |
1584 | "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'" |
1585 | "Perl program to use for encoding a file. | |
b1d06e75 | 1586 | Escape sequence %s is replaced with name of Perl binary. |
89509ea0 | 1587 | This string is passed to `format', so percent characters need to be doubled. |
b1d06e75 KG |
1588 | This implementation requires the MIME::Base64 Perl module to be installed |
1589 | on the remote host.") | |
1590 | ||
1591 | (defvar tramp-perl-decode-with-module | |
1592 | "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'" | |
1593 | "Perl program to use for decoding a file. | |
1594 | Escape sequence %s is replaced with name of Perl binary. | |
89509ea0 | 1595 | This string is passed to `format', so percent characters need to be doubled. |
b1d06e75 KG |
1596 | This implementation requires the MIME::Base64 Perl module to be installed |
1597 | on the remote host.") | |
1598 | ||
1599 | (defvar tramp-perl-encode | |
1600 | "%s -e ' | |
1601 | # This script contributed by Juanma Barranquero <lektu@terra.es>. | |
1602 | # Copyright (C) 2002 Free Software Foundation, Inc. | |
1603 | use strict; | |
1604 | ||
fa32e96a | 1605 | my %%trans = do { |
b1d06e75 KG |
1606 | my $i = 0; |
1607 | map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)} | |
1608 | split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/); | |
1609 | }; | |
1610 | ||
36541701 | 1611 | binmode(\\*STDIN); |
b1d06e75 KG |
1612 | |
1613 | # We read in chunks of 54 bytes, to generate output lines | |
1614 | # of 72 chars (plus end of line) | |
36541701 | 1615 | $/ = \\54; |
b1d06e75 KG |
1616 | |
1617 | while (my $data = <STDIN>) { | |
1618 | my $pad = q(); | |
1619 | ||
1620 | # Only for the last chunk, and only if did not fill the last three-byte packet | |
1621 | if (eof) { | |
fa32e96a | 1622 | my $mod = length($data) %% 3; |
b1d06e75 KG |
1623 | $pad = q(=) x (3 - $mod) if $mod; |
1624 | } | |
1625 | ||
1626 | # Not the fastest method, but it is simple: unpack to binary string, split | |
1627 | # by groups of 6 bits and convert back from binary to byte; then map into | |
1628 | # the translation table | |
1629 | ||
1630 | join q(), | |
1631 | map($trans{$_}, | |
1632 | (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)), | |
1633 | $pad, | |
36541701 | 1634 | qq(\\n); |
b1d06e75 KG |
1635 | } |
1636 | '" | |
1637 | "Perl program to use for encoding a file. | |
fa32e96a | 1638 | Escape sequence %s is replaced with name of Perl binary. |
ccf29586 | 1639 | This string is passed to `format', so percent characters need to be doubled.") |
ac474af1 KG |
1640 | |
1641 | (defvar tramp-perl-decode | |
b1d06e75 KG |
1642 | "%s -e ' |
1643 | # This script contributed by Juanma Barranquero <lektu@terra.es>. | |
1644 | # Copyright (C) 2002 Free Software Foundation, Inc. | |
1645 | use strict; | |
1646 | ||
fa32e96a | 1647 | my %%trans = do { |
b1d06e75 | 1648 | my $i = 0; |
16674e4f | 1649 | map {($_, substr(unpack(q(B8), chr $i++), 2, 6))} |
b1d06e75 KG |
1650 | split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/) |
1651 | }; | |
1652 | ||
fa32e96a | 1653 | my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255; |
b1d06e75 | 1654 | |
36541701 | 1655 | binmode(\\*STDOUT); |
b1d06e75 KG |
1656 | |
1657 | # We are going to accumulate into $pending to accept any line length | |
1658 | # (we do not check they are <= 76 chars as the RFC says) | |
1659 | my $pending = q(); | |
1660 | ||
1661 | while (my $data = <STDIN>) { | |
1662 | chomp $data; | |
1663 | ||
1664 | # If we find one or two =, we have reached the end and | |
1665 | # any following data is to be discarded | |
1666 | my $finished = $data =~ s/(==?).*/$1/; | |
1667 | $pending .= $data; | |
1668 | ||
1669 | my $len = length($pending); | |
16674e4f | 1670 | my $chunk = substr($pending, 0, $len & ~3); |
b1d06e75 KG |
1671 | |
1672 | # Easy method: translate from chars to (pregenerated) six-bit packets, join, | |
1673 | # split in 8-bit chunks and convert back to char. | |
1674 | print join q(), | |
1675 | map $bytes{$_}, | |
1676 | ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g); | |
1677 | ||
1678 | last if $finished; | |
1679 | } | |
1680 | '" | |
ac474af1 | 1681 | "Perl program to use for decoding a file. |
fa32e96a | 1682 | Escape sequence %s is replaced with name of Perl binary. |
ccf29586 | 1683 | This string is passed to `format', so percent characters need to be doubled.") |
fb7933a3 KG |
1684 | |
1685 | ; These values conform to `file-attributes' from XEmacs 21.2. | |
1686 | ; GNU Emacs and other tools not checked. | |
1687 | (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2) | |
1688 | (1 . "p") ; fifo | |
1689 | (2 . "c") ; character device | |
1690 | (3 . "m") ; multiplexed character device (v7) | |
1691 | (4 . "d") ; directory | |
1692 | (5 . "?") ; Named special file (XENIX) | |
1693 | (6 . "b") ; block device | |
1694 | (7 . "?") ; multiplexed block device (v7) | |
1695 | (8 . "-") ; regular file | |
1696 | (9 . "n") ; network special file (HP-UX) | |
1697 | (10 . "l") ; symlink | |
1698 | (11 . "?") ; ACL shadow inode (Solaris, not userspace) | |
1699 | (12 . "s") ; socket | |
1700 | (13 . "D") ; door special (Solaris) | |
1701 | (14 . "w")) ; whiteout (BSD) | |
1702 | "A list of file types returned from the `stat' system call. | |
1703 | This is used to map a mode number to a permission string.") | |
1704 | ||
1705 | (defvar tramp-dos-coding-system | |
1706 | (if (and (fboundp 'coding-system-p) | |
1707 | (funcall 'coding-system-p '(dos))) | |
1708 | 'dos | |
1709 | 'undecided-dos) | |
1710 | "Some Emacsen know the `dos' coding system, others need `undecided-dos'.") | |
1711 | ||
38c65fca KG |
1712 | (defvar tramp-last-cmd nil |
1713 | "Internal Tramp variable recording the last command sent. | |
1714 | This variable is buffer-local in every buffer.") | |
1715 | (make-variable-buffer-local 'tramp-last-cmd) | |
1716 | ||
1717 | (defvar tramp-process-echoes nil | |
1718 | "Whether to process echoes from the remote shell.") | |
1719 | ||
ac474af1 KG |
1720 | (defvar tramp-last-cmd-time nil |
1721 | "Internal Tramp variable recording the time when the last cmd was sent. | |
1722 | This variable is buffer-local in every buffer.") | |
1723 | (make-variable-buffer-local 'tramp-last-cmd-time) | |
fb7933a3 | 1724 | |
dba28077 KG |
1725 | ;; This variable does not have the right value in XEmacs. What should |
1726 | ;; I use instead of find-operation-coding-system in XEmacs? | |
3cdaec13 | 1727 | (defvar tramp-feature-write-region-fix |
16674e4f | 1728 | (when (fboundp 'find-operation-coding-system) |
dba28077 | 1729 | (let ((file-coding-system-alist '(("test" emacs-mule)))) |
38c65fca KG |
1730 | (funcall (symbol-function 'find-operation-coding-system) |
1731 | 'write-region 0 0 "" nil "test"))) | |
dba28077 | 1732 | "Internal variable to say if `write-region' chooses the right coding. |
3cdaec13 KG |
1733 | Older versions of Emacs chose the coding system for `write-region' based |
1734 | on the FILENAME argument, even if VISIT was a string.") | |
1735 | ||
fb7933a3 KG |
1736 | ;; New handlers should be added here. The following operations can be |
1737 | ;; handled using the normal primitives: file-name-as-directory, | |
1738 | ;; file-name-directory, file-name-nondirectory, | |
1739 | ;; file-name-sans-versions, get-file-buffer. | |
1740 | (defconst tramp-file-name-handler-alist | |
1741 | '( | |
1742 | (load . tramp-handle-load) | |
1743 | (make-symbolic-link . tramp-handle-make-symbolic-link) | |
1744 | (file-name-directory . tramp-handle-file-name-directory) | |
1745 | (file-name-nondirectory . tramp-handle-file-name-nondirectory) | |
1746 | (file-truename . tramp-handle-file-truename) | |
1747 | (file-exists-p . tramp-handle-file-exists-p) | |
1748 | (file-directory-p . tramp-handle-file-directory-p) | |
1749 | (file-executable-p . tramp-handle-file-executable-p) | |
1750 | (file-accessible-directory-p . tramp-handle-file-accessible-directory-p) | |
1751 | (file-readable-p . tramp-handle-file-readable-p) | |
1752 | (file-regular-p . tramp-handle-file-regular-p) | |
1753 | (file-symlink-p . tramp-handle-file-symlink-p) | |
1754 | (file-writable-p . tramp-handle-file-writable-p) | |
1755 | (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p) | |
1756 | (file-newer-than-file-p . tramp-handle-file-newer-than-file-p) | |
1757 | (file-attributes . tramp-handle-file-attributes) | |
1758 | (file-modes . tramp-handle-file-modes) | |
1759 | (file-directory-files . tramp-handle-file-directory-files) | |
1760 | (directory-files . tramp-handle-directory-files) | |
1761 | (file-name-all-completions . tramp-handle-file-name-all-completions) | |
1762 | (file-name-completion . tramp-handle-file-name-completion) | |
1763 | (add-name-to-file . tramp-handle-add-name-to-file) | |
1764 | (copy-file . tramp-handle-copy-file) | |
1765 | (rename-file . tramp-handle-rename-file) | |
1766 | (set-file-modes . tramp-handle-set-file-modes) | |
1767 | (make-directory . tramp-handle-make-directory) | |
1768 | (delete-directory . tramp-handle-delete-directory) | |
1769 | (delete-file . tramp-handle-delete-file) | |
1770 | (directory-file-name . tramp-handle-directory-file-name) | |
1771 | (shell-command . tramp-handle-shell-command) | |
1772 | (insert-directory . tramp-handle-insert-directory) | |
1773 | (expand-file-name . tramp-handle-expand-file-name) | |
1774 | (file-local-copy . tramp-handle-file-local-copy) | |
19a87064 | 1775 | (file-remote-p . tramp-handle-file-remote-p) |
fb7933a3 KG |
1776 | (insert-file-contents . tramp-handle-insert-file-contents) |
1777 | (write-region . tramp-handle-write-region) | |
38c65fca | 1778 | (find-backup-file-name . tramp-handle-find-backup-file-name) |
fb7933a3 | 1779 | (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory) |
5ec2cc41 | 1780 | (dired-compress-file . tramp-handle-dired-compress-file) |
fb7933a3 KG |
1781 | (dired-call-process . tramp-handle-dired-call-process) |
1782 | (dired-recursive-delete-directory | |
1783 | . tramp-handle-dired-recursive-delete-directory) | |
1784 | (set-visited-file-modtime . tramp-handle-set-visited-file-modtime) | |
1785 | (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)) | |
1786 | "Alist of handler functions. | |
1787 | Operations not mentioned here will be handled by the normal Emacs functions.") | |
1788 | ||
16674e4f KG |
1789 | ;; Handlers for partial tramp file names. For GNU Emacs just |
1790 | ;; `file-name-all-completions' is needed. The other ones are necessary | |
1791 | ;; for XEmacs. | |
1792 | (defconst tramp-completion-file-name-handler-alist | |
1793 | '( | |
1794 | (file-name-directory . tramp-completion-handle-file-name-directory) | |
1795 | (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory) | |
1796 | (file-exists-p . tramp-completion-handle-file-exists-p) | |
1797 | (file-name-all-completions . tramp-completion-handle-file-name-all-completions) | |
1798 | (file-name-completion . tramp-completion-handle-file-name-completion) | |
1799 | (expand-file-name . tramp-completion-handle-expand-file-name)) | |
1800 | "Alist of completion handler functions. | |
1801 | Used for file names matching `tramp-file-name-regexp'. Operations not | |
1802 | mentioned here will be handled by `tramp-file-name-handler-alist' or the | |
1803 | normal Emacs functions.") | |
1804 | ||
4007ba5b | 1805 | ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here. |
ea9d1443 KG |
1806 | (defvar tramp-foreign-file-name-handler-alist |
1807 | ;; (identity . tramp-sh-file-name-handler) should always be the last | |
1808 | ;; entry, since `identity' always matches. | |
1809 | '((identity . tramp-sh-file-name-handler)) | |
4007ba5b KG |
1810 | "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially. |
1811 | If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by | |
1812 | calling HANDLER.") | |
1813 | ||
fb7933a3 KG |
1814 | ;;; Internal functions which must come first. |
1815 | ||
1816 | (defsubst tramp-message (level fmt-string &rest args) | |
1817 | "Emit a message depending on verbosity level. | |
1818 | First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The | |
1819 | message is emitted only if `tramp-verbose' is greater than or equal to LEVEL. | |
1820 | Calls function `message' with FMT-STRING as control string and the remaining | |
1821 | ARGS to actually emit the message (if applicable). | |
1822 | ||
1823 | This function expects to be called from the tramp buffer only!" | |
1824 | (when (<= level tramp-verbose) | |
1825 | (apply #'message (concat "tramp: " fmt-string) args) | |
1826 | (when tramp-debug-buffer | |
1827 | (save-excursion | |
1828 | (set-buffer | |
1829 | (tramp-get-debug-buffer | |
1830 | tramp-current-multi-method tramp-current-method | |
1831 | tramp-current-user tramp-current-host)) | |
1832 | (goto-char (point-max)) | |
1833 | (tramp-insert-with-face | |
1834 | 'italic | |
1835 | (concat "# " (apply #'format fmt-string args) "\n")))))) | |
1836 | ||
1837 | (defun tramp-message-for-buffer | |
1838 | (multi-method method user host level fmt-string &rest args) | |
1839 | "Like `tramp-message' but temporarily switches to the tramp buffer. | |
1840 | First three args METHOD, USER, and HOST identify the tramp buffer to use, | |
1841 | remaining args passed to `tramp-message'." | |
1842 | (save-excursion | |
1843 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
1844 | (apply 'tramp-message level fmt-string args))) | |
1845 | ||
1846 | (defsubst tramp-line-end-position nil | |
1847 | "Return point at end of line. | |
1848 | Calls `line-end-position' or `point-at-eol' if defined, else | |
1849 | own implementation." | |
1850 | (cond | |
38c65fca KG |
1851 | ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position))) |
1852 | ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol))) | |
fb7933a3 KG |
1853 | (t (save-excursion (end-of-line) (point))))) |
1854 | ||
c62c9d08 KG |
1855 | (defmacro with-parsed-tramp-file-name (filename var &rest body) |
1856 | "Parse a Tramp filename and make components available in the body. | |
1857 | ||
1858 | First arg FILENAME is evaluated and dissected into its components. | |
1859 | Second arg VAR is a symbol. It is used as a variable name to hold | |
1860 | the filename structure. It is also used as a prefix for the variables | |
1861 | holding the components. For example, if VAR is the symbol `foo', then | |
1862 | `foo' will be bound to the whole structure, `foo-multi-method' will | |
1863 | be bound to the multi-method component, and so on for `foo-method', | |
7432277c | 1864 | `foo-user', `foo-host', `foo-localname'. |
c62c9d08 KG |
1865 | |
1866 | Remaining args are Lisp expressions to be evaluated (inside an implicit | |
1867 | `progn'). | |
1868 | ||
1869 | If VAR is nil, then we bind `v' to the structure and `multi-method', | |
7432277c | 1870 | `method', `user', `host', `localname' to the components." |
c62c9d08 KG |
1871 | `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename)) |
1872 | (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method) | |
1873 | (tramp-file-name-multi-method ,(or var 'v))) | |
1874 | (,(if var (intern (concat (symbol-name var) "-method")) 'method) | |
1875 | (tramp-file-name-method ,(or var 'v))) | |
1876 | (,(if var (intern (concat (symbol-name var) "-user")) 'user) | |
1877 | (tramp-file-name-user ,(or var 'v))) | |
1878 | (,(if var (intern (concat (symbol-name var) "-host")) 'host) | |
1879 | (tramp-file-name-host ,(or var 'v))) | |
7432277c KG |
1880 | (,(if var (intern (concat (symbol-name var) "-localname")) 'localname) |
1881 | (tramp-file-name-localname ,(or var 'v)))) | |
c62c9d08 KG |
1882 | ,@body)) |
1883 | ||
1884 | (put 'with-parsed-tramp-file-name 'lisp-indent-function 2) | |
38c65fca KG |
1885 | ;; To be activated for debugging containing this macro |
1886 | (def-edebug-spec with-parsed-tramp-file-name t) | |
c62c9d08 | 1887 | |
2fcaee47 KG |
1888 | (defmacro tramp-let-maybe (variable value &rest body) |
1889 | "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete. | |
1890 | BODY is executed whether or not the variable is obsolete. | |
1891 | The intent is to protect against `obsolete variable' warnings." | |
1892 | `(if (get ',variable 'byte-obsolete-variable) | |
1893 | (progn ,@body) | |
1894 | (let ((,variable ,value)) | |
1895 | ,@body))) | |
1896 | (put 'tramp-let-maybe 'lisp-indent-function 2) | |
1897 | ||
16674e4f KG |
1898 | ;;; Config Manipulation Functions: |
1899 | ||
1900 | (defun tramp-set-completion-function (method function-list) | |
1901 | "Sets the list of completion functions for METHOD. | |
1902 | FUNCTION-LIST is a list of entries of the form (FUNCTION FILE). | |
1903 | The FUNCTION is intended to parse FILE according its syntax. | |
1904 | It might be a predefined FUNCTION, or a user defined FUNCTION. | |
1905 | Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts', | |
8daea7fc KG |
1906 | `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd', |
1907 | and `tramp-parse-netrc'. | |
1908 | ||
16674e4f KG |
1909 | Example: |
1910 | ||
1911 | (tramp-set-completion-function | |
1912 | \"ssh\" | |
8daea7fc KG |
1913 | '((tramp-parse-sconfig \"/etc/ssh_config\") |
1914 | (tramp-parse-sconfig \"~/.ssh/config\")))" | |
16674e4f | 1915 | |
5ec2cc41 KG |
1916 | (let ((r function-list) |
1917 | (v function-list)) | |
1918 | (setq tramp-completion-function-alist | |
1919 | (delete (assoc method tramp-completion-function-alist) | |
1920 | tramp-completion-function-alist)) | |
1921 | ||
1922 | (while v | |
1923 | ;; Remove double entries | |
1924 | (when (member (car v) (cdr v)) | |
1925 | (setcdr v (delete (car v) (cdr v)))) | |
1926 | ;; Check for function and file | |
1927 | (unless (and (functionp (nth 0 (car v))) | |
1928 | (file-exists-p (nth 1 (car v)))) | |
1929 | (setq r (delete (car v) r))) | |
1930 | (setq v (cdr v))) | |
1931 | ||
1932 | (when r | |
4007ba5b | 1933 | (add-to-list 'tramp-completion-function-alist |
5ec2cc41 | 1934 | (cons method r))))) |
16674e4f KG |
1935 | |
1936 | (defun tramp-get-completion-function (method) | |
1937 | "Returns list of completion functions for METHOD. | |
1938 | For definition of that list see `tramp-set-completion-function'." | |
5ec2cc41 | 1939 | (cdr (assoc method tramp-completion-function-alist))) |
16674e4f | 1940 | |
fb7933a3 KG |
1941 | ;;; File Name Handler Functions: |
1942 | ||
fb7933a3 KG |
1943 | (defun tramp-handle-make-symbolic-link |
1944 | (filename linkname &optional ok-if-already-exists) | |
1945 | "Like `make-symbolic-link' for tramp files. | |
cebb4ec6 | 1946 | If LINKNAME is a non-Tramp file, it is used verbatim as the target of |
7432277c | 1947 | the symlink. If LINKNAME is a Tramp file, only the localname component is |
cebb4ec6 KG |
1948 | used as the target of the symlink. |
1949 | ||
7432277c KG |
1950 | If LINKNAME is a Tramp file and the localname component is relative, then |
1951 | it is expanded first, before the localname component is taken. Note that | |
cebb4ec6 KG |
1952 | this can give surprising results if the user/host for the source and |
1953 | target of the symlink differ." | |
c62c9d08 | 1954 | (with-parsed-tramp-file-name linkname l |
487fa986 | 1955 | (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host)) |
7432277c | 1956 | (cwd (file-name-directory l-localname))) |
c62c9d08 KG |
1957 | (unless ln |
1958 | (signal 'file-error | |
1959 | (list "Making a symbolic link." | |
1960 | "ln(1) does not exist on the remote host."))) | |
1961 | ||
1962 | ;; Do the 'confirm if exists' thing. | |
cebb4ec6 | 1963 | (when (file-exists-p linkname) |
c62c9d08 KG |
1964 | ;; What to do? |
1965 | (if (or (null ok-if-already-exists) ; not allowed to exist | |
1966 | (and (numberp ok-if-already-exists) | |
1967 | (not (yes-or-no-p | |
1968 | (format | |
1969 | "File %s already exists; make it a link anyway? " | |
7432277c KG |
1970 | l-localname))))) |
1971 | (signal 'file-already-exists (list "File already exists" l-localname)) | |
cebb4ec6 KG |
1972 | (delete-file linkname))) |
1973 | ||
7432277c | 1974 | ;; If FILENAME is a Tramp name, use just the localname component. |
cebb4ec6 | 1975 | (when (tramp-tramp-file-p filename) |
7432277c | 1976 | (setq filename (tramp-file-name-localname |
cebb4ec6 KG |
1977 | (tramp-dissect-file-name |
1978 | (expand-file-name filename))))) | |
7432277c | 1979 | |
c62c9d08 KG |
1980 | ;; Right, they are on the same host, regardless of user, method, etc. |
1981 | ;; We now make the link on the remote machine. This will occur as the user | |
1982 | ;; that FILENAME belongs to. | |
1983 | (zerop | |
1984 | (tramp-send-command-and-check | |
487fa986 | 1985 | l-multi-method l-method l-user l-host |
c62c9d08 KG |
1986 | (format "cd %s && %s -sf %s %s" |
1987 | cwd ln | |
7432277c KG |
1988 | filename |
1989 | l-localname) | |
c62c9d08 | 1990 | t))))) |
fb7933a3 KG |
1991 | |
1992 | ||
1993 | (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix) | |
1994 | "Like `load' for tramp files. Not implemented!" | |
1995 | (unless (file-name-absolute-p file) | |
7432277c | 1996 | (error "Tramp cannot `load' files without absolute file name")) |
c62c9d08 | 1997 | (with-parsed-tramp-file-name file nil |
c62c9d08 KG |
1998 | (unless nosuffix |
1999 | (cond ((file-exists-p (concat file ".elc")) | |
2000 | (setq file (concat file ".elc"))) | |
2001 | ((file-exists-p (concat file ".el")) | |
2002 | (setq file (concat file ".el"))))) | |
2003 | (when must-suffix | |
2004 | ;; The first condition is always true for absolute file names. | |
2005 | ;; Included for safety's sake. | |
2006 | (unless (or (file-name-directory file) | |
2007 | (string-match "\\.elc?\\'" file)) | |
2008 | (error "File `%s' does not include a `.el' or `.elc' suffix" | |
2009 | file))) | |
2010 | (unless noerror | |
2011 | (when (not (file-exists-p file)) | |
2012 | (error "Cannot load nonexistant file `%s'" file))) | |
2013 | (if (not (file-exists-p file)) | |
2014 | nil | |
2015 | (unless nomessage | |
2016 | (message "Loading %s..." file)) | |
2017 | (let ((local-copy (file-local-copy file))) | |
2018 | ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil. | |
2019 | (load local-copy noerror t t) | |
2020 | (delete-file local-copy)) | |
2021 | (unless nomessage | |
2022 | (message "Loading %s...done" file)) | |
2023 | t))) | |
fb7933a3 | 2024 | |
7432277c | 2025 | ;; Localname manipulation functions that grok TRAMP localnames... |
fb7933a3 KG |
2026 | (defun tramp-handle-file-name-directory (file) |
2027 | "Like `file-name-directory' but aware of TRAMP files." | |
2028 | ;; everything except the last filename thing is the directory | |
c62c9d08 | 2029 | (with-parsed-tramp-file-name file nil |
505edaeb | 2030 | ;; For the following condition, two possibilities should be tried: |
7432277c KG |
2031 | ;; (1) (string= localname "") |
2032 | ;; (2) (or (string= localname "") (string= localname "/")) | |
505edaeb KG |
2033 | ;; The second variant fails when completing a "/" directory on |
2034 | ;; the remote host, that is a filename which looks like | |
2035 | ;; "/user@host:/". But maybe wildcards fail with the first variant. | |
2036 | ;; We should do some investigation. | |
7432277c | 2037 | (if (string= localname "") |
fb7933a3 KG |
2038 | ;; For a filename like "/[foo]", we return "/". The `else' |
2039 | ;; case would return "/[foo]" unchanged. But if we do that, | |
2040 | ;; then `file-expand-wildcards' ceases to work. It's not | |
2041 | ;; quite clear to me what's the intuition that tells that this | |
2042 | ;; behavior is the right behavior, but oh, well. | |
2043 | "/" | |
7432277c | 2044 | ;; run the command on the localname portion only |
fb7933a3 KG |
2045 | ;; CCC: This should take into account the remote machine type, no? |
2046 | ;; --daniel <daniel@danann.net> | |
2047 | (tramp-make-tramp-file-name multi-method method user host | |
2048 | ;; This will not recurse... | |
7432277c | 2049 | (or (file-name-directory localname) ""))))) |
fb7933a3 KG |
2050 | |
2051 | (defun tramp-handle-file-name-nondirectory (file) | |
2052 | "Like `file-name-nondirectory' but aware of TRAMP files." | |
c62c9d08 | 2053 | (with-parsed-tramp-file-name file nil |
7432277c | 2054 | (file-name-nondirectory localname))) |
fb7933a3 KG |
2055 | |
2056 | (defun tramp-handle-file-truename (filename &optional counter prev-dirs) | |
2057 | "Like `file-truename' for tramp files." | |
c62c9d08 | 2058 | (with-parsed-tramp-file-name filename nil |
7432277c | 2059 | (let* ((steps (tramp-split-string localname "/")) |
19a87064 MA |
2060 | (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs |
2061 | (file-name-as-directory localname))) | |
7432277c | 2062 | (is-dir (string= localname localnamedir)) |
c62c9d08 KG |
2063 | (thisstep nil) |
2064 | (numchase 0) | |
2065 | ;; Don't make the following value larger than necessary. | |
2066 | ;; People expect an error message in a timely fashion when | |
2067 | ;; something is wrong; otherwise they might think that Emacs | |
2068 | ;; is hung. Of course, correctness has to come first. | |
2069 | (numchase-limit 20) | |
2070 | (result nil) ;result steps in reverse order | |
c62c9d08 | 2071 | symlink-target) |
fb7933a3 KG |
2072 | (tramp-message-for-buffer |
2073 | multi-method method user host | |
c62c9d08 KG |
2074 | 10 "Finding true name for `%s'" filename) |
2075 | (while (and steps (< numchase numchase-limit)) | |
2076 | (setq thisstep (pop steps)) | |
2077 | (tramp-message-for-buffer | |
2078 | multi-method method user host | |
2079 | 10 "Check %s" | |
2080 | (mapconcat 'identity | |
2081 | (append '("") (reverse result) (list thisstep)) | |
2082 | "/")) | |
2083 | (setq symlink-target | |
c951aecb | 2084 | (nth 0 (file-attributes |
c62c9d08 KG |
2085 | (tramp-make-tramp-file-name |
2086 | multi-method method user host | |
2087 | (mapconcat 'identity | |
b1d06e75 KG |
2088 | (append '("") |
2089 | (reverse result) | |
2090 | (list thisstep)) | |
c62c9d08 KG |
2091 | "/"))))) |
2092 | (cond ((string= "." thisstep) | |
2093 | (tramp-message-for-buffer multi-method method user host | |
2094 | 10 "Ignoring step `.'")) | |
2095 | ((string= ".." thisstep) | |
2096 | (tramp-message-for-buffer multi-method method user host | |
2097 | 10 "Processing step `..'") | |
2098 | (pop result)) | |
2099 | ((stringp symlink-target) | |
2100 | ;; It's a symlink, follow it. | |
2101 | (tramp-message-for-buffer | |
2102 | multi-method method user host | |
2103 | 10 "Follow symlink to %s" symlink-target) | |
2104 | (setq numchase (1+ numchase)) | |
2105 | (when (file-name-absolute-p symlink-target) | |
2106 | (setq result nil)) | |
b25a52cc KG |
2107 | ;; If the symlink was absolute, we'll get a string like |
2108 | ;; "/user@host:/some/target"; extract the | |
2109 | ;; "/some/target" part from it. | |
2110 | (when (tramp-tramp-file-p symlink-target) | |
2111 | (with-parsed-tramp-file-name symlink-target sym | |
2112 | (unless (equal (list multi-method method user host) | |
2113 | (list sym-multi-method sym-method | |
2114 | sym-user sym-host)) | |
2115 | (error "Symlink target `%s' on wrong host" | |
2116 | symlink-target)) | |
2117 | (setq symlink-target localname))) | |
c62c9d08 KG |
2118 | (setq steps |
2119 | (append (tramp-split-string symlink-target "/") steps))) | |
2120 | (t | |
2121 | ;; It's a file. | |
2122 | (setq result (cons thisstep result))))) | |
2123 | (when (>= numchase numchase-limit) | |
2124 | (error "Maximum number (%d) of symlinks exceeded" numchase-limit)) | |
2125 | (setq result (reverse result)) | |
487f4fb7 KG |
2126 | ;; Combine list to form string. |
2127 | (setq result | |
2128 | (if result | |
2129 | (mapconcat 'identity (cons "" result) "/") | |
2130 | "/")) | |
2131 | (when (and is-dir (or (string= "" result) | |
2132 | (not (string= (substring result -1) "/")))) | |
2133 | (setq result (concat result "/"))) | |
c62c9d08 KG |
2134 | (tramp-message-for-buffer |
2135 | multi-method method user host | |
487f4fb7 | 2136 | 10 "True name of `%s' is `%s'" filename result) |
c62c9d08 | 2137 | (tramp-make-tramp-file-name |
487f4fb7 | 2138 | multi-method method user host result)))) |
fb7933a3 KG |
2139 | |
2140 | ;; Basic functions. | |
2141 | ||
2142 | (defun tramp-handle-file-exists-p (filename) | |
2143 | "Like `file-exists-p' for tramp files." | |
c62c9d08 | 2144 | (with-parsed-tramp-file-name filename nil |
fb7933a3 KG |
2145 | (save-excursion |
2146 | (zerop (tramp-send-command-and-check | |
2147 | multi-method method user host | |
2148 | (format | |
c62c9d08 | 2149 | (tramp-get-file-exists-command multi-method method user host) |
7432277c | 2150 | (tramp-shell-quote-argument localname))))))) |
fb7933a3 | 2151 | |
8daea7fc KG |
2152 | ;; Devices must distinguish physical file systems. The device numbers |
2153 | ;; provided by "lstat" aren't unique, because we operate on different hosts. | |
2154 | ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and | |
2155 | ;; EFS use device number "-1". In order to be different, we use device number | |
2156 | ;; (-1 x), whereby "x" is unique for a given (multi-method method user host). | |
2157 | (defvar tramp-devices nil | |
2158 | "Keeps virtual device numbers.") | |
2159 | ||
fb7933a3 KG |
2160 | ;; CCC: This should check for an error condition and signal failure |
2161 | ;; when something goes wrong. | |
2162 | ;; Daniel Pittman <daniel@danann.net> | |
c951aecb KG |
2163 | (defun tramp-handle-file-attributes (filename &optional id-format) |
2164 | "Like `file-attributes' for tramp files." | |
2165 | (let ((nonnumeric (and id-format (equal id-format 'string))) | |
2166 | result) | |
ac474af1 | 2167 | (with-parsed-tramp-file-name filename nil |
ac474af1 KG |
2168 | (when (tramp-handle-file-exists-p filename) |
2169 | ;; file exists, find out stuff | |
2170 | (save-excursion | |
fb7933a3 | 2171 | (if (tramp-get-remote-perl multi-method method user host) |
ac474af1 KG |
2172 | (setq result |
2173 | (tramp-handle-file-attributes-with-perl | |
7432277c | 2174 | multi-method method user host localname nonnumeric)) |
ac474af1 KG |
2175 | (setq result |
2176 | (tramp-handle-file-attributes-with-ls | |
7432277c | 2177 | multi-method method user host localname nonnumeric))) |
8daea7fc KG |
2178 | ;; set virtual device number |
2179 | (setcar (nthcdr 11 result) | |
2180 | (tramp-get-device multi-method method user host))))) | |
ac474af1 | 2181 | result)) |
fb7933a3 | 2182 | |
fb7933a3 | 2183 | (defun tramp-handle-file-attributes-with-ls |
7432277c | 2184 | (multi-method method user host localname &optional nonnumeric) |
fb7933a3 KG |
2185 | "Implement `file-attributes' for tramp files using the ls(1) command." |
2186 | (let (symlinkp dirp | |
2187 | res-inode res-filemodes res-numlinks | |
2188 | res-uid res-gid res-size res-symlink-target) | |
ac474af1 KG |
2189 | (tramp-message-for-buffer multi-method method user host 10 |
2190 | "file attributes with ls: %s" | |
2191 | (tramp-make-tramp-file-name | |
7432277c | 2192 | multi-method method user host localname)) |
fb7933a3 KG |
2193 | (tramp-send-command |
2194 | multi-method method user host | |
2195 | (format "%s %s %s" | |
2196 | (tramp-get-ls-command multi-method method user host) | |
2197 | (if nonnumeric "-ild" "-ildn") | |
7432277c | 2198 | (tramp-shell-quote-argument localname))) |
fb7933a3 KG |
2199 | (tramp-wait-for-output) |
2200 | ;; parse `ls -l' output ... | |
2201 | ;; ... inode | |
2202 | (setq res-inode | |
2203 | (condition-case err | |
2204 | (read (current-buffer)) | |
2205 | (invalid-read-syntax | |
2206 | (when (and (equal (cadr err) | |
2207 | "Integer constant overflow in reader") | |
2208 | (string-match | |
2209 | "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'" | |
2210 | (caddr err))) | |
2211 | (let* ((big (read (substring (caddr err) 0 | |
2212 | (match-beginning 1)))) | |
2213 | (small (read (match-string 1 (caddr err)))) | |
2214 | (twiddle (/ small 65536))) | |
2215 | (cons (+ big twiddle) | |
2216 | (- small (* twiddle 65536)))))))) | |
2217 | ;; ... file mode flags | |
2218 | (setq res-filemodes (symbol-name (read (current-buffer)))) | |
2219 | ;; ... number links | |
2220 | (setq res-numlinks (read (current-buffer))) | |
2221 | ;; ... uid and gid | |
2222 | (setq res-uid (read (current-buffer))) | |
2223 | (setq res-gid (read (current-buffer))) | |
2224 | (unless nonnumeric | |
2225 | (unless (numberp res-uid) (setq res-uid -1)) | |
2226 | (unless (numberp res-gid) (setq res-gid -1))) | |
2227 | ;; ... size | |
2228 | (setq res-size (read (current-buffer))) | |
2229 | ;; From the file modes, figure out other stuff. | |
2230 | (setq symlinkp (eq ?l (aref res-filemodes 0))) | |
2231 | (setq dirp (eq ?d (aref res-filemodes 0))) | |
2232 | ;; if symlink, find out file name pointed to | |
2233 | (when symlinkp | |
2234 | (search-forward "-> ") | |
2235 | (setq res-symlink-target | |
2236 | (buffer-substring (point) | |
2237 | (tramp-line-end-position)))) | |
2238 | ;; return data gathered | |
2239 | (list | |
2240 | ;; 0. t for directory, string (name linked to) for symbolic | |
2241 | ;; link, or nil. | |
2242 | (or dirp res-symlink-target nil) | |
2243 | ;; 1. Number of links to file. | |
2244 | res-numlinks | |
2245 | ;; 2. File uid. | |
2246 | res-uid | |
2247 | ;; 3. File gid. | |
2248 | res-gid | |
2249 | ;; 4. Last access time, as a list of two integers. First | |
2250 | ;; integer has high-order 16 bits of time, second has low 16 | |
2251 | ;; bits. | |
2252 | ;; 5. Last modification time, likewise. | |
2253 | ;; 6. Last status change time, likewise. | |
2254 | '(0 0) '(0 0) '(0 0) ;CCC how to find out? | |
2255 | ;; 7. Size in bytes (-1, if number is out of range). | |
2256 | res-size | |
2257 | ;; 8. File modes, as a string of ten letters or dashes as in ls -l. | |
2258 | res-filemodes | |
2259 | ;; 9. t iff file's gid would change if file were deleted and | |
2260 | ;; recreated. | |
2261 | nil ;hm? | |
2262 | ;; 10. inode number. | |
2263 | res-inode | |
8daea7fc KG |
2264 | ;; 11. Device number. Will be replaced by a virtual device number. |
2265 | -1 | |
fb7933a3 KG |
2266 | ))) |
2267 | ||
2268 | (defun tramp-handle-file-attributes-with-perl | |
7432277c | 2269 | (multi-method method user host localname &optional nonnumeric) |
fb7933a3 KG |
2270 | "Implement `file-attributes' for tramp files using a Perl script. |
2271 | ||
2272 | The Perl command is sent to the remote machine when the connection | |
2273 | is initially created and is kept cached by the remote shell." | |
ac474af1 KG |
2274 | (tramp-message-for-buffer multi-method method user host 10 |
2275 | "file attributes with perl: %s" | |
2276 | (tramp-make-tramp-file-name | |
7432277c | 2277 | multi-method method user host localname)) |
fb7933a3 KG |
2278 | (tramp-send-command |
2279 | multi-method method user host | |
c951aecb KG |
2280 | (format "tramp_file_attributes %s %s" |
2281 | (tramp-shell-quote-argument localname) nonnumeric)) | |
fb7933a3 KG |
2282 | (tramp-wait-for-output) |
2283 | (let ((result (read (current-buffer)))) | |
2284 | (setcar (nthcdr 8 result) | |
2285 | (tramp-file-mode-from-int (nth 8 result))) | |
2286 | result)) | |
2287 | ||
8daea7fc KG |
2288 | (defun tramp-get-device (multi-method method user host) |
2289 | "Returns the virtual device number. | |
2290 | If it doesn't exist, generate a new one." | |
2291 | (let ((string (tramp-make-tramp-file-name multi-method method user host ""))) | |
2292 | (unless (assoc string tramp-devices) | |
2293 | (add-to-list 'tramp-devices | |
2294 | (list string (length tramp-devices)))) | |
2295 | (list -1 (nth 1 (assoc string tramp-devices))))) | |
2296 | ||
fb7933a3 KG |
2297 | (defun tramp-handle-set-visited-file-modtime (&optional time-list) |
2298 | "Like `set-visited-file-modtime' for tramp files." | |
2299 | (unless (buffer-file-name) | |
2300 | (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file" | |
2301 | (buffer-name))) | |
2302 | (when time-list | |
2303 | (tramp-run-real-handler 'set-visited-file-modtime (list time-list))) | |
c62c9d08 KG |
2304 | (let ((f (buffer-file-name)) |
2305 | (coding-system-used nil)) | |
2306 | (with-parsed-tramp-file-name f nil | |
c62c9d08 KG |
2307 | (let* ((attr (file-attributes f)) |
2308 | (modtime (nth 5 attr))) | |
2309 | ;; We use '(0 0) as a don't-know value. See also | |
2310 | ;; `tramp-handle-file-attributes-with-ls'. | |
2311 | (when (boundp 'last-coding-system-used) | |
2312 | (setq coding-system-used last-coding-system-used)) | |
fb7933a3 | 2313 | (if (not (equal modtime '(0 0))) |
c62c9d08 | 2314 | (tramp-run-real-handler 'set-visited-file-modtime (list modtime)) |
fb7933a3 KG |
2315 | (save-excursion |
2316 | (tramp-send-command | |
2317 | multi-method method user host | |
2318 | (format "%s -ild %s" | |
2319 | (tramp-get-ls-command multi-method method user host) | |
7432277c | 2320 | (tramp-shell-quote-argument localname))) |
fb7933a3 KG |
2321 | (tramp-wait-for-output) |
2322 | (setq attr (buffer-substring (point) | |
2323 | (progn (end-of-line) (point))))) | |
c62c9d08 KG |
2324 | (setq tramp-buffer-file-attributes attr)) |
2325 | (when (boundp 'last-coding-system-used) | |
2326 | (setq last-coding-system-used coding-system-used)) | |
fb7933a3 KG |
2327 | nil)))) |
2328 | ||
c62c9d08 KG |
2329 | ;; CCC continue here |
2330 | ||
2331 | ;; This function makes the same assumption as | |
2332 | ;; `tramp-handle-set-visited-file-modtime'. | |
2333 | (defun tramp-handle-verify-visited-file-modtime (buf) | |
2334 | "Like `verify-visited-file-modtime' for tramp files." | |
2335 | (with-current-buffer buf | |
2336 | (let ((f (buffer-file-name))) | |
2337 | (with-parsed-tramp-file-name f nil | |
c62c9d08 KG |
2338 | (let* ((attr (file-attributes f)) |
2339 | (modtime (nth 5 attr))) | |
2340 | (cond ((and attr (not (equal modtime '(0 0)))) | |
2341 | ;; Why does `file-attributes' return a list (HIGH | |
2342 | ;; LOW), but `visited-file-modtime' returns a cons | |
2343 | ;; (HIGH . LOW)? | |
2344 | (let ((mt (visited-file-modtime))) | |
2345 | (< (abs (tramp-time-diff | |
12cd6dfb LT |
2346 | modtime |
2347 | ;; For compatibility, deal with both the old | |
2348 | ;; (HIGH . LOW) and the new (HIGH LOW) | |
2349 | ;; return values of `visited-file-modtime'. | |
2350 | (if (atom (cdr mt)) | |
2351 | (list (car mt) (cdr mt)) | |
2352 | mt))) | |
2353 | 2))) | |
c62c9d08 KG |
2354 | (attr |
2355 | (save-excursion | |
2356 | (tramp-send-command | |
2357 | multi-method method user host | |
2358 | (format "%s -ild %s" | |
2359 | (tramp-get-ls-command multi-method method | |
2360 | user host) | |
7432277c | 2361 | (tramp-shell-quote-argument localname))) |
c62c9d08 KG |
2362 | (tramp-wait-for-output) |
2363 | (setq attr (buffer-substring | |
2364 | (point) (progn (end-of-line) (point))))) | |
2365 | (equal tramp-buffer-file-attributes attr)) | |
2366 | ;; If file does not exist, say it is not modified. | |
487fa986 | 2367 | (t nil))))))) |
c62c9d08 | 2368 | |
fb7933a3 KG |
2369 | (defadvice clear-visited-file-modtime (after tramp activate) |
2370 | "Set `tramp-buffer-file-attributes' back to nil. | |
2371 | Tramp uses this variable as an emulation for the actual modtime of the file, | |
2372 | if the remote host can't provide the modtime." | |
2373 | (setq tramp-buffer-file-attributes nil)) | |
2374 | ||
2375 | (defun tramp-handle-set-file-modes (filename mode) | |
2376 | "Like `set-file-modes' for tramp files." | |
c62c9d08 | 2377 | (with-parsed-tramp-file-name filename nil |
fb7933a3 KG |
2378 | (save-excursion |
2379 | (unless (zerop (tramp-send-command-and-check | |
c62c9d08 KG |
2380 | multi-method method user host |
2381 | (format "chmod %s %s" | |
2382 | (tramp-decimal-to-octal mode) | |
7432277c | 2383 | (tramp-shell-quote-argument localname)))) |
fb7933a3 KG |
2384 | (signal 'file-error |
2385 | (list "Doing chmod" | |
2386 | ;; FIXME: extract the proper text from chmod's stderr. | |
2387 | "error while changing file's mode" | |
2388 | filename)))))) | |
2389 | ||
2390 | ;; Simple functions using the `test' command. | |
2391 | ||
2392 | (defun tramp-handle-file-executable-p (filename) | |
2393 | "Like `file-executable-p' for tramp files." | |
c62c9d08 | 2394 | (with-parsed-tramp-file-name filename nil |
c62c9d08 | 2395 | (zerop (tramp-run-test "-x" filename)))) |
fb7933a3 KG |
2396 | |
2397 | (defun tramp-handle-file-readable-p (filename) | |
2398 | "Like `file-readable-p' for tramp files." | |
c62c9d08 | 2399 | (with-parsed-tramp-file-name filename nil |
c62c9d08 | 2400 | (zerop (tramp-run-test "-r" filename)))) |
fb7933a3 KG |
2401 | |
2402 | (defun tramp-handle-file-accessible-directory-p (filename) | |
2403 | "Like `file-accessible-directory-p' for tramp files." | |
c62c9d08 | 2404 | (with-parsed-tramp-file-name filename nil |
c62c9d08 KG |
2405 | (and (zerop (tramp-run-test "-d" filename)) |
2406 | (zerop (tramp-run-test "-r" filename)) | |
2407 | (zerop (tramp-run-test "-x" filename))))) | |
fb7933a3 KG |
2408 | |
2409 | ;; When the remote shell is started, it looks for a shell which groks | |
2410 | ;; tilde expansion. Here, we assume that all shells which grok tilde | |
2411 | ;; expansion will also provide a `test' command which groks `-nt' (for | |
2412 | ;; newer than). If this breaks, tell me about it and I'll try to do | |
2413 | ;; something smarter about it. | |
2414 | (defun tramp-handle-file-newer-than-file-p (file1 file2) | |
2415 | "Like `file-newer-than-file-p' for tramp files." | |
2416 | (cond ((not (file-exists-p file1)) | |
2417 | nil) | |
2418 | ((not (file-exists-p file2)) | |
2419 | t) | |
91879624 | 2420 | ;; We are sure both files exist at this point. |
fb7933a3 KG |
2421 | (t |
2422 | (save-excursion | |
91879624 KG |
2423 | ;; We try to get the mtime of both files. If they are not |
2424 | ;; equal to the "dont-know" value, then we subtract the times | |
2425 | ;; and obtain the result. | |
2426 | (let ((fa1 (file-attributes file1)) | |
2427 | (fa2 (file-attributes file2))) | |
2428 | (if (and (not (equal (nth 5 fa1) '(0 0))) | |
2429 | (not (equal (nth 5 fa2) '(0 0)))) | |
ea9d1443 | 2430 | (> 0 (tramp-time-diff (nth 5 fa1) (nth 5 fa2))) |
91879624 KG |
2431 | ;; If one of them is the dont-know value, then we can |
2432 | ;; still try to run a shell command on the remote host. | |
2433 | ;; However, this only works if both files are Tramp | |
2434 | ;; files and both have the same method, same user, same | |
2435 | ;; host. | |
c62c9d08 KG |
2436 | (unless (and (tramp-tramp-file-p file1) |
2437 | (tramp-tramp-file-p file2)) | |
91879624 KG |
2438 | (signal |
2439 | 'file-error | |
2440 | (list | |
2441 | "Cannot check if Tramp file is newer than non-Tramp file" | |
2442 | file1 file2))) | |
2443 | (with-parsed-tramp-file-name file1 v1 | |
2444 | (with-parsed-tramp-file-name file2 v2 | |
91879624 KG |
2445 | (unless (and (equal v1-multi-method v2-multi-method) |
2446 | (equal v1-method v2-method) | |
2447 | (equal v1-user v2-user) | |
2448 | (equal v1-host v2-host)) | |
2449 | (signal 'file-error | |
2450 | (list "Files must have same method, user, host" | |
2451 | file1 file2))) | |
2452 | (unless (and (tramp-tramp-file-p file1) | |
2453 | (tramp-tramp-file-p file2)) | |
2454 | (signal 'file-error | |
2455 | (list "Files must be tramp files on same host" | |
2456 | file1 file2))) | |
2457 | (if (tramp-get-test-groks-nt | |
2458 | v1-multi-method v1-method v1-user v1-host) | |
2459 | (zerop (tramp-run-test2 "test" file1 file2 "-nt")) | |
2460 | (zerop (tramp-run-test2 | |
2461 | "tramp_test_nt" file1 file2))))))))))) | |
fb7933a3 KG |
2462 | |
2463 | ;; Functions implemented using the basic functions above. | |
2464 | ||
2465 | (defun tramp-handle-file-modes (filename) | |
2466 | "Like `file-modes' for tramp files." | |
c62c9d08 | 2467 | (with-parsed-tramp-file-name filename nil |
c62c9d08 KG |
2468 | (when (file-exists-p filename) |
2469 | (tramp-mode-string-to-int | |
4007ba5b | 2470 | (nth 8 (file-attributes filename)))))) |
fb7933a3 KG |
2471 | |
2472 | (defun tramp-handle-file-directory-p (filename) | |
2473 | "Like `file-directory-p' for tramp files." | |
2474 | ;; Care must be taken that this function returns `t' for symlinks | |
2475 | ;; pointing to directories. Surely the most obvious implementation | |
2476 | ;; would be `test -d', but that returns false for such symlinks. | |
2477 | ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And | |
2478 | ;; I now think he's right. So we could be using `test -d', couldn't | |
2479 | ;; we? | |
2480 | ;; | |
2481 | ;; Alternatives: `cd %s', `test -d %s' | |
c62c9d08 | 2482 | (with-parsed-tramp-file-name filename nil |
c62c9d08 | 2483 | (save-excursion |
fb7933a3 KG |
2484 | (zerop |
2485 | (tramp-send-command-and-check | |
c62c9d08 KG |
2486 | multi-method method user host |
2487 | (format "test -d %s" | |
7432277c | 2488 | (tramp-shell-quote-argument localname)) |
c62c9d08 | 2489 | t))))) ;run command in subshell |
fb7933a3 KG |
2490 | |
2491 | (defun tramp-handle-file-regular-p (filename) | |
2492 | "Like `file-regular-p' for tramp files." | |
c62c9d08 | 2493 | (with-parsed-tramp-file-name filename nil |
8daea7fc KG |
2494 | (and (file-exists-p filename) |
2495 | (eq ?- (aref (nth 8 (file-attributes filename)) 0))))) | |
fb7933a3 KG |
2496 | |
2497 | (defun tramp-handle-file-symlink-p (filename) | |
2498 | "Like `file-symlink-p' for tramp files." | |
c62c9d08 | 2499 | (with-parsed-tramp-file-name filename nil |
c951aecb | 2500 | (let ((x (car (file-attributes filename)))) |
b25a52cc KG |
2501 | (when (stringp x) |
2502 | ;; When Tramp is running on VMS, then `file-name-absolute-p' | |
2503 | ;; might do weird things. | |
2504 | (if (file-name-absolute-p x) | |
2505 | (tramp-make-tramp-file-name | |
2506 | multi-method method user host x) | |
2507 | x))))) | |
fb7933a3 KG |
2508 | |
2509 | (defun tramp-handle-file-writable-p (filename) | |
2510 | "Like `file-writable-p' for tramp files." | |
c62c9d08 | 2511 | (with-parsed-tramp-file-name filename nil |
c62c9d08 KG |
2512 | (if (tramp-handle-file-exists-p filename) |
2513 | ;; Existing files must be writable. | |
2514 | (zerop (tramp-run-test "-w" filename)) | |
2515 | ;; If file doesn't exist, check if directory is writable. | |
2516 | (and (zerop (tramp-run-test | |
2517 | "-d" (tramp-handle-file-name-directory filename))) | |
2518 | (zerop (tramp-run-test | |
2519 | "-w" (tramp-handle-file-name-directory filename))))))) | |
fb7933a3 KG |
2520 | |
2521 | (defun tramp-handle-file-ownership-preserved-p (filename) | |
2522 | "Like `file-ownership-preserved-p' for tramp files." | |
c62c9d08 | 2523 | (with-parsed-tramp-file-name filename nil |
c62c9d08 KG |
2524 | (or (not (tramp-handle-file-exists-p filename)) |
2525 | ;; Existing files must be writable. | |
2526 | (zerop (tramp-run-test "-O" filename))))) | |
fb7933a3 KG |
2527 | |
2528 | ;; Other file name ops. | |
2529 | ||
83bbd71b | 2530 | ;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de> |
fb7933a3 KG |
2531 | ;; (defun tramp-handle-directory-file-name (directory) |
2532 | ;; "Like `directory-file-name' for tramp files." | |
2533 | ;; (if (and (eq (aref directory (- (length directory) 1)) ?/) | |
2534 | ;; (not (eq (aref directory (- (length directory) 2)) ?:))) | |
2535 | ;; (substring directory 0 (- (length directory) 1)) | |
2536 | ;; directory)) | |
2537 | ||
8daea7fc KG |
2538 | ;; ;; Philippe Troin <phil@fifi.org> |
2539 | ;; (defun tramp-handle-directory-file-name (directory) | |
2540 | ;; "Like `directory-file-name' for tramp files." | |
2541 | ;; (with-parsed-tramp-file-name directory nil | |
2542 | ;; (let ((directory-length-1 (1- (length directory)))) | |
2543 | ;; (save-match-data | |
2544 | ;; (if (and (eq (aref directory directory-length-1) ?/) | |
2545 | ;; (eq (string-match tramp-file-name-regexp directory) 0) | |
2546 | ;; (/= (match-end 0) directory-length-1)) | |
2547 | ;; (substring directory 0 directory-length-1) | |
2548 | ;; directory))))) | |
2549 | ||
fb7933a3 KG |
2550 | (defun tramp-handle-directory-file-name (directory) |
2551 | "Like `directory-file-name' for tramp files." | |
7432277c KG |
2552 | ;; If localname component of filename is "/", leave it unchanged. |
2553 | ;; Otherwise, remove any trailing slash from localname component. | |
8daea7fc KG |
2554 | ;; Method, host, etc, are unchanged. Does it make sense to try |
2555 | ;; to avoid parsing the filename? | |
c62c9d08 | 2556 | (with-parsed-tramp-file-name directory nil |
7432277c KG |
2557 | (if (and (not (zerop (length localname))) |
2558 | (eq (aref localname (1- (length localname))) ?/) | |
2559 | (not (string= localname "/"))) | |
8daea7fc KG |
2560 | (substring directory 0 -1) |
2561 | directory))) | |
fb7933a3 KG |
2562 | |
2563 | ;; Directory listings. | |
2564 | ||
ac474af1 KG |
2565 | (defun tramp-handle-directory-files (directory |
2566 | &optional full match nosort files-only) | |
fb7933a3 | 2567 | "Like `directory-files' for tramp files." |
c62c9d08 | 2568 | (with-parsed-tramp-file-name directory nil |
c62c9d08 | 2569 | (let (result x) |
fb7933a3 KG |
2570 | (save-excursion |
2571 | (tramp-barf-unless-okay | |
2572 | multi-method method user host | |
7432277c | 2573 | (concat "cd " (tramp-shell-quote-argument localname)) |
c62c9d08 KG |
2574 | nil |
2575 | 'file-error | |
2576 | "tramp-handle-directory-files: couldn't `cd %s'" | |
7432277c | 2577 | (tramp-shell-quote-argument localname)) |
fb7933a3 KG |
2578 | (tramp-send-command |
2579 | multi-method method user host | |
c62c9d08 KG |
2580 | (concat (tramp-get-ls-command multi-method method user host) |
2581 | " -a | cat")) | |
fb7933a3 KG |
2582 | (tramp-wait-for-output) |
2583 | (goto-char (point-max)) | |
2584 | (while (zerop (forward-line -1)) | |
c62c9d08 KG |
2585 | (setq x (buffer-substring (point) |
2586 | (tramp-line-end-position))) | |
2587 | (when (or (not match) (string-match match x)) | |
2588 | (if full | |
2589 | (push (concat (file-name-as-directory directory) | |
2590 | x) | |
2591 | result) | |
2592 | (push x result)))) | |
fb7933a3 | 2593 | (tramp-send-command multi-method method user host "cd") |
ac474af1 KG |
2594 | (tramp-wait-for-output) |
2595 | ;; Remove non-files or non-directories if necessary. Using | |
2596 | ;; the remote shell for this would probably be way faster. | |
2597 | ;; Maybe something could be adapted from | |
2598 | ;; tramp-handle-file-name-all-completions. | |
2599 | (when files-only | |
2600 | (let ((temp (nreverse result)) | |
2601 | item) | |
2602 | (setq result nil) | |
2603 | (if (equal files-only t) | |
2604 | ;; files only | |
2605 | (while temp | |
2606 | (setq item (pop temp)) | |
2607 | (when (file-regular-p item) | |
2608 | (push item result))) | |
2609 | ;; directories only | |
2610 | (while temp | |
2611 | (setq item (pop temp)) | |
2612 | (when (file-directory-p item) | |
2613 | (push item result))))))) | |
c62c9d08 KG |
2614 | result))) |
2615 | ||
2616 | ;; This function should return "foo/" for directories and "bar" for | |
2617 | ;; files. We use `ls -ad' to get a list of files (including | |
2618 | ;; directories), and `find . -type d \! -name . -prune' to get a list | |
2619 | ;; of directories. | |
2620 | (defun tramp-handle-file-name-all-completions (filename directory) | |
2621 | "Like `file-name-all-completions' for tramp files." | |
2622 | (with-parsed-tramp-file-name directory nil | |
c62c9d08 KG |
2623 | (unless (save-match-data (string-match "/" filename)) |
2624 | (let* ((nowild tramp-completion-without-shell-p) | |
2625 | result) | |
2626 | (save-excursion | |
2627 | (tramp-barf-unless-okay | |
2628 | multi-method method user host | |
7432277c | 2629 | (format "cd %s" (tramp-shell-quote-argument localname)) |
c62c9d08 KG |
2630 | nil 'file-error |
2631 | "tramp-handle-file-name-all-completions: Couldn't `cd %s'" | |
7432277c | 2632 | (tramp-shell-quote-argument localname)) |
c62c9d08 KG |
2633 | |
2634 | ;; Get a list of directories and files, including reliably | |
2635 | ;; tagging the directories with a trailing '/'. Because I | |
2636 | ;; rock. --daniel@danann.net | |
2637 | (tramp-send-command | |
2638 | multi-method method user host | |
2639 | (format (concat "%s -a %s 2>/dev/null | while read f; do " | |
2640 | "if test -d \"$f\" 2>/dev/null; " | |
2641 | "then echo \"$f/\"; else echo \"$f\"; fi; done") | |
2642 | (tramp-get-ls-command multi-method method user host) | |
2643 | (if (or nowild (zerop (length filename))) | |
2644 | "" | |
2645 | (format "-d %s*" | |
2646 | (tramp-shell-quote-argument filename))))) | |
2647 | ||
2648 | ;; Now grab the output. | |
2649 | (tramp-wait-for-output) | |
2650 | (goto-char (point-max)) | |
2651 | (while (zerop (forward-line -1)) | |
2652 | (push (buffer-substring (point) | |
2653 | (tramp-line-end-position)) | |
2654 | result)) | |
7432277c | 2655 | |
c62c9d08 KG |
2656 | (tramp-send-command multi-method method user host "cd") |
2657 | (tramp-wait-for-output) | |
fb7933a3 | 2658 | |
c62c9d08 KG |
2659 | ;; Return the list. |
2660 | (if nowild | |
2661 | (all-completions filename (mapcar 'list result)) | |
2662 | result)))))) | |
fb7933a3 KG |
2663 | |
2664 | ||
2665 | ;; The following isn't needed for Emacs 20 but for 19.34? | |
2666 | (defun tramp-handle-file-name-completion (filename directory) | |
2667 | "Like `file-name-completion' for tramp files." | |
2668 | (unless (tramp-tramp-file-p directory) | |
2669 | (error | |
2670 | "tramp-handle-file-name-completion invoked on non-tramp directory `%s'" | |
2671 | directory)) | |
c62c9d08 | 2672 | (with-parsed-tramp-file-name directory nil |
c62c9d08 KG |
2673 | (try-completion |
2674 | filename | |
2675 | (mapcar (lambda (x) (cons x nil)) | |
4007ba5b | 2676 | (file-name-all-completions filename directory))))) |
fb7933a3 KG |
2677 | |
2678 | ;; cp, mv and ln | |
2679 | ||
2680 | (defun tramp-handle-add-name-to-file | |
2681 | (filename newname &optional ok-if-already-exists) | |
2682 | "Like `add-name-to-file' for tramp files." | |
c62c9d08 KG |
2683 | (with-parsed-tramp-file-name filename v1 |
2684 | (with-parsed-tramp-file-name newname v2 | |
2685 | (let ((ln (when v1 (tramp-get-remote-ln | |
2686 | v1-multi-method v1-method v1-user v1-host)))) | |
2687 | (unless (and v1-method v2-method v1-user v2-user v1-host v2-host | |
2688 | (equal v1-multi-method v2-multi-method) | |
2689 | (equal v1-method v2-method) | |
2690 | (equal v1-user v2-user) | |
2691 | (equal v1-host v2-host)) | |
2692 | (error "add-name-to-file: %s" | |
2693 | "only implemented for same method, same user, same host")) | |
c62c9d08 KG |
2694 | (when (and (not ok-if-already-exists) |
2695 | (file-exists-p newname) | |
2696 | (not (numberp ok-if-already-exists)) | |
2697 | (y-or-n-p | |
2698 | (format | |
2699 | "File %s already exists; make it a new name anyway? " | |
2700 | newname))) | |
2701 | (error "add-name-to-file: file %s already exists" newname)) | |
2702 | (tramp-barf-unless-okay | |
2703 | v1-multi-method v1-method v1-user v1-host | |
7432277c KG |
2704 | (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname) |
2705 | (tramp-shell-quote-argument v2-localname)) | |
c62c9d08 KG |
2706 | nil 'file-error |
2707 | "error with add-name-to-file, see buffer `%s' for details" | |
2708 | (buffer-name)))))) | |
fb7933a3 KG |
2709 | |
2710 | (defun tramp-handle-copy-file | |
2711 | (filename newname &optional ok-if-already-exists keep-date) | |
2712 | "Like `copy-file' for tramp files." | |
2713 | ;; Check if both files are local -- invoke normal copy-file. | |
2714 | ;; Otherwise, use tramp from local system. | |
2715 | (setq filename (expand-file-name filename)) | |
2716 | (setq newname (expand-file-name newname)) | |
2717 | ;; At least one file a tramp file? | |
2718 | (if (or (tramp-tramp-file-p filename) | |
2719 | (tramp-tramp-file-p newname)) | |
ea9d1443 KG |
2720 | (let ((modes (file-modes filename))) |
2721 | (tramp-do-copy-or-rename-file | |
2722 | 'copy filename newname ok-if-already-exists keep-date) | |
2723 | (set-file-modes newname modes)) | |
fb7933a3 KG |
2724 | (tramp-run-real-handler |
2725 | 'copy-file | |
2726 | (list filename newname ok-if-already-exists keep-date)))) | |
2727 | ||
2728 | (defun tramp-handle-rename-file | |
2729 | (filename newname &optional ok-if-already-exists) | |
2730 | "Like `rename-file' for tramp files." | |
2731 | ;; Check if both files are local -- invoke normal rename-file. | |
2732 | ;; Otherwise, use tramp from local system. | |
2733 | (setq filename (expand-file-name filename)) | |
2734 | (setq newname (expand-file-name newname)) | |
2735 | ;; At least one file a tramp file? | |
2736 | (if (or (tramp-tramp-file-p filename) | |
2737 | (tramp-tramp-file-p newname)) | |
2738 | (tramp-do-copy-or-rename-file | |
2739 | 'rename filename newname ok-if-already-exists) | |
2740 | (tramp-run-real-handler 'rename-file | |
2741 | (list filename newname ok-if-already-exists)))) | |
2742 | ||
2743 | (defun tramp-do-copy-or-rename-file | |
2744 | (op filename newname &optional ok-if-already-exists keep-date) | |
2745 | "Copy or rename a remote file. | |
2746 | OP must be `copy' or `rename' and indicates the operation to perform. | |
2747 | FILENAME specifies the file to copy or rename, NEWNAME is the name of | |
2748 | the new file (for copy) or the new name of the file (for rename). | |
2749 | OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already. | |
2750 | KEEP-DATE means to make sure that NEWNAME has the same timestamp | |
2751 | as FILENAME. | |
2752 | ||
2753 | This function is invoked by `tramp-handle-copy-file' and | |
2754 | `tramp-handle-rename-file'. It is an error if OP is neither of `copy' | |
2755 | and `rename'. FILENAME and NEWNAME must be absolute file names." | |
2756 | (unless (memq op '(copy rename)) | |
2757 | (error "Unknown operation `%s', must be `copy' or `rename'" op)) | |
2758 | (unless ok-if-already-exists | |
2759 | (when (file-exists-p newname) | |
2760 | (signal 'file-already-exists | |
2761 | (list newname)))) | |
90dc758d | 2762 | (let ((t1 (tramp-tramp-file-p filename)) |
5ec2cc41 KG |
2763 | (t2 (tramp-tramp-file-p newname)) |
2764 | v1-multi-method v1-method v1-user v1-host v1-localname | |
2765 | v2-multi-method v2-method v2-user v2-host v2-localname) | |
2766 | ||
90dc758d | 2767 | ;; Check which ones of source and target are Tramp files. |
5ec2cc41 KG |
2768 | ;; We cannot invoke `with-parsed-tramp-file-name'; |
2769 | ;; it fails if the file isn't a Tramp file name. | |
2770 | (if t1 | |
2771 | (with-parsed-tramp-file-name filename l | |
2772 | (setq v1-multi-method l-multi-method | |
2773 | v1-method l-method | |
2774 | v1-user l-user | |
2775 | v1-host l-host | |
2776 | v1-localname l-localname)) | |
2777 | (setq v1-localname filename)) | |
2778 | (if t2 | |
2779 | (with-parsed-tramp-file-name newname l | |
2780 | (setq v2-multi-method l-multi-method | |
2781 | v2-method l-method | |
2782 | v2-user l-user | |
2783 | v2-host l-host | |
2784 | v2-localname l-localname)) | |
2785 | (setq v2-localname newname)) | |
2786 | ||
90dc758d | 2787 | (cond |
5ec2cc41 | 2788 | ;; Both are Tramp files. |
90dc758d | 2789 | ((and t1 t2) |
5ec2cc41 KG |
2790 | (cond |
2791 | ;; Shortcut: if method, host, user are the same for both | |
2792 | ;; files, we invoke `cp' or `mv' on the remote host | |
2793 | ;; directly. | |
2794 | ((and (equal v1-multi-method v2-multi-method) | |
2795 | (equal v1-method v2-method) | |
2796 | (equal v1-user v2-user) | |
2797 | (equal v1-host v2-host)) | |
2798 | (tramp-do-copy-or-rename-file-directly | |
2799 | op v1-multi-method v1-method v1-user v1-host | |
2800 | v1-localname v2-localname keep-date)) | |
2801 | ;; If both source and target are Tramp files, | |
2802 | ;; both are using the same copy-program, then we | |
2803 | ;; can invoke rcp directly. Note that | |
2804 | ;; default-directory should point to a local | |
2805 | ;; directory if we want to invoke rcp. | |
2806 | ((and (not v1-multi-method) | |
2807 | (not v2-multi-method) | |
2808 | (equal v1-method v2-method) | |
2809 | (tramp-method-out-of-band-p | |
2810 | v1-multi-method v1-method v1-user v1-host) | |
2811 | (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)) | |
2812 | (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host))) | |
2813 | (tramp-do-copy-or-rename-file-out-of-band | |
2814 | op filename newname keep-date)) | |
2815 | ;; No shortcut was possible. So we copy the | |
2816 | ;; file first. If the operation was `rename', we go | |
2817 | ;; back and delete the original file (if the copy was | |
2818 | ;; successful). The approach is simple-minded: we | |
2819 | ;; create a new buffer, insert the contents of the | |
2820 | ;; source file into it, then write out the buffer to | |
2821 | ;; the target file. The advantage is that it doesn't | |
2822 | ;; matter which filename handlers are used for the | |
2823 | ;; source and target file. | |
2824 | (t | |
38c65fca | 2825 | (tramp-do-copy-or-rename-file-via-buffer |
5ec2cc41 KG |
2826 | op filename newname keep-date)))) |
2827 | ||
2828 | ;; One file is a Tramp file, the other one is local. | |
7432277c | 2829 | ((or t1 t2) |
5ec2cc41 KG |
2830 | ;; If the Tramp file has an out-of-band method, the corresponding |
2831 | ;; copy-program can be invoked. | |
2832 | (if (and (not v1-multi-method) | |
2833 | (not v2-multi-method) | |
2834 | (or (tramp-method-out-of-band-p | |
2835 | v1-multi-method v1-method v1-user v1-host) | |
2836 | (tramp-method-out-of-band-p | |
2837 | v2-multi-method v2-method v2-user v2-host))) | |
2838 | (tramp-do-copy-or-rename-file-out-of-band | |
2839 | op filename newname keep-date) | |
2840 | ;; Use the generic method via a Tramp buffer. | |
38c65fca KG |
2841 | (tramp-do-copy-or-rename-file-via-buffer |
2842 | op filename newname keep-date))) | |
5ec2cc41 | 2843 | |
7432277c KG |
2844 | (t |
2845 | ;; One of them must be a Tramp file. | |
2846 | (error "Tramp implementation says this cannot happen"))))) | |
2847 | ||
38c65fca | 2848 | (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date) |
90dc758d KG |
2849 | "Use an Emacs buffer to copy or rename a file. |
2850 | First arg OP is either `copy' or `rename' and indicates the operation. | |
2851 | FILENAME is the source file, NEWNAME the target file. | |
2852 | KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME." | |
5ec2cc41 KG |
2853 | (let ((trampbuf (get-buffer-create "*tramp output*")) |
2854 | (modtime (nth 5 (file-attributes filename)))) | |
2855 | (when (and keep-date (or (null modtime) (equal modtime '(0 0)))) | |
90dc758d KG |
2856 | (tramp-message |
2857 | 1 (concat "Warning: cannot preserve file time stamp" | |
2858 | " with inline copying across machines"))) | |
2859 | (save-excursion | |
2860 | (set-buffer trampbuf) (erase-buffer) | |
2861 | (insert-file-contents-literally filename) | |
ea9d1443 KG |
2862 | ;; We don't want the target file to be compressed, so we let-bind |
2863 | ;; `jka-compr-inhibit' to t. | |
2864 | (let ((coding-system-for-write 'binary) | |
2865 | (jka-compr-inhibit t)) | |
5ec2cc41 KG |
2866 | (write-region (point-min) (point-max) newname)) |
2867 | ;; KEEP-DATE handling. | |
38c65fca KG |
2868 | (when keep-date |
2869 | (when (and (not (null modtime)) | |
2870 | (not (equal modtime '(0 0)))) | |
2871 | (tramp-touch newname modtime)) | |
2872 | (set-file-modes newname (file-modes filename)))) | |
90dc758d KG |
2873 | ;; If the operation was `rename', delete the original file. |
2874 | (unless (eq op 'copy) | |
2875 | (delete-file filename)))) | |
fb7933a3 KG |
2876 | |
2877 | (defun tramp-do-copy-or-rename-file-directly | |
7432277c | 2878 | (op multi-method method user host localname1 localname2 keep-date) |
fb7933a3 KG |
2879 | "Invokes `cp' or `mv' on the remote system. |
2880 | OP must be one of `copy' or `rename', indicating `cp' or `mv', | |
2881 | respectively. METHOD, USER, and HOST specify the connection. | |
7432277c | 2882 | LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'. |
fb7933a3 KG |
2883 | If KEEP-DATE is non-nil, preserve the time stamp when copying." |
2884 | ;; CCC: What happens to the timestamp when renaming? | |
2885 | (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p") | |
2886 | ((eq op 'copy) "cp -f") | |
2887 | ((eq op 'rename) "mv -f") | |
2888 | (t (error | |
2889 | "Unknown operation `%s', must be `copy' or `rename'" | |
2890 | op))))) | |
2891 | (save-excursion | |
2892 | (tramp-barf-unless-okay | |
2893 | multi-method method user host | |
2894 | (format "%s %s %s" | |
2895 | cmd | |
7432277c KG |
2896 | (tramp-shell-quote-argument localname1) |
2897 | (tramp-shell-quote-argument localname2)) | |
fb7933a3 KG |
2898 | nil 'file-error |
2899 | "Copying directly failed, see buffer `%s' for details." | |
2900 | (buffer-name))))) | |
2901 | ||
5ec2cc41 | 2902 | (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date) |
7432277c KG |
2903 | "Invoke rcp program to copy. |
2904 | One of FILENAME and NEWNAME must be a Tramp name, the other must | |
2905 | be a local filename. The method used must be an out-of-band method." | |
38c65fca | 2906 | (let ((t1 (tramp-tramp-file-p filename)) |
5ec2cc41 KG |
2907 | (t2 (tramp-tramp-file-p newname)) |
2908 | v1-multi-method v1-method v1-user v1-host v1-localname | |
2909 | v2-multi-method v2-method v2-user v2-host v2-localname | |
38c65fca KG |
2910 | multi-method method user host copy-program copy-args |
2911 | source target trampbuf) | |
5ec2cc41 KG |
2912 | |
2913 | ;; Check which ones of source and target are Tramp files. | |
2914 | ;; We cannot invoke `with-parsed-tramp-file-name'; | |
2915 | ;; it fails if the file isn't a Tramp file name. | |
2916 | (if t1 | |
2917 | (with-parsed-tramp-file-name filename l | |
2918 | (setq v1-multi-method l-multi-method | |
2919 | v1-method l-method | |
2920 | v1-user l-user | |
2921 | v1-host l-host | |
2922 | v1-localname l-localname | |
38c65fca | 2923 | multi-method l-multi-method |
5ec2cc41 KG |
2924 | method (tramp-find-method |
2925 | v1-multi-method v1-method v1-user v1-host) | |
38c65fca KG |
2926 | user l-user |
2927 | host l-host | |
5ec2cc41 KG |
2928 | copy-program (tramp-get-method-parameter |
2929 | v1-multi-method method | |
2930 | v1-user v1-host 'tramp-copy-program) | |
2931 | copy-args (tramp-get-method-parameter | |
2932 | v1-multi-method method | |
2933 | v1-user v1-host 'tramp-copy-args))) | |
2934 | (setq v1-localname filename)) | |
2935 | ||
2936 | (if t2 | |
2937 | (with-parsed-tramp-file-name newname l | |
2938 | (setq v2-multi-method l-multi-method | |
2939 | v2-method l-method | |
2940 | v2-user l-user | |
2941 | v2-host l-host | |
2942 | v2-localname l-localname | |
38c65fca | 2943 | multi-method l-multi-method |
5ec2cc41 KG |
2944 | method (tramp-find-method |
2945 | v2-multi-method v2-method v2-user v2-host) | |
38c65fca KG |
2946 | user l-user |
2947 | host l-host | |
5ec2cc41 KG |
2948 | copy-program (tramp-get-method-parameter |
2949 | v2-multi-method method | |
2950 | v2-user v2-host 'tramp-copy-program) | |
2951 | copy-args (tramp-get-method-parameter | |
2952 | v2-multi-method method | |
2953 | v2-user v2-host 'tramp-copy-args))) | |
2954 | (setq v2-localname newname)) | |
2955 | ||
2956 | ;; The following should be changed. We need a more general | |
2957 | ;; mechanism to parse extra host args. | |
2958 | (if (not t1) | |
2959 | (setq source v1-localname) | |
2960 | (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host) | |
2961 | (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args))) | |
2962 | (setq v1-host (match-string 1 v1-host))) | |
2963 | (setq source | |
2964 | (tramp-make-copy-program-file-name | |
2965 | v1-user v1-host | |
2966 | (tramp-shell-quote-argument v1-localname)))) | |
2967 | ||
2968 | (if (not t2) | |
2969 | (setq target v2-localname) | |
2970 | (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host) | |
2971 | (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args))) | |
2972 | (setq v2-host (match-string 1 v2-host))) | |
2973 | (setq target | |
2974 | (tramp-make-copy-program-file-name | |
2975 | v2-user v2-host | |
2976 | (tramp-shell-quote-argument v2-localname)))) | |
2977 | ||
2978 | ;; Handle keep-date argument | |
2979 | (when keep-date | |
2980 | (if t1 | |
2981 | (setq copy-args | |
2982 | (cons (tramp-get-method-parameter | |
2983 | v1-multi-method method | |
2984 | v1-user v1-host 'tramp-copy-keep-date-arg) | |
2985 | copy-args)) | |
2986 | (setq copy-args | |
2987 | (cons (tramp-get-method-parameter | |
2988 | v2-multi-method method | |
2989 | v2-user v2-host 'tramp-copy-keep-date-arg) | |
2990 | copy-args)))) | |
2991 | ||
38c65fca KG |
2992 | (setq copy-args (append copy-args (list source target)) |
2993 | trampbuf (generate-new-buffer | |
2994 | (tramp-buffer-name multi-method method user host))) | |
5ec2cc41 | 2995 | |
38c65fca KG |
2996 | ;; Use an asynchronous process. By this, password can be handled. |
2997 | (save-excursion | |
2998 | (set-buffer trampbuf) | |
2999 | (setq tramp-current-multi-method multi-method | |
3000 | tramp-current-method method | |
3001 | tramp-current-user user | |
3002 | tramp-current-host host) | |
3003 | (tramp-message | |
3004 | 5 "Transferring %s to file %s..." filename newname) | |
3005 | ||
3006 | ;; Use rcp-like program for file transfer. | |
3007 | (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf | |
3008 | copy-program copy-args))) | |
19a87064 | 3009 | (tramp-set-process-query-on-exit-flag p nil) |
38c65fca KG |
3010 | (tramp-process-actions p multi-method method user host |
3011 | tramp-actions-copy-out-of-band)) | |
3012 | (kill-buffer trampbuf) | |
3013 | (tramp-message | |
3014 | 5 "Transferring %s to file %s...done" filename newname)) | |
5ec2cc41 KG |
3015 | |
3016 | ;; If the operation was `rename', delete the original file. | |
3017 | (unless (eq op 'copy) | |
3018 | (delete-file filename)))) | |
7432277c | 3019 | |
fb7933a3 KG |
3020 | ;; mkdir |
3021 | (defun tramp-handle-make-directory (dir &optional parents) | |
3022 | "Like `make-directory' for tramp files." | |
ac474af1 | 3023 | (setq dir (expand-file-name dir)) |
c62c9d08 | 3024 | (with-parsed-tramp-file-name dir nil |
b1d06e75 KG |
3025 | (save-excursion |
3026 | (tramp-barf-unless-okay | |
3027 | multi-method method user host | |
3028 | (format " %s %s" | |
3029 | (if parents "mkdir -p" "mkdir") | |
7432277c | 3030 | (tramp-shell-quote-argument localname)) |
b1d06e75 KG |
3031 | nil 'file-error |
3032 | "Couldn't make directory %s" dir)))) | |
fb7933a3 KG |
3033 | |
3034 | ;; CCC error checking? | |
3035 | (defun tramp-handle-delete-directory (directory) | |
3036 | "Like `delete-directory' for tramp files." | |
ac474af1 | 3037 | (setq directory (expand-file-name directory)) |
c62c9d08 | 3038 | (with-parsed-tramp-file-name directory nil |
fb7933a3 KG |
3039 | (save-excursion |
3040 | (tramp-send-command | |
c62c9d08 | 3041 | multi-method method user host |
fb7933a3 | 3042 | (format "rmdir %s ; echo ok" |
7432277c | 3043 | (tramp-shell-quote-argument localname))) |
fb7933a3 KG |
3044 | (tramp-wait-for-output)))) |
3045 | ||
3046 | (defun tramp-handle-delete-file (filename) | |
3047 | "Like `delete-file' for tramp files." | |
ac474af1 | 3048 | (setq filename (expand-file-name filename)) |
c62c9d08 | 3049 | (with-parsed-tramp-file-name filename nil |
487fa986 KG |
3050 | (save-excursion |
3051 | (unless (zerop (tramp-send-command-and-check | |
3052 | multi-method method user host | |
3053 | (format "rm -f %s" | |
7432277c | 3054 | (tramp-shell-quote-argument localname)))) |
487fa986 | 3055 | (signal 'file-error "Couldn't delete Tramp file"))))) |
fb7933a3 KG |
3056 | |
3057 | ;; Dired. | |
3058 | ||
3059 | ;; CCC: This does not seem to be enough. Something dies when | |
3060 | ;; we try and delete two directories under TRAMP :/ | |
3061 | (defun tramp-handle-dired-recursive-delete-directory (filename) | |
3062 | "Recursively delete the directory given. | |
3063 | This is like `dired-recursive-delete-directory' for tramp files." | |
c62c9d08 | 3064 | (with-parsed-tramp-file-name filename nil |
7432277c | 3065 | ;; run a shell command 'rm -r <localname>' |
fb7933a3 KG |
3066 | ;; Code shamelessly stolen for the dired implementation and, um, hacked :) |
3067 | (or (tramp-handle-file-exists-p filename) | |
3068 | (signal | |
3069 | 'file-error | |
3070 | (list "Removing old file name" "no such directory" filename))) | |
3071 | ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>) | |
7432277c KG |
3072 | (tramp-send-command multi-method method user host |
3073 | (format "rm -r %s" (tramp-shell-quote-argument localname))) | |
fb7933a3 KG |
3074 | ;; Wait for the remote system to return to us... |
3075 | ;; This might take a while, allow it plenty of time. | |
3076 | (tramp-wait-for-output 120) | |
3077 | ;; Make sure that it worked... | |
3078 | (and (tramp-handle-file-exists-p filename) | |
3079 | (error "Failed to recusively delete %s" filename)))) | |
7432277c | 3080 | |
fb7933a3 KG |
3081 | (defun tramp-handle-dired-call-process (program discard &rest arguments) |
3082 | "Like `dired-call-process' for tramp files." | |
c62c9d08 | 3083 | (with-parsed-tramp-file-name default-directory nil |
fb7933a3 KG |
3084 | (save-excursion |
3085 | (tramp-barf-unless-okay | |
3086 | multi-method method user host | |
7432277c | 3087 | (format "cd %s" (tramp-shell-quote-argument localname)) |
fb7933a3 KG |
3088 | nil 'file-error |
3089 | "tramp-handle-dired-call-process: Couldn't `cd %s'" | |
7432277c | 3090 | (tramp-shell-quote-argument localname)) |
fb7933a3 KG |
3091 | (tramp-send-command |
3092 | multi-method method user host | |
3093 | (mapconcat #'tramp-shell-quote-argument (cons program arguments) " ")) | |
3094 | (tramp-wait-for-output)) | |
3095 | (unless discard | |
3096 | (insert-buffer (tramp-get-buffer multi-method method user host))) | |
3097 | (save-excursion | |
3098 | (prog1 | |
3099 | (tramp-send-command-and-check multi-method method user host nil) | |
3100 | (tramp-send-command multi-method method user host "cd") | |
3101 | (tramp-wait-for-output))))) | |
5ec2cc41 KG |
3102 | |
3103 | (defun tramp-handle-dired-compress-file (file &rest ok-flag) | |
3104 | "Like `dired-compress-file' for tramp files." | |
3105 | ;; OK-FLAG is valid for XEmacs only, but not implemented. | |
3106 | ;; Code stolen mainly from dired-aux.el. | |
3107 | (with-parsed-tramp-file-name file nil | |
3108 | (save-excursion | |
3109 | (let ((suffixes | |
3110 | (if (not (featurep 'xemacs)) | |
3111 | ;; Emacs case | |
3112 | (symbol-value 'dired-compress-file-suffixes) | |
3113 | ;; XEmacs has `dired-compression-method-alist', which is | |
3114 | ;; transformed into `dired-compress-file-suffixes' structure. | |
3115 | (mapcar | |
3116 | '(lambda (x) | |
3117 | (list (concat (regexp-quote (nth 1 x)) "\\'") | |
3118 | nil | |
3119 | (mapconcat 'identity (nth 3 x) " "))) | |
3120 | (symbol-value 'dired-compression-method-alist)))) | |
3121 | suffix) | |
3122 | ;; See if any suffix rule matches this file name. | |
3123 | (while suffixes | |
3124 | (let (case-fold-search) | |
3125 | (if (string-match (car (car suffixes)) localname) | |
3126 | (setq suffix (car suffixes) suffixes nil)) | |
3127 | (setq suffixes (cdr suffixes)))) | |
3128 | ||
3129 | (cond ((file-symlink-p file) | |
3130 | nil) | |
3131 | ((and suffix (nth 2 suffix)) | |
3132 | ;; We found an uncompression rule. | |
3133 | (message "Uncompressing %s..." file) | |
3134 | (when (zerop (tramp-send-command-and-check | |
3135 | multi-method method user host | |
3136 | (concat (nth 2 suffix) " " localname))) | |
3137 | (message "Uncompressing %s...done" file) | |
38c65fca KG |
3138 | ;; `dired-remove-file' is not defined in XEmacs |
3139 | (funcall (symbol-function 'dired-remove-file) file) | |
5ec2cc41 KG |
3140 | (string-match (car suffix) file) |
3141 | (concat (substring file 0 (match-beginning 0))))) | |
3142 | (t | |
3143 | ;; We don't recognize the file as compressed, so compress it. | |
3144 | ;; Try gzip. | |
3145 | (message "Compressing %s..." file) | |
3146 | (when (zerop (tramp-send-command-and-check | |
3147 | multi-method method user host | |
3148 | (concat "gzip -f " localname))) | |
3149 | (message "Compressing %s...done" file) | |
38c65fca KG |
3150 | ;; `dired-remove-file' is not defined in XEmacs |
3151 | (funcall (symbol-function 'dired-remove-file) file) | |
5ec2cc41 KG |
3152 | (cond ((file-exists-p (concat file ".gz")) |
3153 | (concat file ".gz")) | |
3154 | ((file-exists-p (concat file ".z")) | |
3155 | (concat file ".z")) | |
3156 | (t nil))))))))) | |
fb7933a3 KG |
3157 | |
3158 | ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm | |
3159 | ;; not sure at all that this is the right way to do it, but let's hope | |
3160 | ;; it works for now, and wait for a guru to point out the Right Way to | |
3161 | ;; achieve this. | |
3162 | ;;(eval-when-compile | |
3163 | ;; (unless (fboundp 'dired-insert-set-properties) | |
3164 | ;; (fset 'dired-insert-set-properties 'ignore))) | |
3165 | ;; Gerd suggests this: | |
3166 | (eval-when-compile (require 'dired)) | |
3167 | ;; Note that dired is required at run-time, too, when it is needed. | |
3168 | ;; It is only needed on XEmacs for the function | |
3169 | ;; `dired-insert-set-properties'. | |
3170 | ||
3171 | (defun tramp-handle-insert-directory | |
3172 | (filename switches &optional wildcard full-directory-p) | |
3173 | "Like `insert-directory' for tramp files." | |
16674e4f KG |
3174 | ;; For the moment, we assume that the remote "ls" program does not |
3175 | ;; grok "--dired". In the future, we should detect this on | |
3176 | ;; connection setup. | |
3177 | (when (string-match "^--dired\\s-+" switches) | |
3178 | (setq switches (replace-match "" nil t switches))) | |
ac474af1 | 3179 | (setq filename (expand-file-name filename)) |
c62c9d08 | 3180 | (with-parsed-tramp-file-name filename nil |
fb7933a3 KG |
3181 | (tramp-message-for-buffer |
3182 | multi-method method user host 10 | |
3183 | "Inserting directory `ls %s %s', wildcard %s, fulldir %s" | |
3184 | switches filename (if wildcard "yes" "no") | |
3185 | (if full-directory-p "yes" "no")) | |
3186 | (when wildcard | |
7432277c KG |
3187 | (setq wildcard (file-name-nondirectory localname)) |
3188 | (setq localname (file-name-directory localname))) | |
fb7933a3 KG |
3189 | (when (listp switches) |
3190 | (setq switches (mapconcat 'identity switches " "))) | |
3191 | (unless full-directory-p | |
3192 | (setq switches (concat "-d " switches))) | |
3193 | (when wildcard | |
3194 | (setq switches (concat switches " " wildcard))) | |
3195 | (save-excursion | |
3196 | ;; If `full-directory-p', we just say `ls -l FILENAME'. | |
3197 | ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'. | |
3198 | (if full-directory-p | |
c62c9d08 KG |
3199 | (tramp-send-command |
3200 | multi-method method user host | |
3201 | (format "%s %s %s" | |
3202 | (tramp-get-ls-command multi-method method user host) | |
3203 | switches | |
3204 | (if wildcard | |
7432277c KG |
3205 | localname |
3206 | (tramp-shell-quote-argument (concat localname "."))))) | |
c62c9d08 KG |
3207 | (tramp-barf-unless-okay |
3208 | multi-method method user host | |
3209 | (format "cd %s" (tramp-shell-quote-argument | |
7432277c | 3210 | (file-name-directory localname))) |
c62c9d08 KG |
3211 | nil 'file-error |
3212 | "Couldn't `cd %s'" | |
7432277c | 3213 | (tramp-shell-quote-argument (file-name-directory localname))) |
c62c9d08 KG |
3214 | (tramp-send-command |
3215 | multi-method method user host | |
3216 | (format "%s %s %s" | |
3217 | (tramp-get-ls-command multi-method method user host) | |
3218 | switches | |
38c65fca KG |
3219 | (if wildcard |
3220 | localname | |
3221 | (tramp-shell-quote-argument | |
3222 | (file-name-nondirectory localname)))))) | |
c62c9d08 | 3223 | (sit-for 1) ;needed for rsh but not ssh? |
fb7933a3 | 3224 | (tramp-wait-for-output)) |
b1a2b924 KG |
3225 | ;; The following let-binding is used by code that's commented |
3226 | ;; out. Let's leave the let-binding in for a while to see | |
3227 | ;; that the commented-out code is really not needed. Commenting-out | |
3228 | ;; happened on 2003-03-13. | |
16674e4f KG |
3229 | (let ((old-pos (point))) |
3230 | (insert-buffer-substring | |
3231 | (tramp-get-buffer multi-method method user host)) | |
3232 | ;; On XEmacs, we want to call (exchange-point-and-mark t), but | |
3233 | ;; that doesn't exist on Emacs, so we use this workaround instead. | |
3234 | ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to | |
3235 | ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>. | |
3236 | ;; (let ((zmacs-region-stays t)) | |
3237 | ;; (exchange-point-and-mark)) | |
fb7933a3 | 3238 | (save-excursion |
16674e4f KG |
3239 | (tramp-send-command multi-method method user host "cd") |
3240 | (tramp-wait-for-output)) | |
b1a2b924 KG |
3241 | ;; For the time being, the XEmacs kludge is commented out. |
3242 | ;; Please test it on various XEmacs versions to see if it works. | |
3243 | ;; ;; Another XEmacs specialty follows. What's the right way to do | |
3244 | ;; ;; it? | |
3245 | ;; (when (and (featurep 'xemacs) | |
3246 | ;; (eq major-mode 'dired-mode)) | |
3247 | ;; (save-excursion | |
3248 | ;; (require 'dired) | |
3249 | ;; (dired-insert-set-properties old-pos (point)))) | |
3250 | ))) | |
fb7933a3 KG |
3251 | |
3252 | ;; Continuation of kluge to pacify byte-compiler. | |
3253 | ;;(eval-when-compile | |
3254 | ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore) | |
3255 | ;; (fmakunbound 'dired-insert-set-properties))) | |
3256 | ||
3257 | ;; CCC is this the right thing to do? | |
3258 | (defun tramp-handle-unhandled-file-name-directory (filename) | |
3259 | "Like `unhandled-file-name-directory' for tramp files." | |
c62c9d08 | 3260 | (with-parsed-tramp-file-name filename nil |
c62c9d08 | 3261 | (expand-file-name "~/"))) |
fb7933a3 KG |
3262 | |
3263 | ;; Canonicalization of file names. | |
3264 | ||
3265 | (defun tramp-drop-volume-letter (name) | |
3266 | "Cut off unnecessary drive letter from file NAME. | |
3267 | The function `tramp-handle-expand-file-name' calls `expand-file-name' | |
3268 | locally on a remote file name. When the local system is a W32 system | |
3269 | but the remote system is Unix, this introduces a superfluous drive | |
3270 | letter into the file name. This function removes it. | |
3271 | ||
3272 | Doesn't do anything if the NAME does not start with a drive letter." | |
3273 | (if (and (> (length name) 1) | |
3274 | (char-equal (aref name 1) ?:) | |
3275 | (let ((c1 (aref name 0))) | |
3276 | (or (and (>= c1 ?A) (<= c1 ?Z)) | |
3277 | (and (>= c1 ?a) (<= c1 ?z))))) | |
3278 | (substring name 2) | |
3279 | name)) | |
3280 | ||
3281 | (defun tramp-handle-expand-file-name (name &optional dir) | |
7432277c KG |
3282 | "Like `expand-file-name' for tramp files. |
3283 | If the localname part of the given filename starts with \"/../\" then | |
3284 | the result will be a local, non-Tramp, filename." | |
fb7933a3 KG |
3285 | ;; If DIR is not given, use DEFAULT-DIRECTORY or "/". |
3286 | (setq dir (or dir default-directory "/")) | |
3287 | ;; Unless NAME is absolute, concat DIR and NAME. | |
3288 | (unless (file-name-absolute-p name) | |
3289 | (setq name (concat (file-name-as-directory dir) name))) | |
3290 | ;; If NAME is not a tramp file, run the real handler | |
3291 | (if (not (tramp-tramp-file-p name)) | |
3292 | (tramp-run-real-handler 'expand-file-name | |
3293 | (list name nil)) | |
3294 | ;; Dissect NAME. | |
c62c9d08 | 3295 | (with-parsed-tramp-file-name name nil |
7432277c KG |
3296 | (unless (file-name-absolute-p localname) |
3297 | (setq localname (concat "~/" localname))) | |
fb7933a3 KG |
3298 | (save-excursion |
3299 | ;; Tilde expansion if necessary. This needs a shell which | |
3300 | ;; groks tilde expansion! The function `tramp-find-shell' is | |
3301 | ;; supposed to find such a shell on the remote host. Please | |
3302 | ;; tell me about it when this doesn't work on your system. | |
7432277c KG |
3303 | (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname) |
3304 | (let ((uname (match-string 1 localname)) | |
3305 | (fname (match-string 2 localname))) | |
fb7933a3 KG |
3306 | ;; CCC fanatic error checking? |
3307 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
3308 | (erase-buffer) | |
3309 | (tramp-send-command | |
3310 | multi-method method user host | |
3311 | (format "cd %s; pwd" uname) | |
3312 | t) | |
3313 | (tramp-wait-for-output) | |
3314 | (goto-char (point-min)) | |
3315 | (setq uname (buffer-substring (point) (tramp-line-end-position))) | |
7432277c | 3316 | (setq localname (concat uname fname)) |
fb7933a3 | 3317 | (erase-buffer))) |
b1a2b924 KG |
3318 | ;; No tilde characters in file name, do normal |
3319 | ;; expand-file-name (this does "/./" and "/../"). We bind | |
3320 | ;; directory-sep-char here for XEmacs on Windows, which | |
3321 | ;; would otherwise use backslash. | |
19a87064 | 3322 | (tramp-let-maybe directory-sep-char ?/ |
b1a2b924 | 3323 | (tramp-make-tramp-file-name |
b25a52cc KG |
3324 | multi-method (or method (tramp-find-default-method user host)) |
3325 | user host | |
b1a2b924 KG |
3326 | (tramp-drop-volume-letter |
3327 | (tramp-run-real-handler 'expand-file-name | |
3328 | (list localname))))))))) | |
3329 | ||
3330 | ;; old version follows. it uses ".." to cross file handler | |
3331 | ;; boundaries. | |
3332 | ;; ;; Look if localname starts with "/../" construct. If this is | |
3333 | ;; ;; the case, then we return a local name instead of a remote name. | |
3334 | ;; (if (string-match "^/\\.\\./" localname) | |
3335 | ;; (expand-file-name (substring localname 3)) | |
3336 | ;; ;; No tilde characters in file name, do normal | |
3337 | ;; ;; expand-file-name (this does "/./" and "/../"). We bind | |
3338 | ;; ;; directory-sep-char here for XEmacs on Windows, which | |
3339 | ;; ;; would otherwise use backslash. | |
3340 | ;; (let ((directory-sep-char ?/)) | |
3341 | ;; (tramp-make-tramp-file-name | |
3342 | ;; multi-method method user host | |
3343 | ;; (tramp-drop-volume-letter | |
3344 | ;; (tramp-run-real-handler 'expand-file-name | |
3345 | ;; (list localname)))))))))) | |
fb7933a3 KG |
3346 | |
3347 | ;; Remote commands. | |
3348 | ||
5ec2cc41 KG |
3349 | (defvar tramp-async-proc nil |
3350 | "Global variable keeping asyncronous process object. | |
3351 | Used in `tramp-handle-shell-command'") | |
3352 | ||
fb7933a3 KG |
3353 | (defun tramp-handle-shell-command (command &optional output-buffer error-buffer) |
3354 | "Like `shell-command' for tramp files. | |
3355 | This will break if COMMAND prints a newline, followed by the value of | |
3356 | `tramp-end-of-output', followed by another newline." | |
5ec2cc41 KG |
3357 | ;; Asynchronous processes are far from being perfect. But it works at least |
3358 | ;; for `find-grep-dired' and `find-name-dired' in Emacs 21.4. | |
487f4fb7 KG |
3359 | (if (tramp-tramp-file-p default-directory) |
3360 | (with-parsed-tramp-file-name default-directory nil | |
5ec2cc41 KG |
3361 | (let ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command)) |
3362 | status) | |
3363 | (unless output-buffer | |
3364 | (setq output-buffer | |
3365 | (get-buffer-create | |
3366 | (if asynchronous | |
3367 | "*Async Shell Command*" | |
3368 | "*Shell Command Output*"))) | |
3369 | (set-buffer output-buffer) | |
3370 | (erase-buffer)) | |
3371 | (unless (bufferp output-buffer) | |
3372 | (setq output-buffer (current-buffer))) | |
3373 | (set-buffer output-buffer) | |
3374 | ;; Tramp doesn't handle the asynchronous case by an asynchronous | |
3375 | ;; process. Instead of, another asynchronous process is opened | |
3376 | ;; which gets the output of the (synchronous) Tramp process | |
3377 | ;; via process-filter. ERROR-BUFFER is disabled. | |
3378 | (when asynchronous | |
3379 | (setq command (substring command 0 (match-beginning 0)) | |
3380 | error-buffer nil | |
3381 | tramp-async-proc (start-process (buffer-name output-buffer) | |
3382 | output-buffer "cat"))) | |
487f4fb7 KG |
3383 | (save-excursion |
3384 | (tramp-barf-unless-okay | |
3385 | multi-method method user host | |
7432277c | 3386 | (format "cd %s" (tramp-shell-quote-argument localname)) |
487f4fb7 KG |
3387 | nil 'file-error |
3388 | "tramp-handle-shell-command: Couldn't `cd %s'" | |
7432277c | 3389 | (tramp-shell-quote-argument localname)) |
5ec2cc41 KG |
3390 | ;; Define the process filter |
3391 | (when asynchronous | |
3392 | (set-process-filter | |
3393 | (get-buffer-process | |
3394 | (tramp-get-buffer multi-method method user host)) | |
3395 | '(lambda (process string) | |
3396 | ;; Write the output into the Tramp Process | |
3397 | (save-current-buffer | |
3398 | (set-buffer (process-buffer process)) | |
3399 | (goto-char (point-max)) | |
3400 | (insert string)) | |
3401 | ;; Hand-over output to asynchronous process. | |
3402 | (let ((end | |
3403 | (string-match | |
3404 | (regexp-quote tramp-end-of-output) string))) | |
3405 | (when end | |
3406 | (setq string | |
3407 | (substring string 0 (1- (match-beginning 0))))) | |
3408 | (process-send-string tramp-async-proc string) | |
3409 | (when end | |
3410 | (set-process-filter process nil) | |
3411 | (process-send-eof tramp-async-proc)))))) | |
3412 | ;; Send the command | |
90f8dc03 KG |
3413 | (tramp-send-command |
3414 | multi-method method user host | |
3415 | (if error-buffer | |
3416 | (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?" | |
3417 | command) | |
5ec2cc41 KG |
3418 | (format "%s; tramp_old_status=$?" command))) |
3419 | (unless asynchronous | |
3420 | (tramp-wait-for-output))) | |
3421 | (unless asynchronous | |
3422 | (insert-buffer (tramp-get-buffer multi-method method user host))) | |
90f8dc03 KG |
3423 | (when error-buffer |
3424 | (save-excursion | |
3425 | (unless (bufferp error-buffer) | |
3426 | (setq error-buffer (get-buffer-create error-buffer))) | |
3427 | (tramp-send-command | |
3428 | multi-method method user host | |
3429 | "cat /tmp/tramp.$$.err") | |
3430 | (tramp-wait-for-output) | |
3431 | (set-buffer error-buffer) | |
3432 | (insert-buffer (tramp-get-buffer multi-method method user host)) | |
3433 | (tramp-send-command-and-check | |
3434 | multi-method method user host "rm -f /tmp/tramp.$$.err"))) | |
487f4fb7 KG |
3435 | (save-excursion |
3436 | (tramp-send-command multi-method method user host "cd") | |
5ec2cc41 KG |
3437 | (unless asynchronous |
3438 | (tramp-wait-for-output)) | |
487f4fb7 KG |
3439 | (tramp-send-command |
3440 | multi-method method user host | |
3441 | (concat "tramp_set_exit_status $tramp_old_status;" | |
3442 | " echo tramp_exit_status $?")) | |
5ec2cc41 KG |
3443 | (unless asynchronous |
3444 | (tramp-wait-for-output) | |
3445 | (goto-char (point-max)) | |
3446 | (unless (search-backward "tramp_exit_status " nil t) | |
3447 | (error "Couldn't find exit status of `%s'" command)) | |
3448 | (skip-chars-forward "^ ") | |
3449 | (setq status (read (current-buffer))))) | |
487f4fb7 | 3450 | (unless (zerop (buffer-size)) |
4007ba5b | 3451 | (display-buffer output-buffer)) |
487f4fb7 KG |
3452 | status)) |
3453 | ;; The following is only executed if something strange was | |
3454 | ;; happening. Emit a helpful message and do it anyway. | |
3455 | (message "tramp-handle-shell-command called with non-tramp directory: `%s'" | |
3456 | default-directory) | |
3457 | (tramp-run-real-handler 'shell-command | |
3458 | (list command output-buffer error-buffer)))) | |
fb7933a3 KG |
3459 | |
3460 | ;; File Editing. | |
3461 | ||
3462 | (defsubst tramp-make-temp-file () | |
3463 | (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name) | |
3464 | (expand-file-name tramp-temp-name-prefix | |
3465 | (tramp-temporary-file-directory)))) | |
3466 | ||
3467 | (defun tramp-handle-file-local-copy (filename) | |
3468 | "Like `file-local-copy' for tramp files." | |
c62c9d08 | 3469 | (with-parsed-tramp-file-name filename nil |
5ec2cc41 | 3470 | (let ((tramp-buf (tramp-get-buffer multi-method method user host)) |
3b89d388 KG |
3471 | ;; We used to bind the following as late as possible. |
3472 | ;; loc-enc and loc-dec were bound directly before the if | |
3473 | ;; statement that checks them. But the functions | |
3474 | ;; tramp-get-* might invoke the "are you awake" check in | |
3475 | ;; tramp-maybe-open-connection, which is an unfortunate time | |
3476 | ;; since we rely on the buffer contents at that spot. | |
3477 | (rem-enc (tramp-get-remote-encoding multi-method method user host)) | |
3478 | (rem-dec (tramp-get-remote-decoding multi-method method user host)) | |
3479 | (loc-enc (tramp-get-local-encoding multi-method method user host)) | |
3480 | (loc-dec (tramp-get-local-decoding multi-method method user host)) | |
c62c9d08 KG |
3481 | tmpfil) |
3482 | (unless (file-exists-p filename) | |
3483 | (error "Cannot make local copy of non-existing file `%s'" | |
3484 | filename)) | |
3485 | (setq tmpfil (tramp-make-temp-file)) | |
5ec2cc41 | 3486 | |
5ec2cc41 KG |
3487 | (cond ((tramp-method-out-of-band-p multi-method method user host) |
3488 | ;; `copy-file' handles out-of-band methods | |
3489 | (copy-file filename tmpfil t t)) | |
3490 | ||
3b89d388 | 3491 | ((and rem-enc rem-dec) |
c62c9d08 KG |
3492 | ;; Use inline encoding for file transfer. |
3493 | (save-excursion | |
3494 | ;; Following line for setting tramp-current-method, | |
3495 | ;; tramp-current-user, tramp-current-host. | |
3b89d388 | 3496 | (set-buffer tramp-buf) |
c62c9d08 KG |
3497 | (tramp-message 5 "Encoding remote file %s..." filename) |
3498 | (tramp-barf-unless-okay | |
3499 | multi-method method user host | |
7432277c | 3500 | (concat rem-enc " < " (tramp-shell-quote-argument localname)) |
c62c9d08 KG |
3501 | nil 'file-error |
3502 | "Encoding remote file failed, see buffer `%s' for details" | |
3b89d388 | 3503 | tramp-buf) |
c62c9d08 KG |
3504 | ;; Remove trailing status code |
3505 | (goto-char (point-max)) | |
3506 | (delete-region (point) (progn (forward-line -1) (point))) | |
3507 | ||
3508 | (tramp-message 5 "Decoding remote file %s..." filename) | |
16674e4f | 3509 | |
3b89d388 | 3510 | ;; Here is where loc-enc and loc-dec used to be let-bound. |
16674e4f KG |
3511 | (if (and (symbolp loc-dec) (fboundp loc-dec)) |
3512 | ;; If local decoding is a function, we call it. | |
c62c9d08 KG |
3513 | (let ((tmpbuf (get-buffer-create " *tramp tmp*"))) |
3514 | (set-buffer tmpbuf) | |
3515 | (erase-buffer) | |
3b89d388 | 3516 | (insert-buffer tramp-buf) |
c62c9d08 KG |
3517 | (tramp-message-for-buffer |
3518 | multi-method method user host | |
3519 | 6 "Decoding remote file %s with function %s..." | |
16674e4f | 3520 | filename loc-dec) |
c62c9d08 | 3521 | (set-buffer tmpbuf) |
16674e4f KG |
3522 | ;; Douglas Gray Stephens <DGrayStephens@slb.com> |
3523 | ;; says that we need to strip tramp_exit_status | |
3524 | ;; line from the output here. Go to point-max, | |
3525 | ;; search backward for tramp_exit_status, delete | |
3526 | ;; between point and point-max if found. | |
ea9d1443 | 3527 | (let ((coding-system-for-write 'binary)) |
16674e4f | 3528 | (funcall loc-dec (point-min) (point-max)) |
c62c9d08 KG |
3529 | (write-region (point-min) (point-max) tmpfil)) |
3530 | (kill-buffer tmpbuf)) | |
3531 | ;; If tramp-decoding-function is not defined for this | |
3532 | ;; method, we invoke tramp-decoding-command instead. | |
3533 | (let ((tmpfil2 (tramp-make-temp-file))) | |
3534 | (write-region (point-min) (point-max) tmpfil2) | |
3535 | (tramp-message | |
3536 | 6 "Decoding remote file %s with command %s..." | |
16674e4f KG |
3537 | filename loc-dec) |
3538 | (tramp-call-local-coding-command | |
3539 | loc-dec tmpfil2 tmpfil) | |
c62c9d08 KG |
3540 | (delete-file tmpfil2))) |
3541 | (tramp-message-for-buffer | |
3542 | multi-method method user host | |
38c65fca KG |
3543 | 5 "Decoding remote file %s...done" filename) |
3544 | ;; Set proper permissions. | |
3545 | (set-file-modes tmpfil (file-modes filename)))) | |
c62c9d08 KG |
3546 | |
3547 | (t (error "Wrong method specification for `%s'" method))) | |
3548 | tmpfil))) | |
fb7933a3 | 3549 | |
19a87064 MA |
3550 | (defun tramp-handle-file-remote-p (filename) |
3551 | "Like `file-remote-p' for tramp files." | |
15cc764c KG |
3552 | (when (tramp-tramp-file-p filename) |
3553 | (with-parsed-tramp-file-name filename nil | |
3554 | (make-tramp-file-name | |
3555 | :multi-method multi-method | |
3556 | :method method | |
3557 | :user user | |
3558 | :host host | |
3559 | :localname "")))) | |
fb7933a3 KG |
3560 | |
3561 | (defun tramp-handle-insert-file-contents | |
3562 | (filename &optional visit beg end replace) | |
3563 | "Like `insert-file-contents' for tramp files." | |
3564 | (barf-if-buffer-read-only) | |
3565 | (setq filename (expand-file-name filename)) | |
c62c9d08 | 3566 | (with-parsed-tramp-file-name filename nil |
4007ba5b | 3567 | (if (not (file-exists-p filename)) |
fb7933a3 KG |
3568 | (progn |
3569 | (when visit | |
3570 | (setq buffer-file-name filename) | |
3571 | (set-visited-file-modtime) | |
3572 | (set-buffer-modified-p nil)) | |
3573 | (signal 'file-error | |
3574 | (format "File `%s' not found on remote host" filename)) | |
4007ba5b | 3575 | (list (expand-file-name filename) 0)) |
ea9d1443 KG |
3576 | ;; `insert-file-contents-literally' takes care to avoid calling |
3577 | ;; jka-compr. By let-binding inhibit-file-name-operation, we | |
3578 | ;; propagate that care to the file-local-copy operation. | |
3579 | (let ((local-copy | |
90f8dc03 KG |
3580 | (let ((inhibit-file-name-operation |
3581 | (when (eq inhibit-file-name-operation | |
3582 | 'insert-file-contents) | |
3583 | 'file-local-copy))) | |
ea9d1443 | 3584 | (file-local-copy filename))) |
fb7933a3 KG |
3585 | (coding-system-used nil) |
3586 | (result nil)) | |
3587 | (when visit | |
3588 | (setq buffer-file-name filename) | |
3589 | (set-visited-file-modtime) | |
3590 | (set-buffer-modified-p nil)) | |
3591 | (tramp-message-for-buffer | |
3592 | multi-method method user host | |
3593 | 9 "Inserting local temp file `%s'..." local-copy) | |
4007ba5b | 3594 | (setq result (insert-file-contents local-copy nil beg end replace)) |
fb7933a3 KG |
3595 | ;; Now `last-coding-system-used' has right value. Remember it. |
3596 | (when (boundp 'last-coding-system-used) | |
3597 | (setq coding-system-used last-coding-system-used)) | |
16674e4f KG |
3598 | (tramp-message-for-buffer |
3599 | multi-method method user host | |
3600 | 9 "Inserting local temp file `%s'...done" local-copy) | |
fb7933a3 KG |
3601 | (delete-file local-copy) |
3602 | (when (boundp 'last-coding-system-used) | |
3603 | (setq last-coding-system-used coding-system-used)) | |
3604 | (list (expand-file-name filename) | |
3605 | (second result)))))) | |
3606 | ||
38c65fca KG |
3607 | |
3608 | (defun tramp-handle-find-backup-file-name (filename) | |
3609 | "Like `find-backup-file-name' for tramp files." | |
3610 | ||
3611 | (if (or (and (not (featurep 'xemacs)) | |
3612 | (not (boundp 'tramp-backup-directory-alist))) | |
3613 | (and (featurep 'xemacs) | |
3614 | (not (boundp 'tramp-bkup-backup-directory-info)))) | |
3615 | ||
3616 | ;; No tramp backup directory alist defined, or nil | |
3617 | (tramp-run-real-handler 'find-backup-file-name (list filename)) | |
3618 | ||
3619 | (with-parsed-tramp-file-name filename nil | |
3620 | (let* ((backup-var | |
3621 | (copy-tree | |
3622 | (if (featurep 'xemacs) | |
3623 | ;; XEmacs case | |
3624 | (symbol-value 'tramp-bkup-backup-directory-info) | |
3625 | ;; Emacs case | |
3626 | (symbol-value 'tramp-backup-directory-alist)))) | |
3627 | ||
3628 | ;; We set both variables. It doesn't matter whether it is | |
3629 | ;; Emacs or XEmacs | |
3630 | (backup-directory-alist backup-var) | |
3631 | (bkup-backup-directory-info backup-var)) | |
3632 | ||
3633 | (mapcar | |
3634 | '(lambda (x) | |
3635 | (let ((dir (if (consp (cdr x)) (car (cdr x)) (cdr x)))) | |
3636 | (when (and (stringp dir) | |
3637 | (file-name-absolute-p dir) | |
3638 | (not (tramp-file-name-p dir))) | |
3639 | ;; Prepend absolute directory names with tramp prefix | |
3640 | (if (consp (cdr x)) | |
3641 | (setcar (cdr x) | |
3642 | (tramp-make-tramp-file-name | |
3643 | multi-method method user host dir)) | |
3644 | (setcdr x (tramp-make-tramp-file-name | |
3645 | multi-method method user host dir)))))) | |
3646 | backup-var) | |
3647 | ||
3648 | (tramp-run-real-handler 'find-backup-file-name (list filename)))))) | |
3649 | ||
fb7933a3 KG |
3650 | ;; CCC grok APPEND, LOCKNAME, CONFIRM |
3651 | (defun tramp-handle-write-region | |
3652 | (start end filename &optional append visit lockname confirm) | |
3653 | "Like `write-region' for tramp files." | |
3654 | (unless (eq append nil) | |
3655 | (error "Cannot append to file using tramp (`%s')" filename)) | |
3656 | (setq filename (expand-file-name filename)) | |
c62c9d08 KG |
3657 | ;; Following part commented out because we don't know what to do about |
3658 | ;; file locking, and it does not appear to be a problem to ignore it. | |
3659 | ;; Ange-ftp ignores it, too. | |
3660 | ;; (when (and lockname (stringp lockname)) | |
3661 | ;; (setq lockname (expand-file-name lockname))) | |
3662 | ;; (unless (or (eq lockname nil) | |
3663 | ;; (string= lockname filename)) | |
3664 | ;; (error | |
3665 | ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME")) | |
fb7933a3 KG |
3666 | ;; XEmacs takes a coding system as the sevent argument, not `confirm' |
3667 | (when (and (not (featurep 'xemacs)) | |
c62c9d08 | 3668 | confirm (file-exists-p filename)) |
fb7933a3 KG |
3669 | (unless (y-or-n-p (format "File %s exists; overwrite anyway? " |
3670 | filename)) | |
3671 | (error "File not overwritten"))) | |
c62c9d08 | 3672 | (with-parsed-tramp-file-name filename nil |
c62c9d08 | 3673 | (let ((curbuf (current-buffer)) |
16674e4f KG |
3674 | (rem-enc (tramp-get-remote-encoding multi-method method user host)) |
3675 | (rem-dec (tramp-get-remote-decoding multi-method method user host)) | |
3676 | (loc-enc (tramp-get-local-encoding multi-method method user host)) | |
3677 | (loc-dec (tramp-get-local-decoding multi-method method user host)) | |
c62c9d08 | 3678 | (trampbuf (get-buffer-create "*tramp output*")) |
38c65fca | 3679 | (modes (file-modes filename)) |
c62c9d08 KG |
3680 | ;; We use this to save the value of `last-coding-system-used' |
3681 | ;; after writing the tmp file. At the end of the function, | |
3682 | ;; we set `last-coding-system-used' to this saved value. | |
3683 | ;; This way, any intermediary coding systems used while | |
3684 | ;; talking to the remote shell or suchlike won't hose this | |
3685 | ;; variable. This approach was snarfed from ange-ftp.el. | |
3686 | coding-system-used | |
3687 | tmpfil) | |
3688 | ;; Write region into a tmp file. This isn't really needed if we | |
3689 | ;; use an encoding function, but currently we use it always | |
3690 | ;; because this makes the logic simpler. | |
3691 | (setq tmpfil (tramp-make-temp-file)) | |
3692 | ;; We say `no-message' here because we don't want the visited file | |
3693 | ;; modtime data to be clobbered from the temp file. We call | |
3694 | ;; `set-visited-file-modtime' ourselves later on. | |
3695 | (tramp-run-real-handler | |
3696 | 'write-region | |
3697 | (if confirm ; don't pass this arg unless defined for backward compat. | |
3698 | (list start end tmpfil append 'no-message lockname confirm) | |
3699 | (list start end tmpfil append 'no-message lockname))) | |
38c65fca KG |
3700 | ;; The permissions of the temporary file should be set. If |
3701 | ;; filename does not exist (eq modes nil) it has been renamed to | |
3702 | ;; the backup file. This case `save-buffer' handles | |
3703 | ;; permissions. | |
3704 | (when modes (set-file-modes tmpfil modes)) | |
c62c9d08 KG |
3705 | ;; Now, `last-coding-system-used' has the right value. Remember it. |
3706 | (when (boundp 'last-coding-system-used) | |
3707 | (setq coding-system-used last-coding-system-used)) | |
3708 | ;; This is a bit lengthy due to the different methods possible for | |
3709 | ;; file transfer. First, we check whether the method uses an rcp | |
3710 | ;; program. If so, we call it. Otherwise, both encoding and | |
3711 | ;; decoding command must be specified. However, if the method | |
3712 | ;; _also_ specifies an encoding function, then that is used for | |
3713 | ;; encoding the contents of the tmp file. | |
5ec2cc41 KG |
3714 | (cond ((tramp-method-out-of-band-p multi-method method user host) |
3715 | ;; `copy-file' handles out-of-band methods | |
3716 | (copy-file tmpfil filename t t)) | |
3717 | ||
16674e4f | 3718 | ((and rem-enc rem-dec) |
c62c9d08 KG |
3719 | ;; Use inline file transfer |
3720 | (let ((tmpbuf (get-buffer-create " *tramp file transfer*"))) | |
3721 | (save-excursion | |
3722 | ;; Encode tmpfil into tmpbuf | |
3723 | (tramp-message-for-buffer multi-method method user host | |
3724 | 5 "Encoding region...") | |
3725 | (set-buffer tmpbuf) | |
3726 | (erase-buffer) | |
3727 | ;; Use encoding function or command. | |
16674e4f | 3728 | (if (and (symbolp loc-enc) (fboundp loc-enc)) |
c62c9d08 KG |
3729 | (progn |
3730 | (tramp-message-for-buffer | |
3731 | multi-method method user host | |
5ec2cc41 KG |
3732 | 6 "Encoding region using function `%s'..." |
3733 | (symbol-name loc-enc)) | |
c62c9d08 KG |
3734 | (insert-file-contents-literally tmpfil) |
3735 | ;; CCC. The following `let' is a workaround for | |
3736 | ;; the base64.el that comes with pgnus-0.84. If | |
3737 | ;; both of the following conditions are | |
3738 | ;; satisfied, it tries to write to a local file | |
3739 | ;; in default-directory, but at this point, | |
3740 | ;; default-directory is remote. | |
3741 | ;; (CALL-PROCESS-REGION can't write to remote | |
3742 | ;; files, it seems.) The file in question is a | |
3743 | ;; tmp file anyway. | |
3744 | (let ((default-directory | |
3745 | (tramp-temporary-file-directory))) | |
16674e4f | 3746 | (funcall loc-enc (point-min) (point-max))) |
c62c9d08 KG |
3747 | (goto-char (point-max)) |
3748 | (unless (bolp) | |
3749 | (newline))) | |
3750 | (tramp-message-for-buffer | |
3751 | multi-method method user host | |
16674e4f KG |
3752 | 6 "Encoding region using command `%s'..." loc-enc) |
3753 | (unless (equal 0 (tramp-call-local-coding-command | |
3754 | loc-enc tmpfil t)) | |
c62c9d08 KG |
3755 | (pop-to-buffer trampbuf) |
3756 | (error (concat "Cannot write to `%s', local encoding" | |
3757 | " command `%s' failed") | |
16674e4f | 3758 | filename loc-enc))) |
c62c9d08 KG |
3759 | ;; Send tmpbuf into remote decoding command which |
3760 | ;; writes to remote file. Because this happens on the | |
3761 | ;; remote host, we cannot use the function. | |
3762 | (tramp-message-for-buffer | |
3763 | multi-method method user host | |
3764 | 5 "Decoding region into remote file %s..." filename) | |
3765 | (tramp-send-command | |
3766 | multi-method method user host | |
3767 | (format "%s >%s <<'EOF'" | |
16674e4f | 3768 | rem-dec |
7432277c | 3769 | (tramp-shell-quote-argument localname))) |
c62c9d08 KG |
3770 | (set-buffer tmpbuf) |
3771 | (tramp-message-for-buffer | |
3772 | multi-method method user host | |
3773 | 6 "Sending data to remote host...") | |
7432277c KG |
3774 | (tramp-send-string multi-method method user host |
3775 | (buffer-string)) | |
c62c9d08 KG |
3776 | ;; wait for remote decoding to complete |
3777 | (tramp-message-for-buffer | |
3778 | multi-method method user host | |
3779 | 6 "Sending end of data token...") | |
3780 | (tramp-send-command | |
3cdaec13 | 3781 | multi-method method user host "EOF" nil t) |
c62c9d08 KG |
3782 | (tramp-message-for-buffer |
3783 | multi-method method user host 6 | |
3784 | "Waiting for remote host to process data...") | |
3785 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
3786 | (tramp-wait-for-output) | |
3787 | (tramp-barf-unless-okay | |
3788 | multi-method method user host nil nil 'file-error | |
3789 | (concat "Couldn't write region to `%s'," | |
3790 | " decode using `%s' failed") | |
16674e4f | 3791 | filename rem-dec) |
c62c9d08 KG |
3792 | (tramp-message 5 "Decoding region into remote file %s...done" |
3793 | filename) | |
3794 | (kill-buffer tmpbuf)))) | |
3795 | (t | |
3796 | (error | |
3797 | (concat "Method `%s' should specify both encoding and " | |
3798 | "decoding command or an rcp program") | |
3799 | method))) | |
3800 | (delete-file tmpfil) | |
3801 | (unless (equal curbuf (current-buffer)) | |
3802 | (error "Buffer has changed from `%s' to `%s'" | |
3803 | curbuf (current-buffer))) | |
3804 | (when (eq visit t) | |
3805 | (set-visited-file-modtime)) | |
3806 | ;; Make `last-coding-system-used' have the right value. | |
3807 | (when (boundp 'last-coding-system-used) | |
3808 | (setq last-coding-system-used coding-system-used)) | |
3809 | (when (or (eq visit t) | |
3810 | (eq visit nil) | |
3811 | (stringp visit)) | |
3812 | (message "Wrote %s" filename))))) | |
fb7933a3 KG |
3813 | |
3814 | ;; Call down to the real handler. | |
7432277c KG |
3815 | ;; Because EFS does not play nicely with TRAMP (both systems match a |
3816 | ;; TRAMP file name) it is needed to disable efs as well as tramp for the | |
fb7933a3 KG |
3817 | ;; operation. |
3818 | ;; | |
3819 | ;; Other than that, this is the canon file-handler code that the doco | |
3820 | ;; says should be used here. Which is nice. | |
3821 | ;; | |
3822 | ;; Under XEmacs current, EFS also hooks in as | |
7432277c | 3823 | ;; efs-sifn-handler-function to handle any filename with environment |
fb7933a3 | 3824 | ;; variables. This has two implications: |
7432277c | 3825 | ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames |
fb7933a3 KG |
3826 | ;; 2) That TRAMP might want to do the same thing. |
3827 | ;; Details as they come in. | |
3828 | ;; | |
3829 | ;; Daniel Pittman <daniel@danann.net> | |
3830 | ||
3831 | ;; (defun tramp-run-real-handler (operation args) | |
3832 | ;; "Invoke normal file name handler for OPERATION. | |
3833 | ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp. | |
3834 | ;; First arg specifies the OPERATION, remaining ARGS are passed to the | |
3835 | ;; OPERATION." | |
3836 | ;; (let ((inhibit-file-name-handlers | |
3837 | ;; (list 'tramp-file-name-handler | |
3838 | ;; 'efs-file-handler-function | |
3839 | ;; 'ange-ftp-hook-function | |
3840 | ;; (and (eq inhibit-file-name-operation operation) | |
3841 | ;; inhibit-file-name-handlers))) | |
3842 | ;; (inhibit-file-name-operation operation)) | |
3843 | ;; (apply operation args))) | |
3844 | ||
3845 | (defun tramp-run-real-handler (operation args) | |
3846 | "Invoke normal file name handler for OPERATION. | |
c62c9d08 KG |
3847 | First arg specifies the OPERATION, second arg is a list of arguments to |
3848 | pass to the OPERATION." | |
4007ba5b KG |
3849 | (let* ((inhibit-file-name-handlers |
3850 | `(tramp-file-name-handler | |
3851 | tramp-completion-file-name-handler | |
3852 | cygwin-mount-name-hook-function | |
3853 | cygwin-mount-map-drive-hook-function | |
3854 | . | |
3855 | ,(and (eq inhibit-file-name-operation operation) | |
3856 | inhibit-file-name-handlers))) | |
3857 | (inhibit-file-name-operation operation)) | |
16674e4f KG |
3858 | (apply operation args))) |
3859 | ||
3860 | ;; This function is used from `tramp-completion-file-name-handler' functions | |
3861 | ;; only, if `tramp-completion-mode' is true. But this cannot be checked here | |
3862 | ;; because the check is based on a full filename, not available for all | |
3863 | ;; basic I/O operations. | |
3864 | (defun tramp-completion-run-real-handler (operation args) | |
3865 | "Invoke `tramp-file-name-handler' for OPERATION. | |
3866 | First arg specifies the OPERATION, second arg is a list of arguments to | |
3867 | pass to the OPERATION." | |
4007ba5b KG |
3868 | (let* ((inhibit-file-name-handlers |
3869 | `(tramp-completion-file-name-handler | |
3870 | cygwin-mount-name-hook-function | |
3871 | cygwin-mount-map-drive-hook-function | |
3872 | . | |
3873 | ,(and (eq inhibit-file-name-operation operation) | |
3874 | inhibit-file-name-handlers))) | |
3875 | (inhibit-file-name-operation operation)) | |
fb7933a3 KG |
3876 | (apply operation args))) |
3877 | ||
4007ba5b KG |
3878 | ;; We handle here all file primitives. Most of them have the file |
3879 | ;; name as first parameter; nevertheless we check for them explicitly | |
19a87064 | 3880 | ;; in order to be signalled if a new primitive appears. This |
4007ba5b KG |
3881 | ;; scenario is needed because there isn't a way to decide by |
3882 | ;; syntactical means whether a foreign method must be called. It would | |
19a87064 | 3883 | ;; ease the life if `file-name-handler-alist' would support a decision |
4007ba5b KG |
3884 | ;; function as well but regexp only. |
3885 | (defun tramp-file-name-for-operation (operation &rest args) | |
3886 | "Return file name related to OPERATION file primitive. | |
3887 | ARGS are the arguments OPERATION has been called with." | |
3888 | (cond | |
3889 | ; FILE resp DIRECTORY | |
3890 | ((member operation | |
3891 | (list 'access-file 'byte-compiler-base-file-name 'delete-directory | |
3892 | 'delete-file 'diff-latest-backup-file 'directory-file-name | |
3893 | 'directory-files 'directory-files-and-attributes | |
3894 | 'dired-compress-file 'dired-uncache | |
3895 | 'file-accessible-directory-p 'file-attributes | |
3896 | 'file-directory-p 'file-executable-p 'file-exists-p | |
19a87064 MA |
3897 | 'file-local-copy 'file-remote-p 'file-modes |
3898 | 'file-name-as-directory 'file-name-directory | |
3899 | 'file-name-nondirectory 'file-name-sans-versions | |
3900 | 'file-ownership-preserved-p 'file-readable-p | |
3901 | 'file-regular-p 'file-symlink-p 'file-truename | |
3902 | 'file-writable-p 'find-backup-file-name 'find-file-noselect | |
3903 | 'get-file-buffer 'insert-directory 'insert-file-contents | |
3904 | 'load 'make-directory 'make-directory-internal | |
3905 | 'set-file-modes 'substitute-in-file-name | |
3906 | 'unhandled-file-name-directory 'vc-registered | |
4007ba5b KG |
3907 | ; XEmacs only |
3908 | 'abbreviate-file-name 'create-file-buffer | |
3909 | 'dired-file-modtime 'dired-make-compressed-filename | |
3910 | 'dired-recursive-delete-directory 'dired-set-file-modtime | |
3911 | 'dired-shell-unhandle-file-name 'dired-uucode-file | |
3912 | 'insert-file-contents-literally 'recover-file | |
3913 | 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail)) | |
8daea7fc KG |
3914 | (if (file-name-absolute-p (nth 0 args)) |
3915 | (nth 0 args) | |
3916 | (expand-file-name (nth 0 args)))) | |
4007ba5b KG |
3917 | ; FILE DIRECTORY resp FILE1 FILE2 |
3918 | ((member operation | |
3919 | (list 'add-name-to-file 'copy-file 'expand-file-name | |
3920 | 'file-name-all-completions 'file-name-completion | |
3921 | 'file-newer-than-file-p 'make-symbolic-link 'rename-file | |
3922 | ; XEmacs only | |
3923 | 'dired-make-relative-symlink | |
3924 | 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail)) | |
3925 | (save-match-data | |
3926 | (cond | |
3927 | ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args)) | |
3928 | ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args)) | |
3929 | (t (buffer-file-name (current-buffer)))))) | |
3930 | ; START END FILE | |
3931 | ((eq operation 'write-region) | |
3932 | (nth 2 args)) | |
3933 | ; BUF | |
3934 | ((member operation | |
3935 | (list 'set-visited-file-modtime 'verify-visited-file-modtime | |
3936 | ; XEmacs only | |
3937 | 'backup-buffer)) | |
3938 | (buffer-file-name | |
3939 | (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer)))) | |
3940 | ; COMMAND | |
3941 | ((member operation | |
3942 | (list 'dired-call-process 'shell-command | |
3943 | ; XEmacs only | |
3944 | 'dired-print-file 'dired-shell-call-process)) | |
3945 | default-directory) | |
3946 | ; unknown file primitive | |
3947 | (t (error "unknown file I/O primitive: %s" operation)))) | |
3948 | ||
3949 | (defun tramp-find-foreign-file-name-handler (filename) | |
3950 | "Return foreign file name handler if exists." | |
8daea7fc | 3951 | (when (tramp-tramp-file-p filename) |
ea9d1443 KG |
3952 | (let (elt |
3953 | res | |
3954 | (handler-alist tramp-foreign-file-name-handler-alist)) | |
3955 | (while handler-alist | |
3956 | (setq elt (car handler-alist) | |
3957 | handler-alist (cdr handler-alist)) | |
8daea7fc | 3958 | (when (funcall (car elt) filename) |
ea9d1443 | 3959 | (setq handler-alist nil) |
8daea7fc KG |
3960 | (setq res (cdr elt)))) |
3961 | res))) | |
4007ba5b | 3962 | |
fb7933a3 KG |
3963 | ;; Main function. |
3964 | ;;;###autoload | |
3965 | (defun tramp-file-name-handler (operation &rest args) | |
ea9d1443 | 3966 | "Invoke Tramp file name handler. |
fb7933a3 | 3967 | Falls back to normal file name handler if no tramp file name handler exists." |
4007ba5b | 3968 | (save-match-data |
ea9d1443 | 3969 | (let* ((filename (apply 'tramp-file-name-for-operation operation args)) |
4007ba5b KG |
3970 | (foreign (tramp-find-foreign-file-name-handler filename))) |
3971 | (cond | |
3972 | (foreign (apply foreign operation args)) | |
4007ba5b | 3973 | (t (tramp-run-real-handler operation args)))))) |
fb7933a3 | 3974 | |
ea9d1443 KG |
3975 | (defun tramp-sh-file-name-handler (operation &rest args) |
3976 | "Invoke remote-shell Tramp file name handler. | |
3977 | Fall back to normal file name handler if no Tramp handler exists." | |
3978 | (save-match-data | |
3979 | (let ((fn (assoc operation tramp-file-name-handler-alist))) | |
3980 | (if fn | |
3981 | (apply (cdr fn) args) | |
3982 | (tramp-run-real-handler operation args))))) | |
3983 | ||
16674e4f KG |
3984 | ;;;###autoload |
3985 | (defun tramp-completion-file-name-handler (operation &rest args) | |
3986 | "Invoke tramp file name completion handler. | |
3987 | Falls back to normal file name handler if no tramp file name handler exists." | |
3988 | ;; (setq tramp-debug-buffer t) | |
3989 | ;; (tramp-message 1 "%s %s" operation args) | |
3990 | ;; (tramp-message 1 "%s %s\n%s" | |
3991 | ;; operation args (with-output-to-string (backtrace))) | |
3992 | (let ((fn (assoc operation tramp-completion-file-name-handler-alist))) | |
3993 | (if fn | |
4007ba5b | 3994 | (save-match-data (apply (cdr fn) args)) |
16674e4f KG |
3995 | (tramp-completion-run-real-handler operation args)))) |
3996 | ||
b25a52cc KG |
3997 | ;;;###autoload |
3998 | (put 'tramp-completion-file-name-handler 'safe-magic t) | |
3999 | ||
fb7933a3 KG |
4000 | ;; Register in file name handler alist |
4001 | ;;;###autoload | |
4002 | (add-to-list 'file-name-handler-alist | |
4003 | (cons tramp-file-name-regexp 'tramp-file-name-handler)) | |
16674e4f KG |
4004 | (add-to-list 'file-name-handler-alist |
4005 | (cons tramp-completion-file-name-regexp | |
4006 | 'tramp-completion-file-name-handler)) | |
fb7933a3 | 4007 | |
487fa986 KG |
4008 | (defun tramp-repair-jka-compr () |
4009 | "If jka-compr is already loaded, move it to the front of | |
4010 | `file-name-handler-alist'. On Emacs 21.4 or so this will not be | |
4011 | necessary anymore." | |
4012 | (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist))) | |
4013 | (when jka | |
4014 | (setq file-name-handler-alist | |
4015 | (cons jka (delete jka file-name-handler-alist)))))) | |
4016 | (tramp-repair-jka-compr) | |
fb7933a3 | 4017 | |
c62c9d08 | 4018 | |
fb7933a3 KG |
4019 | ;;; Interactions with other packages: |
4020 | ||
4021 | ;; -- complete.el -- | |
4022 | ||
4023 | ;; This function contributed by Ed Sabol | |
4024 | (defun tramp-handle-expand-many-files (name) | |
4025 | "Like `PC-expand-many-files' for tramp files." | |
c62c9d08 | 4026 | (with-parsed-tramp-file-name name nil |
c62c9d08 KG |
4027 | (save-match-data |
4028 | (if (or (string-match "\\*" name) | |
4029 | (string-match "\\?" name) | |
4030 | (string-match "\\[.*\\]" name)) | |
4031 | (save-excursion | |
c62c9d08 | 4032 | (let (bufstr) |
c62c9d08 KG |
4033 | ;; CCC: To do it right, we should quote certain characters |
4034 | ;; in the file name, but since the echo command is going to | |
4035 | ;; break anyway when there are spaces in the file names, we | |
4036 | ;; don't bother. | |
4037 | ;;-(let ((comint-file-name-quote-list | |
4038 | ;;- (set-difference tramp-file-name-quote-list | |
4039 | ;;- '(?\* ?\? ?[ ?])))) | |
4040 | ;;- (tramp-send-command | |
4041 | ;;- multi-method method user host | |
7432277c | 4042 | ;;- (format "echo %s" (comint-quote-filename localname))) |
c62c9d08 KG |
4043 | ;;- (tramp-wait-for-output)) |
4044 | (tramp-send-command multi-method method user host | |
7432277c | 4045 | (format "echo %s" localname)) |
c62c9d08 KG |
4046 | (tramp-wait-for-output) |
4047 | (setq bufstr (buffer-substring (point-min) | |
4048 | (tramp-line-end-position))) | |
4049 | (goto-char (point-min)) | |
7432277c | 4050 | (if (string-equal localname bufstr) |
c62c9d08 KG |
4051 | nil |
4052 | (insert "(\"") | |
4053 | (while (search-forward " " nil t) | |
4054 | (delete-backward-char 1) | |
4055 | (insert "\" \"")) | |
4056 | (goto-char (point-max)) | |
4057 | (delete-backward-char 1) | |
4058 | (insert "\")") | |
4059 | (goto-char (point-min)) | |
4060 | (mapcar | |
4061 | (function (lambda (x) | |
4062 | (tramp-make-tramp-file-name multi-method method | |
4063 | user host x))) | |
4064 | (read (current-buffer)))))) | |
4065 | (list (tramp-handle-expand-file-name name)))))) | |
fb7933a3 KG |
4066 | |
4067 | ;; Check for complete.el and override PC-expand-many-files if appropriate. | |
38c65fca | 4068 | (eval-and-compile |
fb7933a3 KG |
4069 | (defun tramp-save-PC-expand-many-files (name))); avoid compiler warning |
4070 | ||
4071 | (defun tramp-setup-complete () | |
4072 | (fset 'tramp-save-PC-expand-many-files | |
4073 | (symbol-function 'PC-expand-many-files)) | |
4074 | (defun PC-expand-many-files (name) | |
4075 | (if (tramp-tramp-file-p name) | |
4076 | (tramp-handle-expand-many-files name) | |
4077 | (tramp-save-PC-expand-many-files name)))) | |
4078 | ||
4079 | ;; Why isn't eval-after-load sufficient? | |
4080 | (if (fboundp 'PC-expand-many-files) | |
4081 | (tramp-setup-complete) | |
4082 | (eval-after-load "complete" '(tramp-setup-complete))) | |
4083 | ||
16674e4f KG |
4084 | ;;; File name handler functions for completion mode |
4085 | ||
4086 | ;; Necessary because `tramp-file-name-regexp-unified' and | |
4087 | ;; `tramp-completion-file-name-regexp-unified' aren't different. | |
4088 | ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to | |
4089 | ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'. | |
7432277c | 4090 | ;; Using `last-input-event' is a little bit risky, because completing a file |
16674e4f KG |
4091 | ;; might require loading other files, like "~/.netrc", and for them it |
4092 | ;; shouldn't be decided based on that variable. On the other hand, those files | |
4093 | ;; shouldn't have partial tramp file name syntax. Maybe another variable should | |
4094 | ;; be introduced overwriting this check in such cases. Or we change tramp | |
4095 | ;; file name syntax in order to avoid ambiguities, like in XEmacs ... | |
5ec2cc41 KG |
4096 | ;; In case of non unified file names it can be always true (and wouldn't be |
4097 | ;; necessary, because there are different regexp). | |
16674e4f KG |
4098 | (defun tramp-completion-mode (file) |
4099 | "Checks whether method / user name / host name completion is active." | |
4100 | (cond | |
5ec2cc41 | 4101 | ((not tramp-unified-filenames) t) |
16674e4f KG |
4102 | ((string-match "^/.*:.*:$" file) nil) |
4103 | ((string-match | |
487f4fb7 KG |
4104 | (concat tramp-prefix-regexp |
4105 | "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$") | |
16674e4f | 4106 | file) |
4007ba5b | 4107 | (member (match-string 1 file) (mapcar 'car tramp-methods))) |
16674e4f | 4108 | ((or (equal last-input-event 'tab) |
5ec2cc41 | 4109 | ;; Emacs |
8daea7fc KG |
4110 | (and (integerp last-input-event) |
4111 | (not (event-modifiers last-input-event)) | |
16674e4f KG |
4112 | (or (char-equal last-input-event ?\?) |
4113 | (char-equal last-input-event ?\t) ; handled by 'tab already? | |
5ec2cc41 KG |
4114 | (char-equal last-input-event ?\ ))) |
4115 | ;; XEmacs | |
4116 | (and (featurep 'xemacs) | |
4117 | (not (event-modifiers last-input-event)) | |
4118 | (or (char-equal | |
38c65fca KG |
4119 | (funcall (symbol-function 'event-to-character) |
4120 | last-input-event) ?\?) | |
5ec2cc41 | 4121 | (char-equal |
38c65fca KG |
4122 | (funcall (symbol-function 'event-to-character) |
4123 | last-input-event) ?\t) | |
5ec2cc41 | 4124 | (char-equal |
38c65fca KG |
4125 | (funcall (symbol-function 'event-to-character) |
4126 | last-input-event) ?\ )))) | |
16674e4f KG |
4127 | t))) |
4128 | ||
4129 | (defun tramp-completion-handle-file-exists-p (filename) | |
4130 | "Like `file-exists-p' for tramp files." | |
4131 | (if (tramp-completion-mode filename) | |
4132 | (tramp-run-real-handler | |
4133 | 'file-exists-p (list filename)) | |
4134 | (tramp-completion-run-real-handler | |
4135 | 'file-exists-p (list filename)))) | |
4136 | ||
7432277c | 4137 | ;; Localname manipulation in case of partial TRAMP file names. |
16674e4f KG |
4138 | (defun tramp-completion-handle-file-name-directory (file) |
4139 | "Like `file-name-directory' but aware of TRAMP files." | |
4140 | (if (tramp-completion-mode file) | |
4141 | "/" | |
4142 | (tramp-completion-run-real-handler | |
4143 | 'file-name-directory (list file)))) | |
fb7933a3 | 4144 | |
7432277c | 4145 | ;; Localname manipulation in case of partial TRAMP file names. |
16674e4f KG |
4146 | (defun tramp-completion-handle-file-name-nondirectory (file) |
4147 | "Like `file-name-nondirectory' but aware of TRAMP files." | |
4148 | (substring | |
4149 | file (length (tramp-completion-handle-file-name-directory file)))) | |
4150 | ||
4151 | ;; Method, host name and user name completion. | |
4152 | ;; `tramp-completion-dissect-file-name' returns a list of | |
4153 | ;; tramp-file-name structures. For all of them we return possible completions. | |
4154 | (defun tramp-completion-handle-file-name-all-completions (filename directory) | |
4155 | "Like `file-name-all-completions' for partial tramp files." | |
4156 | ||
4157 | (let* | |
4158 | ((fullname (concat directory filename)) | |
16674e4f KG |
4159 | ;; local files |
4160 | (result | |
4161 | (if (tramp-completion-mode fullname) | |
4162 | (tramp-run-real-handler | |
4163 | 'file-name-all-completions (list filename directory)) | |
4164 | (tramp-completion-run-real-handler | |
4165 | 'file-name-all-completions (list filename directory)))) | |
4166 | ;; possible completion structures | |
4167 | (v (tramp-completion-dissect-file-name fullname))) | |
4168 | ||
4169 | (while v | |
4170 | (let* ((car (car v)) | |
4171 | (multi-method (tramp-file-name-multi-method car)) | |
4172 | (method (tramp-file-name-method car)) | |
4173 | (user (tramp-file-name-user car)) | |
4174 | (host (tramp-file-name-host car)) | |
7432277c | 4175 | (localname (tramp-file-name-localname car)) |
16674e4f | 4176 | (m (tramp-find-method multi-method method user host)) |
8daea7fc | 4177 | (tramp-current-user user) ; see `tramp-parse-passwd' |
16674e4f KG |
4178 | all-user-hosts) |
4179 | ||
4180 | (unless (or multi-method ;; Not handled (yet). | |
7432277c | 4181 | localname) ;; Nothing to complete |
16674e4f KG |
4182 | |
4183 | (if (or user host) | |
4184 | ||
4185 | ;; Method dependent user / host combinations | |
4186 | (progn | |
4187 | (mapcar | |
4007ba5b KG |
4188 | (lambda (x) |
4189 | (setq all-user-hosts | |
4190 | (append all-user-hosts | |
4191 | (funcall (nth 0 x) (nth 1 x))))) | |
16674e4f KG |
4192 | (tramp-get-completion-function m)) |
4193 | ||
7432277c | 4194 | (setq result (append result |
16674e4f | 4195 | (mapcar |
4007ba5b KG |
4196 | (lambda (x) |
4197 | (tramp-get-completion-user-host | |
4198 | method user host (nth 0 x) (nth 1 x))) | |
16674e4f KG |
4199 | (delq nil all-user-hosts))))) |
4200 | ||
4201 | ;; Possible methods | |
4202 | (setq result | |
292ffc15 | 4203 | (append result (tramp-get-completion-methods m))))) |
16674e4f KG |
4204 | |
4205 | (setq v (delq car v)))) | |
4206 | ||
4207 | ;;; unify list, remove nil elements | |
4208 | (let (result1) | |
4209 | (while result | |
4210 | (let ((car (car result))) | |
4211 | (when car (add-to-list 'result1 car)) | |
4212 | (setq result (delq car result)))) | |
4213 | ||
4214 | result1))) | |
4215 | ||
4216 | ;; Method, host name and user name completion for a file. | |
4217 | (defun tramp-completion-handle-file-name-completion (filename directory) | |
4218 | "Like `file-name-completion' for tramp files." | |
4219 | (try-completion filename | |
4220 | (mapcar 'list (file-name-all-completions filename directory)))) | |
4221 | ||
4222 | ;; I misuse a little bit the tramp-file-name structure in order to handle | |
4223 | ;; completion possibilities for partial methods / user names / host names. | |
4224 | ;; Return value is a list of tramp-file-name structures according to possible | |
7432277c | 4225 | ;; completions. If "multi-method" or "localname" is non-nil it means there |
16674e4f KG |
4226 | ;; shouldn't be a completion anymore. |
4227 | ||
4228 | ;; Expected results: | |
4229 | ||
4230 | ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y" | |
4231 | ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil] | |
4232 | ;; [nil nil "x" nil nil] | |
4233 | ;; [nil "x" nil nil nil] | |
4234 | ||
7432277c | 4235 | ;; "/x:" "/x:y" "/x:y:" |
16674e4f KG |
4236 | ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""] |
4237 | ;; "/[x/" "/[x/y" | |
4238 | ;; [nil "x" nil "" nil] [nil "x" nil "y" nil] | |
4239 | ;; [nil "x" "" nil nil] [nil "x" "y" nil nil] | |
4240 | ||
4241 | ;; "/x:y@" "/x:y@z" "/x:y@z:" | |
4242 | ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""] | |
4243 | ;; "/[x/y@" "/[x/y@z" | |
4244 | ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil] | |
4245 | (defun tramp-completion-dissect-file-name (name) | |
4246 | "Returns a list of `tramp-file-name' structures. | |
4247 | They are collected by `tramp-completion-dissect-file-name1'." | |
4248 | ||
4249 | (let* ((result) | |
4007ba5b KG |
4250 | (x-nil "\\|\\(\\)") |
4251 | ;; "/method" "/[method" | |
4252 | (tramp-completion-file-name-structure1 | |
4253 | (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$") | |
4254 | 1 nil nil nil)) | |
4255 | ;; "/user" "/[user" | |
4256 | (tramp-completion-file-name-structure2 | |
4257 | (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$") | |
4258 | nil 1 nil nil)) | |
4259 | ;; "/host" "/[host" | |
4260 | (tramp-completion-file-name-structure3 | |
4261 | (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$") | |
4262 | nil nil 1 nil)) | |
4263 | ;; "/user@host" "/[user@host" | |
4264 | (tramp-completion-file-name-structure4 | |
4265 | (list (concat tramp-prefix-regexp | |
4266 | "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp | |
4267 | "\\(" tramp-host-regexp x-nil "\\)$") | |
4268 | nil 1 2 nil)) | |
4269 | ;; "/method:user" "/[method/user" | |
4270 | (tramp-completion-file-name-structure5 | |
4271 | (list (concat tramp-prefix-regexp | |
4272 | "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp | |
4273 | "\\(" tramp-user-regexp x-nil "\\)$") | |
4274 | 1 2 nil nil)) | |
4275 | ;; "/method:host" "/[method/host" | |
4276 | (tramp-completion-file-name-structure6 | |
4277 | (list (concat tramp-prefix-regexp | |
4278 | "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp | |
4279 | "\\(" tramp-host-regexp x-nil "\\)$") | |
4280 | 1 nil 2 nil)) | |
4281 | ;; "/method:user@host" "/[method/user@host" | |
4282 | (tramp-completion-file-name-structure7 | |
4283 | (list (concat tramp-prefix-regexp | |
4284 | "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp | |
4285 | "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp | |
4286 | "\\(" tramp-host-regexp x-nil "\\)$") | |
4287 | 1 2 3 nil))) | |
4288 | ||
4289 | (mapcar (lambda (regexp) | |
16674e4f KG |
4290 | (add-to-list 'result |
4291 | (tramp-completion-dissect-file-name1 regexp name))) | |
4292 | (list | |
4293 | tramp-completion-file-name-structure1 | |
4294 | tramp-completion-file-name-structure2 | |
4295 | tramp-completion-file-name-structure3 | |
4296 | tramp-completion-file-name-structure4 | |
4297 | tramp-completion-file-name-structure5 | |
4298 | tramp-completion-file-name-structure6 | |
4299 | tramp-completion-file-name-structure7 | |
4300 | tramp-file-name-structure)) | |
4301 | ||
4302 | (delq nil result))) | |
4303 | ||
4304 | (defun tramp-completion-dissect-file-name1 (structure name) | |
4305 | "Returns a `tramp-file-name' structure matching STRUCTURE. | |
4306 | The structure consists of multi-method, remote method, remote user, | |
7432277c | 4307 | remote host and localname (filename on remote host)." |
fb7933a3 | 4308 | |
16674e4f KG |
4309 | (let (method) |
4310 | (save-match-data | |
4311 | (when (string-match (nth 0 structure) name) | |
4007ba5b KG |
4312 | (setq method (and (nth 1 structure) |
4313 | (match-string (nth 1 structure) name))) | |
16674e4f KG |
4314 | (if (and method (member method tramp-multi-methods)) |
4315 | ;; Not handled (yet). | |
4316 | (make-tramp-file-name | |
4317 | :multi-method method | |
4318 | :method nil | |
4319 | :user nil | |
4320 | :host nil | |
7432277c | 4321 | :localname nil) |
4007ba5b KG |
4322 | (let ((user (and (nth 2 structure) |
4323 | (match-string (nth 2 structure) name))) | |
4324 | (host (and (nth 3 structure) | |
4325 | (match-string (nth 3 structure) name))) | |
7432277c | 4326 | (localname (and (nth 4 structure) |
4007ba5b | 4327 | (match-string (nth 4 structure) name)))) |
16674e4f KG |
4328 | (make-tramp-file-name |
4329 | :multi-method nil | |
4330 | :method method | |
4331 | :user user | |
4332 | :host host | |
7432277c | 4333 | :localname localname))))))) |
16674e4f KG |
4334 | |
4335 | ;; This function returns all possible method completions, adding the | |
4336 | ;; trailing method delimeter. | |
16674e4f KG |
4337 | (defun tramp-get-completion-methods (partial-method) |
4338 | "Returns all method completions for PARTIAL-METHOD." | |
4007ba5b KG |
4339 | (mapcar |
4340 | (lambda (method) | |
4341 | (and method | |
4342 | (string-match (concat "^" (regexp-quote partial-method)) method) | |
4343 | ;; we must remove leading "/". | |
4344 | (substring (tramp-make-tramp-file-name nil method nil nil nil) 1))) | |
4345 | (delete "multi" (mapcar 'car tramp-methods)))) | |
16674e4f KG |
4346 | |
4347 | ;; Compares partial user and host names with possible completions. | |
4348 | (defun tramp-get-completion-user-host (method partial-user partial-host user host) | |
4349 | "Returns the most expanded string for user and host name completion. | |
4350 | PARTIAL-USER must match USER, PARTIAL-HOST must match HOST." | |
4351 | (cond | |
4352 | ||
4353 | ((and partial-user partial-host) | |
4354 | (if (and host | |
4355 | (string-match (concat "^" (regexp-quote partial-host)) host) | |
4356 | (string-equal partial-user (or user partial-user))) | |
4357 | (setq user partial-user) | |
4358 | (setq user nil | |
4359 | host nil))) | |
4360 | ||
4361 | (partial-user | |
4362 | (setq host nil) | |
4363 | (unless | |
4364 | (and user (string-match (concat "^" (regexp-quote partial-user)) user)) | |
4365 | (setq user nil))) | |
4366 | ||
4367 | (partial-host | |
4368 | (setq user nil) | |
4369 | (unless | |
4370 | (and host (string-match (concat "^" (regexp-quote partial-host)) host)) | |
4371 | (setq host nil))) | |
4372 | ||
4373 | (t (setq user nil | |
4374 | host nil))) | |
4375 | ||
292ffc15 | 4376 | (unless (zerop (+ (length user) (length host))) |
16674e4f KG |
4377 | ;; we must remove leading "/". |
4378 | (substring (tramp-make-tramp-file-name nil method user host nil) 1))) | |
4379 | ||
4380 | (defun tramp-parse-rhosts (filename) | |
4381 | "Return a list of (user host) tuples allowed to access. | |
292ffc15 | 4382 | Either user or host may be nil." |
16674e4f KG |
4383 | |
4384 | (let (res) | |
8daea7fc | 4385 | (when (file-readable-p filename) |
16674e4f KG |
4386 | (with-temp-buffer |
4387 | (insert-file-contents filename) | |
4388 | (goto-char (point-min)) | |
4389 | (while (not (eobp)) | |
292ffc15 | 4390 | (push (tramp-parse-rhosts-group) res)))) |
16674e4f KG |
4391 | res)) |
4392 | ||
4393 | ;; Taken from gnus/netrc.el | |
4394 | (eval-and-compile | |
4395 | (defalias 'tramp-point-at-eol | |
4396 | (if (fboundp 'point-at-eol) | |
4397 | 'point-at-eol | |
4398 | 'line-end-position))) | |
4399 | ||
4400 | (defun tramp-parse-rhosts-group () | |
4401 | "Return a (user host) tuple allowed to access. | |
292ffc15 | 4402 | Either user or host may be nil." |
16674e4f KG |
4403 | |
4404 | (let ((result) | |
4405 | (regexp | |
4406 | (concat | |
4407 | "^\\(" tramp-host-regexp "\\)" | |
4408 | "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?"))) | |
4409 | ||
4410 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4411 | (when (re-search-forward regexp nil t) | |
4412 | (setq result (append (list (match-string 3) (match-string 1))))) | |
4413 | (widen) | |
4414 | (forward-line 1) | |
4415 | result)) | |
4416 | ||
4417 | (defun tramp-parse-shosts (filename) | |
4418 | "Return a list of (user host) tuples allowed to access. | |
4419 | User is always nil." | |
4420 | ||
4421 | (let (res) | |
8daea7fc | 4422 | (when (file-readable-p filename) |
16674e4f KG |
4423 | (with-temp-buffer |
4424 | (insert-file-contents filename) | |
4425 | (goto-char (point-min)) | |
4426 | (while (not (eobp)) | |
292ffc15 | 4427 | (push (tramp-parse-shosts-group) res)))) |
16674e4f KG |
4428 | res)) |
4429 | ||
4430 | (defun tramp-parse-shosts-group () | |
4431 | "Return a (user host) tuple allowed to access. | |
4432 | User is always nil." | |
4433 | ||
4434 | (let ((result) | |
4435 | (regexp (concat "^\\(" tramp-host-regexp "\\)"))) | |
4436 | ||
4437 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4438 | (when (re-search-forward regexp nil t) | |
4439 | (setq result (list nil (match-string 1)))) | |
4440 | (widen) | |
4441 | (or | |
4442 | (> (skip-chars-forward ",") 0) | |
4443 | (forward-line 1)) | |
4444 | result)) | |
4445 | ||
8daea7fc KG |
4446 | (defun tramp-parse-sconfig (filename) |
4447 | "Return a list of (user host) tuples allowed to access. | |
4448 | User is always nil." | |
4449 | ||
4450 | (let (res) | |
4451 | (when (file-readable-p filename) | |
4452 | (with-temp-buffer | |
4453 | (insert-file-contents filename) | |
4454 | (goto-char (point-min)) | |
4455 | (while (not (eobp)) | |
4456 | (push (tramp-parse-sconfig-group) res)))) | |
4457 | res)) | |
4458 | ||
4459 | (defun tramp-parse-sconfig-group () | |
4460 | "Return a (user host) tuple allowed to access. | |
4461 | User is always nil." | |
4462 | ||
4463 | (let ((result) | |
4464 | (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)"))) | |
4465 | ||
4466 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4467 | (when (re-search-forward regexp nil t) | |
4468 | (setq result (list nil (match-string 1)))) | |
4469 | (widen) | |
4470 | (or | |
4471 | (> (skip-chars-forward ",") 0) | |
4472 | (forward-line 1)) | |
4473 | result)) | |
4474 | ||
5ec2cc41 KG |
4475 | (defun tramp-parse-shostkeys (dirname) |
4476 | "Return a list of (user host) tuples allowed to access. | |
4477 | User is always nil." | |
4478 | ||
4479 | (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$")) | |
4480 | (files (when (file-directory-p dirname) (directory-files dirname))) | |
4481 | result) | |
4482 | ||
4483 | (while files | |
4484 | (when (string-match regexp (car files)) | |
4485 | (push (list nil (match-string 1 (car files))) result)) | |
4486 | (setq files (cdr files))) | |
4487 | result)) | |
4488 | ||
4489 | (defun tramp-parse-sknownhosts (dirname) | |
4490 | "Return a list of (user host) tuples allowed to access. | |
4491 | User is always nil." | |
4492 | ||
4493 | (let ((regexp (concat "^\\(" tramp-host-regexp | |
4494 | "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")) | |
4495 | (files (when (file-directory-p dirname) (directory-files dirname))) | |
4496 | result) | |
4497 | ||
4498 | (while files | |
4499 | (when (string-match regexp (car files)) | |
4500 | (push (list nil (match-string 1 (car files))) result)) | |
4501 | (setq files (cdr files))) | |
4502 | result)) | |
4503 | ||
16674e4f KG |
4504 | (defun tramp-parse-hosts (filename) |
4505 | "Return a list of (user host) tuples allowed to access. | |
4506 | User is always nil." | |
4507 | ||
4508 | (let (res) | |
8daea7fc | 4509 | (when (file-readable-p filename) |
16674e4f KG |
4510 | (with-temp-buffer |
4511 | (insert-file-contents filename) | |
4512 | (goto-char (point-min)) | |
4513 | (while (not (eobp)) | |
292ffc15 | 4514 | (push (tramp-parse-hosts-group) res)))) |
16674e4f KG |
4515 | res)) |
4516 | ||
4517 | (defun tramp-parse-hosts-group () | |
4518 | "Return a (user host) tuple allowed to access. | |
4519 | User is always nil." | |
4520 | ||
4521 | (let ((result) | |
4522 | (regexp (concat "^\\(" tramp-host-regexp "\\)"))) | |
4523 | ||
4524 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4525 | (when (re-search-forward regexp nil t) | |
4526 | (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6 | |
4527 | (setq result (list nil (match-string 1))))) | |
4528 | (widen) | |
4529 | (or | |
4530 | (> (skip-chars-forward " \t") 0) | |
4531 | (forward-line 1)) | |
4532 | result)) | |
4533 | ||
8daea7fc KG |
4534 | ;; For su-alike methods it would be desirable to return "root@localhost" |
4535 | ;; as default. Unfortunately, we have no information whether any user name | |
4536 | ;; has been typed already. So we (mis-)use tramp-current-user as indication, | |
4537 | ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'. | |
16674e4f KG |
4538 | (defun tramp-parse-passwd (filename) |
4539 | "Return a list of (user host) tuples allowed to access. | |
4540 | Host is always \"localhost\"." | |
4541 | ||
4542 | (let (res) | |
8daea7fc | 4543 | (if (zerop (length tramp-current-user)) |
16674e4f | 4544 | '(("root" nil)) |
8daea7fc | 4545 | (when (file-readable-p filename) |
16674e4f KG |
4546 | (with-temp-buffer |
4547 | (insert-file-contents filename) | |
4548 | (goto-char (point-min)) | |
4549 | (while (not (eobp)) | |
292ffc15 | 4550 | (push (tramp-parse-passwd-group) res)))) |
16674e4f KG |
4551 | res))) |
4552 | ||
4553 | (defun tramp-parse-passwd-group () | |
4554 | "Return a (user host) tuple allowed to access. | |
292ffc15 | 4555 | Host is always \"localhost\"." |
16674e4f KG |
4556 | |
4557 | (let ((result) | |
4558 | (regexp (concat "^\\(" tramp-user-regexp "\\):"))) | |
4559 | ||
4560 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4561 | (when (re-search-forward regexp nil t) | |
4562 | (setq result (list (match-string 1) "localhost"))) | |
4563 | (widen) | |
4564 | (forward-line 1) | |
4565 | result)) | |
4566 | ||
292ffc15 KG |
4567 | (defun tramp-parse-netrc (filename) |
4568 | "Return a list of (user host) tuples allowed to access. | |
4569 | User may be nil." | |
4570 | ||
4571 | (let (res) | |
8daea7fc | 4572 | (when (file-readable-p filename) |
292ffc15 KG |
4573 | (with-temp-buffer |
4574 | (insert-file-contents filename) | |
4575 | (goto-char (point-min)) | |
4576 | (while (not (eobp)) | |
4577 | (push (tramp-parse-netrc-group) res)))) | |
4578 | res)) | |
4579 | ||
4580 | (defun tramp-parse-netrc-group () | |
4581 | "Return a (user host) tuple allowed to access. | |
4582 | User may be nil." | |
4583 | ||
4584 | (let ((result) | |
4585 | (regexp | |
4586 | (concat | |
4587 | "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)" | |
4588 | "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?"))) | |
4589 | ||
4590 | (narrow-to-region (point) (tramp-point-at-eol)) | |
4591 | (when (re-search-forward regexp nil t) | |
4592 | (setq result (list (match-string 3) (match-string 1)))) | |
4593 | (widen) | |
4594 | (forward-line 1) | |
4595 | result)) | |
4596 | ||
16674e4f KG |
4597 | (defun tramp-completion-handle-expand-file-name (name &optional dir) |
4598 | "Like `expand-file-name' for tramp files." | |
4599 | (let ((fullname (concat (or dir default-directory) name))) | |
8daea7fc KG |
4600 | (tramp-drop-volume-letter |
4601 | (if (tramp-completion-mode fullname) | |
4602 | (tramp-run-real-handler | |
4603 | 'expand-file-name (list name dir)) | |
4604 | (tramp-completion-run-real-handler | |
4605 | 'expand-file-name (list name dir)))))) | |
fb7933a3 KG |
4606 | |
4607 | ;;; Internal Functions: | |
4608 | ||
4609 | (defun tramp-set-auto-save () | |
4610 | (when (and (buffer-file-name) | |
4611 | (tramp-tramp-file-p (buffer-file-name)) | |
4612 | auto-save-default) | |
4613 | (auto-save-mode 1))) | |
4614 | (add-hook 'find-file-hooks 'tramp-set-auto-save t) | |
4615 | ||
4616 | (defun tramp-run-test (switch filename) | |
4617 | "Run `test' on the remote system, given a SWITCH and a FILENAME. | |
4618 | Returns the exit code of the `test' program." | |
4619 | (let ((v (tramp-dissect-file-name filename))) | |
4620 | (save-excursion | |
4621 | (tramp-send-command-and-check | |
4622 | (tramp-file-name-multi-method v) (tramp-file-name-method v) | |
4623 | (tramp-file-name-user v) (tramp-file-name-host v) | |
4624 | (format "test %s %s" switch | |
7432277c | 4625 | (tramp-shell-quote-argument (tramp-file-name-localname v))))))) |
fb7933a3 KG |
4626 | |
4627 | (defun tramp-run-test2 (program file1 file2 &optional switch) | |
4628 | "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2. | |
4629 | The optional SWITCH is inserted between the two files. | |
4630 | Returns the exit code of the `test' PROGRAM. Barfs if the methods, | |
4631 | hosts, or files, disagree." | |
4632 | (let* ((v1 (tramp-dissect-file-name file1)) | |
4633 | (v2 (tramp-dissect-file-name file2)) | |
4634 | (mmethod1 (tramp-file-name-multi-method v1)) | |
4635 | (mmethod2 (tramp-file-name-multi-method v2)) | |
4636 | (method1 (tramp-file-name-method v1)) | |
4637 | (method2 (tramp-file-name-method v2)) | |
4638 | (user1 (tramp-file-name-user v1)) | |
4639 | (user2 (tramp-file-name-user v2)) | |
4640 | (host1 (tramp-file-name-host v1)) | |
4641 | (host2 (tramp-file-name-host v2)) | |
7432277c KG |
4642 | (localname1 (tramp-file-name-localname v1)) |
4643 | (localname2 (tramp-file-name-localname v2))) | |
fb7933a3 KG |
4644 | (unless (and method1 method2 host1 host2 |
4645 | (equal mmethod1 mmethod2) | |
4646 | (equal method1 method2) | |
4647 | (equal user1 user2) | |
4648 | (equal host1 host2)) | |
4649 | (error "tramp-run-test2: %s" | |
4650 | "only implemented for same method, same user, same host")) | |
4651 | (save-excursion | |
4652 | (tramp-send-command-and-check | |
4653 | mmethod1 method1 user1 host1 | |
4654 | (format "%s %s %s %s" | |
4655 | program | |
7432277c | 4656 | (tramp-shell-quote-argument localname1) |
fb7933a3 | 4657 | (or switch "") |
7432277c | 4658 | (tramp-shell-quote-argument localname2)))))) |
fb7933a3 | 4659 | |
5ec2cc41 KG |
4660 | (defun tramp-touch (file time) |
4661 | "Set the last-modified timestamp of the given file. | |
4662 | TIME is an Emacs internal time value as returned by `current-time'." | |
4663 | (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time))) | |
38c65fca KG |
4664 | (if (tramp-tramp-file-p file) |
4665 | (with-parsed-tramp-file-name file nil | |
4666 | (let ((buf (tramp-get-buffer multi-method method user host))) | |
4667 | (unless (zerop (tramp-send-command-and-check | |
4668 | multi-method method user host | |
4669 | (format "touch -t %s %s" | |
4670 | touch-time | |
4671 | localname))) | |
4672 | (pop-to-buffer buf) | |
4673 | (error "tramp-touch: touch failed, see buffer `%s' for details" | |
4674 | buf)))) | |
4675 | ;; It's a local file | |
4676 | (with-temp-buffer | |
4677 | (unless (zerop (call-process | |
4678 | "touch" nil (current-buffer) nil "-t" touch-time file)) | |
4679 | (pop-to-buffer (current-buffer)) | |
4680 | (error "tramp-touch: touch failed")))))) | |
4681 | ||
fb7933a3 KG |
4682 | (defun tramp-buffer-name (multi-method method user host) |
4683 | "A name for the connection buffer for USER at HOST using METHOD." | |
90f8dc03 KG |
4684 | (if multi-method |
4685 | (tramp-buffer-name-multi-method "tramp" multi-method method user host) | |
4686 | (let ((method (tramp-find-method multi-method method user host))) | |
4687 | (if user | |
5ec2cc41 KG |
4688 | (format "*tramp/%s %s@%s*" method user host) |
4689 | (format "*tramp/%s %s*" method host))))) | |
fb7933a3 KG |
4690 | |
4691 | (defun tramp-buffer-name-multi-method (prefix multi-method method user host) | |
4692 | "A name for the multi method connection buffer. | |
4693 | MULTI-METHOD gives the multi method, METHOD the array of methods, | |
4694 | USER the array of user names, HOST the array of host names." | |
4695 | (unless (and (= (length method) (length user)) | |
4696 | (= (length method) (length host))) | |
4697 | (error "Syntax error in multi method (implementation error)")) | |
4698 | (let ((len (length method)) | |
4699 | (i 0) | |
4700 | string-list) | |
4701 | (while (< i len) | |
4702 | (setq string-list | |
4703 | (cons (if (aref user i) | |
4704 | (format "%s#%s@%s:" (aref method i) | |
4705 | (aref user i) (aref host i)) | |
4706 | (format "%s@%s:" (aref method i) (aref host i))) | |
4707 | string-list)) | |
4708 | (incf i)) | |
4709 | (format "*%s/%s %s*" | |
4710 | prefix multi-method | |
4711 | (apply 'concat (reverse string-list))))) | |
4712 | ||
4713 | (defun tramp-get-buffer (multi-method method user host) | |
4714 | "Get the connection buffer to be used for USER at HOST using METHOD." | |
4715 | (get-buffer-create (tramp-buffer-name multi-method method user host))) | |
4716 | ||
4717 | (defun tramp-debug-buffer-name (multi-method method user host) | |
4718 | "A name for the debug buffer for USER at HOST using METHOD." | |
90f8dc03 KG |
4719 | (if multi-method |
4720 | (tramp-buffer-name-multi-method "debug tramp" | |
4721 | multi-method method user host) | |
4722 | (let ((method (tramp-find-method multi-method method user host))) | |
4723 | (if user | |
4724 | (format "*debug tramp/%s %s@%s*" method user host) | |
4725 | (format "*debug tramp/%s %s*" method host))))) | |
fb7933a3 KG |
4726 | |
4727 | (defun tramp-get-debug-buffer (multi-method method user host) | |
4728 | "Get the debug buffer for USER at HOST using METHOD." | |
4729 | (get-buffer-create (tramp-debug-buffer-name multi-method method user host))) | |
4730 | ||
4731 | (defun tramp-find-executable (multi-method method user host | |
4732 | progname dirlist ignore-tilde) | |
4733 | "Searches for PROGNAME in all directories mentioned in DIRLIST. | |
4734 | First args METHOD, USER and HOST specify the connection, PROGNAME | |
4735 | is the program to search for, and DIRLIST gives the list of directories | |
4736 | to search. If IGNORE-TILDE is non-nil, directory names starting | |
4737 | with `~' will be ignored. | |
4738 | ||
7432277c | 4739 | Returns the absolute file name of PROGNAME, if found, and nil otherwise. |
fb7933a3 KG |
4740 | |
4741 | This function expects to be in the right *tramp* buffer." | |
4742 | (let (result) | |
4743 | (when ignore-tilde | |
4744 | ;; Remove all ~/foo directories from dirlist. In Emacs 20, | |
4745 | ;; `remove' is in CL, and we want to avoid CL dependencies. | |
4746 | (let (newdl d) | |
4747 | (while dirlist | |
4748 | (setq d (car dirlist)) | |
4749 | (setq dirlist (cdr dirlist)) | |
4750 | (unless (char-equal ?~ (aref d 0)) | |
4751 | (setq newdl (cons d newdl)))) | |
4752 | (setq dirlist (nreverse newdl)))) | |
4753 | (tramp-send-command | |
4754 | multi-method method user host | |
4755 | (format (concat "while read d; " | |
4756 | "do if test -x $d/%s -a -f $d/%s; " | |
4757 | "then echo tramp_executable $d/%s; " | |
4758 | "break; fi; done <<'EOF'") | |
4759 | progname progname progname)) | |
4760 | (mapcar (lambda (d) | |
4761 | (tramp-send-command multi-method method user host d)) | |
4762 | dirlist) | |
4763 | (tramp-send-command multi-method method user host "EOF") | |
4764 | (tramp-wait-for-output) | |
4765 | (goto-char (point-max)) | |
4766 | (when (search-backward "tramp_executable " nil t) | |
4767 | (skip-chars-forward "^ ") | |
4768 | (skip-chars-forward " ") | |
4769 | (buffer-substring (point) (tramp-line-end-position))))) | |
4770 | ||
4771 | (defun tramp-set-remote-path (multi-method method user host var dirlist) | |
4772 | "Sets the remote environment VAR to existing directories from DIRLIST. | |
4773 | I.e., for each directory in DIRLIST, it is tested whether it exists and if | |
4774 | so, it is added to the environment variable VAR." | |
4775 | (let ((existing-dirs | |
4776 | (mapcar | |
4777 | (lambda (x) | |
4778 | (when (and | |
4779 | (file-exists-p | |
4780 | (tramp-make-tramp-file-name multi-method method user host x)) | |
4781 | (file-directory-p | |
4782 | (tramp-make-tramp-file-name multi-method method user host x))) | |
4783 | x)) | |
4784 | dirlist))) | |
4785 | (tramp-send-command | |
4786 | multi-method method user host | |
4787 | (concat var "=" | |
4788 | (mapconcat 'identity (delq nil existing-dirs) ":") | |
4789 | "; export " var)) | |
4790 | (tramp-wait-for-output))) | |
4791 | ||
4792 | ;; -- communication with external shell -- | |
4793 | ||
4794 | (defun tramp-find-file-exists-command (multi-method method user host) | |
4795 | "Find a command on the remote host for checking if a file exists. | |
4796 | Here, we are looking for a command which has zero exit status if the | |
4797 | file exists and nonzero exit status otherwise." | |
4798 | (make-local-variable 'tramp-file-exists-command) | |
821e6e36 | 4799 | (tramp-message 9 "Finding command to check if file exists") |
fb7933a3 KG |
4800 | (let ((existing |
4801 | (tramp-make-tramp-file-name | |
4802 | multi-method method user host | |
4803 | "/")) ;assume this file always exists | |
4804 | (nonexisting | |
4805 | (tramp-make-tramp-file-name | |
4806 | multi-method method user host | |
4807 | "/ this file does not exist "))) ;assume this never exists | |
4808 | ;; The algorithm is as follows: we try a list of several commands. | |
4809 | ;; For each command, we first run `$cmd /' -- this should return | |
4810 | ;; true, as the root directory always exists. And then we run | |
4811 | ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed | |
4812 | ;; does not exist. This should return false. We use the first | |
4813 | ;; command we find that seems to work. | |
4814 | ;; The list of commands to try is as follows: | |
4815 | ;; `ls -d' This works on most systems, but NetBSD 1.4 | |
4816 | ;; has a bug: `ls' always returns zero exit | |
4817 | ;; status, even for files which don't exist. | |
4818 | ;; `test -e' Some Bourne shells have a `test' builtin | |
4819 | ;; which does not know the `-e' option. | |
4820 | ;; `/bin/test -e' For those, the `test' binary on disk normally | |
4821 | ;; provides the option. Alas, the binary | |
4822 | ;; is sometimes `/bin/test' and sometimes it's | |
4823 | ;; `/usr/bin/test'. | |
4824 | ;; `/usr/bin/test -e' In case `/bin/test' does not exist. | |
4825 | (unless (or | |
fb7933a3 KG |
4826 | (and (setq tramp-file-exists-command "test -e %s") |
4827 | (tramp-handle-file-exists-p existing) | |
4828 | (not (tramp-handle-file-exists-p nonexisting))) | |
4829 | (and (setq tramp-file-exists-command "/bin/test -e %s") | |
4830 | (tramp-handle-file-exists-p existing) | |
4831 | (not (tramp-handle-file-exists-p nonexisting))) | |
4832 | (and (setq tramp-file-exists-command "/usr/bin/test -e %s") | |
5beaf831 KG |
4833 | (tramp-handle-file-exists-p existing) |
4834 | (not (tramp-handle-file-exists-p nonexisting))) | |
4835 | (and (setq tramp-file-exists-command "ls -d %s") | |
fb7933a3 KG |
4836 | (tramp-handle-file-exists-p existing) |
4837 | (not (tramp-handle-file-exists-p nonexisting)))) | |
4838 | (error "Couldn't find command to check if file exists.")))) | |
7432277c | 4839 | |
fb7933a3 KG |
4840 | |
4841 | ;; CCC test ksh or bash found for tilde expansion? | |
4842 | (defun tramp-find-shell (multi-method method user host) | |
4843 | "Find a shell on the remote host which groks tilde expansion." | |
4844 | (let ((shell nil)) | |
4845 | (tramp-send-command multi-method method user host "echo ~root") | |
4846 | (tramp-wait-for-output) | |
4847 | (cond | |
4848 | ((string-match "^~root$" (buffer-string)) | |
4849 | (setq shell | |
4850 | (or (tramp-find-executable multi-method method user host | |
c62c9d08 | 4851 | "bash" tramp-remote-path t) |
fb7933a3 | 4852 | (tramp-find-executable multi-method method user host |
c62c9d08 | 4853 | "ksh" tramp-remote-path t))) |
fb7933a3 KG |
4854 | (unless shell |
4855 | (error "Couldn't find a shell which groks tilde expansion")) | |
c62c9d08 KG |
4856 | ;; Find arguments for this shell. |
4857 | (let ((alist tramp-sh-extra-args) | |
4858 | item extra-args) | |
4859 | (while (and alist (null extra-args)) | |
4860 | (setq item (pop alist)) | |
4861 | (when (string-match (car item) shell) | |
4862 | (setq extra-args (cdr item)))) | |
4863 | (when extra-args (setq shell (concat shell " " extra-args)))) | |
fb7933a3 KG |
4864 | (tramp-message |
4865 | 5 "Starting remote shell `%s' for tilde expansion..." shell) | |
4866 | (tramp-send-command | |
4867 | multi-method method user host | |
89509ea0 | 4868 | (concat "PS1='$ ' exec " shell)) ; |
fb7933a3 KG |
4869 | (unless (tramp-wait-for-regexp |
4870 | (get-buffer-process (current-buffer)) | |
821e6e36 KG |
4871 | 60 (format "\\(\\(%s\\)\\|\\(%s\\)\\)\\'" |
4872 | tramp-shell-prompt-pattern shell-prompt-pattern)) | |
fb7933a3 KG |
4873 | (pop-to-buffer (buffer-name)) |
4874 | (error "Couldn't find remote `%s' prompt." shell)) | |
89509ea0 | 4875 | (tramp-message |
821e6e36 | 4876 | 9 "Setting remote shell prompt...") |
16674e4f KG |
4877 | ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we |
4878 | ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the | |
4879 | ;; last tramp-rsh-end-of-line, Douglas wanted to replace that, | |
4880 | ;; as well. | |
fb7933a3 | 4881 | (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s" |
16674e4f | 4882 | tramp-rsh-end-of-line |
fb7933a3 | 4883 | tramp-end-of-output |
16674e4f | 4884 | tramp-rsh-end-of-line |
fb7933a3 KG |
4885 | tramp-rsh-end-of-line)) |
4886 | (tramp-wait-for-output) | |
89509ea0 | 4887 | (tramp-message |
821e6e36 | 4888 | 9 "Setting remote shell prompt...done") |
685f5858 | 4889 | ) |
fb7933a3 | 4890 | (t (tramp-message 5 "Remote `%s' groks tilde expansion, good" |
c951aecb KG |
4891 | (tramp-get-method-parameter |
4892 | multi-method method user host 'tramp-remote-sh)))))) | |
fb7933a3 KG |
4893 | |
4894 | (defun tramp-check-ls-command (multi-method method user host cmd) | |
4895 | "Checks whether the given `ls' executable groks `-n'. | |
7432277c | 4896 | METHOD, USER and HOST specify the connection, CMD (the absolute file name of) |
fb7933a3 KG |
4897 | the `ls' executable. Returns t if CMD supports the `-n' option, nil |
4898 | otherwise." | |
4899 | (tramp-message 9 "Checking remote `%s' command for `-n' option" | |
4900 | cmd) | |
4901 | (when (tramp-handle-file-executable-p | |
4902 | (tramp-make-tramp-file-name multi-method method user host cmd)) | |
4903 | (let ((result nil)) | |
4904 | (tramp-message 7 "Testing remote command `%s' for -n..." cmd) | |
4905 | (setq result | |
4906 | (tramp-send-command-and-check | |
4907 | multi-method method user host | |
4908 | (format "%s -lnd / >/dev/null" | |
4909 | cmd))) | |
4910 | (tramp-message 7 "Testing remote command `%s' for -n...%s" | |
4911 | cmd | |
4912 | (if (zerop result) "okay" "failed")) | |
4913 | (zerop result)))) | |
4914 | ||
4915 | (defun tramp-check-ls-commands (multi-method method user host cmd dirlist) | |
4916 | "Checks whether the given `ls' executable in one of the dirs groks `-n'. | |
4917 | Returns nil if none was found, else the command is returned." | |
4918 | (let ((dl dirlist) | |
19a87064 MA |
4919 | (result nil)) |
4920 | (tramp-let-maybe directory-sep-char ?/ ;for XEmacs | |
4921 | ;; It would be better to use the CL function `find', but | |
4922 | ;; we don't want run-time dependencies on CL. | |
4923 | (while (and dl (not result)) | |
4924 | (let ((x (concat (file-name-as-directory (car dl)) cmd))) | |
4925 | (when (tramp-check-ls-command multi-method method user host x) | |
4926 | (setq result x))) | |
4927 | (setq dl (cdr dl))) | |
4928 | result))) | |
fb7933a3 KG |
4929 | |
4930 | (defun tramp-find-ls-command (multi-method method user host) | |
4931 | "Finds an `ls' command which groks the `-n' option, returning nil if failed. | |
4932 | \(This option prints numeric user and group ids in a long listing.)" | |
4933 | (tramp-message 9 "Finding a suitable `ls' command") | |
4934 | (or | |
4935 | (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path) | |
4936 | (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path) | |
4937 | (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path))) | |
4938 | ||
7432277c KG |
4939 | ;; ------------------------------------------------------------ |
4940 | ;; -- Functions for establishing connection -- | |
4941 | ;; ------------------------------------------------------------ | |
fb7933a3 | 4942 | |
ac474af1 KG |
4943 | ;; The following functions are actions to be taken when seeing certain |
4944 | ;; prompts from the remote host. See the variable | |
4945 | ;; `tramp-actions-before-shell' for usage of these functions. | |
4946 | ||
4947 | (defun tramp-action-login (p multi-method method user host) | |
4948 | "Send the login name." | |
4949 | (tramp-message 9 "Sending login name `%s'" | |
4950 | (or user (user-login-name))) | |
4951 | (erase-buffer) | |
4952 | (process-send-string nil (concat (or user (user-login-name)) | |
4953 | tramp-rsh-end-of-line))) | |
4954 | ||
4955 | (defun tramp-action-password (p multi-method method user host) | |
4956 | "Query the user for a password." | |
3b89d388 | 4957 | (let ((pw-prompt (match-string 0))) |
3b89d388 KG |
4958 | (tramp-message 9 "Sending password") |
4959 | (tramp-enter-password p pw-prompt))) | |
ac474af1 KG |
4960 | |
4961 | (defun tramp-action-succeed (p multi-method method user host) | |
4962 | "Signal success in finding shell prompt." | |
4963 | (tramp-message 9 "Found remote shell prompt.") | |
4964 | (erase-buffer) | |
4965 | (throw 'tramp-action 'ok)) | |
4966 | ||
4967 | (defun tramp-action-permission-denied (p multi-method method user host) | |
4968 | "Signal permission denied." | |
b1d06e75 | 4969 | (pop-to-buffer (tramp-get-buffer multi-method method user host)) |
ac474af1 KG |
4970 | (tramp-message 9 "Permission denied by remote host.") |
4971 | (kill-process p) | |
ac474af1 KG |
4972 | (throw 'tramp-action 'permission-denied)) |
4973 | ||
4974 | (defun tramp-action-yesno (p multi-method method user host) | |
3cdaec13 KG |
4975 | "Ask the user for confirmation using `yes-or-no-p'. |
4976 | Send \"yes\" to remote process on confirmation, abort otherwise. | |
4977 | See also `tramp-action-yn'." | |
ac474af1 KG |
4978 | (save-window-excursion |
4979 | (pop-to-buffer (tramp-get-buffer multi-method method user host)) | |
4980 | (unless (yes-or-no-p (match-string 0)) | |
4981 | (kill-process p) | |
4982 | (erase-buffer) | |
4983 | (throw 'tramp-action 'permission-denied)) | |
4984 | (process-send-string p (concat "yes" tramp-rsh-end-of-line)) | |
4985 | (erase-buffer))) | |
4986 | ||
3cdaec13 KG |
4987 | (defun tramp-action-yn (p multi-method method user host) |
4988 | "Ask the user for confirmation using `y-or-n-p'. | |
4989 | Send \"y\" to remote process on confirmation, abort otherwise. | |
4990 | See also `tramp-action-yesno'." | |
4991 | (save-window-excursion | |
4992 | (pop-to-buffer (tramp-get-buffer multi-method method user host)) | |
4993 | (unless (y-or-n-p (match-string 0)) | |
4994 | (kill-process p) | |
3cdaec13 | 4995 | (throw 'tramp-action 'permission-denied)) |
4007ba5b | 4996 | (erase-buffer) |
3cdaec13 KG |
4997 | (process-send-string p (concat "y" tramp-rsh-end-of-line)))) |
4998 | ||
487f4fb7 KG |
4999 | (defun tramp-action-terminal (p multi-method method user host) |
5000 | "Tell the remote host which terminal type to use. | |
5001 | The terminal type can be configured with `tramp-terminal-type'." | |
5002 | (tramp-message 9 "Setting `%s' as terminal type." | |
5003 | tramp-terminal-type) | |
5004 | (erase-buffer) | |
5005 | (process-send-string nil (concat tramp-terminal-type | |
5006 | tramp-rsh-end-of-line))) | |
5007 | ||
19a87064 MA |
5008 | (defun tramp-action-process-alive (p multi-method method user host) |
5009 | "Check whether a process has finished." | |
5010 | (unless (memq (process-status p) '(run open)) | |
5011 | (throw 'tramp-action 'process-died))) | |
5012 | ||
38c65fca KG |
5013 | (defun tramp-action-out-of-band (p multi-method method user host) |
5014 | "Check whether an out-of-band copy has finished." | |
5015 | (cond ((and (memq (process-status p) '(stop exit)) | |
5016 | (zerop (process-exit-status p))) | |
5017 | (tramp-message 9 "Process has finished.") | |
5018 | (throw 'tramp-action 'ok)) | |
5019 | ((or (and (memq (process-status p) '(stop exit)) | |
5020 | (not (zerop (process-exit-status p)))) | |
5021 | (memq (process-status p) '(signal))) | |
5022 | (tramp-message 9 "Process has died.") | |
5023 | (throw 'tramp-action 'process-died)) | |
5024 | (t nil))) | |
5025 | ||
ac474af1 KG |
5026 | ;; The following functions are specifically for multi connections. |
5027 | ||
5028 | (defun tramp-multi-action-login (p method user host) | |
5029 | "Send the login name." | |
5030 | (tramp-message 9 "Sending login name `%s'" user) | |
5031 | (erase-buffer) | |
5032 | (process-send-string p (concat user tramp-rsh-end-of-line))) | |
5033 | ||
5034 | (defun tramp-multi-action-password (p method user host) | |
5035 | "Query the user for a password." | |
16674e4f | 5036 | (tramp-message 9 "Sending password") |
ac474af1 KG |
5037 | (tramp-enter-password p (match-string 0))) |
5038 | ||
5039 | (defun tramp-multi-action-succeed (p method user host) | |
5040 | "Signal success in finding shell prompt." | |
5041 | (tramp-message 9 "Found shell prompt on `%s'" host) | |
5042 | (erase-buffer) | |
5043 | (throw 'tramp-action 'ok)) | |
5044 | ||
5045 | (defun tramp-multi-action-permission-denied (p method user host) | |
5046 | "Signal permission denied." | |
5047 | (tramp-message 9 "Permission denied by remote host `%s'" host) | |
5048 | (kill-process p) | |
5049 | (erase-buffer) | |
5050 | (throw 'tramp-action 'permission-denied)) | |
5051 | ||
5052 | ;; Functions for processing the actions. | |
5053 | ||
5054 | (defun tramp-process-one-action (p multi-method method user host actions) | |
5055 | "Wait for output from the shell and perform one action." | |
5056 | (let (found item pattern action todo) | |
5057 | (erase-buffer) | |
5058 | (tramp-message 9 "Waiting 60s for prompt from remote shell") | |
5059 | (with-timeout (60 (throw 'tramp-action 'timeout)) | |
5060 | (while (not found) | |
5061 | (accept-process-output p 1) | |
5062 | (goto-char (point-min)) | |
5063 | (setq todo actions) | |
5064 | (while todo | |
5065 | (goto-char (point-min)) | |
5066 | (setq item (pop todo)) | |
5067 | (setq pattern (symbol-value (nth 0 item))) | |
5068 | (setq action (nth 1 item)) | |
821e6e36 KG |
5069 | (tramp-message 10 "Looking for regexp \"%s\" from remote shell" |
5070 | pattern) | |
ac474af1 KG |
5071 | (when (re-search-forward (concat pattern "\\'") nil t) |
5072 | (setq found (funcall action p multi-method method user host))))) | |
5073 | found))) | |
5074 | ||
5075 | (defun tramp-process-actions (p multi-method method user host actions) | |
5076 | "Perform actions until success." | |
5077 | (let (exit) | |
5078 | (while (not exit) | |
821e6e36 | 5079 | (tramp-message 9 "Waiting for prompts from remote shell") |
ac474af1 KG |
5080 | (setq exit |
5081 | (catch 'tramp-action | |
5082 | (tramp-process-one-action | |
5083 | p multi-method method user host actions) | |
5084 | nil))) | |
5085 | (unless (eq exit 'ok) | |
5ec2cc41 | 5086 | (tramp-clear-passwd user host) |
ac474af1 KG |
5087 | (error "Login failed")))) |
5088 | ||
5089 | ;; For multi-actions. | |
5090 | ||
5091 | (defun tramp-process-one-multi-action (p method user host actions) | |
5092 | "Wait for output from the shell and perform one action." | |
5093 | (let (found item pattern action todo) | |
5094 | (erase-buffer) | |
5095 | (tramp-message 9 "Waiting 60s for prompt from remote shell") | |
5096 | (with-timeout (60 (throw 'tramp-action 'timeout)) | |
5097 | (while (not found) | |
5098 | (accept-process-output p 1) | |
5099 | (setq todo actions) | |
5100 | (goto-char (point-min)) | |
5101 | (while todo | |
5102 | (goto-char (point-min)) | |
5103 | (setq item (pop todo)) | |
5104 | (setq pattern (symbol-value (nth 0 item))) | |
5105 | (setq action (nth 1 item)) | |
821e6e36 KG |
5106 | (tramp-message 10 "Looking for regexp \"%s\" from remote shell" |
5107 | pattern) | |
ac474af1 KG |
5108 | (when (re-search-forward (concat pattern "\\'") nil t) |
5109 | (setq found (funcall action p method user host))))) | |
5110 | found))) | |
5111 | ||
5112 | (defun tramp-process-multi-actions (p method user host actions) | |
5113 | "Perform actions until success." | |
5114 | (let (exit) | |
5115 | (while (not exit) | |
821e6e36 | 5116 | (tramp-message 9 "Waiting for prompts from remote shell") |
ac474af1 KG |
5117 | (setq exit |
5118 | (catch 'tramp-action | |
5119 | (tramp-process-one-multi-action p method user host actions) | |
5120 | nil))) | |
5121 | (unless (eq exit 'ok) | |
5ec2cc41 | 5122 | (tramp-clear-passwd user host) |
ac474af1 KG |
5123 | (error "Login failed")))) |
5124 | ||
90f8dc03 KG |
5125 | ;; Functions to execute when we have seen the remote shell prompt but |
5126 | ;; before we exec the Bourne-ish shell. Note that these commands | |
5127 | ;; might be sent to any shell, not just a Bourne-ish shell. This | |
5128 | ;; means that the commands need to work in all shells. (It is also | |
5129 | ;; okay for some commands to just fail with an error message, but | |
5130 | ;; please make sure that they at least don't crash the odd shell people | |
5131 | ;; might be running...) | |
5132 | (defun tramp-process-initial-commands (p | |
5133 | multi-method method user host | |
5134 | commands) | |
5135 | "Send list of commands to remote host, in order." | |
5136 | (let (cmd) | |
5137 | (while commands | |
5138 | (setq cmd (pop commands)) | |
5139 | (erase-buffer) | |
5140 | (tramp-message 10 "Sending command to remote shell: %s" | |
5141 | cmd) | |
38c65fca | 5142 | (tramp-send-command multi-method method user host cmd nil t) |
90f8dc03 KG |
5143 | (tramp-barf-if-no-shell-prompt |
5144 | p 60 "Remote shell command failed: %s" cmd)) | |
5145 | (erase-buffer))) | |
5146 | ||
ac474af1 | 5147 | ;; The actual functions for opening connections. |
fb7933a3 KG |
5148 | |
5149 | (defun tramp-open-connection-telnet (multi-method method user host) | |
5150 | "Open a connection using a telnet METHOD. | |
5151 | This starts the command `telnet HOST ARGS'[*], then waits for a remote | |
5152 | login prompt, then sends the user name USER, then waits for a remote | |
5153 | password prompt. It queries the user for the password, then sends the | |
5154 | password to the remote host. | |
5155 | ||
5156 | If USER is nil, uses value returned by `(user-login-name)' instead. | |
5157 | ||
821e6e36 KG |
5158 | Recognition of the remote shell prompt is based on the variables |
5159 | `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be | |
5160 | set up correctly. | |
fb7933a3 KG |
5161 | |
5162 | Please note that it is NOT possible to use this connection method | |
5163 | together with an out-of-band transfer method! You must use an inline | |
5164 | transfer method. | |
5165 | ||
5166 | Maybe the different regular expressions need to be tuned. | |
5167 | ||
5168 | * Actually, the telnet program as well as the args to be used can be | |
5169 | specified in the method parameters, see the variable `tramp-methods'." | |
5170 | (save-match-data | |
16674e4f | 5171 | (when (tramp-method-out-of-band-p multi-method method user host) |
fb7933a3 KG |
5172 | (error "Cannot use out-of-band method `%s' with telnet connection method" |
5173 | method)) | |
5174 | (when multi-method | |
5175 | (error "Cannot multi-connect using telnet connection method")) | |
5176 | (tramp-pre-connection multi-method method user host) | |
7432277c | 5177 | (tramp-message 7 "Opening connection for %s@%s using %s..." |
fb7933a3 KG |
5178 | (or user (user-login-name)) host method) |
5179 | (let ((process-environment (copy-sequence process-environment))) | |
5180 | (setenv "TERM" tramp-terminal-type) | |
5181 | (let* ((default-directory (tramp-temporary-file-directory)) | |
16674e4f KG |
5182 | ;; If we omit the conditional here, then we would use |
5183 | ;; `undecided-dos' in some cases. With the conditional, | |
5184 | ;; we use nil in these cases. Which one is right? | |
fb7933a3 KG |
5185 | (coding-system-for-read (unless (and (not (featurep 'xemacs)) |
5186 | (> emacs-major-version 20)) | |
5187 | tramp-dos-coding-system)) | |
5188 | (p (apply 'start-process | |
5189 | (tramp-buffer-name multi-method method user host) | |
5190 | (tramp-get-buffer multi-method method user host) | |
c951aecb | 5191 | (tramp-get-method-parameter |
16674e4f | 5192 | multi-method |
91879624 | 5193 | (tramp-find-method multi-method method user host) |
c951aecb | 5194 | user host 'tramp-login-program) |
fb7933a3 | 5195 | host |
c951aecb | 5196 | (tramp-get-method-parameter |
16674e4f | 5197 | multi-method |
91879624 | 5198 | (tramp-find-method multi-method method user host) |
c951aecb | 5199 | user host 'tramp-login-args))) |
fb7933a3 KG |
5200 | (found nil) |
5201 | (pw nil)) | |
19a87064 | 5202 | (tramp-set-process-query-on-exit-flag p nil) |
ac474af1 KG |
5203 | (set-buffer (tramp-get-buffer multi-method method user host)) |
5204 | (erase-buffer) | |
5205 | (tramp-process-actions p multi-method method user host | |
5206 | tramp-actions-before-shell) | |
fb7933a3 KG |
5207 | (tramp-open-connection-setup-interactive-shell |
5208 | p multi-method method user host) | |
5209 | (tramp-post-connection multi-method method user host))))) | |
5210 | ||
7432277c | 5211 | |
fb7933a3 KG |
5212 | (defun tramp-open-connection-rsh (multi-method method user host) |
5213 | "Open a connection using an rsh METHOD. | |
5214 | This starts the command `rsh HOST -l USER'[*], then waits for a remote | |
5215 | password or shell prompt. If a password prompt is seen, the user is | |
5216 | queried for a password, this function sends the password to the remote | |
5217 | host and waits for a shell prompt. | |
5218 | ||
5219 | If USER is nil, start the command `rsh HOST'[*] instead | |
5220 | ||
821e6e36 KG |
5221 | Recognition of the remote shell prompt is based on the variables |
5222 | `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be | |
5223 | set up correctly. | |
fb7933a3 | 5224 | |
8e3a1104 KG |
5225 | Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to |
5226 | be a port number for ssh, and \"-p yy\" will be added to the list of | |
5227 | arguments, and xx will be used as the host name to connect to. | |
5228 | ||
fb7933a3 KG |
5229 | * Actually, the rsh program to be used can be specified in the |
5230 | method parameters, see the variable `tramp-methods'." | |
5231 | (save-match-data | |
5232 | (when multi-method | |
5233 | (error "Cannot multi-connect using rsh connection method")) | |
5234 | (tramp-pre-connection multi-method method user host) | |
16674e4f | 5235 | (if (and user (not (string= user ""))) |
7432277c | 5236 | (tramp-message 7 "Opening connection for %s@%s using %s..." |
fb7933a3 KG |
5237 | user host method) |
5238 | (tramp-message 7 "Opening connection at %s using %s..." host method)) | |
8e3a1104 KG |
5239 | (let ((process-environment (copy-sequence process-environment)) |
5240 | (bufnam (tramp-buffer-name multi-method method user host)) | |
5241 | (buf (tramp-get-buffer multi-method method user host)) | |
c951aecb | 5242 | (login-program (tramp-get-method-parameter |
16674e4f | 5243 | multi-method |
91879624 | 5244 | (tramp-find-method multi-method method user host) |
c951aecb KG |
5245 | user host 'tramp-login-program)) |
5246 | (login-args (tramp-get-method-parameter | |
16674e4f | 5247 | multi-method |
91879624 | 5248 | (tramp-find-method multi-method method user host) |
c951aecb | 5249 | user host 'tramp-login-args))) |
8e3a1104 KG |
5250 | ;; The following should be changed. We need a more general |
5251 | ;; mechanism to parse extra host args. | |
5252 | (when (string-match "\\([^#]*\\)#\\(.*\\)" host) | |
5ec2cc41 | 5253 | (setq login-args (cons "-p" (cons (match-string 2 host) login-args))) |
8e3a1104 | 5254 | (setq host (match-string 1 host))) |
fb7933a3 KG |
5255 | (setenv "TERM" tramp-terminal-type) |
5256 | (let* ((default-directory (tramp-temporary-file-directory)) | |
16674e4f KG |
5257 | ;; If we omit the conditional, we would use |
5258 | ;; `undecided-dos' in some cases. With the conditional, | |
5259 | ;; we use nil in these cases. Which one is right? | |
fb7933a3 KG |
5260 | (coding-system-for-read (unless (and (not (featurep 'xemacs)) |
5261 | (> emacs-major-version 20)) | |
5262 | tramp-dos-coding-system)) | |
16674e4f | 5263 | (p (if (and user (not (string= user ""))) |
b25a52cc KG |
5264 | (apply #'start-process bufnam buf login-program |
5265 | host "-l" user login-args) | |
5266 | (apply #'start-process bufnam buf login-program | |
5267 | host login-args))) | |
fb7933a3 | 5268 | (found nil)) |
19a87064 | 5269 | (tramp-set-process-query-on-exit-flag p nil) |
ac474af1 KG |
5270 | |
5271 | (set-buffer buf) | |
5272 | (tramp-process-actions p multi-method method user host | |
5273 | tramp-actions-before-shell) | |
fb7933a3 KG |
5274 | (tramp-message 7 "Initializing remote shell") |
5275 | (tramp-open-connection-setup-interactive-shell | |
5276 | p multi-method method user host) | |
5277 | (tramp-post-connection multi-method method user host))))) | |
5278 | ||
fb7933a3 KG |
5279 | (defun tramp-open-connection-su (multi-method method user host) |
5280 | "Open a connection using the `su' program with METHOD. | |
5281 | This starts `su - USER', then waits for a password prompt. The HOST | |
5282 | name must be equal to the local host name or to `localhost'. | |
5283 | ||
5284 | If USER is nil, uses value returned by user-login-name instead. | |
5285 | ||
821e6e36 KG |
5286 | Recognition of the remote shell prompt is based on the variables |
5287 | `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be | |
5288 | set up correctly. Note that the other user may have a different shell | |
5289 | prompt than you do, so it is not at all unlikely that the variable | |
5290 | `shell-prompt-pattern' is set up wrongly!" | |
fb7933a3 | 5291 | (save-match-data |
16674e4f | 5292 | (when (tramp-method-out-of-band-p multi-method method user host) |
fb7933a3 KG |
5293 | (error "Cannot use out-of-band method `%s' with `su' connection method" |
5294 | method)) | |
5295 | (unless (or (string-match (concat "^" (regexp-quote host)) | |
5296 | (system-name)) | |
16674e4f KG |
5297 | (string= "localhost" host) |
5298 | (string= "" host)) | |
fb7933a3 KG |
5299 | (error |
5300 | "Cannot connect to different host `%s' with `su' connection method" | |
5301 | host)) | |
fb7933a3 | 5302 | (tramp-pre-connection multi-method method user host) |
16674e4f KG |
5303 | (tramp-message 7 "Opening connection for `%s' using `%s'..." |
5304 | (or user "<root>") method) | |
fb7933a3 KG |
5305 | (let ((process-environment (copy-sequence process-environment))) |
5306 | (setenv "TERM" tramp-terminal-type) | |
5307 | (let* ((default-directory (tramp-temporary-file-directory)) | |
16674e4f KG |
5308 | ;; If we omit the conditional, we use `undecided-dos' in |
5309 | ;; some cases. With the conditional, we use nil in these | |
5310 | ;; cases. What's the difference? Which one is right? | |
fb7933a3 KG |
5311 | (coding-system-for-read (unless (and (not (featurep 'xemacs)) |
5312 | (> emacs-major-version 20)) | |
5313 | tramp-dos-coding-system)) | |
5314 | (p (apply 'start-process | |
ac474af1 KG |
5315 | (tramp-buffer-name multi-method method user host) |
5316 | (tramp-get-buffer multi-method method user host) | |
c951aecb | 5317 | (tramp-get-method-parameter |
16674e4f | 5318 | multi-method |
91879624 | 5319 | (tramp-find-method multi-method method user host) |
c951aecb | 5320 | user host 'tramp-login-program) |
fb7933a3 | 5321 | (mapcar |
4007ba5b KG |
5322 | (lambda (x) |
5323 | (format-spec x `((?u . ,(or user "root"))))) | |
c951aecb | 5324 | (tramp-get-method-parameter |
16674e4f | 5325 | multi-method |
91879624 | 5326 | (tramp-find-method multi-method method user host) |
c951aecb | 5327 | user host 'tramp-login-args)))) |
fb7933a3 KG |
5328 | (found nil) |
5329 | (pw nil)) | |
19a87064 | 5330 | (tramp-set-process-query-on-exit-flag p nil) |
ac474af1 KG |
5331 | (set-buffer (tramp-get-buffer multi-method method user host)) |
5332 | (tramp-process-actions p multi-method method user host | |
5333 | tramp-actions-before-shell) | |
fb7933a3 KG |
5334 | (tramp-open-connection-setup-interactive-shell |
5335 | p multi-method method user host) | |
7432277c | 5336 | (tramp-post-connection multi-method method |
fb7933a3 KG |
5337 | user host))))) |
5338 | ||
7432277c | 5339 | ;; HHH: Not Changed. Multi method. It is not clear to me how this can |
fb7933a3 KG |
5340 | ;; handle not giving a user name in the "file name". |
5341 | ;; | |
5342 | ;; This is more difficult than for the single-hop method. In the | |
5343 | ;; multi-hop-method, the desired behaviour should be that the | |
5344 | ;; user must specify names for the telnet hops of which the user | |
5345 | ;; name is different than the "original" name (or different from | |
5346 | ;; the previous hop. | |
5347 | (defun tramp-open-connection-multi (multi-method method user host) | |
5348 | "Open a multi-hop connection using METHOD. | |
5349 | This uses a slightly changed file name syntax. The idea is to say | |
5350 | [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file | |
5351 | This will use telnet to log in as u1 to h1, then use rsh from there to | |
5352 | log in as u2 to h2." | |
5353 | (save-match-data | |
5354 | (unless multi-method | |
5355 | (error "Multi-hop open connection function called on non-multi method")) | |
16674e4f | 5356 | (when (tramp-method-out-of-band-p multi-method method user host) |
fb7933a3 KG |
5357 | (error "No out of band multi-hop connections")) |
5358 | (unless (and (arrayp method) (not (stringp method))) | |
5359 | (error "METHOD must be an array of strings for multi methods")) | |
5360 | (unless (and (arrayp user) (not (stringp user))) | |
5361 | (error "USER must be an array of strings for multi methods")) | |
5362 | (unless (and (arrayp host) (not (stringp host))) | |
5363 | (error "HOST must be an array of strings for multi methods")) | |
5364 | (unless (and (= (length method) (length user)) | |
5365 | (= (length method) (length host))) | |
5366 | (error "Arrays METHOD, USER, HOST must have equal length")) | |
5367 | (tramp-pre-connection multi-method method user host) | |
5368 | (tramp-message 7 "Opening `%s' connection..." multi-method) | |
5369 | (let ((process-environment (copy-sequence process-environment))) | |
5370 | (setenv "TERM" tramp-terminal-type) | |
5371 | (let* ((default-directory (tramp-temporary-file-directory)) | |
16674e4f KG |
5372 | ;; If we omit the conditional, we use `undecided-dos' in |
5373 | ;; some cases. With the conditional, we use nil in these | |
5374 | ;; cases. What's the difference? Which one is right? | |
fb7933a3 KG |
5375 | (coding-system-for-read (unless (and (not (featurep 'xemacs)) |
5376 | (> emacs-major-version 20)) | |
5377 | tramp-dos-coding-system)) | |
5378 | (p (start-process (tramp-buffer-name multi-method method user host) | |
5379 | (tramp-get-buffer multi-method method user host) | |
90dc758d | 5380 | tramp-multi-sh-program)) |
fb7933a3 KG |
5381 | (num-hops (length method)) |
5382 | (i 0)) | |
19a87064 | 5383 | (tramp-set-process-query-on-exit-flag p nil) |
fb7933a3 KG |
5384 | (tramp-message 9 "Waiting 60s for local shell to come up...") |
5385 | (unless (tramp-wait-for-regexp | |
821e6e36 KG |
5386 | p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'" |
5387 | shell-prompt-pattern tramp-shell-prompt-pattern)) | |
fb7933a3 KG |
5388 | (pop-to-buffer (buffer-name)) |
5389 | (kill-process p) | |
5390 | (error "Couldn't find local shell prompt")) | |
5391 | ;; Now do all the connections as specified. | |
5392 | (while (< i num-hops) | |
5393 | (let* ((m (aref method i)) | |
5394 | (u (aref user i)) | |
5395 | (h (aref host i)) | |
5396 | (entry (assoc m tramp-multi-connection-function-alist)) | |
5397 | (multi-func (nth 1 entry)) | |
5398 | (command (nth 2 entry))) | |
dba28077 | 5399 | ;; The multi-funcs don't need to do save-match-data, as that |
fb7933a3 KG |
5400 | ;; is done here. |
5401 | (funcall multi-func p m u h command) | |
5402 | (erase-buffer) | |
5403 | (incf i))) | |
5404 | (tramp-open-connection-setup-interactive-shell | |
5405 | p multi-method method user host) | |
5406 | (tramp-post-connection multi-method method user host))))) | |
5407 | ||
5408 | ;; HHH: Changed. Multi method. Don't know how to handle this in the case | |
7432277c | 5409 | ;; of no user name provided. Hack to make it work as it did before: |
fb7933a3 KG |
5410 | ;; changed `user' to `(or user (user-login-name))' in the places where |
5411 | ;; the value is actually used. | |
5412 | (defun tramp-multi-connect-telnet (p method user host command) | |
5413 | "Issue `telnet' command. | |
5414 | Uses shell COMMAND to issue a `telnet' command to log in as USER to | |
5415 | HOST. You can use percent escapes in COMMAND: `%h' is replaced with | |
5416 | the host name, and `%n' is replaced with an end of line character, as | |
5417 | set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent | |
5418 | character. | |
5419 | ||
5420 | If USER is nil, uses the return value of (user-login-name) instead." | |
ac474af1 KG |
5421 | (let ((cmd (format-spec command |
5422 | `((?h . ,host) (?n . ,tramp-rsh-end-of-line)))) | |
5423 | (cmd1 (format-spec command `((?h . ,host) (?n . "")))) | |
fb7933a3 KG |
5424 | found pw) |
5425 | (erase-buffer) | |
5426 | (tramp-message 9 "Sending telnet command `%s'" cmd1) | |
5427 | (process-send-string p cmd) | |
ac474af1 | 5428 | (tramp-process-multi-actions p method user host |
dba28077 | 5429 | tramp-multi-actions))) |
fb7933a3 | 5430 | |
7432277c KG |
5431 | ;; HHH: Changed. Multi method. Don't know how to handle this in the case |
5432 | ;; of no user name provided. Hack to make it work as it did before: | |
fb7933a3 KG |
5433 | ;; changed `user' to `(or user (user-login-name))' in the places where |
5434 | ;; the value is actually used. | |
5435 | (defun tramp-multi-connect-rlogin (p method user host command) | |
5436 | "Issue `rlogin' command. | |
5437 | Uses shell COMMAND to issue an `rlogin' command to log in as USER to | |
5438 | HOST. You can use percent escapes in COMMAND. `%u' will be replaced | |
5439 | with the user name, `%h' will be replaced with the host name, and `%n' | |
5440 | will be replaced with the value of `tramp-rsh-end-of-line'. You can use | |
5441 | `%%' if you want to use a literal percent character. | |
5442 | ||
5443 | If USER is nil, uses the return value of (user-login-name) instead." | |
ac474af1 KG |
5444 | (let ((cmd (format-spec command `((?h . ,host) |
5445 | (?u . ,(or user (user-login-name))) | |
5446 | (?n . ,tramp-rsh-end-of-line)))) | |
5447 | (cmd1 (format-spec command `((?h . ,host) | |
5448 | (?u . ,(or user (user-login-name))) | |
5449 | (?n . "")))) | |
fb7933a3 KG |
5450 | found) |
5451 | (erase-buffer) | |
5452 | (tramp-message 9 "Sending rlogin command `%s'" cmd1) | |
5453 | (process-send-string p cmd) | |
ac474af1 | 5454 | (tramp-process-multi-actions p method user host |
dba28077 | 5455 | tramp-multi-actions))) |
fb7933a3 | 5456 | |
7432277c KG |
5457 | ;; HHH: Changed. Multi method. Don't know how to handle this in the case |
5458 | ;; of no user name provided. Hack to make it work as it did before: | |
fb7933a3 KG |
5459 | ;; changed `user' to `(or user (user-login-name))' in the places where |
5460 | ;; the value is actually used. | |
5461 | (defun tramp-multi-connect-su (p method user host command) | |
5462 | "Issue `su' command. | |
5463 | Uses shell COMMAND to issue a `su' command to log in as USER on | |
5464 | HOST. The HOST name is ignored, this just changes the user id on the | |
5465 | host currently logged in to. | |
5466 | ||
5467 | If USER is nil, uses the return value of (user-login-name) instead. | |
5468 | ||
5469 | You can use percent escapes in the COMMAND. `%u' is replaced with the | |
5470 | user name, and `%n' is replaced with the value of | |
5471 | `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent | |
5472 | character." | |
ac474af1 KG |
5473 | (let ((cmd (format-spec command `((?u . ,(or user (user-login-name))) |
5474 | (?n . ,tramp-rsh-end-of-line)))) | |
5475 | (cmd1 (format-spec command `((?u . ,(or user (user-login-name))) | |
5476 | (?n . "")))) | |
fb7933a3 KG |
5477 | found) |
5478 | (erase-buffer) | |
5479 | (tramp-message 9 "Sending su command `%s'" cmd1) | |
5480 | (process-send-string p cmd) | |
ac474af1 | 5481 | (tramp-process-multi-actions p method user host |
dba28077 | 5482 | tramp-multi-actions))) |
fb7933a3 KG |
5483 | |
5484 | ;; Utility functions. | |
5485 | ||
5486 | (defun tramp-wait-for-regexp (proc timeout regexp) | |
5487 | "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds. | |
5488 | Expects the output of PROC to be sent to the current buffer. Returns | |
5489 | the string that matched, or nil. Waits indefinitely if TIMEOUT is | |
5490 | nil." | |
5491 | (let ((found nil) | |
5492 | (start-time (current-time))) | |
5493 | (cond (timeout | |
5494 | ;; Work around a bug in XEmacs 21, where the timeout | |
5495 | ;; expires faster than it should. This degenerates | |
5496 | ;; to polling for buggy XEmacsen, but oh, well. | |
5497 | (while (and (not found) | |
5498 | (< (tramp-time-diff (current-time) start-time) | |
5499 | timeout)) | |
5500 | (with-timeout (timeout) | |
5501 | (while (not found) | |
5502 | (accept-process-output proc 1) | |
19a87064 MA |
5503 | (unless (memq (process-status proc) '(run open)) |
5504 | (error "Process has died")) | |
fb7933a3 KG |
5505 | (goto-char (point-min)) |
5506 | (setq found (when (re-search-forward regexp nil t) | |
5507 | (tramp-match-string-list))))))) | |
5508 | (t | |
5509 | (while (not found) | |
5510 | (accept-process-output proc 1) | |
19a87064 MA |
5511 | (unless (memq (process-status proc) '(run open)) |
5512 | (error "Process has died")) | |
fb7933a3 KG |
5513 | (goto-char (point-min)) |
5514 | (setq found (when (re-search-forward regexp nil t) | |
5515 | (tramp-match-string-list)))))) | |
5516 | (when tramp-debug-buffer | |
5517 | (append-to-buffer | |
5518 | (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method | |
5519 | tramp-current-user tramp-current-host) | |
5520 | (point-min) (point-max)) | |
5521 | (when (not found) | |
5522 | (save-excursion | |
5523 | (set-buffer | |
5524 | (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method | |
5525 | tramp-current-user tramp-current-host)) | |
5526 | (goto-char (point-max)) | |
5527 | (insert "[[Regexp `" regexp "' not found" | |
487fa986 | 5528 | (if timeout (format " in %d secs" timeout) "") |
fb7933a3 KG |
5529 | "]]")))) |
5530 | found)) | |
5531 | ||
b25a52cc KG |
5532 | (defun tramp-wait-for-shell-prompt (proc timeout) |
5533 | "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds. | |
5534 | See `tramp-wait-for-regexp' for more details. | |
5535 | Shell prompt pattern is determined by variables `shell-prompt-pattern' | |
5536 | and `tramp-shell-prompt-pattern'." | |
5537 | (tramp-wait-for-regexp | |
5538 | proc timeout | |
5539 | (format "\\(%s\\|%s\\)\\'" | |
5540 | shell-prompt-pattern tramp-shell-prompt-pattern))) | |
5541 | ||
5542 | (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args) | |
5543 | "Wait for shell prompt and barf if none appears. | |
5544 | Looks at process PROC to see if a shell prompt appears in TIMEOUT | |
5545 | seconds. If not, it produces an error message with the given ERROR-ARGS." | |
5546 | (unless (tramp-wait-for-shell-prompt proc timeout) | |
5547 | (pop-to-buffer (buffer-name)) | |
5548 | (apply 'error error-args))) | |
5549 | ||
fb7933a3 KG |
5550 | (defun tramp-enter-password (p prompt) |
5551 | "Prompt for a password and send it to the remote end. | |
5552 | Uses PROMPT as a prompt and sends the password to process P." | |
5553 | (let ((pw (tramp-read-passwd prompt))) | |
ac474af1 | 5554 | (erase-buffer) |
90f8dc03 KG |
5555 | (process-send-string |
5556 | p (concat pw | |
c951aecb KG |
5557 | (or (tramp-get-method-parameter |
5558 | tramp-current-multi-method | |
5559 | tramp-current-method | |
5560 | tramp-current-user | |
5561 | tramp-current-host | |
5562 | 'tramp-password-end-of-line) | |
5563 | tramp-default-password-end-of-line))))) | |
fb7933a3 KG |
5564 | |
5565 | ;; HHH: Not Changed. This might handle the case where USER is not | |
5566 | ;; given in the "File name" very poorly. Then, the local | |
19a87064 | 5567 | ;; variable tramp-current-user will be set to nil. |
fb7933a3 KG |
5568 | (defun tramp-pre-connection (multi-method method user host) |
5569 | "Do some setup before actually logging in. | |
5570 | METHOD, USER and HOST specify the connection." | |
5571 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
5572 | (set (make-local-variable 'tramp-current-multi-method) multi-method) | |
5573 | (set (make-local-variable 'tramp-current-method) method) | |
5574 | (set (make-local-variable 'tramp-current-user) user) | |
5575 | (set (make-local-variable 'tramp-current-host) host) | |
5576 | (set (make-local-variable 'inhibit-eol-conversion) nil) | |
5577 | (erase-buffer)) | |
5578 | ||
5579 | (defun tramp-open-connection-setup-interactive-shell | |
5580 | (p multi-method method user host) | |
5581 | "Set up an interactive shell. | |
5582 | Mainly sets the prompt and the echo correctly. P is the shell process | |
5583 | to set up. METHOD, USER and HOST specify the connection." | |
5584 | ;; Wait a bit in case the remote end feels like sending a little | |
5585 | ;; junk first. It seems that fencepost.gnu.org does this when doing | |
5586 | ;; a Kerberos login. | |
5587 | (sit-for 1) | |
5588 | (tramp-discard-garbage-erase-buffer p multi-method method user host) | |
90f8dc03 KG |
5589 | (tramp-process-initial-commands p multi-method method user host |
5590 | tramp-initial-commands) | |
16674e4f KG |
5591 | ;; It is useful to set the prompt in the following command because |
5592 | ;; some people have a setting for $PS1 which /bin/sh doesn't know | |
5593 | ;; about and thus /bin/sh will display a strange prompt. For | |
5594 | ;; example, if $PS1 has "${CWD}" in the value, then ksh will display | |
5595 | ;; the current working directory but /bin/sh will display a dollar | |
5596 | ;; sign. The following command line sets $PS1 to a sane value, and | |
5597 | ;; works under Bourne-ish shells as well as csh-like shells. Daniel | |
5598 | ;; Pittman reports that the unusual positioning of the single quotes | |
7432277c KG |
5599 | ;; makes it work under `rc', too. We also unset the variable $ENV |
5600 | ;; because that is read by some sh implementations (eg, bash when | |
5601 | ;; called as sh) on startup; this way, we avoid the startup file | |
5602 | ;; clobbering $PS1. | |
b25a52cc KG |
5603 | (tramp-send-command-internal |
5604 | multi-method method user host | |
5605 | (format "exec env 'ENV=' 'PS1=$ ' %s" | |
c951aecb KG |
5606 | (tramp-get-method-parameter |
5607 | multi-method method user host 'tramp-remote-sh)) | |
b25a52cc | 5608 | (format "remote `%s' to come up" |
c951aecb KG |
5609 | (tramp-get-method-parameter |
5610 | multi-method method user host 'tramp-remote-sh))) | |
b25a52cc KG |
5611 | (tramp-barf-if-no-shell-prompt |
5612 | p 30 | |
5613 | "Remote `%s' didn't come up. See buffer `%s' for details" | |
c951aecb | 5614 | (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh) |
b25a52cc KG |
5615 | (buffer-name)) |
5616 | (tramp-message 8 "Setting up remote shell environment") | |
fb7933a3 | 5617 | (tramp-discard-garbage-erase-buffer p multi-method method user host) |
b25a52cc KG |
5618 | (tramp-send-command-internal multi-method method user host |
5619 | "stty -inlcr -echo kill '^U'") | |
fb7933a3 | 5620 | (erase-buffer) |
38c65fca KG |
5621 | ;; Ignore garbage after stty command. |
5622 | (tramp-send-command-internal multi-method method user host | |
5623 | "echo foo") | |
5624 | (erase-buffer) | |
b25a52cc KG |
5625 | (tramp-send-command-internal multi-method method user host |
5626 | "TERM=dumb; export TERM") | |
fb7933a3 KG |
5627 | ;; Try to set up the coding system correctly. |
5628 | ;; CCC this can't be the right way to do it. Hm. | |
5629 | (save-excursion | |
5630 | (erase-buffer) | |
5631 | (tramp-message 9 "Determining coding system") | |
b25a52cc KG |
5632 | (tramp-send-command-internal multi-method method user host |
5633 | "echo foo ; echo bar") | |
fb7933a3 KG |
5634 | (goto-char (point-min)) |
5635 | (if (featurep 'mule) | |
5636 | ;; Use MULE to select the right EOL convention for communicating | |
5637 | ;; with the process. | |
5638 | (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided))) | |
5639 | cs-decode cs-encode) | |
5640 | (when (symbolp cs) (setq cs (cons cs cs))) | |
5641 | (setq cs-decode (car cs)) | |
5642 | (setq cs-encode (cdr cs)) | |
5643 | (unless cs-decode (setq cs-decode 'undecided)) | |
5644 | (unless cs-encode (setq cs-encode 'undecided)) | |
5645 | (setq cs-encode (tramp-coding-system-change-eol-conversion | |
5646 | cs-encode 'unix)) | |
5647 | (when (search-forward "\r" nil t) | |
5648 | (setq cs-decode (tramp-coding-system-change-eol-conversion | |
5649 | cs-decode 'dos))) | |
5650 | (set-buffer-process-coding-system cs-decode cs-encode)) | |
5651 | ;; Look for ^M and do something useful if found. | |
5652 | (when (search-forward "\r" nil t) | |
5653 | ;; We have found a ^M but cannot frob the process coding system | |
5654 | ;; because we're running on a non-MULE Emacs. Let's try | |
5655 | ;; stty, instead. | |
90f8dc03 | 5656 | (erase-buffer) |
fb7933a3 | 5657 | (tramp-message 9 "Trying `stty -onlcr'") |
b25a52cc KG |
5658 | (tramp-send-command-internal multi-method method user host |
5659 | "stty -onlcr")))) | |
fb7933a3 KG |
5660 | (erase-buffer) |
5661 | (tramp-message | |
19a87064 MA |
5662 | 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'") |
5663 | (tramp-send-command-internal | |
5664 | multi-method method user host | |
5665 | "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE") | |
fb7933a3 KG |
5666 | (erase-buffer) |
5667 | (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'") | |
b25a52cc KG |
5668 | (tramp-send-command-internal multi-method method user host |
5669 | "set +o vi +o emacs") | |
fb7933a3 KG |
5670 | (erase-buffer) |
5671 | (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'") | |
b25a52cc KG |
5672 | (tramp-send-command-internal |
5673 | multi-method method user host | |
5674 | "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null") | |
fb7933a3 KG |
5675 | (erase-buffer) |
5676 | (tramp-message 9 "Waiting 30s for `unset CDPATH'") | |
b25a52cc KG |
5677 | (tramp-send-command-internal multi-method method user host |
5678 | "unset CDPATH") | |
fb7933a3 KG |
5679 | (erase-buffer) |
5680 | (tramp-message 9 "Setting shell prompt") | |
16674e4f | 5681 | ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must |
3b89d388 KG |
5682 | ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob |
5683 | ;; the last time we sent a command, to avoid tramp-send-command to send | |
5684 | ;; "echo are you awake". | |
5685 | (setq tramp-last-cmd-time (current-time)) | |
fb7933a3 KG |
5686 | (tramp-send-command |
5687 | multi-method method user host | |
5688 | (format "PS1='%s%s%s'; PS2=''; PS3=''" | |
16674e4f | 5689 | tramp-rsh-end-of-line |
fb7933a3 | 5690 | tramp-end-of-output |
16674e4f KG |
5691 | tramp-rsh-end-of-line)) |
5692 | (tramp-wait-for-output)) | |
fb7933a3 KG |
5693 | |
5694 | (defun tramp-post-connection (multi-method method user host) | |
5695 | "Prepare a remote shell before being able to work on it. | |
5696 | METHOD, USER and HOST specify the connection. | |
5697 | Among other things, this finds a shell which groks tilde expansion, | |
5698 | tries to find an `ls' command which groks the `-n' option, sets the | |
5699 | locale to C and sets up the remote shell search path." | |
5700 | ;; Search for a good shell before searching for a command which | |
5701 | ;; checks if a file exists. This is done because Tramp wants to use | |
5702 | ;; "test foo; echo $?" to check if various conditions hold, and | |
5703 | ;; there are buggy /bin/sh implementations which don't execute the | |
5704 | ;; "echo $?" part if the "test" part has an error. In particular, | |
5705 | ;; the Solaris /bin/sh is a problem. I'm betting that all systems | |
5706 | ;; with buggy /bin/sh implementations will have a working bash or | |
5707 | ;; ksh. Whee... | |
5708 | (tramp-find-shell multi-method method user host) | |
fb7933a3 KG |
5709 | ;; Without (sit-for 0.1) at least, my machine will almost always blow |
5710 | ;; up on 'not numberp /root' - a race that causes the 'echo ~root' | |
5711 | ;; output of (tramp-find-shell) to show up along with the output of | |
5712 | ;; (tramp-find-ls-command) testing. | |
5713 | ;; | |
5714 | ;; I can't work out why this is a problem though. The (tramp-wait-for-output) | |
5715 | ;; call in (tramp-find-shell) *should* make this not happen, I thought. | |
5716 | ;; | |
5717 | ;; After much debugging I couldn't find any problem with the implementation | |
5718 | ;; of that function though. The workaround stays for me at least. :/ | |
5719 | ;; | |
5720 | ;; Daniel Pittman <daniel@danann.net> | |
fabf2143 | 5721 | (sleep-for 1) |
5beaf831 | 5722 | (erase-buffer) |
fabf2143 | 5723 | (tramp-find-file-exists-command multi-method method user host) |
fb7933a3 KG |
5724 | (make-local-variable 'tramp-ls-command) |
5725 | (setq tramp-ls-command (tramp-find-ls-command multi-method method user host)) | |
5726 | (unless tramp-ls-command | |
5727 | (tramp-message | |
5728 | 1 | |
5729 | "Danger! Couldn't find ls which groks -n. Muddling through anyway") | |
5730 | (setq tramp-ls-command | |
5731 | (tramp-find-executable multi-method method user host | |
5732 | "ls" tramp-remote-path nil))) | |
5733 | (unless tramp-ls-command | |
5734 | (error "Fatal error: Couldn't find remote executable `ls'")) | |
5735 | (tramp-message 5 "Using remote command `%s' for getting directory listings" | |
5736 | tramp-ls-command) | |
5737 | (tramp-send-command multi-method method user host | |
5738 | (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line | |
5739 | "return $1" tramp-rsh-end-of-line | |
5740 | "}")) | |
5741 | (tramp-wait-for-output) | |
5742 | ;; Set remote PATH variable. | |
5743 | (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path) | |
5744 | ;; Tell remote shell to use standard time format, needed for | |
5745 | ;; parsing `ls -l' output. | |
5746 | (tramp-send-command multi-method method user host | |
5747 | "LC_TIME=C; export LC_TIME; echo huhu") | |
5748 | (tramp-wait-for-output) | |
5749 | (tramp-send-command multi-method method user host | |
5750 | "mesg n; echo huhu") | |
5751 | (tramp-wait-for-output) | |
5752 | (tramp-send-command multi-method method user host | |
5753 | "biff n ; echo huhu") | |
5754 | (tramp-wait-for-output) | |
5755 | ;; Unalias ls(1) to work around issues with those silly people who make it | |
5756 | ;; spit out ANSI escapes or whatever. | |
5757 | (tramp-send-command multi-method method user host | |
5758 | "unalias ls; echo huhu") | |
5759 | (tramp-wait-for-output) | |
5760 | ;; Does `test A -nt B' work? Use abominable `find' construct if it | |
5761 | ;; doesn't. BSD/OS 4.0 wants the parentheses around the command, | |
5762 | ;; for otherwise the shell crashes. | |
5763 | (erase-buffer) | |
5764 | (make-local-variable 'tramp-test-groks-nt) | |
5765 | (tramp-send-command multi-method method user host | |
5766 | "( test / -nt / )") | |
5767 | (tramp-wait-for-output) | |
5768 | (goto-char (point-min)) | |
5769 | (setq tramp-test-groks-nt | |
16674e4f | 5770 | (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output)))) |
fb7933a3 KG |
5771 | (unless tramp-test-groks-nt |
5772 | (tramp-send-command | |
5773 | multi-method method user host | |
5774 | (concat "tramp_test_nt () {" tramp-rsh-end-of-line | |
5775 | "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line | |
5776 | "}"))) | |
5777 | (tramp-wait-for-output) | |
fabf2143 KG |
5778 | ;; Send the fallback `uudecode' script. |
5779 | (erase-buffer) | |
7432277c | 5780 | (tramp-send-string multi-method method user host tramp-uudecode) |
fabf2143 | 5781 | (tramp-wait-for-output) |
fb7933a3 KG |
5782 | ;; Find a `perl'. |
5783 | (erase-buffer) | |
5784 | (let ((tramp-remote-perl | |
5785 | (or (tramp-find-executable multi-method method user host | |
fabf2143 | 5786 | "perl5" tramp-remote-path nil) |
fb7933a3 | 5787 | (tramp-find-executable multi-method method user host |
fabf2143 | 5788 | "perl" tramp-remote-path nil)))) |
fb7933a3 | 5789 | (when tramp-remote-perl |
fabf2143 KG |
5790 | (tramp-set-connection-property "perl" tramp-remote-perl |
5791 | multi-method method user host) | |
fb7933a3 KG |
5792 | ;; Set up stat in Perl if we can. |
5793 | (when tramp-remote-perl | |
5794 | (tramp-message 5 "Sending the Perl `file-attributes' implementation.") | |
7432277c | 5795 | (tramp-send-string |
fb7933a3 KG |
5796 | multi-method method user host |
5797 | (concat "tramp_file_attributes () {\n" | |
5798 | tramp-remote-perl | |
c951aecb | 5799 | " -e '" tramp-perl-file-attributes "' $1 $2 2>/dev/null\n" |
fb7933a3 KG |
5800 | "}")) |
5801 | (tramp-wait-for-output) | |
5ec2cc41 | 5802 | (unless (tramp-method-out-of-band-p multi-method method user host) |
16674e4f | 5803 | (tramp-message 5 "Sending the Perl `mime-encode' implementations.") |
7432277c | 5804 | (tramp-send-string |
16674e4f KG |
5805 | multi-method method user host |
5806 | (concat "tramp_encode () {\n" | |
5807 | (format tramp-perl-encode tramp-remote-perl) | |
5808 | " 2>/dev/null" | |
5809 | "\n}")) | |
5810 | (tramp-wait-for-output) | |
7432277c | 5811 | (tramp-send-string |
16674e4f KG |
5812 | multi-method method user host |
5813 | (concat "tramp_encode_with_module () {\n" | |
5814 | (format tramp-perl-encode-with-module tramp-remote-perl) | |
5815 | " 2>/dev/null" | |
5816 | "\n}")) | |
5817 | (tramp-wait-for-output) | |
5818 | (tramp-message 5 "Sending the Perl `mime-decode' implementations.") | |
7432277c | 5819 | (tramp-send-string |
16674e4f KG |
5820 | multi-method method user host |
5821 | (concat "tramp_decode () {\n" | |
5822 | (format tramp-perl-decode tramp-remote-perl) | |
5823 | " 2>/dev/null" | |
5824 | "\n}")) | |
5825 | (tramp-wait-for-output) | |
7432277c | 5826 | (tramp-send-string |
16674e4f KG |
5827 | multi-method method user host |
5828 | (concat "tramp_decode_with_module () {\n" | |
5829 | (format tramp-perl-decode-with-module tramp-remote-perl) | |
5830 | " 2>/dev/null" | |
5831 | "\n}")) | |
5832 | (tramp-wait-for-output))))) | |
fb7933a3 KG |
5833 | ;; Find ln(1) |
5834 | (erase-buffer) | |
5835 | (let ((ln (tramp-find-executable multi-method method user host | |
5836 | "ln" tramp-remote-path nil))) | |
5837 | (when ln | |
5838 | (tramp-set-connection-property "ln" ln multi-method method user host))) | |
5839 | (erase-buffer) | |
ac474af1 | 5840 | ;; Find the right encoding/decoding commands to use. |
5ec2cc41 | 5841 | (unless (tramp-method-out-of-band-p multi-method method user host) |
ac474af1 | 5842 | (tramp-find-inline-encoding multi-method method user host)) |
fb7933a3 KG |
5843 | ;; If encoding/decoding command are given, test to see if they work. |
5844 | ;; CCC: Maybe it would be useful to run the encoder both locally and | |
5845 | ;; remotely to see if they produce the same result. | |
16674e4f KG |
5846 | (let ((rem-enc (tramp-get-remote-encoding multi-method method user host)) |
5847 | (rem-dec (tramp-get-remote-decoding multi-method method user host)) | |
fb7933a3 | 5848 | (magic-string "xyzzy")) |
16674e4f | 5849 | (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc))) |
fb7933a3 | 5850 | (tramp-kill-process multi-method method user host) |
16674e4f | 5851 | ;; Improve error message and/or error check. |
fb7933a3 KG |
5852 | (error |
5853 | "Must give both decoding and encoding command in method definition")) | |
16674e4f | 5854 | (when (and rem-enc rem-dec) |
fb7933a3 KG |
5855 | (tramp-message |
5856 | 5 | |
5857 | "Checking to see if encoding/decoding commands work on remote host...") | |
5858 | (tramp-send-command | |
5859 | multi-method method user host | |
5860 | (format "echo %s | %s | %s" | |
16674e4f | 5861 | (tramp-shell-quote-argument magic-string) rem-enc rem-dec)) |
fb7933a3 KG |
5862 | (tramp-wait-for-output) |
5863 | (unless (looking-at (regexp-quote magic-string)) | |
5864 | (tramp-kill-process multi-method method user host) | |
5865 | (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details" | |
5866 | (buffer-name))) | |
5867 | (erase-buffer) | |
5868 | (tramp-message | |
5869 | 5 "Checking to see if encoding/decoding commands work on remote host...done")))) | |
5870 | ||
ac474af1 KG |
5871 | ;; CCC: We should either implement a Perl version of base64 encoding |
5872 | ;; and decoding. Then we just use that in the last item. The other | |
5873 | ;; alternative is to use the Perl version of UU encoding. But then | |
5874 | ;; we need a Lisp version of uuencode. | |
16674e4f KG |
5875 | ;; |
5876 | ;; Old text from documentation of tramp-methods: | |
5877 | ;; Using a uuencode/uudecode inline method is discouraged, please use one | |
5878 | ;; of the base64 methods instead since base64 encoding is much more | |
5879 | ;; reliable and the commands are more standardized between the different | |
5880 | ;; Unix versions. But if you can't use base64 for some reason, please | |
5881 | ;; note that the default uudecode command does not work well for some | |
5882 | ;; Unices, in particular AIX and Irix. For AIX, you might want to use | |
5883 | ;; the following command for uudecode: | |
5884 | ;; | |
5885 | ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1 | |
5886 | ;; | |
5887 | ;; For Irix, no solution is known yet. | |
5888 | ||
ac474af1 KG |
5889 | (defvar tramp-coding-commands |
5890 | '(("mimencode -b" "mimencode -u -b" | |
5891 | base64-encode-region base64-decode-region) | |
5892 | ("mmencode -b" "mmencode -u -b" | |
5893 | base64-encode-region base64-decode-region) | |
5894 | ("recode data..base64" "recode base64..data" | |
5895 | base64-encode-region base64-decode-region) | |
5896 | ("uuencode xxx" "uudecode -o -" | |
16674e4f | 5897 | tramp-uuencode-region uudecode-decode-region) |
ac474af1 | 5898 | ("uuencode xxx" "uudecode -p" |
16674e4f | 5899 | tramp-uuencode-region uudecode-decode-region) |
fabf2143 | 5900 | ("uuencode xxx" "tramp_uudecode" |
16674e4f | 5901 | tramp-uuencode-region uudecode-decode-region) |
b1d06e75 KG |
5902 | ("tramp_encode_with_module" "tramp_decode_with_module" |
5903 | base64-encode-region base64-decode-region) | |
ac474af1 KG |
5904 | ("tramp_encode" "tramp_decode" |
5905 | base64-encode-region base64-decode-region)) | |
5906 | "List of coding commands for inline transfer. | |
16674e4f KG |
5907 | Each item is a list that looks like this: |
5908 | ||
5909 | \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING) | |
ac474af1 | 5910 | |
16674e4f KG |
5911 | The REMOTE-ENCODING should be a string, giving a command accepting a |
5912 | plain file on standard input and writing the encoded file to standard | |
5913 | output. The REMOTE-DECODING should also be a string, giving a command | |
5914 | accepting an encoded file on standard input and writing the decoded | |
5915 | file to standard output. | |
ac474af1 | 5916 | |
16674e4f KG |
5917 | LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or |
5918 | symbols, giving functions. If they are strings, then they can contain | |
5919 | the \"%s\" format specifier. If that specifier is present, the input | |
5920 | filename will be put into the command line at that spot. If the | |
5921 | specifier is not present, the input should be read from standard | |
5922 | input. | |
ac474af1 | 5923 | |
16674e4f KG |
5924 | If they are functions, they will be called with two arguments, start |
5925 | and end of region, and are expected to replace the region contents | |
5926 | with the encoded or decoded results, respectively.") | |
ac474af1 KG |
5927 | |
5928 | (defun tramp-find-inline-encoding (multi-method method user host) | |
5929 | "Find an inline transfer encoding that works. | |
5930 | Goes through the list `tramp-coding-commands'." | |
5931 | (let ((commands tramp-coding-commands) | |
3b89d388 | 5932 | (magic "xyzzy") |
ac474af1 KG |
5933 | item found) |
5934 | (while (and commands (null found)) | |
5935 | (setq item (pop commands)) | |
5936 | (catch 'wont-work | |
16674e4f KG |
5937 | (let ((rem-enc (nth 0 item)) |
5938 | (rem-dec (nth 1 item)) | |
5939 | (loc-enc (nth 2 item)) | |
5940 | (loc-dec (nth 3 item))) | |
5941 | ;; Check if remote encoding and decoding commands can be | |
5942 | ;; called remotely with null input and output. This makes | |
5943 | ;; sure there are no syntax errors and the command is really | |
3b89d388 KG |
5944 | ;; found. Note that we do not redirect stdout to /dev/null, |
5945 | ;; for two reaons: when checking the decoding command, we | |
5946 | ;; actually check the output it gives. And also, when | |
5947 | ;; redirecting "mimencode" output to /dev/null, then as root | |
5948 | ;; it might change the permissions of /dev/null! | |
ac474af1 | 5949 | (tramp-message-for-buffer |
821e6e36 | 5950 | multi-method method user host 9 |
16674e4f | 5951 | "Checking remote encoding command `%s' for sanity" rem-enc) |
ac474af1 KG |
5952 | (unless (zerop (tramp-send-command-and-check |
5953 | multi-method method user host | |
3b89d388 | 5954 | (format "%s </dev/null" rem-enc) t)) |
ac474af1 KG |
5955 | (throw 'wont-work nil)) |
5956 | (tramp-message-for-buffer | |
821e6e36 | 5957 | multi-method method user host 9 |
16674e4f | 5958 | "Checking remote decoding command `%s' for sanity" rem-dec) |
ac474af1 KG |
5959 | (unless (zerop (tramp-send-command-and-check |
5960 | multi-method method user host | |
3b89d388 KG |
5961 | (format "echo %s | %s | %s" |
5962 | magic rem-enc rem-dec) t)) | |
ac474af1 | 5963 | (throw 'wont-work nil)) |
3b89d388 KG |
5964 | (save-excursion |
5965 | (goto-char (point-min)) | |
5966 | (unless (looking-at (regexp-quote magic)) | |
5967 | (throw 'wont-work nil))) | |
16674e4f KG |
5968 | ;; If the local encoder or decoder is a string, the |
5969 | ;; corresponding command has to work locally. | |
5970 | (when (stringp loc-enc) | |
ac474af1 | 5971 | (tramp-message-for-buffer |
821e6e36 | 5972 | multi-method method user host 9 |
16674e4f KG |
5973 | "Checking local encoding command `%s' for sanity" loc-enc) |
5974 | (unless (zerop (tramp-call-local-coding-command | |
5975 | loc-enc nil nil)) | |
ac474af1 | 5976 | (throw 'wont-work nil))) |
16674e4f | 5977 | (when (stringp loc-dec) |
ac474af1 | 5978 | (tramp-message-for-buffer |
821e6e36 | 5979 | multi-method method user host 9 |
16674e4f KG |
5980 | "Checking local decoding command `%s' for sanity" loc-dec) |
5981 | (unless (zerop (tramp-call-local-coding-command | |
5982 | loc-dec nil nil)) | |
ac474af1 KG |
5983 | (throw 'wont-work nil))) |
5984 | ;; CCC: At this point, maybe we should check that the output | |
5985 | ;; of the commands is correct. But for the moment we will | |
5986 | ;; assume that commands working on empty input will also | |
5987 | ;; work in practice. | |
5988 | (setq found item)))) | |
5989 | ;; Did we find something? If not, issue error. If so, | |
5990 | ;; set connection properties. | |
5991 | (unless found | |
5992 | (error "Couldn't find an inline transfer encoding")) | |
16674e4f KG |
5993 | (let ((rem-enc (nth 0 found)) |
5994 | (rem-dec (nth 1 found)) | |
5995 | (loc-enc (nth 2 found)) | |
5996 | (loc-dec (nth 3 found))) | |
5997 | (tramp-message 10 "Using remote encoding %s" rem-enc) | |
5998 | (tramp-set-remote-encoding multi-method method user host rem-enc) | |
5999 | (tramp-message 10 "Using remote decoding %s" rem-dec) | |
6000 | (tramp-set-remote-decoding multi-method method user host rem-dec) | |
6001 | (tramp-message 10 "Using local encoding %s" loc-enc) | |
6002 | (tramp-set-local-encoding multi-method method user host loc-enc) | |
6003 | (tramp-message 10 "Using local decoding %s" loc-dec) | |
6004 | (tramp-set-local-decoding multi-method method user host loc-dec)))) | |
6005 | ||
6006 | (defun tramp-call-local-coding-command (cmd input output) | |
6007 | "Call the local encoding or decoding command. | |
6008 | If CMD contains \"%s\", provide input file INPUT there in command. | |
6009 | Otherwise, INPUT is passed via standard input. | |
6010 | INPUT can also be nil which means `/dev/null'. | |
6011 | OUTPUT can be a string (which specifies a filename), or t (which | |
6012 | means standard output and thus the current buffer), or nil (which | |
6013 | means discard it)." | |
6014 | (call-process | |
6015 | tramp-encoding-shell ;program | |
6016 | (when (and input (not (string-match "%s" cmd))) | |
6017 | input) ;input | |
6018 | (if (eq output t) t nil) ;output | |
6019 | nil ;redisplay | |
6020 | tramp-encoding-command-switch | |
6021 | ;; actual shell command | |
6022 | (concat | |
6023 | (if (string-match "%s" cmd) (format cmd input) cmd) | |
6024 | (if (stringp output) (concat "> " output) "")))) | |
fb7933a3 KG |
6025 | |
6026 | (defun tramp-maybe-open-connection (multi-method method user host) | |
6027 | "Maybe open a connection to HOST, logging in as USER, using METHOD. | |
6028 | Does not do anything if a connection is already open, but re-opens the | |
6029 | connection if a previous connection has died for some reason." | |
16674e4f KG |
6030 | (let ((p (get-buffer-process |
6031 | (tramp-get-buffer multi-method method user host))) | |
ac474af1 KG |
6032 | last-cmd-time) |
6033 | ;; If too much time has passed since last command was sent, look | |
6034 | ;; whether process is still alive. If it isn't, kill it. When | |
6035 | ;; using ssh, it can sometimes happen that the remote end has hung | |
6036 | ;; up but the local ssh client doesn't recognize this until it | |
6037 | ;; tries to send some data to the remote end. So that's why we | |
6038 | ;; try to send a command from time to time, then look again | |
6039 | ;; whether the process is really alive. | |
6040 | (save-excursion | |
6041 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
6042 | (when (and tramp-last-cmd-time | |
3b89d388 KG |
6043 | (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60) |
6044 | p (processp p) (memq (process-status p) '(run open))) | |
685f5858 KG |
6045 | (tramp-send-command |
6046 | multi-method method user host "echo are you awake" nil t) | |
6047 | (unless (tramp-wait-for-output 10) | |
ac474af1 KG |
6048 | (delete-process p) |
6049 | (setq p nil)) | |
6050 | (erase-buffer))) | |
6051 | (unless (and p (processp p) (memq (process-status p) '(run open))) | |
fb7933a3 KG |
6052 | (when (and p (processp p)) |
6053 | (delete-process p)) | |
5ec2cc41 KG |
6054 | (let ((process-connection-type tramp-process-connection-type)) |
6055 | (funcall (tramp-get-method-parameter | |
6056 | multi-method | |
6057 | (tramp-find-method multi-method method user host) | |
6058 | user host 'tramp-connection-function) | |
6059 | multi-method method user host))))) | |
fb7933a3 KG |
6060 | |
6061 | (defun tramp-send-command | |
685f5858 | 6062 | (multi-method method user host command &optional noerase neveropen) |
fb7933a3 KG |
6063 | "Send the COMMAND to USER at HOST (logged in using METHOD). |
6064 | Erases temporary buffer before sending the command (unless NOERASE | |
685f5858 KG |
6065 | is true). |
6066 | If optional seventh arg NEVEROPEN is non-nil, never try to open the | |
6067 | connection. This is meant to be used from | |
6068 | `tramp-maybe-open-connection' only." | |
6069 | (or neveropen | |
6070 | (tramp-maybe-open-connection multi-method method user host)) | |
ac474af1 | 6071 | (setq tramp-last-cmd-time (current-time)) |
38c65fca | 6072 | (setq tramp-last-cmd command) |
fb7933a3 KG |
6073 | (when tramp-debug-buffer |
6074 | (save-excursion | |
6075 | (set-buffer (tramp-get-debug-buffer multi-method method user host)) | |
6076 | (goto-char (point-max)) | |
6077 | (tramp-insert-with-face 'bold (format "$ %s\n" command)))) | |
6078 | (let ((proc nil)) | |
6079 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
6080 | (unless noerase (erase-buffer)) | |
6081 | (setq proc (get-buffer-process (current-buffer))) | |
6082 | (process-send-string proc | |
6083 | (concat command tramp-rsh-end-of-line)))) | |
6084 | ||
b25a52cc KG |
6085 | (defun tramp-send-command-internal |
6086 | (multi-method method user host command &optional msg) | |
6087 | "Send command to remote host and wait for success. | |
6088 | Sends COMMAND, then waits 30 seconds for shell prompt." | |
6089 | (tramp-send-command multi-method method user host command t t) | |
6090 | (when msg | |
6091 | (tramp-message 9 "Waiting 30s for %s..." msg)) | |
6092 | (tramp-barf-if-no-shell-prompt | |
6093 | nil 30 | |
6094 | "Couldn't `%s', see buffer `%s'" command (buffer-name))) | |
6095 | ||
fb7933a3 KG |
6096 | (defun tramp-wait-for-output (&optional timeout) |
6097 | "Wait for output from remote rsh command." | |
6098 | (let ((proc (get-buffer-process (current-buffer))) | |
6099 | (found nil) | |
6100 | (start-time (current-time)) | |
38c65fca | 6101 | (start-point (point)) |
fb7933a3 KG |
6102 | (end-of-output (concat "^" |
6103 | (regexp-quote tramp-end-of-output) | |
16674e4f | 6104 | "\r?$"))) |
fb7933a3 KG |
6105 | ;; Algorithm: get waiting output. See if last line contains |
6106 | ;; end-of-output sentinel. If not, wait a bit and again get | |
6107 | ;; waiting output. Repeat until timeout expires or end-of-output | |
6108 | ;; sentinel is seen. Will hang if timeout is nil and | |
6109 | ;; end-of-output sentinel never appears. | |
6110 | (save-match-data | |
6111 | (cond (timeout | |
6112 | ;; Work around an XEmacs bug, where the timeout expires | |
6113 | ;; faster than it should. This degenerates into polling | |
6114 | ;; for buggy XEmacsen, but oh, well. | |
6115 | (while (and (not found) | |
6116 | (< (tramp-time-diff (current-time) start-time) | |
6117 | timeout)) | |
6118 | (with-timeout (timeout) | |
6119 | (while (not found) | |
6120 | (accept-process-output proc 1) | |
19a87064 MA |
6121 | (unless (memq (process-status proc) '(run open)) |
6122 | (error "Process has died")) | |
fb7933a3 KG |
6123 | (goto-char (point-max)) |
6124 | (forward-line -1) | |
6125 | (setq found (looking-at end-of-output)))))) | |
6126 | (t | |
6127 | (while (not found) | |
6128 | (accept-process-output proc 1) | |
19a87064 MA |
6129 | (unless (memq (process-status proc) '(run open)) |
6130 | (error "Process has died")) | |
fb7933a3 KG |
6131 | (goto-char (point-max)) |
6132 | (forward-line -1) | |
6133 | (setq found (looking-at end-of-output)))))) | |
6134 | ;; At this point, either the timeout has expired or we have found | |
6135 | ;; the end-of-output sentinel. | |
6136 | (when found | |
6137 | (goto-char (point-max)) | |
6138 | (forward-line -2) | |
6139 | (delete-region (point) (point-max))) | |
38c65fca KG |
6140 | ;; If processing echoes, look for it in the first line and delete. |
6141 | (when tramp-process-echoes | |
6142 | (save-excursion | |
6143 | (goto-char start-point) | |
6144 | (when (looking-at (regexp-quote tramp-last-cmd)) | |
6145 | (delete-region (point) (forward-line 1))))) | |
fb7933a3 KG |
6146 | ;; Add output to debug buffer if appropriate. |
6147 | (when tramp-debug-buffer | |
6148 | (append-to-buffer | |
6149 | (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method | |
6150 | tramp-current-user tramp-current-host) | |
6151 | (point-min) (point-max)) | |
6152 | (when (not found) | |
6153 | (save-excursion | |
6154 | (set-buffer | |
6155 | (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method | |
6156 | tramp-current-user tramp-current-host)) | |
6157 | (goto-char (point-max)) | |
6158 | (insert "[[Remote prompt `" end-of-output "' not found" | |
487fa986 | 6159 | (if timeout (format " in %d secs" timeout) "") |
fb7933a3 KG |
6160 | "]]")))) |
6161 | (goto-char (point-min)) | |
6162 | ;; Return value is whether end-of-output sentinel was found. | |
6163 | found)) | |
6164 | ||
6165 | (defun tramp-match-string-list (&optional string) | |
6166 | "Returns list of all match strings. | |
6167 | That is, (list (match-string 0) (match-string 1) ...), according to the | |
6168 | number of matches." | |
6169 | (let* ((nmatches (/ (length (match-data)) 2)) | |
6170 | (i (- nmatches 1)) | |
6171 | (res nil)) | |
6172 | (while (>= i 0) | |
6173 | (setq res (cons (match-string i string) res)) | |
6174 | (setq i (- i 1))) | |
6175 | res)) | |
6176 | ||
6177 | (defun tramp-send-command-and-check (multi-method method user host command | |
6178 | &optional subshell) | |
6179 | "Run COMMAND and check its exit status. | |
6180 | MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST. | |
6181 | Sends `echo $?' along with the COMMAND for checking the exit status. If | |
6182 | COMMAND is nil, just sends `echo $?'. Returns the exit status found. | |
6183 | ||
6184 | If the optional argument SUBSHELL is non-nil, the command is executed in | |
6185 | a subshell, ie surrounded by parentheses." | |
6186 | (tramp-send-command multi-method method user host | |
6187 | (concat (if subshell "( " "") | |
6188 | command | |
6189 | (if command " 2>/dev/null; " "") | |
6190 | "echo tramp_exit_status $?" | |
6191 | (if subshell " )" " "))) | |
6192 | (tramp-wait-for-output) | |
6193 | (goto-char (point-max)) | |
6194 | (unless (search-backward "tramp_exit_status " nil t) | |
6195 | (error "Couldn't find exit status of `%s'" command)) | |
6196 | (skip-chars-forward "^ ") | |
6197 | (read (current-buffer))) | |
6198 | ||
6199 | (defun tramp-barf-unless-okay (multi-method method user host command subshell | |
6200 | signal fmt &rest args) | |
6201 | "Run COMMAND, check exit status, throw error if exit status not okay. | |
6202 | Similar to `tramp-send-command-and-check' but accepts two more arguments | |
6203 | FMT and ARGS which are passed to `error'." | |
6204 | (unless (zerop (tramp-send-command-and-check | |
6205 | multi-method method user host command subshell)) | |
6206 | ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more | |
6207 | ;; silent. | |
6208 | (pop-to-buffer (current-buffer)) | |
6209 | (funcall 'signal signal (apply 'format fmt args)))) | |
6210 | ||
7432277c KG |
6211 | ;; It seems that Tru64 Unix does not like it if long strings are sent |
6212 | ;; to it in one go. (This happens when sending the Perl | |
6213 | ;; `file-attributes' implementation, for instance.) Therefore, we | |
6214 | ;; have this function which waits a bit at each line. | |
6215 | (defun tramp-send-string | |
6216 | (multi-method method user host string) | |
6217 | "Send the STRING to USER at HOST using METHOD. | |
6218 | ||
6219 | The STRING is expected to use Unix line-endings, but the lines sent to | |
6220 | the remote host use line-endings as defined in the variable | |
6221 | `tramp-rsh-end-of-line'." | |
fb7933a3 KG |
6222 | (let ((proc (get-buffer-process |
6223 | (tramp-get-buffer multi-method method user host)))) | |
6224 | (unless proc | |
7432277c KG |
6225 | (error "Can't send string to remote host -- not logged in")) |
6226 | ;; debug message | |
6227 | (when tramp-debug-buffer | |
6228 | (save-excursion | |
6229 | (set-buffer (tramp-get-debug-buffer multi-method method user host)) | |
6230 | (goto-char (point-max)) | |
6231 | (tramp-insert-with-face 'bold (format "$ %s\n" string)))) | |
6232 | ;; replace "\n" by `tramp-rsh-end-of-line' | |
6233 | (setq string | |
6234 | (mapconcat 'identity | |
6235 | (split-string string "\n") | |
6236 | tramp-rsh-end-of-line)) | |
49914e04 AS |
6237 | (unless (or (string= string "") |
6238 | (string-equal (substring string -1) tramp-rsh-end-of-line)) | |
7432277c KG |
6239 | (setq string (concat string tramp-rsh-end-of-line))) |
6240 | ;; send the string | |
8daea7fc | 6241 | (if (and tramp-chunksize (not (zerop tramp-chunksize))) |
7432277c KG |
6242 | (let ((pos 0) |
6243 | (end (length string))) | |
16674e4f KG |
6244 | (while (< pos end) |
6245 | (tramp-message-for-buffer | |
6246 | multi-method method user host 10 | |
7432277c KG |
6247 | "Sending chunk from %s to %s" |
6248 | pos (min (+ pos tramp-chunksize) end)) | |
6249 | (process-send-string | |
6250 | proc (substring string pos (min (+ pos tramp-chunksize) end))) | |
16674e4f KG |
6251 | (setq pos (+ pos tramp-chunksize)) |
6252 | (sleep-for 0.1))) | |
7432277c | 6253 | (process-send-string proc string)))) |
fb7933a3 KG |
6254 | |
6255 | (defun tramp-send-eof (multi-method method user host) | |
6256 | "Send EOF to the remote end. | |
25f78d18 | 6257 | METHOD, HOST and USER specify the connection." |
fb7933a3 KG |
6258 | (let ((proc (get-buffer-process |
6259 | (tramp-get-buffer multi-method method user host)))) | |
6260 | (unless proc | |
6261 | (error "Can't send EOF to remote host -- not logged in")) | |
6262 | (process-send-eof proc))) | |
6263 | ; (process-send-string proc "\^D"))) | |
6264 | ||
6265 | (defun tramp-kill-process (multi-method method user host) | |
6266 | "Kill the connection process used by Tramp. | |
25f78d18 | 6267 | MULTI-METHOD, METHOD, USER, and HOST specify the connection." |
fb7933a3 KG |
6268 | (let ((proc (get-buffer-process |
6269 | (tramp-get-buffer multi-method method user host)))) | |
6270 | (kill-process proc))) | |
6271 | ||
6272 | (defun tramp-discard-garbage-erase-buffer (p multi-method method user host) | |
6273 | "Erase buffer, then discard subsequent garbage. | |
6274 | If `tramp-discard-garbage' is nil, just erase buffer." | |
6275 | (if (not tramp-discard-garbage) | |
6276 | (erase-buffer) | |
6277 | (while (prog1 (erase-buffer) (accept-process-output p 0.25)) | |
6278 | (when tramp-debug-buffer | |
6279 | (save-excursion | |
6280 | (set-buffer (tramp-get-debug-buffer multi-method method user host)) | |
6281 | (goto-char (point-max)) | |
6282 | (tramp-insert-with-face | |
6283 | 'bold (format "Additional characters detected\n"))))))) | |
6284 | ||
6285 | (defun tramp-mode-string-to-int (mode-string) | |
6286 | "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits." | |
6287 | (let* ((mode-chars (string-to-vector mode-string)) | |
6288 | (owner-read (aref mode-chars 1)) | |
6289 | (owner-write (aref mode-chars 2)) | |
6290 | (owner-execute-or-setid (aref mode-chars 3)) | |
6291 | (group-read (aref mode-chars 4)) | |
6292 | (group-write (aref mode-chars 5)) | |
6293 | (group-execute-or-setid (aref mode-chars 6)) | |
6294 | (other-read (aref mode-chars 7)) | |
6295 | (other-write (aref mode-chars 8)) | |
6296 | (other-execute-or-sticky (aref mode-chars 9))) | |
6297 | (save-match-data | |
6298 | (logior | |
6299 | (case owner-read | |
6300 | (?r (tramp-octal-to-decimal "00400")) (?- 0) | |
6301 | (t (error "Second char `%c' must be one of `r-'" owner-read))) | |
6302 | (case owner-write | |
6303 | (?w (tramp-octal-to-decimal "00200")) (?- 0) | |
6304 | (t (error "Third char `%c' must be one of `w-'" owner-write))) | |
6305 | (case owner-execute-or-setid | |
6306 | (?x (tramp-octal-to-decimal "00100")) | |
6307 | (?S (tramp-octal-to-decimal "04000")) | |
6308 | (?s (tramp-octal-to-decimal "04100")) | |
6309 | (?- 0) | |
6310 | (t (error "Fourth char `%c' must be one of `xsS-'" | |
6311 | owner-execute-or-setid))) | |
6312 | (case group-read | |
6313 | (?r (tramp-octal-to-decimal "00040")) (?- 0) | |
6314 | (t (error "Fifth char `%c' must be one of `r-'" group-read))) | |
6315 | (case group-write | |
6316 | (?w (tramp-octal-to-decimal "00020")) (?- 0) | |
6317 | (t (error "Sixth char `%c' must be one of `w-'" group-write))) | |
6318 | (case group-execute-or-setid | |
6319 | (?x (tramp-octal-to-decimal "00010")) | |
6320 | (?S (tramp-octal-to-decimal "02000")) | |
6321 | (?s (tramp-octal-to-decimal "02010")) | |
6322 | (?- 0) | |
6323 | (t (error "Seventh char `%c' must be one of `xsS-'" | |
6324 | group-execute-or-setid))) | |
6325 | (case other-read | |
6326 | (?r (tramp-octal-to-decimal "00004")) (?- 0) | |
6327 | (t (error "Eighth char `%c' must be one of `r-'" other-read))) | |
6328 | (case other-write | |
6329 | (?w (tramp-octal-to-decimal "00002")) (?- 0) | |
6330 | (t (error "Nineth char `%c' must be one of `w-'" other-write))) | |
6331 | (case other-execute-or-sticky | |
6332 | (?x (tramp-octal-to-decimal "00001")) | |
6333 | (?T (tramp-octal-to-decimal "01000")) | |
6334 | (?t (tramp-octal-to-decimal "01001")) | |
6335 | (?- 0) | |
6336 | (t (error "Tenth char `%c' must be one of `xtT-'" | |
6337 | other-execute-or-sticky))))))) | |
6338 | ||
6339 | ||
6340 | (defun tramp-file-mode-from-int (mode) | |
6341 | "Turn an integer representing a file mode into an ls(1)-like string." | |
6342 | (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map))) | |
6343 | (user (logand (lsh mode -6) 7)) | |
6344 | (group (logand (lsh mode -3) 7)) | |
6345 | (other (logand (lsh mode -0) 7)) | |
6346 | (suid (> (logand (lsh mode -9) 4) 0)) | |
6347 | (sgid (> (logand (lsh mode -9) 2) 0)) | |
6348 | (sticky (> (logand (lsh mode -9) 1) 0))) | |
6349 | (setq user (tramp-file-mode-permissions user suid "s")) | |
6350 | (setq group (tramp-file-mode-permissions group sgid "s")) | |
6351 | (setq other (tramp-file-mode-permissions other sticky "t")) | |
6352 | (concat type user group other))) | |
6353 | ||
6354 | ||
6355 | (defun tramp-file-mode-permissions (perm suid suid-text) | |
6356 | "Convert a permission bitset into a string. | |
6357 | This is used internally by `tramp-file-mode-from-int'." | |
6358 | (let ((r (> (logand perm 4) 0)) | |
6359 | (w (> (logand perm 2) 0)) | |
6360 | (x (> (logand perm 1) 0))) | |
6361 | (concat (or (and r "r") "-") | |
6362 | (or (and w "w") "-") | |
6363 | (or (and suid x suid-text) ; suid, execute | |
6364 | (and suid (upcase suid-text)) ; suid, !execute | |
6365 | (and x "x") "-")))) ; !suid | |
6366 | ||
6367 | ||
6368 | (defun tramp-decimal-to-octal (i) | |
6369 | "Return a string consisting of the octal digits of I. | |
6370 | Not actually used. Use `(format \"%o\" i)' instead?" | |
6371 | (cond ((< i 0) (error "Cannot convert negative number to octal")) | |
6372 | ((not (integerp i)) (error "Cannot convert non-integer to octal")) | |
6373 | ((zerop i) "0") | |
6374 | (t (concat (tramp-decimal-to-octal (/ i 8)) | |
6375 | (number-to-string (% i 8)))))) | |
6376 | ||
6377 | ||
6378 | ;;(defun tramp-octal-to-decimal (ostr) | |
6379 | ;; "Given a string of octal digits, return a decimal number." | |
6380 | ;; (cond ((null ostr) 0) | |
6381 | ;; ((string= "" ostr) 0) | |
6382 | ;; (t (let ((last (aref ostr (1- (length ostr)))) | |
6383 | ;; (rest (substring ostr 0 (1- (length ostr))))) | |
6384 | ;; (unless (and (>= last ?0) | |
6385 | ;; (<= last ?7)) | |
6386 | ;; (error "Not an octal digit: %c" last)) | |
6387 | ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest))))))) | |
6388 | ;; Kudos to Gerd Moellmann for this suggestion. | |
6389 | (defun tramp-octal-to-decimal (ostr) | |
6390 | "Given a string of octal digits, return a decimal number." | |
6391 | (let ((x (or ostr ""))) | |
6392 | ;; `save-match' is in `tramp-mode-string-to-int' which calls this. | |
6393 | (unless (string-match "\\`[0-7]*\\'" x) | |
6394 | (error "Non-octal junk in string `%s'" x)) | |
6395 | (string-to-number ostr 8))) | |
6396 | ||
6397 | (defun tramp-shell-case-fold (string) | |
6398 | "Converts STRING to shell glob pattern which ignores case." | |
6399 | (mapconcat | |
6400 | (lambda (c) | |
6401 | (if (equal (downcase c) (upcase c)) | |
6402 | (vector c) | |
6403 | (format "[%c%c]" (downcase c) (upcase c)))) | |
6404 | string | |
6405 | "")) | |
6406 | ||
6407 | ||
7432277c KG |
6408 | ;; ------------------------------------------------------------ |
6409 | ;; -- TRAMP file names -- | |
6410 | ;; ------------------------------------------------------------ | |
fb7933a3 KG |
6411 | ;; Conversion functions between external representation and |
6412 | ;; internal data structure. Convenience functions for internal | |
6413 | ;; data structure. | |
6414 | ||
7432277c | 6415 | (defstruct tramp-file-name multi-method method user host localname) |
fb7933a3 KG |
6416 | |
6417 | (defun tramp-tramp-file-p (name) | |
6418 | "Return t iff NAME is a tramp file." | |
6419 | (save-match-data | |
6420 | (string-match tramp-file-name-regexp name))) | |
7432277c | 6421 | |
fb7933a3 KG |
6422 | ;; HHH: Changed. Used to assign the return value of (user-login-name) |
6423 | ;; to the `user' part of the structure if a user name was not | |
6424 | ;; provided, now it assigns nil. | |
6425 | (defun tramp-dissect-file-name (name) | |
6426 | "Return an `tramp-file-name' structure. | |
6427 | The structure consists of remote method, remote user, remote host and | |
7432277c | 6428 | localname (file name on remote host)." |
4007ba5b KG |
6429 | (save-match-data |
6430 | (let* ((match (string-match (nth 0 tramp-file-name-structure) name)) | |
6431 | (method | |
6432 | ; single-hop | |
6433 | (if match (match-string (nth 1 tramp-file-name-structure) name) | |
6434 | ; maybe multi-hop | |
6435 | (string-match | |
6436 | (format (nth 0 tramp-multi-file-name-structure) | |
6437 | (nth 0 tramp-multi-file-name-hop-structure)) name) | |
6438 | (match-string (nth 1 tramp-multi-file-name-structure) name)))) | |
c62c9d08 | 6439 | (if (and method (member method tramp-multi-methods)) |
fb7933a3 KG |
6440 | ;; If it's a multi method, the file name structure contains |
6441 | ;; arrays of method, user and host. | |
6442 | (tramp-dissect-multi-file-name name) | |
c62c9d08 | 6443 | ;; Normal method. First, find out default method. |
4007ba5b | 6444 | (unless match (error "Not a tramp file name: %s" name)) |
c62c9d08 KG |
6445 | (let ((user (match-string (nth 2 tramp-file-name-structure) name)) |
6446 | (host (match-string (nth 3 tramp-file-name-structure) name)) | |
7432277c | 6447 | (localname (match-string (nth 4 tramp-file-name-structure) name))) |
c62c9d08 KG |
6448 | (make-tramp-file-name |
6449 | :multi-method nil | |
6450 | :method method | |
6451 | :user (or user nil) | |
6452 | :host host | |
7432277c | 6453 | :localname localname)))))) |
c62c9d08 KG |
6454 | |
6455 | (defun tramp-find-default-method (user host) | |
6456 | "Look up the right method to use in `tramp-default-method-alist'." | |
6457 | (let ((choices tramp-default-method-alist) | |
6458 | (method tramp-default-method) | |
6459 | item) | |
6460 | (while choices | |
6461 | (setq item (pop choices)) | |
16674e4f | 6462 | (when (and (string-match (nth 0 item) (or host "")) |
c62c9d08 KG |
6463 | (string-match (nth 1 item) (or user ""))) |
6464 | (setq method (nth 2 item)) | |
6465 | (setq choices nil))) | |
6466 | method)) | |
16674e4f KG |
6467 | |
6468 | (defun tramp-find-method (multi-method method user host) | |
6469 | "Return the right method string to use. | |
6470 | This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil. | |
6471 | If both MULTI-METHOD and METHOD are nil, do a lookup in | |
6472 | `tramp-default-method-alist'." | |
6473 | (or multi-method method (tramp-find-default-method user host))) | |
7432277c | 6474 | |
fb7933a3 KG |
6475 | ;; HHH: Not Changed. Multi method. Will probably not handle the case where |
6476 | ;; a user name is not provided in the "file name" very well. | |
6477 | (defun tramp-dissect-multi-file-name (name) | |
6478 | "Not implemented yet." | |
6479 | (let ((regexp (nth 0 tramp-multi-file-name-structure)) | |
6480 | (method-index (nth 1 tramp-multi-file-name-structure)) | |
6481 | (hops-index (nth 2 tramp-multi-file-name-structure)) | |
7432277c | 6482 | (localname-index (nth 3 tramp-multi-file-name-structure)) |
fb7933a3 KG |
6483 | (hop-regexp (nth 0 tramp-multi-file-name-hop-structure)) |
6484 | (hop-method-index (nth 1 tramp-multi-file-name-hop-structure)) | |
6485 | (hop-user-index (nth 2 tramp-multi-file-name-hop-structure)) | |
6486 | (hop-host-index (nth 3 tramp-multi-file-name-hop-structure)) | |
7432277c | 6487 | method hops len hop-methods hop-users hop-hosts localname) |
fb7933a3 KG |
6488 | (unless (string-match (format regexp hop-regexp) name) |
6489 | (error "Not a multi tramp file name: %s" name)) | |
6490 | (setq method (match-string method-index name)) | |
6491 | (setq hops (match-string hops-index name)) | |
6492 | (setq len (/ (length (match-data t)) 2)) | |
7432277c KG |
6493 | (when (< localname-index 0) (incf localname-index len)) |
6494 | (setq localname (match-string localname-index name)) | |
fb7933a3 KG |
6495 | (let ((index 0)) |
6496 | (while (string-match hop-regexp hops index) | |
6497 | (setq index (match-end 0)) | |
6498 | (setq hop-methods | |
6499 | (cons (match-string hop-method-index hops) hop-methods)) | |
6500 | (setq hop-users | |
6501 | (cons (match-string hop-user-index hops) hop-users)) | |
6502 | (setq hop-hosts | |
6503 | (cons (match-string hop-host-index hops) hop-hosts)))) | |
6504 | (make-tramp-file-name | |
6505 | :multi-method method | |
6506 | :method (apply 'vector (reverse hop-methods)) | |
6507 | :user (apply 'vector (reverse hop-users)) | |
6508 | :host (apply 'vector (reverse hop-hosts)) | |
7432277c | 6509 | :localname localname))) |
fb7933a3 | 6510 | |
7432277c KG |
6511 | (defun tramp-make-tramp-file-name (multi-method method user host localname) |
6512 | "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME." | |
fb7933a3 | 6513 | (if multi-method |
7432277c | 6514 | (tramp-make-tramp-multi-file-name multi-method method user host localname) |
16674e4f KG |
6515 | (format-spec |
6516 | (concat tramp-prefix-format | |
6517 | (when method (concat "%m" tramp-postfix-single-method-format)) | |
6518 | (when user (concat "%u" tramp-postfix-user-format)) | |
6519 | (when host (concat "%h" tramp-postfix-host-format)) | |
7432277c KG |
6520 | (when localname (concat "%p"))) |
6521 | `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname))))) | |
fb7933a3 KG |
6522 | |
6523 | ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done | |
6524 | ;; with this when USER is nil? | |
7432277c | 6525 | (defun tramp-make-tramp-multi-file-name (multi-method method user host localname) |
fb7933a3 KG |
6526 | "Constructs a tramp file name for a multi-hop method." |
6527 | (unless tramp-make-multi-tramp-file-format | |
6528 | (error "`tramp-make-multi-tramp-file-format' is nil")) | |
6529 | (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format)) | |
6530 | (hop-format (nth 1 tramp-make-multi-tramp-file-format)) | |
7432277c | 6531 | (localname-format (nth 2 tramp-make-multi-tramp-file-format)) |
ac474af1 | 6532 | (prefix (format-spec prefix-format `((?m . ,multi-method)))) |
fb7933a3 | 6533 | (hops "") |
7432277c | 6534 | (localname (format-spec localname-format `((?p . ,localname)))) |
fb7933a3 KG |
6535 | (i 0) |
6536 | (len (length method))) | |
6537 | (while (< i len) | |
90dc758d KG |
6538 | (let ((m (aref method i)) (u (aref user i)) (h (aref host i))) |
6539 | (setq hops (concat hops (format-spec hop-format | |
ac474af1 | 6540 | `((?m . ,m) (?u . ,u) (?h . ,h))))) |
fb7933a3 | 6541 | (incf i))) |
7432277c | 6542 | (concat prefix hops localname))) |
fb7933a3 | 6543 | |
b25a52cc KG |
6544 | (defun tramp-make-copy-program-file-name (user host localname) |
6545 | "Create a file name suitable to be passed to `rcp' and workalikes." | |
fb7933a3 | 6546 | (if user |
7432277c KG |
6547 | (format "%s@%s:%s" user host localname) |
6548 | (format "%s:%s" host localname))) | |
fb7933a3 | 6549 | |
16674e4f | 6550 | (defun tramp-method-out-of-band-p (multi-method method user host) |
38c65fca | 6551 | "Return t if this is an out-of-band method, nil otherwise." |
c951aecb | 6552 | (tramp-get-method-parameter |
16674e4f | 6553 | multi-method |
91879624 | 6554 | (tramp-find-method multi-method method user host) |
c951aecb | 6555 | user host 'tramp-copy-program)) |
fb7933a3 KG |
6556 | |
6557 | ;; Variables local to connection. | |
6558 | ||
6559 | (defun tramp-get-ls-command (multi-method method user host) | |
6560 | (save-excursion | |
6561 | (tramp-maybe-open-connection multi-method method user host) | |
6562 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
6563 | tramp-ls-command)) | |
6564 | ||
6565 | (defun tramp-get-test-groks-nt (multi-method method user host) | |
6566 | (save-excursion | |
6567 | (tramp-maybe-open-connection multi-method method user host) | |
6568 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
6569 | tramp-test-groks-nt)) | |
6570 | ||
6571 | (defun tramp-get-file-exists-command (multi-method method user host) | |
6572 | (save-excursion | |
6573 | (tramp-maybe-open-connection multi-method method user host) | |
6574 | (set-buffer (tramp-get-buffer multi-method method user host)) | |
6575 | tramp-file-exists-command)) | |
6576 | ||
6577 | (defun tramp-get-remote-perl (multi-method method user host) | |
6578 | (tramp-get-connection-property "perl" nil multi-method method user host)) | |
6579 | ||
6580 | (defun tramp-get-remote-ln (multi-method method user host) | |
6581 | (tramp-get-connection-property "ln" nil multi-method method user host)) | |
6582 | ||
6583 | ;; Get a property of a TRAMP connection. | |
ac474af1 KG |
6584 | (defun tramp-get-connection-property |
6585 | (property default multi-method method user host) | |
fb7933a3 KG |
6586 | "Get the named property for the connection. |
6587 | If the value is not set for the connection, return `default'" | |
6588 | (tramp-maybe-open-connection multi-method method user host) | |
6589 | (with-current-buffer (tramp-get-buffer multi-method method user host) | |
6590 | (let (error) | |
6591 | (condition-case nil | |
6592 | (symbol-value (intern (concat "tramp-connection-property-" property))) | |
6593 | (error default))))) | |
6594 | ||
6595 | ;; Set a property of a TRAMP connection. | |
ac474af1 KG |
6596 | (defun tramp-set-connection-property |
6597 | (property value multi-method method user host) | |
fb7933a3 KG |
6598 | "Set the named property of a TRAMP connection." |
6599 | (tramp-maybe-open-connection multi-method method user host) | |
6600 | (with-current-buffer (tramp-get-buffer multi-method method user host) | |
6601 | (set (make-local-variable | |
6602 | (intern (concat "tramp-connection-property-" property))) | |
6603 | value))) | |
6604 | ||
ac474af1 | 6605 | ;; Some predefined connection properties. |
16674e4f KG |
6606 | (defun tramp-set-remote-encoding (multi-method method user host rem-enc) |
6607 | (tramp-set-connection-property "remote-encoding" rem-enc | |
ac474af1 | 6608 | multi-method method user host)) |
16674e4f KG |
6609 | (defun tramp-get-remote-encoding (multi-method method user host) |
6610 | (tramp-get-connection-property "remote-encoding" nil | |
ac474af1 | 6611 | multi-method method user host)) |
16674e4f KG |
6612 | |
6613 | (defun tramp-set-remote-decoding (multi-method method user host rem-dec) | |
6614 | (tramp-set-connection-property "remote-decoding" rem-dec | |
ac474af1 | 6615 | multi-method method user host)) |
16674e4f KG |
6616 | (defun tramp-get-remote-decoding (multi-method method user host) |
6617 | (tramp-get-connection-property "remote-decoding" nil | |
ac474af1 | 6618 | multi-method method user host)) |
16674e4f KG |
6619 | |
6620 | (defun tramp-set-local-encoding (multi-method method user host loc-enc) | |
6621 | (tramp-set-connection-property "local-encoding" loc-enc | |
ac474af1 | 6622 | multi-method method user host)) |
16674e4f KG |
6623 | (defun tramp-get-local-encoding (multi-method method user host) |
6624 | (tramp-get-connection-property "local-encoding" nil | |
ac474af1 | 6625 | multi-method method user host)) |
16674e4f KG |
6626 | |
6627 | (defun tramp-set-local-decoding (multi-method method user host loc-dec) | |
6628 | (tramp-set-connection-property "local-decoding" loc-dec | |
ac474af1 | 6629 | multi-method method user host)) |
16674e4f KG |
6630 | (defun tramp-get-local-decoding (multi-method method user host) |
6631 | (tramp-get-connection-property "local-decoding" nil | |
ac474af1 | 6632 | multi-method method user host)) |
fb7933a3 | 6633 | |
c951aecb KG |
6634 | (defun tramp-get-method-parameter (multi-method method user host param) |
6635 | "Return the method parameter PARAM. | |
6636 | If the `tramp-methods' entry does not exist, use the variable PARAM | |
6637 | as default." | |
6638 | (unless (boundp param) | |
6639 | (error "Non-existing method parameter `%s'" param)) | |
6640 | (let ((entry (assoc param | |
90f8dc03 KG |
6641 | (assoc (tramp-find-method multi-method method user host) |
6642 | tramp-methods)))) | |
c951aecb KG |
6643 | (if entry |
6644 | (second entry) | |
6645 | (symbol-value param)))) | |
6646 | ||
90f8dc03 | 6647 | |
fb7933a3 KG |
6648 | ;; Auto saving to a special directory. |
6649 | ||
6650 | (defun tramp-make-auto-save-file-name (fn) | |
6651 | "Returns a file name in `tramp-auto-save-directory' for autosaving this file." | |
6652 | (when tramp-auto-save-directory | |
6653 | (unless (file-exists-p tramp-auto-save-directory) | |
6654 | (make-directory tramp-auto-save-directory t))) | |
6655 | ;; jka-compr doesn't like auto-saving, so by appending "~" to the | |
6656 | ;; file name we make sure that jka-compr isn't used for the | |
6657 | ;; auto-save file. | |
6658 | (let ((buffer-file-name (expand-file-name | |
6659 | (tramp-subst-strs-in-string '(("_" . "|") | |
6660 | ("/" . "_a") | |
6661 | (":" . "_b") | |
6662 | ("|" . "__") | |
6663 | ("[" . "_l") | |
6664 | ("]" . "_r")) | |
6665 | fn) | |
6666 | tramp-auto-save-directory))) | |
6667 | (make-auto-save-file-name))) | |
6668 | ||
6669 | (defadvice make-auto-save-file-name | |
6670 | (around tramp-advice-make-auto-save-file-name () activate) | |
6671 | "Invoke `tramp-make-auto-save-file-name' for tramp files." | |
6672 | (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)) | |
6673 | tramp-auto-save-directory) | |
6674 | (setq ad-return-value | |
6675 | (tramp-make-auto-save-file-name (buffer-file-name))) | |
6676 | ad-do-it)) | |
6677 | ||
6678 | (defun tramp-subst-strs-in-string (alist string) | |
6679 | "Replace all occurrences of the string FROM with TO in STRING. | |
6680 | ALIST is of the form ((FROM . TO) ...)." | |
6681 | (save-match-data | |
6682 | (while alist | |
6683 | (let* ((pr (car alist)) | |
6684 | (from (car pr)) | |
6685 | (to (cdr pr))) | |
6686 | (while (string-match (regexp-quote from) string) | |
6687 | (setq string (replace-match to t t string))) | |
6688 | (setq alist (cdr alist)))) | |
6689 | string)) | |
6690 | ||
6691 | (defun tramp-insert-with-face (face string) | |
6692 | "Insert text with a specific face." | |
6693 | (let ((start (point))) | |
6694 | (insert string) | |
6695 | (add-text-properties start (point) (list 'face face)))) | |
6696 | ||
6697 | ;; ------------------------------------------------------------ | |
6698 | ;; -- Compatibility functions section -- | |
6699 | ;; ------------------------------------------------------------ | |
6700 | ||
6701 | (defun tramp-temporary-file-directory () | |
6702 | "Return name of directory for temporary files (compat function). | |
6703 | For Emacs, this is the variable `temporary-file-directory', for XEmacs | |
6704 | this is the function `temp-directory'." | |
6705 | (cond ((boundp 'temporary-file-directory) | |
6706 | (symbol-value 'temporary-file-directory)) | |
6707 | ((fboundp 'temp-directory) | |
6708 | (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler | |
6709 | ((let ((d (getenv "TEMP"))) (and d (file-directory-p d))) | |
6710 | (file-name-as-directory (getenv "TEMP"))) | |
6711 | ((let ((d (getenv "TMP"))) (and d (file-directory-p d))) | |
6712 | (file-name-as-directory (getenv "TMP"))) | |
6713 | ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d))) | |
6714 | (file-name-as-directory (getenv "TMPDIR"))) | |
6715 | ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp")) | |
6716 | (t (message (concat "Neither `temporary-file-directory' nor " | |
6717 | "`temp-directory' is defined -- using /tmp.")) | |
6718 | (file-name-as-directory "/tmp")))) | |
6719 | ||
6720 | (defun tramp-read-passwd (prompt) | |
6721 | "Read a password from user (compat function). | |
5ec2cc41 KG |
6722 | Invokes `password-read' if available, `read-passwd' else." |
6723 | (if (functionp 'password-read) | |
6724 | (let* ((user (or tramp-current-user (user-login-name))) | |
6725 | (host (or tramp-current-host (system-name))) | |
38c65fca KG |
6726 | (key (if (and (stringp user) (stringp host)) |
6727 | (concat user "@" host) | |
6728 | (concat "[" (mapconcat 'identity user "/") "]@[" | |
6729 | (mapconcat 'identity host "/") "]"))) | |
5ec2cc41 KG |
6730 | (password (apply #'password-read (list prompt key)))) |
6731 | (apply #'password-cache-add (list key password)) | |
6732 | password) | |
6733 | (read-passwd prompt))) | |
6734 | ||
6735 | (defun tramp-clear-passwd (&optional user host) | |
6736 | "Clear password cache for connection related to current-buffer." | |
6737 | (interactive) | |
6738 | (let ((filename (or buffer-file-name list-buffers-directory ""))) | |
6739 | (when (and (functionp 'password-cache-remove) | |
6740 | (or (and user host) (tramp-tramp-file-p filename))) | |
6741 | (let* ((v (when (tramp-tramp-file-p filename) | |
6742 | (tramp-dissect-file-name filename))) | |
6743 | (luser (or user (tramp-file-name-user v) (user-login-name))) | |
6744 | (lhost (or host (tramp-file-name-host v) (system-name))) | |
6745 | (key (concat luser "@" lhost))) | |
6746 | (apply #'password-cache-remove (list key)))))) | |
fb7933a3 KG |
6747 | |
6748 | (defun tramp-time-diff (t1 t2) | |
6749 | "Return the difference between the two times, in seconds. | |
6750 | T1 and T2 are time values (as returned by `current-time' for example). | |
6751 | ||
6752 | NOTE: This function will fail if the time difference is too large to | |
6753 | fit in an integer." | |
6754 | ;; Pacify byte-compiler with `symbol-function'. | |
ea9d1443 KG |
6755 | (cond ((and (fboundp 'subtract-time) |
6756 | (fboundp 'float-time)) | |
6757 | (funcall (symbol-function 'float-time) | |
6758 | (funcall (symbol-function 'subtract-time) t1 t2))) | |
6759 | ((and (fboundp 'subtract-time) | |
6760 | (fboundp 'time-to-seconds)) | |
6761 | (funcall (symbol-function 'time-to-seconds) | |
6762 | (funcall (symbol-function 'subtract-time) t1 t2))) | |
fb7933a3 KG |
6763 | ((fboundp 'itimer-time-difference) |
6764 | (floor (funcall | |
6765 | (symbol-function 'itimer-time-difference) | |
6766 | (if (< (length t1) 3) (append t1 '(0)) t1) | |
6767 | (if (< (length t2) 3) (append t2 '(0)) t2)))) | |
6768 | (t | |
ea9d1443 KG |
6769 | ;; snarfed from Emacs 21 time-date.el; combining |
6770 | ;; time-to-seconds and subtract-time | |
6771 | (let ((time (let ((borrow (< (cadr t1) (cadr t2)))) | |
fb7933a3 | 6772 | (list (- (car t1) (car t2) (if borrow 1 0)) |
ea9d1443 KG |
6773 | (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))) |
6774 | (+ (* (car time) 65536.0) | |
6775 | (cadr time) | |
6776 | (/ (or (nth 2 time) 0) 1000000.0)))))) | |
fb7933a3 KG |
6777 | |
6778 | (defun tramp-coding-system-change-eol-conversion (coding-system eol-type) | |
6779 | "Return a coding system like CODING-SYSTEM but with given EOL-TYPE. | |
6780 | EOL-TYPE can be one of `dos', `unix', or `mac'." | |
6781 | (cond ((fboundp 'coding-system-change-eol-conversion) | |
6782 | (apply #'coding-system-change-eol-conversion | |
6783 | (list coding-system eol-type))) | |
6784 | ((fboundp 'subsidiary-coding-system) | |
6785 | (apply | |
6786 | #'subsidiary-coding-system | |
6787 | (list coding-system | |
6788 | (cond ((eq eol-type 'dos) 'crlf) | |
6789 | ((eq eol-type 'unix) 'lf) | |
6790 | ((eq eol-type 'mac) 'cr) | |
6791 | (t | |
6792 | (error "Unknown EOL-TYPE `%s', must be %s" | |
6793 | eol-type | |
6794 | "`dos', `unix', or `mac'")))))) | |
6795 | (t (error "Can't change EOL conversion -- is MULE missing?")))) | |
6796 | ||
6797 | (defun tramp-split-string (string pattern) | |
6798 | "Like `split-string' but omit empty strings. | |
6799 | In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\"). | |
6800 | This is, the first, empty, element is omitted. In XEmacs, the first | |
6801 | element is not omitted. | |
6802 | ||
6803 | Note: this function has been written for `tramp-handle-file-truename'. | |
6804 | If you want to use it for something else, you'll have to check whether | |
6805 | it does the right thing." | |
6806 | (delete "" (split-string string pattern))) | |
6807 | ||
19a87064 MA |
6808 | (defun tramp-set-process-query-on-exit-flag (process flag) |
6809 | "Specify if query is needed for process when Emacs is exited. | |
6810 | If the second argument flag is non-nil, Emacs will query the user before | |
6811 | exiting if process is running." | |
6812 | (if (fboundp 'set-process-query-on-exit-flag) | |
6813 | (set-process-query-on-exit-flag process flag) | |
6814 | (funcall (symbol-function 'process-kill-without-query) | |
6815 | process flag))) | |
6816 | ||
19a87064 | 6817 | |
7432277c KG |
6818 | ;; ------------------------------------------------------------ |
6819 | ;; -- Kludges section -- | |
6820 | ;; ------------------------------------------------------------ | |
fb7933a3 KG |
6821 | |
6822 | ;; Currently (as of Emacs 20.5), the function `shell-quote-argument' | |
6823 | ;; does not deal well with newline characters. Newline is replaced by | |
6824 | ;; backslash newline. But if, say, the string `a backslash newline b' | |
6825 | ;; is passed to a shell, the shell will expand this into "ab", | |
6826 | ;; completely omitting the newline. This is not what was intended. | |
6827 | ;; It does not appear to be possible to make the function | |
6828 | ;; `shell-quote-argument' work with newlines without making it | |
6829 | ;; dependent on the shell used. But within this package, we know that | |
6830 | ;; we will always use a Bourne-like shell, so we use an approach which | |
6831 | ;; groks newlines. | |
6832 | ;; | |
6833 | ;; The approach is simple: we call `shell-quote-argument', then | |
6834 | ;; massage the newline part of the result. | |
6835 | ;; | |
6836 | ;; This function should produce a string which is grokked by a Unix | |
6837 | ;; shell, even if the Emacs is running on Windows. Since this is the | |
6838 | ;; kludges section, we bind `system-type' in such a way that | |
6839 | ;; `shell-quote-arguments' behaves as if on Unix. | |
6840 | ;; | |
6841 | ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this | |
6842 | ;; function to work with Bourne-like shells. | |
6843 | ;; | |
6844 | ;; CCC: This function should be rewritten so that | |
6845 | ;; `shell-quote-argument' is not used. This way, we are safe from | |
6846 | ;; changes in `shell-quote-argument'. | |
6847 | (defun tramp-shell-quote-argument (s) | |
6848 | "Similar to `shell-quote-argument', but groks newlines. | |
6849 | Only works for Bourne-like shells." | |
6850 | (let ((system-type 'not-windows)) | |
6851 | (save-match-data | |
6852 | (let ((result (shell-quote-argument s)) | |
6853 | (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line)))) | |
6854 | (when (and (>= (length result) 2) | |
6855 | (string= (substring result 0 2) "\\~")) | |
6856 | (setq result (substring result 1))) | |
6857 | (while (string-match nl result) | |
6858 | (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line) | |
6859 | t t result))) | |
6860 | result)))) | |
6861 | ||
6862 | ;; ;; EFS hooks itself into the file name handling stuff in more places | |
6863 | ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay | |
7432277c | 6864 | ;; ;; away from tramp.el file names. |
fb7933a3 KG |
6865 | ;; ;; |
6866 | ;; ;; This is needed because EFS installs (efs-dired-before-readin) into | |
6867 | ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP | |
6868 | ;; ;; connection to help it's dired process. Not that I have any real | |
6869 | ;; ;; idea *why* this is helpful to dired. | |
6870 | ;; ;; | |
6871 | ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :) | |
6872 | ;; ;; | |
6873 | ;; ;; Daniel Pittman <daniel@danann.net> | |
6874 | ;; ;; | |
6875 | ;; ;; CCC: when the other defadvice calls have disappeared, make sure | |
6876 | ;; ;; not to call defadvice unless it's necessary. How do we find out whether | |
6877 | ;; ;; it is necessary? (featurep 'efs) is surely the wrong way -- | |
6878 | ;; ;; EFS might nicht be loaded yet. | |
7432277c KG |
6879 | ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect) |
6880 | ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname." | |
fb7933a3 KG |
6881 | ;; (if (tramp-tramp-file-p (ad-get-arg 0)) |
6882 | ;; nil | |
6883 | ;; ad-do-it)) | |
6884 | ||
16674e4f KG |
6885 | ;; We currently (sometimes) use "[" and "]" in the filename format. |
6886 | ;; This means that Emacs wants to expand wildcards if | |
fb7933a3 KG |
6887 | ;; `find-file-wildcards' is non-nil, and then barfs because no |
6888 | ;; expansion could be found. We detect this situation and do | |
6889 | ;; something really awful: we have `file-expand-wildcards' return the | |
6890 | ;; original filename if it can't expand anything. Let's just hope | |
6891 | ;; that this doesn't break anything else. | |
16674e4f KG |
6892 | ;; CCC: This check is now also really awful; we should search all |
6893 | ;; of the filename format, not just the prefix. | |
6894 | (when (string-match "\\[" tramp-prefix-format) | |
fb7933a3 KG |
6895 | (defadvice file-expand-wildcards (around tramp-fix activate) |
6896 | (let ((name (ad-get-arg 0))) | |
6897 | (if (tramp-tramp-file-p name) | |
6898 | ;; If it's a Tramp file, dissect it and look if wildcards | |
6899 | ;; need to be expanded at all. | |
6900 | (let ((v (tramp-dissect-file-name name))) | |
7432277c | 6901 | (if (string-match "[[*?]" (tramp-file-name-localname v)) |
fb7933a3 KG |
6902 | (let ((res ad-do-it)) |
6903 | (setq ad-return-value (or res (list name)))) | |
6904 | (setq ad-return-value (list name)))) | |
6905 | ;; If it is not a Tramp file, just run the original function. | |
6906 | (let ((res ad-do-it)) | |
6907 | (setq ad-return-value (or res (list name))))))) | |
16674e4f | 6908 | ) |
fb7933a3 | 6909 | |
ac474af1 KG |
6910 | ;; Tramp version is useful in a number of situations. |
6911 | ||
6912 | (defun tramp-version (arg) | |
6913 | "Print version number of tramp.el in minibuffer or current buffer." | |
6914 | (interactive "P") | |
6915 | (if arg (insert tramp-version) (message tramp-version))) | |
6916 | ||
fb7933a3 KG |
6917 | ;; Make the `reporter` functionality available for making bug reports about |
6918 | ;; the package. A most useful piece of code. | |
6919 | ||
6920 | (unless (fboundp 'reporter-submit-bug-report) | |
6921 | (autoload 'reporter-submit-bug-report "reporter")) | |
6922 | ||
6923 | (defun tramp-bug () | |
6924 | "Submit a bug report to the TRAMP developers." | |
6925 | (interactive) | |
6926 | (require 'reporter) | |
6927 | (let ((reporter-prompt-for-summary-p t)) | |
6928 | (reporter-submit-bug-report | |
6929 | tramp-bug-report-address ; to-address | |
6930 | (format "tramp (%s)" tramp-version) ; package name and version | |
6931 | `(;; Current state | |
6932 | tramp-ls-command | |
6933 | tramp-test-groks-nt | |
6934 | tramp-file-exists-command | |
6935 | tramp-current-multi-method | |
6936 | tramp-current-method | |
6937 | tramp-current-user | |
6938 | tramp-current-host | |
6939 | ||
6940 | ;; System defaults | |
6941 | tramp-auto-save-directory ; vars to dump | |
6942 | tramp-default-method | |
6943 | tramp-rsh-end-of-line | |
90f8dc03 | 6944 | tramp-default-password-end-of-line |
fb7933a3 KG |
6945 | tramp-remote-path |
6946 | tramp-login-prompt-regexp | |
6947 | tramp-password-prompt-regexp | |
6948 | tramp-wrong-passwd-regexp | |
fabf2143 | 6949 | tramp-yesno-prompt-regexp |
3cdaec13 | 6950 | tramp-yn-prompt-regexp |
38c65fca KG |
6951 | tramp-terminal-prompt-regexp |
6952 | tramp-out-of-band-prompt-regexp | |
fb7933a3 KG |
6953 | tramp-temp-name-prefix |
6954 | tramp-file-name-structure | |
6955 | tramp-file-name-regexp | |
6956 | tramp-multi-file-name-structure | |
6957 | tramp-multi-file-name-hop-structure | |
6958 | tramp-multi-methods | |
6959 | tramp-multi-connection-function-alist | |
16674e4f | 6960 | tramp-methods |
fb7933a3 | 6961 | tramp-end-of-output |
fabf2143 KG |
6962 | tramp-coding-commands |
6963 | tramp-actions-before-shell | |
38c65fca | 6964 | tramp-actions-copy-out-of-band |
fabf2143 | 6965 | tramp-multi-actions |
685f5858 | 6966 | tramp-terminal-type |
821e6e36 | 6967 | tramp-shell-prompt-pattern |
7432277c | 6968 | tramp-chunksize |
38c65fca KG |
6969 | ,(when (boundp 'tramp-backup-directory-alist) |
6970 | 'tramp-backup-directory-alist) | |
6971 | ,(when (boundp 'tramp-bkup-backup-directory-info) | |
6972 | 'tramp-bkup-backup-directory-info) | |
fb7933a3 KG |
6973 | |
6974 | ;; Non-tramp variables of interest | |
6975 | shell-prompt-pattern | |
6976 | backup-by-copying | |
6977 | backup-by-copying-when-linked | |
6978 | backup-by-copying-when-mismatch | |
6979 | ,(when (boundp 'backup-by-copying-when-privileged-mismatch) | |
6980 | 'backup-by-copying-when-privileged-mismatch) | |
38c65fca KG |
6981 | ,(when (boundp 'password-cache) |
6982 | 'password-cache) | |
6983 | ,(when (boundp 'password-cache-expiry) | |
6984 | 'password-cache-expiry) | |
6985 | ,(when (boundp 'backup-directory-alist) | |
6986 | 'backup-directory-alist) | |
6987 | ,(when (boundp 'bkup-backup-directory-info) | |
6988 | 'bkup-backup-directory-info) | |
fb7933a3 KG |
6989 | file-name-handler-alist) |
6990 | nil ; pre-hook | |
6991 | nil ; post-hook | |
6992 | "\ | |
6993 | Enter your bug report in this message, including as much detail as you | |
6994 | possibly can about the problem, what you did to cause it and what the | |
6995 | local and remote machines are. | |
6996 | ||
6997 | If you can give a simple set of instructions to make this bug happen | |
6998 | reliably, please include those. Thank you for helping kill bugs in | |
6999 | TRAMP. | |
89509ea0 KG |
7000 | |
7001 | Another useful thing to do is to put (setq tramp-debug-buffer t) in | |
7002 | the ~/.emacs file and to repeat the bug. Then, include the contents | |
7003 | of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug | |
7004 | report. | |
7005 | ||
fabf2143 KG |
7006 | --bug report follows this line-- |
7007 | "))) | |
fb7933a3 KG |
7008 | |
7009 | (defalias 'tramp-submit-bug 'tramp-bug) | |
7010 | ||
7011 | (provide 'tramp) | |
7012 | ||
7013 | ;; Make sure that we get integration with the VC package. | |
7014 | ;; When it is loaded, we need to pull in the integration module. | |
7015 | ;; This must come after (provide 'tramp) because tramp-vc.el | |
7016 | ;; requires tramp. | |
7017 | (eval-after-load "vc" | |
7018 | '(require 'tramp-vc)) | |
7019 | ||
7020 | ;;; TODO: | |
7021 | ||
4007ba5b KG |
7022 | ;; * Allow putting passwords in the filename. |
7023 | ;; This should be implemented via a general mechanism to add | |
7024 | ;; parameters in filenames. There is currently a kludge for | |
7025 | ;; putting the port number into the filename for ssh and ftp | |
7026 | ;; files. This could be subsumed by the new mechanism as well. | |
7027 | ;; Another approach is to read a netrc file like ~/.authinfo | |
7028 | ;; from Gnus. | |
7029 | ;; * Handle nonlocal exits such as C-g. | |
16674e4f | 7030 | ;; * Autodetect if remote `ls' groks the "--dired" switch. |
b1d06e75 KG |
7031 | ;; * Add fallback for inline encodings. This should be used |
7032 | ;; if the remote end doesn't support mimencode or a similar program. | |
7033 | ;; For reading files from the remote host, we can just parse the output | |
7034 | ;; of `od -b'. For writing files to the remote host, we construct | |
7035 | ;; a shell program which contains only "safe" ascii characters | |
7036 | ;; and which writes the right bytes to the file. We can use printf(1) | |
7037 | ;; or "echo -e" or the printf function in awk and use octal escapes | |
7038 | ;; for the "dangerous" characters. The null byte might be a problem. | |
7039 | ;; On some systems, the octal escape doesn't work. So we try the following | |
7040 | ;; two commands to write a null byte: | |
7041 | ;; dd if=/dev/zero bs=1 count=1 | |
7042 | ;; echo | tr '\n' '\000' | |
16674e4f KG |
7043 | ;; * Separate local `tramp-coding-commands' from remote ones. Connect |
7044 | ;; the two via a format which can be `uu' or `b64'. Then we can search | |
7045 | ;; for the right local commands and the right remote commands separately. | |
fb7933a3 KG |
7046 | ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't |
7047 | ;; work for remote files. | |
fb7933a3 | 7048 | ;; * Rewrite `tramp-shell-quote-argument' to abstain from using |
b1d06e75 | 7049 | ;; `shell-quote-argument'. |
fb7933a3 | 7050 | ;; * Completion gets confused when you leave out the method name. |
fb7933a3 KG |
7051 | ;; * In Emacs 21, `insert-directory' shows total number of bytes used |
7052 | ;; by the files in that directory. Add this here. | |
7053 | ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman) | |
7054 | ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman) | |
7055 | ;; * When logging in, keep looking for questions according to an alist | |
7056 | ;; and then invoke the right function. | |
7057 | ;; * Case-insensitive filename completion. (Norbert Goevert.) | |
7058 | ;; * Running CVS remotely doesn't appear to work right. It thinks | |
7059 | ;; files are locked by somebody else even if I'm the locking user. | |
7060 | ;; Sometimes, one gets `No CVSROOT specified' errors from CVS. | |
7061 | ;; (Skip Montanaro) | |
7062 | ;; * Don't use globbing for directories with many files, as this is | |
7063 | ;; likely to produce long command lines, and some shells choke on | |
7064 | ;; long command lines. | |
fb7933a3 KG |
7065 | ;; * Find out about the new auto-save mechanism in Emacs 21 and |
7066 | ;; do the right thing. | |
7067 | ;; * `vc-directory' does not work. It never displays any files, even | |
7068 | ;; if it does show files when run locally. | |
7069 | ;; * Allow correction of passwords, if the remote end allows this. | |
7070 | ;; (Mark Hershberger) | |
fb7933a3 KG |
7071 | ;; * How to deal with MULE in `insert-file-contents' and `write-region'? |
7072 | ;; * Do asynchronous `shell-command's. | |
7073 | ;; * Grok `append' parameter for `write-region'. | |
7074 | ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'? | |
7075 | ;; * abbreviate-file-name | |
7076 | ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>) | |
fb7933a3 KG |
7077 | ;; * Also allow to omit user names when doing multi-hop. Not sure yet |
7078 | ;; what the user names should default to, though. | |
7079 | ;; * better error checking. At least whenever we see something | |
7080 | ;; strange when doing zerop, we should kill the process and start | |
7081 | ;; again. (Greg Stark) | |
7082 | ;; * Add caching for filename completion. (Greg Stark) | |
7432277c | 7083 | ;; Of course, this has issues with usability (stale cache bites) |
fb7933a3 KG |
7084 | ;; -- <daniel@danann.net> |
7085 | ;; * Provide a local cache of old versions of remote files for the rsync | |
7086 | ;; transfer method to use. (Greg Stark) | |
7087 | ;; * Remove unneeded parameters from methods. | |
7088 | ;; * Invoke rsync once for copying a whole directory hierarchy. | |
83bbd71b | 7089 | ;; (Francesco Potort\e,Al\e(B) |
fb7933a3 KG |
7090 | ;; * Should we set PATH ourselves or should we rely on the remote end |
7091 | ;; to do it? | |
fb7933a3 | 7092 | ;; * Make it work for XEmacs 20, which is missing `with-timeout'. |
fb7933a3 KG |
7093 | ;; * Make it work for different encodings, and for different file name |
7094 | ;; encodings, too. (Daniel Pittman) | |
7095 | ;; * Change applicable functions to pass a struct tramp-file-name rather | |
7432277c | 7096 | ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME. |
fb7933a3 KG |
7097 | ;; * Implement asynchronous shell commands. |
7098 | ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman) | |
7099 | ;; * Progress reports while copying files. (Michael Kifer) | |
7100 | ;; * `Smart' connection method that uses inline for small and out of | |
7101 | ;; band for large files. (Michael Kifer) | |
7102 | ;; * Don't search for perl5 and perl. Instead, only search for perl and | |
7103 | ;; then look if it's the right version (with `perl -v'). | |
7104 | ;; * When editing a remote CVS controlled file as a different user, VC | |
7105 | ;; gets confused about the file locking status. Try to find out why | |
7106 | ;; the workaround doesn't work. | |
fb7933a3 KG |
7107 | ;; * Change `copy-file' to grok the case where the filename handler |
7108 | ;; for the source and the target file are different. Right now, | |
7109 | ;; it looks at the source file and then calls that handler, if | |
7110 | ;; there is one. But since ange-ftp, for instance, does not know | |
7111 | ;; about Tramp, it does not do the right thing if the target file | |
7112 | ;; name is a Tramp name. | |
3cdaec13 | 7113 | ;; * Username and hostname completion. |
16674e4f KG |
7114 | ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to |
7115 | ;; connect to host "blabla" already if that host is unique. No idea | |
7116 | ;; how to suppress. Maybe not an essential problem. | |
16674e4f | 7117 | ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'. |
16674e4f | 7118 | ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords. |
8daea7fc | 7119 | ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'. |
16674e4f KG |
7120 | ;; Code is nearly identical. |
7121 | ;; ** Decide whiche files to take for searching user/host names depending on | |
7122 | ;; operating system (windows-nt) in `tramp-completion-function-alist'. | |
7123 | ;; ** Enhance variables for debug. | |
7124 | ;; ** Implement "/multi:" completion. | |
7125 | ;; ** Add a learning mode for completion. Make results persistent. | |
c951aecb | 7126 | ;; * Allow out-of-band methods as _last_ multi-hop. |
fb7933a3 KG |
7127 | |
7128 | ;; Functions for file-name-handler-alist: | |
7129 | ;; diff-latest-backup-file -- in diff.el | |
fb7933a3 KG |
7130 | ;; dired-uncache -- this will be needed when we do insert-directory caching |
7131 | ;; file-name-as-directory -- use primitive? | |
fb7933a3 | 7132 | ;; file-name-sans-versions -- use primitive? |
fb7933a3 | 7133 | ;; get-file-buffer -- use primitive |
fb7933a3 KG |
7134 | ;; vc-registered |
7135 | ||
ab5796a9 | 7136 | ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a |
fb7933a3 | 7137 | ;;; tramp.el ends here |