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