Run indent(1) on all C source files.

This commit is contained in:
Jordan Bancino 2022-07-25 15:18:35 -04:00
parent ae2b853cc4
commit aabb2a0203
6 changed files with 213 additions and 141 deletions

View file

@ -7,8 +7,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
struct Array { struct Array
void **entries; /* An array of void pointers, to store any data */ {
void **entries; /* An array of void pointers, to
* store any data */
size_t allocated; /* Elements allocated on the heap */ size_t allocated; /* Elements allocated on the heap */
size_t size; /* Elements actually filled */ size_t size; /* Elements actually filled */
}; };

View file

@ -43,10 +43,14 @@ Base64DecodedSize(const char *base64, size_t len)
ret = len / 4 * 3; ret = len / 4 * 3;
for (i = len; i > 0; i--) { for (i = len; i > 0; i--)
if (base64[i] == '=') { {
if (base64[i] == '=')
{
ret--; ret--;
} else { }
else
{
break; break;
} }
} }
@ -86,12 +90,17 @@ Base64Encode(const char *input, size_t len)
if (i + 1 < len) if (i + 1 < len)
{ {
out[j + 2] = Base64EncodeMap[(v >> 6) & 0x3F]; out[j + 2] = Base64EncodeMap[(v >> 6) & 0x3F];
} else { }
else
{
out[j + 2] = '='; out[j + 2] = '=';
} }
if (i + 2 < len) { if (i + 2 < len)
{
out[j + 3] = Base64EncodeMap[v & 0x3F]; out[j + 3] = Base64EncodeMap[v & 0x3F];
} else { }
else
{
out[j + 3] = '='; out[j + 3] = '=';
} }
} }
@ -147,7 +156,8 @@ Base64Decode(const char *input, size_t len)
out[outLen] = '\0'; 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 = Base64DecodeMap[input[i] - 43];
v = (v << 6) | Base64DecodeMap[input[i + 1] - 43]; v = (v << 6) | Base64DecodeMap[input[i + 1] - 43];
v = input[i + 2] == '=' ? v << 6 : (v << 6) | Base64DecodeMap[input[i + 2] - 43]; v = input[i + 2] == '=' ? v << 6 : (v << 6) | Base64DecodeMap[input[i + 2] - 43];
@ -209,4 +219,3 @@ Base64Pad(char **base64Ptr, size_t length)
return newSize; return newSize;
} }

View file

@ -8,20 +8,24 @@
#define CONFIG_BUFFER_BLOCK 32 #define CONFIG_BUFFER_BLOCK 32
#endif #endif
struct ConfigDirective { struct ConfigDirective
{
Array *values; Array *values;
HashMap *children; HashMap *children;
}; };
struct ConfigParseResult { struct ConfigParseResult
{
unsigned int ok:1; unsigned int ok:1;
union { union
{
size_t lineNumber; size_t lineNumber;
HashMap *confMap; HashMap *confMap;
} data; } data;
}; };
typedef enum ConfigToken { typedef enum ConfigToken
{
TOKEN_UNKNOWN, TOKEN_UNKNOWN,
TOKEN_NAME, TOKEN_NAME,
TOKEN_MACRO_ASSIGNMENT, TOKEN_MACRO_ASSIGNMENT,
@ -33,7 +37,8 @@ typedef enum ConfigToken {
TOKEN_EOF TOKEN_EOF
} ConfigToken; } ConfigToken;
typedef struct ConfigParserState { typedef struct ConfigParserState
{
FILE *stream; FILE *stream;
unsigned int line; unsigned int line;
@ -124,6 +129,7 @@ static ConfigParserState *
ConfigParserStateCreate(FILE * stream) ConfigParserStateCreate(FILE * stream)
{ {
ConfigParserState *state = malloc(sizeof(ConfigParserState)); ConfigParserState *state = malloc(sizeof(ConfigParserState));
if (!state) if (!state)
{ {
return NULL; return NULL;
@ -173,6 +179,7 @@ static char
ConfigConsumeWhitespace(ConfigParserState * state) ConfigConsumeWhitespace(ConfigParserState * state)
{ {
int c; int c;
while (isspace(c = fgetc(state->stream))) while (isspace(c = fgetc(state->stream)))
{ {
if (c == '\n') if (c == '\n')
@ -196,10 +203,12 @@ ConfigTokenSeek(ConfigParserState *state)
int c; int c;
/* If we already hit EOF, don't do anything */ /* If we already hit EOF, don't do anything */
if (state->tokenType == TOKEN_EOF) { if (state->tokenType == TOKEN_EOF)
{
return; return;
} }
while ((c = ConfigConsumeWhitespace(state)) == '#') { while ((c = ConfigConsumeWhitespace(state)) == '#')
{
ConfigConsumeLine(state); ConfigConsumeLine(state);
} }
@ -208,26 +217,31 @@ ConfigTokenSeek(ConfigParserState *state)
* token by looking at the next character * token by looking at the next character
*/ */
if (feof(state->stream)) { if (feof(state->stream))
{
state->tokenType = TOKEN_EOF; state->tokenType = TOKEN_EOF;
return; return;
} }
if (ConfigIsNameChar(c)) { if (ConfigIsNameChar(c))
{
state->tokenLen = 0; state->tokenLen = 0;
/* Read the key/macro into state->token */ /* Read the key/macro into state->token */
if (!state->token) { if (!state->token)
{
state->tokenSize = CONFIG_BUFFER_BLOCK; state->tokenSize = CONFIG_BUFFER_BLOCK;
state->token = malloc(CONFIG_BUFFER_BLOCK); state->token = malloc(CONFIG_BUFFER_BLOCK);
} }
state->token[state->tokenLen] = c; state->token[state->tokenLen] = c;
state->tokenLen++; state->tokenLen++;
while (ConfigIsNameChar((c = fgetc(state->stream)))) { while (ConfigIsNameChar((c = fgetc(state->stream))))
{
state->token[state->tokenLen] = c; state->token[state->tokenLen] = c;
state->tokenLen++; state->tokenLen++;
if (state->tokenLen >= state->tokenSize) { if (state->tokenLen >= state->tokenSize)
{
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = realloc(state->token,
state->tokenSize); state->tokenSize);
@ -237,18 +251,25 @@ ConfigTokenSeek(ConfigParserState *state)
state->token[state->tokenLen] = '\0'; state->token[state->tokenLen] = '\0';
state->tokenLen++; state->tokenLen++;
if (!isspace(c)) { if (!isspace(c))
{
state->tokenType = TOKEN_UNKNOWN; state->tokenType = TOKEN_UNKNOWN;
} else { }
else
{
state->tokenType = TOKEN_NAME; state->tokenType = TOKEN_NAME;
if (c == '\n') { if (c == '\n')
{
state->line++; state->line++;
} }
} }
} else { }
switch (c) { else
{
switch (c)
{
case '=': case '=':
state->tokenType = TOKEN_MACRO_ASSIGNMENT; state->tokenType = TOKEN_MACRO_ASSIGNMENT;
break; break;
@ -257,22 +278,26 @@ ConfigTokenSeek(ConfigParserState *state)
state->tokenType = TOKEN_VALUE; state->tokenType = TOKEN_VALUE;
/* read the value into state->curtok */ /* read the value into state->curtok */
while ((c = fgetc(state->stream)) != '"') { while ((c = fgetc(state->stream)) != '"')
if (c == '\n') { {
if (c == '\n')
{
state->line++; state->line++;
} }
/* /*
* End of the stream reached without finding * End of the stream reached without finding
* a closing quote * a closing quote
*/ */
if (feof(state->stream)) { if (feof(state->stream))
{
state->tokenType = TOKEN_EOF; state->tokenType = TOKEN_EOF;
break; break;
} }
state->token[state->tokenLen] = c; state->token[state->tokenLen] = c;
state->tokenLen++; state->tokenLen++;
if (state->tokenLen >= state->tokenSize) { if (state->tokenLen >= state->tokenSize)
{
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = realloc(state->token,
state->tokenSize); state->tokenSize);
@ -293,11 +318,13 @@ ConfigTokenSeek(ConfigParserState *state)
case '$': case '$':
state->tokenLen = 0; state->tokenLen = 0;
/* read the macro name into state->curtok */ /* 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->token[state->tokenLen] = c;
state->tokenLen++; state->tokenLen++;
if (state->tokenLen >= state->tokenSize) { if (state->tokenLen >= state->tokenSize)
{
state->tokenSize += CONFIG_BUFFER_BLOCK; state->tokenSize += CONFIG_BUFFER_BLOCK;
state->token = realloc(state->token, state->token = realloc(state->token,
state->tokenSize); state->tokenSize);
@ -316,7 +343,8 @@ ConfigTokenSeek(ConfigParserState *state)
} }
/* Resize curtok to only use the bytes it needs */ /* Resize curtok to only use the bytes it needs */
if (state->tokenLen) { if (state->tokenLen)
{
state->tokenSize = state->tokenLen; state->tokenSize = state->tokenLen;
state->token = realloc(state->token, state->tokenSize); state->token = realloc(state->token, state->tokenSize);
} }
@ -336,12 +364,15 @@ ConfigParseBlock(ConfigParserState *state, int level)
ConfigTokenSeek(state); ConfigTokenSeek(state);
while (ConfigExpect(state, TOKEN_NAME)) { while (ConfigExpect(state, TOKEN_NAME))
{
char *name = malloc(state->tokenLen + 1); char *name = malloc(state->tokenLen + 1);
strcpy(name, state->token); strcpy(name, state->token);
ConfigTokenSeek(state); ConfigTokenSeek(state);
if (ConfigExpect(state, TOKEN_VALUE) || ConfigExpect(state, TOKEN_MACRO)) { if (ConfigExpect(state, TOKEN_VALUE) || ConfigExpect(state, TOKEN_MACRO))
{
ConfigDirective *directive; ConfigDirective *directive;
directive = malloc(sizeof(ConfigDirective)); directive = malloc(sizeof(ConfigDirective));
@ -349,23 +380,31 @@ ConfigParseBlock(ConfigParserState *state, int level)
directive->values = ArrayCreate(); directive->values = ArrayCreate();
while (ConfigExpect(state, TOKEN_VALUE) || while (ConfigExpect(state, TOKEN_VALUE) ||
ConfigExpect(state, TOKEN_MACRO)) { ConfigExpect(state, TOKEN_MACRO))
{
char *dval; char *dval;
char *dvalCpy; char *dvalCpy;
if (ConfigExpect(state, TOKEN_VALUE)) { if (ConfigExpect(state, TOKEN_VALUE))
{
dval = state->token; dval = state->token;
} else if (ConfigExpect(state, TOKEN_MACRO)) { }
else if (ConfigExpect(state, TOKEN_MACRO))
{
dval = HashMapGet(state->macroMap, state->token); dval = HashMapGet(state->macroMap, state->token);
if (!dval) { if (!dval)
{
goto error; goto error;
} }
} else { }
else
{
dval = NULL; /* Should never happen */ 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); dvalCpy = malloc(strlen(dval) + 1);
strcpy(dvalCpy, dval); strcpy(dvalCpy, dval);
@ -373,10 +412,12 @@ ConfigParseBlock(ConfigParserState *state, int level)
ConfigTokenSeek(state); ConfigTokenSeek(state);
} }
if (ConfigExpect(state, TOKEN_BLOCK_OPEN)) { if (ConfigExpect(state, TOKEN_BLOCK_OPEN))
{
/* token_seek(state); */ /* token_seek(state); */
directive->children = ConfigParseBlock(state, level + 1); directive->children = ConfigParseBlock(state, level + 1);
if (!directive->children) { if (!directive->children)
{
goto error; goto error;
} }
} }
@ -392,36 +433,49 @@ ConfigParseBlock(ConfigParserState *state, int level)
*/ */
ConfigDirectiveFree(HashMapSet(block, name, directive)); ConfigDirectiveFree(HashMapSet(block, name, directive));
} else if (ConfigExpect(state, TOKEN_MACRO_ASSIGNMENT)) { }
else if (ConfigExpect(state, TOKEN_MACRO_ASSIGNMENT))
{
ConfigTokenSeek(state); ConfigTokenSeek(state);
if (ConfigExpect(state, TOKEN_VALUE)) { if (ConfigExpect(state, TOKEN_VALUE))
{
char *valueCopy = malloc(strlen(state->token) + 1); char *valueCopy = malloc(strlen(state->token) + 1);
strcpy(valueCopy, state->token); strcpy(valueCopy, state->token);
free(HashMapSet(state->macroMap, name, state->token)); free(HashMapSet(state->macroMap, name, state->token));
ConfigTokenSeek(state); ConfigTokenSeek(state);
} else { }
else
{
goto error; goto error;
} }
} else { }
else
{
goto error; goto error;
} }
if (!ConfigExpect(state, TOKEN_SEMICOLON)) { if (!ConfigExpect(state, TOKEN_SEMICOLON))
{
goto error; goto error;
} }
ConfigTokenSeek(state); ConfigTokenSeek(state);
} }
if (ConfigExpect(state, level ? TOKEN_BLOCK_CLOSE : TOKEN_EOF)) { if (ConfigExpect(state, level ? TOKEN_BLOCK_CLOSE : TOKEN_EOF))
{
ConfigTokenSeek(state); ConfigTokenSeek(state);
return block; return block;
} else { }
else
{
goto error; goto error;
} }
error: error:
/* Only free the very top level, because this will recurse */ /* Only free the very top level, because this will recurse */
if (!level) { if (!level)
{
ConfigFree(block); ConfigFree(block);
} }
return NULL; return NULL;
@ -438,10 +492,13 @@ ConfigParse(FILE * stream)
state = ConfigParserStateCreate(stream); state = ConfigParserStateCreate(stream);
conf = ConfigParseBlock(state, 0); conf = ConfigParseBlock(state, 0);
if (!conf) { if (!conf)
{
result->ok = 0; result->ok = 0;
result->data.lineNumber = state->line; result->data.lineNumber = state->line;
} else { }
else
{
result->ok = 1; result->ok = 1;
result->data.confMap = conf; result->data.confMap = conf;
} }
@ -449,4 +506,3 @@ ConfigParse(FILE * stream)
ConfigParserStateFree(state); ConfigParserStateFree(state);
return result; return result;
} }

View file

@ -4,12 +4,14 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct HashMapBucket { typedef struct HashMapBucket
{
uint32_t hash; uint32_t hash;
void *value; void *value;
} HashMapBucket; } HashMapBucket;
struct HashMap { struct HashMap
{
size_t count; size_t count;
size_t capacity; size_t capacity;
HashMapBucket **entries; HashMapBucket **entries;
@ -100,6 +102,7 @@ HashMap *
HashMapCreate(void) HashMapCreate(void)
{ {
HashMap *map = malloc(sizeof(HashMap)); HashMap *map = malloc(sizeof(HashMap));
if (!map) if (!map)
{ {
return NULL; return NULL;
@ -288,6 +291,7 @@ HashMapSet(HashMap *map, const char *key, void *value)
if (bucket->hash == hash) if (bucket->hash == hash)
{ {
void *oldValue = bucket->value; void *oldValue = bucket->value;
bucket->value = value; bucket->value = value;
return oldValue; return oldValue;
} }
@ -297,4 +301,3 @@ HashMapSet(HashMap *map, const char *key, void *value)
return NULL; return NULL;
} }

View file

@ -8,7 +8,8 @@
#define LOG_TSBUFFER 64 #define LOG_TSBUFFER 64
struct LogConfig { struct LogConfig
{
LogLevel level; LogLevel level;
size_t indent; size_t indent;
FILE *out; FILE *out;
@ -52,6 +53,7 @@ Log(LogConfig *config, LogLevel level, const char *msg, ...)
if (doColor) if (doColor)
{ {
char *ansi; char *ansi;
switch (level) switch (level)
{ {
case LOG_ERROR: case LOG_ERROR:
@ -140,10 +142,9 @@ Log(LogConfig *config, LogLevel level, const char *msg, ...)
fputc('\n', config->out); fputc('\n', config->out);
va_end(argp); va_end(argp);
/* If we are debugging, there might be something that's /* If we are debugging, there might be something that's going to
* going to segfault the program coming up, so flush the * segfault the program coming up, so flush the output stream
* output stream immediately. * immediately. */
*/
if (config->level == LOG_DEBUG) if (config->level == LOG_DEBUG)
{ {
fflush(config->out); fflush(config->out);

View file

@ -45,7 +45,8 @@ TelodendriaPrintUsage(LogConfig *lc)
Log(lc, LOG_MESSAGE, " -h Print this usage, then exit."); Log(lc, LOG_MESSAGE, " -h Print this usage, then exit.");
} }
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
LogConfig *lc; LogConfig *lc;
int exit = EXIT_SUCCESS; int exit = EXIT_SUCCESS;