1ZSHTCPSYS(1)                General Commands Manual               ZSHTCPSYS(1)
2
3
4

NAME

6       zshtcpsys - zsh tcp system
7

DESCRIPTION

9       A  module  zsh/net/tcp  is  provided to provide network I/O over TCP/IP
10       from within the shell; see its description in zshmodules(1).  This man‐
11       ual page describes a function suite based on the module.  If the module
12       is installed, the functions are usually installed at the same time,  in
13       which  case they will be available for autoloading in the default func‐
14       tion search path.  In addition to the zsh/net/tcp module, the  zsh/zse‐
15       lect  module  is  used  to  implement timeouts on read operations.  For
16       troubleshooting tips, consult the corresponding  advice  for  the  zftp
17       functions described in zshzftpsys(1).
18
19       There  are  functions  corresponding  to the basic I/O operations open,
20       close, read and send, named  tcp_open  etc.,  as  well  as  a  function
21       tcp_expect  for pattern match analysis of data read as input.  The sys‐
22       tem makes it easy to receive data from and send data to multiple  named
23       sessions  at once.  In addition, it can be linked with the shell's line
24       editor in such a way that input data is automatically shown at the ter‐
25       minal.   Other  facilities  available  including logging, filtering and
26       configurable output prompts.
27
28       To use the system where  it  is  available,  it  should  be  enough  to
29       `autoload  -U tcp_open' and run tcp_open as documented below to start a
30       session.  The tcp_open function will autoload the remaining functions.
31

TCP USER FUNCTIONS

33   Basic I/O
34       tcp_open [ -qz ] host port [ sess ]
35       tcp_open [ -qz ] [ -s sess | -l sess[,...] ] ...
36       tcp_open [ -qz ] [ -a fd | -f fd ] [ sess ]
37              Open a new session.  In the first and simplest form, open a  TCP
38              connection to host host at port port; numeric and symbolic forms
39              are understood for both.
40
41              If sess is given, this becomes the name of the session which can
42              be used to refer to multiple different TCP connections.  If sess
43              is not given, the function will  invent  a  numeric  name  value
44              (note  this  is not the same as the file descriptor to which the
45              session is attached).  It is recommended that session names  not
46              include  `funny'  characters,  where  funny  characters  are not
47              well-defined but  certainly  do  not  include  alphanumerics  or
48              underscores, and certainly do include whitespace.
49
50              In  the second case, one or more sessions to be opened are given
51              by name.  A  single  session  name  is  given  after  -s  and  a
52              comma-separated  list  after -l; both options may be repeated as
53              many times as necessary.  A failure to open any  session  causes
54              tcp_open  to  abort.   The  host and port are read from the file
55              .ztcp_sessions in the same directory as the user's zsh initiali‐
56              sation  files,  i.e. usually the home directory, but $ZDOTDIR if
57              that is set.  The file consists of lines each giving  a  session
58              name  and  the  corresponding host and port, in that order (note
59              the session name comes first, not  last),  separated  by  white‐
60              space.
61
62              The  third form allows passive and fake TCP connections.  If the
63              option -a is used, its argument is a file  descriptor  open  for
64              listening for connections.  No function front-end is provided to
65              open such a file descriptor, but a call to `ztcp -l  port'  will
66              create  one  with  the  file  descriptor stored in the parameter
67              $REPLY.  The listening port can be closed with `ztcp -c fd'.   A
68              call  to  `tcp_open -a fd' will block until a remote TCP connec‐
69              tion is made to port on the local machine.   At  this  point,  a
70              session  is  created  in  the usual way and is largely indistin‐
71              guishable from an active connection  created  with  one  of  the
72              first two forms.
73
74              If  the  option  -f  is  used, its argument is a file descriptor
75              which is used directly as if it were a TCP  session.   How  well
76              the remainder of the TCP function system copes with this depends
77              on what actually underlies this file descriptor.  A regular file
78              is  likely  to be unusable; a FIFO (pipe) of some sort will work
79              better, but note that it is not a good idea  for  two  different
80              sessions to attempt to read from the same FIFO at once.
81
82              If  the option -q is given with any of the three forms, tcp_open
83              will not print informational messages, although it will  in  any
84              case exit with an appropriate status.
85
86              If  the line editor (zle) is in use, which is typically the case
87              if the shell is interactive, tcp_open installs a handler  inside
88              zle  which will check for new data at the same time as it checks
89              for keyboard input.  This is convenient as the shell consumes no
90              CPU  time  while waiting; the test is performed by the operating
91              system.  Giving the option -z to any of the  forms  of  tcp_open
92              prevents  the handler from being installed, so data must be read
93              explicitly.  Note, however, this is not necessary for  executing
94              complete  sets of send and read commands from a function, as zle
95              is not active at this point.  Generally speaking, the handler is
96              only  active  when  the  shell is waiting for input at a command
97              prompt or in the vared builtin.  The option has no effect if zle
98              is not active; `[[ -o zle]]' will test for this.
99
100              The  first  session to be opened becomes the current session and
101              subsequent calls to tcp_open do not change it.  The current ses‐
102              sion  is  stored  in the parameter $TCP_SESS; see below for more
103              detail about the parameters used by the system.
104
105              The function tcp_on_open, if defined, is called when  a  session
106              is opened.  See the description below.
107
108       tcp_close [ -qn ] [ -a | -l sess[,...] | sess ... ]
109              Close  the  named  sessions,  or  the current session if none is
110              given, or all open sessions if -a is given.  The options -l  and
111              -s  are both handled for consistency with tcp_open, although the
112              latter is redundant.
113
114              If the session being closed is the  current  one,  $TCP_SESS  is
115              unset,  leaving no current session, even if there are other ses‐
116              sions still open.
117
118              If the session was opened with tcp_open -f, the file  descriptor
119              is  closed  so  long  as  it  is  in the range 0 to 9 accessible
120              directly from the command line.  If the option -n is  given,  no
121              attempt  will  be  made  to close file descriptors in this case.
122              The -n option is not used for genuine  ztcp  session;  the  file
123              descriptors are always closed with the session.
124
125              If  the  option  -q  is given, no informational messages will be
126              printed.
127
128
129       tcp_read [ -bdq ] [ -t TO ] [ -T TO ]
130                [ -a | -u fd[,...] | -l sess[,...] | -s sess ... ]
131              Perform a read operation on the current session, or on a list of
132              sessions  if  any  are given with -u, -l or -s, or all open ses‐
133              sions if the option -a is given.   Any  of  the  -u,  -l  or  -s
134              options may be repeated or mixed together.  The -u option speci‐
135              fies a file descriptor directly (only those managed by this sys‐
136              tem are useful), the other two specify sessions as described for
137              tcp_open above.
138
139              The function checks for new data available on all  the  sessions
140              listed.   Unless the -b option is given, it will not block wait‐
141              ing for new data.  Any one line of data from any of  the  avail‐
142              able  sessions  will be read, stored in the parameter $TCP_LINE,
143              and displayed to standard output unless $TCP_SILENT  contains  a
144              non-empty  string.   When  printed to standard output the string
145              $TCP_PROMPT will be shown at the start of the line; the  default
146              form  for this includes the name of the session being read.  See
147              below for more information on these parameters.  In  this  mode,
148              tcp_read  can  be  called  repeatedly  until it returns status 2
149              which indicates all pending input from  all  specified  sessions
150              has been handled.
151
152              With the option -b, equivalent to an infinite timeout, the func‐
153              tion will block until a line is available to read  from  one  of
154              the   specified  sessions.   However,  only  a  single  line  is
155              returned.
156
157              The option  -d  indicates  that  all  pending  input  should  be
158              drained.   In  this  case tcp_read may process multiple lines in
159              the manner given above; only the last is  stored  in  $TCP_LINE,
160              but the complete set is stored in the array $tcp_lines.  This is
161              cleared at the start of each call to tcp_read.
162
163              The options -t and -T specify a timeout in seconds, which may be
164              a  floating  point  number  for increased accuracy.  With -t the
165              timeout is applied before each line read.  With -T, the  timeout
166              applies  to  the  overall operation, possibly including multiple
167              read operations if  the  option  -d  is  present;  without  this
168              option, there is no distinction between -t and -T.
169
170              The  function  does not print informational messages, but if the
171              option -q is given, no error message is printed for a  non-exis‐
172              tent session.
173
174              A  return  status  of  2 indicates a timeout or no data to read.
175              Any other non-zero return status indicates some error condition.
176
177              See tcp_log for how to control where data is sent by tcp_read.
178
179       tcp_send [ -cnq ] [ -s sess | -l sess[,...] ] data ...
180       tcp_send [ -cnq ] -a data ...
181              Send the supplied data strings to all the specified sessions  in
182              turn.  The underlying operation differs little from a `print -r'
183              to the session's file descriptor, although it attempts  to  pre‐
184              vent  the  shell  from  dying  owing  to  a SIGPIPE caused by an
185              attempt to write to a defunct session.
186
187              The option -c causes tcp_send to  behave  like  cat.   It  reads
188              lines  from  standard input until end of input and sends them in
189              turn to the specified session(s) exactly as if they  were  given
190              as data arguments to individual tcp_send commands.
191
192              The  option  -n  prevents tcp_send from putting a newline at the
193              end of the data strings.
194
195              The remaining options all behave as for tcp_read.
196
197              The data arguments are not further processed once they have been
198              passed to tcp_send; they are simply passed down to print -r.
199
200              If  the  parameter $TCP_OUTPUT is a non-empty string and logging
201              is enabled then the data sent to each session will be echoed  to
202              the  log  file(s)  with  $TCP_OUTPUT in front where appropriate,
203              much in the manner of $TCP_PROMPT.
204
205   Session Management
206       tcp_alias [ -q ] alias=sess ...
207       tcp_alias [ -q ] [ alias ... ]
208       tcp_alias -d [ -q ] alias ...
209              This function is not particularly well tested.
210
211              The first form creates an alias for a session  name;  alias  can
212              then  be  used  to  refer to the existing session sess.  As many
213              aliases may be listed as required.
214
215              The second form lists any aliases specified, or all  aliases  if
216              none.
217
218              The  third  form deletes all the aliases listed.  The underlying
219              sessions are not affected.
220
221              The option -q suppresses  an  inconsistently  chosen  subset  of
222              error messages.
223
224       tcp_log [ -asc ] [ -n | -N ] [ logfile ]
225              With an argument logfile, all future input from tcp_read will be
226              logged to the named file.  Unless -a  (append)  is  given,  this
227              file  will  first  be truncated or created empty.  With no argu‐
228              ments, show the current status of logging.
229
230              With the option -s, per-session logging is enabled.  Input  from
231              tcp_read  is output to the file logfile.sess.  As the session is
232              automatically discriminated by the filename,  the  contents  are
233              raw   (no  $TCP_PROMPT).   The  option   -a  applies  as  above.
234              Per-session logging and logging of all data in one file are  not
235              mutually exclusive.
236
237              The  option -c closes all logging, both complete and per-session
238              logs.
239
240              The options -n and -N respectively turn off or restore output of
241              data  read  by  tcp_read to standard output; hence `tcp_log -cn'
242              turns off all output by tcp_read.
243
244              The function is purely a convenient front  end  to  setting  the
245              parameters   $TCP_LOG,  $TCP_LOG_SESS,  $TCP_SILENT,  which  are
246              described below.
247
248       tcp_rename old new
249              Rename session  old  to  session  new.   The  old  name  becomes
250              invalid.
251
252       tcp_sess [ sess [ command [ arg ... ] ] ]
253              With  no  arguments,  list  all the open sessions and associated
254              file descriptors.  The current session is marked  with  a  star.
255              For   use   in   functions,  direct  access  to  the  parameters
256              $tcp_by_name, $tcp_by_fd and $TCP_SESS is probably  more  conve‐
257              nient; see below.
258
259              With  a sess argument, set the current session to sess.  This is
260              equivalent to changing $TCP_SESS directly.
261
262              With additional arguments, temporarily set the  current  session
263              while  executing  `command arg ...'.  command is re-evaluated so
264              as to expand aliases etc., but the  remaining  args  are  passed
265              through  as  that  appear  to tcp_sess.  The original session is
266              restored when tcp_sess exits.
267
268   Advanced I/O
269       tcp_command send-option ... send-argument ...
270              This is a convenient front-end to tcp_send.  All  arguments  are
271              passed  to  tcp_send, then the function pauses waiting for data.
272              While data is arriving at least every $TCP_TIMEOUT (default 0.3)
273              seconds,  data  is handled and printed out according to the cur‐
274              rent settings.  Status 0 is always returned.
275
276              This is generally only useful for interactive  use,  to  prevent
277              the display becoming fragmented by output returned from the con‐
278              nection.  Within a programme or function it is generally  better
279              to handle reading data by a more explicit method.
280
281
282       tcp_expect [ -q ] [ -p var | -P var ] [ -t TO | -T TO ]
283                  [ -a | -s sess | -l sess[,...] ] pattern ...
284              Wait  for  input  matching any of the given patterns from any of
285              the specified sessions.  Input is ignored until  an  input  line
286              matches  one of the given patterns; at this point status zero is
287              returned, the matching line is stored in $TCP_LINE, and the full
288              set of lines read during the call to tcp_expect is stored in the
289              array $tcp_expect_lines.
290
291              Sessions are specified in the same way as tcp_read: the  default
292              is  to use the current session, otherwise the sessions specified
293              by -a, -s, or -l are used.
294
295              Each pattern is a standard zsh extended-globbing  pattern;  note
296              that  it  needs  to be quoted to avoid it being expanded immedi‐
297              ately by filename generation.  It must match the full  line,  so
298              to  match  a substring there must be a `*' at the start and end.
299              The line matched  against  includes  the  $TCP_PROMPT  added  by
300              tcp_read.   It is possible to include the globbing flags `#b' or
301              `#m' in the patterns to make  backreferences  available  in  the
302              parameters  $MATCH,  $match,  etc., as described in the base zsh
303              documentation on pattern matching.
304
305              Unlike tcp_read, the default behaviour of tcp_expect is to block
306              indefinitely  until  the  required  input is found.  This can be
307              modified by specifying a timeout with -t or -T;  these  function
308              as  in  tcp_read,  specifying  a  per-read  or  overall timeout,
309              respectively, in seconds, as an integer or  floating-point  num‐
310              ber.   As  tcp_read,  the function returns status 2 if a timeout
311              occurs.
312
313              The function returns as soon as any one of  the  patterns  given
314              match.   If  the  caller  needs  to  know  which of the patterns
315              matched, the option -p var can be used; on return, $var  is  set
316              to  the  number of the pattern using ordinary zsh indexing, i.e.
317              the first is 1, and so on.  Note the absence of a `$'  in  front
318              of  var.   To  avoid  clashes,  the  parameter cannot begin with
319              `_expect'.  The index -1 is used if there is a timeout and 0  if
320              there is no match.
321
322              The  option -P var works similarly to -p, but instead of numeri‐
323              cal indexes the regular arguments must begin with a prefix  fol‐
324              lowed by a colon: that prefix is then used as a tag to which var
325              is set when the argument matches.  The tag timeout  is  used  if
326              there  is  a  timeout and the empty string if there is no match.
327              Note it is acceptable for different arguments to start with  the
328              same prefix if the matches do not need to be distinguished.
329
330              The option -q is passed directly down to tcp_read.
331
332              As  all  input  is  done via tcp_read, all the usual rules about
333              output of lines read apply.  One exception is that the parameter
334              $tcp_lines  will  only  reflect  the  line  actually  matched by
335              tcp_expect; use $tcp_expect_lines for the full set of lines read
336              during the function call.
337
338       tcp_proxy
339              This  is a simple-minded function to accept a TCP connection and
340              execute  a  command  with  I/O  redirected  to  the  connection.
341              Extreme  caution should be taken as there is no security whatso‐
342              ever and this can leave your computer open to the  world.   Ide‐
343              ally, it should only be used behind a firewall.
344
345              The first argument is a TCP port on which the function will lis‐
346              ten.
347
348              The remaining arguments give a command and its arguments to exe‐
349              cute  with  standard  input,  standard output and standard error
350              redirected to the file descriptor on which the TCP  session  has
351              been  accepted.   If  no command is given, a new zsh is started.
352              This gives everyone  on  your  network  direct  access  to  your
353              account, which in many cases will be a bad thing.
354
355              The  command  is  run  in  the background, so tcp_proxy can then
356              accept new connections.  It continues to accept new  connections
357              until interrupted.
358
359       tcp_spam [ -ertv ] [ -a | -s sess | -l sess[,...] ] cmd [ arg ... ]
360              Execute  `cmd  [ arg ... ]' for each session in turn.  Note this
361              executes the command and arguments; it does not send the command
362              line as data unless the -t (transmit) option is given.
363
364              The sessions may be selected explicitly with the standard -a, -s
365              or -l options, or may be chosen  implicitly.   If  none  of  the
366              three  options  is  given  the  rules  are:  first, if the array
367              $tcp_spam_list is set, this is taken as the  list  of  sessions,
368              otherwise all sessions are taken.  Second, any sessions given in
369              the array $tcp_no_spam_list are removed from the  list  of  ses‐
370              sions.
371
372              Normally,  any  sessions added by the `-a' flag or when all ses‐
373              sions are chosen implicitly are  spammed  in  alphabetic  order;
374              sessions  given  by  the  $tcp_spam_list array or on the command
375              line are spammed in the order given.  The -r flag  reverses  the
376              order however it was arrived it.
377
378              The  -v  flag specifies that a $TCP_PROMPT will be output before
379              each session.  This is output after any modification to TCP_SESS
380              by   the  user-defined  tcp_on_spam  function  described  below.
381              (Obviously that function is able to generate its own output.)
382
383              If the option -e is present, the line given as `cmd [ arg ... ]'
384              is  executed  using  eval,  otherwise it is executed without any
385              further processing.
386
387       tcp_talk
388              This is a fairly simple-minded attempt to  force  input  to  the
389              line editor to go straight to the default TCP_SESS.
390
391              An  escape  string,  $TCP_TALK_ESCAPE,  default  `:', is used to
392              allow access to normal shell operation.  If it is on its own  at
393              the  start of the line, or followed only by whitespace, the line
394              editor returns to normal operation.  Otherwise, the  string  and
395              any  following  whitespace  are skipped and the remainder of the
396              line executed as shell input without any change of the line edi‐
397              tor's operating mode.
398
399              The current implementation is somewhat deficient in terms of use
400              of the command history.  For this reason, many users will prefer
401              to use some form of alternative approach for sending data easily
402              to the current session.  One simple approach is  to  alias  some
403              special character (such as `%') to `tcp_command --'.
404
405       tcp_wait
406              The  sole  argument is an integer or floating point number which
407              gives the seconds to delay.  The shell will do nothing for  that
408              period  except  wait  for  input  on all TCP sessions by calling
409              tcp_read -a.  This is similar to the  interactive  behaviour  at
410              the command prompt when zle handlers are installed.
411
412   `One-shot' file transfer
413       tcp_point port
414       tcp_shoot host port
415              This  pair  of functions provide a simple way to transfer a file
416              between two hosts within the shell.  Note,  however,  that  bulk
417              data  transfer is currently done using cat.  tcp_point reads any
418              data arriving at port and sends it to standard output; tcp_shoot
419              connects  to  port  on  host  and sends its standard input.  Any
420              unused port may be used; the standard mechanism  for  picking  a
421              port  is to think of a random four-digit number above 1024 until
422              one works.
423
424              To transfer a file from  host  woodcock  to  host  springes,  on
425              springes:
426
427                     tcp_point 8091 >output_file
428
429              and on woodcock:
430
431                     tcp_shoot springes 8091 <input_file
432
433              As  these  two functions do not require tcp_open to set up a TCP
434              connection first, they may need to be autoloaded separately.
435

TCP USER-DEFINED FUNCTIONS

437       Certain functions, if defined by the user, will be called by the  func‐
438       tion  system  in certain contexts.  This facility depends on the module
439       zsh/parameter, which is usually available in interactive shells as  the
440       completion  system  depends  on  it.   None  of  the  functions need be
441       defined; they simply provide convenient hooks when necessary.
442
443       Typically, these are called after the requested action has been  taken,
444       so that the various parameters will reflect the new state.
445
446       tcp_on_alias alias fd
447              When  an alias is defined, this function will be called with two
448              arguments: the name of the alias, and the file descriptor of the
449              corresponding session.
450
451       tcp_on_awol sess fd
452              If  the  function tcp_fd_handler is handling input from the line
453              editor and detects that the file descriptor is no  longer  reus‐
454              able, by default it removes it from the list of file descriptors
455              handled by this method and prints a message.   If  the  function
456              tcp_on_awol  is  defined  it  is  called immediately before this
457              point.  It may return status 100, which indicates that the  nor‐
458              mal  handling should still be performed; any other return status
459              indicates that  no  further  action  should  be  taken  and  the
460              tcp_fd_handler  should return immediately with the given status.
461              Typically the action of tcp_on_awol will be to  close  the  ses‐
462              sion.
463
464              The variable TCP_INVALIDATE_ZLE will be a non-empty string if it
465              is necessary to invalidate the line editor  display  using  `zle
466              -I' before printing output from the function.
467
468              (`AWOL'  is  military  jargon for `absent without leave' or some
469              variation.  It has no pre-existing technical  meaning  known  to
470              the author.)
471
472       tcp_on_close sess fd
473              This  is  called with the name of a session being closed and the
474              file descriptor which corresponded to that session.   Both  will
475              be invalid by the time the function is called.
476
477       tcp_on_open sess fd
478              This  is  called  after  a new session has been defined with the
479              session name and file descriptor as arguments.  If it returns  a
480              non-zero  status, opening the session is assumed to fail and the
481              session is closed again;  however,  tcp_open  will  continue  to
482              attempt  to  open  any  remaining  sessions given on the command
483              line.
484
485       tcp_on_rename oldsess fd newsess
486              This is called after a session has been renamed with  the  three
487              arguments old session name, file descriptor, new session name.
488
489       tcp_on_spam sess command ...
490              This is called once for each session spammed, just before a com‐
491              mand is executed for a session by tcp_spam.  The  arguments  are
492              the  session  name  followed by the command list to be executed.
493              If tcp_spam was called with the option  -t,  the  first  command
494              will be tcp_send.
495
496              This  function  is  called after $TCP_SESS is set to reflect the
497              session to be spammed, but before any use of it is made.   Hence
498              it is possible to alter the value of $TCP_SESS within this func‐
499              tion.  For example, the  session  arguments  to  tcp_spam  could
500              include  extra  information  to be stripped off and processed in
501              tcp_on_spam.
502
503              If the function sets the parameter $REPLY to `done', the command
504              line  is not executed; in addition, no prompt is printed for the
505              -v option to tcp_spam.
506
507       tcp_on_unalias alias fd
508              This is called with the name of an alias and  the  corresponding
509              session's file descriptor after an alias has been deleted.
510

TCP UTILITY FUNCTIONS

512       The  following  functions  are used by the TCP function system but will
513       rarely if ever need to be called directly.
514
515       tcp_fd_handler
516              This is the function installed by tcp_open  for  handling  input
517              from  within the line editor, if that is required.  It is in the
518              format documented for the builtin `zle -F' in zshzle(1) .
519
520              While active, the function sets the parameter TCP_HANDLER_ACTIVE
521              to 1.  This allows shell code called internally (for example, by
522              setting tcp_on_read) to tell if is being called when  the  shell
523              is otherwise idle at the editor prompt.
524
525       tcp_output [ -q ] -P prompt -F fd -S sess
526              This  function  is  used for both logging and handling output to
527              standard output, from within tcp_read  and  (if  $TCP_OUTPUT  is
528              set) tcp_send.
529
530              The  prompt  to use is specified by -P; the default is the empty
531              string.  It can contain:
532              %c     Expands to 1 if the session is the current session,  oth‐
533                     erwise   0.    Used  with  ternary  expressions  such  as
534                     `%(c.-.+)' to output `+' for the current session and  `-'
535                     otherwise.
536
537              %f     Replaced by the session's file descriptor.
538
539              %s     Replaced by the session name.
540
541              %%     Replaced by a single `%'.
542
543              The  option  -q suppresses output to standard output, but not to
544              any log files which are configured.
545
546              The -S and -F options are used to pass in the session  name  and
547              file descriptor for possible replacement in the prompt.
548

TCP USER PARAMETERS

550       Parameters  follow  the  usual  convention  that  uppercase is used for
551       scalars and integers, while lowercase is used for normal  and  associa‐
552       tive  array.  It is always safe for user code to read these parameters.
553       Some parameters may also be set; these are  noted  explicitly.   Others
554       are  included  in this group as they are set by the function system for
555       the user's benefit, i.e. setting them is typically not  useful  but  is
556       benign.
557
558       It  is  often  also useful to make settable parameters local to a func‐
559       tion.  For example, `local TCP_SILENT=1' specifies that data read  dur‐
560       ing  the  function call will not be printed to standard output, regard‐
561       less  of  the  setting  outside   the   function.    Likewise,   `local
562       TCP_SESS=sess'  sets  a  session  for  the  duration of a function, and
563       `local TCP_PROMPT=' specifies that no prompt is used for  input  during
564       the function.
565
566       tcp_expect_lines
567              Array.    The  set  of  lines  read  during  the  last  call  to
568              tcp_expect, including the last ($TCP_LINE).
569
570       tcp_filter
571              Array. May be set directly.  A set of extended globbing patterns
572              which,  if  matched in tcp_output, will cause the line not to be
573              printed to standard output.  The patterns should be  defined  as
574              described  for  the  arguments to tcp_expect.  Output of line to
575              log files is not affected.
576
577       TCP_HANDLER_ACTIVE
578              Scalar.  Set to 1 within tcp_fd_handler to indicate to functions
579              called  recursively  that they have been called during an editor
580              session.  Otherwise unset.
581
582       TCP_LINE
583              The last line read by tcp_read, and hence also tcp_expect.
584
585       TCP_LINE_FD
586              The   file   descriptor   from   which   $TCP_LINE   was   read.
587              ${tcp_by_fd[$TCP_LINE_FD]}  will  give the corresponding session
588              name.
589
590       tcp_lines
591              Array. The set of lines read during the last call  to  tcp_read,
592              including the last ($TCP_LINE).
593
594       TCP_LOG
595              May  be set directly, although it is also controlled by tcp_log.
596              The name of a file to which output from  all  sessions  will  be
597              sent.   The output is proceeded by the usual $TCP_PROMPT.  If it
598              is not an absolute path name, it will follow the user's  current
599              directory.
600
601       TCP_LOG_SESS
602              May  be set directly, although it is also controlled by tcp_log.
603              The prefix for a set of files to which output from each  session
604              separately    will    be    sent;    the    full   filename   is
605              ${TCP_LOG_SESS}.sess.  Output to each file is raw; no prompt  is
606              added.   If  it is not an absolute path name, it will follow the
607              user's current directory.
608
609       tcp_no_spam_list
610              Array.  May be set directly.  See tcp_spam for how this is used.
611
612       TCP_OUTPUT
613              May be set directly.  If a non-empty string, any data sent to  a
614              session  by  tcp_send  will be logged.  This parameter gives the
615              prompt to be used in a file specified by $TCP_LOG but not  in  a
616              file  generated  from  $TCP_LOG_SESS.  The prompt string has the
617              same format as TCP_PROMPT and the same rules for its use apply.
618
619       TCP_PROMPT
620              May be set directly.  Used  as  the  prefix  for  data  read  by
621              tcp_read  which is printed to standard output or to the log file
622              given by $TCP_LOG, if any.  Any `%s', `%f' or `%%' occurring  in
623              the string will be replaced by the name of the session, the ses‐
624              sion's underlying file descriptor,  or  a  single  `%',  respec‐
625              tively.   The  expression `%c' expands to 1 if the session being
626              read is the current session, else 0;  this  is  most  useful  in
627              ternary  expressions such as `%(c.-.+)' which outputs `+' if the
628              session is the current one, else `-'.
629
630              If the prompt starts with %P, this is stripped and the  complete
631              result  of  the previous stage is passed through standard prompt
632              %-style formatting before being output.
633
634       TCP_READ_DEBUG
635              May be set directly.  If this has non-zero length, tcp_read will
636              give some limited diagnostics about data being read.
637
638       TCP_SECONDS_START
639              This value is created and initialised to zero by tcp_open.
640
641              The  functions  tcp_read  and tcp_expect use the shell's SECONDS
642              parameter for their own timing purposes.  If that  parameter  is
643              not  of floating point type on entry to one of the functions, it
644              will create a local parameter SECONDS which  is  floating  point
645              and set the parameter TCP_SECONDS_START to the previous value of
646              $SECONDS.  If the parameter is already  floating  point,  it  is
647              used without a local copy being created and TCP_SECONDS_START is
648              not set.  As the global value is zero, the shell elapsed time is
649              guaranteed to be the sum of $SECONDS and $TCP_SECONDS_START.
650
651              This  can  be  avoided by setting SECONDS globally to a floating
652              point value using `typeset -F SECONDS'; then the  TCP  functions
653              will  never make a local copy and never set TCP_SECONDS_START to
654              a non-zero value.
655
656       TCP_SESS
657              May be set directly.  The current session; must refer to one  of
658              the sessions established by tcp_open.
659
660       TCP_SILENT
661              May  be set directly, although it is also controlled by tcp_log.
662              If of non-zero length, data read by tcp_read will not be written
663              to standard output, though may still be written to a log file.
664
665       tcp_spam_list
666              Array.   May  be set directly.  See the description of the func‐
667              tion tcp_spam for how this is used.
668
669       TCP_TALK_ESCAPE
670              May be set  directly.   See  the  description  of  the  function
671              tcp_talk for how this is used.
672
673       TCP_TIMEOUT
674              May  be  set directly.  Currently this is only used by the func‐
675              tion tcp_command, see above.
676

TCP USER-DEFINED PARAMETERS

678       The following parameters are not set by the function system, but have a
679       special effect if set by the user.
680
681       tcp_on_read
682              This should be an associative array; if it is not, the behaviour
683              is undefined.  Each key is the name of a shell function or other
684              command,  and  the corresponding value is a shell pattern (using
685              EXTENDED_GLOB).  Every line read from a TCP session directly  or
686              indirectly   using   tcp_read  (which  includes  lines  read  by
687              tcp_expect) is  compared  against  the  pattern.   If  the  line
688              matches,  the  command given in the key is called with two argu‐
689              ments: the name of the session from which the line was read, and
690              the line itself.
691
692              If  any function called to handle a line returns a non-zero sta‐
693              tus, the line is not output.  Thus a  tcp_on_read  handler  con‐
694              taining  only the instruction `return 1' can be used to suppress
695              output of particular lines  (see,  however,  tcp_filter  above).
696              However,  the  line  is  still stored in TCP_LINE and tcp_lines;
697              this occurs after all tcp_on_read processing.
698

TCP UTILITY PARAMETERS

700       These parameters are controlled by the function  system;  they  may  be
701       read directly, but should not usually be set by user code.
702
703       tcp_aliases
704              Associative  array.   The  keys are the names of sessions estab‐
705              lished with tcp_open; each value is a  space-separated  list  of
706              aliases which refer to that session.
707
708       tcp_by_fd
709              Associative  array.  The keys are session file descriptors; each
710              value is the name of that session.
711
712       tcp_by_name
713              Associative array.  The keys are the  names  of  sessions;  each
714              value is the file descriptor associated with that session.
715

TCP EXAMPLES

717       Here is a trivial example using a remote calculator.
718
719       To  create a calculator server on port 7337 (see the dc manual page for
720       quite how infuriating the underlying command is):
721
722              tcp_proxy 7337 dc
723
724       To connect to this from the same host with a session also named `dc':
725
726              tcp_open localhost 7337 dc
727
728       To send a command to the remote session and wait a short while for out‐
729       put (assuming dc is the current session):
730
731              tcp_command 2 4 + p
732
733       To close the session:
734
735              tcp_close
736
737       The  tcp_proxy  needs  to  be killed to be stopped.  Note this will not
738       usually kill any connections which have already been accepted, and also
739       that the port is not immediately available for reuse.
740
741       The  following  chunk  of  code  puts  a list of sessions into an xterm
742       header, with the current session followed by a star.
743
744              print -n "\033]2;TCP:" ${(k)tcp_by_name:/$TCP_SESS/$TCP_SESS\*} "\a"
745

TCP BUGS

747       The function tcp_read uses the shell's normal read  builtin.   As  this
748       reads a complete line at once, data arriving without a terminating new‐
749       line can cause the function to block indefinitely.
750
751       Though the function suite works well for interactive use and  for  data
752       arriving  in  small amounts, the performance when large amounts of data
753       are being exchanged is likely to be extremely poor.
754
755
756
757zsh 5.7.1                      February 3, 2019                   ZSHTCPSYS(1)
Impressum