diff options
author | yenatch <yenatch@gmail.com> | 2016-08-24 21:56:07 -0400 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2016-08-24 21:56:07 -0400 |
commit | 2ab468268aa156ae60e0e60a1319552342f692f6 (patch) | |
tree | 42a20f4202f97ed9ab132f6b0b11d0439d0f42fe /tools/scan_includes.c | |
parent | 604778749d26223a21df776c7df6a017e80cead9 (diff) |
Add C build tools.
Diffstat (limited to 'tools/scan_includes.c')
-rw-r--r-- | tools/scan_includes.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tools/scan_includes.c b/tools/scan_includes.c new file mode 100644 index 000000000..cc6eeb1be --- /dev/null +++ b/tools/scan_includes.c @@ -0,0 +1,85 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void usage(void) { + printf("Usage: scan_includes filename\n"); + exit(1); +} + +void scan_file(char* filename) { + FILE* f; + long size; + char* orig; + char* buffer; + char* include; + int length; + + f = fopen(filename, "r"); + if (f == NULL) { + fprintf(stderr, "Could not open file: '%s'\n", filename); + exit(1); + } + + fseek(f, 0, SEEK_END); + size = ftell(f); + rewind(f); + + //fprintf(stderr, "malloc: %s\n", filename); + buffer = malloc(size + 1); + orig = buffer; + fread(buffer, 1, size, f); + buffer[size] = '\0'; + fclose(f); + //fprintf(stderr, "read: %s\n", filename); + + for (; (buffer != NULL) && (buffer != 0) && (buffer - orig < size); buffer++) { + //fprintf(stderr, "%c", buffer[0]); + if (buffer[0] == ';') { + buffer = strchr(buffer, '\n'); + if (buffer == NULL) { + fprintf(stderr, "%s: no newline at end of file\n", filename); + break; + } + continue; + } + if ( + (strncmp(buffer, "INCBIN", 6) == 0) || + (strncmp(buffer, "incbin", 6) == 0) + ) { + buffer = strchr(buffer, '"') + 1; + if (buffer == NULL) break; + length = strcspn(buffer, "\""); + include = malloc(length + 1); + strncpy(include, buffer, length); + include[length] = '\0'; + printf("%s ", include); + free(include); + } else if ( + (strncmp(buffer, "INCLUDE", 7) == 0) || + (strncmp(buffer, "include", 7) == 0) + ) { + buffer = strchr(buffer, '"') + 1; + if (buffer == NULL) break; + length = strcspn(buffer, "\""); + include = malloc(length + 1); + strncpy(include, buffer, length); + include[length] = '\0'; + printf("%s ", include); + scan_file(include); + free(include); + } + } + + //fprintf(stderr, "free: %s\n", filename); + free(orig); + //fprintf(stderr, "freed: %s\n", filename); +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + usage(); + } + scan_file(argv[1]); + return 0; +} |