diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2022-03-08 21:52:57 -0500 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2022-03-08 21:52:57 -0500 |
commit | a75dd222709c92ae136d835ff2451391d5a88e45 (patch) | |
tree | 9bb631acafee493c6fc75335964b4774d023f2e1 /tools/scan_includes.c | |
parent | 7e78c1171d1206ca2f31cc67c55cc715eeb44ee5 (diff) |
Slightly refactor some C tools
Diffstat (limited to 'tools/scan_includes.c')
-rw-r--r-- | tools/scan_includes.c | 179 |
1 files changed, 71 insertions, 108 deletions
diff --git a/tools/scan_includes.c b/tools/scan_includes.c index 63af3bcf..e57ddc35 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -1,135 +1,98 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdbool.h> -#include <getopt.h> +#define PROGRAM_NAME "scan_includes" +#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm" -void usage(void) { - 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"); -} - -struct Options { - bool help; - bool strict; -}; +#include "common.h" -struct Options Options = {0}; +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': + usage_exit(0); + break; + default: + usage_exit(1); + } + } +} -void scan_file(char* filename) { +void scan_file(const char *filename, bool strict) { + errno = 0; FILE *f = fopen(filename, "rb"); if (!f) { - if (Options.strict) { - fprintf(stderr, "Could not open file: '%s'\n", filename); - exit(1); + if (strict) { + error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno)); } else { return; } } - fseek(f, 0, SEEK_END); - long size = ftell(f); - rewind(f); - - char *buffer = malloc(size + 1); - char *orig = buffer; - size = fread(buffer, 1, size, f); - buffer[size] = '\0'; + long size = xfsize(filename, f); + char *contents = xmalloc(size + 1); + xfread((uint8_t *)contents, size, filename, f); fclose(f); + contents[size] = '\0'; - for (; buffer && (buffer - orig < size); buffer++) { - bool is_include = false; - bool is_incbin = false; - switch (*buffer) { - case ';': - buffer = strchr(buffer, '\n'); - if (!buffer) { - fprintf(stderr, "%s: no newline at end of file\n", filename); - break; - } - break; - - case '"': - buffer++; - buffer = strchr(buffer, '"'); - if (!buffer) { - fprintf(stderr, "%s: unterminated string\n", filename); - break; - } - buffer++; - break; - - 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; - } - buffer++; - int length = strcspn(buffer, "\""); - char *include = malloc(length + 1); - strncpy(include, buffer, length); - include[length] = '\0'; - printf("%s ", include); + 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); + } + break; + case '"': + ptr++; + ptr = strchr(ptr, '"'); + if (ptr) { + ptr++; + } else { + fprintf(stderr, "%s: unterminated string\n", filename); + } + 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) { + 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); + scan_file(include_path, strict); } - free(include); - buffer = strchr(buffer, '"'); } - break; - - } - if (!buffer) { + } break; } - } - free(orig); + free(contents); } -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) { - switch (opt) { - case 's': - Options.strict = true; - break; - case 'h': - Options.help = true; - break; - default: - usage(); - exit(1); - break; - } - } +int main(int argc, char *argv[]) { + bool strict = false; + parse_args(argc, argv, &strict); + argc -= optind; argv += optind; - if (Options.help) { - usage(); - return 0; - } if (argc < 1) { - usage(); - exit(1); + usage_exit(1); } - scan_file(argv[0]); + + scan_file(argv[0], strict); return 0; } |