| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 1993-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 |
/* JH 20-10-99 */ /* To make Direct setting of stems Direct and not Symbolic. */ |
| 20 |
|
| 21 |
#include "regina_c.h" |
| 22 |
|
| 23 |
#if defined(WIN32) && defined(__IBMC__) |
| 24 |
# include <windows.h> |
| 25 |
# pragma warning(default: 4115 4201 4214) |
| 26 |
#else |
| 27 |
# if defined(WIN32) && (defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__)) |
| 28 |
# undef APIENTRY |
| 29 |
# include <windows.h> |
| 30 |
# else |
| 31 |
# if defined(__EMX__) && defined(OS2) |
| 32 |
# define DONT_TYPEDEF_PFN |
| 33 |
# undef APIENTRY |
| 34 |
# include <os2emx.h> |
| 35 |
# else |
| 36 |
# if defined(__WATCOMC__) && defined(OS2) |
| 37 |
# undef APIENTRY |
| 38 |
# define DONT_TYPEDEF_PFN |
| 39 |
# include <os2.h> |
| 40 |
# else |
| 41 |
# if defined(_MSC_VER) && !defined(__WINS__) |
| 42 |
# undef APIENTRY |
| 43 |
# if _MSC_VER >= 1100 |
| 44 |
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */ |
| 45 |
# pragma warning(disable: 4115 4201 4214 4514) |
| 46 |
# endif |
| 47 |
# include <windows.h> |
| 48 |
# if _MSC_VER >= 1100 |
| 49 |
# pragma warning(default: 4115 4201 4214) |
| 50 |
# endif |
| 51 |
# else |
| 52 |
# define APIENTRY |
| 53 |
# endif |
| 54 |
# endif |
| 55 |
# endif |
| 56 |
# endif |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef RXLIB /* need __regina_faked_main which is known with RXLIB only */ |
| 60 |
# define RXLIB |
| 61 |
#endif |
| 62 |
#include "rexx.h" |
| 63 |
|
| 64 |
#ifdef HAVE_UNISTD_H |
| 65 |
# include <unistd.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#include "rxiface.h" |
| 69 |
#include <stdio.h> |
| 70 |
#include <string.h> |
| 71 |
|
| 72 |
#ifdef HAVE_ERRNO_H |
| 73 |
#include <errno.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifdef HAVE_ASSERT_H |
| 77 |
# include <assert.h> |
| 78 |
#endif |
| 79 |
|
| 80 |
#ifdef HAVE_STDARG_H |
| 81 |
#include <stdarg.h> |
| 82 |
#endif |
| 83 |
|
| 84 |
#ifndef min |
| 85 |
# define min(a,b) (((a) < (b)) ? (a) : (b)) |
| 86 |
#endif |
| 87 |
|
| 88 |
static int ReMapHook( const tsd_t *TSD, int NetHook ) ; |
| 89 |
|
| 90 |
typedef struct { /* cli_tsd: static variables of this module (thread-safe) */ |
| 91 |
char count_params_buf[32]; /* handle_no_of_params() */ |
| 92 |
|
| 93 |
/* Buffering of external parameters allows a delayed deletion in case of |
| 94 |
* errors. |
| 95 |
*/ |
| 96 |
char * ExternalName ; |
| 97 |
int StringsCount ; |
| 98 |
int * Lengths ; /* StringsCount + 1 elements */ |
| 99 |
char ** Strings ; /* StringsCount + 1 elements */ |
| 100 |
} cli_tsd_t; /* thread-specific but only needed by this module. see |
| 101 |
* init_client |
| 102 |
*/ |
| 103 |
|
| 104 |
/* init_client initializes the module. |
| 105 |
* Currently, we set up the thread specific data. |
| 106 |
* The function returns 1 on success, 0 if memory is short. |
| 107 |
*/ |
| 108 |
int init_client( tsd_t *TSD ) |
| 109 |
{ |
| 110 |
cli_tsd_t *ct; |
| 111 |
|
| 112 |
if ( TSD->cli_tsd != NULL ) |
| 113 |
return(1); |
| 114 |
|
| 115 |
if ( ( TSD->cli_tsd = MallocTSD( sizeof(cli_tsd_t) ) ) == NULL ) |
| 116 |
return(0); |
| 117 |
ct = (cli_tsd_t *)TSD->cli_tsd; |
| 118 |
memset( ct, 0, sizeof(cli_tsd_t) ); |
| 119 |
ct->StringsCount = -1; |
| 120 |
return(1); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/* wrapstring makes a streng from external (application) data. If you get |
| 125 |
* an exception you may have a wild pointer in your application. |
| 126 |
* Returns NULL if string==NULL or length<0. |
| 127 |
*/ |
| 128 |
static streng *wrapstring( const tsd_t *TSD, const char *string, int length ) |
| 129 |
{ |
| 130 |
streng *result=NULL ; |
| 131 |
|
| 132 |
if (length>=0 && string) |
| 133 |
{ |
| 134 |
result = Str_makeTSD( length ) ; |
| 135 |
memcpy( result->value, string, length ) ; |
| 136 |
result->len = length ; |
| 137 |
} |
| 138 |
else |
| 139 |
result = NULL ; |
| 140 |
|
| 141 |
return result ; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
static int map_type( int in ) |
| 146 |
{ |
| 147 |
if (in==RX_TYPE_COMMAND) |
| 148 |
return INVO_COMMAND ; |
| 149 |
else if (in==RX_TYPE_FUNCTION) |
| 150 |
return INVO_FUNCTION ; |
| 151 |
else |
| 152 |
{ |
| 153 |
assert( in == RX_TYPE_SUBROUTINE ) ; |
| 154 |
return INVO_SUBROUTINE ; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
static paramboxptr parametrize( const tsd_t *TSD, int ArgCount, const int *ParLengths, const char **ParStrings ) |
| 160 |
{ |
| 161 |
paramboxptr parms=NULL, root=NULL ; |
| 162 |
int count=0, i=0 ; |
| 163 |
|
| 164 |
count = ArgCount ; |
| 165 |
for (i=0; count--; i++ ) |
| 166 |
{ |
| 167 |
if (parms) |
| 168 |
{ |
| 169 |
parms->next = (paramboxptr)MallocTSD( sizeof(parambox)) ; |
| 170 |
parms = parms->next ; |
| 171 |
} |
| 172 |
else |
| 173 |
parms = root = (paramboxptr)MallocTSD( sizeof(parambox )) ; |
| 174 |
|
| 175 |
parms->value = wrapstring( TSD, ParStrings[i], ParLengths[i] ) ; |
| 176 |
} |
| 177 |
|
| 178 |
if (parms) |
| 179 |
parms->next = NULL ; |
| 180 |
|
| 181 |
return root ; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* IfcPrepareReturnString will copy the content of src to the buffer described |
| 186 |
* by the pair (*targetlen, *targetbuf). The target is not checked, but a |
| 187 |
* value of RX_NO_STRING for *targetlen will be accepted. |
| 188 |
* |
| 189 |
* If src is a NULL pointer the targetlen will become 0 and targetbuf will |
| 190 |
* become NULL. |
| 191 |
* Otherwise the length of the target is checked if it can hold the entire |
| 192 |
* source string PLUS one string termination character. The terminator won't |
| 193 |
* be honoured in *targetlen. |
| 194 |
* A new buffer will be allocated using IfcAllocateString on insufficient size, |
| 195 |
* but the old one is not freed. |
| 196 |
* |
| 197 |
* This function is intended to be the opposite of wrapstring. |
| 198 |
*/ |
| 199 |
static void IfcPrepareReturnString( const streng *src, int *targetlen, |
| 200 |
char **targetbuf ) |
| 201 |
{ |
| 202 |
int len; |
| 203 |
|
| 204 |
/* |
| 205 |
* Check for an invalid source first. |
| 206 |
*/ |
| 207 |
if ( ( src == NULL ) || ( Str_val( src ) == NULL ) ) |
| 208 |
{ |
| 209 |
*targetlen = 0; |
| 210 |
*targetbuf = NULL; |
| 211 |
return; |
| 212 |
} |
| 213 |
len = Str_len( src ); |
| 214 |
if ( *targetlen < len + 1 ) |
| 215 |
{ |
| 216 |
if ( ( *targetbuf = (char *)IfcAllocateMemory( len + 1 ) ) == NULL ) |
| 217 |
{ |
| 218 |
/* |
| 219 |
* Better idea? |
| 220 |
*/ |
| 221 |
*targetlen = 0; |
| 222 |
return; |
| 223 |
} |
| 224 |
} |
| 225 |
memcpy( *targetbuf, Str_val( src ), len ); |
| 226 |
(*targetbuf)[len] = '\0'; |
| 227 |
*targetlen = len; |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* ScriptSetup does the setup step of IfcExecScript. |
| 232 |
* This function is part of the technique to bypass problems with |
| 233 |
* setjmp/longjmp. |
| 234 |
*/ |
| 235 |
static void ScriptSetup( tsd_t *TSD, |
| 236 |
void **instore_buf, unsigned long *instore_length, |
| 237 |
streng **command, const char *Name, int NameLen, |
| 238 |
paramboxptr *params, int ArgCount, |
| 239 |
const int *ParLengths, const char **ParStrings, |
| 240 |
int *ctype, int CallType, |
| 241 |
int restricted, |
| 242 |
int *hooks, int ExitFlags, |
| 243 |
streng **environment, int EnvLen, const char *EnvName ) |
| 244 |
{ |
| 245 |
int i; |
| 246 |
char c; |
| 247 |
streng *h; |
| 248 |
|
| 249 |
*instore_buf = NULL; |
| 250 |
*instore_length = 0; |
| 251 |
*command = wrapstring( TSD, Name, NameLen ); |
| 252 |
TSD->systeminfo->input_file = *command; |
| 253 |
assert( *command ); |
| 254 |
*params = parametrize( TSD, ArgCount, ParLengths, ParStrings ); |
| 255 |
|
| 256 |
*ctype = map_type( CallType ); |
| 257 |
|
| 258 |
TSD->restricted = restricted; |
| 259 |
|
| 260 |
*hooks = 0; |
| 261 |
for( i = 0; i < 30; i++ ) |
| 262 |
{ |
| 263 |
if ( ExitFlags & ( 1 << i ) ) |
| 264 |
*hooks |= 1 << ReMapHook( TSD, i ); |
| 265 |
} |
| 266 |
|
| 267 |
*environment = wrapstring( TSD, EnvName, EnvLen ); |
| 268 |
if ( *environment == NULL ) |
| 269 |
{ |
| 270 |
/* |
| 271 |
* We have to use the extension of the file. That is what the REXX API |
| 272 |
* documentations says and we have been called by the API in this case! |
| 273 |
*/ |
| 274 |
h = TSD->systeminfo->input_file; |
| 275 |
|
| 276 |
for ( i = Str_len( h ) - 1; i >= 0; i-- ) |
| 277 |
{ |
| 278 |
c = Str_val( h )[i]; |
| 279 |
if ( c == '.' ) |
| 280 |
{ |
| 281 |
i++; |
| 282 |
break; |
| 283 |
} |
| 284 |
else if ( strchr( FILE_SEPARATORS, c ) != NULL ) |
| 285 |
{ |
| 286 |
i = -1; |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
/* |
| 291 |
* i == -1: no suffix |
| 292 |
* i otherwise: position after dot (may lead to empty string, too) |
| 293 |
*/ |
| 294 |
if ( i == -1 ) |
| 295 |
i = Str_len( h ); |
| 296 |
|
| 297 |
*environment = Str_nodupTSD( h, i, Str_len( h ) - i ); |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* When executing a script, check if the environment we are running under exists |
| 302 |
* and has been "redirected" by a call to RexxRegisterSubcom???() |
| 303 |
*/ |
| 304 |
if ( envir_exists( TSD, *environment ) ) |
| 305 |
{ |
| 306 |
if ( get_subcomed_envir( TSD, *environment ) ) |
| 307 |
{ |
| 308 |
add_envir( TSD, Str_dupTSD( *environment ), ENVIR_PIPE, 0 ); |
| 309 |
} |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
add_envir( TSD, Str_dupTSD( *environment ), ENVIR_PIPE, 0 ); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* ScriptCleanup does the cleanup step of IfcExecScript. |
| 319 |
* 1) script_exit becomes the new old value for a jumped exit. |
| 320 |
* 2) environment is deregistered and freed. |
| 321 |
* 3) A fresh Ifc-allocated copy of result is put into RetLen/RetString, |
| 322 |
* and result is freed. *RetString is either used or replaced. |
| 323 |
* 4) InterpreterStatus is reset to the given value. |
| 324 |
* This function is part of the technique to bypass problems with |
| 325 |
* setjmp/longjmp. |
| 326 |
*/ |
| 327 |
static void ScriptCleanup(tsd_t *TSD, jmp_buf *script_exit, streng *environment, |
| 328 |
streng *result, int *RetLen, char **RetString, |
| 329 |
unsigned *InterpreterStatus) |
| 330 |
{ |
| 331 |
TSD->systeminfo->script_exit = script_exit; |
| 332 |
del_envir( TSD, environment ); |
| 333 |
Free_stringTSD( environment ); |
| 334 |
RestoreInterpreterStatus( TSD, InterpreterStatus ); |
| 335 |
|
| 336 |
IfcPrepareReturnString( result, RetLen, RetString ); |
| 337 |
|
| 338 |
if ( result ) |
| 339 |
Free_stringTSD( result ); |
| 340 |
} |
| 341 |
|
| 342 |
int IfcExecScript( tsd_t * volatile TSD, int NameLen, const char *Name, |
| 343 |
int ArgCount, const int *ParLengths, const char **ParStrings, |
| 344 |
int CallType, int ExitFlags, int EnvLen, const char *EnvName, |
| 345 |
int SourceCode, int restricted, |
| 346 |
const char *SourceString, unsigned long SourceStringLen, |
| 347 |
const void *TinnedTree, unsigned long TinnedTreeLen, |
| 348 |
int *RetLen, char **RetString, void **instore_buf, |
| 349 |
unsigned long *instore_length ) |
| 350 |
{ |
| 351 |
streng * volatile result=NULL; |
| 352 |
tsd_t * volatile saved_TSD; |
| 353 |
jmp_buf * volatile old_exit_addr; |
| 354 |
int volatile RetCode=0; |
| 355 |
streng * volatile environment=NULL; |
| 356 |
volatile int jumped=0; |
| 357 |
jmp_buf exit_addr; |
| 358 |
streng * command; |
| 359 |
paramboxptr params; |
| 360 |
int ctype,hooks,type; |
| 361 |
unsigned InterpreterStatus[IPRT_BUFSIZE]; |
| 362 |
|
| 363 |
/* |
| 364 |
* NOTE: |
| 365 |
* setjmp and longjmp will clobber all variables not explicitely marked |
| 366 |
* as volatile. The reason is the unexpected jump from anywhere to the |
| 367 |
* code following the setjmp() call. Every assumption of the compiler |
| 368 |
* about valid register variables are wrong. We have to remove all |
| 369 |
* register variables set by the optimizer. This can be done either by |
| 370 |
* using volatile, or by not using variables before the setjmp() INCLUDING |
| 371 |
* initialization or by using non-inline functions for manipulating |
| 372 |
* values. |
| 373 |
* We use a combination here. |
| 374 |
* If you ever need to change ANY line before the setjmp() code, |
| 375 |
* double-check the compiler's chance to cache some variables in registers. |
| 376 |
* Florian |
| 377 |
*/ |
| 378 |
saved_TSD = TSD; |
| 379 |
SaveInterpreterStatus( TSD, InterpreterStatus ); |
| 380 |
|
| 381 |
/* It may be that TSD->systeminfo->script_exit is not set. This may lead to |
| 382 |
* an exit() call, e.g. at Rexx "EXIT". We set TSD->systeminfo->script_exit |
| 383 |
* if it is not set. If a problem occurs we will be informed by a longjmp |
| 384 |
* and we will not completely been killed by an exit(). Remember: This |
| 385 |
* function is typically called if a program uses "us" as a subroutine. |
| 386 |
* Exiting is very harmful in this case. |
| 387 |
* Note: The memory allocated within called subsoutines will NOT be |
| 388 |
* freed. We must change many things to do this. Maybe, it's a good |
| 389 |
* idea to traverse all called subroutine trees and free the leaves. |
| 390 |
* FGC |
| 391 |
*/ |
| 392 |
if ( ( old_exit_addr = TSD->systeminfo->script_exit ) == NULL ) |
| 393 |
{ |
| 394 |
TSD->systeminfo->script_exit = &exit_addr; |
| 395 |
assert( !TSD->in_protected ); |
| 396 |
if ( setjmp( *TSD->systeminfo->script_exit ) ) |
| 397 |
{ |
| 398 |
TSD = saved_TSD; |
| 399 |
result = TSD->systeminfo->result; |
| 400 |
TSD->systeminfo->result = NULL; |
| 401 |
if ( !RetCode ) |
| 402 |
{ |
| 403 |
if ( result ) |
| 404 |
RetCode = atoi( result->value ); |
| 405 |
else |
| 406 |
RetCode = -1; |
| 407 |
} |
| 408 |
result = NULL; /* result was a static buffer provided by exiterror */ |
| 409 |
jumped++; |
| 410 |
} |
| 411 |
} |
| 412 |
if ( !jumped ) |
| 413 |
{ |
| 414 |
ScriptSetup( TSD, |
| 415 |
instore_buf, instore_length, |
| 416 |
&command, Name, NameLen, |
| 417 |
¶ms, ArgCount, ParLengths, ParStrings, |
| 418 |
&ctype, CallType, |
| 419 |
restricted, |
| 420 |
&hooks, ExitFlags, |
| 421 |
(streng **) &environment, EnvLen, EnvName ); |
| 422 |
|
| 423 |
type = SourceCode ; |
| 424 |
if ( type == RX_TYPE_EXTERNAL ) |
| 425 |
{ |
| 426 |
result = execute_external( TSD, command, params, environment, |
| 427 |
(int *) &RetCode, hooks, ctype ); |
| 428 |
} |
| 429 |
else if ( type == RX_TYPE_INSTORE ) |
| 430 |
{ |
| 431 |
result = do_instore( TSD, command, params, environment, |
| 432 |
(int *) &RetCode, hooks, |
| 433 |
TinnedTree, TinnedTreeLen, |
| 434 |
SourceString, SourceStringLen, NULL, ctype ); |
| 435 |
} |
| 436 |
else if ( type == RX_TYPE_MACRO ) |
| 437 |
result = nullstringptr(); |
| 438 |
else if ( type == RX_TYPE_SOURCE ) |
| 439 |
{ |
| 440 |
streng *SrcStr = wrapstring( TSD, SourceString, SourceStringLen ); |
| 441 |
internal_parser_type ipt; |
| 442 |
/* |
| 443 |
* Fix for Bug #436. Enable system exit to trap parsing errors |
| 444 |
*/ |
| 445 |
TSD->systeminfo->hooks = hooks; |
| 446 |
|
| 447 |
ipt = enter_macro( TSD, SrcStr, instore_buf, instore_length ) ; |
| 448 |
if ( ( CallType == RX_TYPE_COMMAND ) |
| 449 |
&& ArgCount |
| 450 |
&& ( *ParStrings != NULL ) |
| 451 |
&& ( *ParLengths == 3 ) |
| 452 |
&& ( memcmp( "//T", *ParStrings, 3 ) == 0 ) ) |
| 453 |
; /* request for tokenisation only, don't execute */ |
| 454 |
else |
| 455 |
{ |
| 456 |
ipt.kill = SrcStr; |
| 457 |
result = do_instore( TSD, command, params, environment, |
| 458 |
(int *) &RetCode, hooks, NULL,0, NULL,0, &ipt, |
| 459 |
ctype); |
| 460 |
/* do_instore already has deleted the internal structure */ |
| 461 |
} |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ); |
| 466 |
return 0; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
ScriptCleanup( TSD, old_exit_addr, environment, result, RetLen, RetString, |
| 471 |
InterpreterStatus ); |
| 472 |
|
| 473 |
return RetCode; |
| 474 |
} |
| 475 |
|
| 476 |
int IfcExecCallBack( tsd_t * volatile TSD, int NameLen, const char *Name, |
| 477 |
int ArgCount, const int *ParLengths, const char **ParStrings, |
| 478 |
int *RetLen, char **RetString ) |
| 479 |
{ |
| 480 |
nodeptr node; |
| 481 |
paramboxptr args; |
| 482 |
streng *result; |
| 483 |
streng *name; |
| 484 |
|
| 485 |
/* |
| 486 |
* If we can't find the label, no point in continuing. |
| 487 |
*/ |
| 488 |
name = Str_ncreTSD( Name, NameLen ); |
| 489 |
if ( ( node = getlabel( TSD, name ) ) == NULL ) |
| 490 |
{ |
| 491 |
FreeTSD( name ); |
| 492 |
return RX_CODE_NOSUCH; |
| 493 |
} |
| 494 |
FreeTSD( name ); |
| 495 |
if ( node->u.trace_only ) |
| 496 |
{ |
| 497 |
FreeTSD( name ); |
| 498 |
return RX_CODE_NOSUCH; |
| 499 |
} |
| 500 |
|
| 501 |
/* |
| 502 |
* Fixes bug 772199 |
| 503 |
*/ |
| 504 |
args = initargs( TSD, ArgCount, ParLengths, ParStrings ); |
| 505 |
result = CallInternalFunction( TSD, node, TSD->currentnode, args ); |
| 506 |
|
| 507 |
IfcPrepareReturnString( result, RetLen, RetString ); |
| 508 |
|
| 509 |
if ( result ) |
| 510 |
Free_stringTSD( result ); |
| 511 |
|
| 512 |
return 0; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
static int handle_source( const tsd_t *TSD, int *Length, char **String, |
| 517 |
int *allocated ) |
| 518 |
{ |
| 519 |
int infile=0, total=0, invoked=0 ; |
| 520 |
const char *stype=NULL ; |
| 521 |
char *ctmp=NULL ; |
| 522 |
int sleng=0 ; |
| 523 |
|
| 524 |
stype = system_type() ; |
| 525 |
sleng = strlen( stype ) ; |
| 526 |
infile = TSD->systeminfo->input_file->len ; |
| 527 |
invoked = strlen(invo_strings[TSD->systeminfo->invoked]) ; |
| 528 |
total = sleng + 1 + invoked + 1 + infile ; |
| 529 |
|
| 530 |
ctmp = *String = (char *)MallocTSD( (*Length=total)+2 ) ; |
| 531 |
sprintf( ctmp, "%s %s ", stype, invo_strings[TSD->systeminfo->invoked]) ; |
| 532 |
strncat( ctmp, TSD->systeminfo->input_file->value, infile ) ; |
| 533 |
|
| 534 |
*allocated = 1; |
| 535 |
return RX_CODE_OK ; |
| 536 |
} |
| 537 |
|
| 538 |
static int handle_param( tsd_t *TSD, int *Length, char **String ) |
| 539 |
{ |
| 540 |
paramboxptr ptr=NULL ; |
| 541 |
streng *value=NULL ; |
| 542 |
int number=0 ; |
| 543 |
|
| 544 |
ptr = TSD->systeminfo->currlevel0->args ; |
| 545 |
value = wrapstring( TSD, *String, *Length ) ; |
| 546 |
number = atopos( TSD, value, "internal", 1 ) ; |
| 547 |
Free_stringTSD( value ) ; |
| 548 |
value = get_parameter( ptr, number ) ; |
| 549 |
|
| 550 |
if (value) |
| 551 |
{ |
| 552 |
*(Length+1) = value->len ; |
| 553 |
*(String+1) = value->value ; |
| 554 |
} |
| 555 |
else |
| 556 |
*(Length+1) = RX_NO_STRING ; |
| 557 |
|
| 558 |
return RX_CODE_OK ; |
| 559 |
} |
| 560 |
|
| 561 |
static int handle_no_of_params( const tsd_t *TSD, int *Length, char **String ) |
| 562 |
{ |
| 563 |
paramboxptr ptr=NULL ; |
| 564 |
int count=0 ; |
| 565 |
cli_tsd_t *ct; |
| 566 |
|
| 567 |
ct = (cli_tsd_t *)TSD->cli_tsd; |
| 568 |
ptr = TSD->systeminfo->currlevel0->args ; |
| 569 |
count = count_params( ptr, PARAM_TYPE_HARD ) ; |
| 570 |
|
| 571 |
sprintf( ct->count_params_buf, "%d", count ) ; |
| 572 |
*String = ct->count_params_buf; |
| 573 |
*Length = strlen(*String) ; |
| 574 |
|
| 575 |
return RX_CODE_OK ; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
static int get_next_var( tsd_t *TSD, int *Lengths, char **Strings, |
| 581 |
int *allocated ) |
| 582 |
{ |
| 583 |
variableptr value=NULL, rval=NULL, rrval=NULL ; |
| 584 |
int l; |
| 585 |
|
| 586 |
*allocated = 0; |
| 587 |
if (TSD->var_indicator==0) |
| 588 |
{ |
| 589 |
get_next_variable( TSD, 1 ) ; |
| 590 |
TSD->var_indicator = 1 ; |
| 591 |
} |
| 592 |
|
| 593 |
for (;;) |
| 594 |
{ |
| 595 |
value = get_next_variable( TSD, 0 ) ; |
| 596 |
for (rval=value; rval && rval->realbox; rval=rval->realbox) |
| 597 |
{ |
| 598 |
; |
| 599 |
} |
| 600 |
if (rval && !(rval->flag & VFLAG_STR)) |
| 601 |
{ |
| 602 |
if (rval->flag & VFLAG_NUM) |
| 603 |
{ |
| 604 |
expand_to_str( TSD, rval ) ; |
| 605 |
break ; |
| 606 |
} |
| 607 |
|
| 608 |
if (!rval->stem) |
| 609 |
continue ; |
| 610 |
|
| 611 |
for (rrval=rval->stem; rrval && rrval->realbox; rrval=rrval->realbox) |
| 612 |
{ |
| 613 |
; |
| 614 |
} |
| 615 |
if (rrval && !(rrval->flag & VFLAG_STR)) |
| 616 |
{ |
| 617 |
if (rval->flag & VFLAG_NUM) |
| 618 |
{ |
| 619 |
expand_to_str( TSD, rval ) ; |
| 620 |
break ; |
| 621 |
} |
| 622 |
else |
| 623 |
continue ; |
| 624 |
} |
| 625 |
} |
| 626 |
break ; |
| 627 |
} |
| 628 |
|
| 629 |
if (rval) |
| 630 |
{ |
| 631 |
if (rval->stem) |
| 632 |
{ |
| 633 |
*allocated |= 1; |
| 634 |
l = Lengths[0] = rval->stem->name->len + rval->name->len ; |
| 635 |
Strings[0] = (char *)MallocTSD( (l < 1) ? 1 : l ) ; |
| 636 |
memcpy(Strings[0], rval->stem->name->value, rval->stem->name->len); |
| 637 |
memcpy(Strings[0]+rval->stem->name->len, |
| 638 |
rval->name->value, rval->name->len ) ; |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
Lengths[0] = rval->name->len ; |
| 643 |
Strings[0] = rval->name->value ; |
| 644 |
} |
| 645 |
|
| 646 |
if (rval->value) |
| 647 |
{ |
| 648 |
Lengths[1] = rval->value->len ; |
| 649 |
Strings[1] = rval->value->value ; |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
*allocated |= 2; |
| 654 |
assert( rval->stem && rrval->value ) ; |
| 655 |
l = Lengths[1] = rval->stem->name->len + rval->name->len ; |
| 656 |
Strings[1] = (char *)MallocTSD( (l < 1) ? 1 : l ) ; |
| 657 |
memcpy( Strings[1], rval->stem->name->value, value->stem->name->len ); |
| 658 |
memcpy( Strings[1]+value->stem->name->len, |
| 659 |
rval->name->value, rval->name->len ) ; |
| 660 |
} |
| 661 |
|
| 662 |
return 2 ; |
| 663 |
} |
| 664 |
else |
| 665 |
{ |
| 666 |
/* |
| 667 |
* Once we have reached the end of all variables, reset var_indicator |
| 668 |
* so next time we can start from the beginning again. |
| 669 |
*/ |
| 670 |
TSD->var_indicator = 0 ; |
| 671 |
return 0 ; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
static int MapHook( const tsd_t *TSD, int RexxHook ) |
| 677 |
{ |
| 678 |
switch ( RexxHook ) |
| 679 |
{ |
| 680 |
case HOOK_STDOUT: return RX_EXIT_STDOUT ; |
| 681 |
case HOOK_STDERR: return RX_EXIT_STDERR ; |
| 682 |
case HOOK_TRCIN: return RX_EXIT_TRCIN ; |
| 683 |
case HOOK_PULL: return RX_EXIT_PULL ; |
| 684 |
|
| 685 |
case HOOK_INIT: return RX_EXIT_INIT ; |
| 686 |
case HOOK_TERMIN: return RX_EXIT_TERMIN ; |
| 687 |
case HOOK_SUBCOM: return RX_EXIT_SUBCOM ; |
| 688 |
|
| 689 |
case HOOK_GETENV: return RX_EXIT_GETENV ; |
| 690 |
case HOOK_SETENV: return RX_EXIT_SETENV ; |
| 691 |
|
| 692 |
case HOOK_GETCWD: return RX_EXIT_GETCWD ; |
| 693 |
case HOOK_SETCWD: return RX_EXIT_SETCWD ; |
| 694 |
|
| 695 |
default: |
| 696 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ); |
| 697 |
} |
| 698 |
|
| 699 |
return 0 ; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
static int ReMapHook( const tsd_t *TSD, int NetHook ) |
| 704 |
{ |
| 705 |
switch ( NetHook ) |
| 706 |
{ |
| 707 |
case RX_EXIT_STDOUT: return HOOK_STDOUT ; |
| 708 |
case RX_EXIT_STDERR: return HOOK_STDERR ; |
| 709 |
case RX_EXIT_TRCIN: return HOOK_TRCIN ; |
| 710 |
case RX_EXIT_PULL: return HOOK_PULL ; |
| 711 |
|
| 712 |
case RX_EXIT_TERMIN: return HOOK_TERMIN ; |
| 713 |
case RX_EXIT_INIT: return HOOK_INIT ; |
| 714 |
case RX_EXIT_SUBCOM: return HOOK_SUBCOM ; |
| 715 |
case RX_EXIT_FUNC: return HOOK_FUNC ; |
| 716 |
|
| 717 |
case RX_EXIT_GETENV: return HOOK_GETENV ; |
| 718 |
case RX_EXIT_SETENV: return HOOK_SETENV ; |
| 719 |
|
| 720 |
case RX_EXIT_GETCWD: return HOOK_GETCWD ; |
| 721 |
case RX_EXIT_SETCWD: return HOOK_SETCWD ; |
| 722 |
|
| 723 |
default: |
| 724 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ); |
| 725 |
} |
| 726 |
|
| 727 |
return 0 ; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
int hookup( tsd_t *TSD, int hook ) |
| 733 |
{ |
| 734 |
int rcode=0, code=0; |
| 735 |
|
| 736 |
code = MapHook ( TSD, hook ); |
| 737 |
assert (code == RX_EXIT_INIT || code == RX_EXIT_TERMIN ); |
| 738 |
rcode = IfcDoExit( TSD, code, 0, NULL, 0, NULL, NULL, NULL ) ; |
| 739 |
if (rcode==RX_HOOK_ERROR) |
| 740 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 741 |
else if (rcode==RX_HOOK_GO_ON) |
| 742 |
rcode = HOOK_GO_ON ; |
| 743 |
else if (rcode==RX_HOOK_NOPE) |
| 744 |
rcode = HOOK_NOPE ; |
| 745 |
else |
| 746 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 747 |
return rcode ; |
| 748 |
} |
| 749 |
|
| 750 |
int hookup_output( tsd_t *TSD, int hook, const streng *outdata ) |
| 751 |
{ |
| 752 |
int rcode=0, code=0 ; |
| 753 |
char *str; |
| 754 |
int len = 0; |
| 755 |
|
| 756 |
code = MapHook( TSD, hook ); |
| 757 |
/* |
| 758 |
* Another level of checking to see if we have an exit handler |
| 759 |
* This probably should be added to hookup_input etc |
| 760 |
*/ |
| 761 |
if ( !IfcExitHandlerExists( TSD, code ) ) |
| 762 |
return HOOK_GO_ON; |
| 763 |
|
| 764 |
if (outdata) |
| 765 |
{ |
| 766 |
str = str_of( TSD, outdata ) ; |
| 767 |
len = outdata->len ; |
| 768 |
} |
| 769 |
else |
| 770 |
{ |
| 771 |
str = (char *)MallocTSD( 1 ) ; |
| 772 |
str[0] = '\0' ; |
| 773 |
len = 0 ; |
| 774 |
} |
| 775 |
|
| 776 |
rcode = IfcDoExit( TSD, code, len, str, 0, NULL, NULL, NULL) ; |
| 777 |
FreeTSD( str ) ; |
| 778 |
if (rcode==RX_HOOK_ERROR) |
| 779 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 780 |
else if (rcode==RX_HOOK_GO_ON) |
| 781 |
rcode = HOOK_GO_ON ; |
| 782 |
else if (rcode==RX_HOOK_NOPE) |
| 783 |
rcode = HOOK_NOPE ; |
| 784 |
else |
| 785 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 786 |
|
| 787 |
return rcode ; |
| 788 |
} |
| 789 |
|
| 790 |
int hookup_output2( tsd_t *TSD, int hook, const streng *outdata1, const streng *outdata2 ) |
| 791 |
{ |
| 792 |
int rcode=0, code=0 ; |
| 793 |
char *str1, *str2; |
| 794 |
int len1 = 0, len2 = 0; |
| 795 |
|
| 796 |
code = MapHook( TSD, hook ); |
| 797 |
/* assert (code == RX_EXIT_SETENV ); */ |
| 798 |
if (outdata1) |
| 799 |
{ |
| 800 |
str1 = str_of( TSD, outdata1 ) ; |
| 801 |
len1 = outdata1->len ; |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
str1 = (char *)MallocTSD( 1 ) ; |
| 806 |
str1[0] = '\0' ; |
| 807 |
len1 = 0 ; |
| 808 |
} |
| 809 |
if (outdata2) |
| 810 |
{ |
| 811 |
str2 = str_of( TSD, outdata2 ) ; |
| 812 |
len2 = outdata2->len ; |
| 813 |
} |
| 814 |
else |
| 815 |
{ |
| 816 |
str2 = (char *)MallocTSD( 1 ) ; |
| 817 |
str2[0] = '\0' ; |
| 818 |
len2 = 0 ; |
| 819 |
} |
| 820 |
|
| 821 |
rcode = IfcDoExit( TSD, code, len1, str1, len2, str2, NULL, NULL) ; |
| 822 |
FreeTSD( str1 ) ; |
| 823 |
FreeTSD( str2 ) ; |
| 824 |
if (rcode==RX_HOOK_ERROR) |
| 825 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 826 |
else if (rcode==RX_HOOK_GO_ON) |
| 827 |
rcode = HOOK_GO_ON ; |
| 828 |
else if (rcode==RX_HOOK_NOPE) |
| 829 |
rcode = HOOK_NOPE ; |
| 830 |
else |
| 831 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 832 |
|
| 833 |
return rcode ; |
| 834 |
} |
| 835 |
|
| 836 |
int hookup_input( tsd_t *TSD, int hook, streng **indata ) |
| 837 |
{ |
| 838 |
int rcode=0, code=0 ; |
| 839 |
/* The following value allows called programs to call "free" to the return |
| 840 |
* parameters without destroying our stack. |
| 841 |
*/ |
| 842 |
#define ILLEGAL_USE_SIZE (8 * sizeof(void *)) |
| 843 |
char RetBuf[ILLEGAL_USE_SIZE + 256] ; |
| 844 |
int retlen=256 ; |
| 845 |
char *retstr=RetBuf; |
| 846 |
|
| 847 |
code = MapHook( TSD, hook ); |
| 848 |
/* assert (code == RX_EXIT_PULL || code == RX_EXIT_TRCIN ); */ |
| 849 |
|
| 850 |
strcpy(RetBuf,""); |
| 851 |
/* FIXME, FGC: The documentation says that the input of a PULL or TRCIN is |
| 852 |
* undefined. Why do we provide a buffer? |
| 853 |
*/ |
| 854 |
rcode = IfcDoExit( TSD, code, 0, NULL, 0, NULL, &retlen, &retstr ) ; |
| 855 |
if (rcode==RX_HOOK_ERROR) |
| 856 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 857 |
else if (rcode==RX_HOOK_GO_ON) |
| 858 |
rcode = HOOK_GO_ON ; |
| 859 |
else if (rcode==RX_HOOK_NOPE) |
| 860 |
rcode = HOOK_NOPE ; |
| 861 |
else |
| 862 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 863 |
|
| 864 |
*indata = wrapstring( TSD, retstr, retlen ) ; |
| 865 |
FreeTSD( retstr ) ; /* retstr was always newly allocated */ |
| 866 |
#undef ILLEGAL_USE_SIZE |
| 867 |
return rcode ; |
| 868 |
} |
| 869 |
|
| 870 |
int hookup_input_output( tsd_t *TSD, int hook, const streng *outdata, streng **indata ) |
| 871 |
{ |
| 872 |
char *str; |
| 873 |
int len = 0; |
| 874 |
int rcode=0, code=0 ; |
| 875 |
/* The following value allows called programs to call "free" to the return |
| 876 |
* parameters without destroying our stack. |
| 877 |
*/ |
| 878 |
#define ILLEGAL_USE_SIZE (8 * sizeof(void *)) |
| 879 |
char RetBuf[ILLEGAL_USE_SIZE + 256] ; |
| 880 |
int retlen=256 ; |
| 881 |
char *retstr=RetBuf; |
| 882 |
|
| 883 |
code = MapHook( TSD, hook ); |
| 884 |
/* assert (code == RX_EXIT_GETENV );*/ |
| 885 |
|
| 886 |
if (outdata) |
| 887 |
{ |
| 888 |
str = str_of( TSD, outdata ) ; |
| 889 |
len = outdata->len ; |
| 890 |
} |
| 891 |
else |
| 892 |
{ |
| 893 |
str = (char *)MallocTSD( 1 ) ; |
| 894 |
str[0] = '\0' ; |
| 895 |
len = 0 ; |
| 896 |
} |
| 897 |
|
| 898 |
strcpy(RetBuf,""); |
| 899 |
/* FIXME, FGC: The documentation says that the input of a PULL or TRCIN is |
| 900 |
* undefined. Why do we provide a buffer? |
| 901 |
*/ |
| 902 |
rcode = IfcDoExit( TSD, code, len, str, 0, NULL, &retlen, &retstr ) ; |
| 903 |
FreeTSD( str ) ; |
| 904 |
if (rcode==RX_HOOK_ERROR) |
| 905 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 906 |
else if (rcode==RX_HOOK_GO_ON) |
| 907 |
rcode = HOOK_GO_ON ; |
| 908 |
else if (rcode==RX_HOOK_NOPE) |
| 909 |
rcode = HOOK_NOPE ; |
| 910 |
else |
| 911 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 912 |
|
| 913 |
*indata = wrapstring( TSD, retstr, retlen ) ; |
| 914 |
FreeTSD( retstr ) ; /* retstr was always newly allocated */ |
| 915 |
#undef ILLEGAL_USE_SIZE |
| 916 |
return rcode ; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
streng *SubCom( tsd_t *TSD, const streng *command, const streng *envir, int *rc ) |
| 922 |
{ |
| 923 |
int tmplen ; |
| 924 |
char *tmpptr ; |
| 925 |
streng *retval ; |
| 926 |
|
| 927 |
*rc = IfcSubCmd( TSD, |
| 928 |
envir ? envir->len : RX_NO_STRING, |
| 929 |
envir ? envir->value : NULL, |
| 930 |
command->len, command->value, &tmplen, &tmpptr ) ; |
| 931 |
|
| 932 |
retval = wrapstring( TSD, tmpptr, tmplen ) ; |
| 933 |
FreeTSD( tmpptr ) ; |
| 934 |
return retval ; |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
/**************************************************************************** |
| 939 |
* |
| 940 |
* JH 13/12/1999 Trying to satisfy both BUG031 and BUG022. |
| 941 |
* |
| 942 |
* BUG022 To make Direct setting of stems Direct and not Symbolic. |
| 943 |
* - Added new parameter, "Code". Assumed values are RX_GETSVAR, and |
| 944 |
* RX_GETVAR. They denote that the actiion is symbolic, or direct |
| 945 |
* (respectively). |
| 946 |
* - if direct call getdirvalue() |
| 947 |
* |
| 948 |
* BUG031 |
| 949 |
* - if default value is being returned, uppercase the whole name for |
| 950 |
* symbolics, and only the stem for directs. This should be a moot |
| 951 |
* issue, as there is code in variable.c to return default values. |
| 952 |
* |
| 953 |
****************************************************************************/ |
| 954 |
static int GetVariable( tsd_t *TSD, int Code, int *Lengths, char *Strings[], |
| 955 |
int *allocated) |
| 956 |
{ |
| 957 |
const streng *value; |
| 958 |
streng *varbl; |
| 959 |
char *retval; |
| 960 |
int i,len,state; |
| 961 |
|
| 962 |
varbl = wrapstring( TSD, Strings[0], Lengths[0] ); |
| 963 |
|
| 964 |
if ( !varbl ) |
| 965 |
{ |
| 966 |
Lengths[1] = RX_NO_STRING; |
| 967 |
return RX_CODE_INVNAME; |
| 968 |
} |
| 969 |
|
| 970 |
if ( !valid_var_symbol( varbl ) ) |
| 971 |
{ |
| 972 |
Free_stringTSD( varbl ); |
| 973 |
Lengths[1] = RX_NO_STRING; |
| 974 |
return RX_CODE_INVNAME; |
| 975 |
} |
| 976 |
|
| 977 |
state = variables_per_SAA( TSD ); |
| 978 |
if ( Code == RX_GETSVAR ) |
| 979 |
value = getvalue( TSD, varbl, -1 ); |
| 980 |
else |
| 981 |
value = getdirvalue( TSD, varbl ); |
| 982 |
restore_variable_state( TSD, state ); |
| 983 |
|
| 984 |
/* |
| 985 |
* getvalue returns varbl or a temporary value on NOVALUE, don't free varbl |
| 986 |
* until the final use of value and varbl. |
| 987 |
*/ |
| 988 |
if ( var_was_found( TSD ) ) |
| 989 |
{ |
| 990 |
Lengths[1] = value->len; |
| 991 |
Strings[1] = (char *) value->value; |
| 992 |
*allocated = 0; |
| 993 |
Free_stringTSD( varbl ); |
| 994 |
return RX_CODE_OK; |
| 995 |
} |
| 996 |
|
| 997 |
Lengths[1] = len = value->len; |
| 998 |
Strings[1] = retval = (char *)MallocTSD( len ); |
| 999 |
memcpy( retval, value->value, len ); |
| 1000 |
*allocated = 2; |
| 1001 |
Free_stringTSD( varbl ); |
| 1002 |
|
| 1003 |
if ( Code == RX_GETSVAR ) |
| 1004 |
{ |
| 1005 |
mem_upper( retval, len ); |
| 1006 |
} |
| 1007 |
else |
| 1008 |
{ |
| 1009 |
/* JH 13-12-99 If this is a direct variable, only uppercase the |
| 1010 |
* stem name. |
| 1011 |
*/ |
| 1012 |
/* |
| 1013 |
* Bypass the dot test for the first character to allow reserved |
| 1014 |
* variables. |
| 1015 |
*/ |
| 1016 |
if ( len ) |
| 1017 |
retval[0] = (unsigned char) rx_toupper( retval[0] ); |
| 1018 |
for( i = 1; ( i < len ) && ( '.' != retval[i] ); i++ ) |
| 1019 |
retval[i] = (unsigned char) rx_toupper( retval[i] ); |
| 1020 |
} |
| 1021 |
return RX_CODE_NOVALUE; |
| 1022 |
} |
| 1023 |
|
| 1024 |
/**************************************************************************** |
| 1025 |
* |
| 1026 |
* JH 13/12/1999 Trying to satisfy both BUG031 and BUG022. |
| 1027 |
* |
| 1028 |
* BUG022 To make Direct setting of stems Direct and not Symbolic. |
| 1029 |
* - Added new parameter, "Code". Assumed values are RX_SETSVAR, and |
| 1030 |
* RX_SETVAR. They denote that the actiion is symbolic, or direct |
| 1031 |
* (respectively). |
| 1032 |
* Until this point setting and dropping variables have been treated as |
| 1033 |
* the same. If there is a value, then it is a set, otherwise drop it. |
| 1034 |
* - if direct call setdirvalue() |
| 1035 |
* - if direct call drop_dirvar() |
| 1036 |
* BUG031 |
| 1037 |
* - uppercase the whole name for symbolics, and only the stem for directs. |
| 1038 |
* |
| 1039 |
****************************************************************************/ |
| 1040 |
static int SetVariable( tsd_t *TSD, int Code, int *Lengths, char *Strings[] ) |
| 1041 |
{ |
| 1042 |
streng *varbl,*varname,*value; |
| 1043 |
int rcode,i,state; |
| 1044 |
|
| 1045 |
varbl = wrapstring( TSD, Strings[0], Lengths[0] ); |
| 1046 |
|
| 1047 |
if ( varbl == NULL ) |
| 1048 |
{ |
| 1049 |
/* |
| 1050 |
* If the variable name is empty, return an error |
| 1051 |
*/ |
| 1052 |
return RX_CODE_INVNAME; |
| 1053 |
} |
| 1054 |
|
| 1055 |
if ( Code == RX_SETSVAR ) |
| 1056 |
varname = Str_upper( Str_dupTSD( varbl ) ); |
| 1057 |
else |
| 1058 |
{ |
| 1059 |
/* |
| 1060 |
* Setting reserved variables is not allowed :-( |
| 1061 |
*/ |
| 1062 |
if ( varbl->len && varbl->value[0] == '.' ) |
| 1063 |
{ |
| 1064 |
Free_stringTSD( varbl ) ; |
| 1065 |
return RX_CODE_INVNAME; |
| 1066 |
} |
| 1067 |
varname = Str_dupTSD( varbl ); |
| 1068 |
/* |
| 1069 |
* Bypass the dot test for the first character to allow reserved |
| 1070 |
* variables. |
| 1071 |
* This bypass is not relevant anymore. See comment above. |
| 1072 |
*/ |
| 1073 |
if ( varname->len ) |
| 1074 |
varname->value[0] = (unsigned char) rx_toupper( varname->value[0] ); |
| 1075 |
for( i = 1; ( i < varname->len ) && ( '.' != varname->value[i] ); i++ ) |
| 1076 |
{ |
| 1077 |
varname->value[i] = (unsigned char) rx_toupper( varname->value[i] ); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
Free_stringTSD( varbl ) ; |
| 1081 |
varbl = NULL; /* For debugging purpose only */ |
| 1082 |
|
| 1083 |
if ( !valid_var_symbol( varname ) ) |
| 1084 |
{ |
| 1085 |
Free_stringTSD( varname ); |
| 1086 |
return RX_CODE_INVNAME; |
| 1087 |
} |
| 1088 |
|
| 1089 |
value = wrapstring( TSD, Strings[1], Lengths[1] ); |
| 1090 |
|
| 1091 |
state = variables_per_SAA( TSD ); |
| 1092 |
rcode = RX_CODE_OK; /* default value */ |
| 1093 |
if ( value ) |
| 1094 |
{ |
| 1095 |
if ( Code == RX_SETSVAR ) |
| 1096 |
setvalue( TSD, varname, value, -1 ); |
| 1097 |
else |
| 1098 |
setdirvalue( TSD, varname, value ); |
| 1099 |
} |
| 1100 |
else |
| 1101 |
{ |
| 1102 |
if ( Code == RX_SETSVAR ) |
| 1103 |
drop_var( TSD, varname ); |
| 1104 |
else |
| 1105 |
drop_dirvar( TSD, varname ); |
| 1106 |
} |
| 1107 |
restore_variable_state( TSD, state ); |
| 1108 |
|
| 1109 |
if ( !var_was_found( TSD ) ) |
| 1110 |
rcode = RX_CODE_NOVALUE; |
| 1111 |
|
| 1112 |
Free_stringTSD( varname ); |
| 1113 |
return rcode; |
| 1114 |
} |
| 1115 |
|
| 1116 |
static int handle_version( int *Length, char **String ) |
| 1117 |
{ |
| 1118 |
*Length = strlen(PARSE_VERSION_STRING) ; |
| 1119 |
*String = PARSE_VERSION_STRING ; |
| 1120 |
return RX_CODE_OK ; |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
static int handle_queue( const tsd_t *TSD, int *Length, char **String ) |
| 1125 |
{ |
| 1126 |
fill_queue_name( TSD, Length, String ); |
| 1127 |
return RX_CODE_OK ; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
/**************************************************************************** |
| 1132 |
* |
| 1133 |
* JH 13/12/1999 |
| 1134 |
* |
| 1135 |
* BUG022 To make Direct setting of stems Direct and not Symbolic. |
| 1136 |
* - Added checks for the direct variable functions RX_GETVAR and RX_SETVAR. |
| 1137 |
* |
| 1138 |
* *allocated will be set, if the returned value(s) is/are allocated and |
| 1139 |
* should be freed after the use. |
| 1140 |
* |
| 1141 |
****************************************************************************/ |
| 1142 |
int IfcVarPool( tsd_t *TSD, int Code, int *Lengths, char *Strings[], |
| 1143 |
int *allocated ) |
| 1144 |
{ |
| 1145 |
int rc=0 ; |
| 1146 |
|
| 1147 |
*allocated = 0; |
| 1148 |
if (Code==RX_GETSVAR ||Code==RX_GETVAR ) |
| 1149 |
rc = GetVariable( TSD, Code, Lengths, Strings, allocated ) ; |
| 1150 |
else if (Code==RX_SETSVAR ||Code==RX_SETVAR ) |
| 1151 |
rc = SetVariable( TSD, Code, Lengths, Strings ) ; |
| 1152 |
else if (Code==RX_NEXTVAR) |
| 1153 |
rc = get_next_var( TSD, Lengths, Strings, allocated ) ; |
| 1154 |
else if (Code==RX_CODE_VERSION) |
| 1155 |
rc = handle_version( Lengths, Strings ) ; |
| 1156 |
else if (Code==RX_CODE_SOURCE) |
| 1157 |
rc = handle_source( TSD, Lengths, Strings, allocated ) ; |
| 1158 |
else if (Code==RX_CODE_QUEUE) |
| 1159 |
rc = handle_queue( TSD, Lengths, Strings ); |
| 1160 |
else if (Code==RX_CODE_PARAMS) |
| 1161 |
rc = handle_no_of_params( TSD, Lengths, Strings ) ; |
| 1162 |
else if (Code==RX_CODE_PARAM) |
| 1163 |
rc = handle_param( TSD, Lengths, Strings ) ; |
| 1164 |
else |
| 1165 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 1166 |
|
| 1167 |
return rc ; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* Removes ct->Strings and ct->Lengths AND ct->ExternalName. */ |
| 1171 |
static void RemoveParams(const tsd_t *TSD) |
| 1172 |
{ |
| 1173 |
int i; |
| 1174 |
cli_tsd_t *ct; |
| 1175 |
|
| 1176 |
ct = (cli_tsd_t *)TSD->cli_tsd; |
| 1177 |
|
| 1178 |
if ( ct->Strings && ct->Lengths ) |
| 1179 |
{ |
| 1180 |
for (i = 0;i < ct->StringsCount;i++) /* The last one is always NULL */ |
| 1181 |
{ |
| 1182 |
if ((ct->Lengths[i] != RX_NO_STRING) && |
| 1183 |
(ct->Strings[i] != NULL)) |
| 1184 |
FreeTSD( ct->Strings[i] ); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
if ( ct->Lengths ) |
| 1189 |
FreeTSD( ct->Lengths ) ; |
| 1190 |
if ( ct->Strings ) |
| 1191 |
FreeTSD( ct->Strings ) ; |
| 1192 |
if ( ct->ExternalName ) |
| 1193 |
FreeTSD( ct->ExternalName ) ; |
| 1194 |
|
| 1195 |
ct->StringsCount = -1; |
| 1196 |
ct->Strings = NULL; |
| 1197 |
ct->Lengths = NULL; |
| 1198 |
ct->ExternalName = NULL; |
| 1199 |
} |
| 1200 |
|
| 1201 |
/* Creates (possibly after a removal of old values) ct->Strings and ct->Lengths |
| 1202 |
* from the given parms. One hidden NULL string is appended. |
| 1203 |
*/ |
| 1204 |
static void MakeParams(const tsd_t *TSD, cparamboxptr parms) |
| 1205 |
{ |
| 1206 |
int i; |
| 1207 |
cparamboxptr p=NULL ; |
| 1208 |
cli_tsd_t *ct; |
| 1209 |
|
| 1210 |
ct = (cli_tsd_t *)TSD->cli_tsd; |
| 1211 |
|
| 1212 |
/* Cleanup the old parameters before we set StringsCount */ |
| 1213 |
RemoveParams(TSD); |
| 1214 |
|
| 1215 |
ct->StringsCount = 0; /* This is the default in case of unused parameters */ |
| 1216 |
/* Detect the index of the last valid parameter */ |
| 1217 |
for (i=0,p=parms; p; p=p->next,i++) |
| 1218 |
{ |
| 1219 |
if (p->value) |
| 1220 |
ct->StringsCount=i+1 ; |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* add one NULL string at the end */ |
| 1224 |
ct->Lengths = (int *)MallocTSD( sizeof(int) * (ct->StringsCount+1) ) ; |
| 1225 |
ct->Strings = (char **)MallocTSD( sizeof(char*) * (ct->StringsCount+1) ) ; |
| 1226 |
|
| 1227 |
for (i=0,p=parms; i < ct->StringsCount; p=p->next,i++) |
| 1228 |
{ |
| 1229 |
if (p->value) |
| 1230 |
{ |
| 1231 |
ct->Lengths[i] = Str_len( p->value ) ; |
| 1232 |
ct->Strings[i] = str_of( TSD, p->value ); |
| 1233 |
} |
| 1234 |
else |
| 1235 |
{ |
| 1236 |
ct->Lengths[i] = RX_NO_STRING ; |
| 1237 |
ct->Strings[i] = NULL ; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
/* Provide a hidden NULL string at the end */ |
| 1242 |
ct->Lengths[ct->StringsCount] = RX_NO_STRING ; |
| 1243 |
ct->Strings[ct->StringsCount] = NULL ; |
| 1244 |
} |
| 1245 |
|
| 1246 |
/* do_an_external calls IfcExecFunc with the appropriate parameters. Basically |
| 1247 |
* it wraps the parameters. Either ExeName or box must be NULL. |
| 1248 |
*/ |
| 1249 |
static streng *do_an_external( tsd_t *TSD, |
| 1250 |
const streng *ExeName, |
| 1251 |
const struct entry_point *box, |
| 1252 |
cparamboxptr parms, |
| 1253 |
char exitonly, |
| 1254 |
char called ) |
| 1255 |
{ |
| 1256 |
int RetLength=0 ; |
| 1257 |
char *RetString=NULL ; |
| 1258 |
streng *retval ; |
| 1259 |
streng *queue; |
| 1260 |
int rc ; |
| 1261 |
int RC ; |
| 1262 |
PFN Func; |
| 1263 |
cli_tsd_t *ct; |
| 1264 |
void *gci_info; |
| 1265 |
volatile char *tmpExternalName; /* used to save ct->ExternalName */ |
| 1266 |
/* when erroring */ |
| 1267 |
|
| 1268 |
ct = (cli_tsd_t *)TSD->cli_tsd; |
| 1269 |
|
| 1270 |
MakeParams( TSD, parms ) ; |
| 1271 |
if (ExeName) |
| 1272 |
{ |
| 1273 |
ct->ExternalName = str_of( TSD, ExeName ); |
| 1274 |
tmpExternalName = tmpstr_of( TSD, ExeName ); |
| 1275 |
Func = NULL; |
| 1276 |
gci_info = NULL; |
| 1277 |
} |
| 1278 |
else |
| 1279 |
{ |
| 1280 |
ct->ExternalName = str_of( TSD, box->name ); |
| 1281 |
tmpExternalName = tmpstr_of( TSD, box->name ); |
| 1282 |
Func = box->addr; |
| 1283 |
gci_info = box->special.gci_info; |
| 1284 |
} |
| 1285 |
|
| 1286 |
queue = get_queue( TSD ); |
| 1287 |
|
| 1288 |
rc = IfcExecFunc( TSD, Func, ct->ExternalName, |
| 1289 |
ct->StringsCount, ct->Lengths, ct->Strings, |
| 1290 |
Str_len( queue ), Str_val( queue ), |
| 1291 |
&RetLength, &RetString, &RC, called, gci_info ); |
| 1292 |
|
| 1293 |
Free_stringTSD( queue ); |
| 1294 |
RemoveParams( TSD ) ; |
| 1295 |
|
| 1296 |
if (RC) |
| 1297 |
{ |
| 1298 |
switch( RC ) |
| 1299 |
{ |
| 1300 |
case ERR_ROUTINE_NOT_FOUND: |
| 1301 |
exiterror( ERR_ROUTINE_NOT_FOUND, 1, tmpExternalName ); |
| 1302 |
break; |
| 1303 |
default: |
| 1304 |
exiterror( RC, 0) ; |
| 1305 |
break; |
| 1306 |
} |
| 1307 |
retval = NULL ; |
| 1308 |
} |
| 1309 |
else if ( RetLength == RX_NO_STRING ) |
| 1310 |
{ |
| 1311 |
/* |
| 1312 |
* ERR_NO_DATA_RETURNED is detected later in expr.c. |
| 1313 |
*/ |
| 1314 |
retval = NULL; |
| 1315 |
} |
| 1316 |
else |
| 1317 |
{ |
| 1318 |
retval = Str_makeTSD( RetLength ) ; |
| 1319 |
memcpy( retval->value, RetString, RetLength ) ; |
| 1320 |
FreeTSD( RetString ); |
| 1321 |
retval->len = RetLength ; |
| 1322 |
} |
| 1323 |
return retval ; |
| 1324 |
|
| 1325 |
} |
| 1326 |
|
| 1327 |
streng *call_unknown_external( tsd_t *TSD, const streng *name, cparamboxptr parms, char called ) |
| 1328 |
{ |
| 1329 |
assert(name); |
| 1330 |
|
| 1331 |
return( do_an_external( TSD, name, NULL, parms, 1, called ) ) ; |
| 1332 |
} |
| 1333 |
|
| 1334 |
streng *call_known_external( tsd_t *TSD, const struct entry_point *vbox, cparamboxptr parms, char called ) |
| 1335 |
{ |
| 1336 |
assert(vbox); |
| 1337 |
|
| 1338 |
return( do_an_external( TSD, NULL, vbox, parms, 0, called ) ) ; |
| 1339 |
} |
| 1340 |
|
| 1341 |
int IfcCreateQueue( tsd_t *TSD, const char *qname, const int qlen, char *data, unsigned long *dupflag, unsigned long buflen ) |
| 1342 |
{ |
| 1343 |
streng *queuename=NULL,*strdata; |
| 1344 |
int rc; |
| 1345 |
|
| 1346 |
if ( qname ) |
| 1347 |
{ |
| 1348 |
queuename = Str_makeTSD( qlen ) ; |
| 1349 |
memcpy( queuename->value, qname, qlen ) ; |
| 1350 |
queuename->len = qlen; |
| 1351 |
} |
| 1352 |
rc = create_queue( TSD, queuename, &strdata ); |
| 1353 |
|
| 1354 |
if ( rc == 0 || rc == 1 ) |
| 1355 |
{ |
| 1356 |
/* |
| 1357 |
* Return the new queue name |
| 1358 |
*/ |
| 1359 |
memcpy( data, strdata->value, min( strdata->len, (int) buflen - 1 ) ); |
| 1360 |
data[min( strdata->len, (int) buflen - 1)] = '\0'; |
| 1361 |
/* |
| 1362 |
* If the returned queue name is different to |
| 1363 |
* the one we requested, set the dupflag |
| 1364 |
*/ |
| 1365 |
if ( queuename |
| 1366 |
&& rc == 1 ) |
| 1367 |
*dupflag = 1; |
| 1368 |
else |
| 1369 |
*dupflag = 0; |
| 1370 |
FreeTSD( strdata ); |
| 1371 |
rc = 0; |
| 1372 |
} |
| 1373 |
if ( queuename) Free_stringTSD( queuename ); |
| 1374 |
return rc; |
| 1375 |
} |
| 1376 |
|
| 1377 |
int IfcDeleteQueue( tsd_t *TSD, const char *qname, const int qlen ) |
| 1378 |
{ |
| 1379 |
streng *queuename; |
| 1380 |
int rc; |
| 1381 |
|
| 1382 |
queuename = Str_makeTSD( qlen ) ; |
| 1383 |
memcpy( queuename->value, qname, qlen ) ; |
| 1384 |
queuename->len = qlen; |
| 1385 |
rc = delete_queue( TSD, queuename ); |
| 1386 |
Free_stringTSD( queuename ); |
| 1387 |
return rc; |
| 1388 |
} |
| 1389 |
|
| 1390 |
int IfcQueryQueue( tsd_t *TSD, const char *qname, const int qlen, unsigned long *count ) |
| 1391 |
{ |
| 1392 |
streng *queuename; |
| 1393 |
int rc; |
| 1394 |
|
| 1395 |
queuename = Str_makeTSD( qlen ) ; |
| 1396 |
memcpy( queuename->value, qname, qlen ) ; |
| 1397 |
queuename->len = qlen; |
| 1398 |
rc = lines_in_stack( TSD, queuename ); |
| 1399 |
if ( rc < 0 ) |
| 1400 |
rc = -rc; |
| 1401 |
else |
| 1402 |
{ |
| 1403 |
*count = rc; |
| 1404 |
rc = 0; |
| 1405 |
} |
| 1406 |
Free_stringTSD( queuename ); |
| 1407 |
return rc; |
| 1408 |
} |
| 1409 |
|
| 1410 |
int IfcAddQueue( tsd_t *TSD, const char *qname, const int qlen, const char *data, const int datalen, unsigned long addlifo ) |
| 1411 |
{ |
| 1412 |
streng *queuename,*strdata; |
| 1413 |
int rc; |
| 1414 |
|
| 1415 |
queuename = Str_makeTSD( qlen ) ; |
| 1416 |
memcpy( queuename->value, qname, qlen ) ; |
| 1417 |
queuename->len = qlen; |
| 1418 |
|
| 1419 |
strdata = Str_makeTSD( datalen ) ; |
| 1420 |
memcpy( strdata->value, data, datalen ) ; |
| 1421 |
strdata->len = datalen; |
| 1422 |
|
| 1423 |
if ( addlifo ) |
| 1424 |
rc = stack_lifo( TSD, strdata, queuename ); |
| 1425 |
else |
| 1426 |
rc = stack_fifo( TSD, strdata, queuename ); |
| 1427 |
Free_stringTSD( queuename ); |
| 1428 |
return rc; |
| 1429 |
} |
| 1430 |
|
| 1431 |
int IfcPullQueue( tsd_t *TSD, const char *qname, const int qlen, char **data, unsigned long *datalen, unsigned long waitforline ) |
| 1432 |
{ |
| 1433 |
streng *queuename,*strdata; |
| 1434 |
int rc, len; |
| 1435 |
char *p; |
| 1436 |
|
| 1437 |
queuename = Str_makeTSD( qlen ) ; |
| 1438 |
memcpy( queuename->value, qname, qlen ) ; |
| 1439 |
queuename->len = qlen; |
| 1440 |
|
| 1441 |
strdata = popline( TSD, queuename, &rc, waitforline ); |
| 1442 |
|
| 1443 |
if ( rc == 0 ) |
| 1444 |
{ |
| 1445 |
if ( strdata == NULL ) |
| 1446 |
{ |
| 1447 |
/* |
| 1448 |
* Queue is empty |
| 1449 |
*/ |
| 1450 |
rc = 8; /* RXQUEUE_EMPTY */ |
| 1451 |
} |
| 1452 |
else |
| 1453 |
{ |
| 1454 |
len = strdata->len; |
| 1455 |
|
| 1456 |
if ( ( *data == NULL ) || ( *datalen <= (unsigned long) len ) ) |
| 1457 |
p = (char *)IfcAllocateMemory( len + 1 ); |
| 1458 |
else |
| 1459 |
p = *data; |
| 1460 |
if ( p == NULL ) |
| 1461 |
rc = 12; /* RXQUEUE_MEMFAIL */ |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
/* |
| 1465 |
* Using the temporary p inhibits use of *data until it's sure |
| 1466 |
* that we return 0. |
| 1467 |
*/ |
| 1468 |
*data = p; |
| 1469 |
memcpy( *data, strdata->value, len ); |
| 1470 |
(*data)[len] = '\0'; |
| 1471 |
*datalen = len; |
| 1472 |
} |
| 1473 |
Free_stringTSD( strdata ); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
Free_stringTSD( queuename ); |
| 1477 |
return rc; |
| 1478 |
} |