]> code.delx.au - gnu-emacs/blob - lisp/ange-ftp.el
(ange-ftp-hook-function): Use ange-ftp-run-real-handler.
[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.48 $")
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 "Major mode for interacting with the FTP process.
1778
1779 \\{comint-mode-map}"
1780 (interactive)
1781 (comint-mode)
1782 (setq major-mode 'internal-ange-ftp-mode)
1783 (setq mode-name "Internal Ange-ftp")
1784 (let ((proc (get-buffer-process (current-buffer))))
1785 (goto-char (point-max))
1786 (set-marker (process-mark proc) (point))
1787 (make-local-variable 'ange-ftp-process-string)
1788 (setq ange-ftp-process-string "")
1789 (make-local-variable 'ange-ftp-process-busy)
1790 (make-local-variable 'ange-ftp-process-result)
1791 (make-local-variable 'ange-ftp-process-msg)
1792 (make-local-variable 'ange-ftp-process-multi-skip)
1793 (make-local-variable 'ange-ftp-process-result-line)
1794 (make-local-variable 'ange-ftp-process-continue)
1795 (make-local-variable 'ange-ftp-hash-mark-count)
1796 (make-local-variable 'ange-ftp-binary-hash-mark-size)
1797 (make-local-variable 'ange-ftp-ascii-hash-mark-size)
1798 (make-local-variable 'ange-ftp-hash-mark-unit)
1799 (make-local-variable 'ange-ftp-xfer-size)
1800 (make-local-variable 'ange-ftp-last-percent)
1801 (setq ange-ftp-hash-mark-count 0)
1802 (setq ange-ftp-xfer-size 0)
1803 (setq ange-ftp-process-result-line "")
1804
1805 (setq comint-prompt-regexp "^ftp> ")
1806 (make-local-variable 'paragraph-start)
1807 (setq paragraph-start comint-prompt-regexp)))
1808
1809 (defun ange-ftp-smart-login (host user pass account proc)
1810 "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
1811 PROC is the FTP-client's process. This routine uses the smart-gateway
1812 host specified in ``ange-ftp-gateway-host''."
1813 (let ((result (ange-ftp-raw-send-cmd
1814 proc
1815 (format "open %s %s"
1816 (ange-ftp-nslookup-host ange-ftp-gateway-host)
1817 ange-ftp-smart-gateway-port)
1818 (format "Opening FTP connection to %s via %s"
1819 host
1820 ange-ftp-gateway-host))))
1821 (or (car result)
1822 (ange-ftp-error host user
1823 (concat "OPEN request failed: "
1824 (cdr result))))
1825 (setq result (ange-ftp-raw-send-cmd
1826 proc (format "user \"%s\"@%s %s %s"
1827 user
1828 (ange-ftp-nslookup-host host)
1829 pass
1830 account)
1831 (format "Logging in as user %s@%s"
1832 user host)))
1833 (or (car result)
1834 (progn
1835 (ange-ftp-set-passwd host user nil) ; reset password
1836 (ange-ftp-set-account host user nil) ; reset account
1837 (ange-ftp-error host user
1838 (concat "USER request failed: "
1839 (cdr result)))))))
1840
1841 (defun ange-ftp-normal-login (host user pass account proc)
1842 "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
1843 PROC is the process to the FTP-client."
1844 (let ((result (ange-ftp-raw-send-cmd
1845 proc
1846 (format "open %s" (ange-ftp-nslookup-host host))
1847 (format "Opening FTP connection to %s" host))))
1848 (or (car result)
1849 (ange-ftp-error host user
1850 (concat "OPEN request failed: "
1851 (cdr result))))
1852 (setq result (ange-ftp-raw-send-cmd
1853 proc
1854 (format "user \"%s\" %s %s" user pass account)
1855 (format "Logging in as user %s@%s" user host)))
1856 (or (car result)
1857 (progn
1858 (ange-ftp-set-passwd host user nil) ;reset password.
1859 (ange-ftp-set-account host user nil) ;reset account.
1860 (ange-ftp-error host user
1861 (concat "USER request failed: "
1862 (cdr result)))))))
1863
1864 ;; ange@hplb.hpl.hp.com says this should not be changed.
1865 (defvar ange-ftp-hash-mark-msgs
1866 "[hH]ash mark [^0-9]*\\([0-9]+\\)"
1867 "*Regexp matching the FTP client's output upon doing a HASH command.")
1868
1869 (defun ange-ftp-guess-hash-mark-size (proc)
1870 (if ange-ftp-send-hash
1871 (save-excursion
1872 (set-buffer (process-buffer proc))
1873 (let* ((status (ange-ftp-raw-send-cmd proc "hash"))
1874 (result (car status))
1875 (line (cdr status)))
1876 (ange-ftp-save-match-data
1877 (if (string-match ange-ftp-hash-mark-msgs line)
1878 (let ((size (string-to-int
1879 (substring line
1880 (match-beginning 1)
1881 (match-end 1)))))
1882 (setq ange-ftp-ascii-hash-mark-size size
1883 ange-ftp-hash-mark-unit (ash size -4))
1884
1885 ;; if a default value for this is set, use that value.
1886 (or ange-ftp-binary-hash-mark-size
1887 (setq ange-ftp-binary-hash-mark-size size)))))))))
1888
1889 (defun ange-ftp-get-process (host user)
1890 "Return an FTP subprocess connected to HOST and logged in as USER.
1891 Create a new process if needed."
1892 (let* ((name (ange-ftp-ftp-process-buffer host user))
1893 (proc (get-process name)))
1894 (if (and proc (memq (process-status proc) '(run open)))
1895 proc
1896 (let ((pass (ange-ftp-quote-string
1897 (ange-ftp-get-passwd host user)))
1898 (account (ange-ftp-quote-string
1899 (ange-ftp-get-account host user))))
1900 ;; grab a suitable process.
1901 (setq proc (ange-ftp-start-process host user name))
1902
1903 ;; login to FTP server.
1904 (if (ange-ftp-use-smart-gateway-p host)
1905 (ange-ftp-smart-login host user pass account proc)
1906 (ange-ftp-normal-login host user pass account proc))
1907
1908 ;; Tell client to send back hash-marks as progress. It isn't usually
1909 ;; fatal if this command fails.
1910 (ange-ftp-guess-hash-mark-size proc)
1911
1912 ;; Guess at the host type.
1913 (ange-ftp-guess-host-type host user)
1914
1915 ;; Run any user-specified hooks. Note that proc, host and user are
1916 ;; dynamically bound at this point.
1917 (run-hooks 'ange-ftp-process-startup-hook))
1918 proc)))
1919
1920 ;; Variables for caching host and host-type
1921 (defvar ange-ftp-host-cache nil)
1922 (defvar ange-ftp-host-type-cache nil)
1923
1924 ;; If ange-ftp-host-type is called with the optional user
1925 ;; argument, it will attempt to guess the host type by connecting
1926 ;; as user, if necessary. For efficiency, I have tried to give this
1927 ;; optional second argument only when necessary. Have I missed any calls
1928 ;; to ange-ftp-host-type where it should have been supplied?
1929
1930 (defun ange-ftp-host-type (host &optional user)
1931 "Return a symbol which represents the type of the HOST given.
1932 If the optional argument USER is given, attempts to guess the
1933 host-type by logging in as USER."
1934 (if (eq host ange-ftp-host-cache)
1935 ange-ftp-host-type-cache
1936 ;; Trigger an ftp connection, in case we need to guess at the host type.
1937 (if (and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache))
1938 ange-ftp-host-type-cache
1939 (setq ange-ftp-host-cache host
1940 ange-ftp-host-type-cache
1941 (cond ((ange-ftp-dumb-unix-host host)
1942 'dumb-unix)
1943 ;; ((and (fboundp 'ange-ftp-vos-host)
1944 ;; (ange-ftp-vos-host host))
1945 ;; 'vos)
1946 ((and (fboundp 'ange-ftp-vms-host)
1947 (ange-ftp-vms-host host))
1948 'vms)
1949 ((and (fboundp 'ange-ftp-mts-host)
1950 (ange-ftp-mts-host host))
1951 'mts)
1952 ((and (fboundp 'ange-ftp-cms-host)
1953 (ange-ftp-cms-host host))
1954 'cms)
1955 (t
1956 'unix))))))
1957
1958 ;; It would be nice to abstract the functions ange-ftp-TYPE-host and
1959 ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions
1960 ;; without sacrificing speed. Also, having separate variables
1961 ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to
1962 ;; set an alist to indicate that a host is of a given type. Even with
1963 ;; automatic host type recognition, setting a regexp is still a good idea
1964 ;; (for efficiency) if you log into a particular non-UNIX host frequently.
1965
1966 (defvar ange-ftp-fix-name-func-alist nil
1967 "Alist saying how to convert file name to the host's syntax.
1968 Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
1969 which can change a UNIX file name into a name more suitable for a host of type
1970 TYPE.")
1971
1972 (defvar ange-ftp-fix-dir-name-func-alist nil
1973 "Alist saying how to convert directory name to the host's syntax.
1974 Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
1975 which can change UNIX directory name into a directory name more suitable
1976 for a host of type TYPE.")
1977
1978 ;; *** Perhaps the sense of this variable should be inverted, since there
1979 ;; *** is only 1 host type that can take ls-style listing options.
1980 (defvar ange-ftp-dumb-host-types '(dumb-unix)
1981 "List of host types that can't take UNIX ls-style listing options.")
1982
1983 (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait)
1984 "Find an ftp process connected to HOST logged in as USER and send it CMD.
1985 MSG is an optional status message to be output before and after issuing the
1986 command.
1987 See the documentation for ange-ftp-raw-send-cmd for a description of CONT
1988 and NOWAIT."
1989 ;; Handle conversion to remote file name syntax and remote ls option
1990 ;; capability.
1991 (let ((cmd0 (car cmd))
1992 (cmd1 (nth 1 cmd))
1993 (ange-ftp-this-user user)
1994 (ange-ftp-this-host host)
1995 (ange-ftp-this-msg msg)
1996 cmd2 cmd3 host-type fix-name-func)
1997
1998 (cond
1999
2000 ;; pwd case (We don't care what host-type.)
2001 ((null cmd1))
2002
2003 ;; cmd == 'dir "remote-name" "local-name" "ls-switches"
2004 ((progn
2005 (setq cmd2 (nth 2 cmd)
2006 host-type (ange-ftp-host-type host user))
2007 ;; This will trigger an FTP login, if one doesn't exist
2008 (eq cmd0 'dir))
2009 (setq cmd1 (funcall
2010 (or (cdr (assq host-type ange-ftp-fix-dir-name-func-alist))
2011 'identity)
2012 cmd1)
2013 cmd3 (nth 3 cmd))
2014 ;; Need to deal with the HP-UX ftp bug. This should also allow
2015 ;; us to resolve symlinks to directories on SysV machines. (Sebastian will
2016 ;; be happy.)
2017 (and (eq host-type 'unix)
2018 (string-match "/$" cmd1)
2019 (not (string-match "R" cmd3))
2020 (setq cmd1 (concat cmd1 ".")))
2021 ;; If the remote ls can take switches, put them in
2022 (or (memq host-type ange-ftp-dumb-host-types)
2023 (setq cmd0 'ls
2024 cmd1 (format "\"%s %s\"" cmd3 cmd1))))
2025
2026 ;; First argument is the remote name
2027 ((progn
2028 (setq fix-name-func (or (cdr (assq host-type
2029 ange-ftp-fix-name-func-alist))
2030 'identity))
2031 (memq cmd0 '(get delete mkdir rmdir cd)))
2032 (setq cmd1 (funcall fix-name-func cmd1)))
2033
2034 ;; Second argument is the remote name
2035 ((memq cmd0 '(append put chmod))
2036 (setq cmd2 (funcall fix-name-func cmd2)))
2037
2038 ;; Both arguments are remote names
2039 ((eq cmd0 'rename)
2040 (setq cmd1 (funcall fix-name-func cmd1)
2041 cmd2 (funcall fix-name-func cmd2))))
2042
2043 ;; Turn the command into one long string
2044 (setq cmd0 (symbol-name cmd0))
2045 (setq cmd (concat cmd0
2046 (and cmd1 (concat " " cmd1))
2047 (and cmd2 (concat " " cmd2))))
2048
2049 ;; Actually send the resulting command.
2050 (let (afsc-result
2051 afsc-line)
2052 (ange-ftp-raw-send-cmd
2053 (ange-ftp-get-process host user)
2054 cmd
2055 msg
2056 (list
2057 (function (lambda (result line host user
2058 cmd msg cont nowait)
2059 (or cont
2060 (setq afsc-result result
2061 afsc-line line))
2062 (if result
2063 (ange-ftp-call-cont cont result line)
2064 (ange-ftp-raw-send-cmd
2065 (ange-ftp-get-process host user)
2066 cmd
2067 msg
2068 (list
2069 (function (lambda (result line cont)
2070 (or cont
2071 (setq afsc-result result
2072 afsc-line line))
2073 (ange-ftp-call-cont cont result line)))
2074 cont)
2075 nowait))))
2076 host user cmd msg cont nowait)
2077 nowait)
2078
2079 (if nowait
2080 nil
2081 (if cont
2082 nil
2083 (cons afsc-result afsc-line))))))
2084
2085 ;; It might be nice to message users about the host type identified,
2086 ;; but there is so much other messaging going on, it would not be
2087 ;; seen. No point in slowing things down just so users can read
2088 ;; a host type message.
2089
2090 (defconst ange-ftp-cms-name-template
2091 (concat
2092 "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
2093 "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
2094 (defconst ange-ftp-vms-name-template
2095 "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
2096 (defconst ange-ftp-mts-name-template
2097 "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
2098
2099 (defun ange-ftp-guess-host-type (host user)
2100 "Guess at the the host type of HOST.
2101 Works by doing a pwd and examining the directory syntax."
2102 (let ((host-type (ange-ftp-host-type host))
2103 (key (concat host "/" user "/~")))
2104 (if (eq host-type 'unix)
2105 ;; Note that ange-ftp-host-type returns unix as the default value.
2106 (ange-ftp-save-match-data
2107 (let* ((result (ange-ftp-get-pwd host user))
2108 (dir (car result))
2109 fix-name-func)
2110 (cond ((null dir)
2111 (message "Warning! Unable to get home directory")
2112 (sit-for 1)
2113 (if (string-match
2114 "^450 No current working directory defined$"
2115 (cdr result))
2116
2117 ;; We'll assume that if pwd bombs with this
2118 ;; error message, then it's CMS.
2119 (progn
2120 (ange-ftp-add-cms-host host)
2121 (setq ange-ftp-host-cache host
2122 ange-ftp-host-type-cache 'cms))))
2123
2124 ;; try for VMS
2125 ((string-match ange-ftp-vms-name-template dir)
2126 (ange-ftp-add-vms-host host)
2127 ;; The add-host functions clear the host type cache.
2128 ;; Therefore, need to set the cache afterwards.
2129 (setq ange-ftp-host-cache host
2130 ange-ftp-host-type-cache 'vms))
2131
2132 ;; try for MTS
2133 ((string-match ange-ftp-mts-name-template dir)
2134 (ange-ftp-add-mts-host host)
2135 (setq ange-ftp-host-cache host
2136 ange-ftp-host-type-cache 'mts))
2137
2138 ;; try for CMS
2139 ((string-match ange-ftp-cms-name-template dir)
2140 (ange-ftp-add-cms-host host)
2141 (setq ange-ftp-host-cache host
2142 ange-ftp-host-type-cache 'cms))
2143
2144 ;; assume UN*X
2145 (t
2146 (setq ange-ftp-host-cache host
2147 ange-ftp-host-type-cache 'unix)))
2148
2149 ;; Now that we have done a pwd, might as well put it in
2150 ;; the expand-dir hashtable.
2151 (let ((ange-ftp-this-user user)
2152 (ange-ftp-this-host host))
2153 (setq fix-name-func (cdr (assq ange-ftp-host-type-cache
2154 ange-ftp-fix-name-func-alist)))
2155 (if fix-name-func
2156 (setq dir (funcall fix-name-func dir 'reverse))))
2157 (ange-ftp-put-hash-entry key dir
2158 ange-ftp-expand-dir-hashtable))))
2159
2160 ;; In the special case of CMS make sure that know the
2161 ;; expansion of the home minidisk now, because we will
2162 ;; be doing a lot of cd's.
2163 (if (and (eq host-type 'cms)
2164 (not (ange-ftp-hash-entry-exists-p
2165 key ange-ftp-expand-dir-hashtable)))
2166 (let ((dir (car (ange-ftp-get-pwd host user))))
2167 (if dir
2168 (ange-ftp-put-hash-entry key (concat "/" dir)
2169 ange-ftp-expand-dir-hashtable)
2170 (message "Warning! Unable to get home directory")
2171 (sit-for 1))))))
2172
2173 \f
2174 ;;;; ------------------------------------------------------------
2175 ;;;; Remote file and directory listing support.
2176 ;;;; ------------------------------------------------------------
2177
2178 ;; Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
2179 ;; to take switch arguments.
2180 (defun ange-ftp-dumb-unix-host (host)
2181 (and ange-ftp-dumb-unix-host-regexp
2182 (ange-ftp-save-match-data
2183 (string-match ange-ftp-dumb-unix-host-regexp host))))
2184
2185 (defun ange-ftp-add-dumb-unix-host (host)
2186 "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
2187 (interactive
2188 (list (read-string "Host: "
2189 (let ((name (or (buffer-file-name) default-directory)))
2190 (and name (car (ange-ftp-ftp-name name)))))))
2191 (if (not (ange-ftp-dumb-unix-host host))
2192 (setq ange-ftp-dumb-unix-host-regexp
2193 (concat "^" (regexp-quote host) "$"
2194 (and ange-ftp-dumb-unix-host-regexp "\\|")
2195 ange-ftp-dumb-unix-host-regexp)
2196 ange-ftp-host-cache nil)))
2197
2198 (defvar ange-ftp-parse-list-func-alist nil
2199 "Alist saying how to parse directory listings for certain OS types.
2200 Association list of \( TYPE \. FUNC \) pairs. The FUNC is a routine
2201 which can parse the output from a DIR listing for a host of type TYPE.")
2202
2203 ;; With no-error nil, this function returns:
2204 ;; an error if file is not an ange-ftp-name
2205 ;; (This should never happen.)
2206 ;; an error if either the listing is unreadable or there is an ftp error.
2207 ;; the listing (a string), if everything works.
2208 ;;
2209 ;; With no-error t, it returns:
2210 ;; an error if not an ange-ftp-name
2211 ;; error if listing is unreable (most likely caused by a slow connection)
2212 ;; nil if ftp error (this is because although asking to list a nonexistent
2213 ;; directory on a remote unix machine usually (except
2214 ;; maybe for dumb hosts) returns an ls error, but no
2215 ;; ftp error, if the same is done on a VMS machine,
2216 ;; an ftp error is returned. Need to trap the error
2217 ;; so we can go on and try to list the parent.)
2218 ;; the listing, if everything works.
2219
2220 ;; If WILDCARD is non-nil, then this implements the guts of insert-directory
2221 ;; in the wildcard case. Then we make a relative directory listing
2222 ;; of FILE within the directory specified by `default-directory'.
2223
2224 (defun ange-ftp-ls (file lsargs parse &optional no-error wildcard)
2225 "Return the output of an `DIR' or `ls' command done over ftp.
2226 FILE is the full name of the remote file, LSARGS is any args to pass to the
2227 `ls' command, and PARSE specifies that the output should be parsed and stored
2228 away in the internal cache."
2229 ;; If parse is t, we assume that file is a directory. i.e. we only parse
2230 ;; full directory listings.
2231 (let* ((ange-ftp-this-file (ange-ftp-expand-file-name file))
2232 (parsed (ange-ftp-ftp-name ange-ftp-this-file)))
2233 (if parsed
2234 (let* ((host (nth 0 parsed))
2235 (user (nth 1 parsed))
2236 (name (ange-ftp-quote-string (nth 2 parsed)))
2237 (key (directory-file-name ange-ftp-this-file))
2238 (host-type (ange-ftp-host-type host user))
2239 (dumb (memq host-type ange-ftp-dumb-host-types))
2240 result
2241 temp
2242 lscmd parse-func)
2243 (if (string-equal name "")
2244 (setq name
2245 (ange-ftp-real-file-name-as-directory
2246 (ange-ftp-expand-dir host user "~"))))
2247 (if (and ange-ftp-ls-cache-file
2248 (string-equal key ange-ftp-ls-cache-file)
2249 ;; Don't care about lsargs for dumb hosts.
2250 (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
2251 ange-ftp-ls-cache-res
2252 (setq temp (ange-ftp-make-tmp-name host))
2253 (if wildcard
2254 (progn
2255 (ange-ftp-cd host user (file-name-directory name))
2256 (setq lscmd (list 'dir file temp lsargs)))
2257 (setq lscmd (list 'dir name temp lsargs)))
2258 (unwind-protect
2259 (if (car (setq result (ange-ftp-send-cmd
2260 host
2261 user
2262 lscmd
2263 (format "Listing %s"
2264 (ange-ftp-abbreviate-filename
2265 ange-ftp-this-file)))))
2266 (save-excursion
2267 (set-buffer (get-buffer-create
2268 ange-ftp-data-buffer-name))
2269 (erase-buffer)
2270 (if (ange-ftp-real-file-readable-p temp)
2271 (ange-ftp-real-insert-file-contents temp)
2272 (sleep-for ange-ftp-retry-time)
2273 ;wait for file to possibly appear
2274 (if (ange-ftp-real-file-readable-p temp)
2275 ;; Try again.
2276 (ange-ftp-real-insert-file-contents temp)
2277 (ange-ftp-error host user
2278 (format
2279 "list data file %s not readable"
2280 temp))))
2281 (if parse
2282 (ange-ftp-set-files
2283 ange-ftp-this-file
2284 (if (setq
2285 parse-func
2286 (cdr (assq host-type
2287 ange-ftp-parse-list-func-alist)))
2288 (funcall parse-func)
2289 (ange-ftp-parse-dired-listing lsargs))))
2290 (setq ange-ftp-ls-cache-file key
2291 ange-ftp-ls-cache-lsargs lsargs
2292 ; For dumb hosts-types this is
2293 ; meaningless but harmless.
2294 ange-ftp-ls-cache-res (buffer-string))
2295 ;; (kill-buffer (current-buffer))
2296 ange-ftp-ls-cache-res)
2297 (if no-error
2298 nil
2299 (ange-ftp-error host user
2300 (concat "DIR failed: " (cdr result)))))
2301 (ange-ftp-del-tmp-name temp))))
2302 (error "Should never happen. Please report. Bug ref. no.: 1"))))
2303 \f
2304 ;;;; ------------------------------------------------------------
2305 ;;;; Directory information caching support.
2306 ;;;; ------------------------------------------------------------
2307
2308 (defconst ange-ftp-date-regexp
2309 (concat
2310 " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
2311 "\\|Nov\\|Dec\\) +[0-3]?[0-9] "))
2312
2313 (defvar ange-ftp-add-file-entry-alist nil
2314 "Alist saying how to add file entries on certain OS types.
2315 Association list of pairs \( TYPE \. FUNC \), where FUNC
2316 is a function to be used to add a file entry for the OS TYPE. The
2317 main reason for this alist is to deal with file versions in VMS.")
2318
2319 (defvar ange-ftp-delete-file-entry-alist nil
2320 "Alist saying how to delete files on certain OS types.
2321 Association list of pairs \( TYPE \. FUNC \), where FUNC
2322 is a function to be used to delete a file entry for the OS TYPE.
2323 The main reason for this alist is to deal with file versions in VMS.")
2324
2325 (defun ange-ftp-add-file-entry (name &optional dir-p)
2326 "Add a file entry for file NAME, if its directory info exists."
2327 (funcall (or (cdr (assq (ange-ftp-host-type
2328 (car (ange-ftp-ftp-name name)))
2329 ange-ftp-add-file-entry-alist))
2330 'ange-ftp-internal-add-file-entry)
2331 name dir-p)
2332 (setq ange-ftp-ls-cache-file nil))
2333
2334 (defun ange-ftp-delete-file-entry (name &optional dir-p)
2335 "Delete the file entry for file NAME, if its directory info exists."
2336 (funcall (or (cdr (assq (ange-ftp-host-type
2337 (car (ange-ftp-ftp-name name)))
2338 ange-ftp-delete-file-entry-alist))
2339 'ange-ftp-internal-delete-file-entry)
2340 name dir-p)
2341 (setq ange-ftp-ls-cache-file nil))
2342
2343 (defmacro ange-ftp-parse-filename ()
2344 ;;Extract the filename from the current line of a dired-like listing.
2345 (` (let ((eol (progn (end-of-line) (point))))
2346 (beginning-of-line)
2347 (if (re-search-forward ange-ftp-date-regexp eol t)
2348 (progn
2349 (skip-chars-forward " ")
2350 (skip-chars-forward "^ " eol)
2351 (skip-chars-forward " " eol)
2352 ;; We bomb on filenames starting with a space.
2353 (buffer-substring (point) eol))))))
2354
2355 ;; This deals with the F switch. Should also do something about
2356 ;; unquoting names obtained with the SysV b switch and the GNU Q
2357 ;; switch. See Sebastian's dired-get-filename.
2358
2359 (defmacro ange-ftp-ls-parser ()
2360 ;; Note that switches is dynamically bound.
2361 ;; Meant to be called by ange-ftp-parse-dired-listing
2362 (` (let ((tbl (ange-ftp-make-hashtable))
2363 (used-F (and (stringp switches)
2364 (string-match "F" switches)))
2365 file-type symlink directory file)
2366 (while (setq file (ange-ftp-parse-filename))
2367 (beginning-of-line)
2368 (skip-chars-forward "\t 0-9")
2369 (setq file-type (following-char)
2370 directory (eq file-type ?d))
2371 (if (eq file-type ?l)
2372 (if (string-match " -> " file)
2373 (setq symlink (substring file (match-end 0))
2374 file (substring file 0 (match-beginning 0)))
2375 ;; Shouldn't happen
2376 (setq symlink ""))
2377 (setq symlink nil))
2378 ;; Only do a costly regexp search if the F switch was used.
2379 (if (and used-F
2380 (not (string-equal file ""))
2381 (looking-at
2382 ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
2383 (let ((socket (eq file-type ?s))
2384 (executable
2385 (and (not symlink) ; x bits don't mean a thing for symlinks
2386 (string-match "[xst]"
2387 (concat
2388 (buffer-substring
2389 (match-beginning 1)
2390 (match-end 1))
2391 (buffer-substring
2392 (match-beginning 2)
2393 (match-end 2))
2394 (buffer-substring
2395 (match-beginning 3)
2396 (match-end 3)))))))
2397 ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
2398 ;; and others don't. (sigh...) Beware, that some Unix's don't
2399 ;; seem to believe in the F-switch
2400 (if (or (and symlink (string-match "@$" file))
2401 (and directory (string-match "/$" file))
2402 (and executable (string-match "*$" file))
2403 (and socket (string-match "=$" file)))
2404 (setq file (substring file 0 -1)))))
2405 (ange-ftp-put-hash-entry file (or symlink directory) tbl)
2406 (forward-line 1))
2407 (ange-ftp-put-hash-entry "." t tbl)
2408 (ange-ftp-put-hash-entry ".." t tbl)
2409 tbl)))
2410
2411 ;;; The dl stuff for descriptive listings
2412
2413 (defvar ange-ftp-dl-dir-regexp nil
2414 "Regexp matching directories which are listed in dl format.
2415 This regexp should not be anchored with a trailing `$', because it should
2416 match subdirectories as well.")
2417
2418 (defun ange-ftp-add-dl-dir (dir)
2419 "Interactively adds a DIR to ange-ftp-dl-dir-regexp."
2420 (interactive
2421 (list (read-string "Directory: "
2422 (let ((name (or (buffer-file-name) default-directory)))
2423 (and name (ange-ftp-ftp-name name)
2424 (file-name-directory name))))))
2425 (if (not (and ange-ftp-dl-dir-regexp
2426 (string-match ange-ftp-dl-dir-regexp dir)))
2427 (setq ange-ftp-dl-dir-regexp
2428 (concat "^" (regexp-quote dir)
2429 (and ange-ftp-dl-dir-regexp "\\|")
2430 ange-ftp-dl-dir-regexp))))
2431
2432 (defmacro ange-ftp-dl-parser ()
2433 ;; Parse the current buffer, which is assumed to be a descriptive
2434 ;; listing, and return a hashtable.
2435 (` (let ((tbl (ange-ftp-make-hashtable)))
2436 (while (not (eobp))
2437 (ange-ftp-put-hash-entry
2438 (buffer-substring (point)
2439 (progn
2440 (skip-chars-forward "^ /\n")
2441 (point)))
2442 (eq (following-char) ?/)
2443 tbl)
2444 (forward-line 1))
2445 (ange-ftp-put-hash-entry "." t tbl)
2446 (ange-ftp-put-hash-entry ".." t tbl)
2447 tbl)))
2448
2449 ;; Parse the current buffer which is assumed to be in a dired-like listing
2450 ;; format, and return a hashtable as the result. If the listing is not really
2451 ;; a listing, then return nil.
2452
2453 (defun ange-ftp-parse-dired-listing (&optional switches)
2454 (ange-ftp-save-match-data
2455 (cond
2456 ((looking-at "^total [0-9]+$")
2457 (forward-line 1)
2458 ;; Some systems put in a blank line here.
2459 (if (eolp) (forward-line 1))
2460 (ange-ftp-ls-parser))
2461 ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
2462 ;; It's an ls error message.
2463 nil)
2464 ((eobp) ; i.e. (zerop (buffer-size))
2465 ;; This could be one of:
2466 ;; (1) An Ultrix ls error message
2467 ;; (2) A listing with the A switch of an empty directory
2468 ;; on a machine which doesn't give a total line.
2469 ;; (3) The twilight zone.
2470 ;; We'll assume (1) for now.
2471 nil)
2472 ((re-search-forward ange-ftp-date-regexp nil t)
2473 (beginning-of-line)
2474 (ange-ftp-ls-parser))
2475 ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
2476 ;; It's a dl listing (I hope).
2477 ;; file is bound by the call to ange-ftp-ls
2478 (ange-ftp-add-dl-dir ange-ftp-this-file)
2479 (beginning-of-line)
2480 (ange-ftp-dl-parser))
2481 (t nil))))
2482
2483 (defun ange-ftp-set-files (directory files)
2484 "For a given DIRECTORY, set or change the associated FILES hashtable."
2485 (and files (ange-ftp-put-hash-entry (file-name-as-directory directory)
2486 files ange-ftp-files-hashtable)))
2487
2488 (defun ange-ftp-get-files (directory &optional no-error)
2489 "Given a given DIRECTORY, return a hashtable of file entries.
2490 This will give an error or return nil, depending on the value of
2491 NO-ERROR, if a listing for DIRECTORY cannot be obtained."
2492 (setq directory (file-name-as-directory directory)) ;normalize
2493 (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable)
2494 (ange-ftp-save-match-data
2495 (and (ange-ftp-ls directory
2496 ;; This is an efficiency hack. We try to
2497 ;; anticipate what sort of listing dired
2498 ;; might want, and cache just such a listing.
2499 (if (and (boundp 'dired-actual-switches)
2500 (stringp dired-actual-switches)
2501 ;; We allow the A switch, which lists
2502 ;; all files except "." and "..".
2503 ;; This is OK because we manually
2504 ;; insert these entries
2505 ;; in the hash table.
2506 (string-match
2507 "[aA]" dired-actual-switches)
2508 (string-match
2509 "l" dired-actual-switches)
2510 (not (string-match
2511 "R" dired-actual-switches)))
2512 dired-actual-switches
2513 (if (and (boundp 'dired-listing-switches)
2514 (stringp dired-listing-switches)
2515 (string-match
2516 "[aA]" dired-listing-switches)
2517 (string-match
2518 "l" dired-listing-switches)
2519 (not (string-match
2520 "R" dired-listing-switches)))
2521 dired-listing-switches
2522 "-al"))
2523 t no-error)
2524 (ange-ftp-get-hash-entry
2525 directory ange-ftp-files-hashtable)))))
2526
2527 ;; Given NAME, return the file part that can be used for looking up the
2528 ;; file's entry in a hashtable.
2529 (defmacro ange-ftp-get-file-part (name)
2530 (` (let ((file (file-name-nondirectory (, name))))
2531 (if (string-equal file "")
2532 "."
2533 file))))
2534
2535 ;; Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
2536 ;; allowed to determine if NAME is a sub-directory by listing it directly,
2537 ;; rather than listing its parent directory. This is used for efficiency so
2538 ;; that a wasted listing is not done:
2539 ;; 1. When looking for a .dired file in dired-x.el.
2540 ;; 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
2541 ;; subdirectory. This is of course an OS dependent judgement.
2542
2543 (defmacro ange-ftp-allow-child-lookup (dir file)
2544 (` (not
2545 (let* ((efile (, file)) ; expand once.
2546 (edir (, dir))
2547 (parsed (ange-ftp-ftp-name edir))
2548 (host-type (ange-ftp-host-type
2549 (car parsed))))
2550 (or
2551 ;;; This variable seems not to exist in Emacs 19 -- rms.
2552 ;;; ;; Deal with dired
2553 ;;; (and (boundp 'dired-local-variables-file)
2554 ;;; (stringp dired-local-variables-file)
2555 ;;; (string-equal dired-local-variables-file efile))
2556 ;; No dots in dir names in vms.
2557 (and (eq host-type 'vms)
2558 (string-match "\\." efile))
2559 ;; No subdirs in mts of cms.
2560 (and (memq host-type '(mts cms))
2561 (not (string-equal "/" (nth 2 parsed)))))))))
2562
2563 (defun ange-ftp-file-entry-p (name)
2564 "Given NAME, return whether there is a file entry for it."
2565 (let* ((name (directory-file-name name))
2566 (dir (file-name-directory name))
2567 (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
2568 (file (ange-ftp-get-file-part name)))
2569 (if ent
2570 (ange-ftp-hash-entry-exists-p file ent)
2571 (or (and (ange-ftp-allow-child-lookup dir file)
2572 (setq ent (ange-ftp-get-files name t))
2573 ;; Try a child lookup. i.e. try to list file as a
2574 ;; subdirectory of dir. This is a good idea because
2575 ;; we may not have read permission for file's parent. Also,
2576 ;; people tend to work down directory trees anyway. We use
2577 ;; no-error ;; because if file does not exist as a subdir.,
2578 ;; then dumb hosts will give an ftp error. Smart unix hosts
2579 ;; will simply send back the ls
2580 ;; error message.
2581 (ange-ftp-get-hash-entry "." ent))
2582 ;; Child lookup failed. Try the parent. If this bombs,
2583 ;; we are at wits end -- signal an error.
2584 ;; Problem: If this signals an error, the error message
2585 ;; may not have a lot to do with what went wrong.
2586 (ange-ftp-hash-entry-exists-p file
2587 (ange-ftp-get-files dir))))))
2588
2589 (defun ange-ftp-get-file-entry (name)
2590 "Given NAME, return the given file entry.
2591 The entry will be either t for a directory, nil for a normal file,
2592 or a string for a symlink. If the file isn't in the hashtable,
2593 this also returns nil."
2594 (let* ((name (directory-file-name name))
2595 (dir (file-name-directory name))
2596 (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
2597 (file (ange-ftp-get-file-part name)))
2598 (if ent
2599 (ange-ftp-get-hash-entry file ent)
2600 (or (and (ange-ftp-allow-child-lookup dir file)
2601 (setq ent (ange-ftp-get-files name t))
2602 (ange-ftp-get-hash-entry "." ent))
2603 ;; i.e. it's a directory by child lookup
2604 (ange-ftp-get-hash-entry file
2605 (ange-ftp-get-files dir))))))
2606
2607 (defun ange-ftp-internal-delete-file-entry (name &optional dir-p)
2608 (if dir-p
2609 (progn
2610 (setq name (file-name-as-directory name))
2611 (ange-ftp-del-hash-entry name ange-ftp-files-hashtable)
2612 (setq name (directory-file-name name))))
2613 ;; Note that file-name-as-directory followed by directory-file-name
2614 ;; serves to canonicalize directory file names to their unix form.
2615 ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
2616 (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
2617 ange-ftp-files-hashtable)))
2618 (if files
2619 (ange-ftp-del-hash-entry (ange-ftp-get-file-part name)
2620 files))))
2621
2622 (defun ange-ftp-internal-add-file-entry (name &optional dir-p)
2623 (and dir-p
2624 (setq name (directory-file-name name)))
2625 (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
2626 ange-ftp-files-hashtable)))
2627 (if files
2628 (ange-ftp-put-hash-entry (ange-ftp-get-file-part name)
2629 dir-p
2630 files))))
2631
2632 (defun ange-ftp-wipe-file-entries (host user)
2633 "Get rid of entry for HOST, USER pair from file entry information hashtable."
2634 (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable))))
2635 (ange-ftp-map-hashtable
2636 (function
2637 (lambda (key val)
2638 (let ((parsed (ange-ftp-ftp-name key)))
2639 (if parsed
2640 (let ((h (nth 0 parsed))
2641 (u (nth 1 parsed)))
2642 (or (and (equal host h) (equal user u))
2643 (ange-ftp-put-hash-entry key val new-tbl)))))))
2644 ange-ftp-files-hashtable)
2645 (setq ange-ftp-files-hashtable new-tbl)))
2646 \f
2647 ;;;; ------------------------------------------------------------
2648 ;;;; File transfer mode support.
2649 ;;;; ------------------------------------------------------------
2650
2651 (defun ange-ftp-set-binary-mode (host user)
2652 "Tell the ftp process for the given HOST & USER to switch to binary mode."
2653 (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
2654 (if (not (car result))
2655 (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
2656 (save-excursion
2657 (set-buffer (process-buffer (ange-ftp-get-process host user)))
2658 (setq ange-ftp-hash-mark-unit (ash ange-ftp-binary-hash-mark-size -4))))))
2659
2660 (defun ange-ftp-set-ascii-mode (host user)
2661 "Tell the ftp process for the given HOST & USER to switch to ascii mode."
2662 (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
2663 (if (not (car result))
2664 (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
2665 (save-excursion
2666 (set-buffer (process-buffer (ange-ftp-get-process host user)))
2667 (setq ange-ftp-hash-mark-unit (ash ange-ftp-ascii-hash-mark-size -4))))))
2668 \f
2669 (defun ange-ftp-cd (host user dir)
2670 (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
2671 (or (car result)
2672 (ange-ftp-error host user (concat "CD failed: " (cdr result))))))
2673
2674 (defun ange-ftp-get-pwd (host user)
2675 "Attempts to get the current working directory for the given HOST/USER pair.
2676 Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
2677 and LINE is the relevant success or fail line from the FTP-client."
2678 (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
2679 (line (cdr result))
2680 dir)
2681 (if (car result)
2682 (ange-ftp-save-match-data
2683 (and (or (string-match "\"\\([^\"]*\\)\"" line)
2684 (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
2685 (setq dir (substring line
2686 (match-beginning 1)
2687 (match-end 1))))))
2688 (cons dir line)))
2689 \f
2690 ;;; ------------------------------------------------------------
2691 ;;; expand-file-name and friends...which currently don't work
2692 ;;; ------------------------------------------------------------
2693
2694 (defun ange-ftp-expand-dir (host user dir)
2695 "Return the result of doing a PWD in the current FTP session.
2696 Use the connection to machine HOST
2697 logged in as user USER and cd'd to directory DIR."
2698 (let* ((host-type (ange-ftp-host-type host user))
2699 ;; It is more efficient to call ange-ftp-host-type
2700 ;; before binding res, because ange-ftp-host-type sometimes
2701 ;; adds to the info in the expand-dir-hashtable.
2702 (fix-name-func
2703 (cdr (assq host-type ange-ftp-fix-name-func-alist)))
2704 (key (concat host "/" user "/" dir))
2705 (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable)))
2706 (or res
2707 (progn
2708 (or
2709 (string-equal user "anonymous")
2710 (string-equal user "ftp")
2711 (not (eq host-type 'unix))
2712 (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
2713 "\\|"
2714 ange-ftp-good-msgs))
2715 (result (ange-ftp-send-cmd host user
2716 (list 'get dir "/dev/null")
2717 (format "expanding %s" dir)))
2718 (line (cdr result)))
2719 (setq res
2720 (if (string-match ange-ftp-expand-dir-regexp line)
2721 (substring line
2722 (match-beginning 1)
2723 (match-end 1))))))
2724 (or res
2725 (if (string-equal dir "~")
2726 (setq res (car (ange-ftp-get-pwd host user)))
2727 (let ((home (ange-ftp-expand-dir host user "~")))
2728 (unwind-protect
2729 (and (ange-ftp-cd host user dir)
2730 (setq res (car (ange-ftp-get-pwd host user))))
2731 (ange-ftp-cd host user home)))))
2732 (if res
2733 (let ((ange-ftp-this-user user)
2734 (ange-ftp-this-host host))
2735 (if fix-name-func
2736 (setq res (funcall fix-name-func res 'reverse)))
2737 (ange-ftp-put-hash-entry
2738 key res ange-ftp-expand-dir-hashtable)))
2739 res))))
2740
2741 (defun ange-ftp-canonize-filename (n)
2742 "Take a string and short-circuit //, /. and /.."
2743 (if (string-match ".+//" n) ;don't upset Apollo users
2744 (setq n (substring n (1- (match-end 0)))))
2745 (let ((parsed (ange-ftp-ftp-name n)))
2746 (if parsed
2747 (let ((host (car parsed))
2748 (user (nth 1 parsed))
2749 (name (nth 2 parsed)))
2750
2751 ;; See if remote name is absolute. If so then just expand it and
2752 ;; replace the name component of the overall name.
2753 (cond ((string-match "^/" name)
2754 name)
2755
2756 ;; Name starts with ~ or ~user. Resolve that part of the name
2757 ;; making it absolute then re-expand it.
2758 ((string-match "^~[^/]*" name)
2759 (let* ((tilda (substring name
2760 (match-beginning 0)
2761 (match-end 0)))
2762 (rest (substring name (match-end 0)))
2763 (dir (ange-ftp-expand-dir host user tilda)))
2764 (if dir
2765 (setq name (concat dir rest))
2766 (error "User \"%s\" is not known"
2767 (substring tilda 1)))))
2768
2769 ;; relative name. Tack on homedir and re-expand.
2770 (t
2771 (let ((dir (ange-ftp-expand-dir host user "~")))
2772 (if dir
2773 (setq name (concat
2774 (ange-ftp-real-file-name-as-directory dir)
2775 name))
2776 (error "Unable to obtain CWD")))))
2777
2778 (setq name (ange-ftp-real-expand-file-name name))
2779
2780 ;; see if hit real expand-file-name bug... this will probably annoy
2781 ;; some Apollo people. I'll wait until they shout, however.
2782 (if (string-match "^//" name)
2783 (setq name (substring name 1)))
2784
2785 ;; Now substitute the expanded name back into the overall filename.
2786 (ange-ftp-replace-name-component n name))
2787
2788 ;; non-ange-ftp name. Just expand normally.
2789 (if (eq (string-to-char n) ?/)
2790 (ange-ftp-real-expand-file-name n)
2791 (ange-ftp-real-expand-file-name
2792 (ange-ftp-real-file-name-nondirectory n)
2793 (ange-ftp-real-file-name-directory n))))))
2794
2795 (defun ange-ftp-expand-file-name (name &optional default)
2796 "Documented as original."
2797 (ange-ftp-save-match-data
2798 (if (eq (string-to-char name) ?/)
2799 (while (cond ((string-match ".+//" name) ;don't upset Apollo users
2800 (setq name (substring name (1- (match-end 0)))))
2801 ((string-match "/~" name)
2802 (setq name (substring name (1- (match-end 0))))))))
2803 (cond ((eq (string-to-char name) ?~)
2804 (ange-ftp-real-expand-file-name name))
2805 ((eq (string-to-char name) ?/)
2806 (ange-ftp-canonize-filename name))
2807 ((zerop (length name))
2808 (ange-ftp-canonize-filename (or default default-directory)))
2809 ((ange-ftp-canonize-filename
2810 (concat (file-name-as-directory (or default default-directory))
2811 name))))))
2812 \f
2813 ;;; These are problems--they are currently not enabled.
2814
2815 (defvar ange-ftp-file-name-as-directory-alist nil
2816 "Association list of \( TYPE \. FUNC \) pairs.
2817 FUNC converts a filename to a directory name for the operating
2818 system TYPE.")
2819
2820 (defun ange-ftp-file-name-as-directory (name)
2821 "Documented as original."
2822 (let ((parsed (ange-ftp-ftp-name name)))
2823 (if parsed
2824 (if (string-equal (nth 2 parsed) "")
2825 name
2826 (funcall (or (cdr (assq
2827 (ange-ftp-host-type (car parsed))
2828 ange-ftp-file-name-as-directory-alist))
2829 'ange-ftp-real-file-name-as-directory)
2830 name))
2831 (ange-ftp-real-file-name-as-directory name))))
2832
2833 (defun ange-ftp-file-name-directory (name)
2834 "Documented as original."
2835 (let ((parsed (ange-ftp-ftp-name name)))
2836 (if parsed
2837 (let ((filename (nth 2 parsed)))
2838 (if (ange-ftp-save-match-data
2839 (string-match "^~[^/]*$" filename))
2840 name
2841 (ange-ftp-replace-name-component
2842 name
2843 (ange-ftp-real-file-name-directory filename))))
2844 (ange-ftp-real-file-name-directory name))))
2845
2846 (defun ange-ftp-file-name-nondirectory (name)
2847 "Documented as original."
2848 (let ((parsed (ange-ftp-ftp-name name)))
2849 (if parsed
2850 (let ((filename (nth 2 parsed)))
2851 (if (ange-ftp-save-match-data
2852 (string-match "^~[^/]*$" filename))
2853 ""
2854 (ange-ftp-real-file-name-nondirectory name)))
2855 (ange-ftp-real-file-name-nondirectory name))))
2856
2857 (defun ange-ftp-directory-file-name (dir)
2858 "Documented as original."
2859 (let ((parsed (ange-ftp-ftp-name dir)))
2860 (if parsed
2861 (ange-ftp-replace-name-component
2862 dir
2863 (ange-ftp-real-directory-file-name (nth 2 parsed)))
2864 (ange-ftp-real-directory-file-name dir))))
2865
2866 \f
2867 ;;; Hooks that handle Emacs primitives.
2868
2869 ;; Returns non-nil if should transfer FILE in binary mode.
2870 (defun ange-ftp-binary-file (file)
2871 (ange-ftp-save-match-data
2872 (string-match ange-ftp-binary-file-name-regexp file)))
2873
2874 (defun ange-ftp-write-region (start end filename &optional append visit)
2875 (setq filename (expand-file-name filename))
2876 (let ((parsed (ange-ftp-ftp-name filename)))
2877 (if parsed
2878 (let* ((host (nth 0 parsed))
2879 (user (nth 1 parsed))
2880 (name (ange-ftp-quote-string (nth 2 parsed)))
2881 (temp (ange-ftp-make-tmp-name host))
2882 (binary (ange-ftp-binary-file filename))
2883 (cmd (if append 'append 'put))
2884 (abbr (ange-ftp-abbreviate-filename filename)))
2885 (unwind-protect
2886 (progn
2887 (let ((executing-macro t)
2888 (filename (buffer-file-name))
2889 (mod-p (buffer-modified-p)))
2890 (unwind-protect
2891 (ange-ftp-real-write-region start end temp nil visit)
2892 ;; cleanup forms
2893 (setq buffer-file-name filename)
2894 (set-buffer-modified-p mod-p)))
2895 (if binary
2896 (ange-ftp-set-binary-mode host user))
2897
2898 ;; tell the process filter what size the transfer will be.
2899 (let ((attr (file-attributes temp)))
2900 (if attr
2901 (ange-ftp-set-xfer-size host user (nth 7 attr))))
2902
2903 ;; put or append the file.
2904 (let ((result (ange-ftp-send-cmd host user
2905 (list cmd temp name)
2906 (format "Writing %s" abbr))))
2907 (or (car result)
2908 (signal 'ftp-error
2909 (list
2910 "Opening output file"
2911 (format "FTP Error: \"%s\"" (cdr result))
2912 filename)))))
2913 (ange-ftp-del-tmp-name temp)
2914 (if binary
2915 (ange-ftp-set-ascii-mode host user)))
2916 (if (eq visit t)
2917 (progn
2918 (set-visited-file-modtime '(0 0))
2919 (ange-ftp-set-buffer-mode)
2920 (setq buffer-file-name filename)
2921 (set-buffer-modified-p nil)))
2922 (ange-ftp-message "Wrote %s" abbr)
2923 (ange-ftp-add-file-entry filename))
2924 (ange-ftp-real-write-region start end filename append visit))))
2925
2926 (defun ange-ftp-insert-file-contents (filename &optional visit beg end replace)
2927 (barf-if-buffer-read-only)
2928 (setq filename (expand-file-name filename))
2929 (let ((parsed (ange-ftp-ftp-name filename)))
2930 (if parsed
2931 (progn
2932 (if visit
2933 (setq buffer-file-name filename))
2934 (if (or (file-exists-p filename)
2935 (progn
2936 (setq ange-ftp-ls-cache-file nil)
2937 (ange-ftp-del-hash-entry (file-name-directory filename)
2938 ange-ftp-files-hashtable)
2939 (file-exists-p filename)))
2940 (let* ((host (nth 0 parsed))
2941 (user (nth 1 parsed))
2942 (name (ange-ftp-quote-string (nth 2 parsed)))
2943 (temp (ange-ftp-make-tmp-name host))
2944 (binary (ange-ftp-binary-file filename))
2945 (abbr (ange-ftp-abbreviate-filename filename))
2946 size)
2947 (unwind-protect
2948 (progn
2949 (if binary
2950 (ange-ftp-set-binary-mode host user))
2951 (let ((result (ange-ftp-send-cmd host user
2952 (list 'get name temp)
2953 (format "Retrieving %s" abbr))))
2954 (or (car result)
2955 (signal 'ftp-error
2956 (list
2957 "Opening input file"
2958 (format "FTP Error: \"%s\"" (cdr result))
2959 filename))))
2960 (if (or (ange-ftp-real-file-readable-p temp)
2961 (sleep-for ange-ftp-retry-time)
2962 ;; Wait for file to hopefully appear.
2963 (ange-ftp-real-file-readable-p temp))
2964 (setq
2965 size
2966 (nth 1 (ange-ftp-real-insert-file-contents
2967 temp visit beg end replace)))
2968 (signal 'ftp-error
2969 (list
2970 "Opening input file:"
2971 (format
2972 "FTP Error: %s not arrived or readable"
2973 filename)))))
2974 (if binary
2975 (ange-ftp-set-ascii-mode host user))
2976 (ange-ftp-del-tmp-name temp))
2977 (if visit
2978 (progn
2979 (set-visited-file-modtime '(0 0))
2980 (setq buffer-file-name filename)))
2981 (list filename size))
2982 (signal 'file-error
2983 (list
2984 "Opening input file"
2985 filename))))
2986 (ange-ftp-real-insert-file-contents filename visit beg end replace))))
2987
2988 (defun ange-ftp-expand-symlink (file dir)
2989 (if (file-name-absolute-p file)
2990 (ange-ftp-replace-name-component dir file)
2991 (expand-file-name file dir)))
2992
2993 (defun ange-ftp-file-symlink-p (file)
2994 ;; call ange-ftp-expand-file-name rather than the normal
2995 ;; expand-file-name to stop loops when using a package that
2996 ;; redefines both file-symlink-p and expand-file-name.
2997 (setq file (ange-ftp-expand-file-name file))
2998 (if (ange-ftp-ftp-name file)
2999 (let ((file-ent
3000 (ange-ftp-get-hash-entry
3001 (ange-ftp-get-file-part file)
3002 (ange-ftp-get-files (file-name-directory file)))))
3003 (if (stringp file-ent)
3004 (if (file-name-absolute-p file-ent)
3005 (ange-ftp-replace-name-component
3006 (file-name-directory file) file-ent)
3007 file-ent)))
3008 (ange-ftp-real-file-symlink-p file)))
3009
3010 (defun ange-ftp-file-exists-p (name)
3011 (setq name (expand-file-name name))
3012 (if (ange-ftp-ftp-name name)
3013 (if (ange-ftp-file-entry-p name)
3014 (let ((file-ent (ange-ftp-get-file-entry name)))
3015 (if (stringp file-ent)
3016 (file-exists-p
3017 (ange-ftp-expand-symlink file-ent
3018 (file-name-directory
3019 (directory-file-name name))))
3020 t)))
3021 (ange-ftp-real-file-exists-p name)))
3022
3023 (defun ange-ftp-file-directory-p (name)
3024 (setq name (expand-file-name name))
3025 (if (ange-ftp-ftp-name name)
3026 ;; We do a file-name-as-directory on name here because some
3027 ;; machines (VMS) use a .DIR to indicate the filename associated
3028 ;; with a directory. This needs to be canonicalized.
3029 (let ((file-ent (ange-ftp-get-file-entry
3030 (ange-ftp-file-name-as-directory name))))
3031 (if (stringp file-ent)
3032 (file-directory-p
3033 (ange-ftp-expand-symlink file-ent
3034 (file-name-directory
3035 (directory-file-name name))))
3036 file-ent))
3037 (ange-ftp-real-file-directory-p name)))
3038
3039 (defun ange-ftp-directory-files (directory &optional full match
3040 &rest v19-args)
3041 (setq directory (expand-file-name directory))
3042 (if (ange-ftp-ftp-name directory)
3043 (progn
3044 (ange-ftp-barf-if-not-directory directory)
3045 (let ((tail (ange-ftp-hash-table-keys
3046 (ange-ftp-get-files directory)))
3047 files f)
3048 (setq directory (file-name-as-directory directory))
3049 (ange-ftp-save-match-data
3050 (while tail
3051 (setq f (car tail)
3052 tail (cdr tail))
3053 (if (or (not match) (string-match match f))
3054 (setq files
3055 (cons (if full (concat directory f) f) files)))))
3056 (nreverse files)))
3057 (apply 'ange-ftp-real-directory-files directory full match v19-args)))
3058
3059 (defun ange-ftp-file-attributes (file)
3060 (setq file (expand-file-name file))
3061 (let ((parsed (ange-ftp-ftp-name file)))
3062 (if parsed
3063 (let ((part (ange-ftp-get-file-part file))
3064 (files (ange-ftp-get-files (file-name-directory file))))
3065 (if (ange-ftp-hash-entry-exists-p part files)
3066 (let ((host (nth 0 parsed))
3067 (user (nth 1 parsed))
3068 (name (nth 2 parsed))
3069 (dirp (ange-ftp-get-hash-entry part files)))
3070 (list (if (and (stringp dirp) (file-name-absolute-p dirp))
3071 (ange-ftp-expand-symlink dirp
3072 (file-name-directory file))
3073 dirp) ;0 file type
3074 -1 ;1 link count
3075 -1 ;2 uid
3076 -1 ;3 gid
3077 '(0 0) ;4 atime
3078 '(0 0) ;5 mtime
3079 '(0 0) ;6 ctime
3080 -1 ;7 size
3081 (concat (if (stringp dirp) "l" (if dirp "d" "-"))
3082 "?????????") ;8 mode
3083 nil ;9 gid weird
3084 ;; Hack to give remote files a unique "inode number".
3085 ;; It's actually the sum of the characters in its name.
3086 (apply '+ (nconc (mapcar 'identity host)
3087 (mapcar 'identity user)
3088 (mapcar 'identity
3089 (directory-file-name name))))
3090 -1 ;11 device number [v19 only]
3091 ))))
3092 (ange-ftp-real-file-attributes file))))
3093
3094 (defun ange-ftp-file-writable-p (file)
3095 (setq file (expand-file-name file))
3096 (if (ange-ftp-ftp-name file)
3097 (or (file-exists-p file) ;guess here for speed
3098 (file-directory-p (file-name-directory file)))
3099 (ange-ftp-real-file-writable-p file)))
3100
3101 (defun ange-ftp-file-readable-p (file)
3102 (setq file (expand-file-name file))
3103 (if (ange-ftp-ftp-name file)
3104 (file-exists-p file)
3105 (ange-ftp-real-file-readable-p file)))
3106
3107 (defun ange-ftp-file-executable-p (file)
3108 (setq file (expand-file-name file))
3109 (if (ange-ftp-ftp-name file)
3110 (file-exists-p file)
3111 (ange-ftp-real-file-executable-p file)))
3112
3113 (defun ange-ftp-delete-file (file)
3114 (interactive "fDelete file: ")
3115 (setq file (expand-file-name file))
3116 (let ((parsed (ange-ftp-ftp-name file)))
3117 (if parsed
3118 (let* ((host (nth 0 parsed))
3119 (user (nth 1 parsed))
3120 (name (ange-ftp-quote-string (nth 2 parsed)))
3121 (abbr (ange-ftp-abbreviate-filename file))
3122 (result (ange-ftp-send-cmd host user
3123 (list 'delete name)
3124 (format "Deleting %s" abbr))))
3125 (or (car result)
3126 (signal 'ftp-error
3127 (list
3128 "Removing old name"
3129 (format "FTP Error: \"%s\"" (cdr result))
3130 file)))
3131 (ange-ftp-delete-file-entry file))
3132 (ange-ftp-real-delete-file file))))
3133
3134 (defun ange-ftp-verify-visited-file-modtime (buf)
3135 (let ((name (buffer-file-name buf)))
3136 (if (and (stringp name) (ange-ftp-ftp-name name))
3137 t
3138 (ange-ftp-real-verify-visited-file-modtime buf))))
3139 \f
3140 ;;;; ------------------------------------------------------------
3141 ;;;; File copying support... totally re-written 6/24/92.
3142 ;;;; ------------------------------------------------------------
3143
3144 (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
3145 (if (file-exists-p absname)
3146 (if (not interactive)
3147 (signal 'file-already-exists (list absname))
3148 (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
3149 absname querystring)))
3150 (signal 'file-already-exists (list absname))))))
3151
3152 ;; async local copy commented out for now since I don't seem to get
3153 ;; the process sentinel called for some processes.
3154 ;;
3155 ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
3156 ;; keep-date cont)
3157 ;; "Kludge to copy a local file and call a continuation when the copy
3158 ;; finishes."
3159 ;; ;; check to see if we can overwrite
3160 ;; (if (or (not ok-if-already-exists)
3161 ;; (numberp ok-if-already-exists))
3162 ;; (ange-ftp-barf-or-query-if-file-exists newname "copy to it"
3163 ;; (numberp ok-if-already-exists)))
3164 ;; (let ((proc (start-process " *copy*"
3165 ;; (generate-new-buffer "*copy*")
3166 ;; "cp"
3167 ;; filename
3168 ;; newname))
3169 ;; res)
3170 ;; (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel))
3171 ;; (process-kill-without-query proc)
3172 ;; (save-excursion
3173 ;; (set-buffer (process-buffer proc))
3174 ;; (make-variable-buffer-local 'copy-cont)
3175 ;; (setq copy-cont cont))))
3176 ;;
3177 ;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
3178 ;; (save-excursion
3179 ;; (set-buffer (process-buffer proc))
3180 ;; (let ((cont copy-cont)
3181 ;; (result (buffer-string)))
3182 ;; (unwind-protect
3183 ;; (if (and (string-equal status "finished\n")
3184 ;; (zerop (length result)))
3185 ;; (ange-ftp-call-cont cont t nil)
3186 ;; (ange-ftp-call-cont cont
3187 ;; nil
3188 ;; (if (zerop (length result))
3189 ;; (substring status 0 -1)
3190 ;; (substring result 0 -1))))
3191 ;; (kill-buffer (current-buffer))))))
3192
3193 ;; this is the extended version of ange-ftp-copy-file-internal that works
3194 ;; asynchronously if asked nicely.
3195 (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
3196 keep-date &optional msg cont nowait)
3197 (setq filename (expand-file-name filename)
3198 newname (expand-file-name newname))
3199
3200 ;; canonicalize newname if a directory.
3201 (if (file-directory-p newname)
3202 (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
3203
3204 (let ((f-parsed (ange-ftp-ftp-name filename))
3205 (t-parsed (ange-ftp-ftp-name newname)))
3206
3207 ;; local file to local file copy?
3208 (if (and (not f-parsed) (not t-parsed))
3209 (progn
3210 (ange-ftp-real-copy-file filename newname ok-if-already-exists
3211 keep-date)
3212 (if cont
3213 (ange-ftp-call-cont cont t "Copied locally")))
3214 ;; one or both files are remote.
3215 (let* ((f-host (and f-parsed (nth 0 f-parsed)))
3216 (f-user (and f-parsed (nth 1 f-parsed)))
3217 (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
3218 (f-abbr (ange-ftp-abbreviate-filename filename))
3219 (t-host (and t-parsed (nth 0 t-parsed)))
3220 (t-user (and t-parsed (nth 1 t-parsed)))
3221 (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
3222 (t-abbr (ange-ftp-abbreviate-filename newname filename))
3223 (binary (or (ange-ftp-binary-file filename)
3224 (ange-ftp-binary-file newname)))
3225 temp1
3226 temp2)
3227
3228 ;; check to see if we can overwrite
3229 (if (or (not ok-if-already-exists)
3230 (numberp ok-if-already-exists))
3231 (ange-ftp-barf-or-query-if-file-exists newname "copy to it"
3232 (numberp ok-if-already-exists)))
3233
3234 ;; do the copying.
3235 (if f-parsed
3236
3237 ;; filename was remote.
3238 (progn
3239 (if (or (ange-ftp-use-gateway-p f-host)
3240 t-parsed)
3241 ;; have to use intermediate file if we are getting via
3242 ;; gateway machine or we are doing a remote to remote copy.
3243 (setq temp1 (ange-ftp-make-tmp-name f-host)))
3244
3245 (if binary
3246 (ange-ftp-set-binary-mode f-host f-user))
3247
3248 (ange-ftp-send-cmd
3249 f-host
3250 f-user
3251 (list 'get f-name (or temp1 newname))
3252 (or msg
3253 (if (and temp1 t-parsed)
3254 (format "Getting %s" f-abbr)
3255 (format "Copying %s to %s" f-abbr t-abbr)))
3256 (list (function ange-ftp-cf1)
3257 filename newname binary msg
3258 f-parsed f-host f-user f-name f-abbr
3259 t-parsed t-host t-user t-name t-abbr
3260 temp1 temp2 cont nowait)
3261 nowait))
3262
3263 ;; filename wasn't remote. newname must be remote. call the
3264 ;; function which does the remainder of the copying work.
3265 (ange-ftp-cf1 t nil
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 nil nil cont nowait))))))
3270
3271 ;; next part of copying routine.
3272 (defun ange-ftp-cf1 (result line
3273 filename newname binary msg
3274 f-parsed f-host f-user f-name f-abbr
3275 t-parsed t-host t-user t-name t-abbr
3276 temp1 temp2 cont nowait)
3277 (if line
3278 ;; filename must have been remote, and we must have just done a GET.
3279 (unwind-protect
3280 (or result
3281 ;; GET failed for some reason. Clean up and get out.
3282 (progn
3283 (and temp1 (ange-ftp-del-tmp-name temp1))
3284 (or cont
3285 (signal 'ftp-error (list "Opening input file"
3286 (format "FTP Error: \"%s\"" line)
3287 filename)))))
3288 ;; cleanup
3289 (if binary
3290 (ange-ftp-set-ascii-mode f-host f-user))))
3291
3292 (if result
3293 ;; We now have to copy either temp1 or filename to newname.
3294 (if t-parsed
3295
3296 ;; newname was remote.
3297 (progn
3298 (if (ange-ftp-use-gateway-p t-host)
3299 (setq temp2 (ange-ftp-make-tmp-name t-host)))
3300
3301 ;; make sure data is moved into the right place for the
3302 ;; outgoing transfer. gateway temporary files complicate
3303 ;; things nicely.
3304 (if temp1
3305 (if temp2
3306 (if (string-equal temp1 temp2)
3307 (setq temp1 nil)
3308 (ange-ftp-real-copy-file temp1 temp2 t))
3309 (setq temp2 temp1 temp1 nil))
3310 (if temp2
3311 (ange-ftp-real-copy-file filename temp2 t)))
3312
3313 (if binary
3314 (ange-ftp-set-binary-mode t-host t-user))
3315
3316 ;; tell the process filter what size the file is.
3317 (let ((attr (file-attributes (or temp2 filename))))
3318 (if attr
3319 (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
3320
3321 (ange-ftp-send-cmd
3322 t-host
3323 t-user
3324 (list 'put (or temp2 filename) t-name)
3325 (or msg
3326 (if (and temp2 f-parsed)
3327 (format "Putting %s" newname)
3328 (format "Copying %s to %s" f-abbr t-abbr)))
3329 (list (function ange-ftp-cf2)
3330 newname t-host t-user binary temp1 temp2 cont)
3331 nowait))
3332
3333 ;; newname wasn't remote.
3334 (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
3335
3336 ;; first copy failed, tell caller
3337 (ange-ftp-call-cont cont result line)))
3338
3339 ;; last part of copying routine.
3340 (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
3341 (unwind-protect
3342 (if line
3343 ;; result from doing a local to remote copy.
3344 (unwind-protect
3345 (progn
3346 (or result
3347 (or cont
3348 (signal 'ftp-error
3349 (list "Opening output file"
3350 (format "FTP Error: \"%s\"" line)
3351 newname))))
3352
3353 (ange-ftp-add-file-entry newname))
3354
3355 ;; cleanup.
3356 (if binary
3357 (ange-ftp-set-ascii-mode t-host t-user)))
3358
3359 ;; newname was local.
3360 (if temp1
3361 (ange-ftp-real-copy-file temp1 newname t)))
3362
3363 ;; clean up
3364 (and temp1 (ange-ftp-del-tmp-name temp1))
3365 (and temp2 (ange-ftp-del-tmp-name temp2))
3366 (ange-ftp-call-cont cont result line)))
3367
3368 (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
3369 keep-date)
3370 (interactive "fCopy file: \nFCopy %s to file: \np")
3371 (ange-ftp-copy-file-internal filename
3372 newname
3373 ok-if-already-exists
3374 keep-date
3375 nil
3376 nil
3377 (interactive-p)))
3378 \f
3379 ;;;; ------------------------------------------------------------
3380 ;;;; File renaming support.
3381 ;;;; ------------------------------------------------------------
3382
3383 (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed
3384 binary)
3385 "Rename remote file FILE to remote file NEWNAME."
3386 (let ((f-host (nth 0 f-parsed))
3387 (f-user (nth 1 f-parsed))
3388 (t-host (nth 0 t-parsed))
3389 (t-user (nth 1 t-parsed)))
3390 (if (and (string-equal f-host t-host)
3391 (string-equal f-user t-user))
3392 (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed)))
3393 (t-name (ange-ftp-quote-string (nth 2 t-parsed)))
3394 (cmd (list 'rename f-name t-name))
3395 (fabbr (ange-ftp-abbreviate-filename filename))
3396 (nabbr (ange-ftp-abbreviate-filename newname filename))
3397 (result (ange-ftp-send-cmd f-host f-user cmd
3398 (format "Renaming %s to %s"
3399 fabbr
3400 nabbr))))
3401 (or (car result)
3402 (signal 'ftp-error
3403 (list
3404 "Renaming"
3405 (format "FTP Error: \"%s\"" (cdr result))
3406 filename
3407 newname)))
3408 (ange-ftp-add-file-entry newname)
3409 (ange-ftp-delete-file-entry filename))
3410 (ange-ftp-copy-file-internal filename newname t nil)
3411 (delete-file filename))))
3412
3413 (defun ange-ftp-rename-local-to-remote (filename newname)
3414 "Rename local FILENAME to remote file NEWNAME."
3415 (let* ((fabbr (ange-ftp-abbreviate-filename filename))
3416 (nabbr (ange-ftp-abbreviate-filename newname filename))
3417 (msg (format "Renaming %s to %s" fabbr nabbr)))
3418 (ange-ftp-copy-file-internal filename newname t nil msg)
3419 (let (ange-ftp-process-verbose)
3420 (delete-file filename))))
3421
3422 (defun ange-ftp-rename-remote-to-local (filename newname)
3423 "Rename remote file FILENAME to local file NEWNAME."
3424 (let* ((fabbr (ange-ftp-abbreviate-filename filename))
3425 (nabbr (ange-ftp-abbreviate-filename newname filename))
3426 (msg (format "Renaming %s to %s" fabbr nabbr)))
3427 (ange-ftp-copy-file-internal filename newname t nil msg)
3428 (let (ange-ftp-process-verbose)
3429 (delete-file filename))))
3430
3431 (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
3432 (interactive "fRename file: \nFRename %s to file: \np")
3433 (setq filename (expand-file-name filename))
3434 (setq newname (expand-file-name newname))
3435 (let* ((f-parsed (ange-ftp-ftp-name filename))
3436 (t-parsed (ange-ftp-ftp-name newname))
3437 (binary (if (or f-parsed t-parsed) (ange-ftp-binary-file filename))))
3438 (if (and (or f-parsed t-parsed)
3439 (or (not ok-if-already-exists)
3440 (numberp ok-if-already-exists)))
3441 (ange-ftp-barf-or-query-if-file-exists
3442 newname
3443 "rename to it"
3444 (numberp ok-if-already-exists)))
3445 (if f-parsed
3446 (if t-parsed
3447 (ange-ftp-rename-remote-to-remote filename newname f-parsed
3448 t-parsed binary)
3449 (ange-ftp-rename-remote-to-local filename newname))
3450 (if t-parsed
3451 (ange-ftp-rename-local-to-remote filename newname)
3452 (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
3453 \f
3454 ;;;; ------------------------------------------------------------
3455 ;;;; File name completion support.
3456 ;;;; ------------------------------------------------------------
3457
3458 ;; If the file entry SYM is a symlink, returns whether its file exists.
3459 ;; Note that `ange-ftp-this-dir' is used as a free variable.
3460 (defun ange-ftp-file-entry-active-p (sym)
3461 (let ((val (get sym 'val)))
3462 (or (not (stringp val))
3463 (file-exists-p (ange-ftp-expand-symlink val ange-ftp-this-dir)))))
3464
3465 ;; If the file entry is not a directory (nor a symlink pointing to a directory)
3466 ;; returns whether the file (or file pointed to by the symlink) is ignored
3467 ;; by completion-ignored-extensions.
3468 ;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern'
3469 ;; are used as free variables.
3470 (defun ange-ftp-file-entry-not-ignored-p (sym)
3471 (let ((val (get sym 'val))
3472 (symname (symbol-name sym)))
3473 (if (stringp val)
3474 (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir)))
3475 (or (file-directory-p file)
3476 (and (file-exists-p file)
3477 (not (string-match ange-ftp-completion-ignored-pattern
3478 symname)))))
3479 (or val ; is a directory name
3480 (not (string-match ange-ftp-completion-ignored-pattern symname))))))
3481
3482 (defun ange-ftp-file-name-all-completions (file dir)
3483 (let ((ange-ftp-this-dir (expand-file-name dir)))
3484 (if (ange-ftp-ftp-name ange-ftp-this-dir)
3485 (progn
3486 (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
3487 (setq ange-ftp-this-dir
3488 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))
3489 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
3490 (completions
3491 (all-completions file tbl
3492 (function ange-ftp-file-entry-active-p))))
3493
3494 ;; see whether each matching file is a directory or not...
3495 (mapcar
3496 (function
3497 (lambda (file)
3498 (let ((ent (ange-ftp-get-hash-entry file tbl)))
3499 (if (and ent
3500 (or (not (stringp ent))
3501 (file-directory-p
3502 (ange-ftp-expand-symlink ent
3503 ange-ftp-this-dir))))
3504 (concat file "/")
3505 file))))
3506 completions)))
3507
3508 (if (string-equal "/" ange-ftp-this-dir)
3509 (nconc (all-completions file (ange-ftp-generate-root-prefixes))
3510 (ange-ftp-real-file-name-all-completions file
3511 ange-ftp-this-dir))
3512 (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir)))))
3513
3514 (defun ange-ftp-file-name-completion (file dir)
3515 (let ((ange-ftp-this-dir (expand-file-name dir)))
3516 (if (ange-ftp-ftp-name ange-ftp-this-dir)
3517 (progn
3518 (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
3519 (if (equal file "")
3520 ""
3521 (setq ange-ftp-this-dir
3522 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) ;real?
3523 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
3524 (ange-ftp-completion-ignored-pattern
3525 (mapconcat (function
3526 (lambda (s) (if (stringp s)
3527 (concat (regexp-quote s) "$")
3528 "/"))) ; / never in filename
3529 completion-ignored-extensions
3530 "\\|")))
3531 (ange-ftp-save-match-data
3532 (or (ange-ftp-file-name-completion-1
3533 file tbl ange-ftp-this-dir
3534 (function ange-ftp-file-entry-not-ignored-p))
3535 (ange-ftp-file-name-completion-1
3536 file tbl ange-ftp-this-dir
3537 (function ange-ftp-file-entry-active-p)))))))
3538
3539 (if (string-equal "/" ange-ftp-this-dir)
3540 (try-completion
3541 file
3542 (nconc (ange-ftp-generate-root-prefixes)
3543 (mapcar 'list
3544 (ange-ftp-real-file-name-all-completions file "/"))))
3545 (ange-ftp-real-file-name-completion file ange-ftp-this-dir)))))
3546
3547
3548 (defun ange-ftp-file-name-completion-1 (file tbl dir predicate)
3549 (let ((bestmatch (try-completion file tbl predicate)))
3550 (if bestmatch
3551 (if (eq bestmatch t)
3552 (if (file-directory-p (expand-file-name file dir))
3553 (concat file "/")
3554 t)
3555 (if (and (eq (try-completion bestmatch tbl predicate) t)
3556 (file-directory-p
3557 (expand-file-name bestmatch dir)))
3558 (concat bestmatch "/")
3559 bestmatch)))))
3560
3561 ;; Put these lines uncommmented in your .emacs if you want C-r to refresh
3562 ;; ange-ftp's cache whilst doing filename completion.
3563 ;;
3564 ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
3565 ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
3566
3567 ;; Force a re-read of the directory DIR. If DIR is omitted then it defaults
3568 ;; to the directory part of the contents of the current buffer.
3569 (defun ange-ftp-re-read-dir (&optional dir)
3570 (interactive)
3571 (if dir
3572 (setq dir (expand-file-name dir))
3573 (setq dir (file-name-directory (expand-file-name (buffer-string)))))
3574 (if (ange-ftp-ftp-name dir)
3575 (progn
3576 (setq ange-ftp-ls-cache-file nil)
3577 (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable)
3578 (ange-ftp-get-files dir t))))
3579 \f
3580 (defun ange-ftp-make-directory (dir &optional parents)
3581 (interactive (list (expand-file-name (read-file-name "Make directory: "))))
3582 (if parents
3583 (let ((parent (file-name-directory (directory-file-name dir))))
3584 (or (file-exists-p parent)
3585 (ange-ftp-make-directory parent parents))))
3586 (if (file-exists-p dir)
3587 (error "Cannot make directory %s: file already exists" dir)
3588 (let ((parsed (ange-ftp-ftp-name dir)))
3589 (if parsed
3590 (let* ((host (nth 0 parsed))
3591 (user (nth 1 parsed))
3592 ;; Some ftp's on unix machines (at least on Suns)
3593 ;; insist that mkdir take a filename, and not a
3594 ;; directory-name name as an arg. Argh!! This is a bug.
3595 ;; Non-unix machines will probably always insist
3596 ;; that mkdir takes a directory-name as an arg
3597 ;; (as the ftp man page says it should).
3598 (name (ange-ftp-quote-string
3599 (if (eq (ange-ftp-host-type host) 'unix)
3600 (ange-ftp-real-directory-file-name (nth 2 parsed))
3601 (ange-ftp-real-file-name-as-directory
3602 (nth 2 parsed)))))
3603 (abbr (ange-ftp-abbreviate-filename dir))
3604 (result (ange-ftp-send-cmd host user
3605 (list 'mkdir name)
3606 (format "Making directory %s"
3607 abbr))))
3608 (or (car result)
3609 (ange-ftp-error host user
3610 (format "Could not make directory %s: %s"
3611 dir
3612 (cdr result))))
3613 (ange-ftp-add-file-entry dir t))
3614 (ange-ftp-real-make-directory dir)))))
3615
3616 (defun ange-ftp-delete-directory (dir)
3617 (if (file-directory-p dir)
3618 (let ((parsed (ange-ftp-ftp-name dir)))
3619 (if parsed
3620 (let* ((host (nth 0 parsed))
3621 (user (nth 1 parsed))
3622 ;; Some ftp's on unix machines (at least on Suns)
3623 ;; insist that rmdir take a filename, and not a
3624 ;; directory-name name as an arg. Argh!! This is a bug.
3625 ;; Non-unix machines will probably always insist
3626 ;; that rmdir takes a directory-name as an arg
3627 ;; (as the ftp man page says it should).
3628 (name (ange-ftp-quote-string
3629 (if (eq (ange-ftp-host-type host) 'unix)
3630 (ange-ftp-real-directory-file-name
3631 (nth 2 parsed))
3632 (ange-ftp-real-file-name-as-directory
3633 (nth 2 parsed)))))
3634 (abbr (ange-ftp-abbreviate-filename dir))
3635 (result (ange-ftp-send-cmd host user
3636 (list 'rmdir name)
3637 (format "Removing directory %s"
3638 abbr))))
3639 (or (car result)
3640 (ange-ftp-error host user
3641 (format "Could not remove directory %s: %s"
3642 dir
3643 (cdr result))))
3644 (ange-ftp-delete-file-entry dir t))
3645 (ange-ftp-real-delete-directory dir)))
3646 (error "Not a directory: %s" dir)))
3647 \f
3648 ;; Make a local copy of FILE and return its name.
3649
3650 (defun ange-ftp-file-local-copy (file)
3651 (let* ((fn1 (expand-file-name file))
3652 (pa1 (ange-ftp-ftp-name fn1)))
3653 (if pa1
3654 (let* ((tmp1 (ange-ftp-make-tmp-name (car pa1)))
3655 (bin1 (ange-ftp-binary-file fn1)))
3656 (ange-ftp-copy-file-internal fn1 tmp1 t nil
3657 (format "Getting %s" fn1))
3658 tmp1))))
3659
3660 (defun ange-ftp-load (file &optional noerror nomessage nosuffix)
3661 (if (ange-ftp-ftp-name file)
3662 (let ((tryfiles (if nosuffix
3663 (list file)
3664 (list (concat file ".elc") (concat file ".el") file)))
3665 copy)
3666 (while (and tryfiles (not copy))
3667 (condition-case error
3668 (setq copy (ange-ftp-file-local-copy (car tryfiles)))
3669 (ftp-error nil)))
3670 (if copy
3671 (unwind-protect
3672 (funcall 'load copy noerror nomessage nosuffix)
3673 (delete-file copy))
3674 (or noerror
3675 (signal 'file-error (list "Cannot open load file" file)))))
3676 (ange-ftp-real-load file noerror nomessage nosuffix)))
3677
3678 ;; Calculate default-unhandled-directory for a given ange-ftp buffer.
3679 (defun ange-ftp-unhandled-file-name-directory (filename)
3680 (file-name-directory ange-ftp-tmp-name-template))
3681
3682 \f
3683 ;; Need the following functions for making filenames of compressed
3684 ;; files, because some OS's (unlike UNIX) do not allow a filename to
3685 ;; have two extensions.
3686
3687 (defvar ange-ftp-make-compressed-filename-alist nil
3688 "Alist of host-type-specific functions to process file names for compression.
3689 Each element has the form (TYPE . FUNC).
3690 FUNC should take one argument, a file name, and return a list
3691 of the form (COMPRESSING NEWNAME).
3692 COMPRESSING should be t if the specified file should be compressed,
3693 and nil if it should be uncompressed (that is, if it is a compressed file).
3694 NEWNAME should be the name to give the new compressed or uncompressed file.")
3695
3696 (defun ange-ftp-dired-compress-file (name)
3697 (let ((parsed (ange-ftp-ftp-name name))
3698 conversion-func)
3699 (if (and parsed
3700 (setq conversion-func
3701 (cdr (assq (ange-ftp-host-type (car parsed))
3702 ange-ftp-make-compressed-filename-alist))))
3703 (let* ((decision
3704 (ange-ftp-save-match-data (funcall conversion-func name)))
3705 (compressing (car decision))
3706 (newfile (nth 1 decision)))
3707 (if compressing
3708 (ange-ftp-compress name newfile)
3709 (ange-ftp-uncompress name newfile)))
3710 (let (file-name-handler-alist)
3711 (dired-compress-file name)))))
3712
3713 ;; Copy FILE to this machine, compress it, and copy out to NFILE.
3714 (defun ange-ftp-compress (file nfile)
3715 (let* ((parsed (ange-ftp-ftp-name file))
3716 (tmp1 (ange-ftp-make-tmp-name (car parsed)))
3717 (tmp2 (ange-ftp-make-tmp-name (car parsed)))
3718 (abbr (ange-ftp-abbreviate-filename file))
3719 (nabbr (ange-ftp-abbreviate-filename nfile))
3720 (msg1 (format "Getting %s" abbr))
3721 (msg2 (format "Putting %s" nabbr)))
3722 (unwind-protect
3723 (progn
3724 (ange-ftp-copy-file-internal file tmp1 t nil msg1)
3725 (and ange-ftp-process-verbose
3726 (ange-ftp-message "Compressing %s..." abbr))
3727 (call-process-region (point)
3728 (point)
3729 shell-file-name
3730 nil
3731 t
3732 nil
3733 "-c"
3734 (format "compress -f -c < %s > %s" tmp1 tmp2))
3735 (and ange-ftp-process-verbose
3736 (ange-ftp-message "Compressing %s...done" abbr))
3737 (if (zerop (buffer-size))
3738 (progn
3739 (let (ange-ftp-process-verbose)
3740 (delete-file file))
3741 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
3742 (ange-ftp-del-tmp-name tmp1)
3743 (ange-ftp-del-tmp-name tmp2))))
3744
3745 ;; Copy FILE to this machine, uncompress it, and copy out to NFILE.
3746 (defun ange-ftp-uncompress (file nfile)
3747 (let* ((parsed (ange-ftp-ftp-name file))
3748 (tmp1 (ange-ftp-make-tmp-name (car parsed)))
3749 (tmp2 (ange-ftp-make-tmp-name (car parsed)))
3750 (abbr (ange-ftp-abbreviate-filename file))
3751 (nabbr (ange-ftp-abbreviate-filename nfile))
3752 (msg1 (format "Getting %s" abbr))
3753 (msg2 (format "Putting %s" nabbr))
3754 ;; ;; Cheap hack because of problems with binary file transfers from
3755 ;; ;; VMS hosts.
3756 ;; (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
3757 )
3758 (unwind-protect
3759 (progn
3760 (ange-ftp-copy-file-internal file tmp1 t nil msg1)
3761 (and ange-ftp-process-verbose
3762 (ange-ftp-message "Uncompressing %s..." abbr))
3763 (call-process-region (point)
3764 (point)
3765 shell-file-name
3766 nil
3767 t
3768 nil
3769 "-c"
3770 (format "uncompress -c < %s > %s" tmp1 tmp2))
3771 (and ange-ftp-process-verbose
3772 (ange-ftp-message "Uncompressing %s...done" abbr))
3773 (if (zerop (buffer-size))
3774 (progn
3775 (let (ange-ftp-process-verbose)
3776 (delete-file file))
3777 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
3778 (ange-ftp-del-tmp-name tmp1)
3779 (ange-ftp-del-tmp-name tmp2))))
3780 \f
3781 ;;; Define the handler for special file names
3782 ;;; that causes ange-ftp to be invoked.
3783
3784 ;;;###autoload
3785 (defun ange-ftp-hook-function (operation &rest args)
3786 (let ((fn (get operation 'ange-ftp)))
3787 (if fn (apply fn args)
3788 (ange-ftp-run-real-handler operation args))))
3789
3790
3791 ;;; This regexp takes care of real ange-ftp file names (with a slash
3792 ;;; and colon).
3793 ;;;###autoload
3794 (or (assoc "^/[^/:]*[^/:]:" file-name-handler-alist)
3795 (setq file-name-handler-alist
3796 (cons '("^/[^/:]*[^/:]:" . ange-ftp-hook-function)
3797 file-name-handler-alist)))
3798
3799 ;;; This regexp recognizes and absolute filenames with only one component,
3800 ;;; for the sake of hostname completion.
3801 ;;;###autoload
3802 (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
3803 (setq file-name-handler-alist
3804 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
3805 file-name-handler-alist)))
3806
3807 ;;; The above two forms are sufficient to cause this file to be loaded
3808 ;;; if the user ever uses a file name with a colon in it.
3809
3810 ;;; This sets the mode
3811 (or (memq 'ange-ftp-set-buffer-mode find-file-hooks)
3812 (setq find-file-hooks
3813 (cons 'ange-ftp-set-buffer-mode find-file-hooks)))
3814
3815 ;;; Now say where to find the handlers for particular operations.
3816
3817 (put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory)
3818 (put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory)
3819 (put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory)
3820 (put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name)
3821 (put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name)
3822 (put 'make-directory 'ange-ftp 'ange-ftp-make-directory)
3823 (put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory)
3824 (put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents)
3825 (put 'directory-files 'ange-ftp 'ange-ftp-directory-files)
3826 (put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p)
3827 (put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p)
3828 (put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p)
3829 (put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p)
3830 (put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p)
3831 (put 'delete-file 'ange-ftp 'ange-ftp-delete-file)
3832 (put 'read-file-name-internal 'ange-ftp 'ange-ftp-read-file-name-internal)
3833 (put 'verify-visited-file-modtime 'ange-ftp
3834 'ange-ftp-verify-visited-file-modtime)
3835 (put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p)
3836 (put 'write-region 'ange-ftp 'ange-ftp-write-region)
3837 (put 'backup-buffer 'ange-ftp 'ange-ftp-backup-buffer)
3838 (put 'copy-file 'ange-ftp 'ange-ftp-copy-file)
3839 (put 'rename-file 'ange-ftp 'ange-ftp-rename-file)
3840 (put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes)
3841 (put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions)
3842 (put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion)
3843 (put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory)
3844 (put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy)
3845 (put 'unhandled-file-name-directory 'ange-ftp
3846 'ange-ftp-unhandled-file-name-directory)
3847 (put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions)
3848 (put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache)
3849 (put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file)
3850 (put 'load 'ange-ftp 'ange-ftp-load)
3851
3852 ;; Turn off truename processing to save time.
3853 ;; Treat each name as its own truename.
3854 (put 'file-truename 'ange-ftp 'identity)
3855
3856 ;; Turn off RCS/SCCS processing to save time.
3857 ;; This returns nil for any file name as argument.
3858 (put 'vc-registered 'ange-ftp 'null)
3859 \f
3860 ;;; Define ways of getting at unmodified Emacs primitives,
3861 ;;; turning off our handler.
3862
3863 (defun ange-ftp-run-real-handler (operation args)
3864 (let ((inhibit-file-name-handlers
3865 (cons 'ange-ftp-hook-function
3866 (cons 'ange-ftp-completion-hook-function
3867 (and (eq inhibit-file-name-operation operation)
3868 inhibit-file-name-handlers))))
3869 (inhibit-file-name-operation operation))
3870 (apply operation args)))
3871
3872 (defun ange-ftp-real-file-name-directory (&rest args)
3873 (ange-ftp-run-real-handler 'file-name-directory args))
3874 (defun ange-ftp-real-file-name-nondirectory (&rest args)
3875 (ange-ftp-run-real-handler 'file-name-nondirectory args))
3876 (defun ange-ftp-real-file-name-as-directory (&rest args)
3877 (ange-ftp-run-real-handler 'file-name-as-directory args))
3878 (defun ange-ftp-real-directory-file-name (&rest args)
3879 (ange-ftp-run-real-handler 'directory-file-name args))
3880 (defun ange-ftp-real-expand-file-name (&rest args)
3881 (ange-ftp-run-real-handler 'expand-file-name args))
3882 (defun ange-ftp-real-make-directory (&rest args)
3883 (ange-ftp-run-real-handler 'make-directory args))
3884 (defun ange-ftp-real-delete-directory (&rest args)
3885 (ange-ftp-run-real-handler 'delete-directory args))
3886 (defun ange-ftp-real-insert-file-contents (&rest args)
3887 (ange-ftp-run-real-handler 'insert-file-contents args))
3888 (defun ange-ftp-real-directory-files (&rest args)
3889 (ange-ftp-run-real-handler 'directory-files args))
3890 (defun ange-ftp-real-file-directory-p (&rest args)
3891 (ange-ftp-run-real-handler 'file-directory-p args))
3892 (defun ange-ftp-real-file-writable-p (&rest args)
3893 (ange-ftp-run-real-handler 'file-writable-p args))
3894 (defun ange-ftp-real-file-readable-p (&rest args)
3895 (ange-ftp-run-real-handler 'file-readable-p args))
3896 (defun ange-ftp-real-file-executable-p (&rest args)
3897 (ange-ftp-run-real-handler 'file-executable-p args))
3898 (defun ange-ftp-real-file-symlink-p (&rest args)
3899 (ange-ftp-run-real-handler 'file-symlink-p args))
3900 (defun ange-ftp-real-delete-file (&rest args)
3901 (ange-ftp-run-real-handler 'delete-file args))
3902 (defun ange-ftp-real-read-file-name-internal (&rest args)
3903 (ange-ftp-run-real-handler 'read-file-name-internal args))
3904 (defun ange-ftp-real-verify-visited-file-modtime (&rest args)
3905 (ange-ftp-run-real-handler 'verify-visited-file-modtime args))
3906 (defun ange-ftp-real-file-exists-p (&rest args)
3907 (ange-ftp-run-real-handler 'file-exists-p args))
3908 (defun ange-ftp-real-write-region (&rest args)
3909 (ange-ftp-run-real-handler 'write-region args))
3910 (defun ange-ftp-real-backup-buffer (&rest args)
3911 (ange-ftp-run-real-handler 'backup-buffer args))
3912 (defun ange-ftp-real-copy-file (&rest args)
3913 (ange-ftp-run-real-handler 'copy-file args))
3914 (defun ange-ftp-real-rename-file (&rest args)
3915 (ange-ftp-run-real-handler 'rename-file args))
3916 (defun ange-ftp-real-file-attributes (&rest args)
3917 (ange-ftp-run-real-handler 'file-attributes args))
3918 (defun ange-ftp-real-file-name-all-completions (&rest args)
3919 (ange-ftp-run-real-handler 'file-name-all-completions args))
3920 (defun ange-ftp-real-file-name-completion (&rest args)
3921 (ange-ftp-run-real-handler 'file-name-completion args))
3922 (defun ange-ftp-real-insert-directory (&rest args)
3923 (ange-ftp-run-real-handler 'insert-directory args))
3924 (defun ange-ftp-real-file-name-sans-versions (&rest args)
3925 (ange-ftp-run-real-handler 'file-name-sans-versions args))
3926 (defun ange-ftp-real-shell-command (&rest args)
3927 (ange-ftp-run-real-handler 'shell-command args))
3928 (defun ange-ftp-real-load (&rest args)
3929 (ange-ftp-run-real-handler 'load args))
3930 \f
3931 ;; Here we support using dired on remote hosts.
3932 ;; I have turned off the support for using dired on foreign directory formats.
3933 ;; That involves too many unclean hooks.
3934 ;; It would be cleaner to support such operations by
3935 ;; converting the foreign directory format to something dired can understand;
3936 ;; something close to ls -l output.
3937 ;; The logical place to do this is in the functions ange-ftp-parse-...-listing.
3938
3939 ;; Some of the old dired hooks would still be needed even if this is done.
3940 ;; I have preserved (and modernized) those hooks.
3941 ;; So the format conversion should be all that is needed.
3942
3943 (defun ange-ftp-insert-directory (file switches &optional wildcard full)
3944 (let ((short (ange-ftp-abbreviate-filename file))
3945 (parsed (ange-ftp-ftp-name file)))
3946 (if parsed
3947 (insert
3948 (if wildcard
3949 (let ((default-directory (file-name-directory file)))
3950 (ange-ftp-ls (file-name-nondirectory file) switches nil nil t))
3951 (ange-ftp-ls file switches full)))
3952 (ange-ftp-real-insert-directory file switches wildcard full))))
3953
3954 (defun ange-ftp-dired-uncache (dir)
3955 (if (ange-ftp-ftp-name (expand-file-name dir))
3956 (setq ange-ftp-ls-cache-file nil)))
3957
3958 (defvar ange-ftp-sans-version-alist nil
3959 "Alist of mapping host type into function to remove file version numbers.")
3960
3961 (defun ange-ftp-file-name-sans-versions (file keep-backup-version)
3962 (setq file (ange-ftp-abbreviate-filename file))
3963 (let ((parsed (ange-ftp-ftp-name file))
3964 host-type func)
3965 (if parsed
3966 (setq host-type (ange-ftp-host-type (car parsed))
3967 func (cdr (assq (ange-ftp-host-type (car parsed))
3968 ange-ftp-sans-version-alist))))
3969 (if func (funcall func file keep-backup-version)
3970 (ange-ftp-real-file-name-sans-versions file keep-backup-version))))
3971
3972 (defvar ange-ftp-remote-shell-file-name
3973 (if (memq system-type '(hpux usg-unix-v)) ; hope that's right
3974 "remsh"
3975 "rsh")
3976 "Name of command to run a remote shell, for ange-ftp.")
3977
3978 ;;; This doesn't work yet; a new hook needs to be created.
3979 ;;; Maybe the new hook should be in call-process.
3980 (defun ange-ftp-shell-command (command)
3981 (let* ((parsed (ange-ftp-ftp-name default-directory))
3982 (host (nth 0 parsed))
3983 (user (nth 1 parsed))
3984 (name (nth 2 parsed)))
3985 (if (not parsed)
3986 (ange-ftp-real-shell-command command)
3987 (if (> (length name) 0) ; else it's $HOME
3988 (setq command (concat "cd " name "; " command)))
3989 (setq command
3990 (format "%s %s \"%s\"" ; remsh -l USER does not work well
3991 ; on a hp-ux machine I tried
3992 ange-ftp-remote-shell-file-name host command))
3993 (ange-ftp-message "Remote command '%s' ..." command)
3994 ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
3995 ;; would prepend "cd default-directory" --- which bombs because
3996 ;; default-directory is in ange-ftp syntax for remote file names.
3997 (ange-ftp-real-shell-command command))))
3998
3999 ;;; Thisis not hooked up yet.
4000 (defun ange-ftp-dired-call-process (program discard &rest arguments)
4001 ;; PROGRAM is always one of those below in the cond in dired.el.
4002 ;; The ARGUMENTS are (nearly) always files.
4003 (if (ange-ftp-ftp-name default-directory)
4004 ;; Can't use ange-ftp-dired-host-type here because the current
4005 ;; buffer is *dired-check-process output*
4006 (condition-case oops
4007 (cond ((equal "chmod" program)
4008 (ange-ftp-call-chmod arguments))
4009 ;; ((equal "chgrp" program))
4010 ;; ((equal dired-chown-program program))
4011 (t (error "Unknown remote command: %s" program)))
4012 (ftp-error (insert (format "%s: %s, %s\n"
4013 (nth 1 oops)
4014 (nth 2 oops)
4015 (nth 3 oops))))
4016 (error (insert (format "%s\n" (nth 1 oops)))))
4017 (apply 'call-process program nil (not discard) nil arguments)))
4018
4019 ;;; This currently does not work; it is never called.
4020 (defun ange-ftp-call-chmod (args)
4021 (if (< (length args) 2)
4022 (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
4023 (let ((mode (car args)))
4024 (mapcar
4025 (function
4026 (lambda (file)
4027 (setq file (expand-file-name file))
4028 (let ((parsed (ange-ftp-ftp-name file)))
4029 (if parsed
4030 (let* ((host (nth 0 parsed))
4031 (user (nth 1 parsed))
4032 (name (ange-ftp-quote-string (nth 2 parsed)))
4033 (abbr (ange-ftp-abbreviate-filename file))
4034 (result (ange-ftp-send-cmd host user
4035 (list 'chmod mode name)
4036 (format "doing chmod %s"
4037 abbr))))
4038 (or (car result)
4039 (ange-ftp-error host user
4040 (format "chmod: %s: \"%s\""
4041 file
4042 (cdr result)))))))))
4043 (cdr args)))
4044 (setq ange-ftp-ls-cache-file nil)) ;stop confusing dired
4045 \f
4046 ;;; This is turned off because it has nothing properly to do
4047 ;;; with dired. It could be reasonable to adapt this to
4048 ;;; replace ange-ftp-copy-file.
4049
4050 ;;;;; ------------------------------------------------------------
4051 ;;;;; Noddy support for async copy-file within dired.
4052 ;;;;; ------------------------------------------------------------
4053
4054 ;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
4055 ;; "Documented as original."
4056 ;; (dired-handle-overwrite to)
4057 ;; (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
4058 ;; cont nowait))
4059
4060 ;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
4061 ;; &optional marker-char op1
4062 ;; how-to)
4063 ;; "Documented as original."
4064 ;; ;; we need to let ange-ftp-dired-create-files know that we indirectly
4065 ;; ;; called it rather than somebody else.
4066 ;; (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
4067 ;; (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
4068 ;; arg marker-char op1 how-to)))
4069
4070 ;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
4071 ;; &optional marker-char)
4072 ;; "Documented as original."
4073 ;; (if (and (boundp 'ange-ftp-dired-do-create-files)
4074 ;; ;; called from ange-ftp-dired-do-create-files?
4075 ;; ange-ftp-dired-do-create-files
4076 ;; ;; any files worth copying?
4077 ;; fn-list
4078 ;; ;; we only support async copy-file at the mo.
4079 ;; (eq file-creator 'dired-copy-file)
4080 ;; ;; it is only worth calling the alternative function for remote files
4081 ;; ;; as we tie ourself in recursive knots otherwise.
4082 ;; (or (ange-ftp-ftp-name (car fn-list))
4083 ;; ;; we can only call the name constructor for dired-do-create-files
4084 ;; ;; since the one for regexps starts prompting here, there and
4085 ;; ;; everywhere.
4086 ;; (ange-ftp-ftp-name (funcall name-constructor (car fn-list)))))
4087 ;; ;; use the process-filter driven routine rather than the iterative one.
4088 ;; (ange-ftp-dcf-1 file-creator
4089 ;; operation
4090 ;; fn-list
4091 ;; name-constructor
4092 ;; (and (boundp 'target) target) ;dynamically bound
4093 ;; marker-char
4094 ;; (current-buffer)
4095 ;; nil ;overwrite-query
4096 ;; nil ;overwrite-backup-query
4097 ;; nil ;failures
4098 ;; nil ;skipped
4099 ;; 0 ;success-count
4100 ;; (length fn-list) ;total
4101 ;; )
4102 ;; ;; normal case... use the interactive routine... much cheaper.
4103 ;; (ange-ftp-real-dired-create-files file-creator operation fn-list
4104 ;; name-constructor marker-char)))
4105
4106 ;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
4107 ;; target marker-char buffer overwrite-query
4108 ;; overwrite-backup-query failures skipped
4109 ;; success-count total)
4110 ;; (let ((old-buf (current-buffer)))
4111 ;; (unwind-protect
4112 ;; (progn
4113 ;; (set-buffer buffer)
4114 ;; (if (null fn-list)
4115 ;; (ange-ftp-dcf-3 failures operation total skipped
4116 ;; success-count buffer)
4117
4118 ;; (let* ((from (car fn-list))
4119 ;; (to (funcall name-constructor from)))
4120 ;; (if (equal to from)
4121 ;; (progn
4122 ;; (setq to nil)
4123 ;; (dired-log "Cannot %s to same file: %s\n"
4124 ;; (downcase operation) from)))
4125 ;; (if (not to)
4126 ;; (ange-ftp-dcf-1 file-creator
4127 ;; operation
4128 ;; (cdr fn-list)
4129 ;; name-constructor
4130 ;; target
4131 ;; marker-char
4132 ;; buffer
4133 ;; overwrite-query
4134 ;; overwrite-backup-query
4135 ;; failures
4136 ;; (cons (dired-make-relative from) skipped)
4137 ;; success-count
4138 ;; total)
4139 ;; (let* ((overwrite (file-exists-p to))
4140 ;; (overwrite-confirmed ; for dired-handle-overwrite
4141 ;; (and overwrite
4142 ;; (let ((help-form '(format "\
4143 ;;Type SPC or `y' to overwrite file `%s',
4144 ;;DEL or `n' to skip to next,
4145 ;;ESC or `q' to not overwrite any of the remaining files,
4146 ;;`!' to overwrite all remaining files with no more questions." to)))
4147 ;; (dired-query 'overwrite-query
4148 ;; "Overwrite `%s'?" to))))
4149 ;; ;; must determine if FROM is marked before file-creator
4150 ;; ;; gets a chance to delete it (in case of a move).
4151 ;; (actual-marker-char
4152 ;; (cond ((integerp marker-char) marker-char)
4153 ;; (marker-char (dired-file-marker from)) ; slow
4154 ;; (t nil))))
4155 ;; (condition-case err
4156 ;; (funcall file-creator from to overwrite-confirmed
4157 ;; (list (function ange-ftp-dcf-2)
4158 ;; nil ;err
4159 ;; file-creator operation fn-list
4160 ;; name-constructor
4161 ;; target
4162 ;; marker-char actual-marker-char
4163 ;; buffer to from
4164 ;; overwrite
4165 ;; overwrite-confirmed
4166 ;; overwrite-query
4167 ;; overwrite-backup-query
4168 ;; failures skipped success-count
4169 ;; total)
4170 ;; t)
4171 ;; (file-error ; FILE-CREATOR aborted
4172 ;; (ange-ftp-dcf-2 nil ;result
4173 ;; nil ;line
4174 ;; err
4175 ;; file-creator operation fn-list
4176 ;; name-constructor
4177 ;; target
4178 ;; marker-char actual-marker-char
4179 ;; buffer to from
4180 ;; overwrite
4181 ;; overwrite-confirmed
4182 ;; overwrite-query
4183 ;; overwrite-backup-query
4184 ;; failures skipped success-count
4185 ;; total))))))))
4186 ;; (set-buffer old-buf))))
4187
4188 ;;(defun ange-ftp-dcf-2 (result line err
4189 ;; file-creator operation fn-list
4190 ;; name-constructor
4191 ;; target
4192 ;; marker-char actual-marker-char
4193 ;; buffer to from
4194 ;; overwrite
4195 ;; overwrite-confirmed
4196 ;; overwrite-query
4197 ;; overwrite-backup-query
4198 ;; failures skipped success-count
4199 ;; total)
4200 ;; (let ((old-buf (current-buffer)))
4201 ;; (unwind-protect
4202 ;; (progn
4203 ;; (set-buffer buffer)
4204 ;; (if (or err (not result))
4205 ;; (progn
4206 ;; (setq failures (cons (dired-make-relative from) failures))
4207 ;; (dired-log "%s `%s' to `%s' failed:\n%s\n"
4208 ;; operation from to (or err line)))
4209 ;; (if overwrite
4210 ;; ;; If we get here, file-creator hasn't been aborted
4211 ;; ;; and the old entry (if any) has to be deleted
4212 ;; ;; before adding the new entry.
4213 ;; (dired-remove-file to))
4214 ;; (setq success-count (1+ success-count))
4215 ;; (message "%s: %d of %d" operation success-count total)
4216 ;; (dired-add-file to actual-marker-char))
4217
4218 ;; (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
4219 ;; name-constructor
4220 ;; target
4221 ;; marker-char
4222 ;; buffer
4223 ;; overwrite-query
4224 ;; overwrite-backup-query
4225 ;; failures skipped success-count
4226 ;; total))
4227 ;; (set-buffer old-buf))))
4228
4229 ;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count
4230 ;; buffer)
4231 ;; (let ((old-buf (current-buffer)))
4232 ;; (unwind-protect
4233 ;; (progn
4234 ;; (set-buffer buffer)
4235 ;; (cond
4236 ;; (failures
4237 ;; (dired-log-summary
4238 ;; (message "%s failed for %d of %d file%s %s"
4239 ;; operation (length failures) total
4240 ;; (dired-plural-s total) failures)))
4241 ;; (skipped
4242 ;; (dired-log-summary
4243 ;; (message "%s: %d of %d file%s skipped %s"
4244 ;; operation (length skipped) total
4245 ;; (dired-plural-s total) skipped)))
4246 ;; (t
4247 ;; (message "%s: %s file%s."
4248 ;; operation success-count (dired-plural-s success-count))))
4249 ;; (dired-move-to-filename))
4250 ;; (set-buffer old-buf))))
4251 \f
4252 ;;;; -----------------------------------------------
4253 ;;;; Unix Descriptive Listing (dl) Support
4254 ;;;; -----------------------------------------------
4255
4256 ;; This is turned off because nothing uses it currently
4257 ;; and because I don't understand what it's supposed to be for. --rms.
4258
4259 ;;(defconst ange-ftp-dired-dl-re-dir
4260 ;; "^. [^ /]+/[ \n]"
4261 ;; "Regular expression to use to search for dl directories.")
4262
4263 ;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
4264 ;; (setq ange-ftp-dired-re-dir-alist
4265 ;; (cons (cons 'unix:dl ange-ftp-dired-dl-re-dir)
4266 ;; ange-ftp-dired-re-dir-alist)))
4267
4268 ;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
4269 ;; "In dired, move to the first character of the filename on this line."
4270 ;; ;; This is the Unix dl version.
4271 ;; (or eol (setq eol (progn (end-of-line) (point))))
4272 ;; (let (case-fold-search)
4273 ;; (beginning-of-line)
4274 ;; (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
4275 ;; (goto-char (+ (point) 2))
4276 ;; (if raise-error
4277 ;; (error "No file on this line")
4278 ;; nil))))
4279
4280 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
4281 ;; (setq ange-ftp-dired-move-to-filename-alist
4282 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
4283 ;; ange-ftp-dired-move-to-filename-alist)))
4284
4285 ;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
4286 ;; ;; Assumes point is at beginning of filename.
4287 ;; ;; So, it should be called only after (dired-move-to-filename t).
4288 ;; ;; On failure, signals an error or returns nil.
4289 ;; ;; This is the Unix dl version.
4290 ;; (let ((opoint (point))
4291 ;; case-fold-search hidden)
4292 ;; (or eol (setq eol (save-excursion (end-of-line) (point))))
4293 ;; (setq hidden (and selective-display
4294 ;; (save-excursion
4295 ;; (search-forward "\r" eol t))))
4296 ;; (if hidden
4297 ;; (if no-error
4298 ;; nil
4299 ;; (error
4300 ;; (substitute-command-keys
4301 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide")))
4302 ;; (skip-chars-forward "^ /" eol)
4303 ;; (if (eq opoint (point))
4304 ;; (if no-error
4305 ;; nil
4306 ;; (error "No file on this line"))
4307 ;; (point)))))
4308
4309 ;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
4310 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist
4311 ;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
4312 ;; ange-ftp-dired-move-to-end-of-filename-alist)))
4313 \f
4314 ;;;; ------------------------------------------------------------
4315 ;;;; VOS support (VOS support is probably broken,
4316 ;;;; but I don't know anything about VOS.)
4317 ;;;; ------------------------------------------------------------
4318 ;
4319 ;(defun ange-ftp-fix-name-for-vos (name &optional reverse)
4320 ; (setq name (copy-sequence name))
4321 ; (let ((from (if reverse ?\> ?\/))
4322 ; (to (if reverse ?\/ ?\>))
4323 ; (i (1- (length name))))
4324 ; (while (>= i 0)
4325 ; (if (= (aref name i) from)
4326 ; (aset name i to))
4327 ; (setq i (1- i)))
4328 ; name))
4329 ;
4330 ;(or (assq 'vos ange-ftp-fix-name-func-alist)
4331 ; (setq ange-ftp-fix-name-func-alist
4332 ; (cons '(vos . ange-ftp-fix-name-for-vos)
4333 ; ange-ftp-fix-name-func-alist)))
4334 ;
4335 ;(or (memq 'vos ange-ftp-dumb-host-types)
4336 ; (setq ange-ftp-dumb-host-types
4337 ; (cons 'vos ange-ftp-dumb-host-types)))
4338 ;
4339 ;(defun ange-ftp-fix-dir-name-for-vos (dir-name)
4340 ; (ange-ftp-fix-name-for-vos
4341 ; (concat dir-name
4342 ; (if (eq ?/ (aref dir-name (1- (length dir-name))))
4343 ; "" "/")
4344 ; "*")))
4345 ;
4346 ;(or (assq 'vos ange-ftp-fix-dir-name-func-alist)
4347 ; (setq ange-ftp-fix-dir-name-func-alist
4348 ; (cons '(vos . ange-ftp-fix-dir-name-for-vos)
4349 ; ange-ftp-fix-dir-name-func-alist)))
4350 ;
4351 ;(defvar ange-ftp-vos-host-regexp nil
4352 ; "If a host matches this regexp then it is assumed to be running VOS.")
4353 ;
4354 ;(defun ange-ftp-vos-host (host)
4355 ; (and ange-ftp-vos-host-regexp
4356 ; (ange-ftp-save-match-data
4357 ; (string-match ange-ftp-vos-host-regexp host))))
4358 ;
4359 ;(defun ange-ftp-parse-vos-listing ()
4360 ; "Parse the current buffer which is assumed to be in VOS list -all
4361 ;format, and return a hashtable as the result."
4362 ; (let ((tbl (ange-ftp-make-hashtable))
4363 ; (type-list
4364 ; '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
4365 ; ("^Dirs: [0-9]+\n+" t 30)))
4366 ; type-regexp type-is-dir type-col file)
4367 ; (goto-char (point-min))
4368 ; (ange-ftp-save-match-data
4369 ; (while type-list
4370 ; (setq type-regexp (car (car type-list))
4371 ; type-is-dir (nth 1 (car type-list))
4372 ; type-col (nth 2 (car type-list))
4373 ; type-list (cdr type-list))
4374 ; (if (re-search-forward type-regexp nil t)
4375 ; (while (eq (char-after (point)) ? )
4376 ; (move-to-column type-col)
4377 ; (setq file (buffer-substring (point)
4378 ; (progn
4379 ; (end-of-line 1)
4380 ; (point))))
4381 ; (ange-ftp-put-hash-entry file type-is-dir tbl)
4382 ; (forward-line 1))))
4383 ; (ange-ftp-put-hash-entry "." 'vosdir tbl)
4384 ; (ange-ftp-put-hash-entry ".." 'vosdir tbl))
4385 ; tbl))
4386 ;
4387 ;(or (assq 'vos ange-ftp-parse-list-func-alist)
4388 ; (setq ange-ftp-parse-list-func-alist
4389 ; (cons '(vos . ange-ftp-parse-vos-listing)
4390 ; ange-ftp-parse-list-func-alist)))
4391 \f
4392 ;;;; ------------------------------------------------------------
4393 ;;;; VMS support.
4394 ;;;; ------------------------------------------------------------
4395
4396 ;; Convert NAME from UNIX-ish to VMS. If REVERSE given then convert from VMS
4397 ;; to UNIX-ish.
4398 (defun ange-ftp-fix-name-for-vms (name &optional reverse)
4399 (ange-ftp-save-match-data
4400 (if reverse
4401 (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name)
4402 (let (drive dir file)
4403 (if (match-beginning 1)
4404 (setq drive (substring name
4405 (match-beginning 1)
4406 (match-end 1))))
4407 (if (match-beginning 2)
4408 (setq dir
4409 (substring name (match-beginning 2) (match-end 2))))
4410 (if (match-beginning 3)
4411 (setq file
4412 (substring name (match-beginning 3) (match-end 3))))
4413 (and dir
4414 (setq dir (apply (function concat)
4415 (mapcar (function
4416 (lambda (char)
4417 (if (= char ?.)
4418 (vector ?/)
4419 (vector char))))
4420 (substring dir 1 -1)))))
4421 (concat (and drive
4422 (concat "/" drive "/"))
4423 dir (and dir "/")
4424 file))
4425 (error "name %s didn't match" name))
4426 (let (drive dir file tmp)
4427 (if (string-match "^/[^:]+:/" name)
4428 (setq drive (substring name 1
4429 (1- (match-end 0)))
4430 name (substring name (match-end 0))))
4431 (setq tmp (file-name-directory name))
4432 (if tmp
4433 (setq dir (apply (function concat)
4434 (mapcar (function
4435 (lambda (char)
4436 (if (= char ?/)
4437 (vector ?.)
4438 (vector char))))
4439 (substring tmp 0 -1)))))
4440 (setq file (file-name-nondirectory name))
4441 (concat drive
4442 (and dir (concat "[" (if drive nil ".") dir "]"))
4443 file)))))
4444
4445 ;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
4446 ;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
4447
4448 (or (assq 'vms ange-ftp-fix-name-func-alist)
4449 (setq ange-ftp-fix-name-func-alist
4450 (cons '(vms . ange-ftp-fix-name-for-vms)
4451 ange-ftp-fix-name-func-alist)))
4452
4453 (or (memq 'vms ange-ftp-dumb-host-types)
4454 (setq ange-ftp-dumb-host-types
4455 (cons 'vms ange-ftp-dumb-host-types)))
4456
4457 ;; It is important that this function barf for directories for which we know
4458 ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
4459 ;; This is because it saves an unnecessary FTP error, or possibly the listing
4460 ;; might succeed, but give erroneous info. This last case is particularly
4461 ;; likely for OS's (like MTS) for which we need to use a wildcard in order
4462 ;; to list a directory.
4463
4464 ;; Convert name from UNIX-ish to VMS ready for a DIRectory listing.
4465 (defun ange-ftp-fix-dir-name-for-vms (dir-name)
4466 ;; Should there be entries for .. -> [-] and . -> [] below. Don't
4467 ;; think so, because expand-filename should have already short-circuited
4468 ;; them.
4469 (cond ((string-equal dir-name "/")
4470 (error "Cannot get listing for fictitious \"/\" directory."))
4471 ((string-match "^/[-A-Z0-9_$]+:/$" dir-name)
4472 (error "Cannot get listing for device."))
4473 ((ange-ftp-fix-name-for-vms dir-name))))
4474
4475 (or (assq 'vms ange-ftp-fix-dir-name-func-alist)
4476 (setq ange-ftp-fix-dir-name-func-alist
4477 (cons '(vms . ange-ftp-fix-dir-name-for-vms)
4478 ange-ftp-fix-dir-name-func-alist)))
4479
4480 (defvar ange-ftp-vms-host-regexp nil)
4481
4482 ;; Return non-nil if HOST is running VMS.
4483 (defun ange-ftp-vms-host (host)
4484 (and ange-ftp-vms-host-regexp
4485 (ange-ftp-save-match-data
4486 (string-match ange-ftp-vms-host-regexp host))))
4487
4488 ;; Because some VMS ftp servers convert filenames to lower case
4489 ;; we allow a-z in the filename regexp. I'm not too happy about this.
4490
4491 (defconst ange-ftp-vms-filename-regexp
4492 (concat
4493 "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\."
4494 "[-_A-Za-z0-9$]*;+[0-9]*\\)")
4495 "Regular expression to match for a valid VMS file name in Dired buffer.
4496 Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
4497 Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
4498 Other orders of $ and _ seem to all work just fine.")
4499
4500 ;; These parsing functions are as general as possible because the syntax
4501 ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
4502 ;; the VMS filename syntax is so rigid. If they bomb on a listing in the
4503 ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
4504 ;; from vms.weird.net, then too bad.
4505
4506 ;; Extract the next filename from a VMS dired-like listing.
4507 (defun ange-ftp-parse-vms-filename ()
4508 (if (re-search-forward
4509 ange-ftp-vms-filename-regexp
4510 nil t)
4511 (buffer-substring (match-beginning 0) (match-end 0))))
4512
4513 ;; Parse the current buffer which is assumed to be in MultiNet FTP dir
4514 ;; format, and return a hashtable as the result.
4515 (defun ange-ftp-parse-vms-listing ()
4516 (let ((tbl (ange-ftp-make-hashtable))
4517 file)
4518 (goto-char (point-min))
4519 (ange-ftp-save-match-data
4520 (while (setq file (ange-ftp-parse-vms-filename))
4521 (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
4522 ;; deal with directories
4523 (ange-ftp-put-hash-entry
4524 (substring file 0 (match-beginning 0)) t tbl)
4525 (ange-ftp-put-hash-entry file nil tbl)
4526 (if (string-match ";[0-9]+$" file) ; deal with extension
4527 ;; sans extension
4528 (ange-ftp-put-hash-entry
4529 (substring file 0 (match-beginning 0)) nil tbl)))
4530 (forward-line 1))
4531 ;; Would like to look for a "Total" line, or a "Directory" line to
4532 ;; make sure that the listing isn't complete garbage before putting
4533 ;; in "." and "..", but we can't even count on all VAX's giving us
4534 ;; either of these.
4535 (ange-ftp-put-hash-entry "." t tbl)
4536 (ange-ftp-put-hash-entry ".." t tbl))
4537 tbl))
4538
4539 (or (assq 'vms ange-ftp-parse-list-func-alist)
4540 (setq ange-ftp-parse-list-func-alist
4541 (cons '(vms . ange-ftp-parse-vms-listing)
4542 ange-ftp-parse-list-func-alist)))
4543
4544 ;; This version only deletes file entries which have
4545 ;; explicit version numbers, because that is all VMS allows.
4546
4547 ;; Can the following two functions be speeded up using file
4548 ;; completion functions?
4549
4550 (defun ange-ftp-vms-delete-file-entry (name &optional dir-p)
4551 (if dir-p
4552 (ange-ftp-internal-delete-file-entry name t)
4553 (ange-ftp-save-match-data
4554 (let ((file (ange-ftp-get-file-part name)))
4555 (if (string-match ";[0-9]+$" file)
4556 ;; In VMS you can't delete a file without an explicit
4557 ;; version number, or wild-card (e.g. FOO;*)
4558 ;; For now, we give up on wildcards.
4559 (let ((files (ange-ftp-get-hash-entry
4560 (file-name-directory name)
4561 ange-ftp-files-hashtable)))
4562 (if files
4563 (let* ((root (substring file 0
4564 (match-beginning 0)))
4565 (regexp (concat "^"
4566 (regexp-quote root)
4567 ";[0-9]+$"))
4568 versions)
4569 (ange-ftp-del-hash-entry file files)
4570 ;; Now we need to check if there are any
4571 ;; versions left. If not, then delete the
4572 ;; root entry.
4573 (mapatoms
4574 '(lambda (sym)
4575 (and (string-match regexp (get sym 'key))
4576 (setq versions t)))
4577 files)
4578 (or versions
4579 (ange-ftp-del-hash-entry root files))))))))))
4580
4581 (or (assq 'vms ange-ftp-delete-file-entry-alist)
4582 (setq ange-ftp-delete-file-entry-alist
4583 (cons '(vms . ange-ftp-vms-delete-file-entry)
4584 ange-ftp-delete-file-entry-alist)))
4585
4586 (defun ange-ftp-vms-add-file-entry (name &optional dir-p)
4587 (if dir-p
4588 (ange-ftp-internal-add-file-entry name t)
4589 (let ((files (ange-ftp-get-hash-entry
4590 (file-name-directory name)
4591 ange-ftp-files-hashtable)))
4592 (if files
4593 (let ((file (ange-ftp-get-file-part name)))
4594 (ange-ftp-save-match-data
4595 (if (string-match ";[0-9]+$" file)
4596 (ange-ftp-put-hash-entry
4597 (substring file 0 (match-beginning 0))
4598 nil files)
4599 ;; Need to figure out what version of the file
4600 ;; is being added.
4601 (let ((regexp (concat "^"
4602 (regexp-quote file)
4603 ";\\([0-9]+\\)$"))
4604 (version 0))
4605 (mapatoms
4606 '(lambda (sym)
4607 (let ((name (get sym 'key)))
4608 (and (string-match regexp name)
4609 (setq version
4610 (max version
4611 (string-to-int
4612 (substring name
4613 (match-beginning 1)
4614 (match-end 1))))))))
4615 files)
4616 (setq version (1+ version))
4617 (ange-ftp-put-hash-entry
4618 (concat file ";" (int-to-string version))
4619 nil files))))
4620 (ange-ftp-put-hash-entry file nil files))))))
4621
4622 (or (assq 'vms ange-ftp-add-file-entry-alist)
4623 (setq ange-ftp-add-file-entry-alist
4624 (cons '(vms . ange-ftp-vms-add-file-entry)
4625 ange-ftp-add-file-entry-alist)))
4626
4627
4628 (defun ange-ftp-add-vms-host (host)
4629 "Mark HOST as the name of a machine running VMS."
4630 (interactive
4631 (list (read-string "Host: "
4632 (let ((name (or (buffer-file-name) default-directory)))
4633 (and name (car (ange-ftp-ftp-name name)))))))
4634 (if (not (ange-ftp-vms-host host))
4635 (setq ange-ftp-vms-host-regexp
4636 (concat "^" (regexp-quote host) "$"
4637 (and ange-ftp-vms-host-regexp "\\|")
4638 ange-ftp-vms-host-regexp)
4639 ange-ftp-host-cache nil)))
4640
4641
4642 (defun ange-ftp-vms-file-name-as-directory (name)
4643 (ange-ftp-save-match-data
4644 (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
4645 (setq name (substring name 0 (match-beginning 0))))
4646 (ange-ftp-real-file-name-as-directory name)))
4647
4648 (or (assq 'vms ange-ftp-file-name-as-directory-alist)
4649 (setq ange-ftp-file-name-as-directory-alist
4650 (cons '(vms . ange-ftp-vms-file-name-as-directory)
4651 ange-ftp-file-name-as-directory-alist)))
4652
4653 ;;; Tree dired support:
4654
4655 ;; For this code I have borrowed liberally from Sebastian Kremer's
4656 ;; dired-vms.el
4657
4658
4659 ;;;; These regexps must be anchored to beginning of line.
4660 ;;;; Beware that the ftpd may put the device in front of the filename.
4661
4662 ;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
4663 ;; "Regular expression to use to search for VMS executable files.")
4664
4665 ;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
4666 ;; "Regular expression to use to search for VMS directories.")
4667
4668 ;;(or (assq 'vms ange-ftp-dired-re-exe-alist)
4669 ;; (setq ange-ftp-dired-re-exe-alist
4670 ;; (cons (cons 'vms ange-ftp-dired-vms-re-exe)
4671 ;; ange-ftp-dired-re-exe-alist)))
4672
4673 ;;(or (assq 'vms ange-ftp-dired-re-dir-alist)
4674 ;; (setq ange-ftp-dired-re-dir-alist
4675 ;; (cons (cons 'vms ange-ftp-dired-vms-re-dir)
4676 ;; ange-ftp-dired-re-dir-alist)))
4677
4678 ;;(defun ange-ftp-dired-vms-insert-headerline (dir)
4679 ;; ;; VMS inserts a headerline. I would prefer the headerline
4680 ;; ;; to be in ange-ftp format. This version tries to
4681 ;; ;; be careful, because we can't count on a headerline
4682 ;; ;; over ftp, and we wouldn't want to delete anything
4683 ;; ;; important.
4684 ;; (save-excursion
4685 ;; (if (looking-at "^ wildcard ")
4686 ;; (forward-line 1))
4687 ;; (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
4688 ;; (delete-region (point) (match-end 0))))
4689 ;; (ange-ftp-real-dired-insert-headerline dir))
4690
4691 ;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist)
4692 ;; (setq ange-ftp-dired-insert-headerline-alist
4693 ;; (cons '(vms . ange-ftp-dired-vms-insert-headerline)
4694 ;; ange-ftp-dired-insert-headerline-alist)))
4695
4696 ;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
4697 ;; "In dired, move to first char of filename on this line.
4698 ;;Returns position (point) or nil if no filename on this line."
4699 ;; ;; This is the VMS version.
4700 ;; (let (case-fold-search)
4701 ;; (or eol (setq eol (progn (end-of-line) (point))))
4702 ;; (beginning-of-line)
4703 ;; (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
4704 ;; (goto-char (match-beginning 1))
4705 ;; (if raise-error
4706 ;; (error "No file on this line")
4707 ;; nil))))
4708
4709 ;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist)
4710 ;; (setq ange-ftp-dired-move-to-filename-alist
4711 ;; (cons '(vms . ange-ftp-dired-vms-move-to-filename)
4712 ;; ange-ftp-dired-move-to-filename-alist)))
4713
4714 ;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
4715 ;; ;; Assumes point is at beginning of filename.
4716 ;; ;; So, it should be called only after (dired-move-to-filename t).
4717 ;; ;; case-fold-search must be nil, at least for VMS.
4718 ;; ;; On failure, signals an error or returns nil.
4719 ;; ;; This is the VMS version.
4720 ;; (let (opoint hidden case-fold-search)
4721 ;; (setq opoint (point))
4722 ;; (or eol (setq eol (save-excursion (end-of-line) (point))))
4723 ;; (setq hidden (and selective-display
4724 ;; (save-excursion (search-forward "\r" eol t))))
4725 ;; (if hidden
4726 ;; nil
4727 ;; (re-search-forward ange-ftp-vms-filename-regexp eol t))
4728 ;; (or no-error
4729 ;; (not (eq opoint (point)))
4730 ;; (error
4731 ;; (if hidden
4732 ;; (substitute-command-keys
4733 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide")
4734 ;; "No file on this line")))
4735 ;; (if (eq opoint (point))
4736 ;; nil
4737 ;; (point))))
4738
4739 ;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
4740 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist
4741 ;; (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
4742 ;; ange-ftp-dired-move-to-end-of-filename-alist)))
4743
4744 ;;(defun ange-ftp-dired-vms-between-files ()
4745 ;; (save-excursion
4746 ;; (beginning-of-line)
4747 ;; (or (equal (following-char) 10) ; newline
4748 ;; (equal (following-char) 9) ; tab
4749 ;; (progn (forward-char 2)
4750 ;; (or (looking-at "Total of")
4751 ;; (equal (following-char) 32))))))
4752
4753 ;;(or (assq 'vms ange-ftp-dired-between-files-alist)
4754 ;; (setq ange-ftp-dired-between-files-alist
4755 ;; (cons '(vms . ange-ftp-dired-vms-between-files)
4756 ;; ange-ftp-dired-between-files-alist)))
4757
4758 ;; Beware! In VMS filenames must be of the form "FILE.TYPE".
4759 ;; Therefore, we cannot just append a ".Z" to filenames for
4760 ;; compressed files. Instead, we turn "FILE.TYPE" into
4761 ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
4762
4763 (defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
4764 (cond
4765 ((string-match "-Z;[0-9]+$" name)
4766 (list nil (substring name 0 (match-beginning 0))))
4767 ((string-match ";[0-9]+$" name)
4768 (list nil (substring name 0 (match-beginning 0))))
4769 ((string-match "-Z$" name)
4770 (list nil (substring name 0 -2)))
4771 (t
4772 (list t
4773 (if (string-match ";[0-9]+$" name)
4774 (concat (substring name 0 (match-beginning 0))
4775 "-Z")
4776 (concat name "-Z"))))))
4777
4778 (or (assq 'vms ange-ftp-make-compressed-filename-alist)
4779 (setq ange-ftp-make-compressed-filename-alist
4780 (cons '(vms . ange-ftp-vms-make-compressed-filename)
4781 ange-ftp-make-compressed-filename-alist)))
4782
4783 ;;;; When the filename is too long, VMS will use two lines to list a file
4784 ;;;; (damn them!) This will confuse dired. To solve this, need to convince
4785 ;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
4786 ;;;; (forward-line 1). This would require a number of changes to dired.el.
4787 ;;;; If dired gets confused, revert-buffer will fix it.
4788
4789 ;;(defun ange-ftp-dired-vms-ls-trim ()
4790 ;; (goto-char (point-min))
4791 ;; (let ((case-fold-search nil))
4792 ;; (re-search-forward ange-ftp-vms-filename-regexp))
4793 ;; (beginning-of-line)
4794 ;; (delete-region (point-min) (point))
4795 ;; (forward-line 1)
4796 ;; (delete-region (point) (point-max)))
4797
4798
4799 ;;(or (assq 'vms ange-ftp-dired-ls-trim-alist)
4800 ;; (setq ange-ftp-dired-ls-trim-alist
4801 ;; (cons '(vms . ange-ftp-dired-vms-ls-trim)
4802 ;; ange-ftp-dired-ls-trim-alist)))
4803
4804 (defun ange-ftp-vms-sans-version (name)
4805 (ange-ftp-save-match-data
4806 (if (string-match ";[0-9]+$" name)
4807 (substring name 0 (match-beginning 0))
4808 name)))
4809
4810 (or (assq 'vms ange-ftp-sans-version-alist)
4811 (setq ange-ftp-sans-version-alist
4812 (cons '(vms . ange-ftp-vms-sans-version)
4813 ange-ftp-sans-version-alist)))
4814
4815 ;;(defvar ange-ftp-file-version-alist)
4816
4817 ;;;;; The vms version of clean-directory has 2 more optional args
4818 ;;;;; than the usual dired version. This is so that it can be used by
4819 ;;;;; ange-ftp-dired-vms-flag-backup-files.
4820
4821 ;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
4822 ;; "Flag numerical backups for deletion.
4823 ;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
4824 ;;Positive prefix arg KEEP overrides `dired-kept-versions';
4825 ;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
4826
4827 ;;To clear the flags on these files, you can use \\[dired-flag-backup-files]
4828 ;;with a prefix argument."
4829 ;;; (interactive "P") ; Never actually called interactively.
4830 ;; (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
4831 ;; (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
4832 ;; ;; late-retention must NEVER be allowed to be less than 1 in VMS!
4833 ;; ;; This could wipe ALL copies of the file.
4834 ;; (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
4835 ;; (action (or msg "Cleaning"))
4836 ;; (ange-ftp-trample-marker (or marker dired-del-marker))
4837 ;; (ange-ftp-file-version-alist ()))
4838 ;; (message (concat action
4839 ;; " numerical backups (keeping %d late, %d old)...")
4840 ;; late-retention early-retention)
4841 ;; ;; Look at each file.
4842 ;; ;; If the file has numeric backup versions,
4843 ;; ;; put on ange-ftp-file-version-alist an element of the form
4844 ;; ;; (FILENAME . VERSION-NUMBER-LIST)
4845 ;; (dired-map-dired-file-lines (function
4846 ;; ange-ftp-dired-vms-collect-file-versions))
4847 ;; ;; Sort each VERSION-NUMBER-LIST,
4848 ;; ;; and remove the versions not to be deleted.
4849 ;; (let ((fval ange-ftp-file-version-alist))
4850 ;; (while fval
4851 ;; (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
4852 ;; (v-count (length sorted-v-list)))
4853 ;; (if (> v-count (+ early-retention late-retention))
4854 ;; (rplacd (nthcdr early-retention sorted-v-list)
4855 ;; (nthcdr (- v-count late-retention)
4856 ;; sorted-v-list)))
4857 ;; (rplacd (car fval)
4858 ;; (cdr sorted-v-list)))
4859 ;; (setq fval (cdr fval))))
4860 ;; ;; Look at each file. If it is a numeric backup file,
4861 ;; ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
4862 ;; (dired-map-dired-file-lines
4863 ;; (function
4864 ;; ange-ftp-dired-vms-trample-file-versions mark))
4865 ;; (message (concat action " numerical backups...done"))))
4866
4867 ;;(or (assq 'vms ange-ftp-dired-clean-directory-alist)
4868 ;; (setq ange-ftp-dired-clean-directory-alist
4869 ;; (cons '(vms . ange-ftp-dired-vms-clean-directory)
4870 ;; ange-ftp-dired-clean-directory-alist)))
4871
4872 ;;(defun ange-ftp-dired-vms-collect-file-versions (fn)
4873 ;; ;; "If it looks like file FN has versions, return a list of the versions.
4874 ;; ;;That is a list of strings which are file names.
4875 ;; ;;The caller may want to flag some of these files for deletion."
4876 ;;(let ((name (nth 2 (ange-ftp-ftp-name fn))))
4877 ;; (if (string-match ";[0-9]+$" name)
4878 ;; (let* ((name (substring name 0 (match-beginning 0)))
4879 ;; (fn (ange-ftp-replace-name-component fn name)))
4880 ;; (if (not (assq fn ange-ftp-file-version-alist))
4881 ;; (let* ((base-versions
4882 ;; (concat (file-name-nondirectory name) ";"))
4883 ;; (bv-length (length base-versions))
4884 ;; (possibilities (file-name-all-completions
4885 ;; base-versions
4886 ;; (file-name-directory fn)))
4887 ;; (versions (mapcar
4888 ;; '(lambda (arg)
4889 ;; (if (and (string-match
4890 ;; "[0-9]+$" arg bv-length)
4891 ;; (= (match-beginning 0) bv-length))
4892 ;; (string-to-int (substring arg bv-length))
4893 ;; 0))
4894 ;; possibilities)))
4895 ;; (if versions
4896 ;; (setq
4897 ;; ange-ftp-file-version-alist
4898 ;; (cons (cons fn versions)
4899 ;; ange-ftp-file-version-alist)))))))))
4900
4901 ;;(defun ange-ftp-dired-vms-trample-file-versions (fn)
4902 ;; (let* ((start-vn (string-match ";[0-9]+$" fn))
4903 ;; base-version-list)
4904 ;; (and start-vn
4905 ;; (setq base-version-list ; there was a base version to which
4906 ;; (assoc (substring fn 0 start-vn) ; this looks like a
4907 ;; ange-ftp-file-version-alist)) ; subversion
4908 ;; (not (memq (string-to-int (substring fn (1+ start-vn)))
4909 ;; base-version-list)) ; this one doesn't make the cut
4910 ;; (progn (beginning-of-line)
4911 ;; (delete-char 1)
4912 ;; (insert ange-ftp-trample-marker)))))
4913
4914 ;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
4915 ;; (let ((dired-kept-versions 1)
4916 ;; (kept-old-versions 0)
4917 ;; marker msg)
4918 ;; (if unflag-p
4919 ;; (setq marker ?\040 msg "Unflagging")
4920 ;; (setq marker dired-del-marker msg "Cleaning"))
4921 ;; (ange-ftp-dired-vms-clean-directory nil marker msg)))
4922
4923 ;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
4924 ;; (setq ange-ftp-dired-flag-backup-files-alist
4925 ;; (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
4926 ;; ange-ftp-dired-flag-backup-files-alist)))
4927
4928 ;;(defun ange-ftp-dired-vms-backup-diff (&optional switches)
4929 ;; (let ((file (dired-get-filename 'no-dir))
4930 ;; bak)
4931 ;; (if (and (string-match ";[0-9]+$" file)
4932 ;; ;; Find most recent previous version.
4933 ;; (let ((root (substring file 0 (match-beginning 0)))
4934 ;; (ver
4935 ;; (string-to-int (substring file (1+ (match-beginning 0)))))
4936 ;; found)
4937 ;; (setq ver (1- ver))
4938 ;; (while (and (> ver 0) (not found))
4939 ;; (setq bak (concat root ";" (int-to-string ver)))
4940 ;; (and (file-exists-p bak) (setq found t))
4941 ;; (setq ver (1- ver)))
4942 ;; found))
4943 ;; (if switches
4944 ;; (diff (expand-file-name bak) (expand-file-name file) switches)
4945 ;; (diff (expand-file-name bak) (expand-file-name file)))
4946 ;; (error "No previous version found for %s" file))))
4947
4948 ;;(or (assq 'vms ange-ftp-dired-backup-diff-alist)
4949 ;; (setq ange-ftp-dired-backup-diff-alist
4950 ;; (cons '(vms . ange-ftp-dired-vms-backup-diff)
4951 ;; ange-ftp-dired-backup-diff-alist)))
4952
4953 \f
4954 ;;;; ------------------------------------------------------------
4955 ;;;; MTS support
4956 ;;;; ------------------------------------------------------------
4957
4958
4959 ;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from
4960 ;; MTS to UNIX-ish.
4961 (defun ange-ftp-fix-name-for-mts (name &optional reverse)
4962 (ange-ftp-save-match-data
4963 (if reverse
4964 (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name)
4965 (let (acct file)
4966 (if (match-beginning 1)
4967 (setq acct (substring name 0 (match-end 1))))
4968 (if (match-beginning 2)
4969 (setq file (substring name
4970 (match-beginning 2) (match-end 2))))
4971 (concat (and acct (concat "/" acct "/"))
4972 file))
4973 (error "name %s didn't match" name))
4974 (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name)
4975 (concat (substring name 1 (match-end 1))
4976 (substring name (match-beginning 2) (match-end 2)))
4977 ;; Let's hope that mts will recognize it anyway.
4978 name))))
4979
4980 (or (assq 'mts ange-ftp-fix-name-func-alist)
4981 (setq ange-ftp-fix-name-func-alist
4982 (cons '(mts . ange-ftp-fix-name-for-mts)
4983 ange-ftp-fix-name-func-alist)))
4984
4985 ;; Convert name from UNIX-ish to MTS ready for a DIRectory listing.
4986 ;; Remember that there are no directories in MTS.
4987 (defun ange-ftp-fix-dir-name-for-mts (dir-name)
4988 (if (string-equal dir-name "/")
4989 (error "Cannot get listing for fictitious \"/\" directory.")
4990 (let ((dir-name (ange-ftp-fix-name-for-mts dir-name)))
4991 (cond
4992 ((string-equal dir-name "")
4993 "?")
4994 ((string-match ":$" dir-name)
4995 (concat dir-name "?"))
4996 (dir-name))))) ; It's just a single file.
4997
4998 (or (assq 'mts ange-ftp-fix-dir-name-func-alist)
4999 (setq ange-ftp-fix-dir-name-func-alist
5000 (cons '(mts . ange-ftp-fix-dir-name-for-mts)
5001 ange-ftp-fix-dir-name-func-alist)))
5002
5003 (or (memq 'mts ange-ftp-dumb-host-types)
5004 (setq ange-ftp-dumb-host-types
5005 (cons 'mts ange-ftp-dumb-host-types)))
5006
5007 (defvar ange-ftp-mts-host-regexp nil)
5008
5009 ;; Return non-nil if HOST is running MTS.
5010 (defun ange-ftp-mts-host (host)
5011 (and ange-ftp-mts-host-regexp
5012 (ange-ftp-save-match-data
5013 (string-match ange-ftp-mts-host-regexp host))))
5014
5015 ;; Parse the current buffer which is assumed to be in mts ftp dir format.
5016 (defun ange-ftp-parse-mts-listing ()
5017 (let ((tbl (ange-ftp-make-hashtable)))
5018 (goto-char (point-min))
5019 (ange-ftp-save-match-data
5020 (while (re-search-forward ange-ftp-date-regexp nil t)
5021 (end-of-line)
5022 (skip-chars-backward " ")
5023 (let ((end (point)))
5024 (skip-chars-backward "-A-Z0-9_.!")
5025 (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl))
5026 (forward-line 1)))
5027 ;; Don't need to bother with ..
5028 (ange-ftp-put-hash-entry "." t tbl)
5029 tbl))
5030
5031 (or (assq 'mts ange-ftp-parse-list-func-alist)
5032 (setq ange-ftp-parse-list-func-alist
5033 (cons '(mts . ange-ftp-parse-mts-listing)
5034 ange-ftp-parse-list-func-alist)))
5035
5036 (defun ange-ftp-add-mts-host (host)
5037 "Mark HOST as the name of a machine running MTS."
5038 (interactive
5039 (list (read-string "Host: "
5040 (let ((name (or (buffer-file-name) default-directory)))
5041 (and name (car (ange-ftp-ftp-name name)))))))
5042 (if (not (ange-ftp-mts-host host))
5043 (setq ange-ftp-mts-host-regexp
5044 (concat "^" (regexp-quote host) "$"
5045 (and ange-ftp-mts-host-regexp "\\|")
5046 ange-ftp-mts-host-regexp)
5047 ange-ftp-host-cache nil)))
5048
5049 ;;; Tree dired support:
5050
5051 ;;;; There aren't too many systems left that use MTS. This dired support will
5052 ;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
5053 ;;;; implement ftp in the same way. If not, it might be necessary to make the
5054 ;;;; following more flexible.
5055
5056 ;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
5057 ;; "In dired, move to first char of filename on this line.
5058 ;;Returns position (point) or nil if no filename on this line."
5059 ;; ;; This is the MTS version.
5060 ;; (or eol (setq eol (progn (end-of-line) (point))))
5061 ;; (beginning-of-line)
5062 ;; (if (re-search-forward
5063 ;; ange-ftp-date-regexp eol t)
5064 ;; (progn
5065 ;; (skip-chars-forward " ") ; Eat blanks after date
5066 ;; (skip-chars-forward "0-9:" eol) ; Eat time or year
5067 ;; (skip-chars-forward " " eol) ; one space before filename
5068 ;; ;; When listing an account other than the users own account it appends
5069 ;; ;; ACCT: to the beginning of the filename. Skip over this.
5070 ;; (and (looking-at "[A-Z0-9_.]+:")
5071 ;; (goto-char (match-end 0)))
5072 ;; (point))
5073 ;; (if raise-error
5074 ;; (error "No file on this line")
5075 ;; nil)))
5076
5077 ;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist)
5078 ;; (setq ange-ftp-dired-move-to-filename-alist
5079 ;; (cons '(mts . ange-ftp-dired-mts-move-to-filename)
5080 ;; ange-ftp-dired-move-to-filename-alist)))
5081
5082 ;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
5083 ;; ;; Assumes point is at beginning of filename.
5084 ;; ;; So, it should be called only after (dired-move-to-filename t).
5085 ;; ;; On failure, signals an error or returns nil.
5086 ;; ;; This is the MTS version.
5087 ;; (let (opoint hidden case-fold-search)
5088 ;; (setq opoint (point)
5089 ;; eol (save-excursion (end-of-line) (point))
5090 ;; hidden (and selective-display
5091 ;; (save-excursion (search-forward "\r" eol t))))
5092 ;; (if hidden
5093 ;; nil
5094 ;; (skip-chars-forward "-A-Z0-9._!" eol))
5095 ;; (or no-error
5096 ;; (not (eq opoint (point)))
5097 ;; (error
5098 ;; (if hidden
5099 ;; (substitute-command-keys
5100 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide")
5101 ;; "No file on this line")))
5102 ;; (if (eq opoint (point))
5103 ;; nil
5104 ;; (point))))
5105
5106 ;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
5107 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist
5108 ;; (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
5109 ;; ange-ftp-dired-move-to-end-of-filename-alist)))
5110 \f
5111 ;;;; ------------------------------------------------------------
5112 ;;;; CMS support
5113 ;;;; ------------------------------------------------------------
5114
5115 ;; Since CMS doesn't have any full file name syntax, we have to fudge
5116 ;; things with cd's. We actually send too many cd's, but it's dangerous
5117 ;; to try to remember the current minidisk, because if the connection
5118 ;; is closed and needs to be reopened, we will find ourselves back in
5119 ;; the default minidisk. This is fairly likely since CMS ftp servers
5120 ;; usually close the connection after 5 minutes of inactivity.
5121
5122 ;; Have I got the filename character set right?
5123
5124 (defun ange-ftp-fix-name-for-cms (name &optional reverse)
5125 (ange-ftp-save-match-data
5126 (if reverse
5127 ;; Since we only convert output from a pwd in this direction,
5128 ;; we'll assume that it's a minidisk, and make it into a
5129 ;; directory file name. Note that the expand-dir-hashtable
5130 ;; stores directories without the trailing /. Is this
5131 ;; consistent?
5132 (concat "/" name)
5133 (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
5134 name)
5135 (let ((minidisk (substring name 1 (match-end 1))))
5136 (if (match-beginning 2)
5137 (let ((file (substring name (match-beginning 2)
5138 (match-end 2)))
5139 (cmd (concat "cd " minidisk))
5140
5141 ;; Note that host and user are bound in the call
5142 ;; to ange-ftp-send-cmd
5143 (proc (ange-ftp-get-process ange-ftp-this-host
5144 ange-ftp-this-user)))
5145
5146 ;; Must use ange-ftp-raw-send-cmd here to avoid
5147 ;; an infinite loop.
5148 (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg))
5149 file
5150 ;; failed... try ONCE more.
5151 (setq proc (ange-ftp-get-process ange-ftp-this-host
5152 ange-ftp-this-user))
5153 (let ((result (ange-ftp-raw-send-cmd proc cmd
5154 ange-ftp-this-msg)))
5155 (if (car result)
5156 file
5157 ;; failed. give up.
5158 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
5159 (format "cd to minidisk %s failed: %s"
5160 minidisk (cdr result)))))))
5161 ;; return the minidisk
5162 minidisk))
5163 (error "Invalid CMS filename")))))
5164
5165 (or (assq 'cms ange-ftp-fix-name-func-alist)
5166 (setq ange-ftp-fix-name-func-alist
5167 (cons '(cms . ange-ftp-fix-name-for-cms)
5168 ange-ftp-fix-name-func-alist)))
5169
5170 (or (memq 'cms ange-ftp-dumb-host-types)
5171 (setq ange-ftp-dumb-host-types
5172 (cons 'cms ange-ftp-dumb-host-types)))
5173
5174 ;; Convert name from UNIX-ish to CMS ready for a DIRectory listing.
5175 (defun ange-ftp-fix-dir-name-for-cms (dir-name)
5176 (cond
5177 ((string-equal "/" dir-name)
5178 (error "Cannot get listing for fictitious \"/\" directory."))
5179 ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name)
5180 (let* ((minidisk (substring dir-name (match-beginning 1) (match-end 1)))
5181 ;; host and user are bound in the call to ange-ftp-send-cmd
5182 (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user))
5183 (cmd (concat "cd " minidisk))
5184 (file (if (match-beginning 2)
5185 ;; it's a single file
5186 (substring dir-name (match-beginning 2)
5187 (match-end 2))
5188 ;; use the wild-card
5189 "*")))
5190 (if (car (ange-ftp-raw-send-cmd proc cmd))
5191 file
5192 ;; try again...
5193 (setq proc (ange-ftp-get-process ange-ftp-this-host
5194 ange-ftp-this-user))
5195 (let ((result (ange-ftp-raw-send-cmd proc cmd)))
5196 (if (car result)
5197 file
5198 ;; give up
5199 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
5200 (format "cd to minidisk %s failed: "
5201 minidisk (cdr result))))))))
5202 (t (error "Invalid CMS file name"))))
5203
5204 (or (assq 'cms ange-ftp-fix-dir-name-func-alist)
5205 (setq ange-ftp-fix-dir-name-func-alist
5206 (cons '(cms . ange-ftp-fix-dir-name-for-cms)
5207 ange-ftp-fix-dir-name-func-alist)))
5208
5209 (defvar ange-ftp-cms-host-regexp nil
5210 "Regular expression to match hosts running the CMS operating system.")
5211
5212 ;; Return non-nil if HOST is running CMS.
5213 (defun ange-ftp-cms-host (host)
5214 (and ange-ftp-cms-host-regexp
5215 (ange-ftp-save-match-data
5216 (string-match ange-ftp-cms-host-regexp host))))
5217
5218 (defun ange-ftp-add-cms-host (host)
5219 "Mark HOST as the name of a CMS host."
5220 (interactive
5221 (list (read-string "Host: "
5222 (let ((name (or (buffer-file-name) default-directory)))
5223 (and name (car (ange-ftp-ftp-name name)))))))
5224 (if (not (ange-ftp-cms-host host))
5225 (setq ange-ftp-cms-host-regexp
5226 (concat "^" (regexp-quote host) "$"
5227 (and ange-ftp-cms-host-regexp "\\|")
5228 ange-ftp-cms-host-regexp)
5229 ange-ftp-host-cache nil)))
5230
5231 (defun ange-ftp-parse-cms-listing ()
5232 ;; Parse the current buffer which is assumed to be a CMS directory listing.
5233 ;; If we succeed in getting a listing, then we will assume that the minidisk
5234 ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
5235 ;; because ange-ftp doesn't know that the root hashtable has only part of
5236 ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
5237 ;; exist. It would be nice if completion worked for minidisks, as we
5238 ;; discover them.
5239 ; (let* ((dir-file (directory-file-name file))
5240 ; (root (file-name-directory dir-file))
5241 ; (minidisk (ange-ftp-get-file-part dir-file))
5242 ; (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
5243 ; (if root-tbl
5244 ; (ange-ftp-put-hash-entry minidisk t root-tbl)
5245 ; (setq root-tbl (ange-ftp-make-hashtable))
5246 ; (ange-ftp-put-hash-entry minidisk t root-tbl)
5247 ; (ange-ftp-put-hash-entry "." t root-tbl)
5248 ; (ange-ftp-set-files root root-tbl)))
5249 ;; Now do the usual parsing
5250 (let ((tbl (ange-ftp-make-hashtable)))
5251 (goto-char (point-min))
5252 (ange-ftp-save-match-data
5253 (while
5254 (re-search-forward
5255 "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
5256 (ange-ftp-put-hash-entry
5257 (concat (buffer-substring (match-beginning 1)
5258 (match-end 1))
5259 "."
5260 (buffer-substring (match-beginning 2)
5261 (match-end 2)))
5262 nil tbl)
5263 (forward-line 1))
5264 (ange-ftp-put-hash-entry "." t tbl))
5265 tbl))
5266
5267 (or (assq 'cms ange-ftp-parse-list-func-alist)
5268 (setq ange-ftp-parse-list-func-alist
5269 (cons '(cms . ange-ftp-parse-cms-listing)
5270 ange-ftp-parse-list-func-alist)))
5271
5272 ;;;;; Tree dired support:
5273
5274 ;;(defconst ange-ftp-dired-cms-re-exe
5275 ;; "^. [-A-Z0-9$_]+ +EXEC "
5276 ;; "Regular expression to use to search for CMS executables.")
5277
5278 ;;(or (assq 'cms ange-ftp-dired-re-exe-alist)
5279 ;; (setq ange-ftp-dired-re-exe-alist
5280 ;; (cons (cons 'cms ange-ftp-dired-cms-re-exe)
5281 ;; ange-ftp-dired-re-exe-alist)))
5282
5283
5284 ;;(defun ange-ftp-dired-cms-insert-headerline (dir)
5285 ;; ;; CMS has no total line, so we insert a blank line for
5286 ;; ;; aesthetics.
5287 ;; (insert "\n")
5288 ;; (forward-char -1)
5289 ;; (ange-ftp-real-dired-insert-headerline dir))
5290
5291 ;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist)
5292 ;; (setq ange-ftp-dired-insert-headerline-alist
5293 ;; (cons '(cms . ange-ftp-dired-cms-insert-headerline)
5294 ;; ange-ftp-dired-insert-headerline-alist)))
5295
5296 ;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
5297 ;; "In dired, move to the first char of filename on this line."
5298 ;; ;; This is the CMS version.
5299 ;; (or eol (setq eol (progn (end-of-line) (point))))
5300 ;; (let (case-fold-search)
5301 ;; (beginning-of-line)
5302 ;; (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
5303 ;; (goto-char (1+ (match-beginning 0)))
5304 ;; (if raise-error
5305 ;; (error "No file on this line")
5306 ;; nil))))
5307
5308 ;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist)
5309 ;; (setq ange-ftp-dired-move-to-filename-alist
5310 ;; (cons '(cms . ange-ftp-dired-cms-move-to-filename)
5311 ;; ange-ftp-dired-move-to-filename-alist)))
5312
5313 ;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
5314 ;; ;; Assumes point is at beginning of filename.
5315 ;; ;; So, it should be called only after (dired-move-to-filename t).
5316 ;; ;; case-fold-search must be nil, at least for VMS.
5317 ;; ;; On failure, signals an error or returns nil.
5318 ;; ;; This is the CMS version.
5319 ;; (let ((opoint (point))
5320 ;; case-fold-search hidden)
5321 ;; (or eol (setq eol (save-excursion (end-of-line) (point))))
5322 ;; (setq hidden (and selective-display
5323 ;; (save-excursion
5324 ;; (search-forward "\r" eol t))))
5325 ;; (if hidden
5326 ;; (if no-error
5327 ;; nil
5328 ;; (error
5329 ;; (substitute-command-keys
5330 ;; "File line is hidden, type \\[dired-hide-subdir] to unhide")))
5331 ;; (skip-chars-forward "-A-Z0-9$_" eol)
5332 ;; (skip-chars-forward " " eol)
5333 ;; (skip-chars-forward "-A-Z0-9$_" eol)
5334 ;; (if (eq opoint (point))
5335 ;; (if no-error
5336 ;; nil
5337 ;; (error "No file on this line"))
5338 ;; (point)))))
5339
5340 ;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
5341 ;; (setq ange-ftp-dired-move-to-end-of-filename-alist
5342 ;; (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
5343 ;; ange-ftp-dired-move-to-end-of-filename-alist)))
5344
5345 (defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
5346 (if (string-match "-Z$" name)
5347 (list nil (substring name 0 -2))
5348 (list t (concat name "-Z"))))
5349
5350 (or (assq 'cms ange-ftp-make-compressed-filename-alist)
5351 (setq ange-ftp-make-compressed-filename-alist
5352 (cons '(cms . ange-ftp-cms-make-compressed-filename)
5353 ange-ftp-make-compressed-filename-alist)))
5354
5355 ;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
5356 ;; (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
5357 ;; (and name
5358 ;; (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
5359 ;; (concat (substring name 0 (match-end 1))
5360 ;; "."
5361 ;; (substring name (match-beginning 2) (match-end 2)))
5362 ;; name))))
5363
5364 ;;(or (assq 'cms ange-ftp-dired-get-filename-alist)
5365 ;; (setq ange-ftp-dired-get-filename-alist
5366 ;; (cons '(cms . ange-ftp-dired-cms-get-filename)
5367 ;; ange-ftp-dired-get-filename-alist)))
5368 \f
5369 ;;;; ------------------------------------------------------------
5370 ;;;; Finally provide package.
5371 ;;;; ------------------------------------------------------------
5372
5373 (provide 'ange-ftp)
5374
5375 ;;; ange-ftp.el ends here