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

Richard M. Stallman's avatar
Richard M. Stallman committed
3
;; Copyright (C) 1989,90,91,92,93,94,95,96  Free Software Foundation, Inc.
Erik Naggum's avatar
Erik Naggum committed
4

Eric S. Raymond's avatar
Oops...  
Eric S. Raymond committed
5
;; Author: Andy Norman (ange@hplb.hpl.hp.com)
Richard M. Stallman's avatar
Richard M. Stallman committed
6
;; Maintainer: FSF
Eric S. Raymond's avatar
Oops...  
Eric S. Raymond committed
7
;; Keywords: comm
Erik Naggum's avatar
Erik Naggum committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs 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.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
25

26
;;; Commentary:
Erik Naggum's avatar
Erik Naggum committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 95 96 97 98 99 100 101 102 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 275 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 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 389 390 391 392 393 394 395 396 397 398 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 455 456 457 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

;; 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
;; '/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'.
;;
;; For example: if find-file is given a filename of:
;;
;;   /ange@anorman:/tmp/notes
;;
;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as
;; 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
;; needs a password to connect then it reads one in the echo area.

;; Extended filename syntax:
;;
;; The default extended filename syntax is '/user@host:name', where the
;; 'user@' part may be omitted.  This syntax can be customised to a certain
;; extent by changing ange-ftp-name-format.  There are limitations.
;;
;; If the user part is omitted then ange-ftp generates a default user
;; instead whose value depends on the variable ange-ftp-default-user.

;; Passwords:
;;
;; 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.

;; Passwords for user "anonymous":
;;
;; 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
;; the value of `user-mail-address' is used; if nil then the user
;; is prompted for a password as normal.

;; "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.
;;
;; Killing the "*ftp user@host*" buffer also kills the ftp process.
;; This should not cause ange-ftp any grief.

;; Binary file transfers:
;;
;; 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.

;; 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:
;;
;; Sometimes it is necessary for the FTP process to be run on a different
;; 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.
;;    This directory is necessary for temporary files created by ange-ftp.
;;
;; 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.
;;
;; 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.

;; 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
;;    end in an @.  If you get yourself into this situation then editing
;;    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:
;;
;; Ange-ftp has full support for VMS hosts.  It
;; 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
;;    latest version of the file. For this reason, in dired "f"
;;    (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
;;    by typing $$.

;; MTS support:
;;
;; Ange-ftp has full support for hosts running
;; 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
;; relative name fashion as
;;   /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:
;; 
;; Ange-ftp has full support for hosts running
;; 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
;;    user.  This is particularly important when logging in as the root user.
;;    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
;;    ange-ftp-kill-ftp-process can restart the ftp process, which
;;    should get things back in sync.
;;
;; 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.
;;
;; 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.
;; 
;; 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/
Richard M. Stallman's avatar
Richard M. Stallman committed
515

Erik Naggum's avatar
Erik Naggum committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 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
;; -----------------------------------------------------------
;; Technical information on this package:
;; -----------------------------------------------------------

;; 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.

;; Checklist for adding non-UNIX support for TYPE
;; 
;; The following functions may need TYPE versions:
;; (not all functions will be needed for every OS)
;;
;; ange-ftp-fix-name-for-TYPE
;; ange-ftp-fix-dir-name-for-TYPE
;; 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
;; ange-ftp-TYPE-make-compressed-filename
;; ange-ftp-TYPE-file-name-sans-versions
;;
;; 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.
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
562
;; t = a remote host of unknown type. Think t as in true, it's remote.
Erik Naggum's avatar
Erik Naggum committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
;;     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,
;; being realists they have put calls to `error' in the program at these
;; 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
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
584

Erik Naggum's avatar
Erik Naggum committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
;; -----------------------------------------------------------
;; 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.
;;
;; Thanks to Sebastian Kremer for dired support and for many ideas and
;; 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.
Richard M. Stallman's avatar
Richard M. Stallman committed
620

621
;;; Code:
Erik Naggum's avatar
Erik Naggum committed
622

623 624
(require 'comint)

Richard M. Stallman's avatar
Richard M. Stallman committed
625 626 627 628
;;;; ------------------------------------------------------------
;;;; User customization variables.
;;;; ------------------------------------------------------------

629
(defvar ange-ftp-name-format
630
  '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4))
631 632 633 634
  "*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
635 636 637 638 639 640 641
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
642
  "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-"
Richard M. Stallman's avatar
Richard M. Stallman committed
643
  "*Regular expression matching the start of a multiline ftp reply.")
Richard M. Stallman's avatar
Richard M. Stallman committed
644 645 646

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

;; 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:\\|"
658
	  "^Data connection \\|"
659 660
	  "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye\\|"
	  "^227 .*[Pp]assive")
Richard M. Stallman's avatar
Richard M. Stallman committed
661
  "*Regular expression matching ftp messages that can be ignored.")
Richard M. Stallman's avatar
Richard M. Stallman committed
662 663 664 665

(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
666 667
  "*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
668 669 670

(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
671
  "*Regular expression matching login failure messages from rlogin/telnet.")
Richard M. Stallman's avatar
Richard M. Stallman committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

(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
694 695 696 697 698
  "*User name to use when none is specified in a file name.
If non-nil but not a string, you are prompted for the name.
If nil, the value of `ange-ftp-netrc-default-user' is used.
If that is nil too, then your login name is used.

Karl Heuer's avatar
Karl Heuer committed
699 700 701 702 703
Once a connection to a given host has been initiated, the user name
and password information for that host are cached and re-used by
ange-ftp.  Use `ange-ftp-set-user' to change the cached values,
since setting `ange-ftp-default-user' directly does not affect
the cached information.")  
704 705 706 707 708

(defvar ange-ftp-netrc-default-user nil
  "Alternate default user name to use when none is specified.
This variable is set from the `default' command in your `.netrc' file,
if there is one.")
Richard M. Stallman's avatar
Richard M. Stallman committed
709 710

(defvar ange-ftp-default-password nil
711
  "*Password to use when the user name equals `ange-ftp-default-user'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
712 713

(defvar ange-ftp-default-account nil
714 715 716 717 718 719 720
  "*Account to use when the user name equals `ange-ftp-default-user'.")

(defvar ange-ftp-netrc-default-password nil
  "*Password to use when the user name equals `ange-ftp-netrc-default-user'.")

(defvar ange-ftp-netrc-default-account nil
  "*Account to use when the user name equals `ange-ftp-netrc-default-user'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
721

Richard M. Stallman's avatar
Richard M. Stallman committed
722
(defvar ange-ftp-generate-anonymous-password t
723 724 725
  "*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
726 727

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

(defvar ange-ftp-binary-file-name-regexp
731
  (concat "\\.[zZ]$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
Richard M. Stallman's avatar
Richard M. Stallman committed
732
	  "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
733 734
	  "\\.EXE\\(;[0-9]+\\)?$\\|\\.[zZ]-part-..$\\|\\.gz$\\|"
	  "\\.taz$\\|\\.tgz$")
Richard M. Stallman's avatar
Richard M. Stallman committed
735 736 737 738 739 740
  "*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
741 742
  "*Regexp selecting hosts which can be reached directly with ftp.
For other hosts the FTP process is started on \`ange-ftp-gateway-host\'
743
instead, and/or reached via \`ange-ftp-gateway-ftp-program-name\'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
744 745

(defvar ange-ftp-gateway-program-interactive nil
Richard M. Stallman's avatar
Richard M. Stallman committed
746 747
  "*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
748

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

754
(defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *"
Richard M. Stallman's avatar
Richard M. Stallman committed
755 756
  "*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
757 758 759 760 761 762 763 764
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
765 766 767
  "*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
768 769

(defvar ange-ftp-smart-gateway nil
770 771 772
  "*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
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

(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"
798
  "*Name of FTP program to run when accessing non-local hosts.
Richard M. Stallman's avatar
Richard M. Stallman committed
799 800 801 802 803 804 805 806 807
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 ()
808
  "*Non-nil means make backup files for \"magic\" remote files.")
Richard M. Stallman's avatar
Richard M. Stallman committed
809 810

(defvar ange-ftp-retry-time 5
Richard M. Stallman's avatar
Richard M. Stallman committed
811 812
  "*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
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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

(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
876
  "Return a sorted list of all the active keys in TABLE, as strings."
Richard M. Stallman's avatar
Richard M. Stallman committed
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 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  (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)
934
(defvar ange-ftp-this-msg)
Richard M. Stallman's avatar
Richard M. Stallman committed
935 936 937 938 939 940 941 942 943 944 945 946
(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)
947 948
  "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
949 950
  (let ((msg (apply (function format) fmt args))
	(max (window-width (minibuffer-window))))
951 952 953 954 955 956
    (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
957 958

(defun ange-ftp-abbreviate-filename (file &optional new)
959 960 961
  "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."
962
  (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
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
    (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))))
998
			(ange-ftp-netrc-default-user)
Richard M. Stallman's avatar
Richard M. Stallman committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
			;; 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."
1012
  (let ((pass nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	(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 "")
1028
    (ange-ftp-repaint-minibuffer)
1029
    (or pass default "")))
Richard M. Stallman's avatar
Richard M. Stallman committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

(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)
1056
    (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
      (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)

1072
  ;; look up password in the hash table first; user might have overridden the
Richard M. Stallman's avatar
Richard M. Stallman committed
1073 1074 1075
  ;; defaults.
  (cond ((ange-ftp-lookup-passwd host user))
	
1076
	;; See if default user and password set.
Richard M. Stallman's avatar
Richard M. Stallman committed
1077 1078 1079 1080 1081
	((and (stringp ange-ftp-default-user)
	      ange-ftp-default-password
	      (string-equal user ange-ftp-default-user))
	 ange-ftp-default-password)
	
1082 1083 1084 1085 1086 1087
	;; See if default user and password set from .netrc file.
	((and (stringp ange-ftp-netrc-default-user)
	      ange-ftp-netrc-default-password
	      (string-equal user ange-ftp-netrc-default-user))
	 ange-ftp-netrc-default-password)
	
Richard M. Stallman's avatar
Richard M. Stallman committed
1088 1089 1090 1091 1092 1093 1094
	;; 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
1095
	   user-mail-address))
Richard M. Stallman's avatar
Richard M. Stallman committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	
	;; 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
1106
			     (format "passwd for %s@%s (default same as %s@%s): "
Richard M. Stallman's avatar
Richard M. Stallman committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
				     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)
1141 1142 1143 1144
	   ange-ftp-default-account)
      (and (stringp ange-ftp-netrc-default-user)
	   (string-equal user ange-ftp-netrc-default-user)
	   ange-ftp-netrc-default-account)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1145 1146 1147 1148 1149 1150

;;;; ------------------------------------------------------------
;;;; ~/.netrc support
;;;; ------------------------------------------------------------

(defun ange-ftp-chase-symlinks (file)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1151
  "Return the filename that FILE references, following all symbolic links."
Richard M. Stallman's avatar
Richard M. Stallman committed
1152 1153 1154 1155 1156 1157 1158 1159
  (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
1160 1161 1162 1163
;; 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
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
(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
1178 1179 1180 1181
;; 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
1182 1183
(defun ange-ftp-parse-netrc-group ()
  (let ((start (point))
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	(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
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	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
1218
		 (setq ange-ftp-netrc-default-user login))
Richard M. Stallman's avatar
Richard M. Stallman committed
1219
	    (and password
1220
		 (setq ange-ftp-netrc-default-password password))
Richard M. Stallman's avatar
Richard M. Stallman committed
1221
	    (and account
1222
		 (setq ange-ftp-netrc-default-account account)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1223 1224
    (goto-char end)))

Richard M. Stallman's avatar
Richard M. Stallman committed
1225 1226 1227
;; 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
1228

Richard M. Stallman's avatar
Richard M. Stallman committed
1229
(defun ange-ftp-parse-netrc ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1230 1231 1232
  ;; 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)
1233 1234 1235 1236 1237
  (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
1238 1239
    (if (and attr			; file exists.
	     (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))	; file changed
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
	  (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))
1257
		(skip-chars-forward " \t\n")
Richard M. Stallman's avatar
Richard M. Stallman committed
1258 1259 1260 1261 1262 1263 1264 1265
		(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
1266 1267 1268
;; 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
1269 1270
(defun ange-ftp-generate-root-prefixes ()
  (ange-ftp-parse-netrc)
1271
  (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    (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)))))

;;;; ------------------------------------------------------------
1290
;;;; Remote file name syntax support.
Richard M. Stallman's avatar
Richard M. Stallman committed
1291 1292
;;;; ------------------------------------------------------------

1293 1294
(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
1295 1296
  (` (let ((elt (nth (, n) (, ns))))
       (if (match-beginning elt)
1297 1298 1299 1300 1301
	   (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
1302 1303
;; 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.
1304 1305 1306 1307 1308
(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
1309
	  (save-match-data
1310
	    (if (posix-string-match (car ange-ftp-name-format) name)
1311 1312 1313 1314
		(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
1315 1316
		  (if (zerop (length user))
		      (setq user (ange-ftp-get-user host)))
1317
		  (list host user name))
Richard M. Stallman's avatar
Richard M. Stallman committed
1318 1319
	      nil)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1320 1321
;; Take a FULLNAME that matches according to ange-ftp-name-format and
;; replace the name component with NAME.
1322
(defun ange-ftp-replace-name-component (fullname name)
1323
  (save-match-data
1324
    (if (posix-string-match (car ange-ftp-name-format) fullname)
1325
	(let* ((ns (cdr ange-ftp-name-format))
Richard M. Stallman's avatar
Richard M. Stallman committed
1326
	       (elt (nth 2 ns)))
1327 1328 1329
	  (concat (substring fullname 0 (match-beginning elt))
		  name
		  (substring fullname (match-end elt)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1330 1331 1332 1333 1334 1335 1336 1337

;;;; ------------------------------------------------------------
;;;; Miscellaneous utils.
;;;; ------------------------------------------------------------

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

1338 1339 1340
(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
1341

Richard M. Stallman's avatar
Richard M. Stallman committed
1342 1343
;; 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
1344 1345 1346
(defun ange-ftp-ftp-process-buffer (host user)
  (concat "*ftp " user "@" host "*"))

Richard M. Stallman's avatar
Richard M. Stallman committed
1347 1348
;; 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
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
(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 ()
1360
  "Set correct modes for the current buffer if visiting a remote file."
Richard M. Stallman's avatar
Richard M. Stallman committed
1361
  (if (and (stringp buffer-file-name)
1362
	   (ange-ftp-ftp-name buffer-file-name))
1363
      (auto-save-mode ange-ftp-auto-save)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1364

1365 1366
(defun ange-ftp-kill-ftp-process (&optional buffer)
  "Kill the FTP process associated with BUFFER (the current buffer, if nil).
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1367
If the BUFFER's visited filename or default-directory is an ftp filename
Richard M. Stallman's avatar
Richard M. Stallman committed
1368 1369 1370
then kill the related ftp process."
  (interactive "bKill FTP process associated with buffer: ")
  (if (null buffer)
1371 1372
      (setq buffer (current-buffer))
    (setq buffer (get-buffer buffer)))
1373 1374
  (let ((file (or (buffer-file-name buffer)
		  (save-excursion (set-buffer buffer) default-directory))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1375
    (if file
1376
	(let ((parsed (ange-ftp-ftp-name (expand-file-name file))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	  (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
1409 1410 1411
  "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
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  (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
1424 1425 1426 1427 1428
	;; 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...
1429 1430
	((string-match ange-ftp-multi-msgs line)
	 (setq ange-ftp-process-multi-skip t))
Richard M. Stallman's avatar
Richard M. Stallman committed
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	((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))
1458 1459
  (and ange-ftp-hash-mark-unit
       ange-ftp-process-msg
Richard M. Stallman's avatar
Richard M. Stallman committed
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
       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
1477 1478 1479 1480 1481
;; 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
1482 1483 1484 1485 1486 1487 1488
(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
1489 1490 1491
;; 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
1492 1493 1494 1495
(defun ange-ftp-process-filter (proc str)
  (let ((buffer (process-buffer proc))
	(old-buffer (current-buffer)))

1496 1497 1498 1499
    ;; Eliminate nulls.
    (while (string-match "\000+" str)
      (setq str (replace-match "" nil nil str))) 

Richard M. Stallman's avatar
Richard M. Stallman committed
1500 1501 1502
    ;; see if the buffer is still around... it could have been deleted.
    (if (buffer-name buffer)
	(unwind-protect
1503
	    (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
1504 1505 1506
	      (set-buffer (process-buffer proc))
	      
	      ;; handle hash mark printing
1507
	      (and ange-ftp-process-busy
Richard M. Stallman's avatar
Richard M. Stallman committed
1508 1509
		   (string-match "^#+$" str)
		   (setq str (ange-ftp-process-handle-hash str)))
1510
	      (comint-output-filter proc str)
1511 1512 1513
	      ;; 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
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	      (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)
1547
			  (ange-ftp-repaint-minibuffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
			  (setq ange-ftp-process-msg nil)))
		    
		    ;; is there a continuation we should be calling?  if so,
		    ;; we'd better call it, making sure we only call it once.
		    (if ange-ftp-process-continue
			(let ((cont ange-ftp-process-continue))
			  (setq ange-ftp-process-continue nil)
			  (ange-ftp-call-cont cont
					      ange-ftp-process-result
					      ange-ftp-process-result-line))))))
	  (set-buffer old-buffer)))))

(defun ange-ftp-process-sentinel (proc str)
  "When ftp process changes state, nuke all file-entries in cache."
1562
  (let ((name (process-name proc)))
1563
    (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)\\*" name)
1564 1565 1566 1567
	(let ((user (substring name (match-beginning 1) (match-end 1)))
	      (host (substring name (match-beginning 2) (match-end 2))))
	  (ange-ftp-wipe-file-entries host user))))
  (setq ange-ftp-ls-cache-file nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577

;;;; ------------------------------------------------------------
;;;; Gateway support.
;;;; ------------------------------------------------------------

(defun ange-ftp-use-gateway-p (host)
  "Returns whether to access this host via a normal (non-smart) gateway."
  ;; yes, I know that I could simplify the following expression, but it is
  ;; clearer (to me at least) this way.
  (and (not ange-ftp-smart-gateway)
1578
       (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1579 1580 1581 1582 1583
	 (not (string-match ange-ftp-local-host-regexp host)))))

(defun ange-ftp-use-smart-gateway-p (host)
  "Returns whether to access this host via a smart gateway."
  (and ange-ftp-smart-gateway
1584
       (save-match-data
Richard M. Stallman's avatar
Richard M. Stallman committed
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
	 (not (string-match ange-ftp-local-host-regexp host)))))


;;; ------------------------------------------------------------
;;; Temporary file location and deletion...
;;; ------------------------------------------------------------

(defvar ange-ftp-tmp-name-files ())
(defvar ange-ftp-tmp-name-hashtable (ange-ftp-make-hashtable 10))
(defvar ange-ftp-pid nil)

(defun ange-ftp-get-pid ()
  "Half-hearted attempt to get the current process's id."
  (setq ange-ftp-pid (substring (make-temp-name "") 1)))

(defun ange-ftp-make-tmp-name (host)
  "This routine will return the name of a new file."
  (let* ((template (if (ange-ftp-use-gateway-p host)
		       ange-ftp-gateway-tmp-name-template
		     ange-ftp-tmp-name-template))
	 (pid (or ange-ftp-pid (ange-ftp-get-pid)))
	 (start ?a)
	 file entry)
    (while 
	(progn
	  (setq file (format "%s%c%s" template start pid))
	  (setq entry (intern file ange-ftp-tmp-name-hashtable))
	  (or (memq entry ange-ftp-tmp-name-files)
	      (ange-ftp-real-file-exists-p file)))
      (if (> (setq start (1+ start)) ?z)
	  (progn
	    (setq template (concat template "X"))
	    (setq start ?a))))
    (setq ange-ftp-tmp-name-files
	  (cons entry ange-ftp-tmp-name-files))
    file))

(defun ange-ftp-del-tmp-name (temp)
  (setq ange-ftp-tmp-name-files
	(delq (intern temp ange-ftp-tmp-name-hashtable)
	      ange-ftp-tmp-name-files))
  (condition-case ()
      (ange-ftp-real-delete-file temp)
    (error nil)))

;;;; ------------------------------------------------------------
;;;; Interactive gateway program support.
;;;; ------------------------------------------------------------

(defvar ange-ftp-gwp-running t)
(defvar ange-ftp-gwp-status nil)

(defun ange-ftp-gwp-sentinel (proc str)
  (setq ange-ftp-gwp-running nil))

(defun ange-ftp-gwp-filter (proc str)
1641
  (comint-output-filter proc str)
1642 1643 1644 1645
  (save-excursion
    (set-buffer (process-buffer proc))
    ;; Replace STR by the result of the comint processing.
    (setq str (buffer-substring comint-last-output-start (process-mark proc))))
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
  (cond ((string-match "login: *$" str)
	 (send-string proc
		      (concat
		       (let ((ange-ftp-default-user t))
			 (ange-ftp-get-user ange-ftp-gateway-host))
		       "\n")))
	((string-match "Password: *$" str)
	 (send-string proc
		      (concat
		       (ange-ftp-get-passwd ange-ftp-gateway-host
					    (ange-ftp-get-user
					     ange-ftp-gateway-host))
		       "\n")))
	((string-match ange-ftp-gateway-fatal-msgs str)
	 (delete-process proc)
	 (setq ange-ftp-gwp-running nil))
	((string-match ange-ftp-gateway-prompt-pattern str)
	 (setq ange-ftp-gwp-running nil
	       ange-ftp-gwp-status t))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1665 1666 1667 1668

(defun ange-ftp-gwp-start (host user name args)
  "Login to the gateway machine and fire up an ftp process."
  (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host))
Richard M. Stallman's avatar
Richard M. Stallman committed
1669 1670 1671 1672
	 ;; It would be nice to make process-connection-type nil,
	 ;; but that doesn't work: ftp never responds.
	 ;; Can anyone find a fix for that?
	 (proc (let ((process-connection-type t))
1673
		 (start-process name name
Richard M. Stallman's avatar
Richard M. Stallman committed
1674 1675
				ange-ftp-gateway-program
				ange-ftp-gateway-host)))