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