Mercurial > emacs
annotate lisp/net/ange-ftp.el @ 38322:245114062ee0
Explain more clearly what it takes to make a customization permanent.
Explain global vs local minor modes first thing, then list the modes.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 08 Jul 2001 16:45:01 +0000 |
parents | b6dd8b1368af |
children | 3c2404604bae |
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) |
38312
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3429 "Return the modification time of remote file FILE. |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3430 Value is (0 0) if the modification time cannot be determined." |
30459 | 3431 (let* ((parsed (ange-ftp-ftp-name file)) |
38312
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3432 ;; At least one FTP server (wu-ftpd) can return a "226 |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3433 ;; Transfer complete" before the "213 MODTIME". Let's skip |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3434 ;; that. |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3435 (ange-ftp-skip-msgs (concat ange-ftp-skip-msgs "\\|^226")) |
30459 | 3436 (res (ange-ftp-send-cmd (car parsed) (cadr parsed) |
38312
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3437 (list 'quote "mdtm" (cadr (cdr parsed))))) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3438 (line (cdr res)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3439 (modtime '(0 0))) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3440 (when (string-match "^213" line) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3441 ;; MDTM should return "213 YYYYMMDDhhmmss" GMT on success. |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3442 (setq modtime |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3443 (encode-time |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3444 (string-to-number (substring line 16 18)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3445 (string-to-number (substring line 14 16)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3446 (string-to-number (substring line 12 14)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3447 (string-to-number (substring line 10 12)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3448 (string-to-number (substring line 8 10)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3449 (string-to-number (substring line 4 8)) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3450 0))) |
b6dd8b1368af
(ange-ftp-file-modtime): Ignore 226 responses
Gerd Moellmann <gerd@gnu.org>
parents:
37813
diff
changeset
|
3451 modtime)) |
30459 | 3452 |
28210 | 3453 (defun ange-ftp-verify-visited-file-modtime (buf) |
3454 (let ((name (buffer-file-name buf))) | |
3455 (if (and (stringp name) (ange-ftp-ftp-name name)) | |
30459 | 3456 (let ((file-mdtm (ange-ftp-file-modtime name)) |
3457 (buf-mdtm (with-current-buffer buf (visited-file-modtime)))) | |
3458 (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
|
3459 (<= (float-time file-mdtm) (float-time buf-mdtm)))) |
28210 | 3460 (ange-ftp-real-verify-visited-file-modtime buf)))) |
3461 | |
3462 ;;;; ------------------------------------------------------------ | |
3463 ;;;; File copying support... totally re-written 6/24/92. | |
3464 ;;;; ------------------------------------------------------------ | |
3465 | |
3466 (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive) | |
3467 (if (file-exists-p absname) | |
3468 (if (not interactive) | |
3469 (signal 'file-already-exists (list absname)) | |
3470 (if (not (yes-or-no-p (format "File %s already exists; %s anyway? " | |
3471 absname querystring))) | |
3472 (signal 'file-already-exists (list absname)))))) | |
3473 | |
3474 ;; async local copy commented out for now since I don't seem to get | |
3475 ;; the process sentinel called for some processes. | |
3476 ;; | |
3477 ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists | |
3478 ;; keep-date cont) | |
3479 ;; "Kludge to copy a local file and call a continuation when the copy | |
3480 ;; finishes." | |
3481 ;; ;; check to see if we can overwrite | |
3482 ;; (if (or (not ok-if-already-exists) | |
3483 ;; (numberp ok-if-already-exists)) | |
30459 | 3484 ;; (ange-ftp-barf-or-query-if-file-exists newname "copy to it" |
28210 | 3485 ;; (numberp ok-if-already-exists))) |
3486 ;; (let ((proc (start-process " *copy*" | |
3487 ;; (generate-new-buffer "*copy*") | |
3488 ;; "cp" | |
3489 ;; filename | |
3490 ;; newname)) | |
3491 ;; res) | |
3492 ;; (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel)) | |
3493 ;; (process-kill-without-query proc) | |
3494 ;; (save-excursion | |
3495 ;; (set-buffer (process-buffer proc)) | |
3496 ;; (make-variable-buffer-local 'copy-cont) | |
3497 ;; (setq copy-cont cont)))) | |
30459 | 3498 ;; |
28210 | 3499 ;; (defun ange-ftp-copy-file-locally-sentinel (proc status) |
3500 ;; (save-excursion | |
3501 ;; (set-buffer (process-buffer proc)) | |
3502 ;; (let ((cont copy-cont) | |
3503 ;; (result (buffer-string))) | |
3504 ;; (unwind-protect | |
3505 ;; (if (and (string-equal status "finished\n") | |
3506 ;; (zerop (length result))) | |
3507 ;; (ange-ftp-call-cont cont t nil) | |
3508 ;; (ange-ftp-call-cont cont | |
3509 ;; nil | |
3510 ;; (if (zerop (length result)) | |
3511 ;; (substring status 0 -1) | |
3512 ;; (substring result 0 -1)))) | |
3513 ;; (kill-buffer (current-buffer)))))) | |
3514 | |
3515 ;; this is the extended version of ange-ftp-copy-file-internal that works | |
3516 ;; asynchronously if asked nicely. | |
3517 (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists | |
3518 keep-date &optional msg cont nowait) | |
3519 (setq filename (expand-file-name filename) | |
3520 newname (expand-file-name newname)) | |
3521 | |
3522 ;; canonicalize newname if a directory. | |
3523 (if (file-directory-p newname) | |
3524 (setq newname (expand-file-name (file-name-nondirectory filename) newname))) | |
3525 | |
3526 (let ((f-parsed (ange-ftp-ftp-name filename)) | |
3527 (t-parsed (ange-ftp-ftp-name newname))) | |
3528 | |
3529 ;; local file to local file copy? | |
3530 (if (and (not f-parsed) (not t-parsed)) | |
3531 (progn | |
3532 (ange-ftp-real-copy-file filename newname ok-if-already-exists | |
3533 keep-date) | |
3534 (if cont | |
3535 (ange-ftp-call-cont cont t "Copied locally"))) | |
3536 ;; one or both files are remote. | |
3537 (let* ((f-host (and f-parsed (nth 0 f-parsed))) | |
3538 (f-user (and f-parsed (nth 1 f-parsed))) | |
3539 (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed)))) | |
3540 (f-abbr (ange-ftp-abbreviate-filename filename)) | |
3541 (t-host (and t-parsed (nth 0 t-parsed))) | |
3542 (t-user (and t-parsed (nth 1 t-parsed))) | |
3543 (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed)))) | |
3544 (t-abbr (ange-ftp-abbreviate-filename newname filename)) | |
3545 (binary (or (ange-ftp-binary-file filename) | |
3546 (ange-ftp-binary-file newname) | |
3547 (and (memq (ange-ftp-host-type f-host f-user) | |
3548 '(unix dumb-unix)) | |
3549 (memq (ange-ftp-host-type t-host t-user) | |
3550 '(unix dumb-unix))))) | |
3551 temp1 | |
3552 temp2) | |
3553 | |
3554 ;; check to see if we can overwrite | |
3555 (if (or (not ok-if-already-exists) | |
3556 (numberp ok-if-already-exists)) | |
30459 | 3557 (ange-ftp-barf-or-query-if-file-exists newname "copy to it" |
28210 | 3558 (numberp ok-if-already-exists))) |
3559 | |
3560 ;; do the copying. | |
3561 (if f-parsed | |
30459 | 3562 |
28210 | 3563 ;; filename was remote. |
3564 (progn | |
3565 (if (or (ange-ftp-use-gateway-p f-host) | |
3566 t-parsed) | |
3567 ;; have to use intermediate file if we are getting via | |
3568 ;; gateway machine or we are doing a remote to remote copy. | |
3569 (setq temp1 (ange-ftp-make-tmp-name f-host))) | |
30459 | 3570 |
28210 | 3571 (if binary |
3572 (ange-ftp-set-binary-mode f-host f-user)) | |
3573 | |
3574 (ange-ftp-send-cmd | |
3575 f-host | |
3576 f-user | |
3577 (list 'get f-name (or temp1 (ange-ftp-quote-string newname))) | |
3578 (or msg | |
3579 (if (and temp1 t-parsed) | |
3580 (format "Getting %s" f-abbr) | |
3581 (format "Copying %s to %s" f-abbr t-abbr))) | |
3582 (list (function ange-ftp-cf1) | |
3583 filename newname binary msg | |
3584 f-parsed f-host f-user f-name f-abbr | |
3585 t-parsed t-host t-user t-name t-abbr | |
3586 temp1 temp2 cont nowait) | |
3587 nowait)) | |
3588 | |
3589 ;; filename wasn't remote. newname must be remote. call the | |
3590 ;; function which does the remainder of the copying work. | |
3591 (ange-ftp-cf1 t nil | |
3592 filename newname binary msg | |
3593 f-parsed f-host f-user f-name f-abbr | |
3594 t-parsed t-host t-user t-name t-abbr | |
3595 nil nil cont nowait)))))) | |
3596 | |
3597 (defvar ange-ftp-waiting-flag nil) | |
3598 | |
3599 ;; next part of copying routine. | |
3600 (defun ange-ftp-cf1 (result line | |
3601 filename newname binary msg | |
3602 f-parsed f-host f-user f-name f-abbr | |
3603 t-parsed t-host t-user t-name t-abbr | |
3604 temp1 temp2 cont nowait) | |
3605 (if line | |
3606 ;; filename must have been remote, and we must have just done a GET. | |
3607 (unwind-protect | |
3608 (or result | |
3609 ;; GET failed for some reason. Clean up and get out. | |
3610 (progn | |
3611 (and temp1 (ange-ftp-del-tmp-name temp1)) | |
3612 (or cont | |
3613 (if ange-ftp-waiting-flag | |
3614 (throw 'ftp-error t) | |
3615 (signal 'ftp-error | |
3616 (list "Opening input file" | |
3617 (format "FTP Error: \"%s\"" line) | |
3618 filename)))))) | |
3619 ;; cleanup | |
3620 (if binary | |
3621 (ange-ftp-set-ascii-mode f-host f-user)))) | |
3622 | |
3623 (if result | |
3624 ;; We now have to copy either temp1 or filename to newname. | |
3625 (if t-parsed | |
30459 | 3626 |
28210 | 3627 ;; newname was remote. |
3628 (progn | |
3629 (if (ange-ftp-use-gateway-p t-host) | |
3630 (setq temp2 (ange-ftp-make-tmp-name t-host))) | |
30459 | 3631 |
28210 | 3632 ;; make sure data is moved into the right place for the |
3633 ;; outgoing transfer. gateway temporary files complicate | |
3634 ;; things nicely. | |
3635 (if temp1 | |
3636 (if temp2 | |
3637 (if (string-equal temp1 temp2) | |
3638 (setq temp1 nil) | |
3639 (ange-ftp-real-copy-file temp1 temp2 t)) | |
3640 (setq temp2 temp1 temp1 nil)) | |
3641 (if temp2 | |
3642 (ange-ftp-real-copy-file filename temp2 t))) | |
30459 | 3643 |
28210 | 3644 (if binary |
3645 (ange-ftp-set-binary-mode t-host t-user)) | |
3646 | |
3647 ;; tell the process filter what size the file is. | |
3648 (let ((attr (file-attributes (or temp2 filename)))) | |
3649 (if attr | |
3650 (ange-ftp-set-xfer-size t-host t-user (nth 7 attr)))) | |
3651 | |
3652 (ange-ftp-send-cmd | |
3653 t-host | |
3654 t-user | |
3655 (list 'put (or temp2 filename) t-name) | |
3656 (or msg | |
3657 (if (and temp2 f-parsed) | |
3658 (format "Putting %s" newname) | |
3659 (format "Copying %s to %s" f-abbr t-abbr))) | |
3660 (list (function ange-ftp-cf2) | |
3661 newname t-host t-user binary temp1 temp2 cont) | |
3662 nowait)) | |
30459 | 3663 |
28210 | 3664 ;; newname wasn't remote. |
3665 (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont)) | |
3666 | |
3667 ;; first copy failed, tell caller | |
3668 (ange-ftp-call-cont cont result line))) | |
3669 | |
3670 ;; last part of copying routine. | |
3671 (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont) | |
3672 (unwind-protect | |
3673 (if line | |
3674 ;; result from doing a local to remote copy. | |
3675 (unwind-protect | |
3676 (progn | |
3677 (or result | |
3678 (or cont | |
3679 (if ange-ftp-waiting-flag | |
3680 (throw 'ftp-error t) | |
3681 (signal 'ftp-error | |
3682 (list "Opening output file" | |
3683 (format "FTP Error: \"%s\"" line) | |
3684 newname))))) | |
30459 | 3685 |
28210 | 3686 (ange-ftp-add-file-entry newname)) |
30459 | 3687 |
28210 | 3688 ;; cleanup. |
3689 (if binary | |
3690 (ange-ftp-set-ascii-mode t-host t-user))) | |
30459 | 3691 |
28210 | 3692 ;; newname was local. |
3693 (if temp1 | |
3694 (ange-ftp-real-copy-file temp1 newname t))) | |
30459 | 3695 |
28210 | 3696 ;; clean up |
3697 (and temp1 (ange-ftp-del-tmp-name temp1)) | |
3698 (and temp2 (ange-ftp-del-tmp-name temp2)) | |
3699 (ange-ftp-call-cont cont result line))) | |
3700 | |
3701 (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists | |
3702 keep-date) | |
3703 (interactive "fCopy file: \nFCopy %s to file: \np") | |
3704 (ange-ftp-copy-file-internal filename | |
3705 newname | |
3706 ok-if-already-exists | |
3707 keep-date | |
3708 nil | |
3709 nil | |
3710 (interactive-p))) | |
3711 | |
3712 ;;;; ------------------------------------------------------------ | |
3713 ;;;; File renaming support. | |
3714 ;;;; ------------------------------------------------------------ | |
3715 | |
3716 (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed) | |
3717 "Rename remote file FILE to remote file NEWNAME." | |
3718 (let ((f-host (nth 0 f-parsed)) | |
3719 (f-user (nth 1 f-parsed)) | |
3720 (t-host (nth 0 t-parsed)) | |
3721 (t-user (nth 1 t-parsed))) | |
3722 (if (and (string-equal f-host t-host) | |
3723 (string-equal f-user t-user)) | |
3724 (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed))) | |
3725 (t-name (ange-ftp-quote-string (nth 2 t-parsed))) | |
3726 (cmd (list 'rename f-name t-name)) | |
3727 (fabbr (ange-ftp-abbreviate-filename filename)) | |
3728 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3729 (result (ange-ftp-send-cmd f-host f-user cmd | |
3730 (format "Renaming %s to %s" | |
3731 fabbr | |
3732 nabbr)))) | |
3733 (or (car result) | |
3734 (signal 'ftp-error | |
3735 (list | |
3736 "Renaming" | |
3737 (format "FTP Error: \"%s\"" (cdr result)) | |
3738 filename | |
3739 newname))) | |
3740 (ange-ftp-add-file-entry newname) | |
3741 (ange-ftp-delete-file-entry filename)) | |
3742 (ange-ftp-copy-file-internal filename newname t nil) | |
3743 (delete-file filename)))) | |
3744 | |
3745 (defun ange-ftp-rename-local-to-remote (filename newname) | |
3746 "Rename local FILENAME to remote file NEWNAME." | |
3747 (let* ((fabbr (ange-ftp-abbreviate-filename filename)) | |
3748 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3749 (msg (format "Renaming %s to %s" fabbr nabbr))) | |
3750 (ange-ftp-copy-file-internal filename newname t nil msg) | |
3751 (let (ange-ftp-process-verbose) | |
3752 (delete-file filename)))) | |
3753 | |
3754 (defun ange-ftp-rename-remote-to-local (filename newname) | |
3755 "Rename remote file FILENAME to local file NEWNAME." | |
3756 (let* ((fabbr (ange-ftp-abbreviate-filename filename)) | |
3757 (nabbr (ange-ftp-abbreviate-filename newname filename)) | |
3758 (msg (format "Renaming %s to %s" fabbr nabbr))) | |
3759 (ange-ftp-copy-file-internal filename newname t nil msg) | |
3760 (let (ange-ftp-process-verbose) | |
3761 (delete-file filename)))) | |
3762 | |
3763 (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists) | |
3764 (interactive "fRename file: \nFRename %s to file: \np") | |
3765 (setq filename (expand-file-name filename)) | |
3766 (setq newname (expand-file-name newname)) | |
3767 (let* ((f-parsed (ange-ftp-ftp-name filename)) | |
3768 (t-parsed (ange-ftp-ftp-name newname))) | |
3769 (if (and (or f-parsed t-parsed) | |
3770 (or (not ok-if-already-exists) | |
3771 (numberp ok-if-already-exists))) | |
3772 (ange-ftp-barf-or-query-if-file-exists | |
3773 newname | |
3774 "rename to it" | |
3775 (numberp ok-if-already-exists))) | |
3776 (if f-parsed | |
3777 (if t-parsed | |
3778 (ange-ftp-rename-remote-to-remote filename newname f-parsed | |
3779 t-parsed) | |
3780 (ange-ftp-rename-remote-to-local filename newname)) | |
3781 (if t-parsed | |
3782 (ange-ftp-rename-local-to-remote filename newname) | |
3783 (ange-ftp-real-rename-file filename newname ok-if-already-exists))))) | |
3784 | |
3785 ;;;; ------------------------------------------------------------ | |
3786 ;;;; File name completion support. | |
3787 ;;;; ------------------------------------------------------------ | |
3788 | |
3789 ;; If the file entry SYM is a symlink, returns whether its file exists. | |
3790 ;; Note that `ange-ftp-this-dir' is used as a free variable. | |
3791 (defun ange-ftp-file-entry-active-p (sym) | |
3792 (let ((val (get sym 'val))) | |
3793 (or (not (stringp val)) | |
3794 (file-exists-p (ange-ftp-expand-symlink val ange-ftp-this-dir))))) | |
3795 | |
3796 ;; If the file entry is not a directory (nor a symlink pointing to a directory) | |
3797 ;; returns whether the file (or file pointed to by the symlink) is ignored | |
3798 ;; by completion-ignored-extensions. | |
3799 ;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern' | |
3800 ;; are used as free variables. | |
3801 (defun ange-ftp-file-entry-not-ignored-p (sym) | |
3802 (let ((val (get sym 'val)) | |
3803 (symname (symbol-name sym))) | |
3804 (if (stringp val) | |
3805 (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir))) | |
3806 (or (file-directory-p file) | |
3807 (and (file-exists-p file) | |
3808 (not (string-match ange-ftp-completion-ignored-pattern | |
3809 symname))))) | |
3810 (or val ; is a directory name | |
3811 (not (string-match ange-ftp-completion-ignored-pattern symname)))))) | |
3812 | |
3813 (defun ange-ftp-file-name-all-completions (file dir) | |
3814 (let ((ange-ftp-this-dir (expand-file-name dir))) | |
3815 (if (ange-ftp-ftp-name ange-ftp-this-dir) | |
3816 (progn | |
3817 (ange-ftp-barf-if-not-directory ange-ftp-this-dir) | |
3818 (setq ange-ftp-this-dir | |
3819 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) | |
3820 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir)) | |
3821 (completions | |
3822 (all-completions file tbl | |
3823 (function ange-ftp-file-entry-active-p)))) | |
3824 | |
3825 ;; see whether each matching file is a directory or not... | |
3826 (mapcar | |
30459 | 3827 (lambda (file) |
3828 (let ((ent (ange-ftp-get-hash-entry file tbl))) | |
3829 (if (and ent | |
3830 (or (not (stringp ent)) | |
3831 (file-directory-p | |
3832 (ange-ftp-expand-symlink ent | |
3833 ange-ftp-this-dir)))) | |
3834 (concat file "/") | |
3835 file))) | |
28210 | 3836 completions))) |
3837 | |
3838 (if (or (and (eq system-type 'windows-nt) | |
3839 (string-match "^[a-zA-Z]:[/\\]$" ange-ftp-this-dir)) | |
3840 (string-equal "/" ange-ftp-this-dir)) | |
3841 (nconc (all-completions file (ange-ftp-generate-root-prefixes)) | |
3842 (ange-ftp-real-file-name-all-completions file | |
3843 ange-ftp-this-dir)) | |
3844 (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir))))) | |
3845 | |
3846 (defun ange-ftp-file-name-completion (file dir) | |
3847 (let ((ange-ftp-this-dir (expand-file-name dir))) | |
3848 (if (ange-ftp-ftp-name ange-ftp-this-dir) | |
3849 (progn | |
3850 (ange-ftp-barf-if-not-directory ange-ftp-this-dir) | |
3851 (if (equal file "") | |
3852 "" | |
3853 (setq ange-ftp-this-dir | |
3854 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) ;real? | |
3855 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir)) | |
3856 (ange-ftp-completion-ignored-pattern | |
30459 | 3857 (mapconcat (lambda (s) (if (stringp s) |
3858 (concat (regexp-quote s) "$") | |
3859 "/")) ; / never in filename | |
28210 | 3860 completion-ignored-extensions |
3861 "\\|"))) | |
3862 (save-match-data | |
3863 (or (ange-ftp-file-name-completion-1 | |
3864 file tbl ange-ftp-this-dir | |
3865 (function ange-ftp-file-entry-not-ignored-p)) | |
3866 (ange-ftp-file-name-completion-1 | |
3867 file tbl ange-ftp-this-dir | |
3868 (function ange-ftp-file-entry-active-p))))))) | |
3869 | |
3870 (if (or (and (eq system-type 'windows-nt) | |
3871 (string-match "^[a-zA-Z]:[/\\]$" ange-ftp-this-dir)) | |
3872 (string-equal "/" ange-ftp-this-dir)) | |
3873 (try-completion | |
3874 file | |
3875 (nconc (ange-ftp-generate-root-prefixes) | |
3876 (mapcar 'list | |
3877 (ange-ftp-real-file-name-all-completions | |
3878 file ange-ftp-this-dir)))) | |
3879 (ange-ftp-real-file-name-completion file ange-ftp-this-dir))))) | |
3880 | |
3881 | |
3882 (defun ange-ftp-file-name-completion-1 (file tbl dir predicate) | |
3883 (let ((bestmatch (try-completion file tbl predicate))) | |
3884 (if bestmatch | |
3885 (if (eq bestmatch t) | |
3886 (if (file-directory-p (expand-file-name file dir)) | |
3887 (concat file "/") | |
3888 t) | |
3889 (if (and (eq (try-completion bestmatch tbl predicate) t) | |
3890 (file-directory-p | |
3891 (expand-file-name bestmatch dir))) | |
3892 (concat bestmatch "/") | |
3893 bestmatch))))) | |
3894 | |
3895 ;; Put these lines uncommmented in your .emacs if you want C-r to refresh | |
3896 ;; ange-ftp's cache whilst doing filename completion. | |
3897 ;; | |
3898 ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir) | |
3899 ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir) | |
3900 | |
3901 ;; The autoload cookie is to make sure the doc is always available. | |
3902 ;;;###autoload (defalias 'ange-ftp-re-read-dir 'ange-ftp-reread-dir) | |
3903 ;;;###autoload | |
3904 (defun ange-ftp-reread-dir (&optional dir) | |
3905 "Reread remote directory DIR to update the directory cache. | |
3906 The implementation of remote ftp file names caches directory contents | |
3907 for speed. Therefore, when new remote files are created, Emacs | |
3908 may not know they exist. You can use this command to reread a specific | |
3909 directory, so that Emacs will know its current contents." | |
3910 (interactive) | |
3911 (if dir | |
3912 (setq dir (expand-file-name dir)) | |
3913 (setq dir (file-name-directory (expand-file-name (buffer-string))))) | |
3914 (if (ange-ftp-ftp-name dir) | |
3915 (progn | |
3916 (setq ange-ftp-ls-cache-file nil) | |
3917 (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable) | |
3918 (ange-ftp-get-files dir t)))) | |
3919 | |
3920 (defun ange-ftp-make-directory (dir &optional parents) | |
3921 (interactive (list (expand-file-name (read-file-name "Make directory: ")))) | |
3922 (if parents | |
3923 (let ((parent (file-name-directory (directory-file-name dir)))) | |
3924 (or (file-exists-p parent) | |
3925 (ange-ftp-make-directory parent parents)))) | |
3926 (if (file-exists-p dir) | |
3927 (error "Cannot make directory %s: file already exists" dir) | |
3928 (let ((parsed (ange-ftp-ftp-name dir))) | |
3929 (if parsed | |
3930 (let* ((host (nth 0 parsed)) | |
3931 (user (nth 1 parsed)) | |
3932 ;; Some ftp's on unix machines (at least on Suns) | |
3933 ;; insist that mkdir take a filename, and not a | |
3934 ;; directory-name name as an arg. Argh!! This is a bug. | |
3935 ;; Non-unix machines will probably always insist | |
3936 ;; that mkdir takes a directory-name as an arg | |
3937 ;; (as the ftp man page says it should). | |
3938 (name (ange-ftp-quote-string | |
3939 (if (eq (ange-ftp-host-type host) 'unix) | |
3940 (ange-ftp-real-directory-file-name (nth 2 parsed)) | |
3941 (ange-ftp-real-file-name-as-directory | |
3942 (nth 2 parsed))))) | |
3943 (abbr (ange-ftp-abbreviate-filename dir)) | |
3944 (result (ange-ftp-send-cmd host user | |
3945 (list 'mkdir name) | |
3946 (format "Making directory %s" | |
3947 abbr)))) | |
3948 (or (car result) | |
3949 (ange-ftp-error host user | |
3950 (format "Could not make directory %s: %s" | |
3951 dir | |
3952 (cdr result)))) | |
3953 (ange-ftp-add-file-entry dir t)) | |
3954 (ange-ftp-real-make-directory dir))))) | |
3955 | |
3956 (defun ange-ftp-delete-directory (dir) | |
3957 (if (file-directory-p dir) | |
3958 (let ((parsed (ange-ftp-ftp-name dir))) | |
3959 (if parsed | |
3960 (let* ((host (nth 0 parsed)) | |
3961 (user (nth 1 parsed)) | |
3962 ;; Some ftp's on unix machines (at least on Suns) | |
3963 ;; insist that rmdir take a filename, and not a | |
3964 ;; directory-name name as an arg. Argh!! This is a bug. | |
3965 ;; Non-unix machines will probably always insist | |
3966 ;; that rmdir takes a directory-name as an arg | |
3967 ;; (as the ftp man page says it should). | |
3968 (name (ange-ftp-quote-string | |
3969 (if (eq (ange-ftp-host-type host) 'unix) | |
3970 (ange-ftp-real-directory-file-name | |
3971 (nth 2 parsed)) | |
3972 (ange-ftp-real-file-name-as-directory | |
3973 (nth 2 parsed))))) | |
3974 (abbr (ange-ftp-abbreviate-filename dir)) | |
3975 (result (ange-ftp-send-cmd host user | |
3976 (list 'rmdir name) | |
3977 (format "Removing directory %s" | |
3978 abbr)))) | |
3979 (or (car result) | |
3980 (ange-ftp-error host user | |
3981 (format "Could not remove directory %s: %s" | |
3982 dir | |
3983 (cdr result)))) | |
3984 (ange-ftp-delete-file-entry dir t)) | |
3985 (ange-ftp-real-delete-directory dir))) | |
3986 (error "Not a directory: %s" dir))) | |
3987 | |
3988 ;; Make a local copy of FILE and return its name. | |
3989 | |
3990 (defun ange-ftp-file-local-copy (file) | |
3991 (let* ((fn1 (expand-file-name file)) | |
3992 (pa1 (ange-ftp-ftp-name fn1))) | |
3993 (if pa1 | |
3994 (let ((tmp1 (ange-ftp-make-tmp-name (car pa1)))) | |
3995 (ange-ftp-copy-file-internal fn1 tmp1 t nil | |
3996 (format "Getting %s" fn1)) | |
3997 tmp1)))) | |
3998 | |
3999 (defun ange-ftp-load (file &optional noerror nomessage nosuffix) | |
4000 (if (ange-ftp-ftp-name file) | |
4001 (let ((tryfiles (if nosuffix | |
4002 (list file) | |
4003 (list (concat file ".elc") (concat file ".el") file))) | |
4004 ;; make sure there are no references to temp files | |
4005 (load-force-doc-strings t) | |
4006 copy) | |
4007 (while (and tryfiles (not copy)) | |
4008 (catch 'ftp-error | |
4009 (let ((ange-ftp-waiting-flag t)) | |
4010 (condition-case error | |
4011 (setq copy (ange-ftp-file-local-copy (car tryfiles))) | |
4012 (ftp-error nil)))) | |
4013 (setq tryfiles (cdr tryfiles))) | |
4014 (if copy | |
4015 (unwind-protect | |
4016 (funcall 'load copy noerror nomessage nosuffix) | |
4017 (delete-file copy)) | |
4018 (or noerror | |
4019 (signal 'file-error (list "Cannot open load file" file))) | |
4020 nil)) | |
4021 (ange-ftp-real-load file noerror nomessage nosuffix))) | |
4022 | |
4023 ;; Calculate default-unhandled-directory for a given ange-ftp buffer. | |
4024 (defun ange-ftp-unhandled-file-name-directory (filename) | |
4025 (file-name-directory ange-ftp-tmp-name-template)) | |
4026 | |
4027 | |
4028 ;; Need the following functions for making filenames of compressed | |
4029 ;; files, because some OS's (unlike UNIX) do not allow a filename to | |
4030 ;; have two extensions. | |
4031 | |
4032 (defvar ange-ftp-make-compressed-filename-alist nil | |
4033 "Alist of host-type-specific functions to process file names for compression. | |
4034 Each element has the form (TYPE . FUNC). | |
4035 FUNC should take one argument, a file name, and return a list | |
4036 of the form (COMPRESSING NEWNAME). | |
30459 | 4037 COMPRESSING should be t if the specified file should be compressed, |
28210 | 4038 and nil if it should be uncompressed (that is, if it is a compressed file). |
4039 NEWNAME should be the name to give the new compressed or uncompressed file.") | |
4040 | |
4041 (defun ange-ftp-dired-compress-file (name) | |
4042 (let ((parsed (ange-ftp-ftp-name name)) | |
4043 conversion-func) | |
4044 (if (and parsed | |
4045 (setq conversion-func | |
4046 (cdr (assq (ange-ftp-host-type (car parsed)) | |
4047 ange-ftp-make-compressed-filename-alist)))) | |
4048 (let* ((decision | |
4049 (save-match-data (funcall conversion-func name))) | |
4050 (compressing (car decision)) | |
4051 (newfile (nth 1 decision))) | |
4052 (if compressing | |
4053 (ange-ftp-compress name newfile) | |
4054 (ange-ftp-uncompress name newfile))) | |
4055 (let (file-name-handler-alist) | |
4056 (dired-compress-file name))))) | |
4057 | |
4058 ;; Copy FILE to this machine, compress it, and copy out to NFILE. | |
4059 (defun ange-ftp-compress (file nfile) | |
4060 (let* ((parsed (ange-ftp-ftp-name file)) | |
4061 (tmp1 (ange-ftp-make-tmp-name (car parsed))) | |
4062 (tmp2 (ange-ftp-make-tmp-name (car parsed))) | |
4063 (abbr (ange-ftp-abbreviate-filename file)) | |
4064 (nabbr (ange-ftp-abbreviate-filename nfile)) | |
4065 (msg1 (format "Getting %s" abbr)) | |
4066 (msg2 (format "Putting %s" nabbr))) | |
4067 (unwind-protect | |
4068 (progn | |
4069 (ange-ftp-copy-file-internal file tmp1 t nil msg1) | |
4070 (and ange-ftp-process-verbose | |
4071 (ange-ftp-message "Compressing %s..." abbr)) | |
4072 (call-process-region (point) | |
4073 (point) | |
4074 shell-file-name | |
4075 nil | |
4076 t | |
4077 nil | |
4078 "-c" | |
4079 (format "compress -f -c < %s > %s" tmp1 tmp2)) | |
4080 (and ange-ftp-process-verbose | |
4081 (ange-ftp-message "Compressing %s...done" abbr)) | |
4082 (if (zerop (buffer-size)) | |
4083 (progn | |
4084 (let (ange-ftp-process-verbose) | |
4085 (delete-file file)) | |
4086 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2)))) | |
4087 (ange-ftp-del-tmp-name tmp1) | |
4088 (ange-ftp-del-tmp-name tmp2)))) | |
30459 | 4089 |
28210 | 4090 ;; Copy FILE to this machine, uncompress it, and copy out to NFILE. |
4091 (defun ange-ftp-uncompress (file nfile) | |
4092 (let* ((parsed (ange-ftp-ftp-name file)) | |
4093 (tmp1 (ange-ftp-make-tmp-name (car parsed))) | |
4094 (tmp2 (ange-ftp-make-tmp-name (car parsed))) | |
4095 (abbr (ange-ftp-abbreviate-filename file)) | |
4096 (nabbr (ange-ftp-abbreviate-filename nfile)) | |
4097 (msg1 (format "Getting %s" abbr)) | |
4098 (msg2 (format "Putting %s" nabbr)) | |
4099 ;; ;; Cheap hack because of problems with binary file transfers from | |
4100 ;; ;; VMS hosts. | |
4101 ;; (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed))))) | |
4102 ) | |
4103 (unwind-protect | |
4104 (progn | |
4105 (ange-ftp-copy-file-internal file tmp1 t nil msg1) | |
4106 (and ange-ftp-process-verbose | |
4107 (ange-ftp-message "Uncompressing %s..." abbr)) | |
4108 (call-process-region (point) | |
4109 (point) | |
4110 shell-file-name | |
4111 nil | |
4112 t | |
4113 nil | |
4114 "-c" | |
4115 (format "uncompress -c < %s > %s" tmp1 tmp2)) | |
4116 (and ange-ftp-process-verbose | |
4117 (ange-ftp-message "Uncompressing %s...done" abbr)) | |
4118 (if (zerop (buffer-size)) | |
4119 (progn | |
4120 (let (ange-ftp-process-verbose) | |
4121 (delete-file file)) | |
4122 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2)))) | |
4123 (ange-ftp-del-tmp-name tmp1) | |
4124 (ange-ftp-del-tmp-name tmp2)))) | |
4125 | |
4126 (defun ange-ftp-find-backup-file-name (fn) | |
4127 ;; Either return the ordinary backup name, etc., | |
4128 ;; or return nil meaning don't make a backup. | |
4129 (if ange-ftp-make-backup-files | |
4130 (ange-ftp-real-find-backup-file-name fn))) | |
4131 | |
4132 ;;; Define the handler for special file names | |
4133 ;;; that causes ange-ftp to be invoked. | |
4134 | |
4135 ;;;###autoload | |
4136 (defun ange-ftp-hook-function (operation &rest args) | |
4137 (let ((fn (get operation 'ange-ftp))) | |
4138 (if fn (apply fn args) | |
4139 (ange-ftp-run-real-handler operation args)))) | |
4140 | |
4141 | |
4142 ;;; This regexp takes care of real ange-ftp file names (with a slash | |
4143 ;;; and colon). | |
4144 ;;; Don't allow the host name to end in a period--some systems use /.: | |
4145 ;;;###autoload | |
4146 (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist) | |
4147 (setq file-name-handler-alist | |
4148 (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function) | |
4149 file-name-handler-alist))) | |
4150 | |
4151 ;;; This regexp recognizes absolute filenames with only one component, | |
4152 ;;; for the sake of hostname completion. | |
4153 ;;;###autoload | |
4154 (or (assoc "^/[^/:]*\\'" file-name-handler-alist) | |
4155 (setq file-name-handler-alist | |
4156 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function) | |
4157 file-name-handler-alist))) | |
4158 | |
4159 ;;; This regexp recognizes absolute filenames with only one component | |
4160 ;;; on Windows, for the sake of hostname completion. | |
4161 ;;; NB. Do not mark this as autoload, because it is very common to | |
4162 ;;; do completions in the root directory of drives on Windows. | |
4163 (and (memq system-type '(ms-dos windows-nt)) | |
4164 (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist) | |
4165 (setq file-name-handler-alist | |
30459 | 4166 (cons '("^[a-zA-Z]:/[^/:]*\\'" . |
28210 | 4167 ange-ftp-completion-hook-function) |
4168 file-name-handler-alist)))) | |
4169 | |
4170 ;;; The above two forms are sufficient to cause this file to be loaded | |
4171 ;;; if the user ever uses a file name with a colon in it. | |
4172 | |
30459 | 4173 ;;; This sets the mode |
28210 | 4174 (or (memq 'ange-ftp-set-buffer-mode find-file-hooks) |
4175 (setq find-file-hooks | |
4176 (cons 'ange-ftp-set-buffer-mode find-file-hooks))) | |
4177 | |
4178 ;;; Now say where to find the handlers for particular operations. | |
4179 | |
4180 (put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory) | |
4181 (put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory) | |
4182 (put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory) | |
4183 (put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name) | |
4184 (put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name) | |
4185 (put 'make-directory 'ange-ftp 'ange-ftp-make-directory) | |
4186 (put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory) | |
4187 (put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents) | |
4188 (put 'directory-files 'ange-ftp 'ange-ftp-directory-files) | |
4189 (put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p) | |
4190 (put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p) | |
4191 (put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p) | |
4192 (put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p) | |
4193 (put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p) | |
4194 (put 'delete-file 'ange-ftp 'ange-ftp-delete-file) | |
4195 (put 'read-file-name-internal 'ange-ftp 'ange-ftp-read-file-name-internal) | |
4196 (put 'verify-visited-file-modtime 'ange-ftp | |
4197 'ange-ftp-verify-visited-file-modtime) | |
4198 (put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p) | |
4199 (put 'write-region 'ange-ftp 'ange-ftp-write-region) | |
4200 (put 'backup-buffer 'ange-ftp 'ange-ftp-backup-buffer) | |
4201 (put 'copy-file 'ange-ftp 'ange-ftp-copy-file) | |
4202 (put 'rename-file 'ange-ftp 'ange-ftp-rename-file) | |
4203 (put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes) | |
30481 | 4204 (put 'file-newer-than-file-p 'ange-ftp 'ange-ftp-file-newer-than-file-p) |
28210 | 4205 (put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions) |
4206 (put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion) | |
4207 (put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory) | |
4208 (put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy) | |
4209 (put 'unhandled-file-name-directory 'ange-ftp | |
4210 'ange-ftp-unhandled-file-name-directory) | |
4211 (put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions) | |
4212 (put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache) | |
4213 (put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file) | |
4214 (put 'load 'ange-ftp 'ange-ftp-load) | |
4215 (put 'find-backup-file-name 'ange-ftp 'ange-ftp-find-backup-file-name) | |
4216 | |
4217 ;; Turn off truename processing to save time. | |
4218 ;; Treat each name as its own truename. | |
4219 (put 'file-truename 'ange-ftp 'identity) | |
4220 | |
4221 ;; Turn off RCS/SCCS processing to save time. | |
4222 ;; This returns nil for any file name as argument. | |
4223 (put 'vc-registered 'ange-ftp 'null) | |
4224 | |
4225 (put 'dired-call-process 'ange-ftp 'ange-ftp-dired-call-process) | |
4226 (put 'shell-command 'ange-ftp 'ange-ftp-shell-command) | |
4227 | |
4228 ;;; Define ways of getting at unmodified Emacs primitives, | |
4229 ;;; turning off our handler. | |
4230 | |
4231 (defun ange-ftp-run-real-handler (operation args) | |
4232 (let ((inhibit-file-name-handlers | |
4233 (cons 'ange-ftp-hook-function | |
4234 (cons 'ange-ftp-completion-hook-function | |
4235 (and (eq inhibit-file-name-operation operation) | |
4236 inhibit-file-name-handlers)))) | |
4237 (inhibit-file-name-operation operation)) | |
4238 (apply operation args))) | |
4239 | |
4240 (defun ange-ftp-real-file-name-directory (&rest args) | |
4241 (ange-ftp-run-real-handler 'file-name-directory args)) | |
4242 (defun ange-ftp-real-file-name-nondirectory (&rest args) | |
4243 (ange-ftp-run-real-handler 'file-name-nondirectory args)) | |
4244 (defun ange-ftp-real-file-name-as-directory (&rest args) | |
4245 (ange-ftp-run-real-handler 'file-name-as-directory args)) | |
4246 (defun ange-ftp-real-directory-file-name (&rest args) | |
4247 (ange-ftp-run-real-handler 'directory-file-name args)) | |
4248 (defun ange-ftp-real-expand-file-name (&rest args) | |
4249 (ange-ftp-run-real-handler 'expand-file-name args)) | |
4250 (defun ange-ftp-real-make-directory (&rest args) | |
4251 (ange-ftp-run-real-handler 'make-directory args)) | |
4252 (defun ange-ftp-real-delete-directory (&rest args) | |
4253 (ange-ftp-run-real-handler 'delete-directory args)) | |
4254 (defun ange-ftp-real-insert-file-contents (&rest args) | |
4255 (ange-ftp-run-real-handler 'insert-file-contents args)) | |
4256 (defun ange-ftp-real-directory-files (&rest args) | |
4257 (ange-ftp-run-real-handler 'directory-files args)) | |
4258 (defun ange-ftp-real-file-directory-p (&rest args) | |
4259 (ange-ftp-run-real-handler 'file-directory-p args)) | |
4260 (defun ange-ftp-real-file-writable-p (&rest args) | |
4261 (ange-ftp-run-real-handler 'file-writable-p args)) | |
4262 (defun ange-ftp-real-file-readable-p (&rest args) | |
4263 (ange-ftp-run-real-handler 'file-readable-p args)) | |
4264 (defun ange-ftp-real-file-executable-p (&rest args) | |
4265 (ange-ftp-run-real-handler 'file-executable-p args)) | |
4266 (defun ange-ftp-real-file-symlink-p (&rest args) | |
4267 (ange-ftp-run-real-handler 'file-symlink-p args)) | |
4268 (defun ange-ftp-real-delete-file (&rest args) | |
4269 (ange-ftp-run-real-handler 'delete-file args)) | |
4270 (defun ange-ftp-real-read-file-name-internal (&rest args) | |
4271 (ange-ftp-run-real-handler 'read-file-name-internal args)) | |
4272 (defun ange-ftp-real-verify-visited-file-modtime (&rest args) | |
4273 (ange-ftp-run-real-handler 'verify-visited-file-modtime args)) | |
4274 (defun ange-ftp-real-file-exists-p (&rest args) | |
4275 (ange-ftp-run-real-handler 'file-exists-p args)) | |
4276 (defun ange-ftp-real-write-region (&rest args) | |
4277 (ange-ftp-run-real-handler 'write-region args)) | |
4278 (defun ange-ftp-real-backup-buffer (&rest args) | |
4279 (ange-ftp-run-real-handler 'backup-buffer args)) | |
4280 (defun ange-ftp-real-copy-file (&rest args) | |
4281 (ange-ftp-run-real-handler 'copy-file args)) | |
4282 (defun ange-ftp-real-rename-file (&rest args) | |
4283 (ange-ftp-run-real-handler 'rename-file args)) | |
4284 (defun ange-ftp-real-file-attributes (&rest args) | |
4285 (ange-ftp-run-real-handler 'file-attributes args)) | |
30481 | 4286 (defun ange-ftp-real-file-newer-than-file-p (&rest args) |
4287 (ange-ftp-run-real-handler 'file-newer-than-file-p args)) | |
28210 | 4288 (defun ange-ftp-real-file-name-all-completions (&rest args) |
4289 (ange-ftp-run-real-handler 'file-name-all-completions args)) | |
4290 (defun ange-ftp-real-file-name-completion (&rest args) | |
4291 (ange-ftp-run-real-handler 'file-name-completion args)) | |
4292 (defun ange-ftp-real-insert-directory (&rest args) | |
4293 (ange-ftp-run-real-handler 'insert-directory args)) | |
4294 (defun ange-ftp-real-file-name-sans-versions (&rest args) | |
4295 (ange-ftp-run-real-handler 'file-name-sans-versions args)) | |
4296 (defun ange-ftp-real-shell-command (&rest args) | |
4297 (ange-ftp-run-real-handler 'shell-command args)) | |
4298 (defun ange-ftp-real-load (&rest args) | |
4299 (ange-ftp-run-real-handler 'load args)) | |
4300 (defun ange-ftp-real-find-backup-file-name (&rest args) | |
4301 (ange-ftp-run-real-handler 'find-backup-file-name args)) | |
4302 | |
4303 ;; Here we support using dired on remote hosts. | |
4304 ;; I have turned off the support for using dired on foreign directory formats. | |
4305 ;; That involves too many unclean hooks. | |
30459 | 4306 ;; It would be cleaner to support such operations by |
28210 | 4307 ;; converting the foreign directory format to something dired can understand; |
4308 ;; something close to ls -l output. | |
4309 ;; The logical place to do this is in the functions ange-ftp-parse-...-listing. | |
4310 | |
4311 ;; Some of the old dired hooks would still be needed even if this is done. | |
4312 ;; I have preserved (and modernized) those hooks. | |
4313 ;; So the format conversion should be all that is needed. | |
4314 | |
4315 (defun ange-ftp-insert-directory (file switches &optional wildcard full) | |
4316 (let ((short (ange-ftp-abbreviate-filename file)) | |
4317 (parsed (ange-ftp-ftp-name (expand-file-name file)))) | |
4318 (if parsed | |
4319 (insert | |
4320 (if wildcard | |
4321 (let ((default-directory (file-name-directory file))) | |
4322 (ange-ftp-ls (file-name-nondirectory file) switches nil nil t)) | |
4323 (ange-ftp-ls file switches full))) | |
4324 (ange-ftp-real-insert-directory file switches wildcard full)))) | |
4325 | |
4326 (defun ange-ftp-dired-uncache (dir) | |
4327 (if (ange-ftp-ftp-name (expand-file-name dir)) | |
4328 (setq ange-ftp-ls-cache-file nil))) | |
4329 | |
4330 (defvar ange-ftp-sans-version-alist nil | |
4331 "Alist of mapping host type into function to remove file version numbers.") | |
4332 | |
4333 (defun ange-ftp-file-name-sans-versions (file keep-backup-version) | |
4334 (let* ((short (ange-ftp-abbreviate-filename file)) | |
4335 (parsed (ange-ftp-ftp-name short)) | |
4336 host-type func) | |
4337 (if parsed | |
4338 (setq host-type (ange-ftp-host-type (car parsed)) | |
4339 func (cdr (assq (ange-ftp-host-type (car parsed)) | |
4340 ange-ftp-sans-version-alist)))) | |
4341 (if func (funcall func file keep-backup-version) | |
4342 (ange-ftp-real-file-name-sans-versions file keep-backup-version)))) | |
4343 | |
4344 ;; This is the handler for shell-command. | |
4345 (defun ange-ftp-shell-command (command &optional output-buffer error-buffer) | |
4346 (let* ((parsed (ange-ftp-ftp-name default-directory)) | |
4347 (host (nth 0 parsed)) | |
4348 (user (nth 1 parsed)) | |
4349 (name (nth 2 parsed))) | |
4350 (if (not parsed) | |
4351 (ange-ftp-real-shell-command command output-buffer error-buffer) | |
4352 (if (> (length name) 0) ; else it's $HOME | |
4353 (setq command (concat "cd " name "; " command))) | |
4354 (setq command | |
4355 (format "%s %s \"%s\"" ; remsh -l USER does not work well | |
4356 ; on a hp-ux machine I tried | |
4357 remote-shell-program host command)) | |
4358 (ange-ftp-message "Remote command '%s' ..." command) | |
4359 ;; Cannot call ange-ftp-real-dired-run-shell-command here as it | |
4360 ;; would prepend "cd default-directory" --- which bombs because | |
4361 ;; default-directory is in ange-ftp syntax for remote file names. | |
4362 (ange-ftp-real-shell-command command output-buffer error-buffer)))) | |
4363 | |
4364 ;;; This is the handler for call-process. | |
4365 (defun ange-ftp-dired-call-process (program discard &rest arguments) | |
4366 ;; PROGRAM is always one of those below in the cond in dired.el. | |
4367 ;; The ARGUMENTS are (nearly) always files. | |
4368 (if (ange-ftp-ftp-name default-directory) | |
4369 ;; Can't use ange-ftp-dired-host-type here because the current | |
4370 ;; buffer is *dired-check-process output* | |
4371 (condition-case oops | |
4372 (cond ((equal dired-chmod-program program) | |
4373 (ange-ftp-call-chmod arguments)) | |
4374 ;; ((equal "chgrp" program)) | |
4375 ;; ((equal dired-chown-program program)) | |
4376 (t (error "Unknown remote command: %s" program))) | |
4377 (ftp-error (insert (format "%s: %s, %s\n" | |
4378 (nth 1 oops) | |
4379 (nth 2 oops) | |
4380 (nth 3 oops))) | |
4381 ;; Caller expects nonzero value to mean failure. | |
4382 1) | |
4383 (error (insert (format "%s\n" (nth 1 oops))) | |
4384 1)) | |
4385 (apply 'call-process program nil (not discard) nil arguments))) | |
4386 | |
30459 | 4387 (defvar ange-ftp-remote-shell "rsh" |
28210 | 4388 "Remote shell to use for chmod, if FTP server rejects the `chmod' command.") |
4389 | |
4390 ;; Handle an attempt to run chmod on a remote file | |
4391 ;; by using the ftp chmod command. | |
4392 (defun ange-ftp-call-chmod (args) | |
4393 (if (< (length args) 2) | |
4394 (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args)) | |
4395 (let ((mode (car args)) | |
4396 (rest (cdr args))) | |
4397 (if (equal "--" (car rest)) | |
4398 (setq rest (cdr rest))) | |
4399 (mapcar | |
30459 | 4400 (lambda (file) |
4401 (setq file (expand-file-name file)) | |
4402 (let ((parsed (ange-ftp-ftp-name file))) | |
4403 (if parsed | |
4404 (let* ((host (nth 0 parsed)) | |
4405 (user (nth 1 parsed)) | |
4406 (name (ange-ftp-quote-string (nth 2 parsed))) | |
4407 (abbr (ange-ftp-abbreviate-filename file)) | |
4408 (result (ange-ftp-send-cmd host user | |
4409 (list 'chmod mode name) | |
4410 (format "doing chmod %s" | |
4411 abbr)))) | |
4412 (or (car result) | |
4413 (call-process | |
4414 ange-ftp-remote-shell | |
4415 nil t nil host dired-chmod-program mode name)))))) | |
28210 | 4416 rest)) |
4417 (setq ange-ftp-ls-cache-file nil) ;Stop confusing Dired. | |
4418 0) | |
4419 | |
4420 ;;; This is turned off because it has nothing properly to do | |
4421 ;;; with dired. It could be reasonable to adapt this to | |
4422 ;;; replace ange-ftp-copy-file. | |
4423 | |
4424 ;;;;; ------------------------------------------------------------ | |
4425 ;;;;; Noddy support for async copy-file within dired. | |
4426 ;;;;; ------------------------------------------------------------ | |
4427 | |
4428 ;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait) | |
4429 ;; "Documented as original." | |
4430 ;; (dired-handle-overwrite to) | |
4431 ;; (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil | |
4432 ;; cont nowait)) | |
4433 | |
4434 ;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg | |
4435 ;; &optional marker-char op1 | |
4436 ;; how-to) | |
4437 ;; "Documented as original." | |
4438 ;; ;; we need to let ange-ftp-dired-create-files know that we indirectly | |
4439 ;; ;; called it rather than somebody else. | |
4440 ;; (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is | |
4441 ;; (ange-ftp-real-dired-do-create-files op-symbol file-creator operation | |
4442 ;; arg marker-char op1 how-to))) | |
4443 | |
4444 ;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor | |
4445 ;; &optional marker-char) | |
4446 ;; "Documented as original." | |
4447 ;; (if (and (boundp 'ange-ftp-dired-do-create-files) | |
4448 ;; ;; called from ange-ftp-dired-do-create-files? | |
4449 ;; ange-ftp-dired-do-create-files | |
4450 ;; ;; any files worth copying? | |
4451 ;; fn-list | |
4452 ;; ;; we only support async copy-file at the mo. | |
4453 ;; (eq file-creator 'dired-copy-file) | |
4454 ;; ;; it is only worth calling the alternative function for remote files | |
4455 ;; ;; as we tie ourself in recursive knots otherwise. | |
4456 ;; (or (ange-ftp-ftp-name (car fn-list)) | |
4457 ;; ;; we can only call the name constructor for dired-do-create-files | |
4458 ;; ;; since the one for regexps starts prompting here, there and | |
4459 ;; ;; everywhere. | |
4460 ;; (ange-ftp-ftp-name (funcall name-constructor (car fn-list))))) | |
4461 ;; ;; use the process-filter driven routine rather than the iterative one. | |
4462 ;; (ange-ftp-dcf-1 file-creator | |
4463 ;; operation | |
4464 ;; fn-list | |
4465 ;; name-constructor | |
4466 ;; (and (boundp 'target) target) ;dynamically bound | |
4467 ;; marker-char | |
4468 ;; (current-buffer) | |
4469 ;; nil ;overwrite-query | |
4470 ;; nil ;overwrite-backup-query | |
4471 ;; nil ;failures | |
4472 ;; nil ;skipped | |
4473 ;; 0 ;success-count | |
4474 ;; (length fn-list) ;total | |
4475 ;; ) | |
4476 ;; ;; normal case... use the interactive routine... much cheaper. | |
4477 ;; (ange-ftp-real-dired-create-files file-creator operation fn-list | |
4478 ;; name-constructor marker-char))) | |
4479 | |
4480 ;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor | |
30459 | 4481 ;; target marker-char buffer overwrite-query |
28210 | 4482 ;; overwrite-backup-query failures skipped |
4483 ;; success-count total) | |
4484 ;; (let ((old-buf (current-buffer))) | |
4485 ;; (unwind-protect | |
4486 ;; (progn | |
4487 ;; (set-buffer buffer) | |
4488 ;; (if (null fn-list) | |
4489 ;; (ange-ftp-dcf-3 failures operation total skipped | |
4490 ;; success-count buffer) | |
30459 | 4491 |
28210 | 4492 ;; (let* ((from (car fn-list)) |
4493 ;; (to (funcall name-constructor from))) | |
4494 ;; (if (equal to from) | |
4495 ;; (progn | |
4496 ;; (setq to nil) | |
4497 ;; (dired-log "Cannot %s to same file: %s\n" | |
4498 ;; (downcase operation) from))) | |
4499 ;; (if (not to) | |
4500 ;; (ange-ftp-dcf-1 file-creator | |
4501 ;; operation | |
4502 ;; (cdr fn-list) | |
4503 ;; name-constructor | |
4504 ;; target | |
4505 ;; marker-char | |
4506 ;; buffer | |
4507 ;; overwrite-query | |
4508 ;; overwrite-backup-query | |
4509 ;; failures | |
4510 ;; (cons (dired-make-relative from) skipped) | |
4511 ;; success-count | |
4512 ;; total) | |
4513 ;; (let* ((overwrite (file-exists-p to)) | |
4514 ;; (overwrite-confirmed ; for dired-handle-overwrite | |
4515 ;; (and overwrite | |
4516 ;; (let ((help-form '(format "\ | |
4517 ;;Type SPC or `y' to overwrite file `%s', | |
4518 ;;DEL or `n' to skip to next, | |
4519 ;;ESC or `q' to not overwrite any of the remaining files, | |
4520 ;;`!' to overwrite all remaining files with no more questions." to))) | |
4521 ;; (dired-query 'overwrite-query | |
4522 ;; "Overwrite `%s'?" to)))) | |
4523 ;; ;; must determine if FROM is marked before file-creator | |
4524 ;; ;; gets a chance to delete it (in case of a move). | |
4525 ;; (actual-marker-char | |
4526 ;; (cond ((integerp marker-char) marker-char) | |
4527 ;; (marker-char (dired-file-marker from)) ; slow | |
4528 ;; (t nil)))) | |
4529 ;; (condition-case err | |
4530 ;; (funcall file-creator from to overwrite-confirmed | |
4531 ;; (list (function ange-ftp-dcf-2) | |
4532 ;; nil ;err | |
4533 ;; file-creator operation fn-list | |
4534 ;; name-constructor | |
4535 ;; target | |
4536 ;; marker-char actual-marker-char | |
4537 ;; buffer to from | |
4538 ;; overwrite | |
4539 ;; overwrite-confirmed | |
30459 | 4540 ;; overwrite-query |
28210 | 4541 ;; overwrite-backup-query |
4542 ;; failures skipped success-count | |
4543 ;; total) | |
4544 ;; t) | |
4545 ;; (file-error ; FILE-CREATOR aborted | |
4546 ;; (ange-ftp-dcf-2 nil ;result | |
4547 ;; nil ;line | |
4548 ;; err | |
4549 ;; file-creator operation fn-list | |
4550 ;; name-constructor | |
4551 ;; target | |
4552 ;; marker-char actual-marker-char | |
4553 ;; buffer to from | |
4554 ;; overwrite | |
4555 ;; overwrite-confirmed | |
30459 | 4556 ;; overwrite-query |
28210 | 4557 ;; overwrite-backup-query |
4558 ;; failures skipped success-count | |
4559 ;; total)))))))) | |
4560 ;; (set-buffer old-buf)))) | |
4561 | |
4562 ;;(defun ange-ftp-dcf-2 (result line err | |
4563 ;; file-creator operation fn-list | |
4564 ;; name-constructor | |
4565 ;; target | |
4566 ;; marker-char actual-marker-char | |
4567 ;; buffer to from | |
4568 ;; overwrite | |
4569 ;; overwrite-confirmed | |
30459 | 4570 ;; overwrite-query |
28210 | 4571 ;; overwrite-backup-query |
4572 ;; failures skipped success-count | |
4573 ;; total) | |
4574 ;; (let ((old-buf (current-buffer))) | |
4575 ;; (unwind-protect | |
4576 ;; (progn | |
4577 ;; (set-buffer buffer) | |
4578 ;; (if (or err (not result)) | |
4579 ;; (progn | |
4580 ;; (setq failures (cons (dired-make-relative from) failures)) | |
4581 ;; (dired-log "%s `%s' to `%s' failed:\n%s\n" | |
4582 ;; operation from to (or err line))) | |
4583 ;; (if overwrite | |
4584 ;; ;; If we get here, file-creator hasn't been aborted | |
4585 ;; ;; and the old entry (if any) has to be deleted | |
4586 ;; ;; before adding the new entry. | |
4587 ;; (dired-remove-file to)) | |
4588 ;; (setq success-count (1+ success-count)) | |
4589 ;; (message "%s: %d of %d" operation success-count total) | |
4590 ;; (dired-add-file to actual-marker-char)) | |
30459 | 4591 |
28210 | 4592 ;; (ange-ftp-dcf-1 file-creator operation (cdr fn-list) |
4593 ;; name-constructor | |
4594 ;; target | |
4595 ;; marker-char | |
4596 ;; buffer | |
30459 | 4597 ;; overwrite-query |
28210 | 4598 ;; overwrite-backup-query |
4599 ;; failures skipped success-count | |
4600 ;; total)) | |
4601 ;; (set-buffer old-buf)))) | |
4602 | |
4603 ;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count | |
4604 ;; buffer) | |
4605 ;; (let ((old-buf (current-buffer))) | |
4606 ;; (unwind-protect | |
4607 ;; (progn | |
4608 ;; (set-buffer buffer) | |
4609 ;; (cond | |
4610 ;; (failures | |
4611 ;; (dired-log-summary | |
4612 ;; (message "%s failed for %d of %d file%s %s" | |
4613 ;; operation (length failures) total | |
4614 ;; (dired-plural-s total) failures))) | |
4615 ;; (skipped | |
4616 ;; (dired-log-summary | |
4617 ;; (message "%s: %d of %d file%s skipped %s" | |
4618 ;; operation (length skipped) total | |
4619 ;; (dired-plural-s total) skipped))) | |
4620 ;; (t | |
4621 ;; (message "%s: %s file%s." | |
4622 ;; operation success-count (dired-plural-s success-count)))) | |
4623 ;; (dired-move-to-filename)) | |
4624 ;; (set-buffer old-buf)))) | |
4625 | |
4626 ;;;; ----------------------------------------------- | |
4627 ;;;; Unix Descriptive Listing (dl) Support | |
4628 ;;;; ----------------------------------------------- | |
4629 | |
4630 ;; This is turned off because nothing uses it currently | |
4631 ;; and because I don't understand what it's supposed to be for. --rms. | |
4632 | |
4633 ;;(defconst ange-ftp-dired-dl-re-dir | |
4634 ;; "^. [^ /]+/[ \n]" | |
4635 ;; "Regular expression to use to search for dl directories.") | |
4636 | |
4637 ;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist) | |
4638 ;; (setq ange-ftp-dired-re-dir-alist | |
4639 ;; (cons (cons 'unix:dl ange-ftp-dired-dl-re-dir) | |
4640 ;; ange-ftp-dired-re-dir-alist))) | |
4641 | |
4642 ;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol) | |
4643 ;; "In dired, move to the first character of the filename on this line." | |
4644 ;; ;; This is the Unix dl version. | |
4645 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
4646 ;; (let (case-fold-search) | |
4647 ;; (beginning-of-line) | |
4648 ;; (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ") | |
4649 ;; (goto-char (+ (point) 2)) | |
4650 ;; (if raise-error | |
4651 ;; (error "No file on this line") | |
4652 ;; nil)))) | |
4653 | |
4654 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist) | |
4655 ;; (setq ange-ftp-dired-move-to-filename-alist | |
4656 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename) | |
4657 ;; ange-ftp-dired-move-to-filename-alist))) | |
4658 | |
4659 ;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol) | |
4660 ;; ;; Assumes point is at beginning of filename. | |
4661 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
4662 ;; ;; On failure, signals an error or returns nil. | |
4663 ;; ;; This is the Unix dl version. | |
4664 ;; (let ((opoint (point)) | |
4665 ;; case-fold-search hidden) | |
4666 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
4667 ;; (setq hidden (and selective-display | |
4668 ;; (save-excursion | |
4669 ;; (search-forward "\r" eol t)))) | |
4670 ;; (if hidden | |
4671 ;; (if no-error | |
4672 ;; nil | |
4673 ;; (error | |
4674 ;; (substitute-command-keys | |
4675 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide"))) | |
4676 ;; (skip-chars-forward "^ /" eol) | |
4677 ;; (if (eq opoint (point)) | |
4678 ;; (if no-error | |
4679 ;; nil | |
4680 ;; (error "No file on this line")) | |
4681 ;; (point))))) | |
4682 | |
4683 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist) | |
4684 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
4685 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename) | |
4686 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
4687 | |
4688 ;;;; ------------------------------------------------------------ | |
4689 ;;;; VOS support (VOS support is probably broken, | |
4690 ;;;; but I don't know anything about VOS.) | |
4691 ;;;; ------------------------------------------------------------ | |
4692 ; | |
4693 ;(defun ange-ftp-fix-name-for-vos (name &optional reverse) | |
4694 ; (setq name (copy-sequence name)) | |
4695 ; (let ((from (if reverse ?\> ?\/)) | |
4696 ; (to (if reverse ?\/ ?\>)) | |
4697 ; (i (1- (length name)))) | |
4698 ; (while (>= i 0) | |
4699 ; (if (= (aref name i) from) | |
4700 ; (aset name i to)) | |
4701 ; (setq i (1- i))) | |
4702 ; name)) | |
4703 ; | |
4704 ;(or (assq 'vos ange-ftp-fix-name-func-alist) | |
4705 ; (setq ange-ftp-fix-name-func-alist | |
4706 ; (cons '(vos . ange-ftp-fix-name-for-vos) | |
4707 ; ange-ftp-fix-name-func-alist))) | |
4708 ; | |
4709 ;(or (memq 'vos ange-ftp-dumb-host-types) | |
4710 ; (setq ange-ftp-dumb-host-types | |
4711 ; (cons 'vos ange-ftp-dumb-host-types))) | |
4712 ; | |
4713 ;(defun ange-ftp-fix-dir-name-for-vos (dir-name) | |
4714 ; (ange-ftp-fix-name-for-vos | |
4715 ; (concat dir-name | |
4716 ; (if (eq ?/ (aref dir-name (1- (length dir-name)))) | |
4717 ; "" "/") | |
4718 ; "*"))) | |
4719 ; | |
4720 ;(or (assq 'vos ange-ftp-fix-dir-name-func-alist) | |
4721 ; (setq ange-ftp-fix-dir-name-func-alist | |
4722 ; (cons '(vos . ange-ftp-fix-dir-name-for-vos) | |
4723 ; ange-ftp-fix-dir-name-func-alist))) | |
4724 ; | |
4725 ;(defvar ange-ftp-vos-host-regexp nil | |
4726 ; "If a host matches this regexp then it is assumed to be running VOS.") | |
4727 ; | |
4728 ;(defun ange-ftp-vos-host (host) | |
4729 ; (and ange-ftp-vos-host-regexp | |
4730 ; (save-match-data | |
4731 ; (string-match ange-ftp-vos-host-regexp host)))) | |
4732 ; | |
4733 ;(defun ange-ftp-parse-vos-listing () | |
4734 ; "Parse the current buffer which is assumed to be in VOS list -all | |
4735 ;format, and return a hashtable as the result." | |
4736 ; (let ((tbl (ange-ftp-make-hashtable)) | |
4737 ; (type-list | |
4738 ; '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40) | |
4739 ; ("^Dirs: [0-9]+\n+" t 30))) | |
4740 ; type-regexp type-is-dir type-col file) | |
4741 ; (goto-char (point-min)) | |
4742 ; (save-match-data | |
4743 ; (while type-list | |
4744 ; (setq type-regexp (car (car type-list)) | |
4745 ; type-is-dir (nth 1 (car type-list)) | |
4746 ; type-col (nth 2 (car type-list)) | |
4747 ; type-list (cdr type-list)) | |
4748 ; (if (re-search-forward type-regexp nil t) | |
4749 ; (while (eq (char-after (point)) ? ) | |
4750 ; (move-to-column type-col) | |
4751 ; (setq file (buffer-substring (point) | |
4752 ; (progn | |
4753 ; (end-of-line 1) | |
4754 ; (point)))) | |
4755 ; (ange-ftp-put-hash-entry file type-is-dir tbl) | |
4756 ; (forward-line 1)))) | |
4757 ; (ange-ftp-put-hash-entry "." 'vosdir tbl) | |
4758 ; (ange-ftp-put-hash-entry ".." 'vosdir tbl)) | |
4759 ; tbl)) | |
4760 ; | |
4761 ;(or (assq 'vos ange-ftp-parse-list-func-alist) | |
4762 ; (setq ange-ftp-parse-list-func-alist | |
4763 ; (cons '(vos . ange-ftp-parse-vos-listing) | |
4764 ; ange-ftp-parse-list-func-alist))) | |
4765 | |
4766 ;;;; ------------------------------------------------------------ | |
4767 ;;;; VMS support. | |
4768 ;;;; ------------------------------------------------------------ | |
4769 | |
4770 ;; Convert NAME from UNIX-ish to VMS. If REVERSE given then convert from VMS | |
4771 ;; to UNIX-ish. | |
4772 (defun ange-ftp-fix-name-for-vms (name &optional reverse) | |
4773 (save-match-data | |
4774 (if reverse | |
4775 (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name) | |
4776 (let (drive dir file) | |
4777 (if (match-beginning 1) | |
4778 (setq drive (substring name | |
4779 (match-beginning 1) | |
4780 (match-end 1)))) | |
4781 (if (match-beginning 2) | |
4782 (setq dir | |
4783 (substring name (match-beginning 2) (match-end 2)))) | |
4784 (if (match-beginning 3) | |
4785 (setq file | |
4786 (substring name (match-beginning 3) (match-end 3)))) | |
4787 (and dir | |
30481 | 4788 (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
|
4789 ?/ ?. (substring dir 1 -1) t))) |
28210 | 4790 (concat (and drive |
4791 (concat "/" drive "/")) | |
4792 dir (and dir "/") | |
4793 file)) | |
4794 (error "name %s didn't match" name)) | |
4795 (let (drive dir file tmp) | |
4796 (if (string-match "^/[^:]+:/" name) | |
4797 (setq drive (substring name 1 | |
4798 (1- (match-end 0))) | |
4799 name (substring name (match-end 0)))) | |
4800 (setq tmp (file-name-directory name)) | |
4801 (if tmp | |
37813
989feabdf50e
(ange-ftp-fix-name-for-vms): Reverse args of
Gerd Moellmann <gerd@gnu.org>
parents:
37774
diff
changeset
|
4802 (setq dir (subst-char-in-string ?/ ?. (substring tmp 0 -1) t))) |
28210 | 4803 (setq file (file-name-nondirectory name)) |
4804 (concat drive | |
4805 (and dir (concat "[" (if drive nil ".") dir "]")) | |
4806 file))))) | |
4807 | |
4808 ;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1") | |
4809 ;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t) | |
4810 | |
4811 (or (assq 'vms ange-ftp-fix-name-func-alist) | |
4812 (setq ange-ftp-fix-name-func-alist | |
4813 (cons '(vms . ange-ftp-fix-name-for-vms) | |
4814 ange-ftp-fix-name-func-alist))) | |
4815 | |
4816 (or (memq 'vms ange-ftp-dumb-host-types) | |
4817 (setq ange-ftp-dumb-host-types | |
4818 (cons 'vms ange-ftp-dumb-host-types))) | |
4819 | |
4820 ;; It is important that this function barf for directories for which we know | |
4821 ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/". | |
4822 ;; This is because it saves an unnecessary FTP error, or possibly the listing | |
4823 ;; might succeed, but give erroneous info. This last case is particularly | |
4824 ;; likely for OS's (like MTS) for which we need to use a wildcard in order | |
4825 ;; to list a directory. | |
4826 | |
4827 ;; Convert name from UNIX-ish to VMS ready for a DIRectory listing. | |
4828 (defun ange-ftp-fix-dir-name-for-vms (dir-name) | |
4829 ;; Should there be entries for .. -> [-] and . -> [] below. Don't | |
4830 ;; think so, because expand-filename should have already short-circuited | |
4831 ;; them. | |
4832 (cond ((string-equal dir-name "/") | |
4833 (error "Cannot get listing for fictitious \"/\" directory.")) | |
4834 ((string-match "^/[-A-Z0-9_$]+:/$" dir-name) | |
4835 (error "Cannot get listing for device.")) | |
4836 ((ange-ftp-fix-name-for-vms dir-name)))) | |
30459 | 4837 |
28210 | 4838 (or (assq 'vms ange-ftp-fix-dir-name-func-alist) |
4839 (setq ange-ftp-fix-dir-name-func-alist | |
4840 (cons '(vms . ange-ftp-fix-dir-name-for-vms) | |
4841 ange-ftp-fix-dir-name-func-alist))) | |
4842 | |
4843 (defvar ange-ftp-vms-host-regexp nil) | |
4844 | |
4845 ;; Return non-nil if HOST is running VMS. | |
4846 (defun ange-ftp-vms-host (host) | |
4847 (and ange-ftp-vms-host-regexp | |
4848 (save-match-data | |
4849 (string-match ange-ftp-vms-host-regexp host)))) | |
4850 | |
4851 ;; Because some VMS ftp servers convert filenames to lower case | |
4852 ;; we allow a-z in the filename regexp. I'm not too happy about this. | |
4853 | |
4854 (defconst ange-ftp-vms-filename-regexp | |
4855 (concat | |
4856 "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\." | |
4857 "[-_A-Za-z0-9$]*;+[0-9]*\\)") | |
4858 "Regular expression to match for a valid VMS file name in Dired buffer. | |
4859 Stupid freaking bug! Position of _ and $ shouldn't matter but they do. | |
4860 Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX | |
4861 Other orders of $ and _ seem to all work just fine.") | |
4862 | |
4863 ;; These parsing functions are as general as possible because the syntax | |
4864 ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that | |
4865 ;; the VMS filename syntax is so rigid. If they bomb on a listing in the | |
4866 ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing | |
4867 ;; from vms.weird.net, then too bad. | |
4868 | |
4869 ;; Extract the next filename from a VMS dired-like listing. | |
4870 (defun ange-ftp-parse-vms-filename () | |
4871 (if (re-search-forward | |
4872 ange-ftp-vms-filename-regexp | |
4873 nil t) | |
4874 (buffer-substring (match-beginning 0) (match-end 0)))) | |
4875 | |
4876 ;; Parse the current buffer which is assumed to be in MultiNet FTP dir | |
4877 ;; format, and return a hashtable as the result. | |
4878 (defun ange-ftp-parse-vms-listing () | |
4879 (let ((tbl (ange-ftp-make-hashtable)) | |
4880 file) | |
4881 (goto-char (point-min)) | |
4882 (save-match-data | |
4883 (while (setq file (ange-ftp-parse-vms-filename)) | |
4884 (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file) | |
4885 ;; deal with directories | |
4886 (ange-ftp-put-hash-entry | |
4887 (substring file 0 (match-beginning 0)) t tbl) | |
4888 (ange-ftp-put-hash-entry file nil tbl) | |
4889 (if (string-match ";[0-9]+$" file) ; deal with extension | |
4890 ;; sans extension | |
4891 (ange-ftp-put-hash-entry | |
4892 (substring file 0 (match-beginning 0)) nil tbl))) | |
4893 (forward-line 1)) | |
4894 ;; Would like to look for a "Total" line, or a "Directory" line to | |
4895 ;; make sure that the listing isn't complete garbage before putting | |
4896 ;; in "." and "..", but we can't even count on all VAX's giving us | |
4897 ;; either of these. | |
4898 (ange-ftp-put-hash-entry "." t tbl) | |
4899 (ange-ftp-put-hash-entry ".." t tbl)) | |
4900 tbl)) | |
4901 | |
4902 (or (assq 'vms ange-ftp-parse-list-func-alist) | |
4903 (setq ange-ftp-parse-list-func-alist | |
4904 (cons '(vms . ange-ftp-parse-vms-listing) | |
4905 ange-ftp-parse-list-func-alist))) | |
4906 | |
4907 ;; This version only deletes file entries which have | |
4908 ;; explicit version numbers, because that is all VMS allows. | |
4909 | |
4910 ;; Can the following two functions be speeded up using file | |
4911 ;; completion functions? | |
4912 | |
4913 (defun ange-ftp-vms-delete-file-entry (name &optional dir-p) | |
4914 (if dir-p | |
4915 (ange-ftp-internal-delete-file-entry name t) | |
4916 (save-match-data | |
4917 (let ((file (ange-ftp-get-file-part name))) | |
4918 (if (string-match ";[0-9]+$" file) | |
30459 | 4919 ;; In VMS you can't delete a file without an explicit |
28210 | 4920 ;; version number, or wild-card (e.g. FOO;*) |
4921 ;; For now, we give up on wildcards. | |
4922 (let ((files (ange-ftp-get-hash-entry | |
4923 (file-name-directory name) | |
4924 ange-ftp-files-hashtable))) | |
4925 (if files | |
4926 (let* ((root (substring file 0 | |
4927 (match-beginning 0))) | |
4928 (regexp (concat "^" | |
4929 (regexp-quote root) | |
4930 ";[0-9]+$")) | |
4931 versions) | |
4932 (ange-ftp-del-hash-entry file files) | |
4933 ;; Now we need to check if there are any | |
4934 ;; versions left. If not, then delete the | |
4935 ;; root entry. | |
4936 (mapatoms | |
29587
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4937 (lambda (sym) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4938 (and (string-match regexp (get sym 'key)) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4939 (setq versions t))) |
28210 | 4940 files) |
4941 (or versions | |
4942 (ange-ftp-del-hash-entry root files)))))))))) | |
4943 | |
4944 (or (assq 'vms ange-ftp-delete-file-entry-alist) | |
4945 (setq ange-ftp-delete-file-entry-alist | |
4946 (cons '(vms . ange-ftp-vms-delete-file-entry) | |
4947 ange-ftp-delete-file-entry-alist))) | |
4948 | |
4949 (defun ange-ftp-vms-add-file-entry (name &optional dir-p) | |
4950 (if dir-p | |
4951 (ange-ftp-internal-add-file-entry name t) | |
4952 (let ((files (ange-ftp-get-hash-entry | |
4953 (file-name-directory name) | |
4954 ange-ftp-files-hashtable))) | |
4955 (if files | |
4956 (let ((file (ange-ftp-get-file-part name))) | |
4957 (save-match-data | |
4958 (if (string-match ";[0-9]+$" file) | |
4959 (ange-ftp-put-hash-entry | |
4960 (substring file 0 (match-beginning 0)) | |
4961 nil files) | |
4962 ;; Need to figure out what version of the file | |
4963 ;; is being added. | |
4964 (let ((regexp (concat "^" | |
4965 (regexp-quote file) | |
4966 ";\\([0-9]+\\)$")) | |
4967 (version 0)) | |
4968 (mapatoms | |
29587
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4969 (lambda (sym) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4970 (let ((name (get sym 'key))) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4971 (and (string-match regexp name) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4972 (setq version |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4973 (max version |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4974 (string-to-int |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4975 (substring name |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4976 (match-beginning 1) |
bcdfb27c5ea4
(ange-ftp-vms-delete-file-entry)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28892
diff
changeset
|
4977 (match-end 1)))))))) |
28210 | 4978 files) |
4979 (setq version (1+ version)) | |
4980 (ange-ftp-put-hash-entry | |
4981 (concat file ";" (int-to-string version)) | |
4982 nil files)))) | |
4983 (ange-ftp-put-hash-entry file nil files)))))) | |
4984 | |
4985 (or (assq 'vms ange-ftp-add-file-entry-alist) | |
4986 (setq ange-ftp-add-file-entry-alist | |
4987 (cons '(vms . ange-ftp-vms-add-file-entry) | |
4988 ange-ftp-add-file-entry-alist))) | |
4989 | |
4990 | |
4991 (defun ange-ftp-add-vms-host (host) | |
4992 "Mark HOST as the name of a machine running VMS." | |
4993 (interactive | |
4994 (list (read-string "Host: " | |
4995 (let ((name (or (buffer-file-name) default-directory))) | |
4996 (and name (car (ange-ftp-ftp-name name))))))) | |
4997 (if (not (ange-ftp-vms-host host)) | |
4998 (setq ange-ftp-vms-host-regexp | |
4999 (concat "^" (regexp-quote host) "$" | |
5000 (and ange-ftp-vms-host-regexp "\\|") | |
5001 ange-ftp-vms-host-regexp) | |
5002 ange-ftp-host-cache nil))) | |
5003 | |
5004 | |
5005 (defun ange-ftp-vms-file-name-as-directory (name) | |
5006 (save-match-data | |
5007 (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name) | |
5008 (setq name (substring name 0 (match-beginning 0)))) | |
5009 (ange-ftp-real-file-name-as-directory name))) | |
5010 | |
5011 (or (assq 'vms ange-ftp-file-name-as-directory-alist) | |
5012 (setq ange-ftp-file-name-as-directory-alist | |
5013 (cons '(vms . ange-ftp-vms-file-name-as-directory) | |
5014 ange-ftp-file-name-as-directory-alist))) | |
5015 | |
5016 ;;; Tree dired support: | |
5017 | |
5018 ;; For this code I have borrowed liberally from Sebastian Kremer's | |
5019 ;; dired-vms.el | |
5020 | |
5021 | |
5022 ;;;; These regexps must be anchored to beginning of line. | |
5023 ;;;; Beware that the ftpd may put the device in front of the filename. | |
5024 | |
5025 ;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]" | |
5026 ;; "Regular expression to use to search for VMS executable files.") | |
5027 | |
5028 ;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]" | |
5029 ;; "Regular expression to use to search for VMS directories.") | |
5030 | |
5031 ;;(or (assq 'vms ange-ftp-dired-re-exe-alist) | |
5032 ;; (setq ange-ftp-dired-re-exe-alist | |
5033 ;; (cons (cons 'vms ange-ftp-dired-vms-re-exe) | |
5034 ;; ange-ftp-dired-re-exe-alist))) | |
5035 | |
5036 ;;(or (assq 'vms ange-ftp-dired-re-dir-alist) | |
5037 ;; (setq ange-ftp-dired-re-dir-alist | |
5038 ;; (cons (cons 'vms ange-ftp-dired-vms-re-dir) | |
5039 ;; ange-ftp-dired-re-dir-alist))) | |
5040 | |
5041 ;;(defun ange-ftp-dired-vms-insert-headerline (dir) | |
5042 ;; ;; VMS inserts a headerline. I would prefer the headerline | |
5043 ;; ;; to be in ange-ftp format. This version tries to | |
5044 ;; ;; be careful, because we can't count on a headerline | |
5045 ;; ;; over ftp, and we wouldn't want to delete anything | |
5046 ;; ;; important. | |
5047 ;; (save-excursion | |
5048 ;; (if (looking-at "^ wildcard ") | |
5049 ;; (forward-line 1)) | |
5050 ;; (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n") | |
5051 ;; (delete-region (point) (match-end 0)))) | |
5052 ;; (ange-ftp-real-dired-insert-headerline dir)) | |
5053 | |
5054 ;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist) | |
5055 ;; (setq ange-ftp-dired-insert-headerline-alist | |
5056 ;; (cons '(vms . ange-ftp-dired-vms-insert-headerline) | |
5057 ;; ange-ftp-dired-insert-headerline-alist))) | |
5058 | |
5059 ;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol) | |
5060 ;; "In dired, move to first char of filename on this line. | |
5061 ;;Returns position (point) or nil if no filename on this line." | |
5062 ;; ;; This is the VMS version. | |
5063 ;; (let (case-fold-search) | |
5064 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5065 ;; (beginning-of-line) | |
5066 ;; (if (re-search-forward ange-ftp-vms-filename-regexp eol t) | |
5067 ;; (goto-char (match-beginning 1)) | |
5068 ;; (if raise-error | |
5069 ;; (error "No file on this line") | |
5070 ;; nil)))) | |
5071 | |
5072 ;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist) | |
5073 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5074 ;; (cons '(vms . ange-ftp-dired-vms-move-to-filename) | |
5075 ;; ange-ftp-dired-move-to-filename-alist))) | |
5076 | |
5077 ;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol) | |
5078 ;; ;; Assumes point is at beginning of filename. | |
5079 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5080 ;; ;; case-fold-search must be nil, at least for VMS. | |
5081 ;; ;; On failure, signals an error or returns nil. | |
5082 ;; ;; This is the VMS version. | |
5083 ;; (let (opoint hidden case-fold-search) | |
5084 ;; (setq opoint (point)) | |
5085 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
5086 ;; (setq hidden (and selective-display | |
5087 ;; (save-excursion (search-forward "\r" eol t)))) | |
5088 ;; (if hidden | |
5089 ;; nil | |
5090 ;; (re-search-forward ange-ftp-vms-filename-regexp eol t)) | |
5091 ;; (or no-error | |
5092 ;; (not (eq opoint (point))) | |
5093 ;; (error | |
5094 ;; (if hidden | |
5095 ;; (substitute-command-keys | |
5096 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide") | |
5097 ;; "No file on this line"))) | |
5098 ;; (if (eq opoint (point)) | |
5099 ;; nil | |
5100 ;; (point)))) | |
5101 | |
5102 ;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist) | |
5103 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5104 ;; (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename) | |
5105 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5106 | |
5107 ;;(defun ange-ftp-dired-vms-between-files () | |
5108 ;; (save-excursion | |
5109 ;; (beginning-of-line) | |
5110 ;; (or (equal (following-char) 10) ; newline | |
5111 ;; (equal (following-char) 9) ; tab | |
5112 ;; (progn (forward-char 2) | |
5113 ;; (or (looking-at "Total of") | |
5114 ;; (equal (following-char) 32)))))) | |
5115 | |
5116 ;;(or (assq 'vms ange-ftp-dired-between-files-alist) | |
5117 ;; (setq ange-ftp-dired-between-files-alist | |
5118 ;; (cons '(vms . ange-ftp-dired-vms-between-files) | |
5119 ;; ange-ftp-dired-between-files-alist))) | |
5120 | |
5121 ;; Beware! In VMS filenames must be of the form "FILE.TYPE". | |
5122 ;; Therefore, we cannot just append a ".Z" to filenames for | |
5123 ;; compressed files. Instead, we turn "FILE.TYPE" into | |
5124 ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do. | |
5125 | |
5126 (defun ange-ftp-vms-make-compressed-filename (name &optional reverse) | |
5127 (cond | |
5128 ((string-match "-Z;[0-9]+$" name) | |
5129 (list nil (substring name 0 (match-beginning 0)))) | |
5130 ((string-match ";[0-9]+$" name) | |
5131 (list nil (substring name 0 (match-beginning 0)))) | |
5132 ((string-match "-Z$" name) | |
5133 (list nil (substring name 0 -2))) | |
5134 (t | |
5135 (list t | |
5136 (if (string-match ";[0-9]+$" name) | |
5137 (concat (substring name 0 (match-beginning 0)) | |
5138 "-Z") | |
5139 (concat name "-Z")))))) | |
5140 | |
5141 (or (assq 'vms ange-ftp-make-compressed-filename-alist) | |
5142 (setq ange-ftp-make-compressed-filename-alist | |
5143 (cons '(vms . ange-ftp-vms-make-compressed-filename) | |
5144 ange-ftp-make-compressed-filename-alist))) | |
5145 | |
5146 ;;;; When the filename is too long, VMS will use two lines to list a file | |
5147 ;;;; (damn them!) This will confuse dired. To solve this, need to convince | |
5148 ;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of | |
5149 ;;;; (forward-line 1). This would require a number of changes to dired.el. | |
5150 ;;;; If dired gets confused, revert-buffer will fix it. | |
5151 | |
5152 ;;(defun ange-ftp-dired-vms-ls-trim () | |
5153 ;; (goto-char (point-min)) | |
5154 ;; (let ((case-fold-search nil)) | |
5155 ;; (re-search-forward ange-ftp-vms-filename-regexp)) | |
5156 ;; (beginning-of-line) | |
5157 ;; (delete-region (point-min) (point)) | |
5158 ;; (forward-line 1) | |
5159 ;; (delete-region (point) (point-max))) | |
5160 | |
5161 | |
5162 ;;(or (assq 'vms ange-ftp-dired-ls-trim-alist) | |
5163 ;; (setq ange-ftp-dired-ls-trim-alist | |
5164 ;; (cons '(vms . ange-ftp-dired-vms-ls-trim) | |
30459 | 5165 ;; ange-ftp-dired-ls-trim-alist))) |
28210 | 5166 |
5167 (defun ange-ftp-vms-sans-version (name &rest args) | |
5168 (save-match-data | |
5169 (if (string-match ";[0-9]+$" name) | |
5170 (substring name 0 (match-beginning 0)) | |
5171 name))) | |
5172 | |
5173 (or (assq 'vms ange-ftp-sans-version-alist) | |
5174 (setq ange-ftp-sans-version-alist | |
5175 (cons '(vms . ange-ftp-vms-sans-version) | |
5176 ange-ftp-sans-version-alist))) | |
5177 | |
5178 ;;(defvar ange-ftp-file-version-alist) | |
5179 | |
5180 ;;;;; The vms version of clean-directory has 2 more optional args | |
5181 ;;;;; than the usual dired version. This is so that it can be used by | |
5182 ;;;;; ange-ftp-dired-vms-flag-backup-files. | |
5183 | |
5184 ;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg) | |
5185 ;; "Flag numerical backups for deletion. | |
5186 ;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest. | |
5187 ;;Positive prefix arg KEEP overrides `dired-kept-versions'; | |
5188 ;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive. | |
5189 | |
5190 ;;To clear the flags on these files, you can use \\[dired-flag-backup-files] | |
5191 ;;with a prefix argument." | |
5192 ;;; (interactive "P") ; Never actually called interactively. | |
5193 ;; (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions))) | |
5194 ;; (let ((early-retention (if (< keep 0) (- keep) kept-old-versions)) | |
5195 ;; ;; late-retention must NEVER be allowed to be less than 1 in VMS! | |
5196 ;; ;; This could wipe ALL copies of the file. | |
5197 ;; (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep))) | |
5198 ;; (action (or msg "Cleaning")) | |
5199 ;; (ange-ftp-trample-marker (or marker dired-del-marker)) | |
5200 ;; (ange-ftp-file-version-alist ())) | |
5201 ;; (message (concat action | |
5202 ;; " numerical backups (keeping %d late, %d old)...") | |
5203 ;; late-retention early-retention) | |
5204 ;; ;; Look at each file. | |
5205 ;; ;; If the file has numeric backup versions, | |
5206 ;; ;; put on ange-ftp-file-version-alist an element of the form | |
5207 ;; ;; (FILENAME . VERSION-NUMBER-LIST) | |
5208 ;; (dired-map-dired-file-lines (function | |
5209 ;; ange-ftp-dired-vms-collect-file-versions)) | |
5210 ;; ;; Sort each VERSION-NUMBER-LIST, | |
5211 ;; ;; and remove the versions not to be deleted. | |
5212 ;; (let ((fval ange-ftp-file-version-alist)) | |
5213 ;; (while fval | |
5214 ;; (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<))) | |
5215 ;; (v-count (length sorted-v-list))) | |
5216 ;; (if (> v-count (+ early-retention late-retention)) | |
5217 ;; (rplacd (nthcdr early-retention sorted-v-list) | |
5218 ;; (nthcdr (- v-count late-retention) | |
5219 ;; sorted-v-list))) | |
5220 ;; (rplacd (car fval) | |
5221 ;; (cdr sorted-v-list))) | |
5222 ;; (setq fval (cdr fval)))) | |
5223 ;; ;; Look at each file. If it is a numeric backup file, | |
5224 ;; ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion. | |
5225 ;; (dired-map-dired-file-lines | |
5226 ;; (function | |
5227 ;; ange-ftp-dired-vms-trample-file-versions mark)) | |
5228 ;; (message (concat action " numerical backups...done")))) | |
5229 | |
5230 ;;(or (assq 'vms ange-ftp-dired-clean-directory-alist) | |
5231 ;; (setq ange-ftp-dired-clean-directory-alist | |
5232 ;; (cons '(vms . ange-ftp-dired-vms-clean-directory) | |
5233 ;; ange-ftp-dired-clean-directory-alist))) | |
5234 | |
5235 ;;(defun ange-ftp-dired-vms-collect-file-versions (fn) | |
5236 ;; ;; "If it looks like file FN has versions, return a list of the versions. | |
5237 ;; ;;That is a list of strings which are file names. | |
5238 ;; ;;The caller may want to flag some of these files for deletion." | |
5239 ;;(let ((name (nth 2 (ange-ftp-ftp-name fn)))) | |
5240 ;; (if (string-match ";[0-9]+$" name) | |
5241 ;; (let* ((name (substring name 0 (match-beginning 0))) | |
5242 ;; (fn (ange-ftp-replace-name-component fn name))) | |
5243 ;; (if (not (assq fn ange-ftp-file-version-alist)) | |
5244 ;; (let* ((base-versions | |
5245 ;; (concat (file-name-nondirectory name) ";")) | |
5246 ;; (bv-length (length base-versions)) | |
5247 ;; (possibilities (file-name-all-completions | |
5248 ;; base-versions | |
5249 ;; (file-name-directory fn))) | |
5250 ;; (versions (mapcar | |
5251 ;; '(lambda (arg) | |
5252 ;; (if (and (string-match | |
5253 ;; "[0-9]+$" arg bv-length) | |
5254 ;; (= (match-beginning 0) bv-length)) | |
5255 ;; (string-to-int (substring arg bv-length)) | |
5256 ;; 0)) | |
5257 ;; possibilities))) | |
5258 ;; (if versions | |
5259 ;; (setq | |
5260 ;; ange-ftp-file-version-alist | |
5261 ;; (cons (cons fn versions) | |
5262 ;; ange-ftp-file-version-alist))))))))) | |
5263 | |
5264 ;;(defun ange-ftp-dired-vms-trample-file-versions (fn) | |
5265 ;; (let* ((start-vn (string-match ";[0-9]+$" fn)) | |
5266 ;; base-version-list) | |
5267 ;; (and start-vn | |
5268 ;; (setq base-version-list ; there was a base version to which | |
5269 ;; (assoc (substring fn 0 start-vn) ; this looks like a | |
5270 ;; ange-ftp-file-version-alist)) ; subversion | |
5271 ;; (not (memq (string-to-int (substring fn (1+ start-vn))) | |
5272 ;; base-version-list)) ; this one doesn't make the cut | |
5273 ;; (progn (beginning-of-line) | |
5274 ;; (delete-char 1) | |
5275 ;; (insert ange-ftp-trample-marker))))) | |
5276 | |
5277 ;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p) | |
5278 ;; (let ((dired-kept-versions 1) | |
5279 ;; (kept-old-versions 0) | |
5280 ;; marker msg) | |
5281 ;; (if unflag-p | |
5282 ;; (setq marker ?\040 msg "Unflagging") | |
5283 ;; (setq marker dired-del-marker msg "Cleaning")) | |
5284 ;; (ange-ftp-dired-vms-clean-directory nil marker msg))) | |
5285 | |
5286 ;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist) | |
5287 ;; (setq ange-ftp-dired-flag-backup-files-alist | |
5288 ;; (cons '(vms . ange-ftp-dired-vms-flag-backup-files) | |
5289 ;; ange-ftp-dired-flag-backup-files-alist))) | |
5290 | |
5291 ;;(defun ange-ftp-dired-vms-backup-diff (&optional switches) | |
5292 ;; (let ((file (dired-get-filename 'no-dir)) | |
5293 ;; bak) | |
5294 ;; (if (and (string-match ";[0-9]+$" file) | |
5295 ;; ;; Find most recent previous version. | |
5296 ;; (let ((root (substring file 0 (match-beginning 0))) | |
5297 ;; (ver | |
5298 ;; (string-to-int (substring file (1+ (match-beginning 0))))) | |
5299 ;; found) | |
5300 ;; (setq ver (1- ver)) | |
5301 ;; (while (and (> ver 0) (not found)) | |
5302 ;; (setq bak (concat root ";" (int-to-string ver))) | |
5303 ;; (and (file-exists-p bak) (setq found t)) | |
5304 ;; (setq ver (1- ver))) | |
5305 ;; found)) | |
5306 ;; (if switches | |
5307 ;; (diff (expand-file-name bak) (expand-file-name file) switches) | |
5308 ;; (diff (expand-file-name bak) (expand-file-name file))) | |
5309 ;; (error "No previous version found for %s" file)))) | |
5310 | |
5311 ;;(or (assq 'vms ange-ftp-dired-backup-diff-alist) | |
5312 ;; (setq ange-ftp-dired-backup-diff-alist | |
5313 ;; (cons '(vms . ange-ftp-dired-vms-backup-diff) | |
5314 ;; ange-ftp-dired-backup-diff-alist))) | |
5315 | |
5316 | |
5317 ;;;; ------------------------------------------------------------ | |
5318 ;;;; MTS support | |
5319 ;;;; ------------------------------------------------------------ | |
5320 | |
5321 | |
5322 ;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from | |
5323 ;; MTS to UNIX-ish. | |
5324 (defun ange-ftp-fix-name-for-mts (name &optional reverse) | |
5325 (save-match-data | |
5326 (if reverse | |
5327 (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name) | |
5328 (let (acct file) | |
5329 (if (match-beginning 1) | |
5330 (setq acct (substring name 0 (match-end 1)))) | |
5331 (if (match-beginning 2) | |
5332 (setq file (substring name | |
5333 (match-beginning 2) (match-end 2)))) | |
5334 (concat (and acct (concat "/" acct "/")) | |
5335 file)) | |
5336 (error "name %s didn't match" name)) | |
5337 (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name) | |
5338 (concat (substring name 1 (match-end 1)) | |
5339 (substring name (match-beginning 2) (match-end 2))) | |
5340 ;; Let's hope that mts will recognize it anyway. | |
5341 name)))) | |
5342 | |
5343 (or (assq 'mts ange-ftp-fix-name-func-alist) | |
5344 (setq ange-ftp-fix-name-func-alist | |
5345 (cons '(mts . ange-ftp-fix-name-for-mts) | |
5346 ange-ftp-fix-name-func-alist))) | |
5347 | |
5348 ;; Convert name from UNIX-ish to MTS ready for a DIRectory listing. | |
5349 ;; Remember that there are no directories in MTS. | |
5350 (defun ange-ftp-fix-dir-name-for-mts (dir-name) | |
5351 (if (string-equal dir-name "/") | |
5352 (error "Cannot get listing for fictitious \"/\" directory.") | |
5353 (let ((dir-name (ange-ftp-fix-name-for-mts dir-name))) | |
5354 (cond | |
5355 ((string-equal dir-name "") | |
5356 "?") | |
5357 ((string-match ":$" dir-name) | |
5358 (concat dir-name "?")) | |
5359 (dir-name))))) ; It's just a single file. | |
5360 | |
5361 (or (assq 'mts ange-ftp-fix-dir-name-func-alist) | |
5362 (setq ange-ftp-fix-dir-name-func-alist | |
5363 (cons '(mts . ange-ftp-fix-dir-name-for-mts) | |
5364 ange-ftp-fix-dir-name-func-alist))) | |
5365 | |
5366 (or (memq 'mts ange-ftp-dumb-host-types) | |
5367 (setq ange-ftp-dumb-host-types | |
5368 (cons 'mts ange-ftp-dumb-host-types))) | |
5369 | |
5370 (defvar ange-ftp-mts-host-regexp nil) | |
5371 | |
5372 ;; Return non-nil if HOST is running MTS. | |
5373 (defun ange-ftp-mts-host (host) | |
5374 (and ange-ftp-mts-host-regexp | |
5375 (save-match-data | |
5376 (string-match ange-ftp-mts-host-regexp host)))) | |
5377 | |
5378 ;; Parse the current buffer which is assumed to be in mts ftp dir format. | |
5379 (defun ange-ftp-parse-mts-listing () | |
5380 (let ((tbl (ange-ftp-make-hashtable))) | |
5381 (goto-char (point-min)) | |
5382 (save-match-data | |
5383 (while (re-search-forward ange-ftp-date-regexp nil t) | |
5384 (end-of-line) | |
5385 (skip-chars-backward " ") | |
5386 (let ((end (point))) | |
5387 (skip-chars-backward "-A-Z0-9_.!") | |
5388 (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl)) | |
5389 (forward-line 1))) | |
5390 ;; Don't need to bother with .. | |
5391 (ange-ftp-put-hash-entry "." t tbl) | |
5392 tbl)) | |
5393 | |
5394 (or (assq 'mts ange-ftp-parse-list-func-alist) | |
5395 (setq ange-ftp-parse-list-func-alist | |
5396 (cons '(mts . ange-ftp-parse-mts-listing) | |
5397 ange-ftp-parse-list-func-alist))) | |
5398 | |
5399 (defun ange-ftp-add-mts-host (host) | |
5400 "Mark HOST as the name of a machine running MTS." | |
5401 (interactive | |
5402 (list (read-string "Host: " | |
5403 (let ((name (or (buffer-file-name) default-directory))) | |
5404 (and name (car (ange-ftp-ftp-name name))))))) | |
5405 (if (not (ange-ftp-mts-host host)) | |
5406 (setq ange-ftp-mts-host-regexp | |
5407 (concat "^" (regexp-quote host) "$" | |
5408 (and ange-ftp-mts-host-regexp "\\|") | |
5409 ange-ftp-mts-host-regexp) | |
5410 ange-ftp-host-cache nil))) | |
5411 | |
5412 ;;; Tree dired support: | |
5413 | |
5414 ;;;; There aren't too many systems left that use MTS. This dired support will | |
5415 ;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems | |
5416 ;;;; implement ftp in the same way. If not, it might be necessary to make the | |
5417 ;;;; following more flexible. | |
5418 | |
5419 ;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol) | |
5420 ;; "In dired, move to first char of filename on this line. | |
5421 ;;Returns position (point) or nil if no filename on this line." | |
5422 ;; ;; This is the MTS version. | |
5423 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5424 ;; (beginning-of-line) | |
5425 ;; (if (re-search-forward | |
5426 ;; ange-ftp-date-regexp eol t) | |
5427 ;; (progn | |
5428 ;; (skip-chars-forward " ") ; Eat blanks after date | |
5429 ;; (skip-chars-forward "0-9:" eol) ; Eat time or year | |
5430 ;; (skip-chars-forward " " eol) ; one space before filename | |
5431 ;; ;; When listing an account other than the users own account it appends | |
5432 ;; ;; ACCT: to the beginning of the filename. Skip over this. | |
5433 ;; (and (looking-at "[A-Z0-9_.]+:") | |
5434 ;; (goto-char (match-end 0))) | |
5435 ;; (point)) | |
5436 ;; (if raise-error | |
5437 ;; (error "No file on this line") | |
5438 ;; nil))) | |
5439 | |
5440 ;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist) | |
5441 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5442 ;; (cons '(mts . ange-ftp-dired-mts-move-to-filename) | |
5443 ;; ange-ftp-dired-move-to-filename-alist))) | |
5444 | |
5445 ;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol) | |
5446 ;; ;; Assumes point is at beginning of filename. | |
5447 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5448 ;; ;; On failure, signals an error or returns nil. | |
5449 ;; ;; This is the MTS version. | |
5450 ;; (let (opoint hidden case-fold-search) | |
5451 ;; (setq opoint (point) | |
5452 ;; eol (save-excursion (end-of-line) (point)) | |
5453 ;; hidden (and selective-display | |
5454 ;; (save-excursion (search-forward "\r" eol t)))) | |
5455 ;; (if hidden | |
5456 ;; nil | |
5457 ;; (skip-chars-forward "-A-Z0-9._!" eol)) | |
5458 ;; (or no-error | |
5459 ;; (not (eq opoint (point))) | |
5460 ;; (error | |
5461 ;; (if hidden | |
5462 ;; (substitute-command-keys | |
5463 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide") | |
5464 ;; "No file on this line"))) | |
5465 ;; (if (eq opoint (point)) | |
5466 ;; nil | |
5467 ;; (point)))) | |
5468 | |
5469 ;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist) | |
5470 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5471 ;; (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename) | |
5472 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5473 | |
5474 ;;;; ------------------------------------------------------------ | |
5475 ;;;; CMS support | |
5476 ;;;; ------------------------------------------------------------ | |
5477 | |
5478 ;; Since CMS doesn't have any full file name syntax, we have to fudge | |
5479 ;; things with cd's. We actually send too many cd's, but it's dangerous | |
5480 ;; to try to remember the current minidisk, because if the connection | |
5481 ;; is closed and needs to be reopened, we will find ourselves back in | |
5482 ;; the default minidisk. This is fairly likely since CMS ftp servers | |
5483 ;; usually close the connection after 5 minutes of inactivity. | |
5484 | |
5485 ;; Have I got the filename character set right? | |
5486 | |
5487 (defun ange-ftp-fix-name-for-cms (name &optional reverse) | |
5488 (save-match-data | |
5489 (if reverse | |
5490 ;; Since we only convert output from a pwd in this direction, | |
5491 ;; we'll assume that it's a minidisk, and make it into a | |
5492 ;; directory file name. Note that the expand-dir-hashtable | |
5493 ;; stores directories without the trailing /. Is this | |
5494 ;; consistent? | |
5495 (concat "/" name) | |
5496 (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" | |
5497 name) | |
5498 (let ((minidisk (substring name 1 (match-end 1)))) | |
5499 (if (match-beginning 2) | |
5500 (let ((file (substring name (match-beginning 2) | |
5501 (match-end 2))) | |
5502 (cmd (concat "cd " minidisk)) | |
5503 | |
5504 ;; Note that host and user are bound in the call | |
5505 ;; to ange-ftp-send-cmd | |
5506 (proc (ange-ftp-get-process ange-ftp-this-host | |
5507 ange-ftp-this-user))) | |
5508 | |
5509 ;; Must use ange-ftp-raw-send-cmd here to avoid | |
5510 ;; an infinite loop. | |
5511 (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg)) | |
5512 file | |
5513 ;; failed... try ONCE more. | |
5514 (setq proc (ange-ftp-get-process ange-ftp-this-host | |
5515 ange-ftp-this-user)) | |
5516 (let ((result (ange-ftp-raw-send-cmd proc cmd | |
5517 ange-ftp-this-msg))) | |
5518 (if (car result) | |
5519 file | |
5520 ;; failed. give up. | |
5521 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user | |
5522 (format "cd to minidisk %s failed: %s" | |
5523 minidisk (cdr result))))))) | |
5524 ;; return the minidisk | |
5525 minidisk)) | |
5526 (error "Invalid CMS filename"))))) | |
5527 | |
5528 (or (assq 'cms ange-ftp-fix-name-func-alist) | |
5529 (setq ange-ftp-fix-name-func-alist | |
5530 (cons '(cms . ange-ftp-fix-name-for-cms) | |
5531 ange-ftp-fix-name-func-alist))) | |
5532 | |
5533 (or (memq 'cms ange-ftp-dumb-host-types) | |
5534 (setq ange-ftp-dumb-host-types | |
5535 (cons 'cms ange-ftp-dumb-host-types))) | |
5536 | |
5537 ;; Convert name from UNIX-ish to CMS ready for a DIRectory listing. | |
5538 (defun ange-ftp-fix-dir-name-for-cms (dir-name) | |
5539 (cond | |
5540 ((string-equal "/" dir-name) | |
5541 (error "Cannot get listing for fictitious \"/\" directory.")) | |
5542 ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name) | |
5543 (let* ((minidisk (substring dir-name (match-beginning 1) (match-end 1))) | |
5544 ;; host and user are bound in the call to ange-ftp-send-cmd | |
5545 (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user)) | |
5546 (cmd (concat "cd " minidisk)) | |
5547 (file (if (match-beginning 2) | |
5548 ;; it's a single file | |
5549 (substring dir-name (match-beginning 2) | |
5550 (match-end 2)) | |
5551 ;; use the wild-card | |
5552 "*"))) | |
5553 (if (car (ange-ftp-raw-send-cmd proc cmd)) | |
5554 file | |
5555 ;; try again... | |
5556 (setq proc (ange-ftp-get-process ange-ftp-this-host | |
5557 ange-ftp-this-user)) | |
5558 (let ((result (ange-ftp-raw-send-cmd proc cmd))) | |
5559 (if (car result) | |
5560 file | |
5561 ;; give up | |
5562 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user | |
5563 (format "cd to minidisk %s failed: %s" | |
5564 minidisk (cdr result)))))))) | |
5565 (t (error "Invalid CMS file name")))) | |
5566 | |
5567 (or (assq 'cms ange-ftp-fix-dir-name-func-alist) | |
5568 (setq ange-ftp-fix-dir-name-func-alist | |
5569 (cons '(cms . ange-ftp-fix-dir-name-for-cms) | |
5570 ange-ftp-fix-dir-name-func-alist))) | |
5571 | |
5572 (defvar ange-ftp-cms-host-regexp nil | |
5573 "Regular expression to match hosts running the CMS operating system.") | |
5574 | |
5575 ;; Return non-nil if HOST is running CMS. | |
5576 (defun ange-ftp-cms-host (host) | |
5577 (and ange-ftp-cms-host-regexp | |
5578 (save-match-data | |
5579 (string-match ange-ftp-cms-host-regexp host)))) | |
5580 | |
5581 (defun ange-ftp-add-cms-host (host) | |
5582 "Mark HOST as the name of a CMS host." | |
5583 (interactive | |
5584 (list (read-string "Host: " | |
5585 (let ((name (or (buffer-file-name) default-directory))) | |
5586 (and name (car (ange-ftp-ftp-name name))))))) | |
5587 (if (not (ange-ftp-cms-host host)) | |
5588 (setq ange-ftp-cms-host-regexp | |
5589 (concat "^" (regexp-quote host) "$" | |
5590 (and ange-ftp-cms-host-regexp "\\|") | |
5591 ange-ftp-cms-host-regexp) | |
5592 ange-ftp-host-cache nil))) | |
5593 | |
5594 (defun ange-ftp-parse-cms-listing () | |
5595 ;; Parse the current buffer which is assumed to be a CMS directory listing. | |
5596 ;; If we succeed in getting a listing, then we will assume that the minidisk | |
5597 ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work | |
5598 ;; because ange-ftp doesn't know that the root hashtable has only part of | |
5599 ;; the info. It will assume that if a minidisk isn't in it, then it doesn't | |
5600 ;; exist. It would be nice if completion worked for minidisks, as we | |
5601 ;; discover them. | |
5602 ; (let* ((dir-file (directory-file-name file)) | |
5603 ; (root (file-name-directory dir-file)) | |
5604 ; (minidisk (ange-ftp-get-file-part dir-file)) | |
5605 ; (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable))) | |
5606 ; (if root-tbl | |
5607 ; (ange-ftp-put-hash-entry minidisk t root-tbl) | |
5608 ; (setq root-tbl (ange-ftp-make-hashtable)) | |
5609 ; (ange-ftp-put-hash-entry minidisk t root-tbl) | |
5610 ; (ange-ftp-put-hash-entry "." t root-tbl) | |
5611 ; (ange-ftp-set-files root root-tbl))) | |
5612 ;; Now do the usual parsing | |
5613 (let ((tbl (ange-ftp-make-hashtable))) | |
5614 (goto-char (point-min)) | |
5615 (save-match-data | |
5616 (while | |
5617 (re-search-forward | |
5618 "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t) | |
5619 (ange-ftp-put-hash-entry | |
5620 (concat (buffer-substring (match-beginning 1) | |
5621 (match-end 1)) | |
5622 "." | |
5623 (buffer-substring (match-beginning 2) | |
5624 (match-end 2))) | |
5625 nil tbl) | |
5626 (forward-line 1)) | |
5627 (ange-ftp-put-hash-entry "." t tbl)) | |
5628 tbl)) | |
5629 | |
5630 (or (assq 'cms ange-ftp-parse-list-func-alist) | |
5631 (setq ange-ftp-parse-list-func-alist | |
5632 (cons '(cms . ange-ftp-parse-cms-listing) | |
5633 ange-ftp-parse-list-func-alist))) | |
30459 | 5634 |
28210 | 5635 ;;;;; Tree dired support: |
5636 | |
5637 ;;(defconst ange-ftp-dired-cms-re-exe | |
5638 ;; "^. [-A-Z0-9$_]+ +EXEC " | |
5639 ;; "Regular expression to use to search for CMS executables.") | |
5640 | |
5641 ;;(or (assq 'cms ange-ftp-dired-re-exe-alist) | |
5642 ;; (setq ange-ftp-dired-re-exe-alist | |
5643 ;; (cons (cons 'cms ange-ftp-dired-cms-re-exe) | |
5644 ;; ange-ftp-dired-re-exe-alist))) | |
5645 | |
5646 | |
5647 ;;(defun ange-ftp-dired-cms-insert-headerline (dir) | |
5648 ;; ;; CMS has no total line, so we insert a blank line for | |
5649 ;; ;; aesthetics. | |
5650 ;; (insert "\n") | |
5651 ;; (forward-char -1) | |
5652 ;; (ange-ftp-real-dired-insert-headerline dir)) | |
5653 | |
5654 ;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist) | |
5655 ;; (setq ange-ftp-dired-insert-headerline-alist | |
5656 ;; (cons '(cms . ange-ftp-dired-cms-insert-headerline) | |
5657 ;; ange-ftp-dired-insert-headerline-alist))) | |
5658 | |
5659 ;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol) | |
5660 ;; "In dired, move to the first char of filename on this line." | |
5661 ;; ;; This is the CMS version. | |
5662 ;; (or eol (setq eol (progn (end-of-line) (point)))) | |
5663 ;; (let (case-fold-search) | |
5664 ;; (beginning-of-line) | |
5665 ;; (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t) | |
5666 ;; (goto-char (1+ (match-beginning 0))) | |
5667 ;; (if raise-error | |
5668 ;; (error "No file on this line") | |
5669 ;; nil)))) | |
5670 | |
5671 ;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist) | |
5672 ;; (setq ange-ftp-dired-move-to-filename-alist | |
5673 ;; (cons '(cms . ange-ftp-dired-cms-move-to-filename) | |
5674 ;; ange-ftp-dired-move-to-filename-alist))) | |
5675 | |
5676 ;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol) | |
5677 ;; ;; Assumes point is at beginning of filename. | |
5678 ;; ;; So, it should be called only after (dired-move-to-filename t). | |
5679 ;; ;; case-fold-search must be nil, at least for VMS. | |
5680 ;; ;; On failure, signals an error or returns nil. | |
5681 ;; ;; This is the CMS version. | |
5682 ;; (let ((opoint (point)) | |
5683 ;; case-fold-search hidden) | |
5684 ;; (or eol (setq eol (save-excursion (end-of-line) (point)))) | |
5685 ;; (setq hidden (and selective-display | |
5686 ;; (save-excursion | |
5687 ;; (search-forward "\r" eol t)))) | |
5688 ;; (if hidden | |
5689 ;; (if no-error | |
5690 ;; nil | |
5691 ;; (error | |
5692 ;; (substitute-command-keys | |
5693 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide"))) | |
5694 ;; (skip-chars-forward "-A-Z0-9$_" eol) | |
5695 ;; (skip-chars-forward " " eol) | |
5696 ;; (skip-chars-forward "-A-Z0-9$_" eol) | |
5697 ;; (if (eq opoint (point)) | |
5698 ;; (if no-error | |
5699 ;; nil | |
5700 ;; (error "No file on this line")) | |
5701 ;; (point))))) | |
5702 | |
5703 ;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist) | |
5704 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist | |
5705 ;; (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename) | |
5706 ;; ange-ftp-dired-move-to-end-of-filename-alist))) | |
5707 | |
5708 (defun ange-ftp-cms-make-compressed-filename (name &optional reverse) | |
5709 (if (string-match "-Z$" name) | |
5710 (list nil (substring name 0 -2)) | |
5711 (list t (concat name "-Z")))) | |
5712 | |
5713 (or (assq 'cms ange-ftp-make-compressed-filename-alist) | |
5714 (setq ange-ftp-make-compressed-filename-alist | |
5715 (cons '(cms . ange-ftp-cms-make-compressed-filename) | |
5716 ange-ftp-make-compressed-filename-alist))) | |
5717 | |
5718 ;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep) | |
5719 ;; (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep))) | |
5720 ;; (and name | |
5721 ;; (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name) | |
5722 ;; (concat (substring name 0 (match-end 1)) | |
5723 ;; "." | |
5724 ;; (substring name (match-beginning 2) (match-end 2))) | |
5725 ;; name)))) | |
5726 | |
5727 ;;(or (assq 'cms ange-ftp-dired-get-filename-alist) | |
5728 ;; (setq ange-ftp-dired-get-filename-alist | |
5729 ;; (cons '(cms . ange-ftp-dired-cms-get-filename) | |
5730 ;; ange-ftp-dired-get-filename-alist))) | |
5731 | |
5732 ;;;; ------------------------------------------------------------ | |
5733 ;;;; Finally provide package. | |
5734 ;;;; ------------------------------------------------------------ | |
5735 | |
5736 (provide 'ange-ftp) | |
5737 | |
5738 ;;; ange-ftp.el ends here |