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