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