]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Tramp's main Emacs version for development is Emacs 24. This
27 ;; package provides compatibility functions for Emacs 22, Emacs 23,
28 ;; XEmacs 21.4+ and SXEmacs 22.
29
30 ;;; Code:
31
32 (eval-when-compile
33
34 ;; Pacify byte-compiler.
35 (require 'cl))
36
37 (eval-and-compile
38
39 ;; Some packages must be required for XEmacs, because we compile
40 ;; with -no-autoloads.
41 (when (featurep 'xemacs)
42 (require 'cus-edit)
43 (require 'env)
44 (require 'executable)
45 (require 'outline)
46 (require 'passwd)
47 (require 'pp)
48 (require 'regexp-opt))
49
50 (require 'advice)
51 (require 'custom)
52 (require 'format-spec)
53 (require 'shell)
54
55 (require 'tramp-loaddefs)
56
57 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
58 ;; mandatory.
59 (if (featurep 'xemacs)
60 (load "password" 'noerror)
61 (or (require 'password-cache nil 'noerror)
62 (require 'password nil 'noerror))) ; Part of contrib.
63
64 ;; auth-source is relatively new.
65 (if (featurep 'xemacs)
66 (load "auth-source" 'noerror)
67 (require 'auth-source nil 'noerror))
68
69 ;; Load the appropriate timer package.
70 (if (featurep 'xemacs)
71 (require 'timer-funcs)
72 (require 'timer))
73
74 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
75 ;; Currently, XEmacs supports this.
76 (when (featurep 'xemacs)
77 (unless (boundp 'byte-compile-default-warnings)
78 (defvar byte-compile-default-warnings nil))
79 (delq 'unused-vars byte-compile-default-warnings))
80
81 ;; `last-coding-system-used' is unknown in XEmacs.
82 (unless (boundp 'last-coding-system-used)
83 (defvar last-coding-system-used nil))
84
85 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
86 ;; used in XEmacs, so we set it here and there. The following is
87 ;; needed to pacify Emacs byte-compiler.
88 ;; Note that it was removed altogether in Emacs 24.1.
89 (when (boundp 'directory-sep-char)
90 (defvar byte-compile-not-obsolete-var nil)
91 (setq byte-compile-not-obsolete-var 'directory-sep-char)
92 ;; Emacs 23.2.
93 (defvar byte-compile-not-obsolete-vars nil)
94 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
95
96 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs 24.1.
97 ;; Besides `t', `nil', and integer, we use also timestamps (as
98 ;; returned by `current-time') internally.
99 (defvar remote-file-name-inhibit-cache nil)
100
101 ;; For not existing functions, or functions with a changed argument
102 ;; list, there are compiler warnings. We want to avoid them in
103 ;; cases we know what we do.
104 (defmacro tramp-compat-funcall (function &rest arguments)
105 (if (featurep 'xemacs)
106 `(funcall (symbol-function ,function) ,@arguments)
107 `(when (or (subrp ,function) (functionp ,function))
108 (with-no-warnings (funcall ,function ,@arguments)))))
109
110 ;; `set-buffer-multibyte' comes from Emacs Leim.
111 (unless (fboundp 'set-buffer-multibyte)
112 (defalias 'set-buffer-multibyte 'ignore))
113
114 ;; The following functions cannot be aliases of the corresponding
115 ;; `tramp-handle-*' functions, because this would bypass the locking
116 ;; mechanism.
117
118 ;; `file-remote-p' has been introduced with Emacs 22. The version
119 ;; of XEmacs is not a magic file name function (yet).
120 (unless (fboundp 'file-remote-p)
121 (defalias 'file-remote-p
122 (lambda (file &optional identification connected)
123 (when (tramp-tramp-file-p file)
124 (tramp-compat-funcall
125 'tramp-file-name-handler
126 'file-remote-p file identification connected)))))
127
128 ;; `process-file' does not exist in XEmacs.
129 (unless (fboundp 'process-file)
130 (defalias 'process-file
131 (lambda (program &optional infile buffer display &rest args)
132 (when (tramp-tramp-file-p default-directory)
133 (apply
134 'tramp-file-name-handler
135 'process-file program infile buffer display args)))))
136
137 ;; `start-file-process' is new in Emacs 23.
138 (unless (fboundp 'start-file-process)
139 (defalias 'start-file-process
140 (lambda (name buffer program &rest program-args)
141 (when (tramp-tramp-file-p default-directory)
142 (apply
143 'tramp-file-name-handler
144 'start-file-process name buffer program program-args)))))
145
146 ;; `set-file-times' is also new in Emacs 23.
147 (unless (fboundp 'set-file-times)
148 (defalias 'set-file-times
149 (lambda (filename &optional time)
150 (when (tramp-tramp-file-p filename)
151 (tramp-compat-funcall
152 'tramp-file-name-handler 'set-file-times filename time)))))
153
154 ;; We currently use "[" and "]" in the filename format for IPv6
155 ;; hosts of GNU Emacs. This means that Emacs wants to expand
156 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
157 ;; because no expansion could be found. We detect this situation
158 ;; and do something really awful: we have `file-expand-wildcards'
159 ;; return the original filename if it can't expand anything. Let's
160 ;; just hope that this doesn't break anything else.
161 ;; It is not needed anymore since GNU Emacs 23.2.
162 (unless (or (featurep 'xemacs)
163 ;; `featurep' has only one argument in XEmacs.
164 (funcall 'featurep 'files 'remote-wildcards))
165 (defadvice file-expand-wildcards
166 (around tramp-advice-file-expand-wildcards activate)
167 (let ((name (ad-get-arg 0)))
168 ;; If it's a Tramp file, look if wildcards need to be expanded
169 ;; at all.
170 (if (and
171 (tramp-tramp-file-p name)
172 (not (string-match
173 "[[*?]" (tramp-compat-funcall
174 'file-remote-p name 'localname))))
175 (setq ad-return-value (list name))
176 ;; Otherwise, just run the original function.
177 ad-do-it)))
178 (add-hook
179 'tramp-unload-hook
180 (lambda ()
181 (ad-remove-advice
182 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
183 (ad-activate 'file-expand-wildcards)))))
184
185 ;; `with-temp-message' does not exists in XEmacs.
186 (if (fboundp 'with-temp-message)
187 (defalias 'tramp-compat-with-temp-message 'with-temp-message)
188 (defmacro tramp-compat-with-temp-message (message &rest body)
189 "Display MESSAGE temporarily if non-nil while BODY is evaluated."
190 `(progn ,@body)))
191
192 ;; `condition-case-unless-debug' is introduced with Emacs 24.
193 (if (fboundp 'condition-case-unless-debug)
194 (defalias 'tramp-compat-condition-case-unless-debug
195 'condition-case-unless-debug)
196 (defmacro tramp-compat-condition-case-unless-debug
197 (var bodyform &rest handlers)
198 "Like `condition-case' except that it does not catch anything when debugging."
199 (declare (debug condition-case) (indent 2))
200 (let ((bodysym (make-symbol "body")))
201 `(let ((,bodysym (lambda () ,bodyform)))
202 (if debug-on-error
203 (funcall ,bodysym)
204 (condition-case ,var
205 (funcall ,bodysym)
206 ,@handlers))))))
207
208 ;; `font-lock-add-keywords' does not exist in XEmacs.
209 (defun tramp-compat-font-lock-add-keywords (mode keywords &optional how)
210 "Add highlighting KEYWORDS for MODE."
211 (ignore-errors
212 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))
213
214 (defsubst tramp-compat-temporary-file-directory ()
215 "Return name of directory for temporary files (compat function).
216 For Emacs, this is the variable `temporary-file-directory', for XEmacs
217 this is the function `temp-directory'."
218 (let (file-name-handler-alist)
219 ;; We must return a local directory. If it is remote, we could
220 ;; run into an infloop.
221 (cond
222 ((and (boundp 'temporary-file-directory)
223 (eval (car (get 'temporary-file-directory 'standard-value)))))
224 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
225 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
226 (file-name-as-directory (getenv "TEMP")))
227 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
228 (file-name-as-directory (getenv "TMP")))
229 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
230 (file-name-as-directory (getenv "TMPDIR")))
231 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
232 (t (message (concat "Neither `temporary-file-directory' nor "
233 "`temp-directory' is defined -- using /tmp."))
234 (file-name-as-directory "/tmp")))))
235
236 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
237 ;; implementation with `make-temp-name', creating the temporary file
238 ;; immediately in order to avoid a security hole.
239 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
240 "Create a temporary file (compat function).
241 Add the extension of FILENAME, if existing."
242 (let* (file-name-handler-alist
243 (prefix (expand-file-name
244 (symbol-value 'tramp-temp-name-prefix)
245 (tramp-compat-temporary-file-directory)))
246 (extension (file-name-extension filename t))
247 result)
248 (condition-case nil
249 (setq result
250 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
251 (error
252 ;; We use our own implementation, taken from files.el.
253 (while
254 (condition-case ()
255 (progn
256 (setq result (concat (make-temp-name prefix) extension))
257 (if dir-flag
258 (make-directory result)
259 (write-region "" nil result nil 'silent))
260 nil)
261 (file-already-exists t))
262 ;; The file was somehow created by someone else between
263 ;; `make-temp-name' and `write-region', let's try again.
264 nil)))
265 result))
266
267 ;; `most-positive-fixnum' does not exist in XEmacs.
268 (defsubst tramp-compat-most-positive-fixnum ()
269 "Return largest positive integer value (compat function)."
270 (cond
271 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
272 ;; Default value in XEmacs.
273 (t 134217727)))
274
275 (defun tramp-compat-decimal-to-octal (i)
276 "Return a string consisting of the octal digits of I.
277 Not actually used. Use `(format \"%o\" i)' instead?"
278 (cond ((< i 0) (error "Cannot convert negative number to octal"))
279 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
280 ((zerop i) "0")
281 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
282 (number-to-string (% i 8))))))
283
284 ;; Kudos to Gerd Moellmann for this suggestion.
285 (defun tramp-compat-octal-to-decimal (ostr)
286 "Given a string of octal digits, return a decimal number."
287 (let ((x (or ostr "")))
288 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
289 (unless (string-match "\\`[0-7]*\\'" x)
290 (error "Non-octal junk in string `%s'" x))
291 (string-to-number ostr 8)))
292
293 ;; ID-FORMAT does not exists in XEmacs.
294 (defun tramp-compat-file-attributes (filename &optional id-format)
295 "Like `file-attributes' for Tramp files (compat function)."
296 (cond
297 ((or (null id-format) (eq id-format 'integer))
298 (file-attributes filename))
299 ((tramp-tramp-file-p filename)
300 (tramp-compat-funcall
301 'tramp-file-name-handler 'file-attributes filename id-format))
302 (t (condition-case nil
303 (tramp-compat-funcall 'file-attributes filename id-format)
304 (wrong-number-of-arguments (file-attributes filename))))))
305
306 ;; PRESERVE-UID-GID does not exist in XEmacs.
307 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
308 ;; (as PRESERVE-SELINUX-CONTEXT), and renamed in Emacs 24.3.
309 (defun tramp-compat-copy-file
310 (filename newname &optional ok-if-already-exists keep-date
311 preserve-uid-gid preserve-extended-attributes)
312 "Like `copy-file' for Tramp files (compat function)."
313 (cond
314 (preserve-extended-attributes
315 (tramp-compat-funcall
316 'copy-file filename newname ok-if-already-exists keep-date
317 preserve-uid-gid preserve-extended-attributes))
318 (preserve-uid-gid
319 (tramp-compat-funcall
320 'copy-file filename newname ok-if-already-exists keep-date
321 preserve-uid-gid))
322 (t
323 (copy-file filename newname ok-if-already-exists keep-date))))
324
325 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
326 ;; is taken from there.
327 (defun tramp-compat-copy-directory
328 (directory newname &optional keep-time parents copy-contents)
329 "Make a copy of DIRECTORY (compat function)."
330 (condition-case nil
331 (tramp-compat-funcall
332 'copy-directory directory newname keep-time parents copy-contents)
333
334 ;; `copy-directory' is either not implemented, or it does not
335 ;; support the the COPY-CONTENTS flag. For the time being, we
336 ;; ignore COPY-CONTENTS as well.
337
338 (error
339 ;; If `default-directory' is a remote directory, make sure we
340 ;; find its `copy-directory' handler.
341 (let ((handler (or (find-file-name-handler directory 'copy-directory)
342 (find-file-name-handler newname 'copy-directory))))
343 (if handler
344 (funcall handler 'copy-directory directory newname keep-time parents)
345
346 ;; Compute target name.
347 (setq directory (directory-file-name (expand-file-name directory))
348 newname (directory-file-name (expand-file-name newname)))
349 (if (and (file-directory-p newname)
350 (not (string-equal (file-name-nondirectory directory)
351 (file-name-nondirectory newname))))
352 (setq newname
353 (expand-file-name
354 (file-name-nondirectory directory) newname)))
355 (if (not (file-directory-p newname)) (make-directory newname parents))
356
357 ;; Copy recursively.
358 (mapc
359 (lambda (file)
360 (if (file-directory-p file)
361 (tramp-compat-copy-directory file newname keep-time parents)
362 (copy-file file newname t keep-time)))
363 ;; We do not want to delete "." and "..".
364 (directory-files
365 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
366
367 ;; Set directory attributes.
368 (set-file-modes newname (file-modes directory))
369 (if keep-time
370 (set-file-times newname (nth 5 (file-attributes directory)))))))))
371
372 ;; TRASH has been introduced with Emacs 24.1.
373 (defun tramp-compat-delete-file (filename &optional trash)
374 "Like `delete-file' for Tramp files (compat function)."
375 (condition-case nil
376 (tramp-compat-funcall 'delete-file filename trash)
377 ;; This Emacs version does not support the TRASH flag.
378 (wrong-number-of-arguments
379 (let ((delete-by-moving-to-trash
380 (and (boundp 'delete-by-moving-to-trash)
381 (symbol-value 'delete-by-moving-to-trash)
382 trash)))
383 (delete-file filename)))))
384
385 ;; RECURSIVE has been introduced with Emacs 23.2.
386 (defun tramp-compat-delete-directory (directory &optional recursive)
387 "Like `delete-directory' for Tramp files (compat function)."
388 (if (null recursive)
389 (delete-directory directory)
390 (condition-case nil
391 (tramp-compat-funcall 'delete-directory directory recursive)
392 ;; This Emacs version does not support the RECURSIVE flag. We
393 ;; use the implementation from Emacs 23.2.
394 (wrong-number-of-arguments
395 (setq directory (directory-file-name (expand-file-name directory)))
396 (if (not (file-symlink-p directory))
397 (mapc (lambda (file)
398 (if (eq t (car (file-attributes file)))
399 (tramp-compat-delete-directory file recursive)
400 (delete-file file)))
401 (directory-files
402 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
403 (delete-directory directory)))))
404
405 ;; `number-sequence' does not exist in XEmacs. Implementation is
406 ;; taken from Emacs 23.
407 (defun tramp-compat-number-sequence (from &optional to inc)
408 "Return a sequence of numbers from FROM to TO as a list (compat function)."
409 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
410 (tramp-compat-funcall 'number-sequence from to inc)
411 (if (or (not to) (= from to))
412 (list from)
413 (or inc (setq inc 1))
414 (when (zerop inc) (error "The increment can not be zero"))
415 (let (seq (n 0) (next from))
416 (if (> inc 0)
417 (while (<= next to)
418 (setq seq (cons next seq)
419 n (1+ n)
420 next (+ from (* n inc))))
421 (while (>= next to)
422 (setq seq (cons next seq)
423 n (1+ n)
424 next (+ from (* n inc)))))
425 (nreverse seq)))))
426
427 (defun tramp-compat-split-string (string pattern)
428 "Like `split-string' but omit empty strings.
429 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
430 This is, the first, empty, element is omitted. In XEmacs, the first
431 element is not omitted."
432 (delete "" (split-string string pattern)))
433
434 (defun tramp-compat-call-process
435 (program &optional infile destination display &rest args)
436 "Calls `call-process' on the local host.
437 This is needed because for some Emacs flavors Tramp has
438 defadvised `call-process' to behave like `process-file'. The
439 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
440 (let ((default-directory
441 (if (file-remote-p default-directory)
442 (tramp-compat-temporary-file-directory)
443 default-directory)))
444 (if (executable-find program)
445 (apply 'call-process program infile destination display args)
446 1)))
447
448 (defun tramp-compat-process-running-p (process-name)
449 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
450 (when (stringp process-name)
451 (cond
452 ;; GNU Emacs 22 on w32.
453 ((fboundp 'w32-window-exists-p)
454 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
455
456 ;; GNU Emacs 23.
457 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
458 (let (result)
459 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
460 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
461 (when (and (string-equal
462 (cdr (assoc 'user attributes)) (user-login-name))
463 (let ((comm (cdr (assoc 'comm attributes))))
464 ;; The returned command name could be truncated
465 ;; to 15 characters. Therefore, we cannot check
466 ;; for `string-equal'.
467 (and comm (string-match
468 (concat "^" (regexp-quote comm))
469 process-name))))
470 (setq result t))))))
471
472 ;; Fallback, if there is no Lisp support yet.
473 (t (let ((default-directory
474 (if (file-remote-p default-directory)
475 (tramp-compat-temporary-file-directory)
476 default-directory))
477 (unix95 (getenv "UNIX95"))
478 result)
479 (setenv "UNIX95" "1")
480 (when (member
481 (user-login-name)
482 (tramp-compat-split-string
483 (shell-command-to-string
484 (format "ps -C %s -o user=" process-name))
485 "[ \f\t\n\r\v]+"))
486 (setq result t))
487 (setenv "UNIX95" unix95)
488 result)))))
489
490 ;; The following functions do not exist in XEmacs. We ignore this;
491 ;; they are used for checking a remote tty.
492 (defun tramp-compat-process-get (process propname)
493 "Return the value of PROCESS' PROPNAME property.
494 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
495 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
496
497 (defun tramp-compat-process-put (process propname value)
498 "Change PROCESS' PROPNAME property to VALUE.
499 It can be retrieved with `(process-get PROCESS PROPNAME)'."
500 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
501
502 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
503 "Specify if query is needed for process when Emacs is exited.
504 If the second argument flag is non-nil, Emacs will query the user before
505 exiting if process is running."
506 (if (fboundp 'set-process-query-on-exit-flag)
507 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
508 (tramp-compat-funcall 'process-kill-without-query process flag)))
509
510 ;; There exist different implementations for this function.
511 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
512 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
513 EOL-TYPE can be one of `dos', `unix', or `mac'."
514 (cond ((fboundp 'coding-system-change-eol-conversion)
515 (tramp-compat-funcall
516 'coding-system-change-eol-conversion coding-system eol-type))
517 ((fboundp 'subsidiary-coding-system)
518 (tramp-compat-funcall
519 'subsidiary-coding-system coding-system
520 (cond ((eq eol-type 'dos) 'crlf)
521 ((eq eol-type 'unix) 'lf)
522 ((eq eol-type 'mac) 'cr)
523 (t
524 (error "Unknown EOL-TYPE `%s', must be %s"
525 eol-type
526 "`dos', `unix', or `mac'")))))
527 (t (error "Can't change EOL conversion -- is MULE missing?"))))
528
529 (add-hook 'tramp-unload-hook
530 (lambda ()
531 (unload-feature 'tramp-compat 'force)))
532
533 (provide 'tramp-compat)
534
535 ;;; TODO:
536
537 ;;; tramp-compat.el ends here