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