2016-08-24 18:56:07 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-04-27 22:41:06 -07:00
|
|
|
#include <stdbool.h>
|
2017-05-27 22:12:16 -07:00
|
|
|
#include <getopt.h>
|
2016-08-24 18:56:07 -07:00
|
|
|
|
|
|
|
void usage(void) {
|
2017-05-27 22:12:16 -07:00
|
|
|
printf("Usage: scan_includes [-h] [-s] filename\n"
|
|
|
|
"-h, --help\n"
|
|
|
|
" Print usage and exit\n"
|
|
|
|
"-s, --strict\n"
|
|
|
|
" Fail if a file cannot be read\n");
|
2016-08-24 18:56:07 -07:00
|
|
|
}
|
|
|
|
|
2017-05-27 22:12:16 -07:00
|
|
|
struct Options {
|
|
|
|
bool help;
|
|
|
|
bool strict;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Options Options = {0};
|
|
|
|
|
2016-08-24 18:56:07 -07:00
|
|
|
void scan_file(char* filename) {
|
2017-08-20 09:32:15 -07:00
|
|
|
FILE *f = fopen(filename, "rb");
|
2017-05-27 22:12:16 -07:00
|
|
|
if (!f) {
|
|
|
|
if (Options.strict) {
|
|
|
|
fprintf(stderr, "Could not open file: '%s'\n", filename);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-24 18:56:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
2017-08-20 09:32:15 -07:00
|
|
|
long size = ftell(f);
|
2016-08-24 18:56:07 -07:00
|
|
|
rewind(f);
|
|
|
|
|
2017-08-20 09:32:15 -07:00
|
|
|
char *buffer = malloc(size + 1);
|
|
|
|
char *orig = buffer;
|
|
|
|
size = fread(buffer, 1, size, f);
|
2016-08-24 18:56:07 -07:00
|
|
|
buffer[size] = '\0';
|
|
|
|
fclose(f);
|
|
|
|
|
2017-04-27 22:41:06 -07:00
|
|
|
for (; buffer && (buffer - orig < size); buffer++) {
|
|
|
|
bool is_include = false;
|
|
|
|
bool is_incbin = false;
|
2017-08-20 09:32:15 -07:00
|
|
|
switch (*buffer) {
|
|
|
|
case ';':
|
|
|
|
buffer = strchr(buffer, '\n');
|
|
|
|
if (!buffer) {
|
|
|
|
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
|
|
|
}
|
2017-04-27 22:41:06 -07:00
|
|
|
break;
|
2017-08-20 09:32:15 -07:00
|
|
|
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
|
|
|
|
is_incbin = true;
|
|
|
|
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
|
|
|
|
is_include = true;
|
|
|
|
}
|
|
|
|
if (is_incbin || is_include) {
|
|
|
|
buffer = strchr(buffer, '"');
|
|
|
|
if (!buffer++) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int length = strcspn(buffer, "\"");
|
|
|
|
char *include = malloc(length + 1);
|
|
|
|
strncpy(include, buffer, length);
|
|
|
|
include[length] = '\0';
|
|
|
|
printf("%s ", include);
|
|
|
|
if (is_include) {
|
|
|
|
scan_file(include);
|
|
|
|
}
|
|
|
|
free(include);
|
|
|
|
buffer = strchr(buffer, '"');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!buffer) {
|
|
|
|
break;
|
2016-08-24 18:56:07 -07:00
|
|
|
}
|
2017-08-20 09:32:15 -07:00
|
|
|
|
2016-08-24 18:56:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
free(orig);
|
|
|
|
}
|
|
|
|
|
2017-08-20 09:32:15 -07:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
int i = 0;
|
|
|
|
struct option long_options[] = {
|
|
|
|
{"strict", no_argument, 0, 's'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
int opt = -1;
|
|
|
|
while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
|
2017-05-27 22:12:16 -07:00
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
Options.strict = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
Options.help = true;
|
|
|
|
break;
|
2017-05-28 20:22:10 -07:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
break;
|
2017-05-27 22:12:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (Options.help) {
|
2016-08-24 18:56:07 -07:00
|
|
|
usage();
|
2017-05-27 22:12:16 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (argc < 1) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
2016-08-24 18:56:07 -07:00
|
|
|
}
|
2017-05-27 22:12:16 -07:00
|
|
|
scan_file(argv[0]);
|
2016-08-24 18:56:07 -07:00
|
|
|
return 0;
|
|
|
|
}
|