| 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
| 3 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 5 | <head> |
| 6 | <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> |
| 7 | <meta name="generator" content="AsciiDoc 10.1.2" /> |
| 8 | <title>The GNU APL Communication Cookbook</title> |
| 9 | <style type="text/css"> |
| 10 | /* Shared CSS for AsciiDoc xhtml11 and html5 backends */ |
| 11 | |
| 12 | /* Default font. */ |
| 13 | body { |
| 14 | font-family: Georgia,serif; |
| 15 | } |
| 16 | |
| 17 | /* Title font. */ |
| 18 | h1, h2, h3, h4, h5, h6, |
| 19 | div.title, caption.title, |
| 20 | thead, p.table.header, |
| 21 | #toctitle, |
| 22 | #author, #revnumber, #revdate, #revremark, |
| 23 | #footer { |
| 24 | font-family: Arial,Helvetica,sans-serif; |
| 25 | } |
| 26 | |
| 27 | body { |
| 28 | margin: 1em 5% 1em 5%; |
| 29 | } |
| 30 | |
| 31 | a { |
| 32 | color: blue; |
| 33 | text-decoration: underline; |
| 34 | } |
| 35 | a:visited { |
| 36 | color: fuchsia; |
| 37 | } |
| 38 | |
| 39 | em { |
| 40 | font-style: italic; |
| 41 | color: navy; |
| 42 | } |
| 43 | |
| 44 | strong { |
| 45 | font-weight: bold; |
| 46 | color: #083194; |
| 47 | } |
| 48 | |
| 49 | h1, h2, h3, h4, h5, h6 { |
| 50 | color: #527bbd; |
| 51 | margin-top: 1.2em; |
| 52 | margin-bottom: 0.5em; |
| 53 | line-height: 1.3; |
| 54 | } |
| 55 | |
| 56 | h1, h2, h3 { |
| 57 | border-bottom: 2px solid silver; |
| 58 | } |
| 59 | h2 { |
| 60 | padding-top: 0.5em; |
| 61 | } |
| 62 | h3 { |
| 63 | float: left; |
| 64 | } |
| 65 | h3 + * { |
| 66 | clear: left; |
| 67 | } |
| 68 | h5 { |
| 69 | font-size: 1.0em; |
| 70 | } |
| 71 | |
| 72 | div.sectionbody { |
| 73 | margin-left: 0; |
| 74 | } |
| 75 | |
| 76 | hr { |
| 77 | border: 1px solid silver; |
| 78 | } |
| 79 | |
| 80 | p { |
| 81 | margin-top: 0.5em; |
| 82 | margin-bottom: 0.5em; |
| 83 | } |
| 84 | |
| 85 | ul, ol, li > p { |
| 86 | margin-top: 0; |
| 87 | } |
| 88 | ul > li { color: #aaa; } |
| 89 | ul > li > * { color: black; } |
| 90 | |
| 91 | .monospaced, code, pre { |
| 92 | font-family: "Courier New", Courier, monospace; |
| 93 | font-size: inherit; |
| 94 | color: navy; |
| 95 | padding: 0; |
| 96 | margin: 0; |
| 97 | } |
| 98 | pre { |
| 99 | white-space: pre-wrap; |
| 100 | } |
| 101 | |
| 102 | #author { |
| 103 | color: #527bbd; |
| 104 | font-weight: bold; |
| 105 | font-size: 1.1em; |
| 106 | } |
| 107 | #email { |
| 108 | } |
| 109 | #revnumber, #revdate, #revremark { |
| 110 | } |
| 111 | |
| 112 | #footer { |
| 113 | font-size: small; |
| 114 | border-top: 2px solid silver; |
| 115 | padding-top: 0.5em; |
| 116 | margin-top: 4.0em; |
| 117 | } |
| 118 | #footer-text { |
| 119 | float: left; |
| 120 | padding-bottom: 0.5em; |
| 121 | } |
| 122 | #footer-badges { |
| 123 | float: right; |
| 124 | padding-bottom: 0.5em; |
| 125 | } |
| 126 | |
| 127 | #preamble { |
| 128 | margin-top: 1.5em; |
| 129 | margin-bottom: 1.5em; |
| 130 | } |
| 131 | div.imageblock, div.exampleblock, div.verseblock, |
| 132 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, |
| 133 | div.admonitionblock { |
| 134 | margin-top: 1.0em; |
| 135 | margin-bottom: 1.5em; |
| 136 | } |
| 137 | div.admonitionblock { |
| 138 | margin-top: 2.0em; |
| 139 | margin-bottom: 2.0em; |
| 140 | margin-right: 10%; |
| 141 | color: #606060; |
| 142 | } |
| 143 | |
| 144 | div.content { /* Block element content. */ |
| 145 | padding: 0; |
| 146 | } |
| 147 | |
| 148 | /* Block element titles. */ |
| 149 | div.title, caption.title { |
| 150 | color: #527bbd; |
| 151 | font-weight: bold; |
| 152 | text-align: left; |
| 153 | margin-top: 1.0em; |
| 154 | margin-bottom: 0.5em; |
| 155 | } |
| 156 | div.title + * { |
| 157 | margin-top: 0; |
| 158 | } |
| 159 | |
| 160 | td div.title:first-child { |
| 161 | margin-top: 0.0em; |
| 162 | } |
| 163 | div.content div.title:first-child { |
| 164 | margin-top: 0.0em; |
| 165 | } |
| 166 | div.content + div.title { |
| 167 | margin-top: 0.0em; |
| 168 | } |
| 169 | |
| 170 | div.sidebarblock > div.content { |
| 171 | background: #ffffee; |
| 172 | border: 1px solid #dddddd; |
| 173 | border-left: 4px solid #f0f0f0; |
| 174 | padding: 0.5em; |
| 175 | } |
| 176 | |
| 177 | div.listingblock > div.content { |
| 178 | border: 1px solid #dddddd; |
| 179 | border-left: 5px solid #f0f0f0; |
| 180 | background: #f8f8f8; |
| 181 | padding: 0.5em; |
| 182 | } |
| 183 | |
| 184 | div.quoteblock, div.verseblock { |
| 185 | padding-left: 1.0em; |
| 186 | margin-left: 1.0em; |
| 187 | margin-right: 10%; |
| 188 | border-left: 5px solid #f0f0f0; |
| 189 | color: #888; |
| 190 | } |
| 191 | |
| 192 | div.quoteblock > div.attribution { |
| 193 | padding-top: 0.5em; |
| 194 | text-align: right; |
| 195 | } |
| 196 | |
| 197 | div.verseblock > pre.content { |
| 198 | font-family: inherit; |
| 199 | font-size: inherit; |
| 200 | } |
| 201 | div.verseblock > div.attribution { |
| 202 | padding-top: 0.75em; |
| 203 | text-align: left; |
| 204 | } |
| 205 | /* DEPRECATED: Pre version 8.2.7 verse style literal block. */ |
| 206 | div.verseblock + div.attribution { |
| 207 | text-align: left; |
| 208 | } |
| 209 | |
| 210 | div.admonitionblock .icon { |
| 211 | vertical-align: top; |
| 212 | font-size: 1.1em; |
| 213 | font-weight: bold; |
| 214 | text-decoration: underline; |
| 215 | color: #527bbd; |
| 216 | padding-right: 0.5em; |
| 217 | } |
| 218 | div.admonitionblock td.content { |
| 219 | padding-left: 0.5em; |
| 220 | border-left: 3px solid #dddddd; |
| 221 | } |
| 222 | |
| 223 | div.exampleblock > div.content { |
| 224 | border-left: 3px solid #dddddd; |
| 225 | padding-left: 0.5em; |
| 226 | } |
| 227 | |
| 228 | div.imageblock div.content { padding-left: 0; } |
| 229 | span.image img { border-style: none; vertical-align: text-bottom; } |
| 230 | a.image:visited { color: white; } |
| 231 | |
| 232 | dl { |
| 233 | margin-top: 0.8em; |
| 234 | margin-bottom: 0.8em; |
| 235 | } |
| 236 | dt { |
| 237 | margin-top: 0.5em; |
| 238 | margin-bottom: 0; |
| 239 | font-style: normal; |
| 240 | color: navy; |
| 241 | } |
| 242 | dd > *:first-child { |
| 243 | margin-top: 0.1em; |
| 244 | } |
| 245 | |
| 246 | ul, ol { |
| 247 | list-style-position: outside; |
| 248 | } |
| 249 | ol.arabic { |
| 250 | list-style-type: decimal; |
| 251 | } |
| 252 | ol.loweralpha { |
| 253 | list-style-type: lower-alpha; |
| 254 | } |
| 255 | ol.upperalpha { |
| 256 | list-style-type: upper-alpha; |
| 257 | } |
| 258 | ol.lowerroman { |
| 259 | list-style-type: lower-roman; |
| 260 | } |
| 261 | ol.upperroman { |
| 262 | list-style-type: upper-roman; |
| 263 | } |
| 264 | |
| 265 | div.compact ul, div.compact ol, |
| 266 | div.compact p, div.compact p, |
| 267 | div.compact div, div.compact div { |
| 268 | margin-top: 0.1em; |
| 269 | margin-bottom: 0.1em; |
| 270 | } |
| 271 | |
| 272 | tfoot { |
| 273 | font-weight: bold; |
| 274 | } |
| 275 | td > div.verse { |
| 276 | white-space: pre; |
| 277 | } |
| 278 | |
| 279 | div.hdlist { |
| 280 | margin-top: 0.8em; |
| 281 | margin-bottom: 0.8em; |
| 282 | } |
| 283 | div.hdlist tr { |
| 284 | padding-bottom: 15px; |
| 285 | } |
| 286 | dt.hdlist1.strong, td.hdlist1.strong { |
| 287 | font-weight: bold; |
| 288 | } |
| 289 | td.hdlist1 { |
| 290 | vertical-align: top; |
| 291 | font-style: normal; |
| 292 | padding-right: 0.8em; |
| 293 | color: navy; |
| 294 | } |
| 295 | td.hdlist2 { |
| 296 | vertical-align: top; |
| 297 | } |
| 298 | div.hdlist.compact tr { |
| 299 | margin: 0; |
| 300 | padding-bottom: 0; |
| 301 | } |
| 302 | |
| 303 | .comment { |
| 304 | background: yellow; |
| 305 | } |
| 306 | |
| 307 | .footnote, .footnoteref { |
| 308 | font-size: 0.8em; |
| 309 | } |
| 310 | |
| 311 | span.footnote, span.footnoteref { |
| 312 | vertical-align: super; |
| 313 | } |
| 314 | |
| 315 | #footnotes { |
| 316 | margin: 20px 0 20px 0; |
| 317 | padding: 7px 0 0 0; |
| 318 | } |
| 319 | |
| 320 | #footnotes div.footnote { |
| 321 | margin: 0 0 5px 0; |
| 322 | } |
| 323 | |
| 324 | #footnotes hr { |
| 325 | border: none; |
| 326 | border-top: 1px solid silver; |
| 327 | height: 1px; |
| 328 | text-align: left; |
| 329 | margin-left: 0; |
| 330 | width: 20%; |
| 331 | min-width: 100px; |
| 332 | } |
| 333 | |
| 334 | div.colist td { |
| 335 | padding-right: 0.5em; |
| 336 | padding-bottom: 0.3em; |
| 337 | vertical-align: top; |
| 338 | } |
| 339 | div.colist td img { |
| 340 | margin-top: 0.3em; |
| 341 | } |
| 342 | |
| 343 | @media print { |
| 344 | #footer-badges { display: none; } |
| 345 | } |
| 346 | |
| 347 | #toc { |
| 348 | margin-bottom: 2.5em; |
| 349 | } |
| 350 | |
| 351 | #toctitle { |
| 352 | color: #527bbd; |
| 353 | font-size: 1.1em; |
| 354 | font-weight: bold; |
| 355 | margin-top: 1.0em; |
| 356 | margin-bottom: 0.1em; |
| 357 | } |
| 358 | |
| 359 | div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { |
| 360 | margin-top: 0; |
| 361 | margin-bottom: 0; |
| 362 | } |
| 363 | div.toclevel2 { |
| 364 | margin-left: 2em; |
| 365 | font-size: 0.9em; |
| 366 | } |
| 367 | div.toclevel3 { |
| 368 | margin-left: 4em; |
| 369 | font-size: 0.9em; |
| 370 | } |
| 371 | div.toclevel4 { |
| 372 | margin-left: 6em; |
| 373 | font-size: 0.9em; |
| 374 | } |
| 375 | |
| 376 | span.aqua { color: aqua; } |
| 377 | span.black { color: black; } |
| 378 | span.blue { color: blue; } |
| 379 | span.fuchsia { color: fuchsia; } |
| 380 | span.gray { color: gray; } |
| 381 | span.green { color: green; } |
| 382 | span.lime { color: lime; } |
| 383 | span.maroon { color: maroon; } |
| 384 | span.navy { color: navy; } |
| 385 | span.olive { color: olive; } |
| 386 | span.purple { color: purple; } |
| 387 | span.red { color: red; } |
| 388 | span.silver { color: silver; } |
| 389 | span.teal { color: teal; } |
| 390 | span.white { color: white; } |
| 391 | span.yellow { color: yellow; } |
| 392 | |
| 393 | span.aqua-background { background: aqua; } |
| 394 | span.black-background { background: black; } |
| 395 | span.blue-background { background: blue; } |
| 396 | span.fuchsia-background { background: fuchsia; } |
| 397 | span.gray-background { background: gray; } |
| 398 | span.green-background { background: green; } |
| 399 | span.lime-background { background: lime; } |
| 400 | span.maroon-background { background: maroon; } |
| 401 | span.navy-background { background: navy; } |
| 402 | span.olive-background { background: olive; } |
| 403 | span.purple-background { background: purple; } |
| 404 | span.red-background { background: red; } |
| 405 | span.silver-background { background: silver; } |
| 406 | span.teal-background { background: teal; } |
| 407 | span.white-background { background: white; } |
| 408 | span.yellow-background { background: yellow; } |
| 409 | |
| 410 | span.big { font-size: 2em; } |
| 411 | span.small { font-size: 0.6em; } |
| 412 | |
| 413 | span.underline { text-decoration: underline; } |
| 414 | span.overline { text-decoration: overline; } |
| 415 | span.line-through { text-decoration: line-through; } |
| 416 | |
| 417 | div.unbreakable { page-break-inside: avoid; } |
| 418 | |
| 419 | |
| 420 | /* |
| 421 | * xhtml11 specific |
| 422 | * |
| 423 | * */ |
| 424 | |
| 425 | div.tableblock { |
| 426 | margin-top: 1.0em; |
| 427 | margin-bottom: 1.5em; |
| 428 | } |
| 429 | div.tableblock > table { |
| 430 | border: 3px solid #527bbd; |
| 431 | } |
| 432 | thead, p.table.header { |
| 433 | font-weight: bold; |
| 434 | color: #527bbd; |
| 435 | } |
| 436 | p.table { |
| 437 | margin-top: 0; |
| 438 | } |
| 439 | /* Because the table frame attribute is overridden by CSS in most browsers. */ |
| 440 | div.tableblock > table[frame="void"] { |
| 441 | border-style: none; |
| 442 | } |
| 443 | div.tableblock > table[frame="hsides"] { |
| 444 | border-left-style: none; |
| 445 | border-right-style: none; |
| 446 | } |
| 447 | div.tableblock > table[frame="vsides"] { |
| 448 | border-top-style: none; |
| 449 | border-bottom-style: none; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * html5 specific |
| 455 | * |
| 456 | * */ |
| 457 | |
| 458 | table.tableblock { |
| 459 | margin-top: 1.0em; |
| 460 | margin-bottom: 1.5em; |
| 461 | } |
| 462 | thead, p.tableblock.header { |
| 463 | font-weight: bold; |
| 464 | color: #527bbd; |
| 465 | } |
| 466 | p.tableblock { |
| 467 | margin-top: 0; |
| 468 | } |
| 469 | table.tableblock { |
| 470 | border-width: 3px; |
| 471 | border-spacing: 0px; |
| 472 | border-style: solid; |
| 473 | border-color: #527bbd; |
| 474 | border-collapse: collapse; |
| 475 | } |
| 476 | th.tableblock, td.tableblock { |
| 477 | border-width: 1px; |
| 478 | padding: 4px; |
| 479 | border-style: solid; |
| 480 | border-color: #527bbd; |
| 481 | } |
| 482 | |
| 483 | table.tableblock.frame-topbot { |
| 484 | border-left-style: hidden; |
| 485 | border-right-style: hidden; |
| 486 | } |
| 487 | table.tableblock.frame-sides { |
| 488 | border-top-style: hidden; |
| 489 | border-bottom-style: hidden; |
| 490 | } |
| 491 | table.tableblock.frame-none { |
| 492 | border-style: hidden; |
| 493 | } |
| 494 | |
| 495 | th.tableblock.halign-left, td.tableblock.halign-left { |
| 496 | text-align: left; |
| 497 | } |
| 498 | th.tableblock.halign-center, td.tableblock.halign-center { |
| 499 | text-align: center; |
| 500 | } |
| 501 | th.tableblock.halign-right, td.tableblock.halign-right { |
| 502 | text-align: right; |
| 503 | } |
| 504 | |
| 505 | th.tableblock.valign-top, td.tableblock.valign-top { |
| 506 | vertical-align: top; |
| 507 | } |
| 508 | th.tableblock.valign-middle, td.tableblock.valign-middle { |
| 509 | vertical-align: middle; |
| 510 | } |
| 511 | th.tableblock.valign-bottom, td.tableblock.valign-bottom { |
| 512 | vertical-align: bottom; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* |
| 517 | * manpage specific |
| 518 | * |
| 519 | * */ |
| 520 | |
| 521 | body.manpage h1 { |
| 522 | padding-top: 0.5em; |
| 523 | padding-bottom: 0.5em; |
| 524 | border-top: 2px solid silver; |
| 525 | border-bottom: 2px solid silver; |
| 526 | } |
| 527 | body.manpage h2 { |
| 528 | border-style: none; |
| 529 | } |
| 530 | body.manpage div.sectionbody { |
| 531 | margin-left: 3em; |
| 532 | } |
| 533 | |
| 534 | @media print { |
| 535 | body.manpage div#toc { display: none; } |
| 536 | } |
| 537 | |
| 538 | |
| 539 | </style> |
| 540 | <script type="text/javascript"> |
| 541 | /*<+'])'); |
| 584 | // Function that scans the DOM tree for header elements (the DOM2 |
| 585 | // nodeIterator API would be a better technique but not supported by all |
| 586 | // browsers). |
| 587 | var iterate = function (el) { |
| 588 | for (var i = el.firstChild; i != null; i = i.nextSibling) { |
| 589 | if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { |
| 590 | var mo = re.exec(i.tagName); |
| 591 | if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") { |
| 592 | result[result.length] = new TocEntry(i, getText(i), mo[1]-1); |
| 593 | } |
| 594 | iterate(i); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | iterate(el); |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | var toc = document.getElementById("toc"); |
| 603 | if (!toc) { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Delete existing TOC entries in case we're reloading the TOC. |
| 608 | var tocEntriesToRemove = []; |
| 609 | var i; |
| 610 | for (i = 0; i < toc.childNodes.length; i++) { |
| 611 | var entry = toc.childNodes[i]; |
| 612 | if (entry.nodeName.toLowerCase() == 'div' |
| 613 | && entry.getAttribute("class") |
| 614 | && entry.getAttribute("class").match(/^toclevel/)) |
| 615 | tocEntriesToRemove.push(entry); |
| 616 | } |
| 617 | for (i = 0; i < tocEntriesToRemove.length; i++) { |
| 618 | toc.removeChild(tocEntriesToRemove[i]); |
| 619 | } |
| 620 | |
| 621 | // Rebuild TOC entries. |
| 622 | var entries = tocEntries(document.getElementById("content"), toclevels); |
| 623 | for (var i = 0; i < entries.length; ++i) { |
| 624 | var entry = entries[i]; |
| 625 | if (entry.element.id == "") |
| 626 | entry.element.id = "_toc_" + i; |
| 627 | var a = document.createElement("a"); |
| 628 | a.href = "#" + entry.element.id; |
| 629 | a.appendChild(document.createTextNode(entry.text)); |
| 630 | var div = document.createElement("div"); |
| 631 | div.appendChild(a); |
| 632 | div.className = "toclevel" + entry.toclevel; |
| 633 | toc.appendChild(div); |
| 634 | } |
| 635 | if (entries.length == 0) |
| 636 | toc.parentNode.removeChild(toc); |
| 637 | }, |
| 638 | |
| 639 | |
| 640 | ///////////////////////////////////////////////////////////////////// |
| 641 | // Footnotes generator |
| 642 | ///////////////////////////////////////////////////////////////////// |
| 643 | |
| 644 | /* Based on footnote generation code from: |
| 645 | * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html |
| 646 | */ |
| 647 | |
| 648 | footnotes: function () { |
| 649 | // Delete existing footnote entries in case we're reloading the footnodes. |
| 650 | var i; |
| 651 | var noteholder = document.getElementById("footnotes"); |
| 652 | if (!noteholder) { |
| 653 | return; |
| 654 | } |
| 655 | var entriesToRemove = []; |
| 656 | for (i = 0; i < noteholder.childNodes.length; i++) { |
| 657 | var entry = noteholder.childNodes[i]; |
| 658 | if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote") |
| 659 | entriesToRemove.push(entry); |
| 660 | } |
| 661 | for (i = 0; i < entriesToRemove.length; i++) { |
| 662 | noteholder.removeChild(entriesToRemove[i]); |
| 663 | } |
| 664 | |
| 665 | // Rebuild footnote entries. |
| 666 | var cont = document.getElementById("content"); |
| 667 | var spans = cont.getElementsByTagName("span"); |
| 668 | var refs = {}; |
| 669 | var n = 0; |
| 670 | for (i=0; i<spans.length; i++) { |
| 671 | if (spans[i].className == "footnote") { |
| 672 | n++; |
| 673 | var note = spans[i].getAttribute("data-note"); |
| 674 | if (!note) { |
| 675 | // Use [\s\S] in place of . so multi-line matches work. |
| 676 | // Because JavaScript has no s (dotall) regex flag. |
| 677 | note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; |
| 678 | spans[i].innerHTML = |
| 679 | "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + |
| 680 | "' title='View footnote' class='footnote'>" + n + "</a>]"; |
| 681 | spans[i].setAttribute("data-note", note); |
| 682 | } |
| 683 | noteholder.innerHTML += |
| 684 | "<div class='footnote' id='_footnote_" + n + "'>" + |
| 685 | "<a href='#_footnoteref_" + n + "' title='Return to text'>" + |
| 686 | n + "</a>. " + note + "</div>"; |
| 687 | var id =spans[i].getAttribute("id"); |
| 688 | if (id != null) refs["#"+id] = n; |
| 689 | } |
| 690 | } |
| 691 | if (n == 0) |
| 692 | noteholder.parentNode.removeChild(noteholder); |
| 693 | else { |
| 694 | // Process footnoterefs. |
| 695 | for (i=0; i<spans.length; i++) { |
| 696 | if (spans[i].className == "footnoteref") { |
| 697 | var href = spans[i].getElementsByTagName("a")[0].getAttribute("href"); |
| 698 | href = href.match(/#.*/)[0]; // Because IE return full URL. |
| 699 | n = refs[href]; |
| 700 | spans[i].innerHTML = |
| 701 | "[<a href='#_footnote_" + n + |
| 702 | "' title='View footnote' class='footnote'>" + n + "</a>]"; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | }, |
| 707 | |
| 708 | install: function(toclevels) { |
| 709 | var timerId; |
| 710 | |
| 711 | function reinstall() { |
| 712 | asciidoc.footnotes(); |
| 713 | if (toclevels) { |
| 714 | asciidoc.toc(toclevels); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | function reinstallAndRemoveTimer() { |
| 719 | clearInterval(timerId); |
| 720 | reinstall(); |
| 721 | } |
| 722 | |
| 723 | timerId = setInterval(reinstall, 500); |
| 724 | if (document.addEventListener) |
| 725 | document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false); |
| 726 | else |
| 727 | window.onload = reinstallAndRemoveTimer; |
| 728 | } |
| 729 | |
| 730 | } |
| 731 | asciidoc.install(); |
| 732 | /*]]>*/ |
| 733 | </script> |
| 734 | </head> |
| 735 | <body class="article"> |
| 736 | <div id="header"> |
| 737 | <h1>The GNU APL Communication Cookbook</h1> |
| 738 | <span id="author">Dr. Jürgen Sauermann, GNU APL</span><br /> |
| 739 | </div> |
| 740 | <div id="content"> |
| 741 | <div class="sect1"> |
| 742 | <h2 id="_abstract">Abstract</h2> |
| 743 | <div class="sectionbody"> |
| 744 | <div class="paragraph"><p>GNU APL provides a number of communication mechanisms that allow it to |
| 745 | control other programs or to be controlled by other programs. With these |
| 746 | communication mechanisms one can construct larger systems in which GNU APL |
| 747 | is only one component. This document gives an overview of the communication |
| 748 | mechanisms, so that the reader can choose the most appropriate mechanisms |
| 749 | for a given application.</p></div> |
| 750 | </div> |
| 751 | </div> |
| 752 | <div class="sect1"> |
| 753 | <h2 id="_terminology">Terminology</h2> |
| 754 | <div class="sectionbody"> |
| 755 | <div class="paragraph"><p>A <strong>server</strong> is a program or process that is controlled by one or more other |
| 756 | programs or processes. A <strong>client</strong> is a program or process that controls |
| 757 | another program or process. A <strong>peer</strong> is a program or process that communicates |
| 758 | with GNU APL, but is not a client or a server with respect to that |
| 759 | communication. In client/server communication, it is the client that |
| 760 | establishes the communication link between the two partners.</p></div> |
| 761 | <div class="paragraph"><p>In general the communication between a client and a server is somewhat simpler |
| 762 | than the communication between two peers. The role of one communication |
| 763 | partner dictates the role of the other:</p></div> |
| 764 | <div class="tableblock"> |
| 765 | <table rules="all" |
| 766 | width="60%" |
| 767 | frame="border" |
| 768 | cellspacing="0" cellpadding="4"> |
| 769 | <caption class="title">Table 1. Communication Roles</caption> |
| 770 | <col width="50%" /> |
| 771 | <col width="50%" /> |
| 772 | <thead> |
| 773 | <tr> |
| 774 | <th align="left" valign="top"> GNU APL </th> |
| 775 | <th align="left" valign="top"> Partner</th> |
| 776 | </tr> |
| 777 | </thead> |
| 778 | <tbody> |
| 779 | <tr> |
| 780 | <td align="left" valign="top"><p class="table">Client</p></td> |
| 781 | <td align="left" valign="top"><p class="table">Server</p></td> |
| 782 | </tr> |
| 783 | <tr> |
| 784 | <td align="left" valign="top"><p class="table">Server</p></td> |
| 785 | <td align="left" valign="top"><p class="table">Client</p></td> |
| 786 | </tr> |
| 787 | <tr> |
| 788 | <td align="left" valign="top"><p class="table">Peer</p></td> |
| 789 | <td align="left" valign="top"><p class="table">Peer</p></td> |
| 790 | </tr> |
| 791 | </tbody> |
| 792 | </table> |
| 793 | </div> |
| 794 | <div class="paragraph"><p>If GNU APL is the client or a peer in a communication then the communication |
| 795 | partner usually needs to exist before GNU APL can connect to the partner. |
| 796 | Some partners are started beforehand by the operating system (Web servers, |
| 797 | SQL servers), some are started automatically by GNU APL (<strong>APserver()</strong> for |
| 798 | shared variables) and some may need to be started by other means before |
| 799 | a GNU APL program can use them.</p></div> |
| 800 | </div> |
| 801 | </div> |
| 802 | <div class="sect1"> |
| 803 | <h2 id="_gnu_apl_communication_mechanisms_overview">GNU APL Communication Mechanisms Overview</h2> |
| 804 | <div class="sectionbody"> |
| 805 | <div class="paragraph"><p>The following table gives an overview of the communication mechanisms that are |
| 806 | implemented in GNU APL. Most communication mechanisms are accessed from an APL |
| 807 | program via system variables. Some system variables need certain libraries and |
| 808 | if these libraries are not present at the time when GNU APL was |
| 809 | <strong>./configure’d</strong>, then these system variables will not exist |
| 810 | (causing a SYNTAX ERROR) or raise a DOMAIN ERROR. The fact that GNU APL is |
| 811 | in one role, for example as server, does not preclude it from being a client |
| 812 | in a different communication context.</p></div> |
| 813 | <div class="tableblock"> |
| 814 | <table rules="all" |
| 815 | width="100%" |
| 816 | frame="border" |
| 817 | cellspacing="0" cellpadding="4"> |
| 818 | <caption class="title">Table 2. GNU APL Communication Mechanisms</caption> |
| 819 | <col width="31%" /> |
| 820 | <col width="9%" /> |
| 821 | <col width="18%" /> |
| 822 | <col width="9%" /> |
| 823 | <col width="31%" /> |
| 824 | <thead> |
| 825 | <tr> |
| 826 | <th align="center" valign="top"> Mechanism</th> |
| 827 | <th align="center" valign="top"> GNU APL<br /> |
| 828 | Role</th> |
| 829 | <th align="center" valign="top"> System Variable(s)</th> |
| 830 | <th align="center" valign="top"> TTFB</th> |
| 831 | <th align="center" valign="top"> Notes</th> |
| 832 | </tr> |
| 833 | </thead> |
| 834 | <tbody> |
| 835 | <tr> |
| 836 | <td align="center" valign="top"><p class="table">libapl</p></td> |
| 837 | <td align="center" valign="top"><p class="table">Server</p></td> |
| 838 | <td align="center" valign="top"><p class="table">-</p></td> |
| 839 | <td align="center" valign="top"><p class="table">low</p></td> |
| 840 | <td align="center" valign="top"><p class="table">-</p></td> |
| 841 | </tr> |
| 842 | <tr> |
| 843 | <td align="center" valign="top"><p class="table">Shared Variables<br /> |
| 844 | (Workspace-to-Workspace)</p></td> |
| 845 | <td align="center" valign="top"><p class="table">Peer</p></td> |
| 846 | <td align="center" valign="top"><p class="table">⎕SVC ⎕SVO ⎕SVQ<br /> |
| 847 | ⎕SVR ⎕SVS</p></td> |
| 848 | <td align="center" valign="top"><p class="table">medium</p></td> |
| 849 | <td align="center" valign="top"><p class="table">for IBM APL2 compatibility only,<br /> |
| 850 | not to be used in new programs</p></td> |
| 851 | </tr> |
| 852 | <tr> |
| 853 | <td align="center" valign="top"><p class="table">Shared Variables<br /> |
| 854 | (Auxiliary Processors)</p></td> |
| 855 | <td align="center" valign="top"><p class="table">Client</p></td> |
| 856 | <td align="center" valign="top"><p class="table">⎕SVC ⎕SVO ⎕SVQ<br /> |
| 857 | ⎕SVR ⎕SVS</p></td> |
| 858 | <td align="center" valign="top"><p class="table">medium</p></td> |
| 859 | <td align="center" valign="top"><p class="table">for IBM APL2 compatibility only,<br /> |
| 860 | not to be used in new programs</p></td> |
| 861 | </tr> |
| 862 | <tr> |
| 863 | <td align="center" valign="top"><p class="table">SQL Interface</p></td> |
| 864 | <td align="center" valign="top"><p class="table">Client</p></td> |
| 865 | <td align="center" valign="top"><p class="table">⎕SQL</p></td> |
| 866 | <td align="center" valign="top"><p class="table">low</p></td> |
| 867 | <td align="center" valign="top"><p class="table">Server is PostgreSQL or SQLite</p></td> |
| 868 | </tr> |
| 869 | <tr> |
| 870 | <td align="center" valign="top"><p class="table">Server forked by GNU APL</p></td> |
| 871 | <td align="center" valign="top"><p class="table">Client</p></td> |
| 872 | <td align="center" valign="top"><p class="table">⎕FIO[57]</p></td> |
| 873 | <td align="center" valign="top"><p class="table">medium</p></td> |
| 874 | <td align="center" valign="top"><p class="table">bidirectional pipe</p></td> |
| 875 | </tr> |
| 876 | <tr> |
| 877 | <td align="center" valign="top"><p class="table">popen()</p></td> |
| 878 | <td align="center" valign="top"><p class="table">Client</p></td> |
| 879 | <td align="center" valign="top"><p class="table">⎕FIO[24]</p></td> |
| 880 | <td align="center" valign="top"><p class="table">low</p></td> |
| 881 | <td align="center" valign="top"><p class="table">unidirectional pipe</p></td> |
| 882 | </tr> |
| 883 | <tr> |
| 884 | <td align="center" valign="top"><p class="table">Sockets</p></td> |
| 885 | <td align="center" valign="top"><p class="table">Any</p></td> |
| 886 | <td align="center" valign="top"><p class="table">⎕FIO[32 … 47]</p></td> |
| 887 | <td align="center" valign="top"><p class="table">medium</p></td> |
| 888 | <td align="center" valign="top"><p class="table">TCP, UDP, or U*ix Domain Sockets</p></td> |
| 889 | </tr> |
| 890 | <tr> |
| 891 | <td align="center" valign="top"><p class="table">Native functions</p></td> |
| 892 | <td align="center" valign="top"><p class="table">Any</p></td> |
| 893 | <td align="center" valign="top"><p class="table">dyadic ⎕FX</p></td> |
| 894 | <td align="center" valign="top"><p class="table">high</p></td> |
| 895 | <td align="center" valign="top"><p class="table">-</p></td> |
| 896 | </tr> |
| 897 | </tbody> |
| 898 | </table> |
| 899 | </div> |
| 900 | <div class="paragraph"><p><strong>Note:</strong> TTFB is the "Time To First Byte" of a communication and shall indicate |
| 901 | how long it takes to get a communication mechanism up and running (APL code |
| 902 | plus possibly code needed for the communication partner).</p></div> |
| 903 | </div> |
| 904 | </div> |
| 905 | <div class="sect1"> |
| 906 | <h2 id="_gnu_apl_communication_mechanism_details">GNU APL Communication Mechanism Details</h2> |
| 907 | <div class="sectionbody"> |
| 908 | <div class="paragraph"><p>In the following, the different communication mechanisms are described in |
| 909 | some more detail.</p></div> |
| 910 | <div class="sect2"> |
| 911 | <h3 id="_libapl">libapl</h3> |
| 912 | <div class="paragraph"><p>Normally GNU APL initializes itself and then enters a <strong>REPL</strong> loop until |
| 913 | the )OFF command is given (interactive mode) or the input stream ends (script |
| 914 | mode). <strong>REPL</strong> stands for <strong>R</strong>ead-<strong>E</strong>valuate-<strong>P</strong>rint-<strong>L</strong>oop and that |
| 915 | describes the top-level operation of an APL interpreter in interactive mode.</p></div> |
| 916 | <div class="paragraph"><p>If GNU APL is <strong>./configure’d</strong> with option <strong>--with-libapl</strong>, then GNU APL does |
| 917 | not build a normal executable that can be started from a script or from a |
| 918 | shell, but instead it builds a library <strong>libapl</strong>. That library can then be |
| 919 | linked with another program that either implements its own <strong>REPL</strong> loop |
| 920 | (thus providing some interactive mode, but with a different user interface), |
| 921 | or only calls GNU APL facilities from time to time.</p></div> |
| 922 | <div class="paragraph"><p>The following is an example C program that demonstrates the use of libapl:</p></div> |
| 923 | <div class="listingblock"> |
| 924 | <div class="content"> |
| 925 | <pre> |
| 926 | |
| 927 | <code>#include "apl/libapl.h" |
| 928 | |
| 929 | int |
| 930 | main(int argc, char * argv[]) |
| 931 | { |
| 932 | init_libapl(argv[0], 0); |
| 933 | return apl_exec("⎕←2 3⍴⍳6"); |
| 934 | }</code> |
| 935 | |
| 936 | </pre> |
| 937 | </div></div> |
| 938 | <div class="paragraph"><p>If the code above is stored in, say, file <strong>test_libapl.c</strong>, and if libapl was |
| 939 | properly installed (and assuming the default installation prefix /usr/local of |
| 940 | GNU APL), the following command should create an executable file test_libapl:</p></div> |
| 941 | <div class="listingblock"> |
| 942 | <div class="content"> |
| 943 | <pre> |
| 944 | |
| 945 | <code>gcc test_libapl.c -L /usr/local/lib/apl -lapl -o test_libapl</code> |
| 946 | |
| 947 | </pre> |
| 948 | </div></div> |
| 949 | <div class="paragraph"><p>And running the newly created <strong>test_libapl</strong> produces what would be expected:</p></div> |
| 950 | <div class="listingblock"> |
| 951 | <div class="content"> |
| 952 | <pre> |
| 953 | |
| 954 | <code>$ ./test_libapl |
| 955 | 1 2 3 |
| 956 | 4 5 6</code> |
| 957 | |
| 958 | </pre> |
| 959 | </div></div> |
| 960 | </div> |
| 961 | <div class="sect2"> |
| 962 | <h3 id="_shared_variables">Shared Variables</h3> |
| 963 | <div class="paragraph"><p>Shared variables is a communication mechanism that reaches back to the early |
| 964 | days of APL. In essence, a shared variable is an APL variable that represents |
| 965 | a communication channel to a peer. If one side assigns a value to a shared |
| 966 | variable, then the other side gets that value when referencing the variable |
| 967 | (and vice versa). The communication channel is established, maintained, |
| 968 | and closed by a set of system variables named <strong>⎕SVC</strong> (shared variable |
| 969 | control), <strong>⎕SVO</strong> (shared variable offer), <strong>⎕SVQ</strong> (shared variable query), <strong>⎕SVR</strong> |
| 970 | (shared variable retract), and <strong>⎕SVS</strong> (shared variable status). The details |
| 971 | of how these system variables are used will not be explained here, because |
| 972 | shared variables exist in GNU APL only for compatibility with IBM APL2. For |
| 973 | new APL programs, the socket functions in <strong>⎕FIO</strong> are much easier to use than |
| 974 | shared variables and have fewer restrictions.</p></div> |
| 975 | <div class="paragraph"><p>Shared variables come in 2 varieties:</p></div> |
| 976 | <div class="ulist"><ul> |
| 977 | <li> |
| 978 | <p> |
| 979 | workspace-to-workspace variables which create a communication link between |
| 980 | two running APL workspaces (in different processes on the same machine. This |
| 981 | is a peer-to-peer communication. |
| 982 | </p> |
| 983 | </li> |
| 984 | <li> |
| 985 | <p> |
| 986 | Auxiliary Processors (APs), which are processes started locally by the APL |
| 987 | interpreter. In this case the APs are servers and APL is the client. GNU |
| 988 | APL only implements 2 APs (AP100 and AP210) and only as examples for how |
| 989 | to write other ones. In the past, APs were used to make specific operating |
| 990 | services available to APL, but in GNU APL the same can be achieved with |
| 991 | <strong>⎕FIO[57]</strong> for services defined by the user and/or <strong>⎕FIO[24]</strong> for services |
| 992 | already available on the operating system. |
| 993 | </p> |
| 994 | </li> |
| 995 | </ul></div> |
| 996 | <div class="paragraph"><p>Those interested in the style of shared variables programming may have a |
| 997 | look at test cases <strong>AP100.tc</strong> for AP100, <strong>AP210.tc</strong> for AP210, and <strong>APnnn.tc</strong> |
| 998 | for workspace-to-workspace shared variables in the directory |
| 999 | <strong>src/testcases/</strong> of GNU APL.</p></div> |
| 1000 | <div class="paragraph"><p>The GNU APL implementation of shared variables is a separate process running |
| 1001 | the program <strong>APserver</strong> (which comes with GNU APL). APserver currently |
| 1002 | implements only AP100 and AP210, but the source code of APserver is open, so |
| 1003 | that a user can add more APs. Is seems, however, that most of the APs that |
| 1004 | were not implemented have been superseded by more modern interfaces (most |
| 1005 | of which are sub-functions of ⎕FIO) that are easier to use than shared |
| 1006 | variables.</p></div> |
| 1007 | <div class="paragraph"><p>For workspace-to-workspace shared variables, the APserver stores the shared |
| 1008 | variables themselves, so that no data is lost if one of the connected |
| 1009 | workspaces exits or *⎕SVR*s a shared variable. APserver also maps the |
| 1010 | peer-to-peer connection between two workspaces onto two client/server |
| 1011 | connections from each workspace (as clients) to APserver (as server). APserver |
| 1012 | is started automatically when an APL workspace uses shared variables for the |
| 1013 | first time.</p></div> |
| 1014 | <div class="paragraph"><p>Since shared variables are considered obsolete, the effort put into their |
| 1015 | implementation was kept at a minimum. As a consequence, the implementation |
| 1016 | is rather crude and not optimized for performance.</p></div> |
| 1017 | </div> |
| 1018 | <div class="sect2"> |
| 1019 | <h3 id="_sql_interface">SQL Interface</h3> |
| 1020 | <div class="paragraph"><p>The system function ⎕SQL provides a direct interface to an SQL database. |
| 1021 | Currently PostgreSQL and SQLite databases are supported. ⎕SQL is a |
| 1022 | container for a number of sub-functions that can be displayed with:</p></div> |
| 1023 | <div class="listingblock"> |
| 1024 | <div class="content"> |
| 1025 | <pre> |
| 1026 | |
| 1027 | <code> ⎕SQL"" |
| 1028 | Available function numbers: |
| 1029 | type ⎕SQL[1] file - open a database file, return reference ID for it |
| 1030 | ⎕SQL[2] ref - close database |
| 1031 | query ⎕SQL[3,db] params - send SQL query |
| 1032 | query ⎕SQL[4,db] params - send SQL update |
| 1033 | ⎕SQL[5] ref - begin a transaction |
| 1034 | ⎕SQL[6] ref - commit current transaction |
| 1035 | ⎕SQL[7] ref - rollback current transaction |
| 1036 | ⎕SQL[8] ref - list tables |
| 1037 | ref ⎕SQL[9] table - list columns for table</code> |
| 1038 | |
| 1039 | </pre> |
| 1040 | </div></div> |
| 1041 | <div class="paragraph"><p>More details on <strong>⎕SQL</strong> can be found in <strong>info apl</strong> (around Section 2.36).</p></div> |
| 1042 | </div> |
| 1043 | <div class="sect2"> |
| 1044 | <h3 id="_server_forked_by_gnu_apl">Server forked by GNU APL</h3> |
| 1045 | <div class="paragraph"><p><strong>Handle←⎕FIO[57] Bs</strong> spawns a process running the program <strong>Bs</strong>. <strong>Bs</strong> is the |
| 1046 | <strong>absolute (!)</strong> path of an executable program or script, which is started |
| 1047 | and connected to GNU APL over a bidirectional pipe.</p></div> |
| 1048 | <div class="paragraph"><p>The return value <strong>Handle</strong> of <strong>⎕FIO[57] Bs</strong> is a file handle that can be used |
| 1049 | on the APL side of the communication to send data to or receive data from the |
| 1050 | spawned program using, for example, <strong>⎕FIO[7] Handle</strong> (aka <strong>fwrite</strong>) or |
| 1051 | <strong>⎕FIO[6] Handle</strong> (aka. <strong>fread</strong>) respectively. The handle is normally closed |
| 1052 | with <strong>⎕FIO[4] Handle</strong> (aka. <strong>fclose</strong>).</p></div> |
| 1053 | <div class="paragraph"><p>At the spawned program’s end of the communication, the communication with |
| 1054 | GNU APL uses file descriptor 3 which is normally only used to <strong>fdopen()</strong> a |
| 1055 | FILE *. An <strong>fclose</strong> from the APL side causes an EOF condition (<strong>fread()</strong> |
| 1056 | returns 0) in the spawned program. Under normal circumstances, the |
| 1057 | communication should be closed from the client (= APL) side.</p></div> |
| 1058 | <div class="paragraph"><p>The following is a simple example for a server written in "C", and GNU APL code |
| 1059 | that forks the server and then sends a TLV (Tag-Length-Value) buffer to the |
| 1060 | server. The server sends the TLV buffers back, but with the tag negated and the |
| 1061 | value bytes mirrored.</p></div> |
| 1062 | <div class="paragraph"><p>Client side (APL):</p></div> |
| 1063 | <div class="listingblock"> |
| 1064 | <div class="content"> |
| 1065 | <pre> |
| 1066 | |
| 1067 | <code> Path ← '/usr/lib/apl/TLV_server' ⍝ or wherever TLV_server was installed |
| 1068 | Handle ← ⎕FIO[57] Path ⍝ start & connect TLV_server |
| 1069 | |
| 1070 | ⍝ typically in a loop,,, |
| 1071 | TLV ← 33 ⎕CR 42,'Forty-Two' ⍝ encode a TLV buffer |
| 1072 | ⊣ TLV ⎕FIO[43] Handle ⍝ send TLV buffer to TLV_server |
| 1073 | TL ← 8 ⎕FIO[6] Handle ⍝ read tag/length from TLV_server |
| 1074 | Value ← (256⊥4↓TL) ⎕FIO[6] Handle ⍝ read value from TLV_server |
| 1075 | 34 ⎕CR TL,Value ⍝ display response tag and value |
| 1076 | |
| 1077 | ⍝ cleanup (one time) |
| 1078 | ⊣ ⎕FIO[4] Handle ⍝ close connection (stops TLV_server)</code> |
| 1079 | |
| 1080 | </pre> |
| 1081 | </div></div> |
| 1082 | <div class="paragraph"><p>Server side (C, this example can also be found in file tools/TLV_server.c):</p></div> |
| 1083 | <div class="listingblock"> |
| 1084 | <div class="content"> |
| 1085 | <pre> |
| 1086 | |
| 1087 | <code>#include <errno.h> |
| 1088 | #include <stdint.h> |
| 1089 | #include <stdio.h> |
| 1090 | #include <string.h> |
| 1091 | #include <unistd.h> |
| 1092 | #include <sys/ioctl.h> |
| 1093 | |
| 1094 | int |
| 1095 | main(int argc, char * argv[]) |
| 1096 | { |
| 1097 | enum { TLV_socket = 3 }; |
| 1098 | char TLV[1000]; // the entire TLV buffer |
| 1099 | char * V = TLV + 8; // the value part of the TLV buffer |
| 1100 | ssize_t rx_len, tx_len; |
| 1101 | int32_t TLV_tag, TLV_len, j; |
| 1102 | |
| 1103 | FILE * f = fdopen(TLV_socket, "r"); |
| 1104 | if (f == 0) |
| 1105 | { |
| 1106 | fprintf(stderr, |
| 1107 | "fdopen() failed: %s\n" |
| 1108 | "That typically happens if this program is started directly,\n" |
| 1109 | "more precisely: without opening file descriptor 3 first. The anticipated\n" |
| 1110 | "usage is to open this program from GNU APL using ⎕FIO[57] and then to send\n" |
| 1111 | "TLV buffers encoded with 33 ⎕CR sent to this program with ⎕FIO[43]\n" |
| 1112 | , strerror(errno)); |
| 1113 | return 1; |
| 1114 | } |
| 1115 | |
| 1116 | for (;;) |
| 1117 | { |
| 1118 | // read the fixed size TL |
| 1119 | // |
| 1120 | rx_len = fread(TLV, 1, 8, f); |
| 1121 | if (rx_len != 8) |
| 1122 | { |
| 1123 | fprintf(stderr, "TLV socket closed (1): %s\n", |
| 1124 | strerror(errno)); |
| 1125 | fclose(f); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | TLV_tag = TLV[0] << 24 | TLV[1] << 16 | TLV[2] << 8 | TLV[3]; |
| 1129 | TLV_len = TLV[4] << 24 | TLV[5] << 16 | TLV[6] << 8 | TLV[7]; |
| 1130 | |
| 1131 | // read the variable-sized V |
| 1132 | // |
| 1133 | if (TLV_len) |
| 1134 | { |
| 1135 | rx_len = fread(V, 1, TLV_len, f); |
| 1136 | if (rx_len != TLV_len) |
| 1137 | { |
| 1138 | fprintf(stderr, "TLV socket closed (2): %s\n", |
| 1139 | strerror(errno)); |
| 1140 | fclose(f); |
| 1141 | return 0; |
| 1142 | } |
| 1143 | V[TLV_len] = 0; |
| 1144 | } |
| 1145 | |
| 1146 | // negate the tag |
| 1147 | // |
| 1148 | TLV_tag = -TLV_tag; |
| 1149 | TLV[0] = TLV_tag >> 24; |
| 1150 | TLV[1] = TLV_tag >> 16; |
| 1151 | TLV[2] = TLV_tag >> 8; |
| 1152 | TLV[3] = TLV_tag; |
| 1153 | |
| 1154 | // mirror the value |
| 1155 | // |
| 1156 | for (j = 0; j < TLV_len/2; ++j) |
| 1157 | { |
| 1158 | const char tmp = V[j]; |
| 1159 | V[j] = V[TLV_len - j - 1]; |
| 1160 | V[TLV_len - j - 1] = tmp; |
| 1161 | } |
| 1162 | |
| 1163 | // send the response |
| 1164 | // |
| 1165 | tx_len = write(TLV_socket, TLV, 8 + TLV_len); |
| 1166 | if (tx_len != (8 + TLV_len)) |
| 1167 | { |
| 1168 | fprintf(stderr, "TLV socket closed (3): %s\n", strerror(errno)); |
| 1169 | fclose(f); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | } |
| 1173 | }</code> |
| 1174 | |
| 1175 | </pre> |
| 1176 | </div></div> |
| 1177 | </div> |
| 1178 | <div class="sect2"> |
| 1179 | <h3 id="_popen">popen()</h3> |
| 1180 | <div class="paragraph"><p><strong>Handle←⎕FIO[24] Bs</strong> (aka. <strong>popen</strong>) performs a function similar to <strong>⎕FIO[57]</strong> |
| 1181 | above, but with the following differences:</p></div> |
| 1182 | <div class="ulist"><ul> |
| 1183 | <li> |
| 1184 | <p> |
| 1185 | the pipe is unidirectional |
| 1186 | </p> |
| 1187 | <div class="ulist"><ul> |
| 1188 | <li> |
| 1189 | <p> |
| 1190 | either to stdin (file descriptor 0) of the spawned process, |
| 1191 | </p> |
| 1192 | </li> |
| 1193 | <li> |
| 1194 | <p> |
| 1195 | or to stdout (file descriptor 1) of the spawned process |
| 1196 | </p> |
| 1197 | </li> |
| 1198 | </ul></div> |
| 1199 | </li> |
| 1200 | <li> |
| 1201 | <p> |
| 1202 | the pipe is closed with <strong>⎕FIO[24] Handle</strong> (aka. <strong>pclose</strong>) instead of |
| 1203 | <strong>fclose</strong>. |
| 1204 | </p> |
| 1205 | </li> |
| 1206 | </ul></div> |
| 1207 | <div class="paragraph"><p>The following APL code executes the operating system command ls and prints |
| 1208 | its output:</p></div> |
| 1209 | <div class="listingblock"> |
| 1210 | <div class="content"> |
| 1211 | <pre> |
| 1212 | |
| 1213 | <code> Command←"ls" |
| 1214 | Handle←⎕FIO[24] Command ⍝ PIPE = popen("ls") |
| 1215 | Data←⎕FIO[6] Handle ⍝ fread(PIPE) |
| 1216 | ⎕UCS Data ⍝ display fread() output |
| 1217 | ⎕FIO[25] Handle ⍝ pclose(PIPE)</code> |
| 1218 | |
| 1219 | </pre> |
| 1220 | </div></div> |
| 1221 | </div> |
| 1222 | <div class="sect2"> |
| 1223 | <h3 id="_sockets">Sockets</h3> |
| 1224 | <div class="paragraph"><p>The <strong>⎕FIO</strong> functions 32…47, i.e. <strong>⎕FIO[32]</strong>, <strong>⎕FIO[33]</strong>, … <strong>⎕FIO[47]</strong> |
| 1225 | give APL access to the most common socket functions in libstdc. APL can use |
| 1226 | these functions to set up sockets in a way that specific low-level properties |
| 1227 | can be set via socket options, transport protocol options, and the like. |
| 1228 | The use of these functions requires some experience with socket programming.</p></div> |
| 1229 | <div class="paragraph"><p><strong>info apl</strong> (around section 2.26) provides some more details about ⎕FIO. One |
| 1230 | can invoke ⎕FIO "" to obtain a list of ⎕FIO sub-functions, in particular the |
| 1231 | names of the corresponding functions in the libstdc library.</p></div> |
| 1232 | <div class="paragraph"><p>The function names can then be used with the <strong>man</strong> command which documents the |
| 1233 | expected function arguments. By and large ⎕FIO takes the same arguments as |
| 1234 | the corresponding libc functions, but some of the function arguments (in |
| 1235 | particular those taking pointer arguments) are mapped to APL byte arrays.</p></div> |
| 1236 | <div class="paragraph"><p>The test case file <strong>src/testcases/Quad_FIO.tc</strong> contains APL code for a short |
| 1237 | client/server communication between two TCP sockets on Port 22222 (around |
| 1238 | line 170 of the test case file).</p></div> |
| 1239 | </div> |
| 1240 | <div class="sect2"> |
| 1241 | <h3 id="_native_functions">Native functions</h3> |
| 1242 | <div class="paragraph"><p>If the capabilities provided by sockets are still not enough, then one |
| 1243 | can get complete control by means of GNU APL native functions. A native |
| 1244 | function is a function that can be called from APL (and returns results back |
| 1245 | to APL, but has an implementation in a different language (typically C or |
| 1246 | C++).</p></div> |
| 1247 | <div class="paragraph"><p>Writing native functions is a little more complicated than using communication |
| 1248 | facilities already present in APL like the mechanisms explained so far. The |
| 1249 | design and use of a native function consists of the following steps:</p></div> |
| 1250 | <div class="paragraph"><p>First of all, preparation of a dynamic library that contains implementations |
| 1251 | for those function signatures that the native function shall support. For |
| 1252 | example, suppose a function FOO shall support the 3 signatures</p></div> |
| 1253 | <div class="ulist"><ul> |
| 1254 | <li> |
| 1255 | <p> |
| 1256 | dyadic function call, i.e. <strong>Z←A FOO B</strong>, |
| 1257 | </p> |
| 1258 | </li> |
| 1259 | <li> |
| 1260 | <p> |
| 1261 | monadic function call, i.e. <strong>Z←FOO B</strong>, and |
| 1262 | </p> |
| 1263 | </li> |
| 1264 | <li> |
| 1265 | <p> |
| 1266 | monadic function with axis call, i.e. <strong>Z←FOO[X] B</strong>. |
| 1267 | </p> |
| 1268 | </li> |
| 1269 | </ul></div> |
| 1270 | <div class="paragraph"><p>The native function library then needs to implement 3 functions and make them |
| 1271 | accessible via <strong>dlsym()</strong>.</p></div> |
| 1272 | <div class="paragraph"><p>After the dynamic library, say <strong>FOO.so</strong>, has been created, GNU APL can open |
| 1273 | the dynamic library, with:</p></div> |
| 1274 | <div class="listingblock"> |
| 1275 | <div class="content"> |
| 1276 | <pre> |
| 1277 | |
| 1278 | <code> 'FOO.so' ⎕FX 'FOO'</code> |
| 1279 | |
| 1280 | </pre> |
| 1281 | </div></div> |
| 1282 | <div class="paragraph"><p>That makes the 3 signatures implemented <strong>FOO.so</strong> available in APL (for the |
| 1283 | same signatures).</p></div> |
| 1284 | <div class="paragraph"><p><strong>Note:</strong> Native functions were not primarily intended for communication purposes. |
| 1285 | But since they import the full power of C/C++ into GNU APL they are a last |
| 1286 | resort if all methods above cannot be used.</p></div> |
| 1287 | <div class="paragraph"><p>The directory <strong>src/native</strong> contains C++ code templates for different APL |
| 1288 | function signatures. You can use these templates as a starting point for your |
| 1289 | own native function(s). If you modify a template in-place (i.e. you do NOT copy |
| 1290 | and rename it) then the GNU APL build system will compile your changes and |
| 1291 | create .so files in directory <strong>src/native/.libs</strong> that can be <strong>⎕FX</strong>ed later on.</p></div> |
| 1292 | </div> |
| 1293 | </div> |
| 1294 | </div> |
| 1295 | <div class="sect1"> |
| 1296 | <h2 id="_communication_over_streams">Communication over streams</h2> |
| 1297 | <div class="sectionbody"> |
| 1298 | <div class="paragraph"><p>The majority of the communication mechanisms above use - explicitly or |
| 1299 | implicitly - some sort of data framing between the communication partners. |
| 1300 | This data framing divides the entire communication between the partners into |
| 1301 | several transactions, where one transaction is, depending on the mechanism:</p></div> |
| 1302 | <div class="ulist"><ul> |
| 1303 | <li> |
| 1304 | <p> |
| 1305 | one libapl function call, or |
| 1306 | </p> |
| 1307 | </li> |
| 1308 | <li> |
| 1309 | <p> |
| 1310 | one shared variable assignment, or |
| 1311 | </p> |
| 1312 | </li> |
| 1313 | <li> |
| 1314 | <p> |
| 1315 | one write() to a ⎕FIO[57] pipe, or |
| 1316 | </p> |
| 1317 | </li> |
| 1318 | <li> |
| 1319 | <p> |
| 1320 | one SQL query, or |
| 1321 | </p> |
| 1322 | </li> |
| 1323 | <li> |
| 1324 | <p> |
| 1325 | one datagram sent over a socket of type SOCK_DGRAM, or |
| 1326 | </p> |
| 1327 | </li> |
| 1328 | <li> |
| 1329 | <p> |
| 1330 | one native function call. |
| 1331 | </p> |
| 1332 | </li> |
| 1333 | </ul></div> |
| 1334 | <div class="paragraph"><p>The remaining mechanisms:</p></div> |
| 1335 | <div class="ulist"><ul> |
| 1336 | <li> |
| 1337 | <p> |
| 1338 | popen(), |
| 1339 | </p> |
| 1340 | </li> |
| 1341 | <li> |
| 1342 | <p> |
| 1343 | sockets of type SOCK_STREAM |
| 1344 | </p> |
| 1345 | </li> |
| 1346 | </ul></div> |
| 1347 | <div class="paragraph"><p>send their data in a byte-by-byte fashion, which may cause a problem for the |
| 1348 | receiver because the receiver cannot reliably detect the boundaries between |
| 1349 | different transactions. In these cases the division of the byte stream(s) |
| 1350 | between the communication partners into transactions has to be performed by |
| 1351 | the APL application, and GNU APL provides a few system functions that can |
| 1352 | be used to <strong>frame</strong> (read: encode) an item at the sender side into a sequence |
| 1353 | of bytes, and to <strong>de-frame</strong> (read: decode) the received byte stream back into |
| 1354 | the item that was sent at the receiver side. The <strong>frame</strong> and <strong>de-frame</strong> |
| 1355 | functions cooperate in such a way that;</p></div> |
| 1356 | <div class="listingblock"> |
| 1357 | <div class="content"> |
| 1358 | <pre> |
| 1359 | |
| 1360 | <code> item ≡ decode encode item |
| 1361 | decode ((encode A),(encode B)) ≡ (decode encode A), (decode encode B)</code> |
| 1362 | |
| 1363 | </pre> |
| 1364 | </div></div> |
| 1365 | <div class="paragraph"><p>The <strong>frame</strong> and <strong>de-frame</strong> differ by the kind of item that is to be sent over |
| 1366 | a byte stream.</p></div> |
| 1367 | <div class="sect2"> |
| 1368 | <h3 id="_tag_length_value_aka_tlv">Tag/Length/Value aka. TLV</h3> |
| 1369 | <div class="paragraph"><p>TLV is a frequently used format for sending byte vectors over a stream. The |
| 1370 | byte vector is prepended by a tag field and a length field indicating the |
| 1371 | length of the byte vector. The tag and length fields have a fixed size |
| 1372 | (typically 1, 2, or 4 bytes) while the length of the byte vector can vary. |
| 1373 | Decoding of the TLV is very simple and efficient. The de-framing as such only |
| 1374 | requires the length field, but the tag is often required to distinguish |
| 1375 | different data types sent over the same stream.</p></div> |
| 1376 | <div class="paragraph"><p>System function <strong>33 ⎕CR</strong> converts <strong>TAG,byte-vector</strong> to a TLV (sender side) |
| 1377 | while its inverse function <strong>¯33 ⎕CR</strong> (or <strong>34 ⎕CR</strong>) converts a TLV back to |
| 1378 | <strong>TAG,byte-vector</strong>. Example:</p></div> |
| 1379 | <div class="listingblock"> |
| 1380 | <div class="content"> |
| 1381 | <pre> |
| 1382 | |
| 1383 | <code> 5 ⎕CR 33 ⎕CR 42,"Hello" |
| 1384 | 0000002A0000000548656C6C6F |
| 1385 | │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 1386 | │ │ │ │ │ │ │ │ └─┴─┴─┴─┴───── "Hello" |
| 1387 | │ │ │ │ └─┴─┴─┴─────────────── 5 (length) |
| 1388 | └─┴─┴─┴─────────────────────── 42 (tag) |
| 1389 | |
| 1390 | ¯33 ⎕CR 33 ⎕CR 42,"Hello" |
| 1391 | 42 Hello</code> |
| 1392 | |
| 1393 | </pre> |
| 1394 | </div></div> |
| 1395 | </div> |
| 1396 | <div class="sect2"> |
| 1397 | <h3 id="_lines_terminated_by_line_feeds">Lines terminated by line feeds</h3> |
| 1398 | <div class="paragraph"><p>Many programs, in particular operating system commands, write their output |
| 1399 | line-by-line to their <strong>stdout</strong>. If these programs are started from APL with |
| 1400 | <strong>Handle←24 ⎕FIO[24] Bs</strong> (aka. popen(Bs, "r") then the handle returned to APL |
| 1401 | is a stream of lines that are terminated by line feeds (ASCII 10 or <strong>\n</strong>). |
| 1402 | The first step in the processing of such a stream in APL is usually to split |
| 1403 | the stream into a (nested) vector of lines. This can be easily and efficiently |
| 1404 | done with <strong>35 ⎕CR</strong>. The terminating line feeds are removed in this conversion.</p></div> |
| 1405 | <div class="paragraph"><p>Example:</p></div> |
| 1406 | <div class="listingblock"> |
| 1407 | <div class="content"> |
| 1408 | <pre> |
| 1409 | |
| 1410 | <code> 9 ⎕CR 35 ⎕CR "Hello\nWorld\n" |
| 1411 | ╔═══════════════╗ |
| 1412 | ║┏→━━━━┓ ┏→━━━━┓║ |
| 1413 | ║┃Hello┃ ┃World┃║ |
| 1414 | ║┗━━━━━┛ ┗━━━━━┛║ |
| 1415 | ╚═══════════════╝</code> |
| 1416 | |
| 1417 | </pre> |
| 1418 | </div></div> |
| 1419 | </div> |
| 1420 | </div> |
| 1421 | </div> |
| 1422 | </div> |
| 1423 | <div id="footnotes"><hr /></div> |
| 1424 | <div id="footer"> |
| 1425 | <div id="footer-text"> |
| 1426 | Last updated |
| 1427 | 2025-04-25 16:47:28 CEST |
| 1428 | </div> |
| 1429 | </div> |
| 1430 | </body> |
| 1431 | </html> |