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