config: multiple parser fixes

Still need to confirm it actually parses, but we're getting there?
This commit is contained in:
snow flurry 2020-11-16 19:46:51 -08:00
parent e7b8a47e49
commit c8494b86bd

View file

@ -5,8 +5,9 @@
#include <string.h> #include <string.h>
#include "config.h" #include "config.h"
#include "debug.h"
#define PMSG(t, x) fprintf(stderr, t ":%s:%d: " x "\n", path, line) #define PMSG(t, x) fprintf(stderr, "%s:%d:%d: " t ": " x "\n", path, line, col)
#define BUF_STORE(name, len) { \ #define BUF_STORE(name, len) { \
if (i >= len) { \ if (i >= len) { \
@ -24,7 +25,7 @@ config_t *
parse_config(char *path) parse_config(char *path)
{ {
FILE *fd; FILE *fd;
int res, state, line, ws, comment, err, i; int res, state, line, ws, comment, err, i, col;
#define ST_FILENAME 0 #define ST_FILENAME 0
#define ST_EXT 1 #define ST_EXT 1
#define ST_START 2 #define ST_START 2
@ -50,7 +51,7 @@ parse_config(char *path)
memset(conf, 0, sizeof(config_t)); memset(conf, 0, sizeof(config_t));
state = ST_FILENAME; state = ST_FILENAME;
line = 1; line = col = 1;
ws = comment = err = 0; ws = comment = err = 0;
i = 0; i = 0;
while ((ch = getc(fd)) != EOF) { while ((ch = getc(fd)) != EOF) {
@ -65,6 +66,7 @@ parse_config(char *path)
i = 0; i = 0;
ws = 1; ws = 1;
state++; state++;
DPRINTF(("%s:%d:%d: switching to state %d\n", path, line, col, state));
switch (state) { switch (state) {
/* convert offsets to uint32_t (or close enough) */ /* convert offsets to uint32_t (or close enough) */
case ST_START: case ST_START:
@ -93,22 +95,28 @@ parse_config(char *path)
(void)getc(fd); (void)getc(fd);
/* FALLTHROUGH */ /* FALLTHROUGH */
case '\n': case '\n':
cur->next = malloc(sizeof(config_t)); /*
if (cur == NULL) { * Only start a new struct if we didn't have a comment at the
perror("couldn't allocate config"); * beginning of a line.
err = 1; */
break; if (state != ST_FILENAME || !comment) {
} cur->next = malloc(sizeof(config_t));
if (cur == NULL) {
perror("couldn't allocate config");
err = 1;
break;
}
if (state != ST_OPTS) {
PMSG("warn", "line ends early");
}
if (state != ST_OPTS) { cur = cur->next;
PMSG("warn", "line ends early"); memset(cur, 0, sizeof(config_t));
} }
/* reset state for next line */ /* reset state for next line */
state = ST_FILENAME; state = ST_FILENAME;
cur = cur->next; ws = col = i = comment = 0;
memset(cur, 0, sizeof(config_t));
ws = i = comment = 0;
line++; line++;
break; break;
case '#': case '#':
@ -142,20 +150,27 @@ parse_config(char *path)
break; break;
case ST_START: case ST_START:
BUF_STORE("start byte", 10); BUF_STORE("start byte", 10);
break;
case ST_END: case ST_END:
BUF_STORE("end byte", 10); BUF_STORE("end byte", 10);
break;
case ST_SKIP_A: case ST_SKIP_A:
BUF_STORE("first byte skip", 10); BUF_STORE("first byte skip", 10);
break;
case ST_SKIP_B: case ST_SKIP_B:
BUF_STORE("second byte skip", 10); BUF_STORE("second byte skip", 10);
break;
case ST_OPTS: case ST_OPTS:
BUF_STORE("option string", 31); BUF_STORE("option string", 31);
break;
default: default:
PMSG("error", "too many columns"); PMSG("error", "too many columns");
err = 1; err = 1;
} }
} }
col++;
if (err) if (err)
break; break;
} }