Better file checking

This commit is contained in:
2024-02-18 00:27:34 -06:00
parent 561ad55a60
commit 0db57614cd

View File

@@ -40,6 +40,7 @@ static void mputc(struct minstate *state, char c, struct ncdgfile *file);
static long putvar(long i, struct minstate *s, struct ncdgfile *out, static long putvar(long i, struct minstate *s, struct ncdgfile *out,
const struct string *file, const struct vector *vars); const struct string *file, const struct vector *vars);
static int defvars(struct expandfile *expanded, char *filename); static int defvars(struct expandfile *expanded, char *filename);
static long count_escapes(struct string *string);
int parsefile(char *template, FILE *out) { int parsefile(char *template, FILE *out) {
struct expandfile expanded; struct expandfile expanded;
@@ -90,6 +91,10 @@ error1:
static int writefile(struct expandfile *file, struct ncdgfile *out) { static int writefile(struct expandfile *file, struct ncdgfile *out) {
long i; long i;
struct minstate s; struct minstate s;
if (count_escapes(file->data) % 2 != 0) {
fputs("File or nest has an odd number of escape chars\n", stderr);
return 1;
}
initminstate(&s); initminstate(&s);
for (i = 0; i < file->data->len; ++i) { for (i = 0; i < file->data->len; ++i) {
if (file->data->data[i] == ESCAPE_CHAR) { if (file->data->data[i] == ESCAPE_CHAR) {
@@ -477,3 +482,14 @@ error2:
error1: error1:
return 1; return 1;
} }
static long count_escapes(struct string *string) {
long i, ret;
ret = 0;
for (i = 0; i < string->len; ++i) {
if (string->data[i] == ESCAPE_CHAR) {
++ret;
}
}
return ret;
}