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