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