1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <set>
#include <string>
#ifdef _MSC_VER
#define FATAL_INPUT_ERROR(format, ...) \
do { \
fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, __VA_ARGS__); \
exit(1); \
} while (0)
#define FATAL_ERROR(format, ...) \
do { \
fprintf(stderr, format, __VA_ARGS__); \
exit(1); \
} while (0)
#else
#define FATAL_INPUT_ERROR(format, ...) \
do { \
fprintf(stderr, "%s:%d " format, m_path.c_str(), m_lineNum, ##__VA_ARGS__); \
exit(1); \
} while (0)
#define FATAL_ERROR(format, ...) \
do { \
fprintf(stderr, format, ##__VA_ARGS__); \
exit(1); \
} while (0)
#endif // _MSC_VER
#define SCANINC_MAX_PATH 255
enum class IncDirectiveType {
None,
Include,
Incbin
};
class AsmFile
{
public:
AsmFile(std::string path);
~AsmFile();
IncDirectiveType ReadUntilIncDirective(std::string &path);
private:
char *m_buffer;
int m_pos;
int m_size;
int m_lineNum;
std::string m_path;
int GetChar()
{
if (m_pos >= m_size)
return -1;
int c = m_buffer[m_pos++];
if (c == '\r') {
if (m_pos < m_size && m_buffer[m_pos++] == '\n') {
m_lineNum++;
return '\n';
} else {
FATAL_INPUT_ERROR("CR line endings are not supported\n");
}
}
if (c == '\n')
m_lineNum++;
return c;
}
// No newline translation because it's not needed for any use of this function.
int PeekChar()
{
if (m_pos >= m_size)
return -1;
return m_buffer[m_pos];
}
void SkipTabsAndSpaces()
{
while (m_pos < m_size && (m_buffer[m_pos] == '\t' || m_buffer[m_pos] == ' '))
m_pos++;
}
bool MatchIncDirective(std::string directiveName, std::string &path)
{
int length = directiveName.length();
int i;
for (i = 0; i < length && m_pos + i < m_size; i++)
if (directiveName[i] != m_buffer[m_pos + i])
return false;
if (i < length)
return false;
m_pos += length;
SkipTabsAndSpaces();
if (GetChar() != '"')
FATAL_INPUT_ERROR("no path after \".%s\" directive\n", directiveName.c_str());
path = ReadPath();
return true;
}
std::string ReadPath();
void SkipEndOfLineComment();
void SkipMultiLineComment();
void SkipString();
};
AsmFile::AsmFile(std::string path)
{
m_path = path;
FILE *fp = fopen(path.c_str(), "rb");
if (fp == NULL)
FATAL_ERROR("Failed to open \"%s\" for reading.\n", path.c_str());
fseek(fp, 0, SEEK_END);
m_size = ftell(fp);
m_buffer = new char[m_size];
rewind(fp);
if (fread(m_buffer, m_size, 1, fp) != 1)
FATAL_ERROR("Failed to read \"%s\".\n", path.c_str());
fclose(fp);
m_pos = 0;
m_lineNum = 1;
}
AsmFile::~AsmFile()
{
delete[] m_buffer;
}
IncDirectiveType AsmFile::ReadUntilIncDirective(std::string &path)
{
// At the beginning of each loop iteration, the current file position
// should be at the start of a line or at the end of the file.
for (;;) {
SkipTabsAndSpaces();
IncDirectiveType incDirectiveType = IncDirectiveType::None;
if (PeekChar() == '.') {
m_pos++;
if (MatchIncDirective("incbin", path))
incDirectiveType = IncDirectiveType::Incbin;
else if (MatchIncDirective("include", path))
incDirectiveType = IncDirectiveType::Include;
}
for (;;) {
int c = GetChar();
if (c == -1)
return incDirectiveType;
if (c == ';') {
SkipEndOfLineComment();
break;
} else if (c == '/' && PeekChar() == '*') {
m_pos++;
SkipMultiLineComment();
} else if (c == '"') {
SkipString();
} else if (c == '\n') {
break;
}
}
if (incDirectiveType != IncDirectiveType::None)
return incDirectiveType;
}
}
std::string AsmFile::ReadPath()
{
int length = 0;
int startPos = m_pos;
for (;;) {
int c = GetChar();
if (c == '"')
break;
if (c == -1)
FATAL_INPUT_ERROR("unexpected EOF in include string\n");
if (c == 0)
FATAL_INPUT_ERROR("unexpected NUL character in include string\n");
if (c == '\n')
FATAL_INPUT_ERROR("unexpected end of line character in include string\n");
if (c == '\\') {
c = GetChar();
if (c != '"')
FATAL_INPUT_ERROR("unknown escape \"\\%c\" in include string\n", c);
}
length++;
if (length > SCANINC_MAX_PATH)
FATAL_INPUT_ERROR("path is too long");
}
return std::string(m_buffer, startPos, length);
}
void AsmFile::SkipEndOfLineComment()
{
int c;
do {
c = GetChar();
} while (c != -1 && c != '\n');
}
void AsmFile::SkipMultiLineComment()
{
for (;;) {
int c = GetChar();
if (c == '*') {
if (PeekChar() == '/') {
m_pos++;
return;
}
} else if (c == -1) {
return;
}
}
}
void AsmFile::SkipString()
{
for (;;) {
int c = GetChar();
if (c == '"')
break;
if (c == -1)
FATAL_INPUT_ERROR("unexpected EOF in string\n");
if (c == '\\') {
c = GetChar();
}
}
}
int main(int argc, char **argv)
{
if (argc < 2)
FATAL_ERROR("Usage: scaninc ASM_FILE_PATH\n");
std::stack<std::string> filesToProcess;
std::set<std::string> dependencies;
filesToProcess.push(std::string(argv[1]));
while (!filesToProcess.empty()) {
AsmFile file(filesToProcess.top());
filesToProcess.pop();
IncDirectiveType incDirectiveType;
std::string path;
while ((incDirectiveType = file.ReadUntilIncDirective(path)) != IncDirectiveType::None) {
bool inserted = dependencies.insert(path).second;
if (inserted && incDirectiveType == IncDirectiveType::Include)
filesToProcess.push(path);
}
}
for (const std::string &path : dependencies) {
printf("%s\n", path.c_str());
}
}
|