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