diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/contentfor.c | 28 | ||||
-rw-r--r-- | src/engine/eachdo.c | 243 | ||||
-rw-r--r-- | src/engine/engine.c | 148 | ||||
-rw-r--r-- | src/engine/include.c | 45 |
4 files changed, 234 insertions, 230 deletions
diff --git a/src/engine/contentfor.c b/src/engine/contentfor.c index f34abf4..f922a3f 100644 --- a/src/engine/contentfor.c +++ b/src/engine/contentfor.c @@ -40,24 +40,24 @@ handle_contentfor(char **buffer, directive_t *directive, list_t *content_headers) { - contentfor_operand_t *operand = directive->operands; - contentfor_operand_t new_operand - = { .content = strdup(operand->content), .key = strdup(operand->key) }; - list_add(content_headers, &new_operand); + contentfor_operand_t *operand = directive->operands; + contentfor_operand_t new_operand + = { .content = strdup(operand->content), .key = strdup(operand->key) }; + list_add(content_headers, &new_operand); #ifdef DEBUG - printf("CONTENTFOR: %s\n", operand->key); - printf("CONTENT: %s\n", operand->content); + printf("CONTENTFOR: %s\n", operand->key); + printf("CONTENT: %s\n", operand->content); #endif - char *temp_buffer = strdup(*buffer); + char *temp_buffer = strdup(*buffer); - free(*buffer); - asprintf(buffer, - "%.*s%s", - match->offset, - temp_buffer, - temp_buffer + strlen(operand->content)); + free(*buffer); + asprintf(buffer, + "%.*s%s", + match->offset, + temp_buffer, + temp_buffer + strlen(operand->content)); - free(temp_buffer); + free(temp_buffer); } diff --git a/src/engine/eachdo.c b/src/engine/eachdo.c index 463ae08..d503256 100644 --- a/src/engine/eachdo.c +++ b/src/engine/eachdo.c @@ -34,7 +34,7 @@ extern msg_t *msg; static int comp(const void *a, const void *b) { - return ((atom_t *) b)->priority - ((atom_t *) a)->priority; + return ((atom_t *) b)->priority - ((atom_t *) a)->priority; } /* @@ -55,42 +55,42 @@ write_eachdo_iteration(list_t *atoms, list_t *values, int priority) { - atom_t *atom = malloc(sizeof(atom_t)); - atom->priority = priority; - atom->content = calloc(1, sizeof(char)); - - for (size_t i = 0; i < directives->size; i++) { - directive_t *_directive = list_get(directives, i); - switch (_directive->type) { - case _RAW: { - atom->content - = realloc(atom->content, - strlen(atom->content) + strlen(_directive->operands) + 1); - strcat(atom->content, _directive->operands); - break; + atom_t *atom = malloc(sizeof(atom_t)); + atom->priority = priority; + atom->content = calloc(1, sizeof(char)); + + for (size_t i = 0; i < directives->size; i++) { + directive_t *_directive = list_get(directives, i); + switch (_directive->type) { + case _RAW: { + atom->content = realloc(atom->content, + strlen(atom->content) + + strlen(_directive->operands) + 1); + strcat(atom->content, _directive->operands); + break; + } + + case PUT: { + char *key = unwrap(list_find_corresponding_value_from_ptr_wrapper( + keys, values, trim(_directive->operands))); + + if (key != NULL) { + atom->content = realloc( + atom->content, strlen(atom->content) + strlen(key) + 1); + strcat(atom->content, key); + } + + break; + } + + default: + /* TODO: Handle this */ + break; + } } - case PUT: { - char *key = unwrap(list_find_corresponding_value_from_ptr_wrapper( - keys, values, trim(_directive->operands))); - - if (key != NULL) { - atom->content - = realloc(atom->content, strlen(atom->content) + strlen(key) + 1); - strcat(atom->content, key); - } - - break; - } - - default: - /* TODO: Handle this */ - break; - } - } - - list_add(atoms, atom); - free(atom); + list_add(atoms, atom); + free(atom); } /* @@ -107,50 +107,50 @@ handle_file_source(list_t *atoms, eachdo_operands_t *operands, list_t *directives) { - char *path; - asprintf(&path, "%s/%s", msg->base_directory, trim(operands->key)); - list_t *files = enumfilesindir(path); - free(path); - - if (files == NULL) { - printf("Could not find key %s\n", trim(operands->key)); - free(operands); - return; - } - - for (size_t i = 0; i < files->size; i++) { - char *file_path = unwrap(list_get(files, i)); - asprintf(&path, - "%s/%s/%s", - msg->base_directory, - trim(operands->key), - file_path); - - int len = strlen(path); - char *comparable = &path[len - strlen("index.html")]; - if (strcmp(comparable, "index.html") == 0) { - free(path); - free(file_path); - continue; - } - - config_t *config = config_fetch_and_parse(path); - - int priority = 0; - char *priority_string - = unwrap(list_find_corresponding_value_from_ptr_wrapper( - config->keys, config->values, "priority")); - if (priority_string != NULL) - priority = atoll(priority_string); + char *path; + asprintf(&path, "%s/%s", msg->base_directory, trim(operands->key)); + list_t *files = enumfilesindir(path); + free(path); - write_eachdo_iteration( - atoms, directives, config->keys, config->values, priority); + if (files == NULL) { + printf("Could not find key %s\n", trim(operands->key)); + free(operands); + return; + } - config_delete(config); - free(file_path); - free(path); - } - list_delete(files); + for (size_t i = 0; i < files->size; i++) { + char *file_path = unwrap(list_get(files, i)); + asprintf(&path, + "%s/%s/%s", + msg->base_directory, + trim(operands->key), + file_path); + + int len = strlen(path); + char *comparable = &path[len - strlen("index.html")]; + if (strcmp(comparable, "index.html") == 0) { + free(path); + free(file_path); + continue; + } + + config_t *config = config_fetch_and_parse(path); + + int priority = 0; + char *priority_string + = unwrap(list_find_corresponding_value_from_ptr_wrapper( + config->keys, config->values, "priority")); + if (priority_string != NULL) + priority = atoll(priority_string); + + write_eachdo_iteration( + atoms, directives, config->keys, config->values, priority); + + config_delete(config); + free(file_path); + free(path); + } + list_delete(files); } /* @@ -165,56 +165,57 @@ handle_file_source(list_t *atoms, void handle_eachdo(char **buffer, key_match_t *match, directive_t *directive) { - eachdo_operands_t *operands = directive->operands; + eachdo_operands_t *operands = directive->operands; - engine_t *engine = engine_ingest(&operands->content); - engine_delete(engine); - list_t *directives = lex(operands->content); + engine_t *engine = engine_ingest(&operands->content); + engine_delete(engine); + list_t *directives = lex(operands->content); - list_t *atoms = list_create(sizeof(atom_t)); + list_t *atoms = list_create(sizeof(atom_t)); - char *content = calloc(1, sizeof(char)); + char *content = calloc(1, sizeof(char)); - if (!strcmp(operands->source, "resources")) - handle_file_source(atoms, operands, directives); - else { - printf("Unknown source: %s\n", operands->source); - /* TODO: handle this gracefully */ - return; - } + if (!strcmp(operands->source, "resources")) + handle_file_source(atoms, operands, directives); + else { + printf("Unknown source: %s\n", operands->source); + /* TODO: handle this gracefully */ + return; + } - /* Sort atoms by priority */ - qsort(atoms->elements, atoms->size, sizeof(atom_t), comp); + /* Sort atoms by priority */ + qsort(atoms->elements, atoms->size, sizeof(atom_t), comp); - for (size_t i = 0; i < atoms->size; i++) { - atom_t *atom = list_get(atoms, i); - content = realloc(content, strlen(content) + strlen(atom->content) + 1); + for (size_t i = 0; i < atoms->size; i++) { + atom_t *atom = list_get(atoms, i); + content + = realloc(content, strlen(content) + strlen(atom->content) + 1); #ifdef DEBUG - printf("PRIORITY: %lu\n", atom->priority); + printf("PRIORITY: %lu\n", atom->priority); #endif - strcat(content, atom->content); - } - - char *temp_buffer = strdup(*buffer); - free(*buffer); - asprintf(buffer, - "%.*s%s%s\n", - match->offset, - temp_buffer, - content, - temp_buffer + operands->length); - free(temp_buffer); - free(content); - - for (size_t i = 0; i < directives->size; i++) { - directive_t *_directive = list_get(directives, i); - free(_directive->operands); - } - list_delete(directives); - - for (size_t i = 0; i < atoms->size; i++) { - atom_t *atom = list_get(atoms, i); - free(atom->content); - } - list_delete(atoms); + strcat(content, atom->content); + } + + char *temp_buffer = strdup(*buffer); + free(*buffer); + asprintf(buffer, + "%.*s%s%s\n", + match->offset, + temp_buffer, + content, + temp_buffer + operands->length); + free(temp_buffer); + free(content); + + for (size_t i = 0; i < directives->size; i++) { + directive_t *_directive = list_get(directives, i); + free(_directive->operands); + } + list_delete(directives); + + for (size_t i = 0; i < atoms->size; i++) { + atom_t *atom = list_get(atoms, i); + free(atom->content); + } + list_delete(atoms); } diff --git a/src/engine/engine.c b/src/engine/engine.c index ecfbc22..0b41c42 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -45,81 +45,83 @@ extern msg_t *msg; engine_t * engine_ingest(char **buffer) { - engine_t *engine = malloc(sizeof(engine_t)); - engine->config = NULL; + engine_t *engine = malloc(sizeof(engine_t)); + engine->config = NULL; - char *p = strstr(*buffer, "---"); - if (p != NULL) { - char *config; - asprintf(&config, "%.*s\n", (int) (p - *buffer), *buffer); - engine->config = config_parse(config); - free(config); + char *p = strstr(*buffer, "---"); + if (p != NULL) { + char *config; + asprintf(&config, "%.*s\n", (int) (p - *buffer), *buffer); + engine->config = config_parse(config); + free(config); - char *tempbuffer = strdup(p); + char *tempbuffer = strdup(p); - free(*buffer); - asprintf(buffer, "%s", tempbuffer + strlen("---")); + free(*buffer); + asprintf(buffer, "%s", tempbuffer + strlen("---")); - free(tempbuffer); - } + free(tempbuffer); + } - key_match_t *match; - engine->content_headers = list_create(sizeof(contentfor_operand_t)); - if (engine->content_headers == NULL) { - printf("Could not create content_headers\n"); - return NULL; - } + key_match_t *match; + engine->content_headers = list_create(sizeof(contentfor_operand_t)); + if (engine->content_headers == NULL) { + printf("Could not create content_headers\n"); + return NULL; + } - size_t skip = 0; - while (true) { - match = find_next_key(*buffer, skip); - if (match == NULL) - break; + size_t skip = 0; + while (true) { + match = find_next_key(*buffer, skip); + if (match == NULL) + break; #ifdef DEBUG - printf("Match: %.*s LENGTH(%d) OFFSET(%d)\n", - match->length, - *buffer + match->offset, - match->length, - match->offset); + printf("Match: %.*s LENGTH(%d) OFFSET(%d)\n", + match->length, + *buffer + match->offset, + match->length, + match->offset); #endif - directive_t *directive = find_directive(*buffer, match); - if (directive == NULL) { - printf( - "Unknown directive: %.*s\n", match->length, *buffer + match->offset); - - break; - } - - switch (directive->type) { - case INCLUDE: - handle_include(buffer, match, directive); - break; - case CONTENTFOR: - handle_contentfor(buffer, match, directive, engine->content_headers); - break; - case EACHDO: - handle_eachdo(buffer, match, directive); - break; - - case PUTPAGE: - /* TODO: handle */ - case PUT: - case ENDEACHDO: - case BODY: - case CONTENT: - case ENDCONTENT: - case _RAW: - skip++; - break; + directive_t *directive = find_directive(*buffer, match); + if (directive == NULL) { + printf("Unknown directive: %.*s\n", + match->length, + *buffer + match->offset); + + break; + } + + switch (directive->type) { + case INCLUDE: + handle_include(buffer, match, directive); + break; + case CONTENTFOR: + handle_contentfor( + buffer, match, directive, engine->content_headers); + break; + case EACHDO: + handle_eachdo(buffer, match, directive); + break; + + case PUTPAGE: + /* TODO: handle */ + case PUT: + case ENDEACHDO: + case BODY: + case CONTENT: + case ENDCONTENT: + case _RAW: + skip++; + break; + } + + directive_delete(directive); + free(match); } - directive_delete(directive); - free(match); - } - - return engine; + return engine; } /* @@ -128,14 +130,14 @@ engine_ingest(char **buffer) void engine_delete(engine_t *engine) { - if (engine->config != NULL) - config_delete(engine->config); - - for (size_t i = 0; i < engine->content_headers->size; i++) { - contentfor_operand_t *operand = list_get(engine->content_headers, i); - free(operand->content); - free(operand->key); - } - list_delete(engine->content_headers); - free(engine); + if (engine->config != NULL) + config_delete(engine->config); + + for (size_t i = 0; i < engine->content_headers->size; i++) { + contentfor_operand_t *operand = list_get(engine->content_headers, i); + free(operand->content); + free(operand->key); + } + list_delete(engine->content_headers); + free(engine); } diff --git a/src/engine/include.c b/src/engine/include.c index 5cb650e..6114f07 100644 --- a/src/engine/include.c +++ b/src/engine/include.c @@ -38,31 +38,32 @@ extern msg_t *msg; void handle_include(char **buffer, key_match_t *match, directive_t *directive) { - char *operand = directive->operands; - char *partial_path; - asprintf(&partial_path, "%s/%s/%s", msg->base_directory, PARTIALS, operand); + char *operand = directive->operands; + char *partial_path; + asprintf( + &partial_path, "%s/%s/%s", msg->base_directory, PARTIALS, operand); - FILE *f = fopen(partial_path, "r"); - if (f == NULL) { - printf("Could not open: %s\n", partial_path); - return; - } - free(partial_path); + FILE *f = fopen(partial_path, "r"); + if (f == NULL) { + printf("Could not open: %s\n", partial_path); + return; + } + free(partial_path); - unsigned int size = fsize(f); - char *partial_content = fcontent(f, size); - fclose(f); + unsigned int size = fsize(f); + char *partial_content = fcontent(f, size); + fclose(f); - char *temp_buffer = strdup(*buffer); + char *temp_buffer = strdup(*buffer); - free(*buffer); - asprintf(buffer, - "%.*s%s%s\n", - match->offset, - temp_buffer, - partial_content, - temp_buffer + match->offset + match->length); + free(*buffer); + asprintf(buffer, + "%.*s%s%s\n", + match->offset, + temp_buffer, + partial_content, + temp_buffer + match->offset + match->length); - free(partial_content); - free(temp_buffer); + free(partial_content); + free(temp_buffer); } |