annotate bootmatch/comp_ranges.c @ 10:39b921d851fb

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