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