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