2021-09-21 21:37:43 +00:00
|
|
|
#define PROGRAM_NAME "scan_includes"
|
|
|
|
#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm"
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-21 21:37:43 +00:00
|
|
|
#include "common.h"
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 21:15:50 +00:00
|
|
|
void parse_args(int argc, char *argv[], bool *strict) {
|
|
|
|
struct option long_options[] = {
|
|
|
|
{"strict", no_argument, 0, 's'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) {
|
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
*strict = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(0);
|
2021-09-02 21:15:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(1);
|
2021-09-02 21:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 05:21:15 +00:00
|
|
|
void scan_file(const char *filename, bool strict) {
|
|
|
|
errno = 0;
|
2017-08-20 16:32:15 +00:00
|
|
|
FILE *f = fopen(filename, "rb");
|
2017-05-28 05:12:16 +00:00
|
|
|
if (!f) {
|
2021-09-02 05:21:15 +00:00
|
|
|
if (strict) {
|
2021-09-02 07:04:40 +00:00
|
|
|
error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
|
2017-05-28 05:12:16 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 02:39:13 +00:00
|
|
|
long size = xfsize(filename, f);
|
|
|
|
char *contents = xmalloc(size + 1);
|
|
|
|
xfread((uint8_t *)contents, size, filename, f);
|
2016-08-25 01:56:07 +00:00
|
|
|
fclose(f);
|
2021-09-02 05:21:15 +00:00
|
|
|
contents[size] = '\0';
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 05:21:15 +00:00
|
|
|
for (char *ptr = contents; ptr && ptr - contents < size; ptr++) {
|
|
|
|
bool is_incbin = false, is_include = false;
|
|
|
|
switch (*ptr) {
|
|
|
|
case ';':
|
|
|
|
ptr = strchr(ptr, '\n');
|
|
|
|
if (!ptr) {
|
|
|
|
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
2017-04-28 05:41:06 +00:00
|
|
|
break;
|
2021-09-02 05:21:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
ptr++;
|
|
|
|
ptr = strchr(ptr, '"');
|
|
|
|
if (!ptr) {
|
|
|
|
fprintf(stderr, "%s: unterminated string\n", filename);
|
2018-03-10 23:17:05 +00:00
|
|
|
break;
|
2021-09-02 05:21:15 +00:00
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
case 'i':
|
|
|
|
is_incbin = !strncmp(ptr, "INCBIN", 6) || !strncmp(ptr, "incbin", 6);
|
|
|
|
is_include = !strncmp(ptr, "INCLUDE", 7) || !strncmp(ptr, "include", 7);
|
|
|
|
if (is_incbin || is_include) {
|
|
|
|
ptr = strchr(ptr, '"');
|
|
|
|
if (!ptr) {
|
|
|
|
break;
|
2017-08-20 16:32:15 +00:00
|
|
|
}
|
2021-09-02 05:21:15 +00:00
|
|
|
ptr++;
|
|
|
|
char *include_path = ptr;
|
|
|
|
size_t length = strcspn(ptr, "\"");
|
|
|
|
ptr += length + 1;
|
|
|
|
include_path[length] = '\0';
|
|
|
|
printf("%s ", include_path);
|
|
|
|
if (is_include) {
|
|
|
|
scan_file(include_path, strict);
|
2017-08-20 16:32:15 +00:00
|
|
|
}
|
2021-09-02 05:21:15 +00:00
|
|
|
}
|
2017-08-20 16:32:15 +00:00
|
|
|
break;
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 05:21:15 +00:00
|
|
|
free(contents);
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 05:21:15 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
bool strict = false;
|
|
|
|
parse_args(argc, argv, &strict);
|
|
|
|
|
2017-05-28 05:12:16 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 1) {
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(1);
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2021-09-02 05:21:15 +00:00
|
|
|
|
|
|
|
scan_file(argv[0], strict);
|
2016-08-25 01:56:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|