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