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