/usr/share/doc/zsh-common/html/TCP-Function-System.html is in zsh-doc 5.0.2-3ubuntu6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- Created on December 21, 2012 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>zsh: 24. TCP Function System</title>
<meta name="description" content="zsh: 24. TCP Function System">
<meta name="keywords" content="zsh: 24. TCP Function System">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="TCP-Function-System"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="Calendar-Function-System.html#Calendar-Bugs" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Description-2" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="Calendar-Function-System.html#Calendar-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="TCP-Function-System-1"></a>
<h1 class="chapter">24. TCP Function System</h1>
<a name="index-TCP-function-system"></a>
<a name="index-ztcp_002c-function-system-based-on"></a>
<hr size="6">
<a name="Description-2"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Function-System" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Functions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">24.1 Description</h2>
<p>A module <tt>zsh/net/tcp</tt> is provided to provide network I/O over
TCP/IP from within the shell; see its description in
<a href="Zsh-Modules.html#Zsh-Modules">Zsh Modules</a>
. 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
<tt>zsh/net/tcp</tt> module, the <tt>zsh/zselect</tt> module is used to implement
timeouts on read operations. For troubleshooting tips, consult the
corresponding advice for the <tt>zftp</tt> functions described in
<a href="Zftp-Function-System.html#Zftp-Function-System">Zftp Function System</a>
.
</p>
<p>There are functions corresponding to the basic I/O operations open, close,
read and send, named <tt>tcp_open</tt> etc., as well as a function
<tt>tcp_expect</tt> for pattern match analysis of data read as input. The
system makes it easy to receive data from and send data to multiple named
sessions 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 logging, filtering and
configurable output prompts.
</p>
<p>To use the system where it is available, it should be enough to
‘<tt>autoload -U tcp_open</tt>’ and run <tt>tcp_open</tt> as documented below to
start a session. The <tt>tcp_open</tt> function will autoload the remaining
functions.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#TCP-Functions">24.2 TCP User Functions</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#TCP-Parameters">24.5 TCP User Parameters</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#TCP-Examples">24.8 TCP Examples</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#TCP-Bugs">24.9 TCP Bugs</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr size="6">
<a name="TCP-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Description-2" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="TCP-User-Functions"></a>
<h2 class="section">24.2 TCP User Functions</h2>
<hr size="6">
<a name="Basic-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Session-Management" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">24.2.1 Basic I/O</h3>
<dl compact="compact">
<dd><a name="index-tcp_005fopen"></a>
</dd>
<dt> <tt>tcp_open [-qz]</tt> <var>host port</var> <tt>[</tt> <var>sess</var> <tt>]</tt></dt>
<dt> <tt>tcp_open [-qz] [ -s</tt> <var>sess</var> <tt>| -l</tt> <var>sess</var><tt>,... ] ... </tt></dt>
<dt> <tt>tcp_open [-qz] [-a</tt> <var>fd</var> <tt>| -f</tt> <var>fd</var> <tt>] [</tt> <var>sess</var> <tt>]</tt></dt>
<dd><p>Open a new session. In the first and simplest form, open a TCP connection
to host <var>host</var> at port <var>port</var>; numeric and symbolic forms are
understood for both.
</p>
<p>If <var>sess</var> is given, this becomes the name of the session which can be
used to refer to multiple different TCP connections. If <var>sess</var> is
not given, the function will invent a numeric name value (note this is
<em>not</em> the same as the file descriptor to which the session is attached).
It is recommended that session names not include ‘funny’ characters, where
funny characters are not well-defined but certainly do not include
alphanumerics or underscores, and certainly do include whitespace.
</p>
<p>In the second case, one or more sessions to be opened are given by name.
A single session name is given after <tt>-s</tt> and a comma-separated list
after <tt>-l</tt>; both options may be repeated as many times as necessary.
A failure to open any session causes <tt>tcp_open</tt> to abort.
The host and port are read from the file <tt>.ztcp_sessions</tt> in the same
directory as the user’s zsh initialisation files, i.e. usually the home
directory, but <tt>$ZDOTDIR</tt> 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.
</p>
<p>The third form allows passive and fake TCP connections. If the option
<tt>-a</tt> 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 ‘<tt>ztcp -l</tt> <var>port</var>’ will create one with the
file descriptor stored in the parameter <tt>$REPLY</tt>. The listening port can
be closed with ‘<tt>ztcp -c</tt> <var>fd</var>’. A call to ‘<tt>tcp_open -a</tt> <var>fd</var>’
will block until a remote TCP connection is made to <var>port</var> on the local
machine. At this point, a session is created in the usual way and is
largely indistinguishable from an active connection created with one of the
first two forms.
</p>
<p>If the option <tt>-f</tt> 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.
</p>
<p>If the option <tt>-q</tt> is given with any of the three forms, <tt>tcp_open</tt>
will not print informational messages, although it will in any case exit
with an appropriate status.
</p>
<p>If the line editor (zle) is in use, which is typically the case if the
shell is interactive, <tt>tcp_open</tt> installs a handler inside <tt>zle</tt> 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 <tt>-z</tt> to
any of the forms of <tt>tcp_open</tt> 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 <tt>vared</tt> builtin. The option has no effect if zle is not
active; ‘<tt>[[ -o zle]]</tt>’ will test for this.
</p>
<p>The first session to be opened becomes the current session and subsequent
calls to <tt>tcp_open</tt> do not change it. The current session is stored
in the parameter <tt>$TCP_SESS</tt>; see below for more detail about the
parameters used by the system.
</p>
<p>The function <tt>tcp_on_open</tt>, if defined, is called when a session
is opened. See the description below.
</p>
<a name="index-tcp_005fclose"></a>
</dd>
<dt> <tt>tcp_close [-qn] [ -a | -l</tt> <var>sess</var><tt>,... |</tt> <var>sess</var> <tt>... ]</tt></dt>
<dd><p>Close the named sessions, or the current session if none is given,
or all open sessions if <tt>-a</tt> is given. The options <tt>-l</tt> and <tt>-s</tt> are
both handled for consistency with <tt>tcp_open</tt>, although the latter is
redundant.
</p>
<p>If the session being closed is the current one, <tt>$TCP_SESS</tt> is unset,
leaving no current session, even if there are other sessions still open.
</p>
<p>If the session was opened with <tt>tcp_open -f</tt>, 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 <tt>-n</tt> is given, no attempt will be made to
close file descriptors in this case. The <tt>-n</tt> option is not used for
genuine <tt>ztcp</tt> session; the file descriptors are always closed with the
session.
</p>
<p>If the option <tt>-q</tt> is given, no informational messages will be printed.
</p>
<a name="index-tcp_005fread"></a>
</dd>
<dt> <tt>tcp_read [-bdq] [ -t</tt> <var>TO</var> <tt>] [ -T</tt> <var>TO</var> <tt>]</tt></dt>
<dt> <tt>[ -a | -u</tt> <var>fd</var> <tt>... | -l</tt> <var>sess</var><tt>,... | -s</tt> <var>sess</var> <tt>...]</tt></dt>
<dd><p>Perform a read operation on the current session, or on a list of
sessions if any are given with <tt>-u</tt>, <tt>-l</tt> or <tt>-s</tt>, or all open
sessions if the option <tt>-a</tt> is given. Any of the <tt>-u</tt>, <tt>-l</tt> or
<tt>-s</tt> options may be repeated or mixed together. The <tt>-u</tt> option
specifies a file descriptor directly (only those managed by this system
are useful), the other two specify sessions as described for
<tt>tcp_open</tt> above.
</p>
<p>The function checks for new data available on all the sessions listed.
Unless the <tt>-b</tt> 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 <tt>$TCP_LINE</tt>, and displayed to standard output
unless <tt>$TCP_SILENT</tt> contains a non-empty string. When printed to
standard output the string <tt>$TCP_PROMPT</tt> will be shown at the start of
the line; the default form for this includes the name of the session being
read. See below for more information on these parameters. In this mode,
<tt>tcp_read</tt> can be called repeatedly until it returns status 2 which
indicates all pending input from all specified sessions has been handled.
</p>
<p>With the option <tt>-b</tt>, 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.
</p>
<p>The option <tt>-d</tt> indicates that all pending input should be drained. In
this case <tt>tcp_read</tt> may process multiple lines in the manner given
above; only the last is stored in <tt>$TCP_LINE</tt>, but the complete set is
stored in the array <tt>$tcp_lines</tt>. This is cleared at the start of each
call to <tt>tcp_read</tt>.
</p>
<p>The options <tt>-t</tt> and <tt>-T</tt> specify a timeout in seconds, which may be a
floating point number for increased accuracy. With <tt>-t</tt> the timeout is
applied before each line read. With <tt>-T</tt>, the timeout applies to the
overall operation, possibly including multiple read operations if the
option <tt>-d</tt> is present; without this option, there is no distinction
between <tt>-t</tt> and <tt>-T</tt>.
</p>
<p>The function does not print informational messages, but if the option
<tt>-q</tt> is given, no error message is printed for a non-existent session.
</p>
<p>A return status of 2 indicates a timeout or no data to read. Any other
non-zero return status indicates some error condition.
</p>
<p>See <tt>tcp_log</tt> for how to control where data is sent by <tt>tcp_read</tt>.
</p>
<a name="index-tcp_005fsend"></a>
</dd>
<dt> <tt>tcp_send [-cnq] [ -s</tt> <var>sess</var> <tt>| -l</tt> <var>sess</var><tt>,... ]</tt> <var>data</var> <tt>...</tt></dt>
<dt> <tt>tcp_send [-cnq] -a</tt> <var>data</var> <tt>...</tt></dt>
<dd><p>Send the supplied data strings to all the specified sessions in turn. The
underlying operation differs little from a ‘<tt>print -r</tt>’ to the session’s
file descriptor, although it attempts to prevent the shell from dying owing
to a <tt>SIGPIPE</tt> caused by an attempt to write to a defunct session.
</p>
<p>The option <tt>-c</tt> causes <tt>tcp_send</tt> to behave like <tt>cat</tt>. 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 <var>data</var>
arguments to individual <tt>tcp_send</tt> commands.
</p>
<p>The option <tt>-n</tt> prevents <tt>tcp_send</tt> from putting a newline at the end
of the data strings.
</p>
<p>The remaining options all behave as for <tt>tcp_read</tt>.
</p>
<p>The data arguments are not further processed once they have been passed to
<tt>tcp_send</tt>; they are simply passed down to <tt>print -r</tt>.
</p>
<p>If the parameter <tt>$TCP_OUTPUT</tt> 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 <tt>$TCP_OUTPUT</tt> in front where appropriate, much
in the manner of <tt>$TCP_PROMPT</tt>.
</p>
</dd>
</dl>
<hr size="6">
<a name="Session-Management"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Basic-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">24.2.2 Session Management</h3>
<dl compact="compact">
<dd><a name="index-tcp_005falias"></a>
</dd>
<dt> <tt>tcp_alias [-q]</tt> <var>alias</var><tt>=</tt><var>sess</var> <tt>...</tt></dt>
<dt> <tt>tcp_alias [-q] [</tt> <var>alias</var> <tt>] ...</tt></dt>
<dt> <tt>tcp_alias -d [-q]</tt> <var>alias</var> <tt>...</tt></dt>
<dd><p>This function is not particularly well tested.
</p>
<p>The first form creates an alias for a session name; <var>alias</var> can then be
used to refer to the existing session <var>sess</var>. As many aliases may be
listed as required.
</p>
<p>The second form lists any aliases specified, or all aliases if none.
</p>
<p>The third form deletes all the aliases listed. The underlying sessions are
not affected.
</p>
<p>The option <tt>-q</tt> suppresses an inconsistently chosen subset of error
messages.
</p>
<a name="index-tcp_005flog"></a>
</dd>
<dt> <tt>tcp_log [-asc] [ -n | -N ] [</tt> <var>logfile</var> <tt>]</tt></dt>
<dd><p>With an argument <var>logfile</var>, all future input from <tt>tcp_read</tt> will be
logged to the named file. Unless <tt>-a</tt> (append) is given, this file will
first be truncated or created empty. With no arguments, show the current
status of logging.
</p>
<p>With the option <tt>-s</tt>, per-session logging is enabled. Input from
<tt>tcp_read</tt> is output to the file <var>logfile</var>.<var>sess</var>. As the
session is automatically discriminated by the filename, the contents are
raw (no <tt>$TCP_PROMPT</tt>). The option <tt>-a</tt> applies as above.
Per-session logging and logging of all data in one file are not mutually
exclusive.
</p>
<p>The option <tt>-c</tt> closes all logging, both complete and per-session logs.
</p>
<p>The options <tt>-n</tt> and <tt>-N</tt> respectively turn off or restore output of
data read by <tt>tcp_read</tt> to standard output; hence ‘<tt>tcp_log -cn</tt>’ turns
off all output by <tt>tcp_read</tt>.
</p>
<p>The function is purely a convenient front end to setting the parameters
<tt>$TCP_LOG</tt>, <tt>$TCP_LOG_SESS</tt>, <tt>$TCP_SILENT</tt>, which are described below.
</p>
<a name="index-tcp_005frename"></a>
</dd>
<dt> <tt>tcp_rename</tt> <var>old</var> <var>new</var></dt>
<dd><p>Rename session <var>old</var> to session <var>new</var>. The old name becomes invalid.
</p>
<a name="index-tcp_005fsess"></a>
</dd>
<dt> <tt>tcp_sess [</tt> <var>sess</var> <tt>[</tt> <var>command</var> <tt>... ] ]</tt></dt>
<dd><p>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 parameters <tt>$tcp_by_name</tt>, <tt>$tcp_by_fd</tt>
and <tt>$TCP_SESS</tt> is probably more convenient; see below.
</p>
<p>With a <var>sess</var> argument, set the current session to <var>sess</var>.
This is equivalent to changing <tt>$TCP_SESS</tt> directly.
</p>
<p>With additional arguments, temporarily set the current session while
executing the string <tt>command ...</tt>. The first argument is re-evaluated
so as to expand aliases etc., but the remaining arguments are passed
through as the appear to <tt>tcp_sess</tt>. The original session is restored
when <tt>tcp_sess</tt> exits.
</p>
</dd>
</dl>
<hr size="6">
<a name="Advanced-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Session-Management" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#g_t_0060One_002dshot_0027-file-transfer" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">24.2.3 Advanced I/O</h3>
<dl compact="compact">
<dd><a name="index-tcp_005fcommand"></a>
</dd>
<dt> <tt>tcp_command</tt> <var>send-options</var> <tt>...</tt> <var>send-arguments</var> <tt>...</tt></dt>
<dd><p>This is a convenient front-end to <tt>tcp_send</tt>. All arguments are passed
to <tt>tcp_send</tt>, then the function pauses waiting for data. While data is
arriving at least every <tt>$TCP_TIMEOUT</tt> (default 0.3) seconds, data is
handled and printed out according to the current settings. Status 0 is
always returned.
</p>
<p>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.
</p>
<a name="index-tcp_005fexpect"></a>
</dd>
<dt> <tt>tcp_expect [ -q ] [ -p</tt> <var>var</var> <tt>] [ -t </tt> <var>to</var> <tt>| -T</tt> <var>TO</var><tt>]</tt></dt>
<dt> <tt> [ -a | -s</tt> <var>sess</var> <tt>... | -l</tt> <var>sess</var><tt>,... ]</tt> <var>pattern</var> ...</dt>
<dd><p>Wait for input matching any of the given <var>pattern</var>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 <tt>$TCP_LINE</tt>, and the full set of lines read during the
call to <tt>tcp_expect</tt> is stored in the array <tt>$tcp_expect_lines</tt>.
</p>
<p>Sessions are specified in the same way as <tt>tcp_read</tt>: the default is to
use the current session, otherwise the sessions specified by <tt>-a</tt>,
<tt>-s</tt>, or <tt>-l</tt> are used.
</p>
<p>Each <var>pattern</var> 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 ‘<tt>*</tt>’ at the start and end. The line matched against includes
the <tt>$TCP_PROMPT</tt> added by <tt>tcp_read</tt>. It is possible to include the
globbing flags ‘<tt>#b</tt>’ or ‘<tt>#m</tt>’ in the patterns to make backreferences
available in the parameters <tt>$MATCH</tt>, <tt>$match</tt>, etc., as described in
the base zsh documentation on pattern matching.
</p>
<p>Unlike <tt>tcp_read</tt>, the default behaviour of <tt>tcp_expect</tt> is to block
indefinitely until the required input is found. This can be modified by
specifying a timeout with <tt>-t</tt> or <tt>-T</tt>; these function as in
<tt>tcp_read</tt>, specifying a per-read or overall timeout, respectively, in
seconds, as an integer or floating-point number. As <tt>tcp_read</tt>, the
function returns status 2 if a timeout occurs.
</p>
<p>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 <tt>-p</tt>
<var>var</var> can be used; on return, <tt>$var</tt> 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 ‘<tt>$</tt>’ in front of <var>var</var>. To avoid clashes, the
parameter cannot begin with ‘<tt>_expect</tt>’.
</p>
<p>The option <tt>-q</tt> is passed directly down to <tt>tcp_read</tt>.
</p>
<p>As all input is done via <tt>tcp_read</tt>, all the usual rules about output of
lines read apply. One exception is that the parameter <tt>$tcp_lines</tt> will
only reflect the line actually matched by <tt>tcp_expect</tt>; use
<tt>$tcp_expect_lines</tt> for the full set of lines read during the function
call.
</p>
<a name="index-tcp_005fproxy"></a>
</dd>
<dt> <tt>tcp_proxy</tt></dt>
<dd><p>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 security whatsoever and this can leave your computer
open to the world. Ideally, it should only be used behind a firewall.
</p>
<p>The first argument is a TCP port on which the function will listen.
</p>
<p>The remaining arguments give a command and its arguments to execute with
standard input, 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.
</p>
<p>The command is run in the background, so <tt>tcp_proxy</tt> can then accept new
connections. It continues to accept new connections until interrupted.
</p>
<a name="index-tcp_005fspam"></a>
</dd>
<dt> <tt>tcp_spam [-ertv] [ -a | -s </tt> <var>sess</var> <tt>| -l</tt> <var>sess</var><tt>,... ]</tt> <var>cmd</var> <tt>...</tt></dt>
<dd><p>Execute ‘<var>cmd</var> <tt>...</tt>’ for each session in turn. Note this executes
the command and arguments; it does not send the command line as data
unless the <tt>-t</tt> (transmit) option is given.
</p>
<p>The sessions may be selected explicitly with the standard <tt>-a</tt>, <tt>-s</tt> or
<tt>-l</tt> options, or may be chosen implicitly. If none of the three options
is given the rules are: first, if the array <tt>$tcp_spam_list</tt> is set, this
is taken as the list of sessions, otherwise all sessions are taken.
Second, any sessions given in the array <tt>$tcp_no_spam_list</tt> are removed
from the list of sessions.
</p>
<p>Normally, any sessions added by the ‘<tt>-a</tt>’ flag or when all sessions are
chosen implicitly are spammed in alphabetic order; sessions given by the
<tt>$tcp_spam_list</tt> array or on the command line are spammed in the order
given. The <tt>-r</tt> flag reverses the order however it was arrived it.
</p>
<p>The <tt>-v</tt> flag specifies that a <tt>$TCP_PROMPT</tt> will be output before each
session. This is output after any modification to TCP_SESS by the
user-defined <tt>tcp_on_spam</tt> function described below. (Obviously that
function is able to generate its own output.)
</p>
<p>If the option <tt>-e</tt> is present, the line given as <var>cmd ...</var> is executed
using <tt>eval</tt>, otherwise it is executed without any further processing.
</p>
<a name="index-tcp_005ftalk"></a>
</dd>
<dt> <tt>tcp_talk</tt></dt>
<dd><p>This is a fairly simple-minded attempt to force input to the line editor to
go straight to the default TCP_SESSION.
</p>
<p>An escape string, <tt>$TCP_TALK_ESCAPE</tt>, 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.
</p>
<p>The current implementation is somewhat deficient in terms of use of the
command history. For this reason, many users will prefer to use some form
of alternative approach for sending data easily to the current session.
One simple approach is to alias some special character (such as ‘<tt>%</tt>’) to
‘<tt>tcp_command -</tt><tt>-</tt>’.
</p>
<a name="index-tcp_005fwait"></a>
</dd>
<dt> <tt>tcp_wait</tt></dt>
<dd><p>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 <tt>tcp_read -a</tt>. This is similar
to the interactive behaviour at the command prompt when zle handlers are
installed.
</p>
</dd>
</dl>
<hr size="6">
<a name="g_t_0060One_002dshot_0027-file-transfer"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Advanced-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-User_002ddefined-Functions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">24.2.4 ‘One-shot’ file transfer</h3>
<dl compact="compact">
<dt> <tt>tcp_point</tt> <var>port</var></dt>
<dt> <tt>tcp_shoot</tt> <var>host</var> <var>port</var></dt>
<dd><p>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 <tt>cat</tt>. <tt>tcp_point</tt> reads any data arriving at
<var>port</var> and sends it to standard output; <tt>tcp_shoot</tt> connects to
<var>port</var> on <var>host</var> and sends its standard input. Any unused <var>port</var>
may be used; the standard mechanism for picking a port is to think of a
random four-digit number above 1024 until one works.
</p>
<p>To transfer a file from host <tt>woodcock</tt> to host <tt>springes</tt>, on
<tt>springes</tt>:
</p>
<table><tr><td> </td><td><pre class="example">tcp_point 8091 >output_file
</pre></td></tr></table>
<p>and on <tt>woodcock</tt>:
</p>
<table><tr><td> </td><td><pre class="example">tcp_shoot springes 8091 <input_file
</pre></td></tr></table>
<p>As these two functions do not require <tt>tcp_open</tt> to set up a TCP
connection first, they may need to be autoloaded separately.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-User_002ddefined-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#g_t_0060One_002dshot_0027-file-transfer" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Utility-Functions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">24.3 TCP User-defined Functions</h2>
<p>Certain functions, if defined by the user, will be called by the function
system in certain contexts. This facility depends on the module
<tt>zsh/parameter</tt>, which is usually available in interactive shells as the
completion system depends on it. None of the functions need be defined;
they simply provide convenient hooks when necessary.
</p>
<p>Typically, these are called after the requested action has been taken, so
that the various parameters will reflect the new state.
</p>
<dl compact="compact">
<dd><a name="index-tcp_005fon_005falias"></a>
</dd>
<dt> <tt>tcp_on_alias</tt> <var>alias</var> <var>fd</var></dt>
<dd><p>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.
</p>
<a name="index-tcp_005fon_005fawol"></a>
</dd>
<dt> <tt>tcp_on_awol</tt> <var>sess</var> <var>fd</var></dt>
<dd><p>If the function <tt>tcp_fd_handler</tt> 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 <tt>tcp_on_awol</tt>
is defined it is called immediately before this point. It may
return status 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 <tt>tcp_fd_handler</tt> should return
immediately with the given status. Typically the action of <tt>tcp_on_awol</tt>
will be to close the session.
</p>
<p>The variable <tt>TCP_INVALIDATE_ZLE</tt> will be a non-empty string if it is
necessary to invalidate the line editor display using ‘<tt>zle -I</tt>’ before
printing output from the function.
</p>
<p>(‘AWOL’ is military jargon for ‘absent without leave’ or some
variation. It has no pre-existing technical meaning known to the author.)
</p>
<a name="index-tcp_005fon_005fclose"></a>
</dd>
<dt> <tt>tcp_on_close</tt> <var>sess</var> <var>fd</var></dt>
<dd><p>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.
</p>
<a name="index-tcp_005fon_005fopen"></a>
</dd>
<dt> <tt>tcp_on_open</tt> <var>sess</var> <var>fd</var></dt>
<dd><p>This is called after a new session has been defined with the session name
and file descriptor as arguments. If it returns a non-zero status,
opening the session is assumed to fail and the session is closed
again; however, <tt>tcp_open</tt> will continue to attempt to open any
remaining sessions given on the command line.
</p>
<a name="index-tcp_005fon_005frename"></a>
</dd>
<dt> <tt>tcp_on_rename</tt> <var>oldsess</var> <var>fd</var> <var>newsess</var></dt>
<dd><p>This is called after a session has been renamed with the three arguments
old session name, file descriptor, new session name.
</p>
<a name="index-tcp_005fon_005fspam"></a>
</dd>
<dt> <tt>tcp_on_spam</tt> <var>sess</var> <var>command</var> <tt>...</tt></dt>
<dd><p>This is called once for each session spammed, just <em>before</em> a command is
executed for a session by <tt>tcp_spam</tt>. The arguments are the session name
followed by the command list to be executed. If <tt>tcp_spam</tt> was called
with the option <tt>-t</tt>, the first command will be <tt>tcp_send</tt>.
</p>
<p>This function is called after <tt>$TCP_SESS</tt> 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 <tt>$TCP_SESS</tt> within this function. For example, the
session arguments to <tt>tcp_spam</tt> could include extra information to be
stripped off and processed in <tt>tcp_on_spam</tt>.
</p>
<p>If the function sets the parameter <tt>$REPLY</tt> to ‘<tt>done</tt>’, the command
line is not executed; in addition, no prompt is printed for the <tt>-v</tt>
option to <tt>tcp_spam</tt>.
</p>
<a name="index-tcp_005fon_005funalias"></a>
</dd>
<dt> <tt>tcp_on_unalias</tt> <var>alias</var> <var>fd</var></dt>
<dd><p>This is called with the name of an alias and the corresponding session’s
file descriptor after an alias has been deleted.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-Utility-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-User_002ddefined-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Parameters" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">24.4 TCP Utility Functions</h2>
<p>The following functions are used by the TCP function system but will rarely
if ever need to be called directly.
</p>
<dl compact="compact">
<dd><a name="index-tcp_005ffd_005fhandler"></a>
</dd>
<dt> <tt>tcp_fd_handler</tt></dt>
<dd><p>This is the function installed by <tt>tcp_open</tt> for handling input from
within the line editor, if that is required. It is in the format
documented for the builtin ‘<tt>zle -F</tt>’ in
<a href="Zsh-Line-Editor.html#Zle-Builtins">Zle Builtins</a>
.
</p>
<p>While active, the function sets the parameter <tt>TCP_HANDLER_ACTIVE</tt> to 1.
This allows shell code called internally (for example, by setting
<tt>tcp_on_read</tt>) to tell if is being called when the shell is otherwise
idle at the editor prompt.
</p>
<a name="index-tcp_005foutput"></a>
</dd>
<dt> <tt>tcp_output [ -q ] -P</tt> <var>prompt</var> <tt>-F</tt> <var>fd</var> <tt>-S</tt> <var>sess</var></dt>
<dd><p>This function is used for both logging and handling output to standard
output, from within <tt>tcp_read</tt> and (if <tt>$TCP_OUTPUT</tt> is set)
<tt>tcp_send</tt>.
</p>
<p>The <var>prompt</var> to use is specified by <tt>-P</tt>; the default is the empty
string. It can contain:
</p><dl compact="compact">
<dt> <tt>%c</tt></dt>
<dd><p>Expands to 1 if the session is the current session, otherwise 0. Used
with ternary expressions such as ‘<tt>%(c.-.+)</tt>’ to
output ‘<tt>+</tt>’ for the current session and ‘<tt>-</tt>’ otherwise.
</p>
</dd>
<dt> <tt>%f</tt></dt>
<dd><p>Replaced by the session’s file descriptor.
</p>
</dd>
<dt> <tt>%s</tt></dt>
<dd><p>Replaced by the session name.
</p>
</dd>
<dt> <tt>%%</tt></dt>
<dd><p>Replaced by a single ‘<tt>%</tt>’.
</p>
</dd>
</dl>
<p>The option <tt>-q</tt> suppresses output to standard output, but not to any log
files which are configured.
</p>
<p>The <tt>-S</tt> and <tt>-F</tt> options are used to pass in the session name and file
descriptor for possible replacement in the prompt.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-Parameters"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Utility-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-User_002ddefined-Parameters" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="TCP-User-Parameters"></a>
<h2 class="section">24.5 TCP User Parameters</h2>
<p>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.
</p>
<p>It is often also useful to make settable parameters local to a function.
For example, ‘<tt>local TCP_SILENT=1</tt>’ specifies that data read during the
function call will not be printed to standard output, regardless of the
setting outside the function. Likewise, ‘<tt>local TCP_SESS=</tt><var>sess</var>’
sets a session for the duration of a function, and ‘<tt>local
TCP_PROMPT=</tt>’ specifies that no prompt is used for input during the
function.
</p>
<dl compact="compact">
<dd><a name="index-tcp_005fexpect_005flines"></a>
</dd>
<dt> <tt>tcp_expect_lines</tt></dt>
<dd><p>Array. The set of lines read during the last call to <tt>tcp_expect</tt>,
including the last (<tt>$TCP_LINE</tt>).
</p>
<a name="index-tcp_005ffilter"></a>
</dd>
<dt> <tt>tcp_filter</tt></dt>
<dd><p>Array. May be set directly. A set of extended globbing patterns which,
if matched in <tt>tcp_output</tt>, will cause the line not to be printed to
standard output. The patterns should be defined as described for the
arguments to <tt>tcp_expect</tt>. Output of line to log files is not affected.
</p>
<a name="index-TCP_005fHANDLER_005fACTIVE"></a>
</dd>
<dt> <tt>TCP_HANDLER_ACTIVE</tt></dt>
<dd><p>Scalar. Set to 1 within <tt>tcp_fd_handler</tt> to indicate to functions
called recursively that they have been called during an editor session.
Otherwise unset.
</p>
<a name="index-TCP_005fLINE"></a>
</dd>
<dt> <tt>TCP_LINE</tt></dt>
<dd><p>The last line read by <tt>tcp_read</tt>, and hence also <tt>tcp_expect</tt>.
</p>
<a name="index-TCP_005fLINE_005fFD"></a>
</dd>
<dt> <tt>TCP_LINE_FD</tt></dt>
<dd><p>The file descriptor from which <tt>$TCP_LINE</tt> was read.
<tt>${tcp_by_fd[$TCP_LINE_FD]}</tt> will give the corresponding session name.
</p>
<a name="index-tcp_005flines"></a>
</dd>
<dt> <tt>tcp_lines</tt></dt>
<dd><p>Array. The set of lines read during the last call to <tt>tcp_read</tt>,
including the last (<tt>$TCP_LINE</tt>).
</p>
<a name="index-TCP_005fLOG"></a>
</dd>
<dt> <tt>TCP_LOG</tt></dt>
<dd><p>May be set directly, although it is also controlled by <tt>tcp_log</tt>.
The name of a file to which output from all sessions will be sent.
The output is proceeded by the usual <tt>$TCP_PROMPT</tt>. If it is not an
absolute path name, it will follow the user’s current directory.
</p>
<a name="index-TCP_005fLOG_005fSESS"></a>
</dd>
<dt> <tt>TCP_LOG_SESS</tt></dt>
<dd><p>May be set directly, although it is also controlled by <tt>tcp_log</tt>.
The prefix for a set of files to which output from each session separately
will be sent; the full filename is <tt>${TCP_LOG_SESS}.</tt><var>sess</var>.
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.
</p>
<a name="index-tcp_005fno_005fspam_005flist"></a>
</dd>
<dt> <tt>tcp_no_spam_list</tt></dt>
<dd><p>Array. May be set directly. See <tt>tcp_spam</tt> for how this is used.
</p>
<a name="index-TCP_005fOUTPUT"></a>
</dd>
<dt> <tt>TCP_OUTPUT</tt></dt>
<dd><p>May be set directly. If a non-empty string, any data sent to a session by
<tt>tcp_send</tt> will be logged. This parameter gives the prompt to be used
in a file specified by <tt>$TCP_LOG</tt> but not in a file generated from
<tt>$TCP_LOG_SESS</tt>. The prompt string has the same format as
<tt>TCP_PROMPT</tt> and the same rules for its use apply.
</p>
<a name="index-TCP_005fPROMPT"></a>
</dd>
<dt> <tt>TCP_PROMPT</tt></dt>
<dd><p>May be set directly. Used as the prefix for data read by <tt>tcp_read</tt>
which is printed to standard output or to the log file given by
<tt>$TCP_LOG</tt>, if any. Any ‘<tt>%s</tt>’, ‘<tt>%f</tt>’ or ‘<tt>%%</tt>’ occurring in the
string will be replaced by the name of the session, the session’s
underlying file descriptor, or a single ‘<tt>%</tt>’, respectively. The
expression ‘<tt>%c</tt>’ expands to 1 if the session being read is the current
session, else 0; this is most useful in ternary expressions such as
‘<tt>%(c.-.+)</tt>’ which outputs ‘<tt>+</tt>’ if the session is
the current one, else ‘<tt>-</tt>’.
</p>
<a name="index-TCP_005fREAD_005fDEBUG"></a>
</dd>
<dt> <tt>TCP_READ_DEBUG</tt></dt>
<dd><p>May be set directly. If this has non-zero length, <tt>tcp_read</tt> will give
some limited diagnostics about data being read.
</p>
<a name="index-TCP_005fSECONDS_005fSTART"></a>
</dd>
<dt> <tt>TCP_SECONDS_START</tt></dt>
<dd><p>This value is created and initialised to zero by tcp_open.
</p>
<p>The functions <tt>tcp_read</tt> and <tt>tcp_expect</tt> use the shell’s
<tt>SECONDS</tt> 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 <tt>SECONDS</tt> which is floating point and set the
parameter <tt>TCP_SECONDS_START</tt> to the previous value of <tt>$SECONDS</tt>.
If the parameter is already floating point, it is used without a local
copy being created and <tt>TCP_SECONDS_START</tt> is not set. As the global
value is zero, the shell elapsed time is guaranteed to be the sum of
<tt>$SECONDS</tt> and <tt>$TCP_SECONDS_START</tt>.
</p>
<p>This can be avoided by setting <tt>SECONDS</tt> globally to a floating point
value using ‘<tt>typeset -F SECONDS</tt>’; then the TCP functions will never
make a local copy and never set <tt>TCP_SECONDS_START</tt> to a non-zero value.
</p>
<a name="index-TCP_005fSESS"></a>
</dd>
<dt> <tt>TCP_SESS</tt></dt>
<dd><p>May be set directly. The current session; must refer to one of the
sessions established by <tt>tcp_open</tt>.
</p>
<a name="index-TCP_005fSILENT"></a>
</dd>
<dt> <tt>TCP_SILENT</tt></dt>
<dd><p>May be set directly, although it is also controlled by <tt>tcp_log</tt>.
If of non-zero length, data read by <tt>tcp_read</tt> will not be written to
standard output, though may still be written to a log file.
</p>
<a name="index-tcp_005fspam_005flist"></a>
</dd>
<dt> <tt>tcp_spam_list</tt></dt>
<dd><p>Array. May be set directly. See the description of the function
<tt>tcp_spam</tt> for how this is used.
</p>
<a name="index-TCP_005fTALK_005fESCAPE"></a>
</dd>
<dt> <tt>TCP_TALK_ESCAPE</tt></dt>
<dd><p>May be set directly. See the description of the function <tt>tcp_talk</tt> for
how this is used.
</p>
<a name="index-TCP_005fTIMEOUT"></a>
</dd>
<dt> <tt>TCP_TIMEOUT</tt></dt>
<dd><p>May be set directly. Currently this is only used by the function
<tt>tcp_command</tt>, see above.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-User_002ddefined-Parameters"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Parameters" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Utility-Parameters" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">24.6 TCP User-defined Parameters</h2>
<p>The following parameters are not set by the function system, but have
a special effect if set by the user.
</p>
<dl compact="compact">
<dd><a name="index-tcp_005fon_005fread"></a>
</dd>
<dt> <tt>tcp_on_read</tt></dt>
<dd><p>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 <tt>EXTENDED_GLOB</tt>).
Every line read from a TCP session directly or indirectly using
<tt>tcp_read</tt> (which includes lines read by <tt>tcp_expect</tt>) 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 itself.
</p>
<p>If any function called to handle a line returns a non-zero status, the
line is not output. Thus a <tt>tcp_on_read</tt> handler containing only
the instruction ‘<tt>return 1</tt>’ can be used to suppress output of
particular lines (see, however, <tt>tcp_filter</tt> above). However, the line
is still stored in <tt>TCP_LINE</tt> and <tt>tcp_lines</tt>; this occurs after all
<tt>tcp_on_read</tt> processing.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-Utility-Parameters"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-User_002ddefined-Parameters" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Examples" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">24.7 TCP Utility Parameters</h2>
<p>These parameters are controlled by the function system; they may be read
directly, but should not usually be set by user code.
</p>
<dl compact="compact">
<dd><a name="index-tcp_005faliases"></a>
</dd>
<dt> <tt>tcp_aliases</tt></dt>
<dd><p>Associative array. The keys are the names of sessions established with
<tt>tcp_open</tt>; each value is a space-separated list of aliases which refer
to that session.
</p>
<a name="index-tcp_005fby_005ffd"></a>
</dd>
<dt> <tt>tcp_by_fd</tt></dt>
<dd><p>Associative array. The keys are session file descriptors; each
value is the name of that session.
</p>
<a name="index-tcp_005fby_005fname"></a>
</dd>
<dt> <tt>tcp_by_name</tt></dt>
<dd><p>Associative array. The keys are the names of sessions; each value is the
file descriptor associated with that session.
</p>
</dd>
</dl>
<hr size="6">
<a name="TCP-Examples"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Utility-Parameters" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Bugs" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="TCP-Examples-1"></a>
<h2 class="section">24.8 TCP Examples</h2>
<p>Here is a trivial example using a remote calculator.
</p>
<p>TO create a calculator server on port 7337 (see the <tt>dc</tt> manual page for
quite how infuriating the underlying command is):
</p>
<table><tr><td> </td><td><pre class="example">tcp_proxy 7337 dc
</pre></td></tr></table>
<p>To connect to this from the same host with a session also named ‘<tt>dc</tt>’:
</p>
<table><tr><td> </td><td><pre class="example">tcp_open localhost 7337 dc
</pre></td></tr></table>
<p>To send a command to the remote session and wait a short while for output
(assuming <tt>dc</tt> is the current session):
</p>
<table><tr><td> </td><td><pre class="example">tcp_command 2 4 + p
</pre></td></tr></table>
<p>To close the session:
</p>
<table><tr><td> </td><td><pre class="example">tcp_close
</pre></td></tr></table>
<p>The <tt>tcp_proxy</tt> needs to be killed to be stopped. Note this will not
usually kill any connections which have already been accepted, and also
that the port is not immediately available for reuse.
</p>
<p>The following chunk of code puts a list of sessions into an xterm header,
with the current session followed by a star.
</p>
<table><tr><td> </td><td><pre class="example">print -n "\033]2;TCP:" ${(k)tcp_by_name:/$TCP_SESS/$TCP_SESS\*} "\a"
</pre></td></tr></table>
<hr size="6">
<a name="TCP-Bugs"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Examples" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#TCP-Function-System" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="TCP-Bugs-1"></a>
<h2 class="section">24.9 TCP Bugs</h2>
<p>The function <tt>tcp_read</tt> uses the shell’s normal <tt>read</tt> builtin. As
this reads a complete line at once, data arriving without a terminating
newline can cause the function to block indefinitely.
</p>
<p>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 extremely poor.
</p><hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#TCP-Function-System" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="Zftp-Function-System.html#Zftp-Function-System" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>Peter Stephenson</em> on <em>December 21, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|