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