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