# zshtcpsys(1) - man - phpMan

[ZSHTCPSYS(1)](https://www.chedong.com/phpMan.php/man/ZSHTCPSYS/1/markdown)                           General Commands Manual                          [ZSHTCPSYS(1)](https://www.chedong.com/phpMan.php/man/ZSHTCPSYS/1/markdown)



## NAME
       zshtcpsys - zsh tcp system

## DESCRIPTION
       A  module  **zsh/net/tcp**  is provided to provide network I/O over TCP/IP from within the shell;
       see its description in [_zshmodules_(1)](https://www.chedong.com/phpMan.php/man/zshmodules/1/markdown).  This manual page describes a function suite  based  on
       the  module.   If  the  module  is installed, the functions are usually installed at the same
       time, in which case they will be available for autoloading in  the  default  function  search
       path.   In  addition  to  the **zsh/net/tcp** module, the **zsh/zselect** module is used to implement
       timeouts on read operations.  For troubleshooting tips, consult the corresponding advice  for
       the **zftp** functions described in [_zshzftpsys_(1)](https://www.chedong.com/phpMan.php/man/zshzftpsys/1/markdown).

       There  are  functions  corresponding  to the basic I/O operations open, close, read and send,
       named **tcp**___**open** etc., as well as a function **tcp**___**expect** for pattern match analysis of data read
       as input.  The system makes it easy to receive data from and send data to multiple named ses‐
       sions at once.  In addition, it can be linked with the shell's line editor in such a way that
       input data is automatically shown at the terminal.  Other facilities available including log‐
       ging, filtering and configurable output prompts.

       To use the system where it is available, it should be enough to `**autoload**  **-U**  **tcp**___**open**'  and
       run **tcp**___**open** as documented below to start a session.  The **tcp**___**open** function will autoload the
       remaining functions.

## TCP USER FUNCTIONS
### Basic I/O
       **tcp**___**open** [ **-qz** ] _host_ _port_ [ _sess_ ]
       **tcp**___**open** [ **-qz** ] [ **-s** _sess_ | **-l** _sess_[**,**...] ] ...
       **tcp**___**open** [ **-qz** ] [ **-a** _fd_ | **-f** _fd_ ] [ _sess_ ]
              Open a new session.  In the first and simplest form, open a  TCP  connection  to  host
              _host_ at port _port_; numeric and symbolic forms are understood for both.

              If  _sess_  is given, this becomes the name of the session which can be used to refer to
              multiple different TCP connections.  If _sess_ is not given, the function will invent  a
              numeric name value (note this is _not_ the same as the file descriptor to which the ses‐
              sion is attached).  It is recommended that session names not include  `funny'  charac‐
              ters,  where  funny  characters  are not well-defined but certainly do not include al‐
              phanumerics or underscores, and certainly do include whitespace.

              In the second case, one or more sessions to be opened are given  by  name.   A  single
              session  name  is given after **-s** and a comma-separated list after **-l**; both options may
              be repeated as many times as necessary.  A failure to open any session causes **tcp**___**open**
              to  abort.  The host and port are read from the file **.ztcp**___**sessions** in the same direc‐
              tory as the user's zsh initialisation files, i.e.  usually  the  home  directory,  but
              **$ZDOTDIR**  if  that  is set.  The file consists of lines each giving a session name and
              the corresponding host and port, in that order (note the session name comes first, not
              last), separated by whitespace.

              The third form allows passive and fake TCP connections.  If the option **-a** is used, its
              argument is a file  descriptor  open  for  listening  for  connections.   No  function
              front-end  is  provided  to  open such a file descriptor, but a call to `**ztcp** **-l** _port_'
              will create one with the file descriptor stored in the parameter **$REPLY**.  The  listen‐
              ing port can be closed with `**ztcp** **-c** _fd_'.  A call to `**tcp**___**open** **-a** _fd_' will block until
              a remote TCP connection is made to _port_ on the local machine.  At this point,  a  ses‐
              sion  is created in the usual way and is largely indistinguishable from an active con‐
              nection created with one of the first two forms.

              If the option **-f** is used, its argument is a file descriptor which is used directly  as
              if  it  were  a  TCP session.  How well the remainder of the TCP function system copes
              with this depends on what actually underlies this file descriptor.  A regular file  is
              likely  to  be unusable; a FIFO (pipe) of some sort will work better, but note that it
              is not a good idea for two different sessions to attempt to read from the same FIFO at
              once.

              If  the option **-q** is given with any of the three forms, **tcp**___**open** will not print infor‐
              mational messages, although it will in any case exit with an appropriate status.

              If the line editor (zle) is in use, which is typically the case if the shell is inter‐
              active,  **tcp**___**open**  installs  a handler inside zle which will check for new data at the
              same time as it checks for keyboard input.  This is convenient as the  shell  consumes
              no  CPU time while waiting; the test is performed by the operating system.  Giving the
              option **-z** to any of the forms of **tcp**___**open** prevents the handler from  being  installed,
              so  data  must be read explicitly.  Note, however, this is not necessary for executing
              complete sets of send and read commands from a function, as zle is not active at  this
              point.   Generally  speaking, the handler is only active when the shell is waiting for
              input at a command prompt or in the **vared** builtin.  The option has no effect if zle is
              not active; `**[[** **-o** **zle]]**' will test for this.

              The  first  session  to  be opened becomes the current session and subsequent calls to
              **tcp**___**open** do not change it.  The current session is stored in the parameter  **$TCP**___**SESS**;
              see below for more detail about the parameters used by the system.

              The function **tcp**___**on**___**open**, if defined, is called when a session is opened.  See the de‐
              scription below.

       **tcp**___**close** [ **-qn** ] [ **-a** | **-l** _sess_[**,**...] | _sess_ ... ]
              Close the named sessions, or the current session if none is given, or  all  open  ses‐
              sions  if  **-a**  is  given.  The options **-l** and **-s** are both handled for consistency with
              **tcp**___**open**, although the latter is redundant.

              If the session being closed is the current one, **$TCP**___**SESS** is unset, leaving no current
              session, even if there are other sessions still open.

              If  the  session was opened with **tcp**___**open** **-f**, the file descriptor is closed so long as
              it is in the range 0 to 9 accessible directly from the command line.  If the option **-n**
              is  given, no attempt will be made to close file descriptors in this case.  The **-n** op‐
              tion is not used for genuine **ztcp** session; the file descriptors are always closed with
              the session.

              If the option **-q** is given, no informational messages will be printed.


       **tcp**___**read** [ **-bdq** ] [ **-t** _TO_ ] [ **-T** _TO_ ]
                [ **-a** | **-u** _fd_[**,**...] | **-l** _sess_[**,**...] | **-s** _sess_ ... ]
              Perform  a  read operation on the current session, or on a list of sessions if any are
              given with **-u**, **-l** or **-s**, or all open sessions if the option **-a** is given.  Any  of  the
              **-u**,  **-l**  or  **-s**  options may be repeated or mixed together.  The **-u** option specifies a
              file descriptor directly (only those managed by this system are useful), the other two
              specify sessions as described for **tcp**___**open** above.

              The  function checks for new data available on all the sessions listed.  Unless the **-b**
              option is given, it will not block waiting for new data.  Any one line  of  data  from
              any  of  the  available  sessions will be read, stored in the parameter **$TCP**___**LINE**, and
              displayed to standard output unless **$TCP**___**SILENT** contains  a  non-empty  string.   When
              printed  to  standard  output the string **$TCP**___**PROMPT** will be shown at the start of the
              line; the default form for this includes the name of the session being read.  See  be‐
              low  for  more  information on these parameters.  In this mode, **tcp**___**read** can be called
              repeatedly until it returns status 2 which indicates all pending input from all speci‐
              fied sessions has been handled.

              With the option **-b**, equivalent to an infinite timeout, the function will block until a
              line is available to read from one of the specified sessions.  However, only a  single
              line is returned.

              The  option  **-d**  indicates  that  all  pending  input should be drained.  In this case
              **tcp**___**read** may process multiple lines in the manner given above; only the last is stored
              in **$TCP**___**LINE**, but the complete set is stored in the array **$tcp**___**lines**.  This is cleared
              at the start of each call to **tcp**___**read**.

              The options **-t** and **-T** specify a timeout in seconds, which may be a floating point num‐
              ber  for  increased  accuracy.   With **-t** the timeout is applied before each line read.
              With **-T**, the timeout applies to the overall  operation,  possibly  including  multiple
              read operations if the option **-d** is present; without this option, there is no distinc‐
              tion between **-t** and **-T**.

              The function does not print informational messages, but if the option **-q** is given,  no
              error message is printed for a non-existent session.

              A  return  status of 2 indicates a timeout or no data to read.  Any other non-zero re‐
              turn status indicates some error condition.

              See **tcp**___**log** for how to control where data is sent by **tcp**___**read**.

       **tcp**___**send** [ **-cnq** ] [ **-s** _sess_ | **-l** _sess_[**,**...] ] _data_ ...
       **tcp**___**send** [ **-cnq** ] **-a** _data_ ...
              Send the supplied data strings to all the specified sessions in turn.  The  underlying
              operation  differs little from a `**print** **-r**' to the session's file descriptor, although
              it attempts to prevent the shell from dying owing to a **SIGPIPE** caused by an attempt to
              write to a defunct session.

              The  option **-c** causes **tcp**___**send** to behave like **cat**.  It reads lines from standard input
              until end of input and sends them in turn to the specified session(s)  exactly  as  if
              they were given as _data_ arguments to individual **tcp**___**send** commands.

              The option **-n** prevents **tcp**___**send** from putting a newline at the end of the data strings.

              The remaining options all behave as for **tcp**___**read**.

              The  data  arguments are not further processed once they have been passed to **tcp**___**send**;
              they are simply passed down to **print** **-r**.

              If the parameter **$TCP**___**OUTPUT** is a non-empty string and logging  is  enabled  then  the
              data  sent to each session will be echoed to the log file(s) with **$TCP**___**OUTPUT** in front
              where appropriate, much in the manner of **$TCP**___**PROMPT**.

### Session Management
       **tcp**___**alias** [ **-q** ] _alias_**=**_sess_ ...
       **tcp**___**alias** [ **-q** ] [ _alias_ ... ]
       **tcp**___**alias** **-d** [ **-q** ] _alias_ ...
              This function is not particularly well tested.

              The first form creates an alias for a session name; _alias_ can then be used to refer to
              the existing session _sess_.  As many aliases may be listed as required.

              The second form lists any aliases specified, or all aliases if none.

              The  third  form  deletes all the aliases listed.  The underlying sessions are not af‐
              fected.

              The option **-q** suppresses an inconsistently chosen subset of error messages.

       **tcp**___**log** [ **-asc** ] [ **-n** | **-N** ] [ _logfile_ ]
              With an argument _logfile_, all future input from **tcp**___**read** will be logged to  the  named
              file.   Unless  **-a**  (append)  is  given,  this file will first be truncated or created
              empty.  With no arguments, show the current status of logging.

              With the option **-s**, per-session logging is enabled.  Input from **tcp**___**read** is output  to
              the file _logfile_**.**_sess_.  As the session is automatically discriminated by the filename,
              the contents are raw (no **$TCP**___**PROMPT**).  The option  **-a** applies as above.   Per-session
              logging and logging of all data in one file are not mutually exclusive.

              The option **-c** closes all logging, both complete and per-session logs.

              The options **-n** and **-N** respectively turn off or restore output of data read by **tcp**___**read**
              to standard output; hence `**tcp**___**log** **-cn**' turns off all output by **tcp**___**read**.

              The function is purely a convenient front end  to  setting  the  parameters  **$TCP**___**LOG**,
              **$TCP**___**LOG**___**SESS**, **$TCP**___**SILENT**, which are described below.

       **tcp**___**rename** _old_ _new_
              Rename session _old_ to session _new_.  The old name becomes invalid.

       **tcp**___**sess** [ _sess_ [ _command_ [ _arg_ ... ] ] ]
              With  no  arguments,  list all the open sessions and associated file descriptors.  The
              current session is marked with a star.  For use in functions, direct access to the pa‐
              rameters  **$tcp**___**by**___**name**,  **$tcp**___**by**___**fd** and **$TCP**___**SESS** is probably more convenient; see be‐
              low.

              With a _sess_ argument, set the current session to _sess_.  This is equivalent to changing
              **$TCP**___**SESS** directly.

              With  additional  arguments, temporarily set the current session while executing `_com__‐
              _mand_ _arg_ ...'.  _command_ is re-evaluated so as to expand aliases etc., but the  remain‐
              ing  _arg_s  are passed through as that appear to **tcp**___**sess**.  The original session is re‐
              stored when **tcp**___**sess** exits.

### Advanced I/O
       **tcp**___**command** _send-option_ ... _send-argument_ ...
              This is a convenient front-end to **tcp**___**send**.  All arguments  are  passed  to  **tcp**___**send**,
              then  the  function  pauses  waiting  for data.  While data is arriving at least every
              **$TCP**___**TIMEOUT** (default 0.3) seconds, data is handled and printed out according  to  the
              current settings.  Status 0 is always returned.

              This  is  generally  only  useful for interactive use, to prevent the display becoming
              fragmented by output returned from the connection.  Within a programme or function  it
              is generally better to handle reading data by a more explicit method.


       **tcp**___**expect** [ **-q** ] [ **-p** _var_ | **-P** _var_ ] [ **-t** _TO_ | **-T** _TO_ ]
                  [ **-a** | **-s** _sess_ | **-l** _sess_[**,**...] ] _pattern_ ...
              Wait  for input matching any of the given _pattern_s from any of the specified sessions.
              Input is ignored until an input line matches one of the given patterns; at this  point
              status zero is returned, the matching line is stored in **$TCP**___**LINE**, and the full set of
              lines read during the call to **tcp**___**expect** is stored in the array **$tcp**___**expect**___**lines**.

              Sessions are specified in the same way as **tcp**___**read**: the default is to use the  current
              session, otherwise the sessions specified by **-a**, **-s**, or **-l** are used.

              Each  _pattern_  is  a  standard zsh extended-globbing pattern; note that it needs to be
              quoted to avoid it being expanded immediately by filename generation.  It  must  match
              the  full line, so to match a substring there must be a `*****' at the start and end.  The
              line matched against includes the **$TCP**___**PROMPT** added by **tcp**___**read**.  It  is  possible  to
              include  the globbing flags `**#b**' or `**#m**' in the patterns to make backreferences avail‐
              able in the parameters **$MATCH**, **$match**, etc., as described in the base  zsh  documenta‐
              tion on pattern matching.

              Unlike  **tcp**___**read**,  the  default behaviour of **tcp**___**expect** is to block indefinitely until
              the required input is found.  This can be modified by specifying a timeout with **-t**  or
              **-T**;  these  function as in **tcp**___**read**, specifying a per-read or overall timeout, respec‐
              tively, in seconds, as an integer or floating-point number.  As **tcp**___**read**, the function
              returns status 2 if a timeout occurs.

              The  function  returns  as soon as any one of the patterns given match.  If the caller
              needs to know which of the patterns matched, the option **-p** _var_ can be used; on return,
              **$var**  is  set to the number of the pattern using ordinary zsh indexing, i.e. the first
              is 1, and so on.  Note the absence of a `**$**' in front of _var_.  To  avoid  clashes,  the
              parameter cannot begin with `___**expect**'.  The index -1 is used if there is a timeout and
              0 if there is no match.

              The option **-P** _var_ works similarly to **-p**, but instead of numerical indexes the  regular
              arguments  must begin with a prefix followed by a colon: that prefix is then used as a
              tag to which _var_ is set when the argument matches.  The tag **timeout** is used  if  there
              is  a  timeout  and  the empty string if there is no match.  Note it is acceptable for
              different arguments to start with the same prefix if the matches do  not  need  to  be
              distinguished.

              The option **-q** is passed directly down to **tcp**___**read**.

              As  all input is done via **tcp**___**read**, all the usual rules about output of lines read ap‐
              ply.  One exception is that the parameter **$tcp**___**lines** will only reflect the line  actu‐
              ally  matched by **tcp**___**expect**; use **$tcp**___**expect**___**lines** for the full set of lines read dur‐
              ing the function call.

       **tcp**___**proxy**
              This is a simple-minded function to accept a TCP connection and execute a command with
              I/O  redirected to the connection.  Extreme caution should be taken as there is no se‐
              curity whatsoever and this can leave your computer open to  the  world.   Ideally,  it
              should only be used behind a firewall.

              The first argument is a TCP port on which the function will listen.

              The  remaining arguments give a command and its arguments to execute with standard in‐
              put, standard output and standard error redirected to the file descriptor on which the
              TCP  session  has  been accepted.  If no command is given, a new zsh is started.  This
              gives everyone on your network direct access to your account, which in many cases will
              be a bad thing.

              The  command  is  run in the background, so **tcp**___**proxy** can then accept new connections.
              It continues to accept new connections until interrupted.

       **tcp**___**spam** [ **-ertv** ] [ **-a** | **-s** _sess_ | **-l** _sess_[**,**...] ] _cmd_ [ _arg_ ... ]
              Execute `_cmd_ [ _arg_ ... ]' for each session in turn.  Note this  executes  the  command
              and  arguments; it does not send the command line as data unless the **-t** (transmit) op‐
              tion is given.

              The sessions may be selected explicitly with the standard **-a**, **-s** or **-l** options, or may
              be  chosen implicitly.  If none of the three options is given the rules are: first, if
              the array **$tcp**___**spam**___**list** is set, this is taken as the list of sessions, otherwise  all
              sessions are taken.  Second, any sessions given in the array **$tcp**___**no**___**spam**___**list** are re‐
              moved from the list of sessions.

              Normally, any sessions added by the `**-a**' flag or when all sessions are chosen  implic‐
              itly are spammed in alphabetic order; sessions given by the **$tcp**___**spam**___**list** array or on
              the command line are spammed in the order given.  The **-r** flag reverses the order  how‐
              ever it was arrived it.

              The  **-v** flag specifies that a **$TCP**___**PROMPT** will be output before each session.  This is
              output after any modification to **TCP**___**SESS** by the user-defined **tcp**___**on**___**spam** function de‐
              scribed below.  (Obviously that function is able to generate its own output.)

              If  the  option  **-e**  is present, the line given as `_cmd_ [ _arg_ ... ]' is executed using
              **eval**, otherwise it is executed without any further processing.

       **tcp**___**talk**
              This is a fairly simple-minded attempt to  force  input  to  the  line  editor  to  go
              straight to the default **TCP**___**SESS**.

              An  escape  string,  **$TCP**___**TALK**___**ESCAPE**,  default `**:**', is used to allow access to normal
              shell operation.  If it is on its own at the start of the line, or  followed  only  by
              whitespace,  the  line  editor returns to normal operation.  Otherwise, the string and
              any following whitespace are skipped and the remainder of the line executed  as  shell
              input without any change of the line editor's operating mode.

              The  current  implementation is somewhat deficient in terms of use of the command his‐
              tory.  For this reason, many users will prefer to use some  form  of  alternative  ap‐
              proach  for  sending  data  easily  to the current session.  One simple approach is to
              alias some special character (such as `**%**') to `**tcp**___**command** **--**'.

       **tcp**___**wait**
              The sole argument is an integer or floating point number which gives  the  seconds  to
              delay.   The  shell  will  do nothing for that period except wait for input on all TCP
              sessions by calling **tcp**___**read** **-a**.  This is similar to the interactive behaviour at  the
              command prompt when zle handlers are installed.

### `One-shot' file transfer
       **tcp**___**point** _port_
       **tcp**___**shoot** _host_ _port_
              This  pair  of  functions  provide  a  simple way to transfer a file between two hosts
              within the shell.  Note, however, that bulk data transfer is currently done using **cat**.
              **tcp**___**point**  reads  any data arriving at _port_ and sends it to standard output; **tcp**___**shoot**
              connects to _port_ on _host_ and sends its standard input.  Any unused _port_ may  be  used;
              the  standard  mechanism  for picking a port is to think of a random four-digit number
              above 1024 until one works.

              To transfer a file from host **woodcock** to host **springes**, on **springes**:

                     **tcp**___**point** **8091** **>output**___**file**

              and on **woodcock**:

                     **tcp**___**shoot** **springes** **8091** **<input**___**file**

              As these two functions do not require **tcp**___**open** to set up a TCP connection first,  they
              may need to be autoloaded separately.

## TCP USER-DEFINED FUNCTIONS
       Certain  functions,  if defined by the user, will be called by the function system in certain
       contexts.  This facility depends on the module **zsh/parameter**, which is usually  available  in
       interactive shells as the completion system depends on it.  None of the functions need be de‐
       fined; they simply provide convenient hooks when necessary.

       Typically, these are called after the requested action has been taken, so  that  the  various
       parameters will reflect the new state.

       **tcp**___**on**___**alias** _alias_ _fd_
              When an alias is defined, this function will be called with two arguments: the name of
              the alias, and the file descriptor of the corresponding session.

       **tcp**___**on**___**awol** _sess_ _fd_
              If the function **tcp**___**fd**___**handler** is handling input from the line editor and detects that
              the  file  descriptor is no longer reusable, by default it removes it from the list of
              file descriptors handled by this  method  and  prints  a  message.   If  the  function
              **tcp**___**on**___**awol** is defined it is called immediately before this point.  It may return sta‐
              tus 100, which indicates that the normal handling should still be performed; any other
              return  status indicates that no further action should be taken and the **tcp**___**fd**___**handler**
              should return immediately with the given status.  Typically the action of  **tcp**___**on**___**awol**
              will be to close the session.

              The  variable  **TCP**___**INVALIDATE**___**ZLE** will be a non-empty string if it is necessary to in‐
              validate the line editor display using `**zle** **-I**' before printing output from the  func‐
              tion.

              (`AWOL'  is  military  jargon for `absent without leave' or some variation.  It has no
              pre-existing technical meaning known to the author.)

       **tcp**___**on**___**close** _sess_ _fd_
              This is called with the name of a session being closed and the file  descriptor  which
              corresponded  to  that  session.   Both  will  be  invalid by the time the function is
              called.

       **tcp**___**on**___**open** _sess_ _fd_
              This is called after a new session has been defined with the session name and file de‐
              scriptor  as  arguments.   If it returns a non-zero status, opening the session is as‐
              sumed to fail and the session is closed again; however, **tcp**___**open** will continue to  at‐
              tempt to open any remaining sessions given on the command line.

       **tcp**___**on**___**rename** _oldsess_ _fd_ _newsess_
              This  is  called after a session has been renamed with the three arguments old session
              name, file descriptor, new session name.

       **tcp**___**on**___**spam** _sess_ _command_ _..._
              This is called once for each session spammed, just _before_ a command is executed for  a
              session  by **tcp**___**spam**.  The arguments are the session name followed by the command list
              to be executed.  If **tcp**___**spam** was called with the option **-t**, the first command will  be
              **tcp**___**send**.

              This  function  is called after **$TCP**___**SESS** is set to reflect the session to be spammed,
              but before any use of it is made.   Hence  it  is  possible  to  alter  the  value  of
              **$TCP**___**SESS**  within this function.  For example, the session arguments to **tcp**___**spam** could
              include extra information to be stripped off and processed in **tcp**___**on**___**spam**.

              If the function sets the parameter **$REPLY** to `**done**', the command line is not executed;
              in addition, no prompt is printed for the **-v** option to **tcp**___**spam**.

       **tcp**___**on**___**unalias** _alias_ _fd_
              This is called with the name of an alias and the corresponding session's file descrip‐
              tor after an alias has been deleted.

## TCP UTILITY FUNCTIONS
       The following functions are used by the TCP function system but will rarely if ever  need  to
       be called directly.

       **tcp**___**fd**___**handler**
              This is the function installed by **tcp**___**open** for handling input from within the line ed‐
              itor, if that is required.  It is in the format documented for the builtin `**zle** **-F**' in
              [_zshzle_(1)](https://www.chedong.com/phpMan.php/man/zshzle/1/markdown) .

              While  active,  the  function sets the parameter **TCP**___**HANDLER**___**ACTIVE** to 1.  This allows
              shell code called internally (for example, by setting **tcp**___**on**___**read**) to tell if is being
              called when the shell is otherwise idle at the editor prompt.

       **tcp**___**output** [ **-q** ] **-P** _prompt_ **-F** _fd_ **-S** _sess_
              This  function  is  used for both logging and handling output to standard output, from
              within **tcp**___**read** and (if **$TCP**___**OUTPUT** is set) **tcp**___**send**.

              The _prompt_ to use is specified by **-P**; the default is the empty string.   It  can  con‐
              tain:
              **%c**     Expands  to  1  if  the session is the current session, otherwise 0.  Used with
                     ternary expressions such as `**%(c.-.+)**' to output `**+**' for  the  current  session
                     and `**-**' otherwise.

              **%f**     Replaced by the session's file descriptor.

              **%s**     Replaced by the session name.

              **%%**     Replaced by a single `**%**'.

              The option **-q** suppresses output to standard output, but not to any log files which are
              configured.

              The **-S** and **-F** options are used to pass in the session name  and  file  descriptor  for
              possible replacement in the prompt.

## TCP USER PARAMETERS
       Parameters follow the usual convention that uppercase is used for scalars and integers, while
       lowercase is used for normal and associative array.  It is always safe for user code to  read
       these  parameters.   Some parameters may also be set; these are noted explicitly.  Others are
       included in this group as they are set by the function system for the  user's  benefit,  i.e.
       setting them is typically not useful but is benign.

       It is often also useful to make settable parameters local to a function.  For example, `**local**
       **TCP**___**SILENT=1**' specifies that data read during the function call will not be printed to  stan‐
       dard output, regardless of the setting outside the function.  Likewise, `**local** **TCP**___**SESS=**_sess_'
       sets a session for the duration of a function, and  `**local**  **TCP**___**PROMPT=**'  specifies  that  no
       prompt is used for input during the function.

       **tcp**___**expect**___**lines**
              Array.   The  set of lines read during the last call to **tcp**___**expect**, including the last
              (**$TCP**___**LINE**).

       **tcp**___**filter**
              Array. May be set directly.  A set of extended globbing patterns which, if matched  in
              **tcp**___**output**,  will  cause  the line not to be printed to standard output.  The patterns
              should be defined as described for the arguments to **tcp**___**expect**.  Output of line to log
              files is not affected.

       **TCP**___**HANDLER**___**ACTIVE**
              Scalar.   Set  to  1 within **tcp**___**fd**___**handler** to indicate to functions called recursively
              that they have been called during an editor session.  Otherwise unset.

       **TCP**___**LINE**
              The last line read by **tcp**___**read**, and hence also **tcp**___**expect**.

       **TCP**___**LINE**___**FD**
              The file descriptor from which **$TCP**___**LINE** was  read.   **${tcp**___**by**___**fd[$TCP**___**LINE**___**FD]}**  will
              give the corresponding session name.

       **tcp**___**lines**
              Array.  The  set  of  lines  read during the last call to **tcp**___**read**, including the last
              (**$TCP**___**LINE**).

       **TCP**___**LOG**
              May be set directly, although it is also controlled by **tcp**___**log**.  The name of a file to
              which  output  from  all  sessions will be sent.  The output is proceeded by the usual
              **$TCP**___**PROMPT**.  If it is not an absolute path name, it will follow  the  user's  current
              directory.

       **TCP**___**LOG**___**SESS**
              May  be set directly, although it is also controlled by **tcp**___**log**.  The prefix for a set
              of files to which output from each session separately will be sent; the full  filename
              is  **${TCP**___**LOG**___**SESS}.**_sess_.   Output  to each file is raw; no prompt is added.  If it is
              not an absolute path name, it will follow the user's current directory.

       **tcp**___**no**___**spam**___**list**
              Array.  May be set directly.  See **tcp**___**spam** for how this is used.

       **TCP**___**OUTPUT**
              May be set directly.  If a non-empty string, any data sent to a  session  by  **tcp**___**send**
              will  be  logged.   This  parameter gives the prompt to be used in a file specified by
              **$TCP**___**LOG** but not in a file generated from **$TCP**___**LOG**___**SESS**.  The prompt  string  has  the
              same format as **TCP**___**PROMPT** and the same rules for its use apply.

       **TCP**___**PROMPT**
              May be set directly.  Used as the prefix for data read by **tcp**___**read** which is printed to
              standard output or to the log file given by **$TCP**___**LOG**, if any.  Any `**%s**', `**%f**' or  `**%%**'
              occurring in the string will be replaced by the name of the session, the session's un‐
              derlying file descriptor, or a single `**%**', respectively.  The expression `**%c**'  expands
              to  1 if the session being read is the current session, else 0; this is most useful in
              ternary expressions such as `**%(c.-.+)**' which outputs `**+**' if the session is the current
              one, else `**-**'.

              If the prompt starts with **%P**, this is stripped and the complete result of the previous
              stage is passed through standard prompt **%**-style formatting before being output.

       **TCP**___**READ**___**DEBUG**
              May be set directly.  If this has non-zero length, **tcp**___**read** will give some limited di‐
              agnostics about data being read.

       **TCP**___**SECONDS**___**START**
              This value is created and initialised to zero by tcp_open.

              The  functions **tcp**___**read** and **tcp**___**expect** use the shell's **SECONDS** parameter for their own
              timing purposes.  If that parameter is not of floating point type on entry to  one  of
              the  functions,  it  will create a local parameter **SECONDS** which is floating point and
              set the parameter **TCP**___**SECONDS**___**START** to the previous value of **$SECONDS**.  If the parame‐
              ter  is  already  floating  point,  it  is used without a local copy being created and
              **TCP**___**SECONDS**___**START** is not set.  As the global value is zero, the shell elapsed time  is
              guaranteed to be the sum of **$SECONDS** and **$TCP**___**SECONDS**___**START**.

              This can be avoided by setting **SECONDS** globally to a floating point value using `**type**‐‐
              **set** **-F** **SECONDS**'; then the TCP functions will never make a local  copy  and  never  set
              **TCP**___**SECONDS**___**START** to a non-zero value.

       **TCP**___**SESS**
              May  be  set  directly.  The current session; must refer to one of the sessions estab‐
              lished by **tcp**___**open**.

       **TCP**___**SILENT**
              May be set directly, although it is  also  controlled  by  **tcp**___**log**.   If  of  non-zero
              length, data read by **tcp**___**read** will not be written to standard output, though may still
              be written to a log file.

       **tcp**___**spam**___**list**
              Array.  May be set directly.  See the description of the  function  **tcp**___**spam**  for  how
              this is used.

       **TCP**___**TALK**___**ESCAPE**
              May  be  set  directly.   See the description of the function **tcp**___**talk** for how this is
              used.

       **TCP**___**TIMEOUT**
              May be set directly.  Currently this is only used by  the  function  **tcp**___**command**,  see
              above.

## TCP USER-DEFINED PARAMETERS
       The following parameters are not set by the function system, but have a special effect if set
       by the user.

       **tcp**___**on**___**read**
              This should be an associative array; if it is not, the behaviour is  undefined.   Each
              key is the name of a shell function or other command, and the corresponding value is a
              shell pattern (using **EXTENDED**___**GLOB**).  Every line read from a TCP session  directly  or
              indirectly  using  **tcp**___**read**  (which  includes  lines  read  by **tcp**___**expect**) is compared
              against the pattern.  If the line matches, the command given in the key is called with
              two  arguments: the name of the session from which the line was read, and the line it‐
              self.

              If any function called to handle a line returns a non-zero status,  the  line  is  not
              output.   Thus a **tcp**___**on**___**read** handler containing only the instruction `**return** **1**' can be
              used to suppress output of particular lines (see, however,  **tcp**___**filter**  above).   How‐
              ever,  the  line  is  still  stored  in  **TCP**___**LINE** and **tcp**___**lines**; this occurs after all
              **tcp**___**on**___**read** processing.

## TCP UTILITY PARAMETERS
       These parameters are controlled by the function system; they may be read directly, but should
       not usually be set by user code.

       **tcp**___**aliases**
              Associative array.  The keys are the names of sessions established with **tcp**___**open**; each
              value is a space-separated list of aliases which refer to that session.

       **tcp**___**by**___**fd**
              Associative array.  The keys are session file descriptors; each value is the  name  of
              that session.

       **tcp**___**by**___**name**
              Associative  array.   The  keys  are the names of sessions; each value is the file de‐
              scriptor associated with that session.

## TCP EXAMPLES
       Here is a trivial example using a remote calculator.

       To create a calculator server on port 7337 (see the **dc** manual page for quite how  infuriating
       the underlying command is):

              **tcp**___**proxy** **7337** **dc**

       To connect to this from the same host with a session also named `**dc**':

              **tcp**___**open** **localhost** **7337** **dc**

       To send a command to the remote session and wait a short while for output (assuming **dc** is the
       current session):

              **tcp**___**command** **2** **4** **+** **p**

       To close the session:

              **tcp**___**close**

       The **tcp**___**proxy** needs to be killed to be stopped.  Note this will not usually kill any  connec‐
       tions  which  have already been accepted, and also that the port is not immediately available
       for reuse.

       The following chunk of code puts a list of sessions into an xterm header,  with  the  current
       session followed by a star.

              **print** **-n** **"\033]2;TCP:"** **${(k)tcp**___**by**___**name:/$TCP**___**SESS/$TCP**___**SESS\*}** **"\a"**

## TCP BUGS
       The function **tcp**___**read** uses the shell's normal **read** builtin.  As this reads a complete line at
       once, data arriving without a terminating newline can cause the  function  to  block  indefi‐
       nitely.

       Though  the  function  suite  works  well  for interactive use and for data arriving in small
       amounts, the performance when large amounts of data are being exchanged is likely to  be  ex‐
       tremely poor.



zsh 5.8.1                                 February 12, 2022                             [ZSHTCPSYS(1)](https://www.chedong.com/phpMan.php/man/ZSHTCPSYS/1/markdown)
