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