This describes the set of shell functions supplied with the source distribution as an interface to the zftp builtin command, allowing you to perform FTP operations from the shell command line or within functions or scripts. The interface is similar to a traditional FTP client (e.g. the ftp command itself, see man page ftp(1)), but as it is entirely done within the shell all the familiar completion, editing and globbing features, and so on, are present, and macros are particularly simple to write as they are just ordinary shell functions.
The prerequisite is that the zftp command, as described in section The zftp Module , must be available in the version of zsh installed at your site. If the shell is configured to load new commands at run time, it probably is: typing `zmodload zftp' will make sure (if that runs silently, it has worked). If this is not the case, it is possible zftp was linked into the shell anyway: to test this, type `which zftp' and if zftp is available you will get the message `zftp: shell built-in command'.
Commands given directly with zftp builtin may be interspersed between the functions in this suite; in a few cases, using zftp directly may cause some of the status information stored in shell parameters to become invalid. Note in particular the description of the variables $ZFTP_TMOUT, $ZFTP_PREFS and $ZFTP_VERBOSE for zftp.
You should make sure all the functions from the Functions/Zftp directory of the source distribution are available; they all begin with the two letters `zf'. They may already have been installed on your system; otherwise, you will need to find them and copy them. The directory should appear as one of the elements of the $fpath array (this should already be the case if they were installed), and at least the function zfinit should be autoloaded; it will autoload the rest. Finally, to initialize the use of the system you need to call the zfinit function. The following code in your .zshrc will arrange for this; assume the functions are stored in the directory ~/myfns:
fpath=(~/myfns $fpath) autoload zfinit zfinit
Note that zfinit assumes you are using the zmodload method to load the zftp command. If it is already built into the shell, change zfinit to zfinit -n. It is helpful (though not essential) if the call to zfinit appears after any code to initialize the new completion system, else unnecessary compctl commands will be given.
The sequence of operations in performing a file transfer is essentially the same as that in a standard FTP client.
As zfopen calls zfparams to store the parameters, this usually need not be called directly.
If the command was successful, and the terminal is an xterm, a summary will appear in the title bar, giving the local host:directory and the remote host:directory; this is handled by the function zftp_chpwd, described below.
Normally, the host, user and password are internally recorded for later re-opening, either by a zfopen with no arguments, or automatically (see below). With the option `-1', no information is stored.
Both zfopen and zfanon (but not zfparams) understand URLs of the form ftp://host/path... as meaning to connect to the host, then change directory to path (which must be a directory, not a file). The `ftp://' can be omitted; the trailing `/' is enough to trigger recognition of the path. Note prefixes other than `ftp:' are not recognized, and that all characters after the first slash beyond host are significant in path.
In the first form with dir present, change to the directory dir. The command `zfcd ..' is treated specially, so is guaranteed to work on non-UNIX servers (note this is handled internally by zftp). If dir is omitted, has the effect of `zfcd ~'.
The second form changes to the directory previously current.
The third form attempts to change the current directory by replacing the first occurrence of the string old with the string new in the current directory.
Note that in this command, and indeed anywhere a remote filename is expected, the string which on the local host corresponds to `~' is converted back to a `~' before being passed to the remote machine. This is convenient because of the way expansion is performed on the command line before zfcd receives a string. For example, suppose the command is `zfcd ~/foo'. The shell will expand this to a full path as in `zfcd /home/user2/pws/foo'. At this stage, zfcd recognises the initial path as `~', and the directory sent to the remote host is ~/foo, so that the `~' will be expanded by the server to the correct remote host directory. Other named directories of the form `~name' are not treated in this fashion.
The directory is usually cached for re-use. In fact, two caches are maintained. One is for use when there is no dir-options or dir, i.e. a full listing of the current remote directory; it is flushed when the current remote directory changes. The other is kept for repeated use of zfdir with the same arguments; for example, repeated use of zfdir /pub/gnu will only require the directory to be retrieved on the first call. Alternatively, this cache can be re-viewed with the -r option. As relative directories will confuse zfdir, the -f option can be used to force the cache to be flushed. Also, the option -d will delete both caches without showing a directory listing.
The commands for retrieving files all take at least two options. -G suppresses remote filename expansion which would otherwise be performed (see below for a more detailed description of that). -t attempts to set the modification time of the local file to that of the remote file: this requires version 5 of perl, see the description of the function zfrtime below for more information.
Note that this requires a commonly implemented, but non-standard, version of the FTP protocol, so is not guaranteed to work on all servers.
In the first form, copy remote-file from the server to the local file local-file.
In the second form, copy all the remote files rfile1 ... into the local directory ldir retaining the same basenames. This assumes UNIX directory semantics.
With two arguments, copy local-file to the server as remote-file.
With more than two arguments, copy all the local files lfile1 ... into the existing remote directory rdir retaining the same basenames. This assumes UNIX directory semantics.
A problem arises if you attempt to use zfpcp lfile1 rdir, i.e. the second form of copying but with two arguments, as the command has no simple way of knowing if rdir corresponds to a directory or a filename. It attempts to resolve this in various ways. First, if the rdir argument is `.' or `..' or ends in a slash, it is assumed to be a directory. Secondly, if the operation of copying to a remote file in the first form failed, and the remote server sends back the expected failure code 553 and a reply including the string `Is a directory', then zfpcp will retry using the second form.
The two functions zfmark and zfgoto allow you to `bookmark' the present location (host, user and directory) of the current FTP connection for later use. The file to be used for storing and retrieving bookmarks is given by the parameter $ZFTP_BMFILE; if not set when one of the two functions is called, it will be set to the file .zfbfmarks in the directory where your zsh startup files live (usually ~).
If not given an argument, list the existing bookmarks and the points to which they refer in the form user@host:directory.
With the option -n, the bookmark is taken to be a nickname stored by the ncftp program in its bookmark file, which is assumed to be ~/.ncftp/bookmarks. The function works identically in other ways. Note that there is no mechanism for adding or modifying ncftp bookmarks from the zftp functions.
Mostly, these functions will not be called directly (apart from zfinit), but are described here for completeness. You may wish to alter zftp_chpwd and zftp_progress, in particular.
Currently this requires perl version 5 to perform the conversion from GMT to local time. This is unfortunately difficult to do using shell code alone.
chpwd() { if [[ -n $ZFTP_USER ]]; then zftp_chpwd else # usual chpwd e.g put host:directory in title bar fi }
The commands for retrieving files usually perform filename expansion (globbing) on their arguments; this can be turned off by passing the option -G to each of the commands. Normally this operates by retrieving a complete list of files for the directory in question, then matching these locally against the pattern supplied. This has the advantage that the full range of zsh patterns (respecting the setting of the option EXTENDED_GLOB) can be used. However, it means that the directory part of a filename will not be expanded and must be given exactly. If the remote server does not support the UNIX directory semantics, directory handling is problematic and it is recommended that globbing only be used within the current directory. The list of files in the current directory, if retrieved, will be cached, so that subsequent globs in the same directory without an intervening zfcd are fast.
If the variable $zfrglob is set to a non-zero length, globbing is instead performed on the remote host: the server is asked for a list of matching files. This is highly dependent on how the server is implemented, though typically UNIX servers will provide support for basic glob patterns. This may in some cases be faster, as it avoids retrieving the entire list of directory contents.
As described for the zfopen command, a subsequent zfopen with no parameters will reopen the connection to the last host (this includes connections made with the zfanon command). Opened in this fashion, the connection starts in the default remote directory and will remain open until explicitly closed.
Automatic re-opening is also available. If a connection is not currently open and a command requiring a connection is given, the last connection is implicitly reopened. In this case the directory which was current when the connection was closed again becomes the current directory (unless, of course, the command given changes it). Automatic reopening will also take place if the connection was close by the remote server for whatever reason (e.g. a timeout). It is not available if the -1 option to zfopen or zfanon was used.
Furthermore, if the command issued is a file transfer, the connection will be closed after the transfer is finished, hence providing a one-shot mode for transfers. This does not apply to directory changing or listing commands; for example a zfdir may reopen a connection but will leave it open. Also, automatic closure will only ever happen in the same command as automatic opening, i.e a zfdir directly followed by a zfget will never close the connection automatically.
Information about the previous connection is given by the zfstat function. So, for example, if that reports:
Not connected. Last session: ftp.bar.com:/pub/textfiles
then the command zfget file.txt will attempt to reopen a connection to ftp.bar.com, retrieve the file /pub/textfiles/file.txt, and immediately close the connection again. On the other hand, zfcd .. will open the connection in the directory /pub and leave it open.
Completion of remote files, directories and bookmarks is supported. The older, compctl-style completion is defined when zfinit is called; support for the new widget-based completion system is provided in the function Completion/Builtins/_zftp, which should be installed with the other functions of the completion system and hence should automatically be available.