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