Debian Codegraph @

expr.c

raw
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
22 #include <string.h>
23 #include <assert.h>
24
25 #define TRACEVALUE(a,b) if (TSD->trace_stat=='I') tracevalue(TSD,a,b)
26
27 #ifdef TRACEMEM
28 static void mark_in_expr( const tsd_t *TSD )
29 {
30 if (TSD->rdes.num)
31 markmemory( TSD->rdes.num, TRC_STATIC ) ;
32 if (TSD->ldes.num)
33 markmemory( TSD->ldes.num, TRC_STATIC ) ;
34 }
35 #endif
36
37 /*
38 * COMP_IGNORE returns 1 if c shall be ignored on a non-strict comparison
39 * between non-numbers. Previously, this was equal to isspace(c). This was
40 * wrong according to ANSI, section 7.4.7. This fixes bug 594674.
41 */
42 #define ANSI_COMP_IGNORE(c) ( (c) == ' ' )
43 #define REGINA_COMP_IGNORE(c) ( rx_isspace(c) )
44
45 #define FREE_TMP_STRING(str) if ( str ) \
46 Free_stringTSD( str )
47 static char *getoperator( int type )
48 {
49 char *retstr;
50 switch ( type )
51 {
52 case X_PLUSS:
53 case X_U_PLUSS: retstr = "+"; break;
54 case 0:
55 case 255:
56 case X_MINUS:
57 case X_U_MINUS:
58 retstr = "-"; break;
59 case X_MULT: retstr = "*"; break;
60 case X_DEVIDE: retstr = "/"; break;
61 case X_MODULUS: retstr = "//"; break;
62 case X_INTDIV: retstr = "%"; break;
63 case X_EXP: retstr = "**"; break;
64 case X_PLUSASSIGN: retstr = "+="; break;
65 case X_MINUSASSIGN: retstr = "-="; break;
66 case X_MULTASSIGN: retstr = "*="; break;
67 case X_DIVASSIGN: retstr = "*="; break;
68 case X_INTDIVASSIGN: retstr = "%="; break;
69 case X_MODULUSASSIGN: retstr = "//="; break;
70 case X_ANDASSIGN: retstr = "&="; break;
71 case X_ORASSIGN: retstr = "|="; break;
72 case X_XORASSIGN: retstr = "&&="; break;
73 case X_CONCATASSIGN: retstr = "||="; break;
74 default: retstr = "unknown"; break;
75 }
76 return retstr;
77 }
78 int init_expr( tsd_t *TSD )
79 {
80 #ifdef TRACEMEM
81 regmarker( TSD, mark_in_expr ) ;
82 #endif
83 return(1);
84 }
85
86
87 static num_descr *copy_num( const tsd_t *TSD, const num_descr *input )
88 {
89 num_descr *newptr=NULL ;
90 newptr = (num_descr *)MallocTSD( sizeof( num_descr )) ;
91 newptr->negative = input->negative ;
92 newptr->size = input->size ;
93 newptr->max = (input->max < 1) ? 1 : input->max ;
94 newptr->exp = input->exp ;
95 newptr->num = (char *)MallocTSD( newptr->max ) ;
96 newptr->used_digits = input->used_digits;
97 memcpy( newptr->num, input->num, newptr->size ) ;
98 return newptr ;
99 }
100
101
102 static streng *num_to_str( const tsd_t *TSD, num_descr *input )
103 {
104 return str_norm( TSD, input, NULL ) ;
105 }
106
107 /* can't use str as it seems to be NULL all the time :-( */
108 static int num_to_bool( tsd_t *TSD, const num_descr *input, streng *str, int suberror, const char *op )
109 {
110 char ch=' ' ;
111
112 if (input==NULL)
113 {
114 if ( op )
115 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, str ) ) ;
116 else
117 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, str ) ) ;
118 }
119
120 if (input->size!=1 || input->exp!=1)
121 {
122 if ( str )
123 {
124 if ( op )
125 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, str ) ) ;
126 else
127 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, str ) ) ;
128 }
129 else
130 exiterror( ERR_UNLOGICAL_VALUE, 0 );
131 }
132 /* size of number MUST be 1 at this point */
133 ch = input->num[0] ;
134 /* if negative, only zero is valid */
135 if ( input->negative && ch!='0' )
136 {
137 if ( str )
138 {
139 if ( op )
140 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, str ) ) ;
141 else
142 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, str ) ) ;
143 }
144 else
145 exiterror( ERR_UNLOGICAL_VALUE, 0 );
146 }
147 if (ch!='0' && ch!='1')
148 {
149 if ( str )
150 if ( op )
151 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, str ) ) ;
152 else
153 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, str ) ) ;
154 else
155 exiterror( ERR_UNLOGICAL_VALUE, 0 );
156 }
157 return ch=='1' ;
158 }
159
160 static int str_to_bool( const streng *input )
161 {
162 char ch=' ' ;
163
164 if (input->len!=1)
165 exiterror( ERR_UNLOGICAL_VALUE, 0 ) ;
166
167 ch = input->value[0] ;
168 if (ch!='0' && ch!='1')
169 exiterror( ERR_UNLOGICAL_VALUE, 0 ) ;
170
171 return ch == '1' ;
172 }
173
174 static streng *bool_to_str( const tsd_t *TSD, int input )
175 {
176 return Str_creTSD( input ? "1" : "0" ) ;
177 }
178
179 static num_descr *bool_to_num( const tsd_t *TSD, int input )
180 {
181 num_descr *num=NULL ;
182
183 num = (num_descr *)MallocTSD( sizeof( num_descr )) ;
184 num->max = 8 ;
185 num->num = (char *)MallocTSD( 8 ) ;
186 num->size = 1 ;
187 num->negative = 0 ;
188 num->exp = 1 ;
189 num->num[0] = (char) ((input) ? '1' : '0') ;
190 num->used_digits = TSD->currlevel->currnumsize;
191 return num ;
192 }
193
194 /*
195 * calcul evaluates a numeric expression. thisptr is the current evaluation tree.
196 * kill? return value?
197 * Note: This is one of the most time-consuming routines. Be careful.
198 * The operator arg is the initial arithmetic operator when called from outside. It is
199 * overridden by the type of the pointer only for arithmetic operators so the most recent
200 * operator is reported when an error occurs
201 */
202 num_descr *calcul( tsd_t *TSD, nodeptr thisptr, num_descr **kill, int side, int operator )
203 {
204 num_descr *numthr, *numone, *numtwo ;
205 num_descr *ntmp1=NULL, *ntmp2=NULL ;
206 num_descr *nptr;
207 streng *sptr;
208 int strip2 = 0; /* fixes bug 1107763, second part */
209
210 switch ( thisptr->type )
211 {
212 case 0:
213 case 255:
214 case X_MINUS:
215 case X_MINUSASSIGN:
216 operator = thisptr->type;
217 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
218 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, operator ) ;
219 if (!ntmp2)
220 ntmp2 = numtwo = copy_num( TSD, numtwo ) ;
221
222 numtwo->negative = !numtwo->negative ;
223 goto do_an_add ;
224
225 case X_PLUSS:
226 case X_PLUSASSIGN:
227 operator = thisptr->type;
228 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
229 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, operator ) ;
230 do_an_add:
231 if (ntmp1)
232 {
233 numthr = numone ;
234 ntmp1 = NULL ;
235 }
236 else if (ntmp2)
237 {
238 numthr = numtwo ;
239 ntmp2 = NULL ;
240 }
241 else
242 numthr = copy_num( TSD, numtwo ) ;
243
244 string_add( TSD, numone, numtwo, numthr, thisptr->p[0], thisptr->p[1] ) ;
245 break ;
246
247 case X_MULT:
248 case X_MULTASSIGN:
249 operator = thisptr->type;
250 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
251 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, operator ) ;
252 if (ntmp1)
253 {
254 numthr = numone ;
255 ntmp1 = NULL ;
256 }
257 else if (ntmp2)
258 {
259 numthr = numtwo ;
260 ntmp2 = NULL ;
261 }
262 else
263 numthr = copy_num( TSD, numtwo ) ;
264
265 string_mul( TSD, numone, numtwo, numthr, thisptr->p[0], thisptr->p[1] );
266 break ;
267
268 case X_DEVIDE:
269 case X_MODULUS:
270 case X_INTDIV:
271 operator = thisptr->type;
272 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
273 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, operator ) ;
274 if (numtwo->size==1 && numtwo->num[0]=='0')
275 exiterror( ERR_ARITH_OVERFLOW, 3 ) ;
276
277 numthr = copy_num( TSD, numtwo ) ;
278 string_div( TSD, numone, numtwo, numthr, NULL,
279 ((thisptr->type==X_DEVIDE) ? DIVTYPE_NORMAL :
280 ((thisptr->type==X_MODULUS) ? DIVTYPE_REMAINDER : DIVTYPE_INTEGER)),
281 thisptr->p[0], thisptr->p[1] );
282 strip2 = 1;
283 break ;
284 case X_DIVASSIGN:
285 case X_INTDIVASSIGN:
286 case X_MODULUSASSIGN:
287 operator = thisptr->type;
288 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
289 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, operator ) ;
290 if (numtwo->size==1 && numtwo->num[0]=='0')
291 exiterror( ERR_ARITH_OVERFLOW, 3 ) ;
292
293 numthr = copy_num( TSD, numtwo ) ;
294 string_div( TSD, numone, numtwo, numthr, NULL,
295 ((thisptr->type==X_DIVASSIGN) ? DIVTYPE_NORMAL :
296 ((thisptr->type==X_MODULUSASSIGN) ? DIVTYPE_REMAINDER : DIVTYPE_INTEGER)),
297 thisptr->p[0], thisptr->p[1] );
298 strip2 = 1;
299 break ;
300
301 case X_EXP:
302 operator = thisptr->type;
303 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, operator ) ;
304 numtwo = ntmp2 = calcul( TSD, thisptr->p[1], NULL, SIDE_RIGHT, operator ) ;
305 numthr = copy_num( TSD, numone ) ;
306 string_pow( TSD, numone, numtwo, numthr, thisptr->p[0], thisptr->p[1] ) ;
307 strip2 = 1;
308 break ;
309
310 case X_STRING:
311 case X_CON_SYMBOL:
312 if ( !thisptr->u.number )
313 thisptr->u.number = get_a_descr( TSD, NULL, 0, thisptr->name ) ;
314
315 if (TSD->trace_stat=='I')
316 tracenumber( TSD, thisptr->u.number, 'L' ) ;
317
318 if (kill)
319 {
320 *kill = NULL ;
321 return thisptr->u.number ;
322 }
323 else
324 return copy_num( TSD, thisptr->u.number ) ;
325
326 case X_SIM_SYMBOL:
327 case X_STEM_SYMBOL:
328 if (kill)
329 *kill = NULL ;
330
331 nptr = shortcutnum( TSD, thisptr ) ;
332 if (!nptr)
333 {
334 /* get value of symbol for error reporting */
335 sptr = evaluate( TSD, thisptr, NULL ) ;
336 exiterror( ERR_BAD_ARITHMETIC, side, tmpstr_of( TSD, sptr ), getoperator( operator ) );
337 }
338
339 if (kill)
340 return nptr ;
341 else
342 return copy_num( TSD, nptr ) ;
343
344 case X_HEAD_SYMBOL:
345 if (kill)
346 *kill = NULL ;
347
348 nptr = fix_compoundnum( TSD, thisptr, NULL, NULL );
349 if (!nptr)
350 exiterror( ERR_BAD_ARITHMETIC, 0 ) ;
351
352 if (kill)
353 return nptr ;
354 else
355 return copy_num( TSD, nptr ) ;
356
357 case X_U_PLUSS:
358 case X_U_MINUS:
359 operator = thisptr->type;
360 numthr = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_RIGHT, operator ) ;
361 if (!ntmp1)
362 numthr = copy_num( TSD, numthr ) ;
363
364 if (thisptr->type==X_U_MINUS)
365 numthr->negative = !numthr->negative ;
366
367 if (kill)
368 *kill = numthr ;
369
370 if (TSD->trace_stat=='I')
371 tracenumber( TSD, numthr, 'P' ) ;
372
373 return numthr ;
374
375 case X_IN_FUNC:
376 case X_IS_INTERNAL:
377 case X_IS_BUILTIN:
378 case X_EX_FUNC:
379 case X_IS_EXTERNAL:
380 case X_CONCAT:
381 case X_SPACE:
382 {
383 numthr = get_a_descr( TSD, NULL, 0, evaluate( TSD, thisptr, &sptr ) );
384 FREE_TMP_STRING( sptr );
385 if (kill)
386 *kill = numthr ;
387 return numthr ;
388 }
389 case X_LOG_NOT:
390 case X_LOG_OR:
391 case X_LOG_AND:
392 case X_LOG_XOR:
393 case X_S_DIFF:
394 case X_S_EQUAL:
395 case X_EQUAL:
396 case X_GT:
397 case X_LT:
398 case X_GTE:
399 case X_LTE:
400 case X_DIFF:
401 case X_SEQUAL:
402 case X_SGT:
403 case X_SLT:
404 case X_SGTE:
405 case X_SLTE:
406 case X_SDIFF:
407 case X_NEQUAL:
408 case X_NGT:
409 case X_NLT:
410 case X_NGTE:
411 case X_NLTE:
412 case X_NDIFF:
413 case X_S_NGT:
414 case X_S_NLT:
415 case X_S_GT:
416 case X_S_GTE:
417 case X_S_LT:
418 case X_S_LTE:
419 case X_ORASSIGN:
420 case X_XORASSIGN:
421 case X_ANDASSIGN:
422 numthr = bool_to_num( TSD, isboolean( TSD, thisptr, 0, NULL )) ;
423 if (kill)
424 *kill = numthr ;
425 return numthr ;
426
427 default:
428 exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ;
429 return NULL ;
430 }
431
432 if (ntmp1)
433 {
434 FreeTSD( numone->num ) ;
435 FreeTSD( numone ) ;
436 }
437 if (ntmp2)
438 {
439 FreeTSD( numtwo->num ) ;
440 FreeTSD( numtwo ) ;
441 }
442 #if 0
443 // this moved below
444 if (kill)
445 *kill = numthr ;
446 #endif
447
448 if (TSD->trace_stat=='I')
449 tracenumber( TSD, numthr, 'O' ) ;
450
451 str_strip( numthr ) ;
452 str_round( numthr, TSD->currlevel->currnumsize ) ;
453 if ( strip2 )
454 {
455 /*
456 * ANSI 7.4.10, PostOp, add. rounding for / and **
457 */
458 strip2 = numthr->size;
459 while ( ( strip2 > 1 ) &&
460 ( numthr->exp < strip2) &&
461 ( numthr->num[strip2 - 1] == '0' ) )
462 strip2--;
463 if ( strip2 != numthr->size )
464 {
465 numthr->size = strip2;
466 if ( strip2 < numthr->used_digits )
467 numthr->used_digits = strip2;
468 }
469 }
470 if (kill)
471 *kill = numthr ;
472 return numthr ;
473 }
474
475 static void strip_whitespace( tsd_t *TSD, unsigned char **s1,
476 unsigned char **e1, unsigned char **s2,
477 unsigned char **e2 )
478 {
479 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI )
480 || get_options_flag( TSD->currlevel, EXT_STRICT_WHITE_SPACE_COMPARISONS ) )
481 {
482 /*
483 * ANSI 7.4.7 behaviour; non-strict comparisons
484 * removed ONLY spaces, so single TAB not equal to single SPACE
485 */
486 /*
487 * Strip leading spaces - ignored in comparison
488 */
489 for( ; ( *s1 < *e1 ) && ANSI_COMP_IGNORE( **s1 ); (*s1)++ )
490 {
491 }
492 for( ; ( *s2 < *e2 ) && ANSI_COMP_IGNORE( **s2 ); (*s2)++ )
493 {
494 }
495 for ( ; ( *s1 < *e1 ) && ( *s2 < *e2 ) && ( **s1 == **s2 ); (*s1)++, (*s2)++ )
496 {
497 }
498 /*
499 * Strip trailing spaces - ignored in comparison
500 */
501 for ( ; ( *e1 > *s1 ) && ANSI_COMP_IGNORE( *( *e1 - 1 ) ); (*e1)-- )
502 {
503 }
504 for ( ; ( *e2 > *s2 ) && ANSI_COMP_IGNORE( *( *e2 - 1 ) ); (*e2)-- )
505 {
506 }
507 }
508 else
509 {
510 /*
511 * Original Regina behaviour; non-strict comparisons
512 * removed ALL white space, so single TAB equalled single SPACE
513 */
514 /*
515 * Strip leading white space - ignored in comparison
516 */
517 for( ; ( *s1 < *e1 ) && REGINA_COMP_IGNORE( **s1 ); (*s1)++ )
518 {
519 }
520 for( ; ( *s2 < *e2 ) && REGINA_COMP_IGNORE( **s2 ); (*s2)++ )
521 {
522 }
523 for ( ; ( *s1 < *e1 ) && ( *s2 < *e2 ) && ( **s1 == **s2 ); (*s1)++, (*s2)++ )
524 {
525 }
526 /*
527 * Strip trailing white space - ignored in comparison
528 */
529 for ( ; ( *e1 > *s1 ) && REGINA_COMP_IGNORE( *( *e1 - 1 ) ); (*e1)-- )
530 {
531 }
532 for ( ; ( *e2 > *s2 ) && REGINA_COMP_IGNORE( *( *e2 - 1 ) ); (*e2)-- )
533 {
534 }
535 }
536 }
537
538
539 /*
540 * evaluate evaluates an expression. The nodeptr "thisptr" must point to an
541 * expression part. The return value is the value of the expression.
542 * For a proper cleanup the caller probably has to delete the returned
543 * value. For this purpose, the caller may set "kill" to non-NULL.
544 * *kill is set to NULL, if the returned value is a const value and must
545 * not be freed. *kill is set to a temporary value which has to be deleted
546 * after the use of the returned value.
547 * The caller may omit kill, this forces evaluate to create a freshly allocated
548 * return value.
549 */
550 streng *evaluate( tsd_t *TSD, nodeptr thisptr, streng **kill )
551 {
552 #define RETURN_NEW(val) if ( kill ) \
553 *kill = val; \
554 return val;
555 streng *strone,*strtwo,*strthr;
556 streng *stmp1,*stmp2;
557 const streng *cstmp;
558 num_descr *ntmp;
559
560 if ( kill )
561 *kill = NULL;
562 switch ( thisptr->type )
563 {
564 case 0:
565 case 255:
566 case X_PLUSS:
567 case X_MINUS:
568 case X_MULT:
569 case X_DEVIDE:
570 case X_MODULUS:
571 case X_INTDIV:
572 case X_EXP:
573 case X_U_MINUS:
574 case X_U_PLUSS:
575 ntmp = NULL;
576 stmp1 = num_to_str( TSD, calcul( TSD, thisptr, &ntmp, SIDE_LEFT, thisptr->type ) );
577 if ( ntmp )
578 {
579 FreeTSD( ntmp->num );
580 FreeTSD( ntmp );
581 }
582 RETURN_NEW( stmp1 );
583
584 case X_NULL:
585 return NULL ;
586
587 case X_STRING:
588 case X_CON_SYMBOL:
589 cstmp = thisptr->name;
590 if ( TSD->trace_stat == 'I' )
591 tracevalue( TSD, cstmp, 'L' );
592 if ( kill )
593 return (streng *) cstmp; /* and *kill is set to NULL above */
594 stmp1 = Str_dupTSD( cstmp );
595 RETURN_NEW( stmp1 );
596
597 case X_HEAD_SYMBOL:
598 /* always duplicate, since stmp1 might point to tmp area */
599 stmp1 = Str_dupTSD( fix_compound( TSD, thisptr, NULL ) );
600 RETURN_NEW( stmp1 );
601
602 case X_STEM_SYMBOL:
603 case X_SIM_SYMBOL:
604 cstmp = shortcut(TSD,thisptr) ;
605 if ( kill )
606 return (streng *) cstmp; /* and *kill is set to NULL above */
607 stmp1 = Str_dupTSD( cstmp );
608 RETURN_NEW( stmp1 );
609
610 case X_IN_FUNC:
611 {
612 nodeptr entry;
613
614 if ( ( entry = getlabel( TSD, thisptr->name ) ) != NULL )
615 {
616 if ( entry->u.trace_only )
617 exiterror( ERR_UNEXISTENT_LABEL, 3, tmpstr_of( TSD, thisptr->name ) );
618 thisptr->type = X_IS_INTERNAL;
619 thisptr->u.node = entry;
620 }
621 else
622 thisptr->u.node = NULL;
623 }
624
625 case X_IS_INTERNAL:
626 {
627 nodeptr entry ;
628 paramboxptr args ;
629 streng *ptr ;
630
631 /*
632 * Check if the internal function name ends with a '.'.
633 * This is an error in the ANSI standard, but it is possible
634 * that existing code allows this, so only generate an error
635 * if STRICT_ANSI OPTION is set.
636 */
637 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
638 {
639 if ( thisptr->name->value[(thisptr->name->len)-1] == '.' )
640 exiterror( ERR_UNQUOTED_FUNC_STOP, 1, tmpstr_of( TSD, thisptr->name ) ) ;
641 }
642 if ( ( entry = thisptr->u.node ) != NULL )
643 {
644 /*
645 * We are in an internal routine...
646 */
647 traceline( TSD, entry, TSD->trace_stat, 0 );
648 set_reserved_value( TSD, POOL0_SIGL, NULL, TSD->currentnode->lineno, VFLAG_NUM );
649 args = initplist( TSD, thisptr );
650
651 ptr = CallInternalFunction( TSD, entry->next, TSD->currentnode,
652 args );
653
654 if (ptr==NULL) /* fixes bug 592393 */
655 exiterror( ERR_NO_DATA_RETURNED, 1, tmpstr_of( TSD, thisptr->name ) );
656
657 if (TSD->trace_stat=='I')
658 tracevalue( TSD, ptr, 'F' );
659
660 RETURN_NEW( ptr );
661 }
662 }
663 /* THIS IS MEANT TO FALL THROUGH! */
664 case X_IS_BUILTIN:
665 case X_EX_FUNC:
666 {
667 streng *ptr ;
668
669 if ((ptr=buildtinfunc( TSD, thisptr )) != NOFUNC)
670 {
671 if (thisptr->type != X_IS_BUILTIN)
672 thisptr->type = X_IS_BUILTIN ;
673
674 if (!ptr)
675 exiterror( ERR_NO_DATA_RETURNED, 1, tmpstr_of( TSD, thisptr->name ) ) ;
676
677 if (TSD->trace_stat=='I')
678 tracevalue( TSD, ptr, 'F' ) ;
679
680 RETURN_NEW( ptr );
681 }
682 else
683 thisptr->type = X_IS_EXTERNAL ;
684 }
685 /* THIS IS MEANT TO FALL THROUGH! */
686 case X_IS_EXTERNAL:
687 {
688 streng *ptr, *command;
689 int stackmark,len,err;
690 paramboxptr args, targs;
691
692 if ( TSD->restricted )
693 exiterror( ERR_RESTRICTED, 5 );
694
695 update_envirs( TSD, TSD->currlevel );
696
697 args = initplist( TSD, thisptr );
698 stackmark = pushcallstack( TSD, TSD->currentnode );
699 ptr = execute_external( TSD, thisptr->name,
700 args,
701 TSD->systeminfo->environment,
702 &err,
703 TSD->systeminfo->hooks,
704 INVO_FUNCTION );
705 popcallstack( TSD, stackmark );
706
707 if ( err == -ERR_PROG_UNREADABLE )
708 {
709 /*
710 * "thisptr->name" wasn't a Rexx program, so
711 * see if it is an OS command.
712 * Only do thisptr if the OPTIONS EXT_COMMANDS_AS_FUNCS is
713 * set and STRICT_ANSI is NOT set.
714 */
715 if ( get_options_flag( TSD->currlevel, EXT_EXT_COMMANDS_AS_FUNCS )
716 && !get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
717 {
718 len = Str_len( thisptr->name );
719 for( targs = args; targs; targs = targs->next )
720 {
721 if ( targs->value )
722 len += 1 + Str_len( targs->value );
723 }
724 command = Str_makeTSD( len );
725 command = Str_catTSD( command, thisptr->name );
726 for( targs = args; targs; targs = targs->next )
727 {
728 if ( targs->value )
729 {
730 command = Str_catstrTSD( command, " " );
731 command = Str_catTSD( command, targs->value );
732 }
733 }
734 ptr = run_popen( TSD, command, TSD->currlevel->environment );
735 if ( ptr != NULL )
736 err = 0;
737 Free_stringTSD( command );
738 }
739 }
740
741 deallocplink( TSD, args );
742
743 if ( ptr && ( TSD->trace_stat == 'I' ) )
744 tracevalue( TSD, ptr, 'F' );
745
746 if ( err == -ERR_PROG_UNREADABLE )
747 {
748 exiterror( ERR_ROUTINE_NOT_FOUND, 1, tmpstr_of( TSD, thisptr->name ) );
749 }
750 else if ( err )
751 {
752 post_process_system_call( TSD, thisptr->name, -err, NULL, thisptr );
753 }
754
755 if ( !ptr )
756 exiterror( ERR_NO_DATA_RETURNED, 1, tmpstr_of( TSD, thisptr->name ) );
757
758 RETURN_NEW( ptr );
759 }
760
761 case X_CONCAT:
762 case X_CONCATASSIGN:
763 case X_SPACE:
764 {
765 char *cptr ;
766 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
767 if ( thisptr->type == X_CONCATASSIGN )
768 {
769 strtwo = (thisptr->p[1]) ? evaluate( TSD, thisptr->p[1], &stmp2 ) : nullstringptr() ;
770 stmp2 = NULL;
771 }
772 else
773 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
774 strthr = Str_makeTSD(Str_len(strone)+Str_len(strtwo)+1) ;
775 cptr = strthr->value ;
776 memcpy( cptr, strone->value, strone->len ) ;
777 cptr += strone->len ;
778 if (thisptr->type==X_SPACE)
779 *(cptr++) = ' ' ;
780
781 memcpy( cptr, strtwo->value, strtwo->len ) ;
782 strthr->len = (cptr-strthr->value) + strtwo->len ;
783
784 if ( stmp1) FREE_TMP_STRING( stmp1 );
785 if ( stmp2) FREE_TMP_STRING( stmp2 );
786
787 if (TSD->trace_stat=='I')
788 tracevalue( TSD, strthr, 'O' ) ;
789
790
791 RETURN_NEW( strthr );
792 }
793
794
795 case X_LOG_NOT:
796 case X_LOG_OR:
797 case X_LOG_AND:
798 case X_LOG_XOR:
799 case X_S_DIFF:
800 case X_S_EQUAL:
801 case X_EQUAL:
802 case X_GT:
803 case X_LT:
804 case X_GTE:
805 case X_LTE:
806 case X_DIFF:
807 case X_SEQUAL:
808 case X_SGT:
809 case X_SLT:
810 case X_SGTE:
811 case X_SLTE:
812 case X_SDIFF:
813 case X_NEQUAL:
814 case X_NGT:
815 case X_NLT:
816 case X_NGTE:
817 case X_NLTE:
818 case X_NDIFF:
819 case X_S_NGT:
820 case X_S_NLT:
821 case X_S_GT:
822 case X_S_GTE:
823 case X_S_LT:
824 case X_S_LTE:
825 stmp1 = bool_to_str( TSD, isboolean( TSD, thisptr, 0, NULL )) ;
826 RETURN_NEW( stmp1 );
827
828 default:
829 exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ;
830 return NULL ;
831 }
832 #undef RETURN_NEW
833 }
834
835
836 /*
837 * isboolean evaluates a boolean expression and returns 0 for false, another
838 * value for true. "thisptr" is the current evaluation tree.
839 * Note: This is one of the most time-consuming routines. Be careful.
840 */
841 int isboolean( tsd_t *TSD, nodeptr thisptr, int suberror, const char *op )
842 {
843 streng *strone,*strtwo;
844 streng *stmp1,*stmp2;
845 int tmp,sint;
846 num_descr *ntmp;
847
848 switch ( thisptr->type )
849 {
850 case 0:
851 case 255:
852 case X_PLUSS:
853 case X_MINUS:
854 case X_MULT:
855 case X_DEVIDE:
856 case X_MODULUS:
857 case X_INTDIV:
858 case X_EXP:
859 case X_U_MINUS:
860 case X_U_PLUSS:
861 ntmp = NULL;
862 tmp = num_to_bool( TSD, calcul( TSD, thisptr, &ntmp, SIDE_LEFT, thisptr->type ), thisptr->name, suberror, op ) ;
863 if (ntmp)
864 {
865 FreeTSD( ntmp->num ) ;
866 FreeTSD( ntmp ) ;
867 }
868 return tmp ;
869
870 case X_STRING:
871 case X_CON_SYMBOL:
872 if ( !thisptr->u.number )
873 thisptr->u.number = get_a_descr( TSD, NULL, 0, thisptr->name ) ;
874 if ( Str_len( thisptr->name ) != 1 )
875 {
876 /* fixes bug 1111931, "01" is not a logical value in ANSI */
877 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
878 {
879 if ( op )
880 {
881 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, thisptr->name ) );
882 }
883 else
884 {
885 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, thisptr->name ) );
886 }
887 }
888 }
889 return num_to_bool( TSD, thisptr->u.number, thisptr->name, suberror, op ) ;
890
891 case X_SIM_SYMBOL:
892 case X_STEM_SYMBOL:
893 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
894 {
895 /* fixes bug 1111931 */
896 stmp1 = (streng *) shortcut( TSD, thisptr );
897 tmp = Str_val( stmp1 )[0] - '0';
898 if ( ( Str_len( stmp1 ) != 1 ) || ( ( tmp != 0 ) && ( tmp != 1 ) ) )
899 {
900 if ( op )
901 {
902 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, tmpstr_of( TSD, stmp1 ) );
903 }
904 else
905 {
906 exiterror( ERR_UNLOGICAL_VALUE, suberror, tmpstr_of( TSD, stmp1 ) );
907 }
908 }
909 return tmp;
910 }
911 return num_to_bool( TSD, shortcutnum( TSD, thisptr ), thisptr->name, suberror, op ) ;
912
913 case X_HEAD_SYMBOL:
914 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
915 {
916 /* fixes bug 1111931 */
917 volatile char *s;
918
919 stmp1 = (streng *) fix_compound( TSD, thisptr, NULL );
920 tmp = Str_val( stmp1 )[0] - '0';
921 if ( ( Str_len( stmp1 ) != 1 ) || ( ( tmp != 0 ) && ( tmp != 1 ) ) )
922 {
923 s = tmpstr_of( TSD, stmp1 );
924 Free_stringTSD( stmp1 );
925 if ( op )
926 {
927 exiterror( ERR_UNLOGICAL_VALUE, suberror, op, s );
928 }
929 else
930 {
931 exiterror( ERR_UNLOGICAL_VALUE, suberror, s );
932 }
933 }
934 Free_stringTSD( stmp1 );
935 return tmp;
936 }
937 return num_to_bool( TSD, fix_compoundnum( TSD, thisptr, NULL, NULL ), thisptr->name, suberror, op );
938
939 case X_IN_FUNC:
940 case X_IS_INTERNAL:
941 case X_IS_BUILTIN:
942 case X_EX_FUNC:
943 case X_IS_EXTERNAL:
944 case X_CONCAT:
945 case X_SPACE:
946 tmp = str_to_bool( evaluate( TSD, thisptr, &stmp1 ) );
947 FREE_TMP_STRING( stmp1 );
948 return tmp;
949
950 case X_LOG_NOT:
951 sint = !isboolean( TSD, thisptr->p[0], 6, "\\" ) ;
952 if (TSD->trace_stat=='I')
953 tracebool( TSD, sint, 'U' ) ;
954 return sint ;
955
956 case X_LOG_OR:
957 case X_ORASSIGN:
958 sint = ( isboolean(TSD, thisptr->p[0], 5, "|") | isboolean( TSD, thisptr->p[1], 6, "|" )) ;
959 if (TSD->trace_stat=='I')
960 tracebool( TSD, sint, 'U' ) ;
961 return sint ;
962
963 case X_LOG_AND:
964 case X_ANDASSIGN:
965 sint = ( isboolean(TSD, thisptr->p[0], 5, "&" ) & isboolean( TSD, thisptr->p[1], 6, "&" )) ;
966 if (TSD->trace_stat=='I')
967 tracebool( TSD, sint, 'U' ) ;
968 return sint ;
969
970 case X_LOG_XOR:
971 case X_XORASSIGN:
972 /* Well, sort of ... */
973 sint = ( isboolean( TSD, thisptr->p[0], 5, "&&" ) ^ isboolean( TSD, thisptr->p[1], 6, "&&" )) ;
974 if (TSD->trace_stat=='I')
975 tracebool( TSD, sint, 'U' ) ;
976 return sint ;
977
978 case X_EQUAL:
979 case X_DIFF:
980 case X_GT:
981 case X_GTE:
982 case X_LT:
983 case X_LTE:
984 {
985 int type ;
986 compflags flags ;
987 num_descr *rnum, *lnum ;
988 streng *lval, *rval ;
989
990 flags = thisptr->u.flags ;
991 rnum = lnum = 0 ;
992 rval = lval = NULL ;
993 stmp1 = stmp2 = NULL ;
994
995 if (flags.lnum)
996 {
997 if ( !thisptr->p[0]->u.number )
998 thisptr->p[0]->u.number = get_a_descr( TSD, NULL, 0, thisptr->p[0]->name );
999
1000 lnum = thisptr->p[0]->u.number ;
1001 if (TSD->trace_stat=='I')
1002 tracenumber( TSD, lnum, 'L' ) ;
1003 }
1004 else if (flags.lsvar)
1005 lnum = shortcutnum( TSD, thisptr->p[0] ) ;
1006 else if (flags.lcvar)
1007 lnum = fix_compoundnum( TSD, thisptr->p[0], NULL, NULL );
1008
1009 if (!lnum)
1010 lval = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1011
1012 if (flags.rnum)
1013 {
1014 if ( !thisptr->p[1]->u.number )
1015 thisptr->p[1]->u.number = get_a_descr( TSD, NULL, 0, thisptr->p[1]->name );
1016
1017 rnum = thisptr->p[1]->u.number ;
1018 if (TSD->trace_stat=='I')
1019 tracenumber( TSD, rnum, 'L' ) ;
1020 }
1021 else if (flags.rsvar)
1022 rnum = shortcutnum( TSD, thisptr->p[1] ) ;
1023 else if (flags.rcvar)
1024 rnum = fix_compoundnum( TSD, thisptr->p[1], NULL, NULL );
1025
1026 if (!rnum)
1027 rval = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1028
1029 if (!lnum && !getdescr( TSD, lval, &TSD->ldes ))
1030 lnum = &TSD->ldes ;
1031
1032 if (!rnum && !getdescr( TSD, rval, &TSD->rdes ))
1033 rnum = &TSD->rdes ;
1034
1035 if (rnum && lnum)
1036 tmp = string_test( TSD, lnum, rnum ) ;
1037 else
1038 {
1039 unsigned char *s1,*s2,*e1,*e2;
1040
1041 if ( !lval )
1042 {
1043 assert( !stmp1 );
1044 stmp1 = lval = str_norm( TSD, lnum, NULL );
1045 }
1046
1047 if ( !rval )
1048 {
1049 assert( !stmp2 );
1050 stmp2 = rval = str_norm( TSD, rnum, NULL );
1051 }
1052
1053 s1 = (unsigned char *) lval->value;
1054 s2 = (unsigned char *) rval->value;
1055 e1 = (unsigned char *) s1 + lval->len;
1056 e2 = (unsigned char *) s2 + rval->len;
1057
1058 strip_whitespace( TSD, &s1, &e1, &s2, &e2 );
1059
1060 if ( s1 == e1 && s2 == e2 )
1061 tmp = 0;
1062 else if ( s1 < e1 && s2 < e2 )
1063 tmp = ( *s1 < *s2 ) ? -1 : 1;
1064 else
1065 tmp = ( s1 < e1 ) ? 1 : -1;
1066 }
1067
1068 FREE_TMP_STRING( stmp1 );
1069 FREE_TMP_STRING( stmp2 );
1070
1071 type = thisptr->type ;
1072 if (tmp==0)
1073 sint = (type==X_GTE || type==X_LTE || type==X_EQUAL) ;
1074 else if (tmp>0)
1075 sint = (type==X_GT || type==X_GTE || type==X_DIFF) ;
1076 else
1077 sint = (type==X_LT || type==X_LTE || type==X_DIFF) ;
1078
1079 if (TSD->trace_stat=='I')
1080 tracebool( TSD, sint, 'O' ) ;
1081
1082 return sint ;
1083 }
1084
1085 case X_SGT:
1086 case X_SLT:
1087 case X_SLTE:
1088 case X_SGTE:
1089 case X_SEQUAL:
1090 case X_SDIFF:
1091 { /* string comparison */
1092 unsigned char *s1, *s2, *e1, *e2 ;
1093 int type ;
1094
1095 type = thisptr->type ;
1096 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1097 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1098
1099 s1 = (unsigned char *) strone->value ;
1100 s2 = (unsigned char *) strtwo->value ;
1101 e1 = (unsigned char *) s1 + strone->len ;
1102 e2 = (unsigned char *) s2 + strtwo->len ;
1103
1104 strip_whitespace( TSD, &s1, &e1, &s2, &e2 );
1105
1106 if (s1==e1 && s2==e2)
1107 tmp = 0 ;
1108 else if (s1<e1 && s2<e2)
1109 tmp = (*s1<*s2) ? -1 : 1 ;
1110 else
1111 tmp = (s1<e1) ? 1 : -1 ;
1112
1113 FREE_TMP_STRING( stmp1 );
1114 FREE_TMP_STRING( stmp2 );
1115
1116 if (tmp==0)
1117 sint = (type==X_SGTE || type==X_SLTE || type==X_SEQUAL) ;
1118 else if (tmp>0)
1119 sint = (type==X_SGT || type==X_SGTE || type==X_SDIFF) ;
1120 else
1121 sint = (type==X_SLT || type==X_SLTE || type==X_SDIFF) ;
1122
1123 if (TSD->trace_stat=='I')
1124 tracebool( TSD, sint, 'O' ) ;
1125
1126 return sint ;
1127 }
1128
1129 case X_NGT:
1130 case X_NLT:
1131 case X_NLTE:
1132 case X_NGTE:
1133 case X_NEQUAL:
1134 case X_NDIFF:
1135 {
1136 /* numeric NOT comparison */
1137 int type ;
1138 num_descr *ntmp1, *ntmp2 ;
1139 num_descr *numone, *numtwo ;
1140
1141 type = thisptr->type ;
1142
1143 ntmp1 = ntmp2 = NULL;
1144 numone = calcul( TSD, thisptr->p[0], &ntmp1, SIDE_LEFT, thisptr->type ) ;
1145 numtwo = calcul( TSD, thisptr->p[1], &ntmp2, SIDE_RIGHT, thisptr->type ) ;
1146 tmp = string_test( TSD, numone, numtwo ) ;
1147
1148 if (ntmp1)
1149 {
1150 FreeTSD( ntmp1->num ) ;
1151 FreeTSD( ntmp1 ) ;
1152 }
1153 if (ntmp2)
1154 {
1155 FreeTSD( ntmp2->num ) ;
1156 FreeTSD( ntmp2 ) ;
1157 }
1158
1159 if (tmp==0)
1160 sint = (type==X_NGTE || type==X_NLTE || type==X_NEQUAL) ;
1161 else if (tmp>0)
1162 sint = (type==X_NGT || type==X_NGTE || type==X_NDIFF) ;
1163 else
1164 sint = (type==X_NLT || type==X_NLTE || type==X_NDIFF) ;
1165
1166 if (TSD->trace_stat=='I')
1167 tracebool( TSD, sint, 'O' ) ;
1168
1169 return sint ;
1170 }
1171
1172 case X_S_DIFF:
1173 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1174 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1175 tmp = Str_cmp(strone,strtwo)!=0 ;
1176
1177 FREE_TMP_STRING( stmp1 );
1178 FREE_TMP_STRING( stmp2 );
1179
1180 if (TSD->trace_stat=='I')
1181 tracebool( TSD, tmp, 'O' ) ;
1182
1183 return tmp ;
1184
1185
1186 case X_S_EQUAL:
1187 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1188 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1189 tmp = Str_cmp(strone,strtwo)==0 ;
1190
1191 FREE_TMP_STRING( stmp1 );
1192 FREE_TMP_STRING( stmp2 );
1193
1194 if (TSD->trace_stat=='I')
1195 tracebool( TSD, tmp, 'O' ) ;
1196
1197 return tmp ;
1198
1199 case X_S_NGT:
1200 case X_S_NLT:
1201 { /* strict string NOT comparison */
1202 unsigned char *s1, *s2, *e1, *e2 ;
1203 int type ;
1204
1205 type = thisptr->type ;
1206 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1207 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1208
1209 s1 = (unsigned char *) strone->value ;
1210 s2 = (unsigned char *) strtwo->value ;
1211 e1 = (unsigned char *) s1 + strone->len ;
1212 e2 = (unsigned char *) s2 + strtwo->len ;
1213 /*
1214 * same compare as non-strict except that leading and trailing spaces
1215 * are retained for comparison.
1216 */
1217 for (;(s1<e1)&&(s2<e2)&&(*s1==*s2);s1++,s2++) ;
1218 if (s1==e1 && s2==e2)
1219 tmp = 0 ;
1220 else if (s1<e1 && s2<e2)
1221 tmp = (*s1<*s2) ? 1 : -1 ;
1222 else
1223 tmp = (s1<e1) ? -1 : 1 ;
1224
1225 FREE_TMP_STRING( stmp1 );
1226 FREE_TMP_STRING( stmp2 );
1227
1228 if (tmp==0)
1229 sint = 1;
1230 else if (tmp>0)
1231 sint = (type==X_S_NGT) ;
1232 else
1233 sint = (type==X_S_NLT) ;
1234
1235 if (TSD->trace_stat=='I')
1236 tracebool( TSD, sint, 'O' ) ;
1237
1238 return sint ;
1239 }
1240
1241 case X_S_GT:
1242 case X_S_GTE:
1243 case X_S_LT:
1244 case X_S_LTE:
1245 { /* strict string comparison */
1246 unsigned char *s1, *s2, *e1, *e2 ;
1247 int type ;
1248
1249 type = thisptr->type ;
1250 strone = evaluate( TSD, thisptr->p[0], &stmp1 ) ;
1251 strtwo = evaluate( TSD, thisptr->p[1], &stmp2 ) ;
1252
1253 s1 = (unsigned char *) strone->value ;
1254 s2 = (unsigned char *) strtwo->value ;
1255 e1 = (unsigned char *) s1 + strone->len ;
1256 e2 = (unsigned char *) s2 + strtwo->len ;
1257 /*
1258 * same compare as non-strict except that leading and trailing spaces
1259 * are retained for comparison.
1260 */
1261 for (;(s1<e1)&&(s2<e2)&&(*s1==*s2);s1++,s2++) ;
1262 if (s1==e1 && s2==e2)
1263 tmp = 0 ;
1264 else if (s1<e1 && s2<e2)
1265 tmp = (*s1<*s2) ? -1 : 1 ;
1266 else
1267 tmp = (s1<e1) ? 1 : -1 ;
1268
1269 FREE_TMP_STRING( stmp1 );
1270 FREE_TMP_STRING( stmp2 );
1271
1272 if (tmp==0)
1273 sint = (type==X_S_GTE || type==X_S_LTE) ;
1274 else if (tmp>0)
1275 sint = (type==X_S_GT || type==X_S_GTE ) ;
1276 else
1277 sint = (type==X_S_LT || type==X_S_LTE ) ;
1278
1279 if (TSD->trace_stat=='I')
1280 tracebool( TSD, sint, 'O' ) ;
1281
1282 return sint ;
1283 }
1284
1285
1286 default:
1287 exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ;
1288 return 0 ;
1289 }
1290 }
1291