Fix permissions handling (CVE-2010-0825).
[emacs.git] / lisp / pcmpl-unix.el
blob9282fe87b5a12c2356ab524789935d0063d4cb6a
1 ;;; pcmpl-unix.el --- standard UNIX completions
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;;; Code:
25 (require 'pcomplete)
27 ;; User Variables:
29 (defcustom pcmpl-unix-group-file "/etc/group"
30 "If non-nil, a string naming the group file on your system."
31 :type '(choice file (const nil))
32 :group 'pcmpl-unix)
34 (defcustom pcmpl-unix-passwd-file "/etc/passwd"
35 "If non-nil, a string naming the passwd file on your system."
36 :type '(choice file (const nil))
37 :group 'pcmpl-unix)
39 (defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts"
40 "If non-nil, a string naming your SSH \"known_hosts\" file.
41 This allows completion of SSH host names. Note that newer
42 versions of ssh hash the hosts by default to prevent
43 Island-hopping SSH attacks. This can be disabled, at some risk,
44 with the SSH option \"HashKnownHosts no\"."
45 :type '(choice file (const nil))
46 :group 'pcmpl-unix
47 :version "23.1")
49 ;; Functions:
51 ;;;###autoload
52 (defun pcomplete/cd ()
53 "Completion for `cd'."
54 (while (pcomplete-here (pcomplete-dirs))))
56 ;;;###autoload
57 (defalias 'pcomplete/pushd 'pcomplete/cd)
59 ;;;###autoload
60 (defun pcomplete/rmdir ()
61 "Completion for `rmdir'."
62 (while (pcomplete-here (pcomplete-dirs))))
64 ;;;###autoload
65 (defun pcomplete/rm ()
66 "Completion for `rm'."
67 (let ((pcomplete-help "(fileutils)rm invocation"))
68 (pcomplete-opt "dfirRv")
69 (while (pcomplete-here (pcomplete-all-entries) nil
70 'expand-file-name))))
72 ;;;###autoload
73 (defun pcomplete/xargs ()
74 "Completion for `xargs'."
75 (pcomplete-here (funcall pcomplete-command-completion-function))
76 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
77 pcomplete-default-completion-function)))
79 ;;;###autoload
80 (defalias 'pcomplete/time 'pcomplete/xargs)
82 ;;;###autoload
83 (defun pcomplete/which ()
84 "Completion for `which'."
85 (while (pcomplete-here (funcall pcomplete-command-completion-function))))
87 (defun pcmpl-unix-read-passwd-file (file)
88 "Return an alist correlating gids to group names in FILE.
90 If FILE is in hashed format (as described in the OpenSSH
91 documentation), this function returns nil."
92 (let (names)
93 (when (file-readable-p file)
94 (with-temp-buffer
95 (insert-file-contents file)
96 (goto-char (point-min))
97 (while (not (eobp))
98 (let* ((fields
99 (split-string (buffer-substring
100 (point) (progn (end-of-line)
101 (point))) ":")))
102 (setq names (cons (nth 0 fields) names)))
103 (forward-line))))
104 (pcomplete-uniqify-list names)))
106 (defsubst pcmpl-unix-group-names ()
107 "Read the contents of /etc/group for group names."
108 (if pcmpl-unix-group-file
109 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
111 (defsubst pcmpl-unix-user-names ()
112 "Read the contents of /etc/passwd for user names."
113 (if pcmpl-unix-passwd-file
114 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
116 ;;;###autoload
117 (defun pcomplete/chown ()
118 "Completion for the `chown' command."
119 (unless (pcomplete-match "\\`-")
120 (if (pcomplete-match "\\`[^.]*\\'" 0)
121 (pcomplete-here* (pcmpl-unix-user-names))
122 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
123 (pcomplete-here* (pcmpl-unix-group-names)
124 (pcomplete-match-string 1 0))
125 (pcomplete-here*))))
126 (while (pcomplete-here (pcomplete-entries))))
128 ;;;###autoload
129 (defun pcomplete/chgrp ()
130 "Completion for the `chgrp' command."
131 (unless (pcomplete-match "\\`-")
132 (pcomplete-here* (pcmpl-unix-group-names)))
133 (while (pcomplete-here (pcomplete-entries))))
136 ;; ssh support by Phil Hagelberg.
137 ;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
139 (defun pcmpl-ssh-hosts ()
140 "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
141 (when (and pcmpl-ssh-known-hosts-file
142 (file-readable-p pcmpl-ssh-known-hosts-file))
143 (with-temp-buffer
144 (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
145 (let (ssh-hosts-list)
146 (while (re-search-forward "^ *\\([-.[:alnum:]]+\\)[, ]" nil t)
147 (add-to-list 'ssh-hosts-list (match-string 1))
148 (while (and (looking-back ",")
149 (re-search-forward "\\([-.[:alnum:]]+\\)[, ]"
150 (line-end-position) t))
151 (add-to-list 'ssh-hosts-list (match-string 1))))
152 ssh-hosts-list))))
154 ;;;###autoload
155 (defun pcomplete/ssh ()
156 "Completion rules for the `ssh' command."
157 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t)
158 (pcomplete-here (pcmpl-ssh-hosts)))
160 ;;;###autoload
161 (defun pcomplete/scp ()
162 "Completion rules for the `scp' command.
163 Includes files as well as host names followed by a colon."
164 (pcomplete-opt "1246BCpqrvcFiloPS")
165 (while t (pcomplete-here (append (pcomplete-all-entries)
166 (mapcar (lambda (host)
167 (concat host ":"))
168 (pcmpl-ssh-hosts))))))
170 (provide 'pcmpl-unix)
172 ;; arch-tag: 3f9eb5af-7e0e-449d-b586-381cbbf8fc5c
173 ;;; pcmpl-unix.el ends here