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