FreeCalypso > hg > fc-am-toolkit
comparison bootmatch/comp_ranges.c @ 9:bfcc8180cf3c
bootmatch compiler written
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 10 Jun 2023 02:55:29 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 8:304ac8119c8a | 9:bfcc8180cf3c |
|---|---|
| 1 /* | |
| 2 * Bootmatch compiler: here we read and parse the *.ranges file. | |
| 3 */ | |
| 4 | |
| 5 #include <sys/types.h> | |
| 6 #include <ctype.h> | |
| 7 #include <stdio.h> | |
| 8 #include <stdlib.h> | |
| 9 #include "comp_defs.h" | |
| 10 | |
| 11 extern struct range range_list[MAX_RANGES]; | |
| 12 extern unsigned range_count; | |
| 13 | |
| 14 static void | |
| 15 process_line(line, filename, lineno) | |
| 16 char *line, *filename; | |
| 17 { | |
| 18 char *cp; | |
| 19 struct range *rp; | |
| 20 u_long hex_start, hex_end; | |
| 21 | |
| 22 for (cp = line; isspace(*cp); cp++) | |
| 23 ; | |
| 24 if (*cp == '\0' || *cp == '#') | |
| 25 return; | |
| 26 if (!isxdigit(*cp)) { | |
| 27 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n", | |
| 28 filename, lineno); | |
| 29 exit(1); | |
| 30 } | |
| 31 hex_start = strtoul(cp, &cp, 16); | |
| 32 if (!isspace(*cp)) | |
| 33 goto inv_syntax; | |
| 34 while (isspace(*cp)) | |
| 35 cp++; | |
| 36 if (!isxdigit(*cp)) | |
| 37 goto inv_syntax; | |
| 38 hex_end = strtoul(cp, &cp, 16); | |
| 39 if (*cp && !isspace(*cp)) | |
| 40 goto inv_syntax; | |
| 41 while (isspace(*cp)) | |
| 42 cp++; | |
| 43 if (*cp && *cp != '#') | |
| 44 goto inv_syntax; | |
| 45 if (hex_start >= BOOT_BLOCK_SIZE) { | |
| 46 fprintf(stderr, "%s line %d: start offset is out of range\n", | |
| 47 filename, lineno); | |
| 48 exit(1); | |
| 49 } | |
| 50 if (hex_end >= BOOT_BLOCK_SIZE) { | |
| 51 fprintf(stderr, "%s line %d: end offset is out of range\n", | |
| 52 filename, lineno); | |
| 53 exit(1); | |
| 54 } | |
| 55 if (hex_start >= hex_end) { | |
| 56 fprintf(stderr, | |
| 57 "%s line %d: end offset must be greater than start offset\n", | |
| 58 filename, lineno); | |
| 59 exit(1); | |
| 60 } | |
| 61 if (range_count >= MAX_RANGES) { | |
| 62 fprintf(stderr, "%s line %d: too many ranges defined\n", | |
| 63 filename, lineno); | |
| 64 exit(1); | |
| 65 } | |
| 66 rp = range_list + range_count; | |
| 67 rp->offset = hex_start; | |
| 68 rp->nbytes = hex_end - hex_start; | |
| 69 range_count++; | |
| 70 } | |
| 71 | |
| 72 void | |
| 73 read_ranges_file(filename) | |
| 74 char *filename; | |
| 75 { | |
| 76 FILE *inf; | |
| 77 char linebuf[256]; | |
| 78 int lineno; | |
| 79 | |
| 80 inf = fopen(filename, "r"); | |
| 81 if (!inf) { | |
| 82 perror(filename); | |
| 83 exit(1); | |
| 84 } | |
| 85 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) | |
| 86 process_line(linebuf, filename, lineno); | |
| 87 fclose(inf); | |
| 88 if (!range_count) { | |
| 89 fprintf(stderr, "error: no ranges defined in %s\n", filename); | |
| 90 exit(1); | |
| 91 } | |
| 92 } |
