Update.
[bpt/emacs.git] / lisp / net / ange-ftp.el
CommitLineData
8749abea
GM
1;;; ange-ftp.el --- transparent FTP support for GNU Emacs
2
5fd6d89f
TTN
3;; Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4;; 2000, 2001, 2002, 2003, 2004, 2005 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:
5fd6d89f 748 ;;
92482d77
SM
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 ".")
8012c835
MA
2331 (setq result (ange-ftp-cd host user
2332 ;; Make sure the target to which
2333 ;; `cd' is performed is a directory.
2334 (file-name-directory (nth 1 cmd))
2335 'noerror)))
2336 ;; Concatenate the switches and the target to be used with `ls'.
2337 (setq cmd1 (concat "\"" cmd3 " " cmd1 "\""))))
25759a92 2338
8749abea
GM
2339 ;; First argument is the remote name
2340 ((progn
2341 (setq fix-name-func (or (cdr (assq host-type
2342 ange-ftp-fix-name-func-alist))
2343 'identity))
2344 (memq cmd0 '(get delete mkdir rmdir cd)))
2345 (setq cmd1 (funcall fix-name-func cmd1)))
2346
2347 ;; Second argument is the remote name
cfc3aed3
GM
2348 ((or (memq cmd0 '(append put chmod))
2349 (and (eq cmd0 'quote) (string= cmd1 "mdtm")))
8749abea 2350 (setq cmd2 (funcall fix-name-func cmd2)))
8749abea
GM
2351 ;; Both arguments are remote names
2352 ((eq cmd0 'rename)
2353 (setq cmd1 (funcall fix-name-func cmd1)
2354 cmd2 (funcall fix-name-func cmd2))))
25759a92
SS
2355
2356 ;; Turn the command into one long string
8749abea
GM
2357 (setq cmd0 (symbol-name cmd0))
2358 (setq cmd (concat cmd0
2359 (and cmd1 (concat " " cmd1))
2360 (and cmd2 (concat " " cmd2))))
2361
2362 ;; Actually send the resulting command.
67f300f8
SM
2363 (if (and (consp result) (null (car result)))
2364 ;; `ange-ftp-cd' has failed, so there's no point sending `cmd'.
2365 result
2366 (let (afsc-result
2367 afsc-line)
2368 (ange-ftp-raw-send-cmd
2369 (ange-ftp-get-process host user)
2370 cmd
2371 msg
2372 (list (lambda (result line host user cmd msg cont nowait)
2373 (or cont (setq afsc-result result
2374 afsc-line line))
2375 (if result (ange-ftp-call-cont cont result line)
25759a92
SS
2376 (ange-ftp-raw-send-cmd
2377 (ange-ftp-get-process host user)
2378 cmd
2379 msg
2380 (list (lambda (result line cont)
2381 (or cont (setq afsc-result result
2382 afsc-line line))
2383 (ange-ftp-call-cont cont result line))
67f300f8
SM
2384 cont)
2385 nowait)))
2386 host user cmd msg cont nowait)
2387 nowait)
2388
2389 (if nowait
8749abea 2390 nil
67f300f8
SM
2391 (if cont
2392 nil
2393 (cons afsc-result afsc-line)))))))
8749abea
GM
2394
2395;; It might be nice to message users about the host type identified,
2396;; but there is so much other messaging going on, it would not be
2397;; seen. No point in slowing things down just so users can read
2398;; a host type message.
2399
2400(defconst ange-ftp-cms-name-template
2401 (concat
2402 "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
2403 "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
2404(defconst ange-ftp-vms-name-template
2405 "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
2406(defconst ange-ftp-mts-name-template
2407 "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
9c35d706
RS
2408(defconst ange-ftp-bs2000-filename-pubset-regexp
2409 ":[A-Z0-9]+:"
2410 "Valid pubset for an BS2000 file name.")
2411(defconst ange-ftp-bs2000-filename-username-regexp
2412 (concat
2413 "\\$[A-Z0-9]*\\.")
2414 "Valid username for an BS2000 file name.")
2415(defconst ange-ftp-bs2000-filename-prefix-regexp
2416 (concat
2417 ange-ftp-bs2000-filename-pubset-regexp
2418 ange-ftp-bs2000-filename-username-regexp)
2419 "Valid prefix for an BS2000 file name (pubset and user).")
2420(defconst ange-ftp-bs2000-name-template
2421 (concat "^" ange-ftp-bs2000-filename-prefix-regexp "$"))
8749abea
GM
2422
2423(defun ange-ftp-guess-host-type (host user)
110c171f 2424 "Guess the host type of HOST.
8749abea
GM
2425Works by doing a pwd and examining the directory syntax."
2426 (let ((host-type (ange-ftp-host-type host))
2427 (key (concat host "/" user "/~")))
2428 (if (eq host-type 'unix)
2429 ;; Note that ange-ftp-host-type returns unix as the default value.
2430 (save-match-data
2431 (let* ((result (ange-ftp-get-pwd host user))
2432 (dir (car result))
2433 fix-name-func)
2434 (cond ((null dir)
2435 (message "Warning! Unable to get home directory")
2436 (sit-for 1)
2437 (if (string-match
2438 "^450 No current working directory defined$"
2439 (cdr result))
25759a92 2440
8749abea
GM
2441 ;; We'll assume that if pwd bombs with this
2442 ;; error message, then it's CMS.
2443 (progn
2444 (ange-ftp-add-cms-host host)
2445 (setq ange-ftp-host-cache host
2446 ange-ftp-host-type-cache 'cms))))
2447
2448 ;; try for VMS
2449 ((string-match ange-ftp-vms-name-template dir)
2450 (ange-ftp-add-vms-host host)
2451 ;; The add-host functions clear the host type cache.
2452 ;; Therefore, need to set the cache afterwards.
2453 (setq ange-ftp-host-cache host
2454 ange-ftp-host-type-cache 'vms))
2455
2456 ;; try for MTS
2457 ((string-match ange-ftp-mts-name-template dir)
2458 (ange-ftp-add-mts-host host)
2459 (setq ange-ftp-host-cache host
2460 ange-ftp-host-type-cache 'mts))
25759a92 2461
8749abea
GM
2462 ;; try for CMS
2463 ((string-match ange-ftp-cms-name-template dir)
2464 (ange-ftp-add-cms-host host)
2465 (setq ange-ftp-host-cache host
2466 ange-ftp-host-type-cache 'cms))
2467
9c35d706
RS
2468 ;; try for BS2000-POSIX
2469 ((ange-ftp-bs2000-posix-host host)
2470 (ange-ftp-add-bs2000-host host)
2471 (setq ange-ftp-host-cache host
2472 ange-ftp-host-type-cache 'text-unix))
2473 ;; try for BS2000
2474 ((and (string-match ange-ftp-bs2000-name-template dir)
2475 (not (ange-ftp-bs2000-posix-host host)))
2476 (ange-ftp-add-bs2000-host host)
2477 (setq ange-ftp-host-cache host
2478 ange-ftp-host-type-cache 'bs2000))
8749abea
GM
2479 ;; assume UN*X
2480 (t
2481 (setq ange-ftp-host-cache host
2482 ange-ftp-host-type-cache 'unix)))
2483
2484 ;; Now that we have done a pwd, might as well put it in
2485 ;; the expand-dir hashtable.
2486 (let ((ange-ftp-this-user user)
2487 (ange-ftp-this-host host))
2488 (setq fix-name-func (cdr (assq ange-ftp-host-type-cache
2489 ange-ftp-fix-name-func-alist)))
2490 (if fix-name-func
2491 (setq dir (funcall fix-name-func dir 'reverse))))
6e85ef5f 2492 (puthash key dir ange-ftp-expand-dir-hashtable))))
8749abea
GM
2493
2494 ;; In the special case of CMS make sure that know the
2495 ;; expansion of the home minidisk now, because we will
2496 ;; be doing a lot of cd's.
2497 (if (and (eq host-type 'cms)
2498 (not (ange-ftp-hash-entry-exists-p
2499 key ange-ftp-expand-dir-hashtable)))
2500 (let ((dir (car (ange-ftp-get-pwd host user))))
2501 (if dir
6e85ef5f 2502 (puthash key (concat "/" dir) ange-ftp-expand-dir-hashtable)
8749abea
GM
2503 (message "Warning! Unable to get home directory")
2504 (sit-for 1))))))
2505
2506\f
2507;;;; ------------------------------------------------------------
2508;;;; Remote file and directory listing support.
2509;;;; ------------------------------------------------------------
2510
2511;; Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
2512;; to take switch arguments.
2513(defun ange-ftp-dumb-unix-host (host)
2514 (and host ange-ftp-dumb-unix-host-regexp
2515 (save-match-data
2516 (string-match ange-ftp-dumb-unix-host-regexp host))))
2517
2518(defun ange-ftp-add-dumb-unix-host (host)
2519 "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
2520 (interactive
2521 (list (read-string "Host: "
2522 (let ((name (or (buffer-file-name) default-directory)))
2523 (and name (car (ange-ftp-ftp-name name)))))))
2524 (if (not (ange-ftp-dumb-unix-host host))
2525 (setq ange-ftp-dumb-unix-host-regexp
2526 (concat "^" (regexp-quote host) "$"
2527 (and ange-ftp-dumb-unix-host-regexp "\\|")
2528 ange-ftp-dumb-unix-host-regexp)
2529 ange-ftp-host-cache nil)))
2530
2531(defvar ange-ftp-parse-list-func-alist nil
2532 "Alist saying how to parse directory listings for certain OS types.
2533Association list of \( TYPE \. FUNC \) pairs. The FUNC is a routine
2534which can parse the output from a DIR listing for a host of type TYPE.")
2535
2536;; With no-error nil, this function returns:
2537;; an error if file is not an ange-ftp-name
25759a92 2538;; (This should never happen.)
8749abea
GM
2539;; an error if either the listing is unreadable or there is an ftp error.
2540;; the listing (a string), if everything works.
25759a92 2541;;
8749abea
GM
2542;; With no-error t, it returns:
2543;; an error if not an ange-ftp-name
2544;; error if listing is unreadable (most likely caused by a slow connection)
2545;; nil if ftp error (this is because although asking to list a nonexistent
2546;; directory on a remote unix machine usually (except
2547;; maybe for dumb hosts) returns an ls error, but no
2548;; ftp error, if the same is done on a VMS machine,
2549;; an ftp error is returned. Need to trap the error
2550;; so we can go on and try to list the parent.)
2551;; the listing, if everything works.
2552
2553;; If WILDCARD is non-nil, then this implements the guts of insert-directory
2554;; in the wildcard case. Then we make a relative directory listing
2555;; of FILE within the directory specified by `default-directory'.
2556
2557(defvar ange-ftp-before-parse-ls-hook nil
2558 "Normal hook run before parsing the text of an ftp directory listing.")
2559
a724ed38
GM
2560(defvar ange-ftp-after-parse-ls-hook nil
2561 "Normal hook run after parsing the text of an ftp directory listing.")
2562
8749abea 2563(defun ange-ftp-ls (file lsargs parse &optional no-error wildcard)
67f300f8 2564 "Return the output of a `DIR' or `ls' command done over ftp.
8749abea
GM
2565FILE is the full name of the remote file, LSARGS is any args to pass to the
2566`ls' command, and PARSE specifies that the output should be parsed and stored
2567away in the internal cache."
1fc85dae
KG
2568 (when (string-match "^--dired\\s-+" lsargs)
2569 (setq lsargs (replace-match "" nil t lsargs)))
8749abea
GM
2570 ;; If parse is t, we assume that file is a directory. i.e. we only parse
2571 ;; full directory listings.
2572 (let* ((ange-ftp-this-file (ange-ftp-expand-file-name file))
2573 (parsed (ange-ftp-ftp-name ange-ftp-this-file)))
2574 (if parsed
2575 (let* ((host (nth 0 parsed))
2576 (user (nth 1 parsed))
2577 (name (ange-ftp-quote-string (nth 2 parsed)))
2578 (key (directory-file-name ange-ftp-this-file))
2579 (host-type (ange-ftp-host-type host user))
2580 (dumb (memq host-type ange-ftp-dumb-host-types))
2581 result
2582 temp
2583 lscmd parse-func)
2584 (if (string-equal name "")
2585 (setq name
2586 (ange-ftp-real-file-name-as-directory
6e85ef5f 2587 (ange-ftp-expand-dir host user "~"))))
8749abea
GM
2588 (if (and ange-ftp-ls-cache-file
2589 (string-equal key ange-ftp-ls-cache-file)
2590 ;; Don't care about lsargs for dumb hosts.
2591 (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
2592 ange-ftp-ls-cache-res
2593 (setq temp (ange-ftp-make-tmp-name host))
2594 (if wildcard
2595 (progn
2596 (ange-ftp-cd host user (file-name-directory name))
2597 (setq lscmd (list 'dir file temp lsargs)))
2598 (setq lscmd (list 'dir name temp lsargs)))
2599 (unwind-protect
2600 (if (car (setq result (ange-ftp-send-cmd
2601 host
2602 user
2603 lscmd
2604 (format "Listing %s"
2605 (ange-ftp-abbreviate-filename
2606 ange-ftp-this-file)))))
2607 (save-excursion
2608 (set-buffer (get-buffer-create
2609 ange-ftp-data-buffer-name))
2610 (erase-buffer)
2611 (if (ange-ftp-real-file-readable-p temp)
2612 (ange-ftp-real-insert-file-contents temp)
2613 (sleep-for ange-ftp-retry-time)
2614 ;wait for file to possibly appear
2615 (if (ange-ftp-real-file-readable-p temp)
2616 ;; Try again.
2617 (ange-ftp-real-insert-file-contents temp)
2618 (ange-ftp-error host user
2619 (format
2620 "list data file %s not readable"
2621 temp))))
9d453139
SS
2622 ;; remove ^M inserted by the win32 ftp client
2623 (while (re-search-forward "\r$" nil t)
2624 (replace-match ""))
2625 (goto-char 1)
8749abea
GM
2626 (run-hooks 'ange-ftp-before-parse-ls-hook)
2627 (if parse
2628 (ange-ftp-set-files
2629 ange-ftp-this-file
2630 (if (setq
2631 parse-func
2632 (cdr (assq host-type
2633 ange-ftp-parse-list-func-alist)))
2634 (funcall parse-func)
2635 (ange-ftp-parse-dired-listing lsargs))))
a724ed38
GM
2636 ;; Place this hook here to convert the contents of the
2637 ;; buffer to a ls compatible format if the host system
2638 ;; that is being queried is other than Unix i.e. VMS
2639 ;; returns an ls format that really sucks.
2640 (run-hooks 'ange-ftp-after-parse-ls-hook)
8749abea
GM
2641 (setq ange-ftp-ls-cache-file key
2642 ange-ftp-ls-cache-lsargs lsargs
2643 ; For dumb hosts-types this is
2644 ; meaningless but harmless.
2645 ange-ftp-ls-cache-res (buffer-string))
2646 ;; (kill-buffer (current-buffer))
67f300f8
SM
2647 (if (equal ange-ftp-ls-cache-res "total 0\n")
2648 ;; wu-ftpd seems to return a successful result
2649 ;; with an empty file-listing when doing a
2650 ;; `DIR /some/file/.' which leads ange-ftp to
2651 ;; believe that /some/file is a directory ;-(
2652 nil
2653 ange-ftp-ls-cache-res))
8749abea
GM
2654 (if no-error
2655 nil
2656 (ange-ftp-error host user
2657 (concat "DIR failed: " (cdr result)))))
2658 (ange-ftp-del-tmp-name temp))))
2659 (error "Should never happen. Please report. Bug ref. no.: 1"))))
2660\f
2661;;;; ------------------------------------------------------------
2662;;;; Directory information caching support.
2663;;;; ------------------------------------------------------------
2664
2665(defconst ange-ftp-date-regexp
2666 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2667 ;; In some locales, month abbreviations are as short as 2 letters,
2668 ;; and they can be padded on the right with spaces.
2669 ;; weiand: changed: month ends with . or , or .,
2670;;old (month (concat l l "+ *"))
2671 (month (concat l l "+[.]?,? *"))
25759a92 2672 ;; Recognize any non-ASCII character.
8749abea
GM
2673 ;; The purpose is to match a Kanji character.
2674 (k "[^\0-\177]")
2675 (s " ")
2676 (mm "[ 0-1][0-9]")
2677 ;; weiand: changed: day ends with .
2678;;old (dd "[ 0-3][0-9]")
2679 (dd "[ 0-3][0-9][.]?")
2680 (western (concat "\\(" month s dd "\\|" dd s month "\\)"))
2681 (japanese (concat mm k s dd k)))
2682 ;; Require the previous column to end in a digit.
2683 ;; This avoids recognizing `1 may 1997' as a date in the line:
2684 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
2685 (concat "[0-9]" s "\\(" western "\\|" japanese "\\)" s))
2686 "Regular expression to match up to the column before the file name in a
2687directory listing. This regular expression is designed to recognize dates
2688regardless of the language.")
2689
2690(defvar ange-ftp-add-file-entry-alist nil
2691 "Alist saying how to add file entries on certain OS types.
2692Association list of pairs \( TYPE \. FUNC \), where FUNC
2693is a function to be used to add a file entry for the OS TYPE. The
2694main reason for this alist is to deal with file versions in VMS.")
2695
2696(defvar ange-ftp-delete-file-entry-alist nil
2697 "Alist saying how to delete files on certain OS types.
2698Association list of pairs \( TYPE \. FUNC \), where FUNC
2699is a function to be used to delete a file entry for the OS TYPE.
2700The main reason for this alist is to deal with file versions in VMS.")
2701
2702(defun ange-ftp-add-file-entry (name &optional dir-p)
2703 "Add a file entry for file NAME, if its directory info exists."
2704 (funcall (or (cdr (assq (ange-ftp-host-type
2705 (car (ange-ftp-ftp-name name)))
2706 ange-ftp-add-file-entry-alist))
2707 'ange-ftp-internal-add-file-entry)
2708 name dir-p)
2709 (setq ange-ftp-ls-cache-file nil))
2710
2711(defun ange-ftp-delete-file-entry (name &optional dir-p)
2712 "Delete the file entry for file NAME, if its directory info exists."
2713 (funcall (or (cdr (assq (ange-ftp-host-type
2714 (car (ange-ftp-ftp-name name)))
2715 ange-ftp-delete-file-entry-alist))
2716 'ange-ftp-internal-delete-file-entry)
2717 name dir-p)
2718 (setq ange-ftp-ls-cache-file nil))
2719
2720(defmacro ange-ftp-parse-filename ()
2721 ;;Extract the filename from the current line of a dired-like listing.
25759a92
SS
2722 `(let ((eol (progn (end-of-line) (point))))
2723 (beginning-of-line)
2724 (if (re-search-forward ange-ftp-date-regexp eol t)
2725 (progn
2726 (skip-chars-forward " ")
2727 (skip-chars-forward "^ " eol)
2728 (skip-chars-forward " " eol)
2729 ;; We bomb on filenames starting with a space.
2730 (buffer-substring (point) eol)))))
2731
8749abea
GM
2732;; This deals with the F switch. Should also do something about
2733;; unquoting names obtained with the SysV b switch and the GNU Q
2734;; switch. See Sebastian's dired-get-filename.
2735
e3441f42 2736(defun ange-ftp-ls-parser (switches)
8749abea 2737 ;; Meant to be called by ange-ftp-parse-dired-listing
6e85ef5f 2738 (let ((tbl (make-hash-table :test 'equal))
c8607dc7
SM
2739 (used-F (and (stringp switches)
2740 (string-match "F" switches)))
2741 file-type symlink directory file)
2742 (while (setq file (ange-ftp-parse-filename))
2743 (beginning-of-line)
2744 (skip-chars-forward "\t 0-9")
2745 (setq file-type (following-char)
2746 directory (eq file-type ?d))
2747 (if (eq file-type ?l)
2748 (let ((end (string-match " -> " file)))
2749 (if end
2750 ;; Sometimes `ls' appends a @ at the end of the target.
2751 (setq symlink (substring file (match-end 0)
2752 (string-match "@\\'" file))
2753 file (substring file 0 end))
2754 ;; Shouldn't happen
2755 (setq symlink "")))
2756 (setq symlink nil))
2757 ;; Only do a costly regexp search if the F switch was used.
2758 (if (and used-F
2759 (not (string-equal file ""))
2760 (looking-at
2761 ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
2762 (let ((socket (eq file-type ?s))
2763 (executable
2764 (and (not symlink) ; x bits don't mean a thing for symlinks
2765 (string-match
2766 "[xst]"
e3441f42
SM
2767 (concat (match-string 1)
2768 (match-string 2)
2769 (match-string 3))))))
c8607dc7
SM
2770 ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
2771 ;; and others don't. (sigh...) Beware, that some Unix's don't
2772 ;; seem to believe in the F-switch
2773 (if (or (and symlink (string-match "@$" file))
2774 (and directory (string-match "/$" file))
2775 (and executable (string-match "*$" file))
2776 (and socket (string-match "=$" file)))
2777 (setq file (substring file 0 -1)))))
6e85ef5f 2778 (puthash file (or symlink directory) tbl)
c8607dc7 2779 (forward-line 1))
6e85ef5f
SM
2780 (puthash "." t tbl)
2781 (puthash ".." t tbl)
25759a92 2782 tbl))
8749abea
GM
2783
2784;;; The dl stuff for descriptive listings
2785
2786(defvar ange-ftp-dl-dir-regexp nil
2787 "Regexp matching directories which are listed in dl format.
2788This regexp should not be anchored with a trailing `$', because it should
2789match subdirectories as well.")
2790
2791(defun ange-ftp-add-dl-dir (dir)
2792 "Interactively adds a DIR to ange-ftp-dl-dir-regexp."
2793 (interactive
2794 (list (read-string "Directory: "
2795 (let ((name (or (buffer-file-name) default-directory)))
2796 (and name (ange-ftp-ftp-name name)
2797 (file-name-directory name))))))
2798 (if (not (and ange-ftp-dl-dir-regexp
2799 (string-match ange-ftp-dl-dir-regexp dir)))
2800 (setq ange-ftp-dl-dir-regexp
2801 (concat "^" (regexp-quote dir)
2802 (and ange-ftp-dl-dir-regexp "\\|")
2803 ange-ftp-dl-dir-regexp))))
2804
2805(defmacro ange-ftp-dl-parser ()
2806 ;; Parse the current buffer, which is assumed to be a descriptive
2807 ;; listing, and return a hashtable.
6e85ef5f 2808 `(let ((tbl (make-hash-table :test 'equal)))
25759a92 2809 (while (not (eobp))
6e85ef5f 2810 (puthash
25759a92
SS
2811 (buffer-substring (point)
2812 (progn
2813 (skip-chars-forward "^ /\n")
2814 (point)))
2815 (eq (following-char) ?/)
2816 tbl)
2817 (forward-line 1))
6e85ef5f
SM
2818 (puthash "." t tbl)
2819 (puthash ".." t tbl)
2820 tbl))
8749abea
GM
2821
2822;; Parse the current buffer which is assumed to be in a dired-like listing
2823;; format, and return a hashtable as the result. If the listing is not really
2824;; a listing, then return nil.
2825
2826(defun ange-ftp-parse-dired-listing (&optional switches)
2827 (save-match-data
2828 (cond
2829 ((looking-at "^total [0-9]+$")
2830 (forward-line 1)
2831 ;; Some systems put in a blank line here.
2832 (if (eolp) (forward-line 1))
e3441f42 2833 (ange-ftp-ls-parser switches))
8749abea
GM
2834 ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
2835 ;; It's an ls error message.
2836 nil)
2837 ((eobp) ; i.e. (zerop (buffer-size))
2838 ;; This could be one of:
2839 ;; (1) An Ultrix ls error message
2840 ;; (2) A listing with the A switch of an empty directory
2841 ;; on a machine which doesn't give a total line.
2842 ;; (3) The twilight zone.
2843 ;; We'll assume (1) for now.
2844 nil)
2845 ((re-search-forward ange-ftp-date-regexp nil t)
2846 (beginning-of-line)
e3441f42 2847 (ange-ftp-ls-parser switches))
8749abea
GM
2848 ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
2849 ;; It's a dl listing (I hope).
2850 ;; file is bound by the call to ange-ftp-ls
2851 (ange-ftp-add-dl-dir ange-ftp-this-file)
2852 (beginning-of-line)
2853 (ange-ftp-dl-parser))
2854 (t nil))))
2855
2856(defun ange-ftp-set-files (directory files)
2857 "For a given DIRECTORY, set or change the associated FILES hashtable."
6e85ef5f
SM
2858 (and files (puthash (file-name-as-directory directory)
2859 files ange-ftp-files-hashtable)))
8749abea
GM
2860
2861(defun ange-ftp-get-files (directory &optional no-error)
2862 "Given a given DIRECTORY, return a hashtable of file entries.
2863This will give an error or return nil, depending on the value of
2864NO-ERROR, if a listing for DIRECTORY cannot be obtained."
2865 (setq directory (file-name-as-directory directory)) ;normalize
6e85ef5f 2866 (or (gethash directory ange-ftp-files-hashtable)
8749abea
GM
2867 (save-match-data
2868 (and (ange-ftp-ls directory
2869 ;; This is an efficiency hack. We try to
2870 ;; anticipate what sort of listing dired
2871 ;; might want, and cache just such a listing.
2872 (if (and (boundp 'dired-actual-switches)
2873 (stringp dired-actual-switches)
2874 ;; We allow the A switch, which lists
2875 ;; all files except "." and "..".
2876 ;; This is OK because we manually
2877 ;; insert these entries
2878 ;; in the hash table.
2879 (string-match
2880 "[aA]" dired-actual-switches)
2881 (string-match
2882 "l" dired-actual-switches)
2883 (not (string-match
2884 "R" dired-actual-switches)))
2885 dired-actual-switches
2886 (if (and (boundp 'dired-listing-switches)
2887 (stringp dired-listing-switches)
2888 (string-match
2889 "[aA]" dired-listing-switches)
2890 (string-match
2891 "l" dired-listing-switches)
2892 (not (string-match
2893 "R" dired-listing-switches)))
2894 dired-listing-switches
2895 "-al"))
2896 t no-error)
6e85ef5f 2897 (gethash directory ange-ftp-files-hashtable)))))
8749abea
GM
2898
2899;; Given NAME, return the file part that can be used for looking up the
2900;; file's entry in a hashtable.
2901(defmacro ange-ftp-get-file-part (name)
25759a92
SS
2902 `(let ((file (file-name-nondirectory ,name)))
2903 (if (string-equal file "")
e3441f42 2904 "."
25759a92 2905 file)))
8749abea
GM
2906
2907;; Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
2908;; allowed to determine if NAME is a sub-directory by listing it directly,
2909;; rather than listing its parent directory. This is used for efficiency so
2910;; that a wasted listing is not done:
2911;; 1. When looking for a .dired file in dired-x.el.
2912;; 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
2913;; subdirectory. This is of course an OS dependent judgement.
2914
e3441f42 2915(defvar dired-local-variables-file)
8749abea 2916(defmacro ange-ftp-allow-child-lookup (dir file)
76a98a50
SM
2917 `(not
2918 (let* ((efile ,file) ; expand once.
2919 (edir ,dir)
2920 (parsed (ange-ftp-ftp-name edir))
2921 (host-type (ange-ftp-host-type
2922 (car parsed))))
2923 (or
2924 ;; Deal with dired
2925 (and (boundp 'dired-local-variables-file) ; in the dired-x package
2926 (stringp dired-local-variables-file)
2927 (string-equal dired-local-variables-file efile))
2928 ;; No dots in dir names in vms.
2929 (and (eq host-type 'vms)
2930 (string-match "\\." efile))
2931 ;; No subdirs in mts of cms.
2932 (and (memq host-type '(mts cms))
2933 (not (string-equal "/" (nth 2 parsed))))
2934 ;; No dots in pseudo-dir names in bs2000.
2935 (and (eq host-type 'bs2000)
2936 (string-match "\\." efile))))))
8749abea
GM
2937
2938(defun ange-ftp-file-entry-p (name)
2939 "Given NAME, return whether there is a file entry for it."
2940 (let* ((name (directory-file-name name))
2941 (dir (file-name-directory name))
6e85ef5f 2942 (ent (gethash dir ange-ftp-files-hashtable))
8749abea
GM
2943 (file (ange-ftp-get-file-part name)))
2944 (if ent
2945 (ange-ftp-hash-entry-exists-p file ent)
2946 (or (and (ange-ftp-allow-child-lookup dir file)
2947 (setq ent (ange-ftp-get-files name t))
2948 ;; Try a child lookup. i.e. try to list file as a
2949 ;; subdirectory of dir. This is a good idea because
2950 ;; we may not have read permission for file's parent. Also,
2951 ;; people tend to work down directory trees anyway. We use
2952 ;; no-error ;; because if file does not exist as a subdir.,
2953 ;; then dumb hosts will give an ftp error. Smart unix hosts
2954 ;; will simply send back the ls
2955 ;; error message.
6e85ef5f 2956 (gethash "." ent))
8749abea 2957 ;; Child lookup failed, so try the parent.
311f32fe
DK
2958 (ange-ftp-hash-entry-exists-p
2959 file (ange-ftp-get-files dir 'no-error))))))
8749abea
GM
2960
2961(defun ange-ftp-get-file-entry (name)
2962 "Given NAME, return the given file entry.
2963The entry will be either t for a directory, nil for a normal file,
2964or a string for a symlink. If the file isn't in the hashtable,
2965this also returns nil."
2966 (let* ((name (directory-file-name name))
2967 (dir (file-name-directory name))
6e85ef5f 2968 (ent (gethash dir ange-ftp-files-hashtable))
8749abea
GM
2969 (file (ange-ftp-get-file-part name)))
2970 (if ent
6e85ef5f 2971 (gethash file ent)
8749abea
GM
2972 (or (and (ange-ftp-allow-child-lookup dir file)
2973 (setq ent (ange-ftp-get-files name t))
6e85ef5f
SM
2974 (gethash "." ent))
2975 ;; i.e. it's a directory by child lookup
57199263
DK
2976 (and (setq ent (ange-ftp-get-files dir t))
2977 (gethash file ent))))))
8749abea
GM
2978
2979(defun ange-ftp-internal-delete-file-entry (name &optional dir-p)
6e85ef5f
SM
2980 (when dir-p
2981 (setq name (file-name-as-directory name))
2982 (remhash name ange-ftp-files-hashtable)
2983 (setq name (directory-file-name name)))
8749abea
GM
2984 ;; Note that file-name-as-directory followed by directory-file-name
2985 ;; serves to canonicalize directory file names to their unix form.
2986 ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
6e85ef5f 2987 (let ((files (gethash (file-name-directory name) ange-ftp-files-hashtable)))
8749abea 2988 (if files
6e85ef5f 2989 (remhash (ange-ftp-get-file-part name) files))))
8749abea
GM
2990
2991(defun ange-ftp-internal-add-file-entry (name &optional dir-p)
2992 (and dir-p
2993 (setq name (directory-file-name name)))
6e85ef5f 2994 (let ((files (gethash (file-name-directory name) ange-ftp-files-hashtable)))
8749abea 2995 (if files
6e85ef5f 2996 (puthash (ange-ftp-get-file-part name) dir-p files))))
8749abea
GM
2997
2998(defun ange-ftp-wipe-file-entries (host user)
2999 "Get rid of entry for HOST, USER pair from file entry information hashtable."
6e85ef5f 3000 (let ((new-tbl (make-hash-table :test 'equal
7adee48a
DK
3001 :size (hash-table-size
3002 ange-ftp-files-hashtable))))
6e85ef5f 3003 (maphash
25759a92
SS
3004 (lambda (key val)
3005 (let ((parsed (ange-ftp-ftp-name key)))
3006 (if parsed
3007 (let ((h (nth 0 parsed))
3008 (u (nth 1 parsed)))
3009 (or (and (equal host h) (equal user u))
6e85ef5f 3010 (puthash key val new-tbl))))))
8749abea
GM
3011 ange-ftp-files-hashtable)
3012 (setq ange-ftp-files-hashtable new-tbl)))
3013\f
3014;;;; ------------------------------------------------------------
3015;;;; File transfer mode support.
3016;;;; ------------------------------------------------------------
3017
3018(defun ange-ftp-set-binary-mode (host user)
3019 "Tell the ftp process for the given HOST & USER to switch to binary mode."
3020 (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
3021 (if (not (car result))
3022 (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
3023 (save-excursion
3024 (set-buffer (process-buffer (ange-ftp-get-process host user)))
3025 (and ange-ftp-binary-hash-mark-size
3026 (setq ange-ftp-hash-mark-unit
3027 (ash ange-ftp-binary-hash-mark-size -4)))))))
3028
3029(defun ange-ftp-set-ascii-mode (host user)
3030 "Tell the ftp process for the given HOST & USER to switch to ascii mode."
3031 (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
3032 (if (not (car result))
3033 (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
3034 (save-excursion
3035 (set-buffer (process-buffer (ange-ftp-get-process host user)))
3036 (and ange-ftp-ascii-hash-mark-size
3037 (setq ange-ftp-hash-mark-unit
3038 (ash ange-ftp-ascii-hash-mark-size -4)))))))
3039\f
67f300f8 3040(defun ange-ftp-cd (host user dir &optional noerror)
8749abea 3041 (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
67f300f8
SM
3042 (if noerror result
3043 (or (car result)
3044 (ange-ftp-error host user (concat "CD failed: " (cdr result)))))))
8749abea
GM
3045
3046(defun ange-ftp-get-pwd (host user)
3047 "Attempts to get the current working directory for the given HOST/USER pair.
3048Returns \( DIR . LINE \) where DIR is either the directory or nil if not found,
3049and LINE is the relevant success or fail line from the FTP-client."
3050 (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
3051 (line (cdr result))
3052 dir)
3053 (if (car result)
3054 (save-match-data
3055 (and (or (string-match "\"\\([^\"]*\\)\"" line)
e3441f42
SM
3056 (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
3057 (setq dir (match-string 1 line)))))
8749abea
GM
3058 (cons dir line)))
3059\f
3060;;; ------------------------------------------------------------
3061;;; expand-file-name and friends...which currently don't work
3062;;; ------------------------------------------------------------
3063
3064(defun ange-ftp-expand-dir (host user dir)
3065 "Return the result of doing a PWD in the current FTP session.
3066Use the connection to machine HOST
3067logged in as user USER and cd'd to directory DIR."
3068 (let* ((host-type (ange-ftp-host-type host user))
3069 ;; It is more efficient to call ange-ftp-host-type
3070 ;; before binding res, because ange-ftp-host-type sometimes
3071 ;; adds to the info in the expand-dir-hashtable.
3072 (fix-name-func
3073 (cdr (assq host-type ange-ftp-fix-name-func-alist)))
3074 (key (concat host "/" user "/" dir))
6e85ef5f 3075 (res (gethash key ange-ftp-expand-dir-hashtable)))
8749abea
GM
3076 (or res
3077 (progn
3078 (or
3079 (string-equal user "anonymous")
3080 (string-equal user "ftp")
3081 (not (eq host-type 'unix))
3082 (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
3083 "\\|"
3084 ange-ftp-good-msgs))
3085 (result (ange-ftp-send-cmd host user
3086 (list 'get dir null-device)
3087 (format "expanding %s" dir)))
3088 (line (cdr result)))
3089 (setq res
3090 (if (string-match ange-ftp-expand-dir-regexp line)
e3441f42 3091 (match-string 1 line)))))
8749abea
GM
3092 (or res
3093 (if (string-equal dir "~")
3094 (setq res (car (ange-ftp-get-pwd host user)))
3095 (let ((home (ange-ftp-expand-dir host user "~")))
3096 (unwind-protect
3097 (and (ange-ftp-cd host user dir)
3098 (setq res (car (ange-ftp-get-pwd host user))))
3099 (ange-ftp-cd host user home)))))
3100 (if res
3101 (let ((ange-ftp-this-user user)
3102 (ange-ftp-this-host host))
3103 (if fix-name-func
3104 (setq res (funcall fix-name-func res 'reverse)))
6e85ef5f 3105 (puthash key res ange-ftp-expand-dir-hashtable)))
8749abea
GM
3106 res))))
3107
3108(defun ange-ftp-canonize-filename (n)
3109 "Take a string and short-circuit //, /. and /.."
3110 (if (string-match "[^:]+//" n) ;don't upset Apollo users
3111 (setq n (substring n (1- (match-end 0)))))
3112 (let ((parsed (ange-ftp-ftp-name n)))
3113 (if parsed
3114 (let ((host (car parsed))
3115 (user (nth 1 parsed))
3116 (name (nth 2 parsed)))
25759a92 3117
8749abea
GM
3118 ;; See if remote name is absolute. If so then just expand it and
3119 ;; replace the name component of the overall name.
3120 (cond ((string-match "^/" name)
3121 name)
25759a92 3122
8749abea
GM
3123 ;; Name starts with ~ or ~user. Resolve that part of the name
3124 ;; making it absolute then re-expand it.
3125 ((string-match "^~[^/]*" name)
e3441f42 3126 (let* ((tilda (match-string 0 name))
8749abea
GM
3127 (rest (substring name (match-end 0)))
3128 (dir (ange-ftp-expand-dir host user tilda)))
3129 (if dir
8012c835
MA
3130 (setq name (cond ((string-equal rest "")
3131 dir)
3132 ((string-equal dir "/")
3133 rest)
3134 (t
3135 (concat dir rest))))
25759a92 3136 (error "User \"%s\" is not known"
8749abea 3137 (substring tilda 1)))))
25759a92 3138
8749abea
GM
3139 ;; relative name. Tack on homedir and re-expand.
3140 (t
3141 (let ((dir (ange-ftp-expand-dir host user "~")))
3142 (if dir
3143 (setq name (concat
3144 (ange-ftp-real-file-name-as-directory dir)
3145 name))
3146 (error "Unable to obtain CWD")))))
25759a92 3147
8749abea
GM
3148 ;; If name starts with //, preserve that, for apollo system.
3149 (if (not (string-match "^//" name))
3150 (progn
3151 (if (not (eq system-type 'windows-nt))
3152 (setq name (ange-ftp-real-expand-file-name name))
3153 ;; Windows UNC default dirs do not make sense for ftp.
3154 (if (string-match "^//" default-directory)
3155 (setq name (ange-ftp-real-expand-file-name name "c:/"))
3156 (setq name (ange-ftp-real-expand-file-name name)))
3157 ;; Strip off possible drive specifier.
3158 (if (string-match "^[a-zA-Z]:" name)
3159 (setq name (substring name 2))))
3160 (if (string-match "^//" name)
3161 (setq name (substring name 1)))))
25759a92 3162
8749abea
GM
3163 ;; Now substitute the expanded name back into the overall filename.
3164 (ange-ftp-replace-name-component n name))
25759a92 3165
8749abea
GM
3166 ;; non-ange-ftp name. Just expand normally.
3167 (if (eq (string-to-char n) ?/)
3168 (ange-ftp-real-expand-file-name n)
3169 (ange-ftp-real-expand-file-name
3170 (ange-ftp-real-file-name-nondirectory n)
3171 (ange-ftp-real-file-name-directory n))))))
3172
3173(defun ange-ftp-expand-file-name (name &optional default)
3174 "Documented as original."
3175 (save-match-data
3176 (setq default (or default default-directory))
3177 (cond ((eq (string-to-char name) ?~)
3178 (ange-ftp-real-expand-file-name name))
3179 ((eq (string-to-char name) ?/)
3180 (ange-ftp-canonize-filename name))
3181 ((and (eq system-type 'windows-nt)
3182 (eq (string-to-char name) ?\\))
3183 (ange-ftp-canonize-filename name))
3184 ((and (eq system-type 'windows-nt)
3185 (or (string-match "^[a-zA-Z]:" name)
3186 (string-match "^[a-zA-Z]:" default)))
3187 (ange-ftp-real-expand-file-name name default))
3188 ((zerop (length name))
3189 (ange-ftp-canonize-filename default))
3190 ((ange-ftp-canonize-filename
3191 (concat (file-name-as-directory default) name))))))
3192\f
3193;;; These are problems--they are currently not enabled.
3194
3195(defvar ange-ftp-file-name-as-directory-alist nil
3196 "Association list of \( TYPE \. FUNC \) pairs.
3197FUNC converts a filename to a directory name for the operating
3198system TYPE.")
3199
3200(defun ange-ftp-file-name-as-directory (name)
3201 "Documented as original."
3202 (let ((parsed (ange-ftp-ftp-name name)))
3203 (if parsed
3204 (if (string-equal (nth 2 parsed) "")
3205 name
3206 (funcall (or (cdr (assq
3207 (ange-ftp-host-type (car parsed))
3208 ange-ftp-file-name-as-directory-alist))
3209 'ange-ftp-real-file-name-as-directory)
3210 name))
3211 (ange-ftp-real-file-name-as-directory name))))
25759a92 3212
8749abea
GM
3213(defun ange-ftp-file-name-directory (name)
3214 "Documented as original."
3215 (let ((parsed (ange-ftp-ftp-name name)))
3216 (if parsed
3217 (let ((filename (nth 2 parsed)))
3218 (if (save-match-data
3219 (string-match "^~[^/]*$" filename))
3220 name
3221 (ange-ftp-replace-name-component
3222 name
3223 (ange-ftp-real-file-name-directory filename))))
3224 (ange-ftp-real-file-name-directory name))))
3225
3226(defun ange-ftp-file-name-nondirectory (name)
3227 "Documented as original."
3228 (let ((parsed (ange-ftp-ftp-name name)))
3229 (if parsed
3230 (let ((filename (nth 2 parsed)))
3231 (if (save-match-data
3232 (string-match "^~[^/]*$" filename))
3233 ""
3234 (ange-ftp-real-file-name-nondirectory filename)))
3235 (ange-ftp-real-file-name-nondirectory name))))
3236
3237(defun ange-ftp-directory-file-name (dir)
3238 "Documented as original."
3239 (let ((parsed (ange-ftp-ftp-name dir)))
3240 (if parsed
3241 (ange-ftp-replace-name-component
e3441f42
SM
3242 dir
3243 (ange-ftp-real-directory-file-name (nth 2 parsed)))
8749abea
GM
3244 (ange-ftp-real-directory-file-name dir))))
3245
3246\f
3247;;; Hooks that handle Emacs primitives.
3248
3249;; Returns non-nil if should transfer FILE in binary mode.
3250(defun ange-ftp-binary-file (file)
3251 (save-match-data
3252 (string-match ange-ftp-binary-file-name-regexp file)))
3253
3254(defun ange-ftp-write-region (start end filename &optional append visit)
3255 (setq filename (expand-file-name filename))
3256 (let ((parsed (ange-ftp-ftp-name filename)))
3257 (if parsed
3258 (let* ((host (nth 0 parsed))
3259 (user (nth 1 parsed))
3260 (name (ange-ftp-quote-string (nth 2 parsed)))
3261 (temp (ange-ftp-make-tmp-name host))
3262 ;; What we REALLY need here is a way to determine if the mode
3263 ;; of the transfer is irrelevant, i.e. we can use binary mode
3264 ;; regardless. Maybe a system-type to host-type lookup?
3265 (binary (or (ange-ftp-binary-file filename)
67f300f8
SM
3266 (and (not (memq system-type
3267 '(ms-dos windows-nt macos vax-vms)))
3268 (memq (ange-ftp-host-type host user)
3269 '(unix dumb-unix)))))
8749abea
GM
3270 (cmd (if append 'append 'put))
3271 (abbr (ange-ftp-abbreviate-filename filename))
3272 ;; we need to reset `last-coding-system-used' to its
3273 ;; value immediately after calling the real write-region,
3274 ;; so that `basic-save-buffer' doesn't see whatever value
3275 ;; might be used when communicating with the ftp process.
3276 (coding-system-used last-coding-system-used))
3277 (unwind-protect
3278 (progn
4494f50d 3279 (let ((filename (buffer-file-name))
8749abea
GM
3280 (mod-p (buffer-modified-p)))
3281 (unwind-protect
2ca0d5f1 3282 (progn
4494f50d
RS
3283 (ange-ftp-real-write-region start end temp nil
3284 (or visit 'quiet))
2ca0d5f1 3285 (setq coding-system-used last-coding-system-used))
8749abea 3286 ;; cleanup forms
2ca0d5f1 3287 (setq coding-system-used last-coding-system-used)
8749abea
GM
3288 (setq buffer-file-name filename)
3289 (set-buffer-modified-p mod-p)))
8749abea
GM
3290 (if binary
3291 (ange-ftp-set-binary-mode host user))
3292
3293 ;; tell the process filter what size the transfer will be.
3294 (let ((attr (file-attributes temp)))
3295 (if attr
3296 (ange-ftp-set-xfer-size host user (nth 7 attr))))
3297
3298 ;; put or append the file.
3299 (let ((result (ange-ftp-send-cmd host user
3300 (list cmd temp name)
3301 (format "Writing %s" abbr))))
3302 (or (car result)
3303 (signal 'ftp-error
3304 (list
3305 "Opening output file"
3306 (format "FTP Error: \"%s\"" (cdr result))
3307 filename)))))
3308 (ange-ftp-del-tmp-name temp)
25759a92 3309 (if binary
8749abea
GM
3310 (ange-ftp-set-ascii-mode host user)))
3311 (if (eq visit t)
3312 (progn
25759a92 3313 (set-visited-file-modtime (ange-ftp-file-modtime filename))
8749abea
GM
3314 (ange-ftp-set-buffer-mode)
3315 (setq buffer-file-name filename)
3316 (set-buffer-modified-p nil)))
3317 ;; ensure `last-coding-system-used' has an appropriate value
3318 (setq last-coding-system-used coding-system-used)
3319 (ange-ftp-message "Wrote %s" abbr)
3320 (ange-ftp-add-file-entry filename))
3321 (ange-ftp-real-write-region start end filename append visit))))
3322
3323(defun ange-ftp-insert-file-contents (filename &optional visit beg end replace)
3324 (barf-if-buffer-read-only)
3325 (setq filename (expand-file-name filename))
3326 (let ((parsed (ange-ftp-ftp-name filename)))
3327 (if parsed
3328 (progn
3329 (if visit
3330 (setq buffer-file-name filename))
3331 (if (or (file-exists-p filename)
3332 (progn
3333 (setq ange-ftp-ls-cache-file nil)
6e85ef5f
SM
3334 (remhash (file-name-directory filename)
3335 ange-ftp-files-hashtable)
8749abea
GM
3336 (file-exists-p filename)))
3337 (let* ((host (nth 0 parsed))
3338 (user (nth 1 parsed))
3339 (name (ange-ftp-quote-string (nth 2 parsed)))
3340 (temp (ange-ftp-make-tmp-name host))
3341 (binary (or (ange-ftp-binary-file filename)
3342 (memq (ange-ftp-host-type host user)
3343 '(unix dumb-unix))))
3344 (abbr (ange-ftp-abbreviate-filename filename))
3345 (coding-system-used last-coding-system-used)
3346 size)
3347 (unwind-protect
3348 (progn
3349 (if binary
3350 (ange-ftp-set-binary-mode host user))
3351 (let ((result (ange-ftp-send-cmd host user
3352 (list 'get name temp)
3353 (format "Retrieving %s" abbr))))
3354 (or (car result)
3355 (signal 'ftp-error
3356 (list
3357 "Opening input file"
3358 (format "FTP Error: \"%s\"" (cdr result))
3359 filename))))
3360 (if (or (ange-ftp-real-file-readable-p temp)
3361 (sleep-for ange-ftp-retry-time)
3362 ;; Wait for file to hopefully appear.
3363 (ange-ftp-real-file-readable-p temp))
3364 (setq
3365 size
3366 (nth 1 (ange-ftp-real-insert-file-contents
3367 temp visit beg end replace))
3368 coding-system-used last-coding-system-used
3369 ;; override autodetection of buffer file type
3370 ;; to ensure buffer is saved in DOS format
3371 buffer-file-type binary)
3372 (signal 'ftp-error
3373 (list
3374 "Opening input file:"
3375 (format
3376 "FTP Error: %s not arrived or readable"
3377 filename)))))
3378 (if binary
3379 ;; We must keep `last-coding-system-used'
3380 ;; unchanged.
3381 (let (last-coding-system-used)
3382 (ange-ftp-set-ascii-mode host user)))
3383 (ange-ftp-del-tmp-name temp))
3384 (if visit
3385 (progn
25759a92
SS
3386 (set-visited-file-modtime
3387 (ange-ftp-file-modtime filename))
8749abea
GM
3388 (setq buffer-file-name filename)))
3389 (setq last-coding-system-used coding-system-used)
3390 (list filename size))
3391 (signal 'file-error
25759a92 3392 (list
8749abea
GM
3393 "Opening input file"
3394 filename))))
3395 (ange-ftp-real-insert-file-contents filename visit beg end replace))))
25759a92 3396
8749abea 3397(defun ange-ftp-expand-symlink (file dir)
64cf2455
KG
3398 (let ((res (if (file-name-absolute-p file)
3399 (ange-ftp-replace-name-component dir file)
3400 (expand-file-name file dir))))
3401 (if (file-symlink-p res)
3402 (ange-ftp-expand-symlink
3403 (ange-ftp-get-file-entry res)
3404 (file-name-directory (directory-file-name res)))
3405 res)))
8749abea
GM
3406
3407(defun ange-ftp-file-symlink-p (file)
3408 ;; call ange-ftp-expand-file-name rather than the normal
3409 ;; expand-file-name to stop loops when using a package that
3410 ;; redefines both file-symlink-p and expand-file-name.
3411 (setq file (ange-ftp-expand-file-name file))
3412 (if (ange-ftp-ftp-name file)
34301ace 3413 (condition-case nil
311f32fe
DK
3414 (let ((ent (ange-ftp-get-files (file-name-directory file))))
3415 (and ent
3416 (stringp (setq ent
3417 (gethash (ange-ftp-get-file-part file) ent)))
3418 ent))
34301ace
RS
3419 ;; If we can't read the parent directory, just assume
3420 ;; this file is not a symlink.
3421 ;; This makes it possible to access a directory that
3422 ;; whose parent is not readable.
3423 (file-error nil))
8749abea
GM
3424 (ange-ftp-real-file-symlink-p file)))
3425
3426(defun ange-ftp-file-exists-p (name)
3427 (setq name (expand-file-name name))
3428 (if (ange-ftp-ftp-name name)
3429 (if (ange-ftp-file-entry-p name)
3430 (let ((file-ent (ange-ftp-get-file-entry name)))
3431 (if (stringp file-ent)
3432 (file-exists-p
3433 (ange-ftp-expand-symlink file-ent
3434 (file-name-directory
3435 (directory-file-name name))))
3436 t)))
3437 (ange-ftp-real-file-exists-p name)))
3438
3439(defun ange-ftp-file-directory-p (name)
3440 (setq name (expand-file-name name))
3441 (if (ange-ftp-ftp-name name)
3442 ;; We do a file-name-as-directory on name here because some
3443 ;; machines (VMS) use a .DIR to indicate the filename associated
3444 ;; with a directory. This needs to be canonicalized.
3445 (let ((file-ent (ange-ftp-get-file-entry
3446 (ange-ftp-file-name-as-directory name))))
3447 (if (stringp file-ent)
3448 (file-directory-p
3449 (ange-ftp-expand-symlink file-ent
3450 (file-name-directory
3451 (directory-file-name name))))
3452 file-ent))
3453 (ange-ftp-real-file-directory-p name)))
3454
3455(defun ange-ftp-directory-files (directory &optional full match
3456 &rest v19-args)
3457 (setq directory (expand-file-name directory))
3458 (if (ange-ftp-ftp-name directory)
3459 (progn
3460 (ange-ftp-barf-if-not-directory directory)
3461 (let ((tail (ange-ftp-hash-table-keys
3462 (ange-ftp-get-files directory)))
3463 files f)
3464 (setq directory (file-name-as-directory directory))
3465 (save-match-data
3466 (while tail
3467 (setq f (car tail)
3468 tail (cdr tail))
3469 (if (or (not match) (string-match match f))
3470 (setq files
3471 (cons (if full (concat directory f) f) files)))))
3472 (nreverse files)))
3473 (apply 'ange-ftp-real-directory-files directory full match v19-args)))
3474
c4383667 3475(defun ange-ftp-file-attributes (file &optional id-format)
8749abea
GM
3476 (setq file (expand-file-name file))
3477 (let ((parsed (ange-ftp-ftp-name file)))
3478 (if parsed
3479 (let ((part (ange-ftp-get-file-part file))
3480 (files (ange-ftp-get-files (file-name-directory file))))
3481 (if (ange-ftp-hash-entry-exists-p part files)
3482 (let ((host (nth 0 parsed))
3483 (user (nth 1 parsed))
3484 (name (nth 2 parsed))
6e85ef5f
SM
3485 (dirp (gethash part files))
3486 (inode (gethash file ange-ftp-inodes-hashtable)))
8749abea
GM
3487 (unless inode
3488 (setq inode ange-ftp-next-inode-number
3489 ange-ftp-next-inode-number (1+ inode))
6e85ef5f 3490 (puthash file inode ange-ftp-inodes-hashtable))
8749abea
GM
3491 (list (if (and (stringp dirp) (file-name-absolute-p dirp))
3492 (ange-ftp-expand-symlink dirp
3493 (file-name-directory file))
3494 dirp) ;0 file type
3495 -1 ;1 link count
3496 -1 ;2 uid
3497 -1 ;3 gid
3498 '(0 0) ;4 atime
25759a92 3499 (ange-ftp-file-modtime file) ;5 mtime
8749abea
GM
3500 '(0 0) ;6 ctime
3501 -1 ;7 size
3502 (concat (if (stringp dirp) "l" (if dirp "d" "-"))
3503 "?????????") ;8 mode
3504 nil ;9 gid weird
3505 inode ;10 "inode number".
3506 -1 ;11 device number [v19 only]
3507 ))))
836078f6
RS
3508 (if id-format
3509 (ange-ftp-real-file-attributes file id-format)
3510 (ange-ftp-real-file-attributes file)))))
8749abea 3511
a1f84f6d
SS
3512(defun ange-ftp-file-newer-than-file-p (f1 f2)
3513 (let ((f1-parsed (ange-ftp-ftp-name f1))
3514 (f2-parsed (ange-ftp-ftp-name f2)))
3515 (if (or f1-parsed f2-parsed)
3516 (let ((f1-mt (nth 5 (file-attributes f1)))
3517 (f2-mt (nth 5 (file-attributes f2))))
3518 (cond ((null f1-mt) nil)
3519 ((null f2-mt) t)
3520 (t (> (float-time f1-mt) (float-time f2-mt)))))
3521 (ange-ftp-real-file-newer-than-file-p f1 f2))))
3522
8749abea 3523(defun ange-ftp-file-writable-p (file)
0e14fe9f
GM
3524 (let ((ange-ftp-process-verbose nil))
3525 (setq file (expand-file-name file))
3526 (if (ange-ftp-ftp-name file)
3527 (or (file-exists-p file) ;guess here for speed
3528 (file-directory-p (file-name-directory file)))
3529 (ange-ftp-real-file-writable-p file))))
8749abea
GM
3530
3531(defun ange-ftp-file-readable-p (file)
0e14fe9f
GM
3532 (let ((ange-ftp-process-verbose nil))
3533 (setq file (expand-file-name file))
3534 (if (ange-ftp-ftp-name file)
3535 (file-exists-p file)
3536 (ange-ftp-real-file-readable-p file))))
8749abea
GM
3537
3538(defun ange-ftp-file-executable-p (file)
0e14fe9f
GM
3539 (let ((ange-ftp-process-verbose nil))
3540 (setq file (expand-file-name file))
3541 (if (ange-ftp-ftp-name file)
3542 (file-exists-p file)
3543 (ange-ftp-real-file-executable-p file))))
8749abea
GM
3544
3545(defun ange-ftp-delete-file (file)
3546 (interactive "fDelete file: ")
3547 (setq file (expand-file-name file))
3548 (let ((parsed (ange-ftp-ftp-name file)))
3549 (if parsed
3550 (let* ((host (nth 0 parsed))
3551 (user (nth 1 parsed))
3552 (name (ange-ftp-quote-string (nth 2 parsed)))
3553 (abbr (ange-ftp-abbreviate-filename file))
3554 (result (ange-ftp-send-cmd host user
3555 (list 'delete name)
3556 (format "Deleting %s" abbr))))
3557 (or (car result)
3558 (signal 'ftp-error
3559 (list
3560 "Removing old name"
3561 (format "FTP Error: \"%s\"" (cdr result))
3562 file)))
3563 (ange-ftp-delete-file-entry file))
3564 (ange-ftp-real-delete-file file))))
3565
25759a92 3566(defun ange-ftp-file-modtime (file)
6573d87f
GM
3567 "Return the modification time of remote file FILE.
3568Value is (0 0) if the modification time cannot be determined."
25759a92 3569 (let* ((parsed (ange-ftp-ftp-name file))
6573d87f
GM
3570 ;; At least one FTP server (wu-ftpd) can return a "226
3571 ;; Transfer complete" before the "213 MODTIME". Let's skip
3572 ;; that.
3573 (ange-ftp-skip-msgs (concat ange-ftp-skip-msgs "\\|^226"))
25759a92 3574 (res (ange-ftp-send-cmd (car parsed) (cadr parsed)
6573d87f
GM
3575 (list 'quote "mdtm" (cadr (cdr parsed)))))
3576 (line (cdr res))
3577 (modtime '(0 0)))
524ac5b2
GM
3578 ;; MDTM should return "213 YYYYMMDDhhmmss" GMT on success
3579 ;; following the Internet draft for FTP extensions.
3580 ;; Bob@rattlesnake.com reports that is returns something different
3581 ;; for at least one FTP server. So, let's use the response only
3582 ;; if it matches the Internet draft.
b52e05e2 3583 (when (save-match-data (string-match "^213 [0-9]\\{14\\}$" line))
6573d87f
GM
3584 (setq modtime
3585 (encode-time
3586 (string-to-number (substring line 16 18))
3587 (string-to-number (substring line 14 16))
3588 (string-to-number (substring line 12 14))
3589 (string-to-number (substring line 10 12))
3590 (string-to-number (substring line 8 10))
3591 (string-to-number (substring line 4 8))
3592 0)))
3593 modtime))
25759a92 3594
8749abea
GM
3595(defun ange-ftp-verify-visited-file-modtime (buf)
3596 (let ((name (buffer-file-name buf)))
3597 (if (and (stringp name) (ange-ftp-ftp-name name))
25759a92
SS
3598 (let ((file-mdtm (ange-ftp-file-modtime name))
3599 (buf-mdtm (with-current-buffer buf (visited-file-modtime))))
3600 (or (zerop (car file-mdtm))
9d453139 3601 (<= (float-time file-mdtm) (float-time buf-mdtm))))
8749abea
GM
3602 (ange-ftp-real-verify-visited-file-modtime buf))))
3603\f
3604;;;; ------------------------------------------------------------
3605;;;; File copying support... totally re-written 6/24/92.
3606;;;; ------------------------------------------------------------
3607
3608(defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
3609 (if (file-exists-p absname)
3610 (if (not interactive)
3611 (signal 'file-already-exists (list absname))
3612 (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
3613 absname querystring)))
3614 (signal 'file-already-exists (list absname))))))
3615
3616;; async local copy commented out for now since I don't seem to get
3617;; the process sentinel called for some processes.
3618;;
3619;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
3620;; keep-date cont)
3621;; "Kludge to copy a local file and call a continuation when the copy
3622;; finishes."
3623;; ;; check to see if we can overwrite
3624;; (if (or (not ok-if-already-exists)
3625;; (numberp ok-if-already-exists))
25759a92 3626;; (ange-ftp-barf-or-query-if-file-exists newname "copy to it"
8749abea
GM
3627;; (numberp ok-if-already-exists)))
3628;; (let ((proc (start-process " *copy*"
3629;; (generate-new-buffer "*copy*")
3630;; "cp"
3631;; filename
3632;; newname))
3633;; res)
e3441f42 3634;; (set-process-sentinel proc 'ange-ftp-copy-file-locally-sentinel)
8749abea 3635;; (process-kill-without-query proc)
67f300f8
SM
3636;; (with-current-buffer (process-buffer proc)
3637;; (set (make-local-variable 'copy-cont) cont))))
25759a92 3638;;
8749abea
GM
3639;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
3640;; (save-excursion
3641;; (set-buffer (process-buffer proc))
3642;; (let ((cont copy-cont)
3643;; (result (buffer-string)))
3644;; (unwind-protect
3645;; (if (and (string-equal status "finished\n")
3646;; (zerop (length result)))
3647;; (ange-ftp-call-cont cont t nil)
3648;; (ange-ftp-call-cont cont
3649;; nil
3650;; (if (zerop (length result))
3651;; (substring status 0 -1)
3652;; (substring result 0 -1))))
3653;; (kill-buffer (current-buffer))))))
3654
3655;; this is the extended version of ange-ftp-copy-file-internal that works
3656;; asynchronously if asked nicely.
3657(defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
3658 keep-date &optional msg cont nowait)
3659 (setq filename (expand-file-name filename)
3660 newname (expand-file-name newname))
3661
3662 ;; canonicalize newname if a directory.
3663 (if (file-directory-p newname)
3664 (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
3665
3666 (let ((f-parsed (ange-ftp-ftp-name filename))
3667 (t-parsed (ange-ftp-ftp-name newname)))
3668
3669 ;; local file to local file copy?
3670 (if (and (not f-parsed) (not t-parsed))
3671 (progn
3672 (ange-ftp-real-copy-file filename newname ok-if-already-exists
3673 keep-date)
3674 (if cont
3675 (ange-ftp-call-cont cont t "Copied locally")))
3676 ;; one or both files are remote.
3677 (let* ((f-host (and f-parsed (nth 0 f-parsed)))
3678 (f-user (and f-parsed (nth 1 f-parsed)))
3679 (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
3680 (f-abbr (ange-ftp-abbreviate-filename filename))
3681 (t-host (and t-parsed (nth 0 t-parsed)))
3682 (t-user (and t-parsed (nth 1 t-parsed)))
3683 (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
3684 (t-abbr (ange-ftp-abbreviate-filename newname filename))
3685 (binary (or (ange-ftp-binary-file filename)
3686 (ange-ftp-binary-file newname)
3687 (and (memq (ange-ftp-host-type f-host f-user)
3688 '(unix dumb-unix))
3689 (memq (ange-ftp-host-type t-host t-user)
3690 '(unix dumb-unix)))))
3691 temp1
3692 temp2)
3693
3694 ;; check to see if we can overwrite
3695 (if (or (not ok-if-already-exists)
3696 (numberp ok-if-already-exists))
25759a92 3697 (ange-ftp-barf-or-query-if-file-exists newname "copy to it"
8749abea
GM
3698 (numberp ok-if-already-exists)))
3699
3700 ;; do the copying.
3701 (if f-parsed
25759a92 3702
8749abea
GM
3703 ;; filename was remote.
3704 (progn
3705 (if (or (ange-ftp-use-gateway-p f-host)
3706 t-parsed)
3707 ;; have to use intermediate file if we are getting via
3708 ;; gateway machine or we are doing a remote to remote copy.
3709 (setq temp1 (ange-ftp-make-tmp-name f-host)))
25759a92 3710
8749abea
GM
3711 (if binary
3712 (ange-ftp-set-binary-mode f-host f-user))
3713
3714 (ange-ftp-send-cmd
3715 f-host
3716 f-user
3717 (list 'get f-name (or temp1 (ange-ftp-quote-string newname)))
3718 (or msg
3719 (if (and temp1 t-parsed)
3720 (format "Getting %s" f-abbr)
3721 (format "Copying %s to %s" f-abbr t-abbr)))
e3441f42 3722 (list 'ange-ftp-cf1
8749abea
GM
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 temp1 temp2 cont nowait)
3727 nowait))
3728
3729 ;; filename wasn't remote. newname must be remote. call the
3730 ;; function which does the remainder of the copying work.
3731 (ange-ftp-cf1 t nil
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 nil nil cont nowait))))))
3736
3737(defvar ange-ftp-waiting-flag nil)
3738
3739;; next part of copying routine.
3740(defun ange-ftp-cf1 (result line
3741 filename newname binary msg
3742 f-parsed f-host f-user f-name f-abbr
3743 t-parsed t-host t-user t-name t-abbr
3744 temp1 temp2 cont nowait)
3745 (if line
3746 ;; filename must have been remote, and we must have just done a GET.
3747 (unwind-protect
3748 (or result
3749 ;; GET failed for some reason. Clean up and get out.
3750 (progn
3751 (and temp1 (ange-ftp-del-tmp-name temp1))
3752 (or cont
3753 (if ange-ftp-waiting-flag
3754 (throw 'ftp-error t)
3755 (signal 'ftp-error
3756 (list "Opening input file"
3757 (format "FTP Error: \"%s\"" line)
3758 filename))))))
3759 ;; cleanup
3760 (if binary
3761 (ange-ftp-set-ascii-mode f-host f-user))))
3762
3763 (if result
3764 ;; We now have to copy either temp1 or filename to newname.
3765 (if t-parsed
25759a92 3766
8749abea
GM
3767 ;; newname was remote.
3768 (progn
3769 (if (ange-ftp-use-gateway-p t-host)
3770 (setq temp2 (ange-ftp-make-tmp-name t-host)))
25759a92 3771
8749abea
GM
3772 ;; make sure data is moved into the right place for the
3773 ;; outgoing transfer. gateway temporary files complicate
3774 ;; things nicely.
3775 (if temp1
3776 (if temp2
3777 (if (string-equal temp1 temp2)
3778 (setq temp1 nil)
3779 (ange-ftp-real-copy-file temp1 temp2 t))
3780 (setq temp2 temp1 temp1 nil))
3781 (if temp2
3782 (ange-ftp-real-copy-file filename temp2 t)))
25759a92 3783
8749abea
GM
3784 (if binary
3785 (ange-ftp-set-binary-mode t-host t-user))
3786
3787 ;; tell the process filter what size the file is.
3788 (let ((attr (file-attributes (or temp2 filename))))
3789 (if attr
3790 (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
3791
3792 (ange-ftp-send-cmd
3793 t-host
3794 t-user
3795 (list 'put (or temp2 filename) t-name)
3796 (or msg
3797 (if (and temp2 f-parsed)
3798 (format "Putting %s" newname)
3799 (format "Copying %s to %s" f-abbr t-abbr)))
e3441f42 3800 (list 'ange-ftp-cf2
8749abea
GM
3801 newname t-host t-user binary temp1 temp2 cont)
3802 nowait))
25759a92 3803
8749abea
GM
3804 ;; newname wasn't remote.
3805 (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
3806
3807 ;; first copy failed, tell caller
3808 (ange-ftp-call-cont cont result line)))
3809
3810;; last part of copying routine.
3811(defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
3812 (unwind-protect
3813 (if line
3814 ;; result from doing a local to remote copy.
3815 (unwind-protect
3816 (progn
3817 (or result
3818 (or cont
3819 (if ange-ftp-waiting-flag
3820 (throw 'ftp-error t)
3821 (signal 'ftp-error
3822 (list "Opening output file"
3823 (format "FTP Error: \"%s\"" line)
3824 newname)))))
25759a92 3825
8749abea 3826 (ange-ftp-add-file-entry newname))
25759a92 3827
8749abea
GM
3828 ;; cleanup.
3829 (if binary
3830 (ange-ftp-set-ascii-mode t-host t-user)))
25759a92 3831
8749abea
GM
3832 ;; newname was local.
3833 (if temp1
3834 (ange-ftp-real-copy-file temp1 newname t)))
25759a92 3835
8749abea
GM
3836 ;; clean up
3837 (and temp1 (ange-ftp-del-tmp-name temp1))
3838 (and temp2 (ange-ftp-del-tmp-name temp2))
3839 (ange-ftp-call-cont cont result line)))
3840
3841(defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
3842 keep-date)
3843 (interactive "fCopy file: \nFCopy %s to file: \np")
3844 (ange-ftp-copy-file-internal filename
3845 newname
3846 ok-if-already-exists
3847 keep-date
3848 nil
3849 nil
3850 (interactive-p)))
b878b8c3
SS
3851
3852(defun ange-ftp-copy-files-async (okay-p line verbose-p files)
3853 "Copy some files in the background.
3854Arguments: (OKAY-P LINE VERBOSE-P FILES)
3855OKAY-P must be T, and LINE does not matter. They are here to make this
3856 function a valid CONT argument for `ange-ftp-raw-send-cmd'.
3857If VERBOSE-P is non-nil, print progress report in the echo area.
3858 When all the files have been copied already, a message is shown anyway.
3859FILES is a list of files to copy in the form
3860 (from-file to-file ok-if-already-exists keep-date)
3861E.g.,
3862 (ange-ftp-copy-files-async t nil t '((\"a\" \"b\" t t) (\"c\" \"d\" t t)))"
3863 (unless okay-p (error "%s: %s" 'ange-ftp-copy-files-async line))
3864 (if files
3865 (let* ((ff (car files))
3866 (from-file (nth 0 ff))
3867 (to-file (nth 1 ff))
3868 (ok-if-exists (nth 2 ff))
3869 (keep-date (nth 3 ff)))
3870 (ange-ftp-copy-file-internal
3871 from-file to-file ok-if-exists keep-date
3872 (and verbose-p (format "%s --> %s" from-file to-file))
3873 (list 'ange-ftp-copy-files-async verbose-p (cdr files))
3874 t))
6e85ef5f 3875 (message "%s: done" 'ange-ftp-copy-files-async)))
b878b8c3 3876
8749abea
GM
3877\f
3878;;;; ------------------------------------------------------------
3879;;;; File renaming support.
3880;;;; ------------------------------------------------------------
3881
3882(defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed)
3883 "Rename remote file FILE to remote file NEWNAME."
3884 (let ((f-host (nth 0 f-parsed))
3885 (f-user (nth 1 f-parsed))
3886 (t-host (nth 0 t-parsed))
3887 (t-user (nth 1 t-parsed)))
3888 (if (and (string-equal f-host t-host)
3889 (string-equal f-user t-user))
3890 (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed)))
3891 (t-name (ange-ftp-quote-string (nth 2 t-parsed)))
3892 (cmd (list 'rename f-name t-name))
3893 (fabbr (ange-ftp-abbreviate-filename filename))
3894 (nabbr (ange-ftp-abbreviate-filename newname filename))
3895 (result (ange-ftp-send-cmd f-host f-user cmd
3896 (format "Renaming %s to %s"
3897 fabbr
3898 nabbr))))
3899 (or (car result)
3900 (signal 'ftp-error
3901 (list
3902 "Renaming"
3903 (format "FTP Error: \"%s\"" (cdr result))
3904 filename
3905 newname)))
3906 (ange-ftp-add-file-entry newname)
3907 (ange-ftp-delete-file-entry filename))
3908 (ange-ftp-copy-file-internal filename newname t nil)
3909 (delete-file filename))))
3910
3911(defun ange-ftp-rename-local-to-remote (filename newname)
3912 "Rename local FILENAME to remote 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-remote-to-local (filename newname)
3921 "Rename remote file FILENAME to local file NEWNAME."
3922 (let* ((fabbr (ange-ftp-abbreviate-filename filename))
3923 (nabbr (ange-ftp-abbreviate-filename newname filename))
3924 (msg (format "Renaming %s to %s" fabbr nabbr)))
3925 (ange-ftp-copy-file-internal filename newname t nil msg)
3926 (let (ange-ftp-process-verbose)
3927 (delete-file filename))))
3928
3929(defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
3930 (interactive "fRename file: \nFRename %s to file: \np")
3931 (setq filename (expand-file-name filename))
3932 (setq newname (expand-file-name newname))
3933 (let* ((f-parsed (ange-ftp-ftp-name filename))
3934 (t-parsed (ange-ftp-ftp-name newname)))
3935 (if (and (or f-parsed t-parsed)
3936 (or (not ok-if-already-exists)
3937 (numberp ok-if-already-exists)))
3938 (ange-ftp-barf-or-query-if-file-exists
3939 newname
3940 "rename to it"
3941 (numberp ok-if-already-exists)))
3942 (if f-parsed
3943 (if t-parsed
3944 (ange-ftp-rename-remote-to-remote filename newname f-parsed
3945 t-parsed)
3946 (ange-ftp-rename-remote-to-local filename newname))
3947 (if t-parsed
3948 (ange-ftp-rename-local-to-remote filename newname)
3949 (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
3950\f
3951;;;; ------------------------------------------------------------
3952;;;; File name completion support.
3953;;;; ------------------------------------------------------------
3954
8749abea
GM
3955;; If the file entry is not a directory (nor a symlink pointing to a directory)
3956;; returns whether the file (or file pointed to by the symlink) is ignored
3957;; by completion-ignored-extensions.
3958;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern'
3959;; are used as free variables.
6e85ef5f
SM
3960(defun ange-ftp-file-entry-not-ignored-p (symname val)
3961 (if (stringp val)
3962 (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir)))
3963 (or (file-directory-p file)
3964 (and (file-exists-p file)
3965 (not (string-match ange-ftp-completion-ignored-pattern
3966 symname)))))
3967 (or val ; is a directory name
3968 (not (string-match ange-ftp-completion-ignored-pattern symname)))))
8749abea 3969
ae0bec0b
SM
3970(defun ange-ftp-root-dir-p (dir)
3971 ;; Maybe we should use something more like
3972 ;; (equal dir (file-name-directory (directory-file-name dir))) -stef
3973 (or (and (eq system-type 'windows-nt)
3974 (string-match "^[a-zA-Z]:[/\\]$" dir))
3975 (string-equal "/" dir)))
3976
8749abea
GM
3977(defun ange-ftp-file-name-all-completions (file dir)
3978 (let ((ange-ftp-this-dir (expand-file-name dir)))
3979 (if (ange-ftp-ftp-name ange-ftp-this-dir)
3980 (progn
3981 (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
3982 (setq ange-ftp-this-dir
3983 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))
3984 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
e3441f42 3985 (completions (all-completions file tbl)))
8749abea
GM
3986
3987 ;; see whether each matching file is a directory or not...
3988 (mapcar
25759a92 3989 (lambda (file)
6e85ef5f 3990 (let ((ent (gethash file tbl)))
25759a92
SS
3991 (if (and ent
3992 (or (not (stringp ent))
3993 (file-directory-p
3994 (ange-ftp-expand-symlink ent
3995 ange-ftp-this-dir))))
3996 (concat file "/")
6e85ef5f 3997 file)))
8749abea
GM
3998 completions)))
3999
ae0bec0b 4000 (if (ange-ftp-root-dir-p ange-ftp-this-dir)
8749abea
GM
4001 (nconc (all-completions file (ange-ftp-generate-root-prefixes))
4002 (ange-ftp-real-file-name-all-completions file
4003 ange-ftp-this-dir))
4004 (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir)))))
4005
4006(defun ange-ftp-file-name-completion (file dir)
4007 (let ((ange-ftp-this-dir (expand-file-name dir)))
4008 (if (ange-ftp-ftp-name ange-ftp-this-dir)
4009 (progn
4010 (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
4011 (if (equal file "")
4012 ""
4013 (setq ange-ftp-this-dir
4014 (ange-ftp-real-file-name-as-directory ange-ftp-this-dir)) ;real?
4015 (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
4016 (ange-ftp-completion-ignored-pattern
25759a92
SS
4017 (mapconcat (lambda (s) (if (stringp s)
4018 (concat (regexp-quote s) "$")
4019 "/")) ; / never in filename
8749abea
GM
4020 completion-ignored-extensions
4021 "\\|")))
4022 (save-match-data
4023 (or (ange-ftp-file-name-completion-1
4024 file tbl ange-ftp-this-dir
e3441f42 4025 'ange-ftp-file-entry-not-ignored-p)
8749abea 4026 (ange-ftp-file-name-completion-1
e3441f42 4027 file tbl ange-ftp-this-dir))))))
8749abea 4028
ae0bec0b 4029 (if (ange-ftp-root-dir-p ange-ftp-this-dir)
8749abea
GM
4030 (try-completion
4031 file
4032 (nconc (ange-ftp-generate-root-prefixes)
7ca26b02
SM
4033 (ange-ftp-real-file-name-all-completions
4034 file ange-ftp-this-dir)))
8749abea
GM
4035 (ange-ftp-real-file-name-completion file ange-ftp-this-dir)))))
4036
4037
e3441f42 4038(defun ange-ftp-file-name-completion-1 (file tbl dir &optional predicate)
8749abea
GM
4039 (let ((bestmatch (try-completion file tbl predicate)))
4040 (if bestmatch
4041 (if (eq bestmatch t)
4042 (if (file-directory-p (expand-file-name file dir))
4043 (concat file "/")
4044 t)
4045 (if (and (eq (try-completion bestmatch tbl predicate) t)
4046 (file-directory-p
4047 (expand-file-name bestmatch dir)))
4048 (concat bestmatch "/")
4049 bestmatch)))))
4050
4051;; Put these lines uncommmented in your .emacs if you want C-r to refresh
4052;; ange-ftp's cache whilst doing filename completion.
4053;;
4054;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
4055;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
4056
b2ce953f
MR
4057;;;###autoload
4058(defalias 'ange-ftp-re-read-dir 'ange-ftp-reread-dir)
4059
8749abea
GM
4060;;;###autoload
4061(defun ange-ftp-reread-dir (&optional dir)
4062 "Reread remote directory DIR to update the directory cache.
4063The implementation of remote ftp file names caches directory contents
4064for speed. Therefore, when new remote files are created, Emacs
4065may not know they exist. You can use this command to reread a specific
4066directory, so that Emacs will know its current contents."
4067 (interactive)
4068 (if dir
4069 (setq dir (expand-file-name dir))
4070 (setq dir (file-name-directory (expand-file-name (buffer-string)))))
4071 (if (ange-ftp-ftp-name dir)
4072 (progn
4073 (setq ange-ftp-ls-cache-file nil)
6e85ef5f 4074 (remhash dir ange-ftp-files-hashtable)
8749abea
GM
4075 (ange-ftp-get-files dir t))))
4076\f
4077(defun ange-ftp-make-directory (dir &optional parents)
4078 (interactive (list (expand-file-name (read-file-name "Make directory: "))))
4079 (if parents
4080 (let ((parent (file-name-directory (directory-file-name dir))))
4081 (or (file-exists-p parent)
4082 (ange-ftp-make-directory parent parents))))
4083 (if (file-exists-p dir)
4084 (error "Cannot make directory %s: file already exists" dir)
4085 (let ((parsed (ange-ftp-ftp-name dir)))
4086 (if parsed
4087 (let* ((host (nth 0 parsed))
4088 (user (nth 1 parsed))
4089 ;; Some ftp's on unix machines (at least on Suns)
4090 ;; insist that mkdir take a filename, and not a
4091 ;; directory-name name as an arg. Argh!! This is a bug.
4092 ;; Non-unix machines will probably always insist
4093 ;; that mkdir takes a directory-name as an arg
4094 ;; (as the ftp man page says it should).
4095 (name (ange-ftp-quote-string
4096 (if (eq (ange-ftp-host-type host) 'unix)
4097 (ange-ftp-real-directory-file-name (nth 2 parsed))
4098 (ange-ftp-real-file-name-as-directory
4099 (nth 2 parsed)))))
4100 (abbr (ange-ftp-abbreviate-filename dir))
4101 (result (ange-ftp-send-cmd host user
4102 (list 'mkdir name)
4103 (format "Making directory %s"
4104 abbr))))
4105 (or (car result)
4106 (ange-ftp-error host user
4107 (format "Could not make directory %s: %s"
4108 dir
4109 (cdr result))))
4110 (ange-ftp-add-file-entry dir t))
4111 (ange-ftp-real-make-directory dir)))))
4112
4113(defun ange-ftp-delete-directory (dir)
4114 (if (file-directory-p dir)
4115 (let ((parsed (ange-ftp-ftp-name dir)))
4116 (if parsed
4117 (let* ((host (nth 0 parsed))
4118 (user (nth 1 parsed))
4119 ;; Some ftp's on unix machines (at least on Suns)
4120 ;; insist that rmdir take a filename, and not a
4121 ;; directory-name name as an arg. Argh!! This is a bug.
4122 ;; Non-unix machines will probably always insist
4123 ;; that rmdir takes a directory-name as an arg
4124 ;; (as the ftp man page says it should).
4125 (name (ange-ftp-quote-string
4126 (if (eq (ange-ftp-host-type host) 'unix)
4127 (ange-ftp-real-directory-file-name
4128 (nth 2 parsed))
4129 (ange-ftp-real-file-name-as-directory
4130 (nth 2 parsed)))))
e3441f42
SM
4131 (abbr (ange-ftp-abbreviate-filename dir))
4132 (result (ange-ftp-send-cmd host user
4133 (list 'rmdir name)
4134 (format "Removing directory %s"
4135 abbr))))
8749abea
GM
4136 (or (car result)
4137 (ange-ftp-error host user
4138 (format "Could not remove directory %s: %s"
4139 dir
4140 (cdr result))))
4141 (ange-ftp-delete-file-entry dir t))
4142 (ange-ftp-real-delete-directory dir)))
4143 (error "Not a directory: %s" dir)))
4144\f
4145;; Make a local copy of FILE and return its name.
4146
4147(defun ange-ftp-file-local-copy (file)
4148 (let* ((fn1 (expand-file-name file))
4149 (pa1 (ange-ftp-ftp-name fn1)))
4150 (if pa1
4151 (let ((tmp1 (ange-ftp-make-tmp-name (car pa1))))
4152 (ange-ftp-copy-file-internal fn1 tmp1 t nil
4153 (format "Getting %s" fn1))
4154 tmp1))))
4155
e28c0668 4156(defun ange-ftp-file-remote-p (file)
3f788773 4157 (ange-ftp-replace-name-component file ""))
e28c0668 4158
8749abea
GM
4159(defun ange-ftp-load (file &optional noerror nomessage nosuffix)
4160 (if (ange-ftp-ftp-name file)
4161 (let ((tryfiles (if nosuffix
4162 (list file)
4163 (list (concat file ".elc") (concat file ".el") file)))
4164 ;; make sure there are no references to temp files
4165 (load-force-doc-strings t)
4166 copy)
4167 (while (and tryfiles (not copy))
4168 (catch 'ftp-error
4169 (let ((ange-ftp-waiting-flag t))
4170 (condition-case error
4171 (setq copy (ange-ftp-file-local-copy (car tryfiles)))
4172 (ftp-error nil))))
4173 (setq tryfiles (cdr tryfiles)))
4174 (if copy
4175 (unwind-protect
4176 (funcall 'load copy noerror nomessage nosuffix)
4177 (delete-file copy))
4178 (or noerror
4179 (signal 'file-error (list "Cannot open load file" file)))
4180 nil))
4181 (ange-ftp-real-load file noerror nomessage nosuffix)))
4182
4183;; Calculate default-unhandled-directory for a given ange-ftp buffer.
4184(defun ange-ftp-unhandled-file-name-directory (filename)
4185 (file-name-directory ange-ftp-tmp-name-template))
4186
4187\f
4188;; Need the following functions for making filenames of compressed
4189;; files, because some OS's (unlike UNIX) do not allow a filename to
4190;; have two extensions.
4191
4192(defvar ange-ftp-make-compressed-filename-alist nil
4193 "Alist of host-type-specific functions to process file names for compression.
4194Each element has the form (TYPE . FUNC).
4195FUNC should take one argument, a file name, and return a list
4196of the form (COMPRESSING NEWNAME).
25759a92 4197COMPRESSING should be t if the specified file should be compressed,
8749abea
GM
4198and nil if it should be uncompressed (that is, if it is a compressed file).
4199NEWNAME should be the name to give the new compressed or uncompressed file.")
4200
4201(defun ange-ftp-dired-compress-file (name)
4202 (let ((parsed (ange-ftp-ftp-name name))
4203 conversion-func)
4204 (if (and parsed
4205 (setq conversion-func
4206 (cdr (assq (ange-ftp-host-type (car parsed))
4207 ange-ftp-make-compressed-filename-alist))))
4208 (let* ((decision
4209 (save-match-data (funcall conversion-func name)))
4210 (compressing (car decision))
4211 (newfile (nth 1 decision)))
4212 (if compressing
4213 (ange-ftp-compress name newfile)
4214 (ange-ftp-uncompress name newfile)))
4215 (let (file-name-handler-alist)
4216 (dired-compress-file name)))))
4217
4218;; Copy FILE to this machine, compress it, and copy out to NFILE.
4219(defun ange-ftp-compress (file nfile)
4220 (let* ((parsed (ange-ftp-ftp-name file))
4221 (tmp1 (ange-ftp-make-tmp-name (car parsed)))
4222 (tmp2 (ange-ftp-make-tmp-name (car parsed)))
4223 (abbr (ange-ftp-abbreviate-filename file))
4224 (nabbr (ange-ftp-abbreviate-filename nfile))
4225 (msg1 (format "Getting %s" abbr))
4226 (msg2 (format "Putting %s" nabbr)))
4227 (unwind-protect
4228 (progn
4229 (ange-ftp-copy-file-internal file tmp1 t nil msg1)
4230 (and ange-ftp-process-verbose
4231 (ange-ftp-message "Compressing %s..." abbr))
4232 (call-process-region (point)
4233 (point)
4234 shell-file-name
4235 nil
4236 t
4237 nil
4238 "-c"
4239 (format "compress -f -c < %s > %s" tmp1 tmp2))
4240 (and ange-ftp-process-verbose
4241 (ange-ftp-message "Compressing %s...done" abbr))
4242 (if (zerop (buffer-size))
4243 (progn
4244 (let (ange-ftp-process-verbose)
4245 (delete-file file))
4246 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
4247 (ange-ftp-del-tmp-name tmp1)
4248 (ange-ftp-del-tmp-name tmp2))))
25759a92 4249
8749abea
GM
4250;; Copy FILE to this machine, uncompress it, and copy out to NFILE.
4251(defun ange-ftp-uncompress (file nfile)
4252 (let* ((parsed (ange-ftp-ftp-name file))
4253 (tmp1 (ange-ftp-make-tmp-name (car parsed)))
4254 (tmp2 (ange-ftp-make-tmp-name (car parsed)))
4255 (abbr (ange-ftp-abbreviate-filename file))
4256 (nabbr (ange-ftp-abbreviate-filename nfile))
4257 (msg1 (format "Getting %s" abbr))
4258 (msg2 (format "Putting %s" nabbr))
4259;; ;; Cheap hack because of problems with binary file transfers from
4260;; ;; VMS hosts.
4261;; (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
4262 )
4263 (unwind-protect
4264 (progn
4265 (ange-ftp-copy-file-internal file tmp1 t nil msg1)
4266 (and ange-ftp-process-verbose
4267 (ange-ftp-message "Uncompressing %s..." abbr))
4268 (call-process-region (point)
4269 (point)
4270 shell-file-name
4271 nil
4272 t
4273 nil
4274 "-c"
4275 (format "uncompress -c < %s > %s" tmp1 tmp2))
4276 (and ange-ftp-process-verbose
4277 (ange-ftp-message "Uncompressing %s...done" abbr))
4278 (if (zerop (buffer-size))
4279 (progn
4280 (let (ange-ftp-process-verbose)
4281 (delete-file file))
4282 (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
4283 (ange-ftp-del-tmp-name tmp1)
4284 (ange-ftp-del-tmp-name tmp2))))
4285
4286(defun ange-ftp-find-backup-file-name (fn)
4287 ;; Either return the ordinary backup name, etc.,
4288 ;; or return nil meaning don't make a backup.
4289 (if ange-ftp-make-backup-files
4290 (ange-ftp-real-find-backup-file-name fn)))
4291\f
4292;;; Define the handler for special file names
4293;;; that causes ange-ftp to be invoked.
4294
4295;;;###autoload
4296(defun ange-ftp-hook-function (operation &rest args)
4297 (let ((fn (get operation 'ange-ftp)))
ae0bec0b 4298 (if fn (save-match-data (apply fn args))
8749abea 4299 (ange-ftp-run-real-handler operation args))))
8749abea 4300
65399e83
KG
4301;; The following code is commented out because Tramp now deals with
4302;; Ange-FTP filenames, too.
4303
4304;;-;;; This regexp takes care of real ange-ftp file names (with a slash
4305;;-;;; and colon).
4306;;-;;; Don't allow the host name to end in a period--some systems use /.:
4307;;-;;;###autoload
4308;;-(or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
4309;;- (setq file-name-handler-alist
4310;;- (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
4311;;- file-name-handler-alist)))
4312;;-
4313;;-;;; This regexp recognizes absolute filenames with only one component,
4314;;-;;; for the sake of hostname completion.
4315;;-;;;###autoload
4316;;-(or (assoc "^/[^/:]*\\'" file-name-handler-alist)
4317;;- (setq file-name-handler-alist
4318;;- (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
4319;;- file-name-handler-alist)))
4320;;-
4321;;-;;; This regexp recognizes absolute filenames with only one component
4322;;-;;; on Windows, for the sake of hostname completion.
4323;;-;;; NB. Do not mark this as autoload, because it is very common to
4324;;-;;; do completions in the root directory of drives on Windows.
4325;;-(and (memq system-type '(ms-dos windows-nt))
4326;;- (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist)
4327;;- (setq file-name-handler-alist
4328;;- (cons '("^[a-zA-Z]:/[^/:]*\\'" .
4329;;- ange-ftp-completion-hook-function)
4330;;- file-name-handler-alist))))
8749abea
GM
4331
4332;;; The above two forms are sufficient to cause this file to be loaded
4333;;; if the user ever uses a file name with a colon in it.
4334
25759a92 4335;;; This sets the mode
c8607dc7 4336(add-hook 'find-file-hook 'ange-ftp-set-buffer-mode)
8749abea
GM
4337
4338;;; Now say where to find the handlers for particular operations.
4339
4340(put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory)
4341(put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory)
4342(put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory)
4343(put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name)
4344(put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name)
4345(put 'make-directory 'ange-ftp 'ange-ftp-make-directory)
4346(put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory)
4347(put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents)
4348(put 'directory-files 'ange-ftp 'ange-ftp-directory-files)
4349(put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p)
4350(put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p)
4351(put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p)
4352(put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p)
4353(put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p)
4354(put 'delete-file 'ange-ftp 'ange-ftp-delete-file)
8749abea
GM
4355(put 'verify-visited-file-modtime 'ange-ftp
4356 'ange-ftp-verify-visited-file-modtime)
4357(put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p)
4358(put 'write-region 'ange-ftp 'ange-ftp-write-region)
8749abea
GM
4359(put 'copy-file 'ange-ftp 'ange-ftp-copy-file)
4360(put 'rename-file 'ange-ftp 'ange-ftp-rename-file)
4361(put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes)
a1f84f6d 4362(put 'file-newer-than-file-p 'ange-ftp 'ange-ftp-file-newer-than-file-p)
8749abea
GM
4363(put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions)
4364(put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion)
4365(put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory)
4366(put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy)
e28c0668 4367(put 'file-remote-p 'ange-ftp 'ange-ftp-file-remote-p)
8749abea
GM
4368(put 'unhandled-file-name-directory 'ange-ftp
4369 'ange-ftp-unhandled-file-name-directory)
4370(put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions)
4371(put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache)
4372(put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file)
4373(put 'load 'ange-ftp 'ange-ftp-load)
4374(put 'find-backup-file-name 'ange-ftp 'ange-ftp-find-backup-file-name)
4375
4376;; Turn off truename processing to save time.
4377;; Treat each name as its own truename.
4378(put 'file-truename 'ange-ftp 'identity)
4379
4380;; Turn off RCS/SCCS processing to save time.
4381;; This returns nil for any file name as argument.
4382(put 'vc-registered 'ange-ftp 'null)
4383
4384(put 'dired-call-process 'ange-ftp 'ange-ftp-dired-call-process)
4385(put 'shell-command 'ange-ftp 'ange-ftp-shell-command)
4386\f
4387;;; Define ways of getting at unmodified Emacs primitives,
4388;;; turning off our handler.
4389
4390(defun ange-ftp-run-real-handler (operation args)
4391 (let ((inhibit-file-name-handlers
4392 (cons 'ange-ftp-hook-function
4393 (cons 'ange-ftp-completion-hook-function
4394 (and (eq inhibit-file-name-operation operation)
4395 inhibit-file-name-handlers))))
4396 (inhibit-file-name-operation operation))
4397 (apply operation args)))
4398
4399(defun ange-ftp-real-file-name-directory (&rest args)
4400 (ange-ftp-run-real-handler 'file-name-directory args))
4401(defun ange-ftp-real-file-name-nondirectory (&rest args)
4402 (ange-ftp-run-real-handler 'file-name-nondirectory args))
4403(defun ange-ftp-real-file-name-as-directory (&rest args)
4404 (ange-ftp-run-real-handler 'file-name-as-directory args))
4405(defun ange-ftp-real-directory-file-name (&rest args)
4406 (ange-ftp-run-real-handler 'directory-file-name args))
4407(defun ange-ftp-real-expand-file-name (&rest args)
4408 (ange-ftp-run-real-handler 'expand-file-name args))
4409(defun ange-ftp-real-make-directory (&rest args)
4410 (ange-ftp-run-real-handler 'make-directory args))
4411(defun ange-ftp-real-delete-directory (&rest args)
4412 (ange-ftp-run-real-handler 'delete-directory args))
4413(defun ange-ftp-real-insert-file-contents (&rest args)
4414 (ange-ftp-run-real-handler 'insert-file-contents args))
4415(defun ange-ftp-real-directory-files (&rest args)
4416 (ange-ftp-run-real-handler 'directory-files args))
4417(defun ange-ftp-real-file-directory-p (&rest args)
4418 (ange-ftp-run-real-handler 'file-directory-p args))
4419(defun ange-ftp-real-file-writable-p (&rest args)
4420 (ange-ftp-run-real-handler 'file-writable-p args))
4421(defun ange-ftp-real-file-readable-p (&rest args)
4422 (ange-ftp-run-real-handler 'file-readable-p args))
4423(defun ange-ftp-real-file-executable-p (&rest args)
4424 (ange-ftp-run-real-handler 'file-executable-p args))
4425(defun ange-ftp-real-file-symlink-p (&rest args)
4426 (ange-ftp-run-real-handler 'file-symlink-p args))
4427(defun ange-ftp-real-delete-file (&rest args)
4428 (ange-ftp-run-real-handler 'delete-file args))
8749abea
GM
4429(defun ange-ftp-real-verify-visited-file-modtime (&rest args)
4430 (ange-ftp-run-real-handler 'verify-visited-file-modtime args))
4431(defun ange-ftp-real-file-exists-p (&rest args)
4432 (ange-ftp-run-real-handler 'file-exists-p args))
4433(defun ange-ftp-real-write-region (&rest args)
4434 (ange-ftp-run-real-handler 'write-region args))
4435(defun ange-ftp-real-backup-buffer (&rest args)
4436 (ange-ftp-run-real-handler 'backup-buffer args))
4437(defun ange-ftp-real-copy-file (&rest args)
4438 (ange-ftp-run-real-handler 'copy-file args))
4439(defun ange-ftp-real-rename-file (&rest args)
4440 (ange-ftp-run-real-handler 'rename-file args))
4441(defun ange-ftp-real-file-attributes (&rest args)
4442 (ange-ftp-run-real-handler 'file-attributes args))
a1f84f6d
SS
4443(defun ange-ftp-real-file-newer-than-file-p (&rest args)
4444 (ange-ftp-run-real-handler 'file-newer-than-file-p args))
8749abea
GM
4445(defun ange-ftp-real-file-name-all-completions (&rest args)
4446 (ange-ftp-run-real-handler 'file-name-all-completions args))
4447(defun ange-ftp-real-file-name-completion (&rest args)
4448 (ange-ftp-run-real-handler 'file-name-completion args))
4449(defun ange-ftp-real-insert-directory (&rest args)
4450 (ange-ftp-run-real-handler 'insert-directory args))
4451(defun ange-ftp-real-file-name-sans-versions (&rest args)
4452 (ange-ftp-run-real-handler 'file-name-sans-versions args))
4453(defun ange-ftp-real-shell-command (&rest args)
4454 (ange-ftp-run-real-handler 'shell-command args))
4455(defun ange-ftp-real-load (&rest args)
4456 (ange-ftp-run-real-handler 'load args))
4457(defun ange-ftp-real-find-backup-file-name (&rest args)
4458 (ange-ftp-run-real-handler 'find-backup-file-name args))
4459\f
4460;; Here we support using dired on remote hosts.
4461;; I have turned off the support for using dired on foreign directory formats.
4462;; That involves too many unclean hooks.
25759a92 4463;; It would be cleaner to support such operations by
8749abea
GM
4464;; converting the foreign directory format to something dired can understand;
4465;; something close to ls -l output.
4466;; The logical place to do this is in the functions ange-ftp-parse-...-listing.
4467
4468;; Some of the old dired hooks would still be needed even if this is done.
4469;; I have preserved (and modernized) those hooks.
4470;; So the format conversion should be all that is needed.
4471
1fc85dae
KG
4472;; When called from dired, SWITCHES may start with "--dired".
4473;; `ange-ftp-ls' handles this.
4474
8749abea
GM
4475(defun ange-ftp-insert-directory (file switches &optional wildcard full)
4476 (let ((short (ange-ftp-abbreviate-filename file))
8839c4a6
RS
4477 (parsed (ange-ftp-ftp-name (expand-file-name file)))
4478 tem)
8749abea 4479 (if parsed
8839c4a6
RS
4480 (if (and (not wildcard)
4481 (setq tem (file-symlink-p (directory-file-name file))))
4482 (ange-ftp-insert-directory
c8607dc7
SM
4483 (ange-ftp-expand-symlink
4484 tem (file-name-directory (directory-file-name file)))
8839c4a6
RS
4485 switches wildcard full)
4486 (insert
4487 (if wildcard
4488 (let ((default-directory (file-name-directory file)))
4489 (ange-ftp-ls (file-name-nondirectory file) switches nil nil t))
ae0bec0b 4490 (ange-ftp-ls file switches full))))
8749abea
GM
4491 (ange-ftp-real-insert-directory file switches wildcard full))))
4492
4493(defun ange-ftp-dired-uncache (dir)
4494 (if (ange-ftp-ftp-name (expand-file-name dir))
4495 (setq ange-ftp-ls-cache-file nil)))
4496
4497(defvar ange-ftp-sans-version-alist nil
4498 "Alist of mapping host type into function to remove file version numbers.")
4499
4500(defun ange-ftp-file-name-sans-versions (file keep-backup-version)
4501 (let* ((short (ange-ftp-abbreviate-filename file))
4502 (parsed (ange-ftp-ftp-name short))
4503 host-type func)
4504 (if parsed
4505 (setq host-type (ange-ftp-host-type (car parsed))
4506 func (cdr (assq (ange-ftp-host-type (car parsed))
4507 ange-ftp-sans-version-alist))))
4508 (if func (funcall func file keep-backup-version)
4509 (ange-ftp-real-file-name-sans-versions file keep-backup-version))))
4510
4511;; This is the handler for shell-command.
4512(defun ange-ftp-shell-command (command &optional output-buffer error-buffer)
4513 (let* ((parsed (ange-ftp-ftp-name default-directory))
4514 (host (nth 0 parsed))
4515 (user (nth 1 parsed))
4516 (name (nth 2 parsed)))
4517 (if (not parsed)
4518 (ange-ftp-real-shell-command command output-buffer error-buffer)
4519 (if (> (length name) 0) ; else it's $HOME
4520 (setq command (concat "cd " name "; " command)))
cdc51589 4521 ;; Remove port from the hostname
ae0bec0b
SM
4522 (when (string-match "\\(.*\\)#" host)
4523 (setq host (match-string 1 host)))
8749abea
GM
4524 (setq command
4525 (format "%s %s \"%s\"" ; remsh -l USER does not work well
4526 ; on a hp-ux machine I tried
4527 remote-shell-program host command))
4528 (ange-ftp-message "Remote command '%s' ..." command)
4529 ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
4530 ;; would prepend "cd default-directory" --- which bombs because
4531 ;; default-directory is in ange-ftp syntax for remote file names.
4532 (ange-ftp-real-shell-command command output-buffer error-buffer))))
4533
4534;;; This is the handler for call-process.
4535(defun ange-ftp-dired-call-process (program discard &rest arguments)
4536 ;; PROGRAM is always one of those below in the cond in dired.el.
4537 ;; The ARGUMENTS are (nearly) always files.
4538 (if (ange-ftp-ftp-name default-directory)
4539 ;; Can't use ange-ftp-dired-host-type here because the current
4540 ;; buffer is *dired-check-process output*
4541 (condition-case oops
4542 (cond ((equal dired-chmod-program program)
4543 (ange-ftp-call-chmod arguments))
4544 ;; ((equal "chgrp" program))
4545 ;; ((equal dired-chown-program program))
4546 (t (error "Unknown remote command: %s" program)))
4547 (ftp-error (insert (format "%s: %s, %s\n"
e3441f42
SM
4548 (nth 1 oops)
4549 (nth 2 oops)
4550 (nth 3 oops)))
8749abea
GM
4551 ;; Caller expects nonzero value to mean failure.
4552 1)
4553 (error (insert (format "%s\n" (nth 1 oops)))
4554 1))
4555 (apply 'call-process program nil (not discard) nil arguments)))
4556
8749abea
GM
4557;; Handle an attempt to run chmod on a remote file
4558;; by using the ftp chmod command.
4559(defun ange-ftp-call-chmod (args)
4560 (if (< (length args) 2)
4561 (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
4562 (let ((mode (car args))
4563 (rest (cdr args)))
4564 (if (equal "--" (car rest))
4565 (setq rest (cdr rest)))
4566 (mapcar
25759a92
SS
4567 (lambda (file)
4568 (setq file (expand-file-name file))
4569 (let ((parsed (ange-ftp-ftp-name file)))
4570 (if parsed
4571 (let* ((host (nth 0 parsed))
4572 (user (nth 1 parsed))
4573 (name (ange-ftp-quote-string (nth 2 parsed)))
4574 (abbr (ange-ftp-abbreviate-filename file))
4575 (result (ange-ftp-send-cmd host user
4576 (list 'chmod mode name)
4577 (format "doing chmod %s"
4578 abbr))))
4579 (or (car result)
4580 (call-process
97e810ff 4581 remote-shell-program
25759a92 4582 nil t nil host dired-chmod-program mode name))))))
8749abea
GM
4583 rest))
4584 (setq ange-ftp-ls-cache-file nil) ;Stop confusing Dired.
4585 0)
4586\f
92482d77
SM
4587;; This is turned off because it has nothing properly to do
4588;; with dired. It could be reasonable to adapt this to
4589;; replace ange-ftp-copy-file.
8749abea
GM
4590
4591;;;;; ------------------------------------------------------------
4592;;;;; Noddy support for async copy-file within dired.
4593;;;;; ------------------------------------------------------------
4594
4595;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
4596;; "Documented as original."
4597;; (dired-handle-overwrite to)
4598;; (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
4599;; cont nowait))
4600
4601;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
4602;; &optional marker-char op1
4603;; how-to)
4604;; "Documented as original."
4605;; ;; we need to let ange-ftp-dired-create-files know that we indirectly
4606;; ;; called it rather than somebody else.
4607;; (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
4608;; (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
4609;; arg marker-char op1 how-to)))
4610
4611;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
4612;; &optional marker-char)
4613;; "Documented as original."
4614;; (if (and (boundp 'ange-ftp-dired-do-create-files)
4615;; ;; called from ange-ftp-dired-do-create-files?
4616;; ange-ftp-dired-do-create-files
4617;; ;; any files worth copying?
4618;; fn-list
4619;; ;; we only support async copy-file at the mo.
4620;; (eq file-creator 'dired-copy-file)
4621;; ;; it is only worth calling the alternative function for remote files
4622;; ;; as we tie ourself in recursive knots otherwise.
4623;; (or (ange-ftp-ftp-name (car fn-list))
4624;; ;; we can only call the name constructor for dired-do-create-files
4625;; ;; since the one for regexps starts prompting here, there and
4626;; ;; everywhere.
4627;; (ange-ftp-ftp-name (funcall name-constructor (car fn-list)))))
4628;; ;; use the process-filter driven routine rather than the iterative one.
4629;; (ange-ftp-dcf-1 file-creator
4630;; operation
4631;; fn-list
4632;; name-constructor
4633;; (and (boundp 'target) target) ;dynamically bound
4634;; marker-char
4635;; (current-buffer)
4636;; nil ;overwrite-query
4637;; nil ;overwrite-backup-query
4638;; nil ;failures
4639;; nil ;skipped
4640;; 0 ;success-count
4641;; (length fn-list) ;total
4642;; )
4643;; ;; normal case... use the interactive routine... much cheaper.
4644;; (ange-ftp-real-dired-create-files file-creator operation fn-list
4645;; name-constructor marker-char)))
4646
4647;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
25759a92 4648;; target marker-char buffer overwrite-query
8749abea
GM
4649;; overwrite-backup-query failures skipped
4650;; success-count total)
4651;; (let ((old-buf (current-buffer)))
4652;; (unwind-protect
4653;; (progn
4654;; (set-buffer buffer)
4655;; (if (null fn-list)
4656;; (ange-ftp-dcf-3 failures operation total skipped
4657;; success-count buffer)
25759a92 4658
8749abea
GM
4659;; (let* ((from (car fn-list))
4660;; (to (funcall name-constructor from)))
4661;; (if (equal to from)
4662;; (progn
4663;; (setq to nil)
4664;; (dired-log "Cannot %s to same file: %s\n"
4665;; (downcase operation) from)))
4666;; (if (not to)
4667;; (ange-ftp-dcf-1 file-creator
4668;; operation
4669;; (cdr fn-list)
4670;; name-constructor
4671;; target
4672;; marker-char
4673;; buffer
4674;; overwrite-query
4675;; overwrite-backup-query
4676;; failures
4677;; (cons (dired-make-relative from) skipped)
4678;; success-count
4679;; total)
4680;; (let* ((overwrite (file-exists-p to))
4681;; (overwrite-confirmed ; for dired-handle-overwrite
4682;; (and overwrite
4683;; (let ((help-form '(format "\
4684;;Type SPC or `y' to overwrite file `%s',
4685;;DEL or `n' to skip to next,
4686;;ESC or `q' to not overwrite any of the remaining files,
4687;;`!' to overwrite all remaining files with no more questions." to)))
4688;; (dired-query 'overwrite-query
4689;; "Overwrite `%s'?" to))))
4690;; ;; must determine if FROM is marked before file-creator
4691;; ;; gets a chance to delete it (in case of a move).
4692;; (actual-marker-char
4693;; (cond ((integerp marker-char) marker-char)
4694;; (marker-char (dired-file-marker from)) ; slow
4695;; (t nil))))
4696;; (condition-case err
4697;; (funcall file-creator from to overwrite-confirmed
e3441f42 4698;; (list 'ange-ftp-dcf-2
8749abea
GM
4699;; nil ;err
4700;; file-creator operation fn-list
4701;; name-constructor
4702;; target
4703;; marker-char actual-marker-char
4704;; buffer to from
4705;; overwrite
4706;; overwrite-confirmed
25759a92 4707;; overwrite-query
8749abea
GM
4708;; overwrite-backup-query
4709;; failures skipped success-count
4710;; total)
4711;; t)
4712;; (file-error ; FILE-CREATOR aborted
4713;; (ange-ftp-dcf-2 nil ;result
4714;; nil ;line
4715;; err
4716;; file-creator operation fn-list
4717;; name-constructor
4718;; target
4719;; marker-char actual-marker-char
4720;; buffer to from
4721;; overwrite
4722;; overwrite-confirmed
25759a92 4723;; overwrite-query
8749abea
GM
4724;; overwrite-backup-query
4725;; failures skipped success-count
4726;; total))))))))
4727;; (set-buffer old-buf))))
4728
4729;;(defun ange-ftp-dcf-2 (result line err
4730;; file-creator operation fn-list
4731;; name-constructor
4732;; target
4733;; marker-char actual-marker-char
4734;; buffer to from
4735;; overwrite
4736;; overwrite-confirmed
25759a92 4737;; overwrite-query
8749abea
GM
4738;; overwrite-backup-query
4739;; failures skipped success-count
4740;; total)
4741;; (let ((old-buf (current-buffer)))
4742;; (unwind-protect
4743;; (progn
4744;; (set-buffer buffer)
4745;; (if (or err (not result))
4746;; (progn
4747;; (setq failures (cons (dired-make-relative from) failures))
4748;; (dired-log "%s `%s' to `%s' failed:\n%s\n"
4749;; operation from to (or err line)))
4750;; (if overwrite
4751;; ;; If we get here, file-creator hasn't been aborted
4752;; ;; and the old entry (if any) has to be deleted
4753;; ;; before adding the new entry.
4754;; (dired-remove-file to))
4755;; (setq success-count (1+ success-count))
4756;; (message "%s: %d of %d" operation success-count total)
4757;; (dired-add-file to actual-marker-char))
25759a92 4758
8749abea
GM
4759;; (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
4760;; name-constructor
4761;; target
4762;; marker-char
4763;; buffer
25759a92 4764;; overwrite-query
8749abea
GM
4765;; overwrite-backup-query
4766;; failures skipped success-count
4767;; total))
4768;; (set-buffer old-buf))))
4769
4770;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count
4771;; buffer)
4772;; (let ((old-buf (current-buffer)))
4773;; (unwind-protect
4774;; (progn
4775;; (set-buffer buffer)
4776;; (cond
4777;; (failures
4778;; (dired-log-summary
4779;; (message "%s failed for %d of %d file%s %s"
4780;; operation (length failures) total
4781;; (dired-plural-s total) failures)))
4782;; (skipped
4783;; (dired-log-summary
4784;; (message "%s: %d of %d file%s skipped %s"
4785;; operation (length skipped) total
4786;; (dired-plural-s total) skipped)))
4787;; (t
4788;; (message "%s: %s file%s."
4789;; operation success-count (dired-plural-s success-count))))
4790;; (dired-move-to-filename))
4791;; (set-buffer old-buf))))
4792\f
4793;;;; -----------------------------------------------
4794;;;; Unix Descriptive Listing (dl) Support
4795;;;; -----------------------------------------------
4796
4797;; This is turned off because nothing uses it currently
4798;; and because I don't understand what it's supposed to be for. --rms.
4799
4800;;(defconst ange-ftp-dired-dl-re-dir
4801;; "^. [^ /]+/[ \n]"
4802;; "Regular expression to use to search for dl directories.")
4803
4804;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
4805;; (setq ange-ftp-dired-re-dir-alist
4806;; (cons (cons 'unix:dl ange-ftp-dired-dl-re-dir)
4807;; ange-ftp-dired-re-dir-alist)))
4808
4809;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
4810;; "In dired, move to the first character of the filename on this line."
4811;; ;; This is the Unix dl version.
4812;; (or eol (setq eol (progn (end-of-line) (point))))
4813;; (let (case-fold-search)
4814;; (beginning-of-line)
4815;; (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
4816;; (goto-char (+ (point) 2))
4817;; (if raise-error
4818;; (error "No file on this line")
4819;; nil))))
4820
4821;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
4822;; (setq ange-ftp-dired-move-to-filename-alist
4823;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
4824;; ange-ftp-dired-move-to-filename-alist)))
4825
4826;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
4827;; ;; Assumes point is at beginning of filename.
4828;; ;; So, it should be called only after (dired-move-to-filename t).
4829;; ;; On failure, signals an error or returns nil.
4830;; ;; This is the Unix dl version.
4831;; (let ((opoint (point))
4832;; case-fold-search hidden)
4833;; (or eol (setq eol (save-excursion (end-of-line) (point))))
4834;; (setq hidden (and selective-display
4835;; (save-excursion
4836;; (search-forward "\r" eol t))))
4837;; (if hidden
4838;; (if no-error
4839;; nil
4840;; (error
4841;; (substitute-command-keys
4842;; "File line is hidden, type \\[dired-hide-subdir] to unhide")))
4843;; (skip-chars-forward "^ /" eol)
4844;; (if (eq opoint (point))
4845;; (if no-error
4846;; nil
4847;; (error "No file on this line"))
4848;; (point)))))
4849
4850;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
4851;; (setq ange-ftp-dired-move-to-end-of-filename-alist
4852;; (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
4853;; ange-ftp-dired-move-to-end-of-filename-alist)))
4854\f
4855;;;; ------------------------------------------------------------
4856;;;; VOS support (VOS support is probably broken,
4857;;;; but I don't know anything about VOS.)
4858;;;; ------------------------------------------------------------
4859;
4860;(defun ange-ftp-fix-name-for-vos (name &optional reverse)
4861; (setq name (copy-sequence name))
4862; (let ((from (if reverse ?\> ?\/))
4863; (to (if reverse ?\/ ?\>))
4864; (i (1- (length name))))
4865; (while (>= i 0)
4866; (if (= (aref name i) from)
4867; (aset name i to))
4868; (setq i (1- i)))
4869; name))
4870;
4871;(or (assq 'vos ange-ftp-fix-name-func-alist)
4872; (setq ange-ftp-fix-name-func-alist
4873; (cons '(vos . ange-ftp-fix-name-for-vos)
4874; ange-ftp-fix-name-func-alist)))
4875;
4876;(or (memq 'vos ange-ftp-dumb-host-types)
4877; (setq ange-ftp-dumb-host-types
4878; (cons 'vos ange-ftp-dumb-host-types)))
4879;
4880;(defun ange-ftp-fix-dir-name-for-vos (dir-name)
4881; (ange-ftp-fix-name-for-vos
4882; (concat dir-name
4883; (if (eq ?/ (aref dir-name (1- (length dir-name))))
4884; "" "/")
4885; "*")))
4886;
4887;(or (assq 'vos ange-ftp-fix-dir-name-func-alist)
4888; (setq ange-ftp-fix-dir-name-func-alist
4889; (cons '(vos . ange-ftp-fix-dir-name-for-vos)
4890; ange-ftp-fix-dir-name-func-alist)))
4891;
4892;(defvar ange-ftp-vos-host-regexp nil
4893; "If a host matches this regexp then it is assumed to be running VOS.")
4894;
4895;(defun ange-ftp-vos-host (host)
4896; (and ange-ftp-vos-host-regexp
4897; (save-match-data
4898; (string-match ange-ftp-vos-host-regexp host))))
4899;
4900;(defun ange-ftp-parse-vos-listing ()
4901; "Parse the current buffer which is assumed to be in VOS list -all
4902;format, and return a hashtable as the result."
4903; (let ((tbl (ange-ftp-make-hashtable))
4904; (type-list
4905; '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
4906; ("^Dirs: [0-9]+\n+" t 30)))
4907; type-regexp type-is-dir type-col file)
4908; (goto-char (point-min))
4909; (save-match-data
4910; (while type-list
4911; (setq type-regexp (car (car type-list))
4912; type-is-dir (nth 1 (car type-list))
4913; type-col (nth 2 (car type-list))
4914; type-list (cdr type-list))
4915; (if (re-search-forward type-regexp nil t)
4916; (while (eq (char-after (point)) ? )
4917; (move-to-column type-col)
4918; (setq file (buffer-substring (point)
4919; (progn
4920; (end-of-line 1)
4921; (point))))
6e85ef5f 4922; (puthash file type-is-dir tbl)
8749abea 4923; (forward-line 1))))
6e85ef5f
SM
4924; (puthash "." 'vosdir tbl)
4925; (puthash ".." 'vosdir tbl))
8749abea
GM
4926; tbl))
4927;
4928;(or (assq 'vos ange-ftp-parse-list-func-alist)
4929; (setq ange-ftp-parse-list-func-alist
4930; (cons '(vos . ange-ftp-parse-vos-listing)
4931; ange-ftp-parse-list-func-alist)))
4932\f
4933;;;; ------------------------------------------------------------
4934;;;; VMS support.
4935;;;; ------------------------------------------------------------
4936
4937;; Convert NAME from UNIX-ish to VMS. If REVERSE given then convert from VMS
4938;; to UNIX-ish.
4939(defun ange-ftp-fix-name-for-vms (name &optional reverse)
4940 (save-match-data
4941 (if reverse
4942 (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name)
4943 (let (drive dir file)
e3441f42
SM
4944 (setq drive (match-string 1 name))
4945 (setq dir (match-string 2 name))
4946 (setq file (match-string 3 name))
8749abea 4947 (and dir
a1f84f6d 4948 (setq dir (subst-char-in-string
a724ed38 4949 ?/ ?. (substring dir 1 -1) t)))
8749abea
GM
4950 (concat (and drive
4951 (concat "/" drive "/"))
4952 dir (and dir "/")
4953 file))
4954 (error "name %s didn't match" name))
4955 (let (drive dir file tmp)
4956 (if (string-match "^/[^:]+:/" name)
4957 (setq drive (substring name 1
4958 (1- (match-end 0)))
4959 name (substring name (match-end 0))))
4960 (setq tmp (file-name-directory name))
4961 (if tmp
a724ed38 4962 (setq dir (subst-char-in-string ?/ ?. (substring tmp 0 -1) t)))
8749abea
GM
4963 (setq file (file-name-nondirectory name))
4964 (concat drive
4965 (and dir (concat "[" (if drive nil ".") dir "]"))
4966 file)))))
4967
4968;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
4969;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
4970
4971(or (assq 'vms ange-ftp-fix-name-func-alist)
4972 (setq ange-ftp-fix-name-func-alist
4973 (cons '(vms . ange-ftp-fix-name-for-vms)
4974 ange-ftp-fix-name-func-alist)))
4975
4976(or (memq 'vms ange-ftp-dumb-host-types)
4977 (setq ange-ftp-dumb-host-types
4978 (cons 'vms ange-ftp-dumb-host-types)))
4979
4980;; It is important that this function barf for directories for which we know
4981;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
4982;; This is because it saves an unnecessary FTP error, or possibly the listing
4983;; might succeed, but give erroneous info. This last case is particularly
4984;; likely for OS's (like MTS) for which we need to use a wildcard in order
4985;; to list a directory.
4986
4987;; Convert name from UNIX-ish to VMS ready for a DIRectory listing.
4988(defun ange-ftp-fix-dir-name-for-vms (dir-name)
4989 ;; Should there be entries for .. -> [-] and . -> [] below. Don't
4990 ;; think so, because expand-filename should have already short-circuited
4991 ;; them.
4992 (cond ((string-equal dir-name "/")
55535639 4993 (error "Cannot get listing for fictitious \"/\" directory"))
8749abea 4994 ((string-match "^/[-A-Z0-9_$]+:/$" dir-name)
55535639 4995 (error "Cannot get listing for device"))
8749abea 4996 ((ange-ftp-fix-name-for-vms dir-name))))
25759a92 4997
8749abea
GM
4998(or (assq 'vms ange-ftp-fix-dir-name-func-alist)
4999 (setq ange-ftp-fix-dir-name-func-alist
5000 (cons '(vms . ange-ftp-fix-dir-name-for-vms)
5001 ange-ftp-fix-dir-name-func-alist)))
5002
5003(defvar ange-ftp-vms-host-regexp nil)
5004
5005;; Return non-nil if HOST is running VMS.
5006(defun ange-ftp-vms-host (host)
5007 (and ange-ftp-vms-host-regexp
5008 (save-match-data
5009 (string-match ange-ftp-vms-host-regexp host))))
5010
5011;; Because some VMS ftp servers convert filenames to lower case
5012;; we allow a-z in the filename regexp. I'm not too happy about this.
5013
5014(defconst ange-ftp-vms-filename-regexp
5015 (concat
5016 "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\."
5017 "[-_A-Za-z0-9$]*;+[0-9]*\\)")
5018 "Regular expression to match for a valid VMS file name in Dired buffer.
5019Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
5020Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
5021Other orders of $ and _ seem to all work just fine.")
5022
5023;; These parsing functions are as general as possible because the syntax
5024;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
5025;; the VMS filename syntax is so rigid. If they bomb on a listing in the
5026;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
5027;; from vms.weird.net, then too bad.
5028
5029;; Extract the next filename from a VMS dired-like listing.
5030(defun ange-ftp-parse-vms-filename ()
5031 (if (re-search-forward
e3441f42
SM
5032 ange-ftp-vms-filename-regexp
5033 nil t)
5034 (match-string 0)))
8749abea
GM
5035
5036;; Parse the current buffer which is assumed to be in MultiNet FTP dir
5037;; format, and return a hashtable as the result.
5038(defun ange-ftp-parse-vms-listing ()
6e85ef5f 5039 (let ((tbl (make-hash-table :test 'equal))
8749abea
GM
5040 file)
5041 (goto-char (point-min))
5042 (save-match-data
5043 (while (setq file (ange-ftp-parse-vms-filename))
5044 (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
5045 ;; deal with directories
6e85ef5f
SM
5046 (puthash (substring file 0 (match-beginning 0)) t tbl)
5047 (puthash file nil tbl)
8749abea
GM
5048 (if (string-match ";[0-9]+$" file) ; deal with extension
5049 ;; sans extension
6e85ef5f 5050 (puthash (substring file 0 (match-beginning 0)) nil tbl)))
8749abea
GM
5051 (forward-line 1))
5052 ;; Would like to look for a "Total" line, or a "Directory" line to
5053 ;; make sure that the listing isn't complete garbage before putting
5054 ;; in "." and "..", but we can't even count on all VAX's giving us
5055 ;; either of these.
6e85ef5f
SM
5056 (puthash "." t tbl)
5057 (puthash ".." t tbl))
8749abea
GM
5058 tbl))
5059
e3441f42
SM
5060(add-to-list 'ange-ftp-parse-list-func-alist
5061 '(vms . ange-ftp-parse-vms-listing))
8749abea
GM
5062
5063;; This version only deletes file entries which have
5064;; explicit version numbers, because that is all VMS allows.
5065
5066;; Can the following two functions be speeded up using file
5067;; completion functions?
5068
5069(defun ange-ftp-vms-delete-file-entry (name &optional dir-p)
5070 (if dir-p
5071 (ange-ftp-internal-delete-file-entry name t)
5072 (save-match-data
5073 (let ((file (ange-ftp-get-file-part name)))
5074 (if (string-match ";[0-9]+$" file)
25759a92 5075 ;; In VMS you can't delete a file without an explicit
8749abea
GM
5076 ;; version number, or wild-card (e.g. FOO;*)
5077 ;; For now, we give up on wildcards.
6e85ef5f
SM
5078 (let ((files (gethash (file-name-directory name)
5079 ange-ftp-files-hashtable)))
8749abea
GM
5080 (if files
5081 (let* ((root (substring file 0
5082 (match-beginning 0)))
5083 (regexp (concat "^"
5084 (regexp-quote root)
5085 ";[0-9]+$"))
5086 versions)
6e85ef5f 5087 (remhash file files)
8749abea
GM
5088 ;; Now we need to check if there are any
5089 ;; versions left. If not, then delete the
5090 ;; root entry.
6e85ef5f
SM
5091 (maphash
5092 (lambda (key val)
5093 (and (string-match regexp key)
aeb18228 5094 (setq versions t)))
8749abea
GM
5095 files)
5096 (or versions
6e85ef5f 5097 (remhash root files))))))))))
8749abea
GM
5098
5099(or (assq 'vms ange-ftp-delete-file-entry-alist)
5100 (setq ange-ftp-delete-file-entry-alist
5101 (cons '(vms . ange-ftp-vms-delete-file-entry)
5102 ange-ftp-delete-file-entry-alist)))
5103
5104(defun ange-ftp-vms-add-file-entry (name &optional dir-p)
5105 (if dir-p
5106 (ange-ftp-internal-add-file-entry name t)
6e85ef5f
SM
5107 (let ((files (gethash (file-name-directory name)
5108 ange-ftp-files-hashtable)))
8749abea
GM
5109 (if files
5110 (let ((file (ange-ftp-get-file-part name)))
5111 (save-match-data
5112 (if (string-match ";[0-9]+$" file)
6e85ef5f 5113 (puthash (substring file 0 (match-beginning 0)) nil files)
8749abea
GM
5114 ;; Need to figure out what version of the file
5115 ;; is being added.
5116 (let ((regexp (concat "^"
5117 (regexp-quote file)
5118 ";\\([0-9]+\\)$"))
5119 (version 0))
6e85ef5f
SM
5120 (maphash
5121 (lambda (name val)
5122 (and (string-match regexp name)
5123 (setq version
5124 (max version
027a4b6b 5125 (string-to-number (match-string 1 name))))))
8749abea
GM
5126 files)
5127 (setq version (1+ version))
6e85ef5f 5128 (puthash
8749abea
GM
5129 (concat file ";" (int-to-string version))
5130 nil files))))
6e85ef5f 5131 (puthash file nil files))))))
8749abea
GM
5132
5133(or (assq 'vms ange-ftp-add-file-entry-alist)
5134 (setq ange-ftp-add-file-entry-alist
5135 (cons '(vms . ange-ftp-vms-add-file-entry)
5136 ange-ftp-add-file-entry-alist)))
5137
5138
5139(defun ange-ftp-add-vms-host (host)
5140 "Mark HOST as the name of a machine running VMS."
5141 (interactive
5142 (list (read-string "Host: "
5143 (let ((name (or (buffer-file-name) default-directory)))
5144 (and name (car (ange-ftp-ftp-name name)))))))
5145 (if (not (ange-ftp-vms-host host))
5146 (setq ange-ftp-vms-host-regexp
5147 (concat "^" (regexp-quote host) "$"
5148 (and ange-ftp-vms-host-regexp "\\|")
5149 ange-ftp-vms-host-regexp)
5150 ange-ftp-host-cache nil)))
5151
5152
5153(defun ange-ftp-vms-file-name-as-directory (name)
5154 (save-match-data
5155 (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
5156 (setq name (substring name 0 (match-beginning 0))))
5157 (ange-ftp-real-file-name-as-directory name)))
5158
5159(or (assq 'vms ange-ftp-file-name-as-directory-alist)
5160 (setq ange-ftp-file-name-as-directory-alist
5161 (cons '(vms . ange-ftp-vms-file-name-as-directory)
5162 ange-ftp-file-name-as-directory-alist)))
5163
5164;;; Tree dired support:
5165
5166;; For this code I have borrowed liberally from Sebastian Kremer's
5167;; dired-vms.el
5168
5169
5170;;;; These regexps must be anchored to beginning of line.
5171;;;; Beware that the ftpd may put the device in front of the filename.
5172
5173;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
5174;; "Regular expression to use to search for VMS executable files.")
5175
5176;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
5177;; "Regular expression to use to search for VMS directories.")
5178
5179;;(or (assq 'vms ange-ftp-dired-re-exe-alist)
5180;; (setq ange-ftp-dired-re-exe-alist
5181;; (cons (cons 'vms ange-ftp-dired-vms-re-exe)
5182;; ange-ftp-dired-re-exe-alist)))
5183
5184;;(or (assq 'vms ange-ftp-dired-re-dir-alist)
5185;; (setq ange-ftp-dired-re-dir-alist
5186;; (cons (cons 'vms ange-ftp-dired-vms-re-dir)
5187;; ange-ftp-dired-re-dir-alist)))
5188
5189;;(defun ange-ftp-dired-vms-insert-headerline (dir)
5190;; ;; VMS inserts a headerline. I would prefer the headerline
5191;; ;; to be in ange-ftp format. This version tries to
5192;; ;; be careful, because we can't count on a headerline
5193;; ;; over ftp, and we wouldn't want to delete anything
5194;; ;; important.
5195;; (save-excursion
5196;; (if (looking-at "^ wildcard ")
5197;; (forward-line 1))
5198;; (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
5199;; (delete-region (point) (match-end 0))))
5200;; (ange-ftp-real-dired-insert-headerline dir))
5201
5202;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist)
5203;; (setq ange-ftp-dired-insert-headerline-alist
5204;; (cons '(vms . ange-ftp-dired-vms-insert-headerline)
5205;; ange-ftp-dired-insert-headerline-alist)))
5206
5207;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
5208;; "In dired, move to first char of filename on this line.
5209;;Returns position (point) or nil if no filename on this line."
5210;; ;; This is the VMS version.
5211;; (let (case-fold-search)
5212;; (or eol (setq eol (progn (end-of-line) (point))))
5213;; (beginning-of-line)
5214;; (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
5215;; (goto-char (match-beginning 1))
5216;; (if raise-error
5217;; (error "No file on this line")
5218;; nil))))
5219
5220;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist)
5221;; (setq ange-ftp-dired-move-to-filename-alist
5222;; (cons '(vms . ange-ftp-dired-vms-move-to-filename)
5223;; ange-ftp-dired-move-to-filename-alist)))
5224
5225;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
5226;; ;; Assumes point is at beginning of filename.
5227;; ;; So, it should be called only after (dired-move-to-filename t).
5228;; ;; case-fold-search must be nil, at least for VMS.
5229;; ;; On failure, signals an error or returns nil.
5230;; ;; This is the VMS version.
5231;; (let (opoint hidden case-fold-search)
5232;; (setq opoint (point))
5233;; (or eol (setq eol (save-excursion (end-of-line) (point))))
5234;; (setq hidden (and selective-display
5235;; (save-excursion (search-forward "\r" eol t))))
5236;; (if hidden
5237;; nil
5238;; (re-search-forward ange-ftp-vms-filename-regexp eol t))
5239;; (or no-error
5240;; (not (eq opoint (point)))
5241;; (error
5242;; (if hidden
5243;; (substitute-command-keys
5244;; "File line is hidden, type \\[dired-hide-subdir] to unhide")
5245;; "No file on this line")))
5246;; (if (eq opoint (point))
5247;; nil
5248;; (point))))
5249
5250;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
5251;; (setq ange-ftp-dired-move-to-end-of-filename-alist
5252;; (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
5253;; ange-ftp-dired-move-to-end-of-filename-alist)))
5254
5255;;(defun ange-ftp-dired-vms-between-files ()
5256;; (save-excursion
5257;; (beginning-of-line)
5258;; (or (equal (following-char) 10) ; newline
5259;; (equal (following-char) 9) ; tab
5260;; (progn (forward-char 2)
5261;; (or (looking-at "Total of")
5262;; (equal (following-char) 32))))))
5263
5264;;(or (assq 'vms ange-ftp-dired-between-files-alist)
5265;; (setq ange-ftp-dired-between-files-alist
5266;; (cons '(vms . ange-ftp-dired-vms-between-files)
5267;; ange-ftp-dired-between-files-alist)))
5268
5269;; Beware! In VMS filenames must be of the form "FILE.TYPE".
5270;; Therefore, we cannot just append a ".Z" to filenames for
5271;; compressed files. Instead, we turn "FILE.TYPE" into
5272;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
5273
5274(defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
5275 (cond
5276 ((string-match "-Z;[0-9]+$" name)
5277 (list nil (substring name 0 (match-beginning 0))))
5278 ((string-match ";[0-9]+$" name)
5279 (list nil (substring name 0 (match-beginning 0))))
5280 ((string-match "-Z$" name)
5281 (list nil (substring name 0 -2)))
5282 (t
5283 (list t
5284 (if (string-match ";[0-9]+$" name)
5285 (concat (substring name 0 (match-beginning 0))
5286 "-Z")
5287 (concat name "-Z"))))))
5288
5289(or (assq 'vms ange-ftp-make-compressed-filename-alist)
5290 (setq ange-ftp-make-compressed-filename-alist
5291 (cons '(vms . ange-ftp-vms-make-compressed-filename)
5292 ange-ftp-make-compressed-filename-alist)))
5293
5294;;;; When the filename is too long, VMS will use two lines to list a file
5295;;;; (damn them!) This will confuse dired. To solve this, need to convince
5296;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
5297;;;; (forward-line 1). This would require a number of changes to dired.el.
5298;;;; If dired gets confused, revert-buffer will fix it.
5299
5300;;(defun ange-ftp-dired-vms-ls-trim ()
5301;; (goto-char (point-min))
5302;; (let ((case-fold-search nil))
5303;; (re-search-forward ange-ftp-vms-filename-regexp))
5304;; (beginning-of-line)
5305;; (delete-region (point-min) (point))
5306;; (forward-line 1)
5307;; (delete-region (point) (point-max)))
5308
5309
5310;;(or (assq 'vms ange-ftp-dired-ls-trim-alist)
5311;; (setq ange-ftp-dired-ls-trim-alist
5312;; (cons '(vms . ange-ftp-dired-vms-ls-trim)
25759a92 5313;; ange-ftp-dired-ls-trim-alist)))
8749abea
GM
5314
5315(defun ange-ftp-vms-sans-version (name &rest args)
5316 (save-match-data
5317 (if (string-match ";[0-9]+$" name)
5318 (substring name 0 (match-beginning 0))
5319 name)))
5320
5321(or (assq 'vms ange-ftp-sans-version-alist)
5322 (setq ange-ftp-sans-version-alist
5323 (cons '(vms . ange-ftp-vms-sans-version)
5324 ange-ftp-sans-version-alist)))
5325
5326;;(defvar ange-ftp-file-version-alist)
5327
5328;;;;; The vms version of clean-directory has 2 more optional args
5329;;;;; than the usual dired version. This is so that it can be used by
5330;;;;; ange-ftp-dired-vms-flag-backup-files.
5331
5332;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
5333;; "Flag numerical backups for deletion.
5334;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
5335;;Positive prefix arg KEEP overrides `dired-kept-versions';
5336;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
5337
5338;;To clear the flags on these files, you can use \\[dired-flag-backup-files]
5339;;with a prefix argument."
5340;;; (interactive "P") ; Never actually called interactively.
5341;; (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
5342;; (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
5343;; ;; late-retention must NEVER be allowed to be less than 1 in VMS!
5344;; ;; This could wipe ALL copies of the file.
5345;; (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
5346;; (action (or msg "Cleaning"))
5347;; (ange-ftp-trample-marker (or marker dired-del-marker))
5348;; (ange-ftp-file-version-alist ()))
5349;; (message (concat action
5350;; " numerical backups (keeping %d late, %d old)...")
5351;; late-retention early-retention)
5352;; ;; Look at each file.
5353;; ;; If the file has numeric backup versions,
5354;; ;; put on ange-ftp-file-version-alist an element of the form
5355;; ;; (FILENAME . VERSION-NUMBER-LIST)
e3441f42 5356;; (dired-map-dired-file-lines 'ange-ftp-dired-vms-collect-file-versions)
8749abea
GM
5357;; ;; Sort each VERSION-NUMBER-LIST,
5358;; ;; and remove the versions not to be deleted.
5359;; (let ((fval ange-ftp-file-version-alist))
5360;; (while fval
5361;; (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
5362;; (v-count (length sorted-v-list)))
5363;; (if (> v-count (+ early-retention late-retention))
5364;; (rplacd (nthcdr early-retention sorted-v-list)
5365;; (nthcdr (- v-count late-retention)
5366;; sorted-v-list)))
5367;; (rplacd (car fval)
5368;; (cdr sorted-v-list)))
5369;; (setq fval (cdr fval))))
5370;; ;; Look at each file. If it is a numeric backup file,
5371;; ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
5372;; (dired-map-dired-file-lines
e3441f42 5373;; 'ange-ftp-dired-vms-trample-file-versions mark)
8749abea
GM
5374;; (message (concat action " numerical backups...done"))))
5375
5376;;(or (assq 'vms ange-ftp-dired-clean-directory-alist)
5377;; (setq ange-ftp-dired-clean-directory-alist
5378;; (cons '(vms . ange-ftp-dired-vms-clean-directory)
5379;; ange-ftp-dired-clean-directory-alist)))
5380
5381;;(defun ange-ftp-dired-vms-collect-file-versions (fn)
5382;; ;; "If it looks like file FN has versions, return a list of the versions.
5383;; ;;That is a list of strings which are file names.
5384;; ;;The caller may want to flag some of these files for deletion."
5385;;(let ((name (nth 2 (ange-ftp-ftp-name fn))))
5386;; (if (string-match ";[0-9]+$" name)
5387;; (let* ((name (substring name 0 (match-beginning 0)))
5388;; (fn (ange-ftp-replace-name-component fn name)))
5389;; (if (not (assq fn ange-ftp-file-version-alist))
5390;; (let* ((base-versions
5391;; (concat (file-name-nondirectory name) ";"))
5392;; (bv-length (length base-versions))
5393;; (possibilities (file-name-all-completions
5394;; base-versions
5395;; (file-name-directory fn)))
5396;; (versions (mapcar
5397;; '(lambda (arg)
5398;; (if (and (string-match
5399;; "[0-9]+$" arg bv-length)
5400;; (= (match-beginning 0) bv-length))
5401;; (string-to-int (substring arg bv-length))
5402;; 0))
5403;; possibilities)))
5404;; (if versions
5405;; (setq
5406;; ange-ftp-file-version-alist
5407;; (cons (cons fn versions)
5408;; ange-ftp-file-version-alist)))))))))
5409
5410;;(defun ange-ftp-dired-vms-trample-file-versions (fn)
5411;; (let* ((start-vn (string-match ";[0-9]+$" fn))
5412;; base-version-list)
5413;; (and start-vn
5414;; (setq base-version-list ; there was a base version to which
5415;; (assoc (substring fn 0 start-vn) ; this looks like a
5416;; ange-ftp-file-version-alist)) ; subversion
5417;; (not (memq (string-to-int (substring fn (1+ start-vn)))
5418;; base-version-list)) ; this one doesn't make the cut
5419;; (progn (beginning-of-line)
5420;; (delete-char 1)
5421;; (insert ange-ftp-trample-marker)))))
5422
5423;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
5424;; (let ((dired-kept-versions 1)
5425;; (kept-old-versions 0)
5426;; marker msg)
5427;; (if unflag-p
5428;; (setq marker ?\040 msg "Unflagging")
5429;; (setq marker dired-del-marker msg "Cleaning"))
5430;; (ange-ftp-dired-vms-clean-directory nil marker msg)))
5431
5432;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
5433;; (setq ange-ftp-dired-flag-backup-files-alist
5434;; (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
5435;; ange-ftp-dired-flag-backup-files-alist)))
5436
5437;;(defun ange-ftp-dired-vms-backup-diff (&optional switches)
5438;; (let ((file (dired-get-filename 'no-dir))
5439;; bak)
5440;; (if (and (string-match ";[0-9]+$" file)
5441;; ;; Find most recent previous version.
5442;; (let ((root (substring file 0 (match-beginning 0)))
5443;; (ver
5444;; (string-to-int (substring file (1+ (match-beginning 0)))))
5445;; found)
5446;; (setq ver (1- ver))
5447;; (while (and (> ver 0) (not found))
5448;; (setq bak (concat root ";" (int-to-string ver)))
5449;; (and (file-exists-p bak) (setq found t))
5450;; (setq ver (1- ver)))
5451;; found))
5452;; (if switches
5453;; (diff (expand-file-name bak) (expand-file-name file) switches)
5454;; (diff (expand-file-name bak) (expand-file-name file)))
5455;; (error "No previous version found for %s" file))))
5456
5457;;(or (assq 'vms ange-ftp-dired-backup-diff-alist)
5458;; (setq ange-ftp-dired-backup-diff-alist
5459;; (cons '(vms . ange-ftp-dired-vms-backup-diff)
5460;; ange-ftp-dired-backup-diff-alist)))
5461
5462\f
5463;;;; ------------------------------------------------------------
5464;;;; MTS support
5465;;;; ------------------------------------------------------------
5466
5467
5468;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from
5469;; MTS to UNIX-ish.
5470(defun ange-ftp-fix-name-for-mts (name &optional reverse)
5471 (save-match-data
5472 (if reverse
5473 (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name)
5474 (let (acct file)
e3441f42
SM
5475 (setq acct (match-string 1 name))
5476 (setq file (match-string 2 name))
8749abea
GM
5477 (concat (and acct (concat "/" acct "/"))
5478 file))
5479 (error "name %s didn't match" name))
5480 (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name)
e3441f42 5481 (concat (match-string 1 name) (match-string 2 name))
8749abea
GM
5482 ;; Let's hope that mts will recognize it anyway.
5483 name))))
5484
5485(or (assq 'mts ange-ftp-fix-name-func-alist)
5486 (setq ange-ftp-fix-name-func-alist
5487 (cons '(mts . ange-ftp-fix-name-for-mts)
5488 ange-ftp-fix-name-func-alist)))
5489
5490;; Convert name from UNIX-ish to MTS ready for a DIRectory listing.
5491;; Remember that there are no directories in MTS.
5492(defun ange-ftp-fix-dir-name-for-mts (dir-name)
5493 (if (string-equal dir-name "/")
55535639 5494 (error "Cannot get listing for fictitious \"/\" directory")
8749abea
GM
5495 (let ((dir-name (ange-ftp-fix-name-for-mts dir-name)))
5496 (cond
5497 ((string-equal dir-name "")
5498 "?")
5499 ((string-match ":$" dir-name)
5500 (concat dir-name "?"))
5501 (dir-name))))) ; It's just a single file.
5502
5503(or (assq 'mts ange-ftp-fix-dir-name-func-alist)
5504 (setq ange-ftp-fix-dir-name-func-alist
5505 (cons '(mts . ange-ftp-fix-dir-name-for-mts)
5506 ange-ftp-fix-dir-name-func-alist)))
5507
5508(or (memq 'mts ange-ftp-dumb-host-types)
5509 (setq ange-ftp-dumb-host-types
5510 (cons 'mts ange-ftp-dumb-host-types)))
5511
5512(defvar ange-ftp-mts-host-regexp nil)
5513
5514;; Return non-nil if HOST is running MTS.
5515(defun ange-ftp-mts-host (host)
5516 (and ange-ftp-mts-host-regexp
5517 (save-match-data
5518 (string-match ange-ftp-mts-host-regexp host))))
5519
5520;; Parse the current buffer which is assumed to be in mts ftp dir format.
5521(defun ange-ftp-parse-mts-listing ()
6e85ef5f 5522 (let ((tbl (make-hash-table :test 'equal)))
8749abea
GM
5523 (goto-char (point-min))
5524 (save-match-data
5525 (while (re-search-forward ange-ftp-date-regexp nil t)
5526 (end-of-line)
5527 (skip-chars-backward " ")
5528 (let ((end (point)))
5529 (skip-chars-backward "-A-Z0-9_.!")
6e85ef5f 5530 (puthash (buffer-substring (point) end) nil tbl))
8749abea 5531 (forward-line 1)))
6e85ef5f
SM
5532 ;; Don't need to bother with ..
5533 (puthash "." t tbl)
8749abea
GM
5534 tbl))
5535
e3441f42
SM
5536(add-to-list 'ange-ftp-parse-list-func-alist
5537 '(mts . ange-ftp-parse-mts-listing))
8749abea
GM
5538
5539(defun ange-ftp-add-mts-host (host)
5540 "Mark HOST as the name of a machine running MTS."
5541 (interactive
5542 (list (read-string "Host: "
5543 (let ((name (or (buffer-file-name) default-directory)))
5544 (and name (car (ange-ftp-ftp-name name)))))))
5545 (if (not (ange-ftp-mts-host host))
5546 (setq ange-ftp-mts-host-regexp
5547 (concat "^" (regexp-quote host) "$"
5548 (and ange-ftp-mts-host-regexp "\\|")
5549 ange-ftp-mts-host-regexp)
5550 ange-ftp-host-cache nil)))
5551
5552;;; Tree dired support:
5553
5554;;;; There aren't too many systems left that use MTS. This dired support will
5555;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
5556;;;; implement ftp in the same way. If not, it might be necessary to make the
5557;;;; following more flexible.
5558
5559;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
5560;; "In dired, move to first char of filename on this line.
5561;;Returns position (point) or nil if no filename on this line."
5562;; ;; This is the MTS version.
5563;; (or eol (setq eol (progn (end-of-line) (point))))
5564;; (beginning-of-line)
5565;; (if (re-search-forward
5566;; ange-ftp-date-regexp eol t)
5567;; (progn
5568;; (skip-chars-forward " ") ; Eat blanks after date
5569;; (skip-chars-forward "0-9:" eol) ; Eat time or year
5570;; (skip-chars-forward " " eol) ; one space before filename
5571;; ;; When listing an account other than the users own account it appends
5572;; ;; ACCT: to the beginning of the filename. Skip over this.
5573;; (and (looking-at "[A-Z0-9_.]+:")
5574;; (goto-char (match-end 0)))
5575;; (point))
5576;; (if raise-error
5577;; (error "No file on this line")
5578;; nil)))
5579
5580;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist)
5581;; (setq ange-ftp-dired-move-to-filename-alist
5582;; (cons '(mts . ange-ftp-dired-mts-move-to-filename)
5583;; ange-ftp-dired-move-to-filename-alist)))
5584
5585;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
5586;; ;; Assumes point is at beginning of filename.
5587;; ;; So, it should be called only after (dired-move-to-filename t).
5588;; ;; On failure, signals an error or returns nil.
5589;; ;; This is the MTS version.
5590;; (let (opoint hidden case-fold-search)
5591;; (setq opoint (point)
5592;; eol (save-excursion (end-of-line) (point))
5593;; hidden (and selective-display
5594;; (save-excursion (search-forward "\r" eol t))))
5595;; (if hidden
5596;; nil
5597;; (skip-chars-forward "-A-Z0-9._!" eol))
5598;; (or no-error
5599;; (not (eq opoint (point)))
5600;; (error
5601;; (if hidden
5602;; (substitute-command-keys
5603;; "File line is hidden, type \\[dired-hide-subdir] to unhide")
5604;; "No file on this line")))
5605;; (if (eq opoint (point))
5606;; nil
5607;; (point))))
5608
5609;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
5610;; (setq ange-ftp-dired-move-to-end-of-filename-alist
5611;; (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
5612;; ange-ftp-dired-move-to-end-of-filename-alist)))
5613\f
5614;;;; ------------------------------------------------------------
5615;;;; CMS support
5616;;;; ------------------------------------------------------------
5617
5618;; Since CMS doesn't have any full file name syntax, we have to fudge
5619;; things with cd's. We actually send too many cd's, but it's dangerous
5620;; to try to remember the current minidisk, because if the connection
5621;; is closed and needs to be reopened, we will find ourselves back in
5622;; the default minidisk. This is fairly likely since CMS ftp servers
5623;; usually close the connection after 5 minutes of inactivity.
5624
5625;; Have I got the filename character set right?
5626
5627(defun ange-ftp-fix-name-for-cms (name &optional reverse)
5628 (save-match-data
5629 (if reverse
5630 ;; Since we only convert output from a pwd in this direction,
5631 ;; we'll assume that it's a minidisk, and make it into a
5632 ;; directory file name. Note that the expand-dir-hashtable
5633 ;; stores directories without the trailing /. Is this
5634 ;; consistent?
5635 (concat "/" name)
5636 (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
5637 name)
e3441f42 5638 (let ((minidisk (match-string 1 name)))
8749abea 5639 (if (match-beginning 2)
e3441f42 5640 (let ((file (match-string 2 name))
8749abea
GM
5641 (cmd (concat "cd " minidisk))
5642
5643 ;; Note that host and user are bound in the call
5644 ;; to ange-ftp-send-cmd
5645 (proc (ange-ftp-get-process ange-ftp-this-host
5646 ange-ftp-this-user)))
5647
5648 ;; Must use ange-ftp-raw-send-cmd here to avoid
5649 ;; an infinite loop.
5650 (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg))
5651 file
5652 ;; failed... try ONCE more.
5653 (setq proc (ange-ftp-get-process ange-ftp-this-host
5654 ange-ftp-this-user))
5655 (let ((result (ange-ftp-raw-send-cmd proc cmd
5656 ange-ftp-this-msg)))
5657 (if (car result)
5658 file
5659 ;; failed. give up.
5660 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
5661 (format "cd to minidisk %s failed: %s"
5662 minidisk (cdr result)))))))
5663 ;; return the minidisk
5664 minidisk))
5665 (error "Invalid CMS filename")))))
5666
5667(or (assq 'cms ange-ftp-fix-name-func-alist)
5668 (setq ange-ftp-fix-name-func-alist
5669 (cons '(cms . ange-ftp-fix-name-for-cms)
5670 ange-ftp-fix-name-func-alist)))
5671
5672(or (memq 'cms ange-ftp-dumb-host-types)
5673 (setq ange-ftp-dumb-host-types
5674 (cons 'cms ange-ftp-dumb-host-types)))
5675
5676;; Convert name from UNIX-ish to CMS ready for a DIRectory listing.
5677(defun ange-ftp-fix-dir-name-for-cms (dir-name)
5678 (cond
5679 ((string-equal "/" dir-name)
55535639 5680 (error "Cannot get listing for fictitious \"/\" directory"))
8749abea 5681 ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name)
e3441f42 5682 (let* ((minidisk (match-string 1 dir-name))
8749abea
GM
5683 ;; host and user are bound in the call to ange-ftp-send-cmd
5684 (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user))
5685 (cmd (concat "cd " minidisk))
5686 (file (if (match-beginning 2)
5687 ;; it's a single file
e3441f42 5688 (match-string 2 dir-name)
8749abea
GM
5689 ;; use the wild-card
5690 "*")))
5691 (if (car (ange-ftp-raw-send-cmd proc cmd))
5692 file
5693 ;; try again...
5694 (setq proc (ange-ftp-get-process ange-ftp-this-host
5695 ange-ftp-this-user))
5696 (let ((result (ange-ftp-raw-send-cmd proc cmd)))
5697 (if (car result)
5698 file
5699 ;; give up
5700 (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
5701 (format "cd to minidisk %s failed: %s"
5702 minidisk (cdr result))))))))
5703 (t (error "Invalid CMS file name"))))
5704
5705(or (assq 'cms ange-ftp-fix-dir-name-func-alist)
5706 (setq ange-ftp-fix-dir-name-func-alist
5707 (cons '(cms . ange-ftp-fix-dir-name-for-cms)
5708 ange-ftp-fix-dir-name-func-alist)))
5709
5710(defvar ange-ftp-cms-host-regexp nil
5711 "Regular expression to match hosts running the CMS operating system.")
5712
5713;; Return non-nil if HOST is running CMS.
5714(defun ange-ftp-cms-host (host)
5715 (and ange-ftp-cms-host-regexp
5716 (save-match-data
5717 (string-match ange-ftp-cms-host-regexp host))))
5718
5719(defun ange-ftp-add-cms-host (host)
5720 "Mark HOST as the name of a CMS host."
5721 (interactive
5722 (list (read-string "Host: "
5723 (let ((name (or (buffer-file-name) default-directory)))
5724 (and name (car (ange-ftp-ftp-name name)))))))
5725 (if (not (ange-ftp-cms-host host))
5726 (setq ange-ftp-cms-host-regexp
5727 (concat "^" (regexp-quote host) "$"
5728 (and ange-ftp-cms-host-regexp "\\|")
5729 ange-ftp-cms-host-regexp)
5730 ange-ftp-host-cache nil)))
5731
5732(defun ange-ftp-parse-cms-listing ()
5733 ;; Parse the current buffer which is assumed to be a CMS directory listing.
5734 ;; If we succeed in getting a listing, then we will assume that the minidisk
5735 ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
5736 ;; because ange-ftp doesn't know that the root hashtable has only part of
5737 ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
5738 ;; exist. It would be nice if completion worked for minidisks, as we
5739 ;; discover them.
5740; (let* ((dir-file (directory-file-name file))
5741; (root (file-name-directory dir-file))
5742; (minidisk (ange-ftp-get-file-part dir-file))
5743; (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
5744; (if root-tbl
6e85ef5f 5745; (puthash minidisk t root-tbl)
8749abea 5746; (setq root-tbl (ange-ftp-make-hashtable))
6e85ef5f
SM
5747; (puthash minidisk t root-tbl)
5748; (puthash "." t root-tbl)
8749abea
GM
5749; (ange-ftp-set-files root root-tbl)))
5750 ;; Now do the usual parsing
6e85ef5f 5751 (let ((tbl (make-hash-table :test 'equal)))
8749abea
GM
5752 (goto-char (point-min))
5753 (save-match-data
5754 (while
5755 (re-search-forward
5756 "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
e3441f42 5757 (puthash (concat (match-string 1) "." (match-string 2)) nil tbl)
8749abea 5758 (forward-line 1))
6e85ef5f 5759 (puthash "." t tbl))
8749abea
GM
5760 tbl))
5761
e3441f42
SM
5762(add-to-list 'ange-ftp-parse-list-func-alist
5763 '(cms . ange-ftp-parse-cms-listing))
25759a92 5764
8749abea
GM
5765;;;;; Tree dired support:
5766
5767;;(defconst ange-ftp-dired-cms-re-exe
5768;; "^. [-A-Z0-9$_]+ +EXEC "
5769;; "Regular expression to use to search for CMS executables.")
5770
5771;;(or (assq 'cms ange-ftp-dired-re-exe-alist)
5772;; (setq ange-ftp-dired-re-exe-alist
5773;; (cons (cons 'cms ange-ftp-dired-cms-re-exe)
5774;; ange-ftp-dired-re-exe-alist)))
5775
5776
5777;;(defun ange-ftp-dired-cms-insert-headerline (dir)
5778;; ;; CMS has no total line, so we insert a blank line for
5779;; ;; aesthetics.
5780;; (insert "\n")
5781;; (forward-char -1)
5782;; (ange-ftp-real-dired-insert-headerline dir))
5783
5784;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist)
5785;; (setq ange-ftp-dired-insert-headerline-alist
5786;; (cons '(cms . ange-ftp-dired-cms-insert-headerline)
5787;; ange-ftp-dired-insert-headerline-alist)))
5788
5789;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
5790;; "In dired, move to the first char of filename on this line."
5791;; ;; This is the CMS version.
5792;; (or eol (setq eol (progn (end-of-line) (point))))
5793;; (let (case-fold-search)
5794;; (beginning-of-line)
5795;; (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
5796;; (goto-char (1+ (match-beginning 0)))
5797;; (if raise-error
5798;; (error "No file on this line")
5799;; nil))))
5800
5801;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist)
5802;; (setq ange-ftp-dired-move-to-filename-alist
5803;; (cons '(cms . ange-ftp-dired-cms-move-to-filename)
5804;; ange-ftp-dired-move-to-filename-alist)))
5805
5806;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
5807;; ;; Assumes point is at beginning of filename.
5808;; ;; So, it should be called only after (dired-move-to-filename t).
5809;; ;; case-fold-search must be nil, at least for VMS.
5810;; ;; On failure, signals an error or returns nil.
5811;; ;; This is the CMS version.
5812;; (let ((opoint (point))
5813;; case-fold-search hidden)
5814;; (or eol (setq eol (save-excursion (end-of-line) (point))))
5815;; (setq hidden (and selective-display
5816;; (save-excursion
5817;; (search-forward "\r" eol t))))
5818;; (if hidden
5819;; (if no-error
5820;; nil
5821;; (error
5822;; (substitute-command-keys
5823;; "File line is hidden, type \\[dired-hide-subdir] to unhide")))
5824;; (skip-chars-forward "-A-Z0-9$_" eol)
5825;; (skip-chars-forward " " eol)
5826;; (skip-chars-forward "-A-Z0-9$_" eol)
5827;; (if (eq opoint (point))
5828;; (if no-error
5829;; nil
5830;; (error "No file on this line"))
5831;; (point)))))
5832
5833;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
5834;; (setq ange-ftp-dired-move-to-end-of-filename-alist
5835;; (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
5836;; ange-ftp-dired-move-to-end-of-filename-alist)))
5837
5838(defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
5839 (if (string-match "-Z$" name)
5840 (list nil (substring name 0 -2))
5841 (list t (concat name "-Z"))))
5842
5843(or (assq 'cms ange-ftp-make-compressed-filename-alist)
5844 (setq ange-ftp-make-compressed-filename-alist
5845 (cons '(cms . ange-ftp-cms-make-compressed-filename)
5846 ange-ftp-make-compressed-filename-alist)))
5847
5848;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
5849;; (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
5850;; (and name
5851;; (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
5852;; (concat (substring name 0 (match-end 1))
5853;; "."
5854;; (substring name (match-beginning 2) (match-end 2)))
5855;; name))))
5856
5857;;(or (assq 'cms ange-ftp-dired-get-filename-alist)
5858;; (setq ange-ftp-dired-get-filename-alist
5859;; (cons '(cms . ange-ftp-dired-cms-get-filename)
5860;; ange-ftp-dired-get-filename-alist)))
5861\f
9c35d706
RS
5862;;;; ------------------------------------------------------------
5863;;;; BS2000 support
5864;;;; ------------------------------------------------------------
5865
5866;; There seems to be an error with regexps. '-' has to be the first
5867;; character inside of the square brackets.
5868(defconst ange-ftp-bs2000-short-filename-regexp
5869 "[-A-Z0-9$#@.]*[A-Z][-A-Z0-9$#@.]*"
5870 "Regular expression to match for a valid short BS2000 file name.")
5871
5872(defconst ange-ftp-bs2000-fix-name-regexp-reverse
5873 (concat
5874 "^\\(" ange-ftp-bs2000-filename-pubset-regexp "\\)?"
5875 "\\(" ange-ftp-bs2000-filename-username-regexp "\\)?"
5876 "\\(" ange-ftp-bs2000-short-filename-regexp "\\)?")
6e85ef5f 5877 "Regular expression used in ange-ftp-fix-name-for-bs2000.")
9c35d706
RS
5878
5879(defconst ange-ftp-bs2000-fix-name-regexp
5880 (concat
5881 "/?\\(" ange-ftp-bs2000-filename-pubset-regexp "/\\)?"
5882 "\\(\\$[A-Z0-9]*/\\)?"
5883 "\\(" ange-ftp-bs2000-short-filename-regexp "\\)?")
6e85ef5f 5884 "Regular expression used in ange-ftp-fix-name-for-bs2000.")
9c35d706
RS
5885
5886(defcustom ange-ftp-bs2000-special-prefix
5887 "X"
5888 "*Prefix used for filenames starting with '#' or '@'."
5889 :group 'ange-ftp
5890 :type 'string)
5891
5892;; Convert NAME from UNIX-ish to BS2000. If REVERSE given then convert from
5893;; BS2000 to UNIX-ish.
5894(defun ange-ftp-fix-name-for-bs2000 (name &optional reverse)
5895 (save-match-data
5896 (if reverse
5897 (if (string-match
5898 ange-ftp-bs2000-fix-name-regexp-reverse
5899 name)
5900 (let ((pubset (if (match-beginning 1)
5901 (substring name 0 (match-end 1))))
5902 (userid (if (match-beginning 2)
5903 (substring name
5904 (match-beginning 2)
5905 (1- (match-end 2)))))
5906 (filename (if (match-beginning 3)
5907 (substring name (match-beginning 3)))))
5908 (concat
5909 "/"
5910 ;; we have to insert "_/" here to prevent expand-file-name to
5911 ;; interpret BS2000 pubsets as the special escape prefix:
5912 (and pubset (concat "_/" pubset "/"))
5913 (and userid (concat userid "/"))
5914 filename))
5915 (error "name %s didn't match" name))
5916 ;; and here we (maybe) have to remove the inserted "_/" 'cause
5917 ;; of our prevention of the special escape prefix above:
5918 (if (string-match (concat "^/_/") name)
5919 (setq name (substring name 2)))
5920 (if (string-match
5921 ange-ftp-bs2000-fix-name-regexp
5922 name)
5923 (let ((pubset (if (match-beginning 1)
5924 (substring name
5925 (match-beginning 1)
5926 (1- (match-end 1)))))
5927 (userid (if (match-beginning 2)
5928 (substring name
5929 (match-beginning 2)
5930 (1- (match-end 2)))))
5931 (filename (if (match-beginning 3)
5932 (substring name (match-beginning 3)))))
5933 (if (and (boundp 'filename)
5934 (stringp filename)
5935 (string-match "[#@].+" filename))
5936 (setq filename (concat ange-ftp-bs2000-special-prefix
5937 (substring filename 1))))
5938 (upcase
5939 (concat
5940 pubset
5941 (and userid (concat userid "."))
5942 ;; change every '/' in filename to a '.', normally not neccessary
5943 (and filename
e3441f42 5944 (subst-char-in-string ?/ ?. filename)))))
9c35d706
RS
5945 ;; Let's hope that BS2000 recognize this anyway:
5946 name))))
5947
5948(or (assq 'bs2000 ange-ftp-fix-name-func-alist)
5949 (setq ange-ftp-fix-name-func-alist
5950 (cons '(bs2000 . ange-ftp-fix-name-for-bs2000)
5951 ange-ftp-fix-name-func-alist)))
5952
5953;; Convert name from UNIX-ish to BS2000 ready for a DIRectory listing.
5954;; Remember that there are no directories in BS2000.
5955(defun ange-ftp-fix-dir-name-for-bs2000 (dir-name)
5956 (if (string-equal dir-name "/")
5957 "*" ;; Don't use an empty string here!
5958 (ange-ftp-fix-name-for-bs2000 dir-name)))
5959
5960(or (assq 'bs2000 ange-ftp-fix-dir-name-func-alist)
5961 (setq ange-ftp-fix-dir-name-func-alist
5962 (cons '(bs2000 . ange-ftp-fix-dir-name-for-bs2000)
5963 ange-ftp-fix-dir-name-func-alist)))
5964
5965(or (memq 'bs2000 ange-ftp-dumb-host-types)
5966 (setq ange-ftp-dumb-host-types
5967 (cons 'bs2000 ange-ftp-dumb-host-types)))
5968
5969(defvar ange-ftp-bs2000-host-regexp nil)
5970(defvar ange-ftp-bs2000-posix-host-regexp nil)
5971
5972;; Return non-nil if HOST is running BS2000.
5973(defun ange-ftp-bs2000-host (host)
5974 (and ange-ftp-bs2000-host-regexp
5975 (save-match-data
5976 (string-match ange-ftp-bs2000-host-regexp host))))
5977;; Return non-nil if HOST is running BS2000 with POSIX subsystem.
5978(defun ange-ftp-bs2000-posix-host (host)
5979 (and ange-ftp-bs2000-posix-host-regexp
5980 (save-match-data
5981 (string-match ange-ftp-bs2000-posix-host-regexp host))))
5982
5983(defun ange-ftp-add-bs2000-host (host)
5984 "Mark HOST as the name of a machine running BS2000."
5985 (interactive
5986 (list (read-string "Host: "
5987 (let ((name (or (buffer-file-name) default-directory)))
5988 (and name (car (ange-ftp-ftp-name name)))))))
5989 (if (not (ange-ftp-bs2000-host host))
5990 (setq ange-ftp-bs2000-host-regexp
5991 (concat "^" (regexp-quote host) "$"
5992 (and ange-ftp-bs2000-host-regexp "\\|")
5993 ange-ftp-bs2000-host-regexp)
5994 ange-ftp-host-cache nil)))
5995
5996(defun ange-ftp-add-bs2000-posix-host (host)
5997 "Mark HOST as the name of a machine running BS2000 with POSIX subsystem."
5998 (interactive
5999 (list (read-string "Host: "
6000 (let ((name (or (buffer-file-name) default-directory)))
6001 (and name (car (ange-ftp-ftp-name name)))))))
6002 (if (not (ange-ftp-bs2000-posix-host host))
6003 (setq ange-ftp-bs2000-posix-host-regexp
6004 (concat "^" (regexp-quote host) "$"
6005 (and ange-ftp-bs2000-posix-host-regexp "\\|")
6006 ange-ftp-bs2000-posix-host-regexp)
6007 ange-ftp-host-cache nil))
6008 ;; Install CD hook to cd to posix on connecting:
e3441f42 6009 (add-hook 'ange-ftp-process-startup-hook 'ange-ftp-bs2000-cd-to-posix)
9c35d706
RS
6010 host)
6011
6012(defconst ange-ftp-bs2000-filename-regexp
6013 (concat
6014 "\\(" ange-ftp-bs2000-filename-prefix-regexp "\\)?"
6015 "\\(" ange-ftp-bs2000-short-filename-regexp "\\)")
6016 "Regular expression to match for a valid BS2000 file name.")
6017
6018(defcustom ange-ftp-bs2000-additional-pubsets
6019 nil
6020 "*List of additional pubsets available to all users."
6021 :group 'ange-ftp
33ceea4f 6022 :type '(repeat string))
9c35d706
RS
6023
6024;; These parsing functions are as general as possible because the syntax
6025;; of ftp listings from BS2000 hosts is a bit erratic. What saves us is that
6026;; the BS2000 filename syntax is so rigid.
6027
6028;; Extract the next filename from a BS2000 dired-like listing.
6029(defun ange-ftp-parse-bs2000-filename ()
6030 (if (re-search-forward ange-ftp-bs2000-filename-regexp nil t)
e3441f42 6031 (match-string 2)))
9c35d706
RS
6032
6033;; Parse the current buffer which is assumed to be in (some) BS2000 FTP dir
6034;; format, and return a hashtable as the result.
6035(defun ange-ftp-parse-bs2000-listing ()
6e85ef5f 6036 (let ((tbl (make-hash-table :test 'equal))
9c35d706
RS
6037 pubset
6038 file)
6039 ;; get current pubset
6040 (goto-char (point-min))
6041 (if (re-search-forward ange-ftp-bs2000-filename-pubset-regexp nil t)
e3441f42 6042 (setq pubset (match-string 0)))
9c35d706
RS
6043 ;; add files to hashtable
6044 (goto-char (point-min))
6045 (save-match-data
6046 (while (setq file (ange-ftp-parse-bs2000-filename))
6e85ef5f 6047 (puthash file nil tbl)))
9c35d706 6048 ;; add . and ..
6e85ef5f
SM
6049 (puthash "." t tbl)
6050 (puthash ".." t tbl)
9c35d706
RS
6051 ;; add all additional pubsets, if not listing one of them
6052 (if (not (member pubset ange-ftp-bs2000-additional-pubsets))
6e85ef5f 6053 (mapcar (lambda (pubset) (puthash pubset t tbl))
9c35d706
RS
6054 ange-ftp-bs2000-additional-pubsets))
6055 tbl))
6056
e3441f42
SM
6057(add-to-list 'ange-ftp-parse-list-func-alist
6058 '(bs2000 . ange-ftp-parse-bs2000-listing))
9c35d706 6059
9c35d706
RS
6060(defun ange-ftp-bs2000-cd-to-posix ()
6061 "cd to POSIX subsystem if the current host matches
e3441f42
SM
6062`ange-ftp-bs2000-posix-host-regexp'. All BS2000 hosts with POSIX subsystem
6063MUST BE EXPLICITLY SET with `ange-ftp-add-bs2000-posix-host' for they cannot
9c35d706 6064be recognized automatically (they are all valid BS2000 hosts too)."
e3441f42 6065 (if (and ange-ftp-this-host (ange-ftp-bs2000-posix-host ange-ftp-this-host))
9c35d706
RS
6066 (progn
6067 ;; change to POSIX:
6068; (ange-ftp-raw-send-cmd proc "cd %POSIX")
e3441f42 6069 (ange-ftp-cd ange-ftp-this-host ange-ftp-this-user "%POSIX")
9c35d706 6070 ;; put new home directory in the expand-dir hashtable.
e3441f42
SM
6071 ;; `ange-ftp-this-host' and `ange-ftp-this-user' are bound in
6072 ;; ange-ftp-get-process.
6073 (puthash (concat ange-ftp-this-host "/" ange-ftp-this-user "/~")
6074 (car (ange-ftp-get-pwd ange-ftp-this-host ange-ftp-this-user))
6e85ef5f 6075 ange-ftp-expand-dir-hashtable))))
9c35d706
RS
6076
6077;; Not available yet:
6078;; ange-ftp-bs2000-delete-file-entry
6079;; ange-ftp-bs2000-add-file-entry
6080;; ange-ftp-bs2000-file-name-as-directory
6081;; ange-ftp-bs2000-make-compressed-filename
6082;; ange-ftp-bs2000-file-name-sans-versions
6083\f
8749abea
GM
6084;;;; ------------------------------------------------------------
6085;;;; Finally provide package.
6086;;;; ------------------------------------------------------------
6087
6088(provide 'ange-ftp)
6089
ab5796a9 6090;;; arch-tag: 2987ef88-cb56-4ec1-87a9-79132572e316
8749abea 6091;;; ange-ftp.el ends here