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