1
.TH "ZSHTCPSYS" "1" "June 1, 2009" "zsh 4\&.3\&.10"
3
zshtcpsys \- zsh tcp system
4
.\" Yodl file: Zsh/tcpsys.yo
7
A module \fBzsh/net/tcp\fP is provided to provide network I/O over
8
TCP/IP from within the shell; see its description in
10
\&. This manual page describes a function suite based on the module\&.
11
If the module is installed, the functions are usually installed at the
12
same time, in which case they will be available for
13
autoloading in the default function search path\&. In addition to the
14
\fBzsh/net/tcp\fP module, the \fBzsh/zselect\fP module is used to implement
15
timeouts on read operations\&. For troubleshooting tips, consult the
16
corresponding advice for the \fBzftp\fP functions described in
20
There are functions corresponding to the basic I/O operations open, close,
21
read and send, named \fBtcp_open\fP etc\&., as well as a function
22
\fBtcp_expect\fP for pattern match analysis of data read as input\&. The
23
system makes it easy to receive data from and send data to multiple named
24
sessions at once\&. In addition, it can be linked with the shell\&'s line
25
editor in such a way that input data is automatically shown at the
26
terminal\&. Other facilities available including logging, filtering and
27
configurable output prompts\&.
29
To use the system where it is available, it should be enough to
30
`\fBautoload \-U tcp_open\fP\&' and run \fBtcp_open\fP as documented below to
31
start a session\&. The \fBtcp_open\fP function will autoload the remaining
35
.SH "TCP USER FUNCTIONS"
42
\fBtcp_open [\-qz]\fP \fIhost port\fP \fB[\fP \fIsess\fP \fB]\fP
45
\fBtcp_open [\-qz] [ \-s\fP \fIsess\fP \fB| \-l\fP \fIsess\fP\fB,\&.\&.\&. ] \&.\&.\&. \fP
48
\fBtcp_open [\-qz] [\-a\fP \fIfd\fP \fB| \-f\fP \fIfd\fP \fB] [\fP \fIsess\fP \fB]\fP
49
Open a new session\&. In the first and simplest form, open a TCP connection
50
to host \fIhost\fP at port \fIport\fP; numeric and symbolic forms are
51
understood for both\&.
54
If \fIsess\fP is given, this becomes the name of the session which can be
55
used to refer to multiple different TCP connections\&. If \fIsess\fP is
56
not given, the function will invent a numeric name value (note this is
57
\fInot\fP the same as the file descriptor to which the session is attached)\&.
58
It is recommended that session names not include `funny\&' characters, where
59
funny characters are not well\-defined but certainly do not include
60
alphanumerics or underscores, and certainly do include whitespace\&.
62
In the second case, one or more sessions to be opened are given by name\&.
63
A single session name is given after \fB\-s\fP and a comma\-separated list
64
after \fB\-l\fP; both options may be repeated as many times as necessary\&.
65
A failure to open any session causes \fBtcp_open\fP to abort\&.
66
The host and port are read from the file \fB\&.ztcp_sessions\fP in the same
67
directory as the user\&'s zsh initialisation files, i\&.e\&. usually the home
68
directory, but \fB$ZDOTDIR\fP if that is set\&. The file consists of lines
69
each giving a session name and the corresponding host and port, in that
70
order (note the session name comes first, not last), separated by
73
The third form allows passive and fake TCP connections\&. If the option
74
\fB\-a\fP is used, its argument is a file descriptor open for listening for
75
connections\&. No function front\-end is provided to open such a file
76
descriptor, but a call to `\fBztcp \-l\fP \fIport\fP\&' will create one with the
77
file descriptor stored in the parameter \fB$REPLY\fP\&. The listening port can
78
be closed with `\fBztcp \-c\fP \fIfd\fP\&'\&. A call to `\fBtcp_open \-a\fP \fIfd\fP'
79
will block until a remote TCP connection is made to \fIport\fP on the local
80
machine\&. At this point, a session is created in the usual way and is
81
largely indistinguishable from an active connection created with one of the
84
If the option \fB\-f\fP is used, its argument is a file descriptor which is
85
used directly as if it were a TCP session\&. How well the remainder of the
86
TCP function system copes with this depends on what actually underlies this
87
file descriptor\&. A regular file is likely to be unusable; a FIFO (pipe) of
88
some sort will work better, but note that it is not a good idea for two
89
different sessions to attempt to read from the same FIFO at once\&.
91
If the option \fB\-q\fP is given with any of the three forms, \fBtcp_open\fP
92
will not print informational messages, although it will in any case exit
93
with an appropriate status\&.
95
If the line editor (zle) is in use, which is typically the case if the
96
shell is interactive, \fBtcp_open\fP installs a handler inside \fBzle\fP which
97
will check for new data at the same time as it checks for keyboard input\&.
98
This is convenient as the shell consumes no CPU time while waiting; the
99
test is performed by the operating system\&. Giving the option \fB\-z\fP to
100
any of the forms of \fBtcp_open\fP prevents the handler from being
101
installed, so data must be read explicitly\&. Note, however, this is not
102
necessary for executing complete sets of send and read commands from a
103
function, as zle is not active at this point\&. Generally speaking, the
104
handler is only active when the shell is waiting for input at a command
105
prompt or in the \fBvared\fP builtin\&. The option has no effect if zle is not
106
active; `\fB[[ \-o zle]]\fP\&' will test for this\&.
108
The first session to be opened becomes the current session and subsequent
109
calls to \fBtcp_open\fP do not change it\&. The current session is stored
110
in the parameter \fB$TCP_SESS\fP; see below for more detail about the
111
parameters used by the system\&.
113
The function \fBtcp_on_open\fP, if defined, is called when a session
114
is opened\&. See the description below\&.
117
\fBtcp_close [\-qn] [ \-a | \-l\fP \fIsess\fP\fB,\&.\&.\&. |\fP \fIsess\fP \fB\&.\&.\&. ]\fP
118
Close the named sessions, or the current session if none is given,
119
or all open sessions if \fB\-a\fP is given\&. The options \fB\-l\fP and \fB\-s\fP are
120
both handled for consistency with \fBtcp_open\fP, although the latter is
124
If the session being closed is the current one, \fB$TCP_SESS\fP is unset,
125
leaving no current session, even if there are other sessions still open\&.
127
If the session was opened with \fBtcp_open \-f\fP, the file descriptor is
128
closed so long as it is in the range 0 to 9 accessible directly from the
129
command line\&. If the option \fB\-n\fP is given, no attempt will be made to
130
close file descriptors in this case\&. The \fB\-n\fP option is not used for
131
genuine \fBztcp\fP session; the file descriptors are always closed with the
134
If the option \fB\-q\fP is given, no informational messages will be printed\&.
138
\fBtcp_read [\-bdq] [ \-t\fP \fITO\fP \fB] [ \-T\fP \fITO\fP \fB]\fP
141
\fB[ \-a | \-u\fP \fIfd\fP \fB\&.\&.\&. | \-l\fP \fIsess\fP\fB,\&.\&.\&. | \-s\fP \fIsess\fP \fB\&.\&.\&.]\fP
142
Perform a read operation on the current session, or on a list of
143
sessions if any are given with \fB\-u\fP, \fB\-l\fP or \fB\-s\fP, or all open
144
sessions if the option \fB\-a\fP is given\&. Any of the \fB\-u\fP, \fB\-l\fP or
145
\fB\-s\fP options may be repeated or mixed together\&. The \fB\-u\fP option
146
specifies a file descriptor directly (only those managed by this system
147
are useful), the other two specify sessions as described for
148
\fBtcp_open\fP above\&.
151
The function checks for new data available on all the sessions listed\&.
152
Unless the \fB\-b\fP option is given, it will not block waiting for new data\&.
153
Any one line of data from any of the available sessions will be read,
154
stored in the parameter \fB$TCP_LINE\fP, and displayed to standard output
155
unless \fB$TCP_SILENT\fP contains a non\-empty string\&. When printed to
156
standard output the string \fB$TCP_PROMPT\fP will be shown at the start of
157
the line; the default form for this includes the name of the session being
158
read\&. See below for more information on these parameters\&. In this mode,
159
\fBtcp_read\fP can be called repeatedly until it returns status 2 which
160
indicates all pending input from all specified sessions has been handled\&.
162
With the option \fB\-b\fP, equivalent to an infinite timeout, the function
163
will block until a line is available to read from one of the specified
164
sessions\&. However, only a single line is returned\&.
166
The option \fB\-d\fP indicates that all pending input should be drained\&. In
167
this case \fBtcp_read\fP may process multiple lines in the manner given
168
above; only the last is stored in \fB$TCP_LINE\fP, but the complete set is
169
stored in the array \fB$tcp_lines\fP\&. This is cleared at the start of each
170
call to \fBtcp_read\fP\&.
172
The options \fB\-t\fP and \fB\-T\fP specify a timeout in seconds, which may be a
173
floating point number for increased accuracy\&. With \fB\-t\fP the timeout is
174
applied before each line read\&. With \fB\-T\fP, the timeout applies to the
175
overall operation, possibly including multiple read operations if the
176
option \fB\-d\fP is present; without this option, there is no distinction
177
between \fB\-t\fP and \fB\-T\fP\&.
179
The function does not print informational messages, but if the option
180
\fB\-q\fP is given, no error message is printed for a non\-existent session\&.
182
A return status of 2 indicates a timeout or no data to read\&. Any other
183
non\-zero return status indicates some error condition\&.
185
See \fBtcp_log\fP for how to control where data is sent by \fBtcp_read\fP\&.
189
\fBtcp_send [\-cnq] [ \-s\fP \fIsess\fP \fB| \-l\fP \fIsess\fP\fB,\&.\&.\&. ]\fP \fIdata\fP \fB\&.\&.\&.\fP
192
\fBtcp_send [\-cnq] \-a\fP \fIdata\fP \fB\&.\&.\&.\fP
193
Send the supplied data strings to all the specified sessions in turn\&. The
194
underlying operation differs little from a `\fBprint \-r\fP\&' to the session's
195
file descriptor, although it attempts to prevent the shell from dying owing
196
to a \fBSIGPIPE\fP caused by an attempt to write to a defunct session\&.
199
The option \fB\-c\fP causes \fBtcp_send\fP to behave like \fBcat\fP\&. It reads
200
lines from standard input until end of input and sends them in turn to the
201
specified session(s) exactly as if they were given as \fIdata\fP
202
arguments to individual \fBtcp_send\fP commands\&.
204
The option \fB\-n\fP prevents \fBtcp_send\fP from putting a newline at the end
205
of the data strings\&.
207
The remaining options all behave as for \fBtcp_read\fP\&.
209
The data arguments are not further processed once they have been passed to
210
\fBtcp_send\fP; they are simply passed down to \fBprint \-r\fP\&.
212
If the parameter \fB$TCP_OUTPUT\fP is a non\-empty string and logging is
213
enabled then the data sent to each session will be echoed to the log
214
file(s) with \fB$TCP_OUTPUT\fP in front where appropriate, much
215
in the manner of \fB$TCP_PROMPT\fP\&.
219
.SS "Session Management"
224
\fBtcp_alias [\-q]\fP \fIalias\fP\fB=\fP\fIsess\fP \fB\&.\&.\&.\fP
227
\fBtcp_alias [\-q] [\fP \fIalias\fP \fB] \&.\&.\&.\fP
230
\fBtcp_alias \-d [\-q]\fP \fIalias\fP \fB\&.\&.\&.\fP
231
This function is not particularly well tested\&.
234
The first form creates an alias for a session name; \fIalias\fP can then be
235
used to refer to the existing session \fIsess\fP\&. As many aliases may be
236
listed as required\&.
238
The second form lists any aliases specified, or all aliases if none\&.
240
The third form deletes all the aliases listed\&. The underlying sessions are
243
The option \fB\-q\fP suppresses an inconsistently chosen subset of error
247
\fBtcp_log [\-asc] [ \-n | \-N ] [\fP \fIlogfile\fP \fB]\fP
248
With an argument \fIlogfile\fP, all future input from \fBtcp_read\fP will be
249
logged to the named file\&. Unless \fB\-a\fP (append) is given, this file will
250
first be truncated or created empty\&. With no arguments, show the current
254
With the option \fB\-s\fP, per\-session logging is enabled\&. Input from
255
\fBtcp_read\fP is output to the file \fIlogfile\fP\&.\fIsess\fP\&. As the
256
session is automatically discriminated by the filename, the contents are
257
raw (no \fB$TCP_PROMPT\fP)\&. The option \fB\-a\fP applies as above\&.
258
Per\-session logging and logging of all data in one file are not mutually
261
The option \fB\-c\fP closes all logging, both complete and per\-session logs\&.
263
The options \fB\-n\fP and \fB\-N\fP respectively turn off or restore output of
264
data read by \fBtcp_read\fP to standard output; hence `\fBtcp_log \-cn\fP\&' turns
265
off all output by \fBtcp_read\fP\&.
267
The function is purely a convenient front end to setting the parameters
268
\fB$TCP_LOG\fP, \fB$TCP_LOG_SESS\fP, \fB$TCP_SILENT\fP, which are described below\&.
271
\fBtcp_rename\fP \fIold\fP \fInew\fP
272
Rename session \fIold\fP to session \fInew\fP\&. The old name becomes invalid\&.
274
\fBtcp_sess [\fP \fIsess\fP \fB[\fP \fIcommand\fP \fB\&.\&.\&. ] ]\fP
275
With no arguments, list all the open sessions and associated file
276
descriptors\&. The current session is marked with a star\&. For use in
277
functions, direct access to the parameters \fB$tcp_by_name\fP, \fB$tcp_by_fd\fP
278
and \fB$TCP_SESS\fP is probably more convenient; see below\&.
281
With a \fIsess\fP argument, set the current session to \fIsess\fP\&.
282
This is equivalent to changing \fB$TCP_SESS\fP directly\&.
284
With additional arguments, temporarily set the current session while
285
executing the string \fBcommand \&.\&.\&.\fP\&. The first argument is re\-evaluated
286
so as to expand aliases etc\&., but the remaining arguments are passed
287
through as the appear to \fBtcp_sess\fP\&. The original session is restored
288
when \fBtcp_sess\fP exits\&.
297
\fBtcp_command\fP \fIsend\-options\fP \fB\&.\&.\&.\fP \fIsend\-arguments\fP \fB\&.\&.\&.\fP
298
This is a convenient front\-end to \fBtcp_send\fP\&. All arguments are passed
299
to \fBtcp_send\fP, then the function pauses waiting for data\&. While data is
300
arriving at least every \fB$TCP_TIMEOUT\fP (default 0\&.3) seconds, data is
301
handled and printed out according to the current settings\&. Status 0 is
305
This is generally only useful for interactive use, to prevent the display
306
becoming fragmented by output returned from the connection\&. Within a
307
programme or function it is generally better to handle reading data by a
308
more explicit method\&.
312
\fBtcp_expect [ \-q ] [ \-p\fP \fIvar\fP \fB] [ \-t \fP \fIto\fP \fB| \-T\fP \fITO\fP\fB]\fP
315
\fB [ \-a | \-s\fP \fIsess\fP \fB\&.\&.\&. | \-l\fP \fIsess\fP\fB,\&.\&.\&. ]\fP \fIpattern\fP \&.\&.\&.
316
Wait for input matching any of the given \fIpattern\fPs from any of the
317
specified sessions\&. Input is ignored until an input line matches one of
318
the given patterns; at this point status zero is returned, the matching
319
line is stored in \fB$TCP_LINE\fP, and the full set of lines read during the
320
call to \fBtcp_expect\fP is stored in the array \fB$tcp_expect_lines\fP\&.
323
Sessions are specified in the same way as \fBtcp_read\fP: the default is to
324
use the current session, otherwise the sessions specified by \fB\-a\fP,
325
\fB\-s\fP, or \fB\-l\fP are used\&.
327
Each \fIpattern\fP is a standard zsh extended\-globbing pattern; note that it
328
needs to be quoted to avoid it being expanded immediately by filename
329
generation\&. It must match the full line, so to match a substring there
330
must be a `\fB*\fP\&' at the start and end\&. The line matched against includes
331
the \fB$TCP_PROMPT\fP added by \fBtcp_read\fP\&. It is possible to include the
332
globbing flags `\fB#b\fP\&' or `\fB#m\fP' in the patterns to make backreferences
333
available in the parameters \fB$MATCH\fP, \fB$match\fP, etc\&., as described in
334
the base zsh documentation on pattern matching\&.
336
Unlike \fBtcp_read\fP, the default behaviour of \fBtcp_expect\fP is to block
337
indefinitely until the required input is found\&. This can be modified by
338
specifying a timeout with \fB\-t\fP or \fB\-T\fP; these function as in
339
\fBtcp_read\fP, specifying a per\-read or overall timeout, respectively, in
340
seconds, as an integer or floating\-point number\&. As \fBtcp_read\fP, the
341
function returns status 2 if a timeout occurs\&.
343
The function returns as soon as any one of the patterns given match\&. If
344
the caller needs to know which of the patterns matched, the option \fB\-p\fP
345
\fIvar\fP can be used; on return, \fB$var\fP is set to the number of the
346
pattern using ordinary zsh indexing, i\&.e\&. the first is 1, and so on\&. Note
347
the absence of a `\fB$\fP\&' in front of \fIvar\fP\&. To avoid clashes, the
348
parameter cannot begin with `\fB_expect\fP\&'\&.
350
The option \fB\-q\fP is passed directly down to \fBtcp_read\fP\&.
352
As all input is done via \fBtcp_read\fP, all the usual rules about output of
353
lines read apply\&. One exception is that the parameter \fB$tcp_lines\fP will
354
only reflect the line actually matched by \fBtcp_expect\fP; use
355
\fB$tcp_expect_lines\fP for the full set of lines read during the function
360
This is a simple\-minded function to accept a TCP connection and execute a
361
command with I/O redirected to the connection\&. Extreme caution should be
362
taken as there is no security whatsoever and this can leave your computer
363
open to the world\&. Ideally, it should only be used behind a firewall\&.
366
The first argument is a TCP port on which the function will listen\&.
368
The remaining arguments give a command and its arguments to execute with
369
standard input, standard output and standard error redirected to the
370
file descriptor on which the TCP session has been accepted\&.
371
If no command is given, a new zsh is started\&. This gives everyone on
372
your network direct access to your account, which in many cases will be a
375
The command is run in the background, so \fBtcp_proxy\fP can then accept new
376
connections\&. It continues to accept new connections until interrupted\&.
379
\fBtcp_spam [\-ertv] [ \-a | \-s \fP \fIsess\fP \fB| \-l\fP \fIsess\fP\fB,\&.\&.\&. ]\fP \fIcmd\fP \fB\&.\&.\&.\fP
380
Execute `\fIcmd\fP \fB\&.\&.\&.\fP\&' for each session in turn\&. Note this executes
381
the command and arguments; it does not send the command line as data
382
unless the \fB\-t\fP (transmit) option is given\&.
385
The sessions may be selected explicitly with the standard \fB\-a\fP, \fB\-s\fP or
386
\fB\-l\fP options, or may be chosen implicitly\&. If none of the three options
387
is given the rules are: first, if the array \fB$tcp_spam_list\fP is set, this
388
is taken as the list of sessions, otherwise all sessions are taken\&.
389
Second, any sessions given in the array \fB$tcp_no_spam_list\fP are removed
390
from the list of sessions\&.
392
Normally, any sessions added by the `\fB\-a\fP\&' flag or when all sessions are
393
chosen implicitly are spammed in alphabetic order; sessions given by the
394
\fB$tcp_spam_list\fP array or on the command line are spammed in the order
395
given\&. The \fB\-r\fP flag reverses the order however it was arrived it\&.
397
The \fB\-v\fP flag specifies that a \fB$TCP_PROMPT\fP will be output before each
398
session\&. This is output after any modification to TCP_SESS by the
399
user\-defined \fBtcp_on_spam\fP function described below\&. (Obviously that
400
function is able to generate its own output\&.)
402
If the option \fB\-e\fP is present, the line given as \fIcmd \&.\&.\&.\fP is executed
403
using \fBeval\fP, otherwise it is executed without any further processing\&.
407
This is a fairly simple\-minded attempt to force input to the line editor to
408
go straight to the default TCP_SESSION\&.
411
An escape string, \fB$TCP_TALK_ESCAPE\fP, default `:\&', is used to allow
412
access to normal shell operation\&. If it is on its own at the start of the
413
line, or followed only by whitespace, the line editor returns to normal
414
operation\&. Otherwise, the string and any following whitespace are skipped
415
and the remainder of the line executed as shell input without any change of
416
the line editor\&'s operating mode\&.
418
The current implementation is somewhat deficient in terms of use of the
419
command history\&. For this reason, many users will prefer to use some form
420
of alternative approach for sending data easily to the current session\&.
421
One simple approach is to alias some special character (such as `\fB%\fP\&') to
422
`\fBtcp_command \-\fP\fB\-\fP\&'\&.
426
The sole argument is an integer or floating point number which gives the
427
seconds to delay\&. The shell will do nothing for that period except wait
428
for input on all TCP sessions by calling \fBtcp_read \-a\fP\&. This is similar
429
to the interactive behaviour at the command prompt when zle handlers are
432
.SS "`One\-shot\&' file transfer"
436
\fBtcp_point\fP \fIport\fP
439
\fBtcp_shoot\fP \fIhost\fP \fIport\fP
440
This pair of functions provide a simple way to transfer a file between
441
two hosts within the shell\&. Note, however, that bulk data transfer is
442
currently done using \fBcat\fP\&. \fBtcp_point\fP reads any data arriving at
443
\fIport\fP and sends it to standard output; \fBtcp_shoot\fP connects to
444
\fIport\fP on \fIhost\fP and sends its standard input\&. Any unused \fIport\fP
445
may be used; the standard mechanism for picking a port is to think of a
446
random four\-digit number above 1024 until one works\&.
449
To transfer a file from host \fBwoodcock\fP to host \fBspringes\fP, on
454
\fBtcp_point 8091 >output_file\fP
458
and on \fBwoodcock\fP:
462
\fBtcp_shoot springes 8091 <input_file\fP
466
As these two functions do not require \fBtcp_open\fP to set up a TCP
467
connection first, they may need to be autoloaded separately\&.
471
.SH "TCP USER\-DEFINED FUNCTIONS"
473
Certain functions, if defined by the user, will be called by the function
474
system in certain contexts\&. This facility depends on the module
475
\fBzsh/parameter\fP, which is usually available in interactive shells as the
476
completion system depends on it\&. None of the functions need be defined;
477
they simply provide convenient hooks when necessary\&.
479
Typically, these are called after the requested action has been taken, so
480
that the various parameters will reflect the new state\&.
485
\fBtcp_on_alias\fP \fIalias\fP \fIfd\fP
486
When an alias is defined, this function will be called with two arguments:
487
the name of the alias, and the file descriptor of the corresponding session\&.
489
\fBtcp_on_awol\fP \fIsess\fP \fIfd\fP
490
If the function \fBtcp_fd_handler\fP is handling input from the line
491
editor and detects that the file descriptor is no longer reusable,
492
by default it removes it from the list of file descriptors handled
493
by this method and prints a message\&. If the function \fBtcp_on_awol\fP
494
is defined it is called immediately before this point\&. It may
495
return status 100, which indicates that the normal handling should
496
still be performed; any other return status indicates that no further
497
action should be taken and the \fBtcp_fd_handler\fP should return
498
immediately with the given status\&. Typically the action of \fBtcp_on_awol\fP
499
will be to close the session\&.
502
The variable \fBTCP_INVALIDATE_ZLE\fP will be a non\-empty string if it is
503
necessary to invalidate the line editor display using `\fBzle \-I\fP\&' before
504
printing output from the function\&.
506
(`AWOL\&' is military jargon for `absent without leave' or some
507
variation\&. It has no pre\-existing technical meaning known to the author\&.)
510
\fBtcp_on_close\fP \fIsess\fP \fIfd\fP
511
This is called with the name of a session being closed and the file
512
descriptor which corresponded to that session\&. Both will be invalid by
513
the time the function is called\&.
515
\fBtcp_on_open\fP \fIsess\fP \fIfd\fP
516
This is called after a new session has been defined with the session name
517
and file descriptor as arguments\&. If it returns a non\-zero status,
518
opening the session is assumed to fail and the session is closed
519
again; however, \fBtcp_open\fP will continue to attempt to open any
520
remaining sessions given on the command line\&.
522
\fBtcp_on_rename\fP \fIoldsess\fP \fIfd\fP \fInewsess\fP
523
This is called after a session has been renamed with the three arguments
524
old session name, file descriptor, new session name\&.
526
\fBtcp_on_spam\fP \fIsess\fP \fIcommand\fP \fB\&.\&.\&.\fP
527
This is called once for each session spammed, just \fIbefore\fP a command is
528
executed for a session by \fBtcp_spam\fP\&. The arguments are the session name
529
followed by the command list to be executed\&. If \fBtcp_spam\fP was called
530
with the option \fB\-t\fP, the first command will be \fBtcp_send\fP\&.
533
This function is called after \fB$TCP_SESS\fP is set to reflect the session
534
to be spammed, but before any use of it is made\&. Hence it is possible to
535
alter the value of \fB$TCP_SESS\fP within this function\&. For example, the
536
session arguments to \fBtcp_spam\fP could include extra information to be
537
stripped off and processed in \fBtcp_on_spam\fP\&.
539
If the function sets the parameter \fB$REPLY\fP to `\fBdone\fP\&', the command
540
line is not executed; in addition, no prompt is printed for the \fB\-v\fP
541
option to \fBtcp_spam\fP\&.
544
\fBtcp_on_unalias\fP \fIalias\fP \fIfd\fP
545
This is called with the name of an alias and the corresponding session\&'s
546
file descriptor after an alias has been deleted\&.
548
.SH "TCP UTILITY FUNCTIONS"
550
The following functions are used by the TCP function system but will rarely
551
if ever need to be called directly\&.
557
This is the function installed by \fBtcp_open\fP for handling input from
558
within the line editor, if that is required\&. It is in the format
559
documented for the builtin `\fBzle \-F\fP\&' in
564
While active, the function sets the parameter \fBTCP_HANDLER_ACTIVE\fP to 1\&.
565
This allows shell code called internally (for example, by setting
566
\fBtcp_on_read\fP) to tell if is being called when the shell is otherwise
567
idle at the editor prompt\&.
570
\fBtcp_output [ \-q ] \-P\fP \fIprompt\fP \fB\-F\fP \fIfd\fP \fB\-S\fP \fIsess\fP
571
This function is used for both logging and handling output to standard
572
output, from within \fBtcp_read\fP and (if \fB$TCP_OUTPUT\fP is set)
576
The \fIprompt\fP to use is specified by \fB\-P\fP; the default is the empty
577
string\&. It can contain:
582
Expands to 1 if the session is the current session, otherwise 0\&. Used
583
with ternary expressions such as `\fB%(c\&.\-\&.+)\fP\&' to
584
output `\fB+\fP\&' for the current session and `\fB\-\fP' otherwise\&.
587
Replaced by the session\&'s file descriptor\&.
590
Replaced by the session name\&.
593
Replaced by a single `\fB%\fP\&'\&.
595
The option \fB\-q\fP suppresses output to standard output, but not to any log
596
files which are configured\&.
598
The \fB\-S\fP and \fB\-F\fP options are used to pass in the session name and file
599
descriptor for possible replacement in the prompt\&.
603
.SH "TCP USER PARAMETERS"
605
Parameters follow the usual convention that uppercase is used for scalars
606
and integers, while lowercase is used for normal and associative array\&.
607
It is always safe for user code to read these parameters\&. Some parameters
608
may also be set; these are noted explicitly\&. Others are included in this
609
group as they are set by the function system for the user\&'s benefit,
610
i\&.e\&. setting them is typically not useful but is benign\&.
612
It is often also useful to make settable parameters local to a function\&.
613
For example, `\fBlocal TCP_SILENT=1\fP\&' specifies that data read during the
614
function call will not be printed to standard output, regardless of the
615
setting outside the function\&. Likewise, `\fBlocal TCP_SESS=\fP\fIsess\fP\&'
616
sets a session for the duration of a function, and `\fBlocal
617
TCP_PROMPT=\fP\&' specifies that no prompt is used for input during the
623
\fBtcp_expect_lines\fP
624
Array\&. The set of lines read during the last call to \fBtcp_expect\fP,
625
including the last (\fB$TCP_LINE\fP)\&.
628
Array\&. May be set directly\&. A set of extended globbing patterns which,
629
if matched in \fBtcp_output\fP, will cause the line not to be printed to
630
standard output\&. The patterns should be defined as described for the
631
arguments to \fBtcp_expect\fP\&. Output of line to log files is not affected\&.
633
\fBTCP_HANDLER_ACTIVE\fP
634
Scalar\&. Set to 1 within \fBtcp_fd_handler\fP to indicate to functions
635
called recursively that they have been called during an editor session\&.
639
The last line read by \fBtcp_read\fP, and hence also \fBtcp_expect\fP\&.
642
The file descriptor from which \fB$TCP_LINE\fP was read\&.
643
\fB${tcp_by_fd[$TCP_LINE_FD]}\fP will give the corresponding session name\&.
646
Array\&. The set of lines read during the last call to \fBtcp_read\fP,
647
including the last (\fB$TCP_LINE\fP)\&.
650
May be set directly, although it is also controlled by \fBtcp_log\fP\&.
651
The name of a file to which output from all sessions will be sent\&.
652
The output is proceeded by the usual \fB$TCP_PROMPT\fP\&. If it is not an
653
absolute path name, it will follow the user\&'s current directory\&.
656
May be set directly, although it is also controlled by \fBtcp_log\fP\&.
657
The prefix for a set of files to which output from each session separately
658
will be sent; the full filename is \fB${TCP_LOG_SESS}\&.\fP\fIsess\fP\&.
659
Output to each file is raw; no prompt is added\&. If it is not an absolute
660
path name, it will follow the user\&'s current directory\&.
662
\fBtcp_no_spam_list\fP
663
Array\&. May be set directly\&. See \fBtcp_spam\fP for how this is used\&.
666
May be set directly\&. If a non\-empty string, any data sent to a session by
667
\fBtcp_send\fP will be logged\&. This parameter gives the prompt to be used
668
in a file specified by \fB$TCP_LOG\fP but not in a file generated from
669
\fB$TCP_LOG_SESS\fP\&. The prompt string has the same format as
670
\fBTCP_PROMPT\fP and the same rules for its use apply\&.
673
May be set directly\&. Used as the prefix for data read by \fBtcp_read\fP
674
which is printed to standard output or to the log file given by
675
\fB$TCP_LOG\fP, if any\&. Any `\fB%s\fP\&', `\fB%f\fP' or `\fB%%\fP' occurring in the
676
string will be replaced by the name of the session, the session\&'s
677
underlying file descriptor, or a single `\fB%\fP\&', respectively\&. The
678
expression `\fB%c\fP\&' expands to 1 if the session being read is the current
679
session, else 0; this is most useful in ternary expressions such as
680
`\fB%(c\&.\-\&.+)\fP\&' which outputs `\fB+\fP' if the session is
681
the current one, else `\fB\-\fP\&'\&.
684
May be set directly\&. If this has non\-zero length, \fBtcp_read\fP will give
685
some limited diagnostics about data being read\&.
687
\fBTCP_SECONDS_START\fP
688
This value is created and initialised to zero by tcp_open\&.
691
The functions \fBtcp_read\fP and \fBtcp_expect\fP use the shell\&'s
692
\fBSECONDS\fP parameter for their own timing purposes\&. If that parameter
693
is not of floating point type on entry to one of the functions, it will
694
create a local parameter \fBSECONDS\fP which is floating point and set the
695
parameter \fBTCP_SECONDS_START\fP to the previous value of \fB$SECONDS\fP\&.
696
If the parameter is already floating point, it is used without a local
697
copy being created and \fBTCP_SECONDS_START\fP is not set\&. As the global
698
value is zero, the shell elapsed time is guaranteed to be the sum of
699
\fB$SECONDS\fP and \fB$TCP_SECONDS_START\fP\&.
701
This can be avoided by setting \fBSECONDS\fP globally to a floating point
702
value using `\fBtypeset \-F SECONDS\fP\&'; then the TCP functions will never
703
make a local copy and never set \fBTCP_SECONDS_START\fP to a non\-zero value\&.
707
May be set directly\&. The current session; must refer to one of the
708
sessions established by \fBtcp_open\fP\&.
711
May be set directly, although it is also controlled by \fBtcp_log\fP\&.
712
If of non\-zero length, data read by \fBtcp_read\fP will not be written to
713
standard output, though may still be written to a log file\&.
716
Array\&. May be set directly\&. See the description of the function
717
\fBtcp_spam\fP for how this is used\&.
719
\fBTCP_TALK_ESCAPE\fP
720
May be set directly\&. See the description of the function \fBtcp_talk\fP for
724
May be set directly\&. Currently this is only used by the function
725
\fBtcp_command\fP, see above\&.
727
.SH "TCP USER\-DEFINED PARAMETERS"
729
The following parameters are not set by the function system, but have
730
a special effect if set by the user\&.
736
This should be an associative array; if it is not, the behaviour is
737
undefined\&. Each key is the name of a shell function or other command,
738
and the corresponding value is a shell pattern (using \fBEXTENDED_GLOB\fP)\&.
739
Every line read from a TCP session directly or indirectly using
740
\fBtcp_read\fP (which includes lines read by \fBtcp_expect\fP) is compared
741
against the pattern\&. If the line matches, the command given in the key is
742
called with two arguments: the name of the session from which the line was
743
read, and the line itself\&.
746
If any function called to handle a line returns a non\-zero status, the
747
line is not output\&. Thus a \fBtcp_on_read\fP handler containing only
748
the instruction `\fBreturn 1\fP\&' can be used to suppress output of
749
particular lines (see, however, \fBtcp_filter\fP above)\&. However, the line
750
is still stored in \fBTCP_LINE\fP and \fBtcp_lines\fP; this occurs after all
751
\fBtcp_on_read\fP processing\&.
755
.SH "TCP UTILITY PARAMETERS"
757
These parameters are controlled by the function system; they may be read
758
directly, but should not usually be set by user code\&.
764
Associative array\&. The keys are the names of sessions established with
765
\fBtcp_open\fP; each value is a space\-separated list of aliases which refer
769
Associative array\&. The keys are session file descriptors; each
770
value is the name of that session\&.
773
Associative array\&. The keys are the names of sessions; each value is the
774
file descriptor associated with that session\&.
778
Here is a trivial example using a remote calculator\&.
780
TO create a calculator server on port 7337 (see the \fBdc\fP manual page for
781
quite how infuriating the underlying command is):
785
\fBtcp_proxy 7337 dc\fP
789
To connect to this from the same host with a session also named `\fBdc\fP\&':
793
\fBtcp_open localhost 7337 dc\fP
797
To send a command to the remote session and wait a short while for output
798
(assuming \fBdc\fP is the current session):
802
\fBtcp_command 2 4 + p\fP
806
To close the session:
814
The \fBtcp_proxy\fP needs to be killed to be stopped\&. Note this will not
815
usually kill any connections which have already been accepted, and also
816
that the port is not immediately available for reuse\&.
818
The following chunk of code puts a list of sessions into an xterm header,
819
with the current session followed by a star\&.
823
\fBprint \-n "\e033]2;TCP:" ${(k)tcp_by_name:/$TCP_SESS/$TCP_SESS\e*} "\ea"\fP
829
The function \fBtcp_read\fP uses the shell\&'s normal \fBread\fP builtin\&. As
830
this reads a complete line at once, data arriving without a terminating
831
newline can cause the function to block indefinitely\&.
833
Though the function suite works well for interactive use and for data
834
arriving in small amounts, the performance when large amounts of data are
835
being exchanged is likely to be extremely poor\&.