Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/tools/scan_includes.c
1270 views
1
#define PROGRAM_NAME "scan_includes"
2
#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm"
3
4
#include "common.h"
5
6
#include <ctype.h>
7
8
void parse_args(int argc, char *argv[], bool *strict) {
9
struct option long_options[] = {
10
{"strict", no_argument, 0, 's'},
11
{"help", no_argument, 0, 'h'},
12
{0}
13
};
14
for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) {
15
switch (opt) {
16
case 's':
17
*strict = true;
18
break;
19
case 'h':
20
usage_exit(0);
21
break;
22
default:
23
usage_exit(1);
24
}
25
}
26
}
27
28
void scan_file(const char *filename, bool strict) {
29
errno = 0;
30
FILE *f = fopen(filename, "rb");
31
if (!f) {
32
if (strict) {
33
error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
34
} else {
35
return;
36
}
37
}
38
39
long size = xfsize(filename, f);
40
char *contents = xmalloc(size + 1);
41
xfread((uint8_t *)contents, size, filename, f);
42
fclose(f);
43
contents[size] = '\0';
44
45
for (char *ptr = contents; ptr && ptr < contents + size; ptr++) {
46
ptr = strpbrk(ptr, ";\"Ii");
47
if (!ptr) {
48
break;
49
}
50
switch (*ptr) {
51
case ';':
52
// Skip comments until the end of the line
53
ptr += strcspn(ptr + 1, "\r\n");
54
if (*ptr) {
55
ptr++;
56
}
57
break;
58
59
case '"':
60
// Skip string literal until the closing quote
61
ptr += strcspn(ptr + 1, "\"");
62
if (*ptr) {
63
ptr++;
64
}
65
break;
66
67
case 'I':
68
case 'i':
69
/* empty statement between the label and the variable declaration */;
70
// Check that an INCLUDE/INCBIN starts as its own token
71
char before = ptr > contents ? *(ptr - 1) : '\n';
72
if (!isspace((unsigned)before) && before != ':') {
73
break;
74
}
75
bool is_incbin = !strncmp(ptr, "INCBIN", 6) || !strncmp(ptr, "incbin", 6);
76
bool is_include = !strncmp(ptr, "INCLUDE", 7) || !strncmp(ptr, "include", 7);
77
if (is_incbin || is_include) {
78
// Check that an INCLUDE/INCBIN ends as its own token
79
ptr += is_include ? 7 : 6;
80
if (!isspace((unsigned)*ptr) && *ptr != '"') {
81
break;
82
}
83
ptr += strspn(ptr, " \t");
84
if (*ptr == '"') {
85
// Print the file path and recursively scan INCLUDEs
86
ptr++;
87
char *include_path = ptr;
88
size_t length = strcspn(ptr, "\"");
89
ptr += length + 1;
90
include_path[length] = '\0';
91
printf("%s ", include_path);
92
if (is_include) {
93
scan_file(include_path, strict);
94
}
95
} else {
96
fprintf(stderr, "%s: no file path after INC%s\n", filename, is_include ? "LUDE" : "BIN");
97
// Continue to process a comment
98
if (*ptr == ';') {
99
ptr--;
100
}
101
}
102
}
103
break;
104
}
105
}
106
107
free(contents);
108
}
109
110
int main(int argc, char *argv[]) {
111
bool strict = false;
112
parse_args(argc, argv, &strict);
113
114
argc -= optind;
115
argv += optind;
116
if (argc < 1) {
117
usage_exit(1);
118
}
119
120
scan_file(argv[0], strict);
121
return 0;
122
}
123
124