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