| 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 |
|
| 20 |
#include "rexx.h" |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#if defined(VMS) |
| 24 |
# define fork() vfork() |
| 25 |
/* |
| 26 |
* At least with OpenVMS 7.3-1 on Alpha, the Posix way seems to work. |
| 27 |
* So there is no need to redirect on (now) bogus code. |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifdef VMS_DO_COMMAND |
| 31 |
# ifdef posix_do_command |
| 32 |
# undef posix_do_command |
| 33 |
# endif |
| 34 |
# define posix_do_command __regina_vms_do_command |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
|
| 38 |
/* |
| 39 |
* The following strings must match AddressWithType enums in regina_t.h |
| 40 |
*/ |
| 41 |
static char *env_type[] = { "NORMAL", "STREAM", "STEM", "LIFO", "FIFO" }; |
| 42 |
|
| 43 |
struct envir |
| 44 |
{ |
| 45 |
environment e ; |
| 46 |
int type ; |
| 47 |
struct envir *next, *prev ; |
| 48 |
}; |
| 49 |
|
| 50 |
/* clear_environpart sets initial values of an environpart. |
| 51 |
* attention: cleanup_environpart() closes open file handles and removes |
| 52 |
* temporary files, this function not. |
| 53 |
*/ |
| 54 |
static void clear_environpart(environpart *ep) |
| 55 |
{ |
| 56 |
ep->currnum = -1 ; |
| 57 |
ep->maxnum = -1 ; |
| 58 |
ep->SameAsOutput = 0; |
| 59 |
ep->FileRedirected = 0; |
| 60 |
ep->tempname = NULL ; |
| 61 |
ep->tmp_queue = NULL ; |
| 62 |
ep->queue = NULL ; |
| 63 |
ep->type = 0 ; |
| 64 |
ep->hdls[2] = ep->hdls[1] = ep->hdls[0] = -1; |
| 65 |
} |
| 66 |
|
| 67 |
void add_envir( tsd_t *TSD, const streng *name, int type, int subtype ) |
| 68 |
/* an environment with the same name can be added more than once! */ |
| 69 |
{ |
| 70 |
struct envir *ptr; |
| 71 |
|
| 72 |
ptr = (struct envir *)MallocTSD( sizeof( struct envir ) ); |
| 73 |
memset( &ptr->e, 0, sizeof(ptr->e) ); |
| 74 |
clear_environpart( &ptr->e.input ); |
| 75 |
clear_environpart( &ptr->e.output ); |
| 76 |
clear_environpart( &ptr->e.error ); |
| 77 |
ptr->e.input.flags.isinput = 1; |
| 78 |
ptr->e.error.flags.iserror = 1; |
| 79 |
|
| 80 |
ptr->e.name = Str_dupTSD( name ); |
| 81 |
ptr->e.subtype = subtype; |
| 82 |
ptr->e.subcomed = 0; |
| 83 |
ptr->type = type; |
| 84 |
ptr->prev = (struct envir *)TSD->firstenvir; |
| 85 |
ptr->next = NULL; |
| 86 |
TSD->firstenvir = ptr; |
| 87 |
if ( ptr->prev ) |
| 88 |
ptr->prev->next = ptr; |
| 89 |
} |
| 90 |
|
| 91 |
#ifdef TRACEMEM |
| 92 |
static void markenvir( const tsd_t *TSD ) |
| 93 |
{ |
| 94 |
struct envir *eptr=NULL ; |
| 95 |
|
| 96 |
eptr = (struct envir *) TSD->firstenvir ; |
| 97 |
for (; eptr; eptr=eptr->prev ) |
| 98 |
{ |
| 99 |
markmemory( eptr, TRC_ENVIRBOX ) ; |
| 100 |
markmemory( eptr->e.name, TRC_ENVIRNAME ) ; |
| 101 |
if (eptr->e.input.name) |
| 102 |
markmemory( eptr->e.input.name, TRC_ENVIRNAME ) ; |
| 103 |
if (eptr->e.output.name) |
| 104 |
markmemory( eptr->e.output.name, TRC_ENVIRNAME ) ; |
| 105 |
if (eptr->e.error.name) |
| 106 |
markmemory( eptr->e.error.name, TRC_ENVIRNAME ) ; |
| 107 |
} |
| 108 |
} |
| 109 |
#endif /* TRACEMEM */ |
| 110 |
|
| 111 |
|
| 112 |
static struct envir *find_envir( const tsd_t *TSD, const streng *name ) |
| 113 |
{ |
| 114 |
struct envir *ptr=NULL ; |
| 115 |
|
| 116 |
for ( ptr = (struct envir *)TSD->firstenvir; ptr; ptr = ptr->prev ) |
| 117 |
{ |
| 118 |
if ( !Str_cmp( ptr->e.name, name ) ) |
| 119 |
return ptr ; |
| 120 |
} |
| 121 |
return NULL ; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* This function is used to determine if an environment exists. It is the |
| 126 |
* external equivalent of find_envir() |
| 127 |
*/ |
| 128 |
int envir_exists( const tsd_t *TSD, const streng *name ) |
| 129 |
{ |
| 130 |
if ( find_envir( TSD, name ) == NULL ) |
| 131 |
return 0; |
| 132 |
else |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* This function returns textual information about the current environment. |
| 138 |
* Suitable output for ADDRESS BIF |
| 139 |
*/ |
| 140 |
streng *get_envir_details( const tsd_t *TSD, char opt, const streng *name ) |
| 141 |
{ |
| 142 |
struct envir *ptr=NULL ; |
| 143 |
streng *result, *env_resource, *raw_resource; |
| 144 |
char *env_position; |
| 145 |
int awt, ant, len; |
| 146 |
|
| 147 |
ptr = find_envir( TSD, name ); |
| 148 |
switch( opt ) |
| 149 |
{ |
| 150 |
case 'I': |
| 151 |
env_position = (char *)"INPUT"; |
| 152 |
awt = ptr->e.input.flags.awt; |
| 153 |
ant = ptr->e.input.flags.ant; |
| 154 |
raw_resource = ptr->e.input.name; |
| 155 |
break; |
| 156 |
case 'O': |
| 157 |
env_position = (char *)( (ptr->e.output.flags.append) ? "APPEND" : "REPLACE" ); |
| 158 |
awt = ptr->e.output.flags.awt; |
| 159 |
ant = ptr->e.output.flags.ant; |
| 160 |
raw_resource = ptr->e.output.name; |
| 161 |
break; |
| 162 |
case 'E': |
| 163 |
env_position = (char *)( (ptr->e.error.flags.append) ? "APPEND" : "REPLACE" ); |
| 164 |
awt = ptr->e.error.flags.awt; |
| 165 |
ant = ptr->e.error.flags.ant; |
| 166 |
raw_resource = ptr->e.error.name; |
| 167 |
break; |
| 168 |
default: |
| 169 |
/* |
| 170 |
* Should not get here!! |
| 171 |
*/ |
| 172 |
awt = ant = 0; |
| 173 |
env_position = NULL; |
| 174 |
raw_resource = NULL; |
| 175 |
break; |
| 176 |
} |
| 177 |
/* |
| 178 |
* Determine the resource string... |
| 179 |
*/ |
| 180 |
if ( raw_resource == NULL ) |
| 181 |
{ |
| 182 |
env_resource = nullstringptr(); |
| 183 |
} |
| 184 |
else if ( awt == awtSTEM ) |
| 185 |
{ |
| 186 |
env_resource = raw_resource; |
| 187 |
} |
| 188 |
else if ( ant == antSTRING ) |
| 189 |
{ |
| 190 |
if ( raw_resource == NULL ) |
| 191 |
{ |
| 192 |
env_resource = nullstringptr(); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
env_resource = raw_resource; |
| 197 |
} |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
/* |
| 202 |
* Get the value of the symbol... |
| 203 |
*/ |
| 204 |
env_resource = (streng *)getdirvalue( (tsd_t *)TSD, raw_resource ); |
| 205 |
} |
| 206 |
len = 3 + strlen( env_position) + strlen( env_type[awt] ) + env_resource->len; |
| 207 |
result = Str_makeTSD( len ); |
| 208 |
result = Str_catstrTSD( result, env_position ); |
| 209 |
result = Str_catstrTSD( result, " " ); |
| 210 |
result = Str_catstrTSD( result, env_type[awt] ); |
| 211 |
if ( env_resource->len ) |
| 212 |
{ |
| 213 |
result = Str_catstrTSD( result, " " ); |
| 214 |
result = Str_catTSD( result, env_resource ); |
| 215 |
} |
| 216 |
return result; |
| 217 |
} |
| 218 |
|
| 219 |
/* |
| 220 |
* This function is used to set the subcomed flag in an environment exists. |
| 221 |
*/ |
| 222 |
int set_subcomed_envir( const tsd_t *TSD, const streng *name, int subcomed ) |
| 223 |
{ |
| 224 |
struct envir *ptr=NULL ; |
| 225 |
if ( ( ptr = find_envir( TSD, name ) ) == NULL ) |
| 226 |
return 0; |
| 227 |
ptr->e.subcomed = subcomed; |
| 228 |
return 1; |
| 229 |
} |
| 230 |
/* |
| 231 |
* This function is used to get the subcomed flag from an environment. |
| 232 |
*/ |
| 233 |
int get_subcomed_envir( const tsd_t *TSD, const streng *name ) |
| 234 |
{ |
| 235 |
struct envir *ptr=NULL ; |
| 236 |
if ( ( ptr = find_envir( TSD, name ) ) == NULL ) |
| 237 |
return 0; |
| 238 |
return ptr->e.subcomed; |
| 239 |
} |
| 240 |
|
| 241 |
static void del_envirpart( const tsd_t *TSD, environpart *e ) |
| 242 |
/* Deletes all allocated parts of the environpart e. |
| 243 |
* The names are set to NULL afterwards, thus allowing multiple calls. |
| 244 |
*/ |
| 245 |
{ |
| 246 |
if ( e->name ) |
| 247 |
Free_stringTSD( e->name ) ; |
| 248 |
if ( e->base ) |
| 249 |
Free_stringTSD( e->base ) ; |
| 250 |
if ( e->currname ) |
| 251 |
Free_stringTSD( e->currname ) ; |
| 252 |
e->name = |
| 253 |
e->base = |
| 254 |
e->currname = NULL; |
| 255 |
/* Temporary files must be deleted under all circumstances! */ |
| 256 |
cleanup_envirpart(TSD, e); |
| 257 |
} |
| 258 |
|
| 259 |
void del_envir( tsd_t *TSD, const streng *name ) |
| 260 |
/* Deletes all allocated parts of the environment with the given name and |
| 261 |
* removes it from the linked list. |
| 262 |
*/ |
| 263 |
{ |
| 264 |
struct envir *ptr=NULL ; |
| 265 |
|
| 266 |
ptr = find_envir( TSD, name ) ; |
| 267 |
if (!ptr) |
| 268 |
return; |
| 269 |
|
| 270 |
if (ptr->prev) |
| 271 |
ptr->prev->next = ptr->next ; |
| 272 |
if (ptr->next) |
| 273 |
ptr->next->prev = ptr->prev ; |
| 274 |
if (TSD->firstenvir==ptr) |
| 275 |
TSD->firstenvir = ptr->prev ; |
| 276 |
|
| 277 |
/* Delete the names in the environment */ |
| 278 |
if ( ptr->e.name ) |
| 279 |
Free_stringTSD( ptr->e.name ) ; |
| 280 |
del_envirpart( TSD, &ptr->e.input ) ; |
| 281 |
del_envirpart( TSD, &ptr->e.output ) ; |
| 282 |
del_envirpart( TSD, &ptr->e.error ) ; |
| 283 |
FreeTSD( ptr ) ; |
| 284 |
} |
| 285 |
|
| 286 |
static void update_environpart( const tsd_t *TSD, environpart *e, const nodeptr newptr) |
| 287 |
/* e is the environpart which has to be reset. new is the new part and has |
| 288 |
* to been valid. new->name may be NULL (for NORMAL behaviour). |
| 289 |
*/ |
| 290 |
{ |
| 291 |
del_envirpart( TSD, e ) ; |
| 292 |
|
| 293 |
if (newptr->name) |
| 294 |
{ |
| 295 |
e->name = Str_dupTSD( newptr->name ) ; |
| 296 |
e->base = Str_makeTSD( 3*sizeof(int) ) ; |
| 297 |
} |
| 298 |
|
| 299 |
e->flags = newptr->u.of ; |
| 300 |
clear_environpart(e); |
| 301 |
} |
| 302 |
|
| 303 |
int set_envir( const tsd_t *TSD, const streng *envirname, const nodeptr ios ) |
| 304 |
/* This function sets all three IO-redirections INPUT, OUTPUT and ERROR. |
| 305 |
* ios->p[0] must be the redirection of INPUT, |
| 306 |
* ios->p[1] must be the redirection of OUTPUT, |
| 307 |
* ios->p[2] must be the redirection of ERROR |
| 308 |
* ios==NULL || envirname==NULL --> don't do anything. |
| 309 |
* ios->p[x]==NULL --> don't touch this redirection. |
| 310 |
* ios->p[x].name may be NULL (for NORMAL behaviour). |
| 311 |
* |
| 312 |
* The types of all four nodes may or may not be X_ADDR_WITH. |
| 313 |
* |
| 314 |
* returns 1 on success, 0 if the envirname doesn't exist. |
| 315 |
*/ |
| 316 |
{ |
| 317 |
struct envir *e; |
| 318 |
|
| 319 |
if ( ( envirname == NULL ) || ( ios == NULL ) ) |
| 320 |
return 1; |
| 321 |
|
| 322 |
if ( ( e = find_envir( TSD, envirname ) ) == NULL ) |
| 323 |
return 0; |
| 324 |
if (ios->p[0]) update_environpart( TSD, &e->e.input, ios->p[0] ); |
| 325 |
if (ios->p[1]) update_environpart( TSD, &e->e.output, ios->p[1] ); |
| 326 |
if (ios->p[2]) update_environpart( TSD, &e->e.error, ios->p[2] ); |
| 327 |
e->e.input.flags.isinput = 1; |
| 328 |
e->e.error.flags.iserror = 1; |
| 329 |
|
| 330 |
return 1; |
| 331 |
} |
| 332 |
|
| 333 |
static void dup_environpart( const tsd_t *TSD, environpart *dest, |
| 334 |
const nodeptr prefer, const environpart *src ) |
| 335 |
/* Copies src to dest if prefer isn't set. In this case prefer is used. |
| 336 |
*/ |
| 337 |
{ |
| 338 |
if (prefer) |
| 339 |
update_environpart( TSD, dest, prefer ) ; |
| 340 |
else |
| 341 |
{ |
| 342 |
if (src->name) |
| 343 |
{ |
| 344 |
dest->name = Str_dupTSD( src->name ) ; |
| 345 |
dest->base = Str_makeTSD( 3*sizeof(int) ) ; |
| 346 |
} |
| 347 |
dest->flags = src->flags ; |
| 348 |
} |
| 349 |
clear_environpart(dest); |
| 350 |
} |
| 351 |
|
| 352 |
static struct envir *dup_envir( tsd_t *TSD, const streng *name, cnodeptr ios ) |
| 353 |
/* This functions returns a new instance of the environment with the given |
| 354 |
* name or NULL if there is not such an environment name. |
| 355 |
* The current IO-redirection settings are overwritten with the |
| 356 |
* environment parts given in ios->p[0..2] for INPUT, OUTPUT and ERROR. |
| 357 |
*/ |
| 358 |
{ |
| 359 |
struct envir *prev, *newptr; |
| 360 |
|
| 361 |
if ( ( prev = find_envir( TSD, name ) ) == NULL ) |
| 362 |
return NULL; |
| 363 |
|
| 364 |
add_envir( TSD, name, prev->type, prev->e.subtype ); |
| 365 |
newptr = (struct envir *)TSD->firstenvir; |
| 366 |
|
| 367 |
dup_environpart( TSD, &newptr->e.input, ios->p[0], &prev->e.input ); |
| 368 |
dup_environpart( TSD, &newptr->e.output, ios->p[1], &prev->e.output ); |
| 369 |
dup_environpart( TSD, &newptr->e.error, ios->p[2], &prev->e.error ); |
| 370 |
newptr->e.input.flags.isinput = 1; |
| 371 |
newptr->e.error.flags.iserror = 1; |
| 372 |
|
| 373 |
return newptr; |
| 374 |
} |
| 375 |
|
| 376 |
int init_envir( tsd_t *TSD ) |
| 377 |
{ |
| 378 |
static const struct { |
| 379 |
const char *name ; |
| 380 |
int subtype ; |
| 381 |
} locals[] = { |
| 382 |
{ "COMMAND", SUBENVIR_PATH } , /* was SUBENVIR_COMMAND */ |
| 383 |
{ "SYSTEM", SUBENVIR_SYSTEM } , |
| 384 |
{ "OS2ENVIRONMENT", SUBENVIR_SYSTEM } , |
| 385 |
{ "ENVIRONMENT", SUBENVIR_SYSTEM } , |
| 386 |
{ "CMD", SUBENVIR_PATH } , /* was SUBENVIR_COMMAND */ |
| 387 |
{ "PATH", SUBENVIR_PATH } , |
| 388 |
{ "REGINA", SUBENVIR_REXX } , |
| 389 |
{ "REXX", SUBENVIR_REXX } |
| 390 |
}; |
| 391 |
unsigned i; |
| 392 |
streng *tmp; |
| 393 |
|
| 394 |
for ( i = 0; i < sizeof(locals) / sizeof(locals[0]); i++ ) |
| 395 |
{ |
| 396 |
tmp = Str_creTSD( locals[i].name ) ; |
| 397 |
add_envir( TSD, tmp, ENVIR_SHELL, locals[i].subtype ) ; |
| 398 |
Free_stringTSD( tmp ) ; |
| 399 |
} |
| 400 |
#ifdef TRACEMEM |
| 401 |
regmarker( TSD, markenvir ) ; |
| 402 |
#endif |
| 403 |
return(1); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
static int get_io_flag( tsd_t *TSD, streng *command, streng **rxqueue ) |
| 408 |
{ |
| 409 |
int length=0, i=0, pos=0 ; |
| 410 |
int flag=0,have_space=0 ; |
| 411 |
int qname_start,qname_end; |
| 412 |
streng *tmpq=NULL; |
| 413 |
|
| 414 |
flag = REDIR_NONE ; |
| 415 |
/* |
| 416 |
* All I/O redirection valid with INTERNAL queues only |
| 417 |
*/ |
| 418 |
if ((length=Str_len(command)) > 5 |
| 419 |
&& (get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) |
| 420 |
|| external_queues_used( TSD ) == 0 ) ) |
| 421 |
{ |
| 422 |
if ((!memcmp(command->value,"lifo>",5)) || |
| 423 |
(!memcmp(command->value,"LIFO>",5))) |
| 424 |
flag |= REDIR_INPUT ; |
| 425 |
|
| 426 |
if ((!memcmp(command->value+length-5,">lifo",5)) |
| 427 |
|| (!memcmp(command->value+length-5,">LIFO",5))) |
| 428 |
{ |
| 429 |
flag |= REDIR_OUTLIFO ; |
| 430 |
command->len -= 5; |
| 431 |
} |
| 432 |
else |
| 433 |
{ |
| 434 |
if ( ( !memcmp( command->value+length-5, ">fifo" ,5 ) ) |
| 435 |
|| ( !memcmp( command->value+length-5, ">FIFO", 5 ) ) ) |
| 436 |
{ |
| 437 |
flag |= REDIR_OUTFIFO ; |
| 438 |
command->len -= 5; |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
if ( length >= 8 ) |
| 443 |
{ |
| 444 |
|
| 445 |
for ( i = 0, pos = -1; i < length; i++ ) |
| 446 |
{ |
| 447 |
if ( *(command->value + i ) == '|' ) |
| 448 |
{ |
| 449 |
pos = i; |
| 450 |
/* don't break here, as we want the last '|' */ |
| 451 |
} |
| 452 |
} |
| 453 |
if ( pos != -1 ) |
| 454 |
{ |
| 455 |
/* allow "|" [whitespace] "rxqueue" [whitespace[args]] */ |
| 456 |
/* "|" already checked */ |
| 457 |
for ( i = pos + 1; i < length; i++ ) |
| 458 |
{ |
| 459 |
if ( !rx_isspace(command->value[i] ) ) |
| 460 |
break; |
| 461 |
} |
| 462 |
if ( i+7 <= length ) |
| 463 |
{ |
| 464 |
if (mem_cmpic( command->value + i, "RXQUEUE", 7 ) == 0 ) |
| 465 |
{ |
| 466 |
i += 7; |
| 467 |
for ( ; i < length; i++ ) |
| 468 |
{ |
| 469 |
if ( !rx_isspace( command->value[i] ) ) |
| 470 |
break; |
| 471 |
have_space = 1; |
| 472 |
} |
| 473 |
if ( i == length ) |
| 474 |
{ |
| 475 |
flag |= REDIR_OUTFIFO ; |
| 476 |
command->len = pos; |
| 477 |
} |
| 478 |
else if ( have_space ) |
| 479 |
{ |
| 480 |
if ( *(command->value + i) == '/' ) |
| 481 |
{ |
| 482 |
/* |
| 483 |
* Only a switch, not a queuename |
| 484 |
*/ |
| 485 |
if ( i+6 <= length |
| 486 |
&& mem_cmpic( command->value + i, "/CLEAR", 6 ) == 0 ) |
| 487 |
{ |
| 488 |
flag |= REDIR_CLEAR; |
| 489 |
flag |= REDIR_OUTFIFO ; |
| 490 |
} |
| 491 |
else if ( i+5 <= length ) |
| 492 |
{ |
| 493 |
if (mem_cmpic( command->value + i, "/FIFO", 5 ) == 0 ) |
| 494 |
flag |= REDIR_OUTFIFO ; |
| 495 |
else if (mem_cmpic( command->value + i, "/LIFO", 5 ) == 0 ) |
| 496 |
flag |= REDIR_OUTLIFO ; |
| 497 |
else |
| 498 |
flag |= REDIR_OUTFIFO ; |
| 499 |
/* |
| 500 |
* Let the queue name be determined by the caller |
| 501 |
*/ |
| 502 |
} |
| 503 |
} |
| 504 |
else |
| 505 |
{ |
| 506 |
/* |
| 507 |
* First word must be a queue name, optionally |
| 508 |
* followed by a switch |
| 509 |
*/ |
| 510 |
have_space = 0; |
| 511 |
qname_start = i; |
| 512 |
for ( ; i < length; i++ ) |
| 513 |
{ |
| 514 |
if ( rx_isspace( command->value[i] ) ) |
| 515 |
{ |
| 516 |
have_space = 1; |
| 517 |
qname_end = i; |
| 518 |
/* |
| 519 |
* Get queuename from RXQUEUE env |
| 520 |
* variable, or if not set, use SESSION |
| 521 |
*/ |
| 522 |
tmpq = Str_make_TSD( TSD, qname_end - qname_start ); |
| 523 |
tmpq->len = qname_end - qname_start; |
| 524 |
memcpy( tmpq->value, command->value + qname_start, qname_end - qname_start ); |
| 525 |
break; |
| 526 |
} |
| 527 |
} |
| 528 |
if ( have_space ) |
| 529 |
{ |
| 530 |
/* |
| 531 |
* Eat up all spaces. If we have |
| 532 |
* any non-spaces left, it should be |
| 533 |
* a switch |
| 534 |
*/ |
| 535 |
for ( ; i < length; i++ ) |
| 536 |
{ |
| 537 |
if ( !rx_isspace( command->value[i] ) ) |
| 538 |
break; |
| 539 |
} |
| 540 |
if ( i+6 <= length |
| 541 |
&& mem_cmpic( command->value + i, "/CLEAR", 6 ) == 0 ) |
| 542 |
{ |
| 543 |
flag |= REDIR_CLEAR; |
| 544 |
flag |= REDIR_OUTFIFO ; |
| 545 |
} |
| 546 |
else if ( i+5 <= length ) |
| 547 |
{ |
| 548 |
if (mem_cmpic( command->value + i, "/FIFO", 5 ) == 0 ) |
| 549 |
flag |= REDIR_OUTFIFO ; |
| 550 |
else if (mem_cmpic( command->value + i, "/LIFO", 5 ) == 0 ) |
| 551 |
flag |= REDIR_OUTLIFO ; |
| 552 |
else |
| 553 |
flag |= REDIR_OUTFIFO ; |
| 554 |
} |
| 555 |
} |
| 556 |
else |
| 557 |
{ |
| 558 |
/* |
| 559 |
* Only have a queue name |
| 560 |
*/ |
| 561 |
tmpq = Str_make_TSD( TSD, length - qname_start ); |
| 562 |
tmpq->len = length - qname_start; |
| 563 |
memcpy( tmpq->value, command->value + qname_start, length - qname_start ); |
| 564 |
flag |= REDIR_OUTFIFO ; |
| 565 |
} |
| 566 |
} |
| 567 |
command->len = pos; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
if (flag & REDIR_INPUT) |
| 578 |
{ |
| 579 |
for(i=5; i<Str_len(command); i++ ) /* avoid buffer overrun */ |
| 580 |
command->value[i-5]=command->value[i] ; |
| 581 |
command->len -= 5 ; |
| 582 |
} |
| 583 |
*rxqueue = tmpq; |
| 584 |
return flag ; |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* ANSI 8.2.4 and 8.3.5 and others forces us to set some variables showing |
| 589 |
* the result and to raise some conditions in case of errors after |
| 590 |
* processing an external command. |
| 591 |
*/ |
| 592 |
void post_process_system_call( tsd_t *TSD, const streng *cmd, |
| 593 |
int rc_code, const streng *rc_value, |
| 594 |
cnodeptr thisptr ) |
| 595 |
{ |
| 596 |
int rs; |
| 597 |
trap *traps; |
| 598 |
int type; |
| 599 |
|
| 600 |
/* |
| 601 |
* ANSI 8.2.4 etc. forces us to do the following. |
| 602 |
* |
| 603 |
* 10-08-2004 MH interpreted 8.2.4 as only being applicable to |
| 604 |
* clauses entered at the interactive prompt. |
| 605 |
*/ |
| 606 |
#if 0 |
| 607 |
if ( !TSD->systeminfo->interactive ) |
| 608 |
#endif |
| 609 |
{ |
| 610 |
if ( rc_value != NULL ) |
| 611 |
set_reserved_value( TSD, POOL0_RC, Str_dupTSD( rc_value ), 0, |
| 612 |
VFLAG_STR ); |
| 613 |
else |
| 614 |
set_reserved_value( TSD, POOL0_RC, NULL, rc_code, VFLAG_NUM ); |
| 615 |
} |
| 616 |
|
| 617 |
/* set .RS: -1==Failure, 0=OK, 1=Error */ |
| 618 |
if ( rc_code == 0 ) |
| 619 |
rs = 0; |
| 620 |
else if ( rc_code < 0 ) |
| 621 |
rs = -1; |
| 622 |
else |
| 623 |
rs = 1; |
| 624 |
set_reserved_value( TSD, POOL0_RS, NULL, rs, VFLAG_NUM ); |
| 625 |
|
| 626 |
if ( rc_code ) |
| 627 |
{ |
| 628 |
traceerror( TSD, thisptr, rc_code ); |
| 629 |
traps = gettraps( TSD, TSD->currlevel ); |
| 630 |
type = ( rc_code > 0 ) ? SIGNAL_ERROR : SIGNAL_FAILURE; |
| 631 |
|
| 632 |
if ( traps[type].on_off ) |
| 633 |
condition_hook( TSD, type, rc_code, 0, thisptr->lineno, Str_dupTSD( cmd ), NULL ); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
streng *perform( tsd_t *TSD, const streng *command, const streng *envir, cnodeptr thisptr, cnodeptr overwrite ) |
| 638 |
/* If and only if overwrite is set, a new instance of the environment is |
| 639 |
* temporarily created and reset to the new IO-redirections. |
| 640 |
*/ |
| 641 |
{ |
| 642 |
int rc=0, io_flag=0, clearq=0, tempenvir=0; |
| 643 |
struct envir *eptr; |
| 644 |
streng *retstr=NULL ; |
| 645 |
streng *rxqueue=NULL; |
| 646 |
streng *cmd=Str_dupTSD(command); |
| 647 |
streng *saved_queue=NULL; |
| 648 |
char *rxq=NULL; |
| 649 |
|
| 650 |
if ( overwrite ) |
| 651 |
{ |
| 652 |
/* |
| 653 |
* fixes bug 653214, overwrite was this->p[1] in former versions, which |
| 654 |
* was wrong is this wasn't an ADDRESS statement. |
| 655 |
*/ |
| 656 |
if (( eptr = dup_envir( TSD, envir, overwrite ) ) != NULL) |
| 657 |
tempenvir = 1; |
| 658 |
} |
| 659 |
else |
| 660 |
{ |
| 661 |
eptr = find_envir( TSD, envir ) ; |
| 662 |
} |
| 663 |
|
| 664 |
if (eptr) |
| 665 |
{ |
| 666 |
if ( TSD->restricted |
| 667 |
&& eptr->e.subtype ) |
| 668 |
exiterror( ERR_RESTRICTED, 5 ) ; |
| 669 |
switch (eptr->type) |
| 670 |
{ |
| 671 |
case ENVIR_PIPE: |
| 672 |
retstr = SubCom( TSD, cmd, envir, &rc ) ; |
| 673 |
break ; |
| 674 |
|
| 675 |
case ENVIR_SHELL: |
| 676 |
io_flag = get_io_flag( TSD, cmd, &rxqueue ) ; |
| 677 |
/* |
| 678 |
* Save the current queue name |
| 679 |
* Then change it to the value of rxqueue |
| 680 |
* Only for internal queues |
| 681 |
*/ |
| 682 |
if ( get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) ) |
| 683 |
{ |
| 684 |
/* |
| 685 |
* If no queuename specified, |
| 686 |
* get queuename from RXQUEUE env |
| 687 |
* variable, or if not set, use SESSION |
| 688 |
*/ |
| 689 |
if ( rxqueue == NULL ) |
| 690 |
{ |
| 691 |
if ( ( rxq = getenv("RXQUEUE") ) == NULL ) |
| 692 |
{ |
| 693 |
rxqueue = Str_cre_TSD( TSD, "SESSION" ); |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
rxqueue = Str_cre_TSD( TSD, rxq ); |
| 698 |
} |
| 699 |
} |
| 700 |
saved_queue = set_queue( TSD, rxqueue ); |
| 701 |
if ( io_flag & REDIR_CLEAR ) |
| 702 |
{ |
| 703 |
clearq = 1; |
| 704 |
io_flag -= REDIR_CLEAR; |
| 705 |
} |
| 706 |
} |
| 707 |
rc = posix_do_command( TSD, cmd, io_flag, &eptr->e, NULL ) ; |
| 708 |
/* |
| 709 |
* Change the current queue name back |
| 710 |
* to the saved name |
| 711 |
*/ |
| 712 |
if ( get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) ) |
| 713 |
{ |
| 714 |
if ( clearq ) |
| 715 |
drop_buffer( TSD, 0 ) ; |
| 716 |
set_queue( TSD, saved_queue ); |
| 717 |
if ( rxqueue != NULL ) |
| 718 |
Free_stringTSD( rxqueue ); |
| 719 |
} |
| 720 |
retstr = int_to_streng( TSD, rc ) ; |
| 721 |
break ; |
| 722 |
|
| 723 |
default: |
| 724 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 725 |
} |
| 726 |
} |
| 727 |
else |
| 728 |
{ |
| 729 |
retstr = SubCom( TSD, cmd, envir, &rc ) ; |
| 730 |
} |
| 731 |
|
| 732 |
if (tempenvir) |
| 733 |
del_envir( TSD, envir ) ; |
| 734 |
|
| 735 |
post_process_system_call( TSD, cmd, rc, retstr, thisptr ); |
| 736 |
|
| 737 |
Free_stringTSD( cmd ) ; |
| 738 |
return retstr ; |
| 739 |
} |
| 740 |
|
| 741 |
/* run_popen is a special implementation of a direct call to posix_do_command. |
| 742 |
* It redirects input and error to "normal" and fetches the output. |
| 743 |
* This output has blanks as line-delimiters. All lines are returned |
| 744 |
* concatenated on success; NULL is returned if an errors occurs. |
| 745 |
* The global variable "RC" contains the return code of the called process. |
| 746 |
* |
| 747 |
* The command is executed in the current environment. If the environment |
| 748 |
* isn't a standard environment, SYSTEM is used. |
| 749 |
*/ |
| 750 |
streng *run_popen( tsd_t *TSD, const streng *command, const streng *envir ) |
| 751 |
{ |
| 752 |
int rc; |
| 753 |
streng *retval; |
| 754 |
struct envir *ptr=NULL ; |
| 755 |
Queue *q ; |
| 756 |
|
| 757 |
for ( ptr = (struct envir *)TSD->firstenvir; ptr; ptr = ptr->prev ) |
| 758 |
{ |
| 759 |
if ( ( ptr->type == ENVIR_SHELL ) && ( Str_cmp( ptr->e.name, envir ) == 0 ) ) |
| 760 |
break; |
| 761 |
} |
| 762 |
if ( ptr == NULL ) |
| 763 |
{ |
| 764 |
retval = Str_creTSD( "SYSTEM" ); /* temporary misuse */ |
| 765 |
ptr = find_envir( TSD, retval ); |
| 766 |
Free_stringTSD( retval ) ; |
| 767 |
} |
| 768 |
|
| 769 |
/* Create a new environment with no redirections. */ |
| 770 |
add_envir(TSD, ptr->e.name, ENVIR_SHELL, ptr->e.subtype); |
| 771 |
|
| 772 |
q = find_free_slot( TSD ) ; |
| 773 |
q->type = QisTemp; |
| 774 |
rc = posix_do_command(TSD, command, REDIR_OUTSTRING, (environment *)TSD->firstenvir, q ) ; |
| 775 |
retval = stack_to_line( TSD, q ) ; |
| 776 |
|
| 777 |
/* restore the previous environment and delete the temporary one */ |
| 778 |
del_envir(TSD, ptr->e.name); |
| 779 |
|
| 780 |
set_reserved_value( TSD, POOL0_RC, NULL, rc, VFLAG_NUM ); |
| 781 |
|
| 782 |
if (rc >= 0) |
| 783 |
return(retval); |
| 784 |
|
| 785 |
/* rc shows an error while calling: */ |
| 786 |
Free_stringTSD(retval); |
| 787 |
return(NULL); |
| 788 |
} |