| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 1992-1994 Anders Christensen <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library; if not, write to the Free |
| 17 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Concept of implementation: |
| 21 |
|
| 22 |
* In this description, the stack is described as growing upwards |
| 23 |
|
| 24 |
* The stack has two pointers to the ends of a double-linked list, |
| 25 |
* which contains entries consisting of just a pointer to the text (in |
| 26 |
* a struct streng) in addition to pointers to next and previous |
| 27 |
* entry. Last box' next, and first box' prev is both NIL. The text |
| 28 |
* pointer points to the text of that line in the stack. |
| 29 |
|
| 30 |
* If the text pointer is NIL, then that box in the stack-structure is |
| 31 |
* just symbolizing a stack buffer mark. The integer variable |
| 32 |
* 'buffers' contains the current number of buffermarks in the stack. |
| 33 |
* A buffer is the lines between two buffermarks. The first buffer mark |
| 34 |
* (for buffer 0) is not marked, but is implicit the bottom of the |
| 35 |
* stack. The last buffer is the lines between the uppermost |
| 36 |
* buffermark and the top of the stack. |
| 37 |
|
| 38 |
* Initialy all lines are put in buffer 0. When a new buffer is |
| 39 |
* created, lines pushed lifo is put on top of that buffer, and lines |
| 40 |
* queued fifo are put in the bottom of that buffer (i.e. not in the |
| 41 |
* bottom of buffer 0.) Lines pushed on the stack are 'eaten' and |
| 42 |
* there must not be any referances to them in other parts of the |
| 43 |
* program after they have been pushed. |
| 44 |
|
| 45 |
* When reading lines from the stack, and all lines in the current |
| 46 |
* buffer has been read, the buffer-mark will be removed, and lines |
| 47 |
* are read from the buffer underneath. Lines read from the stack must |
| 48 |
* be free'ed by the routine that popped them from the stack. |
| 49 |
|
| 50 |
* When the whole stack is empty, lines are read from standard input. |
| 51 |
|
| 52 |
* Buffer 0 contains the lines between the bottom of the stack, and |
| 53 |
* the first buffermark on the stack, the first buffer, is the lines |
| 54 |
* between buffermark 1 and buffer mark 2. |
| 55 |
|
| 56 |
* When creating a buffer, the value returned will be the number of |
| 57 |
* buffermarks in the stack after the new buffermark is created. |
| 58 |
* When destroying buffers, the parameter given will equal the number |
| 59 |
* of the lowest buffermark to be destroyed (i.e dropbuf 4) will leave |
| 60 |
* 3 buffermarks on the stack). |
| 61 |
|
| 62 |
* A special variable buf is now part of a stack element. It points to |
| 63 |
* the last buffer' begin or is NULL for buffer 0. |
| 64 |
* In short: |
| 65 |
* |
| 66 |
* Overview of internal or temporary queues. Even temporary queues may |
| 67 |
* contain buffer marks if the content was moved from an internal queue to |
| 68 |
* a temporary queue. |
| 69 |
* The firstbox is equivalent to firstline and lastbox to lastline. |
| 70 |
* The ends of the double linked list are NULL values. p is short for prev, |
| 71 |
* n for next. b is short for buf. buf is either NULL or points to the element |
| 72 |
* which is the next buffer mark. |
| 73 |
* A buffer mark is a StackLine (SL) with a contents-field of NULL. |
| 74 |
* |
| 75 |
* -->---------->---------->---------->---------->-+ |
| 76 |
* ^ ^ ^ ^ ^ | NULL NULL |
| 77 |
* b b b b b / b b |
| 78 |
* | | | | | / | | |
| 79 |
* p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n <-> p-SL-n |
| 80 |
* || || || |
| 81 |
* firstbox Buffer lastbox |
| 82 |
* Mark |
| 83 |
* |
| 84 |
* READER'S END FIFO'S END |
| 85 |
* LIFO'S END |
| 86 |
* |
| 87 |
|
| 88 |
* Possible extentions: |
| 89 |
* o A new 'hard-buffer', that is not removed when a line is popped |
| 90 |
* from it while it is empty |
| 91 |
*/ |
| 92 |
|
| 93 |
#include "rexx.h" |
| 94 |
#include <stdio.h> |
| 95 |
#include <string.h> |
| 96 |
#include <assert.h> |
| 97 |
|
| 98 |
#ifdef HAVE_UNISTD_H |
| 99 |
#include <unistd.h> |
| 100 |
#endif |
| 101 |
|
| 102 |
#ifdef HAVE_PROCESS_H |
| 103 |
# include <process.h> |
| 104 |
#endif |
| 105 |
|
| 106 |
#include "extstack.h" |
| 107 |
|
| 108 |
#define NUMBER_QUEUES 100 |
| 109 |
/* |
| 110 |
* Queue 0 is SESSION and cannot be added/deleted |
| 111 |
*/ |
| 112 |
|
| 113 |
#define ENSURE_BUFFER(iq) { \ |
| 114 |
/* Ensures that at least one buffer exists for an internal queue. \ |
| 115 |
*/ \ |
| 116 |
if ( (iq)->u.i.top == NULL ) \ |
| 117 |
{ \ |
| 118 |
(iq)->u.i.top = (iq)->u.i.bottom = (Buffer *)MallocTSD( sizeof( Buffer ) ) ; \ |
| 119 |
memset( (iq)->u.i.top, 0, sizeof( Buffer ) ) ; \ |
| 120 |
assert( (iq)->u.i.elements == 0 ) ; \ |
| 121 |
assert( (iq)->u.i.buffers == 0 ) ; \ |
| 122 |
(iq)->u.i.elements = 0 ; \ |
| 123 |
(iq)->u.i.buffers = 1 ; \ |
| 124 |
} \ |
| 125 |
} |
| 126 |
|
| 127 |
typedef struct { /* stk_tsd: static variables of this module (thread-safe) */ |
| 128 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 129 |
int initialized ; |
| 130 |
#endif |
| 131 |
/* |
| 132 |
* The pointer of the current queue points to one of the elements of queue. |
| 133 |
*/ |
| 134 |
Queue *current_queue; |
| 135 |
Queue queue[ NUMBER_QUEUES ] ; |
| 136 |
/* |
| 137 |
* The current queue's name is buffered for a faster access. |
| 138 |
*/ |
| 139 |
streng *current_queue_name; |
| 140 |
/* |
| 141 |
* runner is needed to create fresh ids. |
| 142 |
*/ |
| 143 |
int runner ; |
| 144 |
} stk_tsd_t; /* thread-specific but only needed by this module. see |
| 145 |
* init_stacks |
| 146 |
*/ |
| 147 |
|
| 148 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 149 |
static int get_socket_details_and_connect( tsd_t *TSD, Queue *target ); |
| 150 |
static int save_parse_queue( tsd_t *TSD, streng *queue, Queue *q, |
| 151 |
int ignore_name); |
| 152 |
static Queue *open_external( tsd_t *TSD, const streng *queue, Queue *q, |
| 153 |
int *rc, int ignore_name, streng **basename ); |
| 154 |
#endif |
| 155 |
|
| 156 |
/* init_stacks initializes the module. |
| 157 |
* Currently, we set up the thread specific data. |
| 158 |
* The function returns 1 on success, 0 if memory is short. |
| 159 |
*/ |
| 160 |
int init_stacks( tsd_t *TSD ) |
| 161 |
{ |
| 162 |
stk_tsd_t *st; |
| 163 |
|
| 164 |
if (TSD->stk_tsd != NULL) |
| 165 |
return(1); |
| 166 |
|
| 167 |
if ( ( TSD->stk_tsd = MallocTSD( sizeof(stk_tsd_t) ) ) == NULL ) |
| 168 |
return(0); |
| 169 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 170 |
memset( st, 0, sizeof(stk_tsd_t) ); /* correct for all values */ |
| 171 |
/* |
| 172 |
* Create the default internal SESSION queue on demand. purge_stacks() |
| 173 |
* deletes the queuename very often. |
| 174 |
*/ |
| 175 |
st->queue[0].type = QisSESSION ; |
| 176 |
st->queue[0].u.i.isReal = 1 ; |
| 177 |
st->current_queue = &st->queue[0] ; |
| 178 |
return(1); |
| 179 |
} |
| 180 |
|
| 181 |
/* delete_buffer_content deletes the content of a buffer and resets the |
| 182 |
* buffer to an empty buffer. |
| 183 |
*/ |
| 184 |
static void delete_buffer_content( const tsd_t *TSD, stk_tsd_t *st, Buffer *b ) |
| 185 |
{ |
| 186 |
StackLine *p, *h ; |
| 187 |
|
| 188 |
/* prepare the deletion and become reentrant in case of exceptions */ |
| 189 |
p = b->top ; |
| 190 |
b->top = b->bottom = NULL ; |
| 191 |
b->elements = 0 ; |
| 192 |
|
| 193 |
while ( p != NULL ) { |
| 194 |
h = p ; |
| 195 |
p = p->lower ; |
| 196 |
if ( h->contents != NULL ) |
| 197 |
Free_stringTSD( h->contents ) ; |
| 198 |
FreeTSD( h ) ; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/* delete_an_internal_queue deletes the content of an internal queue (including |
| 203 |
* SESSION) resets the queue to "not used" or "empty" in case of "SESSION". |
| 204 |
*/ |
| 205 |
static void delete_an_internal_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q ) |
| 206 |
{ |
| 207 |
Buffer *p, *h ; |
| 208 |
streng *n ; |
| 209 |
int type = q->type ; |
| 210 |
|
| 211 |
if ( type == QisUnused ) |
| 212 |
return ; |
| 213 |
assert( ( type == QisInternal ) || ( type == QisSESSION ) ) ; |
| 214 |
|
| 215 |
/* prepare the deletion and become reentrant in case of exceptions */ |
| 216 |
p = q->u.i.top ; |
| 217 |
n = q->u.i.name ; |
| 218 |
memset( q, 0, sizeof( Queue ) ) ; |
| 219 |
q->type = ( type == QisSESSION ) ? QisSESSION : QisUnused ; |
| 220 |
|
| 221 |
/* delete the name and all content buffers */ |
| 222 |
if ( n != NULL ) |
| 223 |
Free_stringTSD( n ) ; |
| 224 |
while ( p != NULL ) { |
| 225 |
h = p ; |
| 226 |
p = p->lower ; |
| 227 |
delete_buffer_content( TSD, st, h ) ; |
| 228 |
FreeTSD( h ) ; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/* delete_a_temp_queue cleans up one of the internal stacks by freeing its |
| 233 |
* content. The queue is reset to "not used". |
| 234 |
*/ |
| 235 |
static void delete_a_temp_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q ) |
| 236 |
{ |
| 237 |
Buffer b ; |
| 238 |
|
| 239 |
if ( q->type == QisUnused ) /* happens very often */ |
| 240 |
return ; |
| 241 |
assert( q->type == QisTemp ) ; |
| 242 |
|
| 243 |
/* prepare the deletion and become reentrant in case of exceptions */ |
| 244 |
b = q->u.t ; |
| 245 |
memset( q, 0, sizeof( Queue ) ) ; |
| 246 |
q->type = QisUnused ; |
| 247 |
|
| 248 |
delete_buffer_content( TSD, st, &b ) ; |
| 249 |
} |
| 250 |
|
| 251 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 252 |
/* delete_an_external_queue cleans up one of the external stacks by |
| 253 |
* disconnecting from the server. |
| 254 |
*/ |
| 255 |
static void delete_an_external_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q ) |
| 256 |
{ |
| 257 |
Queue b ; |
| 258 |
|
| 259 |
if ( q->type == QisUnused ) |
| 260 |
return ; |
| 261 |
assert( q->type == QisExternal ) ; |
| 262 |
|
| 263 |
/* prepare the deletion and become reentrant in case of exceptions */ |
| 264 |
b = *q ; |
| 265 |
memset( q, 0, sizeof( Queue ) ) ; |
| 266 |
q->type = QisUnused ; |
| 267 |
|
| 268 |
disconnect_from_rxstack( TSD, &b ) ; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
/* delete_a_queue cleans up any queue by purging or disconnecting. */ |
| 273 |
static void delete_a_queue( const tsd_t *TSD, stk_tsd_t *st, Queue *q ) |
| 274 |
{ |
| 275 |
switch ( q->type ) |
| 276 |
{ |
| 277 |
case QisTemp: |
| 278 |
delete_a_temp_queue( TSD, st, q ) ; |
| 279 |
break; |
| 280 |
|
| 281 |
case QisInternal: |
| 282 |
case QisSESSION: |
| 283 |
delete_an_internal_queue( TSD, st, q ) ; |
| 284 |
break; |
| 285 |
|
| 286 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 287 |
case QisExternal: |
| 288 |
delete_an_external_queue( TSD, st, q ) ; |
| 289 |
break; |
| 290 |
#endif |
| 291 |
|
| 292 |
default: |
| 293 |
assert( q->type == QisUnused ) ; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
* This routine clears all the internal queues we have made |
| 299 |
* and if we have a connection to rxstack, disconnects from it |
| 300 |
*/ |
| 301 |
void purge_stacks( const tsd_t *TSD ) |
| 302 |
{ |
| 303 |
stk_tsd_t *st; |
| 304 |
int i; |
| 305 |
|
| 306 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 307 |
for ( i = 0; i < NUMBER_QUEUES; i++ ) |
| 308 |
delete_a_queue( TSD, st, &st->queue[i] ); |
| 309 |
st->current_queue = &st->queue[0]; |
| 310 |
if ( st->current_queue_name != NULL ) |
| 311 |
Free_stringTSD( st->current_queue_name ); |
| 312 |
st->current_queue_name = NULL; |
| 313 |
} |
| 314 |
|
| 315 |
static void SetSessionName( const tsd_t *TSD, stk_tsd_t *st ) |
| 316 |
{ |
| 317 |
Queue *q = &st->queue[0]; |
| 318 |
|
| 319 |
assert( q->type == QisSESSION ) ; |
| 320 |
if ( q->u.i.name == NULL ) |
| 321 |
{ |
| 322 |
q->u.i.name = Str_creTSD( "SESSION" ) ; |
| 323 |
q->u.i.isReal = 1 ; |
| 324 |
st->current_queue_name = Str_dupTSD( q->u.i.name ); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Find the named internal queue - case insensitive |
| 330 |
*/ |
| 331 |
static Queue *find_queue( const tsd_t *TSD, stk_tsd_t *st, const streng *queue_name ) |
| 332 |
{ |
| 333 |
int i; |
| 334 |
|
| 335 |
if ( st->queue[0].u.i.name == NULL ) |
| 336 |
SetSessionName( TSD, st ) ; |
| 337 |
for ( i = 0; i < NUMBER_QUEUES; i++ ) /* inefficient !! */ |
| 338 |
{ |
| 339 |
if ( ( st->queue[i].type != QisInternal ) |
| 340 |
&& ( st->queue[i].type != QisSESSION ) ) |
| 341 |
continue ; |
| 342 |
|
| 343 |
if ( Str_ccmp( st->queue[i].u.i.name, queue_name ) == 0 ) |
| 344 |
return &st->queue[i] ; |
| 345 |
} |
| 346 |
return NULL ; |
| 347 |
} |
| 348 |
|
| 349 |
/* This function creates a new queue. |
| 350 |
* The returned queue's type must be overwritten. |
| 351 |
*/ |
| 352 |
Queue *find_free_slot( const tsd_t *TSD ) |
| 353 |
{ |
| 354 |
int i; |
| 355 |
stk_tsd_t *st = (stk_tsd_t *)TSD->stk_tsd ; |
| 356 |
|
| 357 |
for ( i = 1; i < NUMBER_QUEUES; i++ ) /* never ever select SESSION */ |
| 358 |
{ |
| 359 |
if ( st->queue[i].type == QisUnused ) |
| 360 |
return &st->queue[i] ; |
| 361 |
} |
| 362 |
if ( !TSD->called_from_saa ) |
| 363 |
exiterror( ERR_STORAGE_EXHAUSTED, 0 ); /* message not very helpful */ |
| 364 |
return NULL ; |
| 365 |
} |
| 366 |
|
| 367 |
/* |
| 368 |
* Glues the content of the temporary stack src to the top buffer of |
| 369 |
* dst which must not be temporary. is_fifo controls the position of |
| 370 |
* the glue. is_fifo should reflect the type of insertion in the |
| 371 |
* temporary buffer. |
| 372 |
* src is destroyed at the end of the operation. |
| 373 |
*/ |
| 374 |
void flush_stack( const tsd_t *TSD, Queue *src, Queue *dst, int is_fifo ) |
| 375 |
{ |
| 376 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 377 |
StackLine *ptr, *h ; |
| 378 |
#endif |
| 379 |
stk_tsd_t *st; |
| 380 |
|
| 381 |
if ( src == NULL ) /* no temporary stack? may happen */ |
| 382 |
return ; |
| 383 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 384 |
/* |
| 385 |
* I think this is never used. Let's verify it. Florian, 06.01.2005 |
| 386 |
*/ |
| 387 |
fprintf( stderr, "Regina internal error detected in %s, line %u.\n" |
| 388 |
"Please, send an email to [email protected].\n", |
| 389 |
__FILE__, __LINE__ ); |
| 390 |
|
| 391 |
assert( src->type == QisTemp ) ; |
| 392 |
assert( dst->type == QisSESSION |
| 393 |
|| dst->type == QisInternal |
| 394 |
|| dst->type == QisExternal ) ; |
| 395 |
if ( src->u.t.top == NULL ) |
| 396 |
{ |
| 397 |
/* nothing in temporary stack to flush */ |
| 398 |
assert( src->u.t.bottom == NULL ) ; |
| 399 |
return ; |
| 400 |
} |
| 401 |
|
| 402 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 403 |
if ( dst->type == QisExternal ) |
| 404 |
{ |
| 405 |
for ( ptr = src->u.t.top; ptr != NULL; ) |
| 406 |
{ |
| 407 |
if (is_fifo) |
| 408 |
{ |
| 409 |
queue_line_fifo_to_rxstack( TSD, dst->u.e.socket, ptr->contents ) ; |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
queue_line_lifo_to_rxstack( TSD, dst->u.e.socket, ptr->contents ) ; |
| 414 |
} |
| 415 |
|
| 416 |
h = ptr ; |
| 417 |
ptr = ptr->lower ; |
| 418 |
Free_stringTSD( h->contents ) ; |
| 419 |
FreeTSD( h ) ; |
| 420 |
} |
| 421 |
src->u.t.top = src->u.t.bottom = NULL ; /* allow safe cleanup */ |
| 422 |
delete_a_temp_queue( TSD, st, src ) ; |
| 423 |
return ; |
| 424 |
} |
| 425 |
#endif |
| 426 |
|
| 427 |
/* dst->type is QisSESSION or QisInternal, stack it either fifo or lifo */ |
| 428 |
ENSURE_BUFFER( dst ) ; |
| 429 |
|
| 430 |
dst->u.i.elements += src->u.t.elements ; |
| 431 |
if ( is_fifo ) |
| 432 |
{ |
| 433 |
GLUE_BUFFER2( &src->u.t, dst->u.i.top ); /* begin of newest buffer */ |
| 434 |
} |
| 435 |
else |
| 436 |
{ |
| 437 |
GLUE_BUFFER1( dst->u.i.top, &src->u.t ); |
| 438 |
} |
| 439 |
delete_a_temp_queue( TSD, st, src ) ; |
| 440 |
} |
| 441 |
|
| 442 |
/* |
| 443 |
* stack_to_line converts the content of the temporary stack to one blank |
| 444 |
* delimited line. |
| 445 |
* An empty string is returned if nothing is in the queue. |
| 446 |
*/ |
| 447 |
streng *stack_to_line( const tsd_t *TSD, Queue *q ) |
| 448 |
{ |
| 449 |
StackLine *ptr, *h ; |
| 450 |
unsigned size = 0; |
| 451 |
char *dest; |
| 452 |
stk_tsd_t *st; |
| 453 |
streng *retval; |
| 454 |
|
| 455 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 456 |
|
| 457 |
if (q->type == QisUnused ) |
| 458 |
return(nullstringptr()); |
| 459 |
|
| 460 |
assert( q->type == QisTemp ) ; |
| 461 |
|
| 462 |
if ( q->u.t.top == NULL ) |
| 463 |
{ |
| 464 |
/* nothing in temporary stack to flush */ |
| 465 |
assert( q->u.t.bottom == NULL ) ; |
| 466 |
delete_a_temp_queue( TSD, st, q ) ; |
| 467 |
return(nullstringptr()); |
| 468 |
} |
| 469 |
|
| 470 |
/* first, count the needed string length. */ |
| 471 |
for ( ptr = q->u.t.top; ptr != NULL; ptr=ptr->lower ) |
| 472 |
{ |
| 473 |
size += Str_len(ptr->contents) + 1 ; /* blank is delimiter */ |
| 474 |
} |
| 475 |
|
| 476 |
retval = Str_makeTSD( size ) ; |
| 477 |
dest = retval->value ; |
| 478 |
|
| 479 |
for ( ptr = q->u.t.top; ptr != NULL; ) |
| 480 |
{ |
| 481 |
memcpy( dest, ptr->contents->value, Str_len( ptr->contents ) ) ; |
| 482 |
dest += Str_len( ptr->contents ) ; |
| 483 |
*dest++ = ' '; |
| 484 |
|
| 485 |
h = ptr ; |
| 486 |
ptr = ptr->lower ; |
| 487 |
Free_stringTSD( h->contents ) ; |
| 488 |
FreeTSD( h ) ; |
| 489 |
} |
| 490 |
|
| 491 |
/* strip any blanks at the end of the string. */ |
| 492 |
while ((dest != retval->value) && (dest[-1] == ' ')) |
| 493 |
dest--; |
| 494 |
|
| 495 |
/* finally, terminate the string */ |
| 496 |
*dest = '\0'; /* DON'T increment dest */ |
| 497 |
Str_len( retval ) = (int)( dest - retval->value ) ; |
| 498 |
|
| 499 |
/* reset the pointers, to signal that temporary stack is empty */ |
| 500 |
q->u.t.top = q->u.t.bottom = NULL ; |
| 501 |
delete_a_temp_queue( TSD, st, q ) ; |
| 502 |
return(retval); |
| 503 |
} |
| 504 |
|
| 505 |
/* |
| 506 |
* The input for a command may be overwritten if both input and output or |
| 507 |
* input and error are STEM values. A temporary queue is created in this |
| 508 |
* case to prevent the destruction of an unread value. |
| 509 |
* fill_input_queue_stem fills up a new queue with a copy of a stems |
| 510 |
* content. stemname must end with a period and stem0 is the count of lines |
| 511 |
* in stem. |
| 512 |
* OPTIMIZING HINT: The function can be rewritten to access just only the |
| 513 |
* stem leaves. |
| 514 |
*/ |
| 515 |
Queue *fill_input_queue_stem(tsd_t *TSD, streng *stemname, int stem0) |
| 516 |
{ |
| 517 |
int i,stemlen = Str_len( stemname ) ; |
| 518 |
streng *stem, *newstr ; |
| 519 |
StackLine *line ; |
| 520 |
Queue *q = find_free_slot( TSD ) ; |
| 521 |
|
| 522 |
q->type = QisTemp ; |
| 523 |
|
| 524 |
stem = Str_makeTSD(stemlen + 1 + 3*sizeof(int)); |
| 525 |
memcpy(stem->value, stemname->value, stemlen); |
| 526 |
|
| 527 |
for (i = 1; i <= stem0; i++) |
| 528 |
{ /* Fetch the value: */ |
| 529 |
stem->len = stemlen + sprintf(stem->value + stemlen, "%d", i); |
| 530 |
newstr = Str_dupTSD(get_it_anyway_compound(TSD, stem)); |
| 531 |
|
| 532 |
line = (StackLine *) MallocTSD( sizeof( StackLine ) ) ; |
| 533 |
line->contents = newstr ; |
| 534 |
FIFO_LINE( &q->u.t, line ) ; |
| 535 |
} |
| 536 |
|
| 537 |
Free_stringTSD(stem); |
| 538 |
return q ; |
| 539 |
} |
| 540 |
|
| 541 |
/* |
| 542 |
* The input for a command may be overwritten if both input and output or |
| 543 |
* input and error are STREAM values. A temporary queue is created in this |
| 544 |
* case to prevent the destruction of an unread value. |
| 545 |
* fill_input_queue_stream fills up a new queue with a copy of a streams |
| 546 |
* content. fileptr must be an opened file stream pointer gotten by |
| 547 |
* addr_reopen_file() for an input stream. |
| 548 |
*/ |
| 549 |
Queue *fill_input_queue_stream( tsd_t *TSD, void *fileptr ) |
| 550 |
{ |
| 551 |
StackLine *line; |
| 552 |
streng *c; |
| 553 |
Queue *q = find_free_slot( TSD ); |
| 554 |
|
| 555 |
q->type = QisTemp; |
| 556 |
|
| 557 |
for ( ; ; ) |
| 558 |
{ /* Fetch the value: */ |
| 559 |
c = addr_io_file( TSD, fileptr, NULL ); |
| 560 |
if ( c == NULL ) |
| 561 |
break; |
| 562 |
if ( c->len == 0 ) |
| 563 |
{ |
| 564 |
Free_stringTSD( c ); |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
line = (StackLine *) MallocTSD( sizeof( StackLine ) ); |
| 569 |
line->contents = c; |
| 570 |
FIFO_LINE( &q->u.t, line ); |
| 571 |
} |
| 572 |
|
| 573 |
return q; |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
* use_external checks whether or not an external queue must be processed. |
| 578 |
* The different reasons why not to do it are checked. |
| 579 |
* use_external returns either 0, if queue_name is a local queue name or |
| 580 |
* the current queue is a local queue, or it returns 1 for an external queue |
| 581 |
* name or if the current queue is external. |
| 582 |
*/ |
| 583 |
static int use_external( const tsd_t *TSD, const streng *queue_name ) |
| 584 |
{ |
| 585 |
stk_tsd_t *st = (stk_tsd_t *)TSD->stk_tsd; |
| 586 |
|
| 587 |
/* A little bit off topic, but purge_stacks() may kill "SESSION" at the |
| 588 |
* end of RexxStart() and we need a working "SESSION" always. Not deleting |
| 589 |
* it will produce "errors" with the tracemem feature. |
| 590 |
* This function is called at top of each relevant function. |
| 591 |
*/ |
| 592 |
if ( st->queue[0].u.i.name == NULL ) |
| 593 |
SetSessionName( TSD, st ); |
| 594 |
|
| 595 |
#if defined(NO_EXTERNAL_QUEUES) |
| 596 |
(queue_name); |
| 597 |
return 0; /* trivial */ |
| 598 |
#else |
| 599 |
|
| 600 |
if ( !st->initialized ) |
| 601 |
{ |
| 602 |
/* |
| 603 |
* Attention, set the current queue type to default. This may be |
| 604 |
* either internal or external. |
| 605 |
*/ |
| 606 |
st->initialized = 1; |
| 607 |
init_external_queue( TSD ); |
| 608 |
} |
| 609 |
|
| 610 |
if ( get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) ) |
| 611 |
return 0; /* user forces a local queue in every case */ |
| 612 |
if ( ( queue_name == NULL ) || ( PSTRENGLEN( queue_name ) == 0 ) ) |
| 613 |
return st->current_queue->type == QisExternal; |
| 614 |
|
| 615 |
if ( get_options_flag( TSD->currlevel, EXT_QUEUES_301 ) == 0 ) |
| 616 |
{ |
| 617 |
/* |
| 618 |
* A name exists, check it. |
| 619 |
*/ |
| 620 |
if ( memchr( queue_name->value, '@', Str_len( queue_name ) ) == NULL ) |
| 621 |
return 0; |
| 622 |
} |
| 623 |
|
| 624 |
return 1; |
| 625 |
#endif |
| 626 |
} |
| 627 |
|
| 628 |
/* |
| 629 |
* Returns 1 if we have started using external queues, 0 if we haven't |
| 630 |
* started using external queues |
| 631 |
*/ |
| 632 |
int external_queues_used( const tsd_t *TSD ) |
| 633 |
{ |
| 634 |
return use_external( TSD, NULL ) ; |
| 635 |
} |
| 636 |
|
| 637 |
/* |
| 638 |
* Pushes 'line' onto the REXX stack, lifo, and sets 'lastline' to |
| 639 |
* point to the new line. The line is put on top of the current |
| 640 |
* buffer. |
| 641 |
* |
| 642 |
* A queue_name is supplied by external reference, only. See IfcAddQueue. |
| 643 |
* Reuses the current connection if possible. |
| 644 |
*/ |
| 645 |
int stack_lifo( tsd_t *TSD, streng *line, const streng *queue_name ) |
| 646 |
{ |
| 647 |
StackLine *newbox ; |
| 648 |
Buffer *b ; |
| 649 |
stk_tsd_t *st ; |
| 650 |
Queue *q ; |
| 651 |
int rc=0; |
| 652 |
|
| 653 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 654 |
|
| 655 |
assert( line != NULL ) ; |
| 656 |
|
| 657 |
if ( !use_external( TSD, queue_name ) ) |
| 658 |
{ |
| 659 |
if ( queue_name ) |
| 660 |
{ |
| 661 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 662 |
{ |
| 663 |
return 9; |
| 664 |
} |
| 665 |
} |
| 666 |
else |
| 667 |
q = st->current_queue; |
| 668 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 669 |
|
| 670 |
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ; |
| 671 |
newbox->contents = line ; |
| 672 |
ENSURE_BUFFER( q ) ; |
| 673 |
b = q->u.i.top ; |
| 674 |
LIFO_LINE( b, newbox ) ; |
| 675 |
q->u.i.elements++; |
| 676 |
} |
| 677 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 678 |
else |
| 679 |
{ |
| 680 |
Queue q1,*work; |
| 681 |
|
| 682 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 0, NULL ) ) == NULL ) |
| 683 |
return rc; |
| 684 |
|
| 685 |
if ( ( rc = queue_line_lifo_to_rxstack( TSD, work->u.e.socket, line ) ) == -1 ) |
| 686 |
rc = 100; |
| 687 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 688 |
} |
| 689 |
#endif |
| 690 |
return rc; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
/* |
| 695 |
* Puts 'line' on the REXX stack, fifo. This routine is similar to |
| 696 |
* stack_lifo but the differences are big enough to have a separate |
| 697 |
* routine. The line is put in the bottom of the current buffer, |
| 698 |
* that is just above the uppermost buffer mark, or at the bottom |
| 699 |
* of the stack, if there are no buffer marks. |
| 700 |
* |
| 701 |
* A queue_name is supplied by external reference, only. See IfcAddQueue. |
| 702 |
* Reuses the current connection if possible. |
| 703 |
*/ |
| 704 |
int stack_fifo( tsd_t *TSD, streng *line, const streng *queue_name ) |
| 705 |
{ |
| 706 |
StackLine *newbox ; |
| 707 |
stk_tsd_t *st; |
| 708 |
Queue *q; |
| 709 |
Buffer *b ; |
| 710 |
int rc=0; |
| 711 |
|
| 712 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 713 |
|
| 714 |
assert( line != NULL ) ; |
| 715 |
|
| 716 |
if ( !use_external( TSD, queue_name ) ) |
| 717 |
{ |
| 718 |
if ( queue_name ) |
| 719 |
{ |
| 720 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 721 |
{ |
| 722 |
return 9; |
| 723 |
} |
| 724 |
} |
| 725 |
else |
| 726 |
q = st->current_queue; |
| 727 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 728 |
|
| 729 |
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ; |
| 730 |
newbox->contents = line ; |
| 731 |
ENSURE_BUFFER( q ) ; |
| 732 |
b = q->u.i.top ; |
| 733 |
FIFO_LINE( b, newbox ) ; |
| 734 |
q->u.i.elements++; |
| 735 |
} |
| 736 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 737 |
else |
| 738 |
{ |
| 739 |
Queue q1, *work; |
| 740 |
|
| 741 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 0, NULL ) ) == NULL ) |
| 742 |
return rc; |
| 743 |
|
| 744 |
if ( ( rc = queue_line_fifo_to_rxstack( TSD, work->u.e.socket, line ) ) == -1 ) |
| 745 |
rc = 100; |
| 746 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 747 |
} |
| 748 |
#endif |
| 749 |
return rc; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
/* |
| 754 |
* Removes one (or several) buffers from the stack. Number must be a |
| 755 |
* non-negative number giving the number of first buffer from the bottom |
| 756 |
* which has to be removed. You can pass a number of buffers to destroy |
| 757 |
* from top of the stack if you supply the negative number of buffers. |
| 758 |
* |
| 759 |
* Example: Queue has buffers 0, 1, 2, 3, 4 and the number is 3. Then |
| 760 |
* the buffers 3 and 4 are removed and 2 becomes the current buffer. |
| 761 |
* If the number is -2 then the top two buffers are destroyed and |
| 762 |
* 2 becomes the current buffer. |
| 763 |
* |
| 764 |
* Return values: -2: specified buffer doesn't exist |
| 765 |
* -1: external queues don't support buffers |
| 766 |
* others: remaining buffers after success |
| 767 |
*/ |
| 768 |
|
| 769 |
int drop_buffer( const tsd_t *TSD, int number ) |
| 770 |
{ |
| 771 |
Buffer *b, *h ; |
| 772 |
stk_tsd_t *st; |
| 773 |
Queue *curr ; |
| 774 |
|
| 775 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 776 |
curr = st->current_queue ; |
| 777 |
if ( curr->type == QisExternal ) |
| 778 |
exiterror( ERR_EXTERNAL_QUEUE, 110, "DROPBUF" ) ; |
| 779 |
assert( ( curr->type == QisInternal ) || ( curr->type == QisSESSION ) ) ; |
| 780 |
|
| 781 |
ENSURE_BUFFER( curr ) ; /* makes things easier */ |
| 782 |
if ( number < 0 ) |
| 783 |
number = curr->u.i.buffers + number ; |
| 784 |
|
| 785 |
if ( number >= (int) curr->u.i.buffers ) |
| 786 |
return -2 ; |
| 787 |
|
| 788 |
for ( b = curr->u.i.bottom; ( number > 0 ) && ( b != NULL ); number-- ) |
| 789 |
b = b->higher ; |
| 790 |
|
| 791 |
assert( b ) ; /* must exist */ |
| 792 |
/* disconnect the buffer sequence marked for deletion */ |
| 793 |
if ( b == curr->u.i.bottom ) |
| 794 |
{ |
| 795 |
/* preserve en empty buffer */ |
| 796 |
curr->u.i.elements -= b->elements ; |
| 797 |
delete_buffer_content( TSD, st, b ) ; |
| 798 |
b = b->higher ; |
| 799 |
curr->u.i.top = curr->u.i.bottom ; |
| 800 |
curr->u.i.top->higher = NULL ; |
| 801 |
} |
| 802 |
else |
| 803 |
{ |
| 804 |
curr->u.i.top = b->lower ; |
| 805 |
curr->u.i.top->higher = NULL ; |
| 806 |
} |
| 807 |
|
| 808 |
/* delete each buffer and its content */ |
| 809 |
while ( b != NULL ) { |
| 810 |
curr->u.i.elements -= b->elements ; |
| 811 |
delete_buffer_content( TSD, st, b ) ; |
| 812 |
|
| 813 |
h = b ; |
| 814 |
b = b->higher ; |
| 815 |
FreeTSD( h ) ; |
| 816 |
curr->u.i.buffers-- ; |
| 817 |
} |
| 818 |
|
| 819 |
return curr->u.i.buffers - 1 ; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
/* |
| 824 |
* Fetches a line from the top of the stack. If the current buffer |
| 825 |
* does not contain any lines, it is removed and the second current |
| 826 |
* buffer is search for a line etc. If there isn't any lines in the |
| 827 |
* stack, a line is read from the standard input. |
| 828 |
* Reuses the current connection if possible. |
| 829 |
*/ |
| 830 |
streng *popline( tsd_t *TSD, const streng *queue_name, int *result, unsigned long waitflag ) |
| 831 |
{ |
| 832 |
streng *contents=NULL ; |
| 833 |
StackLine *line ; |
| 834 |
stk_tsd_t *st ; |
| 835 |
int need_line_from_stdin=0; |
| 836 |
int rc=0; |
| 837 |
Queue *q ; |
| 838 |
Buffer *b ; |
| 839 |
|
| 840 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 841 |
|
| 842 |
if ( !use_external( TSD, queue_name ) ) |
| 843 |
{ |
| 844 |
if ( queue_name ) |
| 845 |
{ |
| 846 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 847 |
{ |
| 848 |
if ( result ) |
| 849 |
*result = 9; |
| 850 |
if ( !TSD->called_from_saa ) |
| 851 |
contents = nullstringptr(); |
| 852 |
return contents; |
| 853 |
} |
| 854 |
} |
| 855 |
else |
| 856 |
q = st->current_queue; |
| 857 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 858 |
|
| 859 |
line = NULL ; |
| 860 |
while ( ( b = q->u.i.top ) != NULL ) |
| 861 |
{ |
| 862 |
POP_LINE( b, line ) ; |
| 863 |
if ( line != NULL ) |
| 864 |
{ |
| 865 |
q->u.i.elements-- ; |
| 866 |
break ; |
| 867 |
} |
| 868 |
|
| 869 |
/* buffer is empty, fetch the next one and drop the empty one |
| 870 |
*/ |
| 871 |
q->u.i.top = b->lower ; |
| 872 |
if ( q->u.i.top ) |
| 873 |
q->u.i.top->higher = NULL ; /* fixes bug 1068204 */ |
| 874 |
q->u.i.buffers-- ; |
| 875 |
FreeTSD( b ) ; |
| 876 |
} |
| 877 |
if ( line != NULL ) |
| 878 |
{ |
| 879 |
contents = line->contents ; |
| 880 |
FreeTSD( line ) ; |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
/* q->u.i.top == NULL, too; but bottom's not set */ |
| 885 |
q->u.i.bottom = NULL ; |
| 886 |
assert( q->u.i.elements == 0 ) ; |
| 887 |
assert( q->u.i.buffers == 0 ) ; |
| 888 |
need_line_from_stdin = 1; |
| 889 |
} |
| 890 |
} |
| 891 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 892 |
else |
| 893 |
{ |
| 894 |
Queue q1, *work; |
| 895 |
|
| 896 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 0, NULL ) ) == NULL ) |
| 897 |
{ |
| 898 |
if ( result ) |
| 899 |
*result = rc; |
| 900 |
return NULL; |
| 901 |
} |
| 902 |
/* |
| 903 |
* waitflag is 0 if called from a Rexx program as [PARSE] PULL, but can be |
| 904 |
* 1 when called from the Rexx API. We override the waitflag when the current |
| 905 |
* queue is external and a timeout has been set for the queue |
| 906 |
*/ |
| 907 |
if ( work->u.e.timeoutSet ) |
| 908 |
waitflag = 1; |
| 909 |
rc = get_line_from_rxstack( TSD, work->u.e.socket, &contents, ( waitflag == 0 ) ); |
| 910 |
switch ( rc ) |
| 911 |
{ |
| 912 |
case -1: rc = 100; break; |
| 913 |
case RXSTACK_ERROR: rc = 9; break; /* map generic error to RXQUEUE_NOTREG */ |
| 914 |
default: ; |
| 915 |
} |
| 916 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 917 |
if ( rc == RXSTACK_TIMEOUT ) |
| 918 |
{ |
| 919 |
condition_hook( TSD, SIGNAL_NOTREADY, 94, 1, -1, NULL, Str_cre_TSD( TSD, "Timeout on external queue" ) ); |
| 920 |
} |
| 921 |
if ( ( rc == RXSTACK_EMPTY ) || ( rc == RXSTACK_TIMEOUT ) ) /* empty or timeout */ |
| 922 |
need_line_from_stdin = 1; |
| 923 |
} |
| 924 |
#endif |
| 925 |
|
| 926 |
if ( need_line_from_stdin ) |
| 927 |
{ |
| 928 |
if ( TSD->called_from_saa ) |
| 929 |
{ |
| 930 |
rc = 8; /* RXQUEUE_EMPTY */ |
| 931 |
} |
| 932 |
else if ( rc == RXSTACK_TIMEOUT ) |
| 933 |
{ |
| 934 |
rc = 8; /* RXQUEUE_EMPTY */ |
| 935 |
} |
| 936 |
else |
| 937 |
{ |
| 938 |
int rc2 = HOOK_GO_ON ; |
| 939 |
if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_PULL ) ) |
| 940 |
rc2 = hookup_input( TSD, HOOK_PULL, &contents ); |
| 941 |
|
| 942 |
if ( rc2 == HOOK_GO_ON ) |
| 943 |
contents = readkbdline( TSD ); |
| 944 |
|
| 945 |
assert( contents ); |
| 946 |
} |
| 947 |
rc = 0; |
| 948 |
} |
| 949 |
if ( result ) |
| 950 |
*result = rc; |
| 951 |
else if ( ( contents == NULL ) && !TSD->called_from_saa ) |
| 952 |
contents = nullstringptr(); |
| 953 |
|
| 954 |
return contents; |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
/* |
| 959 |
* Counts the lines in the stack. |
| 960 |
* Reuses the current connection if possible. |
| 961 |
*/ |
| 962 |
int lines_in_stack( tsd_t *TSD, const streng *queue_name ) |
| 963 |
{ |
| 964 |
stk_tsd_t *st ; |
| 965 |
int lines ; |
| 966 |
Queue *q ; |
| 967 |
|
| 968 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 969 |
if ( !use_external( TSD, queue_name ) ) |
| 970 |
{ |
| 971 |
if ( queue_name ) |
| 972 |
{ |
| 973 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 974 |
{ |
| 975 |
return -9; |
| 976 |
} |
| 977 |
} |
| 978 |
else |
| 979 |
q = st->current_queue; |
| 980 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 981 |
|
| 982 |
lines = q->u.i.elements ; |
| 983 |
} |
| 984 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 985 |
else |
| 986 |
{ |
| 987 |
Queue q1, *work; |
| 988 |
int rc; |
| 989 |
|
| 990 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 0, NULL ) ) == NULL ) |
| 991 |
return -rc; |
| 992 |
|
| 993 |
lines = get_number_in_queue_from_rxstack( TSD, work->u.e.socket, &rc ); |
| 994 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 995 |
if ( rc != 0 ) |
| 996 |
lines = -rc; |
| 997 |
} |
| 998 |
#endif |
| 999 |
return lines ; |
| 1000 |
} |
| 1001 |
|
| 1002 |
#ifdef TRACEMEM |
| 1003 |
/* |
| 1004 |
* Marks all chunks of dynamic allocated memory that are allocated |
| 1005 |
* to the stack subsystem. |
| 1006 |
*/ |
| 1007 |
static void mark_buffer( const Buffer *b ) |
| 1008 |
{ |
| 1009 |
StackLine *ptr ; |
| 1010 |
|
| 1011 |
for ( ptr = b->top; ptr != NULL; ptr = ptr->lower ) |
| 1012 |
{ |
| 1013 |
markmemory( ptr, TRC_STACKBOX ) ; |
| 1014 |
markmemory( ptr->contents, TRC_STACKLINE ) ; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
void mark_stack( const tsd_t *TSD ) |
| 1019 |
{ |
| 1020 |
Buffer *b ; |
| 1021 |
Queue *q ; |
| 1022 |
stk_tsd_t *st; |
| 1023 |
int i; |
| 1024 |
|
| 1025 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1026 |
q = st->current_queue ; |
| 1027 |
for ( i = 0; i < NUMBER_QUEUES; i++ ) |
| 1028 |
{ |
| 1029 |
switch ( st->queue[i].type ) |
| 1030 |
{ |
| 1031 |
default: |
| 1032 |
assert( st->queue[i].type ) ; |
| 1033 |
case QisUnused: |
| 1034 |
break; |
| 1035 |
|
| 1036 |
case QisSESSION: |
| 1037 |
case QisInternal: |
| 1038 |
if ( st->queue[i].u.i.name ) |
| 1039 |
{ |
| 1040 |
markmemory( st->queue[i].u.i.name, TRC_STACKBOX ) ; |
| 1041 |
} |
| 1042 |
|
| 1043 |
for ( b = st->queue[i].u.i.top; b != NULL; b = b->lower ) |
| 1044 |
{ |
| 1045 |
markmemory( b, TRC_STACKBOX ) ; |
| 1046 |
mark_buffer( b ) ; |
| 1047 |
} |
| 1048 |
break ; |
| 1049 |
|
| 1050 |
case QisTemp: |
| 1051 |
mark_buffer( &st->queue[i].u.t ) ; |
| 1052 |
break ; |
| 1053 |
|
| 1054 |
case QisExternal: |
| 1055 |
markmemory( st->queue[i].u.e.name, TRC_STACKBOX ) ; |
| 1056 |
break ; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
#endif |
| 1061 |
|
| 1062 |
|
| 1063 |
/* |
| 1064 |
* Creates a new buffer and returns the number of buffers in the stack |
| 1065 |
* excluding the zeroth buffer. |
| 1066 |
*/ |
| 1067 |
int make_buffer( tsd_t *TSD ) |
| 1068 |
{ |
| 1069 |
stk_tsd_t *st; |
| 1070 |
Buffer *b ; |
| 1071 |
Queue *q ; |
| 1072 |
|
| 1073 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1074 |
q = st->current_queue ; |
| 1075 |
if ( q->type == QisExternal ) |
| 1076 |
exiterror( ERR_EXTERNAL_QUEUE, 110, "MAKEBUF" ) ; |
| 1077 |
assert( ( q->type == QisSESSION ) |
| 1078 |
|| ( q->type == QisInternal ) ) ; |
| 1079 |
|
| 1080 |
ENSURE_BUFFER( q ) ; /* Possibly create the zeroth buffer */ |
| 1081 |
|
| 1082 |
/* Make a *new* buffer */ |
| 1083 |
b = (Buffer *)MallocTSD( sizeof( Buffer ) ) ; |
| 1084 |
memset( b, 0, sizeof( Buffer ) ) ; |
| 1085 |
|
| 1086 |
b->lower = q->u.i.top ; /* The zeroth buffer exists */ |
| 1087 |
b->lower->higher = b ; |
| 1088 |
q->u.i.top = b ; |
| 1089 |
q->u.i.buffers++ ; |
| 1090 |
|
| 1091 |
return q->u.i.buffers - 1 ; |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
/* |
| 1096 |
* Dumps the contents of the stack to standard error. Buffer marks are |
| 1097 |
* indicated in the printout. |
| 1098 |
*/ |
| 1099 |
void type_buffer( tsd_t *TSD ) |
| 1100 |
{ |
| 1101 |
Buffer *b ; |
| 1102 |
StackLine *ptr ; |
| 1103 |
char *cptr, *stop ; |
| 1104 |
int counter ; |
| 1105 |
streng *name ; |
| 1106 |
stk_tsd_t *st; |
| 1107 |
Queue *q ; |
| 1108 |
|
| 1109 |
if (TSD->stddump == NULL) |
| 1110 |
return; |
| 1111 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1112 |
q = st->current_queue ; |
| 1113 |
name = get_queue( TSD ) ; |
| 1114 |
fprintf(TSD->stddump,"==> Name: %.*s\n", Str_len(name), name->value ) ; |
| 1115 |
fprintf(TSD->stddump,"==> Lines: %d\n", lines_in_stack( TSD, NULL )) ; |
| 1116 |
if ( q->type == QisExternal ) |
| 1117 |
return ; |
| 1118 |
|
| 1119 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 1120 |
ENSURE_BUFFER( q ) ; /* Possibly create the zeroth buffer */ |
| 1121 |
for ( counter = q->u.i.buffers, b = q->u.i.top; b != NULL; b = b->lower ) |
| 1122 |
{ |
| 1123 |
fprintf(TSD->stddump,"==> Buffer: %d\n", --counter ) ; |
| 1124 |
for ( ptr = b->top; ptr != NULL; ptr = ptr->lower ) |
| 1125 |
{ |
| 1126 |
putc( '"', TSD->stddump ) ; |
| 1127 |
stop = Str_end( ptr->contents ) ; |
| 1128 |
for ( cptr = ptr->contents->value ; cptr < stop; cptr++ ) |
| 1129 |
putc( ( rx_isprint( *cptr ) ? ( *cptr ) : '?' ), TSD->stddump ) ; |
| 1130 |
|
| 1131 |
putc( '"', TSD->stddump ) ; |
| 1132 |
#if defined(DOS) || defined(OS2) || defined(WIN32) |
| 1133 |
putc( REGINA_CR, TSD->stddump ) ; |
| 1134 |
#endif |
| 1135 |
putc( REGINA_EOL, TSD->stddump ) ; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
fprintf(TSD->stddump,"==> End of Stack\n") ; |
| 1140 |
fflush(TSD->stddump) ; |
| 1141 |
} |
| 1142 |
|
| 1143 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1144 |
/* |
| 1145 |
* The following functions allow interfacing to the external |
| 1146 |
* queue process; rxstack |
| 1147 |
* The connection to eq is opened if and only if it isn't opened yet. |
| 1148 |
*/ |
| 1149 |
static int get_socket_details_and_connect( tsd_t *TSD, Queue *q ) |
| 1150 |
{ |
| 1151 |
assert( q->type == QisExternal ) ; |
| 1152 |
|
| 1153 |
/* Fill in default values. Missing values are rarely used but it may |
| 1154 |
* happen. |
| 1155 |
*/ |
| 1156 |
if ( q->u.e.name == NULL ) |
| 1157 |
q->u.e.name = default_external_name( TSD ) ; |
| 1158 |
if ( q->u.e.portno == 0 ) |
| 1159 |
q->u.e.portno = default_port_number() ; |
| 1160 |
if ( q->u.e.address == 0 ) |
| 1161 |
q->u.e.address = default_external_address() ; |
| 1162 |
|
| 1163 |
if ( q->u.e.socket == -1 ) |
| 1164 |
{ |
| 1165 |
if ( connect_to_rxstack( TSD, q ) == -1 ) |
| 1166 |
return 100; /* RXQUEUE_NETERROR */ |
| 1167 |
} |
| 1168 |
q->u.e.timeoutSet = 0; |
| 1169 |
return 0; |
| 1170 |
} |
| 1171 |
|
| 1172 |
/* |
| 1173 |
* save_parse_queue wraps parse_queue and exiterrors in case of an error. |
| 1174 |
* Returns 1 if queue is (probably) different from the current queue, 0 else. |
| 1175 |
* *q is filled even with a return value of 0. |
| 1176 |
* A negative return code indicates a RXQUEUE_??? error code which must be |
| 1177 |
* passed back to the SAA interface immediately. |
| 1178 |
* Queues are treated as equal if ignore_name is set but host address and port |
| 1179 |
* numbers are the same. |
| 1180 |
*/ |
| 1181 |
static int save_parse_queue( tsd_t *TSD, streng *queue, Queue *q, |
| 1182 |
int ignore_name) |
| 1183 |
{ |
| 1184 |
stk_tsd_t *st = (stk_tsd_t *)TSD->stk_tsd; |
| 1185 |
Queue *curr ; |
| 1186 |
int rc; |
| 1187 |
|
| 1188 |
if ( ( rc = parse_queue( TSD, queue, q ) ) <= 0 ) |
| 1189 |
return rc; |
| 1190 |
|
| 1191 |
if ( !ignore_name && ( PSTRENGLEN( queue ) != 0 ) ) |
| 1192 |
return 1 ; |
| 1193 |
curr = st->current_queue ; |
| 1194 |
if ( curr->type != QisExternal ) |
| 1195 |
return 1 ; |
| 1196 |
if ( q->u.e.address != curr->u.e.address ) |
| 1197 |
return 1 ; |
| 1198 |
if ( q->u.e.portno != curr->u.e.portno ) |
| 1199 |
return 1 ; |
| 1200 |
return 0 ; |
| 1201 |
} |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* open_external opens the connection to the external queue will a full |
| 1205 |
* error checking. NULL is returned on error and *rc describes the |
| 1206 |
* RXQUEUE_??? error. |
| 1207 |
* On success the opened queue is returned and *q is set to the old or |
| 1208 |
* temporary queue. queue is either NULL or describes the new temporary |
| 1209 |
* queue's name. |
| 1210 |
* Queues are treated as equal if ignore_name is set but host address and port |
| 1211 |
* numbers are the same. No set_queue_in_rxstack() is called if ignore_name |
| 1212 |
* is set. |
| 1213 |
* *basename is set to the queue's name without the network extension if |
| 1214 |
* basename != NULL. It must be freed later. *basename may become NULL. |
| 1215 |
*/ |
| 1216 |
static Queue *open_external( tsd_t *TSD, const streng *queue, Queue *q, |
| 1217 |
int *rc, int ignore_name, streng **basename ) |
| 1218 |
{ |
| 1219 |
stk_tsd_t *st = (stk_tsd_t *)TSD->stk_tsd; |
| 1220 |
streng *qn; |
| 1221 |
int h; |
| 1222 |
Queue *retval; |
| 1223 |
|
| 1224 |
qn = ( queue == NULL ) ? NULL : Str_dupTSD( queue ); |
| 1225 |
|
| 1226 |
if ( ( h = save_parse_queue( TSD, qn, q, ignore_name ) ) < 0 ) |
| 1227 |
{ |
| 1228 |
if ( qn != NULL ) |
| 1229 |
Free_stringTSD( qn ); |
| 1230 |
*rc = -h; |
| 1231 |
return NULL; |
| 1232 |
} |
| 1233 |
if ( h == 1 ) |
| 1234 |
{ |
| 1235 |
if ( ( h = get_socket_details_and_connect( TSD, q ) ) != 0 ) |
| 1236 |
{ |
| 1237 |
if ( qn != NULL ) |
| 1238 |
Free_stringTSD( qn ); |
| 1239 |
disconnect_from_rxstack( TSD, q ); |
| 1240 |
*rc = h; |
| 1241 |
return NULL; |
| 1242 |
} |
| 1243 |
if ( !ignore_name ) |
| 1244 |
{ |
| 1245 |
if ( ( h = set_queue_in_rxstack( TSD, q->u.e.socket, qn ) ) != 0 ) |
| 1246 |
{ |
| 1247 |
if ( qn != NULL ) |
| 1248 |
Free_stringTSD( qn ); |
| 1249 |
disconnect_from_rxstack( TSD, q ); |
| 1250 |
*rc = h; |
| 1251 |
return NULL; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
retval = q ; |
| 1255 |
} |
| 1256 |
else |
| 1257 |
{ |
| 1258 |
retval = st->current_queue ; |
| 1259 |
} |
| 1260 |
|
| 1261 |
if ( basename != NULL ) |
| 1262 |
*basename = qn; |
| 1263 |
else if ( qn != NULL ) |
| 1264 |
Free_stringTSD( qn ); |
| 1265 |
|
| 1266 |
*rc = 0; |
| 1267 |
return retval; |
| 1268 |
} |
| 1269 |
#endif |
| 1270 |
|
| 1271 |
/* SetCurrentQueue set the current queue and makes all the necessary cleanup |
| 1272 |
* of the "old" current queue. |
| 1273 |
* new_name is assigned to the current queue's name, the former value is |
| 1274 |
* returned. |
| 1275 |
*/ |
| 1276 |
static streng *SetCurrentQueue( const tsd_t *TSD, stk_tsd_t *st, Queue *q, |
| 1277 |
streng *new_name ) |
| 1278 |
{ |
| 1279 |
streng *retval; |
| 1280 |
|
| 1281 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1282 |
if ( ( st->current_queue->type == QisExternal ) |
| 1283 |
&& ( st->current_queue != q ) ) |
| 1284 |
delete_an_external_queue( TSD, st, st->current_queue ); |
| 1285 |
#else |
| 1286 |
(TSD); |
| 1287 |
#endif |
| 1288 |
st->current_queue = q; |
| 1289 |
|
| 1290 |
retval = st->current_queue_name; |
| 1291 |
st->current_queue_name = new_name; |
| 1292 |
return retval; |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* |
| 1296 |
* Create a new queue |
| 1297 |
* RXQUEUE( 'Create' ) |
| 1298 |
* Reuses the current connection if possible. The newly created current |
| 1299 |
* queue becomes the current queue. |
| 1300 |
*/ |
| 1301 |
int create_queue( tsd_t *TSD, const streng *queue_name, streng **result ) |
| 1302 |
{ |
| 1303 |
streng *new_queue = NULL ; |
| 1304 |
stk_tsd_t *st; |
| 1305 |
char buf[50]; |
| 1306 |
Queue *q = NULL ; |
| 1307 |
int rc = 0 ; |
| 1308 |
|
| 1309 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1310 |
if ( !use_external( TSD, queue_name ) ) |
| 1311 |
{ |
| 1312 |
if ( queue_name == NULL ) |
| 1313 |
{ |
| 1314 |
/* |
| 1315 |
* Create a unique queue name |
| 1316 |
*/ |
| 1317 |
sprintf(buf,"S%d-%ld-%d", getpid(), clock(), st->runner++ ); |
| 1318 |
new_queue = Str_cre_TSD( TSD, buf ) ; |
| 1319 |
} |
| 1320 |
else |
| 1321 |
{ |
| 1322 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 1323 |
{ |
| 1324 |
/* |
| 1325 |
* No queue of that name, so use it. |
| 1326 |
*/ |
| 1327 |
new_queue = Str_dupTSD( queue_name ) ; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
if ( q->type == QisSESSION ) |
| 1332 |
{ |
| 1333 |
if ( !TSD->called_from_saa ) |
| 1334 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue from stack" ); |
| 1335 |
|
| 1336 |
return 5 ; |
| 1337 |
} |
| 1338 |
/* |
| 1339 |
* If the queue we found is a false queue, we can still |
| 1340 |
* use the supplied name and the slot |
| 1341 |
*/ |
| 1342 |
if ( q->u.i.isReal ) |
| 1343 |
{ |
| 1344 |
/* |
| 1345 |
* Create a unique queue name |
| 1346 |
*/ |
| 1347 |
sprintf(buf,"S%d-%ld-%d", getpid(), clock(), st->runner++ ); |
| 1348 |
new_queue = Str_cre_TSD( TSD, buf ) ; |
| 1349 |
rc = 1; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
if ( new_queue != NULL ) /* new name for a new slot */ |
| 1355 |
{ |
| 1356 |
q = find_free_slot( TSD ); |
| 1357 |
if ( q == NULL ) |
| 1358 |
{ |
| 1359 |
if ( new_queue != NULL ) |
| 1360 |
Free_stringTSD( new_queue ); |
| 1361 |
return 12; /* RXQUEUE_MEMFAIL */ |
| 1362 |
} |
| 1363 |
q->type = QisInternal ; |
| 1364 |
if ( new_queue == queue_name ) /* need a fresh one */ |
| 1365 |
new_queue = Str_dupTSD( new_queue ) ; |
| 1366 |
q->u.i.name = Str_upper( new_queue ) ; |
| 1367 |
} |
| 1368 |
assert( q->type == QisInternal ) ; |
| 1369 |
q->u.i.isReal = 1; |
| 1370 |
/* |
| 1371 |
* result created here; up to caller to free it |
| 1372 |
*/ |
| 1373 |
*result = Str_dupTSD( q->u.i.name ); |
| 1374 |
} |
| 1375 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1376 |
else |
| 1377 |
{ |
| 1378 |
Queue q1, *work; |
| 1379 |
streng *base; |
| 1380 |
|
| 1381 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 1, &base ) ) == NULL ) |
| 1382 |
return rc; |
| 1383 |
|
| 1384 |
if ( ( rc = create_queue_on_rxstack( TSD, work, base, result ) ) == -1 ) |
| 1385 |
rc = 100; |
| 1386 |
|
| 1387 |
if ( base != NULL ) |
| 1388 |
Free_stringTSD( base ); |
| 1389 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 1390 |
} |
| 1391 |
#endif |
| 1392 |
return rc; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/* |
| 1396 |
* Delete a queue |
| 1397 |
* RXQUEUE( 'Delete' ) |
| 1398 |
* Reuses the current connection if possible. SESSION becomes the current |
| 1399 |
* queue always. |
| 1400 |
*/ |
| 1401 |
int delete_queue( tsd_t *TSD, const streng *queue_name ) |
| 1402 |
{ |
| 1403 |
int rc = 0; |
| 1404 |
stk_tsd_t *st; |
| 1405 |
Queue *q; |
| 1406 |
streng *h; |
| 1407 |
|
| 1408 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1409 |
if ( !use_external( TSD, queue_name ) ) |
| 1410 |
{ |
| 1411 |
if ( ( queue_name == NULL ) || ( PSTRENGLEN( queue_name ) == 0 ) ) |
| 1412 |
return 9 ; |
| 1413 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 1414 |
return 9 ; |
| 1415 |
if ( q->type == QisSESSION ) |
| 1416 |
return 5 ; /* SESSION can't be deleted */ |
| 1417 |
assert( q->type == QisInternal ) ; |
| 1418 |
/* |
| 1419 |
* If we found a false queue, return 9 |
| 1420 |
*/ |
| 1421 |
if ( !q->u.i.isReal ) |
| 1422 |
rc = 9; |
| 1423 |
/* |
| 1424 |
* Delete the contents of the queue |
| 1425 |
* and mark it as gone. |
| 1426 |
*/ |
| 1427 |
delete_an_internal_queue( TSD, st, q ); |
| 1428 |
h = SetCurrentQueue( TSD, st, &st->queue[0], Str_creTSD( "SESSION" ) ) ; |
| 1429 |
Free_stringTSD( h ); |
| 1430 |
} |
| 1431 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1432 |
else |
| 1433 |
{ |
| 1434 |
Queue q1, *work; |
| 1435 |
streng *base; |
| 1436 |
|
| 1437 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 1, &base ) ) == NULL ) |
| 1438 |
return rc; |
| 1439 |
|
| 1440 |
if ( ( base == NULL ) || ( PSTRENGLEN( base ) == 0 ) ) |
| 1441 |
{ |
| 1442 |
if ( base != NULL ) |
| 1443 |
Free_stringTSD( base ); |
| 1444 |
if ( TSD->called_from_saa ) |
| 1445 |
return 9; |
| 1446 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of(TSD, queue_name ) ) ; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if ( ( rc = delete_queue_from_rxstack( TSD, work->u.e.socket, base ) ) == 0 ) |
| 1450 |
{ |
| 1451 |
/* This will disconnect the current external queue if needed */ |
| 1452 |
h = SetCurrentQueue( TSD, st, &st->queue[0], Str_creTSD( "SESSION" ) ); |
| 1453 |
Free_stringTSD( h ); |
| 1454 |
} |
| 1455 |
|
| 1456 |
if ( base != NULL ) |
| 1457 |
Free_stringTSD( base ); |
| 1458 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 1459 |
|
| 1460 |
if ( rc == -1 ) |
| 1461 |
rc = 100; |
| 1462 |
} |
| 1463 |
#endif |
| 1464 |
return rc; |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* |
| 1468 |
* Set timeout for queue |
| 1469 |
* RXQUEUE( 'Timeout' ) |
| 1470 |
* Do not reuse the current connection. A different external queue name |
| 1471 |
* leads to an expensive NOP. |
| 1472 |
* Timeout only valid for external queues. |
| 1473 |
*/ |
| 1474 |
int timeout_queue( tsd_t *TSD, const streng *timeout, const streng *queue_name ) |
| 1475 |
{ |
| 1476 |
stk_tsd_t *st; |
| 1477 |
int rc = 0 ; |
| 1478 |
|
| 1479 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1480 |
if ( !use_external( TSD, queue_name ) ) |
| 1481 |
{ |
| 1482 |
exiterror( ERR_EXTERNAL_QUEUE, 111, "TIMEOUT" ) ; |
| 1483 |
} |
| 1484 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1485 |
else |
| 1486 |
{ |
| 1487 |
Queue q, *work; |
| 1488 |
int val, err; |
| 1489 |
|
| 1490 |
if ( ( work = open_external( TSD, queue_name, &q, &rc, 1, NULL ) ) == NULL ) |
| 1491 |
return rc; |
| 1492 |
/* |
| 1493 |
* Convert incoming streng to positive (or zero) integer |
| 1494 |
*/ |
| 1495 |
val = streng_to_int( TSD, timeout, &err ) ; |
| 1496 |
if ( ( val < 0 ) || err ) |
| 1497 |
{ |
| 1498 |
disconnect_from_rxstack( TSD, &q ) ; |
| 1499 |
exiterror( ERR_INCORRECT_CALL, 930, 999999999, tmpstr_of( TSD, timeout ) ); |
| 1500 |
} |
| 1501 |
/* |
| 1502 |
* Indicate that this queue has a timeout |
| 1503 |
*/ |
| 1504 |
work->u.e.timeoutSet = 1; |
| 1505 |
rc = timeout_queue_on_rxstack( TSD, work->u.e.socket, val ); |
| 1506 |
disconnect_from_rxstack( TSD, &q ) ; |
| 1507 |
} |
| 1508 |
#endif |
| 1509 |
return rc ; |
| 1510 |
} |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* Return the name of the current queue. The value is silently nul terminated. |
| 1514 |
* RXQUEUE( 'Get' ) |
| 1515 |
* Returns queue name even if it is a false queue |
| 1516 |
*/ |
| 1517 |
streng *get_queue( tsd_t *TSD ) |
| 1518 |
{ |
| 1519 |
streng *result; |
| 1520 |
stk_tsd_t *st; |
| 1521 |
int l; |
| 1522 |
char *p; |
| 1523 |
|
| 1524 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1525 |
if ( st->queue[0].u.i.name == NULL ) |
| 1526 |
SetSessionName( TSD, st ) ; |
| 1527 |
|
| 1528 |
assert( st->current_queue_name != NULL ); |
| 1529 |
|
| 1530 |
l = Str_len( st->current_queue_name ); |
| 1531 |
result = Str_makeTSD( l + 1 ); |
| 1532 |
p = Str_val( result ); |
| 1533 |
memcpy( p, Str_val( st->current_queue_name ), l ); |
| 1534 |
p[l] = '\0'; |
| 1535 |
Str_len( result ) = l; |
| 1536 |
|
| 1537 |
return result; |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* |
| 1541 |
* Return the name and length of the current queue in argument pointers. No |
| 1542 |
* copy is done. |
| 1543 |
*/ |
| 1544 |
void fill_queue_name( const tsd_t *TSD, int *len, char **name ) |
| 1545 |
{ |
| 1546 |
stk_tsd_t *st; |
| 1547 |
|
| 1548 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1549 |
if ( st->queue[0].u.i.name == NULL ) |
| 1550 |
SetSessionName( TSD, st ) ; |
| 1551 |
|
| 1552 |
assert( st->current_queue_name != NULL ); |
| 1553 |
|
| 1554 |
*len = Str_len( st->current_queue_name ); |
| 1555 |
*name = Str_val( st->current_queue_name ); |
| 1556 |
} |
| 1557 |
|
| 1558 |
/* |
| 1559 |
* Set the current queue |
| 1560 |
* RXQUEUE( 'Set' ) |
| 1561 |
* Object Rexx allows the queue to be set to an unknown queue |
| 1562 |
* This is stupid; the user should be told its not valid, and |
| 1563 |
* return an empty string. |
| 1564 |
* But we validly set the false queue :-( |
| 1565 |
* Reuses the current connection if possible. |
| 1566 |
*/ |
| 1567 |
streng *set_queue( tsd_t *TSD, const streng *queue_name ) |
| 1568 |
{ |
| 1569 |
stk_tsd_t *st ; |
| 1570 |
Queue *q ; |
| 1571 |
|
| 1572 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1573 |
if ( !use_external( TSD, queue_name ) ) |
| 1574 |
{ |
| 1575 |
if ( ( q = find_queue( TSD, st, queue_name ) ) == NULL ) |
| 1576 |
{ |
| 1577 |
/* |
| 1578 |
* We didn't find a real or a false queue, so create |
| 1579 |
* a false queue |
| 1580 |
*/ |
| 1581 |
q = find_free_slot( TSD ); |
| 1582 |
q->type = QisInternal ; |
| 1583 |
q->u.i.name = Str_upper( Str_dupTSD( queue_name ) ) ; |
| 1584 |
q->u.i.isReal = 0 ; /* false queue */ |
| 1585 |
} |
| 1586 |
assert( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) ; |
| 1587 |
return SetCurrentQueue( TSD, st, q, Str_dupTSD( q->u.i.name ) ); |
| 1588 |
} |
| 1589 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1590 |
else |
| 1591 |
{ |
| 1592 |
Queue q1, *work; |
| 1593 |
streng *base, *result; |
| 1594 |
int rc; |
| 1595 |
|
| 1596 |
if ( ( work = open_external( TSD, queue_name, &q1, &rc, 1, &base ) ) == NULL ) |
| 1597 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue from stack" ); |
| 1598 |
|
| 1599 |
if ( ( base == NULL ) || ( PSTRENGLEN( base ) == 0 ) ) |
| 1600 |
{ |
| 1601 |
if ( base != NULL ) |
| 1602 |
Free_stringTSD( base ); |
| 1603 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 1604 |
assert( !TSD->called_from_saa ); |
| 1605 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of(TSD, queue_name ) ) ; |
| 1606 |
} |
| 1607 |
if ( ( rc = set_queue_in_rxstack( TSD, work->u.e.socket, base ) ) == 0 ) |
| 1608 |
{ |
| 1609 |
Free_stringTSD( base ); |
| 1610 |
if ( ( rc = get_queue_from_rxstack( TSD, work, &result ) ) != 0 ) |
| 1611 |
{ |
| 1612 |
disconnect_from_rxstack( TSD, &q1 ); |
| 1613 |
assert( !TSD->called_from_saa ); |
| 1614 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue back from stack" ); |
| 1615 |
} |
| 1616 |
if ( work == &q1 ) |
| 1617 |
{ |
| 1618 |
work = find_free_slot( TSD ); |
| 1619 |
*work = q1; |
| 1620 |
} |
| 1621 |
return SetCurrentQueue( TSD, st, work, result ); |
| 1622 |
} |
| 1623 |
Free_stringTSD( base ); |
| 1624 |
disconnect_from_rxstack( TSD, &q1 ) ; |
| 1625 |
assert( !TSD->called_from_saa ); |
| 1626 |
exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue from stack" ); |
| 1627 |
} |
| 1628 |
#endif |
| 1629 |
return NULL; |
| 1630 |
} |
| 1631 |
|
| 1632 |
Queue *addr_reopen_queue( tsd_t *TSD, const streng *queuename, char code ) |
| 1633 |
/* This is the open routine for the ADDRESS WITH-redirection. queuename is |
| 1634 |
* the name of the queue. code is either 'r' for "READ", |
| 1635 |
* 'A' for "WRITE APPEND", 'R' for "WRITE REPLACE". |
| 1636 |
* The queue named queuename will be (re)opened in all cases. |
| 1637 |
* In case of REPLACE the queue's content must be purged later by |
| 1638 |
* addr_purge_queue. |
| 1639 |
* A structure for queues is returned and should be used for calls |
| 1640 |
* to addr_io_queue. |
| 1641 |
* The return value *may* be NULL in case of an error. A NOTREADY condition |
| 1642 |
* may have been raised in this case. |
| 1643 |
*/ |
| 1644 |
{ |
| 1645 |
stk_tsd_t *st ; |
| 1646 |
Queue *q ; |
| 1647 |
streng *name ; |
| 1648 |
|
| 1649 |
st = (stk_tsd_t *)TSD->stk_tsd ; |
| 1650 |
|
| 1651 |
if ( ( queuename == NULL ) || ( PSTRENGLEN( queuename ) == 0 ) ) |
| 1652 |
return st->current_queue ; |
| 1653 |
|
| 1654 |
if ( !use_external( TSD, queuename ) ) |
| 1655 |
{ |
| 1656 |
q = find_queue( TSD, st, queuename ) ; |
| 1657 |
if ( ( code == 'r' ) || ( q != NULL ) ) |
| 1658 |
{ |
| 1659 |
if ( q != NULL ) |
| 1660 |
q->u.i.isReal = 1 ; /* not sure if this's right */ |
| 1661 |
return q ; |
| 1662 |
} |
| 1663 |
/* create a new queue */ |
| 1664 |
q = find_free_slot( TSD ) ; |
| 1665 |
q->type = QisInternal ; |
| 1666 |
name = Str_dupTSD( queuename ) ; |
| 1667 |
q->u.i.name = Str_upper( name ) ; |
| 1668 |
q->u.i.isReal = 1 ; |
| 1669 |
return q ; |
| 1670 |
} |
| 1671 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1672 |
/* always use a fresh connection for external queues. There are too many |
| 1673 |
* circumstances to keep in mind to check cleanly, whether or not to use |
| 1674 |
* the current connection or if it exists already. The caller will check |
| 1675 |
* for same names later. |
| 1676 |
*/ |
| 1677 |
q = find_free_slot( TSD ) ; |
| 1678 |
|
| 1679 |
name = Str_dupTSD( queuename ) ; |
| 1680 |
if ( save_parse_queue( TSD, name, q, 1 ) < 0 ) |
| 1681 |
{ |
| 1682 |
Free_stringTSD( name ); |
| 1683 |
return NULL; |
| 1684 |
} |
| 1685 |
if ( get_socket_details_and_connect( TSD, q ) != 0 ) |
| 1686 |
{ |
| 1687 |
disconnect_from_rxstack( TSD, q ) ; |
| 1688 |
return NULL; |
| 1689 |
} |
| 1690 |
set_queue_in_rxstack( TSD, q->u.e.socket, name ); |
| 1691 |
|
| 1692 |
return q ; |
| 1693 |
#else |
| 1694 |
return NULL ; |
| 1695 |
#endif |
| 1696 |
} |
| 1697 |
|
| 1698 |
/* returns 1 if the two queue pointers point to the same physical queue, |
| 1699 |
* 0 else. |
| 1700 |
*/ |
| 1701 |
int addr_same_queue( const tsd_t *TSD, const Queue *q1, const Queue *q2 ) |
| 1702 |
{ |
| 1703 |
int t1, t2, retval = 0 ; |
| 1704 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1705 |
streng *n1, *n2 ; |
| 1706 |
#endif |
| 1707 |
|
| 1708 |
if ( q1 == q2 ) |
| 1709 |
return 1 ; |
| 1710 |
if ( ( t1 = q1->type ) == QisSESSION ) |
| 1711 |
t1 = QisInternal ; |
| 1712 |
if ( ( t2 = q2->type ) == QisSESSION ) |
| 1713 |
t2 = QisInternal ; |
| 1714 |
if ( t1 != t2 ) |
| 1715 |
return 0 ; |
| 1716 |
if (t1 == QisInternal) |
| 1717 |
return !Str_cmp( q1->u.i.name, q2->u.i.name ) ; |
| 1718 |
assert( q1->type == QisExternal ) ; |
| 1719 |
if ( q1->u.e.address != q2->u.e.address ) |
| 1720 |
return 0 ; |
| 1721 |
if ( q1->u.e.portno != q2->u.e.portno ) |
| 1722 |
return 0 ; |
| 1723 |
/* we have to use the slow name comparison */ |
| 1724 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1725 |
if ( get_queue_from_rxstack( TSD, q1, &n1 ) != 0 ) |
| 1726 |
return 0; |
| 1727 |
if ( get_queue_from_rxstack( TSD, q2, &n2 ) != 0 ) |
| 1728 |
{ |
| 1729 |
Free_stringTSD( n1 ) ; |
| 1730 |
return 0; |
| 1731 |
} |
| 1732 |
retval = !Str_cmp( n1, n2 ) ; |
| 1733 |
Free_stringTSD( n1 ) ; |
| 1734 |
Free_stringTSD( n2 ) ; |
| 1735 |
#endif |
| 1736 |
return retval ; |
| 1737 |
} |
| 1738 |
|
| 1739 |
/* addr_redir_queue moves the content of the top buffer of an existing queue |
| 1740 |
* to a new temporary queue. |
| 1741 |
*/ |
| 1742 |
Queue *addr_redir_queue( const tsd_t *TSD, Queue *q ) |
| 1743 |
{ |
| 1744 |
stk_tsd_t *st ; |
| 1745 |
Queue *retval ; |
| 1746 |
Buffer *b ; |
| 1747 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1748 |
streng *contents ; |
| 1749 |
StackLine *ptr ; |
| 1750 |
#endif |
| 1751 |
|
| 1752 |
st = (stk_tsd_t *)TSD->stk_tsd ; |
| 1753 |
assert( ( q->type == QisSESSION ) |
| 1754 |
|| ( q->type == QisInternal ) |
| 1755 |
|| ( q->type == QisExternal ) ) ; |
| 1756 |
retval = find_free_slot( TSD ) ; |
| 1757 |
retval->type = QisTemp ; |
| 1758 |
|
| 1759 |
if ( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) |
| 1760 |
{ |
| 1761 |
/* trivial */ |
| 1762 |
if ( ( b = q->u.i.top ) != NULL ) |
| 1763 |
{ |
| 1764 |
/* |
| 1765 |
* Fixes bug 777645 |
| 1766 |
*/ |
| 1767 |
q->u.i.elements -= b->elements ; |
| 1768 |
retval->u.t = *b ; |
| 1769 |
retval->u.t.higher = retval->u.t.lower = NULL ; |
| 1770 |
b->top = b->bottom = NULL ; |
| 1771 |
b->elements = 0 ; |
| 1772 |
} |
| 1773 |
} |
| 1774 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1775 |
else |
| 1776 |
{ |
| 1777 |
/* loop until EOF. We can't use lines_in_stack, since the number may |
| 1778 |
* change because of multiple accesses by different clients. |
| 1779 |
*/ |
| 1780 |
while ( get_line_from_rxstack( TSD, q->u.e.socket, &contents, 1 ) == 0 ) |
| 1781 |
{ |
| 1782 |
ptr = (StackLine *) MallocTSD( sizeof( StackLine ) ) ; |
| 1783 |
ptr->contents = contents ; |
| 1784 |
FIFO_LINE( &retval->u.t, ptr ) ; |
| 1785 |
} |
| 1786 |
} |
| 1787 |
#endif |
| 1788 |
return retval ; |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* addr_purge_queue discards the contents of a queue. It is caused by a |
| 1792 |
* ADDRESS WITH ??? REPLACE ??? or at the end of the use of an input |
| 1793 |
* queue. |
| 1794 |
*/ |
| 1795 |
void addr_purge_queue( const tsd_t *TSD, Queue *q ) |
| 1796 |
{ |
| 1797 |
stk_tsd_t *st ; |
| 1798 |
Buffer *b ; |
| 1799 |
|
| 1800 |
st = (stk_tsd_t *)TSD->stk_tsd ; |
| 1801 |
assert( ( q->type == QisSESSION ) |
| 1802 |
|| ( q->type == QisInternal ) |
| 1803 |
|| ( q->type == QisExternal ) ) ; |
| 1804 |
|
| 1805 |
if ( ( q->type == QisSESSION ) || ( q->type == QisInternal ) ) |
| 1806 |
{ |
| 1807 |
ENSURE_BUFFER( q ) ; |
| 1808 |
b = q->u.i.top ; |
| 1809 |
q->u.i.elements -= b->elements ; |
| 1810 |
delete_buffer_content( TSD, st, b ) ; |
| 1811 |
} |
| 1812 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1813 |
else |
| 1814 |
{ |
| 1815 |
clear_queue_on_rxstack( TSD, q->u.e.socket ) ; |
| 1816 |
} |
| 1817 |
#endif |
| 1818 |
} |
| 1819 |
|
| 1820 |
streng *addr_io_queue( tsd_t *TSD, Queue *q, streng *buffer, int isFIFO ) |
| 1821 |
/* This is the working routine for the ADDRESS WITH-redirection. q is |
| 1822 |
* the return value of addr_reopen_queue. buffer must be NULL for a read |
| 1823 |
* operation or a filled buffer. |
| 1824 |
* The return value is NULL in case of a write operation or in case of EOF |
| 1825 |
* while reading. |
| 1826 |
* buffer is consumed if given and indicates a push operation instead of a |
| 1827 |
* pull operation. isFIFO indicates the type of the push operation. |
| 1828 |
*/ |
| 1829 |
{ |
| 1830 |
StackLine *ptr, *newbox ; |
| 1831 |
Buffer *b ; |
| 1832 |
streng *retval ; |
| 1833 |
|
| 1834 |
if ( buffer == NULL ) |
| 1835 |
{ |
| 1836 |
/* pull operation */ |
| 1837 |
switch ( q->type ) |
| 1838 |
{ |
| 1839 |
case QisSESSION: |
| 1840 |
case QisInternal: |
| 1841 |
ENSURE_BUFFER( q ) ; |
| 1842 |
b = q->u.i.top ; /* Work on the top buffer only */ |
| 1843 |
POP_LINE( b, ptr ) ; |
| 1844 |
if ( ptr == NULL ) |
| 1845 |
return NULL ; |
| 1846 |
q->u.i.elements-- ; |
| 1847 |
retval = ptr->contents ; |
| 1848 |
FreeTSD( ptr ) ; |
| 1849 |
return retval ; |
| 1850 |
|
| 1851 |
case QisTemp: |
| 1852 |
b = &q->u.t ; |
| 1853 |
POP_LINE( b, ptr ) ; |
| 1854 |
if ( ptr == NULL ) |
| 1855 |
return NULL ; |
| 1856 |
retval = ptr->contents ; |
| 1857 |
FreeTSD( ptr ) ; |
| 1858 |
return retval ; |
| 1859 |
|
| 1860 |
#if !defined( NO_EXTERNAL_QUEUES ) |
| 1861 |
case QisExternal: |
| 1862 |
if ( get_line_from_rxstack( TSD, q->u.e.socket, &retval, 1 ) == 0 ) |
| 1863 |
return retval ; |
| 1864 |
return NULL ; |
| 1865 |
#endif |
| 1866 |
|
| 1867 |
default: |
| 1868 |
break ; |
| 1869 |
} |
| 1870 |
/* default: (probably an unused queue) */ |
| 1871 |
return NULL ; |
| 1872 |
} |
| 1873 |
|
| 1874 |
/* buffer's not NULL */ |
| 1875 |
assert( ( Str_max( buffer ) >= Str_len( buffer ) ) ) ; |
| 1876 |
|
| 1877 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1878 |
if ( q->type == QisExternal ) |
| 1879 |
{ |
| 1880 |
if ( isFIFO ) |
| 1881 |
queue_line_fifo_to_rxstack( TSD, q->u.e.socket, buffer ) ; |
| 1882 |
else |
| 1883 |
queue_line_lifo_to_rxstack( TSD, q->u.e.socket, buffer ) ; |
| 1884 |
return NULL ; |
| 1885 |
} |
| 1886 |
#endif |
| 1887 |
|
| 1888 |
newbox = (StackLine *) MallocTSD( sizeof( StackLine ) ) ; |
| 1889 |
newbox->contents = buffer ; |
| 1890 |
if ( isFIFO ) |
| 1891 |
{ |
| 1892 |
/* push FIFO operation */ |
| 1893 |
switch ( q->type ) |
| 1894 |
{ |
| 1895 |
case QisSESSION: |
| 1896 |
case QisInternal: |
| 1897 |
ENSURE_BUFFER( q ) ; |
| 1898 |
b = q->u.i.top ; |
| 1899 |
q->u.i.elements++ ; |
| 1900 |
FIFO_LINE( b, newbox ) ; |
| 1901 |
break ; |
| 1902 |
|
| 1903 |
case QisTemp: |
| 1904 |
b = &q->u.t ; |
| 1905 |
q->u.i.elements++ ; |
| 1906 |
FIFO_LINE( b, newbox ) ; |
| 1907 |
break ; |
| 1908 |
|
| 1909 |
default: |
| 1910 |
break ; |
| 1911 |
} |
| 1912 |
return NULL ; |
| 1913 |
} |
| 1914 |
|
| 1915 |
/* finally, we process push LIFO */ |
| 1916 |
switch ( q->type ) |
| 1917 |
{ |
| 1918 |
case QisSESSION: |
| 1919 |
case QisInternal: |
| 1920 |
ENSURE_BUFFER( q ) ; |
| 1921 |
b = q->u.i.top ; |
| 1922 |
q->u.i.elements++ ; |
| 1923 |
LIFO_LINE( b, newbox ) ; |
| 1924 |
break ; |
| 1925 |
|
| 1926 |
case QisTemp: |
| 1927 |
b = &q->u.t ; |
| 1928 |
q->u.i.elements++ ; |
| 1929 |
LIFO_LINE( b, newbox ) ; |
| 1930 |
break ; |
| 1931 |
|
| 1932 |
default: |
| 1933 |
break ; |
| 1934 |
} |
| 1935 |
return NULL ; |
| 1936 |
} |
| 1937 |
|
| 1938 |
/* addr_close_queue closes a queue opened by addr_reopen_queue. |
| 1939 |
* Never use another routine to do this. |
| 1940 |
* It is also capable to purge and close temporary stacks. |
| 1941 |
*/ |
| 1942 |
void addr_close_queue( const tsd_t *TSD, Queue *q ) |
| 1943 |
{ |
| 1944 |
stk_tsd_t *st ; |
| 1945 |
|
| 1946 |
st = (stk_tsd_t *)TSD->stk_tsd; |
| 1947 |
if ( q->type == QisTemp ) |
| 1948 |
{ |
| 1949 |
delete_a_temp_queue( TSD, st, q ) ; |
| 1950 |
return ; |
| 1951 |
} |
| 1952 |
#if !defined(NO_EXTERNAL_QUEUES) |
| 1953 |
if ( ( q != st->current_queue ) && ( q->type == QisExternal ) ) |
| 1954 |
delete_an_external_queue( TSD, st, q ) ; |
| 1955 |
#endif |
| 1956 |
} |