1 /*
2 * Generic Call Interface for Rexx
3 * Copyright � 2003-2004, Florian Gro�e-Coosmann
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 *
21 * This file parses an input tree for GCI usage. Note that the value's tree
22 * isn't parsed. It is accessed directly after checking the generated
23 * internal tree.
24 */
25
26 #include "gci.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 /*
33 * We don't want to pass n+1 elements on each recursive call. We put all
34 * needed things together and pass it in one chunk.
35 * Most elements are copied from GCI_parsetree near the file's end. Look
36 * there.
37 */
38 typedef struct {
39 void *hidden;
40 GCI_str *buffer; /* The name of the current variable. */
41 int depth; /* starting with 0 */
42 GCI_result (*callback)(int depth,
43 int itemnumber,
44 void *arg,
45 const GCI_parseinfo *info);
46 void *arg;
47 GCI_str tempbuf; /* buffer for the content of each structure line*/
48 char helper[80]; /* buffer for the textual iterator */
49 int recurCount; /* counts recursions in LIKE */
50 const char *prefixChar; /* must be used in front of stem names */
51 } callblock;
52
53 /*
54 * checkname tries to interpret the first characters of a string and returns
55 * the symbolic name of it.
56 * The string is taken from *str with a length of *size.
57 *
58 * On success (return value not GCI_unknown) *str is set to the first
59 * character after the recognized word and *size is decremented by the
60 * length of the recognized word.
61 *
62 * This function doesn't jump over leading whitespaces and the function
63 * doesn't check for any word delimiters. *str must have been uppercased.
64 */
checkname(const char ** str,int * size)65 static GCI_basetype checkname( const char **str,
66 int *size )
67 {
68 static const struct {
69 GCI_basetype type;
70 int length;
71 const char *name;
72 } list[] = {
73 { GCI_array, 5, "ARRAY" },
74 { GCI_char, 4, "CHAR" },
75 { GCI_container, 9, "CONTAINER" },
76 { GCI_float, 5, "FLOAT" },
77 { GCI_indirect, 8, "INDIRECT" },
78 { GCI_like, 4, "LIKE" },
79 { GCI_integer, 7, "INTEGER" },
80 { GCI_raw, 3, "RAW", },
81 { GCI_string, 6, "STRING" },
82 { GCI_unsigned, 8, "UNSIGNED" }
83 };
84 const char *s = *str;
85 int i, l, len = *size;
86
87 for ( i = 0; i < (int) elements( list ); i++ )
88 {
89 l = list[i].length;
90 if ( len < l )
91 continue;
92 if ( memcmp( s, list[i].name, l ) == 0 )
93 {
94 *str = s + l;
95 *size -= l;
96 return list[i].type;
97 }
98 }
99 return GCI_unknown;
100 }
101
102 /*
103 * decode decodes one line of the type structure named str into the
104 * a parseinfo block called pi. depth is the current depth which may be 0.
105 *
106 * newName is set in case of ARRAY (to the empty string) or CONTAINER. In this
107 * case is set to the LIKE parameter or to the empty string.
108 *
109 * Leading and trailing spaces are ignored completely, "INDIRECTFLOAT32" is
110 * acceptable. Differences in case are ignored.
111 *
112 * THE GENERATED TYPE MAY HAVE AN ILLEGAL BIT SIZE. IT ISN'T CHECKED ALWAYS!
113 *
114 * Return values:
115 * GCI_OK: Line understood, *pi filled.
116 * GCI_UnsupportedType: Wrong type of input, e.g. FLOAT31 or the empty string.
117 * GCI_NoBaseType: The type won't fit the requirements for basic types.
118 */
decode(void * hidden,const GCI_str * str,GCI_parseinfo * pi,int depth,GCI_str * newName)119 static GCI_result decode( void *hidden,
120 const GCI_str *str,
121 GCI_parseinfo *pi,
122 int depth,
123 GCI_str *newName )
124 {
125 const char *ptr = GCI_ccontent( str );
126 int size = GCI_strlen( str );
127
128 /*
129 * Chop off leading and trailing spaces. We really need it.
130 */
131 while ( ( size > 0 ) && GCI_isspace( *ptr ) )
132 {
133 ptr++;
134 size--;
135 }
136 while ( ( size > 0 ) && GCI_isspace( ptr[size - 1] ) )
137 size--;
138
139 memset( pi, 0, sizeof( GCI_parseinfo ) );
140
141 if ( ( pi->type = checkname( &ptr, &size ) ) == GCI_unknown )
142 return GCI_UnsupportedType;
143 if ( pi->type == GCI_indirect )
144 {
145 while ( ( size > 0 ) && GCI_isspace( *ptr ) )
146 {
147 ptr++;
148 size--;
149 }
150 pi->type = checkname( &ptr, &size );
151 if ( ( pi->type == GCI_unknown ) || ( pi->type == GCI_indirect ) )
152 return GCI_UnsupportedType;
153 pi->indirect = 1;
154 }
155 else
156 pi->indirect = 0;
157
158 /*
159 * Check for a size operand.
160 */
161 while ( ( size > 0 ) && GCI_isspace( *ptr ) )
162 {
163 ptr++;
164 size--;
165 }
166
167 /*
168 * We may have a size operand only if not an array or container is
169 * processed!
170 * This implementation shall support plain types like "INTEGER" without
171 * any bit size.
172 */
173 switch ( pi->type )
174 {
175 case GCI_container:
176 if ( size > 0 )
177 {
178 GCI_str tmp;
179
180 if ( checkname( &ptr, &size ) != GCI_like )
181 return GCI_UnsupportedType;
182 while ( ( size > 0 ) && GCI_isspace( *ptr ) )
183 {
184 ptr++;
185 size--;
186 }
187 if ( size == 0 )
188 {
189 /*
190 * Single "like" after "container".
191 */
192 return GCI_UnsupportedType;
193 }
194 while ( GCI_isspace( ptr[size - 1] ) )
195 size--;
196 /*
197 * Cut off a final dot, we append one later.
198 */
199 if ( ptr[size - 1] == '.' )
200 {
201 /*
202 * Check for single "." as stem.
203 */
204 if ( --size == 0 )
205 return GCI_UnsupportedType;
206 }
207 if ( GCI_stralloc( hidden, newName, size + 256 ) != GCI_OK )
208 return GCI_NoMemory;
209 GCI_strfromascii( &tmp, (char *) ptr, size );
210 GCI_strsetlen( &tmp, size );
211 GCI_strcpy( newName, &tmp );
212 size = 0;
213 }
214 /* fall through */
215
216 case GCI_array:
217 if ( size > 0 )
218 return GCI_UnsupportedType;
219 if ( ( depth == 0 ) && !pi->indirect )
220 {
221 if ( GCI_content( newName ) != NULL )
222 GCI_strfree( hidden, newName );
223 return GCI_NoBaseType;
224 }
225 pi->size = 0;
226 return GCI_OK;
227
228 case GCI_integer:
229 if ( size == 0 )
230 pi->size = 8 * sizeof( int );
231 break;
232
233 case GCI_unsigned:
234 if ( size == 0 )
235 pi->size = 8 * sizeof( unsigned );
236 break;
237
238 case GCI_float:
239 if ( size == 0 )
240 pi->size = 8 * sizeof( double ); /* surprised? */
241 break;
242
243 case GCI_char:
244 if ( size == 0 )
245 pi->size = 8 * 1; /* always, even in unicode or utf8 systems */
246 break;
247
248 case GCI_string:
249 case GCI_raw:
250 if ( size == 0 ) /* length must be supplied */
251 return GCI_UnsupportedType;
252 break;
253
254 default:
255 return GCI_UnsupportedType;
256 }
257
258 if ( size > 0 )
259 {
260 if ( GCI_string2bin( hidden,
261 ptr,
262 size,
263 &pi->size,
264 sizeof( pi->size ),
265 GCI_unsigned ) != GCI_OK )
266 return GCI_UnsupportedType;
267 }
268
269 if ( ( ( pi->type == GCI_string ) || ( pi->type == GCI_raw ) ) &&
270 ( pi->size > 0 ) )
271 return GCI_OK;
272
273 /*
274 * A byte has 8 bit, always! We don't support PDP10.
275 */
276 if ( ( pi->type != GCI_string ) && ( pi->type != GCI_raw ) )
277 {
278 if ( pi->size % 8 )
279 return GCI_UnsupportedType;
280 pi->size /= 8;
281 }
282
283 return GCI_validate( pi->size, pi->type, depth || pi->indirect );
284 }
285
286 /*
287 * parse is the local implementation of GCI_parsetree below. Most parameters
288 * are in *cb. Have a look at callblock at top of file or at GCI_parsestring
289 * below.
290 * itemnumber is the iterator of the container item or array item, the later
291 * always has number 1.
292 *
293 * The function loops over a type structure tree, the current node name is
294 * placed in cb->buffer. We do a depth-first iteration.
295 *
296 * Indirect array[x] are replaced by a combination of
297 * indirect container[1], array[x]. This allows a better addressing later.
298 * The indirect container is flagged as "generated" in this case.
299 *
300 * THE GENERATED TYPES MAY HAVE ILLEGAL BIT SIZES. IT ISN'T CHECKED ALWAYS!
301 *
302 * Return values:
303 * GCI_OK: Everything is fine.
304 *
305 * In case of an error cb->buffer will contain the
306 * variable's name where the problem raises first.
307 *
308 * GCI_MissingName: A variable's name isn't set. This is the equivalence
309 * for GCI_MissingValue in the type parsing step. The
310 * system may or may not raise a NOVALUE condition instead
311 * depending on the implementation.
312 * GCI_BufferTooSmall: The variable's name buffer cb->buffer can't hold the
313 * complete variable's name or the type string exceeds
314 * 256 byte.
315 * GCI_IllegalName: The variables name in cb->buffer is illegal in terms of
316 * Rexx. In general, the basename of GCI_paretree is
317 * wrong.
318 * GCI_RexxError: An unexpected error is returned by the interpreter
319 * while trying to access Rexx variables.
320 * GCI_UnsupportedType: Wrong type of input, e.g. FLOAT31 or the empty string
321 * in a type description string. Another reason is an
322 * internal error since the default sizes for "unsigned"
323 * and "integer" are not supported.
324 * GCI_WrongInput: Strange characters occur in the input string as the
325 * bit size of the type.
326 * GCI_NumberRange: Number to small or big to fit into the desired type
327 * with the desired destbyte-size. This applies to the
328 * element count of an "ARRAY" or "CONTAINER" type size
329 * or the bit size of the plain type.
330 * GCI_NoBaseType: The type won't fit the requirements for basic types.
331 *
332 * And there are numerous other possible errors returned by cb->callback.
333 */
parse(callblock * cb,int itemnumber)334 static GCI_result parse( callblock *cb,
335 int itemnumber )
336 {
337 GCI_parseinfo pi;
338 GCI_str newName;
339 static const GCI_parseinfo indirectArray = { GCI_container, 1, 1, 1 };
340 GCI_result rc;
341 unsigned i;
342 int origlen = GCI_strlen( cb->buffer );
343
344 GCI_strfromascii( &newName, NULL, 0 );
345 GCI_strcats( cb->buffer, "." );
346 GCI_strcats( cb->buffer, cb->prefixChar );
347 if ( ( rc = GCI_strcats( cb->buffer, "TYPE" ) ) != GCI_OK )
348 return rc;
349 if ( ( rc = GCI_readRexx( cb->hidden,
350 cb->buffer,
351 &cb->tempbuf,
352 0,
353 1,
354 NULL ) ) != GCI_OK )
355 {
356 if ( rc == GCI_MissingValue )
357 rc = GCI_MissingName;
358 return rc;
359 }
360 GCI_uppercase( cb->hidden, &cb->tempbuf );
361
362 if ( ( rc = decode( cb->hidden, &cb->tempbuf, &pi, cb->depth, &newName ) )
363 != GCI_OK )
364 return rc;
365 GCI_strsetlen( cb->buffer, origlen );
366
367 if ( GCI_content( &newName ) != NULL ) {
368 if (cb->recurCount++ >= 100) {
369 GCI_strfree( cb->hidden, &newName );
370 return GCI_NestingOverflow;
371 }
372 GCI_strswap( &newName, cb->buffer );
373 origlen = GCI_strlen( cb->buffer );
374 }
375
376 /*
377 * Alright, we have it, but we have to fetch the number of elements
378 * if we parse a container or an array.
379 */
380 if ( ( pi.type == GCI_container ) || ( pi.type == GCI_array ) )
381 {
382 if ( ( rc = GCI_strcats( cb->buffer, ".0" ) ) != GCI_OK )
383 {
384 /*
385 * The tmp buffer persists for error displaying, kill the other.
386 */
387 if ( GCI_content( &newName ) != NULL )
388 GCI_strfree( cb->hidden, &newName );
389 return rc;
390 }
391 if ( ( rc = GCI_readRexx( cb->hidden,
392 cb->buffer,
393 &cb->tempbuf,
394 0,
395 1,
396 NULL ) ) != GCI_OK )
397 {
398 if ( rc == GCI_MissingValue )
399 rc = GCI_MissingName;
400 /*
401 * The tmp buffer persists for error displaying, kill the other.
402 */
403 if ( GCI_content( &newName ) != NULL )
404 GCI_strfree( cb->hidden, &newName );
405 return rc;
406 }
407
408 /*
409 * The result shall be a whole, positive number. Lets see...
410 */
411 if ( ( rc = GCI_string2bin( cb->hidden,
412 GCI_content( &cb->tempbuf ),
413 GCI_strlen( &cb->tempbuf ),
414 &pi.size,
415 sizeof( pi.size ),
416 GCI_unsigned ) ) != GCI_OK )
417 {
418 /*
419 * The tmp buffer persists for error displaying, kill the other.
420 */
421 if ( GCI_content( &newName ) != NULL )
422 GCI_strfree( cb->hidden, &newName );
423 return rc;
424 }
425 if ( pi.size == 0 )
426 {
427 /*
428 * The tmp buffer persists for error displaying, kill the other.
429 */
430 if ( GCI_content( &newName ) != NULL )
431 GCI_strfree( cb->hidden, &newName );
432 return GCI_NumberRange;
433 }
434 GCI_strsetlen( cb->buffer, origlen );
435 }
436
437 if ( pi.indirect && ( pi.type == GCI_array ) )
438 {
439 if ( ( rc = cb->callback( cb->depth,
440 itemnumber,
441 cb->arg,
442 &indirectArray) ) != GCI_OK )
443 return rc;
444 pi.indirect = 0;
445 if ( ( rc = cb->callback( cb->depth,
446 itemnumber,
447 cb->arg,
448 &pi) ) != GCI_OK )
449 return rc;
450 pi.indirect = 1;
451 }
452 else
453 {
454 if ( ( rc = cb->callback( cb->depth,
455 itemnumber,
456 cb->arg,
457 &pi) ) != GCI_OK )
458 {
459 /*
460 * The tmp buffer persists for error displaying, kill the other.
461 */
462 if ( GCI_content( &newName ) != NULL )
463 GCI_strfree( cb->hidden, &newName );
464 return rc;
465 }
466 }
467
468 if ( ( pi.type != GCI_container ) && ( pi.type != GCI_array ) )
469 return GCI_OK;
470
471 cb->depth++;
472 for ( i = 0; i < pi.size; i++ )
473 {
474 sprintf( cb->helper, ".%u", i + 1 );
475
476 if ( ( rc = GCI_strcats( cb->buffer, cb->helper ) ) != GCI_OK )
477 {
478 /*
479 * The tmp buffer persists for error displaying, kill the other.
480 */
481 if ( GCI_content( &newName ) != NULL )
482 GCI_strfree( cb->hidden, &newName );
483 return rc;
484 }
485 if ( ( rc = parse( cb, i ) ) != GCI_OK )
486 {
487 /*
488 * The tmp buffer persists for error displaying, kill the other.
489 */
490 if ( GCI_content( &newName ) != NULL )
491 GCI_strfree( cb->hidden, &newName );
492 return rc;
493 }
494 GCI_strsetlen( cb->buffer, origlen );
495
496 if ( pi.type == GCI_array )
497 break;
498 }
499 cb->depth--;
500 cb->recurCount--;
501 if ( GCI_content( &newName ) != NULL )
502 {
503 GCI_strswap( &newName, cb->buffer );
504 GCI_strfree( cb->hidden, &newName );
505 }
506
507 if ( pi.indirect && ( pi.type == GCI_array ) )
508 {
509 pi.indirect = 0;
510 if ( ( rc = cb->callback( cb->depth, -1, cb->arg, &pi) ) != GCI_OK )
511 return rc;
512 return cb->callback( cb->depth, -1, cb->arg, &indirectArray );
513 }
514 return cb->callback( cb->depth, -1, cb->arg, &pi );
515 }
516
517 /*****************************************************************************
518 *****************************************************************************
519 ** GLOBAL FUNCTIONS *********************************************************
520 *****************************************************************************
521 *****************************************************************************/
522
523 /*
524 * GCI_parsetree tries to interpret the type information of the first
525 * generation. That are the branches "branch.return", "branch.1",
526 * "branch.2", ... The value of "base" is such an identifier.
527 * base must be a string large enough to hold the longest type variable name
528 * which occurs in the tree including the branch name. This argument will
529 * contain the variable's name which forces the error in case of an error.
530 * The buffer is always modified.
531 *
532 * callback is described later. arg is passed to callback without further
533 * interpretation.
534 *
535 * prefixChar is the prefix that must be used in front of stem names.
536 *
537 * The function loops over a type structure tree, the current node name is
538 * placed in cb->buffer. We do a depth-first iteration. The callback function
539 * is called at least for each type declaration. The callback is described
540 * below the error codes.
541 *
542 * THE COMPLETE PARSING IS STOPPED IF THE callback RETURNS ANOTHER VALUE THAN
543 * GCI_OK.
544 *
545 * THE GENERATED TYPES MAY HAVE ILLEGAL BIT SIZES. IT ISN'T CHECKED ALWAYS!
546 *
547 * Return values:
548 * GCI_OK: Everything is fine.
549 *
550 * In case of an error cb->buffer will contain the
551 * variable's name where the problem raises first.
552 *
553 * GCI_MissingName: A variable's name isn't set. This is the equivalence
554 * for GCI_MissingValue in the type parsing step. The
555 * system may or may not raise a NOVALUE condition instead
556 * depending on the implementation.
557 * GCI_BufferTooSmall: The variable's name buffer cb->buffer can't hold the
558 * complete variable's name or the type string exceeds
559 * 256 byte.
560 * GCI_IllegalName: The variables name in cb->buffer is illegal in terms of
561 * Rexx. In general, the basename of GCI_paretree is
562 * wrong.
563 * GCI_RexxError: An unexpected error is returned by the interpreter
564 * while trying to access Rexx variables.
565 * GCI_UnsupportedType: Wrong type of input, e.g. FLOAT31 or the empty string
566 * in a type description string. Another reason is an
567 * internal error since the default sizes for "unsigned"
568 * and "integer" are not supported.
569 * GCI_WrongInput: Strange characters occur in the input string as the
570 * bit size of the type.
571 * GCI_NumberRange: Number to small or big to fit into the desired type
572 * with the desired destbyte-size. This applies to the
573 * element count of an "ARRAY" or "CONTAINER" type size
574 * or the bit size of the plain type.
575 * GCI_NoBaseType: The type won't fit the requirements for basic types.
576 *
577 * And there are numerous other possible errors returned by cb->callback.
578 *
579 *****************************************************************************
580 *
581 * Description of the callback:
582 * This function will be called when a type string has been parsed. The
583 * GCI_parseinfo structure is filled to allow the caller allocating enough
584 * memory.
585 *
586 * depth is the current depth within the structure starting with 0. itemnumber
587 * is the current number of the item within a structure starting with 1.
588 * Arrays will have an item of 1, not more, not less. The item describes all
589 * identical values in the array.
590 * itemnumber will be 0 for the base element (of depth 0).
591 *
592 * itemnumber may be -1! This happens on containers and arrays after
593 * processing the last subitem. This allows the callback to close the
594 * current container and to do some final processing.
595 *
596 * arg is the copy of the arg parameter of GCI_parsetree. A hidden parameter
597 * must be passed in arg (too) if it is needed.
598 *
599 * The caller of GCI_parsetree is responsible to understand additional
600 * return codes of the callback.
601 */
GCI_parsetree(void * hidden,GCI_str * base,GCI_result (* callback)(int depth,int itemnumber,void * arg,const GCI_parseinfo * info),void * arg,const char * prefixChar)602 GCI_result GCI_parsetree( void *hidden,
603 GCI_str *base,
604 GCI_result (*callback)(int depth,
605 int itemnumber,
606 void *arg,
607 const GCI_parseinfo *info),
608 void *arg,
609 const char *prefixChar )
610 {
611 callblock cb;
612 /*
613 * All simple type values must fit into a static buffer.
614 */
615 char tmp[256];
616 GCI_strOfCharBuffer( tmp );
617
618 /*
619 * We wrap the parameters in a block to prevent further parameters.
620 * We use the given buffer for all further processing. We'll have the
621 * errorneous string at once in that case.
622 */
623 cb.hidden = hidden;
624 cb.buffer = base;
625 cb.depth = 0;
626 cb.callback = callback;
627 cb.arg = arg;
628 cb.tempbuf = str_tmp;
629 cb.recurCount = 0;
630 cb.prefixChar = prefixChar;
631
632 return parse( &cb, 0 );
633 }
634