| 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 | fac->arch = NULL; |
| 271 | |
| 272 | while(1) { |
| 273 | token = getToken(in); |
| 274 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 275 | ungetToken(in); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | if(!strcmp("name",token)) { |
| 280 | getEqual(in); |
| 281 | car = seekNextChar(in); |
| 282 | if(car == EOF) in->error(in,"name was expected"); |
| 283 | else if(car == '\"') fac->name = allocAndCopy(getQuotedString(in)); |
| 284 | else fac->name = allocAndCopy(getName(in)); |
| 285 | } else if(!strcmp("arch", token)) { |
| 286 | getEqual(in); |
| 287 | car = seekNextChar(in); |
| 288 | if(car == '\"') fac->name = allocAndCopy(getQuotedString(in)); |
| 289 | else fac->arch = allocAndCopy(getName(in)); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /************************************************************************** |
| 295 | * Function : |
| 296 | * getFieldAttributes |
| 297 | * Description : |
| 298 | * Read the attribute from the input file. |
| 299 | * |
| 300 | * Parameters : |
| 301 | * in , input file handle. |
| 302 | * f , the field to fill. |
| 303 | * |
| 304 | **************************************************************************/ |
| 305 | |
| 306 | void getFieldAttributes(parse_file_t *in, field_t *f) |
| 307 | { |
| 308 | char * token; |
| 309 | char car; |
| 310 | |
| 311 | f->name = NULL; |
| 312 | |
| 313 | while(1) { |
| 314 | token = getToken(in); |
| 315 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 316 | ungetToken(in); |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | if(!strcmp("name",token)) { |
| 321 | getEqual(in); |
| 322 | car = seekNextChar(in); |
| 323 | if(car == EOF) in->error(in,"name was expected"); |
| 324 | else if(car == '\"') f->name = allocAndCopy(getQuotedString(in)); |
| 325 | else f->name = allocAndCopy(getName(in)); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | char *getNameAttribute(parse_file_t *in) |
| 331 | { |
| 332 | char * token; |
| 333 | char *name = NULL; |
| 334 | char car; |
| 335 | |
| 336 | while(1) { |
| 337 | token = getToken(in); |
| 338 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
| 339 | ungetToken(in); |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | if(!strcmp("name",token)) { |
| 344 | getEqual(in); |
| 345 | car = seekNextChar(in); |
| 346 | if(car == EOF) in->error(in,"name was expected"); |
| 347 | else if(car == '\"') name = allocAndCopy(getQuotedString(in)); |
| 348 | else name = allocAndCopy(getName(in)); |
| 349 | } |
| 350 | } |
| 351 | if(name == NULL) in->error(in, "Name was expected"); |
| 352 | return name; |
| 353 | |
| 354 | } |
| 355 | |
| 356 | |
| 357 | |
| 358 | //for <label name=label_name value=n format="..."/>, value is an option |
| 359 | char * getValueStrAttribute(parse_file_t *in) |
| 360 | { |
| 361 | char * token; |
| 362 | |
| 363 | token = getToken(in); |
| 364 | if(strcmp("/",token) == 0){ |
| 365 | ungetToken(in); |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | if(strcmp("value",token))in->error(in,"value was expected"); |
| 370 | getEqual(in); |
| 371 | token = getToken(in); |
| 372 | if(in->type != NUMBER) in->error(in,"number was expected"); |
| 373 | return token; |
| 374 | } |
| 375 | |
| 376 | char * getDescription(parse_file_t *in) |
| 377 | { |
| 378 | long int pos; |
| 379 | char * token, car, *str; |
| 380 | |
| 381 | pos = ftell(in->fp); |
| 382 | |
| 383 | getLAnglebracket(in); |
| 384 | token = getName(in); |
| 385 | if(strcmp("description",token)){ |
| 386 | fseek(in->fp, pos, SEEK_SET); |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | getRAnglebracket(in); |
| 391 | |
| 392 | pos = 0; |
| 393 | while((car = getc(in->fp)) != EOF) { |
| 394 | if(car == '<') break; |
| 395 | if(car == '\0') continue; |
| 396 | in->buffer[pos] = car; |
| 397 | pos++; |
| 398 | } |
| 399 | if(car == EOF)in->error(in,"not a valid description"); |
| 400 | in->buffer[pos] = '\0'; |
| 401 | |
| 402 | str = allocAndCopy(in->buffer); |
| 403 | |
| 404 | getForwardslash(in); |
| 405 | token = getName(in); |
| 406 | if(strcmp("description", token))in->error(in,"not a valid description"); |
| 407 | getRAnglebracket(in); |
| 408 | |
| 409 | return str; |
| 410 | } |
| 411 | |
| 412 | /***************************************************************************** |
| 413 | *Function name |
| 414 | * parseFacility : generate event list |
| 415 | *Input params |
| 416 | * in : input file handle |
| 417 | * fac : empty facility |
| 418 | *Output params |
| 419 | * fac : facility filled with event list |
| 420 | ****************************************************************************/ |
| 421 | |
| 422 | void parseFacility(parse_file_t *in, facility_t * fac) |
| 423 | { |
| 424 | char * token; |
| 425 | event_t *ev; |
| 426 | |
| 427 | getFacilityAttributes(in, fac); |
| 428 | if(fac->name == NULL) in->error(in, "Attribute not named"); |
| 429 | |
| 430 | fac->capname = allocAndCopy(fac->name); |
| 431 | strupper(fac->capname); |
| 432 | getRAnglebracket(in); |
| 433 | |
| 434 | fac->description = getDescription(in); |
| 435 | |
| 436 | while(1){ |
| 437 | getLAnglebracket(in); |
| 438 | |
| 439 | token = getToken(in); |
| 440 | if(in->type == ENDFILE) |
| 441 | in->error(in,"the definition of the facility is not finished"); |
| 442 | |
| 443 | if(strcmp("event",token) == 0){ |
| 444 | ev = (event_t*) memAlloc(sizeof(event_t)); |
| 445 | sequence_push(&(fac->events),ev); |
| 446 | parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types)); |
| 447 | }else if(strcmp("type",token) == 0){ |
| 448 | parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types)); |
| 449 | }else if(in->type == FORWARDSLASH){ |
| 450 | break; |
| 451 | }else in->error(in,"event or type token expected\n"); |
| 452 | } |
| 453 | |
| 454 | token = getName(in); |
| 455 | if(strcmp("facility",token)) in->error(in,"not the end of the facility"); |
| 456 | getRAnglebracket(in); //</facility> |
| 457 | } |
| 458 | |
| 459 | /***************************************************************************** |
| 460 | *Function name |
| 461 | * parseEvent : generate event from event definition |
| 462 | *Input params |
| 463 | * in : input file handle |
| 464 | * ev : new event |
| 465 | * unnamed_types : array of unamed types |
| 466 | * named_types : array of named types |
| 467 | *Output params |
| 468 | * ev : new event (parameters are passed to it) |
| 469 | ****************************************************************************/ |
| 470 | |
| 471 | void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types, |
| 472 | table_t * named_types) |
| 473 | { |
| 474 | char *token; |
| 475 | field_t *f; |
| 476 | |
| 477 | sequence_init(&(ev->fields)); |
| 478 | //<event name=eventtype_name> |
| 479 | getEventAttributes(in, ev); |
| 480 | if(ev->name == NULL) in->error(in, "Event not named"); |
| 481 | getRAnglebracket(in); |
| 482 | |
| 483 | //<description>...</description> |
| 484 | ev->description = getDescription(in); |
| 485 | |
| 486 | int got_end = 0; |
| 487 | /* Events can have multiple fields. each field form at least a function |
| 488 | * parameter of the logging function. */ |
| 489 | while(!got_end) { |
| 490 | getLAnglebracket(in); |
| 491 | token = getToken(in); |
| 492 | |
| 493 | switch(in->type) { |
| 494 | case FORWARDSLASH: /* </event> */ |
| 495 | token = getName(in); |
| 496 | if(strcmp("event",token))in->error(in,"not an event definition"); |
| 497 | getRAnglebracket(in); //</event> |
| 498 | got_end = 1; |
| 499 | break; |
| 500 | case NAME: /* a field */ |
| 501 | if(strcmp("field",token))in->error(in,"expecting a field"); |
| 502 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 503 | sequence_push(&(ev->fields),f); |
| 504 | parseFields(in, f, unnamed_types, named_types, 1); |
| 505 | break; |
| 506 | default: |
| 507 | in->error(in, "expecting </event> or <field >"); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | #if 0 |
| 512 | if(in->type == FORWARDSLASH){ //</event> NOTHING |
| 513 | ev->type = NULL; |
| 514 | }else if(in->type == NAME){ |
| 515 | if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){ |
| 516 | ungetToken(in); |
| 517 | ev->type = parseType(in,NULL, unnamed_types, named_types); |
| 518 | if(ev->type->type != STRUCT && ev->type->type != NONE) |
| 519 | in->error(in,"type must be a struct"); |
| 520 | }else in->error(in, "not a valid type"); |
| 521 | |
| 522 | getLAnglebracket(in); |
| 523 | getForwardslash(in); |
| 524 | }else in->error(in,"not a struct type"); |
| 525 | getLAnglebracket(in); |
| 526 | getForwardslash(in); |
| 527 | token = getName(in); |
| 528 | if(strcmp("event",token))in->error(in,"not an event definition"); |
| 529 | getRAnglebracket(in); //</event> |
| 530 | #endif //0 |
| 531 | } |
| 532 | |
| 533 | /***************************************************************************** |
| 534 | *Function name |
| 535 | * parseField : get field infomation from buffer |
| 536 | *Input params |
| 537 | * in : input file handle |
| 538 | * f : field |
| 539 | * unnamed_types : array of unamed types |
| 540 | * named_types : array of named types |
| 541 | * tag : is field surrounded by a <field> </field> tag ? |
| 542 | ****************************************************************************/ |
| 543 | |
| 544 | void parseFields(parse_file_t *in, field_t *f, |
| 545 | sequence_t * unnamed_types, |
| 546 | table_t * named_types, |
| 547 | int tag) |
| 548 | { |
| 549 | char * token; |
| 550 | if(tag) { |
| 551 | //<field name=field_name> <description> <type> </field> |
| 552 | getFieldAttributes(in, f); |
| 553 | if(f->name == NULL) in->error(in, "Field not named"); |
| 554 | getRAnglebracket(in); |
| 555 | |
| 556 | f->description = getDescription(in); |
| 557 | } |
| 558 | |
| 559 | //<int size=...> |
| 560 | getLAnglebracket(in); |
| 561 | f->type = parseType(in,NULL, unnamed_types, named_types); |
| 562 | |
| 563 | if(tag) { |
| 564 | getLAnglebracket(in); |
| 565 | getForwardslash(in); |
| 566 | token = getName(in); |
| 567 | if(strcmp("field",token))in->error(in,"not a valid field definition"); |
| 568 | getRAnglebracket(in); //</field> |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /***************************************************************************** |
| 574 | *Function name |
| 575 | * parseType : get type information, type can be : |
| 576 | * Primitive: |
| 577 | * int(size,fmt); uint(size,fmt); float(size,fmt); |
| 578 | * string(fmt); enum(size,fmt,(label1,label2...)) |
| 579 | * Compound: |
| 580 | * array(arraySize, type); sequence(lengthSize,type) |
| 581 | * struct(field(name,type,description)...) |
| 582 | * type name: |
| 583 | * type(name,type) |
| 584 | *Input params |
| 585 | * in : input file handle |
| 586 | * inType : a type descriptor |
| 587 | * unnamed_types : array of unamed types |
| 588 | * named_types : array of named types |
| 589 | *Return values |
| 590 | * type_descriptor* : a type descriptor |
| 591 | ****************************************************************************/ |
| 592 | |
| 593 | type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType, |
| 594 | sequence_t * unnamed_types, table_t * named_types) |
| 595 | { |
| 596 | char *token; |
| 597 | type_descriptor_t *t; |
| 598 | field_t *f; |
| 599 | |
| 600 | if(inType == NULL) { |
| 601 | t = (type_descriptor_t *) memAlloc(sizeof(type_descriptor_t)); |
| 602 | t->type_name = NULL; |
| 603 | t->type = NONE; |
| 604 | t->fmt = NULL; |
| 605 | sequence_push(unnamed_types,t); |
| 606 | } |
| 607 | else t = inType; |
| 608 | |
| 609 | token = getName(in); |
| 610 | |
| 611 | if(strcmp(token,"struct") == 0) { |
| 612 | t->type = STRUCT; |
| 613 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 614 | getRAnglebracket(in); //<struct> |
| 615 | getLAnglebracket(in); //<field name=..> |
| 616 | token = getToken(in); |
| 617 | sequence_init(&(t->fields)); |
| 618 | while(strcmp("field",token) == 0){ |
| 619 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 620 | sequence_push(&(t->fields),f); |
| 621 | |
| 622 | parseFields(in, f, unnamed_types, named_types, 1); |
| 623 | |
| 624 | //next field |
| 625 | getLAnglebracket(in); |
| 626 | token = getToken(in); |
| 627 | } |
| 628 | if(strcmp("/",token))in->error(in,"not a valid structure definition"); |
| 629 | token = getName(in); |
| 630 | if(strcmp("struct",token)!=0) |
| 631 | in->error(in,"not a valid structure definition"); |
| 632 | getRAnglebracket(in); //</struct> |
| 633 | } |
| 634 | else if(strcmp(token,"union") == 0) { |
| 635 | t->type = UNION; |
| 636 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 637 | getRAnglebracket(in); //<union> |
| 638 | |
| 639 | getLAnglebracket(in); //<field name=..> |
| 640 | token = getToken(in); |
| 641 | sequence_init(&(t->fields)); |
| 642 | while(strcmp("field",token) == 0){ |
| 643 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 644 | sequence_push(&(t->fields),f); |
| 645 | parseFields(in, f, unnamed_types, named_types, 1); |
| 646 | |
| 647 | //next field |
| 648 | getLAnglebracket(in); |
| 649 | token = getToken(in); |
| 650 | } |
| 651 | if(strcmp("/",token))in->error(in,"not a valid union definition"); |
| 652 | token = getName(in); |
| 653 | if(strcmp("union",token)!=0) |
| 654 | in->error(in,"not a valid union definition"); |
| 655 | getRAnglebracket(in); //</union> |
| 656 | } |
| 657 | else if(strcmp(token,"array") == 0) { |
| 658 | t->type = ARRAY; |
| 659 | sequence_init(&(t->fields)); |
| 660 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 661 | if(t->size == 0) in->error(in, "Array has empty size"); |
| 662 | getForwardslash(in); |
| 663 | getRAnglebracket(in); //<array size=n> |
| 664 | |
| 665 | //getLAnglebracket(in); //<subtype> |
| 666 | /* subfield */ |
| 667 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 668 | sequence_push(&(t->fields),f); |
| 669 | parseFields(in, f, unnamed_types, named_types, 0); |
| 670 | |
| 671 | //getLAnglebracket(in); //<type struct> |
| 672 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
| 673 | |
| 674 | getLAnglebracket(in); //</array> |
| 675 | getForwardslash(in); |
| 676 | token = getName(in); |
| 677 | if(strcmp("array",token))in->error(in,"not a valid array definition"); |
| 678 | getRAnglebracket(in); //</array> |
| 679 | } |
| 680 | else if(strcmp(token,"sequence") == 0) { |
| 681 | t->type = SEQUENCE; |
| 682 | sequence_init(&(t->fields)); |
| 683 | //getTypeAttributes(in, t, unnamed_types, named_types); |
| 684 | //getForwardslash(in); |
| 685 | getRAnglebracket(in); //<sequence> |
| 686 | |
| 687 | //getLAnglebracket(in); //<sequence size type> |
| 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); //<subtype> |
| 694 | /* subfield */ |
| 695 | f = (field_t *)memAlloc(sizeof(field_t)); |
| 696 | sequence_push(&(t->fields),f); |
| 697 | parseFields(in, f, unnamed_types, named_types, 0); |
| 698 | |
| 699 | //getLAnglebracket(in); //<type sequence> |
| 700 | //t->length_type = parseType(in, NULL, unnamed_types, named_types); |
| 701 | |
| 702 | //getLAnglebracket(in); //<type sequence> |
| 703 | |
| 704 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
| 705 | |
| 706 | if(t->fields.position < 1) in->error(in, "Sequence has no length type"); |
| 707 | if(t->fields.position < 2) in->error(in, "Sequence has no subtype"); |
| 708 | switch(((field_t*)t->fields.array[0])->type->type) { |
| 709 | case UINT_FIXED : |
| 710 | case UCHAR : |
| 711 | case USHORT : |
| 712 | case UINT : |
| 713 | case ULONG : |
| 714 | case SIZE_T : |
| 715 | case OFF_T : |
| 716 | break; |
| 717 | default: |
| 718 | in->error(in, "Wrong length type for sequence"); |
| 719 | } |
| 720 | |
| 721 | getLAnglebracket(in); //</sequence> |
| 722 | getForwardslash(in); |
| 723 | token = getName(in); |
| 724 | if(strcmp("sequence",token))in->error(in,"not a valid sequence definition"); |
| 725 | getRAnglebracket(in); //</sequence> |
| 726 | } |
| 727 | else if(strcmp(token,"enum") == 0) { |
| 728 | char * str; |
| 729 | int value = -1; |
| 730 | |
| 731 | t->type = ENUM; |
| 732 | sequence_init(&(t->labels)); |
| 733 | sequence_init(&(t->labels_values)); |
| 734 | sequence_init(&(t->labels_description)); |
| 735 | t->already_printed = 0; |
| 736 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 737 | //if(t->size == 0) in->error(in, "Sequence has empty size"); |
| 738 | //Mathieu : we fix enum size to target int size. GCC is always like this. |
| 739 | //fox copy optimisation. |
| 740 | if(t->size != 0) in->error(in, "Enum has fixed size of target int."); |
| 741 | t->size = 0; |
| 742 | getRAnglebracket(in); |
| 743 | |
| 744 | //<label name=label1 value=n/> |
| 745 | getLAnglebracket(in); |
| 746 | token = getToken(in); //"label" or "/" |
| 747 | while(strcmp("label",token) == 0){ |
| 748 | int *label_value = malloc(sizeof(int)); |
| 749 | |
| 750 | str = allocAndCopy(getNameAttribute(in)); |
| 751 | token = getValueStrAttribute(in); |
| 752 | |
| 753 | sequence_push(&(t->labels),str); |
| 754 | |
| 755 | if(token) value = strtol(token, NULL, 0); |
| 756 | else value++; |
| 757 | |
| 758 | *label_value = value; |
| 759 | sequence_push(&(t->labels_values), label_value); |
| 760 | |
| 761 | getForwardslash(in); |
| 762 | getRAnglebracket(in); |
| 763 | |
| 764 | //read description if any. May be NULL. |
| 765 | str = allocAndCopy(getDescription(in)); |
| 766 | sequence_push(&(t->labels_description),str); |
| 767 | |
| 768 | //next label definition |
| 769 | getLAnglebracket(in); |
| 770 | token = getToken(in); //"label" or "/" |
| 771 | } |
| 772 | if(strcmp("/",token))in->error(in, "not a valid enum definition"); |
| 773 | token = getName(in); |
| 774 | if(strcmp("enum",token))in->error(in, "not a valid enum definition"); |
| 775 | getRAnglebracket(in); //</label> |
| 776 | } |
| 777 | else if(strcmp(token,"int_fixed") == 0) { |
| 778 | t->type = INT_FIXED; |
| 779 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 780 | if(t->size == 0) in->error(in, "int has empty size"); |
| 781 | getForwardslash(in); |
| 782 | getRAnglebracket(in); |
| 783 | } |
| 784 | else if(strcmp(token,"uint_fixed") == 0) { |
| 785 | t->type = UINT_FIXED; |
| 786 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 787 | if(t->size == 0) in->error(in, "uint has empty size"); |
| 788 | getForwardslash(in); |
| 789 | getRAnglebracket(in); |
| 790 | } |
| 791 | else if(strcmp(token,"char") == 0) { |
| 792 | t->type = CHAR; |
| 793 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 794 | t->size = 1; |
| 795 | getForwardslash(in); |
| 796 | getRAnglebracket(in); |
| 797 | } |
| 798 | else if(strcmp(token,"uchar") == 0) { |
| 799 | t->type = UCHAR; |
| 800 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 801 | t->size = 1; |
| 802 | getForwardslash(in); |
| 803 | getRAnglebracket(in); |
| 804 | } |
| 805 | else if(strcmp(token,"short") == 0) { |
| 806 | t->type = SHORT; |
| 807 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 808 | t->size = 2; |
| 809 | getForwardslash(in); |
| 810 | getRAnglebracket(in); |
| 811 | } |
| 812 | else if(strcmp(token,"ushort") == 0) { |
| 813 | t->type = USHORT; |
| 814 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 815 | t->size = 2; |
| 816 | getForwardslash(in); |
| 817 | getRAnglebracket(in); |
| 818 | } |
| 819 | else if(strcmp(token,"int") == 0) { |
| 820 | t->type = INT; |
| 821 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 822 | getForwardslash(in); |
| 823 | getRAnglebracket(in); |
| 824 | } |
| 825 | else if(strcmp(token,"uint") == 0) { |
| 826 | t->type = UINT; |
| 827 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 828 | getForwardslash(in); |
| 829 | getRAnglebracket(in); |
| 830 | } |
| 831 | |
| 832 | else if(strcmp(token,"pointer") == 0) { |
| 833 | t->type = POINTER; |
| 834 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 835 | getForwardslash(in); |
| 836 | getRAnglebracket(in); |
| 837 | } |
| 838 | else if(strcmp(token,"long") == 0) { |
| 839 | t->type = LONG; |
| 840 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 841 | getForwardslash(in); |
| 842 | getRAnglebracket(in); |
| 843 | } |
| 844 | else if(strcmp(token,"ulong") == 0) { |
| 845 | t->type = ULONG; |
| 846 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 847 | getForwardslash(in); |
| 848 | getRAnglebracket(in); |
| 849 | } |
| 850 | else if(strcmp(token,"size_t") == 0) { |
| 851 | t->type = SIZE_T; |
| 852 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 853 | getForwardslash(in); |
| 854 | getRAnglebracket(in); |
| 855 | } |
| 856 | else if(strcmp(token,"ssize_t") == 0) { |
| 857 | t->type = SSIZE_T; |
| 858 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 859 | getForwardslash(in); |
| 860 | getRAnglebracket(in); |
| 861 | } |
| 862 | else if(strcmp(token,"off_t") == 0) { |
| 863 | t->type = OFF_T; |
| 864 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 865 | getForwardslash(in); |
| 866 | getRAnglebracket(in); |
| 867 | } |
| 868 | else if(strcmp(token,"float") == 0) { |
| 869 | t->type = FLOAT; |
| 870 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 871 | getForwardslash(in); |
| 872 | getRAnglebracket(in); |
| 873 | } |
| 874 | else if(strcmp(token,"string") == 0) { |
| 875 | t->type = STRING; |
| 876 | getTypeAttributes(in, t, unnamed_types, named_types); |
| 877 | getForwardslash(in); |
| 878 | getRAnglebracket(in); |
| 879 | } |
| 880 | else if(strcmp(token,"typeref") == 0){ |
| 881 | // Must be a named type |
| 882 | free(t); |
| 883 | sequence_pop(unnamed_types); |
| 884 | token = getNameAttribute(in); |
| 885 | t = find_named_type(token, named_types); |
| 886 | if(t == NULL) in->error(in,"Named referred to must be pre-declared."); |
| 887 | getForwardslash(in); //<typeref name=type_name/> |
| 888 | getRAnglebracket(in); |
| 889 | return t; |
| 890 | }else in->error(in,"not a valid type"); |
| 891 | |
| 892 | return t; |
| 893 | } |
| 894 | |
| 895 | /***************************************************************************** |
| 896 | *Function name |
| 897 | * find_named_type : find a named type from hash table |
| 898 | *Input params |
| 899 | * name : type name |
| 900 | * named_types : array of named types |
| 901 | *Return values |
| 902 | * type_descriptor * : a type descriptor |
| 903 | *****************************************************************************/ |
| 904 | |
| 905 | type_descriptor_t * find_named_type(char *name, table_t * named_types) |
| 906 | { |
| 907 | type_descriptor_t *t; |
| 908 | |
| 909 | t = table_find(named_types,name); |
| 910 | |
| 911 | return t; |
| 912 | } |
| 913 | |
| 914 | type_descriptor_t * create_named_type(char *name, table_t * named_types) |
| 915 | { |
| 916 | type_descriptor_t *t; |
| 917 | |
| 918 | t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t)); |
| 919 | t->type_name = allocAndCopy(name); |
| 920 | t->type = NONE; |
| 921 | t->fmt = NULL; |
| 922 | table_insert(named_types,t->type_name,t); |
| 923 | // table_insert(named_types,allocAndCopy(name),t); |
| 924 | return t; |
| 925 | } |
| 926 | |
| 927 | /***************************************************************************** |
| 928 | *Function name |
| 929 | * parseTypeDefinition : get type information from type definition |
| 930 | *Input params |
| 931 | * in : input file handle |
| 932 | * unnamed_types : array of unamed types |
| 933 | * named_types : array of named types |
| 934 | *****************************************************************************/ |
| 935 | |
| 936 | void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types, |
| 937 | table_t * named_types) |
| 938 | { |
| 939 | char *token; |
| 940 | type_descriptor_t *t; |
| 941 | |
| 942 | token = getNameAttribute(in); |
| 943 | if(token == NULL) in->error(in, "Type has empty name"); |
| 944 | t = create_named_type(token, named_types); |
| 945 | |
| 946 | if(t->type != NONE) in->error(in,"redefinition of named type"); |
| 947 | getRAnglebracket(in); //<type name=type_name> |
| 948 | getLAnglebracket(in); //< |
| 949 | token = getName(in); |
| 950 | //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition"); |
| 951 | ungetToken(in); |
| 952 | parseType(in,t, unnamed_types, named_types); |
| 953 | |
| 954 | //</type> |
| 955 | getLAnglebracket(in); |
| 956 | getForwardslash(in); |
| 957 | token = getName(in); |
| 958 | if(strcmp("type",token))in->error(in,"not a valid type definition"); |
| 959 | getRAnglebracket(in); //</type> |
| 960 | } |
| 961 | |
| 962 | /************************************************************************** |
| 963 | * Function : |
| 964 | * getComa, getName, getNumber, getEqual |
| 965 | * Description : |
| 966 | * Read a token from the input file, check its type, return it scontent. |
| 967 | * |
| 968 | * Parameters : |
| 969 | * in , input file handle. |
| 970 | * |
| 971 | * Return values : |
| 972 | * address of token content. |
| 973 | * |
| 974 | **************************************************************************/ |
| 975 | |
| 976 | char *getName(parse_file_t * in) |
| 977 | { |
| 978 | char *token; |
| 979 | |
| 980 | token = getToken(in); |
| 981 | // Optional descriptions |
| 982 | // if(in->type != NAME) in->error(in,"Name token was expected"); |
| 983 | return token; |
| 984 | } |
| 985 | |
| 986 | int getNumber(parse_file_t * in) |
| 987 | { |
| 988 | char *token; |
| 989 | |
| 990 | token = getToken(in); |
| 991 | if(in->type != NUMBER) in->error(in, "Number token was expected"); |
| 992 | return atoi(token); |
| 993 | } |
| 994 | |
| 995 | char *getForwardslash(parse_file_t * in) |
| 996 | { |
| 997 | char *token; |
| 998 | |
| 999 | token = getToken(in); |
| 1000 | //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected"); |
| 1001 | /* Mathieu : final / is optional now. */ |
| 1002 | if(in->type != FORWARDSLASH) ungetToken(in); |
| 1003 | |
| 1004 | return token; |
| 1005 | } |
| 1006 | |
| 1007 | char *getLAnglebracket(parse_file_t * in) |
| 1008 | { |
| 1009 | char *token; |
| 1010 | |
| 1011 | token = getToken(in); |
| 1012 | if(in->type != LANGLEBRACKET) in->error(in, "Left angle bracket was expected"); |
| 1013 | return token; |
| 1014 | } |
| 1015 | |
| 1016 | char *getRAnglebracket(parse_file_t * in) |
| 1017 | { |
| 1018 | char *token; |
| 1019 | |
| 1020 | token = getToken(in); |
| 1021 | if(in->type != RANGLEBRACKET) in->error(in, "Right angle bracket was expected"); |
| 1022 | return token; |
| 1023 | } |
| 1024 | |
| 1025 | char *getQuotedString(parse_file_t * in) |
| 1026 | { |
| 1027 | char *token; |
| 1028 | |
| 1029 | token = getToken(in); |
| 1030 | if(in->type != QUOTEDSTRING) in->error(in, "quoted string was expected"); |
| 1031 | return token; |
| 1032 | } |
| 1033 | |
| 1034 | char * getEqual(parse_file_t *in) |
| 1035 | { |
| 1036 | char *token; |
| 1037 | |
| 1038 | token = getToken(in); |
| 1039 | if(in->type != EQUAL) in->error(in, "equal was expected"); |
| 1040 | return token; |
| 1041 | } |
| 1042 | |
| 1043 | char seekNextChar(parse_file_t *in) |
| 1044 | { |
| 1045 | char car; |
| 1046 | while((car = getc(in->fp)) != EOF) { |
| 1047 | if(!isspace(car)){ |
| 1048 | ungetc(car,in->fp); |
| 1049 | return car; |
| 1050 | } |
| 1051 | } |
| 1052 | return EOF; |
| 1053 | } |
| 1054 | |
| 1055 | /****************************************************************** |
| 1056 | * Function : |
| 1057 | * getToken, ungetToken |
| 1058 | * Description : |
| 1059 | * Read a token from the input file and return its type and content. |
| 1060 | * Line numbers are accounted for and whitespace/comments are skipped. |
| 1061 | * |
| 1062 | * Parameters : |
| 1063 | * in, input file handle. |
| 1064 | * |
| 1065 | * Return values : |
| 1066 | * address of token content. |
| 1067 | * |
| 1068 | ******************************************************************/ |
| 1069 | |
| 1070 | void ungetToken(parse_file_t * in) |
| 1071 | { |
| 1072 | in->unget = 1; |
| 1073 | } |
| 1074 | |
| 1075 | char *getToken(parse_file_t * in) |
| 1076 | { |
| 1077 | FILE *fp = in->fp; |
| 1078 | char car, car1; |
| 1079 | int pos = 0, escaped; |
| 1080 | |
| 1081 | if(in->unget == 1) { |
| 1082 | in->unget = 0; |
| 1083 | return in->buffer; |
| 1084 | } |
| 1085 | |
| 1086 | /* skip whitespace and comments */ |
| 1087 | |
| 1088 | while((car = getc(fp)) != EOF) { |
| 1089 | if(car == '/') { |
| 1090 | car1 = getc(fp); |
| 1091 | if(car1 == '*') skipComment(in); |
| 1092 | else if(car1 == '/') skipEOL(in); |
| 1093 | else { |
| 1094 | car1 = ungetc(car1,fp); |
| 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | else if(car == '\n') in->lineno++; |
| 1099 | else if(!isspace(car)) break; |
| 1100 | } |
| 1101 | |
| 1102 | switch(car) { |
| 1103 | case EOF: |
| 1104 | in->type = ENDFILE; |
| 1105 | break; |
| 1106 | case '/': |
| 1107 | in->type = FORWARDSLASH; |
| 1108 | in->buffer[pos] = car; |
| 1109 | pos++; |
| 1110 | break; |
| 1111 | case '<': |
| 1112 | in->type = LANGLEBRACKET; |
| 1113 | in->buffer[pos] = car; |
| 1114 | pos++; |
| 1115 | break; |
| 1116 | case '>': |
| 1117 | in->type = RANGLEBRACKET; |
| 1118 | in->buffer[pos] = car; |
| 1119 | pos++; |
| 1120 | break; |
| 1121 | case '=': |
| 1122 | in->type = EQUAL; |
| 1123 | in->buffer[pos] = car; |
| 1124 | pos++; |
| 1125 | break; |
| 1126 | case '"': |
| 1127 | escaped = 0; |
| 1128 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1129 | if(car == '\\' && escaped == 0) { |
| 1130 | in->buffer[pos] = car; |
| 1131 | pos++; |
| 1132 | escaped = 1; |
| 1133 | continue; |
| 1134 | } |
| 1135 | if(car == '"' && escaped == 0) break; |
| 1136 | if(car == '\n' && escaped == 0) { |
| 1137 | in->error(in, "non escaped newline inside quoted string"); |
| 1138 | } |
| 1139 | if(car == '\n') in->lineno++; |
| 1140 | in->buffer[pos] = car; |
| 1141 | pos++; |
| 1142 | escaped = 0; |
| 1143 | } |
| 1144 | if(car == EOF) in->error(in,"no ending quotemark"); |
| 1145 | if(pos == BUFFER_SIZE) in->error(in, "quoted string token too large"); |
| 1146 | in->type = QUOTEDSTRING; |
| 1147 | break; |
| 1148 | default: |
| 1149 | if(isdigit(car)) { |
| 1150 | in->buffer[pos] = car; |
| 1151 | pos++; |
| 1152 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1153 | if(!isdigit(car)) { |
| 1154 | ungetc(car,fp); |
| 1155 | break; |
| 1156 | } |
| 1157 | in->buffer[pos] = car; |
| 1158 | pos++; |
| 1159 | } |
| 1160 | if(car == EOF) ungetc(car,fp); |
| 1161 | if(pos == BUFFER_SIZE) in->error(in, "number token too large"); |
| 1162 | in->type = NUMBER; |
| 1163 | } |
| 1164 | else if(isalnum(car) || car == '_' || car == '-') { |
| 1165 | in->buffer[0] = car; |
| 1166 | pos = 1; |
| 1167 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
| 1168 | if(!(isalnum(car) || car == '_' || car == '-')) { |
| 1169 | ungetc(car,fp); |
| 1170 | break; |
| 1171 | } |
| 1172 | in->buffer[pos] = car; |
| 1173 | pos++; |
| 1174 | } |
| 1175 | if(car == EOF) ungetc(car,fp); |
| 1176 | if(pos == BUFFER_SIZE) in->error(in, "name token too large"); |
| 1177 | in->type = NAME; |
| 1178 | } |
| 1179 | else in->error(in, "invalid character, unrecognized token"); |
| 1180 | } |
| 1181 | in->buffer[pos] = 0; |
| 1182 | return in->buffer; |
| 1183 | } |
| 1184 | |
| 1185 | void skipComment(parse_file_t * in) |
| 1186 | { |
| 1187 | char car; |
| 1188 | while((car = getc(in->fp)) != EOF) { |
| 1189 | if(car == '\n') in->lineno++; |
| 1190 | else if(car == '*') { |
| 1191 | car = getc(in->fp); |
| 1192 | if(car ==EOF) break; |
| 1193 | if(car == '/') return; |
| 1194 | ungetc(car,in->fp); |
| 1195 | } |
| 1196 | } |
| 1197 | if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'"); |
| 1198 | } |
| 1199 | |
| 1200 | void skipEOL(parse_file_t * in) |
| 1201 | { |
| 1202 | char car; |
| 1203 | while((car = getc(in->fp)) != EOF) { |
| 1204 | if(car == '\n') { |
| 1205 | ungetc(car,in->fp); |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | if(car == EOF)ungetc(car, in->fp); |
| 1210 | } |
| 1211 | |
| 1212 | /***************************************************************************** |
| 1213 | *Function name |
| 1214 | * checkNamedTypesImplemented : check if all named types have definition |
| 1215 | ****************************************************************************/ |
| 1216 | |
| 1217 | void checkNamedTypesImplemented(table_t * named_types) |
| 1218 | { |
| 1219 | type_descriptor_t *t; |
| 1220 | int pos; |
| 1221 | char str[256]; |
| 1222 | |
| 1223 | for(pos = 0 ; pos < named_types->values.position; pos++) { |
| 1224 | t = (type_descriptor_t *) named_types->values.array[pos]; |
| 1225 | if(t->type == NONE){ |
| 1226 | sprintf(str,"named type '%s' has no definition", |
| 1227 | (char*)named_types->keys.array[pos]); |
| 1228 | error_callback(NULL,str); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | /***************************************************************************** |
| 1235 | *Function name |
| 1236 | * generateChecksum : generate checksum for the facility |
| 1237 | *Input Params |
| 1238 | * facName : name of facility |
| 1239 | *Output Params |
| 1240 | * checksum : checksum for the facility |
| 1241 | ****************************************************************************/ |
| 1242 | |
| 1243 | void generateChecksum(char* facName, |
| 1244 | unsigned int * checksum, sequence_t * events) |
| 1245 | { |
| 1246 | unsigned long crc ; |
| 1247 | int pos; |
| 1248 | event_t * ev; |
| 1249 | unsigned int i; |
| 1250 | |
| 1251 | crc = crc32(facName); |
| 1252 | for(pos = 0; pos < events->position; pos++){ |
| 1253 | ev = (event_t *)(events->array[pos]); |
| 1254 | crc = partial_crc32(ev->name, crc); |
| 1255 | for(i = 0; i < ev->fields.position; i++) { |
| 1256 | field_t *f = (field_t*)ev->fields.array[i]; |
| 1257 | crc = partial_crc32(f->name, crc); |
| 1258 | crc = getTypeChecksum(crc, f->type); |
| 1259 | } |
| 1260 | } |
| 1261 | *checksum = crc; |
| 1262 | } |
| 1263 | |
| 1264 | /***************************************************************************** |
| 1265 | *Function name |
| 1266 | * getTypeChecksum : generate checksum by type info |
| 1267 | *Input Params |
| 1268 | * crc : checksum generated so far |
| 1269 | * type : type descriptor containing type info |
| 1270 | *Return value |
| 1271 | * unsigned long : checksum |
| 1272 | *****************************************************************************/ |
| 1273 | |
| 1274 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type) |
| 1275 | { |
| 1276 | unsigned long crc = aCrc; |
| 1277 | char * str = NULL, buf[16]; |
| 1278 | int flag = 0, pos; |
| 1279 | field_t * fld; |
| 1280 | |
| 1281 | switch(type->type){ |
| 1282 | case INT_FIXED: |
| 1283 | str = intOutputTypes[getSizeindex(type->size)]; |
| 1284 | break; |
| 1285 | case UINT_FIXED: |
| 1286 | str = uintOutputTypes[getSizeindex(type->size)]; |
| 1287 | break; |
| 1288 | case POINTER: |
| 1289 | str = allocAndCopy("void *"); |
| 1290 | flag = 1; |
| 1291 | break; |
| 1292 | case CHAR: |
| 1293 | str = allocAndCopy("signed char"); |
| 1294 | flag = 1; |
| 1295 | break; |
| 1296 | case UCHAR: |
| 1297 | str = allocAndCopy("unsigned char"); |
| 1298 | flag = 1; |
| 1299 | break; |
| 1300 | case SHORT: |
| 1301 | str = allocAndCopy("short"); |
| 1302 | flag = 1; |
| 1303 | break; |
| 1304 | case USHORT: |
| 1305 | str = allocAndCopy("unsigned short"); |
| 1306 | flag = 1; |
| 1307 | break; |
| 1308 | case INT: |
| 1309 | str = allocAndCopy("int"); |
| 1310 | flag = 1; |
| 1311 | break; |
| 1312 | case UINT: |
| 1313 | str = allocAndCopy("uint"); |
| 1314 | flag = 1; |
| 1315 | break; |
| 1316 | case LONG: |
| 1317 | str = allocAndCopy("long"); |
| 1318 | flag = 1; |
| 1319 | break; |
| 1320 | case ULONG: |
| 1321 | str = allocAndCopy("unsigned long"); |
| 1322 | flag = 1; |
| 1323 | break; |
| 1324 | case SIZE_T: |
| 1325 | str = allocAndCopy("size_t"); |
| 1326 | flag = 1; |
| 1327 | break; |
| 1328 | case SSIZE_T: |
| 1329 | str = allocAndCopy("ssize_t"); |
| 1330 | flag = 1; |
| 1331 | break; |
| 1332 | case OFF_T: |
| 1333 | str = allocAndCopy("off_t"); |
| 1334 | flag = 1; |
| 1335 | break; |
| 1336 | case FLOAT: |
| 1337 | str = floatOutputTypes[getSizeindex(type->size)]; |
| 1338 | break; |
| 1339 | case STRING: |
| 1340 | str = allocAndCopy("string"); |
| 1341 | flag = 1; |
| 1342 | break; |
| 1343 | case ENUM: |
| 1344 | //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]); |
| 1345 | str = allocAndCopy("enum"); |
| 1346 | flag = 1; |
| 1347 | break; |
| 1348 | case ARRAY: |
| 1349 | sprintf(buf,"%zu", type->size); |
| 1350 | str = appendString("array ",buf); |
| 1351 | flag = 1; |
| 1352 | break; |
| 1353 | case SEQUENCE: |
| 1354 | str = allocAndCopy("sequence "); |
| 1355 | flag = 1; |
| 1356 | break; |
| 1357 | case STRUCT: |
| 1358 | str = allocAndCopy("struct"); |
| 1359 | flag = 1; |
| 1360 | break; |
| 1361 | case UNION: |
| 1362 | str = allocAndCopy("union"); |
| 1363 | flag = 1; |
| 1364 | break; |
| 1365 | default: |
| 1366 | error_callback(NULL, "named type has no definition"); |
| 1367 | break; |
| 1368 | } |
| 1369 | |
| 1370 | crc = partial_crc32(str,crc); |
| 1371 | if(flag) free(str); |
| 1372 | |
| 1373 | if(type->fmt) crc = partial_crc32(type->fmt,crc); |
| 1374 | |
| 1375 | if(type->type == ARRAY){ |
| 1376 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
| 1377 | } else if(type->type ==SEQUENCE) { |
| 1378 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
| 1379 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type); |
| 1380 | } else if(type->type == STRUCT || type->type == UNION){ |
| 1381 | for(pos =0; pos < type->fields.position; pos++){ |
| 1382 | fld = (field_t *) type->fields.array[pos]; |
| 1383 | crc = partial_crc32(fld->name,crc); |
| 1384 | crc = getTypeChecksum(crc, fld->type); |
| 1385 | } |
| 1386 | }else if(type->type == ENUM){ |
| 1387 | for(pos = 0; pos < type->labels.position; pos++) |
| 1388 | crc = partial_crc32((char*)type->labels.array[pos],crc); |
| 1389 | } |
| 1390 | |
| 1391 | return crc; |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | /* Event type descriptors */ |
| 1396 | void freeType(type_descriptor_t * tp) |
| 1397 | { |
| 1398 | int pos2; |
| 1399 | field_t *f; |
| 1400 | |
| 1401 | if(tp->fmt != NULL) free(tp->fmt); |
| 1402 | if(tp->type == ENUM) { |
| 1403 | for(pos2 = 0; pos2 < tp->labels.position; pos2++) { |
| 1404 | free(tp->labels.array[pos2]); |
| 1405 | } |
| 1406 | sequence_dispose(&(tp->labels)); |
| 1407 | for(pos2 = 0; pos2 < tp->labels_values.position; pos2++) { |
| 1408 | free(tp->labels_values.array[pos2]); |
| 1409 | } |
| 1410 | sequence_dispose(&(tp->labels_values)); |
| 1411 | } |
| 1412 | if(tp->type == STRUCT) { |
| 1413 | for(pos2 = 0; pos2 < tp->fields.position; pos2++) { |
| 1414 | f = (field_t *) tp->fields.array[pos2]; |
| 1415 | free(f->name); |
| 1416 | free(f->description); |
| 1417 | free(f); |
| 1418 | } |
| 1419 | sequence_dispose(&(tp->fields)); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | void freeNamedType(table_t * t) |
| 1424 | { |
| 1425 | int pos; |
| 1426 | type_descriptor_t * td; |
| 1427 | |
| 1428 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1429 | free((char *)t->keys.array[pos]); |
| 1430 | td = (type_descriptor_t*)t->values.array[pos]; |
| 1431 | freeType(td); |
| 1432 | free(td); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | void freeTypes(sequence_t *t) |
| 1437 | { |
| 1438 | int pos; |
| 1439 | type_descriptor_t *tp; |
| 1440 | |
| 1441 | for(pos = 0 ; pos < t->position; pos++) { |
| 1442 | tp = (type_descriptor_t *)t->array[pos]; |
| 1443 | freeType(tp); |
| 1444 | free(tp); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | void freeEvents(sequence_t *t) |
| 1449 | { |
| 1450 | int pos; |
| 1451 | event_t *ev; |
| 1452 | |
| 1453 | for(pos = 0 ; pos < t->position; pos++) { |
| 1454 | ev = (event_t *) t->array[pos]; |
| 1455 | free(ev->name); |
| 1456 | free(ev->description); |
| 1457 | sequence_dispose(&ev->fields); |
| 1458 | free(ev); |
| 1459 | } |
| 1460 | |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | /* Extensible array */ |
| 1465 | |
| 1466 | void sequence_init(sequence_t *t) |
| 1467 | { |
| 1468 | t->size = 10; |
| 1469 | t->position = 0; |
| 1470 | t->array = (void **)memAlloc(t->size * sizeof(void *)); |
| 1471 | } |
| 1472 | |
| 1473 | void sequence_dispose(sequence_t *t) |
| 1474 | { |
| 1475 | t->size = 0; |
| 1476 | free(t->array); |
| 1477 | t->array = NULL; |
| 1478 | } |
| 1479 | |
| 1480 | void sequence_push(sequence_t *t, void *elem) |
| 1481 | { |
| 1482 | void **tmp; |
| 1483 | |
| 1484 | if(t->position >= t->size) { |
| 1485 | tmp = t->array; |
| 1486 | t->array = (void **)memAlloc(t->size * 2 * sizeof(void *)); |
| 1487 | memcpy(t->array, tmp, t->size * sizeof(void *)); |
| 1488 | t->size = t->size * 2; |
| 1489 | free(tmp); |
| 1490 | } |
| 1491 | t->array[t->position] = elem; |
| 1492 | t->position++; |
| 1493 | } |
| 1494 | |
| 1495 | void *sequence_pop(sequence_t *t) |
| 1496 | { |
| 1497 | return t->array[t->position--]; |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | /* Hash table API, implementation is just linear search for now */ |
| 1502 | |
| 1503 | void table_init(table_t *t) |
| 1504 | { |
| 1505 | sequence_init(&(t->keys)); |
| 1506 | sequence_init(&(t->values)); |
| 1507 | } |
| 1508 | |
| 1509 | void table_dispose(table_t *t) |
| 1510 | { |
| 1511 | sequence_dispose(&(t->keys)); |
| 1512 | sequence_dispose(&(t->values)); |
| 1513 | } |
| 1514 | |
| 1515 | void table_insert(table_t *t, char *key, void *value) |
| 1516 | { |
| 1517 | sequence_push(&(t->keys),key); |
| 1518 | sequence_push(&(t->values),value); |
| 1519 | } |
| 1520 | |
| 1521 | void *table_find(table_t *t, char *key) |
| 1522 | { |
| 1523 | int pos; |
| 1524 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1525 | if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0) |
| 1526 | return(t->values.array[pos]); |
| 1527 | } |
| 1528 | return NULL; |
| 1529 | } |
| 1530 | |
| 1531 | void table_insert_int(table_t *t, int *key, void *value) |
| 1532 | { |
| 1533 | sequence_push(&(t->keys),key); |
| 1534 | sequence_push(&(t->values),value); |
| 1535 | } |
| 1536 | |
| 1537 | void *table_find_int(table_t *t, int *key) |
| 1538 | { |
| 1539 | int pos; |
| 1540 | for(pos = 0 ; pos < t->keys.position; pos++) { |
| 1541 | if(*key == *(int *)t->keys.array[pos]) |
| 1542 | return(t->values.array[pos]); |
| 1543 | } |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | /* Concatenate strings */ |
| 1549 | |
| 1550 | char *appendString(char *s, char *suffix) |
| 1551 | { |
| 1552 | char *tmp; |
| 1553 | if(suffix == NULL) return s; |
| 1554 | |
| 1555 | tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1); |
| 1556 | strcpy(tmp,s); |
| 1557 | strcat(tmp,suffix); |
| 1558 | return tmp; |
| 1559 | } |