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