telodendria/src/Config.c

509 lines
12 KiB
C
Raw Normal View History

2022-07-23 00:19:12 +00:00
#include <Config.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifndef CONFIG_BUFFER_BLOCK
#define CONFIG_BUFFER_BLOCK 32
#endif
2022-07-25 19:18:35 +00:00
struct ConfigDirective
{
2022-07-23 00:19:12 +00:00
Array *values;
HashMap *children;
};
2022-07-25 19:18:35 +00:00
struct ConfigParseResult
{
unsigned int ok:1;
union
{
2022-07-23 00:19:12 +00:00
size_t lineNumber;
HashMap *confMap;
} data;
};
2022-07-25 19:18:35 +00:00
typedef enum ConfigToken
{
2022-07-23 00:19:12 +00:00
TOKEN_UNKNOWN,
TOKEN_NAME,
TOKEN_MACRO_ASSIGNMENT,
TOKEN_VALUE,
TOKEN_SEMICOLON,
TOKEN_BLOCK_OPEN,
TOKEN_BLOCK_CLOSE,
TOKEN_MACRO,
TOKEN_EOF
} ConfigToken;
2022-07-25 19:18:35 +00:00
typedef struct ConfigParserState
{
FILE *stream;
unsigned int line;
2022-07-23 00:19:12 +00:00
2022-07-25 19:18:35 +00:00
char *token;
size_t tokenSize;
size_t tokenLen;
2022-07-23 00:19:12 +00:00
ConfigToken tokenType;
HashMap *macroMap;
} ConfigParserState;
unsigned int
2022-07-25 19:18:35 +00:00
ConfigParseResultOk(ConfigParseResult * result)
2022-07-23 00:19:12 +00:00
{
return result ? result->ok : 0;
}
size_t
2022-07-25 19:18:35 +00:00
ConfigParseResultLineNumber(ConfigParseResult * result)
2022-07-23 00:19:12 +00:00
{
return result && !result->ok ? result->data.lineNumber : 0;
}
HashMap *
2022-07-25 19:18:35 +00:00
ConfigParseResultGet(ConfigParseResult * result)
2022-07-23 00:19:12 +00:00
{
return result && result->ok ? result->data.confMap : NULL;
}
void
2022-07-25 19:18:35 +00:00
ConfigParseResultFree(ConfigParseResult * result)
2022-07-23 00:19:12 +00:00
{
/*
* Note that if the parse was valid, the hash map
* needs to be freed separately.
*/
free(result);
}
Array *
2022-07-25 19:18:35 +00:00
ConfigValuesGet(ConfigDirective * directive)
2022-07-23 00:19:12 +00:00
{
return directive ? directive->values : NULL;
}
HashMap *
2022-07-25 19:18:35 +00:00
ConfigChildrenGet(ConfigDirective * directive)
2022-07-23 00:19:12 +00:00
{
return directive ? directive->children : NULL;
}
/*
* Takes a void pointer because it is only used with
* HashMapIterate(), which requires a pointer to a function
* that takes a void pointer.
*/
static void
ConfigDirectiveFree(void *ptr)
{
ConfigDirective *directive = ptr;
size_t i;
if (!directive)
{
return;
}
for (i = 0; i < ArraySize(directive->values); i++)
{
free(ArrayGet(directive->values, i));
}
ArrayFree(directive->values);
ConfigFree(directive->children);
free(directive);
}
void
2022-07-25 19:18:35 +00:00
ConfigFree(HashMap * conf)
2022-07-23 00:19:12 +00:00
{
HashMapIterate(conf, ConfigDirectiveFree);
HashMapFree(conf);
}
static ConfigParserState *
ConfigParserStateCreate(FILE * stream)
{
ConfigParserState *state = malloc(sizeof(ConfigParserState));
2022-07-25 19:18:35 +00:00
2022-07-23 00:19:12 +00:00
if (!state)
{
return NULL;
}
state->macroMap = HashMapCreate();
if (!state->macroMap)
{
free(state);
return NULL;
}
state->stream = stream;
state->line = 1;
state->token = NULL;
state->tokenSize = 0;
state->tokenLen = 0;
state->tokenType = TOKEN_UNKNOWN;
return state;
}
static void
2022-07-25 19:18:35 +00:00
ConfigParserStateFree(ConfigParserState * state)
2022-07-23 00:19:12 +00:00
{
if (!state)
{
return;
}
free(state->token);
HashMapIterate(state->macroMap, free);
HashMapFree(state->macroMap);
free(state);
}
static int
ConfigIsNameChar(int c)
{
return isdigit(c) || isalpha(c) || (c == '-' || c == '_');
}
static char
2022-07-25 19:18:35 +00:00
ConfigConsumeWhitespace(ConfigParserState * state)
2022-07-23 00:19:12 +00:00
{
int c;
2022-07-25 19:18:35 +00:00
2022-07-23 00:19:12 +00:00
while (isspace(c = fgetc(state->stream)))
{
if (c == '\n')
{
state->line++;
}
}
return c;
}
static void
2022-07-25 19:18:35 +00:00
ConfigConsumeLine(ConfigParserState * state)
2022-07-23 00:19:12 +00:00
{
while (fgetc(state->stream) != '\n');
state->line++;
}
static void
2022-07-25 19:18:35 +00:00
ConfigTokenSeek(ConfigParserState * state)
2022-07-23 00:19:12 +00:00
{
int c;
/* If we already hit EOF, don't do anything */
2022-07-25 19:18:35 +00:00
if (state->tokenType == TOKEN_EOF)
{
2022-07-23 00:19:12 +00:00
return;
}
2022-07-25 19:18:35 +00:00
while ((c = ConfigConsumeWhitespace(state)) == '#')
{
2022-07-23 00:19:12 +00:00
ConfigConsumeLine(state);
}
/*
* After all whitespace and comments are consumed, identify the
* token by looking at the next character
*/
2022-07-25 19:18:35 +00:00
if (feof(state->stream))
{
2022-07-23 00:19:12 +00:00
state->tokenType = TOKEN_EOF;
return;
}
2022-07-25 19:18:35 +00:00
if (ConfigIsNameChar(c))
{
2022-07-23 00:19:12 +00:00
state->tokenLen = 0;
/* Read the key/macro into state->token */
2022-07-25 19:18:35 +00:00
if (!state->token)
{
2022-07-23 00:19:12 +00:00
state->tokenSize = CONFIG_BUFFER_BLOCK;
state->token = malloc(CONFIG_BUFFER_BLOCK);
}
state->token[state->tokenLen] = c;
state->tokenLen++;
2022-07-25 19:18:35 +00:00
while (ConfigIsNameChar((c = fgetc(state->stream))))
{
2022-07-23 00:19:12 +00:00
state->token[state->tokenLen] = c;
state->tokenLen++;
2022-07-25 19:18:35 +00:00
if (state->tokenLen >= state->tokenSize)
{
2022-07-23 00:19:12 +00:00
state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token,
2022-07-25 19:18:35 +00:00
state->tokenSize);
2022-07-23 00:19:12 +00:00
}
}
state->token[state->tokenLen] = '\0';
state->tokenLen++;
2022-07-25 19:18:35 +00:00
if (!isspace(c))
{
2022-07-23 00:19:12 +00:00
state->tokenType = TOKEN_UNKNOWN;
2022-07-25 19:18:35 +00:00
}
else
{
2022-07-23 00:19:12 +00:00
state->tokenType = TOKEN_NAME;
2022-07-25 19:18:35 +00:00
if (c == '\n')
{
2022-07-23 00:19:12 +00:00
state->line++;
}
}
2022-07-25 19:18:35 +00:00
}
else
{
switch (c)
{
2022-07-23 00:19:12 +00:00
case '=':
state->tokenType = TOKEN_MACRO_ASSIGNMENT;
break;
case '"':
state->tokenLen = 0;
state->tokenType = TOKEN_VALUE;
/* read the value into state->curtok */
2022-07-25 19:18:35 +00:00
while ((c = fgetc(state->stream)) != '"')
{
if (c == '\n')
{
2022-07-23 00:19:12 +00:00
state->line++;
}
/*
* End of the stream reached without finding
* a closing quote
*/
2022-07-25 19:18:35 +00:00
if (feof(state->stream))
{
2022-07-23 00:19:12 +00:00
state->tokenType = TOKEN_EOF;
break;
}
state->token[state->tokenLen] = c;
state->tokenLen++;
2022-07-25 19:18:35 +00:00
if (state->tokenLen >= state->tokenSize)
{
2022-07-23 00:19:12 +00:00
state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token,
2022-07-25 19:18:35 +00:00
state->tokenSize);
2022-07-23 00:19:12 +00:00
}
}
state->token[state->tokenLen] = '\0';
state->tokenLen++;
break;
case ';':
state->tokenType = TOKEN_SEMICOLON;
break;
case '{':
state->tokenType = TOKEN_BLOCK_OPEN;
break;
case '}':
state->tokenType = TOKEN_BLOCK_CLOSE;
break;
case '$':
state->tokenLen = 0;
/* read the macro name into state->curtok */
2022-07-25 19:18:35 +00:00
while (ConfigIsNameChar(c = fgetc(state->stream)))
{
2022-07-23 00:19:12 +00:00
state->token[state->tokenLen] = c;
state->tokenLen++;
2022-07-25 19:18:35 +00:00
if (state->tokenLen >= state->tokenSize)
{
2022-07-23 00:19:12 +00:00
state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token,
2022-07-25 19:18:35 +00:00
state->tokenSize);
2022-07-23 00:19:12 +00:00
}
}
state->token[state->tokenLen] = '\0';
state->tokenLen++;
state->tokenType = TOKEN_MACRO;
ungetc(c, state->stream);
break;
default:
state->tokenType = TOKEN_UNKNOWN;
break;
}
}
/* Resize curtok to only use the bytes it needs */
2022-07-25 19:18:35 +00:00
if (state->tokenLen)
{
2022-07-23 00:19:12 +00:00
state->tokenSize = state->tokenLen;
state->token = realloc(state->token, state->tokenSize);
}
}
static int
2022-07-25 19:18:35 +00:00
ConfigExpect(ConfigParserState * state, ConfigToken tokenType)
2022-07-23 00:19:12 +00:00
{
return state->tokenType == tokenType;
}
static HashMap *
2022-07-25 19:18:35 +00:00
ConfigParseBlock(ConfigParserState * state, int level)
2022-07-23 00:19:12 +00:00
{
HashMap *block = HashMapCreate();
ConfigTokenSeek(state);
2022-07-25 19:18:35 +00:00
while (ConfigExpect(state, TOKEN_NAME))
{
char *name = malloc(state->tokenLen + 1);
2022-07-23 00:19:12 +00:00
strcpy(name, state->token);
ConfigTokenSeek(state);
2022-07-25 19:18:35 +00:00
if (ConfigExpect(state, TOKEN_VALUE) || ConfigExpect(state, TOKEN_MACRO))
{
2022-07-23 00:19:12 +00:00
ConfigDirective *directive;
directive = malloc(sizeof(ConfigDirective));
directive->children = NULL;
directive->values = ArrayCreate();
while (ConfigExpect(state, TOKEN_VALUE) ||
2022-07-25 19:18:35 +00:00
ConfigExpect(state, TOKEN_MACRO))
{
2022-07-23 00:19:12 +00:00
char *dval;
char *dvalCpy;
2022-07-25 19:18:35 +00:00
if (ConfigExpect(state, TOKEN_VALUE))
{
2022-07-23 00:19:12 +00:00
dval = state->token;
2022-07-25 19:18:35 +00:00
}
else if (ConfigExpect(state, TOKEN_MACRO))
{
2022-07-23 00:19:12 +00:00
dval = HashMapGet(state->macroMap, state->token);
2022-07-25 19:18:35 +00:00
if (!dval)
{
2022-07-23 00:19:12 +00:00
goto error;
}
2022-07-25 19:18:35 +00:00
}
else
{
dval = NULL; /* Should never happen */
2022-07-23 00:19:12 +00:00
}
2022-07-25 19:18:35 +00:00
/* dval is a pointer which is overwritten with the next
* token. */
2022-07-23 00:19:12 +00:00
dvalCpy = malloc(strlen(dval) + 1);
strcpy(dvalCpy, dval);
ArrayAdd(directive->values, dvalCpy);
ConfigTokenSeek(state);
}
2022-07-25 19:18:35 +00:00
if (ConfigExpect(state, TOKEN_BLOCK_OPEN))
{
2022-07-23 00:19:12 +00:00
/* token_seek(state); */
directive->children = ConfigParseBlock(state, level + 1);
2022-07-25 19:18:35 +00:00
if (!directive->children)
{
2022-07-23 00:19:12 +00:00
goto error;
}
}
/*
* Append this directive to the current block,
* overwriting a directive at this level with the same name.
*
* Note that if a value already exists with this name, it is
* returned by HashMapSet() and then immediately passed to
* ConfigDirectiveFree(). If the value does not exist, then
* NULL is sent to ConfigDirectiveFree(), making it a no-op.
*/
ConfigDirectiveFree(HashMapSet(block, name, directive));
2022-07-25 19:18:35 +00:00
}
else if (ConfigExpect(state, TOKEN_MACRO_ASSIGNMENT))
{
2022-07-23 00:19:12 +00:00
ConfigTokenSeek(state);
2022-07-25 19:18:35 +00:00
if (ConfigExpect(state, TOKEN_VALUE))
{
char *valueCopy = malloc(strlen(state->token) + 1);
2022-07-23 00:19:12 +00:00
strcpy(valueCopy, state->token);
free(HashMapSet(state->macroMap, name, state->token));
ConfigTokenSeek(state);
2022-07-25 19:18:35 +00:00
}
else
{
2022-07-23 00:19:12 +00:00
goto error;
}
2022-07-25 19:18:35 +00:00
}
else
{
2022-07-23 00:19:12 +00:00
goto error;
}
2022-07-25 19:18:35 +00:00
if (!ConfigExpect(state, TOKEN_SEMICOLON))
{
2022-07-23 00:19:12 +00:00
goto error;
}
ConfigTokenSeek(state);
}
2022-07-25 19:18:35 +00:00
if (ConfigExpect(state, level ? TOKEN_BLOCK_CLOSE : TOKEN_EOF))
{
2022-07-23 00:19:12 +00:00
ConfigTokenSeek(state);
return block;
2022-07-25 19:18:35 +00:00
}
else
{
2022-07-23 00:19:12 +00:00
goto error;
}
2022-07-25 19:18:35 +00:00
error:
2022-07-23 00:19:12 +00:00
/* Only free the very top level, because this will recurse */
2022-07-25 19:18:35 +00:00
if (!level)
{
2022-07-23 00:19:12 +00:00
ConfigFree(block);
}
return NULL;
}
ConfigParseResult *
ConfigParse(FILE * stream)
{
ConfigParseResult *result;
HashMap *conf;
ConfigParserState *state;
result = malloc(sizeof(ConfigParseResult));
state = ConfigParserStateCreate(stream);
conf = ConfigParseBlock(state, 0);
2022-07-25 19:18:35 +00:00
if (!conf)
{
2022-07-23 00:19:12 +00:00
result->ok = 0;
result->data.lineNumber = state->line;
2022-07-25 19:18:35 +00:00
}
else
{
2022-07-23 00:19:12 +00:00
result->ok = 1;
result->data.confMap = conf;
}
ConfigParserStateFree(state);
return result;
}