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