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