| 1 | /* |
| 2 | |
| 3 | parser.c: Generate helper declarations and functions to trace events |
| 4 | from an event description file. |
| 5 | |
| 6 | Copyright (C) 2005, Mathieu Desnoyers |
| 7 | Copyright (C) 2002, Xianxiu Yang |
| 8 | Copyright (C) 2002, Michel Dagenais |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; version 2 of the License. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | /* This program reads the ".xml" event definitions input files |
| 24 | and constructs structure for each event. |
| 25 | |
| 26 | The program uses a very simple tokenizer, called from a hand written |
| 27 | recursive descent parser to fill a data structure describing the events. |
| 28 | The result is a sequence of events definitions which refer to type |
| 29 | definitions. |
| 30 | |
| 31 | A table of named types is maintained to allow refering to types by name |
| 32 | when the same type is used at several places. Finally a sequence of |
| 33 | all types is maintained to facilitate the freeing of all type |
| 34 | information when the processing of an ".xml" file is finished. */ |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdarg.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <assert.h> |
| 42 | #include <ctype.h> |
| 43 | |
| 44 | #include "parser.h" |
| 45 | |
| 46 | |
| 47 | char *intOutputTypes[] = { |
| 48 | "int8_t", "int16_t", "int32_t", "int64_t" }; |
| 49 | |
| 50 | char *uintOutputTypes[] = { |
| 51 | "uint8_t", "uint16_t", "uint32_t", "uint64_t" }; |
| 52 | |
| 53 | char *floatOutputTypes[] = { |
| 54 | "undef", "undef", "float", "double" }; |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | /* helper function */ |
| 60 | void strupper(char *string) |
| 61 | { |
| 62 | char *ptr = string; |
| 63 | |
| 64 | while(*ptr != '\0') { |
| 65 | *ptr = toupper(*ptr); |
| 66 | ptr++; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | int getSizeindex(unsigned int value) |
| 72 | { |
| 73 | switch(value) { |
| 74 | case 1: |
| 75 | return 0; |
| 76 | case 2: |
| 77 | return 1; |
| 78 | case 4: |
| 79 | return 2; |
| 80 | case 8: |
| 81 | return 3; |
| 82 | default: |
| 83 | printf("Error : unknown value size %d\n", value); |
| 84 | exit(-1); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /***************************************************************************** |
| 89 | *Function name |
| 90 | * getSize : translate from string to integer |
| 91 | *Input params |
| 92 | * in : input file handle |
| 93 | *Return values |
| 94 | * size |
| 95 | *****************************************************************************/ |
| 96 | |
| 97 | unsigned long long int getSize(parse_file_t *in) |
| 98 | { |
| 99 | char *token; |
| 100 | |
| 101 | token = getToken(in); |
| 102 | if(in->type == NUMBER) { |
| 103 | return strtoull(token, NULL, 0); |
| 104 | } |
| 105 | in->error(in,"incorrect size specification"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | /***************************************************************************** |
| 110 | *Function name |
| 111 | * error_callback : print out error info |
| 112 | *Input params |
| 113 | * in : input file handle |
| 114 | * msg : message to be printed |
| 115 | ****************************************************************************/ |
| 116 | |
| 117 | void error_callback(parse_file_t *in, char *msg) |
| 118 | { |
| 119 | if(in) |
| 120 | printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg); |
| 121 | else |
| 122 | printf("%s\n",msg); |
| 123 | assert(0); |
| 124 | exit(1); |
| 125 | } |
| 126 | |
| 127 | /***************************************************************************** |
| 128 | *Function name |
| 129 | * memAlloc : allocate memory |
| 130 | *Input params |
| 131 | * size : required memory size |
| 132 | *return value |
| 133 | * void * : pointer to allocate memory or NULL |
| 134 | ****************************************************************************/ |
| 135 | |
| 136 | void * memAlloc(int size) |
| 137 | { |
| 138 | void * addr; |
| 139 | if(size == 0) return NULL; |
| 140 | addr = malloc(size); |
| 141 | if(!addr){ |
| 142 | printf("Failed to allocate memory"); |
| 143 | exit(1); |
| 144 | } |
| 145 | return addr; |
| 146 | } |
| 147 | |
| 148 | /***************************************************************************** |
| 149 | *Function name |
| 150 | * allocAndCopy : allocate memory and initialize it |
| 151 | *Input params |
| 152 | * str : string to be put in memory |
| 153 | *return value |
| 154 | * char * : pointer to allocate memory or NULL |
| 155 | ****************************************************************************/ |
| 156 | |
| 157 | char *allocAndCopy(char *str) |
| 158 | { |
| 159 | char * addr; |
| 160 | if(str == NULL) return NULL; |
| 161 | addr = (char *)memAlloc(strlen(str)+1); |
| 162 | strcpy(addr,str); |
| 163 | return addr; |
| 164 | } |
| 165 | |
| 166 | /************************************************************************** |
| 167 | * Function : |
| 168 | * getTypeAttributes |
| 169 | * Description : |
| 170 | * Read the attribute from the input file. |
| 171 | * |
| 172 | * Parameters : |
| 173 | * in , input file handle. |
| 174 | * t , the type descriptor to fill. |
| 175 | * |
| 176 | **************************************************************************/ |
| 177 | |
| 178 | void getTypeAttributes(parse_file_t *in, type_descriptor_t *t, |
| 179 | sequence_t * unnamed_types, table_t * named_types) |
| 180 | { |
| 181 | char * token; |
| 182 | |
| 183 | t->fmt = NULL; |
| 184 | t->size = 0; |
| 185 | |
| 186 | while(1) { |
| 187 | token = getToken(in); |
| 188 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 189 | ungetToken(in); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | if(!strcmp("format",token)) { |
| 194 | getEqual(in); |
| 195 | t->fmt = allocAndCopy(getQuotedString(in)); |
| 196 | //} else if(!strcmp("name",token)) { |
| 197 | // getEqual(in); |
| 198 | // car = seekNextChar(in); |
| 199 | // if(car == EOF) in->error(in,"name was expected"); |
| 200 | // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in)); |
| 201 | // else t->type_name = allocAndCopy(getName(in)); |
| 202 | } else if(!strcmp("size",token)) { |
| 203 | getEqual(in); |
| 204 | t->size = getSize(in); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /************************************************************************** |
| 210 | * Function : |
| 211 | * getEventAttributes |
| 212 | * Description : |
| 213 | * Read the attribute from the input file. |
| 214 | * |
| 215 | * Parameters : |
| 216 | * in , input file handle. |
| 217 | * ev , the event to fill. |
| 218 | * |
| 219 | **************************************************************************/ |
| 220 | |
| 221 | void getEventAttributes(parse_file_t *in, event_t *ev) |
| 222 | { |
| 223 | char * token; |
| 224 | char car; |
| 225 | |
| 226 | ev->name = NULL; |
| 227 | ev->per_trace = 0; |
| 228 | ev->per_tracefile = 0; |
| 229 | |
| 230 | while(1) { |
| 231 | token = getToken(in); |
| 232 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 233 | ungetToken(in); |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | if(!strcmp("name",token)) { |
| 238 | getEqual(in); |
| 239 | car = seekNextChar(in); |
| 240 | if(car == EOF) in->error(in,"name was expected"); |
| 241 | else if(car == '\"') ev->name = allocAndCopy(getQuotedString(in)); |
| 242 | else ev->name = allocAndCopy(getName(in)); |
| 243 | } else if(!strcmp("per_trace", token)) { |
| 244 | ev->per_trace = 1; |
| 245 | } else if(!strcmp("per_tracefile", token)) { |
| 246 | ev->per_tracefile = 1; |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /************************************************************************** |
| 253 | * Function : |
| 254 | * getFacilityAttributes |
| 255 | * Description : |
| 256 | * Read the attribute from the input file. |
| 257 | * |
| 258 | * Parameters : |
| 259 | * in , input file handle. |
| 260 | * fac , the facility to fill. |
| 261 | * |
| 262 | **************************************************************************/ |
| 263 | |
| 264 | void getFacilityAttributes(parse_file_t *in, facility_t *fac) |
| 265 | { |
| 266 | char * token; |
| 267 | char car; |
| 268 | |
| 269 | fac->name = NULL; |
| 270 | |
| 271 | while(1) { |
| 272 | token = getToken(in); |
| 273 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 274 | ungetToken(in); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | if(!strcmp("name",token)) { |
| 279 | getEqual(in); |
| 280 | car = seekNextChar(in); |
| 281 | if(car == EOF) in->error(in,"name was expected"); |
| 282 | else if(car == '\"') fac->name = allocAndCopy(getQuotedString(in)); |
| 283 | else fac->name = allocAndCopy(getName(in)); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /************************************************************************** |
| 289 | * Function : |
| 290 | * getFieldAttributes |
| 291 | * Description : |
| 292 | * Read the attribute from the input file. |
| 293 | * |
| 294 | * Parameters : |
| 295 | * in , input file handle. |
| 296 | * f , the field to fill. |
| 297 | * |
| 298 | **************************************************************************/ |
| 299 | |
| 300 | void getFieldAttributes(parse_file_t *in, field_t *f) |
| 301 | { |
| 302 | char * token; |
| 303 | char car; |
| 304 | |
| 305 | f->name = NULL; |
| 306 | |
| 307 | while(1) { |
| 308 | token = getToken(in); |
| 309 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 310 | ungetToken(in); |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | if(!strcmp("name",token)) { |
| 315 | getEqual(in); |
| 316 | car = seekNextChar(in); |
| 317 | if(car == EOF) in->error(in,"name was expected"); |
| 318 | else if(car == '\"') f->name = allocAndCopy(getQuotedString(in)); |
| 319 | else f->name = allocAndCopy(getName(in)); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | char *getNameAttribute(parse_file_t *in) |
| 325 | { |
| 326 | char * token; |
| 327 | char *name = NULL; |
| 328 | char car; |
| 329 | |
| 330 | while(1) { |
| 331 | token = getToken(in); |
| 332 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 333 | ungetToken(in); |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | if(!strcmp("name",token)) { |
| 338 | getEqual(in); |
| 339 | car = seekNextChar(in); |
| 340 | if(car == EOF) in->error(in,"name was expected"); |
| 341 | else if(car == '\"') name = allocAndCopy(getQuotedString(in)); |
| 342 | else name = allocAndCopy(getName(in)); |
| 343 | } |
| 344 | } |
| 345 | if(name == NULL) in->error(in, "Name was expected"); |
| 346 | return name; |
| 347 | |
| 348 | } |
| 349 | |
| 350 | |
| 351 | |
| 352 | //for <label name=label_name value=n format="..."/>, value is an option |
| 353 | char * getValueStrAttribute(parse_file_t *in) |
| 354 | { |
| 355 | char * token; |
| 356 | |
| 357 | token = getToken(in); |
| 358 | if(strcmp("/",token) == 0){ |
| 359 | ungetToken(in); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | if(strcmp("value",token))in->error(in,"value was expected"); |
| 364 | getEqual(in); |
| 365 | token = getToken(in); |
| 366 | if(in->type != NUMBER) in->error(in,"number was expected"); |
| 367 | return token; |
| 368 | } |
| 369 | |
| 370 | char * getDescription(parse_file_t *in) |
| 371 | { |
| 372 | long int pos; |
| 373 | char * token, car, *str; |
| 374 | |
| 375 | pos = ftell(in->fp); |
| 376 | |
| 377 | getLAnglebracket(in); |
| 378 | token = getName(in); |
| 379 | if(strcmp("description",token)){ |
| 380 | fseek(in->fp, pos, SEEK_SET); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | getRAnglebracket(in); |
| 385 | |
| 386 | pos = 0; |
| 387 | while((car = getc(in->fp)) != EOF) { |
| 388 | if(car == '<') break; |
| 389 | if(car == '\0') continue; |
| 390 | in->buffer[pos] = car; |
| 391 | pos++; |
| 392 | } |
| 393 | if(car == EOF)in->error(in,"not a valid description"); |
| 394 | in->buffer[pos] = '\0'; |
| 395 | |
| 396 | str = allocAndCopy(in->buffer); |
| 397 | |
| 398 | getForwardslash(in); |
| 399 | token = getName(in); |
| 400 | if(strcmp("description", token))in->error(in,"not a valid description"); |
| 401 | getRAnglebracket(in); |
| 402 | |
| 403 | return str; |
| 404 | } |
| 405 | |
| 406 | /***************************************************************************** |
| 407 | *Function name |
| 408 | * parseFacility : generate event list |
| 409 | *Input params |
| 410 | * in : input file handle |
| 411 | * fac : empty facility |
| 412 | *Output params |
| 413 | * fac : facility filled with event list |
| 414 | ****************************************************************************/ |
| 415 | |
| 416 | void parseFacility(parse_file_t *in, facility_t * fac) |
| 417 | { |
| 418 | char * token; |
| 419 | event_t *ev; |
| 420 | |
| 421 | getFacilityAttributes(in, fac); |
| 422 | if(fac->name == NULL) in->error(in, "Attribute not named"); |
| 423 | |
| 424 | fac->capname = allocAndCopy(fac->name); |
| 425 | strupper(fac->capname); |
| 426 | getRAnglebracket(in); |
| 427 | |
| 428 | fac->description = getDescription(in); |
| 429 | |
| 430 | while(1){ |
| 431 | getLAnglebracket(in); |
| 432 | |
| 433 | token = getToken(in); |
| 434 | if(in->type == ENDFILE) |
| 435 | in->error(in,"the definition of the facility is not finished"); |
| 436 | |
| 437 | if(strcmp("event",token) == 0){ |
| 438 | ev = (event_t*) memAlloc(sizeof(event_t)); |
| 439 | sequence_push(&(fac->events),ev); |
| 440 | parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types)); |
| 441 | }else if(strcmp("type",token) == 0){ |
| 442 | parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types)); |
| 443 | }else if(in->type == FORWARDSLASH){ |
| 444 | break; |
| 445 | }else in->error(in,"event or type token expected\n"); |
| 446 | } |
| 447 | |
| 448 | token = getName(in); |
| 449 | if(strcmp("facility",token)) in->error(in,"not the end of the facility"); |
| 450 | getRAnglebracket(in); //</facility> |
| 451 | } |
| 452 | |
| 453 | /***************************************************************************** |
| 454 | *Function name |
| 455 | * parseEvent : generate event from event definition |
| 456 | *Input params |
| 457 | * in : input file handle |
| 458 | * ev : new event |
| 459 | * unnamed_types : array of unamed types |
| 460 | * named_types : array of named types |
| 461 | *Output params |
| 462 | * ev : new event (parameters are passed to it) |
| 463 | ****************************************************************************/ |
| 464 | |
| 465 | void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types, |
| 466 | table_t * named_types) |
| 467 | { |
| 468 | char *token; |
| 469 | field_t *f; |
| 470 | |
| 471 | sequence_init(&(ev->fields)); |
| 472 | //<event name=eventtype_name> |
| 473 | getEventAttributes(in, ev); |
| 474 | if(ev->name == NULL) in->error(in, "Event not named"); |
| 475 | getRAnglebracket(in); |
| 476 | |
| 477 | //<description>...</description> |
| 478 | ev->description = getDescription(in); |
| 479 | |
| 480 | int got_end = 0; |
| 481 | /* Events can have multiple fields. each field form at least a function |
| 482 | * parameter of the logging function. */ |
| 483 | while(!got_end) { |
| 484 | getLAnglebracket(in); |
| 485 | token = getToken(in); |
| 486 | |
| 487 | switch(in->type) { |
| 488 | case FORWARDSLASH: /* </event> */ |
| 489 | token = getName(in); |
| 490 | if(strcmp("event",token))in->error(in,"not an event definition"); |
| 491 | getRAnglebracket(in); //</event> |
| 492 | got_end = 1; |
| 493 | break; |
| 494 | case NAME: /* a field */ |
| 495 | if(strcmp("field",token))in->error(in,"expecting a field"); |
| 496 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 497 | sequence_push(&(ev->fields),f); |
| 498 | parseFields(in, f, unnamed_types, named_types, 1); |
| 499 | break; |
| 500 | default: |
| 501 | in->error(in, "expecting </event> or <field >"); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | #if 0 |
| 506 | if(in->type == FORWARDSLASH){ //</event> NOTHING |
| 507 | ev->type = NULL; |
| 508 | }else if(in->type == NAME){ |
| 509 | if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){ |
| 510 | ungetToken(in); |
| 511 | ev->type = parseType(in,NULL, unnamed_types, named_types); |
| 512 | if(ev->type->type != STRUCT && ev->type->type != NONE) |
| 513 | in->error(in,"type must be a struct"); |
| 514 | }else in->error(in, "not a valid type"); |
| 515 | |
| 516 | getLAnglebracket(in); |
| 517 | getForwardslash(in); |
| 518 | }else in->error(in,"not a struct type"); |
| 519 | getLAnglebracket(in); |
| 520 | getForwardslash(in); |
| 521 | token = getName(in); |
| 522 | if(strcmp("event",token))in->error(in,"not an event definition"); |
| 523 | getRAnglebracket(in); //</event> |
| 524 | #endif //0 |
| 525 | } |
| 526 | |
| 527 | /***************************************************************************** |
| 528 | *Function name |
| 529 | * parseField : get field infomation from buffer |
| 530 | *Input params |
| 531 | * in : input file handle |
| 532 | * f : field |
| 533 | * unnamed_types : array of unamed types |
| 534 | * named_types : array of named types |
| 535 | * tag : is field surrounded by a <field> </field> tag ? |
| 536 | ****************************************************************************/ |
| 537 | |
| 538 | void parseFields(parse_file_t *in, field_t *f, |
| 539 | sequence_t * unnamed_types, |
| 540 | table_t * named_types, |
| 541 | int tag) |
| 542 | { |
| 543 | char * token; |
| 544 | if(tag) { |
| 545 | //<field name=field_name> <description> <type> </field> |
| 546 | getFieldAttributes(in, f); |
| 547 | if(f->name == NULL) in->error(in, "Field not named"); |
| 548 | getRAnglebracket(in); |
| 549 | |
| 550 | f->description = getDescription(in); |
| 551 | } |
| 552 | |
| 553 | //<int size=...> |
| 554 | getLAnglebracket(in); |
| 555 | f->type = parseType(in,NULL, unnamed_types, named_types); |
| 556 | |
| 557 | if(tag) { |
| 558 | getLAnglebracket(in); |
| 559 | getForwardslash(in); |
| 560 | token = getName(in); |
| 561 | if(strcmp("field",token))in->error(in,"not a valid field definition"); |
| 562 | getRAnglebracket(in); //</field> |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /***************************************************************************** |
| 568 | *Function name |
| 569 | * parseType : get type information, type can be : |
| 570 | * Primitive: |
| 571 | * int(size,fmt); uint(size,fmt); float(size,fmt); |
| 572 | * string(fmt); enum(size,fmt,(label1,label2...)) |
| 573 | * Compound: |
| 574 | * array(arraySize, type); sequence(lengthSize,type) |
| 575 | * struct(field(name,type,description)...) |
| 576 | * type name: |
| 577 | * type(name,type) |
| 578 | *Input params |
| 579 | * in : input file handle |
| 580 | * inType : a type descriptor |
| 581 | * unnamed_types : array of unamed types |
| 582 | * named_types : array of named types |
| 583 | *Return values |
| 584 | * type_descriptor* : a type descriptor |
| 585 | ****************************************************************************/ |
| 586 | |
| 587 | type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType, |
| 588 | sequence_t * unnamed_types, table_t * named_types) |
| 589 | { |
| 590 | char *token; |
| 591 | type_descriptor_t *t; |
| 592 | field_t *f; |
| 593 | |
| 594 | if(inType == NULL) { |
| 595 | t = (type_descriptor_t *) memAlloc(sizeof(type_descriptor_t)); |
| 596 | t->type_name = NULL; |
| 597 | t->type = NONE; |
| 598 | t->fmt = NULL; |
| 599 | sequence_push(unnamed_types,t); |
| 600 | } |
| 601 | else t = inType; |
| 602 | |
| 603 | token = getName(in); |
| 604 | |
| 605 | if(strcmp(token,"struct") == 0) { |
| 606 | t->type = STRUCT; |
| 607 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 608 | getRAnglebracket(in); //<struct> |
| 609 | getLAnglebracket(in); //<field name=..> |
| 610 | token = getToken(in); |
| 611 | sequence_init(&(t->fields)); |
| 612 | while(strcmp("field",token) == 0){ |
| 613 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 614 | sequence_push(&(t->fields),f); |
| 615 | |
| 616 | parseFields(in, f, unnamed_types, named_types, 1); |
| 617 | |
| 618 | //next field |
| 619 | getLAnglebracket(in); |
| 620 | token = getToken(in); |
| 621 | } |
| 622 | if(strcmp("/",token))in->error(in,"not a valid structure definition"); |
| 623 | token = getName(in); |
| 624 | if(strcmp("struct",token)!=0) |
| 625 | in->error(in,"not a valid structure definition"); |
| 626 | getRAnglebracket(in); //</struct> |
| 627 | } |
| 628 | else if(strcmp(token,"union") == 0) { |
| 629 | t->type = UNION; |
| 630 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 631 | getRAnglebracket(in); //<union> |
| 632 | |
| 633 | getLAnglebracket(in); //<field name=..> |
| 634 | token = getToken(in); |
| 635 | sequence_init(&(t->fields)); |
| 636 | while(strcmp("field",token) == 0){ |
| 637 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 638 | sequence_push(&(t->fields),f); |
| 639 | parseFields(in, f, unnamed_types, named_types, 1); |
| 640 | |
| 641 | //next field |
| 642 | getLAnglebracket(in); |
| 643 | token = getToken(in); |
| 644 | } |
| 645 | if(strcmp("/",token))in->error(in,"not a valid union definition"); |
| 646 | token = getName(in); |
| 647 | if(strcmp("union",token)!=0) |
| 648 | in->error(in,"not a valid union definition"); |
| 649 | getRAnglebracket(in); //</union> |
| 650 | } |
| 651 | else if(strcmp(token,"array") == 0) { |
| 652 | t->type = ARRAY; |
| 653 | sequence_init(&(t->fields)); |
| 654 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 655 | if(t->size == 0) in->error(in, "Array has empty size"); |
| 656 | getForwardslash(in); |
| 657 | getRAnglebracket(in); //<array size=n> |
| 658 | |
| 659 | //getLAnglebracket(in); //<subtype> |
| 660 | /* subfield */ |
| 661 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 662 | sequence_push(&(t->fields),f); |
| 663 | parseFields(in, f, unnamed_types, named_types, 0); |
| 664 | |
| 665 | //getLAnglebracket(in); //<type struct> |
| 666 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
| 667 | |
| 668 | getLAnglebracket(in); //</array> |
| 669 | getForwardslash(in); |
| 670 | token = getName(in); |
| 671 | if(strcmp("array",token))in->error(in,"not a valid array definition"); |
| 672 | getRAnglebracket(in); //</array> |
| 673 | } |
| 674 | else if(strcmp(token,"sequence") == 0) { |
| 675 | t->type = SEQUENCE; |
| 676 | sequence_init(&(t->fields)); |
| 677 | //getTypeAttributes(in, t, unnamed_types, named_types); |
| 678 | //getForwardslash(in); |
| 679 | getRAnglebracket(in); //<sequence> |
| 680 | |
| 681 | //getLAnglebracket(in); //<sequence size type> |
| 682 | /* subfield */ |
| 683 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 684 | sequence_push(&(t->fields),f); |
| 685 | parseFields(in, f, unnamed_types, named_types, 0); |
| 686 | |
| 687 | //getLAnglebracket(in); //<subtype> |
| 688 | /* subfield */ |
| 689 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 690 | sequence_push(&(t->fields),f); |
| 691 | parseFields(in, f, unnamed_types, named_types, 0); |
| 692 | |
| 693 | //getLAnglebracket(in); //<type sequence> |
| 694 | //t->length_type = parseType(in, NULL, unnamed_types, named_types); |
| 695 | |
| 696 | //getLAnglebracket(in); //<type sequence> |
| 697 | |
| 698 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
| 699 | |
| 700 | if(t->fields.position < 1) in->error(in, "Sequence has no length type"); |
| 701 | if(t->fields.position < 2) in->error(in, "Sequence has no subtype"); |
| 702 | switch(((field_t*)t->fields.array[0])->type->type) { |
| 703 | case UINT_FIXED : |
| 704 | case UCHAR : |
| 705 | case USHORT : |
| 706 | case UINT : |
| 707 | case ULONG : |
| 708 | case SIZE_T : |
| 709 | case OFF_T : |
| 710 | break; |
| 711 | default: |
| 712 | in->error(in, "Wrong length type for sequence"); |
| 713 | } |
| 714 | |
| 715 | getLAnglebracket(in); //</sequence> |
| 716 | getForwardslash(in); |
| 717 | token = getName(in); |
| 718 | if(strcmp("sequence",token))in->error(in,"not a valid sequence definition"); |
| 719 | getRAnglebracket(in); //</sequence> |
| 720 | } |
| 721 | else if(strcmp(token,"enum") == 0) { |
| 722 | char * str; |
| 723 | int value = -1; |
| 724 | |
| 725 | t->type = ENUM; |
| 726 | sequence_init(&(t->labels)); |
| 727 | sequence_init(&(t->labels_values)); |
| 728 | sequence_init(&(t->labels_description)); |
| 729 | t->already_printed = 0; |
| 730 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 731 | //if(t->size == 0) in->error(in, "Sequence has empty size"); |
| 732 | //Mathieu : we fix enum size to target int size. GCC is always like this. |
| 733 | //fox copy optimisation. |
| 734 | if(t->size != 0) in->error(in, "Enum has fixed size of target int."); |
| 735 | t->size = 0; |
| 736 | getRAnglebracket(in); |
| 737 | |
| 738 | //<label name=label1 value=n/> |
| 739 | getLAnglebracket(in); |
| 740 | token = getToken(in); //"label" or "/" |
| 741 | while(strcmp("label",token) == 0){ |
| 742 | int *label_value = malloc(sizeof(int)); |
| 743 | |
| 744 | str = allocAndCopy(getNameAttribute(in)); |
| 745 | token = getValueStrAttribute(in); |
| 746 | |
| 747 | sequence_push(&(t->labels),str); |
| 748 | |
| 749 | if(token) value = strtol(token, NULL, 0); |
| 750 | else value++; |
| 751 | |
| 752 | *label_value = value; |
| 753 | sequence_push(&(t->labels_values), label_value); |
| 754 | |
| 755 | getForwardslash(in); |
| 756 | getRAnglebracket(in); |
| 757 | |
| 758 | //read description if any. May be NULL. |
| 759 | str = allocAndCopy(getDescription(in)); |
| 760 | sequence_push(&(t->labels_description),str); |
| 761 | |
| 762 | //next label definition |
| 763 | getLAnglebracket(in); |
| 764 | token = getToken(in); //"label" or "/" |
| 765 | } |
| 766 | if(strcmp("/",token))in->error(in, "not a valid enum definition"); |
| 767 | token = getName(in); |
| 768 | if(strcmp("enum",token))in->error(in, "not a valid enum definition"); |
| 769 | getRAnglebracket(in); //</label> |
| 770 | } |
| 771 | else if(strcmp(token,"int_fixed") == 0) { |
| 772 | t->type = INT_FIXED; |
| 773 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 774 | if(t->size == 0) in->error(in, "int has empty size"); |
| 775 | getForwardslash(in); |
| 776 | getRAnglebracket(in); |
| 777 | } |
| 778 | else if(strcmp(token,"uint_fixed") == 0) { |
| 779 | t->type = UINT_FIXED; |
| 780 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 781 | if(t->size == 0) in->error(in, "uint has empty size"); |
| 782 | getForwardslash(in); |
| 783 | getRAnglebracket(in); |
| 784 | } |
| 785 | else if(strcmp(token,"char") == 0) { |
| 786 | t->type = CHAR; |
| 787 | t->size = 1; |
| 788 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 789 | getForwardslash(in); |
| 790 | getRAnglebracket(in); |
| 791 | } |
| 792 | else if(strcmp(token,"uchar") == 0) { |
| 793 | t->type = UCHAR; |
| 794 | t->size = 1; |
| 795 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 796 | getForwardslash(in); |
| 797 | getRAnglebracket(in); |
| 798 | } |
| 799 | else if(strcmp(token,"short") == 0) { |
| 800 | t->type = SHORT; |
| 801 | t->size = 2; |
| 802 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 803 | getForwardslash(in); |
| 804 | getRAnglebracket(in); |
| 805 | } |
| 806 | else if(strcmp(token,"ushort") == 0) { |
| 807 | t->type = USHORT; |
| 808 | t->size = 2; |
| 809 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 810 | getForwardslash(in); |
| 811 | getRAnglebracket(in); |
| 812 | } |
| 813 | else if(strcmp(token,"int") == 0) { |
| 814 | t->type = INT; |
| 815 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 816 | getForwardslash(in); |
| 817 | getRAnglebracket(in); |
| 818 | } |
| 819 | else if(strcmp(token,"uint") == 0) { |
| 820 | t->type = UINT; |
| 821 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 822 | getForwardslash(in); |
| 823 | getRAnglebracket(in); |
| 824 | } |
| 825 | |
| 826 | else if(strcmp(token,"pointer") == 0) { |
| 827 | t->type = POINTER; |
| 828 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 829 | getForwardslash(in); |
| 830 | getRAnglebracket(in); |
| 831 | } |
| 832 | else if(strcmp(token,"long") == 0) { |
| 833 | t->type = LONG; |
| 834 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 835 | getForwardslash(in); |
| 836 | getRAnglebracket(in); |
| 837 | } |
| 838 | else if(strcmp(token,"ulong") == 0) { |
| 839 | t->type = ULONG; |
| 840 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 841 | getForwardslash(in); |
| 842 | getRAnglebracket(in); |
| 843 | } |
| 844 | else if(strcmp(token,"size_t") == 0) { |
| 845 | t->type = SIZE_T; |
| 846 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 847 | getForwardslash(in); |
| 848 | getRAnglebracket(in); |
| 849 | } |
| 850 | else if(strcmp(token,"ssize_t") == 0) { |
| 851 | t->type = SSIZE_T; |
| 852 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 853 | getForwardslash(in); |
| 854 | getRAnglebracket(in); |
| 855 | } |
| 856 | else if(strcmp(token,"off_t") == 0) { |
| 857 | t->type = OFF_T; |
| 858 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 859 | getForwardslash(in); |
| 860 | getRAnglebracket(in); |
| 861 | } |
| 862 | else if(strcmp(token,"float") == 0) { |
| 863 | t->type = FLOAT; |
| 864 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 865 | getForwardslash(in); |
| 866 | getRAnglebracket(in); |
| 867 | } |
| 868 | else if(strcmp(token,"string") == 0) { |
| 869 | t->type = STRING; |
| 870 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 871 | getForwardslash(in); |
| 872 | getRAnglebracket(in); |
| 873 | } |
| 874 | else if(strcmp(token,"typeref") == 0){ |
| 875 | // Must be a named type |
| 876 | free(t); |
| 877 | sequence_pop(unnamed_types); |
| 878 | token = getNameAttribute(in); |
| 879 | t = find_named_type(token, named_types); |
| 880 | if(t == NULL) in->error(in,"Named referred to must be pre-declared."); |
| 881 | getForwardslash(in); //<typeref name=type_name/> |
| 882 | getRAnglebracket(in); |
| 883 | return t; |
| 884 | }else in->error(in,"not a valid type"); |
| 885 | |
| 886 | return t; |
| 887 | } |
| 888 | |
| 889 | /***************************************************************************** |
| 890 | *Function name |
| 891 | * find_named_type : find a named type from hash table |
| 892 | *Input params |
| 893 | * name : type name |
| 894 | * named_types : array of named types |
| 895 | *Return values |
| 896 | * type_descriptor * : a type descriptor |
| 897 | *****************************************************************************/ |
| 898 | |
| 899 | type_descriptor_t * find_named_type(char *name, table_t * named_types) |
| 900 | { |
| 901 | type_descriptor_t *t; |
| 902 | |
| 903 | t = table_find(named_types,name); |
| 904 | |
| 905 | return t; |
| 906 | } |
| 907 | |
| 908 | type_descriptor_t * create_named_type(char *name, table_t * named_types) |
| 909 | { |
| 910 | type_descriptor_t *t; |
| 911 | |
| 912 | t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t)); |
| 913 | t->type_name = allocAndCopy(name); |
| 914 | t->type = NONE; |
| 915 | t->fmt = NULL; |
| 916 | table_insert(named_types,t->type_name,t); |
| 917 | // table_insert(named_types,allocAndCopy(name),t); |
| 918 | return t; |
| 919 | } |
| 920 | |
| 921 | /***************************************************************************** |
| 922 | *Function name |
| 923 | * parseTypeDefinition : get type information from type definition |
| 924 | *Input params |
| 925 | * in : input file handle |
| 926 | * unnamed_types : array of unamed types |
| 927 | * named_types : array of named types |
| 928 | *****************************************************************************/ |
| 929 | |
| 930 | void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types, |
| 931 | table_t * named_types) |
| 932 | { |
| 933 | char *token; |
| 934 | type_descriptor_t *t; |
| 935 | |
| 936 | token = getNameAttribute(in); |
| 937 | if(token == NULL) in->error(in, "Type has empty name"); |
| 938 | t = create_named_type(token, named_types); |
| 939 | |
| 940 | if(t->type != NONE) in->error(in,"redefinition of named type"); |
| 941 | getRAnglebracket(in); //<type name=type_name> |
| 942 | getLAnglebracket(in); //< |
| 943 | token = getName(in); |
| 944 | //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition"); |
| 945 | ungetToken(in); |
| 946 | parseType(in,t, unnamed_types, named_types); |
| 947 | |
| 948 | //</type> |
| 949 | getLAnglebracket(in); |
| 950 | getForwardslash(in); |
| 951 | token = getName(in); |
| 952 | if(strcmp("type",token))in->error(in,"not a valid type definition"); |
| 953 | getRAnglebracket(in); //</type> |
| 954 | } |
| 955 | |
| 956 | /************************************************************************** |
| 957 | * Function : |
| 958 | * getComa, getName, getNumber, getEqual |
| 959 | * Description : |
| 960 | * Read a token from the input file, check its type, return it scontent. |
| 961 | * |
| 962 | * Parameters : |
| 963 | * in , input file handle. |
| 964 | * |
| 965 | * Return values : |
| 966 | * address of token content. |
| 967 | * |
| 968 | **************************************************************************/ |
| 969 | |
| 970 | char *getName(parse_file_t * in) |
| 971 | { |
| 972 | char *token; |
| 973 | |
| 974 | token = getToken(in); |
| 975 | if(in->type != NAME) in->error(in,"Name token was expected"); |
| 976 | return token; |
| 977 | } |
| 978 | |
| 979 | int getNumber(parse_file_t * in) |
| 980 | { |
| 981 | char *token; |
| 982 | |
| 983 | token = getToken(in); |
| 984 | if(in->type != NUMBER) in->error(in, "Number token was expected"); |
| 985 | return atoi(token); |
| 986 | } |
| 987 | |
| 988 | char *getForwardslash(parse_file_t * in) |
| 989 | { |
| 990 | char *token; |
| 991 | |
| 992 | token = getToken(in); |
| 993 | //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected"); |
| 994 | /* Mathieu : final / is optional now. */ |
| 995 | if(in->type != FORWARDSLASH) ungetToken(in); |
| 996 | |
| 997 | return token; |
| 998 | } |
| 999 | |
| 1000 | char *getLAnglebracket(parse_file_t * in) |
| 1001 | { |
| 1002 | char *token; |
| 1003 | |
| 1004 | token = getToken(in); |
| 1005 | if(in->type != LANGLEBRACKET) in->error(in, "Left angle bracket was expected"); |
| 1006 | return token; |
| 1007 | } |
| 1008 | |
| 1009 | char *getRAnglebracket(parse_file_t * in) |
| 1010 | { |
| 1011 | char *token; |
| 1012 | |
| 1013 | token = getToken(in); |
| 1014 | if(in->type != RANGLEBRACKET) in->error(in, "Right angle bracket was expected"); |
| 1015 | return token; |
| 1016 | } |
| 1017 | |
| 1018 | char *getQuotedString(parse_file_t * in) |
| 1019 | { |
| 1020 | char *token; |
| 1021 | |
| 1022 | token = getToken(in); |
| 1023 | if(in->type != QUOTEDSTRING) in->error(in, "quoted string was expected"); |
| 1024 | return token; |
| 1025 | } |
| 1026 | |
| 1027 | char * getEqual(parse_file_t *in) |
| 1028 | { |
| 1029 | char *token; |
| 1030 | |
| 1031 | token = getToken(in); |
| 1032 | if(in->type != EQUAL) in->error(in, "equal was expected"); |
| 1033 | return token; |
| 1034 | } |
| 1035 | |
| 1036 | char seekNextChar(parse_file_t *in) |
| 1037 | { |
| 1038 | char car; |
| 1039 | while((car = getc(in->fp)) != EOF) { |
| 1040 | if(!isspace(car)){ |
| 1041 | ungetc(car,in->fp); |
| 1042 | return car; |
| 1043 | } |
| 1044 | } |
| 1045 | return EOF; |
| 1046 | } |
| 1047 | |
| 1048 | /****************************************************************** |
| 1049 | * Function : |
| 1050 | * getToken, ungetToken |
| 1051 | * Description : |
| 1052 | * Read a token from the input file and return its type and content. |
| 1053 | * Line numbers are accounted for and whitespace/comments are skipped. |
| 1054 | * |
| 1055 | * Parameters : |
| 1056 | * in, input file handle. |
| 1057 | * |
| 1058 | * Return values : |
| 1059 | * address of token content. |
| 1060 | * |
| 1061 | ******************************************************************/ |
| 1062 | |
| 1063 | void ungetToken(parse_file_t * in) |
| 1064 | { |
| 1065 | in->unget = 1; |
| 1066 | } |
| 1067 | |
| 1068 | char *getToken(parse_file_t * in) |
| 1069 | { |
| 1070 | FILE *fp = in->fp; |
| 1071 | char car, car1; |
| 1072 | int pos = 0, escaped; |
| 1073 | |
| 1074 | if(in->unget == 1) { |
| 1075 | in->unget = 0; |
| 1076 | return in->buffer; |
| 1077 | } |
| 1078 | |
| 1079 | /* skip whitespace and comments */ |
| 1080 | |
| 1081 | while((car = getc(fp)) != EOF) { |
| 1082 | if(car == '/') { |
| 1083 | car1 = getc(fp); |
| 1084 | if(car1 == '*') skipComment(in); |
| 1085 | else if(car1 == '/') skipEOL(in); |
| 1086 | else { |
| 1087 | car1 = ungetc(car1,fp); |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | else if(car == '\n') in->lineno++; |
| 1092 | else if(!isspace(car)) break; |
| 1093 | } |
| 1094 | |
| 1095 | switch(car) { |
| 1096 | case EOF: |
| 1097 | in->type = ENDFILE; |
| 1098 | break; |
| 1099 | case '/': |
| 1100 | in->type = FORWARDSLASH; |
| 1101 | in->buffer[pos] = car; |
| 1102 | pos++; |
| 1103 | break; |
| 1104 | case '<': |
| 1105 | in->type = LANGLEBRACKET; |
| 1106 | in->buffer[pos] = car; |
| 1107 | pos++; |
| 1108 | break; |
| 1109 | case '>': |
| 1110 | in->type = RANGLEBRACKET; |
| 1111 | in->buffer[pos] = car; |
| 1112 | pos++; |
| 1113 | break; |
| 1114 | case '=': |
| 1115 | in->type = EQUAL; |
| 1116 | in->buffer[pos] = car; |
| 1117 | pos++; |
| 1118 | break; |
| 1119 | case '"': |
| 1120 | escaped = 0; |
| 1121 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1122 | if(car == '\\' && escaped == 0) { |
| 1123 | in->buffer[pos] = car; |
| 1124 | pos++; |
| 1125 | escaped = 1; |
| 1126 | continue; |
| 1127 | } |
| 1128 | if(car == '"' && escaped == 0) break; |
| 1129 | if(car == '\n' && escaped == 0) { |
| 1130 | in->error(in, "non escaped newline inside quoted string"); |
| 1131 | } |
| 1132 | if(car == '\n') in->lineno++; |
| 1133 | in->buffer[pos] = car; |
| 1134 | pos++; |
| 1135 | escaped = 0; |
| 1136 | } |
| 1137 | if(car == EOF) in->error(in,"no ending quotemark"); |
| 1138 | if(pos == BUFFER_SIZE) in->error(in, "quoted string token too large"); |
| 1139 | in->type = QUOTEDSTRING; |
| 1140 | break; |
| 1141 | default: |
| 1142 | if(isdigit(car)) { |
| 1143 | in->buffer[pos] = car; |
| 1144 | pos++; |
| 1145 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1146 | if(!isdigit(car)) { |
| 1147 | ungetc(car,fp); |
| 1148 | break; |
| 1149 | } |
| 1150 | in->buffer[pos] = car; |
| 1151 | pos++; |
| 1152 | } |
| 1153 | if(car == EOF) ungetc(car,fp); |
| 1154 | if(pos == BUFFER_SIZE) in->error(in, "number token too large"); |
| 1155 | in->type = NUMBER; |
| 1156 | } |
| 1157 | else if(isalpha(car)) { |
| 1158 | in->buffer[0] = car; |
| 1159 | pos = 1; |
| 1160 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1161 | if(!(isalnum(car) || car == '_')) { |
| 1162 | ungetc(car,fp); |
| 1163 | break; |
| 1164 | } |
| 1165 | in->buffer[pos] = car; |
| 1166 | pos++; |
| 1167 | } |
| 1168 | if(car == EOF) ungetc(car,fp); |
| 1169 | if(pos == BUFFER_SIZE) in->error(in, "name token too large"); |
| 1170 | in->type = NAME; |
| 1171 | } |
| 1172 | else in->error(in, "invalid character, unrecognized token"); |
| 1173 | } |
| 1174 | in->buffer[pos] = 0; |
| 1175 | return in->buffer; |
| 1176 | } |
| 1177 | |
| 1178 | void skipComment(parse_file_t * in) |
| 1179 | { |
| 1180 | char car; |
| 1181 | while((car = getc(in->fp)) != EOF) { |
| 1182 | if(car == '\n') in->lineno++; |
| 1183 | else if(car == '*') { |
| 1184 | car = getc(in->fp); |
| 1185 | if(car ==EOF) break; |
| 1186 | if(car == '/') return; |
| 1187 | ungetc(car,in->fp); |
| 1188 | } |
| 1189 | } |
| 1190 | if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'"); |
| 1191 | } |
| 1192 | |
| 1193 | void skipEOL(parse_file_t * in) |
| 1194 | { |
| 1195 | char car; |
| 1196 | while((car = getc(in->fp)) != EOF) { |
| 1197 | if(car == '\n') { |
| 1198 | ungetc(car,in->fp); |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | if(car == EOF)ungetc(car, in->fp); |
| 1203 | } |
| 1204 | |
| 1205 | /***************************************************************************** |
| 1206 | *Function name |
| 1207 | * checkNamedTypesImplemented : check if all named types have definition |
| 1208 | ****************************************************************************/ |
| 1209 | |
| 1210 | void checkNamedTypesImplemented(table_t * named_types) |
| 1211 | { |
| 1212 | type_descriptor_t *t; |
| 1213 | int pos; |
| 1214 | char str[256]; |
| 1215 | |
| 1216 | for(pos = 0 ; pos < named_types->values.position; pos++) { |
| 1217 | t = (type_descriptor_t *) named_types->values.array[pos]; |
| 1218 | if(t->type == NONE){ |
| 1219 | sprintf(str,"named type '%s' has no definition", |
| 1220 | (char*)named_types->keys.array[pos]); |
| 1221 | error_callback(NULL,str); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | /***************************************************************************** |
| 1228 | *Function name |
| 1229 | * generateChecksum : generate checksum for the facility |
| 1230 | *Input Params |
| 1231 | * facName : name of facility |
| 1232 | *Output Params |
| 1233 | * checksum : checksum for the facility |
| 1234 | ****************************************************************************/ |
| 1235 | |
| 1236 | void generateChecksum(char* facName, |
| 1237 | unsigned int * checksum, sequence_t * events) |
| 1238 | { |
| 1239 | unsigned long crc ; |
| 1240 | int pos; |
| 1241 | event_t * ev; |
| 1242 | |
| 1243 | crc = crc32(facName); |
| 1244 | for(pos = 0; pos < events->position; pos++){ |
| 1245 | ev = (event_t *)(events->array[pos]); |
| 1246 | crc = partial_crc32(ev->name, crc); |
| 1247 | for(unsigned int i = 0; i < ev->fields.position; i++) { |
| 1248 | field_t *f = (field_t*)ev->fields.array[i]; |
| 1249 | crc = partial_crc32(f->name, crc); |
| 1250 | crc = getTypeChecksum(crc, f->type); |
| 1251 | } |
| 1252 | } |
| 1253 | *checksum = crc; |
| 1254 | } |
| 1255 | |
| 1256 | /***************************************************************************** |
| 1257 | *Function name |
| 1258 | * getTypeChecksum : generate checksum by type info |
| 1259 | *Input Params |
| 1260 | * crc : checksum generated so far |
| 1261 | * type : type descriptor containing type info |
| 1262 | *Return value |
| 1263 | * unsigned long : checksum |
| 1264 | *****************************************************************************/ |
| 1265 | |
| 1266 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type) |
| 1267 | { |
| 1268 | unsigned long crc = aCrc; |
| 1269 | char * str = NULL, buf[16]; |
| 1270 | int flag = 0, pos; |
| 1271 | field_t * fld; |
| 1272 | |
| 1273 | switch(type->type){ |
| 1274 | case INT_FIXED: |
| 1275 | str = intOutputTypes[getSizeindex(type->size)]; |
| 1276 | break; |
| 1277 | case UINT_FIXED: |
| 1278 | str = uintOutputTypes[getSizeindex(type->size)]; |
| 1279 | break; |
| 1280 | case POINTER: |
| 1281 | str = allocAndCopy("void *"); |
| 1282 | flag = 1; |
| 1283 | break; |
| 1284 | case CHAR: |
| 1285 | str = allocAndCopy("signed char"); |
| 1286 | flag = 1; |
| 1287 | break; |
| 1288 | case UCHAR: |
| 1289 | str = allocAndCopy("unsigned char"); |
| 1290 | flag = 1; |
| 1291 | break; |
| 1292 | case SHORT: |
| 1293 | str = allocAndCopy("short"); |
| 1294 | flag = 1; |
| 1295 | break; |
| 1296 | case USHORT: |
| 1297 | str = allocAndCopy("unsigned short"); |
| 1298 | flag = 1; |
| 1299 | break; |
| 1300 | case INT: |
| 1301 | str = allocAndCopy("int"); |
| 1302 | flag = 1; |
| 1303 | break; |
| 1304 | case UINT: |
| 1305 | str = allocAndCopy("uint"); |
| 1306 | flag = 1; |
| 1307 | break; |
| 1308 | case LONG: |
| 1309 | str = allocAndCopy("long"); |
| 1310 | flag = 1; |
| 1311 | break; |
| 1312 | case ULONG: |
| 1313 | str = allocAndCopy("unsigned long"); |
| 1314 | flag = 1; |
| 1315 | break; |
| 1316 | case SIZE_T: |
| 1317 | str = allocAndCopy("size_t"); |
| 1318 | flag = 1; |
| 1319 | break; |
| 1320 | case SSIZE_T: |
| 1321 | str = allocAndCopy("ssize_t"); |
| 1322 | flag = 1; |
| 1323 | break; |
| 1324 | case OFF_T: |
| 1325 | str = allocAndCopy("off_t"); |
| 1326 | flag = 1; |
| 1327 | break; |
| 1328 | case FLOAT: |
| 1329 | str = floatOutputTypes[getSizeindex(type->size)]; |
| 1330 | break; |
| 1331 | case STRING: |
| 1332 | str = allocAndCopy("string"); |
| 1333 | flag = 1; |
| 1334 | break; |
| 1335 | case ENUM: |
| 1336 | //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]); |
| 1337 | str = allocAndCopy("enum"); |
| 1338 | flag = 1; |
| 1339 | break; |
| 1340 | case ARRAY: |
| 1341 | sprintf(buf,"%zu", type->size); |
| 1342 | str = appendString("array ",buf); |
| 1343 | flag = 1; |
| 1344 | break; |
| 1345 | case SEQUENCE: |
| 1346 | sprintf(buf,"%zu", type->size); |
| 1347 | str = appendString("sequence ",buf); |
| 1348 | flag = 1; |
| 1349 | break; |
| 1350 | case STRUCT: |
| 1351 | str = allocAndCopy("struct"); |
| 1352 | flag = 1; |
| 1353 | break; |
| 1354 | case UNION: |
| 1355 | str = allocAndCopy("union"); |
| 1356 | flag = 1; |
| 1357 | break; |
| 1358 | default: |
| 1359 | error_callback(NULL, "named type has no definition"); |
| 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | crc = partial_crc32(str,crc); |
| 1364 | if(flag) free(str); |
| 1365 | |
| 1366 | if(type->fmt) crc = partial_crc32(type->fmt,crc); |
| 1367 | |
| 1368 | if(type->type == ARRAY){ |
| 1369 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
| 1370 | } else if(type->type ==SEQUENCE) { |
| 1371 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
| 1372 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type); |
| 1373 | } else if(type->type == STRUCT || type->type == UNION){ |
| 1374 | for(pos =0; pos < type->fields.position; pos++){ |
| 1375 | fld = (field_t *) type->fields.array[pos]; |
| 1376 | crc = partial_crc32(fld->name,crc); |
| 1377 | crc = getTypeChecksum(crc, fld->type); |
| 1378 | } |
| 1379 | }else if(type->type == ENUM){ |
| 1380 | for(pos = 0; pos < type->labels.position; pos++) |
| 1381 | crc = partial_crc32((char*)type->labels.array[pos],crc); |
| 1382 | } |
| 1383 | |
| 1384 | return crc; |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | /* Event type descriptors */ |
| 1389 | void freeType(type_descriptor_t * tp) |
| 1390 | { |
| 1391 | int pos2; |
| 1392 | field_t *f; |
| 1393 | |
| 1394 | if(tp->fmt != NULL) free(tp->fmt); |
| 1395 | if(tp->type == ENUM) { |
| 1396 | for(pos2 = 0; pos2 < tp->labels.position; pos2++) { |
| 1397 | free(tp->labels.array[pos2]); |
| 1398 | } |
| 1399 | sequence_dispose(&(tp->labels)); |
| 1400 | for(pos2 = 0; pos2 < tp->labels_values.position; pos2++) { |
| 1401 | free(tp->labels_values.array[pos2]); |
| 1402 | } |
| 1403 | sequence_dispose(&(tp->labels_values)); |
| 1404 | } |
| 1405 | if(tp->type == STRUCT) { |
| 1406 | for(pos2 = 0; pos2 < tp->fields.position; pos2++) { |
| 1407 | f = (field_t *) tp->fields.array[pos2]; |
| 1408 | free(f->name); |
| 1409 | free(f->description); |
| 1410 | free(f); |
| 1411 | } |
| 1412 | sequence_dispose(&(tp->fields)); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | void freeNamedType(table_t * t) |
| 1417 | { |
| 1418 | int pos; |
| 1419 | type_descriptor_t * td; |
| 1420 | |
| 1421 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1422 | free((char *)t->keys.array[pos]); |
| 1423 | td = (type_descriptor_t*)t->values.array[pos]; |
| 1424 | freeType(td); |
| 1425 | free(td); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | void freeTypes(sequence_t *t) |
| 1430 | { |
| 1431 | int pos; |
| 1432 | type_descriptor_t *tp; |
| 1433 | |
| 1434 | for(pos = 0 ; pos < t->position; pos++) { |
| 1435 | tp = (type_descriptor_t *)t->array[pos]; |
| 1436 | freeType(tp); |
| 1437 | free(tp); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | void freeEvents(sequence_t *t) |
| 1442 | { |
| 1443 | int pos; |
| 1444 | event_t *ev; |
| 1445 | |
| 1446 | for(pos = 0 ; pos < t->position; pos++) { |
| 1447 | ev = (event_t *) t->array[pos]; |
| 1448 | free(ev->name); |
| 1449 | free(ev->description); |
| 1450 | sequence_dispose(&ev->fields); |
| 1451 | free(ev); |
| 1452 | } |
| 1453 | |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | /* Extensible array */ |
| 1458 | |
| 1459 | void sequence_init(sequence_t *t) |
| 1460 | { |
| 1461 | t->size = 10; |
| 1462 | t->position = 0; |
| 1463 | t->array = (void **)memAlloc(t->size * sizeof(void *)); |
| 1464 | } |
| 1465 | |
| 1466 | void sequence_dispose(sequence_t *t) |
| 1467 | { |
| 1468 | t->size = 0; |
| 1469 | free(t->array); |
| 1470 | t->array = NULL; |
| 1471 | } |
| 1472 | |
| 1473 | void sequence_push(sequence_t *t, void *elem) |
| 1474 | { |
| 1475 | void **tmp; |
| 1476 | |
| 1477 | if(t->position >= t->size) { |
| 1478 | tmp = t->array; |
| 1479 | t->array = (void **)memAlloc(t->size * 2 * sizeof(void *)); |
| 1480 | memcpy(t->array, tmp, t->size * sizeof(void *)); |
| 1481 | t->size = t->size * 2; |
| 1482 | free(tmp); |
| 1483 | } |
| 1484 | t->array[t->position] = elem; |
| 1485 | t->position++; |
| 1486 | } |
| 1487 | |
| 1488 | void *sequence_pop(sequence_t *t) |
| 1489 | { |
| 1490 | return t->array[t->position--]; |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | /* Hash table API, implementation is just linear search for now */ |
| 1495 | |
| 1496 | void table_init(table_t *t) |
| 1497 | { |
| 1498 | sequence_init(&(t->keys)); |
| 1499 | sequence_init(&(t->values)); |
| 1500 | } |
| 1501 | |
| 1502 | void table_dispose(table_t *t) |
| 1503 | { |
| 1504 | sequence_dispose(&(t->keys)); |
| 1505 | sequence_dispose(&(t->values)); |
| 1506 | } |
| 1507 | |
| 1508 | void table_insert(table_t *t, char *key, void *value) |
| 1509 | { |
| 1510 | sequence_push(&(t->keys),key); |
| 1511 | sequence_push(&(t->values),value); |
| 1512 | } |
| 1513 | |
| 1514 | void *table_find(table_t *t, char *key) |
| 1515 | { |
| 1516 | int pos; |
| 1517 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1518 | if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0) |
| 1519 | return(t->values.array[pos]); |
| 1520 | } |
| 1521 | return NULL; |
| 1522 | } |
| 1523 | |
| 1524 | void table_insert_int(table_t *t, int *key, void *value) |
| 1525 | { |
| 1526 | sequence_push(&(t->keys),key); |
| 1527 | sequence_push(&(t->values),value); |
| 1528 | } |
| 1529 | |
| 1530 | void *table_find_int(table_t *t, int *key) |
| 1531 | { |
| 1532 | int pos; |
| 1533 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1534 | if(*key == *(int *)t->keys.array[pos]) |
| 1535 | return(t->values.array[pos]); |
| 1536 | } |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | /* Concatenate strings */ |
| 1542 | |
| 1543 | char *appendString(char *s, char *suffix) |
| 1544 | { |
| 1545 | char *tmp; |
| 1546 | if(suffix == NULL) return s; |
| 1547 | |
| 1548 | tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1); |
| 1549 | strcpy(tmp,s); |
| 1550 | strcat(tmp,suffix); |
| 1551 | return tmp; |
| 1552 | } |
| 1553 | |
| 1554 | |