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