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