diff --git a/tools/scan_includes.c b/tools/scan_includes.c index 538d9d77..571b2b93 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -1,31 +1,77 @@ #define PROGRAM_NAME "scan_includes" -#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm" +#define USAGE_OPTS "[-h|--help] [-s|--strict] [-I|--include path] filename.asm" #include "common.h" #include -void parse_args(int argc, char *argv[], bool *strict) { +struct Options { + bool strict; + char **include_paths; + int include_paths_len; +}; + +struct Options options = {0}; + +void add_include_path(const char *path) { + size_t arg_len = strlen(path); + bool has_trailing_separator = path[arg_len - 1] == '/'; + char *include_path = xmalloc(arg_len + (has_trailing_separator ? 1 : 2)); + sprintf(include_path, "%s%s", path, has_trailing_separator ? "" : "/"); + options.include_paths = xrealloc(options.include_paths, sizeof(options.include_paths[0]) * ++options.include_paths_len); + options.include_paths[options.include_paths_len - 1] = include_path; +} + +void parse_args(int argc, char *argv[]) { struct option long_options[] = { {"strict", no_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, + {"include", required_argument, 0, 'I'}, {0} }; - for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) { + for (int opt; (opt = getopt_long(argc, argv, "shI:", long_options)) != -1;) { switch (opt) { case 's': - *strict = true; + options.strict = true; break; case 'h': usage_exit(0); break; + case 'I': + add_include_path(optarg); + break; default: usage_exit(1); } } } -void scan_file(const char *filename, bool strict, bool top_level) { +char *check_include_paths_for_file(const char *filepath, bool is_incbin) { + char *scratch_path = NULL; + for (char **include_path = options.include_paths; include_path < options.include_paths + options.include_paths_len; include_path++) { + size_t len = strlen(*include_path) + strlen(filepath) + 1; + scratch_path = xmalloc(len); + snprintf(scratch_path, len, "%s%s", *include_path, filepath); + FILE *f = fopen(scratch_path, "r"); + + // If the file exists, use this include path + if (f) { + fclose(f); + break; + } + + // If the INCBIN doesn't exist and there is exactly 1 include path, assume it will get created there + if (is_incbin && options.include_paths_len == 1) { + break; + } + + free(scratch_path); + scratch_path = NULL; + } + return scratch_path; +} + +void scan_file(const char *filename, bool top_level) { long size = 0; char *contents = NULL; if (top_level && !strcmp(filename, "-")) { @@ -36,7 +82,7 @@ void scan_file(const char *filename, bool strict, bool top_level) { errno = 0; FILE *f = fopen(filename, "rb"); if (!f) { - if (strict) { + if (options.strict) { error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno)); } else { return; @@ -90,16 +136,32 @@ void scan_file(const char *filename, bool strict, bool top_level) { } ptr += strspn(ptr, " \t"); if (*ptr == '"') { - // Print the file path and recursively scan INCLUDEs ptr++; - char *include_path = ptr; + char *include_filepath = 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, false); + include_filepath[length] = '\0'; + + // Check if the file exists, and if not then check options.include_paths for the file + FILE *f = fopen(include_filepath, "r"); + char *scratch_path = NULL; + if (!f) { + scratch_path = check_include_paths_for_file(include_filepath, is_incbin); + } else { + fclose(f); } + + if (scratch_path) { + include_filepath = scratch_path; + } + + // Print the file path and recursively scan INCLUDEs + printf("%s ", include_filepath); + if (is_include) { + scan_file(include_filepath, false); + } + + free(scratch_path); } else { fprintf(stderr, "%s: no file path after INC%s\n", filename, is_include ? "LUDE" : "BIN"); // Continue to process a comment @@ -116,8 +178,7 @@ void scan_file(const char *filename, bool strict, bool top_level) { } int main(int argc, char *argv[]) { - bool strict = false; - parse_args(argc, argv, &strict); + parse_args(argc, argv); argc -= optind; argv += optind; @@ -125,6 +186,6 @@ int main(int argc, char *argv[]) { usage_exit(1); } - scan_file(argv[0], strict, true); + scan_file(argv[0], true); return 0; }