Phil Hagelberg <phil at evri.com>
[bpt/emacs.git] / lisp / pcmpl-unix.el
CommitLineData
60370d40 1;;; pcmpl-unix.el --- standard UNIX completions
4fa9f636 2
c90f2757 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4fa9f636
GM
5
6;; This file is part of GNU Emacs.
7
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
b4aa6026 10;; the Free Software Foundation; either version 3, or (at your option)
4fa9f636
GM
11;; any later version.
12
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.
17
18;; You should have received a copy of the GNU General Public License
19;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
20;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21;; Boston, MA 02110-1301, USA.
4fa9f636 22
60370d40
PJ
23;;; Commentary:
24
4fa9f636
GM
25;;; Code:
26
27(provide 'pcmpl-unix)
28
29(require 'pcomplete)
30
31;; User Variables:
32
33(defcustom pcmpl-unix-group-file "/etc/group"
34 "*If non-nil, a string naming the group file on your system."
35 :type 'file
36 :group 'pcmpl-unix)
37
38(defcustom pcmpl-unix-passwd-file "/etc/passwd"
39 "*If non-nil, a string naming the passwd file on your system."
40 :type 'file
41 :group 'pcmpl-unix)
42
9c519cf2
GM
43(defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts"
44 "The location of the user's SSH `known_hosts' file."
45 :type 'file
46 :group 'pcmpl-unix)
47
4fa9f636
GM
48;; Functions:
49
50;;;###autoload
51(defun pcomplete/cd ()
52 "Completion for `cd'."
53 (pcomplete-here (pcomplete-dirs)))
54
55;;;###autoload
56(defalias 'pcomplete/pushd 'pcomplete/cd)
57
58;;;###autoload
59(defun pcomplete/rmdir ()
60 "Completion for `rmdir'."
61 (while (pcomplete-here (pcomplete-dirs))))
62
63;;;###autoload
64(defun pcomplete/rm ()
65 "Completion for `rm'."
66 (let ((pcomplete-help "(fileutils)rm invocation"))
67 (pcomplete-opt "dfirRv")
68 (while (pcomplete-here (pcomplete-all-entries) nil
69 'expand-file-name))))
70
71;;;###autoload
72(defun pcomplete/xargs ()
73 "Completion for `xargs'."
74 (pcomplete-here (funcall pcomplete-command-completion-function))
75 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
76 pcomplete-default-completion-function)))
77
78;;;###autoload
79(defalias 'pcomplete/time 'pcomplete/xargs)
80
81;;;###autoload
82(defun pcomplete/which ()
83 "Completion for `which'."
84 (while (pcomplete-here (funcall pcomplete-command-completion-function))))
85
86(defun pcmpl-unix-read-passwd-file (file)
87 "Return an alist correlating gids to group names in FILE."
88 (let (names)
89 (when (file-readable-p file)
90 (with-temp-buffer
91 (insert-file-contents file)
92 (goto-char (point-min))
93 (while (not (eobp))
94 (let* ((fields
95 (split-string (buffer-substring
96 (point) (progn (end-of-line)
97 (point))) ":")))
98 (setq names (cons (nth 0 fields) names)))
99 (forward-line))))
100 (pcomplete-uniqify-list names)))
101
102(defsubst pcmpl-unix-group-names ()
103 "Read the contents of /etc/group for group names."
104 (if pcmpl-unix-group-file
105 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
106
107(defsubst pcmpl-unix-user-names ()
108 "Read the contents of /etc/passwd for user names."
109 (if pcmpl-unix-passwd-file
110 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
111
112;;;###autoload
113(defun pcomplete/chown ()
114 "Completion for the `chown' command."
115 (unless (pcomplete-match "\\`-")
116 (if (pcomplete-match "\\`[^.]*\\'" 0)
117 (pcomplete-here* (pcmpl-unix-user-names))
118 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
119 (pcomplete-here* (pcmpl-unix-group-names)
120 (pcomplete-match-string 1 0))
121 (pcomplete-here*))))
122 (while (pcomplete-here (pcomplete-entries))))
123
124;;;###autoload
125(defun pcomplete/chgrp ()
126 "Completion for the `chgrp' command."
127 (unless (pcomplete-match "\\`-")
128 (pcomplete-here* (pcmpl-unix-group-names)))
129 (while (pcomplete-here (pcomplete-entries))))
130
9c519cf2
GM
131;; ssh support by Phil Hagelberg.
132;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
133
134;; This will allow eshell to autocomplete SSH hosts from the list of
135;; known hosts in your ~/.ssh/known_hosts file. Note that newer
136;; versions of ssh hash the hosts by default to prevent Island-hopping
137;; SSH attacks. (https://itso.iu.edu/Hashing_the_OpenSSH_known__hosts_File)
138;; You can disable this by putting the following line in your ~/.ssh/config
139;; file following the "Host *" directive:
140
141;; HashKnownHosts no
142
143;; Note that this will make you vulnerable to the Island-hopping
144;; attack described in the link above if you allow key-based
145;; passwordless logins and your account is compromised.
146
147;;;###autoload
148(defun pcomplete/ssh ()
149 "Completion rules for the `ssh' command."
150 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t)
151 (pcomplete-here (pcmpl-ssh-hosts)))
152
153;;;###autoload
154(defun pcomplete/scp ()
155 "Completion rules for the `scp' command.
156
157Includes files as well as host names followed by a colon."
158 (pcomplete-opt "1246BCpqrvcFiloPS")
159 (while t (pcomplete-here (append (pcomplete-all-entries)
160 (mapcar (lambda (host) (concat host ":")) (pcmpl-ssh-hosts))))))
161
162(defun pcmpl-ssh-hosts ()
163 "Returns a list of hosts found in the users `known_hosts' file."
164 (if (file-readable-p pcmpl-ssh-known-hosts-file)
165 (with-temp-buffer
166 (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
167 (let ((ssh-hosts-list) '())
168 (while (not (eobp))
169 (let ((hostname (buffer-substring (point) (- (search-forward-regexp "[, ]") 1))))
170 (unless (string-match "^|" hostname)
171 (add-to-list 'ssh-hosts-list hostname)))
172 (forward-line))
173 ssh-hosts-list))))
174
ab5796a9 175;;; arch-tag: 3f9eb5af-7e0e-449d-b586-381cbbf8fc5c
4fa9f636 176;;; pcmpl-unix.el ends here