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