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