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