COMMENT(!MOD!zsh/zftp
A builtin FTP client.
!MOD!)
The tt(zsh/zftp) module makes available one builtin command:

startitem()
findex(zftp)
cindex(FTP)
cindex(files, transferring)
item(tt(zftp) var(subcommand) [ var(args) ])(
The tt(zsh/zftp) module is a client for FTP (file transfer protocol).  It
is implemented as a builtin to allow full use of shell command line
editing, file I/O, and job control mechanisms.  Often, users will
access it via shell functions providing a more powerful interface; a set is
provided with the tt(zsh) distribution and is described in
ifzman(zmanref(zshzftpsys))\
ifnzman(noderef(Zftp Function System))\
.  However, the tt(zftp) command is entirely usable in its
own right.

All commands consist of the command name tt(zftp) followed by the name
of a subcommand.  These are listed below.  The return status of each
subcommand is supposed to reflect the success or failure of the remote
operation.  See a description of the variable tt(ZFTP_VERBOSE) for
more information on how responses from the server may be printed.
)
enditem()

subsect(Subcommands)
cindex(zftp, subcommands)

startitem()
cindex(FTP, starting a session)
item(tt(open) var(host) [ var(user) [ var(password) [ var(account) ] ] ])(
Open a new FTP session to var(host), which may be the name of a TCP/IP
connected host or an IP number in the standard dot notation.
Remaining arguments are passed to the tt(login) subcommand.  Note that
if no arguments beyond var(host) are supplied, tt(open) will em(not)
automatically call tt(login).  If no arguments at all are supplied,
tt(open) will use the parameters set by the tt(params) subcommand.

After a successful open, the shell variables tt(ZFTP_HOST),
tt(ZFTP_IP) and tt(ZFTP_SYSTEM) are available; see `Variables'
below.
)
xitem(tt(login) [ var(name) [ var(password) [ var(account) ] ] ])
item(tt(user) [ var(name) [ var(password) [ var(account) ] ] ])(
Login the user var(name) with parameters var(password) and var(account).
Any of the parameters can be omitted, and will be read from standard
input if needed (var(name) is always needed).  If 
standard input is a terminal, a prompt for each one will be printed on
standard error and var(password) will not be echoed.  If any of the
parameters are not used, a warning message is printed.

After a successful login, the shell variables tt(ZFTP_USER),
tt(ZFTP_ACCOUNT) and tt(ZFTP_PWD) are available; see `Variables'
below.

This command may be re-issued when a user is already logged in, and
the server will first be reinitialized for a new user.
)
xitem(tt(params) [ var(host) [ var(user) [ var(password) \
[ var(account) ] ] ] ])
item(tt(params) tt(-))(
Store the given parameters for a later tt(open) command with no
arguments.  Only those given on the command line will be remembered.
If no arguments are given, the parameters currently set are printed,
although the password will appear as a line of stars; the return value is
one if no parameters were set, zero otherwise.

Any of the parameters may be specified as a `tt(?)', which
may need to be quoted to protect it from shell expansion.  In this case,
the appropriate parameter will be read from stdin as with the
tt(login) subcommand, including special handling of var(password).  If the
`tt(?)' is followed by a string, that is used as the prompt for reading the
parameter instead of the default message (any necessary punctuation and
whitespace should be included at the end of the prompt).  The first letter
of the parameter (only) may be quoted with a `tt(\)'; hence an argument
tt("\\$word") guarantees that the string from the shell parameter tt($word)
will be treated literally, whether or not it begins with a `tt(?)'.

If instead a single `tt(-)' is given, the existing parameters, if any,
are deleted.  In that case, calling tt(open) with no arguments will
cause an error.

The list of parameters is not deleted after a tt(close), however it
will be deleted if the tt(zsh/zftp) module is unloaded.

For example,

example(zftp params ftp.elsewhere.xx juser '?Password for juser: ')

will store the host tt(ftp.elsewhere.xx) and the user tt(juser) and
then prompt the user for the corresponding password with the given prompt.
)
item(tt(test))(
Test the connection; if the server has reported
that it has closed the connection (maybe due to a timeout), return
status 2; if no connection was open anyway, return status 1; else
return status 0.  The tt(test) subcommand is
silent, apart from messages printed by the tt($ZFTP_VERBOSE)
mechanism, or error messages if the connection closes.  There is no
network overhead for this test.

The test is only supported on systems with either the tt(select(2)) or
tt(poll(2)) system calls; otherwise the message `tt(not
supported on this system)' is printed instead.

The tt(test) subcommand will automatically be called at the start of any
other subcommand for the current session when a connection is open.
)
item(tt(cd) var(directory))(
Change the remote directory to var(directory).  Also alters the shell
variable tt(ZFTP_PWD).
)
item(tt(cdup))(
Change the remote directory to the one higher in the directory tree.
Note that tt(cd ..) will also work correctly on non-UNIX systems.
)
item(tt(dir) [ var(args...) ])(
Give a (verbose) listing of the remote directory.  The var(args) are
passed directly to the server. The command's behaviour is implementation
dependent, but a UNIX server will typically interpret var(args) as
arguments to the tt(ls) command and with no arguments return the
result of `tt(ls -l)'. The directory is listed to standard output.
)
item(tt(ls) [ var(args) ])(
Give a (short) listing of the remote directory.  With no var(args),
produces a raw list of the files in the directory, one per line.
Otherwise, up to vagaries of the server implementation, behaves
similar to tt(dir).
)
item(tt(type) [ var(type) ])(
Change the type for the transfer to var(type), or print the current type
if var(type) is absent.  The allowed values are `tt(A)' (ASCII),
`tt(I)' (Image, i.e. binary), or `tt(B)' (a synonym for `tt(I)').

The FTP default for a transfer is ASCII.  However, if tt(zftp) finds
that the remote host is a UNIX machine with 8-bit byes, it will
automatically switch to using binary for file transfers upon
tt(open).  This can subsequently be overridden.

The transfer type is only passed to the remote host when a data
connection is established; this command involves no network overhead.
)
item(tt(ascii))(
The same as tt(type A).
)
item(tt(binary))(
The same as tt(type I).
)
item(tt(mode) [ tt(S) | tt(B) ])(
Set the mode type to stream (tt(S)) or block (tt(B)).  Stream mode is
the default; block mode is not widely supported.
)
xitem(tt(remote) var(files...))
item(tt(local) [ var(files...) ])(
Print the size and last modification time of the remote or local
files.  If there is more than one item on the list, the name of the
file is printed first.  The first number is the file size, the second
is the last modification time of the file in the format
tt(CCYYMMDDhhmmSS) consisting of year, month, date, hour, minutes and
seconds in GMT.  Note that this format, including the length, is
guaranteed, so that time strings can be directly compared via the
tt([[) builtin's tt(<) and tt(>) operators, even if they are too long
to be represented as integers.

Not all servers support the commands for retrieving this information.
In that case, the tt(remote) command will print nothing and return
status 2, compared with status 1 for a file not found.

The tt(local) command (but not tt(remote)) may be used with no
arguments, in which case the information comes from examining file
descriptor zero.  This is the same file as seen by a tt(put) command
with no further redirection.
)
item(tt(get) var(file) [...])(
Retrieve all var(file)s from the server, concatenating them
and sending them to standard output.
)
item(tt(put) var(file) [...])(
For each var(file), read a file from standard input and send that to
the remote host with the given name.
)
item(tt(append) var(file) [...])(
As tt(put), but if the remote var(file) already exists, data is
appended to it instead of overwriting it.
)
xitem(tt(getat) var(file) var(point))
xitem(tt(putat) var(file) var(point))
item(tt(appendat) var(file) var(point))(
Versions of tt(get), tt(put) and tt(append) which will start the
transfer at the given var(point) in the remote var(file).  This is
useful for appending to an incomplete local file.  However, note that
this ability is not universally supported by servers (and is not quite
the behaviour specified by the standard).
)
item(tt(delete) var(file) [...])(
Delete the list of files on the server.
)
item(tt(mkdir) var(directory))(
Create a new directory var(directory) on the server.
)
item(tt(rmdir) var(directory))(
Delete the directory var(directory)  on the server.
)
item(tt(rename) var(old-name) var(new-name))(
Rename file var(old-name) to var(new-name) on the server.
)
item(tt(site) var(args...))(
Send a host-specific command to the server.  You will probably
only need this if instructed by the server to use it.
)
item(tt(quote) var(args...))(
Send the raw FTP command sequence to the server.  You should be
familiar with the FTP command set as defined in RFC959 before doing
this.  Useful commands may include tt(STAT) and tt(HELP).  Note also
the mechanism for returning messages as described for the variable
tt(ZFTP_VERBOSE) below, in particular that all messages from the
control connection are sent to standard error.
)
xitem(tt(close))
item(tt(quit))(
Close the current data connection.  This unsets the shell parameters
tt(ZFTP_HOST), tt(ZFTP_IP), tt(ZFTP_SYSTEM), tt(ZFTP_USER),
tt(ZFTP_ACCOUNT), tt(ZFTP_PWD), tt(ZFTP_TYPE) and tt(ZFTP_MODE).
)
item(tt(session) [ var(sessname) ])(
Allows multiple FTP sessions to be used at once.  The name of the session
is an arbitrary string of characters; the default session is called
`tt(default)'.  If this command is called without an argument, it will list
all the current sessions; with an argument, it will either switch to the
existing session called var(sessname), or create a new session of that name.

Each session remembers the status of the connection, the set of
connection-specific shell parameters (the same set as are unset when a
connection closes, as given in the description of tt(close)), and any user
parameters specified with the tt(params) subcommand.  Changing to a
previous session restores those values; changing to a new session
initialises them in the same way as if tt(zftp) had just been loaded.  The
name of the current session is given by the parameter tt(ZFTP_SESSION).
)
item(tt(rmsession) [ var(sessname) ])(
Delete a session; if a name is not given, the current session is deleted.
If the current session is deleted, the earliest existing session becomes
the new current session, otherwise the current session is not changed.
If the session being deleted is the only one, a new session called
`tt(default)' is created and becomes the current session; note that this is
a new session even if the session being deleted is also called
`tt(default)'. It is recommended that sessions not be deleted while
background commands which use tt(zftp) are still active.
)
enditem()

subsect(Parameters)
cindex(zftp, parameters)
The following shell parameters are used by tt(zftp).  Currently none
of them are special.

startitem()
vindex(ZFTP_TMOUT)
item(tt(ZFTP_TMOUT))(
Integer.  The time in seconds to wait for a network operation to
complete before returning an error.  If this is not set when the
module is loaded, it will be given the default value 60.  A value of
zero turns off timeouts.  If a timeout occurs on the control
connection it will be closed.  Use a larger value if this occurs too
frequently.
)
vindex(ZFTP_IP)
item(tt(ZFTP_IP))(
Readonly.  The IP address of the current connection in dot notation.
)
vindex(ZFTP_HOST)
item(tt(ZFTP_HOST))(
Readonly.  The hostname of the current remote server.  If the host was
opened as an IP number, tt(ZFTP_HOST) contains that instead; this
saves the overhead for a name lookup, as IP numbers are most commonly
used when a nameserver is unavailable.
)
vindex(ZFTP_SYSTEM)
item(tt(ZFTP_SYSTEM))(
Readonly.  The system type string returned by the server in response
to an FTP tt(SYST) request.  The most interesting case is a string
beginning tt("UNIX Type: L8"), which ensures maximum compatibility
with a local UNIX host.
)
vindex(ZFTP_TYPE)
item(tt(ZFTP_TYPE))(
Readonly.  The type to be used for data transfers , either `tt(A)' or
`tt(I)'.   Use the tt(type) subcommand to change this.
)
vindex(ZFTP_USER)
item(tt(ZFTP_USER))(
Readonly.  The username currently logged in, if any.
)
vindex(ZFTP_ACCOUNT)
item(tt(ZFTP_ACCOUNT))(
Readonly.  The account name of the current user, if any.  Most servers
do not require an account name.
)
vindex(ZFTP_PWD)
item(tt(ZFTP_PWD))(
Readonly.  The current directory on the server.
)
vindex(ZFTP_CODE)
item(tt(ZFTP_CODE))(
Readonly.  The three digit code of the last FTP reply from the server
as a string.  This can still be read after the connection is closed, and
is not changed when the current session changes.
)
vindex(ZFTP_REPLY)
item(tt(ZFTP_REPLY))(
Readonly.  The last line of the last reply sent by the server.  This
can still be read after the connection is closed, and is not changed when
the current session changes.
)
vindex(ZFTP_SESSION)
item(tt(ZFTP_SESSION))(
Readonly.  The name of the current FTP session; see the description of the
tt(session) subcommand.
)
vindex(ZFTP_PREFS)
item(tt(ZFTP_PREFS))(
A string of preferences for altering aspects of tt(zftp)'s behaviour.
Each preference is a single character.  The following are defined:

startitem()
item(tt(P))(
Passive:  attempt to make the remote server initiate data transfers.
This is slightly more efficient than sendport mode.  If the letter
tt(S) occurs later in the string, tt(zftp) will use sendport mode if
passive mode is not available.
)
item(tt(S))(
Sendport:  initiate transfers by the FTP tt(PORT) command.  If this
occurs before any tt(P) in the string, passive mode will never be
attempted.
)
item(tt(D))(
Dumb:  use only the bare minimum of FTP commands.  This prevents
the variables tt(ZFTP_SYSTEM) and tt(ZFTP_PWD) from being set, and
will mean all connections default to ASCII type.  It may prevent
tt(ZFTP_SIZE) from being set during a transfer if the server
does not send it anyway (many servers do).
)
enditem()

If tt(ZFTP_PREFS) is not set when tt(zftp) is loaded, it will be set to a
default of `tt(PS)', i.e. use passive mode if available, otherwise
fall back to sendport mode.
)
vindex(ZFTP_VERBOSE)
item(tt(ZFTP_VERBOSE))(
A string of digits between 0 and 5 inclusive, specifying which
responses from the server should be printed.  All responses go to
standard error.  If any of the numbers 1 to 5 appear in the string,
raw responses from the server with reply codes beginning with that
digit will be printed to standard error.  The first digit of the three
digit reply code is defined by RFC959 to correspond to:

startitem()
item(1.)(
A positive preliminary reply.
)
item(2.)(
A positive completion reply.
)
item(3.)(
A positive intermediate reply.
)
item(4.)(
A transient negative completion reply.
)
item(5.)(
A permanent negative completion reply.
)
enditem()

It should be noted that, for unknown reasons, the reply `Service not
available', which forces termination of a connection, is classified as
421, i.e. `transient negative', an interesting interpretation of the word
`transient'.

The code 0 is special:  it indicates that all but the last line of
multiline replies read from the server will be printed to standard
error in a processed format.  By convention, servers use this
mechanism for sending information for the user to read.  The
appropriate reply code, if it matches the same response, takes
priority.

If tt(ZFTP_VERBOSE) is not set when tt(zftp) is loaded, it will be
set to the default value tt(450), i.e., messages destined for the user
and all errors will be printed.  A null string is valid and
specifies that no messages should be printed.
)
enditem()

subsect(Functions)
cindex(zftp, functions)

startitem()
findex(zftp_chpwd, specification)
item(tt(zftp_chpwd))(
If this function is set by the user, it is called every time the
directory changes on the server, including when a user is logged
in, or when a connection is closed.  In the last case, tt($ZFTP_PWD)
will be unset; otherwise it will reflect the new directory.
)
findex(zftp_progress, specification)
item(tt(zftp_progress))(
If this function is set by the user, it will be called during
a tt(get), tt(put) or tt(append) operation each time sufficient data
has been received from the host.  During a tt(get), the data is sent
to standard output, so it is vital that this function should write
to standard error or directly to the terminal, em(not) to standard
output.

When it is called with a transfer in progress, the following
additional shell parameters are set:

startitem()
vindex(ZFTP_FILE)
item(tt(ZFTP_FILE))(
The name of the remote file being transferred from or to.
)
vindex(ZFTP_TRANSFER)
item(tt(ZFTP_TRANSFER))(
A tt(G) for a tt(get) operation and a tt(P) for a tt(put) operation.
)
vindex(ZFTP_SIZE)
item(tt(ZFTP_SIZE))(
The total size of the complete file being transferred:
the same as the first value provided by the
tt(remote) and tt(local) subcommands for a particular file.
If the server cannot supply this value for a remote file being
retrieved, it will not be set.  If input is from a pipe the value may
be incorrect and correspond simply to a full pipe buffer.
)
vindex(ZFTP_COUNT)
item(tt(ZFTP_COUNT))(
The amount of data so far transferred; a number between zero and
tt($ZFTP_SIZE), if that is set.  This number is always available.
)
enditem()

The function is initially called with tt(ZFTP_TRANSFER) set
appropriately and tt(ZFTP_COUNT) set to zero.  After the transfer is
finished, the function will be called one more time with
tt(ZFTP_TRANSFER) set to tt(GF) or tt(PF), in case it wishes to tidy
up.  It is otherwise never called twice with the same value of
tt(ZFTP_COUNT).

Sometimes the progress meter may cause disruption.  It is up to the
user to decide whether the function should be defined and to use
tt(unfunction) when necessary.
)
enditem()

subsect(Problems)
cindex(zftp, problems)

A connection may not be opened in the left hand side of a pipe as this
occurs in a subshell and the file information is not updated in the main
shell.  In the case of type or mode changes or closing the connection in a
subshell, the information is returned but variables are not updated until
the next call to tt(zftp).  Other status changes in subshells will not be
reflected by changes to the variables (but should be otherwise harmless).

Deleting sessions while a tt(zftp) command is active in the background can
have unexpected effects, even if it does not use the session being deleted.
This is because all shell subprocesses share information on the state of
all connections, and deleting a session changes the ordering of that
information.

On some operating systems, the control connection is not valid after a
fork(), so that operations in subshells, on the left hand side of a
pipeline, or in the background are not possible, as they should be.  This
is presumably a bug in the operating system.
