#define _GNU_SOURCE #include #include #include #include #include #include #include #include "../config.h" 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", DIRECTORY, PARTIALS, operand); FILE *f = fopen(partial_path, "r"); if (f == NULL) { printf("Could not open: %s\n", partial_path); return; } unsigned int size = fsize(f); char *partial_content = fcontent(f, size); char *temp_buffer; asprintf(&temp_buffer, "%s", *buffer); free(*buffer); asprintf(buffer, "%.*s%s%s\n", match->offset, temp_buffer, partial_content, temp_buffer + match->offset + match->length); free(temp_buffer); } void handle_contentfor(char **buffer, key_match_t *match, directive_t *directive, list_t *content_headers) { contentfor_operand_t *operand = directive->operands; list_add(content_headers, operand); #ifdef DEBUG printf("CONTENTFOR: %s\n", operand->key); printf("CONTENT: %s\n", operand->content); #endif char *temp_buffer; asprintf(&temp_buffer, "%s", *buffer); free(*buffer); asprintf(buffer, "%.*s%s", match->offset, temp_buffer, temp_buffer + operand->length); free(temp_buffer); free(operand); } void ingest(char **buffer) { key_match_t *match; list_t *content_headers = list_create(sizeof(contentfor_operand_t)); if (content_headers == NULL) { printf("Could not create content_headers\n"); return; } while (true) { match = find_next_key(*buffer); if (match == NULL) break; #ifdef DEBUG 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, content_headers); break; /* TODO: Handle this gracefully */ case BODY: case CONTENT: case ENDCONTENT: case _RAW: break; } if (directive != NULL) free(directive); if (match != NULL) free(match); } for (size_t i = 0; i < content_headers->size; i++) { contentfor_operand_t *op = list_get(content_headers, i); free(op->content); free(op->key); } list_delete(content_headers); }