Mercurial > emacs
annotate lisp/pcmpl-unix.el @ 91643:d3922949ea68
Phil Hagelberg <phil at evri.com>
(pcmpl-ssh-known-hosts-file): New defcustom.
(pcomplete/ssh, pcomplete/scp, pcmpl-ssh-hosts): New functions.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Fri, 08 Feb 2008 05:07:50 +0000 |
parents | 73661ddc7ac7 |
children | 681a1b7488c2 |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
29959
diff
changeset
|
1 ;;; pcmpl-unix.el --- standard UNIX completions |
29959 | 2 |
74442 | 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
79721 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
29959 | 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 | |
78236
9355f9b7bbff
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
10 ;; the Free Software Foundation; either version 3, or (at your option) |
29959 | 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 | |
64091 | 20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 ;; Boston, MA 02110-1301, USA. | |
29959 | 22 |
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
29959
diff
changeset
|
23 ;;; Commentary: |
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
29959
diff
changeset
|
24 |
29959 | 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 | |
91643
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
43 (defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts" |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
44 "The location of the user's SSH `known_hosts' file." |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
45 :type 'file |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
46 :group 'pcmpl-unix) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
47 |
29959 | 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 | |
91643
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
131 ;; ssh support by Phil Hagelberg. |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
132 ;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
133 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
134 ;; This will allow eshell to autocomplete SSH hosts from the list of |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
135 ;; known hosts in your ~/.ssh/known_hosts file. Note that newer |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
136 ;; versions of ssh hash the hosts by default to prevent Island-hopping |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
137 ;; SSH attacks. (https://itso.iu.edu/Hashing_the_OpenSSH_known__hosts_File) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
138 ;; You can disable this by putting the following line in your ~/.ssh/config |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
139 ;; file following the "Host *" directive: |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
140 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
141 ;; HashKnownHosts no |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
142 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
143 ;; Note that this will make you vulnerable to the Island-hopping |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
144 ;; attack described in the link above if you allow key-based |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
145 ;; passwordless logins and your account is compromised. |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
146 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
147 ;;;###autoload |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
148 (defun pcomplete/ssh () |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
149 "Completion rules for the `ssh' command." |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
150 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
151 (pcomplete-here (pcmpl-ssh-hosts))) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
152 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
153 ;;;###autoload |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
154 (defun pcomplete/scp () |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
155 "Completion rules for the `scp' command. |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
156 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
157 Includes files as well as host names followed by a colon." |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
158 (pcomplete-opt "1246BCpqrvcFiloPS") |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
159 (while t (pcomplete-here (append (pcomplete-all-entries) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
160 (mapcar (lambda (host) (concat host ":")) (pcmpl-ssh-hosts)))))) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
161 |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
162 (defun pcmpl-ssh-hosts () |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
163 "Returns a list of hosts found in the users `known_hosts' file." |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
164 (if (file-readable-p pcmpl-ssh-known-hosts-file) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
165 (with-temp-buffer |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
166 (insert-file-contents-literally pcmpl-ssh-known-hosts-file) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
167 (let ((ssh-hosts-list) '()) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
168 (while (not (eobp)) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
169 (let ((hostname (buffer-substring (point) (- (search-forward-regexp "[, ]") 1)))) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
170 (unless (string-match "^|" hostname) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
171 (add-to-list 'ssh-hosts-list hostname))) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
172 (forward-line)) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
173 ssh-hosts-list)))) |
d3922949ea68
Phil Hagelberg <phil at evri.com>
Glenn Morris <rgm@gnu.org>
parents:
79721
diff
changeset
|
174 |
52401 | 175 ;;; arch-tag: 3f9eb5af-7e0e-449d-b586-381cbbf8fc5c |
29959 | 176 ;;; pcmpl-unix.el ends here |