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 <stdlib.h>
struct Array {
void **entries; /* An array of void pointers, to store any data */
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 */
};

View file

@ -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] = '=';
}
}
@ -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];
@ -209,4 +219,3 @@ Base64Pad(char **base64Ptr, size_t length)
return newSize;
}

View file

@ -8,20 +8,24 @@
#define CONFIG_BUFFER_BLOCK 32
#endif
struct ConfigDirective {
struct ConfigDirective
{
Array *values;
HashMap *children;
};
struct ConfigParseResult {
struct ConfigParseResult
{
unsigned int ok:1;
union {
union
{
size_t lineNumber;
HashMap *confMap;
} data;
};
typedef enum ConfigToken {
typedef enum ConfigToken
{
TOKEN_UNKNOWN,
TOKEN_NAME,
TOKEN_MACRO_ASSIGNMENT,
@ -33,7 +37,8 @@ typedef enum ConfigToken {
TOKEN_EOF
} ConfigToken;
typedef struct ConfigParserState {
typedef struct ConfigParserState
{
FILE *stream;
unsigned int line;
@ -124,6 +129,7 @@ static ConfigParserState *
ConfigParserStateCreate(FILE * stream)
{
ConfigParserState *state = malloc(sizeof(ConfigParserState));
if (!state)
{
return NULL;
@ -173,6 +179,7 @@ static char
ConfigConsumeWhitespace(ConfigParserState * state)
{
int c;
while (isspace(c = fgetc(state->stream)))
{
if (c == '\n')
@ -196,10 +203,12 @@ 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,26 +217,31 @@ 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);
@ -237,18 +251,25 @@ ConfigTokenSeek(ConfigParserState *state)
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,22 +278,26 @@ 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);
@ -293,11 +318,13 @@ 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);
@ -316,7 +343,8 @@ 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);
}
@ -336,12 +364,15 @@ ConfigParseBlock(ConfigParserState *state, int level)
ConfigTokenSeek(state);
while (ConfigExpect(state, TOKEN_NAME)) {
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 {
}
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)) {
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:
/* 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;
}

View file

@ -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;
@ -100,6 +102,7 @@ HashMap *
HashMapCreate(void)
{
HashMap *map = malloc(sizeof(HashMap));
if (!map)
{
return NULL;
@ -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;
}

View file

@ -8,7 +8,8 @@
#define LOG_TSBUFFER 64
struct LogConfig {
struct LogConfig
{
LogLevel level;
size_t indent;
FILE *out;
@ -52,6 +53,7 @@ Log(LogConfig *config, LogLevel level, const char *msg, ...)
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);

View file

@ -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;