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 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
|
#ifndef lint
static char *RCSid = "$Id: stack.c,v 1.29 2004/04/24 09:32:58 florian Exp $";
#endif
/*
* The Regina Rexx Interpreter
* Copyright (C) 1992-1994 Anders Christensen <anders@pvv.unit.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Concept of implementation:
* In this description, the stack is described as growing upwards
* The stack has two pointers to the ends of a double-linked list,
* which contains entries consisting of just a pointer to the text (in
* a struct streng) in addition to pointers to next and previous
* entry. Last box' next, and first box' prev is both NIL. The text
* pointer points to the text of that line in the stack.
* If the text pointer is NIL, then that box in the stack-structure is
* just symbolizing a stack buffer mark. The integer variable
* 'buffers' contains the current number of buffermarks in the stack.
* A buffer is the lines between two buffermarks. The first buffer mark
* (for buffer 0) is not marked, but is implicit the bottom of the
* stack. The last buffer is the lines between the uppermost
* buffermark and the top of the stack.
* Initialy all lines are put in buffer 0. When a new buffer is
* created, lines pushed lifo is put on top of that buffer, and lines
* queued fifo are put in the bottom of that buffer (i.e. not in the
* bottom of buffer 0.) Lines pushed on the stack are 'eaten' and
* there must not be any referances to them in other parts of the
* program after they have been pushed.
* When reading lines from the stack, and all lines in the current
* buffer has been read, the buffer-mark will be removed, and lines
* are read from the buffer underneath. Lines read from the stack must
* be free'ed by the routine that popped them from the stack.
* When the whole stack is empty, lines are read from standard input.
* Buffer 0 contains the lines between the bottom of the stack, and
* the first buffermark on the stack, the first buffer, is the lines
* between buffermark 1 and buffer mark 2.
* When creating a buffer, the value returned will be the number of
* buffermarks in the stack after the new buffermark is created.
* When destroying buffers, the parameter given will equal the number
* of the lowest buffermark to be destroyed (i.e dropbuf 4) will leave
* 3 buffermarks on the stack).
* A special variable buf is now part of a stack element. It points to
* the last buffer' begin or is NULL for buffer 0.
* In short:
*
* Overview of internal or temporary queues. Even temporary queues may
* contain buffer marks if the content was moved from an internal queue to
* a temporary queue.
* The firstbox is equivalent to firstline and lastbox to lastline.
* The ends of the double linked list are NULL values. p is short for prev,
* n for next. b is short for buf. buf is either NULL or points to the element
* which is the next buffer mark.
* A buffer mark is a StackLine (SL) with a contents-field of NULL.
*
* -->---------->---------->---------->---------->-+
* ^ ^ ^ ^ ^ | NULL NULL
* b b b b b / b b
* | | | | | / | |
* p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n
* || || ||
* firstbox Buffer lastbox
* Mark
*
* READER'S END FIFO'S END
* LIFO'S END
*
* Possible extentions:
* o A new 'hard-buffer', that is not removed when a line is popped
* from it while it is empty
*/
#include "rexx.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PROCESS_H
# include <process.h>
#endif
#include "extstack.h"
#define NUMBER_QUEUES 100
/*
* Queue 0 is SESSION and cannot be added/deleted
*/
#define ENSURE_BUFFER(iq) { \
/* Ensures that at least one buffer exists for an internal queue. \
*/ \
if ( (iq)->u.i.top == NULL ) \
{ \
(iq)->u.i.top = (iq)->u.i.bottom = MallocTSD( sizeof( Buffer ) ) ; \
memset( (iq)->u.i.top, 0, sizeof( Buffer ) ) ; \
assert( (iq)->u.i.elements == 0 ) ; \
assert( (iq)->u.i.buffers == 0 ) ; \
(iq)->u.i.elements = 0 ; \
(iq)->u.i.buffers = 1 ; \
} \
}
typedef struct { /* stk_tsd: static variables of this module (thread-safe) */
#if !defined(NO_EXTERNAL_QUEUES)
int initialized ;
#endif
/*
* The pointer of the current queue points to one of the elements of queue.
*/
Queue *current_queue;
Queue queue[ NUMBER_QUEUES ] ;
/*
* The current queue's name is buffered for a faster access.
*/
streng *current_queue_name;
/*
* runner is needed to create fresh ids.
*/
int runner ;
} stk_tsd_t; /* thread-specific but only needed by this module. see
* init_stacks
*/
#if !defined(NO_EXTERNAL_QUEUES)
static int get_socket_details_and_connect( tsd_t *TSD, Queue *target );
static int save_parse_queue( tsd_t *TSD, streng *queue, Queue *q,
int ignore_name);
static Queue *open_external( tsd_t *TSD, const streng *queue, Queue *q,
int *rc, int ignore_name, streng **basename );
#endif
/* init_stacks initializes the module.
* Currently, we set up the thread specific data.
* The function returns 1 on success, 0 if memory is short.
*/
int init_stacks( tsd_t *TSD )
{
stk_tsd_t *st;
if (TSD->stk_tsd != NULL)
return(1);
if ((st = TSD->stk_tsd = MallocTSD(sizeof(stk_tsd_t))) == NULL)
return(0);
memset(st,0,sizeof(stk_tsd_t)); /* correct for all values */
/*
* Create the default internal SESSION queue on demand. purge_stacks()
* deletes the queuename very often.
*/
st->queue[0].type = QisSESSION ;
st->queue[0].u.i.isReal = 1 ;
st->current_queue = &st->queue[0] ;
return(1);
}
/* delete_buffer_content deletes the content of a buffer and resets the
* buffer to an empty buffer.
*/
static void delete_buffer_content( const tsd_t *TSD, stk_tsd_t *st, Buffer *b )
{
StackLine *p, *h ;
/* prepare the deletion and become reentrant in case of exceptions */
p = b->top ;
b->top = b->bottom = NULL ;
b->elements = 0 ;
while ( p != NULL ) {
h = p ;
p = p->lower ;
if ( h->contents != NULL )
Free_stringTSD( h->contents ) ;
FreeTSD( h ) ;
}
}
/* delete_an_internal_queue deletes the content of an internal queue (including
* SESSION) resets the queue to "not used" or "empty" in case of "SESSION".
*/
static void delete_an_internal_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q )
{
Buffer *p, *h ;
streng *n ;
int type = q->type ;
if ( type == QisUnused )
return ;
assert( ( type == QisInternal ) || ( type == QisSESSION ) ) ;
/* prepare the deletion and become reentrant in case of exceptions */
p = q->u.i.top ;
n = q->u.i.name ;
memset( q, 0, sizeof( Queue ) ) ;
q->type = ( type == QisSESSION ) ? QisSESSION : QisUnused ;
/* delete the name and all content buffers */
if ( n != NULL )
Free_stringTSD( n ) ;
while ( p != NULL ) {
h = p ;
p = p->lower ;
delete_buffer_content( TSD, st, h ) ;
FreeTSD( h ) ;
}
}
/* delete_a_temp_queue cleans up one of the internal stacks by freeing its
* content. The queue is reset to "not used".
*/
static void delete_a_temp_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q )
{
Buffer b ;
if ( q->type == QisUnused ) /* happens very often */
return ;
assert( q->type == QisTemp ) ;
/* prepare the deletion and become reentrant in case of exceptions */
b = q->u.t ;
memset( q, 0, sizeof( Queue ) ) ;
q->type = QisUnused ;
delete_buffer_content( TSD, st, &b ) ;
}
#if !defined(NO_EXTERNAL_QUEUES)
/* delete_an_external_queue cleans up one of the external stacks by
* disconnecting from the server.
*/
static void delete_an_external_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q )
{
Queue b ;
if ( q->type == QisUnused )
return ;
assert( q->type == QisExternal ) ;
/* prepare the deletion and become reentrant in case of exceptions */
b = *q ;
memset( q, 0, sizeof( Queue ) ) ;
q->type = QisUnused ;
disconnect_from_rxstack( TSD, &b ) ;
}
#endif
/* delete_a_queue cleans up any queue by purging or disconnecting. */
static void delete_a_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q )
{
switch ( q->type )
{
case QisTemp:
delete_a_temp_queue( TSD, st, q ) ;
break;
case QisInternal:
case QisSESSION:
delete_an_internal_queue( TSD, st, q ) ;
break;
#if !defined(NO_EXTERNAL_QUEUES)
case QisExternal:
delete_an_external_queue( TSD, st, q ) ;
break;
#endif
default:
assert( q->type == QisUnused ) ;
}
}
/*
* This routine clears all the internal queues we have made
* and if we have a connection to rxstack, disconnects from it
*/
void purge_stacks( const tsd_t *TSD )
{
stk_tsd_t *st;
int i;
st = TSD->stk_tsd;
for ( i = 0; i < NUMBER_QUEUES; i++ )
delete_a_queue( TSD, st, &st->queue[i] );
st->current_queue = &st->queue[0];
if ( st->current_queue_name != NULL )
Free_stringTSD( st->current_queue_name );
st->current_queue_name = NULL;
}
static void SetSessionName( const tsd_t *TSD, stk_tsd_t *st )
{
Queue *q = &st->queue[0];
assert( q->type == QisSESSION ) ;
if ( q->u.i.name == NULL )
{
q->u.i.name = Str_creTSD( "SESSION" ) ;
q->u.i.isReal = 1 ;
st->current_queue_name = Str_dupTSD( q->u.i.name );
}
}
/*
* Find the named internal queue - case insensitive
*/
static Queue *find_queue( const tsd_t *TSD, stk_tsd_t *st, const streng *queue_name )
{
int i;
if ( st->queue[0].u.i.name == NULL )
SetSessionName( TSD, st ) ;
for ( i = 0; i < NUMBER_QUEUES; i++ ) /* inefficient !! */
{
if ( ( st->queue[i].type != QisInternal )
&& ( st->queue[i].type != QisSESSION ) )
continue ;
if ( Str_ccmp( st->queue[i].u.i.name, queue_name ) == 0 )
return &st->queue[i] ;
}
return NULL ;
}
/* This function creates a new queue.
* The returned queue's type must be overwritten.
*/
Queue *find_free_slot( const tsd_t *TSD )
{
int i;
stk_tsd_t *st = TSD->stk_tsd ;
for ( i = 1; i < NUMBER_QUEUES; i++ ) /* never ever select SESSION */
{
if ( st->queue[i].type == QisUnused )
return &st->queue[i] ;
}
if ( !TSD->called_from_saa )
exiterror( ERR_STORAGE_EXHAUSTED, 0 ); /* message not very helpful */
return NULL ;
}
/*
* Glues the content of the temporary stack src to the top buffer of
* dst which must not be temporary. is_fifo controls the position of
* the glue. is_fifo should reflect the type of insertion in the
* temporary buffer.
* src is destroyed at the end of the operation.
*/
void flush_stack( const tsd_t *TSD, Queue *src, Queue *dst, int is_fifo )
{
#if !defined( NO_EXTERNAL_QUEUES )
StackLine *ptr, *h ;
#endif
stk_tsd_t *st;
if ( src == NULL ) /* no temporary stack? may happen */
return ;
st = TSD->stk_tsd;
assert( src->type == QisTemp ) ;
assert( dst->type == QisSESSION
|| dst->type == QisInternal
|| dst->type == QisExternal ) ;
if ( src->u.t.top == NULL )
{
/* nothing in temporary stack to flush */
assert( src->u.t.bottom == NULL ) ;
return ;
}
#if !defined(NO_EXTERNAL_QUEUES)
if ( dst->type == QisExternal )
{
for ( ptr = src->u.t.top; ptr != NULL; )
{
if (is_fifo)
{
queue_line_fifo_to_rxstack( TSD, dst->u.e.socket, ptr->contents ) ;
}
else
{
queue_line_lifo_to_rxstack( TSD, dst->u.e.socket, ptr->contents ) ;
}
h = ptr ;
ptr = ptr->lower ;
FreeTSD( h->contents ) ;
FreeTSD( h ) ;
}
src->u.t.top = src->u.t.bottom = NULL ; /* allow safe cleanup */
delete_a_temp_queue( TSD, st, src ) ;
return ;
}
#endif
/* dst->type is QisSESSION or QisInternal, stack it either fifo or lifo */
ENSURE_BUFFER( dst ) ;
dst->u.i.elements += src->u.t.elements ;
if ( is_fifo )
{
GLUE_BUFFER2( &src->u.t, dst->u.i.top ); /* begin of newest buffer */
}
else
{
GLUE_BUFFER1( dst->u.i.top, &src->u.t );
}
delete_a_temp_queue( TSD, st, src ) ;
}
/*
* stack_to_line converts the content of the temporary stack to one blank
* delimited line.
* An empty string is returned if nothing is in the queue.
*/
streng *stack_to_line( const tsd_t *TSD, Queue *q )
{
StackLine *ptr, *h ;
unsigned size = 0;
char *dest;
stk_tsd_t *st;
streng *retval;
st = TSD->stk_tsd;
if (q->type == QisUnused )
return(nullstringptr());
assert( q->type == QisTemp ) ;
if ( q->u.t.top == NULL )
{
/* nothing in temporary stack to flush */
assert( q->u.t.bottom == NULL ) ;
delete_a_temp_queue( TSD, st, q ) ;
return(nullstringptr());
}
/* first, count the needed string length. */
for ( ptr = q->u.t.top; ptr != NULL; ptr=ptr->lower )
{
size += Str_len(ptr->contents) + 1 ; /* blank is delimiter */
}
retval = Str_makeTSD( size ) ;
dest = retval->value ;
for ( ptr = q->u.t.top; ptr != NULL; )
{
memcpy( dest, ptr->contents->value, Str_len( ptr->contents ) ) ;
dest += Str_len( ptr->contents ) ;
*dest++ = ' ';
h = ptr ;
ptr = ptr->lower ;
Free_stringTSD( h->contents ) ;
FreeTSD( h ) ;
}
/* strip any blanks at the end of the string. */
while ((dest != retval->value) && (dest[-1] == ' '))
dest--;
/* finally, terminate the string */
*dest = '\0'; /* DON'T increment dest */
Str_len( retval ) = (int)( dest - retval->value ) ;
/* reset the pointers, to signal that temporary stack is empty */
q->u.t.top = q->u.t.bottom = NULL ;
delete_a_temp_queue( TSD, st, q ) ;
return(retval);
}
/*
* The input for a command may be overwritten if both input and output or
* input and error are STEM values. A temporary queue is created in this
* case to prevent the destruction of an unread value.
* fill_input_queue_stem fills up a new queue with a copy of a stems
* content. stemname must end with a period and stem0 is the count of lines
* in stem.
* OPTIMIZING HINT: The function can be rewritten to access just only the
* stem leaves.
*/
Queue *fill_input_queue_stem(tsd_t *TSD, streng *stemname, int stem0)
{
int i,stemlen = Str_len( stemname ) ;
streng *stem, *new ;
StackLine *line ;
Queue *q = find_free_slot( TSD ) ;
q->type = QisTemp ;
stem = Str_makeTSD(stemlen + 1 + 3*sizeof(int));
memcpy(stem->value, stemname->value, stemlen);
for (i = 1; i <= stem0; i++)
{ /* Fetch the value: */
stem->len = stemlen + sprintf(stem->value + stemlen, "%d", i);
new = Str_dupTSD(get_it_anyway_compound(TSD, stem));
line = (StackLine *) MallocTSD( sizeof( StackLine ) ) ;
line->contents = new ;
FIFO_LINE( &q->u.t, line ) ;
}
Free_stringTSD(stem);
return q ;
}
/*
* The input for a command may be overwritten if both input and output or
* input and error are STREAM values. A temporary queue is created in this
* case to prevent the destruction of an unread value.
* fill_input_queue_stream fills up a new queue with a copy of a streams
* content. fileptr must be an opened file stream pointer gotten by
* addr_reopen_file() for an input stream.
*/
Queue *fill_input_queue_stream( tsd_t *TSD, void *fileptr )
{
StackLine *line;
streng *c;
Queue *q = find_free_slot( TSD );
q->type = QisTemp;
for ( ; ; )
{ /* Fetch the value: */
c = addr_io_file( TSD, fileptr, NULL );
if ( c == NULL )
break;
if ( c->len == 0 )
{
Free_stringTSD( c );
break;
}
line = (StackLine *) MallocTSD( sizeof( StackLine ) );
line->contents = c;
FIFO_LINE( &q->u.t, line );
}
return q;
}
/*
* use_external checks whether or not an external queue must be processed.
* The different reasons why not to do it are checked.
* use_external returns either 0, if queue_name is a local queue name or
* the current queue is a local queue, or it returns 1 for an external queue
* name or if the current queue is external.
*/
static int use_external( const tsd_t *TSD, const streng *queue_name )
{
stk_tsd_t *st = TSD->stk_tsd;
/* A little bit off topic, but purge_stacks() may kill "SESSION" at the
* end of RexxStart() and we need a working "SESSION" always. Not deleting
* it will produce "errors" with the tracemem feature.
* This function is called at top of each relevant function.
*/
if ( st->queue[0].u.i.name == NULL )
SetSessionName( TSD, st );
#if defined(NO_EXTERNAL_QUEUES)
(queue_name);
return 0; /* trivial */
#else
if ( !st->initialized )
{
/*
* Attention, set the current queue type to default. This may be
* either internal or external.
*/
st->initialized = 1;
init_external_queue( TSD );
}
if ( get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) )
return 0; /* user forces a local queue in every case */
if ( ( queue_name == NULL ) || ( PSTRENGLEN( queue_name ) == 0 ) )
return st->current_queue->type == QisExternal;
if ( get_options_flag( TSD->currlevel, EXT_QUEUES_301 ) == 0 )
{
/*
* A name exists, check it.
*/
if ( memchr( queue_name->value, '@', Str_len( queue_name ) ) == NULL )
return 0;
}
return 1;
#endif
}
/*
* Returns 1 if we have started using external queues, 0 if we haven't
* started using external queues
*/
int external_queues_used( const tsd_t *TSD )
{
return use_external( TSD, NULL ) ;
}
/*
* Pushes 'line' onto the REXX stack, lifo, and sets 'lastline' to
* point to the new line. The line is put on top of the current
* buffer.
*
* A queue_name is supplied by external reference, only. See IfcAddQueue.
* Reuses the current connection if possible.
*/
int stack_lifo( tsd_t *TSD, streng *line, const streng *queue_name )
{
StackLine *newbox ;
Buffer *b ;
stk_tsd_t *st ;
Queue *q ;
int rc=0;
st = TSD->stk_tsd;
assert( line != NULL ) ;
if ( !use_external( TSD, queue_name ) )
{
if ( queue_name )
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
return 9;
}
}
else
q = st->current_queue;
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ;
newbox->contents = line ;
ENSURE_BUFFER( q ) ;
b = q->u.i.top ;
LIFO_LINE( b, newbox ) ;
q->u.i.elements++;
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 0, NULL ) ) == NULL )
return rc;
if ( ( rc = queue_line_lifo_to_rxstack( TSD, work->u.e.socket, line ) )
== -1 )
rc = 100;
disconnect_from_rxstack( TSD, &q ) ;
}
#endif
return rc;
}
/*
* Puts 'line' on the REXX stack, fifo. This routine is similar to
* stack_lifo but the differences are big enough to have a separate
* routine. The line is put in the bottom of the current buffer,
* that is just above the uppermost buffer mark, or at the bottom
* of the stack, if there are no buffer marks.
*
* A queue_name is supplied by external reference, only. See IfcAddQueue.
* Reuses the current connection if possible.
*/
int stack_fifo( tsd_t *TSD, streng *line, const streng *queue_name )
{
StackLine *newbox ;
stk_tsd_t *st;
Queue *q;
Buffer *b ;
int rc=0;
st = TSD->stk_tsd;
assert( line != NULL ) ;
if ( !use_external( TSD, queue_name ) )
{
if ( queue_name )
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
return 9;
}
}
else
q = st->current_queue;
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ;
newbox->contents = line ;
ENSURE_BUFFER( q ) ;
b = q->u.i.top ;
FIFO_LINE( b, newbox ) ;
q->u.i.elements++;
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 0, NULL ) ) == NULL )
return rc;
if ( ( rc = queue_line_fifo_to_rxstack( TSD, work->u.e.socket, line ) )
== -1 )
rc = 100;
disconnect_from_rxstack( TSD, &q ) ;
}
#endif
return rc;
}
/*
* Removes one (or several) buffers from the stack. Number must be a
* non-negative number giving the number of first buffer from the bottom
* which has to be removed. You can pass a number of buffers to destroy
* from top of the stack if you supply the negative number of buffers.
*
* Example: Queue has buffers 0, 1, 2, 3, 4 and the number is 3. Then
* the buffers 3 and 4 are removed and 2 becomes the current buffer.
* If the number is -2 then the top two buffers are destroyed and
* 2 becomes the current buffer.
*
* Return values: -2: specified buffer doesn't exist
* -1: external queues don't support buffers
* others: remaining buffers after success
*/
int drop_buffer( const tsd_t *TSD, int number )
{
Buffer *b, *h ;
stk_tsd_t *st;
Queue *curr ;
st = TSD->stk_tsd;
curr = st->current_queue ;
if ( curr->type == QisExternal )
exiterror( ERR_EXTERNAL_QUEUE, 110, "DROPBUF" ) ;
assert( ( curr->type == QisInternal ) || ( curr->type == QisSESSION ) ) ;
ENSURE_BUFFER( curr ) ; /* makes things easier */
if ( number < 0 )
number = curr->u.i.buffers + number ;
if ( number >= (int) curr->u.i.buffers )
return -2 ;
for ( b = curr->u.i.bottom; ( number > 0 ) && ( b != NULL ); number-- )
b = b->higher ;
assert( b ) ; /* must exist */
/* disconnect the buffer sequence marked for deletion */
if ( b == curr->u.i.bottom )
{
/* preserve en empty buffer */
curr->u.i.elements -= b->elements ;
delete_buffer_content( TSD, st, b ) ;
b = b->higher ;
curr->u.i.top = curr->u.i.bottom ;
curr->u.i.top->higher = NULL ;
}
else
{
curr->u.i.top = b->lower ;
curr->u.i.top->higher = NULL ;
}
/* delete each buffer and its content */
while ( b != NULL ) {
curr->u.i.elements -= b->elements ;
delete_buffer_content( TSD, st, b ) ;
h = b ;
b = b->higher ;
FreeTSD( h ) ;
curr->u.i.buffers-- ;
}
return curr->u.i.buffers - 1 ;
}
/*
* Fetches a line from the top of the stack. If the current buffer
* does not contain any lines, it is removed and the second current
* buffer is search for a line etc. If there isn't any lines in the
* stack, a line is read from the standard input.
* Reuses the current connection if possible.
*/
streng *popline( tsd_t *TSD, const streng *queue_name, int *result, unsigned long waitflag )
{
streng *contents=NULL ;
StackLine *line ;
stk_tsd_t *st ;
int need_line_from_stdin=0;
int rc=0;
Queue *q ;
Buffer *b ;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
if ( queue_name )
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
if ( result )
*result = 9;
if ( !TSD->called_from_saa )
contents = nullstringptr();
return contents;
}
}
else
q = st->current_queue;
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
line = NULL ;
while ( ( b = q->u.i.top ) != NULL )
{
POP_LINE( b, line ) ;
if ( line != NULL )
{
q->u.i.elements-- ;
break ;
}
/* buffer is empty, fetch the next one and drop the empty one
*/
q->u.i.top = b->lower ;
q->u.i.buffers-- ;
FreeTSD( b ) ;
}
if ( line != NULL )
{
contents = line->contents ;
FreeTSD( line ) ;
}
else
{
/* q->u.i.top == NULL, too; but bottom's not set */
q->u.i.bottom = NULL ;
assert( q->u.i.elements == 0 ) ;
assert( q->u.i.buffers == 0 ) ;
need_line_from_stdin = 1;
}
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 0, NULL ) ) == NULL )
{
if ( result )
*result = rc;
return NULL;
}
rc = get_line_from_rxstack( TSD, work->u.e.socket, &contents, ( waitflag == 0 ) );
switch ( rc )
{
case -1: rc = 100; break;
case 2: rc = 9; break; /* map generic error to RXQUEUE_NOTREG */
default: ;
}
disconnect_from_rxstack( TSD, &q ) ;
if ( ( rc == 1 ) || ( rc == 4 ) ) /* empty or timeout */
need_line_from_stdin = 1;
}
#endif
if ( need_line_from_stdin )
{
if ( TSD->called_from_saa )
{
rc = 8; /* RXQUEUE_EMPTY */
}
else if ( rc == 4 )
{
rc = 8; /* RXQUEUE_EMPTY */
}
else
{
int rc2 = HOOK_GO_ON ;
if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_PULL ) )
rc2 = hookup_input( TSD, HOOK_PULL, &contents );
if ( rc2 == HOOK_GO_ON )
contents = readkbdline( TSD );
assert( contents );
rc = 0;
}
rc = 0;
}
if ( result )
*result = rc;
else if ( ( contents == NULL ) && !TSD->called_from_saa )
contents = nullstringptr();
return contents;
}
/*
* Counts the lines in the stack.
* Reuses the current connection if possible.
*/
int lines_in_stack( tsd_t *TSD, const streng *queue_name )
{
stk_tsd_t *st ;
int lines ;
Queue *q ;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
if ( queue_name )
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
return -9;
}
}
else
q = st->current_queue;
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
lines = q->u.i.elements ;
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
int rc;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 0, NULL ) ) == NULL )
return -rc;
lines = get_number_in_queue_from_rxstack( TSD, work->u.e.socket, &rc );
disconnect_from_rxstack( TSD, &q ) ;
if ( rc != 0 )
lines = -rc;
}
#endif
return lines ;
}
#ifdef TRACEMEM
/*
* Marks all chunks of dynamic allocated memory that are allocated
* to the stack subsystem.
*/
static void mark_buffer( const Buffer *b )
{
StackLine *ptr ;
for ( ptr = b->top; ptr != NULL; ptr = ptr->lower )
{
markmemory( ptr, TRC_STACKBOX ) ;
markmemory( ptr->contents, TRC_STACKLINE ) ;
}
}
void mark_stack( const tsd_t *TSD )
{
Buffer *b ;
Queue *q ;
stk_tsd_t *st;
int i;
st = TSD->stk_tsd;
q = st->current_queue ;
for ( i = 0; i < NUMBER_QUEUES; i++ )
{
switch ( st->queue[i].type )
{
default:
assert( st->queue[i].type ) ;
case QisUnused:
break;
case QisSESSION:
case QisInternal:
if ( st->queue[i].u.i.name )
{
markmemory( st->queue[i].u.i.name, TRC_STACKBOX ) ;
}
for ( b = st->queue[i].u.i.top; b != NULL; b = b->lower )
{
markmemory( b, TRC_STACKBOX ) ;
mark_buffer( b ) ;
}
break ;
case QisTemp:
mark_buffer( &st->queue[i].u.t ) ;
break ;
case QisExternal:
markmemory( st->queue[i].u.e.name, TRC_STACKBOX ) ;
break ;
}
}
}
#endif
/*
* Creates a new buffer and returns the number of buffers in the stack
* excluding the zeroth buffer.
*/
int make_buffer( tsd_t *TSD )
{
stk_tsd_t *st;
Buffer *b ;
Queue *q ;
st = TSD->stk_tsd;
q = st->current_queue ;
if ( q->type == QisExternal )
exiterror( ERR_EXTERNAL_QUEUE, 110, "MAKEBUF" ) ;
assert( ( q->type == QisSESSION )
|| ( q->type == QisInternal ) ) ;
ENSURE_BUFFER( q ) ; /* Possibly create the zeroth buffer */
/* Make a *new* buffer */
b = MallocTSD( sizeof( Buffer ) ) ;
memset( b, 0, sizeof( Buffer ) ) ;
b->lower = q->u.i.top ; /* The zeroth buffer exists */
b->lower->higher = b ;
q->u.i.top = b ;
q->u.i.buffers++ ;
return q->u.i.buffers - 1 ;
}
/*
* Dumps the contents of the stack to standard error. Buffer marks are
* indicated in the printout.
*/
void type_buffer( tsd_t *TSD )
{
Buffer *b ;
StackLine *ptr ;
char *cptr, *stop ;
int counter ;
streng *name ;
stk_tsd_t *st;
Queue *q ;
if (TSD->stddump == NULL)
return;
st = TSD->stk_tsd;
q = st->current_queue ;
name = get_queue( TSD ) ;
fprintf(TSD->stddump,"==> Name: %.*s\n", Str_len(name), name->value ) ;
fprintf(TSD->stddump,"==> Lines: %d\n", lines_in_stack( TSD, NULL )) ;
if ( q->type == QisExternal )
return ;
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
ENSURE_BUFFER( q ) ; /* Possibly create the zeroth buffer */
for ( counter = q->u.i.buffers, b = q->u.i.top; b != NULL; b = b->lower )
{
fprintf(TSD->stddump,"==> Buffer: %d\n", --counter ) ;
for ( ptr = b->top; ptr != NULL; ptr = ptr->lower )
{
putc( '"', TSD->stddump ) ;
stop = Str_end( ptr->contents ) ;
for ( cptr = ptr->contents->value ; cptr < stop; cptr++ )
putc( ( rx_isprint( *cptr ) ? ( *cptr ) : '?' ), TSD->stddump ) ;
putc( '"', TSD->stddump ) ;
#if defined(DOS) || defined(OS2) || defined(WIN32)
putc( REGINA_CR, TSD->stddump ) ;
#endif
putc( REGINA_EOL, TSD->stddump ) ;
}
}
fprintf(TSD->stddump,"==> End of Stack\n") ;
fflush(TSD->stddump) ;
}
#if !defined(NO_EXTERNAL_QUEUES)
/*
* The following functions allow interfacing to the external
* queue process; rxstack
* The connection to eq is opened if and only if it isn't opened yet.
*/
static int get_socket_details_and_connect( tsd_t *TSD, Queue *q )
{
stk_tsd_t *st;
st = TSD->stk_tsd;
assert( q->type == QisExternal ) ;
/* Fill in default values. Missing values are rarely used but it may
* happen.
*/
if ( q->u.e.name == NULL )
q->u.e.name = default_external_name( TSD ) ;
if ( q->u.e.portno == 0 )
q->u.e.portno = default_port_number() ;
if ( q->u.e.address == 0 )
q->u.e.address = default_external_address() ;
if ( q->u.e.socket == -1 )
{
if ( connect_to_rxstack( TSD, q ) == -1 )
return 100; /* RXQUEUE_NETERROR */
}
return 0;
}
/*
* save_parse_queue wraps parse_queue and exiterrors in case of an error.
* Returns 1 if queue is (probably) different from the current queue, 0 else.
* *q is filled even with a return value of 0.
* A negative return code indicates a RXQUEUE_??? error code which must be
* passed back to the SAA interface immediately.
* Queues are treated as equal if ignore_name is set but host address and port
* numbers are the same.
*/
static int save_parse_queue( tsd_t *TSD, streng *queue, Queue *q,
int ignore_name)
{
stk_tsd_t *st = TSD->stk_tsd;
Queue *curr ;
int rc;
if ( ( rc = parse_queue( TSD, queue, q ) ) <= 0 )
return rc;
if ( !ignore_name && ( PSTRENGLEN( queue ) != 0 ) )
return 1 ;
curr = st->current_queue ;
if ( curr->type != QisExternal )
return 1 ;
if ( q->u.e.address != curr->u.e.address )
return 1 ;
if ( q->u.e.portno != curr->u.e.portno )
return 1 ;
return 0 ;
}
/*
* open_external opens the connection to the external queue will a full
* error checking. NULL is returned on error and *rc describes the
* RXQUEUE_??? error.
* On success the opened queue is returned and *q is set to the old or
* temporary queue. queue is either NULL or describes the new temporary
* queue's name.
* Queues are treated as equal if ignore_name is set but host address and port
* numbers are the same. No set_queue_in_rxstack() is called if ignore_name
* is set.
* *basename is set to the queue's name without the network extension if
* basename != NULL. It must be freed later. *basename may become NULL.
*/
static Queue *open_external( tsd_t *TSD, const streng *queue, Queue *q,
int *rc, int ignore_name, streng **basename )
{
stk_tsd_t *st = TSD->stk_tsd;
streng *qn;
int h;
Queue *retval;
qn = ( queue == NULL ) ? NULL : Str_dupTSD( queue );
if ( ( h = save_parse_queue( TSD, qn, q, ignore_name ) ) < 0 )
{
if ( qn != NULL )
Free_stringTSD( qn );
*rc = -h;
return NULL;
}
if ( h == 1 )
{
if ( ( h = get_socket_details_and_connect( TSD, q ) ) != 0 )
{
if ( qn != NULL )
Free_stringTSD( qn );
disconnect_from_rxstack( TSD, q );
*rc = h;
return NULL;
}
if ( !ignore_name )
{
if ( ( h = set_queue_in_rxstack( TSD, q->u.e.socket, qn ) ) != 0 )
{
if ( qn != NULL )
Free_stringTSD( qn );
disconnect_from_rxstack( TSD, q );
*rc = h;
return NULL;
}
}
retval = q ;
}
else
{
retval = st->current_queue ;
}
if ( basename != NULL )
*basename = qn;
else if ( qn != NULL )
Free_stringTSD( qn );
*rc = 0;
return retval;
}
#endif
/* SetCurrentQueue set the current queue and makes all the necessary cleanup
* of the "old" current queue.
* new_name is assigned to the current queue's name, the former value is
* returned.
*/
static streng *SetCurrentQueue( const tsd_t *TSD, stk_tsd_t *st, Queue *q,
streng *new_name )
{
streng *retval;
#if !defined( NO_EXTERNAL_QUEUES )
if ( ( st->current_queue->type == QisExternal )
&& ( st->current_queue != q ) )
delete_an_external_queue( TSD, st, st->current_queue );
#else
(TSD);
#endif
st->current_queue = q;
retval = st->current_queue_name;
st->current_queue_name = new_name;
return retval;
}
/*
* Create a new queue
* RXQUEUE( 'Create' )
* Reuses the current connection if possible. The newly created current
* queue becomes the current queue.
*/
int create_queue( tsd_t *TSD, const streng *queue_name, streng **result )
{
streng *new_queue = NULL ;
stk_tsd_t *st;
char buf[50];
Queue *q = NULL ;
int rc = 0 ;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
if ( queue_name == NULL )
{
/*
* Create a unique queue name
*/
sprintf(buf,"S%d-%ld-%d", getpid(), clock(), st->runner++ );
new_queue = Str_cre_TSD( TSD, buf ) ;
}
else
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
/*
* No queue of that name, so use it.
*/
new_queue = Str_dupTSD( queue_name ) ;
}
else
{
if ( q->type == QisSESSION )
{
if ( !TSD->called_from_saa )
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue from stack" );
return 5 ;
}
/*
* If the queue we found is a false queue, we can still
* use the supplied name and the slot
*/
if ( q->u.i.isReal )
{
/*
* Create a unique queue name
*/
sprintf(buf,"S%d-%ld-%d", getpid(), clock(), st->runner++ );
new_queue = Str_cre_TSD( TSD, buf ) ;
rc = 1;
}
}
}
if ( new_queue != NULL ) /* new name for a new slot */
{
q = find_free_slot( TSD );
if ( q == NULL )
{
if ( new_queue != NULL )
Free_stringTSD( new_queue );
return 12; /* RXQUEUE_MEMFAIL */
}
q->type = QisInternal ;
if ( new_queue == queue_name ) /* need a fresh one */
new_queue = Str_dupTSD( new_queue ) ;
q->u.i.name = Str_upper( new_queue ) ;
}
assert( q->type == QisInternal ) ;
q->u.i.isReal = 1;
/*
* result created here; up to caller to free it
*/
*result = Str_dupTSD( q->u.i.name );
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
streng *base;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 1, &base ) ) == NULL )
return rc;
if ( ( rc = create_queue_on_rxstack( TSD, work, base, result ) ) == -1 )
rc = 100;
if ( base != NULL )
Free_stringTSD( base );
disconnect_from_rxstack( TSD, &q ) ;
}
#endif
return rc;
}
/*
* Delete a queue
* RXQUEUE( 'Delete' )
* Reuses the current connection if possible. SESSION becomes the current
* queue always.
*/
int delete_queue( tsd_t *TSD, const streng *queue_name )
{
int rc = 0;
stk_tsd_t *st;
Queue *q;
streng *h;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
if ( ( queue_name == NULL ) || ( PSTRENGLEN( queue_name ) == 0 ) )
return 9 ;
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
return 9 ;
if ( q->type == QisSESSION )
return 5 ; /* SESSION can't be deleted */
assert( q->type == QisInternal ) ;
/*
* If we found a false queue, return 9
*/
if ( !q->u.i.isReal )
rc = 9;
/*
* Delete the contents of the queue
* and mark it as gone.
*/
delete_an_internal_queue( TSD, st, q );
h = SetCurrentQueue( TSD, st, &st->queue[0], Str_creTSD( "SESSION" ) ) ;
Free_stringTSD( h );
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
streng *base;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 1, &base ) ) == NULL )
return rc;
if ( ( base == NULL ) || ( PSTRENGLEN( base ) == 0 ) )
{
if ( base != NULL )
Free_stringTSD( base );
if ( TSD->called_from_saa )
return 9;
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of(TSD, queue_name ) ) ;
}
if ( ( rc = delete_queue_from_rxstack( TSD, work->u.e.socket, base ) ) == 0 )
{
/* This will disconnect the current external queue if needed */
h = SetCurrentQueue( TSD, st, &st->queue[0], Str_creTSD( "SESSION" ) );
Free_stringTSD( h );
}
if ( base != NULL )
Free_stringTSD( base );
disconnect_from_rxstack( TSD, &q ) ;
if ( rc == -1 )
rc = 100;
}
#endif
return rc;
}
/*
* Set timeout for queue
* RXQUEUE( 'Timeout' )
* Do not reuse the current connection. A different external queue name
* leads to an expensive NOP.
* Timeout only valid for external queues.
*/
int timeout_queue( tsd_t *TSD, const streng *timeout, const streng *queue_name )
{
stk_tsd_t *st;
int rc = 0 ;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
exiterror( ERR_EXTERNAL_QUEUE, 111, "TIMEOUT" ) ;
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
int val, err;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 1, NULL ) ) == NULL )
return rc;
/*
* Convert incoming streng to positive (or zero) integer
*/
val = streng_to_int( TSD, timeout, &err ) ;
if ( ( val < -1 ) || err )
{
disconnect_from_rxstack( TSD, &q ) ;
exiterror( ERR_INCORRECT_CALL, 930, 999999999, tmpstr_of( TSD, timeout ) );
}
rc = timeout_queue_on_rxstack( TSD, work->u.e.socket, val );
disconnect_from_rxstack( TSD, &q ) ;
}
#endif
return rc ;
}
/*
* Return the name of the current queue. The value is silently nul terminated.
* RXQUEUE( 'Get' )
* Returns queue name even if it is a false queue
*/
streng *get_queue( tsd_t *TSD )
{
streng *result;
stk_tsd_t *st;
int l;
char *p;
st = TSD->stk_tsd;
if ( st->queue[0].u.i.name == NULL )
SetSessionName( TSD, st ) ;
assert( st->current_queue_name != NULL );
l = Str_len( st->current_queue_name );
result = Str_makeTSD( l + 1 );
p = Str_val( result );
memcpy( p, Str_val( st->current_queue_name ), l );
p[l] = '\0';
Str_len( result ) = l;
return result;
}
/*
* Return the name and length of the current queue in argument pointers. No
* copy is done.
*/
void fill_queue_name( const tsd_t *TSD, int *len, char **name )
{
stk_tsd_t *st;
st = TSD->stk_tsd;
if ( st->queue[0].u.i.name == NULL )
SetSessionName( TSD, st ) ;
assert( st->current_queue_name != NULL );
*len = Str_len( st->current_queue_name );
*name = Str_val( st->current_queue_name );
}
/*
* Set the current queue
* RXQUEUE( 'Set' )
* Object Rexx allows the queue to be set to an unknown queue
* This is stupid; the user should be told its not valid, and
* return an empty string.
* But we validly set the false queue :-(
* Reuses the current connection if possible.
*/
streng *set_queue( tsd_t *TSD, const streng *queue_name )
{
stk_tsd_t *st ;
Queue *q ;
st = TSD->stk_tsd;
if ( !use_external( TSD, queue_name ) )
{
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL )
{
/*
* We didn't find a real or a false queue, so create
* a false queue
*/
q = find_free_slot( TSD );
q->type = QisInternal ;
q->u.i.name = Str_upper( Str_dupTSD( queue_name ) ) ;
q->u.i.isReal = 0 ; /* false queue */
}
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ;
return SetCurrentQueue( TSD, st, q, Str_dupTSD( q->u.i.name ) );
}
#if !defined(NO_EXTERNAL_QUEUES)
else
{
Queue q, *work;
streng *base, *result;
int rc;
if ( ( work = open_external( TSD, queue_name, &q, &rc, 1, &base ) ) == NULL )
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue from stack" );
if ( ( base == NULL ) || ( PSTRENGLEN( base ) == 0 ) )
{
if ( base != NULL )
Free_stringTSD( base );
disconnect_from_rxstack( TSD, &q ) ;
assert( !TSD->called_from_saa );
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of(TSD, queue_name ) ) ;
}
if ( ( rc = set_queue_in_rxstack( TSD, work->u.e.socket, base ) ) == 0 )
{
Free_stringTSD( base );
if ( ( rc = get_queue_from_rxstack( TSD, work, &result ) ) != 0 )
{
disconnect_from_rxstack( TSD, &q );
assert( !TSD->called_from_saa );
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue back from stack" );
}
if ( work == &q )
{
work = find_free_slot( TSD );
*work = q;
}
return SetCurrentQueue( TSD, st, work, result );
}
Free_stringTSD( base );
disconnect_from_rxstack( TSD, &q ) ;
assert( !TSD->called_from_saa );
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue from stack" );
}
#endif
return NULL;
}
Queue *addr_reopen_queue( tsd_t *TSD, const streng *queuename, char code )
/* This is the open routine for the ADDRESS WITH-redirection. queuename is
* the name of the queue. code is either 'r' for "READ",
* 'A' for "WRITE APPEND", 'R' for "WRITE REPLACE".
* The queue named queuename will be (re)opened in all cases.
* In case of REPLACE the queue's content must be purged later by
* addr_purge_queue.
* A structure for queues is returned and should be used for calls
* to addr_io_queue.
* The return value *may* be NULL in case of an error. A NOTREADY condition
* may have been raised in this case.
*/
{
stk_tsd_t *st ;
Queue *q ;
streng *name ;
st = TSD->stk_tsd ;
if ( ( queuename == NULL ) || ( PSTRENGLEN( queuename ) == 0 ) )
return st->current_queue ;
if ( !use_external( TSD, queuename ) )
{
q = find_queue( TSD, st, queuename ) ;
if ( ( code == 'r' ) || ( q != NULL ) )
{
if ( q != NULL )
q->u.i.isReal = 1 ; /* not sure if this's right */
return q ;
}
/* create a new queue */
q = find_free_slot( TSD ) ;
q->type = QisInternal ;
name = Str_dupTSD( queuename ) ;
q->u.i.name = Str_upper( name ) ;
q->u.i.isReal = 1 ;
return q ;
}
#if !defined( NO_EXTERNAL_QUEUES )
/* always use a fresh connection for external queues. There are too many
* circumstances to keep in mind to check cleanly, whether or not to use
* the current connection or if it exists already. The caller will check
* for same names later.
*/
q = find_free_slot( TSD ) ;
name = Str_dupTSD( queuename ) ;
if ( save_parse_queue( TSD, name, q, 1 ) < 0 )
{
Free_stringTSD( name );
return NULL;
}
if ( get_socket_details_and_connect( TSD, q ) != 0 )
{
disconnect_from_rxstack( TSD, q ) ;
return NULL;
}
set_queue_in_rxstack( TSD, q->u.e.socket, name );
/* We can't be sure using a real queue at this place.
* FIXME: But we can't determine any difference at this stage.
* Thus, continue and that's it.
*/
return q ;
#else
return NULL ;
#endif
}
/* returns 1 if the two queue pointers point to the same physical queue,
* 0 else.
*/
int addr_same_queue( const tsd_t *TSD, const Queue *q1, const Queue *q2 )
{
int t1, t2, retval = 0 ;
#if !defined( NO_EXTERNAL_QUEUES )
streng *n1, *n2 ;
#endif
if ( q1 == q2 )
return 1 ;
if ( ( t1 = q1->type ) == QisSESSION )
t1 = QisInternal ;
if ( ( t2 = q2->type ) == QisSESSION )
t2 = QisInternal ;
if ( t1 != t2 )
return 0 ;
if (t1 == QisInternal)
return !Str_cmp( q1->u.i.name, q2->u.i.name ) ;
assert( q1->type == QisExternal ) ;
if ( q1->u.e.address != q2->u.e.address )
return 0 ;
if ( q1->u.e.portno != q2->u.e.portno )
return 0 ;
/* we have to use the slow name comparison */
#if !defined( NO_EXTERNAL_QUEUES )
if ( get_queue_from_rxstack( TSD, q1, &n1 ) != 0 )
return 0;
if ( get_queue_from_rxstack( TSD, q2, &n2 ) != 0 )
{
Free_stringTSD( n1 ) ;
return 0;
}
retval = !Str_cmp( n1, n2 ) ;
Free_stringTSD( n1 ) ;
Free_stringTSD( n2 ) ;
#endif
return retval ;
}
/* addr_redir_queue moves the content of the top buffer of an existing queue
* to a new temporary queue.
*/
Queue *addr_redir_queue( const tsd_t *TSD, Queue *q )
{
stk_tsd_t *st ;
Queue *retval ;
Buffer *b ;
#if !defined( NO_EXTERNAL_QUEUES )
streng *contents ;
StackLine *ptr ;
#endif
st = TSD->stk_tsd ;
assert( ( q->type == QisSESSION )
|| ( q->type == QisInternal )
|| ( q->type == QisExternal ) ) ;
retval = find_free_slot( TSD ) ;
retval->type = QisTemp ;
if ( ( q->type == QisSESSION ) || ( q->type == QisInternal ) )
{
/* trivial */
if ( ( b = q->u.i.top ) != NULL )
{
/*
* Fixes bug 777645
*/
q->u.i.elements -= b->elements ;
retval->u.t = *b ;
retval->u.t.higher = retval->u.t.lower = NULL ;
b->top = b->bottom = NULL ;
b->elements = 0 ;
}
}
#if !defined( NO_EXTERNAL_QUEUES )
else
{
/* loop until EOF. We can't use lines_in_stack, since the number may
* change because of multiple accesses by different clients.
*/
while ( get_line_from_rxstack( TSD, q->u.e.socket, &contents, 1 ) == 0 )
{
ptr = (StackLine *) MallocTSD( sizeof( StackLine ) ) ;
ptr->contents = contents ;
FIFO_LINE( &retval->u.t, ptr ) ;
}
}
#endif
return retval ;
}
/* addr_purge_queue discards the contents of a queue. It is caused by a
* ADDRESS WITH ??? REPLACE ??? or at the end of the use of an input
* queue.
*/
void addr_purge_queue( const tsd_t *TSD, Queue *q )
{
stk_tsd_t *st ;
Buffer *b ;
st = TSD->stk_tsd ;
assert( ( q->type == QisSESSION )
|| ( q->type == QisInternal )
|| ( q->type == QisExternal ) ) ;
if ( ( q->type == QisSESSION ) || ( q->type == QisInternal ) )
{
ENSURE_BUFFER( q ) ;
b = q->u.i.top ;
q->u.i.elements -= b->elements ;
delete_buffer_content( TSD, st, b ) ;
}
#if !defined( NO_EXTERNAL_QUEUES )
else
{
clear_queue_on_rxstack( TSD, q->u.e.socket ) ;
}
#endif
}
streng *addr_io_queue( tsd_t *TSD, Queue *q, streng *buffer, int isFIFO )
/* This is the working routine for the ADDRESS WITH-redirection. q is
* the return value of addr_reopen_queue. buffer must be NULL for a read
* operation or a filled buffer.
* The return value is NULL in case of a write operation or in case of EOF
* while reading.
* buffer is consumed if given and indicates a push operation instead of a
* pull operation. isFIFO indicates the type of the push operation.
*/
{
StackLine *ptr, *newbox ;
Buffer *b ;
streng *retval ;
if ( buffer == NULL )
{
/* pull operation */
switch ( q->type )
{
case QisSESSION:
case QisInternal:
ENSURE_BUFFER( q ) ;
b = q->u.i.top ; /* Work on the top buffer only */
POP_LINE( b, ptr ) ;
if ( ptr == NULL )
return NULL ;
q->u.i.elements-- ;
retval = ptr->contents ;
FreeTSD( ptr ) ;
return retval ;
case QisTemp:
b = &q->u.t ;
POP_LINE( b, ptr ) ;
if ( ptr == NULL )
return NULL ;
retval = ptr->contents ;
FreeTSD( ptr ) ;
return retval ;
#if !defined( NO_EXTERNAL_QUEUES )
case QisExternal:
if ( get_line_from_rxstack( TSD, q->u.e.socket, &retval, 1 ) == 0 )
return retval ;
return NULL ;
#endif
default:
break ;
}
/* default: (probably an unused queue) */
return NULL ;
}
/* buffer's not NULL */
assert( ( Str_max( buffer ) >= Str_len( buffer ) ) ) ;
#if !defined(NO_EXTERNAL_QUEUES)
if ( q->type == QisExternal )
{
if ( isFIFO )
queue_line_fifo_to_rxstack( TSD, q->u.e.socket, buffer ) ;
else
queue_line_lifo_to_rxstack( TSD, q->u.e.socket, buffer ) ;
return NULL ;
}
#endif
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ;
newbox->contents = buffer ;
if ( isFIFO )
{
/* push FIFO operation */
switch ( q->type )
{
case QisSESSION:
case QisInternal:
ENSURE_BUFFER( q ) ;
b = q->u.i.top ;
q->u.i.elements++ ;
FIFO_LINE( b, newbox ) ;
break ;
case QisTemp:
b = &q->u.t ;
q->u.i.elements++ ;
FIFO_LINE( b, newbox ) ;
break ;
default:
break ;
}
return NULL ;
}
/* finally, we process push LIFO */
switch ( q->type )
{
case QisSESSION:
case QisInternal:
ENSURE_BUFFER( q ) ;
b = q->u.i.top ;
q->u.i.elements++ ;
LIFO_LINE( b, newbox ) ;
break ;
case QisTemp:
b = &q->u.t ;
q->u.i.elements++ ;
LIFO_LINE( b, newbox ) ;
break ;
default:
break ;
}
return NULL ;
}
/* addr_close_queue closes a queue opened by addr_reopen_queue.
* Never use another routine to do this.
* It is also capable to purge and close temporary stacks.
*/
void addr_close_queue( const tsd_t *TSD, Queue *q )
{
stk_tsd_t *st ;
st = TSD->stk_tsd;
if ( q->type == QisTemp )
{
delete_a_temp_queue( TSD, st, q ) ;
return ;
}
#if !defined(NO_EXTERNAL_QUEUES)
if ( ( q != st->current_queue ) && ( q->type == QisExternal ) )
delete_an_external_queue( TSD, st, q ) ;
#endif
}
|