diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 150 | ||||
-rw-r--r-- | src/copy.c | 82 | ||||
-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 | ||||
-rw-r--r-- | src/filehandler.c | 44 | ||||
-rw-r--r-- | src/lexer.c | 508 | ||||
-rw-r--r-- | src/list.c | 68 | ||||
-rw-r--r-- | src/main.c | 112 | ||||
-rw-r--r-- | src/msg.c | 304 | ||||
-rw-r--r-- | src/template.c | 241 | ||||
-rw-r--r-- | src/util.c | 54 |
13 files changed, 1016 insertions, 1011 deletions
diff --git a/src/config.c b/src/config.c index 0d09bb1..1ca48a4 100644 --- a/src/config.c +++ b/src/config.c @@ -29,97 +29,97 @@ config_t * config_parse(char *content) { - list_t *keys = list_create(sizeof(ptr_wrapper_t)); - list_t *values = list_create(sizeof(ptr_wrapper_t)); - list_t *array_values = list_create(sizeof(ptr_wrapper_t)); - - char *buffer = strdup(content); - /* For free() */ - char *x = buffer; - - char *key = trim(strsep(&buffer, DELIM)); - - while (buffer != NULL) { - buffer = ltrim(buffer); - list_wrap_and_add(keys, strdup(key)); - - if (*buffer == '{') { - buffer++; - list_t *l = list_create(sizeof(ptr_wrapper_t)); - char *raw_array = strsep(&buffer, "}"); - - char *value = strsep(&raw_array, DELIM_ARRAY); - while (value != NULL) { - list_wrap_and_add(l, strdup(trim(value))); - value = strsep(&raw_array, DELIM_ARRAY); - } - - list_wrap_and_add(array_values, l); - list_wrap_and_add(values, NULL); - } else { - char *value = trim(strsep(&buffer, "\n")); - - list_wrap_and_add(array_values, NULL); - list_wrap_and_add(values, strdup(value)); - } + list_t *keys = list_create(sizeof(ptr_wrapper_t)); + list_t *values = list_create(sizeof(ptr_wrapper_t)); + list_t *array_values = list_create(sizeof(ptr_wrapper_t)); + + char *buffer = strdup(content); + /* For free() */ + char *x = buffer; + + char *key = trim(strsep(&buffer, DELIM)); + + while (buffer != NULL) { + buffer = ltrim(buffer); + list_wrap_and_add(keys, strdup(key)); + + if (*buffer == '{') { + buffer++; + list_t *l = list_create(sizeof(ptr_wrapper_t)); + char *raw_array = strsep(&buffer, "}"); - key = trim(strsep(&buffer, DELIM)); - } + char *value = strsep(&raw_array, DELIM_ARRAY); + while (value != NULL) { + list_wrap_and_add(l, strdup(trim(value))); + value = strsep(&raw_array, DELIM_ARRAY); + } - free(x); + list_wrap_and_add(array_values, l); + list_wrap_and_add(values, NULL); + } else { + char *value = trim(strsep(&buffer, "\n")); - config_t *config = malloc(sizeof(config_t)); - config->keys = keys; - config->values = values; - config->array_values = array_values; - return config; + list_wrap_and_add(array_values, NULL); + list_wrap_and_add(values, strdup(value)); + } + + key = trim(strsep(&buffer, DELIM)); + } + + free(x); + + config_t *config = malloc(sizeof(config_t)); + config->keys = keys; + config->values = values; + config->array_values = array_values; + return config; } void config_delete(config_t *config) { - for (size_t i = 0; i < config->keys->size; i++) { - ptr_wrapper_t *wrapper; - - wrapper = list_get(config->keys, i); - if (wrapper->ptr != NULL) - free(wrapper->ptr); - - wrapper = list_get(config->values, i); - if (wrapper->ptr != NULL) - free(wrapper->ptr); - - list_t *l = unwrap(list_get(config->array_values, i)); - if (l != NULL) { - for (size_t y = 0; y < l->size; y++) { - wrapper = list_get(l, y); - free(wrapper->ptr); - } - list_delete(l); + for (size_t i = 0; i < config->keys->size; i++) { + ptr_wrapper_t *wrapper; + + wrapper = list_get(config->keys, i); + if (wrapper->ptr != NULL) + free(wrapper->ptr); + + wrapper = list_get(config->values, i); + if (wrapper->ptr != NULL) + free(wrapper->ptr); + + list_t *l = unwrap(list_get(config->array_values, i)); + if (l != NULL) { + for (size_t y = 0; y < l->size; y++) { + wrapper = list_get(l, y); + free(wrapper->ptr); + } + list_delete(l); + } } - } - list_delete(config->keys); - list_delete(config->values); - list_delete(config->array_values); - free(config); + list_delete(config->keys); + list_delete(config->values); + list_delete(config->array_values); + free(config); } config_t * config_fetch_and_parse(char *path) { - FILE *f = fopen(path, "r"); - if (f == NULL) { - printf("Could not open %s\n", path); - return NULL; - } + FILE *f = fopen(path, "r"); + if (f == NULL) { + printf("Could not open %s\n", path); + return NULL; + } - size_t s = fsize(f); - char *content = fcontent(f, s); - fclose(f); + size_t s = fsize(f); + char *content = fcontent(f, s); + fclose(f); - config_t *config = config_parse(content); - free(content); + config_t *config = config_parse(content); + free(content); - return config; + return config; } @@ -38,53 +38,53 @@ copy_recursively(const char *fpath, int typeflag, struct FTW *ftwbuf) { - (void) sb; - (void) ftwbuf; - - const char *path = fpath + strlen(msg->base_directory) + 1; - char *output_path = NULL; - asprintf(&output_path, "%s/%s", msg->output_directory, path); - - if (typeflag == FTW_D) - goto exit; - - if (typeflag != FTW_F) - goto exit; - - char *temppath = strdup(path); - char *directory; - asprintf(&directory, "%s/%s", msg->output_directory, dirname(temppath)); - char *next = calloc(strlen(directory) + 1, sizeof(char)); - strcpy(next, ""); - - char *token; - for (token = strtok(directory, "/"); token != NULL; - token = strtok(NULL, "/")) { - if (strcmp(next, "") != 0) { - strcat(next, "/"); + (void) sb; + (void) ftwbuf; + + const char *path = fpath + strlen(msg->base_directory) + 1; + char *output_path = NULL; + asprintf(&output_path, "%s/%s", msg->output_directory, path); + + if (typeflag == FTW_D) + goto exit; + + if (typeflag != FTW_F) + goto exit; + + char *temppath = strdup(path); + char *directory; + asprintf(&directory, "%s/%s", msg->output_directory, dirname(temppath)); + char *next = calloc(strlen(directory) + 1, sizeof(char)); + strcpy(next, ""); + + char *token; + for (token = strtok(directory, "/"); token != NULL; + token = strtok(NULL, "/")) { + if (strcmp(next, "") != 0) { + strcat(next, "/"); + } + + strcat(next, token); + mkdir(next, 0700); } - strcat(next, token); - mkdir(next, 0700); - } + free(temppath); + free(directory); + free(next); - free(temppath); - free(directory); - free(next); + FILE *in = fopen(fpath, "r"); + size_t size = fsize(in); + fclose(in); - FILE *in = fopen(fpath, "r"); - size_t size = fsize(in); - fclose(in); + int in_fd = open(fpath, O_RDONLY); + int out_fd = open(output_path, O_WRONLY | O_CREAT, 0700); - int in_fd = open(fpath, O_RDONLY); - int out_fd = open(output_path, O_WRONLY | O_CREAT, 0700); + sendfile(out_fd, in_fd, 0, size); - sendfile(out_fd, in_fd, 0, size); - - close(in_fd); - close(out_fd); + close(in_fd); + close(out_fd); exit: - free(output_path); - return FTW_CONTINUE; + free(output_path); + return FTW_CONTINUE; } 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); } diff --git a/src/filehandler.c b/src/filehandler.c index 642cbab..e634ffb 100644 --- a/src/filehandler.c +++ b/src/filehandler.c @@ -29,44 +29,44 @@ char * fcontent(FILE *f, unsigned int size) { - char *buffer = calloc(size, sizeof(char)); + char *buffer = calloc(size, sizeof(char)); - fseek(f, 0, SEEK_SET); - int bytesread = fread(buffer, sizeof(char), size, f); - if (bytesread < 0) - return NULL; + fseek(f, 0, SEEK_SET); + int bytesread = fread(buffer, sizeof(char), size, f); + if (bytesread < 0) + return NULL; - return buffer; + return buffer; } unsigned int fsize(FILE *f) { - unsigned int current = ftell(f); + unsigned int current = ftell(f); - fseek(f, 0, SEEK_END); - unsigned int s = ftell(f); - fseek(f, current, SEEK_SET); + fseek(f, 0, SEEK_END); + unsigned int s = ftell(f); + fseek(f, current, SEEK_SET); - return s + 1; + return s + 1; } list_t * enumfilesindir(char *path) { - DIR *d; - struct dirent *dir; + DIR *d; + struct dirent *dir; - d = opendir(path); - if (!d) - return NULL; + d = opendir(path); + if (!d) + return NULL; - list_t *l = list_create(sizeof(ptr_wrapper_t *)); + list_t *l = list_create(sizeof(ptr_wrapper_t *)); - while ((dir = readdir(d)) != NULL) - if (dir->d_type == DT_REG) - list_wrap_and_add(l, strdup(dir->d_name)); + while ((dir = readdir(d)) != NULL) + if (dir->d_type == DT_REG) + list_wrap_and_add(l, strdup(dir->d_name)); - closedir(d); - return l; + closedir(d); + return l; } diff --git a/src/lexer.c b/src/lexer.c index be5f8e6..a81d950 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -30,93 +30,93 @@ list_t * lex(char *buffer) { - list_t *directives = list_create(sizeof(directive_t)); - size_t current_offset = 0; + list_t *directives = list_create(sizeof(directive_t)); + size_t current_offset = 0; - while (true) { - key_match_t *match = find_next_key(buffer, 0); - if (match == NULL) - break; + while (true) { + key_match_t *match = find_next_key(buffer, 0); + if (match == NULL) + break; - directive_t *directive = find_directive(buffer, match); - /* TODO: Handle unknown directive */ - if (directive == NULL) - break; + directive_t *directive = find_directive(buffer, match); + /* TODO: Handle unknown directive */ + if (directive == NULL) + break; - current_offset += match->length + match->offset; + current_offset += match->length + match->offset; - if (current_offset != 0) { - char *raw_content = strndup(buffer, match->offset); + if (current_offset != 0) { + char *raw_content = strndup(buffer, match->offset); - directive_t *raw_directive = malloc(sizeof(directive_t)); + directive_t *raw_directive = malloc(sizeof(directive_t)); - raw_directive->type = _RAW; - raw_directive->operands = raw_content; - list_add(directives, raw_directive); + raw_directive->type = _RAW; + raw_directive->operands = raw_content; + list_add(directives, raw_directive); - free(raw_directive); - } + free(raw_directive); + } - buffer += match->offset + match->length; + buffer += match->offset + match->length; - list_add(directives, directive); + list_add(directives, directive); - free(directive); - free(match); - } + free(directive); + free(match); + } - if (strlen(buffer) > 0) { - char *raw_content = strdup(buffer); + if (strlen(buffer) > 0) { + char *raw_content = strdup(buffer); - directive_t *raw_directive = malloc(sizeof(directive_t)); + directive_t *raw_directive = malloc(sizeof(directive_t)); - raw_directive->type = _RAW; - raw_directive->operands = raw_content; - list_add(directives, raw_directive); + raw_directive->type = _RAW; + raw_directive->operands = raw_content; + list_add(directives, raw_directive); - free(raw_directive); - } + free(raw_directive); + } - return directives; + return directives; } key_match_t * find_next_key(char *buffer, size_t skip) { - key_match_t *match = calloc(1, sizeof(key_match_t)); - size_t count = 0; - - for (size_t i = 0; i < strlen(buffer); i++) { - if (buffer[i] == '{' && buffer[i + 1] == '{') { - count++; - - if (count > skip) { - match->offset = i; - break; - } - } - - if (i == strlen(buffer) - 1) { - free(match); - return NULL; - } - } - - char *subbuffer = buffer + match->offset; - for (size_t i = 0; i < strlen(subbuffer); i++) { - if (subbuffer[i] == '}' && subbuffer[i + 1] == '}') { - match->length = i + 2; - break; + key_match_t *match = calloc(1, sizeof(key_match_t)); + size_t count = 0; + + for (size_t i = 0; i < strlen(buffer); i++) { + if (buffer[i] == '{' && buffer[i + 1] == '{') { + count++; + + if (count > skip) { + match->offset = i; + break; + } + } + + if (i == strlen(buffer) - 1) { + free(match); + return NULL; + } } - if (i == strlen(buffer) - 1) { - printf("Unterminated Key\n"); - free(match); - return NULL; + char *subbuffer = buffer + match->offset; + for (size_t i = 0; i < strlen(subbuffer); i++) { + if (subbuffer[i] == '}' && subbuffer[i + 1] == '}') { + match->length = i + 2; + break; + } + + if (i == strlen(buffer) - 1) { + printf("Unterminated Key\n"); + free(match); + return NULL; + } } - } - return match; + return match; } void @@ -125,17 +125,17 @@ lexer_handle_include(directive_t *directive, char *buffer, size_t n) { - directive->type = INCLUDE; - - char *operand = NULL; - for (size_t i = n + strlen("include"); i < match->length - n; i++) - if (isalnum(buffer[i])) { - sscanf(buffer + i, "%ms\"", &operand); - operand[strlen(operand) - 1] = '\0'; - break; - } + directive->type = INCLUDE; + + char *operand = NULL; + for (size_t i = n + strlen("include"); i < match->length - n; i++) + if (isalnum(buffer[i])) { + sscanf(buffer + i, "%ms\"", &operand); + operand[strlen(operand) - 1] = '\0'; + break; + } - directive->operands = operand; + directive->operands = operand; } void @@ -145,55 +145,55 @@ lexer_handle_contentfor(directive_t *directive, char *content, size_t n) { - directive->type = CONTENTFOR; - directive->operands = NULL; - contentfor_operand_t *operands = malloc(sizeof(contentfor_operand_t)); - - for (size_t i = n + strlen("contentfor"); i < match->length; i++) - if (isalnum(buffer[i])) { - sscanf(buffer + i, "%ms\"", &operands->key); - operands->key[strlen(operands->key) - 1] = '\0'; - break; - } - - buffer = content + match->length + match->offset; - - key_match_t *new_match; - - while (true) { - new_match = find_next_key(buffer, 0); - if (new_match == NULL) { - printf("Cannot find endcontent\n"); - free(new_match); - directive_delete(directive); - /* TODO: Handle early returns */ - return; - } - - directive_t *new_directive = find_directive(buffer, new_match); - if (new_directive == NULL) { - printf("Cannot find directive: %.*s\n", - new_match->length, - buffer + new_match->offset); - directive_delete(new_directive); - free(new_match); - directive_delete(directive); - return; - } - - if (new_directive->type == ENDCONTENT) { - directive_delete(new_directive); - break; + directive->type = CONTENTFOR; + directive->operands = NULL; + contentfor_operand_t *operands = malloc(sizeof(contentfor_operand_t)); + + for (size_t i = n + strlen("contentfor"); i < match->length; i++) + if (isalnum(buffer[i])) { + sscanf(buffer + i, "%ms\"", &operands->key); + operands->key[strlen(operands->key) - 1] = '\0'; + break; + } + + buffer = content + match->length + match->offset; + + key_match_t *new_match; + + while (true) { + new_match = find_next_key(buffer, 0); + if (new_match == NULL) { + printf("Cannot find endcontent\n"); + free(new_match); + directive_delete(directive); + /* TODO: Handle early returns */ + return; + } + + directive_t *new_directive = find_directive(buffer, new_match); + if (new_directive == NULL) { + printf("Cannot find directive: %.*s\n", + new_match->length, + buffer + new_match->offset); + directive_delete(new_directive); + free(new_match); + directive_delete(directive); + return; + } + + if (new_directive->type == ENDCONTENT) { + directive_delete(new_directive); + break; + } + + directive_delete(new_directive); + free(new_match); } - directive_delete(new_directive); + operands->content = strndup(buffer, new_match->offset); free(new_match); - } - operands->content = strndup(buffer, new_match->offset); - free(new_match); - - directive->operands = operands; + directive->operands = operands; } void @@ -202,65 +202,65 @@ lexer_handle_eachdo(directive_t *directive, char *buffer, size_t n) { - directive->type = EACHDO; - eachdo_operands_t *operands = malloc(sizeof(eachdo_operands_t)); - - char *subbuffer = strndup(buffer + n + strlen("eachdo"), - match->length - n - strlen("eachdo") - 2); - char *original_subbuffer = subbuffer; - - char *source = trim(strsep(&subbuffer, ".")); - if (subbuffer == NULL) { - printf("Failed to split eachdo operands by .\n"); - return; - } - - operands->key = strdup(subbuffer); - operands->source = strdup(source); - - free(original_subbuffer); - - buffer += match->length; - key_match_t *new_match; - size_t skip = 0; - - while (true) { - new_match = find_next_key(buffer, skip); - if (new_match == NULL) { - printf("Cannot find endeachdo\n"); - free(new_match); - directive_delete(directive); - /* TODO: Handle early returns */ - return; - } + directive->type = EACHDO; + eachdo_operands_t *operands = malloc(sizeof(eachdo_operands_t)); + + char *subbuffer = strndup(buffer + n + strlen("eachdo"), + match->length - n - strlen("eachdo") - 2); + char *original_subbuffer = subbuffer; - directive_t *new_directive = find_directive(buffer, new_match); - if (new_directive == NULL) { - printf("Cannot find directive: %.*s\n", - new_match->length, - buffer + new_match->offset); - free(new_match); - directive_delete(directive); - return; + char *source = trim(strsep(&subbuffer, ".")); + if (subbuffer == NULL) { + printf("Failed to split eachdo operands by .\n"); + return; } - if (new_directive->type == ENDEACHDO) { - directive_delete(new_directive); - break; + operands->key = strdup(subbuffer); + operands->source = strdup(source); + + free(original_subbuffer); + + buffer += match->length; + key_match_t *new_match; + size_t skip = 0; + + while (true) { + new_match = find_next_key(buffer, skip); + if (new_match == NULL) { + printf("Cannot find endeachdo\n"); + free(new_match); + directive_delete(directive); + /* TODO: Handle early returns */ + return; + } + + directive_t *new_directive = find_directive(buffer, new_match); + if (new_directive == NULL) { + printf("Cannot find directive: %.*s\n", + new_match->length, + buffer + new_match->offset); + free(new_match); + directive_delete(directive); + return; + } + + if (new_directive->type == ENDEACHDO) { + directive_delete(new_directive); + break; + } + + directive_delete(new_directive); + free(new_match); + + skip++; } - directive_delete(new_directive); + operands->content = strndup(buffer, new_match->offset); + operands->length = match->offset + match->length + new_match->offset + + new_match->length; free(new_match); - skip++; - } - - operands->content = strndup(buffer, new_match->offset); - operands->length - = match->offset + match->length + new_match->offset + new_match->length; - free(new_match); - - directive->operands = operands; + directive->operands = operands; } void @@ -269,11 +269,11 @@ lexer_handle_put(directive_t *directive, char *buffer, size_t n) { - directive->type = PUT; + directive->type = PUT; - /* TODO: Use this for include and contentfor too instead of sscanf() */ - directive->operands = strndup(buffer + n + strlen("put"), - match->length - n - strlen("put") - 2); + /* TODO: Use this for include and contentfor too instead of sscanf() */ + directive->operands = strndup(buffer + n + strlen("put"), + match->length - n - strlen("put") - 2); } void @@ -282,9 +282,9 @@ lexer_handle_putpage(directive_t *directive, char *buffer, size_t n) { - directive->type = PUTPAGE; - directive->operands = strndup(buffer + n + strlen("putpage"), - match->length - n - strlen("putpage") - 2); + directive->type = PUTPAGE; + directive->operands = strndup(buffer + n + strlen("putpage"), + match->length - n - strlen("putpage") - 2); } void @@ -293,105 +293,105 @@ lexer_handle_content(directive_t *directive, char *buffer, size_t n) { - directive->type = CONTENT; - - char *operand = NULL; - for (size_t i = n + strlen("content"); i < match->length - n; i++) - if (isalnum(buffer[i])) { - sscanf(buffer + i, "%ms\"", &operand); - operand[strlen(operand) - 1] = '\0'; - break; - } + directive->type = CONTENT; - directive->operands = operand; + char *operand = NULL; + for (size_t i = n + strlen("content"); i < match->length - n; i++) + if (isalnum(buffer[i])) { + sscanf(buffer + i, "%ms\"", &operand); + operand[strlen(operand) - 1] = '\0'; + break; + } + + directive->operands = operand; } directive_t * find_directive(char *content, key_match_t *match) { - directive_t *directive; + directive_t *directive; - char *buffer = content + match->offset; - size_t n = 0; + char *buffer = content + match->offset; + size_t n = 0; - for (size_t i = 0; i < match->length; i++) - if (isspace(buffer[i]) || buffer[i] == '{') - n++; - else - goto found_start; + for (size_t i = 0; i < match->length; i++) + if (isspace(buffer[i]) || buffer[i] == '{') + n++; + else + goto found_start; - return NULL; + return NULL; found_start: - directive = (directive_t *) calloc(1, sizeof(directive_t)); + directive = (directive_t *) calloc(1, sizeof(directive_t)); + + if (DIRECTIVE_IS("endcontent")) { + directive->type = ENDCONTENT; + directive->operands = NULL; + } else if (DIRECTIVE_IS("endeachdo")) { + directive->type = ENDEACHDO; + directive->operands = NULL; + } else if (DIRECTIVE_IS("body")) { + directive->type = BODY; + directive->operands = NULL; + } else if (DIRECTIVE_IS("include")) { + lexer_handle_include(directive, match, buffer, n); + } else if (DIRECTIVE_IS("contentfor")) { + lexer_handle_contentfor(directive, match, buffer, content, n); + } else if (DIRECTIVE_IS("content")) { + lexer_handle_content(directive, match, buffer, n); + } else if (DIRECTIVE_IS("eachdo")) { + lexer_handle_eachdo(directive, match, buffer, n); + } else if (DIRECTIVE_IS("putpage")) { + lexer_handle_putpage(directive, match, buffer, n); + } else if (DIRECTIVE_IS("put")) { + lexer_handle_put(directive, match, buffer, n); + } else { + directive_delete(directive); + directive = NULL; + } - if (DIRECTIVE_IS("endcontent")) { - directive->type = ENDCONTENT; - directive->operands = NULL; - } else if (DIRECTIVE_IS("endeachdo")) { - directive->type = ENDEACHDO; - directive->operands = NULL; - } else if (DIRECTIVE_IS("body")) { - directive->type = BODY; - directive->operands = NULL; - } else if (DIRECTIVE_IS("include")) { - lexer_handle_include(directive, match, buffer, n); - } else if (DIRECTIVE_IS("contentfor")) { - lexer_handle_contentfor(directive, match, buffer, content, n); - } else if (DIRECTIVE_IS("content")) { - lexer_handle_content(directive, match, buffer, n); - } else if (DIRECTIVE_IS("eachdo")) { - lexer_handle_eachdo(directive, match, buffer, n); - } else if (DIRECTIVE_IS("putpage")) { - lexer_handle_putpage(directive, match, buffer, n); - } else if (DIRECTIVE_IS("put")) { - lexer_handle_put(directive, match, buffer, n); - } else { - directive_delete(directive); - directive = NULL; - } - - return directive; + return directive; } char * find_contentfor_value(list_t *content_headers, char *key) { - for (size_t i = 0; i < content_headers->size; i++) { - contentfor_operand_t *operand = list_get(content_headers, i); + for (size_t i = 0; i < content_headers->size; i++) { + contentfor_operand_t *operand = list_get(content_headers, i); - if (strcmp(key, operand->key) == 0) - return operand->content; - } + if (strcmp(key, operand->key) == 0) + return operand->content; + } - return NULL; + return NULL; } void directive_delete(directive_t *directive) { - switch (directive->type) { - case EACHDO: { - eachdo_operands_t *operands = directive->operands; - free(operands->content); - free(operands->key); - free(operands->source); - free(operands); - break; - } - case CONTENTFOR: { - contentfor_operand_t *operands = directive->operands; - free(operands->content); - free(operands->key); - free(operands); - break; - } - - default: - if (directive->operands != NULL) - free(directive->operands); - break; - } - - free(directive); + switch (directive->type) { + case EACHDO: { + eachdo_operands_t *operands = directive->operands; + free(operands->content); + free(operands->key); + free(operands->source); + free(operands); + break; + } + case CONTENTFOR: { + contentfor_operand_t *operands = directive->operands; + free(operands->content); + free(operands->key); + free(operands); + break; + } + + default: + if (directive->operands != NULL) + free(directive->operands); + break; + } + + free(directive); } @@ -27,61 +27,61 @@ list_t * list_create(size_t element_size) { - list_t *list = malloc(sizeof(list_t)); - if (list == NULL) - return NULL; + list_t *list = malloc(sizeof(list_t)); + if (list == NULL) + return NULL; - list->element_size = element_size; - list->size = 0; - list->max = START_SIZE; - list->elements = (uint8_t *) calloc(list->max, list->element_size); + list->element_size = element_size; + list->size = 0; + list->max = START_SIZE; + list->elements = (uint8_t *) calloc(list->max, list->element_size); - return list; + return list; } void list_add(list_t *list, void *element) { - if (list->size == list->max) { - list->max += INCREMENT_BY; - list->elements - = (uint8_t *) realloc(list->elements, list->element_size * list->max); - if (list->elements == NULL) { - /* TODO: Handle error */ - printf("Failed to reallocate array\n"); - return; + if (list->size == list->max) { + list->max += INCREMENT_BY; + list->elements = (uint8_t *) realloc(list->elements, + list->element_size * list->max); + if (list->elements == NULL) { + /* TODO: Handle error */ + printf("Failed to reallocate array\n"); + return; + } } - } - void *new_element = list->elements + list->element_size * list->size; - new_element = memcpy(new_element, element, list->element_size); + void *new_element = list->elements + list->element_size * list->size; + new_element = memcpy(new_element, element, list->element_size); - if (new_element == NULL) - printf("Failed to add a new element\n"); + if (new_element == NULL) + printf("Failed to add a new element\n"); - list->size++; + list->size++; } void list_wrap_and_add(list_t *list, void *element) { - ptr_wrapper_t *wrapped = wrap_ptr(element); - list_add(list, wrapped); - free(wrapped); + ptr_wrapper_t *wrapped = wrap_ptr(element); + list_add(list, wrapped); + free(wrapped); } void * list_get(list_t *list, size_t i) { - return list->elements + (i * list->element_size); + return list->elements + (i * list->element_size); } void list_delete(list_t *list) { - free(list->elements); - free(list); + free(list->elements); + free(list); } void * @@ -89,12 +89,12 @@ list_find_corresponding_value_from_ptr_wrapper(list_t *keys, list_t *values, char *key) { - for (size_t i = 0; i < keys->size; i++) { - ptr_wrapper_t *wrapper = list_get(keys, i); - if (strcmp(wrapper->ptr, key) == 0) { - return list_get(values, i); + for (size_t i = 0; i < keys->size; i++) { + ptr_wrapper_t *wrapper = list_get(keys, i); + if (strcmp(wrapper->ptr, key) == 0) { + return list_get(values, i); + } } - } - return NULL; + return NULL; } @@ -35,84 +35,84 @@ bool regen = false; void signal_handler(int x) { - (void) x; - stop = true; + (void) x; + stop = true; } void usage(char *program) { - printf("Usage: %s [-h] [-r] [-v] [-o <output>] <directory>\n" - "\t-h : Help\n" - "\t-v : Verbose\n" - "\t-r : Regenerate every 500ms\n" - "\t-o <output>: Output directory\n" - "\t<directory>: Working directory\n", - program); + printf("Usage: %s [-h] [-r] [-v] [-o <output>] <directory>\n" + "\t-h : Help\n" + "\t-v : Verbose\n" + "\t-r : Regenerate every 500ms\n" + "\t-o <output>: Output directory\n" + "\t<directory>: Working directory\n", + program); } void config(void) { - printf("Base Directory: %s\n" - "Output Directory: %s\n" - "Verbose: %s\n" - "Regenerating: %s\n\n", - msg->base_directory, - msg->output_directory, - msg->verbose ? "true" : "false", - regen ? "true" : "false"); + printf("Base Directory: %s\n" + "Output Directory: %s\n" + "Verbose: %s\n" + "Regenerating: %s\n\n", + msg->base_directory, + msg->output_directory, + msg->verbose ? "true" : "false", + regen ? "true" : "false"); } int main(int argc, char **argv) { - printf("msg: The Minimal Static Site Generator\n\n"); + printf("msg: The Minimal Static Site Generator\n\n"); - int opt; - msg = malloc(sizeof(msg_t)); - msg->base_directory = "."; - msg->output_directory = "dist"; - msg->verbose = false; + int opt; + msg = malloc(sizeof(msg_t)); + msg->base_directory = "."; + msg->output_directory = "dist"; + msg->verbose = false; - while ((opt = getopt(argc, argv, "o:hvr")) != -1) { - switch (opt) { - case 'o': - msg->output_directory = optarg; - break; - case 'r': - regen = true; - break; - case 'v': - msg->verbose = true; - break; - case 'h': - default: - usage(argv[0]); - return EXIT_SUCCESS; + while ((opt = getopt(argc, argv, "o:hvr")) != -1) { + switch (opt) { + case 'o': + msg->output_directory = optarg; + break; + case 'r': + regen = true; + break; + case 'v': + msg->verbose = true; + break; + case 'h': + default: + usage(argv[0]); + return EXIT_SUCCESS; + } } - } - if (optind < argc) - msg->base_directory = argv[optind]; + if (optind < argc) + msg->base_directory = argv[optind]; - config(); + config(); - int r = run(true); - if (!regen || r != EXIT_SUCCESS) { - free(msg); - return r; - } + int r = run(true); + if (!regen || r != EXIT_SUCCESS) { + free(msg); + return r; + } - signal(SIGINT, signal_handler); + signal(SIGINT, signal_handler); - while (!stop) { - printf("."); - fflush(stdout); - r = run(false); - msleep(500); - } + while (!stop) { + printf("."); + fflush(stdout); + r = run(false); + msleep(500); + } - free(msg); - return r; + free(msg); + return r; } @@ -44,178 +44,178 @@ extern msg_t *msg; void handle_file(const char *path) { - char *inpath; - char *outpath; - - asprintf(&inpath, "%s/%s", msg->base_directory, path); - - char *dot = strrchr(inpath, '.'); - if (dot && strcmp(dot, ".md") == 0) { - asprintf(&outpath, - "%s/%.*s.html", - msg->output_directory, - (int) strlen(path) - 3, - path); - } else { - asprintf(&outpath, "%s/%s", msg->output_directory, path); - } - - char *temp_outpath = strdup(outpath); - char *directory = dirname(temp_outpath); - char *next = calloc(strlen(directory) + 1, sizeof(char)); - strcpy(next, ""); - - char *token; - for (token = strtok(directory, "/"); token != NULL; - token = strtok(NULL, "/")) { - if (strcmp(next, "") != 0) { - strcat(next, "/"); + char *inpath; + char *outpath; + + asprintf(&inpath, "%s/%s", msg->base_directory, path); + + char *dot = strrchr(inpath, '.'); + if (dot && strcmp(dot, ".md") == 0) { + asprintf(&outpath, + "%s/%.*s.html", + msg->output_directory, + (int) strlen(path) - 3, + path); + } else { + asprintf(&outpath, "%s/%s", msg->output_directory, path); } - strcat(next, token); - mkdir(next, 0700); - } + char *temp_outpath = strdup(outpath); + char *directory = dirname(temp_outpath); + char *next = calloc(strlen(directory) + 1, sizeof(char)); + strcpy(next, ""); - free(next); - free(temp_outpath); + char *token; + for (token = strtok(directory, "/"); token != NULL; + token = strtok(NULL, "/")) { + if (strcmp(next, "") != 0) { + strcat(next, "/"); + } - FILE *in = fopen(inpath, "r"); - if (in == NULL) { - printf("Failed to open %s\n", inpath); - return; - } - - FILE *out = fopen(outpath, "w"); - if (out == NULL) { - printf("Failed to open %s\n", outpath); - fclose(in); - return; - } - - unsigned int size = fsize(in); - char *buffer = fcontent(in, size); - - if (dot && strcmp(dot, ".md") == 0) { - engine_t engine = { .config = NULL, .content_headers = 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 *tempbuffer = strdup(p); + strcat(next, token); + mkdir(next, 0700); + } - free(buffer); - asprintf(&buffer, "%s", tempbuffer + strlen("---")); + free(next); + free(temp_outpath); - free(tempbuffer); + FILE *in = fopen(inpath, "r"); + if (in == NULL) { + printf("Failed to open %s\n", inpath); + return; } - mkd_flag_t *flags = mkd_flags(); - mkd_set_flag_num(flags, MKD_FENCEDCODE); - MMIOT *doc = mkd_string(buffer, strlen(buffer), flags); - free(flags); - template_write(&engine, out, doc, true); + FILE *out = fopen(outpath, "w"); + if (out == NULL) { + printf("Failed to open %s\n", outpath); + fclose(in); + return; + } - if (engine.config != NULL) - config_delete(engine.config); - } else if (strlen(buffer) != 0) { - engine_t *engine = engine_ingest(&buffer); - template_write(engine, out, buffer, false); - engine_delete(engine); - } + unsigned int size = fsize(in); + char *buffer = fcontent(in, size); + + if (dot && strcmp(dot, ".md") == 0) { + engine_t engine = { .config = NULL, .content_headers = 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 *tempbuffer = strdup(p); + + free(buffer); + asprintf(&buffer, "%s", tempbuffer + strlen("---")); + + free(tempbuffer); + } + + mkd_flag_t *flags = mkd_flags(); + mkd_set_flag_num(flags, MKD_FENCEDCODE); + MMIOT *doc = mkd_string(buffer, strlen(buffer), flags); + free(flags); + template_write(&engine, out, doc, true); + + if (engine.config != NULL) + config_delete(engine.config); + } else if (strlen(buffer) != 0) { + engine_t *engine = engine_ingest(&buffer); + template_write(engine, out, buffer, false); + engine_delete(engine); + } - free(buffer); + free(buffer); - fclose(in); - fclose(out); + fclose(in); + fclose(out); - free(inpath); - free(outpath); + free(inpath); + free(outpath); } int run(bool log) { - if (log) { - time_t rawtime; - struct tm *timeinfo; - time(&rawtime); - timeinfo = localtime(&rawtime); - printf("Generation started at: %s", asctime(timeinfo)); - } - - struct stat sb; - if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) { - printf("%s does not exist.\n", msg->base_directory); - return EXIT_FAILURE; - } - - char *config_path; - asprintf(&config_path, "%s/%s", msg->base_directory, CONFIG_FILE); - config_t *config = config_fetch_and_parse(config_path); - free(config_path); - - if (config == NULL) - return EXIT_FAILURE; - - template_initialize(); - - int err = mkdir(msg->output_directory, 0700); - if (err != 0 && errno != EEXIST) { - perror("mkdir"); - return EXIT_FAILURE; - } - - list_t *static_ = unwrap(list_find_corresponding_value_from_ptr_wrapper( - config->keys, config->array_values, "static")); - - if (static_ == NULL) { - printf("Could not find resources in config.cfg\n"); - return EXIT_FAILURE; - } - - for (size_t i = 0; i < static_->size; i++) { - ptr_wrapper_t *value = list_get(static_, i); - char *path = NULL; - asprintf(&path, "%s/%s", msg->base_directory, (char *) value->ptr); - - struct stat path_stat; - stat(path, &path_stat); - - /* TODO: Error handling */ - if (S_ISREG(path_stat.st_mode)) - copy_recursively(path, NULL, FTW_F, NULL); - else if (S_ISDIR(path_stat.st_mode)) - nftw(path, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL); - - free(path); - } - - list_t *resources = unwrap(list_find_corresponding_value_from_ptr_wrapper( - config->keys, config->array_values, "resources")); - - if (resources == NULL) { - printf("Could not find resources in config.cfg\n"); - return EXIT_FAILURE; - } - - for (size_t i = 0; i < resources->size; i++) { - ptr_wrapper_t *value = list_get(resources, i); - char *path = value->ptr; if (log) { - if (i < LOG_THRESHOLD || msg->verbose) - printf("\tProcessing %s\n", path); - else if (i == LOG_THRESHOLD && !msg->verbose) - printf("\t...\n"); + time_t rawtime; + struct tm *timeinfo; + time(&rawtime); + timeinfo = localtime(&rawtime); + printf("Generation started at: %s", asctime(timeinfo)); + } + + struct stat sb; + if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) { + printf("%s does not exist.\n", msg->base_directory); + return EXIT_FAILURE; + } + + char *config_path; + asprintf(&config_path, "%s/%s", msg->base_directory, CONFIG_FILE); + config_t *config = config_fetch_and_parse(config_path); + free(config_path); + + if (config == NULL) + return EXIT_FAILURE; + + template_initialize(); + + int err = mkdir(msg->output_directory, 0700); + if (err != 0 && errno != EEXIST) { + perror("mkdir"); + return EXIT_FAILURE; + } + + list_t *static_ = unwrap(list_find_corresponding_value_from_ptr_wrapper( + config->keys, config->array_values, "static")); + + if (static_ == NULL) { + printf("Could not find resources in config.cfg\n"); + return EXIT_FAILURE; + } + + for (size_t i = 0; i < static_->size; i++) { + ptr_wrapper_t *value = list_get(static_, i); + char *path = NULL; + asprintf(&path, "%s/%s", msg->base_directory, (char *) value->ptr); + + struct stat path_stat; + stat(path, &path_stat); + + /* TODO: Error handling */ + if (S_ISREG(path_stat.st_mode)) + copy_recursively(path, NULL, FTW_F, NULL); + else if (S_ISDIR(path_stat.st_mode)) + nftw(path, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL); + + free(path); } - handle_file(path); - } + list_t *resources = unwrap(list_find_corresponding_value_from_ptr_wrapper( + config->keys, config->array_values, "resources")); + + if (resources == NULL) { + printf("Could not find resources in config.cfg\n"); + return EXIT_FAILURE; + } + + for (size_t i = 0; i < resources->size; i++) { + ptr_wrapper_t *value = list_get(resources, i); + char *path = value->ptr; + if (log) { + if (i < LOG_THRESHOLD || msg->verbose) + printf("\tProcessing %s\n", path); + else if (i == LOG_THRESHOLD && !msg->verbose) + printf("\t...\n"); + } + + handle_file(path); + } - template_clean(); - config_delete(config); + template_clean(); + config_delete(config); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/src/template.c b/src/template.c index 58d8ac6..a522475 100644 --- a/src/template.c +++ b/src/template.c @@ -40,165 +40,166 @@ list_t *templates; static void delete_components(template_t *template) { - for (size_t i = 0; i < template->components->size; i++) { - directive_t *directive = list_get(template->components, i); - switch (directive->type) { - case _RAW: - case CONTENT: - case PUTPAGE: - free(directive->operands); - break; - - default: - break; + for (size_t i = 0; i < template->components->size; i++) { + directive_t *directive = list_get(template->components, i); + switch (directive->type) { + case _RAW: + case CONTENT: + case PUTPAGE: + free(directive->operands); + break; + + default: + break; + } } - } - list_delete(template->components); + list_delete(template->components); } void template_initialize(void) { - keys = list_create(sizeof(ptr_wrapper_t)); - templates = list_create(sizeof(template_t)); + keys = list_create(sizeof(ptr_wrapper_t)); + templates = list_create(sizeof(template_t)); - char *template_directory; - asprintf(&template_directory, "%s/%s", msg->base_directory, TEMPLATES); - DIR *dir = opendir(template_directory); + char *template_directory; + asprintf(&template_directory, "%s/%s", msg->base_directory, TEMPLATES); + DIR *dir = opendir(template_directory); - if (dir == NULL) { - printf("Could not open %s\n", template_directory); - return; - } - free(template_directory); + if (dir == NULL) { + printf("Could not open %s\n", template_directory); + return; + } + free(template_directory); - struct dirent *f; - while ((f = readdir(dir)) != NULL) { - if (f->d_type != DT_REG) - continue; + struct dirent *f; + while ((f = readdir(dir)) != NULL) { + if (f->d_type != DT_REG) + continue; - template_t *t = template_create(f->d_name); - list_wrap_and_add(keys, strdup(f->d_name)); - list_add(templates, t); + template_t *t = template_create(f->d_name); + list_wrap_and_add(keys, strdup(f->d_name)); + list_add(templates, t); - free(t); - } + free(t); + } - closedir(dir); + closedir(dir); } void template_clean(void) { - for (size_t i = 0; i < keys->size; i++) { - template_t *template = list_get(templates, i); - delete_components(template); + for (size_t i = 0; i < keys->size; i++) { + template_t *template = list_get(templates, i); + delete_components(template); - ptr_wrapper_t *wrapper = list_get(keys, i); - free(wrapper->ptr); - } + ptr_wrapper_t *wrapper = list_get(keys, i); + free(wrapper->ptr); + } - list_delete(keys); - list_delete(templates); + list_delete(keys); + list_delete(templates); } template_t * template_create(char *template_name) { - template_t *template = malloc(sizeof(template_t)); + template_t *template = malloc(sizeof(template_t)); - char *path; - asprintf(&path, "%s/%s/%s", msg->base_directory, TEMPLATES, template_name); - FILE *base = fopen(path, "r"); - free(path); + char *path; + asprintf(&path, "%s/%s/%s", msg->base_directory, TEMPLATES, template_name); + FILE *base = fopen(path, "r"); + free(path); - unsigned int size = fsize(base); - char *buffer = fcontent(base, size); - fclose(base); + unsigned int size = fsize(base); + char *buffer = fcontent(base, size); + fclose(base); - engine_t *engine = engine_ingest(&buffer); - engine_delete(engine); - template->components = lex(buffer); + engine_t *engine = engine_ingest(&buffer); + engine_delete(engine); + template->components = lex(buffer); - free(buffer); - return template; + free(buffer); + return template; } void template_delete(template_t *template) { - delete_components(template); - free(template); + delete_components(template); + free(template); } void template_write(engine_t *engine, FILE *f, void *doc, bool is_markdown) { - template_t *template = list_find_corresponding_value_from_ptr_wrapper( - keys, templates, "base.html"); - - if (engine != NULL && engine->config != NULL) { - char *template_name - = unwrap(list_find_corresponding_value_from_ptr_wrapper( - engine->config->keys, engine->config->values, "template")); - - if (template_name != NULL) { - template = list_find_corresponding_value_from_ptr_wrapper( - keys, templates, template_name); - - if (template == NULL) { - printf("Could not find template %s\n", template_name); - return; - } - } - } - - for (size_t i = 0; i < template->components->size; i++) { - directive_t *directive = list_get(template->components, i); - - switch (directive->type) { - case _RAW: - fprintf(f, "%s", (char *) directive->operands); - break; - - case CONTENT: { - /* TODO: handle this gracefully */ - if (!is_markdown) { - char *content = find_contentfor_value(engine->content_headers, - directive->operands); - fprintf(f, "%s", content); - } - break; - } - - case BODY: { - if (is_markdown) { - - mkd_flag_t *flags = mkd_flags(); - mkd_set_flag_num(flags, MKD_FENCEDCODE); - markdown(doc, f, flags); - free(flags); - } else { - fprintf(f, "%s", (char *) doc); - } - - break; - } - - case PUTPAGE: { - - char *content = unwrap(list_find_corresponding_value_from_ptr_wrapper( - engine->config->keys, - engine->config->values, - trim(directive->operands))); - fprintf(f, "%s", content); - - break; + template_t *template = list_find_corresponding_value_from_ptr_wrapper( + keys, templates, "base.html"); + + if (engine != NULL && engine->config != NULL) { + char *template_name + = unwrap(list_find_corresponding_value_from_ptr_wrapper( + engine->config->keys, engine->config->values, "template")); + + if (template_name != NULL) { + template = list_find_corresponding_value_from_ptr_wrapper( + keys, templates, template_name); + + if (template == NULL) { + printf("Could not find template %s\n", template_name); + return; + } + } } - /* TODO: Handle this gracefully */ - default: - break; + for (size_t i = 0; i < template->components->size; i++) { + directive_t *directive = list_get(template->components, i); + + switch (directive->type) { + case _RAW: + fprintf(f, "%s", (char *) directive->operands); + break; + + case CONTENT: { + /* TODO: handle this gracefully */ + if (!is_markdown) { + char *content = find_contentfor_value(engine->content_headers, + directive->operands); + fprintf(f, "%s", content); + } + break; + } + + case BODY: { + if (is_markdown) { + + mkd_flag_t *flags = mkd_flags(); + mkd_set_flag_num(flags, MKD_FENCEDCODE); + markdown(doc, f, flags); + free(flags); + } else { + fprintf(f, "%s", (char *) doc); + } + + break; + } + + case PUTPAGE: { + + char *content + = unwrap(list_find_corresponding_value_from_ptr_wrapper( + engine->config->keys, + engine->config->values, + trim(directive->operands))); + fprintf(f, "%s", content); + + break; + } + + /* TODO: Handle this gracefully */ + default: + break; + } } - } } @@ -28,61 +28,61 @@ int msleep(long msec) { - struct timespec ts; - int res; + struct timespec ts; + int res; - if (msec < 0) { - errno = EINVAL; - return -1; - } + if (msec < 0) { + errno = EINVAL; + return -1; + } - ts.tv_sec = msec / 1000; - ts.tv_nsec = (msec % 1000) * 1000000; + ts.tv_sec = msec / 1000; + ts.tv_nsec = (msec % 1000) * 1000000; - do { - res = nanosleep(&ts, &ts); - } while (res && errno == EINTR); + do { + res = nanosleep(&ts, &ts); + } while (res && errno == EINTR); - return res; + return res; } char * ltrim(char *s) { - while (isspace(*s)) - s++; - return s; + while (isspace(*s)) + s++; + return s; } char * rtrim(char *s) { - char *back = s + strlen(s); - while (isspace(*--back)) - ; - *(back + 1) = '\0'; - return s; + char *back = s + strlen(s); + while (isspace(*--back)) + ; + *(back + 1) = '\0'; + return s; } char * trim(char *s) { - return rtrim(ltrim(s)); + return rtrim(ltrim(s)); } ptr_wrapper_t * wrap_ptr(void *ptr) { - ptr_wrapper_t *wrapper = malloc(sizeof(ptr_wrapper_t)); - wrapper->ptr = ptr; - return wrapper; + ptr_wrapper_t *wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = ptr; + return wrapper; } void * unwrap(ptr_wrapper_t *wrapper) { - if (wrapper == NULL) - return NULL; + if (wrapper == NULL) + return NULL; - return wrapper->ptr; + return wrapper->ptr; } |