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