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