* Specialized parser for targets. */
| 325 | * Specialized parser for targets. |
| 326 | */ |
| 327 | void parseTargets(const char *filename, const Instr *Is, size_t size, |
| 328 | Targets &targets) |
| 329 | { |
| 330 | FILE *stream = fopen(filename, "r"); |
| 331 | if (stream == nullptr) |
| 332 | error("failed to open CSV file \"%s\" for reading: %s", |
| 333 | filename, strerror(errno)); |
| 334 | |
| 335 | Record record; |
| 336 | CSV csv = {stream, filename, -1, 0}; |
| 337 | for (size_t i = 0; ; i++) |
| 338 | { |
| 339 | if (!parseRecord(csv, record)) |
| 340 | break; |
| 341 | if (record.size() < 1) |
| 342 | error("failed to parse CSV file \"%s\" at line %u; record with " |
| 343 | "empty length", csv.filename, csv.lineno); |
| 344 | MatchVal &addr = record[0]; |
| 345 | if (addr.type != MATCH_TYPE_INTEGER) |
| 346 | { |
| 347 | record.clear(); |
| 348 | if (i == 0) |
| 349 | continue; // Skip presumed header |
| 350 | error("failed to parse CSV file \"%s\" at line %u; first record " |
| 351 | "entry must be an address", csv.filename, csv.lineno); |
| 352 | } |
| 353 | TargetKind kind = 0; |
| 354 | const TargetKind kinds[3] = {TARGET_DIRECT, TARGET_INDIRECT, |
| 355 | TARGET_FUNCTION}; |
| 356 | const char *counts[3] = {"second", "third", "fourth"}; |
| 357 | for (size_t i = 0; i < 3; i++) |
| 358 | if (record.size() >= 2) |
| 359 | { |
| 360 | if (i+1 >= record.size()) |
| 361 | break; |
| 362 | MatchVal &func = record[i+1]; |
| 363 | if (func.type != MATCH_TYPE_INTEGER) |
| 364 | error("failed to parse CSV file \"%s\" at line %u; %s " |
| 365 | "record entry must be Boolean value (0 or 1)", |
| 366 | counts[i], csv.filename, csv.lineno); |
| 367 | kind |= (func.i != 0? kinds[i]: 0); |
| 368 | } |
| 369 | record.clear(); |
| 370 | if (kind == 0 || findInstr(Is, size, addr.i) < 0) |
| 371 | continue; |
| 372 | auto r = targets.insert({addr.i, kind}); |
| 373 | if (!r.second) |
| 374 | error("failed to parse CSV file \"%s\" at line %u; duplicate " |
| 375 | "record with address 0x%lx", csv.filename, csv.lineno, |
| 376 | addr); |
| 377 | } |
| 378 | fclose(stream); |
| 379 | } |
| 380 |