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