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