events cycle count fix, reverse byte order fix
[lttv.git] / ltt / branches / poly / ltt / parser.c
CommitLineData
6cd62ccf 1/*
2
3parser.c: Generate helper declarations and functions to trace events
4 from an event description file.
5
6Copyright (C) 2002, Xianxiu Yang
7Copyright (C) 2002, Michel Dagenais
8This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10the Free Software Foundation; version 2 of the License.
11
12This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
963b5f2d 22/* This program reads the ".xml" event definitions input files
23 and constructs structure for each event.
6cd62ccf 24
25 The program uses a very simple tokenizer, called from a hand written
26 recursive descent parser to fill a data structure describing the events.
27 The result is a sequence of events definitions which refer to type
28 definitions.
29
30 A table of named types is maintained to allow refering to types by name
31 when the same type is used at several places. Finally a sequence of
32 all types is maintained to facilitate the freeing of all type
963b5f2d 33 information when the processing of an ".xml" file is finished. */
6cd62ccf 34
35#include <stdlib.h>
36#include <string.h>
37#include <stdio.h>
38#include <stdarg.h>
8d1e6362 39#include <ctype.h>
6cd62ccf 40#include <linux/errno.h>
41
42
43#include "parser.h"
44
45
fc063e40 46static int ltt_isalpha(char c)
47{
48 int i,j;
49 if(c == '_')return 1;
50 i = c - 'a';
51 j = c - 'A';
52 if((i>=0 && i<26) || (j>=0 && j<26)) return 1;
53 return 0;
54}
55
56static int ltt_isalnum(char c)
57{
58 return (ltt_isalpha(c) || isdigit(c));
59}
60
6cd62ccf 61/*****************************************************************************
62 *Function name
63 * getSize : translate from string to integer
64 *Input params
65 * in : input file handle
66 *Return values
67 * size
68 *****************************************************************************/
69
70int getSize(parse_file *in)
71{
72 char *token;
73
74 token = getToken(in);
75 if(in->type == NUMBER) {
76 if(strcmp(token,"1") == 0) return 0;
77 else if(strcmp(token,"2") == 0) return 1;
78 else if(strcmp(token,"4") == 0) return 2;
79 else if(strcmp(token,"8") == 0) return 3;
80 }
81 else if(in->type == NAME) {
82 if(strcmp(token,"short") == 0) return 4;
83 else if(strcmp(token,"medium") == 0) return 5;
84 else if(strcmp(token,"long") == 0) return 6;
85 }
86 in->error(in,"incorrect size specification");
87 return -1;
88}
89
90/*****************************************************************************
91 *Function name
92 * error_callback : print out error info
93 *Input params
94 * in : input file handle
95 * msg : message to be printed
96 ****************************************************************************/
97
98void error_callback(parse_file *in, char *msg)
99{
100 if(in)
101 printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg);
102 else
103 printf("%s\n",msg);
6cd62ccf 104}
105
106/*****************************************************************************
107 *Function name
108 * memAlloc : allocate memory
109 *Input params
110 * size : required memory size
111 *return value
112 * void * : pointer to allocate memory or NULL
113 ****************************************************************************/
114
115void * memAlloc(int size)
116{
963b5f2d 117 void * addr;
118 if(size == 0) return NULL;
119 addr = malloc(size);
6cd62ccf 120 if(!addr){
121 printf("Failed to allocate memory");
122 exit(1);
123 }
124 return addr;
125}
126
127/*****************************************************************************
128 *Function name
129 * allocAndCopy : allocate memory and initialize it
130 *Input params
131 * str : string to be put in memory
132 *return value
133 * char * : pointer to allocate memory or NULL
134 ****************************************************************************/
135
136char *allocAndCopy(char *str)
137{
963b5f2d 138 char * addr;
139 if(str == NULL) return NULL;
140 addr = (char *)memAlloc(strlen(str)+1);
6cd62ccf 141 strcpy(addr,str);
142 return addr;
143}
144
963b5f2d 145/**************************************************************************
146 * Function :
147 * getNameAttribute,getFormatAttribute,getSizeAttribute,getValueAttribute
148 * getValueStrAttribute
149 * Description :
150 * Read the attribute from the input file.
151 *
152 * Parameters :
153 * in , input file handle.
154 *
155 * Return values :
156 * address of the attribute.
157 *
158 **************************************************************************/
159
160char * getNameAttribute(parse_file *in)
161{
162 char * token, car;
163 token = getName(in);
164 if(strcmp("name",token))in->error(in,"name was expected");
165 getEqual(in);
166
167 car = seekNextChar(in);
168 if(car == EOF)in->error(in,"name was expected");
169 else if(car == '\"')token = getQuotedString(in);
170 else token = getName(in);
171 return token;
172}
173
174char * getFormatAttribute(parse_file *in)
175{
176 char * token;
177
178 //format is an option
179 token = getToken(in);
bf410332 180 if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
963b5f2d 181 ungetToken(in);
182 return NULL;
183 }
184
185 if(strcmp("format",token))in->error(in,"format was expected");
186 getEqual(in);
187 token = getQuotedString(in);
188 return token;
189}
190
191int getSizeAttribute(parse_file *in)
192{
8d1e6362 193 /* skip name and equal */
963b5f2d 194 getName(in);
195 getEqual(in);
196
197 return getSize(in);
198}
199
200int getValueAttribute(parse_file *in)
201{
8d1e6362 202 /* skip name and equal */
963b5f2d 203 getName(in);
204 getEqual(in);
205
206 return getNumber(in);
207}
208
209//for <label name=label_name value=n/>, value is an option
210char * getValueStrAttribute(parse_file *in)
211{
212 char * token;
213
214 token = getToken(in);
215 if(strcmp("/",token) == 0){
216 ungetToken(in);
217 return NULL;
218 }
219
220 if(strcmp("value",token))in->error(in,"value was expected");
221 getEqual(in);
222 token = getToken(in);
223 if(in->type != NUMBER) in->error(in,"number was expected");
224 return token;
225}
226
227char * getDescription(parse_file *in)
228{
229 long int pos;
230 char * token, car, *str;
231
232 pos = ftell(in->fp);
233
234 getLAnglebracket(in);
235 token = getName(in);
236 if(strcmp("description",token)){
237 fseek(in->fp, pos, SEEK_SET);
238 return NULL;
239 }
240
241 getRAnglebracket(in);
242
243 pos = 0;
244 while((car = getc(in->fp)) != EOF) {
245 if(car == '<') break;
246 if(car == '\0') continue;
247 in->buffer[pos] = car;
248 pos++;
249 }
250 if(car == EOF)in->error(in,"not a valid description");
251 in->buffer[pos] = '\0';
252
253 str = allocAndCopy(in->buffer);
254
255 getForwardslash(in);
256 token = getName(in);
257 if(strcmp("description", token))in->error(in,"not a valid description");
258 getRAnglebracket(in);
259
260 return str;
261}
262
263/*****************************************************************************
264 *Function name
265 * parseFacility : generate event list
266 *Input params
267 * in : input file handle
268 * fac : empty facility
269 *Output params
270 * fac : facility filled with event list
271 ****************************************************************************/
272
8d1e6362 273void parseFacility(parse_file *in, facility_t * fac)
963b5f2d 274{
275 char * token;
8d1e6362 276 event_t *ev;
963b5f2d 277
278 fac->name = allocAndCopy(getNameAttribute(in));
279 getRAnglebracket(in);
280
ed9d56bd 281 fac->description = getDescription(in);
963b5f2d 282
283 while(1){
284 getLAnglebracket(in);
285
286 token = getToken(in);
287 if(in->type == ENDFILE)
288 in->error(in,"the definition of the facility is not finished");
289
290 if(strcmp("event",token) == 0){
8d1e6362 291 ev = (event_t*) memAlloc(sizeof(event_t));
963b5f2d 292 sequence_push(&(fac->events),ev);
293 parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types));
294 }else if(strcmp("type",token) == 0){
295 parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types));
296 }else if(in->type == FORWARDSLASH){
297 break;
298 }else in->error(in,"event or type token expected\n");
299 }
300
301 token = getName(in);
302 if(strcmp("facility",token)) in->error(in,"not the end of the facility");
303 getRAnglebracket(in); //</facility>
304}
6cd62ccf 305
306/*****************************************************************************
307 *Function name
308 * parseEvent : generate event from event definition
309 *Input params
310 * in : input file handle
311 * ev : new event
963b5f2d 312 * unnamed_types : array of unamed types
313 * named_types : array of named types
6cd62ccf 314 *Output params
315 * ev : new event (parameters are passed to it)
316 ****************************************************************************/
317
8d1e6362 318void parseEvent(parse_file *in, event_t * ev, sequence * unnamed_types,
6cd62ccf 319 table * named_types)
320{
321 char *token;
6cd62ccf 322
963b5f2d 323 //<event name=eventtype_name>
324 ev->name = allocAndCopy(getNameAttribute(in));
325 getRAnglebracket(in);
6cd62ccf 326
963b5f2d 327 //<description>...</descriptio>
ed9d56bd 328 ev->description = getDescription(in);
6cd62ccf 329
963b5f2d 330 //event can have STRUCT, TYPEREF or NOTHING
331 getLAnglebracket(in);
6cd62ccf 332
963b5f2d 333 token = getToken(in);
334 if(in->type == FORWARDSLASH){ //</event> NOTHING
335 ev->type = NULL;
336 }else if(in->type == NAME){
337 if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
338 ungetToken(in);
339 ev->type = parseType(in,NULL, unnamed_types, named_types);
340 if(ev->type->type != STRUCT && ev->type->type != NONE)
341 in->error(in,"type must be a struct");
342 }else in->error(in, "not a valid type");
343
344 getLAnglebracket(in);
345 getForwardslash(in);
6cd62ccf 346 }else in->error(in,"not a struct type");
347
963b5f2d 348 token = getName(in);
349 if(strcmp("event",token))in->error(in,"not an event definition");
350 getRAnglebracket(in); //</event>
6cd62ccf 351}
352
353/*****************************************************************************
354 *Function name
963b5f2d 355 * parseField : get field infomation from buffer
6cd62ccf 356 *Input params
963b5f2d 357 * in : input file handle
358 * t : type descriptor
359 * unnamed_types : array of unamed types
360 * named_types : array of named types
6cd62ccf 361 ****************************************************************************/
362
363void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types,
364 table * named_types)
365{
366 char * token;
8d1e6362 367 type_fields *f;
6cd62ccf 368
8d1e6362 369 f = (type_fields *)memAlloc(sizeof(type_fields));
963b5f2d 370 sequence_push(&(t->fields),f);
6cd62ccf 371
963b5f2d 372 //<field name=field_name> <description> <type> </field>
373 f->name = allocAndCopy(getNameAttribute(in));
374 getRAnglebracket(in);
375
ed9d56bd 376 f->description = getDescription(in);
963b5f2d 377
378 //<int size=...>
379 getLAnglebracket(in);
380 f->type = parseType(in,NULL, unnamed_types, named_types);
6cd62ccf 381
963b5f2d 382 getLAnglebracket(in);
383 getForwardslash(in);
384 token = getName(in);
385 if(strcmp("field",token))in->error(in,"not a valid field definition");
386 getRAnglebracket(in); //</field>
6cd62ccf 387}
388
389
390/*****************************************************************************
391 *Function name
392 * parseType : get type information, type can be :
393 * Primitive:
394 * int(size,fmt); uint(size,fmt); float(size,fmt);
395 * string(fmt); enum(size,fmt,(label1,label2...))
396 * Compound:
397 * array(arraySize, type); sequence(lengthSize,type)
398 * struct(field(name,type,description)...)
399 * type name:
400 * type(name,type)
401 *Input params
402 * in : input file handle
403 * inType : a type descriptor
963b5f2d 404 * unnamed_types : array of unamed types
405 * named_types : array of named types
6cd62ccf 406 *Return values
407 * type_descriptor* : a type descriptor
408 ****************************************************************************/
409
410type_descriptor *parseType(parse_file *in, type_descriptor *inType,
411 sequence * unnamed_types, table * named_types)
412{
963b5f2d 413 char *token;
6cd62ccf 414 type_descriptor *t;
415
416 if(inType == NULL) {
417 t = (type_descriptor *) memAlloc(sizeof(type_descriptor));
418 t->type_name = NULL;
419 t->type = NONE;
420 t->fmt = NULL;
421 sequence_push(unnamed_types,t);
422 }
423 else t = inType;
424
425 token = getName(in);
426
427 if(strcmp(token,"struct") == 0) {
428 t->type = STRUCT;
963b5f2d 429 getRAnglebracket(in); //<struct>
430 getLAnglebracket(in); //<field name=..>
431 token = getToken(in);
432 sequence_init(&(t->fields));
433 while(strcmp("field",token) == 0){
434 parseFields(in,t, unnamed_types, named_types);
435
436 //next field
437 getLAnglebracket(in);
438 token = getToken(in);
439 }
440 if(strcmp("/",token))in->error(in,"not a valid structure definition");
441 token = getName(in);
442 if(strcmp("struct",token)!=0)
443 in->error(in,"not a valid structure definition");
444 getRAnglebracket(in); //</struct>
445 }
446 else if(strcmp(token,"union") == 0) {
447 t->type = UNION;
448 t->size = getSizeAttribute(in);
449 getRAnglebracket(in); //<union typecodesize=isize>
450
451 getLAnglebracket(in); //<field name=..>
452 token = getToken(in);
453 sequence_init(&(t->fields));
454 while(strcmp("field",token) == 0){
455 parseFields(in,t, unnamed_types, named_types);
456
457 //next field
458 getLAnglebracket(in);
459 token = getToken(in);
460 }
461 if(strcmp("/",token))in->error(in,"not a valid union definition");
462 token = getName(in);
463 if(strcmp("union",token)!=0)
464 in->error(in,"not a valid union definition");
465 getRAnglebracket(in); //</union>
6cd62ccf 466 }
467 else if(strcmp(token,"array") == 0) {
468 t->type = ARRAY;
963b5f2d 469 t->size = getValueAttribute(in);
470 getRAnglebracket(in); //<array size=n>
471
472 getLAnglebracket(in); //<type struct>
6cd62ccf 473 t->nested_type = parseType(in,NULL, unnamed_types, named_types);
963b5f2d 474
475 getLAnglebracket(in); //</array>
476 getForwardslash(in);
477 token = getName(in);
478 if(strcmp("array",token))in->error(in,"not a valid array definition");
479 getRAnglebracket(in); //</array>
6cd62ccf 480 }
481 else if(strcmp(token,"sequence") == 0) {
482 t->type = SEQUENCE;
963b5f2d 483 t->size = getSizeAttribute(in);
484 getRAnglebracket(in); //<array lengthsize=isize>
485
486 getLAnglebracket(in); //<type struct>
6cd62ccf 487 t->nested_type = parseType(in,NULL, unnamed_types, named_types);
963b5f2d 488
489 getLAnglebracket(in); //</sequence>
490 getForwardslash(in);
491 token = getName(in);
492 if(strcmp("sequence",token))in->error(in,"not a valid sequence definition");
493 getRAnglebracket(in); //</sequence>
6cd62ccf 494 }
495 else if(strcmp(token,"enum") == 0) {
963b5f2d 496 char * str, *str1;
6cd62ccf 497 t->type = ENUM;
498 sequence_init(&(t->labels));
963b5f2d 499 t->size = getSizeAttribute(in);
500 t->fmt = allocAndCopy(getFormatAttribute(in));
501 getRAnglebracket(in);
502
503 //<label name=label1 value=n/>
504 getLAnglebracket(in);
505 token = getToken(in); //"label" or "/"
506 while(strcmp("label",token) == 0){
507 str = allocAndCopy(getNameAttribute(in));
508 token = getValueStrAttribute(in);
509 if(token){
510 str1 = appendString(str,"=");
511 free(str);
512 str = appendString(str1,token);
513 free(str1);
becaca6d 514 sequence_push(&(t->labels),str);
963b5f2d 515 }else
becaca6d 516 sequence_push(&(t->labels),str);
963b5f2d 517
518 getForwardslash(in);
519 getRAnglebracket(in);
520
521 //next label definition
522 getLAnglebracket(in);
523 token = getToken(in); //"label" or "/"
524 }
525 if(strcmp("/",token))in->error(in, "not a valid enum definition");
526 token = getName(in);
527 if(strcmp("enum",token))in->error(in, "not a valid enum definition");
528 getRAnglebracket(in); //</label>
6cd62ccf 529 }
530 else if(strcmp(token,"int") == 0) {
531 t->type = INT;
963b5f2d 532 t->size = getSizeAttribute(in);
533 t->fmt = allocAndCopy(getFormatAttribute(in));
534 getForwardslash(in);
535 getRAnglebracket(in);
6cd62ccf 536 }
537 else if(strcmp(token,"uint") == 0) {
538 t->type = UINT;
963b5f2d 539 t->size = getSizeAttribute(in);
540 t->fmt = allocAndCopy(getFormatAttribute(in));
541 getForwardslash(in);
542 getRAnglebracket(in);
6cd62ccf 543 }
544 else if(strcmp(token,"float") == 0) {
545 t->type = FLOAT;
963b5f2d 546 t->size = getSizeAttribute(in);
547 t->fmt = allocAndCopy(getFormatAttribute(in));
548 getForwardslash(in);
549 getRAnglebracket(in);
6cd62ccf 550 }
551 else if(strcmp(token,"string") == 0) {
552 t->type = STRING;
963b5f2d 553 t->fmt = allocAndCopy(getFormatAttribute(in));
554 getForwardslash(in);
555 getRAnglebracket(in);
6cd62ccf 556 }
963b5f2d 557 else if(strcmp(token,"typeref") == 0){
558 // Must be a named type
6cd62ccf 559 if(inType != NULL)
560 in->error(in,"Named type cannot refer to a named type");
561 else {
562 free(t);
563 sequence_pop(unnamed_types);
963b5f2d 564 token = getNameAttribute(in);
565 t = find_named_type(token, named_types);
566 getForwardslash(in); //<typeref name=type_name/>
567 getRAnglebracket(in);
568 return t;
6cd62ccf 569 }
963b5f2d 570 }else in->error(in,"not a valid type");
6cd62ccf 571
572 return t;
573}
574
575/*****************************************************************************
576 *Function name
577 * find_named_type : find a named type from hash table
578 *Input params
579 * name : type name
963b5f2d 580 * named_types : array of named types
6cd62ccf 581 *Return values
582 * type_descriptor * : a type descriptor
583 *****************************************************************************/
584
585type_descriptor * find_named_type(char *name, table * named_types)
586{
587 type_descriptor *t;
588
589 t = table_find(named_types,name);
590 if(t == NULL) {
591 t = (type_descriptor *)memAlloc(sizeof(type_descriptor));
592 t->type_name = allocAndCopy(name);
593 t->type = NONE;
594 t->fmt = NULL;
908f42fa 595 table_insert(named_types,t->type_name,t);
596 // table_insert(named_types,allocAndCopy(name),t);
6cd62ccf 597 }
598 return t;
599}
600
601/*****************************************************************************
602 *Function name
603 * parseTypeDefinition : get type information from type definition
604 *Input params
605 * in : input file handle
963b5f2d 606 * unnamed_types : array of unamed types
607 * named_types : array of named types
6cd62ccf 608 *****************************************************************************/
609
610void parseTypeDefinition(parse_file * in, sequence * unnamed_types,
611 table * named_types)
612{
613 char *token;
614 type_descriptor *t;
615
963b5f2d 616 token = getNameAttribute(in);
6cd62ccf 617 t = find_named_type(token, named_types);
6cd62ccf 618
619 if(t->type != NONE) in->error(in,"redefinition of named type");
963b5f2d 620 getRAnglebracket(in); //<type name=type_name>
621 getLAnglebracket(in); //<struct>
622 token = getName(in);
623 if(strcmp("struct",token))in->error(in,"not a valid type definition");
624 ungetToken(in);
6cd62ccf 625 parseType(in,t, unnamed_types, named_types);
963b5f2d 626
627 //</type>
628 getLAnglebracket(in);
629 getForwardslash(in);
630 token = getName(in);
631 if(strcmp("type",token))in->error(in,"not a valid type definition");
632 getRAnglebracket(in); //</type>
6cd62ccf 633}
634
635/**************************************************************************
636 * Function :
963b5f2d 637 * getComa, getName, getNumber, getEqual
6cd62ccf 638 * Description :
639 * Read a token from the input file, check its type, return it scontent.
640 *
641 * Parameters :
642 * in , input file handle.
643 *
644 * Return values :
645 * address of token content.
646 *
647 **************************************************************************/
648
649char *getName(parse_file * in)
650{
651 char *token;
652
653 token = getToken(in);
654 if(in->type != NAME) in->error(in,"Name token was expected");
655 return token;
656}
657
658int getNumber(parse_file * in)
659{
660 char *token;
661
662 token = getToken(in);
663 if(in->type != NUMBER) in->error(in, "Number token was expected");
664 return atoi(token);
665}
666
963b5f2d 667char *getForwardslash(parse_file * in)
6cd62ccf 668{
669 char *token;
670
671 token = getToken(in);
963b5f2d 672 if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
6cd62ccf 673 return token;
674}
675
963b5f2d 676char *getLAnglebracket(parse_file * in)
6cd62ccf 677{
678 char *token;
679
680 token = getToken(in);
963b5f2d 681 if(in->type != LANGLEBRACKET) in->error(in, "Left angle bracket was expected");
6cd62ccf 682 return token;
683}
684
963b5f2d 685char *getRAnglebracket(parse_file * in)
6cd62ccf 686{
687 char *token;
688
689 token = getToken(in);
963b5f2d 690 if(in->type != RANGLEBRACKET) in->error(in, "Right angle bracket was expected");
6cd62ccf 691 return token;
692}
693
694char *getQuotedString(parse_file * in)
695{
696 char *token;
697
698 token = getToken(in);
699 if(in->type != QUOTEDSTRING) in->error(in, "quoted string was expected");
700 return token;
701}
702
963b5f2d 703char * getEqual(parse_file *in)
6cd62ccf 704{
705 char *token;
706
707 token = getToken(in);
963b5f2d 708 if(in->type != EQUAL) in->error(in, "equal was expected");
6cd62ccf 709 return token;
710}
711
963b5f2d 712char seekNextChar(parse_file *in)
6cd62ccf 713{
963b5f2d 714 char car;
715 while((car = getc(in->fp)) != EOF) {
716 if(!isspace(car)){
717 ungetc(car,in->fp);
718 return car;
719 }
720 }
721 return EOF;
6cd62ccf 722}
723
724/******************************************************************
725 * Function :
726 * getToken, ungetToken
727 * Description :
728 * Read a token from the input file and return its type and content.
729 * Line numbers are accounted for and whitespace/comments are skipped.
730 *
731 * Parameters :
732 * in, input file handle.
733 *
734 * Return values :
735 * address of token content.
736 *
737 ******************************************************************/
738
739void ungetToken(parse_file * in)
740{
741 in->unget = 1;
742}
743
744char *getToken(parse_file * in)
745{
746 FILE *fp = in->fp;
747 char car, car1;
748 int pos = 0, escaped;
749
750 if(in->unget == 1) {
751 in->unget = 0;
752 return in->buffer;
753 }
754
755 /* skip whitespace and comments */
756
757 while((car = getc(fp)) != EOF) {
758 if(car == '/') {
759 car1 = getc(fp);
760 if(car1 == '*') skipComment(in);
761 else if(car1 == '/') skipEOL(in);
762 else {
763 car1 = ungetc(car1,fp);
764 break;
765 }
766 }
767 else if(car == '\n') in->lineno++;
768 else if(!isspace(car)) break;
769 }
770
771 switch(car) {
772 case EOF:
773 in->type = ENDFILE;
774 break;
963b5f2d 775 case '/':
776 in->type = FORWARDSLASH;
6cd62ccf 777 in->buffer[pos] = car;
778 pos++;
779 break;
963b5f2d 780 case '<':
781 in->type = LANGLEBRACKET;
6cd62ccf 782 in->buffer[pos] = car;
783 pos++;
784 break;
963b5f2d 785 case '>':
786 in->type = RANGLEBRACKET;
6cd62ccf 787 in->buffer[pos] = car;
788 pos++;
789 break;
790 case '=':
791 in->type = EQUAL;
792 in->buffer[pos] = car;
793 pos++;
794 break;
795 case '"':
796 escaped = 0;
797 while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
798 if(car == '\\' && escaped == 0) {
799 in->buffer[pos] = car;
800 pos++;
801 escaped = 1;
802 continue;
803 }
804 if(car == '"' && escaped == 0) break;
805 if(car == '\n' && escaped == 0) {
806 in->error(in, "non escaped newline inside quoted string");
807 }
808 if(car == '\n') in->lineno++;
809 in->buffer[pos] = car;
810 pos++;
811 escaped = 0;
812 }
813 if(car == EOF) in->error(in,"no ending quotemark");
814 if(pos == BUFFER_SIZE) in->error(in, "quoted string token too large");
815 in->type = QUOTEDSTRING;
816 break;
817 default:
818 if(isdigit(car)) {
819 in->buffer[pos] = car;
820 pos++;
821 while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
822 if(!isdigit(car)) {
823 ungetc(car,fp);
824 break;
825 }
826 in->buffer[pos] = car;
827 pos++;
828 }
829 if(car == EOF) ungetc(car,fp);
830 if(pos == BUFFER_SIZE) in->error(in, "number token too large");
831 in->type = NUMBER;
832 }
fc063e40 833 else if(ltt_isalpha(car)) {
6cd62ccf 834 in->buffer[0] = car;
835 pos = 1;
836 while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
fc063e40 837 if(!ltt_isalnum(car)) {
6cd62ccf 838 ungetc(car,fp);
839 break;
840 }
841 in->buffer[pos] = car;
842 pos++;
843 }
844 if(car == EOF) ungetc(car,fp);
845 if(pos == BUFFER_SIZE) in->error(in, "name token too large");
846 in->type = NAME;
847 }
848 else in->error(in, "invalid character, unrecognized token");
849 }
850 in->buffer[pos] = 0;
851 return in->buffer;
852}
853
854void skipComment(parse_file * in)
855{
856 char car;
857 while((car = getc(in->fp)) != EOF) {
858 if(car == '\n') in->lineno++;
859 else if(car == '*') {
860 car = getc(in->fp);
861 if(car ==EOF) break;
862 if(car == '/') return;
863 ungetc(car,in->fp);
864 }
865 }
866 if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'");
867}
868
869void skipEOL(parse_file * in)
870{
871 char car;
872 while((car = getc(in->fp)) != EOF) {
873 if(car == '\n') {
874 ungetc(car,in->fp);
875 break;
876 }
877 }
878 if(car == EOF)ungetc(car, in->fp);
879}
880
6cd62ccf 881/*****************************************************************************
882 *Function name
883 * checkNamedTypesImplemented : check if all named types have definition
fc063e40 884 * returns -1 on error, 0 if ok
6cd62ccf 885 ****************************************************************************/
886
fc063e40 887int checkNamedTypesImplemented(table * named_types)
6cd62ccf 888{
889 type_descriptor *t;
890 int pos;
891 char str[256];
892
893 for(pos = 0 ; pos < named_types->values.position; pos++) {
894 t = (type_descriptor *) named_types->values.array[pos];
895 if(t->type == NONE){
fc063e40 896 sprintf(str,"named type '%s' has no definition",
897 (char*)named_types->keys.array[pos]);
898 error_callback(NULL,str);
899 return -1;
6cd62ccf 900 }
901 }
fc063e40 902 return 0;
6cd62ccf 903}
904
905
906/*****************************************************************************
907 *Function name
908 * generateChecksum : generate checksum for the facility
909 *Input Params
910 * facName : name of facility
911 *Output Params
912 * checksum : checksum for the facility
913 ****************************************************************************/
914
fc063e40 915int generateChecksum(char* facName, unsigned long * checksum, sequence * events)
6cd62ccf 916{
917 unsigned long crc ;
963b5f2d 918 int pos;
8d1e6362 919 event_t * ev;
6cd62ccf 920 char str[256];
921
922 crc = crc32(facName);
923 for(pos = 0; pos < events->position; pos++){
8d1e6362 924 ev = (event_t *)(events->array[pos]);
6cd62ccf 925 crc = partial_crc32(ev->name,crc);
963b5f2d 926 if(!ev->type) continue; //event without type
6cd62ccf 927 if(ev->type->type != STRUCT){
928 sprintf(str,"event '%s' has a type other than STRUCT",ev->name);
929 error_callback(NULL, str);
fc063e40 930 return -1;
6cd62ccf 931 }
963b5f2d 932 crc = getTypeChecksum(crc, ev->type);
6cd62ccf 933 }
934 *checksum = crc;
fc063e40 935 return 0;
6cd62ccf 936}
937
938/*****************************************************************************
939 *Function name
940 * getTypeChecksum : generate checksum by type info
941 *Input Params
942 * crc : checksum generated so far
943 * type : type descriptor containing type info
944 *Return value
945 * unsigned long : checksum
946 *****************************************************************************/
947
963b5f2d 948unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
6cd62ccf 949{
950 unsigned long crc = aCrc;
951 char * str = NULL, buf[16];
963b5f2d 952 int flag = 0, pos;
8d1e6362 953 type_fields * fld;
6cd62ccf 954
955 switch(type->type){
956 case INT:
957 str = intOutputTypes[type->size];
958 break;
959 case UINT:
960 str = uintOutputTypes[type->size];
961 break;
962 case FLOAT:
963 str = floatOutputTypes[type->size];
964 break;
965 case STRING:
966 str = allocAndCopy("string");
967 flag = 1;
968 break;
969 case ENUM:
970 str = appendString("enum ", uintOutputTypes[type->size]);
971 flag = 1;
972 break;
973 case ARRAY:
8d1e6362 974 sprintf(buf,"%d",type->size);
6cd62ccf 975 str = appendString("array ",buf);
976 flag = 1;
977 break;
978 case SEQUENCE:
8d1e6362 979 sprintf(buf,"%d",type->size);
6cd62ccf 980 str = appendString("sequence ",buf);
981 flag = 1;
982 break;
983 case STRUCT:
984 str = allocAndCopy("struct");
985 flag = 1;
986 break;
963b5f2d 987 case UNION:
988 str = allocAndCopy("union");
989 flag = 1;
990 break;
6cd62ccf 991 default:
992 error_callback(NULL, "named type has no definition");
993 break;
994 }
995
996 crc = partial_crc32(str,crc);
997 if(flag) free(str);
998
999 if(type->fmt) crc = partial_crc32(type->fmt,crc);
1000
1001 if(type->type == ARRAY || type->type == SEQUENCE){
963b5f2d 1002 crc = getTypeChecksum(crc,type->nested_type);
1003 }else if(type->type == STRUCT || type->type == UNION){
1004 for(pos =0; pos < type->fields.position; pos++){
8d1e6362 1005 fld = (type_fields *) type->fields.array[pos];
963b5f2d 1006 crc = partial_crc32(fld->name,crc);
1007 crc = getTypeChecksum(crc, fld->type);
1008 }
6cd62ccf 1009 }else if(type->type == ENUM){
1010 for(pos = 0; pos < type->labels.position; pos++)
963b5f2d 1011 crc = partial_crc32((char*)type->labels.array[pos],crc);
6cd62ccf 1012 }
1013
1014 return crc;
1015}
1016
1017
1018/* Event type descriptors */
1019void freeType(type_descriptor * tp)
1020{
1021 int pos2;
8d1e6362 1022 type_fields *f;
6cd62ccf 1023
1024 if(tp->fmt != NULL) free(tp->fmt);
1025 if(tp->type == ENUM) {
1026 for(pos2 = 0; pos2 < tp->labels.position; pos2++) {
1027 free(tp->labels.array[pos2]);
1028 }
1029 sequence_dispose(&(tp->labels));
1030 }
1031 if(tp->type == STRUCT) {
1032 for(pos2 = 0; pos2 < tp->fields.position; pos2++) {
8d1e6362 1033 f = (type_fields *) tp->fields.array[pos2];
6cd62ccf 1034 free(f->name);
1035 free(f->description);
1036 free(f);
1037 }
1038 sequence_dispose(&(tp->fields));
1039 }
1040}
1041
1042void freeNamedType(table * t)
1043{
1044 int pos;
1045 type_descriptor * td;
1046
1047 for(pos = 0 ; pos < t->keys.position; pos++) {
1048 free((char *)t->keys.array[pos]);
1049 td = (type_descriptor*)t->values.array[pos];
1050 freeType(td);
1051 free(td);
1052 }
1053}
1054
1055void freeTypes(sequence *t)
1056{
8d1e6362 1057 int pos;
6cd62ccf 1058 type_descriptor *tp;
6cd62ccf 1059
1060 for(pos = 0 ; pos < t->position; pos++) {
1061 tp = (type_descriptor *)t->array[pos];
1062 freeType(tp);
1063 free(tp);
1064 }
1065}
1066
1067void freeEvents(sequence *t)
1068{
1069 int pos;
8d1e6362 1070 event_t *ev;
6cd62ccf 1071
1072 for(pos = 0 ; pos < t->position; pos++) {
8d1e6362 1073 ev = (event_t *) t->array[pos];
6cd62ccf 1074 free(ev->name);
1075 free(ev->description);
1076 free(ev);
1077 }
1078
1079}
1080
1081
1082/* Extensible array */
1083
1084void sequence_init(sequence *t)
1085{
1086 t->size = 10;
1087 t->position = 0;
1088 t->array = (void **)memAlloc(t->size * sizeof(void *));
1089}
1090
1091void sequence_dispose(sequence *t)
1092{
1093 t->size = 0;
1094 free(t->array);
1095 t->array = NULL;
1096}
1097
1098void sequence_push(sequence *t, void *elem)
1099{
1100 void **tmp;
1101
1102 if(t->position >= t->size) {
1103 tmp = t->array;
1104 t->array = (void **)memAlloc(t->size * 2 * sizeof(void *));
1105 memcpy(t->array, tmp, t->size * sizeof(void *));
1106 t->size = t->size * 2;
1107 free(tmp);
1108 }
1109 t->array[t->position] = elem;
1110 t->position++;
1111}
1112
1113void *sequence_pop(sequence *t)
1114{
1115 return t->array[t->position--];
1116}
1117
1118
1119/* Hash table API, implementation is just linear search for now */
1120
1121void table_init(table *t)
1122{
1123 sequence_init(&(t->keys));
1124 sequence_init(&(t->values));
1125}
1126
1127void table_dispose(table *t)
1128{
1129 sequence_dispose(&(t->keys));
1130 sequence_dispose(&(t->values));
1131}
1132
1133void table_insert(table *t, char *key, void *value)
1134{
1135 sequence_push(&(t->keys),key);
1136 sequence_push(&(t->values),value);
1137}
1138
1139void *table_find(table *t, char *key)
1140{
1141 int pos;
1142 for(pos = 0 ; pos < t->keys.position; pos++) {
1143 if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0)
1144 return(t->values.array[pos]);
1145 }
1146 return NULL;
1147}
1148
1149void table_insert_int(table *t, int *key, void *value)
1150{
1151 sequence_push(&(t->keys),key);
1152 sequence_push(&(t->values),value);
1153}
1154
1155void *table_find_int(table *t, int *key)
1156{
1157 int pos;
1158 for(pos = 0 ; pos < t->keys.position; pos++) {
1159 if(*key == *(int *)t->keys.array[pos])
1160 return(t->values.array[pos]);
1161 }
1162 return NULL;
1163}
1164
1165
1166/* Concatenate strings */
1167
1168char *appendString(char *s, char *suffix)
1169{
1170 char *tmp;
963b5f2d 1171 if(suffix == NULL) return s;
6cd62ccf 1172
1173 tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1);
1174 strcpy(tmp,s);
1175 strcat(tmp,suffix);
1176 return tmp;
1177}
1178
1179
This page took 0.077088 seconds and 4 git commands to generate.