ange-ftp.el 203 KB
Newer Older
1
;;; ange-ftp.el --- transparent FTP support for GNU Emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Karl Heuer's avatar
Karl Heuer committed
3
;;; Copyright (C) 1989,90,91,92,93,94,95  Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;;;
Eric S. Raymond's avatar
Oops...  
Eric S. Raymond committed
5 6
;; Author: Andy Norman (ange@hplb.hpl.hp.com)
;; Keywords: comm
Richard M. Stallman's avatar
Richard M. Stallman committed
7 8 9
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
10
;;; the Free Software Foundation; either version 2, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13 14 15 16 17 18 19 20 21 22
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; A copy of the GNU General Public License can be obtained from this
;;; program's author (send electronic mail to ange@hplb.hpl.hp.com) or from
;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
;;; 02139, USA.

23
;;; Commentary:
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30 31 32
;;;
;;; This package attempts to make accessing files and directories using FTP
;;; from within GNU Emacs as simple and transparent as possible.  A subset of
;;; the common file-handling routines are extended to interact with FTP.

;;; Usage:
;;;
;;; Some of the common GNU Emacs file-handling operations have been made
;;; FTP-smart.  If one of these routines is given a filename that matches
33 34
;;; '/user@host:name' then it will spawn an FTP process connecting to machine
;;; 'host' as account 'user' and perform its operation on the file 'name'.
Richard M. Stallman's avatar
Richard M. Stallman committed
35 36 37 38 39
;;;
;;; For example: if find-file is given a filename of:
;;;
;;;   /ange@anorman:/tmp/notes
;;;
40
;;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as
Richard M. Stallman's avatar
Richard M. Stallman committed
41 42
;;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
;;; contents of that file as if it were on the local filesystem.  If ange-ftp
43
;;; needs a password to connect then it reads one in the echo area.
Richard M. Stallman's avatar
Richard M. Stallman committed
44 45 46

;;; Extended filename syntax:
;;;
47
;;; The default extended filename syntax is '/user@host:name', where the
Richard M. Stallman's avatar
Richard M. Stallman committed
48
;;; 'user@' part may be omitted.  This syntax can be customised to a certain
49
;;; extent by changing ange-ftp-name-format.  There are limitations.
Richard M. Stallman's avatar
Richard M. Stallman committed
50
;;;
51
;;; If the user part is omitted then ange-ftp generates a default user
Richard M. Stallman's avatar
Richard M. Stallman committed
52 53 54 55
;;; instead whose value depends on the variable ange-ftp-default-user.

;;; Passwords:
;;;
56 57 58
;;; A password is required for each host/user pair.  Ange-ftp reads passwords
;;; as needed.  You can also specify a password with ange-ftp-set-passwd, or
;;; in a *valid* ~/.netrc file.
Richard M. Stallman's avatar
Richard M. Stallman committed
59 60 61

;;; Passwords for user "anonymous":
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
62 63 64 65
;;; Passwords for the user "anonymous" (or "ftp") are handled
;;; specially.  The variable `ange-ftp-generate-anonymous-password'
;;; controls what happens: if the value of this variable is a string,
;;; then this is used as the password; if non-nil (the default), then
66
;;; the value of `user-mail-address' is used; if nil then the user
Richard M. Stallman's avatar
Richard M. Stallman committed
67
;;; is prompted for a password as normal.
Richard M. Stallman's avatar
Richard M. Stallman committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

;;; "Dumb" UNIX hosts:
;;;
;;; The FTP servers on some UNIX machines have problems if the 'ls' command is
;;; used.
;;;
;;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
;;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
;;; that this change will take effect for the current GNU Emacs session only.
;;; See below for a discussion of non-UNIX hosts.  If a large number of
;;; machines with similar hostnames have this problem then it is easier to set
;;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp
;;; is unable to automatically recognize dumb unix hosts.

;;; File name completion:
;;;
;;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
;;; To do filename completion, ange-ftp needs a listing from the remote host.
;;; Therefore, for very slow connections, it might not save any time.

;;; FTP processes:
;;;
;;; When ange-ftp starts up an FTP process, it leaves it running for speed
;;; purposes.  Some FTP servers will close the connection after a period of
;;; time, but ange-ftp should be able to quietly reconnect the next time that
;;; the process is needed.
;;;
95 96
;;; Killing the "*ftp user@host*" buffer also kills the ftp process.
;;; This should not cause ange-ftp any grief.
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99

;;; Binary file transfers:
;;;
100 101 102
;;; By default ange-ftp transfers files in ASCII mode.  If a file being
;;; transferred matches the value of ange-ftp-binary-file-name-regexp then
;;; binary mode is used for that transfer.
Richard M. Stallman's avatar
Richard M. Stallman committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

;;; Account passwords:
;;;
;;; Some FTP servers require an additional password which is sent by the
;;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
;;; specify an account password by either calling ange-ftp-set-account, or by
;;; specifying an account token in the .netrc file.  If the account password
;;; is set by either of these methods then ange-ftp will issue an ACCOUNT
;;; command upon starting the FTP process.

;;; Preloading:
;;;
;;; ange-ftp can be preloaded, but must be put in the site-init.el file and
;;; not the site-load.el file in order for the documentation strings for the
;;; functions being overloaded to be available.

;;; Status reports:
;;;
;;; Most ange-ftp commands that talk to the FTP process output a status
;;; message on what they are doing.  In addition, ange-ftp can take advantage
;;; of the FTP client's HASH command to display the status of transferring
;;; files and listing directories.  See the documentation for the variables
;;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
;;; ange-ftp-process-verbose for more details.

;;; Gateways:
;;;
130
;;; Sometimes it is necessary for the FTP process to be run on a different
Richard M. Stallman's avatar
Richard M. Stallman committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
;;; machine than the machine running GNU Emacs.  This can happen when the
;;; local machine has restrictions on what hosts it can access.
;;;
;;; ange-ftp has support for running the ftp process on a different (gateway)
;;; machine.  The way it works is as follows:
;;;
;;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
;;;    that doesn't have the access restrictions.
;;;
;;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
;;;    that matches hosts that can be contacted from running a local ftp
;;;    process, but fails to match hosts that can't be accessed locally.  For
;;;    example:
;;;
;;;    "\\.hp\\.com$\\|^[^.]*$"
;;;
;;;    will match all hosts that are in the .hp.com domain, or don't have an
;;;    explicit domain in their name, but will fail to match hosts with
;;;    explicit domains or that are specified by their ip address.
;;;
;;; 3) Using NFS and symlinks, make sure that there is a shared directory with
;;;    the *same* name between the local machine and the gateway machine.
153
;;;    This directory is necessary for temporary files created by ange-ftp.
Richard M. Stallman's avatar
Richard M. Stallman committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
;;;
;;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
;;;    this directory plus an identifying filename prefix.  For example:
;;;
;;;    "/nfs/hplose/ange/ange-ftp"
;;;
;;;    where /nfs/hplose/ange is a directory that is shared between the
;;;    gateway machine and the local machine.
;;;
;;; The simplest way of getting a ftp process running on the gateway machine
;;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
;;; can't do this for some reason such as security then points 7 onwards will
;;; discuss an alternative approach.
;;;
;;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
;;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
;;;
;;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
;;;    isn't already.  This tells ange-ftp that you are using a remote shell
;;;    rather than logging in using telnet or rlogin.
;;;
;;; That should be all you need to allow ange-ftp to spawn a ftp process on
;;; the gateway machine.  If you have to use telnet or rlogin to get to the
;;; gateway machine then follow the instructions below.
;;;
;;; 7) Set the variable ange-ftp-gateway-program to the name of the program
;;;    that lets you log onto the gateway machine.  This may be something like
;;;    telnet or rlogin.
;;;
;;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
;;;    expression that matches the prompt you get when you login to the
;;;    gateway machine.  Be very specific here; this regexp must not match
;;;    *anything* in your login banner except this prompt.
;;;    shell-prompt-pattern is far too general as it appears to match some
;;;    login banners from Sun machines.  For example:
;;;
;;;    "^$*$ *"
;;;
;;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
;;;    ange-ftp know that it has to "hand-hold" the login to the gateway
;;;    machine.
;;;
;;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
;;;     that will put the pty connected to the gateway machine into a
;;;     no-echoing mode, and will strip off carriage-returns from output from
;;;     the gateway machine.  For example:
;;;
;;;     "stty -onlcr -echo"
;;;
;;;     will work on HP-UX machines, whereas:
;;;
;;;     "stty -echo nl"
;;;
;;;     appears to work for some Sun machines.
;;;
;;; That's all there is to it.

;;; Smart gateways:
;;;
;;; If you have a "smart" ftp program that allows you to issue commands like
;;; "USER foo@bar" which do nice proxy things, then look at the variables
;;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
216 217 218 219 220 221 222 223
;;;
;;; Otherwise, if there is an alternate ftp program that implements proxy in
;;; a transparent way (i.e. w/o specifying the proxy host), that will
;;; connect you directly to the desired destination host:
;;; Set ange-ftp-gateway-ftp-program-name to that program's name.
;;; Set ange-ftp-local-host-regexp to a value as stated earlier on.
;;; Leave ange-ftp-gateway-host set to nil.
;;; Set ange-ftp-smart-gateway to t.
Richard M. Stallman's avatar
Richard M. Stallman committed
224 225 226 227 228 229 230 231 232

;;; Tips for using ange-ftp:
;;;
;;; 1. For dired to work on a host which marks symlinks with a trailing @ in
;;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
;;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
;;;    there is a chance you might connect to an ULTRIX machine (such as
;;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
;;;    the side effect that dired will have problems with symlinks whose names
233
;;;    end in an @.  If you get yourself into this situation then editing
Richard M. Stallman's avatar
Richard M. Stallman committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
;;;    dired's ls-switches to remove "F", will temporarily fix things.
;;;
;;; 2. If you know that you are connecting to a certain non-UNIX machine
;;;    frequently, and ange-ftp seems to be unable to guess its host-type,
;;;    then setting the appropriate host-type regexp
;;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
;;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
;;;    ange-ftp's inability to recognize the host-type as a bug.
;;;
;;; 3. For slow connections, you might get "listing unreadable" error
;;;    messages, or get an empty buffer for a file that you know has something
;;;    in it. The solution is to increase the value of ange-ftp-retry-time.
;;;    Its default value is 5 which is plenty for reasonable connections.
;;;    However, for some transatlantic connections I set this to 20.
;;;
;;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
;;;    copying the file to the local machine, compressing it there, and then
;;;    sending it back. Binary file transfers between machines of different
;;;    architectures can be a risky business. Test things out first on some
;;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
;;;    moving them through the local machine. Again, be careful when doing
;;;    this with binary files on non-Unix machines.
;;;
;;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
;;;    (list of dired commands for which confirmation is not asked).  You
;;;    might want to reconsider your setting of this variable, because you
;;;    might want confirmation for more commands on remote direds than on
;;;    local direds. For example, I strongly recommend that you not include
;;;    compress and uncompress in this list. If there is enough demand it
;;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
;;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
;;;    is a list of commands for which confirmation would be suppressed.  Then
;;;    remote dired listings would take their (buffer-local) value of
;;;    dired-no-confirm from this alist. Who votes for this?

;;; ---------------------------------------------------------------------
;;; Non-UNIX support:
;;; ---------------------------------------------------------------------

;;; VMS support:
;;;
275
;;; Ange-ftp has full support for VMS hosts.  It
Richard M. Stallman's avatar
Richard M. Stallman committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
;;; should be able to automatically recognize any VMS machine. However, if it
;;; fails to do this, you can use the command ange-ftp-add-vms-host.  As well,
;;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We
;;; would be grateful if you would report any failures to automatically
;;; recognize a VMS host as a bug.
;;;
;;; Filename Syntax:
;;;
;;; For ease of *implementation*, the user enters the VMS filename syntax in a
;;; UNIX-y way.  For example:
;;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
;;; would be entered as:
;;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
;;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
;;;  [.CSV.POLICY]RULES.MEM
;;; you would type:
;;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
;;;
;;; A legal VMS filename is of the form: FILE.TYPE;##
;;; where FILE can be up to 39 characters
;;;       TYPE can be up to 39 characters
;;;       ## is a version number (an integer between 1 and 32,767)
;;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
;;; $ cannot begin a filename, and - cannot be used as the first or last
;;; character.
;;;
;;; Tips:
;;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
;;;    Therefore, to access a VMS file, you must enter the filename with upper
;;;    case letters.
;;; 2. To access the latest version of file under VMS, you use the filename
;;;    without the ";" and version number. You should always edit the latest
;;;    version of a file. If you want to edit an earlier version, copy it to a
;;;    new file first. This has nothing to do with ange-ftp, but is simply
;;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
;;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
;;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
;;;    that VMS will not allow you to save the file because it will refuse to
;;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
;;;    attach the buffer to this file. To get out of this situation, M-x
;;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
317
;;;    latest version of the file. For this reason, in dired "f"
Richard M. Stallman's avatar
Richard M. Stallman committed
318 319 320 321 322 323
;;;    (dired-find-file), always loads the file sans version, whereas "v",
;;;    (dired-view-file), always loads the explicit version number. The
;;;    reasoning being that it reasonable to view old versions of a file, but
;;;    not to edit them.
;;; 3. EMACS has a feature in which it does environment variable substitution
;;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
324
;;;    by typing $$.
Richard M. Stallman's avatar
Richard M. Stallman committed
325 326 327

;;; MTS support:
;;;
328
;;; Ange-ftp has full support for hosts running
Richard M. Stallman's avatar
Richard M. Stallman committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
;;; the Michigan terminal system.  It should be able to automatically
;;; recognize any MTS machine. However, if it fails to do this, you can use
;;; the command ange-ftp-add-mts-host.  As well, you can set the variable
;;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you
;;; would report any failures to automatically recognize a MTS host as a bug.
;;;
;;; Filename syntax:
;;; 
;;; MTS filenames are entered in a UNIX-y way. For example, if your account
;;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
;;; entered as
;;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
;;; In other words, MTS accounts are treated as UNIX directories. Of course,
;;; to access a file in another account, you must have access permission for
;;; it.  If FILE were in your own account, then you could enter it in a
344
;;; relative name fashion as
Richard M. Stallman's avatar
Richard M. Stallman committed
345 346 347 348 349 350 351 352 353
;;;   /YYYY@mtsg.ubc.ca:FILE
;;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
;;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
;;; like.) MTS filenames are always in upper case, and hence be sure to enter
;;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
;;; is.

;;; CMS support:
;;; 
354
;;; Ange-ftp has full support for hosts running
Richard M. Stallman's avatar
Richard M. Stallman committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
;;; CMS.  It should be able to automatically recognize any CMS machine.
;;; However, if it fails to do this, you can use the command
;;; ange-ftp-add-cms-host.  As well, you can set the variable
;;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you
;;; would report any failures to automatically recognize a CMS host as a bug.
;;; 
;;; Filename syntax:
;;;
;;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
;;; treated as UNIX directories. For example to access the file READ.ME in
;;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
;;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
;;; If *.301 is the default minidisk for this account, you could access
;;; FOO.BAR on this minidisk as
;;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
;;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
;;; up to 8 characters. Again, beware that CMS filenames are always upper
;;; case, and hence must be entered as such.
;;;
;;; Tips:
;;; 1. CMS machines, with the exception of anonymous accounts, nearly always
;;;    need an account password. To have ange-ftp send an account password,
;;;    you can either include it in your .netrc file, or use
;;;    ange-ftp-set-account.
;;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
;;;    can fix this.
;;;
;;; ------------------------------------------------------------------
;;; Bugs:
;;; ------------------------------------------------------------------
;;; 
;;; 1. Umask problems:
;;;    Be warned that files created by using ange-ftp will take account of the
;;;    umask of the ftp daemon process rather than the umask of the creating
389
;;;    user.  This is particularly important when logging in as the root user.
Richard M. Stallman's avatar
Richard M. Stallman committed
390 391 392 393 394 395 396
;;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
;;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
;;;    suspect that there is something similar on other systems.
;;;
;;; 2. Some combinations of FTP clients and servers break and get out of sync
;;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
;;;    machines cause this problem for some FTP clients. Using
397
;;;    ange-ftp-kill-ftp-process can restart the ftp process, which
Karl Heuer's avatar
Karl Heuer committed
398
;;;    should get things back in sync.
Richard M. Stallman's avatar
Richard M. Stallman committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
;;;
;;; 3. Ange-ftp does not check to make sure that when creating a new file,
;;;    you provide a valid filename for the remote operating system.
;;;    If you do not, then the remote FTP server will most likely
;;;    translate your filename in some way. This may cause ange-ftp to
;;;    get confused about what exactly is the name of the file. The
;;;    most common causes of this are using lower case filenames on systems
;;;    which support only upper case, and using filenames which are too
;;;    long.
;;;
;;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
;;;
;;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
;;;    for some reason creates a FTP process that only talks via pipes then
;;;    ange-ftp won't be getting the information it requires at the time that
;;;    it wants it since pipes flush at different times to pty's.  One
;;;    disgusting way around this problem is to talk to the FTP process via
;;;    rlogin which does the 'right' things with pty's.
;;;
;;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
;;;    worried about this too much. Eventually, we should have some caching
;;;    of the current minidisk.
;;;    
;;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
;;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
;;;    to it. This is (understandably) beyond ange-ftp.
;;;
;;; 8. Remote to remote copying of files on non-Unix machines can be risky.
;;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
;;;    will use binary mode for the copy. Between systems of different
;;;    architecture, this still may not be enough to guarantee the integrity
;;;    of binary files. Binary file transfers from VMS machines are
;;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
;;;    an alist of OS type, regexp pairs?
;;;
;;; 9. The code to do compression of files over ftp is not as careful as it
;;;    should be. It deletes the old remote version of the file, before
;;;    actually checking if the local to remote transfer of the compressed
;;;    file succeeds. Of course to delete the original version of the file
;;;    after transferring the compressed version back is also dangerous,
;;;    because some OS's have severe restrictions on the length of filenames,
;;;    and when the compressed version is copied back the "-Z" or ".Z" may be
;;;    truncated. Then, ange-ftp would delete the only remaining version of
;;;    the file.  Maybe ange-ftp should make backups when it compresses files
;;;    (of course, the backup "~" could also be truncated off, sigh...).
;;;    Suggestions?
;;;

;;; 10. If a dir listing is attempted for an empty directory on (at least
;;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
;;;     I don't know how to get ange-ftp work to around it.
;;;
;;; 11. Bombs on filenames that start with a space. Deals well with filenames
;;;     containing spaces, but beware that the remote ftpd may not like them
;;;     much.
;;;
455 456 457
;;; 12. The dired support for non-Unix-like systems does not currently work.
;;;     It needs to be reimplemented by modifying the parse-...-listing
;;;	functions to convert the directory listing to ls -l format.
Richard M. Stallman's avatar
Richard M. Stallman committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
;;; 
;;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
;;;     with a trailing @ in a ls -alF listing. In order to account for this
;;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
;;;     parsing a listing with the F switch. This will cause ange-ftp to
;;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
;;;     ends in an @. ange-ftp will correct itself if you take F out of the
;;;     dired ls switches (C-u s will allow you to edit the switches). The
;;;     dired buffer will be automatically reverted, which will allow ange-ftp
;;;     to fix its files hashtable.  A cookie to anyone who can think of a
;;;     fast, sure-fire way to recognize ULTRIX over ftp.

;;; If you find any bugs or problems with this package, PLEASE either e-mail
;;; the above author, or send a message to the ange-ftp-lovers mailing list
;;; below.  Ideas and constructive comments are especially welcome.

;;; ange-ftp-lovers:
;;;
;;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
;;; users of ange-ftp are welcome to subscribe (see below) and to discuss
;;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
;;; the mailing list.
;;;
;;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
;;; list, please mail one of the following addresses:
;;;
;;;     ange-ftp-lovers-request@anorman.hpl.hp.com
;;; or
;;;     ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com
;;;
;;; Please don't forget the -request part.
;;;
;;; For mail to be posted directly to ange-ftp-lovers, send to one of the
;;; following addresses:
;;; 
;;;     ange-ftp-lovers@anorman.hpl.hp.com
;;; or
;;;     ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com
;;;
;;; Alternatively, there is a mailing list that only gets announcements of new
;;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
;;; subscribed to by e-mailing to the -request address as above.  Please make
;;; it clear in the request which mailing list you wish to join.

;;; The latest version of ange-ftp can usually be obtained via anonymous ftp
;;; from:
;;;     alpha.gnu.ai.mit.edu:ange-ftp/ange-ftp.tar.Z
;;; or:
;;;     ugle.unit.no:/pub/gnu/emacs-lisp/ange-ftp.tar.Z
;;; or:
;;;   archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages/ange-ftp.tar.Z

;;; The archives for ange-ftp-lovers can be found via anonymous ftp under:
;;;
;;;     ftp.reed.edu:pub/mailing-lists/ange-ftp/

;;; -----------------------------------------------------------
;;; Technical information on this package:
;;; -----------------------------------------------------------

518 519 520
;;; ange-ftp works by putting a handler on file-name-handler-alist
;;; which is called by many primitives, and a few non-primitives,
;;; whenever they see a file name of the appropriate sort.
Richard M. Stallman's avatar
Richard M. Stallman committed
521 522 523 524 525 526

;;; Checklist for adding non-UNIX support for TYPE
;;; 
;;; The following functions may need TYPE versions:
;;; (not all functions will be needed for every OS)
;;;
527 528
;;; ange-ftp-fix-name-for-TYPE
;;; ange-ftp-fix-dir-name-for-TYPE
Richard M. Stallman's avatar
Richard M. Stallman committed
529 530 531 532 533 534
;;; ange-ftp-TYPE-host
;;; ange-ftp-TYPE-add-host
;;; ange-ftp-parse-TYPE-listing
;;; ange-ftp-TYPE-delete-file-entry
;;; ange-ftp-TYPE-add-file-entry
;;; ange-ftp-TYPE-file-name-as-directory
535 536
;;; ange-ftp-TYPE-make-compressed-filename
;;; ange-ftp-TYPE-file-name-sans-versions
Richard M. Stallman's avatar
Richard M. Stallman committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
;;;
;;; Variables:
;;;
;;; ange-ftp-TYPE-host-regexp
;;; May need to add TYPE to ange-ftp-dumb-host-types
;;;
;;; Check the following functions for OS dependent coding:
;;;
;;; ange-ftp-host-type
;;; ange-ftp-guess-host-type
;;; ange-ftp-allow-child-lookup

;;; Host type conventions:
;;;
;;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
;;; (mostly) follow the following conventions for remote host types.  At
;;; least, I think that future code should try to follow these conventions,
;;; and the current code should eventually be made compliant.
;;;
;;; nil = local host type, whatever that is (probably unix).
;;;       Think nil as in "not a remote host". This value is used by
;;;       ange-ftp-dired-host-type for local buffers.
;;;
;;; t = a remote host of unknown type. Think t is in true, it's remote.
;;;     Currently, 'unix is used as the default remote host type.
;;;     Maybe we should use t.
;;;
;;; 'type = a remote host of TYPE type.
;;;
;;; 'type:list = a remote host of TYPE type, using a specialized ftp listing
;;;              program called list. This is currently only used for Unix
;;;              dl (descriptive listings), when ange-ftp-dired-host-type
;;;              is set to 'unix:dl.

;;; Bug report codes:
;;;
;;; Because of their naive faith in this code, there are certain situations
;;; which the writers of this program believe could never happen. However,
575
;;; being realists they have put calls to `error' in the program at these
Richard M. Stallman's avatar
Richard M. Stallman committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
;;; points. These errors provide a code, which is an integer, greater than 1.
;;; To aid debugging.  the error codes, and the functions in which they reside
;;; are listed below.
;;; 
;;; 1: See ange-ftp-ls
;;;

;;; -----------------------------------------------------------
;;; Hall of fame:
;;; -----------------------------------------------------------
;;; 
;;; Thanks to Roland McGrath for improving the filename syntax handling,
;;; for suggesting many enhancements and for numerous cleanups to the code.
;;;
;;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
;;;
;;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
;;; dired / shell auto-loading.
;;;
595
;;; Thanks to Sebastian Kremer for dired support and for many ideas and
Richard M. Stallman's avatar
Richard M. Stallman committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
;;; bugfixes.
;;;
;;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
;;; VOS support, and hostname completion.
;;;
;;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
;;; with file-name expansion, efficiency worries, stylistic concerns and many
;;; bugfixes.
;;;
;;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
;;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
;;; auto-recognition of the host type.
;;;
;;; Thanks to Dave Smith who wrote the info file for ange-ftp.
;;;
;;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
;;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
;;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
;;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
;;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
;;; whose names I've forgotten who have helped to debug and fix problems with
;;; ange-ftp.el.

619 620

;;; Code:
621 622
(require 'comint)

Richard M. Stallman's avatar
Richard M. Stallman committed
623 624 625 626
;;;; ------------------------------------------------------------
;;;; User customization variables.
;;;; ------------------------------------------------------------

627
(defvar ange-ftp-name-format
628
  '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4))
629 630 631 632
  "*Format of a fully expanded remote file name.
This is a list of the form \(REGEXP HOST USER NAME\),
where REGEXP is a regular expression matching
the full remote name, and HOST, USER, and NAME are the numbers of
Richard M. Stallman's avatar
Richard M. Stallman committed
633 634 635 636 637 638 639
parenthesized expressions in REGEXP for the components (in that order).")

;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.

(defvar ange-ftp-multi-msgs
640
  "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-"
Richard M. Stallman's avatar
Richard M. Stallman committed
641
  "*Regular expression matching the start of a multiline ftp reply.")
Richard M. Stallman's avatar
Richard M. Stallman committed
642 643 644

(defvar ange-ftp-good-msgs
  "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark"
Richard M. Stallman's avatar
Richard M. Stallman committed
645
  "*Regular expression matching ftp \"success\" messages.")
Richard M. Stallman's avatar
Richard M. Stallman committed
646 647 648 649 650 651 652 653 654 655

;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
;; Also CMS machines use a multiline 550- reply to say that you
;; don't have write permission. ange-ftp gets into multi-line skip
;; mode and hangs. Have it ignore 550- instead. It will then barf
;; when it gets the 550 line, as it should.

(defvar ange-ftp-skip-msgs
  (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
	  "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
656
	  "^Data connection \\|"
Richard M. Stallman's avatar
Richard M. Stallman committed
657
	  "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye")
Richard M. Stallman's avatar
Richard M. Stallman committed
658
  "*Regular expression matching ftp messages that can be ignored.")
Richard M. Stallman's avatar
Richard M. Stallman committed
659 660 661 662

(defvar ange-ftp-fatal-msgs
  (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
	  "^No control connection\\|unknown host\\|^lost connection")
Richard M. Stallman's avatar
Richard M. Stallman committed
663 664
  "*Regular expression matching ftp messages that indicate serious errors.
These mean that the FTP process should (or already has) been killed.")
Richard M. Stallman's avatar
Richard M. Stallman committed
665 666 667

(defvar ange-ftp-gateway-fatal-msgs
  "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
Richard M. Stallman's avatar
Richard M. Stallman committed
668
  "*Regular expression matching login failure messages from rlogin/telnet.")
Richard M. Stallman's avatar
Richard M. Stallman committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

(defvar ange-ftp-xfer-size-msgs
  "^150 .* connection for .* (\\([0-9]+\\) bytes)"
  "*Regular expression used to determine the number of bytes in a FTP transfer.")

(defvar ange-ftp-tmp-name-template "/tmp/ange-ftp"
  "*Template used to create temporary files.")

(defvar ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
  "*Template used to create temporary files when ftp-ing through a gateway.
Files starting with this prefix need to be accessible from BOTH the local
machine and the gateway machine, and need to have the SAME name on both
machines, that is, /tmp is probably NOT what you want, since that is rarely
cross-mounted.")

(defvar ange-ftp-netrc-filename "~/.netrc"
  "*File in .netrc format to search for passwords.")

(defvar ange-ftp-disable-netrc-security-check nil
  "*If non-nil avoid checking permissions on the .netrc file.")

(defvar ange-ftp-default-user nil
691
  "*User name to use when none is specied in a file name.
Richard M. Stallman's avatar
Richard M. Stallman committed
692 693 694 695 696 697 698 699 700
If nil, then the name under which the user is logged in is used.
If non-nil but not a string, the user is prompted for the name.")

(defvar ange-ftp-default-password nil
  "*Password to use when the user is the same as ange-ftp-default-user.")

(defvar ange-ftp-default-account nil
  "*Account password to use when the user is the same as ange-ftp-default-user.")

Richard M. Stallman's avatar
Richard M. Stallman committed
701
(defvar ange-ftp-generate-anonymous-password t
702 703 704
  "*If t, use value of `user-mail-address' as password for anonymous ftp.
If a string, then use that string as the password.
If nil, prompt the user for a password.")
Richard M. Stallman's avatar
Richard M. Stallman committed
705 706

(defvar ange-ftp-dumb-unix-host-regexp nil
Richard M. Stallman's avatar
Richard M. Stallman committed
707
  "*If non-nil, regexp matching hosts on which `dir' command lists directory.")
Richard M. Stallman's avatar
Richard M. Stallman committed
708 709

(defvar ange-ftp-binary-file-name-regexp
710
  (concat "\\.[zZ]$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
Richard M. Stallman's avatar
Richard M. Stallman committed
711
	  "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
712 713
	  "\\.EXE\\(;[0-9]+\\)?$\\|\\.[zZ]-part-..$\\|\\.gz$\\|"
	  "\\.taz$\\|\\.tgz$")
Richard M. Stallman's avatar
Richard M. Stallman committed
714 715 716 717 718 719
  "*If a file matches this regexp then it is transferred in binary mode.")

(defvar ange-ftp-gateway-host nil
  "*Name of host to use as gateway machine when local FTP isn't possible.")

(defvar ange-ftp-local-host-regexp ".*"
Richard M. Stallman's avatar
Richard M. Stallman committed
720 721
  "*Regexp selecting hosts which can be reached directly with ftp.
For other hosts the FTP process is started on \`ange-ftp-gateway-host\'
722
instead, and/or reached via \`ange-ftp-gateway-ftp-program-name\'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
723 724

(defvar ange-ftp-gateway-program-interactive nil
Richard M. Stallman's avatar
Richard M. Stallman committed
725 726
  "*If non-nil then the gateway program should  give a shell prompt.
Both telnet and rlogin do something like this.")
Richard M. Stallman's avatar
Richard M. Stallman committed
727

728
(defvar ange-ftp-gateway-program remote-shell-program
Richard M. Stallman's avatar
Richard M. Stallman committed
729
  "*Name of program to spawn a shell on the gateway machine.
730
Valid candidates are rsh (remsh on some systems), telnet and rlogin.  See
Richard M. Stallman's avatar
Richard M. Stallman committed
731
also the gateway variable above.")
Richard M. Stallman's avatar
Richard M. Stallman committed
732

733
(defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *"
Richard M. Stallman's avatar
Richard M. Stallman committed
734 735
  "*Regexp matching prompt after complete login sequence on gateway machine.
A match for this means the shell is now awaiting input.  Make this regexp as
Richard M. Stallman's avatar
Richard M. Stallman committed
736 737 738 739 740 741 742 743
strict as possible; it shouldn't match *anything* at all except the user's
initial prompt.  The above string will fail under most SUN-3's since it
matches the login banner.")

(defvar ange-ftp-gateway-setup-term-command
  (if (eq system-type 'hpux)
      "stty -onlcr -echo\n"
    "stty -echo nl\n")
Richard M. Stallman's avatar
Richard M. Stallman committed
744 745 746
  "*Set up terminal after logging in to the gateway machine.
This command should stop the terminal from echoing each command, and
arrange to strip out trailing ^M characters.")
Richard M. Stallman's avatar
Richard M. Stallman committed
747 748

(defvar ange-ftp-smart-gateway nil
749 750 751
  "*Non-nil means the ftp gateway and/or the gateway ftp program is smart.
Don't bother telnetting, etc., already connected to desired host transparently,
or just issue a user@host command in case \`ange-ftp-gateway-host\' is non-nil.")
Richard M. Stallman's avatar
Richard M. Stallman committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

(defvar ange-ftp-smart-gateway-port "21"
  "*Port on gateway machine to use when smart gateway is in operation.")

(defvar ange-ftp-send-hash t
  "*If non-nil, send the HASH command to the FTP client.")

(defvar ange-ftp-binary-hash-mark-size nil
  "*Default size, in bytes, between hash-marks when transferring a binary file.
If NIL, this variable will be locally overridden if the FTP client outputs a
suitable response to the HASH command.  If non-NIL then this value takes
precedence over the local value.")

(defvar ange-ftp-ascii-hash-mark-size 1024
  "*Default size, in bytes, between hash-marks when transferring an ASCII file.
This variable is buffer-local and will be locally overridden if the FTP client
outputs a suitable response to the HASH command.")

(defvar ange-ftp-process-verbose t
  "*If non-NIL then be chatty about interaction with the FTP process.")

(defvar ange-ftp-ftp-program-name "ftp"
  "*Name of FTP program to run.")

(defvar ange-ftp-gateway-ftp-program-name "ftp"
777
  "*Name of FTP program to run when accessing non-local hosts.
Richard M. Stallman's avatar
Richard M. Stallman committed
778 779 780 781 782 783 784 785 786
Some AT&T folks claim to use something called `pftp' here.")

(defvar ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
  "*A list of arguments passed to the FTP program when started.")

(defvar ange-ftp-nslookup-program nil
  "*If non-NIL then a string naming nslookup program." )

(defvar ange-ftp-make-backup-files ()
787
  "*Non-nil means make backup files for \"magic\" remote files.")
Richard M. Stallman's avatar
Richard M. Stallman committed
788 789

(defvar ange-ftp-retry-time 5
Richard M. Stallman's avatar
Richard M. Stallman committed
790 791
  "*Number of seconds to wait before retry if file or listing doesn't arrive.
This might need to be increased for very slow connections.")
Richard M. Stallman's avatar
Richard M. Stallman committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

(defvar ange-ftp-auto-save 0
  "If 1, allows ange-ftp files to be auto-saved.
If 0, suppresses auto-saving of ange-ftp files.
Don't use any other value.")

;;;; ------------------------------------------------------------
;;;; Hash table support.
;;;; ------------------------------------------------------------

(require 'backquote)

(defun ange-ftp-make-hashtable (&optional size)
  "Make an obarray suitable for use as a hashtable.
SIZE, if supplied, should be a prime number."
  (make-vector (or size 31) 0))

(defun ange-ftp-map-hashtable (fun tbl)
  "Call FUNCTION on each key and value in HASHTABLE."
  (mapatoms
   (function 
    (lambda (sym)
      (funcall fun (get sym 'key) (get sym 'val))))
   tbl))

(defmacro ange-ftp-make-hash-key (key)
  "Convert KEY into a suitable key for a hashtable."
  (` (if (stringp (, key))
	 (, key)
       (prin1-to-string (, key)))))

(defun ange-ftp-get-hash-entry (key tbl)
  "Return the value associated with KEY in HASHTABLE."
  (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl)))
    (and sym (get sym 'val))))

(defun ange-ftp-put-hash-entry (key val tbl)
  "Record an association between KEY and VALUE in HASHTABLE."
  (let ((sym (intern (ange-ftp-make-hash-key key) tbl)))
    (put sym 'val val)
    (put sym 'key key)))

(defun ange-ftp-del-hash-entry (key tbl)
  "Copy all symbols except KEY in HASHTABLE and return modified hashtable."
  (let* ((len (length tbl))
	 (new-tbl (ange-ftp-make-hashtable len))
	 (i (1- len)))
    (ange-ftp-map-hashtable
     (function
      (lambda (k v)
	(or (equal k key)
	    (ange-ftp-put-hash-entry k v new-tbl))))
     tbl)
    (while (>= i 0)
      (aset tbl i (aref new-tbl i))
      (setq i (1- i)))
    tbl))

(defun ange-ftp-hash-entry-exists-p (key tbl)
  "Return whether there is an association for KEY in TABLE."
  (intern-soft (ange-ftp-make-hash-key key) tbl))

(defun ange-ftp-hash-table-keys (tbl)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
855
  "Return a sorted list of all the active keys in TABLE, as strings."
Richard M. Stallman's avatar
Richard M. Stallman committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  (sort (all-completions "" tbl)
	(function string-lessp)))

;;;; ------------------------------------------------------------
;;;; Internal variables.
;;;; ------------------------------------------------------------

(defvar ange-ftp-data-buffer-name " *ftp data*"
  "Buffer name to hold directory listing data received from ftp process.")

(defvar ange-ftp-netrc-modtime nil
  "Last modified time of the netrc file from file-attributes.")

(defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable)
  "Hash table holding associations between HOST, USER pairs.")

(defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable)
  "Mapping between a HOST, USER pair and a PASSWORD for them.")

(defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable)
  "Mapping between a HOST, USER pair and a ACCOUNT password for them.")

(defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97)
  "Hash table for storing directories and their respective files.")

(defvar ange-ftp-ls-cache-lsargs nil
  "Last set of args used by ange-ftp-ls.")

(defvar ange-ftp-ls-cache-file nil
  "Last file passed to ange-ftp-ls.")

(defvar ange-ftp-ls-cache-res nil
  "Last result returned from ange-ftp-ls.")

(defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable))

(defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")

;; These are local variables in each FTP process buffer.
(defvar ange-ftp-hash-mark-unit nil)
(defvar ange-ftp-hash-mark-count nil)
(defvar ange-ftp-xfer-size nil)
(defvar ange-ftp-process-string nil)
(defvar ange-ftp-process-result-line nil)
(defvar ange-ftp-process-busy nil)
(defvar ange-ftp-process-result nil)
(defvar ange-ftp-process-multi-skip nil)
(defvar ange-ftp-process-msg nil)
(defvar ange-ftp-process-continue nil)
(defvar ange-ftp-last-percent nil)

;; These variables are bound by one function and examined by another.
;; Leave them void globally for error checking.
(defvar ange-ftp-this-file)
(defvar ange-ftp-this-dir)
(defvar ange-ftp-this-user)
(defvar ange-ftp-this-host)
913
(defvar ange-ftp-this-msg)
Richard M. Stallman's avatar
Richard M. Stallman committed
914 915 916 917 918 919 920 921 922 923 924 925
(defvar ange-ftp-completion-ignored-pattern)
(defvar ange-ftp-trample-marker)

;; New error symbols.
(put 'ftp-error 'error-conditions '(ftp-error file-error error))
;; (put 'ftp-error 'error-message "FTP error")

;;; ------------------------------------------------------------
;;; Enhanced message support.
;;; ------------------------------------------------------------

(defun ange-ftp-message (fmt &rest args)
926 927
  "Display message in echo area, but indicate if truncated.
Args are as in `message': a format string, plus arguments to be formatted."
Richard M. Stallman's avatar
Richard M. Stallman committed
928 929
  (let ((msg (apply (function format) fmt args))
	(max (window-width (minibuffer-window))))
930 931 932 933 934 935
    (if noninteractive
	msg
      (if (>= (length msg) max)
	  ;; Take just the last MAX - 3 chars of the string.
	  (setq msg (concat "> " (substring msg (- 3 max)))))
      (message "%s" msg))))
Richard M. Stallman's avatar
Richard M. Stallman committed
936 937

(defun ange-ftp-abbreviate-filename (file &optional new)
938 939 940
  "Abbreviate the file name FILE relative to the default-directory.
If the optional parameter NEW is given and the non-directory parts match,
only return the directory part of FILE."
941
  (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    (if (and default-directory
	     (string-match (concat "^"
				   (regexp-quote default-directory)
				   ".") file))
	(setq file (substring file (1- (match-end 0)))))
    (if (and new
	     (string-equal (file-name-nondirectory file)
			   (file-name-nondirectory new)))
	(setq file (file-name-directory file)))
    (or file "./")))

;;;; ------------------------------------------------------------
;;;; User / Host mapping support.
;;;; ------------------------------------------------------------

(defun ange-ftp-set-user (host user)
  "For a given HOST, set or change the default USER."
  (interactive "sHost: \nsUser: ")
  (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable))

(defun ange-ftp-get-user (host)
  "Given a HOST, return the default USER."
  (ange-ftp-parse-netrc)
  (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable)))
    (or user
	(prog1
	    (setq user
		  (cond ((stringp ange-ftp-default-user)
			 ;; We have a default name.  Use it.
			 ange-ftp-default-user)
			(ange-ftp-default-user
			 ;; Ask the user.
			 (let ((enable-recursive-minibuffers t))
			   (read-string (format "User for %s: " host)
					(user-login-name))))
			;; Default to the user's login name.
			(t
			 (user-login-name))))
	  (ange-ftp-set-user host user)))))

;;;; ------------------------------------------------------------
;;;; Password support.
;;;; ------------------------------------------------------------

(defun ange-ftp-read-passwd (prompt &optional default)
  "Read a password, echoing `.' for each character typed.
End with RET, LFD, or ESC.  DEL or C-h rubs out.  C-u kills line.
Optional DEFAULT is password to start with."
  (let ((pass (if default default ""))
	(c 0)
	(echo-keystrokes 0)
	(cursor-in-echo-area t))
    (while (progn (message "%s%s"
			   prompt
			   (make-string (length pass) ?.))
		  (setq c (read-char))
		  (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
      (if (= c ?\C-u)
	  (setq pass "")
	(if (and (/= c ?\b) (/= c ?\177))
	    (setq pass (concat pass (char-to-string c)))
	  (if (> (length pass) 0)
	      (setq pass (substring pass 0 -1))))))
    (message "")
1006
    (ange-ftp-repaint-minibuffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    pass))

(defmacro ange-ftp-generate-passwd-key (host user)
  (` (concat (, host) "/" (, user))))

(defmacro ange-ftp-lookup-passwd (host user)
  (` (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key (, host) (, user))
			      ange-ftp-passwd-hashtable)))

(defun ange-ftp-set-passwd (host user passwd)
  "For a given HOST and USER, set or change the associated PASSWORD."
  (interactive (list (read-string "Host: ")
		     (read-string "User: ")
		     (ange-ftp-read-passwd "Password: ")))
  (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
			   passwd
			   ange-ftp-passwd-hashtable))

(defun ange-ftp-get-host-with-passwd (user)
  "Given a USER, return a host we know the password for."
  (ange-ftp-parse-netrc)
  (catch 'found-one
    (ange-ftp-map-hashtable
     (function (lambda (host val)
		 (if (ange-ftp-lookup-passwd host user)
		     (throw 'found-one host))))
     ange-ftp-user-hashtable)
1034
    (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
      (ange-ftp-map-hashtable
       (function
	(lambda (key value)
	  (if (string-match "^[^/]*\\(/\\).*$" key)
	      (let ((host (substring key 0 (match-beginning 1))))
		(if (and (string-equal user (substring key (match-end 1)))
			 value)
		    (throw 'found-one host))))))
       ange-ftp-passwd-hashtable))
    nil))

(defun ange-ftp-get-passwd (host user)
  "Return the password for specified HOST and USER, asking user if necessary."
  (ange-ftp-parse-netrc)

1050
  ;; look up password in the hash table first; user might have overridden the
Richard M. Stallman's avatar
Richard M. Stallman committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  ;; defaults.
  (cond ((ange-ftp-lookup-passwd host user))
	
	;; see if default user and password set from the .netrc file.
	((and (stringp ange-ftp-default-user)
	      ange-ftp-default-password
	      (string-equal user ange-ftp-default-user))
	 ange-ftp-default-password)
	
	;; anonymous ftp password is handled specially since there is an
	;; unwritten rule about how that is used on the Internet.
	((and (or (string-equal user "anonymous")
		  (string-equal user "ftp"))
	      ange-ftp-generate-anonymous-password)
	 (if (stringp ange-ftp-generate-anonymous-password)
	     ange-ftp-generate-anonymous-password
1067
	   user-mail-address))
Richard M. Stallman's avatar
Richard M. Stallman committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	
	;; see if same user has logged in to other hosts; if so then prompt
	;; with the password that was used there.
	(t
	 (let* ((other (ange-ftp-get-host-with-passwd user))
		(passwd (if other
			    
			    ;; found another machine with the same user.
			    ;; Try that account.
			    (ange-ftp-read-passwd
			     (format "passwd for %s@%s (same as %s@%s): "
				     user host user other)
			     (ange-ftp-lookup-passwd other user))
			  
			  ;; I give up.  Ask the user for the password.
			  (ange-ftp-read-passwd
			   (format "Password for %s@%s: " user host)))))
	   (ange-ftp-set-passwd host user passwd)
	   passwd))))

;;;; ------------------------------------------------------------
;;;; Account support
;;;; ------------------------------------------------------------

;; Account passwords must be either specified in the .netrc file, or set
;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
;; check to see whether the FTP process is actually prompting for an account
;; password.
 
(defun ange-ftp-set-account (host user account)
  "For a given HOST and USER, set or change the associated ACCOUNT password."
  (interactive (list (read-string "Host: ")
		     (read-string "User: ")
		     (ange-ftp-read-passwd "Account password: ")))
  (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
			   account
			   ange-ftp-account-hashtable))

(defun ange-ftp-get-account (host user)
  "Given a HOST and USER, return the FTP account."
  (ange-ftp-parse-netrc)
  (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user)
			       ange-ftp-account-hashtable)
      (and (stringp ange-ftp-default-user)
	   (string-equal user ange-ftp-default-user)
	   ange-ftp-default-account)))

;;;; ------------------------------------------------------------
;;;; ~/.netrc support
;;;; ------------------------------------------------------------

(defun ange-ftp-chase-symlinks (file)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1120
  "Return the filename that FILE references, following all symbolic links."
Richard M. Stallman's avatar
Richard M. Stallman committed
1121 1122 1123 1124 1125 1126 1127 1128
  (let (temp)
    (while (setq temp (ange-ftp-real-file-symlink-p file))
      (setq file
	    (if (file-name-absolute-p temp)
		temp
	      (concat (file-name-directory file) temp)))))
  file)

Richard M. Stallman's avatar
Richard M. Stallman committed
1129 1130 1131 1132
;; Move along current line looking for the value of the TOKEN.
;; Valid separators between TOKEN and its value are commas and
;; whitespace.  Second arg LIMIT is a limit for the search.

Richard M. Stallman's avatar
Richard M. Stallman committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
(defun ange-ftp-parse-netrc-token (token limit)
  (if (search-forward token limit t)
      (let (beg)
	(skip-chars-forward ", \t\r\n" limit)
	(if (eq (following-char) ?\")	;quoted token value
	    (progn (forward-char 1)
		   (setq beg (point))
		   (skip-chars-forward "^\"" limit)
		   (forward-char 1)
		   (buffer-substring beg (1- (point))))
	  (setq beg (point))
	  (skip-chars-forward "^, \t\r\n" limit)
	  (buffer-substring beg (point))))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1147 1148 1149 1150
;; Extract the values for the tokens `machine', `login',
;; `password' and `account' in the current buffer.  If successful,
;; record the information found.

Richard M. Stallman's avatar
Richard M. Stallman committed
1151 1152
(defun ange-ftp-parse-netrc-group ()
  (let ((start (point))
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	(end (save-excursion
	       (if (looking-at "machine\\>")
		   ;; Skip `machine' and the machine name that follows.
		   (progn
		     (skip-chars-forward "^ \t\n")
		     (skip-chars-forward " \t\n")
		     (skip-chars-forward "^ \t\n"))
		 ;; Skip `default'.
		 (skip-chars-forward "^ \t\n"))
	       ;; Find start of the next `machine' or `default'
	       ;; or the end of the buffer.
	       (if (re-search-forward "machine\\>\\|default\\>" nil t)
		   (match-beginning 0)
		 (point-max))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	machine login password account)
    (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
	  login    (ange-ftp-parse-netrc-token "login"    end)
	  password (ange-ftp-parse-netrc-token "password" end)
	  account  (ange-ftp-parse-netrc-token "account"  end))
    (if (and machine login)
	;; found a `machine` token.
	(progn
	  (ange-ftp-set-user machine login)
	  (ange-ftp-set-passwd machine login password)
	  (and account
	       (ange-ftp-set-account machine login account)))
      (goto-char start)
      (if (search-forward "default" end t)
	  ;; found a `default' token
	  (progn
	    (setq login    (ange-ftp-parse-netrc-token "login"    end)
		  password (ange-ftp-parse-netrc-token "password" end)
		  account  (ange-ftp-parse-netrc-token "account"  end))
	    (and login
		 (setq ange-ftp-default-user login))
	    (and password
		 (setq ange-ftp-default-password password))
	    (and account
		 (setq ange-ftp-default-account account)))))
    (goto-char end)))

Richard M. Stallman's avatar
Richard M. Stallman committed
1194 1195 1196
;; Read in ~/.netrc, if one exists.  If ~/.netrc file exists and has
;; the correct permissions then extract the \`machine\', \`login\',
;; \`password\' and \`account\' information from within.
Richard M. Stallman's avatar
Richard M. Stallman committed
1197

Richard M. Stallman's avatar
Richard M. Stallman committed
1198
(defun ange-ftp-parse-netrc ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1199 1200 1201
  ;; We set this before actually doing it to avoid the possibility
  ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  (interactive)
1202 1203 1204 1205 1206
  (let (file attr)
    (let ((default-directory "/"))
      (setq file (ange-ftp-chase-symlinks
		  (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
      (setq attr (ange-ftp-real-file-attributes file)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1207 1208
    (if (and attr			; file exists.
	     (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))	; file changed
1209
	(save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	  (if (or ange-ftp-disable-netrc-security-check
		  (and (eq (nth 2 attr) (user-uid)) ; Same uids.
		       (string-match ".r..------" (nth 8 attr))))
	      (save-excursion
		;; we are cheating a bit here.  I'm trying to do the equivalent
		;; of find-file on the .netrc file, but then nuke it afterwards.
		;; with the bit of logic below we should be able to have
		;; encrypted .netrc files.
		(set-buffer (generate-new-buffer "*ftp-.netrc*"))
		(ange-ftp-real-insert-file-contents file)
		(setq buffer-file-name file)
		(setq default-directory (file-name-directory file))
		(normal-mode t)
		(mapcar 'funcall find-file-hooks)
		(setq buffer-file-name nil)
		(goto-char (point-min))
1226
		(skip-chars-forward " \t\n")
Richard M. Stallman's avatar
Richard M. Stallman committed
1227 1228 1229 1230 1231 1232 1233 1234
		(while (not (eobp))
		  (ange-ftp-parse-netrc-group))
		(kill-buffer (current-buffer)))
	    (ange-ftp-message "%s either not owned by you or badly protected."
			      ange-ftp-netrc-filename)
	    (sit-for 1))
	  (setq ange-ftp-netrc-modtime (nth 5 attr))))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1235 1236 1237
;; Return a list of prefixes of the form 'user@host:' to be used when
;; completion is done in the root directory.

Richard M. Stallman's avatar
Richard M. Stallman committed
1238 1239
(defun ange-ftp-generate-root-prefixes ()
  (ange-ftp-parse-netrc)
1240
  (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    (let (res)
      (ange-ftp-map-hashtable
       (function
	(lambda (key value)
	  (if (string-match "^[^/]*\\(/\\).*$" key)
	      (let ((host (substring key 0 (match-beginning 1)))
		    (user (substring key (match-end 1))))
		(setq res (cons (list (concat user "@" host ":"))
				res))))))
       ange-ftp-passwd-hashtable)
      (ange-ftp-map-hashtable
       (function (lambda (host user)
		   (setq res (cons (list (concat host ":"))
				   res))))
       ange-ftp-user-hashtable)
      (or res (list nil)))))

;;;; ------------------------------------------------------------
1259
;;;; Remote file name syntax support.
Richard M. Stallman's avatar
Richard M. Stallman committed
1260 1261
;;;; ------------------------------------------------------------

1262 1263
(defmacro ange-ftp-ftp-name-component (n ns name)
  "Extract the Nth ftp file name component from NS."
Richard M. Stallman's avatar
Richard M. Stallman committed
1264 1265
  (` (let ((elt (nth (, n) (, ns))))
       (if (match-beginning elt)
1266 1267 1268 1269 1270
	   (substring (, name) (match-beginning elt) (match-end elt))))))

(defvar ange-ftp-ftp-name-arg "")
(defvar ange-ftp-ftp-name-res nil)

Richard M. Stallman's avatar
Richard M. Stallman committed
1271 1272
;; Parse NAME according to `ange-ftp-name-format' (which see).
;; Returns a list (HOST USER NAME), or nil if NAME does not match the format.
1273 1274 1275 1276 1277
(defun ange-ftp-ftp-name (name)
  (if (string-equal name ange-ftp-ftp-name-arg)
      ange-ftp-ftp-name-res
    (setq ange-ftp-ftp-name-arg name
	  ange-ftp-ftp-name-res
1278
	  (save-match-data
1279
	    (if (posix-string-match (car ange-ftp-name-format) name)
1280 1281 1282 1283
		(let* ((ns (cdr ange-ftp-name-format))
		       (host (ange-ftp-ftp-name-component 0 ns name))
		       (user (ange-ftp-ftp-name-component 1 ns name))
		       (name (ange-ftp-ftp-name-component 2 ns name)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1284 1285
		  (if (zerop (length user))
		      (setq user (ange-ftp-get-user host)))
1286
		  (list host user name))
Richard M. Stallman's avatar
Richard M. Stallman committed
1287 1288
	      nil)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1289 1290
;; Take a FULLNAME that matches according to ange-ftp-name-format and
;; replace the name component with NAME.
1291
(defun ange-ftp-replace-name-component (fullname name)
1292
  (save-match-data
1293
    (if (posix-string-match (car ange-ftp-name-format) fullname)
1294
	(let* ((ns (cdr ange-ftp-name-format))
Richard M. Stallman's avatar
Richard M. Stallman committed
1295
	       (elt (nth 2 ns)))
1296 1297 1298
	  (concat (substring fullname 0 (match-beginning elt))
		  name
		  (substring fullname (match-end elt)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1299 1300 1301 1302 1303 1304 1305 1306

;;;; ------------------------------------------------------------
;;;; Miscellaneous utils.
;;;; ------------------------------------------------------------

;; (setq ange-ftp-tmp-keymap (make-sparse-keymap))
;; (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)

1307 1308 1309
(defun ange-ftp-repaint-minibuffer ()
  "Clear any existing minibuffer message; let the minibuffer contents show."
  (message nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
1310

Richard M. Stallman's avatar
Richard M. Stallman committed
1311 1312
;; Return the name of the buffer that collects output from the ftp process
;; connected to the given HOST and USER pair.
Richard M. Stallman's avatar
Richard M. Stallman committed
1313 1314 1315
(defun ange-ftp-ftp-process-buffer (host user)
  (concat "*ftp " user "@" host "*"))

Richard M. Stallman's avatar
Richard M. Stallman committed
1316 1317
;; Display the last chunk of output from the ftp process for the given HOST
;; USER pair, and signal an error including MSG in the text.
Richard M. Stallman's avatar
Richard M. Stallman committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
(defun ange-ftp-error (host user msg)
  (let ((cur (selected-window))
	(pop-up-windows t))
    (pop-to-buffer
     (get-buffer-create
      (ange-ftp-ftp-process-buffer host user)))
    (goto-char (point-max))
    (select-window cur))
  (signal 'ftp-error (list (format "FTP Error: %s" msg))))

(defun ange-ftp-set-buffer-mode ()
1329
  "Set correct modes for the current buffer if visiting a remote file."
Richard M. Stallman's avatar
Richard M. Stallman committed
1330
  (if (and (stringp buffer-file-name)
1331
	   (ange-ftp-ftp-name buffer-file-name))
1332
      (auto-save-mode ange-ftp-auto-save)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1333 1334

(defun ange-ftp-kill-ftp-process (buffer)
1335
  "Kill the FTP process associated with BUFFER.
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1336
If the BUFFER's visited filename or default-directory is an ftp filename
Richard M. Stallman's avatar
Richard M. Stallman committed
1337 1338 1339 1340 1341 1342
then kill the related ftp process."
  (interactive "bKill FTP process associated with buffer: ")
  (if (null buffer)
      (setq buffer (current-buffer)))
  (let ((file (or (buffer-file-name) default-directory)))
    (if file
1343
	(let ((parsed (ange-ftp-ftp-name (expand-file-name file))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	  (if parsed
	      (let ((host (nth 0 parsed))
		    (user (nth 1 parsed)))
		(kill-buffer (ange-ftp-ftp-process-buffer host user))))))))

(defun ange-ftp-quote-string (string)
  "Quote any characters in STRING that may confuse the ftp process."
  (apply (function concat)
	 (mapcar (function
		   (lambda (char)
		     (if (or (<= char ? )
			     (> char ?\~)
		             (= char ?\")
			     (= char ?\\))
			 (vector ?\\ char)
		       (vector char))))
		 string)))

(defun ange-ftp-barf-if-not-directory (directory)
  (or (file-directory-p directory)
      (signal 'file-error
	      (list "Opening directory"
		    (if (file-exists-p directory)
			"not a directory"
		      "no such file or directory")
		    directory))))

;;;; ------------------------------------------------------------
;;;; FTP process filter support.
;;;; ------------------------------------------------------------

(defun ange-ftp-process-handle-line (line proc)
Richard M. Stallman's avatar
Richard M. Stallman committed
1376 1377 1378
  "Look at the given LINE from the ftp process PROC.
Try to categorize it into one of four categories:
good, skip, fatal, or unknown."
Richard M. Stallman's avatar
Richard M. Stallman committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
  (cond ((string-match ange-ftp-xfer-size-msgs line)
	 (setq ange-ftp-xfer-size
	       (ash (string-to-int (substring line
					      (match-beginning 1)
					      (match-end 1)))
		    -10)))
	((string-match ange-ftp-skip-msgs line)
	 t)
	((string-match ange-ftp-good-msgs line)
	 (setq ange-ftp-process-busy nil
	       ange-ftp-process-result t
	       ange-ftp-process-result-line line))
Richard M. Stallman's avatar
Richard M. Stallman committed
1391 1392 1393 1394 1395
	;; Check this before checking for errors.
	;; Otherwise the last line of these three seems to be an error:
	;; 230-see a significant impact from the move.  For those of you who can't
	;; 230-use DNS to resolve hostnames and get an error message like
	;; 230-"ftp.stsci.edu: unknown host", the new IP address will be...
1396 1397
	((string-match ange-ftp-multi-msgs line)
	 (setq ange-ftp-process-multi-skip t))
Richard M. Stallman's avatar
Richard M. Stallman committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	((string-match ange-ftp-fatal-msgs line)
	 (delete-process proc)
	 (setq ange-ftp-process-busy nil
	       ange-ftp-process-result-line line))
	(ange-ftp-process-multi-skip
	 t)
	(t
	 (setq ange-ftp-process-busy nil
	       ange-ftp-process-result-line line))))

(defun ange-ftp-set-xfer-size (host user bytes)
  "Set the size of the next FTP transfer in bytes."
  (let ((proc (ange-ftp-get-process host user)))
    (if proc
	(let ((buf (process-buffer proc)))
	  (if buf
	      (save-excursion
		(set-buffer buf)
		(setq ange-ftp-xfer-size (ash bytes -10))))))))

(defun ange-ftp-process-handle-hash (str)
  "Remove hash marks from STRING and display count so far."
  (setq str (concat (substring str 0 (match-beginning 0))
		    (substring str (match-end 0)))
	ange-ftp-hash-mark-count (+ (- (match-end 0)
				       (match-beginning 0))
				    ange-ftp-hash-mark-count))
1425 1426
  (and ange-ftp-hash-mark-unit
       ange-ftp-process-msg
Richard M. Stallman's avatar
Richard M. Stallman committed
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
       ange-ftp-process-verbose
       (not (eq (selected-window) (minibuffer-window)))
       (not (boundp 'search-message))	;screws up isearch otherwise
       (not cursor-in-echo-area)	;screws up y-or-n-p otherwise
       (let ((kbytes (ash (* ange-ftp-hash-mark-unit
			     ange-ftp-hash-mark-count)
			  -6)))
       (if (zerop ange-ftp-xfer-size)
	   (ange-ftp-message "%s...%dk" ange-ftp-process-msg kbytes)
	 (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size)))
	   ;; cut out the redisplay of identical %-age messages.
	   (if (not (eq percent ange-ftp-last-percent))
	       (progn
		 (setq ange-ftp-last-percent percent)
		 (ange-ftp-message "%s...%d%%" ange-ftp-process-msg percent)))))))
  str)

Richard M. Stallman's avatar
Richard M. Stallman committed
1444 1445 1446 1447 1448
;; Call the function specified by CONT.  CONT can be either a function
;; or a list of a function and some args.  The first two parameters
;; passed to the function will be RESULT and LINE.  The remaining args
;; will be taken from CONT if a list was passed.

Richard M. Stallman's avatar
Richard M. Stallman committed
1449 1450 1451 1452 1453 1454 1455
(defun ange-ftp-call-cont (cont result line)
  (if cont
      (if (and (listp cont)
	       (not (eq (car cont) 'lambda)))
	  (apply (car cont) result line (cdr cont))
	(funcall cont result line))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1456 1457 1458
;; Build up a complete line of output from the ftp PROCESS and pass it
;; on to ange-ftp-process-handle-line to deal with.

Richard M. Stallman's avatar
Richard M. Stallman committed
1459 1460 1461 1462 1463 1464 1465
(defun ange-ftp-process-filter (proc str)
  (let ((buffer (process-buffer proc))
	(old-buffer (current-buffer)))

    ;; see if the buffer is still around... it could have been deleted.
    (if (buffer-name buffer)
	(unwind-protect
1466
	    (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
1467 1468 1469
	      (set-buffer (process-buffer proc))
	      
	      ;; handle hash mark printing
1470
	      (and ange-ftp-process-busy
Richard M. Stallman's avatar
Richard M. Stallman committed
1471 1472
		   (string-match "^#+$" str)
		   (setq str (ange-ftp-process-handle-hash str)))
1473
	      (comint-output-filter proc str)
1474 1475 1476
	      ;; Replace STR by the result of the comint processing.
	      (setq str (buffer-substring comint-last-output-start
					  (process-mark proc)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	      (if ange-ftp-process-busy
		  (progn
		    (setq ange-ftp-process-string (concat ange-ftp-process-string
							  str))
		    
		    ;; if we gave an empty password to the USER command earlier
		    ;; then we should send a null password now.
		    (if (string-match "Password: *$" ange-ftp-process-string)
			(send-string proc "\n"))))
	      (while (and ange-ftp-process-busy
			  (string-match "\n" ange-ftp-process-string))
		(let ((line (substring ange-ftp-process-string
				       0
				       (match-beginning 0))))
		  (setq ange-ftp-process-string (substring ange-ftp-process-string
							   (match-end 0)))
		  (while (string-match "^ftp> *" line)
		    (setq line (substring line (match-end 0))))
		  (ange-ftp-process-handle-line line proc)))

	      ;; has the ftp client finished?  if so then do some clean-up
	      ;; actions.
	      (if (not ange-ftp-process-busy)
		  (progn
		    ;; reset the xfer size
		    (setq ange-ftp-xfer-size 0)

		    ;; issue the "done" message since we've finished.
		    (if (and ange-ftp-process-msg
			     ange-ftp-process-verbose
			     ange-ftp-process-result)
			(progn
			  (ange-ftp-message "%s...done" ange-ftp-process-msg)
1510
			  (ange-ftp-repaint-minibuffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
1511 1512