Rewrite tools/scan_includes.c to use common.h and factor out a parse_args function

This commit is contained in:
Rangi 2021-09-02 01:21:15 -04:00
parent 313deab552
commit 0d1a029e81
4 changed files with 74 additions and 103 deletions

View File

@ -12,9 +12,10 @@
#include <getopt.h> #include <getopt.h>
int getopt_long_index; int getopt_long_index;
#define getopt_long(c, v, s, l) getopt_long(c, v, s, l, &getopt_long_index) #define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index)
void *malloc_verbose(size_t size) { void *malloc_verbose(size_t size) {
errno = 0;
void *m = malloc(size); void *m = malloc(size);
if (!m) { if (!m) {
fprintf(stderr, "Could not allocate %zu bytes: %s\n", size, strerror(errno)); fprintf(stderr, "Could not allocate %zu bytes: %s\n", size, strerror(errno));
@ -25,6 +26,7 @@ void *malloc_verbose(size_t size) {
FILE *fopen_verbose(const char *filename, char rw) { FILE *fopen_verbose(const char *filename, char rw) {
char mode[3] = {rw, 'b', '\0'}; char mode[3] = {rw, 'b', '\0'};
errno = 0;
FILE *f = fopen(filename, mode); FILE *f = fopen(filename, mode);
if (!f) { if (!f) {
fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno)); fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
@ -34,6 +36,7 @@ FILE *fopen_verbose(const char *filename, char rw) {
} }
void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) { void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) {
errno = 0;
if (fread(data, 1, size, f) != size) { if (fread(data, 1, size, f) != size) {
fprintf(stderr, "Could not read from file \"%s\": %s\n", filename, strerror(errno)); fprintf(stderr, "Could not read from file \"%s\": %s\n", filename, strerror(errno));
fclose(f); fclose(f);
@ -42,6 +45,7 @@ void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) {
} }
void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) { void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) {
errno = 0;
if (fwrite(data, 1, size, f) != size) { if (fwrite(data, 1, size, f) != size) {
fprintf(stderr, "Could not write to file \"%s\": %s\n", filename, strerror(errno)); fprintf(stderr, "Could not write to file \"%s\": %s\n", filename, strerror(errno));
fclose(f); fclose(f);
@ -49,15 +53,16 @@ void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE
} }
} }
long file_size(const char *filename, FILE *f) { long file_size_verbose(const char *filename, FILE *f) {
long size = 0; long size = -1;
errno = 0;
if (!fseek(f, 0, SEEK_END)) { if (!fseek(f, 0, SEEK_END)) {
size = ftell(f); size = ftell(f);
if (size != -1) { if (size != -1) {
rewind(f); rewind(f);
} }
} }
if (errno) { if (size == -1) {
fprintf(stderr, "Could not measure file \"%s\": %s\n", filename, strerror(errno)); fprintf(stderr, "Could not measure file \"%s\": %s\n", filename, strerror(errno));
exit(1); exit(1);
} }
@ -66,7 +71,7 @@ long file_size(const char *filename, FILE *f) {
uint8_t *read_u8(const char *filename, long *size) { uint8_t *read_u8(const char *filename, long *size) {
FILE *f = fopen_verbose(filename, 'r'); FILE *f = fopen_verbose(filename, 'r');
*size = file_size(filename, f); *size = file_size_verbose(filename, f);
uint8_t *data = malloc_verbose(*size); uint8_t *data = malloc_verbose(*size);
fread_verbose(data, *size, filename, f); fread_verbose(data, *size, filename, f);
fclose(f); fclose(f);

View File

@ -1,7 +1,7 @@
#include "common.h" #include "common.h"
void usage() { void usage() {
fprintf(stderr, "Usage: png_dimensions in.png out.dimensions\n"); fputs("Usage: png_dimensions in.png out.dimensions\n", stderr);
} }
uint8_t read_dimensions(const char *filename) { uint8_t read_dimensions(const char *filename) {
@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
usage(); usage();
exit(1); exit(1);
} }
uint8_t output_byte = read_dimensions(argv[1]); uint8_t output_byte = read_dimensions(argv[1]);
write_u8(argv[2], &output_byte, 1); write_u8(argv[2], &output_byte, 1);
return 0; return 0;

View File

@ -1,135 +1,105 @@
#include <stdio.h> #include "common.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
void usage(void) { void usage(void) {
printf("Usage: scan_includes [-h] [-s] filename\n" fputs("Usage: scan_includes [-h|--help] [-s|--strict] filename.asm\n", stderr);
"-h, --help\n"
" Print usage and exit\n"
"-s, --strict\n"
" Fail if a file cannot be read\n");
} }
struct Options { void scan_file(const char *filename, bool strict) {
bool help; errno = 0;
bool strict;
};
struct Options Options = {0};
void scan_file(char* filename) {
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
if (!f) { if (!f) {
if (Options.strict) { if (strict) {
fprintf(stderr, "Could not open file: '%s'\n", filename); fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
exit(1); exit(1);
} else { } else {
return; return;
} }
} }
fseek(f, 0, SEEK_END); long size = file_size_verbose(filename, f);
long size = ftell(f); char *contents = malloc_verbose(size + 1);
rewind(f); fread_verbose((uint8_t *)contents, size, filename, f);
char *buffer = malloc(size + 1);
char *orig = buffer;
size = fread(buffer, 1, size, f);
buffer[size] = '\0';
fclose(f); fclose(f);
contents[size] = '\0';
for (; buffer && (buffer - orig < size); buffer++) { for (char *ptr = contents; ptr && ptr - contents < size; ptr++) {
bool is_include = false; bool is_incbin = false, is_include = false;
bool is_incbin = false; switch (*ptr) {
switch (*buffer) { case ';':
case ';': ptr = strchr(ptr, '\n');
buffer = strchr(buffer, '\n'); if (!ptr) {
if (!buffer) { fprintf(stderr, "%s: no newline at end of file\n", filename);
fprintf(stderr, "%s: no newline at end of file\n", filename); break;
}
break;
case '"':
ptr++;
ptr = strchr(ptr, '"');
if (!ptr) {
fprintf(stderr, "%s: unterminated string\n", filename);
break;
}
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; break;
} }
break; ptr++;
char *include_path = ptr;
case '"': size_t length = strcspn(ptr, "\"");
buffer++; ptr += length + 1;
buffer = strchr(buffer, '"'); include_path[length] = '\0';
if (!buffer) { printf("%s ", include_path);
fprintf(stderr, "%s: unterminated string\n", filename); if (is_include) {
break; scan_file(include_path, strict);
} }
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);
if (is_include) {
scan_file(include);
}
free(include);
buffer = strchr(buffer, '"');
}
break;
}
if (!buffer) {
break; break;
} }
} }
free(orig); free(contents);
} }
int main(int argc, char* argv[]) { void parse_args(int argc, char *argv[], bool *strict) {
int i = 0;
struct option long_options[] = { struct option long_options[] = {
{"strict", no_argument, 0, 's'}, {"strict", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
int opt = -1; for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) {
while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
switch (opt) { switch (opt) {
case 's': case 's':
Options.strict = true; *strict = true;
break; break;
case 'h': case 'h':
Options.help = true; usage();
exit(0);
break; break;
default: default:
usage(); usage();
exit(1); exit(1);
break;
} }
} }
}
int main(int argc, char *argv[]) {
bool strict = false;
parse_args(argc, argv, &strict);
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (Options.help) {
usage();
return 0;
}
if (argc < 1) { if (argc < 1) {
usage(); usage();
exit(1); exit(1);
} }
scan_file(argv[0]);
scan_file(argv[0], strict);
return 0; return 0;
} }

View File

@ -37,7 +37,7 @@ uint8_t dbg_base[BASESIZE] = {'b', 'a', 's', 'e', 1, 0, 0, 0,
uint8_t n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'}; uint8_t n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'};
static void usage(void) { static void usage(void) {
fprintf(stderr, "Usage: stadium [-h|--help] [-b|--base us|eu|dbg] romfile\n"); fputs("Usage: stadium [-h|--help] [-b|--base us|eu|dbg] romfile\n", stderr);
} }
void parse_args(int argc, char *argv[], Base *b) { void parse_args(int argc, char *argv[], Base *b) {
@ -46,8 +46,8 @@ void parse_args(int argc, char *argv[], Base *b) {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
for (int opt = 0; opt != -1;) { for (int opt; (opt = getopt_long(argc, argv, "hb:", long_options)) != -1;) {
switch (opt = getopt_long(argc, argv, "hb:", long_options)) { switch (opt) {
case 'h': case 'h':
usage(); usage();
exit(0); exit(0);
@ -58,13 +58,9 @@ void parse_args(int argc, char *argv[], Base *b) {
!strcmp(optarg, "dbg") ? BASE_DEBUG : !strcmp(optarg, "dbg") ? BASE_DEBUG :
BASE_NONE; BASE_NONE;
break; break;
case 0:
case -1:
break;
default: default:
usage(); usage();
exit(1); exit(1);
break;
} }
} }
} }
@ -157,6 +153,5 @@ int main(int argc, char *argv[]) {
uint8_t *file = read_u8(filename, &filesize); uint8_t *file = read_u8(filename, &filesize);
calculate_checksums(file, filesize, base); calculate_checksums(file, filesize, base);
write_u8(filename, file, filesize); write_u8(filename, file, filesize);
return 0; return 0;
} }