Mercurial > emacs
annotate lisp/net/ange-ftp.el @ 38038:f874bc96a6a5
(rot13-other-window): Some more doc fix.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Thu, 14 Jun 2001 15:42:57 +0000 |
parents | 989feabdf50e |
children | b6dd8b1368af |
rev | line source |
---|---|
28210 | 1 ;;; ange-ftp.el --- transparent FTP support for GNU Emacs |
2 | |
37774
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
3 ;; Copyright (C) 1989,90,91,92,93,94,95,96,98, 2000, 2001 |
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
4 ;; Free Software Foundation, Inc. |
28210 | 5 |
6 ;; Author: Andy Norman (ange@hplb.hpl.hp.com) | |
7 ;; Maintainer: FSF | |
8 ;; Keywords: comm | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;; This package attempts to make accessing files and directories using FTP | |
30 ;; from within GNU Emacs as simple and transparent as possible. A subset of | |
31 ;; the common file-handling routines are extended to interact with FTP. | |
32 | |
33 ;; Usage: | |
34 ;; | |
35 ;; Some of the common GNU Emacs file-handling operations have been made | |
36 ;; FTP-smart. If one of these routines is given a filename that matches | |
37 ;; '/user@host:name' then it will spawn an FTP process connecting to machine | |
38 ;; 'host' as account 'user' and perform its operation on the file 'name'. | |
39 ;; | |
40 ;; For example: if find-file is given a filename of: | |
41 ;; | |
42 ;; /ange@anorman:/tmp/notes | |
43 ;; | |
44 ;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as | |
45 ;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the | |
46 ;; contents of that file as if it were on the local filesystem. If ange-ftp | |
47 ;; needs a password to connect then it reads one in the echo area. | |
48 | |
49 ;; Extended filename syntax: | |
50 ;; | |
51 ;; The default extended filename syntax is '/user@host:name', where the | |
52 ;; 'user@' part may be omitted. This syntax can be customised to a certain | |
53 ;; extent by changing ange-ftp-name-format. There are limitations. | |
54 ;; The `host' part has an optional suffix `#port' which may be used to | |
55 ;; specify a non-default port number for the connexion. | |
56 ;; | |
57 ;; If the user part is omitted then ange-ftp generates a default user | |
58 ;; instead whose value depends on the variable ange-ftp-default-user. | |
59 | |
60 ;; Passwords: | |
61 ;; | |
62 ;; A password is required for each host/user pair. Ange-ftp reads passwords | |
63 ;; as needed. You can also specify a password with ange-ftp-set-passwd, or | |
64 ;; in a *valid* ~/.netrc file. | |
65 | |
66 ;; Passwords for user "anonymous": | |
67 ;; | |
68 ;; Passwords for the user "anonymous" (or "ftp") are handled | |
69 ;; specially. The variable `ange-ftp-generate-anonymous-password' | |
70 ;; controls what happens: if the value of this variable is a string, | |
71 ;; then this is used as the password; if non-nil (the default), then | |
72 ;; the value of `user-mail-address' is used; if nil then the user | |
73 ;; is prompted for a password as normal. | |
74 | |
75 ;; "Dumb" UNIX hosts: | |
76 ;; | |
77 ;; The FTP servers on some UNIX machines have problems if the 'ls' command is | |
78 ;; used. | |
79 ;; | |
80 ;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to | |
81 ;; limit itself to the DIR command and not 'ls' for a given UNIX host. Note | |
82 ;; that this change will take effect for the current GNU Emacs session only. | |
83 ;; See below for a discussion of non-UNIX hosts. If a large number of | |
84 ;; machines with similar hostnames have this problem then it is easier to set | |
85 ;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp | |
86 ;; is unable to automatically recognize dumb unix hosts. | |
87 | |
88 ;; File name completion: | |
89 ;; | |
90 ;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts. | |
91 ;; To do filename completion, ange-ftp needs a listing from the remote host. | |
92 ;; Therefore, for very slow connections, it might not save any time. | |
93 | |
94 ;; FTP processes: | |
95 ;; | |
96 ;; When ange-ftp starts up an FTP process, it leaves it running for speed | |
97 ;; purposes. Some FTP servers will close the connection after a period of | |
98 ;; time, but ange-ftp should be able to quietly reconnect the next time that | |
99 ;; the process is needed. | |
100 ;; | |
101 ;; Killing the "*ftp user@host*" buffer also kills the ftp process. | |
102 ;; This should not cause ange-ftp any grief. | |
103 | |
104 ;; Binary file transfers: | |
105 ;; | |
106 ;; By default ange-ftp transfers files in ASCII mode. If a file being | |
107 ;; transferred matches the value of ange-ftp-binary-file-name-regexp then | |
108 ;; binary mode is used for that transfer. | |
109 | |
110 ;; Account passwords: | |
111 ;; | |
112 ;; Some FTP servers require an additional password which is sent by the | |
113 ;; ACCOUNT command. ange-ftp partially supports this by allowing the user to | |
114 ;; specify an account password by either calling ange-ftp-set-account, or by | |
115 ;; specifying an account token in the .netrc file. If the account password | |
116 ;; is set by either of these methods then ange-ftp will issue an ACCOUNT | |
117 ;; command upon starting the FTP process. | |
118 | |
119 ;; Preloading: | |
120 ;; | |
121 ;; ange-ftp can be preloaded, but must be put in the site-init.el file and | |
122 ;; not the site-load.el file in order for the documentation strings for the | |
123 ;; functions being overloaded to be available. | |
124 | |
125 ;; Status reports: | |
126 ;; | |
127 ;; Most ange-ftp commands that talk to the FTP process output a status | |
128 ;; message on what they are doing. In addition, ange-ftp can take advantage | |
129 ;; of the FTP client's HASH command to display the status of transferring | |
130 ;; files and listing directories. See the documentation for the variables | |
131 ;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and | |
132 ;; ange-ftp-process-verbose for more details. | |
133 | |
134 ;; Gateways: | |
135 ;; | |
136 ;; Sometimes it is necessary for the FTP process to be run on a different | |
137 ;; machine than the machine running GNU Emacs. This can happen when the | |
138 ;; local machine has restrictions on what hosts it can access. | |
139 ;; | |
140 ;; ange-ftp has support for running the ftp process on a different (gateway) | |
141 ;; machine. The way it works is as follows: | |
142 ;; | |
143 ;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine | |
144 ;; that doesn't have the access restrictions. | |
145 ;; | |
146 ;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression | |
147 ;; that matches hosts that can be contacted from running a local ftp | |
148 ;; process, but fails to match hosts that can't be accessed locally. For | |
149 ;; example: | |
150 ;; | |
151 ;; "\\.hp\\.com$\\|^[^.]*$" | |
152 ;; | |
153 ;; will match all hosts that are in the .hp.com domain, or don't have an | |
154 ;; explicit domain in their name, but will fail to match hosts with | |
155 ;; explicit domains or that are specified by their ip address. | |
156 ;; | |
157 ;; 3) Using NFS and symlinks, make sure that there is a shared directory with | |
158 ;; the *same* name between the local machine and the gateway machine. | |
159 ;; This directory is necessary for temporary files created by ange-ftp. | |
160 ;; | |
161 ;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of | |
162 ;; this directory plus an identifying filename prefix. For example: | |
163 ;; | |
164 ;; "/nfs/hplose/ange/ange-ftp" | |
165 ;; | |
166 ;; where /nfs/hplose/ange is a directory that is shared between the | |
167 ;; gateway machine and the local machine. | |
168 ;; | |
169 ;; The simplest way of getting a ftp process running on the gateway machine | |
170 ;; is if you can spawn a remote shell using either 'rsh' or 'remsh'. If you | |
171 ;; can't do this for some reason such as security then points 7 onwards will | |
172 ;; discuss an alternative approach. | |
173 ;; | |
174 ;; 5) Set the variable ange-ftp-gateway-program to the name of the remote | |
175 ;; shell process such as 'remsh' or 'rsh' if the default isn't correct. | |
176 ;; | |
177 ;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it | |
178 ;; isn't already. This tells ange-ftp that you are using a remote shell | |
179 ;; rather than logging in using telnet or rlogin. | |
180 ;; | |
181 ;; That should be all you need to allow ange-ftp to spawn a ftp process on | |
182 ;; the gateway machine. If you have to use telnet or rlogin to get to the | |
183 ;; gateway machine then follow the instructions below. | |
184 ;; | |
185 ;; 7) Set the variable ange-ftp-gateway-program to the name of the program | |
186 ;; that lets you log onto the gateway machine. This may be something like | |
187 ;; telnet or rlogin. | |
188 ;; | |
189 ;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular | |
190 ;; expression that matches the prompt you get when you login to the | |
191 ;; gateway machine. Be very specific here; this regexp must not match | |
192 ;; *anything* in your login banner except this prompt. | |
193 ;; shell-prompt-pattern is far too general as it appears to match some | |
194 ;; login banners from Sun machines. For example: | |
195 ;; | |
196 ;; "^$*$ *" | |
197 ;; | |
198 ;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let | |
199 ;; ange-ftp know that it has to "hand-hold" the login to the gateway | |
200 ;; machine. | |
201 ;; | |
202 ;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command | |
203 ;; that will put the pty connected to the gateway machine into a | |
204 ;; no-echoing mode, and will strip off carriage-returns from output from | |
205 ;; the gateway machine. For example: | |
206 ;; | |
207 ;; "stty -onlcr -echo" | |
208 ;; | |
209 ;; will work on HP-UX machines, whereas: | |
210 ;; | |
211 ;; "stty -echo nl" | |
212 ;; | |
213 ;; appears to work for some Sun machines. | |
214 ;; | |
215 ;; That's all there is to it. | |
216 | |
217 ;; Smart gateways: | |
218 ;; | |
219 ;; If you have a "smart" ftp program that allows you to issue commands like | |
220 ;; "USER foo@bar" which do nice proxy things, then look at the variables | |
221 ;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port. | |
222 ;; | |
223 ;; Otherwise, if there is an alternate ftp program that implements proxy in | |
224 ;; a transparent way (i.e. w/o specifying the proxy host), that will | |
225 ;; connect you directly to the desired destination host: | |
226 ;; Set ange-ftp-gateway-ftp-program-name to that program's name. | |
227 ;; Set ange-ftp-local-host-regexp to a value as stated earlier on. | |
228 ;; Leave ange-ftp-gateway-host set to nil. | |
229 ;; Set ange-ftp-smart-gateway to t. | |
230 | |
231 ;; Tips for using ange-ftp: | |
232 ;; | |
233 ;; 1. For dired to work on a host which marks symlinks with a trailing @ in | |
234 ;; an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t). | |
235 ;; Most UNIX systems do not do this, but ULTRIX does. If you think that | |
236 ;; there is a chance you might connect to an ULTRIX machine (such as | |
237 ;; prep.ai.mit.edu), then set this variable accordingly. This will have | |
238 ;; the side effect that dired will have problems with symlinks whose names | |
239 ;; end in an @. If you get yourself into this situation then editing | |
240 ;; dired's ls-switches to remove "F", will temporarily fix things. | |
241 ;; | |
242 ;; 2. If you know that you are connecting to a certain non-UNIX machine | |
243 ;; frequently, and ange-ftp seems to be unable to guess its host-type, | |
244 ;; then setting the appropriate host-type regexp | |
245 ;; (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or | |
246 ;; ange-ftp-cms-host-regexp) accordingly should help. Also, please report | |
247 ;; ange-ftp's inability to recognize the host-type as a bug. | |
248 ;; | |
249 ;; 3. For slow connections, you might get "listing unreadable" error | |
250 ;; messages, or get an empty buffer for a file that you know has something | |
251 ;; in it. The solution is to increase the value of ange-ftp-retry-time. | |
252 ;; Its default value is 5 which is plenty for reasonable connections. | |
253 ;; However, for some transatlantic connections I set this to 20. | |
254 ;; | |
255 ;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by | |
256 ;; copying the file to the local machine, compressing it there, and then | |
257 ;; sending it back. Binary file transfers between machines of different | |
258 ;; architectures can be a risky business. Test things out first on some | |
259 ;; test files. See "Bugs" below. Also, note that ange-ftp copies files by | |
260 ;; moving them through the local machine. Again, be careful when doing | |
261 ;; this with binary files on non-Unix machines. | |
262 ;; | |
263 ;; 5. Beware that dired over ftp will use your setting of dired-no-confirm | |
264 ;; (list of dired commands for which confirmation is not asked). You | |
265 ;; might want to reconsider your setting of this variable, because you | |
266 ;; might want confirmation for more commands on remote direds than on | |
267 ;; local direds. For example, I strongly recommend that you not include | |
268 ;; compress and uncompress in this list. If there is enough demand it | |
269 ;; might be a good idea to have an alist ange-ftp-dired-no-confirm of | |
270 ;; pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST | |
271 ;; is a list of commands for which confirmation would be suppressed. Then | |
272 ;; remote dired listings would take their (buffer-local) value of | |
273 ;; dired-no-confirm from this alist. Who votes for this? | |
274 | |
275 ;; --------------------------------------------------------------------- | |
276 ;; Non-UNIX support: | |
277 ;; --------------------------------------------------------------------- | |
278 | |
279 ;; VMS support: | |
280 ;; | |
281 ;; Ange-ftp has full support for VMS hosts. It | |
282 ;; should be able to automatically recognize any VMS machine. However, if it | |
283 ;; fails to do this, you can use the command ange-ftp-add-vms-host. As well, | |
284 ;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We | |
285 ;; would be grateful if you would report any failures to automatically | |
286 ;; recognize a VMS host as a bug. | |
287 ;; | |
288 ;; Filename Syntax: | |
289 ;; | |
290 ;; For ease of *implementation*, the user enters the VMS filename syntax in a | |
291 ;; UNIX-y way. For example: | |
292 ;; PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1 | |
293 ;; would be entered as: | |
294 ;; /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1 | |
295 ;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file: | |
296 ;; [.CSV.POLICY]RULES.MEM | |
297 ;; you would type: | |
298 ;; C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM | |
299 ;; | |
300 ;; A legal VMS filename is of the form: FILE.TYPE;## | |
301 ;; where FILE can be up to 39 characters | |
302 ;; TYPE can be up to 39 characters | |
303 ;; ## is a version number (an integer between 1 and 32,767) | |
304 ;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $ | |
305 ;; $ cannot begin a filename, and - cannot be used as the first or last | |
306 ;; character. | |
307 ;; | |
308 ;; Tips: | |
309 ;; 1. Although VMS is not case sensitive, EMACS running under UNIX is. | |
310 ;; Therefore, to access a VMS file, you must enter the filename with upper | |
311 ;; case letters. | |
312 ;; 2. To access the latest version of file under VMS, you use the filename | |
313 ;; without the ";" and version number. You should always edit the latest | |
314 ;; version of a file. If you want to edit an earlier version, copy it to a | |
315 ;; new file first. This has nothing to do with ange-ftp, but is simply | |
316 ;; good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is | |
317 ;; latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you | |
318 ;; inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find | |
319 ;; that VMS will not allow you to save the file because it will refuse to | |
320 ;; overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and | |
321 ;; attach the buffer to this file. To get out of this situation, M-x | |
322 ;; write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to | |
323 ;; latest version of the file. For this reason, in dired "f" | |
324 ;; (dired-find-file), always loads the file sans version, whereas "v", | |
325 ;; (dired-view-file), always loads the explicit version number. The | |
326 ;; reasoning being that it reasonable to view old versions of a file, but | |
327 ;; not to edit them. | |
328 ;; 3. EMACS has a feature in which it does environment variable substitution | |
329 ;; in filenames. Therefore, to enter a $ in a filename, you must quote it | |
330 ;; by typing $$. | |
331 | |
332 ;; MTS support: | |
333 ;; | |
334 ;; Ange-ftp has full support for hosts running | |
335 ;; the Michigan terminal system. It should be able to automatically | |
336 ;; recognize any MTS machine. However, if it fails to do this, you can use | |
337 ;; the command ange-ftp-add-mts-host. As well, you can set the variable | |
338 ;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you | |
339 ;; would report any failures to automatically recognize a MTS host as a bug. | |
340 ;; | |
341 ;; Filename syntax: | |
30459 | 342 ;; |
28210 | 343 ;; MTS filenames are entered in a UNIX-y way. For example, if your account |
344 ;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be | |
345 ;; entered as | |
346 ;; /YYYY@mtsg.ubc.ca:/XXXX:/FILE | |
347 ;; In other words, MTS accounts are treated as UNIX directories. Of course, | |
348 ;; to access a file in another account, you must have access permission for | |
349 ;; it. If FILE were in your own account, then you could enter it in a | |
350 ;; relative name fashion as | |
351 ;; /YYYY@mtsg.ubc.ca:FILE | |
352 ;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the | |
353 ;; filename does not contain a TYPE (i.e. it can have as many "."'s as you | |
354 ;; like.) MTS filenames are always in upper case, and hence be sure to enter | |
355 ;; them as such! MTS is not case sensitive, but an EMACS running under UNIX | |
356 ;; is. | |
357 | |
358 ;; CMS support: | |
30459 | 359 ;; |
28210 | 360 ;; Ange-ftp has full support for hosts running |
361 ;; CMS. It should be able to automatically recognize any CMS machine. | |
362 ;; However, if it fails to do this, you can use the command | |
363 ;; ange-ftp-add-cms-host. As well, you can set the variable | |
364 ;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you | |
365 ;; would report any failures to automatically recognize a CMS host as a bug. | |
30459 | 366 ;; |
28210 | 367 ;; Filename syntax: |
368 ;; | |
369 ;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are | |
370 ;; treated as UNIX directories. For example to access the file READ.ME in | |
371 ;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter | |
372 ;; /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME | |
373 ;; If *.301 is the default minidisk for this account, you could access | |
374 ;; FOO.BAR on this minidisk as | |
375 ;; /anonymous@cuvmb.cc.columbia.edu:FOO.BAR | |
376 ;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be | |
377 ;; up to 8 characters. Again, beware that CMS filenames are always upper | |
378 ;; case, and hence must be entered as such. | |
379 ;; | |
380 ;; Tips: | |
381 ;; 1. CMS machines, with the exception of anonymous accounts, nearly always | |
382 ;; need an account password. To have ange-ftp send an account password, | |
383 ;; you can either include it in your .netrc file, or use | |
384 ;; ange-ftp-set-account. | |
385 ;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we | |
386 ;; can fix this. | |
387 ;; | |
388 ;; ------------------------------------------------------------------ | |
389 ;; Bugs: | |
390 ;; ------------------------------------------------------------------ | |
30459 | 391 ;; |
28210 | 392 ;; 1. Umask problems: |
393 ;; Be warned that files created by using ange-ftp will take account of the | |
394 ;; umask of the ftp daemon process rather than the umask of the creating | |
395 ;; user. This is particularly important when logging in as the root user. | |
396 ;; The way that I tighten up the ftp daemon's umask under HP-UX is to make | |
397 ;; sure that the umask is changed to 027 before I spawn /etc/inetd. I | |
398 ;; suspect that there is something similar on other systems. | |
399 ;; | |
400 ;; 2. Some combinations of FTP clients and servers break and get out of sync | |
401 ;; when asked to list a non-existent directory. Some of the ai.mit.edu | |
402 ;; machines cause this problem for some FTP clients. Using | |
403 ;; ange-ftp-kill-ftp-process can restart the ftp process, which | |
404 ;; should get things back in sync. | |
405 ;; | |
406 ;; 3. Ange-ftp does not check to make sure that when creating a new file, | |
407 ;; you provide a valid filename for the remote operating system. | |
408 ;; If you do not, then the remote FTP server will most likely | |
409 ;; translate your filename in some way. This may cause ange-ftp to | |
410 ;; get confused about what exactly is the name of the file. The | |
411 ;; most common causes of this are using lower case filenames on systems | |
412 ;; which support only upper case, and using filenames which are too | |
413 ;; long. | |
414 ;; | |
415 ;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons. | |
416 ;; | |
417 ;; 5. Ange-ftp likes to use pty's to talk to its FTP processes. If GNU Emacs | |
418 ;; for some reason creates a FTP process that only talks via pipes then | |
419 ;; ange-ftp won't be getting the information it requires at the time that | |
420 ;; it wants it since pipes flush at different times to pty's. One | |
421 ;; disgusting way around this problem is to talk to the FTP process via | |
422 ;; rlogin which does the 'right' things with pty's. | |
423 ;; | |
424 ;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't | |
425 ;; worried about this too much. Eventually, we should have some caching | |
426 ;; of the current minidisk. | |
30459 | 427 ;; |
28210 | 428 ;; 7. Some CMS machines do not assign a default minidisk when you ftp them as |
429 ;; anonymous. It is then necessary to guess a valid minidisk name, and cd | |
430 ;; to it. This is (understandably) beyond ange-ftp. | |
431 ;; | |
432 ;; 8. Remote to remote copying of files on non-Unix machines can be risky. | |
433 ;; Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp | |
434 ;; will use binary mode for the copy. Between systems of different | |
435 ;; architecture, this still may not be enough to guarantee the integrity | |
436 ;; of binary files. Binary file transfers from VMS machines are | |
437 ;; particularly problematical. Should ange-ftp-binary-file-name-regexp be | |
438 ;; an alist of OS type, regexp pairs? | |
439 ;; | |
440 ;; 9. The code to do compression of files over ftp is not as careful as it | |
441 ;; should be. It deletes the old remote version of the file, before | |
442 ;; actually checking if the local to remote transfer of the compressed | |
443 ;; file succeeds. Of course to delete the original version of the file | |
444 ;; after transferring the compressed version back is also dangerous, | |
445 ;; because some OS's have severe restrictions on the length of filenames, | |
446 ;; and when the compressed version is copied back the "-Z" or ".Z" may be | |
447 ;; truncated. Then, ange-ftp would delete the only remaining version of | |
448 ;; the file. Maybe ange-ftp should make backups when it compresses files | |
449 ;; (of course, the backup "~" could also be truncated off, sigh...). | |
450 ;; Suggestions? | |
451 ;; | |
452 ;; 10. If a dir listing is attempted for an empty directory on (at least | |
453 ;; some) VMS hosts, an ftp error is given. This is really an ftp bug, and | |
454 ;; I don't know how to get ange-ftp work to around it. | |
455 ;; | |
456 ;; 11. Bombs on filenames that start with a space. Deals well with filenames | |
457 ;; containing spaces, but beware that the remote ftpd may not like them | |
458 ;; much. | |
459 ;; | |
460 ;; 12. The dired support for non-Unix-like systems does not currently work. | |
461 ;; It needs to be reimplemented by modifying the parse-...-listing | |
462 ;; functions to convert the directory listing to ls -l format. | |
30459 | 463 ;; |
28210 | 464 ;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks |
465 ;; with a trailing @ in a ls -alF listing. In order to account for this | |
466 ;; ange-ftp looks to chop trailing @'s off of symlink names when it is | |
467 ;; parsing a listing with the F switch. This will cause ange-ftp to | |
468 ;; incorrectly get the name of a symlink on a non-ULTRIX host if its name | |
469 ;; ends in an @. ange-ftp will correct itself if you take F out of the | |
470 ;; dired ls switches (C-u s will allow you to edit the switches). The | |
471 ;; dired buffer will be automatically reverted, which will allow ange-ftp | |
472 ;; to fix its files hashtable. A cookie to anyone who can think of a | |
473 ;; fast, sure-fire way to recognize ULTRIX over ftp. | |
474 | |
475 ;; If you find any bugs or problems with this package, PLEASE either e-mail | |
476 ;; the above author, or send a message to the ange-ftp-lovers mailing list | |
477 ;; below. Ideas and constructive comments are especially welcome. | |
478 | |
479 ;; ange-ftp-lovers: | |
480 ;; | |
481 ;; ange-ftp has its own mailing list modestly called ange-ftp-lovers. All | |
482 ;; users of ange-ftp are welcome to subscribe (see below) and to discuss | |
483 ;; aspects of ange-ftp. New versions of ange-ftp are posted periodically to | |
484 ;; the mailing list. | |
485 | |
486 ;; [The following information about lists may be obsolete.] | |
487 | |
488 ;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the | |
489 ;; list, please mail one of the following addresses: | |
490 ;; | |
491 ;; ange-ftp-lovers-request@anorman.hpl.hp.com | |
492 ;; or | |
493 ;; ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com | |
494 ;; | |
495 ;; Please don't forget the -request part. | |
496 ;; | |
497 ;; For mail to be posted directly to ange-ftp-lovers, send to one of the | |
498 ;; following addresses: | |
30459 | 499 ;; |
28210 | 500 ;; ange-ftp-lovers@anorman.hpl.hp.com |
501 ;; or | |
502 ;; ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com | |
503 ;; | |
504 ;; Alternatively, there is a mailing list that only gets announcements of new | |
505 ;; ange-ftp releases. This is called ange-ftp-lovers-announce, and can be | |
506 ;; subscribed to by e-mailing to the -request address as above. Please make | |
507 ;; it clear in the request which mailing list you wish to join. | |
508 | |
509 ;; The archives for ange-ftp-lovers can be found via anonymous ftp under: | |
510 ;; | |
511 ;; ftp.reed.edu:pub/mailing-lists/ange-ftp/ | |
512 | |
513 ;; ----------------------------------------------------------- | |
514 ;; Technical information on this package: | |
515 ;; ----------------------------------------------------------- | |
516 | |
517 ;; ange-ftp works by putting a handler on file-name-handler-alist | |
518 ;; which is called by many primitives, and a few non-primitives, | |
519 ;; whenever they see a file name of the appropriate sort. | |
520 | |
521 ;; Checklist for adding non-UNIX support for TYPE | |
30459 | 522 ;; |
28210 | 523 ;; The following functions may need TYPE versions: |
524 ;; (not all functions will be needed for every OS) | |
525 ;; | |
526 ;; ange-ftp-fix-name-for-TYPE | |
527 ;; ange-ftp-fix-dir-name-for-TYPE | |
528 ;; ange-ftp-TYPE-host | |
529 ;; ange-ftp-TYPE-add-host | |
530 ;; ange-ftp-parse-TYPE-listing | |
531 ;; ange-ftp-TYPE-delete-file-entry | |
532 ;; ange-ftp-TYPE-add-file-entry | |
533 ;; ange-ftp-TYPE-file-name-as-directory | |
534 ;; ange-ftp-TYPE-make-compressed-filename | |
535 ;; ange-ftp-TYPE-file-name-sans-versions | |
536 ;; | |
537 ;; Variables: | |
538 ;; | |
539 ;; ange-ftp-TYPE-host-regexp | |
540 ;; May need to add TYPE to ange-ftp-dumb-host-types | |
541 ;; | |
542 ;; Check the following functions for OS dependent coding: | |
543 ;; | |
544 ;; ange-ftp-host-type | |
545 ;; ange-ftp-guess-host-type | |
546 ;; ange-ftp-allow-child-lookup | |
547 | |
548 ;; Host type conventions: | |
549 ;; | |
550 ;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type | |
551 ;; (mostly) follow the following conventions for remote host types. At | |
552 ;; least, I think that future code should try to follow these conventions, | |
553 ;; and the current code should eventually be made compliant. | |
554 ;; | |
555 ;; nil = local host type, whatever that is (probably unix). | |
556 ;; Think nil as in "not a remote host". This value is used by | |
557 ;; ange-ftp-dired-host-type for local buffers. | |
558 ;; | |
559 ;; t = a remote host of unknown type. Think t as in true, it's remote. | |
560 ;; Currently, `unix' is used as the default remote host type. | |
561 ;; Maybe we should use t. | |
562 ;; | |
563 ;; TYPE = a remote host of TYPE type. | |
564 ;; | |
565 ;; TYPE:LIST = a remote host of TYPE type, using a specialized ftp listing | |
566 ;; program called list. This is currently only used for Unix | |
567 ;; dl (descriptive listings), when ange-ftp-dired-host-type | |
568 ;; is set to `unix:dl'. | |
569 | |
570 ;; Bug report codes: | |
571 ;; | |
572 ;; Because of their naive faith in this code, there are certain situations | |
573 ;; which the writers of this program believe could never happen. However, | |
574 ;; being realists they have put calls to `error' in the program at these | |
575 ;; points. These errors provide a code, which is an integer, greater than 1. | |
576 ;; To aid debugging. the error codes, and the functions in which they reside | |
577 ;; are listed below. | |
30459 | 578 ;; |
28210 | 579 ;; 1: See ange-ftp-ls |
580 ;; | |
581 | |
582 ;; ----------------------------------------------------------- | |
583 ;; Hall of fame: | |
584 ;; ----------------------------------------------------------- | |
30459 | 585 ;; |
28210 | 586 ;; Thanks to Roland McGrath for improving the filename syntax handling, |
587 ;; for suggesting many enhancements and for numerous cleanups to the code. | |
588 ;; | |
589 ;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways. | |
590 ;; | |
591 ;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and | |
592 ;; dired / shell auto-loading. | |
593 ;; | |
594 ;; Thanks to Sebastian Kremer for dired support and for many ideas and | |
595 ;; bugfixes. | |
596 ;; | |
597 ;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support, | |
598 ;; VOS support, and hostname completion. | |
599 ;; | |
600 ;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help | |
601 ;; with file-name expansion, efficiency worries, stylistic concerns and many | |
602 ;; bugfixes. | |
603 ;; | |
604 ;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS, | |
605 ;; MTS, CMS and UNIX-dls. Sandy also added dired-support for non-UNIX OS and | |
606 ;; auto-recognition of the host type. | |
607 ;; | |
608 ;; Thanks to Dave Smith who wrote the info file for ange-ftp. | |
609 ;; | |
610 ;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping | |
611 ;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann, | |
612 ;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill | |
613 ;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay | |
614 ;; Mathur, the folks on the ange-ftp-lovers mailing list and many others | |
615 ;; whose names I've forgotten who have helped to debug and fix problems with | |
616 ;; ange-ftp.el. | |
617 | |
618 ;;; Code: | |
619 | |
620 (require 'comint) | |
621 ;; Silence compiler: | |
622 (eval-when-compile | |
623 (require 'dired) | |
624 (defvar comint-last-output-start nil) | |
625 (defvar comint-last-input-start nil) | |
626 (defvar comint-last-input-end nil)) | |
627 | |
628 ;;;; ------------------------------------------------------------ | |
629 ;;;; User customization variables. | |
630 ;;;; ------------------------------------------------------------ | |
631 | |
632 (defgroup ange-ftp nil | |
30459 | 633 "Accessing remote files and directories using FTP |
28210 | 634 made as simple and transparent as possible." |
635 :group 'files | |
636 :prefix "ange-ftp-") | |
637 | |
638 (defcustom ange-ftp-name-format | |
639 '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4)) | |
640 "*Format of a fully expanded remote file name. | |
641 | |
642 This is a list of the form \(REGEXP HOST USER NAME\), | |
643 where REGEXP is a regular expression matching | |
644 the full remote name, and HOST, USER, and NAME are the numbers of | |
645 parenthesized expressions in REGEXP for the components (in that order)." | |
646 :group 'ange-ftp | |
30459 | 647 :type '(list regexp |
28210 | 648 (integer :tag "Host group") |
649 (integer :tag "User group") | |
650 (integer :tag "Name group"))) | |
651 | |
652 ;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of | |
653 ;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs. | |
654 ;; Otherwise, ange-ftp will go into multi-skip mode, and never come out. | |
655 | |
656 (defvar ange-ftp-multi-msgs | |
657 "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-" | |
658 "*Regular expression matching the start of a multiline ftp reply.") | |
659 | |
660 (defvar ange-ftp-good-msgs | |
661 "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark" | |
662 "*Regular expression matching ftp \"success\" messages.") | |
663 | |
664 ;; CMS and the odd VMS machine say 200 Port rather than 200 PORT. | |
665 ;; Also CMS machines use a multiline 550- reply to say that you | |
666 ;; don't have write permission. ange-ftp gets into multi-line skip | |
667 ;; mode and hangs. Have it ignore 550- instead. It will then barf | |
668 ;; when it gets the 550 line, as it should. | |
669 | |
28892
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
670 ;; RFC2228 "FTP Security Extensions" defines extensions to the FTP |
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
671 ;; protocol which involve the client requesting particular |
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
672 ;; authentication methods (typically) at connection establishment. Non |
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
673 ;; security-aware FTP servers should respond to this with a 500 code, |
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
674 ;; which we ignore. |
28210 | 675 (defcustom ange-ftp-skip-msgs |
676 (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|" | |
677 "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|" | |
678 "^Data connection \\|" | |
679 "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye\\|" | |
28892
19ae39682b38
(ange-ftp-skip-msgs): Include 500 code.for
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
680 "^500 .*AUTH \\(KERBEROS\\|GSSAPI\\)\\|^KERBEROS\\|" |
28210 | 681 "^227 .*[Pp]assive\\|^200 EPRT\\|^500 .*EPRT") |
682 "*Regular expression matching ftp messages that can be ignored." | |
683 :group 'ange-ftp | |
684 :type 'regexp) | |
685 | |
686 (defcustom ange-ftp-fatal-msgs | |
687 (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|" | |
688 "^No control connection\\|unknown host\\|^lost connection") | |
689 "*Regular expression matching ftp messages that indicate serious errors. | |
690 | |
691 These mean that the FTP process should (or already has) been killed." | |
692 :group 'ange-ftp | |
693 :type 'regexp) | |
694 | |
695 (defcustom ange-ftp-gateway-fatal-msgs | |
696 "No route to host\\|Connection closed\\|No such host\\|Login incorrect" | |
697 "*Regular expression matching login failure messages from rlogin/telnet." | |
698 :group 'ange-ftp | |
699 :type 'regexp) | |
700 | |
701 (defcustom ange-ftp-xfer-size-msgs | |
702 "^150 .* connection for .* (\\([0-9]+\\) bytes)" | |
703 "*Regular expression used to determine the number of bytes in a FTP transfer." | |
704 :group 'ange-ftp | |
705 :type 'regexp) | |
706 | |
30459 | 707 (defcustom ange-ftp-tmp-name-template |
28210 | 708 (expand-file-name "ange-ftp" temporary-file-directory) |
709 "*Template used to create temporary files." | |
710 :group 'ange-ftp | |
711 :type 'directory) | |
712 | |
713 (defcustom ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp" | |
714 "*Template used to create temporary files when ftp-ing through a gateway. | |
715 | |
716 Files starting with this prefix need to be accessible from BOTH the local | |
717 machine and the gateway machine, and need to have the SAME name on both | |
718 machines, that is, /tmp is probably NOT what you want, since that is rarely | |
719 cross-mounted." | |
720 :group 'ange-ftp | |
721 :type 'directory) | |
722 | |
723 (defcustom ange-ftp-netrc-filename "~/.netrc" | |
724 "*File in .netrc format to search for passwords." | |
725 :group 'ange-ftp | |
726 :type 'file) | |
727 | |
728 (defcustom ange-ftp-disable-netrc-security-check (eq system-type 'windows-nt) | |
729 "*If non-nil avoid checking permissions on the .netrc file." | |
730 :group 'ange-ftp | |
731 :type 'boolean) | |
732 | |
733 (defcustom ange-ftp-default-user nil | |
734 "*User name to use when none is specified in a file name. | |
735 | |
736 If non-nil but not a string, you are prompted for the name. | |
737 If nil, the value of `ange-ftp-netrc-default-user' is used. | |
738 If that is nil too, then your login name is used. | |
739 | |
740 Once a connection to a given host has been initiated, the user name | |
741 and password information for that host are cached and re-used by | |
742 ange-ftp. Use \\[ange-ftp-set-user] to change the cached values, | |
743 since setting `ange-ftp-default-user' directly does not affect | |
744 the cached information." | |
745 :group 'ange-ftp | |
746 :type '(choice (const :tag "Default" nil) | |
747 string | |
748 (other :tag "Prompt" t))) | |
749 | |
750 (defcustom ange-ftp-netrc-default-user nil | |
751 "Alternate default user name to use when none is specified. | |
752 | |
753 This variable is set from the `default' command in your `.netrc' file, | |
754 if there is one." | |
755 :group 'ange-ftp | |
756 :type '(choice (const :tag "Default" nil) | |
757 string)) | |
758 | |
759 (defcustom ange-ftp-default-password nil | |
760 "*Password to use when the user name equals `ange-ftp-default-user'." | |
761 :group 'ange-ftp | |
762 :type '(choice (const :tag "Default" nil) | |
763 string)) | |
764 | |
765 (defcustom ange-ftp-default-account nil | |
766 "*Account to use when the user name equals `ange-ftp-default-user'." | |
767 :group 'ange-ftp | |
768 :type '(choice (const :tag "Default" nil) | |
769 string)) | |
770 | |
771 (defcustom ange-ftp-netrc-default-password nil | |
772 "*Password to use when the user name equals `ange-ftp-netrc-default-user'." | |
773 :group 'ange-ftp | |
774 :type '(choice (const :tag "Default" nil) | |
775 string)) | |
776 | |
777 (defcustom ange-ftp-netrc-default-account nil | |
778 "*Account to use when the user name equals `ange-ftp-netrc-default-user'." | |
779 :group 'ange-ftp | |
780 :type '(choice (const :tag "Default" nil) | |
781 string)) | |
782 | |
783 (defcustom ange-ftp-generate-anonymous-password t | |
784 "*If t, use value of `user-mail-address' as password for anonymous ftp. | |
785 | |
786 If a string, then use that string as the password. | |
787 If nil, prompt the user for a password." | |
788 :group 'ange-ftp | |
789 :type '(choice (const :tag "Prompt" nil) | |
790 string | |
791 (other :tag "User address" t))) | |
792 | |
793 (defcustom ange-ftp-dumb-unix-host-regexp nil | |
794 "*If non-nil, regexp matching hosts on which `dir' command lists directory." | |
795 :group 'ange-ftp | |
796 :type '(choice (const :tag "Default" nil) | |
797 string)) | |
798 | |
799 (defcustom ange-ftp-binary-file-name-regexp | |
800 (concat "\\.[zZ]$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|" | |
801 "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|" | |
802 "\\.EXE\\(;[0-9]+\\)?$\\|\\.[zZ]-part-..$\\|\\.gz$\\|" | |
803 "\\.taz$\\|\\.tgz$") | |
804 "*If a file matches this regexp then it is transferred in binary mode." | |
805 :group 'ange-ftp | |
806 :type 'regexp) | |
807 | |
808 (defcustom ange-ftp-gateway-host nil | |
809 "*Name of host to use as gateway machine when local FTP isn't possible." | |
810 :group 'ange-ftp | |
811 :type '(choice (const :tag "Default" nil) | |
812 string)) | |
813 | |
814 (defcustom ange-ftp-local-host-regexp ".*" | |
815 "*Regexp selecting hosts which can be reached directly with ftp. | |
816 | |
817 For other hosts the FTP process is started on \`ange-ftp-gateway-host\' | |
818 instead, and/or reached via \`ange-ftp-gateway-ftp-program-name\'." | |
819 :group 'ange-ftp | |
820 :type 'regexp) | |
821 | |
822 (defcustom ange-ftp-gateway-program-interactive nil | |
823 "*If non-nil then the gateway program should give a shell prompt. | |
824 | |
825 Both telnet and rlogin do something like this." | |
826 :group 'ange-ftp | |
827 :type 'boolean) | |
828 | |
829 (defcustom ange-ftp-gateway-program remote-shell-program | |
830 "*Name of program to spawn a shell on the gateway machine. | |
831 | |
832 Valid candidates are rsh (remsh on some systems), telnet and rlogin. See | |
833 also the gateway variable above." | |
834 :group 'ange-ftp | |
835 :type '(choice (const "rsh") | |
836 (const "telnet") | |
837 (const "rlogin") | |
838 string)) | |
839 | |
840 (defcustom ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *" | |
841 "*Regexp matching prompt after complete login sequence on gateway machine. | |
842 | |
843 A match for this means the shell is now awaiting input. Make this regexp as | |
844 strict as possible; it shouldn't match *anything* at all except the user's | |
845 initial prompt. The above string will fail under most SUN-3's since it | |
846 matches the login banner." | |
847 :group 'ange-ftp | |
848 :type 'regexp) | |
849 | |
850 (defvar ange-ftp-gateway-setup-term-command | |
851 (if (eq system-type 'hpux) | |
852 "stty -onlcr -echo\n" | |
853 "stty -echo nl\n") | |
854 "*Set up terminal after logging in to the gateway machine. | |
855 This command should stop the terminal from echoing each command, and | |
856 arrange to strip out trailing ^M characters.") | |
857 | |
858 (defcustom ange-ftp-smart-gateway nil | |
859 "*Non-nil means the ftp gateway and/or the gateway ftp program is smart. | |
860 | |
861 Don't bother telnetting, etc., already connected to desired host transparently, | |
862 or just issue a user@host command in case \`ange-ftp-gateway-host\' is non-nil." | |
863 :group 'ange-ftp | |
864 :type 'boolean) | |
865 | |
866 (defcustom ange-ftp-smart-gateway-port "21" | |
867 "*Port on gateway machine to use when smart gateway is in operation." | |
868 :group 'ange-ftp | |
869 :type 'string) | |
870 | |
871 (defcustom ange-ftp-send-hash t | |
872 "*If non-nil, send the HASH command to the FTP client." | |
873 :group 'ange-ftp | |
874 :type 'boolean) | |
875 | |
876 (defcustom ange-ftp-binary-hash-mark-size nil | |
877 "*Default size, in bytes, between hash-marks when transferring a binary file. | |
878 If nil, this variable will be locally overridden if the FTP client outputs a | |
879 suitable response to the HASH command. If non-nil, this value takes | |
880 precedence over the local value." | |
881 :group 'ange-ftp | |
882 :type '(choice (const :tag "Overridden" nil) | |
883 integer)) | |
884 | |
885 (defcustom ange-ftp-ascii-hash-mark-size 1024 | |
886 "*Default size, in bytes, between hash-marks when transferring an ASCII file. | |
887 This variable is buffer-local and will be locally overridden if the FTP client | |
888 outputs a suitable response to the HASH command." | |
889 :group 'ange-ftp | |
890 :type 'integer) | |
891 | |
892 (defcustom ange-ftp-process-verbose t | |
893 "*If non-nil then be chatty about interaction with the FTP process." | |
894 :group 'ange-ftp | |
895 :type 'boolean) | |
896 | |
897 (defcustom ange-ftp-ftp-program-name "ftp" | |
898 "*Name of FTP program to run." | |
899 :group 'ange-ftp | |
900 :type 'string) | |
901 | |
902 (defcustom ange-ftp-gateway-ftp-program-name "ftp" | |
903 "*Name of FTP program to run when accessing non-local hosts. | |
904 | |
905 Some AT&T folks claim to use something called `pftp' here." | |
906 :group 'ange-ftp | |
907 :type 'string) | |
908 | |
909 (defcustom ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v") | |
910 "*A list of arguments passed to the FTP program when started." | |
911 :group 'ange-ftp | |
912 :type '(repeat string)) | |
913 | |
914 (defcustom ange-ftp-nslookup-program nil | |
30459 | 915 "*If non-nil, this is a string naming the nslookup program." |
28210 | 916 :group 'ange-ftp |
917 :type '(choice (const :tag "None" nil) | |
918 string)) | |
919 | |
920 (defcustom ange-ftp-make-backup-files () | |
921 "*Non-nil means make backup files for \"magic\" remote files." | |
922 :group 'ange-ftp | |
923 :type 'boolean) | |
924 | |
925 (defcustom ange-ftp-retry-time 5 | |
926 "*Number of seconds to wait before retry if file or listing doesn't arrive. | |
927 This might need to be increased for very slow connections." | |
928 :group 'ange-ftp | |
929 :type 'integer) | |
930 | |
931 (defcustom ange-ftp-auto-save 0 | |
932 "If 1, allow ange-ftp files to be auto-saved. | |
933 If 0, inhibit auto-saving of ange-ftp files. | |
934 Don't use any other value." | |
935 :group 'ange-ftp | |
936 :type '(choice (const :tag "Suppress" 0) | |
937 (const :tag "Allow" 1))) | |
938 | |
939 (defcustom ange-ftp-try-passive-mode nil | |
940 "It t, try to use passive mode in ftp, if the client program | |
941 supports the `passive' command." | |
942 :group 'ange-ftp | |
943 :type 'boolean | |
944 :version 21.1) | |
945 | |
946 | |
947 ;;;; ------------------------------------------------------------ | |
948 ;;;; Hash table support. | |
949 ;;;; ------------------------------------------------------------ | |
950 | |
951 (require 'backquote) | |
952 | |
953 (defun ange-ftp-make-hashtable (&optional size) | |
954 "Make an obarray suitable for use as a hashtable. | |
955 SIZE, if supplied, should be a prime number." | |
956 (make-vector (or size 31) 0)) | |
957 | |
958 (defun ange-ftp-map-hashtable (fun tbl) | |
959 "Call FUNCTION on each key and value in HASHTABLE." | |
960 (mapatoms | |
30459 | 961 (function |
28210 | 962 (lambda (sym) |
963 (funcall fun (get sym 'key) (get sym 'val)))) | |
964 tbl)) | |
965 | |
966 (defmacro ange-ftp-make-hash-key (key) | |
967 "Convert KEY into a suitable key for a hashtable." | |
30459 | 968 `(if (stringp ,key) |
969 ,key | |
970 (prin1-to-string ,key))) | |
28210 | 971 |
972 (defun ange-ftp-get-hash-entry (key tbl) | |
973 "Return the value associated with KEY in HASHTABLE." | |
974 (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl))) | |
975 (and sym (get sym 'val)))) | |
976 | |
977 (defun ange-ftp-put-hash-entry (key val tbl) | |
978 "Record an association between KEY and VALUE in HASHTABLE." | |
979 (let ((sym (intern (ange-ftp-make-hash-key key) tbl))) | |
980 (put sym 'val val) | |
981 (put sym 'key key))) | |
982 | |
983 (defun ange-ftp-del-hash-entry (key tbl) | |
984 "Copy all symbols except KEY in HASHTABLE and return modified hashtable." | |
985 (let* ((len (length tbl)) | |
986 (new-tbl (ange-ftp-make-hashtable len)) | |
987 (i (1- len))) | |
988 (ange-ftp-map-hashtable | |
989 (function | |
990 (lambda (k v) | |
991 (or (equal k key) | |
992 (ange-ftp-put-hash-entry k v new-tbl)))) | |
993 tbl) | |
994 (while (>= i 0) | |
995 (aset tbl i (aref new-tbl i)) | |
996 (setq i (1- i))) | |
997 tbl)) | |
998 | |
999 (defun ange-ftp-hash-entry-exists-p (key tbl) | |
1000 "Return whether there is an association for KEY in TABLE." | |
1001 (intern-soft (ange-ftp-make-hash-key key) tbl)) | |
1002 | |
1003 (defun ange-ftp-hash-table-keys (tbl) | |
1004 "Return a sorted list of all the active keys in TABLE, as strings." | |
1005 (sort (all-completions "" tbl) | |
1006 (function string-lessp))) | |
1007 | |
1008 ;;;; ------------------------------------------------------------ | |
1009 ;;;; Internal variables. | |
1010 ;;;; ------------------------------------------------------------ | |
1011 | |
1012 (defvar ange-ftp-data-buffer-name " *ftp data*" | |
1013 "Buffer name to hold directory listing data received from ftp process.") | |
1014 | |
1015 (defvar ange-ftp-netrc-modtime nil | |
1016 "Last modified time of the netrc file from file-attributes.") | |
1017 | |
1018 (defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable) | |
1019 "Hash table holding associations between HOST, USER pairs.") | |
1020 | |
1021 (defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable) | |
1022 "Mapping between a HOST, USER pair and a PASSWORD for them. | |
1023 All HOST values should be in lower case.") | |
1024 | |
1025 (defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable) | |
1026 "Mapping between a HOST, USER pair and a ACCOUNT password for them.") | |
1027 | |
1028 (defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97) | |
1029 "Hash table for storing directories and their respective files.") | |
1030 | |
1031 (defvar ange-ftp-inodes-hashtable (ange-ftp-make-hashtable 97) | |
1032 "Hash table for storing file names and their \"inode numbers\".") | |
1033 | |
1034 (defvar ange-ftp-next-inode-number 1 | |
1035 "Next \"inode number\" value. We give each file name a unique number.") | |
1036 | |
1037 (defvar ange-ftp-ls-cache-lsargs nil | |
1038 "Last set of args used by ange-ftp-ls.") | |
1039 | |
1040 (defvar ange-ftp-ls-cache-file nil | |
1041 "Last file passed to ange-ftp-ls.") | |
1042 | |
1043 (defvar ange-ftp-ls-cache-res nil | |
1044 "Last result returned from ange-ftp-ls.") | |
1045 | |
1046 (defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable)) | |
1047 | |
1048 (defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):") | |
1049 | |
1050 ;; These are local variables in each FTP process buffer. | |
1051 (defvar ange-ftp-hash-mark-unit nil) | |
1052 (defvar ange-ftp-hash-mark-count nil) | |
1053 (defvar ange-ftp-xfer-size nil) | |
1054 (defvar ange-ftp-process-string nil) | |
1055 (defvar ange-ftp-process-result-line nil) | |
1056 (defvar ange-ftp-process-busy nil) | |
1057 (defvar ange-ftp-process-result nil) | |
1058 (defvar ange-ftp-process-multi-skip nil) | |
1059 (defvar ange-ftp-process-msg nil) | |
1060 (defvar ange-ftp-process-continue nil) | |
1061 (defvar ange-ftp-last-percent nil) | |
1062 | |
1063 ;; These variables are bound by one function and examined by another. | |
1064 ;; Leave them void globally for error checking. | |
1065 (defvar ange-ftp-this-file) | |
1066 (defvar ange-ftp-this-dir) | |
1067 (defvar ange-ftp-this-user) | |
1068 (defvar ange-ftp-this-host) | |
1069 (defvar ange-ftp-this-msg) | |
1070 (defvar ange-ftp-completion-ignored-pattern) | |
1071 (defvar ange-ftp-trample-marker) | |
1072 | |
1073 ;; New error symbols. | |
1074 (put 'ftp-error 'error-conditions '(ftp-error file-error error)) | |
1075 ;; (put 'ftp-error 'error-message "FTP error") | |
1076 | |
1077 ;;; ------------------------------------------------------------ | |
1078 ;;; Enhanced message support. | |
1079 ;;; ------------------------------------------------------------ | |
1080 | |
1081 (defun ange-ftp-message (fmt &rest args) | |
1082 "Display message in echo area, but indicate if truncated. | |
1083 Args are as in `message': a format string, plus arguments to be formatted." | |
1084 (let ((msg (apply (function format) fmt args)) | |
1085 (max (window-width (minibuffer-window)))) | |
1086 (if noninteractive | |
1087 msg | |
1088 (if (>= (length msg) max) | |
1089 ;; Take just the last MAX - 3 chars of the string. | |
1090 (setq msg (concat "> " (substring msg (- 3 max))))) | |
1091 (message "%s" msg)))) | |
1092 | |
1093 (defun ange-ftp-abbreviate-filename (file &optional new) | |
30459 | 1094 "Abbreviate the file name FILE relative to the `default-directory'. |
28210 | 1095 If the optional parameter NEW is given and the non-directory parts match, |
1096 only return the directory part of FILE." | |
1097 (save-match-data | |
1098 (if (and default-directory | |
1099 (string-match (concat "^" | |
1100 (regexp-quote default-directory) | |
1101 ".") file)) | |
1102 (setq file (substring file (1- (match-end 0))))) | |
1103 (if (and new | |
1104 (string-equal (file-name-nondirectory file) | |
1105 (file-name-nondirectory new))) | |
1106 (setq file (file-name-directory file))) | |
1107 (or file "./"))) | |
1108 | |
1109 ;;;; ------------------------------------------------------------ | |
1110 ;;;; User / Host mapping support. | |
1111 ;;;; ------------------------------------------------------------ | |
1112 | |
1113 (defun ange-ftp-set-user (host user) | |
1114 "For a given HOST, set or change the default USER." | |
1115 (interactive "sHost: \nsUser: ") | |
1116 (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable)) | |
1117 | |
1118 (defun ange-ftp-get-user (host) | |
1119 "Given a HOST, return the default USER." | |
1120 (ange-ftp-parse-netrc) | |
1121 (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable))) | |
1122 (or user | |
1123 (prog1 | |
1124 (setq user | |
1125 (cond ((stringp ange-ftp-default-user) | |
1126 ;; We have a default name. Use it. | |
1127 ange-ftp-default-user) | |
1128 (ange-ftp-default-user | |
1129 ;; Ask the user. | |
1130 (let ((enable-recursive-minibuffers t)) | |
1131 (read-string (format "User for %s: " host) | |
1132 (user-login-name)))) | |
1133 (ange-ftp-netrc-default-user) | |
1134 ;; Default to the user's login name. | |
1135 (t | |
1136 (user-login-name)))) | |
1137 (ange-ftp-set-user host user))))) | |
1138 | |
1139 ;;;; ------------------------------------------------------------ | |
1140 ;;;; Password support. | |
1141 ;;;; ------------------------------------------------------------ | |
1142 | |
1143 (defmacro ange-ftp-generate-passwd-key (host user) | |
30459 | 1144 `(concat (downcase ,host) "/" ,user)) |
28210 | 1145 |
1146 (defmacro ange-ftp-lookup-passwd (host user) | |
30459 | 1147 `(ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key ,host ,user) |
1148 ange-ftp-passwd-hashtable)) | |
28210 | 1149 |
1150 (defun ange-ftp-set-passwd (host user passwd) | |
1151 "For a given HOST and USER, set or change the associated PASSWORD." | |
1152 (interactive (list (read-string "Host: ") | |
1153 (read-string "User: ") | |
1154 (read-passwd "Password: "))) | |
1155 (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user) | |
1156 passwd | |
1157 ange-ftp-passwd-hashtable)) | |
1158 | |
1159 (defun ange-ftp-get-host-with-passwd (user) | |
1160 "Given a USER, return a host we know the password for." | |
1161 (ange-ftp-parse-netrc) | |
1162 (catch 'found-one | |
1163 (ange-ftp-map-hashtable | |
1164 (function (lambda (host val) | |
1165 (if (ange-ftp-lookup-passwd host user) | |
1166 (throw 'found-one host)))) | |
1167 ange-ftp-user-hashtable) | |
1168 (save-match-data | |
1169 (ange-ftp-map-hashtable | |
1170 (function | |
1171 (lambda (key value) | |
1172 (if (string-match "^[^/]*\\(/\\).*$" key) | |
1173 (let ((host (substring key 0 (match-beginning 1)))) | |
1174 (if (and (string-equal user (substring key (match-end 1))) | |
1175 value) | |
1176 (throw 'found-one host)))))) | |
1177 ange-ftp-passwd-hashtable)) | |
1178 nil)) | |
1179 | |
1180 (defun ange-ftp-get-passwd (host user) | |
1181 "Return the password for specified HOST and USER, asking user if necessary." | |
1182 (ange-ftp-parse-netrc) | |
1183 | |
1184 ;; look up password in the hash table first; user might have overridden the | |
1185 ;; defaults. | |
1186 (cond ((ange-ftp-lookup-passwd host user)) | |
30459 | 1187 |
28210 | 1188 ;; See if default user and password set. |
1189 ((and (stringp ange-ftp-default-user) | |
1190 ange-ftp-default-password | |
1191 (string-equal user ange-ftp-default-user)) | |
1192 ange-ftp-default-password) | |
30459 | 1193 |
28210 | 1194 ;; See if default user and password set from .netrc file. |
1195 ((and (stringp ange-ftp-netrc-default-user) | |
1196 ange-ftp-netrc-default-password | |
1197 (string-equal user ange-ftp-netrc-default-user)) | |
1198 ange-ftp-netrc-default-password) | |
30459 | 1199 |
28210 | 1200 ;; anonymous ftp password is handled specially since there is an |
1201 ;; unwritten rule about how that is used on the Internet. | |
1202 ((and (or (string-equal user "anonymous") | |
1203 (string-equal user "ftp")) | |
1204 ange-ftp-generate-anonymous-password) | |
1205 (if (stringp ange-ftp-generate-anonymous-password) | |
1206 ange-ftp-generate-anonymous-password | |
1207 user-mail-address)) | |
30459 | 1208 |
28210 | 1209 ;; see if same user has logged in to other hosts; if so then prompt |
1210 ;; with the password that was used there. | |
1211 (t | |
1212 (let* ((other (ange-ftp-get-host-with-passwd user)) | |
1213 (passwd (if other | |
30459 | 1214 |
28210 | 1215 ;; found another machine with the same user. |
1216 ;; Try that account. | |
1217 (read-passwd | |
1218 (format "passwd for %s@%s (default same as %s@%s): " | |
1219 user host user other) | |
1220 nil | |
1221 (ange-ftp-lookup-passwd other user)) | |
30459 | 1222 |
28210 | 1223 ;; I give up. Ask the user for the password. |
1224 (read-passwd | |
1225 (format "Password for %s@%s: " user host))))) | |
1226 (ange-ftp-set-passwd host user passwd) | |
1227 passwd)))) | |
1228 | |
1229 ;;;; ------------------------------------------------------------ | |
1230 ;;;; Account support | |
1231 ;;;; ------------------------------------------------------------ | |
1232 | |
1233 ;; Account passwords must be either specified in the .netrc file, or set | |
1234 ;; manually by calling ange-ftp-set-account. For the moment, ange-ftp doesn't | |
1235 ;; check to see whether the FTP process is actually prompting for an account | |
1236 ;; password. | |
30459 | 1237 |
28210 | 1238 (defun ange-ftp-set-account (host user account) |
1239 "For a given HOST and USER, set or change the associated ACCOUNT password." | |
1240 (interactive (list (read-string "Host: ") | |
1241 (read-string "User: ") | |
1242 (read-passwd "Account password: "))) | |
1243 (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user) | |
1244 account | |
1245 ange-ftp-account-hashtable)) | |
1246 | |
1247 (defun ange-ftp-get-account (host user) | |
1248 "Given a HOST and USER, return the FTP account." | |
1249 (ange-ftp-parse-netrc) | |
1250 (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user) | |
1251 ange-ftp-account-hashtable) | |
1252 (and (stringp ange-ftp-default-user) | |
1253 (string-equal user ange-ftp-default-user) | |
1254 ange-ftp-default-account) | |
1255 (and (stringp ange-ftp-netrc-default-user) | |
1256 (string-equal user ange-ftp-netrc-default-user) | |
1257 ange-ftp-netrc-default-account))) | |
1258 | |
1259 ;;;; ------------------------------------------------------------ | |
1260 ;;;; ~/.netrc support | |
1261 ;;;; ------------------------------------------------------------ | |
1262 | |
1263 (defun ange-ftp-chase-symlinks (file) | |
1264 "Return the filename that FILE references, following all symbolic links." | |
1265 (let (temp) | |
1266 (while (setq temp (ange-ftp-real-file-symlink-p file)) | |
1267 (setq file | |
1268 (if (file-name-absolute-p temp) | |
1269 temp | |
1270 (concat (file-name-directory file) temp))))) | |
1271 file) | |
1272 | |
1273 ;; Move along current line looking for the value of the TOKEN. | |
1274 ;; Valid separators between TOKEN and its value are commas and | |
1275 ;; whitespace. Second arg LIMIT is a limit for the search. | |
1276 | |
1277 (defun ange-ftp-parse-netrc-token (token limit) | |
1278 (if (search-forward token limit t) | |
1279 (let (beg) | |
1280 (skip-chars-forward ", \t\r\n" limit) | |
1281 (if (eq (following-char) ?\") ;quoted token value | |
1282 (progn (forward-char 1) | |
1283 (setq beg (point)) | |
1284 (skip-chars-forward "^\"" limit) | |
1285 (forward-char 1) | |
1286 (buffer-substring beg (1- (point)))) | |
1287 (setq beg (point)) | |
1288 (skip-chars-forward "^, \t\r\n" limit) | |
1289 (buffer-substring beg (point)))))) | |
1290 | |
1291 ;; Extract the values for the tokens `machine', `login', | |
1292 ;; `password' and `account' in the current buffer. If successful, | |
1293 ;; record the information found. | |
1294 | |
1295 (defun ange-ftp-parse-netrc-group () | |
1296 (let ((start (point)) | |
1297 (end (save-excursion | |
1298 (if (looking-at "machine\\>") | |
1299 ;; Skip `machine' and the machine name that follows. | |
1300 (progn | |
1301 (skip-chars-forward "^ \t\r\n") | |
1302 (skip-chars-forward " \t\r\n") | |
1303 (skip-chars-forward "^ \t\r\n")) | |
1304 ;; Skip `default'. | |
1305 (skip-chars-forward "^ \t\r\n")) | |
1306 ;; Find start of the next `machine' or `default' | |
1307 ;; or the end of the buffer. | |
1308 (if (re-search-forward "machine\\>\\|default\\>" nil t) | |
1309 (match-beginning 0) | |
1310 (point-max)))) | |
1311 machine login password account) | |
1312 (setq machine (ange-ftp-parse-netrc-token "machine" end) | |
1313 login (ange-ftp-parse-netrc-token "login" end) | |
1314 password (ange-ftp-parse-netrc-token "password" end) | |
1315 account (ange-ftp-parse-netrc-token "account" end)) | |
1316 (if (and machine login) | |
1317 ;; found a `machine` token. | |
1318 (progn | |
1319 (ange-ftp-set-user machine login) | |
1320 (ange-ftp-set-passwd machine login password) | |
1321 (and account | |
1322 (ange-ftp-set-account machine login account))) | |
1323 (goto-char start) | |
1324 (if (search-forward "default" end t) | |
1325 ;; found a `default' token | |
1326 (progn | |
1327 (setq login (ange-ftp-parse-netrc-token "login" end) | |
1328 password (ange-ftp-parse-netrc-token "password" end) | |
1329 account (ange-ftp-parse-netrc-token "account" end)) | |
1330 (and login | |
1331 (setq ange-ftp-netrc-default-user login)) | |
1332 (and password | |
1333 (setq ange-ftp-netrc-default-password password)) | |
1334 (and account | |
1335 (setq ange-ftp-netrc-default-account account))))) | |
1336 (goto-char end))) | |
1337 | |
1338 ;; Read in ~/.netrc, if one exists. If ~/.netrc file exists and has | |
1339 ;; the correct permissions then extract the \`machine\', \`login\', | |
1340 ;; \`password\' and \`account\' information from within. | |
1341 | |
1342 (defun ange-ftp-parse-netrc () | |
1343 ;; We set this before actually doing it to avoid the possibility | |
1344 ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file. | |
1345 (interactive) | |
1346 (let (file attr) | |
1347 (let ((default-directory "/")) | |
1348 (setq file (ange-ftp-chase-symlinks | |
1349 (ange-ftp-real-expand-file-name ange-ftp-netrc-filename))) | |
1350 (setq attr (ange-ftp-real-file-attributes file))) | |
1351 (if (and attr ; file exists. | |
1352 (not (equal (nth 5 attr) ange-ftp-netrc-modtime))) ; file changed | |
1353 (save-match-data | |
1354 (if (or ange-ftp-disable-netrc-security-check | |
1355 (and (eq (nth 2 attr) (user-uid)) ; Same uids. | |
1356 (string-match ".r..------" (nth 8 attr)))) | |
1357 (save-excursion | |
1358 ;; we are cheating a bit here. I'm trying to do the equivalent | |
1359 ;; of find-file on the .netrc file, but then nuke it afterwards. | |
1360 ;; with the bit of logic below we should be able to have | |
1361 ;; encrypted .netrc files. | |
1362 (set-buffer (generate-new-buffer "*ftp-.netrc*")) | |
1363 (ange-ftp-real-insert-file-contents file) | |
1364 (setq buffer-file-name file) | |
1365 (setq default-directory (file-name-directory file)) | |
1366 (normal-mode t) | |
1367 (mapcar 'funcall find-file-hooks) | |
1368 (setq buffer-file-name nil) | |
1369 (goto-char (point-min)) | |
1370 (skip-chars-forward " \t\r\n") | |
1371 (while (not (eobp)) | |
1372 (ange-ftp-parse-netrc-group)) | |
1373 (kill-buffer (current-buffer))) | |
1374 (ange-ftp-message "%s either not owned by you or badly protected." | |
1375 ange-ftp-netrc-filename) | |
1376 (sit-for 1)) | |
1377 (setq ange-ftp-netrc-modtime (nth 5 attr)))))) | |
1378 | |
1379 ;; Return a list of prefixes of the form 'user@host:' to be used when | |
1380 ;; completion is done in the root directory. | |
1381 | |
1382 (defun ange-ftp-generate-root-prefixes () | |
1383 (ange-ftp-parse-netrc) | |
1384 (save-match-data | |
1385 (let (res) | |
1386 (ange-ftp-map-hashtable | |
1387 (function | |
1388 (lambda (key value) | |
1389 (if (string-match "^[^/]*\\(/\\).*$" key) | |
1390 (let ((host (substring key 0 (match-beginning 1))) | |
1391 (user (substring key (match-end 1)))) | |
1392 (setq res (cons (list (concat user "@" host ":")) | |
1393 res)))))) | |
1394 ange-ftp-passwd-hashtable) | |
1395 (ange-ftp-map-hashtable | |
1396 (function (lambda (host user) | |
1397 (setq res (cons (list (concat host ":")) | |
1398 res)))) | |
1399 ange-ftp-user-hashtable) | |
1400 (or res (list nil))))) | |
1401 | |
1402 ;;;; ------------------------------------------------------------ | |
1403 ;;;; Remote file name syntax support. | |
1404 ;;;; ------------------------------------------------------------ | |
1405 | |
1406 (defmacro ange-ftp-ftp-name-component (n ns name) | |
1407 "Extract the Nth ftp file name component from NS." | |
30459 | 1408 `(let ((elt (nth ,n ,ns))) |
1409 (if (match-beginning elt) | |
1410 (substring ,name (match-beginning elt) (match-end elt))))) | |
28210 | 1411 |
1412 (defvar ange-ftp-ftp-name-arg "") | |
1413 (defvar ange-ftp-ftp-name-res nil) | |
1414 | |
1415 ;; Parse NAME according to `ange-ftp-name-format' (which see). | |
1416 ;; Returns a list (HOST USER NAME), or nil if NAME does not match the format. | |
1417 (defun ange-ftp-ftp-name (name) | |
1418 (if (string-equal name ange-ftp-ftp-name-arg) | |
1419 ange-ftp-ftp-name-res | |
1420 (setq ange-ftp-ftp-name-arg name | |
1421 ange-ftp-ftp-name-res | |
1422 (save-match-data | |
1423 (if (posix-string-match (car ange-ftp-name-format) name) | |
1424 (let* ((ns (cdr ange-ftp-name-format)) | |
1425 (host (ange-ftp-ftp-name-component 0 ns name)) | |
1426 (user (ange-ftp-ftp-name-component 1 ns name)) | |
1427 (name (ange-ftp-ftp-name-component 2 ns name))) | |
1428 (if (zerop (length user)) | |
1429 (setq user (ange-ftp-get-user host))) | |
1430 (list host user name)) | |
1431 nil))))) | |
1432 | |
1433 ;; Take a FULLNAME that matches according to ange-ftp-name-format and | |
1434 ;; replace the name component with NAME. | |
1435 (defun ange-ftp-replace-name-component (fullname name) | |
1436 (save-match-data | |
1437 (if (posix-string-match (car ange-ftp-name-format) fullname) | |
1438 (let* ((ns (cdr ange-ftp-name-format)) | |
1439 (elt (nth 2 ns))) | |
1440 (concat (substring fullname 0 (match-beginning elt)) | |
1441 name | |
1442 (substring fullname (match-end elt))))))) | |
1443 | |
1444 ;;;; ------------------------------------------------------------ | |
1445 ;;;; Miscellaneous utils. | |
1446 ;;;; ------------------------------------------------------------ | |
1447 | |
1448 ;; (setq ange-ftp-tmp-keymap (make-sparse-keymap)) | |
1449 ;; (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer) | |
1450 | |
1451 (defun ange-ftp-repaint-minibuffer () | |
1452 "Clear any existing minibuffer message; let the minibuffer contents show." | |
1453 (message nil)) | |
1454 | |
1455 ;; Return the name of the buffer that collects output from the ftp process | |
1456 ;; connected to the given HOST and USER pair. | |
1457 (defun ange-ftp-ftp-process-buffer (host user) | |
1458 (concat "*ftp " user "@" host "*")) | |
1459 | |
1460 ;; Display the last chunk of output from the ftp process for the given HOST | |
1461 ;; USER pair, and signal an error including MSG in the text. | |
1462 (defun ange-ftp-error (host user msg) | |
1463 (let ((cur (selected-window)) | |
1464 (pop-up-windows t)) | |
1465 (pop-to-buffer | |
1466 (get-buffer-create | |
1467 (ange-ftp-ftp-process-buffer host user))) | |
1468 (goto-char (point-max)) | |
1469 (select-window cur)) | |
1470 (signal 'ftp-error (list (format "FTP Error: %s" msg)))) | |
1471 | |
1472 (defun ange-ftp-set-buffer-mode () | |
1473 "Set correct modes for the current buffer if visiting a remote file." | |
1474 (if (and (stringp buffer-file-name) | |
1475 (ange-ftp-ftp-name buffer-file-name)) | |
1476 (auto-save-mode ange-ftp-auto-save))) | |
1477 | |
1478 (defun ange-ftp-kill-ftp-process (&optional buffer) | |
1479 "Kill the FTP process associated with BUFFER (the current buffer, if nil). | |
1480 If the BUFFER's visited filename or default-directory is an ftp filename | |
1481 then kill the related ftp process." | |
1482 (interactive "bKill FTP process associated with buffer: ") | |
1483 (if (null buffer) | |
1484 (setq buffer (current-buffer)) | |
1485 (setq buffer (get-buffer buffer))) | |
1486 (let ((file (or (buffer-file-name buffer) | |
1487 (save-excursion (set-buffer buffer) default-directory)))) | |
1488 (if file | |
1489 (let ((parsed (ange-ftp-ftp-name (expand-file-name file)))) | |
1490 (if parsed | |
1491 (let ((host (nth 0 parsed)) | |
1492 (user (nth 1 parsed))) | |
1493 (kill-buffer (get-buffer (ange-ftp-ftp-process-buffer host user))))))))) | |
1494 | |
1495 (defun ange-ftp-quote-string (string) | |
1496 "Quote any characters in STRING that may confuse the ftp process." | |
1497 (apply (function concat) | |
1498 (mapcar (function | |
1499 ;; This is said to be wrong; ftp is said to | |
1500 ;; need quoting only for ", and that by doubling it. | |
1501 ;; But experiment says this kind of quoting is correct | |
1502 ;; when talking to ftp on GNU/Linux systems. | |
1503 (lambda (char) | |
1504 (if (or (<= char ? ) | |
1505 (> char ?\~) | |
1506 (= char ?\") | |
1507 (= char ?\\)) | |
1508 (vector ?\\ char) | |
1509 (vector char)))) | |
1510 string))) | |
1511 | |
1512 (defun ange-ftp-barf-if-not-directory (directory) | |
1513 (or (file-directory-p directory) | |
1514 (signal 'file-error | |
1515 (list "Opening directory" | |
1516 (if (file-exists-p directory) | |
1517 "not a directory" | |
1518 "no such file or directory") | |
1519 directory)))) | |
1520 | |
1521 ;;;; ------------------------------------------------------------ | |
1522 ;;;; FTP process filter support. | |
1523 ;;;; ------------------------------------------------------------ | |
1524 | |
1525 (defun ange-ftp-process-handle-line (line proc) | |
1526 "Look at the given LINE from the ftp process PROC. | |
1527 Try to categorize it into one of four categories: | |
1528 good, skip, fatal, or unknown." | |
1529 (cond ((string-match ange-ftp-xfer-size-msgs line) | |
1530 (setq ange-ftp-xfer-size | |
1531 (ash (string-to-int (substring line | |
1532 (match-beginning 1) | |
1533 (match-end 1))) | |
1534 -10))) | |
1535 ((string-match ange-ftp-skip-msgs line) | |
1536 t) | |
1537 ((string-match ange-ftp-good-msgs line) | |
1538 (setq ange-ftp-process-busy nil | |
1539 ange-ftp-process-result t | |
1540 ange-ftp-process-result-line line)) | |
1541 ;; Check this before checking for errors. | |
1542 ;; Otherwise the last line of these three seems to be an error: | |
1543 ;; 230-see a significant impact from the move. For those of you who can't | |
1544 ;; 230-use DNS to resolve hostnames and get an error message like | |
1545 ;; 230-"ftp.stsci.edu: unknown host", the new IP address will be... | |
1546 ((string-match ange-ftp-multi-msgs line) | |
1547 (setq ange-ftp-process-multi-skip t)) | |
1548 ((string-match ange-ftp-fatal-msgs line) | |
1549 (delete-process proc) | |
1550 (setq ange-ftp-process-busy nil | |
1551 ange-ftp-process-result-line line)) | |
1552 (ange-ftp-process-multi-skip | |
1553 t) | |
1554 (t | |
1555 (setq ange-ftp-process-busy nil | |
1556 ange-ftp-process-result-line line)))) | |
1557 | |
1558 (defun ange-ftp-set-xfer-size (host user bytes) | |
1559 "Set the size of the next FTP transfer in bytes." | |
1560 (let ((proc (ange-ftp-get-process host user))) | |
1561 (if proc | |
1562 (let ((buf (process-buffer proc))) | |
1563 (if buf | |
1564 (save-excursion | |
1565 (set-buffer buf) | |
1566 (setq ange-ftp-xfer-size (ash bytes -10)))))))) | |
1567 | |
1568 (defun ange-ftp-process-handle-hash (str) | |
1569 "Remove hash marks from STRING and display count so far." | |
1570 (setq str (concat (substring str 0 (match-beginning 0)) | |
1571 (substring str (match-end 0))) | |
1572 ange-ftp-hash-mark-count (+ (- (match-end 0) | |
1573 (match-beginning 0)) | |
1574 ange-ftp-hash-mark-count)) | |
1575 (and ange-ftp-hash-mark-unit | |
1576 ange-ftp-process-msg | |
1577 ange-ftp-process-verbose | |
1578 (not (eq (selected-window) (minibuffer-window))) | |
1579 (not (boundp 'search-message)) ;screws up isearch otherwise | |
1580 (not cursor-in-echo-area) ;screws up y-or-n-p otherwise | |
1581 (let ((kbytes (ash (* ange-ftp-hash-mark-unit | |
1582 ange-ftp-hash-mark-count) | |
1583 -6))) | |
1584 (if (zerop ange-ftp-xfer-size) | |
1585 (ange-ftp-message "%s...%dk" ange-ftp-process-msg kbytes) | |
1586 (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size))) | |
1587 ;; cut out the redisplay of identical %-age messages. | |
1588 (if (not (eq percent ange-ftp-last-percent)) | |
1589 (progn | |
1590 (setq ange-ftp-last-percent percent) | |
1591 (ange-ftp-message "%s...%d%%" ange-ftp-process-msg percent))))))) | |
1592 str) | |
1593 | |
1594 ;; Call the function specified by CONT. CONT can be either a function | |
1595 ;; or a list of a function and some args. The first two parameters | |
1596 ;; passed to the function will be RESULT and LINE. The remaining args | |
1597 ;; will be taken from CONT if a list was passed. | |
1598 | |
1599 (defun ange-ftp-call-cont (cont result line) | |
1600 (if cont | |
1601 (if (and (listp cont) | |
1602 (not (eq (car cont) 'lambda))) | |
1603 (apply (car cont) result line (cdr cont)) | |
1604 (funcall cont result line)))) | |
1605 | |
1606 ;; Build up a complete line of output from the ftp PROCESS and pass it | |
1607 ;; on to ange-ftp-process-handle-line to deal with. | |
1608 | |
1609 (defun ange-ftp-process-filter (proc str) | |
1610 (let ((buffer (process-buffer proc)) | |
1611 (old-buffer (current-buffer))) | |
1612 | |
1613 ;; Eliminate nulls. | |
1614 (while (string-match "\000+" str) | |
30459 | 1615 (setq str (replace-match "" nil nil str))) |
28210 | 1616 |
1617 ;; see if the buffer is still around... it could have been deleted. | |
1618 (if (buffer-name buffer) | |
1619 (unwind-protect | |
1620 (progn | |
1621 (set-buffer (process-buffer proc)) | |
30459 | 1622 |
28210 | 1623 ;; handle hash mark printing |
1624 (and ange-ftp-process-busy | |
1625 (string-match "^#+$" str) | |
1626 (setq str (ange-ftp-process-handle-hash str))) | |
1627 (comint-output-filter proc str) | |
1628 ;; Replace STR by the result of the comint processing. | |
1629 (setq str (buffer-substring comint-last-output-start | |
1630 (process-mark proc))) | |
1631 (if ange-ftp-process-busy | |
1632 (progn | |
1633 (setq ange-ftp-process-string (concat ange-ftp-process-string | |
1634 str)) | |
30459 | 1635 |
28210 | 1636 ;; if we gave an empty password to the USER command earlier |
1637 ;; then we should send a null password now. | |
1638 (if (string-match "Password: *$" ange-ftp-process-string) | |
1639 (send-string proc "\n")))) | |
1640 (while (and ange-ftp-process-busy | |
1641 (string-match "\n" ange-ftp-process-string)) | |
1642 (let ((line (substring ange-ftp-process-string | |
1643 0 | |
1644 (match-beginning 0)))) | |
1645 (setq ange-ftp-process-string (substring ange-ftp-process-string | |
1646 (match-end 0))) | |
1647 (while (string-match "^ftp> *" line) | |
1648 (setq line (substring line (match-end 0)))) | |
1649 (ange-ftp-process-handle-line line proc))) | |
1650 | |
1651 ;; has the ftp client finished? if so then do some clean-up | |
1652 ;; actions. | |
1653 (if (not ange-ftp-process-busy) | |
1654 (progn | |
1655 ;; reset the xfer size | |
1656 (setq ange-ftp-xfer-size 0) | |
1657 | |
1658 ;; issue the "done" message since we've finished. | |
1659 (if (and ange-ftp-process-msg | |
1660 ange-ftp-process-verbose | |
1661 ange-ftp-process-result) | |
1662 (progn | |
1663 (ange-ftp-message "%s...done" ange-ftp-process-msg) | |
1664 (ange-ftp-repaint-minibuffer) | |
1665 (setq ange-ftp-process-msg nil))) | |
30459 | 1666 |
28210 | 1667 ;; is there a continuation we should be calling? if so, |
1668 ;; we'd better call it, making sure we only call it once. | |
1669 (if ange-ftp-process-continue | |
1670 (let ((cont ange-ftp-process-continue)) | |
1671 (setq ange-ftp-process-continue nil) | |
1672 (ange-ftp-call-cont cont | |
1673 ange-ftp-process-result | |
1674 ange-ftp-process-result-line)))))) | |
1675 (set-buffer old-buffer))))) | |
1676 | |
1677 (defun ange-ftp-process-sentinel (proc str) | |
1678 "When ftp process changes state, nuke all file-entries in cache." | |
1679 (let ((name (process-name proc))) | |
1680 (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)\\*" name) | |
1681 (let ((user (substring name (match-beginning 1) (match-end 1))) | |
1682 (host (substring name (match-beginning 2) (match-end 2)))) | |
1683 (ange-ftp-wipe-file-entries host user)))) | |
1684 (setq ange-ftp-ls-cache-file nil)) | |
1685 | |
1686 ;;;; ------------------------------------------------------------ | |
1687 ;;;; Gateway support. | |
1688 ;;;; ------------------------------------------------------------ | |
1689 | |
1690 (defun ange-ftp-use-gateway-p (host) | |
1691 "Returns whether to access this host via a normal (non-smart) gateway." | |
1692 ;; yes, I know that I could simplify the following expression, but it is | |
1693 ;; clearer (to me at least) this way. | |
1694 (and (not ange-ftp-smart-gateway) | |
1695 (save-match-data | |
1696 (not (string-match ange-ftp-local-host-regexp host))))) | |
1697 | |
1698 (defun ange-ftp-use-smart-gateway-p (host) | |
1699 "Returns whether to access this host via a smart gateway." | |
1700 (and ange-ftp-smart-gateway | |
1701 (save-match-data | |
1702 (not (string-match ange-ftp-local-host-regexp host))))) | |
1703 | |
1704 | |
1705 ;;; ------------------------------------------------------------ | |
1706 ;;; Temporary file location and deletion... | |
1707 ;;; ------------------------------------------------------------ | |
1708 | |
1709 (defun ange-ftp-make-tmp-name (host) | |
1710 "This routine will return the name of a new file." | |
1711 (make-temp-file (if (ange-ftp-use-gateway-p host) | |
1712 ange-ftp-gateway-tmp-name-template | |
1713 ange-ftp-tmp-name-template))) | |
1714 | |
1715 (defalias 'ange-ftp-del-tmp-name 'delete-file) | |
1716 | |
1717 ;;;; ------------------------------------------------------------ | |
1718 ;;;; Interactive gateway program support. | |
1719 ;;;; ------------------------------------------------------------ | |
1720 | |
1721 (defvar ange-ftp-gwp-running t) | |
1722 (defvar ange-ftp-gwp-status nil) | |
1723 | |
1724 (defun ange-ftp-gwp-sentinel (proc str) | |
1725 (setq ange-ftp-gwp-running nil)) | |
1726 | |
1727 (defun ange-ftp-gwp-filter (proc str) | |
1728 (comint-output-filter proc str) | |
1729 (save-excursion | |
1730 (set-buffer (process-buffer proc)) | |
1731 ;; Replace STR by the result of the comint processing. | |
1732 (setq str (buffer-substring comint-last-output-start (process-mark proc)))) | |
1733 (cond ((string-match "login: *$" str) | |
1734 (send-string proc | |
1735 (concat | |
1736 (let ((ange-ftp-default-user t)) | |
1737 (ange-ftp-get-user ange-ftp-gateway-host)) | |
1738 "\n"))) | |
1739 ((string-match "Password: *$" str) | |
1740 (send-string proc | |
1741 (concat | |
1742 (ange-ftp-get-passwd ange-ftp-gateway-host | |
1743 (ange-ftp-get-user | |
1744 ange-ftp-gateway-host)) | |
1745 "\n"))) | |
1746 ((string-match ange-ftp-gateway-fatal-msgs str) | |
1747 (delete-process proc) | |
1748 (setq ange-ftp-gwp-running nil)) | |
1749 ((string-match ange-ftp-gateway-prompt-pattern str) | |
1750 (setq ange-ftp-gwp-running nil | |
1751 ange-ftp-gwp-status t)))) | |
1752 | |
1753 (defun ange-ftp-gwp-start (host user name args) | |
1754 "Login to the gateway machine and fire up an ftp process." | |
1755 (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host)) | |
1756 ;; It would be nice to make process-connection-type nil, | |
1757 ;; but that doesn't work: ftp never responds. | |
1758 ;; Can anyone find a fix for that? | |
1759 (proc (let ((process-connection-type t)) | |
1760 (start-process name name | |
1761 ange-ftp-gateway-program | |
1762 ange-ftp-gateway-host))) | |
1763 (ftp (mapconcat (function identity) args " "))) | |
1764 (process-kill-without-query proc) | |
1765 (set-process-sentinel proc (function ange-ftp-gwp-sentinel)) | |
1766 (set-process-filter proc (function ange-ftp-gwp-filter)) | |
1767 (save-excursion | |
1768 (set-buffer (process-buffer proc)) | |
1769 (goto-char (point-max)) | |
1770 (set-marker (process-mark proc) (point))) | |
1771 (setq ange-ftp-gwp-running t | |
1772 ange-ftp-gwp-status nil) | |
1773 (ange-ftp-message "Connecting to gateway %s..." ange-ftp-gateway-host) | |
1774 (while ange-ftp-gwp-running ;perform login sequence | |
1775 (accept-process-output proc)) | |
1776 (if (not ange-ftp-gwp-status) | |
1777 (ange-ftp-error host user "unable to login to gateway")) | |
1778 (ange-ftp-message "Connecting to gateway %s...done" ange-ftp-gateway-host) | |
1779 (setq ange-ftp-gwp-running t | |
1780 ange-ftp-gwp-status nil) | |
1781 (process-send-string proc ange-ftp-gateway-setup-term-command) | |
1782 (while ange-ftp-gwp-running ;zap ^M's and double echoing. | |
1783 (accept-process-output proc)) | |
1784 (if (not ange-ftp-gwp-status) | |
1785 (ange-ftp-error host user "unable to set terminal modes on gateway")) | |
1786 (setq ange-ftp-gwp-running t | |
1787 ange-ftp-gwp-status nil) | |
1788 (process-send-string proc (concat "exec " ftp "\n")) ;spawn ftp process | |
1789 proc)) | |
1790 | |
1791 ;;;; ------------------------------------------------------------ | |
1792 ;;;; Support for sending commands to the ftp process. | |
1793 ;;;; ------------------------------------------------------------ | |
1794 | |
1795 (defun ange-ftp-raw-send-cmd (proc cmd &optional msg cont nowait) | |
1796 "Low-level routine to send the given ftp CMD to the ftp PROCESS. | |
1797 MSG is an optional message to output before and after the command. | |
30459 | 1798 If CONT is non-nil then it is either a function or a list of function |
1799 and some arguments. The function will be called when the ftp command | |
1800 has completed. | |
1801 If CONT is nil then this routine will return \(RESULT . LINE\) where RESULT | |
28210 | 1802 is whether the command was successful, and LINE is the line from the FTP |
1803 process that caused the command to complete. | |
1804 If NOWAIT is given then the routine will return immediately the command has | |
1805 been queued with no result. CONT will still be called, however." | |
1806 (if (memq (process-status proc) '(run open)) | |
1807 (save-excursion | |
1808 (set-buffer (process-buffer proc)) | |
1809 (ange-ftp-wait-not-busy proc) | |
1810 (setq ange-ftp-process-string "" | |
1811 ange-ftp-process-result-line "" | |
1812 ange-ftp-process-busy t | |
1813 ange-ftp-process-result nil | |
1814 ange-ftp-process-multi-skip nil | |
1815 ange-ftp-process-msg msg | |
1816 ange-ftp-process-continue cont | |
1817 ange-ftp-hash-mark-count 0 | |
1818 ange-ftp-last-percent -1 | |
1819 cmd (concat cmd "\n")) | |
1820 (and msg ange-ftp-process-verbose (ange-ftp-message "%s..." msg)) | |
1821 (goto-char (point-max)) | |
1822 (move-marker comint-last-input-start (point)) | |
1823 ;; don't insert the password into the buffer on the USER command. | |
1824 (save-match-data | |
1825 (if (string-match "^user \"[^\"]*\"" cmd) | |
1826 (insert (substring cmd 0 (match-end 0)) " Turtle Power!\n") | |
1827 (insert cmd))) | |
1828 (move-marker comint-last-input-end (point)) | |
1829 (send-string proc cmd) | |
1830 (set-marker (process-mark proc) (point)) | |
1831 (if nowait | |
1832 nil | |
1833 (ange-ftp-wait-not-busy proc) | |
1834 (if cont | |
1835 nil ;cont has already been called | |
1836 (cons ange-ftp-process-result ange-ftp-process-result-line)))))) | |
1837 | |
1838 ;; Wait for the ange-ftp process PROC not to be busy. | |
1839 (defun ange-ftp-wait-not-busy (proc) | |
1840 (save-excursion | |
1841 (set-buffer (process-buffer proc)) | |
1842 (condition-case nil | |
1843 ;; This is a kludge to let user quit in case ftp gets hung. | |
1844 ;; It matters because this function can be called from the filter. | |
1845 ;; It is bad to allow quitting in a filter, but getting hung | |
1846 ;; is worse. By binding quit-flag to nil, we might avoid | |
1847 ;; most of the probability of getting screwed because the user | |
1848 ;; wants to quit some command. | |
1849 (let ((quit-flag nil) | |
1850 (inhibit-quit nil)) | |
1851 (while ange-ftp-process-busy | |
1852 (accept-process-output proc))) | |
1853 (quit | |
1854 ;; If the user does quit out of this, | |
1855 ;; kill the process. That stops any transfer in progress. | |
1856 ;; The next operation will open a new ftp connection. | |
1857 (delete-process proc) | |
1858 (signal 'quit nil))))) | |
1859 | |
1860 (defun ange-ftp-nslookup-host (host) | |
1861 "Attempt to resolve the given HOSTNAME using nslookup if possible." | |
1862 (interactive "sHost: ") | |
1863 (if ange-ftp-nslookup-program | |
1864 (let ((default-directory | |
1865 (if (file-accessible-directory-p default-directory) | |
1866 default-directory | |
1867 exec-directory)) | |
1868 ;; It would be nice to make process-connection-type nil, | |
1869 ;; but that doesn't work: ftp never responds. | |
1870 ;; Can anyone find a fix for that? | |
1871 (proc (let ((process-connection-type t)) | |
1872 (start-process " *nslookup*" " *nslookup*" | |
1873 ange-ftp-nslookup-program host))) | |
1874 (res host)) | |
1875 (process-kill-without-query proc) | |
1876 (save-excursion | |
1877 (set-buffer (process-buffer proc)) | |
1878 (while (memq (process-status proc) '(run open)) | |
1879 (accept-process-output proc)) | |
1880 (goto-char (point-min)) | |
1881 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t) | |
1882 (setq res (buffer-substring (match-beginning 1) | |
1883 (match-end 1)))) | |
1884 (kill-buffer (current-buffer))) | |
1885 res) | |
1886 host)) | |
1887 | |
1888 (defun ange-ftp-start-process (host user name) | |
1889 "Spawn a new ftp process ready to connect to machine HOST and give it NAME. | |
1890 If HOST is only ftp-able through a gateway machine then spawn a shell | |
1891 on the gateway machine to do the ftp instead." | |
1892 (let* ((use-gateway (ange-ftp-use-gateway-p host)) | |
1893 (use-smart-ftp (and (not ange-ftp-gateway-host) | |
1894 (ange-ftp-use-smart-gateway-p host))) | |
1895 (ftp-prog (if (or use-gateway | |
30459 | 1896 use-smart-ftp) |
28210 | 1897 ange-ftp-gateway-ftp-program-name |
1898 ange-ftp-ftp-program-name)) | |
1899 (args (append (list ftp-prog) ange-ftp-ftp-program-args)) | |
1900 ;; Without the following binding, ange-ftp-start-process | |
1901 ;; recurses on file-accessible-directory-p, since it needs to | |
1902 ;; restart its process in order to determine anything about | |
1903 ;; default-directory. | |
1904 (file-name-handler-alist) | |
1905 (default-directory | |
1906 (if (file-accessible-directory-p default-directory) | |
1907 default-directory | |
1908 exec-directory)) | |
1909 proc) | |
1910 ;; It would be nice to make process-connection-type nil, | |
1911 ;; but that doesn't work: ftp never responds. | |
1912 ;; Can anyone find a fix for that? | |
1913 (let ((process-connection-type t) | |
1914 (process-environment process-environment) | |
1915 (buffer (get-buffer-create name))) | |
1916 (save-excursion | |
1917 (set-buffer buffer) | |
1918 (internal-ange-ftp-mode)) | |
1919 ;; This tells GNU ftp not to output any fancy escape sequences. | |
1920 (setenv "TERM" "dumb") | |
1921 (if use-gateway | |
1922 (if ange-ftp-gateway-program-interactive | |
1923 (setq proc (ange-ftp-gwp-start host user name args)) | |
1924 (setq proc (apply 'start-process name name | |
1925 (append (list ange-ftp-gateway-program | |
1926 ange-ftp-gateway-host) | |
1927 args)))) | |
1928 (setq proc (apply 'start-process name name args)))) | |
1929 (save-excursion | |
1930 (set-buffer (process-buffer proc)) | |
1931 (goto-char (point-max)) | |
1932 (set-marker (process-mark proc) (point))) | |
1933 (process-kill-without-query proc) | |
1934 (set-process-sentinel proc (function ange-ftp-process-sentinel)) | |
1935 (set-process-filter proc (function ange-ftp-process-filter)) | |
1936 ;; On Windows, the standard ftp client buffers its output (because | |
1937 ;; stdout is a pipe handle) so the startup message may never appear: | |
1938 ;; `accept-process-output' at this point would hang indefinitely. | |
1939 ;; However, sending an innocuous command ("help foo") forces some | |
1940 ;; output that will be ignored, which is just as good. Once we | |
1941 ;; start sending normal commands, the output no longer appears to be | |
1942 ;; buffered, and everything works correctly. My guess is that the | |
1943 ;; output of interest is being sent to stderr which is not buffered. | |
1944 (when (eq system-type 'windows-nt) | |
1945 ;; force ftp output to be treated as DOS text, otherwise the | |
1946 ;; output of "help foo" confuses the EOL detection logic. | |
1947 (set-process-coding-system proc 'raw-text-dos) | |
1948 (process-send-string proc "help foo\n")) | |
1949 (accept-process-output proc) ;wait for ftp startup message | |
1950 proc)) | |
1951 | |
1952 (put 'internal-ange-ftp-mode 'mode-class 'special) | |
1953 | |
1954 (defun internal-ange-ftp-mode () | |
1955 "Major mode for interacting with the FTP process. | |
1956 | |
1957 \\{comint-mode-map}" | |
1958 (interactive) | |
1959 (comint-mode) | |
1960 (setq major-mode 'internal-ange-ftp-mode) | |
1961 (setq mode-name "Internal Ange-ftp") | |
1962 (let ((proc (get-buffer-process (current-buffer)))) | |
1963 (make-local-variable 'ange-ftp-process-string) | |
1964 (setq ange-ftp-process-string "") | |
1965 (make-local-variable 'ange-ftp-process-busy) | |
1966 (make-local-variable 'ange-ftp-process-result) | |
1967 (make-local-variable 'ange-ftp-process-msg) | |
1968 (make-local-variable 'ange-ftp-process-multi-skip) | |
1969 (make-local-variable 'ange-ftp-process-result-line) | |
1970 (make-local-variable 'ange-ftp-process-continue) | |
1971 (make-local-variable 'ange-ftp-hash-mark-count) | |
1972 (make-local-variable 'ange-ftp-binary-hash-mark-size) | |
1973 (make-local-variable 'ange-ftp-ascii-hash-mark-size) | |
1974 (make-local-variable 'ange-ftp-hash-mark-unit) | |
1975 (make-local-variable 'ange-ftp-xfer-size) | |
1976 (make-local-variable 'ange-ftp-last-percent) | |
1977 (setq ange-ftp-hash-mark-count 0) | |
1978 (setq ange-ftp-xfer-size 0) | |
1979 (setq ange-ftp-process-result-line "") | |
1980 | |
1981 (setq comint-prompt-regexp "^ftp> ") | |
1982 (make-local-variable 'comint-password-prompt-regexp) | |
1983 ;; This is a regexp that can't match anything. | |
1984 ;; ange-ftp has its own ways of handling passwords. | |
1985 (setq comint-password-prompt-regexp "^a\\'z") | |
1986 (make-local-variable 'paragraph-start) | |
1987 (setq paragraph-start comint-prompt-regexp))) | |
1988 | |
1989 (defun ange-ftp-smart-login (host user pass account proc) | |
1990 "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT. | |
1991 PROC is the FTP-client's process. This routine uses the smart-gateway | |
30459 | 1992 host specified in `ange-ftp-gateway-host'." |
28210 | 1993 (let ((result (ange-ftp-raw-send-cmd |
1994 proc | |
1995 (format "open %s %s" | |
1996 (ange-ftp-nslookup-host ange-ftp-gateway-host) | |
1997 ange-ftp-smart-gateway-port) | |
1998 (format "Opening FTP connection to %s via %s" | |
1999 host | |
2000 ange-ftp-gateway-host)))) | |
2001 (or (car result) | |
30459 | 2002 (ange-ftp-error host user |
28210 | 2003 (concat "OPEN request failed: " |
2004 (cdr result)))) | |
2005 (setq result (ange-ftp-raw-send-cmd | |
2006 proc (format "user \"%s\"@%s %s %s" | |
2007 user | |
2008 (ange-ftp-nslookup-host host) | |
2009 pass | |
2010 account) | |
2011 (format "Logging in as user %s@%s" | |
2012 user host))) | |
2013 (or (car result) | |
2014 (progn | |
2015 (ange-ftp-set-passwd host user nil) ; reset password | |
2016 (ange-ftp-set-account host user nil) ; reset account | |
2017 (ange-ftp-error host user | |
2018 (concat "USER request failed: " | |
2019 (cdr result))))))) | |
2020 | |
2021 (defun ange-ftp-normal-login (host user pass account proc) | |
2022 "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT. | |
2023 PROC is the process to the FTP-client. HOST may have an optional | |
2024 suffix of the form #PORT to specify a non-default port" | |
2025 (save-match-data | |
2026 (string-match "^\\([^#]+\\)\\(#\\([0-9]+\\)\\)?\\'" host) | |
2027 (let* ((nshost (ange-ftp-nslookup-host (match-string 1 host))) | |
2028 (port (match-string 3 host)) | |
2029 (result (ange-ftp-raw-send-cmd | |
2030 proc | |
2031 (if port | |
2032 (format "open %s %s" nshost port) | |
2033 (format "open %s" nshost)) | |
2034 (format "Opening FTP connection to %s" host)))) | |
2035 (or (car result) | |
2036 (ange-ftp-error host user | |
2037 (concat "OPEN request failed: " | |
2038 (cdr result)))) | |
2039 (setq result (ange-ftp-raw-send-cmd | |
2040 proc | |
2041 (if (and (ange-ftp-use-smart-gateway-p host) | |
2042 ange-ftp-gateway-host) | |
2043 (format "user \"%s\"@%s %s %s" user nshost pass account) | |
2044 (format "user \"%s\" %s %s" user pass account)) | |
2045 (format "Logging in as user %s@%s" user host))) | |
2046 (or (car result) | |
2047 (progn | |
2048 (ange-ftp-set-passwd host user nil) ;reset password. | |
2049 (ange-ftp-set-account host user nil) ;reset account. | |
2050 (ange-ftp-error host user | |
2051 (concat "USER request failed: " | |
2052 (cdr result)))))))) | |
2053 | |
2054 ;; ange@hplb.hpl.hp.com says this should not be changed. | |
2055 (defvar ange-ftp-hash-mark-msgs | |
2056 "[hH]ash mark [^0-9]*\\([0-9]+\\)" | |
2057 "*Regexp matching the FTP client's output upon doing a HASH command.") | |
2058 | |
2059 (defun ange-ftp-guess-hash-mark-size (proc) | |
2060 (if ange-ftp-send-hash | |
2061 (save-excursion | |
2062 (set-buffer (process-buffer proc)) | |
2063 (let* ((status (ange-ftp-raw-send-cmd proc "hash")) | |
2064 (result (car status)) | |
2065 (line (cdr status))) | |
2066 (save-match-data | |
2067 (if (string-match ange-ftp-hash-mark-msgs line) | |
2068 (let ((size (string-to-int | |
2069 (substring line | |
2070 (match-beginning 1) | |
2071 (match-end 1))))) | |
2072 (setq ange-ftp-ascii-hash-mark-size size | |
2073 ange-ftp-hash-mark-unit (ash size -4)) | |
2074 | |
2075 ;; if a default value for this is set, use that value. | |
2076 (or ange-ftp-binary-hash-mark-size | |
2077 (setq ange-ftp-binary-hash-mark-size size))))))))) | |
2078 | |
2079 (defun ange-ftp-get-process (host user) | |
2080 "Return an FTP subprocess connected to HOST and logged in as USER. | |
2081 Create a new process if needed." | |
2082 (let* ((name (ange-ftp-ftp-process-buffer host user)) | |
2083 (proc (get-process name))) | |
2084 (if (and proc (memq (process-status proc) '(run open))) | |
2085 proc | |
2086 ;; Must delete dead process so that new process can reuse the name. | |
2087 (if proc (delete-process proc)) | |
2088 (let ((pass (ange-ftp-quote-string | |
2089 (ange-ftp-get-passwd host user))) | |
2090 (account (ange-ftp-quote-string | |
2091 (ange-ftp-get-account host user)))) | |
2092 ;; grab a suitable process. | |
2093 (setq proc (ange-ftp-start-process host user name)) | |
30459 | 2094 |
28210 | 2095 ;; login to FTP server. |
2096 (if (and (ange-ftp-use-smart-gateway-p host) | |
2097 ange-ftp-gateway-host) | |
2098 (ange-ftp-smart-login host user pass account proc) | |
2099 (ange-ftp-normal-login host user pass account proc)) | |
30459 | 2100 |
28210 | 2101 ;; Tell client to send back hash-marks as progress. It isn't usually |
2102 ;; fatal if this command fails. | |
2103 (ange-ftp-guess-hash-mark-size proc) | |
2104 | |
2105 ;; Guess at the host type. | |
2106 (ange-ftp-guess-host-type host user) | |
2107 | |
2108 ;; Try to use passive mode if asked to. | |
2109 (when ange-ftp-try-passive-mode | |
2110 (let ((answer (cdr (ange-ftp-raw-send-cmd | |
2111 proc "passive" "Trying passive mode..." nil)))) | |
2112 (if (string-match "\\?\\|refused" answer) | |
2113 (message "Trying passive mode...ok") | |
2114 (message "Trying passive mode...failed")))) | |
2115 | |
2116 ;; Run any user-specified hooks. Note that proc, host and user are | |
2117 ;; dynamically bound at this point. | |
2118 (run-hooks 'ange-ftp-process-startup-hook)) | |
2119 proc))) | |
2120 | |
2121 ;; Variables for caching host and host-type | |
2122 (defvar ange-ftp-host-cache nil) | |
2123 (defvar ange-ftp-host-type-cache nil) | |
2124 | |
2125 ;; If ange-ftp-host-type is called with the optional user | |
2126 ;; argument, it will attempt to guess the host type by connecting | |
2127 ;; as user, if necessary. For efficiency, I have tried to give this | |
2128 ;; optional second argument only when necessary. Have I missed any calls | |
2129 ;; to ange-ftp-host-type where it should have been supplied? | |
2130 | |
2131 (defun ange-ftp-host-type (host &optional user) | |
2132 "Return a symbol which represents the type of the HOST given. | |
2133 If the optional argument USER is given, attempts to guess the | |
2134 host-type by logging in as USER." | |
2135 (cond ((null host) 'unix) | |
2136 ;; Return `unix' if HOST is nil, since that's the most vanilla | |
2137 ;; possible return value. | |
2138 ((eq host ange-ftp-host-cache) | |
2139 ange-ftp-host-type-cache) | |
2140 ;; Trigger an ftp connection, in case we need to guess at the host type. | |
2141 ((and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache)) | |
2142 ange-ftp-host-type-cache) | |
2143 (t | |
2144 (setq ange-ftp-host-cache host | |
2145 ange-ftp-host-type-cache | |
2146 (cond ((ange-ftp-dumb-unix-host host) | |
2147 'dumb-unix) | |
2148 ;; ((and (fboundp 'ange-ftp-vos-host) | |
2149 ;; (ange-ftp-vos-host host)) | |
2150 ;; 'vos) | |
2151 ((and (fboundp 'ange-ftp-vms-host) | |
2152 (ange-ftp-vms-host host)) | |
2153 'vms) | |
2154 ((and (fboundp 'ange-ftp-mts-host) | |
2155 (ange-ftp-mts-host host)) | |
2156 'mts) | |
2157 ((and (fboundp 'ange-ftp-cms-host) | |
2158 (ange-ftp-cms-host host)) | |
2159 'cms) | |
2160 (t | |
2161 'unix)))))) | |
2162 | |
2163 ;; It would be nice to abstract the functions ange-ftp-TYPE-host and | |
2164 ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions | |
2165 ;; without sacrificing speed. Also, having separate variables | |
2166 ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to | |
2167 ;; set an alist to indicate that a host is of a given type. Even with | |
2168 ;; automatic host type recognition, setting a regexp is still a good idea | |
2169 ;; (for efficiency) if you log into a particular non-UNIX host frequently. | |
2170 | |
2171 (defvar ange-ftp-fix-name-func-alist nil | |
2172 "Alist saying how to convert file name to the host's syntax. | |
2173 Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine | |
2174 which can change a UNIX file name into a name more suitable for a host of type | |
2175 TYPE.") | |
2176 | |
2177 (defvar ange-ftp-fix-dir-name-func-alist nil | |
2178 "Alist saying how to convert directory name to the host's syntax. | |
2179 Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine | |
2180 which can change UNIX directory name into a directory name more suitable | |
2181 for a host of type TYPE.") | |
2182 | |
2183 ;; *** Perhaps the sense of this variable should be inverted, since there | |
2184 ;; *** is only 1 host type that can take ls-style listing options. | |
2185 (defvar ange-ftp-dumb-host-types '(dumb-unix) | |
2186 "List of host types that can't take UNIX ls-style listing options.") | |
2187 | |
2188 (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait) | |
2189 "Find an ftp process connected to HOST logged in as USER and send it CMD. | |
2190 MSG is an optional status message to be output before and after issuing the | |
2191 command. | |
30459 | 2192 See the documentation for `ange-ftp-raw-send-cmd' for a description of CONT |
28210 | 2193 and NOWAIT." |
2194 ;; Handle conversion to remote file name syntax and remote ls option | |
2195 ;; capability. | |
2196 (let ((cmd0 (car cmd)) | |
2197 (cmd1 (nth 1 cmd)) | |
2198 (ange-ftp-this-user user) | |
2199 (ange-ftp-this-host host) | |
2200 (ange-ftp-this-msg msg) | |
2201 cmd2 cmd3 host-type fix-name-func) | |
2202 | |
2203 (cond | |
30459 | 2204 |
28210 | 2205 ;; pwd case (We don't care what host-type.) |
2206 ((null cmd1)) | |
30459 | 2207 |
28210 | 2208 ;; cmd == 'dir "remote-name" "local-name" "ls-switches" |
2209 ((progn | |
2210 (setq cmd2 (nth 2 cmd) | |
2211 host-type (ange-ftp-host-type host user)) | |
2212 ;; This will trigger an FTP login, if one doesn't exist | |
2213 (eq cmd0 'dir)) | |
2214 (setq cmd1 (funcall | |
2215 (or (cdr (assq host-type ange-ftp-fix-dir-name-func-alist)) | |
2216 'identity) | |
2217 cmd1) | |
2218 cmd3 (nth 3 cmd)) | |
2219 ;; Need to deal with the HP-UX ftp bug. This should also allow | |
2220 ;; us to resolve symlinks to directories on SysV machines. (Sebastian will | |
2221 ;; be happy.) | |
2222 (and (eq host-type 'unix) | |
2223 (string-match "/$" cmd1) | |
2224 (not (string-match "R" cmd3)) | |
2225 (setq cmd1 (concat cmd1 "."))) | |
2226 | |
2227 ;; If the dir name contains a space, some ftp servers will | |
2228 ;; refuse to list it. We instead change directory to the | |
2229 ;; directory in question and ls ".". | |
2230 (when (string-match " " cmd1) | |
2231 (ange-ftp-cd host user (nth 1 cmd)) | |
2232 (setq cmd1 ".")) | |
2233 | |
2234 ;; If the remote ls can take switches, put them in | |
2235 (or (memq host-type ange-ftp-dumb-host-types) | |
2236 (setq cmd0 'ls | |
2237 cmd1 (format "\"%s %s\"" cmd3 cmd1)))) | |
30459 | 2238 |
28210 | 2239 ;; First argument is the remote name |
2240 ((progn | |
2241 (setq fix-name-func (or (cdr (assq host-type | |
2242 ange-ftp-fix-name-func-alist)) | |
2243 'identity)) | |
2244 (memq cmd0 '(get delete mkdir rmdir cd))) | |
2245 (setq cmd1 (funcall fix-name-func cmd1))) | |
2246 | |
2247 ;; Second argument is the remote name | |
2248 ((memq cmd0 '(append put chmod)) | |
2249 (setq cmd2 (funcall fix-name-func cmd2))) | |
2250 | |
2251 ;; Both arguments are remote names | |
2252 ((eq cmd0 'rename) | |
2253 (setq cmd1 (funcall fix-name-func cmd1) | |
2254 cmd2 (funcall fix-name-func cmd2)))) | |
30459 | 2255 |
2256 ;; Turn the command into one long string | |
28210 | 2257 (setq cmd0 (symbol-name cmd0)) |
2258 (setq cmd (concat cmd0 | |
2259 (and cmd1 (concat " " cmd1)) | |
2260 (and cmd2 (concat " " cmd2)))) | |
2261 | |
2262 ;; Actually send the resulting command. | |
2263 (let (afsc-result | |
2264 afsc-line) | |
2265 (ange-ftp-raw-send-cmd | |
2266 (ange-ftp-get-process host user) | |
2267 cmd | |
2268 msg | |
30459 | 2269 (list (lambda (result line host user cmd msg cont nowait) |
2270 (or cont (setq afsc-result result | |
2271 afsc-line line)) | |
2272 (if result (ange-ftp-call-cont cont result line) | |
2273 (ange-ftp-raw-send-cmd | |
2274 (ange-ftp-get-process host user) | |
2275 cmd | |
2276 msg | |
2277 (list (lambda (result line cont) | |
2278 (or cont (setq afsc-result result | |
2279 afsc-line line)) | |
2280 (ange-ftp-call-cont cont result line)) | |
2281 cont)) | |
2282 nowait)) | |
2283 host user cmd msg cont nowait) | |
28210 | 2284 nowait) |
2285 | |
2286 (if nowait | |
2287 nil | |
2288 (if cont | |
2289 nil | |
2290 (cons afsc-result afsc-line)))))) | |
2291 | |
2292 ;; It might be nice to message users about the host type identified, | |
2293 ;; but there is so much other messaging going on, it would not be | |
2294 ;; seen. No point in slowing things down just so users can read | |
2295 ;; a host type message. | |
2296 | |
2297 (defconst ange-ftp-cms-name-template | |
2298 (concat | |
2299 "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?" | |
2300 "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$")) | |
2301 (defconst ange-ftp-vms-name-template | |
2302 "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$") | |
2303 (defconst ange-ftp-mts-name-template | |
2304 "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$") | |
2305 | |
2306 (defun ange-ftp-guess-host-type (host user) | |
2307 "Guess at the the host type of HOST. | |
2308 Works by doing a pwd and examining the directory syntax." | |
2309 (let ((host-type (ange-ftp-host-type host)) | |
2310 (key (concat host "/" user "/~"))) | |
2311 (if (eq host-type 'unix) | |
2312 ;; Note that ange-ftp-host-type returns unix as the default value. | |
2313 (save-match-data | |
2314 (let* ((result (ange-ftp-get-pwd host user)) | |
2315 (dir (car result)) | |
2316 fix-name-func) | |
2317 (cond ((null dir) | |
2318 (message "Warning! Unable to get home directory") | |
2319 (sit-for 1) | |
2320 (if (string-match | |
2321 "^450 No current working directory defined$" | |
2322 (cdr result)) | |
30459 | 2323 |
28210 | 2324 ;; We'll assume that if pwd bombs with this |
2325 ;; error message, then it's CMS. | |
2326 (progn | |
2327 (ange-ftp-add-cms-host host) | |
2328 (setq ange-ftp-host-cache host | |
2329 ange-ftp-host-type-cache 'cms)))) | |
2330 | |
2331 ;; try for VMS | |
2332 ((string-match ange-ftp-vms-name-template dir) | |
2333 (ange-ftp-add-vms-host host) | |
2334 ;; The add-host functions clear the host type cache. | |
2335 ;; Therefore, need to set the cache afterwards. | |
2336 (setq ange-ftp-host-cache host | |
2337 ange-ftp-host-type-cache 'vms)) | |
2338 | |
2339 ;; try for MTS | |
2340 ((string-match ange-ftp-mts-name-template dir) | |
2341 (ange-ftp-add-mts-host host) | |
2342 (setq ange-ftp-host-cache host | |
2343 ange-ftp-host-type-cache 'mts)) | |
30459 | 2344 |
28210 | 2345 ;; try for CMS |
2346 ((string-match ange-ftp-cms-name-template dir) | |
2347 (ange-ftp-add-cms-host host) | |
2348 (setq ange-ftp-host-cache host | |
2349 ange-ftp-host-type-cache 'cms)) | |
2350 | |
2351 ;; assume UN*X | |
2352 (t | |
2353 (setq ange-ftp-host-cache host | |
2354 ange-ftp-host-type-cache 'unix))) | |
2355 | |
2356 ;; Now that we have done a pwd, might as well put it in | |
2357 ;; the expand-dir hashtable. | |
2358 (let ((ange-ftp-this-user user) | |
2359 (ange-ftp-this-host host)) | |
2360 (setq fix-name-func (cdr (assq ange-ftp-host-type-cache | |
2361 ange-ftp-fix-name-func-alist))) | |
2362 (if fix-name-func | |
2363 (setq dir (funcall fix-name-func dir 'reverse)))) | |
2364 (ange-ftp-put-hash-entry key dir | |
2365 ange-ftp-expand-dir-hashtable)))) | |
2366 | |
2367 ;; In the special case of CMS make sure that know the | |
2368 ;; expansion of the home minidisk now, because we will | |
2369 ;; be doing a lot of cd's. | |
2370 (if (and (eq host-type 'cms) | |
2371 (not (ange-ftp-hash-entry-exists-p | |
2372 key ange-ftp-expand-dir-hashtable))) | |
2373 (let ((dir (car (ange-ftp-get-pwd host user)))) | |
2374 (if dir | |
2375 (ange-ftp-put-hash-entry key (concat "/" dir) | |
2376 ange-ftp-expand-dir-hashtable) | |
2377 (message "Warning! Unable to get home directory") | |
2378 (sit-for 1)))))) | |
2379 | |
2380 | |
2381 ;;;; ------------------------------------------------------------ | |
2382 ;;;; Remote file and directory listing support. | |
2383 ;;;; ------------------------------------------------------------ | |
2384 | |
2385 ;; Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands | |
2386 ;; to take switch arguments. | |
2387 (defun ange-ftp-dumb-unix-host (host) | |
2388 (and host ange-ftp-dumb-unix-host-regexp | |
2389 (save-match-data | |
2390 (string-match ange-ftp-dumb-unix-host-regexp host)))) | |
2391 | |
2392 (defun ange-ftp-add-dumb-unix-host (host) | |
2393 "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp." | |
2394 (interactive | |
2395 (list (read-string "Host: " | |
2396 (let ((name (or (buffer-file-name) default-directory))) | |
2397 (and name (car (ange-ftp-ftp-name name))))))) | |
2398 (if (not (ange-ftp-dumb-unix-host host)) | |
2399 (setq ange-ftp-dumb-unix-host-regexp | |
2400 (concat "^" (regexp-quote host) "$" | |
2401 (and ange-ftp-dumb-unix-host-regexp "\\|") | |
2402 ange-ftp-dumb-unix-host-regexp) | |
2403 ange-ftp-host-cache nil))) | |
2404 | |
2405 (defvar ange-ftp-parse-list-func-alist nil | |
2406 "Alist saying how to parse directory listings for certain OS types. | |
2407 Association list of \( TYPE \. FUNC \) pairs. The FUNC is a routine | |
2408 which can parse the output from a DIR listing for a host of type TYPE.") | |
2409 | |
2410 ;; With no-error nil, this function returns: | |
2411 ;; an error if file is not an ange-ftp-name | |
30459 | 2412 ;; (This should never happen.) |
28210 | 2413 ;; an error if either the listing is unreadable or there is an ftp error. |
2414 ;; the listing (a string), if everything works. | |
30459 | 2415 ;; |
28210 | 2416 ;; With no-error t, it returns: |
2417 ;; an error if not an ange-ftp-name | |
2418 ;; error if listing is unreadable (most likely caused by a slow connection) | |
2419 ;; nil if ftp error (this is because although asking to list a nonexistent | |
2420 ;; directory on a remote unix machine usually (except | |
2421 ;; maybe for dumb hosts) returns an ls error, but no | |
2422 ;; ftp error, if the same is done on a VMS machine, | |
2423 ;; an ftp error is returned. Need to trap the error | |
2424 ;; so we can go on and try to list the parent.) | |
2425 ;; the listing, if everything works. | |
2426 | |
2427 ;; If WILDCARD is non-nil, then this implements the guts of insert-directory | |
2428 ;; in the wildcard case. Then we make a relative directory listing | |
2429 ;; of FILE within the directory specified by `default-directory'. | |
2430 | |
2431 (defvar ange-ftp-before-parse-ls-hook nil | |
2432 "Normal hook run before parsing the text of an ftp directory listing.") | |
2433 | |
37813
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2434 (defvar ange-ftp-after-parse-ls-hook nil |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2435 "Normal hook run after parsing the text of an ftp directory listing.") |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2436 |
28210 | 2437 (defun ange-ftp-ls (file lsargs parse &optional no-error wildcard) |
2438 "Return the output of an `DIR' or `ls' command done over ftp. | |
2439 FILE is the full name of the remote file, LSARGS is any args to pass to the | |
2440 `ls' command, and PARSE specifies that the output should be parsed and stored | |
2441 away in the internal cache." | |
2442 ;; If parse is t, we assume that file is a directory. i.e. we only parse | |
2443 ;; full directory listings. | |
2444 (let* ((ange-ftp-this-file (ange-ftp-expand-file-name file)) | |
2445 (parsed (ange-ftp-ftp-name ange-ftp-this-file))) | |
2446 (if parsed | |
2447 (let* ((host (nth 0 parsed)) | |
2448 (user (nth 1 parsed)) | |
2449 (name (ange-ftp-quote-string (nth 2 parsed))) | |
2450 (key (directory-file-name ange-ftp-this-file)) | |
2451 (host-type (ange-ftp-host-type host user)) | |
2452 (dumb (memq host-type ange-ftp-dumb-host-types)) | |
2453 result | |
2454 temp | |
2455 lscmd parse-func) | |
2456 (if (string-equal name "") | |
2457 (setq name | |
2458 (ange-ftp-real-file-name-as-directory | |
2459 (ange-ftp-expand-dir host user "~")))) | |
2460 (if (and ange-ftp-ls-cache-file | |
2461 (string-equal key ange-ftp-ls-cache-file) | |
2462 ;; Don't care about lsargs for dumb hosts. | |
2463 (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs))) | |
2464 ange-ftp-ls-cache-res | |
2465 (setq temp (ange-ftp-make-tmp-name host)) | |
2466 (if wildcard | |
2467 (progn | |
2468 (ange-ftp-cd host user (file-name-directory name)) | |
2469 (setq lscmd (list 'dir file temp lsargs))) | |
2470 (setq lscmd (list 'dir name temp lsargs))) | |
2471 (unwind-protect | |
2472 (if (car (setq result (ange-ftp-send-cmd | |
2473 host | |
2474 user | |
2475 lscmd | |
2476 (format "Listing %s" | |
2477 (ange-ftp-abbreviate-filename | |
2478 ange-ftp-this-file))))) | |
2479 (save-excursion | |
2480 (set-buffer (get-buffer-create | |
2481 ange-ftp-data-buffer-name)) | |
2482 (erase-buffer) | |
2483 (if (ange-ftp-real-file-readable-p temp) | |
2484 (ange-ftp-real-insert-file-contents temp) | |
2485 (sleep-for ange-ftp-retry-time) | |
2486 ;wait for file to possibly appear | |
2487 (if (ange-ftp-real-file-readable-p temp) | |
2488 ;; Try again. | |
2489 (ange-ftp-real-insert-file-contents temp) | |
2490 (ange-ftp-error host user | |
2491 (format | |
2492 "list data file %s not readable" | |
2493 temp)))) | |
30518
ba9bd1bf0ef8
Ange-ftp handles the output of the w32-style clients
Sam Steingold <sds@gnu.org>
parents:
30481
diff
changeset
|
2494 ;; remove ^M inserted by the win32 ftp client |
ba9bd1bf0ef8
Ange-ftp handles the output of the w32-style clients
Sam Steingold <sds@gnu.org>
parents:
30481
diff
changeset
|
2495 (while (re-search-forward "\r$" nil t) |
ba9bd1bf0ef8
Ange-ftp handles the output of the w32-style clients
Sam Steingold <sds@gnu.org>
parents:
30481
diff
changeset
|
2496 (replace-match "")) |
ba9bd1bf0ef8
Ange-ftp handles the output of the w32-style clients
Sam Steingold <sds@gnu.org>
parents:
30481
diff
changeset
|
2497 (goto-char 1) |
28210 | 2498 (run-hooks 'ange-ftp-before-parse-ls-hook) |
2499 (if parse | |
2500 (ange-ftp-set-files | |
2501 ange-ftp-this-file | |
2502 (if (setq | |
2503 parse-func | |
2504 (cdr (assq host-type | |
2505 ange-ftp-parse-list-func-alist))) | |
2506 (funcall parse-func) | |
2507 (ange-ftp-parse-dired-listing lsargs)))) | |
37813
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2508 ;; Place this hook here to convert the contents of the |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2509 ;; buffer to a ls compatible format if the host system |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2510 ;; that is being queried is other than Unix i.e. VMS |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2511 ;; returns an ls format that really sucks. |
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
2512 (run-hooks 'ange-ftp-after-parse-ls-hook) |
28210 | 2513 (setq ange-ftp-ls-cache-file key |
2514 ange-ftp-ls-cache-lsargs lsargs | |
2515 ; For dumb hosts-types this is | |
2516 ; meaningless but harmless. | |
2517 ange-ftp-ls-cache-res (buffer-string)) | |
2518 ;; (kill-buffer (current-buffer)) | |
2519 ange-ftp-ls-cache-res) | |
2520 (if no-error | |
2521 nil | |
2522 (ange-ftp-error host user | |
2523 (concat "DIR failed: " (cdr result))))) | |
2524 (ange-ftp-del-tmp-name temp)))) | |
2525 (error "Should never happen. Please report. Bug ref. no.: 1")))) | |
2526 | |
2527 ;;;; ------------------------------------------------------------ | |
2528 ;;;; Directory information caching support. | |
2529 ;;;; ------------------------------------------------------------ | |
2530 | |
2531 (defconst ange-ftp-date-regexp | |
2532 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)") | |
2533 ;; In some locales, month abbreviations are as short as 2 letters, | |
2534 ;; and they can be padded on the right with spaces. | |
2535 ;; weiand: changed: month ends with . or , or ., | |
2536 ;;old (month (concat l l "+ *")) | |
2537 (month (concat l l "+[.]?,? *")) | |
30459 | 2538 ;; Recognize any non-ASCII character. |
28210 | 2539 ;; The purpose is to match a Kanji character. |
2540 (k "[^\0-\177]") | |
2541 (s " ") | |
2542 (mm "[ 0-1][0-9]") | |
2543 ;; weiand: changed: day ends with . | |
2544 ;;old (dd "[ 0-3][0-9]") | |
2545 (dd "[ 0-3][0-9][.]?") | |
2546 (western (concat "\\(" month s dd "\\|" dd s month "\\)")) | |
2547 (japanese (concat mm k s dd k))) | |
2548 ;; Require the previous column to end in a digit. | |
2549 ;; This avoids recognizing `1 may 1997' as a date in the line: | |
2550 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README | |
2551 (concat "[0-9]" s "\\(" western "\\|" japanese "\\)" s)) | |
2552 "Regular expression to match up to the column before the file name in a | |
2553 directory listing. This regular expression is designed to recognize dates | |
2554 regardless of the language.") | |
2555 | |
2556 (defvar ange-ftp-add-file-entry-alist nil | |
2557 "Alist saying how to add file entries on certain OS types. | |
2558 Association list of pairs \( TYPE \. FUNC \), where FUNC | |
2559 is a function to be used to add a file entry for the OS TYPE. The | |
2560 main reason for this alist is to deal with file versions in VMS.") | |
2561 | |
2562 (defvar ange-ftp-delete-file-entry-alist nil | |
2563 "Alist saying how to delete files on certain OS types. | |
2564 Association list of pairs \( TYPE \. FUNC \), where FUNC | |
2565 is a function to be used to delete a file entry for the OS TYPE. | |
2566 The main reason for this alist is to deal with file versions in VMS.") | |
2567 | |
2568 (defun ange-ftp-add-file-entry (name &optional dir-p) | |
2569 "Add a file entry for file NAME, if its directory info exists." | |
2570 (funcall (or (cdr (assq (ange-ftp-host-type | |
2571 (car (ange-ftp-ftp-name name))) | |
2572 ange-ftp-add-file-entry-alist)) | |
2573 'ange-ftp-internal-add-file-entry) | |
2574 name dir-p) | |
2575 (setq ange-ftp-ls-cache-file nil)) | |
2576 | |
2577 (defun ange-ftp-delete-file-entry (name &optional dir-p) | |
2578 "Delete the file entry for file NAME, if its directory info exists." | |
2579 (funcall (or (cdr (assq (ange-ftp-host-type | |
2580 (car (ange-ftp-ftp-name name))) | |
2581 ange-ftp-delete-file-entry-alist)) | |
2582 'ange-ftp-internal-delete-file-entry) | |
2583 name dir-p) | |
2584 (setq ange-ftp-ls-cache-file nil)) | |
2585 | |
2586 (defmacro ange-ftp-parse-filename () | |
2587 ;;Extract the filename from the current line of a dired-like listing. | |
30459 | 2588 `(let ((eol (progn (end-of-line) (point)))) |
2589 (beginning-of-line) | |
2590 (if (re-search-forward ange-ftp-date-regexp eol t) | |
2591 (progn | |
2592 (skip-chars-forward " ") | |
2593 (skip-chars-forward "^ " eol) | |
2594 (skip-chars-forward " " eol) | |
2595 ;; We bomb on filenames starting with a space. | |
2596 (buffer-substring (point) eol))))) | |
2597 | |
28210 | 2598 ;; This deals with the F switch. Should also do something about |
2599 ;; unquoting names obtained with the SysV b switch and the GNU Q | |
2600 ;; switch. See Sebastian's dired-get-filename. | |
2601 | |
2602 (defmacro ange-ftp-ls-parser () | |
2603 ;; Note that switches is dynamically bound. | |
2604 ;; Meant to be called by ange-ftp-parse-dired-listing | |
30459 | 2605 `(let ((tbl (ange-ftp-make-hashtable)) |
2606 (used-F (and (stringp switches) | |
2607 (string-match "F" switches))) | |
2608 file-type symlink directory file) | |
2609 (while (setq file (ange-ftp-parse-filename)) | |
2610 (beginning-of-line) | |
2611 (skip-chars-forward "\t 0-9") | |
2612 (setq file-type (following-char) | |
2613 directory (eq file-type ?d)) | |
2614 (if (eq file-type ?l) | |
2615 (if (string-match " -> " file) | |
2616 (setq symlink (substring file (match-end 0)) | |
2617 file (substring file 0 (match-beginning 0))) | |
2618 ;; Shouldn't happen | |
2619 (setq symlink "")) | |
2620 (setq symlink nil)) | |
2621 ;; Only do a costly regexp search if the F switch was used. | |
2622 (if (and used-F | |
2623 (not (string-equal file "")) | |
2624 (looking-at | |
2625 ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)")) | |
2626 (let ((socket (eq file-type ?s)) | |
2627 (executable | |
2628 (and (not symlink) ; x bits don't mean a thing for symlinks | |
2629 (string-match | |
2630 "[xst]" | |
2631 (concat (buffer-substring | |
2632 (match-beginning 1) (match-end 1)) | |
2633 (buffer-substring | |
2634 (match-beginning 2) (match-end 2)) | |
2635 (buffer-substring | |
2636 (match-beginning 3) (match-end 3))))))) | |
2637 ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX) | |
2638 ;; and others don't. (sigh...) Beware, that some Unix's don't | |
2639 ;; seem to believe in the F-switch | |
2640 (if (or (and symlink (string-match "@$" file)) | |
2641 (and directory (string-match "/$" file)) | |
2642 (and executable (string-match "*$" file)) | |
2643 (and socket (string-match "=$" file))) | |
2644 (setq file (substring file 0 -1))))) | |
2645 (ange-ftp-put-hash-entry file (or symlink directory) tbl) | |
2646 (forward-line 1)) | |
2647 (ange-ftp-put-hash-entry "." t tbl) | |
2648 (ange-ftp-put-hash-entry ".." t tbl) | |
2649 tbl)) | |
28210 | 2650 |
2651 ;;; The dl stuff for descriptive listings | |
2652 | |
2653 (defvar ange-ftp-dl-dir-regexp nil | |
2654 "Regexp matching directories which are listed in dl format. | |
2655 This regexp should not be anchored with a trailing `$', because it should | |
2656 match subdirectories as well.") | |
2657 | |
2658 (defun ange-ftp-add-dl-dir (dir) | |
2659 "Interactively adds a DIR to ange-ftp-dl-dir-regexp." | |
2660 (interactive | |
2661 (list (read-string "Directory: " | |
2662 (let ((name (or (buffer-file-name) default-directory))) | |
2663 (and name (ange-ftp-ftp-name name) | |
2664 (file-name-directory name)))))) | |
2665 (if (not (and ange-ftp-dl-dir-regexp | |
2666 (string-match ange-ftp-dl-dir-regexp dir))) | |
2667 (setq ange-ftp-dl-dir-regexp | |
2668 (concat "^" (regexp-quote dir) | |
2669 (and ange-ftp-dl-dir-regexp "\\|") | |
2670 ange-ftp-dl-dir-regexp)))) | |
2671 | |
2672 (defmacro ange-ftp-dl-parser () | |
2673 ;; Parse the current buffer, which is assumed to be a descriptive | |
2674 ;; listing, and return a hashtable. | |
30459 | 2675 `(let ((tbl (ange-ftp-make-hashtable))) |
2676 (while (not (eobp)) | |
2677 (ange-ftp-put-hash-entry | |
2678 (buffer-substring (point) | |
2679 (progn | |
2680 (skip-chars-forward "^ /\n") | |
2681 (point))) | |
2682 (eq (following-char) ?/) | |
2683 tbl) | |
2684 (forward-line 1)) | |
2685 (ange-ftp-put-hash-entry "." t tbl) | |
2686 (ange-ftp-put-hash-entry ".." t tbl) | |
2687 tbl)) | |
28210 | 2688 |
2689 ;; Parse the current buffer which is assumed to be in a dired-like listing | |
2690 ;; format, and return a hashtable as the result. If the listing is not really | |
2691 ;; a listing, then return nil. | |
2692 | |
2693 (defun ange-ftp-parse-dired-listing (&optional switches) | |
2694 (save-match-data | |
2695 (cond | |
2696 ((looking-at "^total [0-9]+$") | |
2697 (forward-line 1) | |
2698 ;; Some systems put in a blank line here. | |
2699 (if (eolp) (forward-line 1)) | |
2700 (ange-ftp-ls-parser)) | |
2701 ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'") | |
2702 ;; It's an ls error message. | |
2703 nil) | |
2704 ((eobp) ; i.e. (zerop (buffer-size)) | |
2705 ;; This could be one of: | |
2706 ;; (1) An Ultrix ls error message | |
2707 ;; (2) A listing with the A switch of an empty directory | |
2708 ;; on a machine which doesn't give a total line. | |
2709 ;; (3) The twilight zone. | |
2710 ;; We'll assume (1) for now. | |
2711 nil) | |
2712 ((re-search-forward ange-ftp-date-regexp nil t) | |
2713 (beginning-of-line) | |
2714 (ange-ftp-ls-parser)) | |
2715 ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t) | |
2716 ;; It's a dl listing (I hope). | |
2717 ;; file is bound by the call to ange-ftp-ls | |
2718 (ange-ftp-add-dl-dir ange-ftp-this-file) | |
2719 (beginning-of-line) | |
2720 (ange-ftp-dl-parser)) | |
2721 (t nil)))) | |
2722 | |
2723 (defun ange-ftp-set-files (directory files) | |
2724 "For a given DIRECTORY, set or change the associated FILES hashtable." | |
2725 (and files (ange-ftp-put-hash-entry (file-name-as-directory directory) | |
2726 files ange-ftp-files-hashtable))) | |
2727 | |
2728 (defun ange-ftp-get-files (directory &optional no-error) | |
2729 "Given a given DIRECTORY, return a hashtable of file entries. | |
2730 This will give an error or return nil, depending on the value of | |
2731 NO-ERROR, if a listing for DIRECTORY cannot be obtained." | |
2732 (setq directory (file-name-as-directory directory)) ;normalize | |
2733 (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable) | |
2734 (save-match-data | |
2735 (and (ange-ftp-ls directory | |
2736 ;; This is an efficiency hack. We try to | |
2737 ;; anticipate what sort of listing dired | |
2738 ;; might want, and cache just such a listing. | |
2739 (if (and (boundp 'dired-actual-switches) | |
2740 (stringp dired-actual-switches) | |
2741 ;; We allow the A switch, which lists | |
2742 ;; all files except "." and "..". | |
2743 ;; This is OK because we manually | |
2744 ;; insert these entries | |
2745 ;; in the hash table. | |
2746 (string-match | |
2747 "[aA]" dired-actual-switches) | |
2748 (string-match | |
2749 "l" dired-actual-switches) | |
2750 (not (string-match | |
2751 "R" dired-actual-switches))) | |
2752 dired-actual-switches | |
2753 (if (and (boundp 'dired-listing-switches) | |
2754 (stringp dired-listing-switches) | |
2755 (string-match | |
2756 "[aA]" dired-listing-switches) | |
2757 (string-match | |
2758 "l" dired-listing-switches) | |
2759 (not (string-match | |
2760 "R" dired-listing-switches))) | |
2761 dired-listing-switches | |
2762 "-al")) | |
2763 t no-error) | |
2764 (ange-ftp-get-hash-entry | |
2765 directory ange-ftp-files-hashtable))))) | |
2766 | |
2767 ;; Given NAME, return the file part that can be used for looking up the | |
2768 ;; file's entry in a hashtable. | |
2769 (defmacro ange-ftp-get-file-part (name) | |
30459 | 2770 `(let ((file (file-name-nondirectory ,name))) |
2771 (if (string-equal file "") | |
2772 "." | |
2773 file))) | |
28210 | 2774 |
2775 ;; Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are | |
2776 ;; allowed to determine if NAME is a sub-directory by listing it directly, | |
2777 ;; rather than listing its parent directory. This is used for efficiency so | |
2778 ;; that a wasted listing is not done: | |
2779 ;; 1. When looking for a .dired file in dired-x.el. | |
2780 ;; 2. The syntax of FILE and DIR make it impossible that FILE could be a valid | |
2781 ;; subdirectory. This is of course an OS dependent judgement. | |
2782 | |
2783 (defmacro ange-ftp-allow-child-lookup (dir file) | |
30459 | 2784 `(not |
2785 (let* ((efile ,file) ; expand once. | |
2786 (edir ,dir) | |
2787 (parsed (ange-ftp-ftp-name edir)) | |
2788 (host-type (ange-ftp-host-type | |
2789 (car parsed)))) | |
2790 (or | |
2791 ;; Deal with dired | |
2792 (and (boundp 'dired-local-variables-file) ; in the dired-x package | |
2793 (stringp dired-local-variables-file) | |
2794 (string-equal dired-local-variables-file efile)) | |
2795 ;; No dots in dir names in vms. | |
2796 (and (eq host-type 'vms) | |
2797 (string-match "\\." efile)) | |
2798 ;; No subdirs in mts of cms. | |
2799 (and (memq host-type '(mts cms)) | |
2800 (not (string-equal "/" (nth 2 parsed)))))))) | |
28210 | 2801 |
2802 (defun ange-ftp-file-entry-p (name) | |
2803 "Given NAME, return whether there is a file entry for it." | |
2804 (let* ((name (directory-file-name name)) | |
2805 (dir (file-name-directory name)) | |
2806 (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable)) | |
2807 (file (ange-ftp-get-file-part name))) | |
2808 (if ent | |
2809 (ange-ftp-hash-entry-exists-p file ent) | |
2810 (or (and (ange-ftp-allow-child-lookup dir file) | |
2811 (setq ent (ange-ftp-get-files name t)) | |
2812 ;; Try a child lookup. i.e. try to list file as a | |
2813 ;; subdirectory of dir. This is a good idea because | |
2814 ;; we may not have read permission for file's parent. Also, | |
2815 ;; people tend to work down directory trees anyway. We use | |
2816 ;; no-error ;; because if file does not exist as a subdir., | |
2817 ;; then dumb hosts will give an ftp error. Smart unix hosts | |
2818 ;; will simply send back the ls | |
2819 ;; error message. | |
2820 (ange-ftp-get-hash-entry "." ent)) | |
2821 ;; Child lookup failed, so try the parent. | |
2822 (let ((table (ange-ftp-get-files dir))) | |
2823 ;; If the dir doesn't exist, don't use it as a hash table. | |
2824 (and table | |
2825 (ange-ftp-hash-entry-exists-p file | |
2826 table))))))) | |
2827 | |
2828 (defun ange-ftp-get-file-entry (name) | |
2829 "Given NAME, return the given file entry. | |
2830 The entry will be either t for a directory, nil for a normal file, | |
2831 or a string for a symlink. If the file isn't in the hashtable, | |
2832 this also returns nil." | |
2833 (let* ((name (directory-file-name name)) | |
2834 (dir (file-name-directory name)) | |
2835 (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable)) | |
2836 (file (ange-ftp-get-file-part name))) | |
2837 (if ent | |
2838 (ange-ftp-get-hash-entry file ent) | |
2839 (or (and (ange-ftp-allow-child-lookup dir file) | |
2840 (setq ent (ange-ftp-get-files name t)) | |
2841 (ange-ftp-get-hash-entry "." ent)) | |
2842 ;; i.e. it's a directory by child lookup | |
2843 (ange-ftp-get-hash-entry file | |
2844 (ange-ftp-get-files dir)))))) | |
2845 | |
2846 (defun ange-ftp-internal-delete-file-entry (name &optional dir-p) | |
2847 (if dir-p | |
30459 | 2848 (progn |
28210 | 2849 (setq name (file-name-as-directory name)) |
2850 (ange-ftp-del-hash-entry name ange-ftp-files-hashtable) | |
2851 (setq name (directory-file-name name)))) | |
2852 ;; Note that file-name-as-directory followed by directory-file-name | |
2853 ;; serves to canonicalize directory file names to their unix form. | |
2854 ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO | |
2855 (let ((files (ange-ftp-get-hash-entry (file-name-directory name) | |
2856 ange-ftp-files-hashtable))) | |
2857 (if files | |
2858 (ange-ftp-del-hash-entry (ange-ftp-get-file-part name) | |
2859 files)))) | |
2860 | |
2861 (defun ange-ftp-internal-add-file-entry (name &optional dir-p) | |
2862 (and dir-p | |
2863 (setq name (directory-file-name name))) | |
2864 (let ((files (ange-ftp-get-hash-entry (file-name-directory name) | |
2865 ange-ftp-files-hashtable))) | |
2866 (if files | |
2867 (ange-ftp-put-hash-entry (ange-ftp-get-file-part name) | |
2868 dir-p | |
2869 files)))) | |
2870 | |
2871 (defun ange-ftp-wipe-file-entries (host user) | |
2872 "Get rid of entry for HOST, USER pair from file entry information hashtable." | |
2873 (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable)))) | |
2874 (ange-ftp-map-hashtable | |
30459 | 2875 (lambda (key val) |
2876 (let ((parsed (ange-ftp-ftp-name key))) | |
2877 (if parsed | |
2878 (let ((h (nth 0 parsed)) | |
2879 (u (nth 1 parsed))) | |
2880 (or (and (equal host h) (equal user u)) | |
2881 (ange-ftp-put-hash-entry key val new-tbl)))))) | |
28210 | 2882 ange-ftp-files-hashtable) |
2883 (setq ange-ftp-files-hashtable new-tbl))) | |
2884 | |
2885 ;;;; ------------------------------------------------------------ | |
2886 ;;;; File transfer mode support. | |
2887 ;;;; ------------------------------------------------------------ | |
2888 | |
2889 (defun ange-ftp-set-binary-mode (host user) | |
2890 "Tell the ftp process for the given HOST & USER to switch to binary mode." | |
2891 (let ((result (ange-ftp-send-cmd host user '(type "binary")))) | |
2892 (if (not (car result)) | |
2893 (ange-ftp-error host user (concat "BINARY failed: " (cdr result))) | |
2894 (save-excursion | |
2895 (set-buffer (process-buffer (ange-ftp-get-process host user))) | |
2896 (and ange-ftp-binary-hash-mark-size | |
2897 (setq ange-ftp-hash-mark-unit | |
2898 (ash ange-ftp-binary-hash-mark-size -4))))))) | |
2899 | |
2900 (defun ange-ftp-set-ascii-mode (host user) | |
2901 "Tell the ftp process for the given HOST & USER to switch to ascii mode." | |
2902 (let ((result (ange-ftp-send-cmd host user '(type "ascii")))) | |
2903 (if (not (car result)) | |
2904 (ange-ftp-error host user (concat "ASCII failed: " (cdr result))) | |
2905 (save-excursion | |
2906 (set-buffer (process-buffer (ange-ftp-get-process host user))) | |
2907 (and ange-ftp-ascii-hash-mark-size | |
2908 (setq ange-ftp-hash-mark-unit | |
2909 (ash ange-ftp-ascii-hash-mark-size -4))))))) | |
2910 | |
2911 (defun ange-ftp-cd (host user dir) | |
2912 (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD"))) | |
2913 (or (car result) | |
2914 (ange-ftp-error host user (concat "CD failed: " (cdr result)))))) | |
2915 | |
2916 (defun ange-ftp-get-pwd (host user) | |
2917 "Attempts to get the current working directory for the given HOST/USER pair. | |
2918 Returns \( DIR . LINE \) where DIR is either the directory or nil if not found, | |
2919 and LINE is the relevant success or fail line from the FTP-client." | |
2920 (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD")) | |
2921 (line (cdr result)) | |
2922 dir) | |
2923 (if (car result) | |
2924 (save-match-data | |
2925 (and (or (string-match "\"\\([^\"]*\\)\"" line) | |
2926 (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers! | |
2927 (setq dir (substring line | |
2928 (match-beginning 1) | |
2929 (match-end 1)))))) | |
2930 (cons dir line))) | |
2931 | |
2932 ;;; ------------------------------------------------------------ | |
2933 ;;; expand-file-name and friends...which currently don't work | |
2934 ;;; ------------------------------------------------------------ | |
2935 | |
2936 (defun ange-ftp-expand-dir (host user dir) | |
2937 "Return the result of doing a PWD in the current FTP session. | |
2938 Use the connection to machine HOST | |
2939 logged in as user USER and cd'd to directory DIR." | |
2940 (let* ((host-type (ange-ftp-host-type host user)) | |
2941 ;; It is more efficient to call ange-ftp-host-type | |
2942 ;; before binding res, because ange-ftp-host-type sometimes | |
2943 ;; adds to the info in the expand-dir-hashtable. | |
2944 (fix-name-func | |
2945 (cdr (assq host-type ange-ftp-fix-name-func-alist))) | |
2946 (key (concat host "/" user "/" dir)) | |
2947 (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable))) | |
2948 (or res | |
2949 (progn | |
2950 (or | |
2951 (string-equal user "anonymous") | |
2952 (string-equal user "ftp") | |
2953 (not (eq host-type 'unix)) | |
2954 (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp | |
2955 "\\|" | |
2956 ange-ftp-good-msgs)) | |
2957 (result (ange-ftp-send-cmd host user | |
2958 (list 'get dir null-device) | |
2959 (format "expanding %s" dir))) | |
2960 (line (cdr result))) | |
2961 (setq res | |
2962 (if (string-match ange-ftp-expand-dir-regexp line) | |
30459 | 2963 (substring line |
28210 | 2964 (match-beginning 1) |
2965 (match-end 1)))))) | |
2966 (or res | |
2967 (if (string-equal dir "~") | |
2968 (setq res (car (ange-ftp-get-pwd host user))) | |
2969 (let ((home (ange-ftp-expand-dir host user "~"))) | |
2970 (unwind-protect | |
2971 (and (ange-ftp-cd host user dir) | |
2972 (setq res (car (ange-ftp-get-pwd host user)))) | |
2973 (ange-ftp-cd host user home))))) | |
2974 (if res | |
2975 (let ((ange-ftp-this-user user) | |
2976 (ange-ftp-this-host host)) | |
2977 (if fix-name-func | |
2978 (setq res (funcall fix-name-func res 'reverse))) | |
2979 (ange-ftp-put-hash-entry | |
2980 key res ange-ftp-expand-dir-hashtable))) | |
2981 res)))) | |
2982 | |
2983 (defun ange-ftp-canonize-filename (n) | |
2984 "Take a string and short-circuit //, /. and /.." | |
2985 (if (string-match "[^:]+//" n) ;don't upset Apollo users | |
2986 (setq n (substring n (1- (match-end 0))))) | |
2987 (let ((parsed (ange-ftp-ftp-name n))) | |
2988 (if parsed | |
2989 (let ((host (car parsed)) | |
2990 (user (nth 1 parsed)) | |
2991 (name (nth 2 parsed))) | |
30459 | 2992 |
28210 | 2993 ;; See if remote name is absolute. If so then just expand it and |
2994 ;; replace the name component of the overall name. | |
2995 (cond ((string-match "^/" name) | |
2996 name) | |
30459 | 2997 |
28210 | 2998 ;; Name starts with ~ or ~user. Resolve that part of the name |
2999 ;; making it absolute then re-expand it. | |
3000 ((string-match "^~[^/]*" name) | |
3001 (let* ((tilda (substring name | |
30459 | 3002 (match-beginning 0) |
28210 | 3003 (match-end 0))) |
3004 (rest (substring name (match-end 0))) | |
3005 (dir (ange-ftp-expand-dir host user tilda))) | |
3006 (if dir | |
3007 (setq name (concat dir rest)) | |
30459 | 3008 (error "User \"%s\" is not known" |
28210 | 3009 (substring tilda 1))))) |
30459 | 3010 |
28210 | 3011 ;; relative name. Tack on homedir and re-expand. |
3012 (t | |
3013 (let ((dir (ange-ftp-expand-dir host user "~"))) | |
3014 (if dir | |
3015 (setq name (concat | |
3016 (ange-ftp-real-file-name-as-directory dir) | |
3017 name)) | |
3018 (error "Unable to obtain CWD"))))) | |
30459 | 3019 |
28210 | 3020 ;; If name starts with //, preserve that, for apollo system. |
3021 (if (not (string-match "^//" name)) | |
3022 (progn | |
3023 (if (not (eq system-type 'windows-nt)) | |
3024 (setq name (ange-ftp-real-expand-file-name name)) | |
3025 ;; Windows UNC default dirs do not make sense for ftp. | |
3026 (if (string-match "^//" default-directory) | |
3027 (setq name (ange-ftp-real-expand-file-name name "c:/")) | |
3028 (setq name (ange-ftp-real-expand-file-name name))) | |
3029 ;; Strip off possible drive specifier. | |
3030 (if (string-match "^[a-zA-Z]:" name) | |
3031 (setq name (substring name 2)))) | |
3032 (if (string-match "^//" name) | |
3033 (setq name (substring name 1))))) | |
30459 | 3034 |
28210 | 3035 ;; Now substitute the expanded name back into the overall filename. |
3036 (ange-ftp-replace-name-component n name)) | |
30459 | 3037 |
28210 | 3038 ;; non-ange-ftp name. Just expand normally. |
3039 (if (eq (string-to-char n) ?/) | |
3040 (ange-ftp-real-expand-file-name n) | |
3041 (ange-ftp-real-expand-file-name | |
3042 (ange-ftp-real-file-name-nondirectory n) | |
3043 (ange-ftp-real-file-name-directory n)))))) | |
3044 | |
3045 (defun ange-ftp-expand-file-name (name &optional default) | |
3046 "Documented as original." | |
3047 (save-match-data | |
3048 (setq default (or default default-directory)) | |
3049 (cond ((eq (string-to-char name) ?~) | |
3050 (ange-ftp-real-expand-file-name name)) | |
3051 ((eq (string-to-char name) ?/) | |
3052 (ange-ftp-canonize-filename name)) | |
3053 ((and (eq system-type 'windows-nt) | |
3054 (eq (string-to-char name) ?\\)) | |
3055 (ange-ftp-canonize-filename name)) | |
3056 ((and (eq system-type 'windows-nt) | |
3057 (or (string-match "^[a-zA-Z]:" name) | |
3058 (string-match "^[a-zA-Z]:" default))) | |
3059 (ange-ftp-real-expand-file-name name default)) | |
3060 ((zerop (length name)) | |
3061 (ange-ftp-canonize-filename default)) | |
3062 ((ange-ftp-canonize-filename | |
3063 (concat (file-name-as-directory default) name)))))) | |
3064 | |
3065 ;;; These are problems--they are currently not enabled. | |
3066 | |
3067 (defvar ange-ftp-file-name-as-directory-alist nil | |
3068 "Association list of \( TYPE \. FUNC \) pairs. | |
3069 FUNC converts a filename to a directory name for the operating | |
3070 system TYPE.") | |
3071 | |
3072 (defun ange-ftp-file-name-as-directory (name) | |
3073 "Documented as original." | |
3074 (let ((parsed (ange-ftp-ftp-name name))) | |
3075 (if parsed | |
3076 (if (string-equal (nth 2 parsed) "") | |
3077 name | |
3078 (funcall (or (cdr (assq | |
3079 (ange-ftp-host-type (car parsed)) | |
3080 ange-ftp-file-name-as-directory-alist)) | |
3081 'ange-ftp-real-file-name-as-directory) | |
3082 name)) | |
3083 (ange-ftp-real-file-name-as-directory name)))) | |
30459 | 3084 |
28210 | 3085 (defun ange-ftp-file-name-directory (name) |
3086 "Documented as original." | |
3087 (let ((parsed (ange-ftp-ftp-name name))) | |
3088 (if parsed | |
3089 (let ((filename (nth 2 parsed))) | |
3090 (if (save-match-data | |
3091 (string-match "^~[^/]*$" filename)) | |
3092 name | |
3093 (ange-ftp-replace-name-component | |
3094 name | |
3095 (ange-ftp-real-file-name-directory filename)))) | |
3096 (ange-ftp-real-file-name-directory name)))) | |
3097 | |
3098 (defun ange-ftp-file-name-nondirectory (name) | |
3099 "Documented as original." | |
3100 (let ((parsed (ange-ftp-ftp-name name))) | |
3101 (if parsed | |
3102 (let ((filename (nth 2 parsed))) | |
3103 (if (save-match-data | |
3104 (string-match "^~[^/]*$" filename)) | |
3105 "" | |
3106 (ange-ftp-real-file-name-nondirectory filename))) | |
3107 (ange-ftp-real-file-name-nondirectory name)))) | |
3108 | |
3109 (defun ange-ftp-directory-file-name (dir) | |
3110 "Documented as original." | |
3111 (let ((parsed (ange-ftp-ftp-name dir))) | |
3112 (if parsed | |
3113 (ange-ftp-replace-name-component | |
3114 dir | |
3115 (ange-ftp-real-directory-file-name (nth 2 parsed))) | |
3116 (ange-ftp-real-directory-file-name dir)))) | |
3117 | |
3118 | |
3119 ;;; Hooks that handle Emacs primitives. | |
3120 | |
3121 ;; Returns non-nil if should transfer FILE in binary mode. | |
3122 (defun ange-ftp-binary-file (file) | |
3123 (save-match-data | |
3124 (string-match ange-ftp-binary-file-name-regexp file))) | |
3125 | |
3126 (defun ange-ftp-write-region (start end filename &optional append visit) | |
3127 (setq filename (expand-file-name filename)) | |
3128 (let ((parsed (ange-ftp-ftp-name filename))) | |
3129 (if parsed | |
3130 (let* ((host (nth 0 parsed)) | |
3131 (user (nth 1 parsed)) | |
3132 (name (ange-ftp-quote-string (nth 2 parsed))) | |
3133 (temp (ange-ftp-make-tmp-name host)) | |
3134 ;; What we REALLY need here is a way to determine if the mode | |
3135 ;; of the transfer is irrelevant, i.e. we can use binary mode | |
3136 ;; regardless. Maybe a system-type to host-type lookup? | |
3137 (binary (or (ange-ftp-binary-file filename) | |
3138 (memq (ange-ftp-host-type host user) | |
3139 '(unix dumb-unix)))) | |
3140 (cmd (if append 'append 'put)) | |
3141 (abbr (ange-ftp-abbreviate-filename filename)) | |
3142 ;; we need to reset `last-coding-system-used' to its | |
3143 ;; value immediately after calling the real write-region, | |
3144 ;; so that `basic-save-buffer' doesn't see whatever value | |
3145 ;; might be used when communicating with the ftp process. | |
3146 (coding-system-used last-coding-system-used)) | |
3147 (unwind-protect | |
3148 (progn | |
3149 (let ((executing-kbd-macro t) | |
3150 (filename (buffer-file-name)) | |
3151 (mod-p (buffer-modified-p))) | |
3152 (unwind-protect | |
37774
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
3153 (progn |
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
3154 (ange-ftp-real-write-region start end temp nil visit) |
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
3155 (setq coding-system-used last-coding-system-used)) |
28210 | 3156 ;; cleanup forms |
37774
a3b587b05ab3
(ange-ftp-write-region): Make sure to record the
Gerd Moellmann <gerd@gnu.org>
parents:
33539
diff
changeset
|
3157 (setq coding-system-used last-coding-system-used) |
28210 | 3158 (setq buffer-file-name filename) |
3159 (set-buffer-modified-p mod-p))) | |
3160 (if binary | |
3161 (ange-ftp-set-binary-mode host user)) | |
3162 | |
3163 ;; tell the process filter what size the transfer will be. | |
3164 (let ((attr (file-attributes temp))) | |
3165 (if attr | |
3166 (ange-ftp-set-xfer-size host user (nth 7 attr)))) | |
3167 | |
3168 ;; put or append the file. | |
3169 (let ((result (ange-ftp-send-cmd host user | |
3170 (list cmd temp name) | |
3171 (format "Writing %s" abbr)))) | |
3172 (or (car result) | |
3173 (signal 'ftp-error | |
3174 (list | |
3175 "Opening output file" | |
3176 (format "FTP Error: \"%s\"" (cdr result)) | |
3177 filename))))) | |
3178 (ange-ftp-del-tmp-name temp) | |
30459 | 3179 (if binary |
28210 | 3180 (ange-ftp-set-ascii-mode host user))) |
3181 (if (eq visit t) | |
3182 (progn | |
30459 | 3183 (set-visited-file-modtime (ange-ftp-file-modtime filename)) |
28210 | 3184 (ange-ftp-set-buffer-mode) |
3185 (setq buffer-file-name filename) | |
3186 (set-buffer-modified-p nil))) | |
3187 ;; ensure `last-coding-system-used' has an appropriate value | |
3188 (setq last-coding-system-used coding-system-used) | |
3189 (ange-ftp-message "Wrote %s" abbr) | |
3190 (ange-ftp-add-file-entry filename)) | |
3191 (ange-ftp-real-write-region start end filename append visit)))) | |
3192 | |
3193 (defun ange-ftp-insert-file-contents (filename &optional visit beg end replace) | |
3194 (barf-if-buffer-read-only) | |
3195 (setq filename (expand-file-name filename)) | |
3196 (let ((parsed (ange-ftp-ftp-name filename))) | |
3197 (if parsed | |
3198 (progn | |
3199 (if visit | |
3200 (setq buffer-file-name filename)) | |
3201 (if (or (file-exists-p filename) | |
3202 (progn | |
3203 (setq ange-ftp-ls-cache-file nil) | |
3204 (ange-ftp-del-hash-entry (file-name-directory filename) | |
3205 ange-ftp-files-hashtable) | |
3206 (file-exists-p filename))) | |
3207 (let* ((host (nth 0 parsed)) | |
3208 (user (nth 1 parsed)) | |
3209 (name (ange-ftp-quote-string (nth 2 parsed))) | |
3210 (temp (ange-ftp-make-tmp-name host)) | |
3211 (binary (or (ange-ftp-binary-file filename) | |
3212 (memq (ange-ftp-host-type host user) | |
3213 '(unix dumb-unix)))) | |
3214 (abbr (ange-ftp-abbreviate-filename filename)) | |
3215 (coding-system-used last-coding-system-used) | |
3216 size) | |
3217 (unwind-protect | |
3218 (progn | |
3219 (if binary | |
3220 (ange-ftp-set-binary-mode host user)) | |
3221 (let ((result (ange-ftp-send-cmd host user | |
3222 (list 'get name temp) | |
3223 (format "Retrieving %s" abbr)))) | |
3224 (or (car result) | |
3225 (signal 'ftp-error | |
3226 (list | |
3227 "Opening input file" | |
3228 (format "FTP Error: \"%s\"" (cdr result)) | |
3229 filename)))) | |
3230 (if (or (ange-ftp-real-file-readable-p temp) | |
3231 (sleep-for ange-ftp-retry-time) | |
3232 ;; Wait for file to hopefully appear. | |
3233 (ange-ftp-real-file-readable-p temp)) | |
3234 (setq | |
3235 size | |
3236 (nth 1 (ange-ftp-real-insert-file-contents | |
3237 temp visit beg end replace)) | |
3238 coding-system-used last-coding-system-used | |
3239 ;; override autodetection of buffer file type | |
3240 ;; to ensure buffer is saved in DOS format | |
3241 buffer-file-type binary) | |
3242 (signal 'ftp-error | |
3243 (list | |
3244 "Opening input file:" | |
3245 (format | |
3246 "FTP Error: %s not arrived or readable" | |
3247 filename))))) | |
3248 (if binary | |
3249 ;; We must keep `last-coding-system-used' | |
3250 ;; unchanged. | |
3251 (let (last-coding-system-used) | |
3252 (ange-ftp-set-ascii-mode host user))) | |
3253 (ange-ftp-del-tmp-name temp)) | |
3254 (if visit | |
3255 (progn | |
30459 | 3256 (set-visited-file-modtime |
3257 (ange-ftp-file-modtime filename)) | |
28210 | 3258 (setq buffer-file-name filename))) |
3259 (setq last-coding-system-used coding-system-used) | |
3260 (list filename size)) | |
3261 (signal 'file-error | |
30459 | 3262 (list |
28210 | 3263 "Opening input file" |
3264 filename)))) | |
3265 (ange-ftp-real-insert-file-contents filename visit beg end replace)))) | |
30459 | 3266 |
28210 | 3267 (defun ange-ftp-expand-symlink (file dir) |
3268 (if (file-name-absolute-p file) | |
3269 (ange-ftp-replace-name-component dir file) | |
3270 (expand-file-name file dir))) | |
3271 | |
3272 (defun ange-ftp-file-symlink-p (file) | |
3273 ;; call ange-ftp-expand-file-name rather than the normal | |
3274 ;; expand-file-name to stop loops when using a package that | |
3275 ;; redefines both file-symlink-p and expand-file-name. | |
3276 (setq file (ange-ftp-expand-file-name file)) | |
3277 (if (ange-ftp-ftp-name file) | |
3278 (let ((file-ent | |
3279 (ange-ftp-get-hash-entry | |
3280 (ange-ftp-get-file-part file) | |
3281 (ange-ftp-get-files (file-name-directory file))))) | |
3282 (if (stringp file-ent) | |
3283 (if (file-name-absolute-p file-ent) | |
3284 (ange-ftp-replace-name-component | |
3285 (file-name-directory file) file-ent) | |
3286 file-ent))) | |
3287 (ange-ftp-real-file-symlink-p file))) | |
3288 | |
3289 (defun ange-ftp-file-exists-p (name) | |
3290 (setq name (expand-file-name name)) | |
3291 (if (ange-ftp-ftp-name name) | |
3292 (if (ange-ftp-file-entry-p name) | |
3293 (let ((file-ent (ange-ftp-get-file-entry name))) | |
3294 (if (stringp file-ent) | |
3295 (file-exists-p | |
3296 (ange-ftp-expand-symlink file-ent | |
3297 (file-name-directory | |
3298 (directory-file-name name)))) | |
3299 t))) | |
3300 (ange-ftp-real-file-exists-p name))) | |
3301 | |
3302 (defun ange-ftp-file-directory-p (name) | |
3303 (setq name (expand-file-name name)) | |
3304 (if (ange-ftp-ftp-name name) | |
3305 ;; We do a file-name-as-directory on name here because some | |
3306 ;; machines (VMS) use a .DIR to indicate the filename associated | |
3307 ;; with a directory. This needs to be canonicalized. | |
3308 (let ((file-ent (ange-ftp-get-file-entry | |
3309 (ange-ftp-file-name-as-directory name)))) | |
3310 (if (stringp file-ent) | |
3311 (file-directory-p | |
3312 (ange-ftp-expand-symlink file-ent | |
3313 (file-name-directory | |
3314 (directory-file-name name)))) | |
3315 file-ent)) | |
3316 (ange-ftp-real-file-directory-p name))) | |
3317 | |
3318 (defun ange-ftp-directory-files (directory &optional full match | |
3319 &rest v19-args) | |
3320 (setq directory (expand-file-name directory)) | |
3321 (if (ange-ftp-ftp-name directory) | |
3322 (progn | |
3323 (ange-ftp-barf-if-not-directory directory) | |
3324 (let ((tail (ange-ftp-hash-table-keys | |
3325 (ange-ftp-get-files directory))) | |
3326 files f) | |
3327 (setq directory (file-name-as-directory directory)) | |
3328 (save-match-data | |
3329 (while tail | |
3330 (setq f (car tail) | |
3331 tail (cdr tail)) | |
3332 (if (or (not match) (string-match match f)) | |
3333 (setq files | |
3334 (cons (if full (concat directory f) f) files))))) | |
3335 (nreverse files))) | |
3336 (apply 'ange-ftp-real-directory-files directory full match v19-args))) | |
3337 | |
3338 (defun ange-ftp-file-attributes (file) | |
3339 (setq file (expand-file-name file)) | |
3340 (let ((parsed (ange-ftp-ftp-name file))) | |
3341 (if parsed | |
3342 (let ((part (ange-ftp-get-file-part file)) | |
3343 (files (ange-ftp-get-files (file-name-directory file)))) | |
3344 (if (ange-ftp-hash-entry-exists-p part files) | |
3345 (let ((host (nth 0 parsed)) | |
3346 (user (nth 1 parsed)) | |
3347 (name (nth 2 parsed)) | |
3348 (dirp (ange-ftp-get-hash-entry part files)) | |
3349 (inode (ange-ftp-get-hash-entry | |
3350 file ange-ftp-inodes-hashtable))) | |
3351 (unless inode | |
3352 (setq inode ange-ftp-next-inode-number | |
3353 ange-ftp-next-inode-number (1+ inode)) | |
3354 (ange-ftp-put-hash-entry file inode ange-ftp-inodes-hashtable)) | |
3355 (list (if (and (stringp dirp) (file-name-absolute-p dirp)) | |
3356 (ange-ftp-expand-symlink dirp | |
3357 (file-name-directory file)) | |
3358 dirp) ;0 file type | |
3359 -1 ;1 link count | |
3360 -1 ;2 uid | |
3361 -1 ;3 gid | |
3362 '(0 0) ;4 atime | |
30459 | 3363 (ange-ftp-file-modtime file) ;5 mtime |
28210 | 3364 '(0 0) ;6 ctime |
3365 -1 ;7 size | |
3366 (concat (if (stringp dirp) "l" (if dirp "d" "-")) | |
3367 "?????????") ;8 mode | |
3368 nil ;9 gid weird | |
3369 inode ;10 "inode number". | |
3370 -1 ;11 device number [v19 only] | |
3371 )))) | |
3372 (ange-ftp-real-file-attributes file)))) | |
3373 | |
30481 | 3374 (defun ange-ftp-file-newer-than-file-p (f1 f2) |
3375 (let ((f1-parsed (ange-ftp-ftp-name f1)) | |
3376 (f2-parsed (ange-ftp-ftp-name f2))) | |
3377 (if (or f1-parsed f2-parsed) | |
3378 (let ((f1-mt (nth 5 (file-attributes f1))) | |
3379 (f2-mt (nth 5 (file-attributes f2)))) | |
3380 (cond ((null f1-mt) nil) | |
3381 ((null f2-mt) t) | |
3382 (t (> (float-time f1-mt) (float-time f2-mt))))) | |
3383 (ange-ftp-real-file-newer-than-file-p f1 f2)))) | |
3384 | |
28210 | 3385 (defun ange-ftp-file-writable-p (file) |
33533 | 3386 (let ((ange-ftp-process-verbose nil)) |
3387 (setq file (expand-file-name file)) | |
3388 (if (ange-ftp-ftp-name file) | |
3389 (or (file-exists-p file) ;guess here for speed | |
3390 (file-directory-p (file-name-directory file))) | |
3391 (ange-ftp-real-file-writable-p file)))) | |
28210 | 3392 |
3393 (defun ange-ftp-file-readable-p (file) | |
33533 | 3394 (let ((ange-ftp-process-verbose nil)) |
3395 (setq file (expand-file-name file)) | |
3396 (if (ange-ftp-ftp-name file) | |
3397 (file-exists-p file) | |
3398 (ange-ftp-real-file-readable-p file)))) | |
28210 | 3399 |
3400 (defun ange-ftp-file-executable-p (file) | |
33533 | 3401 (let ((ange-ftp-process-verbose nil)) |
3402 (setq file (expand-file-name file)) | |
3403 (if (ange-ftp-ftp-name file) | |
3404 (file-exists-p file) | |
3405 (ange-ftp-real-file-executable-p file)))) | |
28210 | 3406 |
3407 (defun ange-ftp-delete-file (file) | |
3408 (interactive "fDelete file: ") | |
3409 (setq file (expand-file-name file)) | |
3410 (let ((parsed (ange-ftp-ftp-name file))) | |
3411 (if parsed | |
3412 (let* ((host (nth 0 parsed)) | |
3413 (user (nth 1 parsed)) | |
3414 (name (ange-ftp-quote-string (nth 2 parsed))) | |
3415 (abbr (ange-ftp-abbreviate-filename file)) | |
3416 (result (ange-ftp-send-cmd host user | |
3417 (list 'delete name) | |
3418 (format "Deleting %s" abbr)))) | |
3419 (or (car result) | |
3420 (signal 'ftp-error | |
3421 (list | |
3422 "Removing old name" | |
3423 (format "FTP Error: \"%s\"" (cdr result)) | |
3424 file))) | |
3425 (ange-ftp-delete-file-entry file)) | |
3426 (ange-ftp-real-delete-file file)))) | |
3427 | |
30459 | 3428 (defun ange-ftp-file-modtime (file) |
3429 (let* ((parsed (ange-ftp-ftp-name file)) | |
3430 (res (ange-ftp-send-cmd (car parsed) (cadr parsed) | |
33539
ac44722a4f19
(ange-ftp-file-modtime): Don't use `caddr'.
Miles Bader <miles@gnu.org>
parents:
33533
diff
changeset
|
3431 (list 'quote "mdtm" (cadr (cdr parsed)))))) |
30459 | 3432 (if (= ?5 (aref (cdr res) 0)) '(0 0) |
3433 (encode-time ; MDTM returns "YYYYMMDDHHMMSS" GMT | |
3434 (string-to-number (substring (cdr res) 16 18)) | |
3435 (string-to-number (substring (cdr res) 14 16)) | |
3436 (string-to-number (substring (cdr res) 12 14)) | |
3437 (string-to-number (substring (cdr res) 10 12)) | |
3438 (string-to-number (substring (cdr res) 8 10)) | |
3439 (string-to-number (substring (cdr res) 4 8)) | |
3440 0)))) | |
3441 | |
28210 | 3442 (defun ange-ftp-verify-visited-file-modtime (buf) |
3443 (let ((name (buffer-file-name buf))) | |
3444 (if (and (stringp name) (ange-ftp-ftp-name name)) | |
30459 | 3445 (let ((file-mdtm (ange-ftp-file-modtime name)) |
3446 (buf-mdtm (with-current-buffer buf (visited-file-modtime)))) | |
3447 (or (zerop (car file-mdtm)) | |
30518
ba9bd1bf0ef8
Ange-ftp handles the output of the w32-style clients
Sam Steingold <sds@gnu.org>
parents:
30481
diff
changeset
|
3448 (<= (float-time file-mdtm) (float-time buf-mdtm)))) |
28210 | 3449 (ange-ftp-real-verify-visited-file-modtime buf)))) |
3450 | |
3451 ;;;; ------------------------------------------------------------ | |
3452 ;;;; File copying support... totally re-written 6/24/92. | |
3453 ;;;; ------------------------------------------------------------ | |
3454 | |
3455 (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive) | |
3456 (if (file-exists-p absname) | |
3457 (if (not interactive) | |
3458 (signal 'file-already-exists (list absname)) | |
3459 (if (not (yes-or-no-p (format "File %s already exists; %s anyway? " | |
3460 absname querystring))) | |
3461 (signal 'file-already-exists (list absname)))))) | |
3462 | |
3463 ;; async local copy commented out for now since I don't seem to get | |
3464 ;; the process sentinel called for some processes. | |
3465 ;; | |
3466 ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists | |
3467 ;; keep-date cont) | |
3468 ;; "Kludge to copy a local file and call a continuation when the copy | |
3469 ;; finishes." | |
3470 ;; ;; check to see if we can overwrite | |
3471 ;; (if (or (not ok-if-already-exists) | |
3472 ;; (numberp ok-if-already-exists)) | |
30459 | 3473 ;; (ange-ftp-barf-or-query-if-file-exists newname "copy to it" |
28210 | 3474 ;; (numberp ok-if-already-exists))) |
3475 ;; (let ((proc (start-process " *copy*" | |
3476 ;; (generate-new-buffer "*copy*") | |
3477 ;; "cp" | |
3478 ;; filename | |
3479 ;; newname)) | |
3480 ;; res) | |
3481 ;; (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel)) | |
3482 ;; (process-kill-without-query proc) | |
3483 ;; (save-excursion | |
3484 ;; (set-buffer (process-buffer proc)) | |
3485 ;; (make-variable-buffer-local 'copy-cont) | |
3486 ;; (setq copy-cont cont)))) | |
30459 | 3487 ;; |
28210 | 3488 ;; (defun ange-ftp-copy-file-locally-sentinel (proc status) |
3489 ;; (save-excursion | |
3490 ;; (set-buffer (process-buffer proc)) | |
3491 ;; (let ((cont copy-cont) | |
3492 ;; (result (buffer-string))) | |
3493 ;; (unwind-protect | |
3494 ;; (if (and (string-equal status "finished\n") | |
3495 ;; (zerop (length result))) | |
3496 ;; (ange-ftp-call-cont cont t nil) | |
3497 ;; (ange-ftp-call-cont cont | |
3498 ;; nil | |
3499 ;; (if (zerop (length result)) | |
3500 ;; (substring status 0 -1) | |
3501 ;; (substring result 0 -1)))) | |
3502 ;; (kill-buffer (current-buffer)))))) | |
3503 | |
3504 ;; this is the extended version of ange-ftp-copy-file-internal that works | |
3505 ;; asynchronously if asked nicely. | |
3506 (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists | |
3507 keep-date &optional msg cont nowait) | |
3508 (setq filename (expand-file-name filename) | |
3509 newname (expand-file-name newname)) | |
3510 | |
3511 ;; canonicalize newname if a directory. | |
3512 (if (file-directory-p newname) | |
3513 (setq newname (expand-file-name (file-name-nondirectory filename) newname))) | |
3514 | |
3515 (let ((f-parsed (ange-ftp-ftp-name filename)) | |
3516 (t-parsed (ange-ftp-ftp-name newname))) | |
3517 | |
3518 ;; local file to local file copy? | |
3519 (if (and (not f-parsed) (not t-parsed)) | |
3520 (progn | |
3521 (ange-ftp-real-copy-file filename newname ok-if-already-exists | |
3522 keep-date) | |
3523 (if cont | |
3524 (ange-ftp-call-cont cont t "Copied locally"))) | |
3525 ;; one or both files are remote. | |
3526 (let* ((f-host (and f-parsed (nth 0 f-parsed))) | |
3527 (f-user (and f-parsed (nth 1 f-parsed))) | |
3528 (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed)))) | |
3529 (f-abbr (ange-ftp-abbreviate-filename filename)) | |
3530 (t-host (and t-parsed (nth 0 t-parsed))) | |
3531 (t-user (and t-parsed (nth 1 t-parsed))) | |
3532 (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed)))) | |
3533 (t-abbr (ange-ftp-abbreviate-filename newname filename)) | |
3534 (binary (or (ange-ftp-binary-file filename) | |
3535 (ange-ftp-binary-file newname) | |
3536 (and (memq (ange-ftp-host-type f-host f-user) | |
3537 '(unix dumb-unix)) | |
3538 (memq (ange-ftp-host-type t-host t-user) | |
3539 '(unix dumb-unix))))) | |
3540 temp1 | |
3541 temp2) | |
3542 | |
3543 ;; check to see if we can overwrite | |
3544 (if (or (not ok-if-already-exists) | |
3545 (numberp ok-if-already-exists)) | |
30459 | 3546 (ange-ftp-barf-or-query-if-file-exists newname "copy to it" |
28210 | 3547 (numberp ok-if-already-exists))) |
3548 | |
3549 ;; do the copying. | |
3550 (if f-parsed | |
30459 | 3551 |
28210 | 3552 ;; filename was remote. |
3553 (progn | |
3554 (if (or (ange-ftp-use-gateway-p f-host) | |
3555 t-parsed) | |
3556 ;; have to use intermediate file if we are getting via | |
3557 ;; gateway machine or we are doing a remote to remote copy. | |
3558 (setq temp1 (ange-ftp-make-tmp-name f-host))) | |
30459 | 3559 |
28210 | 3560 (if binary |
3561 (ange-ftp-set-binary-mode f-host f-user)) | |
3562 | |
3563 (ange-ftp-send-cmd | |
3564 f-host | |
3565 f-user | |
3566 (list 'get f-name (or temp1 (ange-ftp-quote-string newname))) | |
3567 (or msg | |
3568 (if (and temp1 t-parsed) | |
3569 (format "Getting %s" f-abbr) | |
3570 (format "Copying %s to %s" f-abbr t-abbr))) | |
3571 (list (function ange-ftp-cf1) | |
3572 filename newname binary msg | |
3573 f-parsed f-host f-user f-name f-abbr | |
3574 t-parsed t-host t-user t-name t-abbr | |
3575 temp1 temp2 cont nowait) | |
3576 nowait)) | |
3577 | |
3578 ;; filename wasn't remote. newname must be remote. call the | |
3579 ;; function which does the remainder of the copying work. | |
3580 (ange-ftp-cf1 t nil | |
3581 filename newname binary msg | |
3582 f-parsed f-host f-user f-name f-abbr | |
3583 t-parsed t-host t-user t-name t-abbr | |
3584 nil nil cont nowait)))))) | |
3585 | |
3586 (defvar ange-ftp-waiting-flag nil) | |
3587 | |
3588 ;; next part of copying routine. | |
3589 (defun ange-ftp-cf1 (result line | |
3590 filename newname binary msg | |
3591 f-parsed f-host f-user f-name f-abbr | |
3592 t-parsed t-host t-user t-name t-abbr | |
3593 temp1 temp2 cont nowait) | |
3594 (if line | |
3595 ;; filename must have been remote, and we must have just done a GET. | |
3596 (unwind-protect | |
3597 (or result | |
3598 ;; GET failed for some reason. Clean up and get out. | |
3599 (progn | |
3600 (and temp1 (ange-ftp-del-tmp-name temp1)) | |
3601 (or cont | |
3602 (if ange-ftp-waiting-flag | |
3603 (throw 'ftp-error t) | |
3604 (signal 'ftp-error | |
3605 (list "Opening input file" | |
3606 (format "FTP Error: \"%s\"" line) | |
3607 filename)))))) | |
3608 ;; cleanup | |
3609 (if binary | |
3610 (ange-ftp-set-ascii-mode f-host f-user)))) | |
3611 | |
3612 (if result | |
3613 ;; We now have to copy either temp1 or filename to newname. | |
3614 (if t-parsed | |
30459 | 3615 |
28210 | 3616 ;; newname was remote. |
3617 (progn | |
3618 (if (ange-ftp-use-gateway-p t-host) | |
3619 (setq temp2 (ange-ftp-make-tmp-name t-host))) | |
30459 | 3620 |
28210 | 3621 ;; make sure data is moved into the right place for the |
3622 ;; outgoing transfer. gateway temporary files complicate | |
3623 ;; things nicely. | |
3624 (if temp1 | |
3625 (if temp2 | |
3626 (if (string-equal temp1 temp2) | |
3627 (setq temp1 nil) | |
3628 (ange-ftp-real-copy-file temp1 temp2 t)) | |
3629 (setq temp2 temp1 temp1 nil)) | |
3630 (if temp2 | |
3631 (ange-ftp-real-copy-file filename temp2 t))) | |
30459 | 3632 |
28210 | 3633 (if binary |
3634 (ange-ftp-set-binary-mode t-host t-user)) | |
3635 | |
3636 ;; tell the process filter what size the file is. | |
3637 (let ((attr (file-attributes (or temp2 filename)))) | |
3638 (if attr | |
3639 (ange-ftp-set-xfer-size t-host t-user (nth 7 attr)))) | |
3640 | |
3641 (ange-ftp-send-cmd | |
3642 t-host | |
3643 t-user | |
3644 (list 'put (or temp2 filename) t-name) | |
3645 (or msg | |
3646 (if (and temp2 f-parsed) | |
3647 (format "Putting %s" newname) | |
3648 (format "Copying %s to %s" f-abbr t-abbr))) | |
3649 (list (function ange-ftp-cf2) | |
3650 newname t-host t-user binary temp1 temp2 cont) | |
3651 nowait)) | |
30459 | 3652 |
28210 | 3653 ;; newname wasn't remote. |
3654 (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont)) | |
3655 | |
3656 ;; first copy failed, tell caller | |
3657 (ange-ftp-call-cont cont result line))) | |
3658 | |
3659 ;; last part of copying routine. | |
3660 (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont) | |
3661 (unwind-protect | |
3662 (if line | |
3663 ;; result from doing a local to remote copy. | |
3664 (unwind-protect | |
3665 (progn | |
3666 (or result | |
3667 (or cont | |
3668 (if ange-ftp-waiting-flag | |
3669 (throw 'ftp-error t) | |
3670 (signal 'ftp-error | |
3671 (list "Opening output file" | |
3672 (format "FTP Error: \"%s\"" line) | |
3673 newname))))) | |
30459 | 3674 |
28210 | 3675 (ange-ftp-add-file-entry newname)) |
30459 | 3676 |
28210 | 3677 ;; cleanup. |
3678 (if binary | |
3679 (ange-ftp-set-ascii-mode t-host t-user))) | |
30459 | 3680 |
28210 | 3681 ;; newname was local. |
3682 (if temp1 | |
3683 (ange-ftp-real-copy-file temp1 newname t))) | |
30459 | 3684 |
28210 | 3685 ;; clean up |
3686 (and temp1 (ange-ftp-del-tmp-name temp1)) | |
3687 (and temp2 (ange-ftp-del-tmp-name temp2)) | |
3688 (ange-ftp-call-cont cont result line))) | |
3689 | |
3690 (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists | |
3691 keep-date) | |
3692 (interactive "fCopy file: \nFCopy %s to file: \np") | |
3693 (ange-ftp-copy-file-internal filename | |
3694 newname | |
3695 ok-if-already-exists | |
3696 keep-date | |
3697 nil | |
3698 nil | |
3699 (interactive-p))) | |
3700 | |
3701 ;;;; ------------------------------------------------------------ | |
3702 ;;;; File renaming support. | |
3703 ;;;; ------------------------------------------------------------ | |
3704 | |
3705 (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed) | |
3706 "Rename remote file FILE to remote file NEWNAME." | |
3707 (let ((f-host (nth 0 f-parsed)) | |
3708 (f-user (nth 1 f-parsed)) | |
3709 (t-host (nth 0 t-parsed)) | |
3710 (t-user (nth 1 t-parsed))) | |
3711 (if (and (string-equal f-host t-host) | |
3712 (string-equal f-user t-user)) | |
3713 (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed))) | |
3714 (t-name (ange-ftp-quote-string (nth 2 t-parsed))) | |
3715 (cmd (list 'rename f-name t-name)) | |
3716 (fabbr (ange-ftp-abbreviate-filename filename)) | |
3717 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3718 (result (ange-ftp-send-cmd f-host f-user cmd | |
3719 (format "Renaming %s to %s" | |
3720 fabbr | |
3721 nabbr)))) | |
3722 (or (car result) | |
3723 (signal 'ftp-error | |
3724 (list | |
3725 "Renaming" | |
3726 (format "FTP Error: \"%s\"" (cdr result)) | |
3727 filename | |
3728 newname))) | |
3729 (ange-ftp-add-file-entry newname) | |
3730 (ange-ftp-delete-file-entry filename)) | |
3731 (ange-ftp-copy-file-internal filename newname t nil) | |
3732 (delete-file filename)))) | |
3733 | |
3734 (defun ange-ftp-rename-local-to-remote (filename newname) | |
3735 "Rename local FILENAME to remote file NEWNAME." | |
3736 (let* ((fabbr (ange-ftp-abbreviate-filename filename)) | |
3737 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3738 (msg (format "Renaming %s to %s" fabbr nabbr))) | |
3739 (ange-ftp-copy-file-internal filename newname t nil msg) | |
3740 (let (ange-ftp-process-verbose) | |
3741 (delete-file filename)))) | |
3742 | |
3743 (defun ange-ftp-rename-remote-to-local (filename newname) | |
3744 "Rename remote file FILENAME to local file NEWNAME." | |
3745 (let* ((fabbr (ange-ftp-abbreviate-filename filename)) | |
3746 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3747 (msg (format "Renaming %s to %s" fabbr nabbr))) | |
3748 (ange-ftp-copy-file-internal filename newname t nil msg) | |
3749 (let (ange-ftp-process-verbose) | |
3750 (delete-file filename)))) | |
3751 | |
3752 (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists) | |
3753 (interactive "fRename file: \nFRename %s to file: \np") | |
3754 (setq filename (expand-file-name filename)) | |
3755 (setq newname (expand-file-name newname)) | |
3756 (let* ((f-parsed (ange-ftp-ftp-name filename)) | |
3757 (t-parsed (ange-ftp-ftp-name newname))) | |
3758 (if (and (or f-parsed t-parsed) | |
3759 (or (not ok-if-already-exists) | |
3760 (numberp ok-if-already-exists))) | |
3761 (ange-ftp-barf-or-query-if-file-exists | |
3762 newname | |
3763 "rename to it" | |
3764 (numberp ok-if-already-exists))) | |
3765 (if f-parsed | |
3766 (if t-parsed | |
3767 (ange-ftp-rename-remote-to-remote filename newname f-parsed | |
3768 t-parsed) | |
3769 (ange-ftp-rename-remote-to-local filename newname)) | |
3770 (if t-parsed | |
3771 (ange-ftp-rename-local-to-remote filename newname) | |
3772 (ange-ftp-real-rename-file filename newname ok-if-already-exists))))) | |
3773 | |
3774 ;;;; ------------------------------------------------------------ | |
3775 ;;;; File name completion support. | |
3776 ;;;; ------------------------------------------------------------ | |
3777 | |
3778 ;; If the file entry SYM is a symlink, returns whether its file exists. | |
3779 ;; Note that `ange-ftp-this-dir' is used as a free variable. | |
3780 (defun ange-ftp-file-entry-active-p (sym) | |
3781 (let ((val (get sym 'val))) | |
3782 (or (not (stringp val)) | |
3783 (file-exists-p (ange-ftp-expand-symlink val ange-ftp-this-dir))))) | |
3784 | |
3785 ;; If the file entry is not a directory (nor a symlink pointing to a directory) | |
3786 ;; returns whether the file (or file pointed to by the symlink) is ignored | |
3787 ;; by completion-ignored-extensions. | |
3788 ;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern' | |
3789 ;; are used as free variables. | |
3790 (defun ange-ftp-file-entry-not-ignored-p (sym) | |
3791 (let ((val (get sym 'val)) | |
3792 (symname (symbol-name sym))) | |
3793 (if (stringp val) | |
3794 (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir))) | |
3795 (or (file-directory-p file) | |
3796 (and (file-exists-p file) | |
3797 (not (string-match ange-ftp-completion-ignored-pattern | |
3798 symname))))) | |
3799 (or val ; is a directory name | |
3800 (not (string-match ange-ftp-completion-ignored-pattern symname)))))) | |
3801 | |
3802 (defun ange-ftp-file-name-all-completions (file dir) | |
3803 (let ((ange-ftp-this-dir (expand-file-name dir))) | |
3804 (if (ange-ftp-ftp-name ange-ftp-this-dir) | |
3805 (progn | |
3806 (ange-ftp-barf-if-not-directory ange-ftp-this-dir) | |
3807 (setq ange-ftp-this-dir | |
3808 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) | |
3809 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir)) | |
3810 (completions | |
3811 (all-completions file tbl | |
3812 (function ange-ftp-file-entry-active-p)))) | |
3813 | |
3814 ;; see whether each matching file is a directory or not... | |
3815 (mapcar | |
30459 | 3816 (lambda (file) |
3817 (let ((ent (ange-ftp-get-hash-entry file tbl))) | |
3818 (if (and ent | |
3819 (or (not (stringp ent)) | |
3820 (file-directory-p | |
3821 (ange-ftp-expand-symlink ent | |
3822 ange-ftp-this-dir)))) | |
3823 (concat file "/") | |
3824 file))) | |
28210 | 3825 completions))) |
3826 | |
3827 (if (or (and (eq system-type 'windows-nt) | |
3828 (string-match "^[a-zA-Z]:[/\\]$" ange-ftp-this-dir)) | |
3829 (string-equal "/" ange-ftp-this-dir)) | |
3830 (nconc (all-completions file (ange-ftp-generate-root-prefixes)) | |
3831 (ange-ftp-real-file-name-all-completions file | |
3832 ange-ftp-this-dir)) | |
3833 (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir))))) | |
3834 | |
3835 (defun ange-ftp-file-name-completion (file dir) | |
3836 (let ((ange-ftp-this-dir (expand-file-name dir))) | |
3837 (if (ange-ftp-ftp-name ange-ftp-this-dir) | |
3838 (progn | |
3839 (ange-ftp-barf-if-not-directory ange-ftp-this-dir) | |
3840 (if (equal file "") | |
3841 "" | |
3842 (setq ange-ftp-this-dir | |
3843 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) ;real? | |
3844 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir)) | |
3845 (ange-ftp-completion-ignored-pattern | |
30459 | 3846 (mapconcat (lambda (s) (if (stringp s) |
3847 (concat (regexp-quote s) "$") | |
3848 "/")) ; / never in filename | |
28210 | 3849 completion-ignored-extensions |
3850 "\\|"))) | |
3851 (save-match-data | |
3852 (or (ange-ftp-file-name-completion-1 | |
3853 file tbl ange-ftp-this-dir | |
3854 (function ange-ftp-file-entry-not-ignored-p)) | |
3855 (ange-ftp-file-name-completion-1 | |
3856 file tbl ange-ftp-this-dir | |
3857 (function ange-ftp-file-entry-active-p))))))) | |
3858 | |
3859 (if (or (and (eq system-type 'windows-nt) | |
3860 (string-match "^[a-zA-Z]:[/\\]$" ange-ftp-this-dir)) | |
3861 (string-equal "/" ange-ftp-this-dir)) | |
3862 (try-completion | |
3863 file | |
3864 (nconc (ange-ftp-generate-root-prefixes) | |
3865 (mapcar 'list | |
3866 (ange-ftp-real-file-name-all-completions | |
3867 file ange-ftp-this-dir)))) | |
3868 (ange-ftp-real-file-name-completion file ange-ftp-this-dir))))) | |
3869 | |
3870 | |
3871 (defun ange-ftp-file-name-completion-1 (file tbl dir predicate) | |
3872 (let ((bestmatch (try-completion file tbl predicate))) | |
3873 (if bestmatch | |
3874 (if (eq bestmatch t) | |
3875 (if (file-directory-p (expand-file-name file dir)) | |
3876 (concat file "/") | |
3877 t) | |
3878 (if (and (eq (try-completion bestmatch tbl predicate) t) | |
3879 (file-directory-p | |
3880 (expand-file-name bestmatch dir))) | |
3881 (concat bestmatch "/") | |
3882 bestmatch))))) | |
3883 | |
3884 ;; Put these lines uncommmented in your .emacs if you want C-r to refresh | |
3885 ;; ange-ftp's cache whilst doing filename completion. | |
3886 ;; | |
3887 ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir) | |
3888 ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir) | |
3889 | |
3890 ;; The autoload cookie is to make sure the doc is always available. | |
3891 ;;;###autoload (defalias 'ange-ftp-re-read-dir 'ange-ftp-reread-dir) | |
3892 ;;;###autoload | |
3893 (defun ange-ftp-reread-dir (&optional dir) | |
3894 "Reread remote directory DIR to update the directory cache. | |
3895 The implementation of remote ftp file names caches directory contents | |
3896 for speed. Therefore, when new remote files are created, Emacs | |
3897 may not know they exist. You can use this command to reread a specific | |
3898 directory, so that Emacs will know its current contents." | |
3899 (interactive) | |
3900 (if dir | |
3901 (setq dir (expand-file-name dir)) | |
3902 (setq dir (file-name-directory (expand-file-name (buffer-string))))) | |
3903 (if (ange-ftp-ftp-name dir) | |
3904 (progn | |
3905 (setq ange-ftp-ls-cache-file nil) | |
3906 (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable) | |
3907 (ange-ftp-get-files dir t)))) | |
3908 | |
3909 (defun ange-ftp-make-directory (dir &optional parents) | |
3910 (interactive (list (expand-file-name (read-file-name "Make directory: ")))) | |
3911 (if parents | |
3912 (let ((parent (file-name-directory (directory-file-name dir)))) | |
3913 (or (file-exists-p parent) | |
3914 (ange-ftp-make-directory parent parents)))) | |
3915 (if (file-exists-p dir) | |
3916 (error "Cannot make directory %s: file already exists" dir) | |
3917 (let ((parsed (ange-ftp-ftp-name dir))) | |
3918 (if parsed | |
3919 (let* ((host (nth 0 parsed)) | |
3920 (user (nth 1 parsed)) | |
3921 ;; Some ftp's on unix machines (at least on Suns) | |
3922 ;; insist that mkdir take a filename, and not a | |
3923 ;; directory-name name as an arg. Argh!! This is a bug. | |
3924 ;; Non-unix machines will probably always insist | |
3925 ;; that mkdir takes a directory-name as an arg | |
3926 ;; (as the ftp man page says it should). | |
3927 (name (ange-ftp-quote-string | |
3928 (if (eq (ange-ftp-host-type host) 'unix) | |
3929 (ange-ftp-real-directory-file-name (nth 2 parsed)) | |
3930 (ange-ftp-real-file-name-as-directory | |
3931 (nth 2 parsed))))) | |
3932 (abbr (ange-ftp-abbreviate-filename dir)) | |
3933 (result (ange-ftp-send-cmd host user | |
3934 (list 'mkdir name) | |
3935 (format "Making directory %s" | |
3936 abbr)))) | |
3937 (or (car result) | |
3938 (ange-ftp-error host user | |
3939 (format "Could not make directory %s: %s" | |
3940 dir | |
3941 (cdr result)))) | |
3942 (ange-ftp-add-file-entry dir t)) | |
3943 (ange-ftp-real-make-directory dir))))) | |
3944 | |
3945 (defun ange-ftp-delete-directory (dir) | |
3946 (if (file-directory-p dir) | |
3947 (let ((parsed (ange-ftp-ftp-name dir))) | |
3948 (if parsed | |
3949 (let* ((host (nth 0 parsed)) | |
3950 (user (nth 1 parsed)) | |
3951 ;; Some ftp's on unix machines (at least on Suns) | |
3952 ;; insist that rmdir take a filename, and not a | |
3953 ;; directory-name name as an arg. Argh!! This is a bug. | |
3954 ;; Non-unix machines will probably always insist | |
3955 ;; that rmdir takes a directory-name as an arg | |
3956 ;; (as the ftp man page says it should). | |
3957 (name (ange-ftp-quote-string | |
3958 (if (eq (ange-ftp-host-type host) 'unix) | |
3959 (ange-ftp-real-directory-file-name | |
3960 (nth 2 parsed)) | |
3961 (ange-ftp-real-file-name-as-directory | |
3962 (nth 2 parsed))))) | |
3963 (abbr (ange-ftp-abbreviate-filename dir)) | |
3964 (result (ange-ftp-send-cmd host user | |
3965 (list 'rmdir name) | |
3966 (format "Removing directory %s" | |
3967 abbr)))) | |
3968 (or (car result) | |
3969 (ange-ftp-error host user | |
3970 (format "Could not remove directory %s: %s" | |
3971 dir | |
3972 (cdr result)))) | |
3973 (ange-ftp-delete-file-entry dir t)) | |
3974 (ange-ftp-real-delete-directory dir))) | |
3975 (error "Not a directory: %s" dir))) | |
3976 | |
3977 ;; Make a local copy of FILE and return its name. | |
3978 | |
3979 (defun ange-ftp-file-local-copy (file) | |
3980 (let* ((fn1 (expand-file-name file)) | |
3981 (pa1 (ange-ftp-ftp-name fn1))) | |
3982 (if pa1 | |
3983 (let ((tmp1 (ange-ftp-make-tmp-name (car pa1)))) | |
3984 (ange-ftp-copy-file-internal fn1 tmp1 t nil | |
3985 (format "Getting %s" fn1)) | |
3986 tmp1)))) | |
3987 | |
3988 (defun ange-ftp-load (file &optional noerror nomessage nosuffix) | |
3989 (if (ange-ftp-ftp-name file) | |
3990 (let ((tryfiles (if nosuffix | |
3991 (list file) | |
3992 (list (concat file ".elc") (concat file ".el") file))) | |
3993 ;; make sure there are no references to temp files | |
3994 (load-force-doc-strings t) | |
3995 copy) | |
3996 (while (and tryfiles (not copy)) | |
3997 (catch 'ftp-error | |
3998 (let ((ange-ftp-waiting-flag t)) | |
3999 (condition-case error | |
4000 (setq copy (ange-ftp-file-local-copy (car tryfiles))) | |
4001 (ftp-error nil)))) | |
4002 (setq tryfiles (cdr tryfiles))) | |
4003 (if copy | |
4004 (unwind-protect | |
4005 (funcall 'load copy noerror nomessage nosuffix) | |
4006 (delete-file copy)) | |
4007 (or noerror | |
4008 (signal 'file-error (list "Cannot open load file" file))) | |
4009 nil)) | |
4010 (ange-ftp-real-load file noerror nomessage nosuffix))) | |
4011 | |
4012 ;; Calculate default-unhandled-directory for a given ange-ftp buffer. | |
4013 (defun ange-ftp-unhandled-file-name-directory (filename) | |
4014 (file-name-directory ange-ftp-tmp-name-template)) | |
4015 | |
4016 | |
4017 ;; Need the following functions for making filenames of compressed | |
4018 ;; files, because some OS's (unlike UNIX) do not allow a filename to | |
4019 ;; have two extensions. | |
4020 | |
4021 (defvar ange-ftp-make-compressed-filename-alist nil | |
4022 "Alist of host-type-specific functions to process file names for compression. | |
4023 Each element has the form (TYPE . FUNC). | |
4024 FUNC should take one argument, a file name, and return a list | |
4025 of the form (COMPRESSING NEWNAME). | |
30459 | 4026 COMPRESSING should be t if the specified file should be compressed, |
28210 | 4027 and nil if it should be uncompressed (that is, if it is a compressed file). |
4028 NEWNAME should be the name to give the new compressed or uncompressed file.") | |
4029 | |
4030 (defun ange-ftp-dired-compress-file (name) | |
4031 (let ((parsed (ange-ftp-ftp-name name)) | |
4032 conversion-func) | |
4033 (if (and parsed | |
4034 (setq conversion-func | |
4035 (cdr (assq (ange-ftp-host-type (car parsed)) | |
4036 ange-ftp-make-compressed-filename-alist)))) | |
4037 (let* ((decision | |
4038 (save-match-data (funcall conversion-func name))) | |
4039 (compressing (car decision)) | |
4040 (newfile (nth 1 decision))) | |
4041 (if compressing | |
4042 (ange-ftp-compress name newfile) | |
4043 (ange-ftp-uncompress name newfile))) | |
4044 (let (file-name-handler-alist) | |
4045 (dired-compress-file name))))) | |
4046 | |
4047 ;; Copy FILE to this machine, compress it, and copy out to NFILE. | |
4048 (defun ange-ftp-compress (file nfile) | |
4049 (let* ((parsed (ange-ftp-ftp-name file)) | |
4050 (tmp1 (ange-ftp-make-tmp-name (car parsed))) | |
4051 (tmp2 (ange-ftp-make-tmp-name (car parsed))) | |
4052 (abbr (ange-ftp-abbreviate-filename file)) | |
4053 (nabbr (ange-ftp-abbreviate-filename nfile)) | |
4054 (msg1 (format "Getting %s" abbr)) | |
4055 (msg2 (format "Putting %s" nabbr))) | |
4056 (unwind-protect | |
4057 (progn | |
4058 (ange-ftp-copy-file-internal file tmp1 t nil msg1) | |
4059 (and ange-ftp-process-verbose | |
4060 (ange-ftp-message "Compressing %s..." abbr)) | |
4061 (call-process-region (point) | |
4062 (point) | |
4063 shell-file-name | |
4064 nil | |
4065 t | |
4066 nil | |
4067 "-c" | |
4068 (format "compress -f -c < %s > %s" tmp1 tmp2)) | |
4069 (and ange-ftp-process-verbose | |
4070 (ange-ftp-message "Compressing %s...done" abbr)) | |
4071 (if (zerop (buffer-size)) | |
4072 (progn | |
4073 (let (ange-ftp-process-verbose) | |
4074 (delete-file file)) | |
4075 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2)))) | |
4076 (ange-ftp-del-tmp-name tmp1) | |
4077 (ange-ftp-del-tmp-name tmp2)))) | |
30459 | 4078 |
28210 | 4079 ;; Copy FILE to this machine, uncompress it, and copy out to NFILE. |
4080 (defun ange-ftp-uncompress (file nfile) | |
4081 (let* ((parsed (ange-ftp-ftp-name file)) | |
4082 (tmp1 (ange-ftp-make-tmp-name (car parsed))) | |
4083 (tmp2 (ange-ftp-make-tmp-name (car parsed))) | |
4084 (abbr (ange-ftp-abbreviate-filename file)) | |
4085 (nabbr (ange-ftp-abbreviate-filename nfile)) | |
4086 (msg1 (format "Getting %s" abbr)) | |
4087 (msg2 (format "Putting %s" nabbr)) | |
4088 ;; ;; Cheap hack because of problems with binary file transfers from | |
4089 ;; ;; VMS hosts. | |
4090 ;; (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed))))) | |
4091 ) | |
4092 (unwind-protect | |
4093 (progn | |
4094 (ange-ftp-copy-file-internal file tmp1 t nil msg1) | |
4095 (and ange-ftp-process-verbose | |
4096 (ange-ftp-message "Uncompressing %s..." abbr)) | |
4097 (call-process-region (point) | |
4098 (point) | |
4099 shell-file-name | |
4100 nil | |
4101 t | |
4102 nil | |
4103 "-c" | |
4104 (format "uncompress -c < %s > %s" tmp1 tmp2)) | |
4105 (and ange-ftp-process-verbose | |
4106 (ange-ftp-message "Uncompressing %s...done" abbr)) | |
4107 (if (zerop (buffer-size)) | |
4108 (progn | |
4109 (let (ange-ftp-process-verbose) | |
4110 (delete-file file)) | |
4111 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2)))) | |
4112 (ange-ftp-del-tmp-name tmp1) | |
4113 (ange-ftp-del-tmp-name tmp2)))) | |
4114 | |
4115 (defun ange-ftp-find-backup-file-name (fn) | |
4116 ;; Either return the ordinary backup name, etc., | |
4117 ;; or return nil meaning don't make a backup. | |
4118 (if ange-ftp-make-backup-files | |
4119 (ange-ftp-real-find-backup-file-name fn))) | |
4120 | |
4121 ;;; Define the handler for special file names | |
4122 ;;; that causes ange-ftp to be invoked. | |
4123 | |
4124 ;;;###autoload | |
4125 (defun ange-ftp-hook-function (operation &rest args) | |
4126 (let ((fn (get operation 'ange-ftp))) | |
4127 (if fn (apply fn args) | |
4128 (ange-ftp-run-real-handler operation args)))) | |
4129 | |
4130 | |
4131 ;;; This regexp takes care of real ange-ftp file names (with a slash | |
4132 ;;; and colon). | |
4133 ;;; Don't allow the host name to end in a period--some systems use /.: | |
4134 ;;;###autoload | |
4135 (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist) | |
4136 (setq file-name-handler-alist | |
4137 (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function) | |
4138 file-name-handler-alist))) | |
4139 | |
4140 ;;; This regexp recognizes absolute filenames with only one component, | |
4141 ;;; for the sake of hostname completion. | |
4142 ;;;###autoload | |
4143 (or (assoc "^/[^/:]*\\'" file-name-handler-alist) | |
4144 (setq file-name-handler-alist | |
4145 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function) | |
4146 file-name-handler-alist))) | |
4147 | |
4148 ;;; This regexp recognizes absolute filenames with only one component | |
4149 ;;; on Windows, for the sake of hostname completion. | |
4150 ;;; NB. Do not mark this as autoload, because it is very common to | |
4151 ;;; do completions in the root directory of drives on Windows. | |
4152 (and (memq system-type '(ms-dos windows-nt)) | |
4153 (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist) | |
4154 (setq file-name-handler-alist | |
30459 | 4155 (cons '("^[a-zA-Z]:/[^/:]*\\'" . |
28210 | 4156 ange-ftp-completion-hook-function) |
4157 file-name-handler-alist)))) | |
4158 | |
4159 ;;; The above two forms are sufficient to cause this file to be loaded | |
4160 ;;; if the user ever uses a file name with a colon in it. | |
4161 | |
30459 | 4162 ;;; This sets the mode |
28210 | 4163 (or (memq 'ange-ftp-set-buffer-mode find-file-hooks) |
4164 (setq find-file-hooks | |
4165 (cons 'ange-ftp-set-buffer-mode find-file-hooks))) | |
4166 | |
4167 ;;; Now say where to find the handlers for particular operations. | |
4168 | |
4169 (put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory) | |
4170 (put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory) | |
4171 (put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory) | |
4172 (put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name) | |
4173 (put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name) | |
4174 (put 'make-directory 'ange-ftp 'ange-ftp-make-directory) | |
4175 (put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory) | |
4176 (put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents) | |
4177 (put 'directory-files 'ange-ftp 'ange-ftp-directory-files) | |
4178 (put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p) | |
4179 (put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p) | |
4180 (put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p) | |
4181 (put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p) | |
4182 (put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p) | |
4183 (put 'delete-file 'ange-ftp 'ange-ftp-delete-file) | |
4184 (put 'read-file-name-internal 'ange-ftp 'ange-ftp-read-file-name-internal) | |
4185 (put 'verify-visited-file-modtime 'ange-ftp | |
4186 'ange-ftp-verify-visited-file-modtime) | |
4187 (put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p) | |
4188 (put 'write-region 'ange-ftp 'ange-ftp-write-region) | |
4189 (put 'backup-buffer 'ange-ftp 'ange-ftp-backup-buffer) | |
4190 (put 'copy-file 'ange-ftp 'ange-ftp-copy-file) | |
4191 (put 'rename-file 'ange-ftp 'ange-ftp-rename-file) | |
4192 (put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes) | |
30481 | 4193 (put 'file-newer-than-file-p 'ange-ftp 'ange-ftp-file-newer-than-file-p) |
28210 | 4194 (put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions) |
4195 (put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion) | |
4196 (put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory) | |
4197 (put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy) | |
4198 (put 'unhandled-file-name-directory 'ange-ftp | |
4199 'ange-ftp-unhandled-file-name-directory) | |
4200 (put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions) | |
4201 (put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache) | |
4202 (put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file) | |
4203 (put 'load 'ange-ftp 'ange-ftp-load) | |
4204 (put 'find-backup-file-name 'ange-ftp 'ange-ftp-find-backup-file-name) | |
4205 | |
4206 ;; Turn off truename processing to save time. | |
4207 ;; Treat each name as its own truename. | |
4208 (put 'file-truename 'ange-ftp 'identity) | |
4209 | |
4210 ;; Turn off RCS/SCCS processing to save time. | |
4211 ;; This returns nil for any file name as argument. | |
4212 (put 'vc-registered 'ange-ftp 'null) | |
4213 | |
4214 (put 'dired-call-process 'ange-ftp 'ange-ftp-dired-call-process) | |
4215 (put 'shell-command 'ange-ftp 'ange-ftp-shell-command) | |
4216 | |
4217 ;;; Define ways of getting at unmodified Emacs primitives, | |
4218 ;;; turning off our handler. | |
4219 | |
4220 (defun ange-ftp-run-real-handler (operation args) | |
4221 (let ((inhibit-file-name-handlers | |
4222 (cons 'ange-ftp-hook-function | |
4223 (cons 'ange-ftp-completion-hook-function | |
4224 (and (eq inhibit-file-name-operation operation) | |
4225 inhibit-file-name-handlers)))) | |
4226 (inhibit-file-name-operation operation)) | |
4227 (apply operation args))) | |
4228 | |
4229 (defun ange-ftp-real-file-name-directory (&rest args) | |
4230 (ange-ftp-run-real-handler 'file-name-directory args)) | |
4231 (defun ange-ftp-real-file-name-nondirectory (&rest args) | |
4232 (ange-ftp-run-real-handler 'file-name-nondirectory args)) | |
4233 (defun ange-ftp-real-file-name-as-directory (&rest args) | |
4234 (ange-ftp-run-real-handler 'file-name-as-directory args)) | |
4235 (defun ange-ftp-real-directory-file-name (&rest args) | |
4236 (ange-ftp-run-real-handler 'directory-file-name args)) | |
4237 (defun ange-ftp-real-expand-file-name (&rest args) | |
4238 (ange-ftp-run-real-handler 'expand-file-name args)) | |
4239 (defun ange-ftp-real-make-directory (&rest args) | |
4240 (ange-ftp-run-real-handler 'make-directory args)) | |
4241 (defun ange-ftp-real-delete-directory (&rest args) | |
4242 (ange-ftp-run-real-handler 'delete-directory args)) | |
4243 (defun ange-ftp-real-insert-file-contents (&rest args) | |
4244 (ange-ftp-run-real-handler 'insert-file-contents args)) | |
4245 (defun ange-ftp-real-directory-files (&rest args) | |
4246 (ange-ftp-run-real-handler 'directory-files args)) | |
4247 (defun ange-ftp-real-file-directory-p (&rest args) | |
4248 (ange-ftp-run-real-handler 'file-directory-p args)) | |
4249 (defun ange-ftp-real-file-writable-p (&rest args) | |
4250 (ange-ftp-run-real-handler 'file-writable-p args)) | |
4251 (defun ange-ftp-real-file-readable-p (&rest args) | |
4252 (ange-ftp-run-real-handler 'file-readable-p args)) | |
4253 (defun ange-ftp-real-file-executable-p (&rest args) | |
4254 (ange-ftp-run-real-handler 'file-executable-p args)) | |
4255 (defun ange-ftp-real-file-symlink-p (&rest args) | |
4256 (ange-ftp-run-real-handler 'file-symlink-p args)) | |
4257 (defun ange-ftp-real-delete-file (&rest args) | |
4258 (ange-ftp-run-real-handler 'delete-file args)) | |
4259 (defun ange-ftp-real-read-file-name-internal (&rest args) | |
4260 (ange-ftp-run-real-handler 'read-file-name-internal args)) | |
4261 (defun ange-ftp-real-verify-visited-file-modtime (&rest args) | |
4262 (ange-ftp-run-real-handler 'verify-visited-file-modtime args)) | |
4263 (defun ange-ftp-real-file-exists-p (&rest args) | |
4264 (ange-ftp-run-real-handler 'file-exists-p args)) | |
4265 (defun ange-ftp-real-write-region (&rest args) | |
4266 (ange-ftp-run-real-handler 'write-region args)) | |
4267 (defun ange-ftp-real-backup-buffer (&rest args) | |
4268 (ange-ftp-run-real-handler 'backup-buffer args)) | |
4269 (defun ange-ftp-real-copy-file (&rest args) | |
4270 (ange-ftp-run-real-handler 'copy-file args)) | |
4271 (defun ange-ftp-real-rename-file (&rest args) | |
4272 (ange-ftp-run-real-handler 'rename-file args)) | |
4273 (defun ange-ftp-real-file-attributes (&rest args) | |
4274 (ange-ftp-run-real-handler 'file-attributes args)) | |
30481 | 4275 (defun ange-ftp-real-file-newer-than-file-p (&rest args) |
4276 (ange-ftp-run-real-handler 'file-newer-than-file-p args)) | |
28210 | 4277 (defun ange-ftp-real-file-name-all-completions (&rest args) |
4278 (ange-ftp-run-real-handler 'file-name-all-completions args)) | |
4279 (defun ange-ftp-real-file-name-completion (&rest args) | |
4280 (ange-ftp-run-real-handler 'file-name-completion args)) | |
4281 (defun ange-ftp-real-insert-directory (&rest args) | |
4282 (ange-ftp-run-real-handler 'insert-directory args)) | |
4283 (defun ange-ftp-real-file-name-sans-versions (&rest args) | |
4284 (ange-ftp-run-real-handler 'file-name-sans-versions args)) | |
4285 (defun ange-ftp-real-shell-command (&rest args) | |
4286 (ange-ftp-run-real-handler 'shell-command args)) | |
4287 (defun ange-ftp-real-load (&rest args) | |
4288 (ange-ftp-run-real-handler 'load args)) | |
4289 (defun ange-ftp-real-find-backup-file-name (&rest args) | |
4290 (ange-ftp-run-real-handler 'find-backup-file-name args)) | |
4291 | |
4292 ;; Here we support using dired on remote hosts. | |
4293 ;; I have turned off the support for using dired on foreign directory formats. | |
4294 ;; That involves too many unclean hooks. | |
30459 | 4295 ;; It would be cleaner to support such operations by |
28210 | 4296 ;; converting the foreign directory format to something dired can understand; |
4297 ;; something close to ls -l output. | |
4298 ;; The logical place to do this is in the functions ange-ftp-parse-...-listing. | |
4299 | |
4300 ;; Some of the old dired hooks would still be needed even if this is done. | |
4301 ;; I have preserved (and modernized) those hooks. | |
4302 ;; So the format conversion should be all that is needed. | |
4303 | |
4304 (defun ange-ftp-insert-directory (file switches &optional wildcard full) | |
4305 (let ((short (ange-ftp-abbreviate-filename file)) | |
4306 (parsed (ange-ftp-ftp-name (expand-file-name file)))) | |
4307 (if parsed | |
4308 (insert | |
4309 (if wildcard | |
4310 (let ((default-directory (file-name-directory file))) | |
4311 (ange-ftp-ls (file-name-nondirectory file) switches nil nil t)) | |
4312 (ange-ftp-ls file switches full))) | |
4313 (ange-ftp-real-insert-directory file switches wildcard full)))) | |
4314 | |
4315 (defun ange-ftp-dired-uncache (dir) | |
4316 (if (ange-ftp-ftp-name (expand-file-name dir)) | |
4317 (setq ange-ftp-ls-cache-file nil))) | |
4318 | |
4319 (defvar ange-ftp-sans-version-alist nil | |
4320 "Alist of mapping host type into function to remove file version numbers.") | |
4321 | |
4322 (defun ange-ftp-file-name-sans-versions (file keep-backup-version) | |
4323 (let* ((short (ange-ftp-abbreviate-filename file)) | |
4324 (parsed (ange-ftp-ftp-name short)) | |
4325 host-type func) | |
4326 (if parsed | |
4327 (setq host-type (ange-ftp-host-type (car parsed)) | |
4328 func (cdr (assq (ange-ftp-host-type (car parsed)) | |
4329 ange-ftp-sans-version-alist)))) | |
4330 (if func (funcall func file keep-backup-version) | |
4331 (ange-ftp-real-file-name-sans-versions file keep-backup-version)))) | |
4332 | |
4333 ;; This is the handler for shell-command. | |
4334 (defun ange-ftp-shell-command (command &optional output-buffer error-buffer) | |
4335 (let* ((parsed (ange-ftp-ftp-name default-directory)) | |
4336 (host (nth 0 parsed)) | |
4337 (user (nth 1 parsed)) | |
4338 (name (nth 2 parsed))) | |
4339 (if (not parsed) | |
4340 (ange-ftp-real-shell-command command output-buffer error-buffer) | |
4341 (if (> (length name) 0) ; else it's $HOME | |
4342 (setq command (concat "cd " name "; " command))) | |
4343 (setq command | |
4344 (format "%s %s \"%s\"" ; remsh -l USER does not work well | |
4345 ; on a hp-ux machine I tried | |
4346 remote-shell-program host command)) | |
4347 (ange-ftp-message "Remote command '%s' ..." command) | |
4348 ;; Cannot call ange-ftp-real-dired-run-shell-command here as it | |
4349 ;; would prepend "cd default-directory" --- which bombs because | |
4350 ;; default-directory is in ange-ftp syntax for remote file names. | |
4351 (ange-ftp-real-shell-command command output-buffer error-buffer)))) | |
4352 | |
4353 ;;; This is the handler for call-process. | |
4354 (defun ange-ftp-dired-call-process (program discard &rest arguments) | |
4355 ;; PROGRAM is always one of those below in the cond in dired.el. | |
4356 ;; The ARGUMENTS are (nearly) always files. | |
4357 (if (ange-ftp-ftp-name default-directory) | |
4358 ;; Can't use ange-ftp-dired-host-type here because the current | |
4359 ;; buffer is *dired-check-process output* | |
4360 (condition-case oops | |
4361 (cond ((equal dired-chmod-program program) | |
4362 (ange-ftp-call-chmod arguments)) | |
4363 ;; ((equal "chgrp" program)) | |
4364 ;; ((equal dired-chown-program program)) | |
4365 (t (error "Unknown remote command: %s" program))) | |
4366 (ftp-error (insert (format "%s: %s, %s\n" | |
4367 (nth 1 oops) | |
4368 (nth 2 oops) | |
4369 (nth 3 oops))) | |
4370 ;; Caller expects nonzero value to mean failure. | |
4371 1) | |
4372 (error (insert (format "%s\n" (nth 1 oops))) | |
4373 1)) | |
4374 (apply 'call-process program nil (not discard) nil arguments))) | |
4375 | |
30459 | 4376 (defvar ange-ftp-remote-shell "rsh" |
28210 | 4377 "Remote shell to use for chmod, if FTP server rejects the `chmod' command.") |
4378 | |
4379 ;; Handle an attempt to run chmod on a remote file | |
4380 ;; by using the ftp chmod command. | |
4381 (defun ange-ftp-call-chmod (args) | |
4382 (if (< (length args) 2) | |
4383 (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args)) | |
4384 (let ((mode (car args)) | |
4385 (rest (cdr args))) | |
4386 (if (equal "--" (car rest)) | |
4387 (setq rest (cdr rest))) | |
4388 (mapcar | |
30459 | 4389 (lambda (file) |
4390 (setq file (expand-file-name file)) | |
4391 (let ((parsed (ange-ftp-ftp-name file))) | |
4392 (if parsed | |
4393 (let* ((host (nth 0 parsed)) | |
4394 (user (nth 1 parsed)) | |
4395 (name (ange-ftp-quote-string (nth 2 parsed))) | |
4396 (abbr (ange-ftp-abbreviate-filename file)) | |
4397 (result (ange-ftp-send-cmd host user | |
4398 (list 'chmod mode name) | |
4399 (format "doing chmod %s" | |
4400 abbr)))) | |
4401 (or (car result) | |
4402 (call-process | |
4403 ange-ftp-remote-shell | |
4404 nil t nil host dired-chmod-program mode name)))))) | |
28210 | 4405 rest)) |
4406 (setq ange-ftp-ls-cache-file nil) ;Stop confusing Dired. | |
4407 0) | |
4408 | |
4409 ;;; This is turned off because it has nothing properly to do | |
4410 ;;; with dired. It could be reasonable to adapt this to | |
4411 ;;; replace ange-ftp-copy-file. | |
4412 | |
4413 ;;;;; ------------------------------------------------------------ | |
4414 ;;;;; Noddy support for async copy-file within dired. | |
4415 ;;;;; ------------------------------------------------------------ | |
4416 | |
4417 ;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait) | |
4418 ;; "Documented as original." | |
4419 ;; (dired-handle-overwrite to) | |
4420 ;; (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil | |
4421 ;; cont nowait)) | |
4422 | |
4423 ;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg | |
4424 ;; &optional marker-char op1 | |
4425 ;; how-to) | |
4426 ;; "Documented as original." | |
4427 ;; ;; we need to let ange-ftp-dired-create-files know that we indirectly | |
4428 ;; ;; called it rather than somebody else. | |
4429 ;; (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is | |
4430 ;; (ange-ftp-real-dired-do-create-files op-symbol file-creator operation | |
4431 ;; arg marker-char op1 how-to))) | |
4432 | |
4433 ;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor | |
4434 ;; &optional marker-char) | |
4435 ;; "Documented as original." | |
4436 ;; (if (and (boundp 'ange-ftp-dired-do-create-files) | |
4437 ;; ;; called from ange-ftp-dired-do-create-files? | |
4438 ;; ange-ftp-dired-do-create-files | |
4439 ;; ;; any files worth copying? | |
4440 ;; fn-list | |
4441 ;; ;; we only support async copy-file at the mo. | |
4442 ;; (eq file-creator 'dired-copy-file) | |
4443 ;; ;; it is only worth calling the alternative function for remote files | |
4444 ;; ;; as we tie ourself in recursive knots otherwise. | |
4445 ;; (or (ange-ftp-ftp-name (car fn-list)) | |
4446 ;; ;; we can only call the name constructor for dired-do-create-files | |
4447 ;; ;; since the one for regexps starts prompting here, there and | |
4448 ;; ;; everywhere. | |
4449 ;; (ange-ftp-ftp-name (funcall name-constructor (car fn-list))))) | |
4450 ;; ;; use the process-filter driven routine rather than the iterative one. | |
4451 ;; (ange-ftp-dcf-1 file-creator | |
4452 ;; operation | |
4453 ;; fn-list | |
4454 ;; name-constructor | |
4455 ;; (and (boundp 'target) target) ;dynamically bound | |
4456 ;; marker-char | |
4457 ;; (current-buffer) | |
4458 ;; nil ;overwrite-query | |
4459 ;; nil ;overwrite-backup-query | |
4460 ;; nil ;failures | |
4461 ;; nil ;skipped | |
4462 ;; 0 ;success-count | |
4463 ;; (length fn-list) ;total | |
4464 ;; ) | |
4465 ;; ;; normal case... use the interactive routine... much cheaper. | |
4466 ;; (ange-ftp-real-dired-create-files file-creator operation fn-list | |
4467 ;; name-constructor marker-char))) | |
4468 | |
4469 ;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor | |
30459 | 4470 ;; target marker-char buffer overwrite-query |
28210 | 4471 ;; overwrite-backup-query failures skipped |
4472 ;; success-count total) | |
4473 ;; (let ((old-buf (current-buffer))) | |
4474 ;; (unwind-protect | |
4475 ;; (progn | |
4476 ;; (set-buffer buffer) | |
4477 ;; (if (null fn-list) | |
4478 ;; (ange-ftp-dcf-3 failures operation total skipped | |
4479 ;; success-count buffer) | |
30459 | 4480 |
28210 | 4481 ;; (let* ((from (car fn-list)) |
4482 ;; (to (funcall name-constructor from))) | |
4483 ;; (if (equal to from) | |
4484 ;; (progn | |
4485 ;; (setq to nil) | |
4486 ;; (dired-log "Cannot %s to same file: %s\n" | |
4487 ;; (downcase operation) from))) | |
4488 ;; (if (not to) | |
4489 ;; (ange-ftp-dcf-1 file-creator | |
4490 ;; operation | |
4491 ;; (cdr fn-list) | |
4492 ;; name-constructor | |
4493 ;; target | |
4494 ;; marker-char | |
4495 ;; buffer | |
4496 ;; overwrite-query | |
4497 ;; overwrite-backup-query | |
4498 ;; failures | |
4499 ;; (cons (dired-make-relative from) skipped) | |
4500 ;; success-count | |
4501 ;; total) | |
4502 ;; (let* ((overwrite (file-exists-p to)) | |
4503 ;; (overwrite-confirmed ; for dired-handle-overwrite | |
4504 ;; (and overwrite | |
4505 ;; (let ((help-form '(format "\ | |
4506 ;;Type SPC or `y' to overwrite file `%s', | |
4507 ;;DEL or `n' to skip to next, | |
4508 ;;ESC or `q' to not overwrite any of the remaining files, | |
4509 ;;`!' to overwrite all remaining files with no more questions." to))) | |
4510 ;; (dired-query 'overwrite-query | |
4511 ;; "Overwrite `%s'?" to)))) | |
4512 ;; ;; must determine if FROM is marked before file-creator | |
4513 ;; ;; gets a chance to delete it (in case of a move). | |
4514 ;; (actual-marker-char | |
4515 ;; (cond ((integerp marker-char) marker-char) | |
4516 ;; (marker-char (dired-file-marker from)) ; slow | |
4517 ;; (t nil)))) | |
4518 ;; (condition-case err | |
4519 ;; (funcall file-creator from to overwrite-confirmed | |
4520 ;; (list (function ange-ftp-dcf-2) | |
4521 ;; nil ;err | |
4522 ;; file-creator operation fn-list | |
4523 ;; name-constructor | |
4524 ;; target | |
4525 ;; marker-char actual-marker-char | |
4526 ;; buffer to from | |
4527 ;; overwrite | |
4528 ;; overwrite-confirmed | |
30459 | 4529 ;; overwrite-query |
28210 | 4530 ;; overwrite-backup-query |
4531 ;; failures skipped success-count | |
4532 ;; total) | |
4533 ;; t) | |
4534 ;; (file-error ; FILE-CREATOR aborted | |
4535 ;; (ange-ftp-dcf-2 nil ;result | |
4536 ;; nil ;line | |
4537 ;; err | |
4538 ;; file-creator operation fn-list | |
4539 ;; name-constructor | |
4540 ;; target | |
4541 ;; marker-char actual-marker-char | |
4542 ;; buffer to from | |
4543 ;; overwrite | |
4544 ;; overwrite-confirmed | |
30459 | 4545 ;; overwrite-query |
28210 | 4546 ;; overwrite-backup-query |
4547 ;; failures skipped success-count | |
4548 ;; total)))))))) | |
4549 ;; (set-buffer old-buf)))) | |
4550 | |
4551 ;;(defun ange-ftp-dcf-2 (result line err | |
4552 ;; file-creator operation fn-list | |
4553 ;; name-constructor | |
4554 ;; target | |
4555 ;; marker-char actual-marker-char | |
4556 ;; buffer to from | |
4557 ;; overwrite | |
4558 ;; overwrite-confirmed | |
30459 | 4559 ;; overwrite-query |
28210 | 4560 ;; overwrite-backup-query |
4561 ;; failures skipped success-count | |
4562 ;; total) | |
4563 ;; (let ((old-buf (current-buffer))) | |
4564 ;; (unwind-protect | |
4565 ;; (progn | |
4566 ;; (set-buffer buffer) | |
4567 ;; (if (or err (not result)) | |
4568 ;; (progn | |
4569 ;; (setq failures (cons (dired-make-relative from) failures)) | |
4570 ;; (dired-log "%s `%s' to `%s' failed:\n%s\n" | |
4571 ;; operation from to (or err line))) | |
4572 ;; (if overwrite | |
4573 ;; ;; If we get here, file-creator hasn't been aborted | |
4574 ;; ;; and the old entry (if any) has to be deleted | |
4575 ;; ;; before adding the new entry. | |
4576 ;; (dired-remove-file to)) | |
4577 ;; (setq success-count (1+ success-count)) | |
4578 ;; (message "%s: %d of %d" operation success-count total) | |
4579 ;; (dired-add-file to actual-marker-char)) | |
30459 | 4580 |
28210 | 4581 ;; (ange-ftp-dcf-1 file-creator operation (cdr fn-list) |
4582 ;; name-constructor | |
4583 ;; target | |
4584 ;; marker-char | |
4585 ;; buffer | |
30459 | 4586 ;; overwrite-query |
28210 | 4587 ;; overwrite-backup-query |
4588 ;; failures skipped success-count | |
4589 ;; total)) | |
4590 ;; (set-buffer old-buf)))) | |
4591 | |
4592 ;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count | |
4593 ;; buffer) | |
4594 ;; (let ((old-buf (current-buffer))) | |
4595 ;; (unwind-protect | |
4596 ;; (progn | |
4597 ;; (set-buffer buffer) | |
4598 ;; (cond | |
4599 ;; (failures | |
4600 ;; (dired-log-summary | |
4601 ;; (message "%s failed for %d of %d file%s %s" | |
4602 ;; operation (length failures) total | |
4603 ;; (dired-plural-s total) failures))) | |
4604 ;; (skipped | |
4605 ;; (dired-log-summary | |
4606 ;; (message "%s: %d of %d file%s skipped %s" | |
4607 ;; operation (length skipped) total | |
4608 ;; (dired-plural-s total) skipped))) | |
4609 ;; (t | |
4610 ;; (message "%s: %s file%s." | |
4611 ;; operation success-count (dired-plural-s success-count)))) | |
4612 ;; (dired-move-to-filename)) | |
4613 ;; (set-buffer old-buf)))) | |
4614 | |
4615 ;;;; ----------------------------------------------- | |
4616 ;;;; Unix Descriptive Listing (dl) Support | |
4617 ;;;; ----------------------------------------------- | |
4618 | |
4619 ;; This is turned off because nothing uses it currently | |
4620 ;; and because I don't understand what it's supposed to be for. --rms. | |
4621 | |
4622 ;;(defconst ange-ftp-dired-dl-re-dir | |
4623 ;; "^. [^ /]+/[ \n]" | |
4624 ;; "Regular expression to use to search for dl directories.") | |
4625 | |
4626 ;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist) | |
4627 ;; (setq ange-ftp-dired-re-dir-alist | |
4628 ;; (cons (cons 'unix:dl ange-ftp-dired-dl-re-dir) | |
4629 ;; ange-ftp-dired-re-dir-alist))) | |
4630 | |
4631 ;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol) | |
4632 ;; "In dired, move to the first character of the filename on this line." | |
4633 ;; ;; This is the Unix dl version. | |
4634 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
4635 ;; (let (case-fold-search) | |
4636 ;; (beginning-of-line) | |
4637 ;; (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ") | |
4638 ;; (goto-char (+ (point) 2)) | |
4639 ;; (if raise-error | |
4640 ;; (error "No file on this line") | |
4641 ;; nil)))) | |
4642 | |
4643 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist) | |
4644 ;; (setq ange-ftp-dired-move-to-filename-alist | |
4645 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename) | |
4646 ;; ange-ftp-dired-move-to-filename-alist))) | |
4647 | |
4648 ;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol) | |
4649 ;; ;; Assumes point is at beginning of filename. | |
4650 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
4651 ;; ;; On failure, signals an error or returns nil. | |
4652 ;; ;; This is the Unix dl version. | |
4653 ;; (let ((opoint (point)) | |
4654 ;; case-fold-search hidden) | |
4655 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
4656 ;; (setq hidden (and selective-display | |
4657 ;; (save-excursion | |
4658 ;; (search-forward "\r" eol t)))) | |
4659 ;; (if hidden | |
4660 ;; (if no-error | |
4661 ;; nil | |
4662 ;; (error | |
4663 ;; (substitute-command-keys | |
4664 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide"))) | |
4665 ;; (skip-chars-forward "^ /" eol) | |
4666 ;; (if (eq opoint (point)) | |
4667 ;; (if no-error | |
4668 ;; nil | |
4669 ;; (error "No file on this line")) | |
4670 ;; (point))))) | |
4671 | |
4672 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist) | |
4673 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
4674 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename) | |
4675 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
4676 | |
4677 ;;;; ------------------------------------------------------------ | |
4678 ;;;; VOS support (VOS support is probably broken, | |
4679 ;;;; but I don't know anything about VOS.) | |
4680 ;;;; ------------------------------------------------------------ | |
4681 ; | |
4682 ;(defun ange-ftp-fix-name-for-vos (name &optional reverse) | |
4683 ; (setq name (copy-sequence name)) | |
4684 ; (let ((from (if reverse ?\> ?\/)) | |
4685 ; (to (if reverse ?\/ ?\>)) | |
4686 ; (i (1- (length name)))) | |
4687 ; (while (>= i 0) | |
4688 ; (if (= (aref name i) from) | |
4689 ; (aset name i to)) | |
4690 ; (setq i (1- i))) | |
4691 ; name)) | |
4692 ; | |
4693 ;(or (assq 'vos ange-ftp-fix-name-func-alist) | |
4694 ; (setq ange-ftp-fix-name-func-alist | |
4695 ; (cons '(vos . ange-ftp-fix-name-for-vos) | |
4696 ; ange-ftp-fix-name-func-alist))) | |
4697 ; | |
4698 ;(or (memq 'vos ange-ftp-dumb-host-types) | |
4699 ; (setq ange-ftp-dumb-host-types | |
4700 ; (cons 'vos ange-ftp-dumb-host-types))) | |
4701 ; | |
4702 ;(defun ange-ftp-fix-dir-name-for-vos (dir-name) | |
4703 ; (ange-ftp-fix-name-for-vos | |
4704 ; (concat dir-name | |
4705 ; (if (eq ?/ (aref dir-name (1- (length dir-name)))) | |
4706 ; "" "/") | |
4707 ; "*"))) | |
4708 ; | |
4709 ;(or (assq 'vos ange-ftp-fix-dir-name-func-alist) | |
4710 ; (setq ange-ftp-fix-dir-name-func-alist | |
4711 ; (cons '(vos . ange-ftp-fix-dir-name-for-vos) | |
4712 ; ange-ftp-fix-dir-name-func-alist))) | |
4713 ; | |
4714 ;(defvar ange-ftp-vos-host-regexp nil | |
4715 ; "If a host matches this regexp then it is assumed to be running VOS.") | |
4716 ; | |
4717 ;(defun ange-ftp-vos-host (host) | |
4718 ; (and ange-ftp-vos-host-regexp | |
4719 ; (save-match-data | |
4720 ; (string-match ange-ftp-vos-host-regexp host)))) | |
4721 ; | |
4722 ;(defun ange-ftp-parse-vos-listing () | |
4723 ; "Parse the current buffer which is assumed to be in VOS list -all | |
4724 ;format, and return a hashtable as the result." | |
4725 ; (let ((tbl (ange-ftp-make-hashtable)) | |
4726 ; (type-list | |
4727 ; '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40) | |
4728 ; ("^Dirs: [0-9]+\n+" t 30))) | |
4729 ; type-regexp type-is-dir type-col file) | |
4730 ; (goto-char (point-min)) | |
4731 ; (save-match-data | |
4732 ; (while type-list | |
4733 ; (setq type-regexp (car (car type-list)) | |
4734 ; type-is-dir (nth 1 (car type-list)) | |
4735 ; type-col (nth 2 (car type-list)) | |
4736 ; type-list (cdr type-list)) | |
4737 ; (if (re-search-forward type-regexp nil t) | |
4738 ; (while (eq (char-after (point)) ? ) | |
4739 ; (move-to-column type-col) | |
4740 ; (setq file (buffer-substring (point) | |
4741 ; (progn | |
4742 ; (end-of-line 1) | |
4743 ; (point)))) | |
4744 ; (ange-ftp-put-hash-entry file type-is-dir tbl) | |
4745 ; (forward-line 1)))) | |
4746 ; (ange-ftp-put-hash-entry "." 'vosdir tbl) | |
4747 ; (ange-ftp-put-hash-entry ".." 'vosdir tbl)) | |
4748 ; tbl)) | |
4749 ; | |
4750 ;(or (assq 'vos ange-ftp-parse-list-func-alist) | |
4751 ; (setq ange-ftp-parse-list-func-alist | |
4752 ; (cons '(vos . ange-ftp-parse-vos-listing) | |
4753 ; ange-ftp-parse-list-func-alist))) | |
4754 | |
4755 ;;;; ------------------------------------------------------------ | |
4756 ;;;; VMS support. | |
4757 ;;;; ------------------------------------------------------------ | |
4758 | |
4759 ;; Convert NAME from UNIX-ish to VMS. If REVERSE given then convert from VMS | |
4760 ;; to UNIX-ish. | |
4761 (defun ange-ftp-fix-name-for-vms (name &optional reverse) | |
4762 (save-match-data | |
4763 (if reverse | |
4764 (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name) | |
4765 (let (drive dir file) | |
4766 (if (match-beginning 1) | |
4767 (setq drive (substring name | |
4768 (match-beginning 1) | |
4769 (match-end 1)))) | |
4770 (if (match-beginning 2) | |
4771 (setq dir | |
4772 (substring name (match-beginning 2) (match-end 2)))) | |
4773 (if (match-beginning 3) | |
4774 (setq file | |
4775 (substring name (match-beginning 3) (match-end 3)))) | |
4776 (and dir | |
30481 | 4777 (setq dir (subst-char-in-string |
37813
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
4778 ?/ ?. (substring dir 1 -1) t))) |
28210 | 4779 (concat (and drive |
4780 (concat "/" drive "/")) | |
4781 dir (and dir "/") | |
4782 file)) | |
4783 (error "name %s didn't match" name)) | |
4784 (let (drive dir file tmp) | |
4785 (if (string-match "^/[^:]+:/" name) | |
4786 (setq drive (substring name 1 | |
4787 (1- (match-end 0))) | |
4788 name (substring name (match-end 0)))) | |
4789 (setq tmp (file-name-directory name)) | |
4790 (if tmp | |
37813
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
4791 (setq dir (subst-char-in-string ?/ ?. (substring tmp 0 -1) t))) |
28210 | 4792 (setq file (file-name-nondirectory name)) |
4793 (concat drive | |
4794 (and dir (concat "[" (if drive nil ".") dir "]")) | |
4795 file))))) | |
4796 | |
4797 ;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1") | |
4798 ;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t) | |
4799 | |
4800 (or (assq 'vms ange-ftp-fix-name-func-alist) | |
4801 (setq ange-ftp-fix-name-func-alist | |
4802 (cons '(vms . ange-ftp-fix-name-for-vms) | |
4803 ange-ftp-fix-name-func-alist))) | |
4804 | |
4805 (or (memq 'vms ange-ftp-dumb-host-types) | |
4806 (setq ange-ftp-dumb-host-types | |
4807 (cons 'vms ange-ftp-dumb-host-types))) | |
4808 | |
4809 ;; It is important that this function barf for directories for which we know | |
4810 ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/". | |
4811 ;; This is because it saves an unnecessary FTP error, or possibly the listing | |
4812 ;; might succeed, but give erroneous info. This last case is particularly | |
4813 ;; likely for OS's (like MTS) for which we need to use a wildcard in order | |
4814 ;; to list a directory. | |
4815 | |
4816 ;; Convert name from UNIX-ish to VMS ready for a DIRectory listing. | |
4817 (defun ange-ftp-fix-dir-name-for-vms (dir-name) | |
4818 ;; Should there be entries for .. -> [-] and . -> [] below. Don't | |
4819 ;; think so, because expand-filename should have already short-circuited | |
4820 ;; them. | |
4821 (cond ((string-equal dir-name "/") | |
4822 (error "Cannot get listing for fictitious \"/\" directory.")) | |
4823 ((string-match "^/[-A-Z0-9_$]+:/$" dir-name) | |
4824 (error "Cannot get listing for device.")) | |
4825 ((ange-ftp-fix-name-for-vms dir-name)))) | |
30459 | 4826 |
28210 | 4827 (or (assq 'vms ange-ftp-fix-dir-name-func-alist) |
4828 (setq ange-ftp-fix-dir-name-func-alist | |
4829 (cons '(vms . ange-ftp-fix-dir-name-for-vms) | |
4830 ange-ftp-fix-dir-name-func-alist))) | |
4831 | |
4832 (defvar ange-ftp-vms-host-regexp nil) | |
4833 | |
4834 ;; Return non-nil if HOST is running VMS. | |
4835 (defun ange-ftp-vms-host (host) | |
4836 (and ange-ftp-vms-host-regexp | |
4837 (save-match-data | |
4838 (string-match ange-ftp-vms-host-regexp host)))) | |
4839 | |
4840 ;; Because some VMS ftp servers convert filenames to lower case | |
4841 ;; we allow a-z in the filename regexp. I'm not too happy about this. | |
4842 | |
4843 (defconst ange-ftp-vms-filename-regexp | |
4844 (concat | |
4845 "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\." | |
4846 "[-_A-Za-z0-9$]*;+[0-9]*\\)") | |
4847 "Regular expression to match for a valid VMS file name in Dired buffer. | |
4848 Stupid freaking bug! Position of _ and $ shouldn't matter but they do. | |
4849 Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX | |
4850 Other orders of $ and _ seem to all work just fine.") | |
4851 | |
4852 ;; These parsing functions are as general as possible because the syntax | |
4853 ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that | |
4854 ;; the VMS filename syntax is so rigid. If they bomb on a listing in the | |
4855 ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing | |
4856 ;; from vms.weird.net, then too bad. | |
4857 | |
4858 ;; Extract the next filename from a VMS dired-like listing. | |
4859 (defun ange-ftp-parse-vms-filename () | |
4860 (if (re-search-forward | |
4861 ange-ftp-vms-filename-regexp | |
4862 nil t) | |
4863 (buffer-substring (match-beginning 0) (match-end 0)))) | |
4864 | |
4865 ;; Parse the current buffer which is assumed to be in MultiNet FTP dir | |
4866 ;; format, and return a hashtable as the result. | |
4867 (defun ange-ftp-parse-vms-listing () | |
4868 (let ((tbl (ange-ftp-make-hashtable)) | |
4869 file) | |
4870 (goto-char (point-min)) | |
4871 (save-match-data | |
4872 (while (setq file (ange-ftp-parse-vms-filename)) | |
4873 (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file) | |
4874 ;; deal with directories | |
4875 (ange-ftp-put-hash-entry | |
4876 (substring file 0 (match-beginning 0)) t tbl) | |
4877 (ange-ftp-put-hash-entry file nil tbl) | |
4878 (if (string-match ";[0-9]+$" file) ; deal with extension | |
4879 ;; sans extension | |
4880 (ange-ftp-put-hash-entry | |
4881 (substring file 0 (match-beginning 0)) nil tbl))) | |
4882 (forward-line 1)) | |
4883 ;; Would like to look for a "Total" line, or a "Directory" line to | |
4884 ;; make sure that the listing isn't complete garbage before putting | |
4885 ;; in "." and "..", but we can't even count on all VAX's giving us | |
4886 ;; either of these. | |
4887 (ange-ftp-put-hash-entry "." t tbl) | |
4888 (ange-ftp-put-hash-entry ".." t tbl)) | |
4889 tbl)) | |
4890 | |
4891 (or (assq 'vms ange-ftp-parse-list-func-alist) | |
4892 (setq ange-ftp-parse-list-func-alist | |
4893 (cons '(vms . ange-ftp-parse-vms-listing) | |
4894 ange-ftp-parse-list-func-alist))) | |
4895 | |
4896 ;; This version only deletes file entries which have | |
4897 ;; explicit version numbers, because that is all VMS allows. | |
4898 | |
4899 ;; Can the following two functions be speeded up using file | |
4900 ;; completion functions? | |
4901 | |
4902 (defun ange-ftp-vms-delete-file-entry (name &optional dir-p) | |
4903 (if dir-p | |
4904 (ange-ftp-internal-delete-file-entry name t) | |
4905 (save-match-data | |
4906 (let ((file (ange-ftp-get-file-part name))) | |
4907 (if (string-match ";[0-9]+$" file) | |
30459 | 4908 ;; In VMS you can't delete a file without an explicit |
28210 | 4909 ;; version number, or wild-card (e.g. FOO;*) |
4910 ;; For now, we give up on wildcards. | |
4911 (let ((files (ange-ftp-get-hash-entry | |
4912 (file-name-directory name) | |
4913 ange-ftp-files-hashtable))) | |
4914 (if files | |
4915 (let* ((root (substring file 0 | |
4916 (match-beginning 0))) | |
4917 (regexp (concat "^" | |
4918 (regexp-quote root) | |
4919 ";[0-9]+$")) | |
4920 versions) | |
4921 (ange-ftp-del-hash-entry file files) | |
4922 ;; Now we need to check if there are any | |
4923 ;; versions left. If not, then delete the | |
4924 ;; root entry. | |
4925 (mapatoms | |
29587
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4926 (lambda (sym) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4927 (and (string-match regexp (get sym 'key)) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4928 (setq versions t))) |
28210 | 4929 files) |
4930 (or versions | |
4931 (ange-ftp-del-hash-entry root files)))))))))) | |
4932 | |
4933 (or (assq 'vms ange-ftp-delete-file-entry-alist) | |
4934 (setq ange-ftp-delete-file-entry-alist | |
4935 (cons '(vms . ange-ftp-vms-delete-file-entry) | |
4936 ange-ftp-delete-file-entry-alist))) | |
4937 | |
4938 (defun ange-ftp-vms-add-file-entry (name &optional dir-p) | |
4939 (if dir-p | |
4940 (ange-ftp-internal-add-file-entry name t) | |
4941 (let ((files (ange-ftp-get-hash-entry | |
4942 (file-name-directory name) | |
4943 ange-ftp-files-hashtable))) | |
4944 (if files | |
4945 (let ((file (ange-ftp-get-file-part name))) | |
4946 (save-match-data | |
4947 (if (string-match ";[0-9]+$" file) | |
4948 (ange-ftp-put-hash-entry | |
4949 (substring file 0 (match-beginning 0)) | |
4950 nil files) | |
4951 ;; Need to figure out what version of the file | |
4952 ;; is being added. | |
4953 (let ((regexp (concat "^" | |
4954 (regexp-quote file) | |
4955 ";\\([0-9]+\\)$")) | |
4956 (version 0)) | |
4957 (mapatoms | |
29587
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4958 (lambda (sym) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4959 (let ((name (get sym 'key))) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4960 (and (string-match regexp name) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4961 (setq version |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4962 (max version |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4963 (string-to-int |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4964 (substring name |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4965 (match-beginning 1) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4966 (match-end 1)))))))) |
28210 | 4967 files) |
4968 (setq version (1+ version)) | |
4969 (ange-ftp-put-hash-entry | |
4970 (concat file ";" (int-to-string version)) | |
4971 nil files)))) | |
4972 (ange-ftp-put-hash-entry file nil files)))))) | |
4973 | |
4974 (or (assq 'vms ange-ftp-add-file-entry-alist) | |
4975 (setq ange-ftp-add-file-entry-alist | |
4976 (cons '(vms . ange-ftp-vms-add-file-entry) | |
4977 ange-ftp-add-file-entry-alist))) | |
4978 | |
4979 | |
4980 (defun ange-ftp-add-vms-host (host) | |
4981 "Mark HOST as the name of a machine running VMS." | |
4982 (interactive | |
4983 (list (read-string "Host: " | |
4984 (let ((name (or (buffer-file-name) default-directory))) | |
4985 (and name (car (ange-ftp-ftp-name name))))))) | |
4986 (if (not (ange-ftp-vms-host host)) | |
4987 (setq ange-ftp-vms-host-regexp | |
4988 (concat "^" (regexp-quote host) "$" | |
4989 (and ange-ftp-vms-host-regexp "\\|") | |
4990 ange-ftp-vms-host-regexp) | |
4991 ange-ftp-host-cache nil))) | |
4992 | |
4993 | |
4994 (defun ange-ftp-vms-file-name-as-directory (name) | |
4995 (save-match-data | |
4996 (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name) | |
4997 (setq name (substring name 0 (match-beginning 0)))) | |
4998 (ange-ftp-real-file-name-as-directory name))) | |
4999 | |
5000 (or (assq 'vms ange-ftp-file-name-as-directory-alist) | |
5001 (setq ange-ftp-file-name-as-directory-alist | |
5002 (cons '(vms . ange-ftp-vms-file-name-as-directory) | |
5003 ange-ftp-file-name-as-directory-alist))) | |
5004 | |
5005 ;;; Tree dired support: | |
5006 | |
5007 ;; For this code I have borrowed liberally from Sebastian Kremer's | |
5008 ;; dired-vms.el | |
5009 | |
5010 | |
5011 ;;;; These regexps must be anchored to beginning of line. | |
5012 ;;;; Beware that the ftpd may put the device in front of the filename. | |
5013 | |
5014 ;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]" | |
5015 ;; "Regular expression to use to search for VMS executable files.") | |
5016 | |
5017 ;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]" | |
5018 ;; "Regular expression to use to search for VMS directories.") | |
5019 | |
5020 ;;(or (assq 'vms ange-ftp-dired-re-exe-alist) | |
5021 ;; (setq ange-ftp-dired-re-exe-alist | |
5022 ;; (cons (cons 'vms ange-ftp-dired-vms-re-exe) | |
5023 ;; ange-ftp-dired-re-exe-alist))) | |
5024 | |
5025 ;;(or (assq 'vms ange-ftp-dired-re-dir-alist) | |
5026 ;; (setq ange-ftp-dired-re-dir-alist | |
5027 ;; (cons (cons 'vms ange-ftp-dired-vms-re-dir) | |
5028 ;; ange-ftp-dired-re-dir-alist))) | |
5029 | |
5030 ;;(defun ange-ftp-dired-vms-insert-headerline (dir) | |
5031 ;; ;; VMS inserts a headerline. I would prefer the headerline | |
5032 ;; ;; to be in ange-ftp format. This version tries to | |
5033 ;; ;; be careful, because we can't count on a headerline | |
5034 ;; ;; over ftp, and we wouldn't want to delete anything | |
5035 ;; ;; important. | |
5036 ;; (save-excursion | |
5037 ;; (if (looking-at "^ wildcard ") | |
5038 ;; (forward-line 1)) | |
5039 ;; (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n") | |
5040 ;; (delete-region (point) (match-end 0)))) | |
5041 ;; (ange-ftp-real-dired-insert-headerline dir)) | |
5042 | |
5043 ;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist) | |
5044 ;; (setq ange-ftp-dired-insert-headerline-alist | |
5045 ;; (cons '(vms . ange-ftp-dired-vms-insert-headerline) | |
5046 ;; ange-ftp-dired-insert-headerline-alist))) | |
5047 | |
5048 ;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol) | |
5049 ;; "In dired, move to first char of filename on this line. | |
5050 ;;Returns position (point) or nil if no filename on this line." | |
5051 ;; ;; This is the VMS version. | |
5052 ;; (let (case-fold-search) | |
5053 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5054 ;; (beginning-of-line) | |
5055 ;; (if (re-search-forward ange-ftp-vms-filename-regexp eol t) | |
5056 ;; (goto-char (match-beginning 1)) | |
5057 ;; (if raise-error | |
5058 ;; (error "No file on this line") | |
5059 ;; nil)))) | |
5060 | |
5061 ;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist) | |
5062 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5063 ;; (cons '(vms . ange-ftp-dired-vms-move-to-filename) | |
5064 ;; ange-ftp-dired-move-to-filename-alist))) | |
5065 | |
5066 ;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol) | |
5067 ;; ;; Assumes point is at beginning of filename. | |
5068 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5069 ;; ;; case-fold-search must be nil, at least for VMS. | |
5070 ;; ;; On failure, signals an error or returns nil. | |
5071 ;; ;; This is the VMS version. | |
5072 ;; (let (opoint hidden case-fold-search) | |
5073 ;; (setq opoint (point)) | |
5074 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
5075 ;; (setq hidden (and selective-display | |
5076 ;; (save-excursion (search-forward "\r" eol t)))) | |
5077 ;; (if hidden | |
5078 ;; nil | |
5079 ;; (re-search-forward ange-ftp-vms-filename-regexp eol t)) | |
5080 ;; (or no-error | |
5081 ;; (not (eq opoint (point))) | |
5082 ;; (error | |
5083 ;; (if hidden | |
5084 ;; (substitute-command-keys | |
5085 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide") | |
5086 ;; "No file on this line"))) | |
5087 ;; (if (eq opoint (point)) | |
5088 ;; nil | |
5089 ;; (point)))) | |
5090 | |
5091 ;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist) | |
5092 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5093 ;; (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename) | |
5094 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5095 | |
5096 ;;(defun ange-ftp-dired-vms-between-files () | |
5097 ;; (save-excursion | |
5098 ;; (beginning-of-line) | |
5099 ;; (or (equal (following-char) 10) ; newline | |
5100 ;; (equal (following-char) 9) ; tab | |
5101 ;; (progn (forward-char 2) | |
5102 ;; (or (looking-at "Total of") | |
5103 ;; (equal (following-char) 32)))))) | |
5104 | |
5105 ;;(or (assq 'vms ange-ftp-dired-between-files-alist) | |
5106 ;; (setq ange-ftp-dired-between-files-alist | |
5107 ;; (cons '(vms . ange-ftp-dired-vms-between-files) | |
5108 ;; ange-ftp-dired-between-files-alist))) | |
5109 | |
5110 ;; Beware! In VMS filenames must be of the form "FILE.TYPE". | |
5111 ;; Therefore, we cannot just append a ".Z" to filenames for | |
5112 ;; compressed files. Instead, we turn "FILE.TYPE" into | |
5113 ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do. | |
5114 | |
5115 (defun ange-ftp-vms-make-compressed-filename (name &optional reverse) | |
5116 (cond | |
5117 ((string-match "-Z;[0-9]+$" name) | |
5118 (list nil (substring name 0 (match-beginning 0)))) | |
5119 ((string-match ";[0-9]+$" name) | |
5120 (list nil (substring name 0 (match-beginning 0)))) | |
5121 ((string-match "-Z$" name) | |
5122 (list nil (substring name 0 -2))) | |
5123 (t | |
5124 (list t | |
5125 (if (string-match ";[0-9]+$" name) | |
5126 (concat (substring name 0 (match-beginning 0)) | |
5127 "-Z") | |
5128 (concat name "-Z")))))) | |
5129 | |
5130 (or (assq 'vms ange-ftp-make-compressed-filename-alist) | |
5131 (setq ange-ftp-make-compressed-filename-alist | |
5132 (cons '(vms . ange-ftp-vms-make-compressed-filename) | |
5133 ange-ftp-make-compressed-filename-alist))) | |
5134 | |
5135 ;;;; When the filename is too long, VMS will use two lines to list a file | |
5136 ;;;; (damn them!) This will confuse dired. To solve this, need to convince | |
5137 ;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of | |
5138 ;;;; (forward-line 1). This would require a number of changes to dired.el. | |
5139 ;;;; If dired gets confused, revert-buffer will fix it. | |
5140 | |
5141 ;;(defun ange-ftp-dired-vms-ls-trim () | |
5142 ;; (goto-char (point-min)) | |
5143 ;; (let ((case-fold-search nil)) | |
5144 ;; (re-search-forward ange-ftp-vms-filename-regexp)) | |
5145 ;; (beginning-of-line) | |
5146 ;; (delete-region (point-min) (point)) | |
5147 ;; (forward-line 1) | |
5148 ;; (delete-region (point) (point-max))) | |
5149 | |
5150 | |
5151 ;;(or (assq 'vms ange-ftp-dired-ls-trim-alist) | |
5152 ;; (setq ange-ftp-dired-ls-trim-alist | |
5153 ;; (cons '(vms . ange-ftp-dired-vms-ls-trim) | |
30459 | 5154 ;; ange-ftp-dired-ls-trim-alist))) |
28210 | 5155 |
5156 (defun ange-ftp-vms-sans-version (name &rest args) | |
5157 (save-match-data | |
5158 (if (string-match ";[0-9]+$" name) | |
5159 (substring name 0 (match-beginning 0)) | |
5160 name))) | |
5161 | |
5162 (or (assq 'vms ange-ftp-sans-version-alist) | |
5163 (setq ange-ftp-sans-version-alist | |
5164 (cons '(vms . ange-ftp-vms-sans-version) | |
5165 ange-ftp-sans-version-alist))) | |
5166 | |
5167 ;;(defvar ange-ftp-file-version-alist) | |
5168 | |
5169 ;;;;; The vms version of clean-directory has 2 more optional args | |
5170 ;;;;; than the usual dired version. This is so that it can be used by | |
5171 ;;;;; ange-ftp-dired-vms-flag-backup-files. | |
5172 | |
5173 ;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg) | |
5174 ;; "Flag numerical backups for deletion. | |
5175 ;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest. | |
5176 ;;Positive prefix arg KEEP overrides `dired-kept-versions'; | |
5177 ;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive. | |
5178 | |
5179 ;;To clear the flags on these files, you can use \\[dired-flag-backup-files] | |
5180 ;;with a prefix argument." | |
5181 ;;; (interactive "P") ; Never actually called interactively. | |
5182 ;; (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions))) | |
5183 ;; (let ((early-retention (if (< keep 0) (- keep) kept-old-versions)) | |
5184 ;; ;; late-retention must NEVER be allowed to be less than 1 in VMS! | |
5185 ;; ;; This could wipe ALL copies of the file. | |
5186 ;; (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep))) | |
5187 ;; (action (or msg "Cleaning")) | |
5188 ;; (ange-ftp-trample-marker (or marker dired-del-marker)) | |
5189 ;; (ange-ftp-file-version-alist ())) | |
5190 ;; (message (concat action | |
5191 ;; " numerical backups (keeping %d late, %d old)...") | |
5192 ;; late-retention early-retention) | |
5193 ;; ;; Look at each file. | |
5194 ;; ;; If the file has numeric backup versions, | |
5195 ;; ;; put on ange-ftp-file-version-alist an element of the form | |
5196 ;; ;; (FILENAME . VERSION-NUMBER-LIST) | |
5197 ;; (dired-map-dired-file-lines (function | |
5198 ;; ange-ftp-dired-vms-collect-file-versions)) | |
5199 ;; ;; Sort each VERSION-NUMBER-LIST, | |
5200 ;; ;; and remove the versions not to be deleted. | |
5201 ;; (let ((fval ange-ftp-file-version-alist)) | |
5202 ;; (while fval | |
5203 ;; (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<))) | |
5204 ;; (v-count (length sorted-v-list))) | |
5205 ;; (if (> v-count (+ early-retention late-retention)) | |
5206 ;; (rplacd (nthcdr early-retention sorted-v-list) | |
5207 ;; (nthcdr (- v-count late-retention) | |
5208 ;; sorted-v-list))) | |
5209 ;; (rplacd (car fval) | |
5210 ;; (cdr sorted-v-list))) | |
5211 ;; (setq fval (cdr fval)))) | |
5212 ;; ;; Look at each file. If it is a numeric backup file, | |
5213 ;; ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion. | |
5214 ;; (dired-map-dired-file-lines | |
5215 ;; (function | |
5216 ;; ange-ftp-dired-vms-trample-file-versions mark)) | |
5217 ;; (message (concat action " numerical backups...done")))) | |
5218 | |
5219 ;;(or (assq 'vms ange-ftp-dired-clean-directory-alist) | |
5220 ;; (setq ange-ftp-dired-clean-directory-alist | |
5221 ;; (cons '(vms . ange-ftp-dired-vms-clean-directory) | |
5222 ;; ange-ftp-dired-clean-directory-alist))) | |
5223 | |
5224 ;;(defun ange-ftp-dired-vms-collect-file-versions (fn) | |
5225 ;; ;; "If it looks like file FN has versions, return a list of the versions. | |
5226 ;; ;;That is a list of strings which are file names. | |
5227 ;; ;;The caller may want to flag some of these files for deletion." | |
5228 ;;(let ((name (nth 2 (ange-ftp-ftp-name fn)))) | |
5229 ;; (if (string-match ";[0-9]+$" name) | |
5230 ;; (let* ((name (substring name 0 (match-beginning 0))) | |
5231 ;; (fn (ange-ftp-replace-name-component fn name))) | |
5232 ;; (if (not (assq fn ange-ftp-file-version-alist)) | |
5233 ;; (let* ((base-versions | |
5234 ;; (concat (file-name-nondirectory name) ";")) | |
5235 ;; (bv-length (length base-versions)) | |
5236 ;; (possibilities (file-name-all-completions | |
5237 ;; base-versions | |
5238 ;; (file-name-directory fn))) | |
5239 ;; (versions (mapcar | |
5240 ;; '(lambda (arg) | |
5241 ;; (if (and (string-match | |
5242 ;; "[0-9]+$" arg bv-length) | |
5243 ;; (= (match-beginning 0) bv-length)) | |
5244 ;; (string-to-int (substring arg bv-length)) | |
5245 ;; 0)) | |
5246 ;; possibilities))) | |
5247 ;; (if versions | |
5248 ;; (setq | |
5249 ;; ange-ftp-file-version-alist | |
5250 ;; (cons (cons fn versions) | |
5251 ;; ange-ftp-file-version-alist))))))))) | |
5252 | |
5253 ;;(defun ange-ftp-dired-vms-trample-file-versions (fn) | |
5254 ;; (let* ((start-vn (string-match ";[0-9]+$" fn)) | |
5255 ;; base-version-list) | |
5256 ;; (and start-vn | |
5257 ;; (setq base-version-list ; there was a base version to which | |
5258 ;; (assoc (substring fn 0 start-vn) ; this looks like a | |
5259 ;; ange-ftp-file-version-alist)) ; subversion | |
5260 ;; (not (memq (string-to-int (substring fn (1+ start-vn))) | |
5261 ;; base-version-list)) ; this one doesn't make the cut | |
5262 ;; (progn (beginning-of-line) | |
5263 ;; (delete-char 1) | |
5264 ;; (insert ange-ftp-trample-marker))))) | |
5265 | |
5266 ;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p) | |
5267 ;; (let ((dired-kept-versions 1) | |
5268 ;; (kept-old-versions 0) | |
5269 ;; marker msg) | |
5270 ;; (if unflag-p | |
5271 ;; (setq marker ?\040 msg "Unflagging") | |
5272 ;; (setq marker dired-del-marker msg "Cleaning")) | |
5273 ;; (ange-ftp-dired-vms-clean-directory nil marker msg))) | |
5274 | |
5275 ;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist) | |
5276 ;; (setq ange-ftp-dired-flag-backup-files-alist | |
5277 ;; (cons '(vms . ange-ftp-dired-vms-flag-backup-files) | |
5278 ;; ange-ftp-dired-flag-backup-files-alist))) | |
5279 | |
5280 ;;(defun ange-ftp-dired-vms-backup-diff (&optional switches) | |
5281 ;; (let ((file (dired-get-filename 'no-dir)) | |
5282 ;; bak) | |
5283 ;; (if (and (string-match ";[0-9]+$" file) | |
5284 ;; ;; Find most recent previous version. | |
5285 ;; (let ((root (substring file 0 (match-beginning 0))) | |
5286 ;; (ver | |
5287 ;; (string-to-int (substring file (1+ (match-beginning 0))))) | |
5288 ;; found) | |
5289 ;; (setq ver (1- ver)) | |
5290 ;; (while (and (> ver 0) (not found)) | |
5291 ;; (setq bak (concat root ";" (int-to-string ver))) | |
5292 ;; (and (file-exists-p bak) (setq found t)) | |
5293 ;; (setq ver (1- ver))) | |
5294 ;; found)) | |
5295 ;; (if switches | |
5296 ;; (diff (expand-file-name bak) (expand-file-name file) switches) | |
5297 ;; (diff (expand-file-name bak) (expand-file-name file))) | |
5298 ;; (error "No previous version found for %s" file)))) | |
5299 | |
5300 ;;(or (assq 'vms ange-ftp-dired-backup-diff-alist) | |
5301 ;; (setq ange-ftp-dired-backup-diff-alist | |
5302 ;; (cons '(vms . ange-ftp-dired-vms-backup-diff) | |
5303 ;; ange-ftp-dired-backup-diff-alist))) | |
5304 | |
5305 | |
5306 ;;;; ------------------------------------------------------------ | |
5307 ;;;; MTS support | |
5308 ;;;; ------------------------------------------------------------ | |
5309 | |
5310 | |
5311 ;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from | |
5312 ;; MTS to UNIX-ish. | |
5313 (defun ange-ftp-fix-name-for-mts (name &optional reverse) | |
5314 (save-match-data | |
5315 (if reverse | |
5316 (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name) | |
5317 (let (acct file) | |
5318 (if (match-beginning 1) | |
5319 (setq acct (substring name 0 (match-end 1)))) | |
5320 (if (match-beginning 2) | |
5321 (setq file (substring name | |
5322 (match-beginning 2) (match-end 2)))) | |
5323 (concat (and acct (concat "/" acct "/")) | |
5324 file)) | |
5325 (error "name %s didn't match" name)) | |
5326 (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name) | |
5327 (concat (substring name 1 (match-end 1)) | |
5328 (substring name (match-beginning 2) (match-end 2))) | |
5329 ;; Let's hope that mts will recognize it anyway. | |
5330 name)))) | |
5331 | |
5332 (or (assq 'mts ange-ftp-fix-name-func-alist) | |
5333 (setq ange-ftp-fix-name-func-alist | |
5334 (cons '(mts . ange-ftp-fix-name-for-mts) | |
5335 ange-ftp-fix-name-func-alist))) | |
5336 | |
5337 ;; Convert name from UNIX-ish to MTS ready for a DIRectory listing. | |
5338 ;; Remember that there are no directories in MTS. | |
5339 (defun ange-ftp-fix-dir-name-for-mts (dir-name) | |
5340 (if (string-equal dir-name "/") | |
5341 (error "Cannot get listing for fictitious \"/\" directory.") | |
5342 (let ((dir-name (ange-ftp-fix-name-for-mts dir-name))) | |
5343 (cond | |
5344 ((string-equal dir-name "") | |
5345 "?") | |
5346 ((string-match ":$" dir-name) | |
5347 (concat dir-name "?")) | |
5348 (dir-name))))) ; It's just a single file. | |
5349 | |
5350 (or (assq 'mts ange-ftp-fix-dir-name-func-alist) | |
5351 (setq ange-ftp-fix-dir-name-func-alist | |
5352 (cons '(mts . ange-ftp-fix-dir-name-for-mts) | |
5353 ange-ftp-fix-dir-name-func-alist))) | |
5354 | |
5355 (or (memq 'mts ange-ftp-dumb-host-types) | |
5356 (setq ange-ftp-dumb-host-types | |
5357 (cons 'mts ange-ftp-dumb-host-types))) | |
5358 | |
5359 (defvar ange-ftp-mts-host-regexp nil) | |
5360 | |
5361 ;; Return non-nil if HOST is running MTS. | |
5362 (defun ange-ftp-mts-host (host) | |
5363 (and ange-ftp-mts-host-regexp | |
5364 (save-match-data | |
5365 (string-match ange-ftp-mts-host-regexp host)))) | |
5366 | |
5367 ;; Parse the current buffer which is assumed to be in mts ftp dir format. | |
5368 (defun ange-ftp-parse-mts-listing () | |
5369 (let ((tbl (ange-ftp-make-hashtable))) | |
5370 (goto-char (point-min)) | |
5371 (save-match-data | |
5372 (while (re-search-forward ange-ftp-date-regexp nil t) | |
5373 (end-of-line) | |
5374 (skip-chars-backward " ") | |
5375 (let ((end (point))) | |
5376 (skip-chars-backward "-A-Z0-9_.!") | |
5377 (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl)) | |
5378 (forward-line 1))) | |
5379 ;; Don't need to bother with .. | |
5380 (ange-ftp-put-hash-entry "." t tbl) | |
5381 tbl)) | |
5382 | |
5383 (or (assq 'mts ange-ftp-parse-list-func-alist) | |
5384 (setq ange-ftp-parse-list-func-alist | |
5385 (cons '(mts . ange-ftp-parse-mts-listing) | |
5386 ange-ftp-parse-list-func-alist))) | |
5387 | |
5388 (defun ange-ftp-add-mts-host (host) | |
5389 "Mark HOST as the name of a machine running MTS." | |
5390 (interactive | |
5391 (list (read-string "Host: " | |
5392 (let ((name (or (buffer-file-name) default-directory))) | |
5393 (and name (car (ange-ftp-ftp-name name))))))) | |
5394 (if (not (ange-ftp-mts-host host)) | |
5395 (setq ange-ftp-mts-host-regexp | |
5396 (concat "^" (regexp-quote host) "$" | |
5397 (and ange-ftp-mts-host-regexp "\\|") | |
5398 ange-ftp-mts-host-regexp) | |
5399 ange-ftp-host-cache nil))) | |
5400 | |
5401 ;;; Tree dired support: | |
5402 | |
5403 ;;;; There aren't too many systems left that use MTS. This dired support will | |
5404 ;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems | |
5405 ;;;; implement ftp in the same way. If not, it might be necessary to make the | |
5406 ;;;; following more flexible. | |
5407 | |
5408 ;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol) | |
5409 ;; "In dired, move to first char of filename on this line. | |
5410 ;;Returns position (point) or nil if no filename on this line." | |
5411 ;; ;; This is the MTS version. | |
5412 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5413 ;; (beginning-of-line) | |
5414 ;; (if (re-search-forward | |
5415 ;; ange-ftp-date-regexp eol t) | |
5416 ;; (progn | |
5417 ;; (skip-chars-forward " ") ; Eat blanks after date | |
5418 ;; (skip-chars-forward "0-9:" eol) ; Eat time or year | |
5419 ;; (skip-chars-forward " " eol) ; one space before filename | |
5420 ;; ;; When listing an account other than the users own account it appends | |
5421 ;; ;; ACCT: to the beginning of the filename. Skip over this. | |
5422 ;; (and (looking-at "[A-Z0-9_.]+:") | |
5423 ;; (goto-char (match-end 0))) | |
5424 ;; (point)) | |
5425 ;; (if raise-error | |
5426 ;; (error "No file on this line") | |
5427 ;; nil))) | |
5428 | |
5429 ;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist) | |
5430 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5431 ;; (cons '(mts . ange-ftp-dired-mts-move-to-filename) | |
5432 ;; ange-ftp-dired-move-to-filename-alist))) | |
5433 | |
5434 ;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol) | |
5435 ;; ;; Assumes point is at beginning of filename. | |
5436 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5437 ;; ;; On failure, signals an error or returns nil. | |
5438 ;; ;; This is the MTS version. | |
5439 ;; (let (opoint hidden case-fold-search) | |
5440 ;; (setq opoint (point) | |
5441 ;; eol (save-excursion (end-of-line) (point)) | |
5442 ;; hidden (and selective-display | |
5443 ;; (save-excursion (search-forward "\r" eol t)))) | |
5444 ;; (if hidden | |
5445 ;; nil | |
5446 ;; (skip-chars-forward "-A-Z0-9._!" eol)) | |
5447 ;; (or no-error | |
5448 ;; (not (eq opoint (point))) | |
5449 ;; (error | |
5450 ;; (if hidden | |
5451 ;; (substitute-command-keys | |
5452 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide") | |
5453 ;; "No file on this line"))) | |
5454 ;; (if (eq opoint (point)) | |
5455 ;; nil | |
5456 ;; (point)))) | |
5457 | |
5458 ;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist) | |
5459 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5460 ;; (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename) | |
5461 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5462 | |
5463 ;;;; ------------------------------------------------------------ | |
5464 ;;;; CMS support | |
5465 ;;;; ------------------------------------------------------------ | |
5466 | |
5467 ;; Since CMS doesn't have any full file name syntax, we have to fudge | |
5468 ;; things with cd's. We actually send too many cd's, but it's dangerous | |
5469 ;; to try to remember the current minidisk, because if the connection | |
5470 ;; is closed and needs to be reopened, we will find ourselves back in | |
5471 ;; the default minidisk. This is fairly likely since CMS ftp servers | |
5472 ;; usually close the connection after 5 minutes of inactivity. | |
5473 | |
5474 ;; Have I got the filename character set right? | |
5475 | |
5476 (defun ange-ftp-fix-name-for-cms (name &optional reverse) | |
5477 (save-match-data | |
5478 (if reverse | |
5479 ;; Since we only convert output from a pwd in this direction, | |
5480 ;; we'll assume that it's a minidisk, and make it into a | |
5481 ;; directory file name. Note that the expand-dir-hashtable | |
5482 ;; stores directories without the trailing /. Is this | |
5483 ;; consistent? | |
5484 (concat "/" name) | |
5485 (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" | |
5486 name) | |
5487 (let ((minidisk (substring name 1 (match-end 1)))) | |
5488 (if (match-beginning 2) | |
5489 (let ((file (substring name (match-beginning 2) | |
5490 (match-end 2))) | |
5491 (cmd (concat "cd " minidisk)) | |
5492 | |
5493 ;; Note that host and user are bound in the call | |
5494 ;; to ange-ftp-send-cmd | |
5495 (proc (ange-ftp-get-process ange-ftp-this-host | |
5496 ange-ftp-this-user))) | |
5497 | |
5498 ;; Must use ange-ftp-raw-send-cmd here to avoid | |
5499 ;; an infinite loop. | |
5500 (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg)) | |
5501 file | |
5502 ;; failed... try ONCE more. | |
5503 (setq proc (ange-ftp-get-process ange-ftp-this-host | |
5504 ange-ftp-this-user)) | |
5505 (let ((result (ange-ftp-raw-send-cmd proc cmd | |
5506 ange-ftp-this-msg))) | |
5507 (if (car result) | |
5508 file | |
5509 ;; failed. give up. | |
5510 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user | |
5511 (format "cd to minidisk %s failed: %s" | |
5512 minidisk (cdr result))))))) | |
5513 ;; return the minidisk | |
5514 minidisk)) | |
5515 (error "Invalid CMS filename"))))) | |
5516 | |
5517 (or (assq 'cms ange-ftp-fix-name-func-alist) | |
5518 (setq ange-ftp-fix-name-func-alist | |
5519 (cons '(cms . ange-ftp-fix-name-for-cms) | |
5520 ange-ftp-fix-name-func-alist))) | |
5521 | |
5522 (or (memq 'cms ange-ftp-dumb-host-types) | |
5523 (setq ange-ftp-dumb-host-types | |
5524 (cons 'cms ange-ftp-dumb-host-types))) | |
5525 | |
5526 ;; Convert name from UNIX-ish to CMS ready for a DIRectory listing. | |
5527 (defun ange-ftp-fix-dir-name-for-cms (dir-name) | |
5528 (cond | |
5529 ((string-equal "/" dir-name) | |
5530 (error "Cannot get listing for fictitious \"/\" directory.")) | |
5531 ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name) | |
5532 (let* ((minidisk (substring dir-name (match-beginning 1) (match-end 1))) | |
5533 ;; host and user are bound in the call to ange-ftp-send-cmd | |
5534 (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user)) | |
5535 (cmd (concat "cd " minidisk)) | |
5536 (file (if (match-beginning 2) | |
5537 ;; it's a single file | |
5538 (substring dir-name (match-beginning 2) | |
5539 (match-end 2)) | |
5540 ;; use the wild-card | |
5541 "*"))) | |
5542 (if (car (ange-ftp-raw-send-cmd proc cmd)) | |
5543 file | |
5544 ;; try again... | |
5545 (setq proc (ange-ftp-get-process ange-ftp-this-host | |
5546 ange-ftp-this-user)) | |
5547 (let ((result (ange-ftp-raw-send-cmd proc cmd))) | |
5548 (if (car result) | |
5549 file | |
5550 ;; give up | |
5551 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user | |
5552 (format "cd to minidisk %s failed: %s" | |
5553 minidisk (cdr result)))))))) | |
5554 (t (error "Invalid CMS file name")))) | |
5555 | |
5556 (or (assq 'cms ange-ftp-fix-dir-name-func-alist) | |
5557 (setq ange-ftp-fix-dir-name-func-alist | |
5558 (cons '(cms . ange-ftp-fix-dir-name-for-cms) | |
5559 ange-ftp-fix-dir-name-func-alist))) | |
5560 | |
5561 (defvar ange-ftp-cms-host-regexp nil | |
5562 "Regular expression to match hosts running the CMS operating system.") | |
5563 | |
5564 ;; Return non-nil if HOST is running CMS. | |
5565 (defun ange-ftp-cms-host (host) | |
5566 (and ange-ftp-cms-host-regexp | |
5567 (save-match-data | |
5568 (string-match ange-ftp-cms-host-regexp host)))) | |
5569 | |
5570 (defun ange-ftp-add-cms-host (host) | |
5571 "Mark HOST as the name of a CMS host." | |
5572 (interactive | |
5573 (list (read-string "Host: " | |
5574 (let ((name (or (buffer-file-name) default-directory))) | |
5575 (and name (car (ange-ftp-ftp-name name))))))) | |
5576 (if (not (ange-ftp-cms-host host)) | |
5577 (setq ange-ftp-cms-host-regexp | |
5578 (concat "^" (regexp-quote host) "$" | |
5579 (and ange-ftp-cms-host-regexp "\\|") | |
5580 ange-ftp-cms-host-regexp) | |
5581 ange-ftp-host-cache nil))) | |
5582 | |
5583 (defun ange-ftp-parse-cms-listing () | |
5584 ;; Parse the current buffer which is assumed to be a CMS directory listing. | |
5585 ;; If we succeed in getting a listing, then we will assume that the minidisk | |
5586 ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work | |
5587 ;; because ange-ftp doesn't know that the root hashtable has only part of | |
5588 ;; the info. It will assume that if a minidisk isn't in it, then it doesn't | |
5589 ;; exist. It would be nice if completion worked for minidisks, as we | |
5590 ;; discover them. | |
5591 ; (let* ((dir-file (directory-file-name file)) | |
5592 ; (root (file-name-directory dir-file)) | |
5593 ; (minidisk (ange-ftp-get-file-part dir-file)) | |
5594 ; (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable))) | |
5595 ; (if root-tbl | |
5596 ; (ange-ftp-put-hash-entry minidisk t root-tbl) | |
5597 ; (setq root-tbl (ange-ftp-make-hashtable)) | |
5598 ; (ange-ftp-put-hash-entry minidisk t root-tbl) | |
5599 ; (ange-ftp-put-hash-entry "." t root-tbl) | |
5600 ; (ange-ftp-set-files root root-tbl))) | |
5601 ;; Now do the usual parsing | |
5602 (let ((tbl (ange-ftp-make-hashtable))) | |
5603 (goto-char (point-min)) | |
5604 (save-match-data | |
5605 (while | |
5606 (re-search-forward | |
5607 "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t) | |
5608 (ange-ftp-put-hash-entry | |
5609 (concat (buffer-substring (match-beginning 1) | |
5610 (match-end 1)) | |
5611 "." | |
5612 (buffer-substring (match-beginning 2) | |
5613 (match-end 2))) | |
5614 nil tbl) | |
5615 (forward-line 1)) | |
5616 (ange-ftp-put-hash-entry "." t tbl)) | |
5617 tbl)) | |
5618 | |
5619 (or (assq 'cms ange-ftp-parse-list-func-alist) | |
5620 (setq ange-ftp-parse-list-func-alist | |
5621 (cons '(cms . ange-ftp-parse-cms-listing) | |
5622 ange-ftp-parse-list-func-alist))) | |
30459 | 5623 |
28210 | 5624 ;;;;; Tree dired support: |
5625 | |
5626 ;;(defconst ange-ftp-dired-cms-re-exe | |
5627 ;; "^. [-A-Z0-9$_]+ +EXEC " | |
5628 ;; "Regular expression to use to search for CMS executables.") | |
5629 | |
5630 ;;(or (assq 'cms ange-ftp-dired-re-exe-alist) | |
5631 ;; (setq ange-ftp-dired-re-exe-alist | |
5632 ;; (cons (cons 'cms ange-ftp-dired-cms-re-exe) | |
5633 ;; ange-ftp-dired-re-exe-alist))) | |
5634 | |
5635 | |
5636 ;;(defun ange-ftp-dired-cms-insert-headerline (dir) | |
5637 ;; ;; CMS has no total line, so we insert a blank line for | |
5638 ;; ;; aesthetics. | |
5639 ;; (insert "\n") | |
5640 ;; (forward-char -1) | |
5641 ;; (ange-ftp-real-dired-insert-headerline dir)) | |
5642 | |
5643 ;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist) | |
5644 ;; (setq ange-ftp-dired-insert-headerline-alist | |
5645 ;; (cons '(cms . ange-ftp-dired-cms-insert-headerline) | |
5646 ;; ange-ftp-dired-insert-headerline-alist))) | |
5647 | |
5648 ;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol) | |
5649 ;; "In dired, move to the first char of filename on this line." | |
5650 ;; ;; This is the CMS version. | |
5651 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5652 ;; (let (case-fold-search) | |
5653 ;; (beginning-of-line) | |
5654 ;; (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t) | |
5655 ;; (goto-char (1+ (match-beginning 0))) | |
5656 ;; (if raise-error | |
5657 ;; (error "No file on this line") | |
5658 ;; nil)))) | |
5659 | |
5660 ;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist) | |
5661 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5662 ;; (cons '(cms . ange-ftp-dired-cms-move-to-filename) | |
5663 ;; ange-ftp-dired-move-to-filename-alist))) | |
5664 | |
5665 ;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol) | |
5666 ;; ;; Assumes point is at beginning of filename. | |
5667 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5668 ;; ;; case-fold-search must be nil, at least for VMS. | |
5669 ;; ;; On failure, signals an error or returns nil. | |
5670 ;; ;; This is the CMS version. | |
5671 ;; (let ((opoint (point)) | |
5672 ;; case-fold-search hidden) | |
5673 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
5674 ;; (setq hidden (and selective-display | |
5675 ;; (save-excursion | |
5676 ;; (search-forward "\r" eol t)))) | |
5677 ;; (if hidden | |
5678 ;; (if no-error | |
5679 ;; nil | |
5680 ;; (error | |
5681 ;; (substitute-command-keys | |
5682 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide"))) | |
5683 ;; (skip-chars-forward "-A-Z0-9$_" eol) | |
5684 ;; (skip-chars-forward " " eol) | |
5685 ;; (skip-chars-forward "-A-Z0-9$_" eol) | |
5686 ;; (if (eq opoint (point)) | |
5687 ;; (if no-error | |
5688 ;; nil | |
5689 ;; (error "No file on this line")) | |
5690 ;; (point))))) | |
5691 | |
5692 ;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist) | |
5693 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5694 ;; (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename) | |
5695 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5696 | |
5697 (defun ange-ftp-cms-make-compressed-filename (name &optional reverse) | |
5698 (if (string-match "-Z$" name) | |
5699 (list nil (substring name 0 -2)) | |
5700 (list t (concat name "-Z")))) | |
5701 | |
5702 (or (assq 'cms ange-ftp-make-compressed-filename-alist) | |
5703 (setq ange-ftp-make-compressed-filename-alist | |
5704 (cons '(cms . ange-ftp-cms-make-compressed-filename) | |
5705 ange-ftp-make-compressed-filename-alist))) | |
5706 | |
5707 ;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep) | |
5708 ;; (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep))) | |
5709 ;; (and name | |
5710 ;; (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name) | |
5711 ;; (concat (substring name 0 (match-end 1)) | |
5712 ;; "." | |
5713 ;; (substring name (match-beginning 2) (match-end 2))) | |
5714 ;; name)))) | |
5715 | |
5716 ;;(or (assq 'cms ange-ftp-dired-get-filename-alist) | |
5717 ;; (setq ange-ftp-dired-get-filename-alist | |
5718 ;; (cons '(cms . ange-ftp-dired-cms-get-filename) | |
5719 ;; ange-ftp-dired-get-filename-alist))) | |
5720 | |
5721 ;;;; ------------------------------------------------------------ | |
5722 ;;;; Finally provide package. | |
5723 ;;;; ------------------------------------------------------------ | |
5724 | |
5725 (provide 'ange-ftp) | |
5726 | |
5727 ;;; ange-ftp.el ends here |