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