Avoid goto, support '\r', and fix typo in scan_includes.c

This commit is contained in:
Rangi
2023-08-15 19:19:15 -04:00
parent 8e71632536
commit a5c36a72c4

View File

@@ -3,6 +3,8 @@
#include "common.h" #include "common.h"
#include <ctype.h>
void parse_args(int argc, char *argv[], bool *strict) { void parse_args(int argc, char *argv[], bool *strict) {
struct option long_options[] = { struct option long_options[] = {
{"strict", no_argument, 0, 's'}, {"strict", no_argument, 0, 's'},
@@ -40,8 +42,7 @@ void scan_file(const char *filename, bool strict) {
fclose(f); fclose(f);
contents[size] = '\0'; contents[size] = '\0';
for (char *ptr = contents; ptr && ptr - contents < size; ptr++) { for (char *ptr = contents; ptr && ptr < contents + size; ptr++) {
bool is_incbin = false, is_include = false;
ptr = strpbrk(ptr, ";\"Ii"); ptr = strpbrk(ptr, ";\"Ii");
if (!ptr) { if (!ptr) {
break; break;
@@ -49,36 +50,34 @@ void scan_file(const char *filename, bool strict) {
switch (*ptr) { switch (*ptr) {
case ';': case ';':
// Skip comments until the end of the line // Skip comments until the end of the line
ptr = strchr(ptr + 1, '\n'); ptr += strcspn(ptr + 1, "\r\n");
if (!ptr) { if (*ptr) {
fprintf(stderr, "%s: no newline at end of file\n", filename);
goto done;
}
break;
case '"':
// Skip string iteral until the closing quote
ptr = strchr(ptr + 1, '"');
if (ptr) {
ptr++; ptr++;
} else {
fprintf(stderr, "%s: unterminated string\n", filename);
goto done;
} }
break; break;
case '"':
// Skip string literal until the closing quote
ptr += strcspn(ptr + 1, "\"");
if (*ptr) {
ptr++;
}
break;
case 'I': case 'I':
case 'i': case 'i':
/* empty statement between the label and the variable declaration */; /* empty statement between the label and the variable declaration */;
// Check that an INCLUDE/INCBIN starts as its own token // Check that an INCLUDE/INCBIN starts as its own token
char before = ptr > contents ? *(ptr - 1) : '\n'; char before = ptr > contents ? *(ptr - 1) : '\n';
if (before != ' ' && before != '\t' && before != '\n' && before != ':') { if (!isspace((unsigned)before) && before != ':') {
break; break;
} }
is_incbin = !strncmp(ptr, "INCBIN", 6) || !strncmp(ptr, "incbin", 6); bool is_incbin = !strncmp(ptr, "INCBIN", 6) || !strncmp(ptr, "incbin", 6);
is_include = !strncmp(ptr, "INCLUDE", 7) || !strncmp(ptr, "include", 7); bool is_include = !strncmp(ptr, "INCLUDE", 7) || !strncmp(ptr, "include", 7);
if (is_incbin || is_include) { if (is_incbin || is_include) {
// Check that an INCLUDE/INCBIN ends as its own token // Check that an INCLUDE/INCBIN ends as its own token
ptr += is_include ? 7 : 6; ptr += is_include ? 7 : 6;
if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n' && *ptr != '"') { if (!isspace((unsigned)*ptr) && *ptr != '"') {
break; break;
} }
ptr += strspn(ptr, " \t"); ptr += strspn(ptr, " \t");
@@ -105,7 +104,6 @@ void scan_file(const char *filename, bool strict) {
} }
} }
done:
free(contents); free(contents);
} }