forked from Telodendria/Telodendria
Run indent(1) on all C source files.
This commit is contained in:
parent
ae2b853cc4
commit
aabb2a0203
6 changed files with 213 additions and 141 deletions
24
src/Array.c
24
src/Array.c
|
@ -7,14 +7,16 @@
|
|||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Array {
|
||||
void **entries; /* An array of void pointers, to store any data */
|
||||
size_t allocated; /* Elements allocated on the heap */
|
||||
size_t size; /* Elements actually filled */
|
||||
struct Array
|
||||
{
|
||||
void **entries; /* An array of void pointers, to
|
||||
* store any data */
|
||||
size_t allocated; /* Elements allocated on the heap */
|
||||
size_t size; /* Elements actually filled */
|
||||
};
|
||||
|
||||
int
|
||||
ArrayAdd(Array *array, void *value)
|
||||
ArrayAdd(Array * array, void *value)
|
||||
{
|
||||
if (!array)
|
||||
{
|
||||
|
@ -48,7 +50,7 @@ ArrayCreate(void)
|
|||
}
|
||||
|
||||
void *
|
||||
ArrayDelete(Array *array, size_t index)
|
||||
ArrayDelete(Array * array, size_t index)
|
||||
{
|
||||
size_t i;
|
||||
void *element;
|
||||
|
@ -71,7 +73,7 @@ ArrayDelete(Array *array, size_t index)
|
|||
}
|
||||
|
||||
void
|
||||
ArrayFree(Array *array)
|
||||
ArrayFree(Array * array)
|
||||
{
|
||||
if (array)
|
||||
{
|
||||
|
@ -81,7 +83,7 @@ ArrayFree(Array *array)
|
|||
}
|
||||
|
||||
void *
|
||||
ArrayGet(Array *array, size_t index)
|
||||
ArrayGet(Array * array, size_t index)
|
||||
{
|
||||
if (!array)
|
||||
{
|
||||
|
@ -98,7 +100,7 @@ ArrayGet(Array *array, size_t index)
|
|||
|
||||
|
||||
extern int
|
||||
ArrayInsert(Array *array, void *value, size_t index)
|
||||
ArrayInsert(Array * array, void *value, size_t index)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
|
@ -139,7 +141,7 @@ ArrayInsert(Array *array, void *value, size_t index)
|
|||
}
|
||||
|
||||
size_t
|
||||
ArraySize(Array *array)
|
||||
ArraySize(Array * array)
|
||||
{
|
||||
if (!array)
|
||||
{
|
||||
|
@ -150,7 +152,7 @@ ArraySize(Array *array)
|
|||
}
|
||||
|
||||
int
|
||||
ArrayTrim(Array *array)
|
||||
ArrayTrim(Array * array)
|
||||
{
|
||||
void **tmp;
|
||||
|
||||
|
|
59
src/Base64.c
59
src/Base64.c
|
@ -3,15 +3,15 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
static const char Base64EncodeMap[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static const int Base64DecodeMap[] = {
|
||||
62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
|
||||
59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5,
|
||||
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
|
||||
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
|
||||
43, 44, 45, 46, 47, 48, 49, 50, 51
|
||||
62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
|
||||
59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5,
|
||||
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
|
||||
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
|
||||
43, 44, 45, 46, 47, 48, 49, 50, 51
|
||||
};
|
||||
|
||||
size_t
|
||||
|
@ -43,10 +43,14 @@ Base64DecodedSize(const char *base64, size_t len)
|
|||
|
||||
ret = len / 4 * 3;
|
||||
|
||||
for (i = len; i > 0; i--) {
|
||||
if (base64[i] == '=') {
|
||||
for (i = len; i > 0; i--)
|
||||
{
|
||||
if (base64[i] == '=')
|
||||
{
|
||||
ret--;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -86,12 +90,17 @@ Base64Encode(const char *input, size_t len)
|
|||
if (i + 1 < len)
|
||||
{
|
||||
out[j + 2] = Base64EncodeMap[(v >> 6) & 0x3F];
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
out[j + 2] = '=';
|
||||
}
|
||||
if (i + 2 < len) {
|
||||
if (i + 2 < len)
|
||||
{
|
||||
out[j + 3] = Base64EncodeMap[v & 0x3F];
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
out[j + 3] = '=';
|
||||
}
|
||||
}
|
||||
|
@ -103,11 +112,11 @@ static int
|
|||
Base64IsValidChar(char c)
|
||||
{
|
||||
return (c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'Z') ||
|
||||
(c >= 'a' && c <= 'z') ||
|
||||
(c == '+') ||
|
||||
(c == '/') ||
|
||||
(c == '=');
|
||||
(c >= 'A' && c <= 'Z') ||
|
||||
(c >= 'a' && c <= 'z') ||
|
||||
(c == '+') ||
|
||||
(c == '/') ||
|
||||
(c == '=');
|
||||
}
|
||||
|
||||
char *
|
||||
|
@ -126,11 +135,11 @@ Base64Decode(const char *input, size_t len)
|
|||
outLen = Base64DecodedSize(input, len);
|
||||
if (len % 4)
|
||||
{
|
||||
/* Invalid length; must have incorrect padding */
|
||||
/* Invalid length; must have incorrect padding */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Scan for invalid characters. */
|
||||
/* Scan for invalid characters. */
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (!Base64IsValidChar(input[i]))
|
||||
|
@ -147,7 +156,8 @@ Base64Decode(const char *input, size_t len)
|
|||
|
||||
out[outLen] = '\0';
|
||||
|
||||
for (i = 0, j = 0; i < len; i += 4, j += 3) {
|
||||
for (i = 0, j = 0; i < len; i += 4, j += 3)
|
||||
{
|
||||
v = Base64DecodeMap[input[i] - 43];
|
||||
v = (v << 6) | Base64DecodeMap[input[i + 1] - 43];
|
||||
v = input[i + 2] == '=' ? v << 6 : (v << 6) | Base64DecodeMap[input[i + 2] - 43];
|
||||
|
@ -188,7 +198,7 @@ Base64Pad(char **base64Ptr, size_t length)
|
|||
|
||||
if (length % 4 == 0)
|
||||
{
|
||||
return length; /* Success: no padding needed */
|
||||
return length; /* Success: no padding needed */
|
||||
}
|
||||
|
||||
newSize = length + (4 - (length % 4));
|
||||
|
@ -196,9 +206,9 @@ Base64Pad(char **base64Ptr, size_t length)
|
|||
tmp = realloc(*base64Ptr, newSize + 100);;
|
||||
if (!tmp)
|
||||
{
|
||||
return 0; /* Memory error */
|
||||
return 0; /* Memory error */
|
||||
}
|
||||
*base64Ptr = tmp;
|
||||
*base64Ptr = tmp;
|
||||
|
||||
for (i = length; i < newSize; i++)
|
||||
{
|
||||
|
@ -209,4 +219,3 @@ Base64Pad(char **base64Ptr, size_t length)
|
|||
|
||||
return newSize;
|
||||
}
|
||||
|
||||
|
|
198
src/Config.c
198
src/Config.c
|
@ -8,20 +8,24 @@
|
|||
#define CONFIG_BUFFER_BLOCK 32
|
||||
#endif
|
||||
|
||||
struct ConfigDirective {
|
||||
struct ConfigDirective
|
||||
{
|
||||
Array *values;
|
||||
HashMap *children;
|
||||
};
|
||||
|
||||
struct ConfigParseResult {
|
||||
unsigned int ok : 1;
|
||||
union {
|
||||
struct ConfigParseResult
|
||||
{
|
||||
unsigned int ok:1;
|
||||
union
|
||||
{
|
||||
size_t lineNumber;
|
||||
HashMap *confMap;
|
||||
} data;
|
||||
};
|
||||
|
||||
typedef enum ConfigToken {
|
||||
typedef enum ConfigToken
|
||||
{
|
||||
TOKEN_UNKNOWN,
|
||||
TOKEN_NAME,
|
||||
TOKEN_MACRO_ASSIGNMENT,
|
||||
|
@ -33,13 +37,14 @@ typedef enum ConfigToken {
|
|||
TOKEN_EOF
|
||||
} ConfigToken;
|
||||
|
||||
typedef struct ConfigParserState {
|
||||
FILE *stream;
|
||||
unsigned int line;
|
||||
typedef struct ConfigParserState
|
||||
{
|
||||
FILE *stream;
|
||||
unsigned int line;
|
||||
|
||||
char *token;
|
||||
size_t tokenSize;
|
||||
size_t tokenLen;
|
||||
char *token;
|
||||
size_t tokenSize;
|
||||
size_t tokenLen;
|
||||
ConfigToken tokenType;
|
||||
|
||||
HashMap *macroMap;
|
||||
|
@ -47,25 +52,25 @@ typedef struct ConfigParserState {
|
|||
} ConfigParserState;
|
||||
|
||||
unsigned int
|
||||
ConfigParseResultOk(ConfigParseResult *result)
|
||||
ConfigParseResultOk(ConfigParseResult * result)
|
||||
{
|
||||
return result ? result->ok : 0;
|
||||
}
|
||||
|
||||
size_t
|
||||
ConfigParseResultLineNumber(ConfigParseResult *result)
|
||||
ConfigParseResultLineNumber(ConfigParseResult * result)
|
||||
{
|
||||
return result && !result->ok ? result->data.lineNumber : 0;
|
||||
}
|
||||
|
||||
HashMap *
|
||||
ConfigParseResultGet(ConfigParseResult *result)
|
||||
ConfigParseResultGet(ConfigParseResult * result)
|
||||
{
|
||||
return result && result->ok ? result->data.confMap : NULL;
|
||||
}
|
||||
|
||||
void
|
||||
ConfigParseResultFree(ConfigParseResult *result)
|
||||
ConfigParseResultFree(ConfigParseResult * result)
|
||||
{
|
||||
/*
|
||||
* Note that if the parse was valid, the hash map
|
||||
|
@ -75,13 +80,13 @@ ConfigParseResultFree(ConfigParseResult *result)
|
|||
}
|
||||
|
||||
Array *
|
||||
ConfigValuesGet(ConfigDirective *directive)
|
||||
ConfigValuesGet(ConfigDirective * directive)
|
||||
{
|
||||
return directive ? directive->values : NULL;
|
||||
}
|
||||
|
||||
HashMap *
|
||||
ConfigChildrenGet(ConfigDirective *directive)
|
||||
ConfigChildrenGet(ConfigDirective * directive)
|
||||
{
|
||||
return directive ? directive->children : NULL;
|
||||
}
|
||||
|
@ -114,7 +119,7 @@ ConfigDirectiveFree(void *ptr)
|
|||
}
|
||||
|
||||
void
|
||||
ConfigFree(HashMap *conf)
|
||||
ConfigFree(HashMap * conf)
|
||||
{
|
||||
HashMapIterate(conf, ConfigDirectiveFree);
|
||||
HashMapFree(conf);
|
||||
|
@ -124,6 +129,7 @@ static ConfigParserState *
|
|||
ConfigParserStateCreate(FILE * stream)
|
||||
{
|
||||
ConfigParserState *state = malloc(sizeof(ConfigParserState));
|
||||
|
||||
if (!state)
|
||||
{
|
||||
return NULL;
|
||||
|
@ -148,7 +154,7 @@ ConfigParserStateCreate(FILE * stream)
|
|||
}
|
||||
|
||||
static void
|
||||
ConfigParserStateFree(ConfigParserState *state)
|
||||
ConfigParserStateFree(ConfigParserState * state)
|
||||
{
|
||||
if (!state)
|
||||
{
|
||||
|
@ -170,9 +176,10 @@ ConfigIsNameChar(int c)
|
|||
}
|
||||
|
||||
static char
|
||||
ConfigConsumeWhitespace(ConfigParserState *state)
|
||||
ConfigConsumeWhitespace(ConfigParserState * state)
|
||||
{
|
||||
int c;
|
||||
|
||||
while (isspace(c = fgetc(state->stream)))
|
||||
{
|
||||
if (c == '\n')
|
||||
|
@ -184,22 +191,24 @@ ConfigConsumeWhitespace(ConfigParserState *state)
|
|||
}
|
||||
|
||||
static void
|
||||
ConfigConsumeLine(ConfigParserState *state)
|
||||
ConfigConsumeLine(ConfigParserState * state)
|
||||
{
|
||||
while (fgetc(state->stream) != '\n');
|
||||
state->line++;
|
||||
}
|
||||
|
||||
static void
|
||||
ConfigTokenSeek(ConfigParserState *state)
|
||||
ConfigTokenSeek(ConfigParserState * state)
|
||||
{
|
||||
int c;
|
||||
|
||||
/* If we already hit EOF, don't do anything */
|
||||
if (state->tokenType == TOKEN_EOF) {
|
||||
if (state->tokenType == TOKEN_EOF)
|
||||
{
|
||||
return;
|
||||
}
|
||||
while ((c = ConfigConsumeWhitespace(state)) == '#') {
|
||||
while ((c = ConfigConsumeWhitespace(state)) == '#')
|
||||
{
|
||||
ConfigConsumeLine(state);
|
||||
}
|
||||
|
||||
|
@ -208,47 +217,59 @@ ConfigTokenSeek(ConfigParserState *state)
|
|||
* token by looking at the next character
|
||||
*/
|
||||
|
||||
if (feof(state->stream)) {
|
||||
if (feof(state->stream))
|
||||
{
|
||||
state->tokenType = TOKEN_EOF;
|
||||
return;
|
||||
}
|
||||
if (ConfigIsNameChar(c)) {
|
||||
if (ConfigIsNameChar(c))
|
||||
{
|
||||
state->tokenLen = 0;
|
||||
|
||||
/* Read the key/macro into state->token */
|
||||
if (!state->token) {
|
||||
if (!state->token)
|
||||
{
|
||||
state->tokenSize = CONFIG_BUFFER_BLOCK;
|
||||
state->token = malloc(CONFIG_BUFFER_BLOCK);
|
||||
}
|
||||
state->token[state->tokenLen] = c;
|
||||
state->tokenLen++;
|
||||
|
||||
while (ConfigIsNameChar((c = fgetc(state->stream)))) {
|
||||
while (ConfigIsNameChar((c = fgetc(state->stream))))
|
||||
{
|
||||
state->token[state->tokenLen] = c;
|
||||
state->tokenLen++;
|
||||
|
||||
if (state->tokenLen >= state->tokenSize) {
|
||||
if (state->tokenLen >= state->tokenSize)
|
||||
{
|
||||
state->tokenSize += CONFIG_BUFFER_BLOCK;
|
||||
state->token = realloc(state->token,
|
||||
state->tokenSize);
|
||||
state->tokenSize);
|
||||
}
|
||||
}
|
||||
|
||||
state->token[state->tokenLen] = '\0';
|
||||
state->tokenLen++;
|
||||
|
||||
if (!isspace(c)) {
|
||||
if (!isspace(c))
|
||||
{
|
||||
state->tokenType = TOKEN_UNKNOWN;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
state->tokenType = TOKEN_NAME;
|
||||
|
||||
if (c == '\n') {
|
||||
if (c == '\n')
|
||||
{
|
||||
state->line++;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (c) {
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '=':
|
||||
state->tokenType = TOKEN_MACRO_ASSIGNMENT;
|
||||
break;
|
||||
|
@ -257,25 +278,29 @@ ConfigTokenSeek(ConfigParserState *state)
|
|||
state->tokenType = TOKEN_VALUE;
|
||||
|
||||
/* read the value into state->curtok */
|
||||
while ((c = fgetc(state->stream)) != '"') {
|
||||
if (c == '\n') {
|
||||
while ((c = fgetc(state->stream)) != '"')
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
state->line++;
|
||||
}
|
||||
/*
|
||||
* End of the stream reached without finding
|
||||
* a closing quote
|
||||
*/
|
||||
if (feof(state->stream)) {
|
||||
if (feof(state->stream))
|
||||
{
|
||||
state->tokenType = TOKEN_EOF;
|
||||
break;
|
||||
}
|
||||
state->token[state->tokenLen] = c;
|
||||
state->tokenLen++;
|
||||
|
||||
if (state->tokenLen >= state->tokenSize) {
|
||||
if (state->tokenLen >= state->tokenSize)
|
||||
{
|
||||
state->tokenSize += CONFIG_BUFFER_BLOCK;
|
||||
state->token = realloc(state->token,
|
||||
state->tokenSize);
|
||||
state->tokenSize);
|
||||
}
|
||||
}
|
||||
state->token[state->tokenLen] = '\0';
|
||||
|
@ -293,14 +318,16 @@ ConfigTokenSeek(ConfigParserState *state)
|
|||
case '$':
|
||||
state->tokenLen = 0;
|
||||
/* read the macro name into state->curtok */
|
||||
while (ConfigIsNameChar(c = fgetc(state->stream))) {
|
||||
while (ConfigIsNameChar(c = fgetc(state->stream)))
|
||||
{
|
||||
state->token[state->tokenLen] = c;
|
||||
state->tokenLen++;
|
||||
|
||||
if (state->tokenLen >= state->tokenSize) {
|
||||
if (state->tokenLen >= state->tokenSize)
|
||||
{
|
||||
state->tokenSize += CONFIG_BUFFER_BLOCK;
|
||||
state->token = realloc(state->token,
|
||||
state->tokenSize);
|
||||
state->tokenSize);
|
||||
}
|
||||
}
|
||||
state->token[state->tokenLen] = '\0';
|
||||
|
@ -316,32 +343,36 @@ ConfigTokenSeek(ConfigParserState *state)
|
|||
}
|
||||
|
||||
/* Resize curtok to only use the bytes it needs */
|
||||
if (state->tokenLen) {
|
||||
if (state->tokenLen)
|
||||
{
|
||||
state->tokenSize = state->tokenLen;
|
||||
state->token = realloc(state->token, state->tokenSize);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
ConfigExpect(ConfigParserState *state, ConfigToken tokenType)
|
||||
ConfigExpect(ConfigParserState * state, ConfigToken tokenType)
|
||||
{
|
||||
return state->tokenType == tokenType;
|
||||
}
|
||||
|
||||
|
||||
static HashMap *
|
||||
ConfigParseBlock(ConfigParserState *state, int level)
|
||||
ConfigParseBlock(ConfigParserState * state, int level)
|
||||
{
|
||||
HashMap *block = HashMapCreate();
|
||||
|
||||
ConfigTokenSeek(state);
|
||||
|
||||
while (ConfigExpect(state, TOKEN_NAME)) {
|
||||
char *name = malloc(state->tokenLen + 1);
|
||||
while (ConfigExpect(state, TOKEN_NAME))
|
||||
{
|
||||
char *name = malloc(state->tokenLen + 1);
|
||||
|
||||
strcpy(name, state->token);
|
||||
|
||||
ConfigTokenSeek(state);
|
||||
if (ConfigExpect(state, TOKEN_VALUE) || ConfigExpect(state, TOKEN_MACRO)) {
|
||||
if (ConfigExpect(state, TOKEN_VALUE) || ConfigExpect(state, TOKEN_MACRO))
|
||||
{
|
||||
ConfigDirective *directive;
|
||||
|
||||
directive = malloc(sizeof(ConfigDirective));
|
||||
|
@ -349,23 +380,31 @@ ConfigParseBlock(ConfigParserState *state, int level)
|
|||
directive->values = ArrayCreate();
|
||||
|
||||
while (ConfigExpect(state, TOKEN_VALUE) ||
|
||||
ConfigExpect(state, TOKEN_MACRO)) {
|
||||
ConfigExpect(state, TOKEN_MACRO))
|
||||
{
|
||||
|
||||
char *dval;
|
||||
char *dvalCpy;
|
||||
|
||||
if (ConfigExpect(state, TOKEN_VALUE)) {
|
||||
if (ConfigExpect(state, TOKEN_VALUE))
|
||||
{
|
||||
dval = state->token;
|
||||
} else if (ConfigExpect(state, TOKEN_MACRO)) {
|
||||
}
|
||||
else if (ConfigExpect(state, TOKEN_MACRO))
|
||||
{
|
||||
dval = HashMapGet(state->macroMap, state->token);
|
||||
if (!dval) {
|
||||
if (!dval)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
dval = NULL; /* Should never happen */
|
||||
}
|
||||
else
|
||||
{
|
||||
dval = NULL; /* Should never happen */
|
||||
}
|
||||
|
||||
/* dval is a pointer which is overwritten with the next token. */
|
||||
/* dval is a pointer which is overwritten with the next
|
||||
* token. */
|
||||
dvalCpy = malloc(strlen(dval) + 1);
|
||||
strcpy(dvalCpy, dval);
|
||||
|
||||
|
@ -373,10 +412,12 @@ ConfigParseBlock(ConfigParserState *state, int level)
|
|||
ConfigTokenSeek(state);
|
||||
}
|
||||
|
||||
if (ConfigExpect(state, TOKEN_BLOCK_OPEN)) {
|
||||
if (ConfigExpect(state, TOKEN_BLOCK_OPEN))
|
||||
{
|
||||
/* token_seek(state); */
|
||||
directive->children = ConfigParseBlock(state, level + 1);
|
||||
if (!directive->children) {
|
||||
if (!directive->children)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
@ -392,36 +433,49 @@ ConfigParseBlock(ConfigParserState *state, int level)
|
|||
*/
|
||||
ConfigDirectiveFree(HashMapSet(block, name, directive));
|
||||
|
||||
} else if (ConfigExpect(state, TOKEN_MACRO_ASSIGNMENT)) {
|
||||
}
|
||||
else if (ConfigExpect(state, TOKEN_MACRO_ASSIGNMENT))
|
||||
{
|
||||
ConfigTokenSeek(state);
|
||||
if (ConfigExpect(state, TOKEN_VALUE)) {
|
||||
char * valueCopy = malloc(strlen(state->token) + 1);
|
||||
if (ConfigExpect(state, TOKEN_VALUE))
|
||||
{
|
||||
char *valueCopy = malloc(strlen(state->token) + 1);
|
||||
|
||||
strcpy(valueCopy, state->token);
|
||||
free(HashMapSet(state->macroMap, name, state->token));
|
||||
ConfigTokenSeek(state);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!ConfigExpect(state, TOKEN_SEMICOLON)) {
|
||||
if (!ConfigExpect(state, TOKEN_SEMICOLON))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
ConfigTokenSeek(state);
|
||||
}
|
||||
|
||||
if (ConfigExpect(state, level ? TOKEN_BLOCK_CLOSE : TOKEN_EOF)) {
|
||||
if (ConfigExpect(state, level ? TOKEN_BLOCK_CLOSE : TOKEN_EOF))
|
||||
{
|
||||
ConfigTokenSeek(state);
|
||||
return block;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
error:
|
||||
/* Only free the very top level, because this will recurse */
|
||||
if (!level) {
|
||||
if (!level)
|
||||
{
|
||||
ConfigFree(block);
|
||||
}
|
||||
return NULL;
|
||||
|
@ -438,10 +492,13 @@ ConfigParse(FILE * stream)
|
|||
state = ConfigParserStateCreate(stream);
|
||||
conf = ConfigParseBlock(state, 0);
|
||||
|
||||
if (!conf) {
|
||||
if (!conf)
|
||||
{
|
||||
result->ok = 0;
|
||||
result->data.lineNumber = state->line;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
result->ok = 1;
|
||||
result->data.confMap = conf;
|
||||
}
|
||||
|
@ -449,4 +506,3 @@ ConfigParse(FILE * stream)
|
|||
ConfigParserStateFree(state);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,12 +4,14 @@
|
|||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct HashMapBucket {
|
||||
typedef struct HashMapBucket
|
||||
{
|
||||
uint32_t hash;
|
||||
void *value;
|
||||
} HashMapBucket;
|
||||
|
||||
struct HashMap {
|
||||
struct HashMap
|
||||
{
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
HashMapBucket **entries;
|
||||
|
@ -36,7 +38,7 @@ HashMapHashKey(const char *key)
|
|||
|
||||
|
||||
static int
|
||||
HashMapGrow(HashMap *map)
|
||||
HashMapGrow(HashMap * map)
|
||||
{
|
||||
size_t oldCapacity;
|
||||
size_t i;
|
||||
|
@ -100,6 +102,7 @@ HashMap *
|
|||
HashMapCreate(void)
|
||||
{
|
||||
HashMap *map = malloc(sizeof(HashMap));
|
||||
|
||||
if (!map)
|
||||
{
|
||||
return NULL;
|
||||
|
@ -120,7 +123,7 @@ HashMapCreate(void)
|
|||
}
|
||||
|
||||
void *
|
||||
HashMapDelete(HashMap *map, const char *key)
|
||||
HashMapDelete(HashMap * map, const char *key)
|
||||
{
|
||||
uint32_t hash;
|
||||
size_t index;
|
||||
|
@ -155,7 +158,7 @@ HashMapDelete(HashMap *map, const char *key)
|
|||
}
|
||||
|
||||
void
|
||||
HashMapFree(HashMap *map)
|
||||
HashMapFree(HashMap * map)
|
||||
{
|
||||
if (map)
|
||||
{
|
||||
|
@ -174,7 +177,7 @@ HashMapFree(HashMap *map)
|
|||
}
|
||||
|
||||
void *
|
||||
HashMapGet(HashMap *map, const char *key)
|
||||
HashMapGet(HashMap * map, const char *key)
|
||||
{
|
||||
uint32_t hash;
|
||||
size_t index;
|
||||
|
@ -208,7 +211,7 @@ HashMapGet(HashMap *map, const char *key)
|
|||
}
|
||||
|
||||
void
|
||||
HashMapIterate(HashMap *map, void (*iteratorFunc)(void *))
|
||||
HashMapIterate(HashMap * map, void (*iteratorFunc) (void *))
|
||||
{
|
||||
size_t i;
|
||||
|
||||
|
@ -229,7 +232,7 @@ HashMapIterate(HashMap *map, void (*iteratorFunc)(void *))
|
|||
}
|
||||
|
||||
void
|
||||
HashMapMaxLoadSet(HashMap *map, float load)
|
||||
HashMapMaxLoadSet(HashMap * map, float load)
|
||||
{
|
||||
if (!map)
|
||||
{
|
||||
|
@ -241,7 +244,7 @@ HashMapMaxLoadSet(HashMap *map, float load)
|
|||
|
||||
|
||||
void *
|
||||
HashMapSet(HashMap *map, const char *key, void *value)
|
||||
HashMapSet(HashMap * map, const char *key, void *value)
|
||||
{
|
||||
uint32_t hash;
|
||||
size_t index;
|
||||
|
@ -288,6 +291,7 @@ HashMapSet(HashMap *map, const char *key, void *value)
|
|||
if (bucket->hash == hash)
|
||||
{
|
||||
void *oldValue = bucket->value;
|
||||
|
||||
bucket->value = value;
|
||||
return oldValue;
|
||||
}
|
||||
|
@ -297,4 +301,3 @@ HashMapSet(HashMap *map, const char *key, void *value)
|
|||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
41
src/Log.c
41
src/Log.c
|
@ -8,7 +8,8 @@
|
|||
|
||||
#define LOG_TSBUFFER 64
|
||||
|
||||
struct LogConfig {
|
||||
struct LogConfig
|
||||
{
|
||||
LogLevel level;
|
||||
size_t indent;
|
||||
FILE *out;
|
||||
|
@ -17,7 +18,7 @@ struct LogConfig {
|
|||
};
|
||||
|
||||
void
|
||||
Log(LogConfig *config, LogLevel level, const char *msg, ...)
|
||||
Log(LogConfig * config, LogLevel level, const char *msg,...)
|
||||
{
|
||||
int i;
|
||||
int doColor;
|
||||
|
@ -47,11 +48,12 @@ Log(LogConfig *config, LogLevel level, const char *msg, ...)
|
|||
}
|
||||
|
||||
doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
|
||||
&& isatty(fileno(config->out));
|
||||
&& isatty(fileno(config->out));
|
||||
|
||||
if (doColor)
|
||||
{
|
||||
char *ansi;
|
||||
|
||||
switch (level)
|
||||
{
|
||||
case LOG_ERROR:
|
||||
|
@ -140,10 +142,9 @@ Log(LogConfig *config, LogLevel level, const char *msg, ...)
|
|||
fputc('\n', config->out);
|
||||
va_end(argp);
|
||||
|
||||
/* If we are debugging, there might be something that's
|
||||
* going to segfault the program coming up, so flush the
|
||||
* output stream immediately.
|
||||
*/
|
||||
/* If we are debugging, there might be something that's going to
|
||||
* segfault the program coming up, so flush the output stream
|
||||
* immediately. */
|
||||
if (config->level == LOG_DEBUG)
|
||||
{
|
||||
fflush(config->out);
|
||||
|
@ -164,7 +165,7 @@ LogConfigCreate(void)
|
|||
|
||||
LogConfigLevelSet(config, LOG_MESSAGE);
|
||||
LogConfigIndentSet(config, 0);
|
||||
LogConfigOutputSet(config, NULL); /* Will set to stdout */
|
||||
LogConfigOutputSet(config, NULL); /* Will set to stdout */
|
||||
LogConfigFlagSet(config, LOG_FLAG_COLOR);
|
||||
LogConfigTimeStampFormatSet(config, "%y-%m-%d %H:%M:%S");
|
||||
|
||||
|
@ -172,7 +173,7 @@ LogConfigCreate(void)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigFlagClear(LogConfig *config, int flags)
|
||||
LogConfigFlagClear(LogConfig * config, int flags)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -183,7 +184,7 @@ LogConfigFlagClear(LogConfig *config, int flags)
|
|||
}
|
||||
|
||||
int
|
||||
LogConfigFlagGet(LogConfig *config, int flags)
|
||||
LogConfigFlagGet(LogConfig * config, int flags)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -194,7 +195,7 @@ LogConfigFlagGet(LogConfig *config, int flags)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigFlagSet(LogConfig *config, int flags)
|
||||
LogConfigFlagSet(LogConfig * config, int flags)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -205,13 +206,13 @@ LogConfigFlagSet(LogConfig *config, int flags)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigFree(LogConfig *config)
|
||||
LogConfigFree(LogConfig * config)
|
||||
{
|
||||
free(config);
|
||||
}
|
||||
|
||||
void
|
||||
LogConfigIndent(LogConfig *config)
|
||||
LogConfigIndent(LogConfig * config)
|
||||
{
|
||||
if (config)
|
||||
{
|
||||
|
@ -220,7 +221,7 @@ LogConfigIndent(LogConfig *config)
|
|||
}
|
||||
|
||||
size_t
|
||||
LogConfigIndentGet(LogConfig *config)
|
||||
LogConfigIndentGet(LogConfig * config)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -231,7 +232,7 @@ LogConfigIndentGet(LogConfig *config)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigIndentSet(LogConfig *config, size_t indent)
|
||||
LogConfigIndentSet(LogConfig * config, size_t indent)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -242,7 +243,7 @@ LogConfigIndentSet(LogConfig *config, size_t indent)
|
|||
}
|
||||
|
||||
LogLevel
|
||||
LogConfigLevelGet(LogConfig *config)
|
||||
LogConfigLevelGet(LogConfig * config)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -253,7 +254,7 @@ LogConfigLevelGet(LogConfig *config)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigLevelSet(LogConfig *config, LogLevel level)
|
||||
LogConfigLevelSet(LogConfig * config, LogLevel level)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -273,7 +274,7 @@ LogConfigLevelSet(LogConfig *config, LogLevel level)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigOutputSet(LogConfig *config, FILE *out)
|
||||
LogConfigOutputSet(LogConfig * config, FILE * out)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
|
@ -292,7 +293,7 @@ LogConfigOutputSet(LogConfig *config, FILE *out)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigTimeStampFormatSet(LogConfig *config, char *tsFmt)
|
||||
LogConfigTimeStampFormatSet(LogConfig * config, char *tsFmt)
|
||||
{
|
||||
if (config)
|
||||
{
|
||||
|
@ -301,7 +302,7 @@ LogConfigTimeStampFormatSet(LogConfig *config, char *tsFmt)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigUnindent(LogConfig *config)
|
||||
LogConfigUnindent(LogConfig * config)
|
||||
{
|
||||
if (config && config->indent >= 2)
|
||||
{
|
||||
|
|
|
@ -15,7 +15,7 @@ typedef enum ArgFlag
|
|||
} ArgFlag;
|
||||
|
||||
static void
|
||||
TelodendriaPrintHeader(LogConfig *lc)
|
||||
TelodendriaPrintHeader(LogConfig * lc)
|
||||
{
|
||||
Log(lc, LOG_MESSAGE,
|
||||
" _____ _ _ _ _");
|
||||
|
@ -26,7 +26,7 @@ TelodendriaPrintHeader(LogConfig *lc)
|
|||
Log(lc, LOG_MESSAGE,
|
||||
" | | __/ | (_) | (_| | __/ | | | (_| | | | | (_| |");
|
||||
Log(lc, LOG_MESSAGE,
|
||||
" |_|\\___|_|\\___/ \\__,_|\\___|_| |_|\\__,_|_| |_|\\__,_|");
|
||||
" |_|\\___|_|\\___/ \\__,_|\\___|_| |_|\\__,_|_| |_|\\__,_|");
|
||||
Log(lc, LOG_MESSAGE, "Telodendria v" TELODENDRIA_VERSION);
|
||||
Log(lc, LOG_MESSAGE, "");
|
||||
Log(lc, LOG_MESSAGE,
|
||||
|
@ -37,7 +37,7 @@ TelodendriaPrintHeader(LogConfig *lc)
|
|||
}
|
||||
|
||||
static void
|
||||
TelodendriaPrintUsage(LogConfig *lc)
|
||||
TelodendriaPrintUsage(LogConfig * lc)
|
||||
{
|
||||
Log(lc, LOG_MESSAGE, "Usage:");
|
||||
Log(lc, LOG_MESSAGE, " -c <file> Configuration file ('-' for stdin).");
|
||||
|
@ -45,7 +45,8 @@ TelodendriaPrintUsage(LogConfig *lc)
|
|||
Log(lc, LOG_MESSAGE, " -h Print this usage, then exit.");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
LogConfig *lc;
|
||||
int exit = EXIT_SUCCESS;
|
||||
|
|
Loading…
Reference in a new issue