diff options
Diffstat (limited to 'src/template.c')
| -rw-r--r-- | src/template.c | 413 |
1 files changed, 203 insertions, 210 deletions
diff --git a/src/template.c b/src/template.c index 8365166..fd03480 100644 --- a/src/template.c +++ b/src/template.c @@ -11,8 +11,7 @@ static char *render_template_segment(const char *template_segment, static ContextVar *find_context_var(TemplateContext *ctx, const char *key) { if (ctx == NULL || key == NULL) { - fprintf(stderr, - "[ERROR] find_context_var: Invalid NULL input (ctx or key).\n"); + beaker_log("ERROR", "find_context_var: Invalid NULL input (ctx or key).\n"); return NULL; } for (int i = 0; i < ctx->count; i++) { @@ -90,8 +89,8 @@ TemplateContext new_context() { void context_set(TemplateContext *ctx, const char *key, const char *value) { if (ctx == NULL || key == NULL || value == NULL) { - fprintf(stderr, - "[ERROR] context_set: Invalid NULL input (ctx, key, or value).\n"); + beaker_log("ERROR", + "context_set: Invalid NULL input (ctx, key, or value).\n"); return; } @@ -114,25 +113,25 @@ void context_set(TemplateContext *ctx, const char *key, const char *value) { new_var->type = CONTEXT_TYPE_STRING; ctx->count++; } else { - fprintf(stderr, - "[WARNING] context_set: TemplateContext is full. Cannot add key " - "'%s'.\n", - key); + beaker_log("WARN", + "context_set: TemplateContext is full. Cannot add key " + "'%s'.\n", + key); } } void context_set_string_array(TemplateContext *ctx, const char *key, char *values[], int count) { if (ctx == NULL || key == NULL || values == NULL) { - fprintf(stderr, "[ERROR] context_set_string_array: Invalid NULL input " - "(ctx, key, or values).\n"); + beaker_log("ERROR", "context_set_string_array: Invalid NULL input " + "(ctx, key, or values).\n"); return; } if (count < 0 || count > MAX_OUTER_ARRAY_ITEMS) { - fprintf(stderr, - "[ERROR] context_set_string_array: Invalid count %d for string " - "array context '%s'. Max %d allowed.\n", - count, key, MAX_OUTER_ARRAY_ITEMS); + beaker_log("ERROR", + "context_set_string_array: Invalid count %d for string " + "array context '%s'. Max %d allowed.\n", + count, key, MAX_OUTER_ARRAY_ITEMS); return; } @@ -147,10 +146,10 @@ void context_set_string_array(TemplateContext *ctx, const char *key, var->key[MAX_KEY_LEN - 1] = '\0'; ctx->count++; } else { - fprintf(stderr, - "[WARNING] context_set_string_array: TemplateContext is full. " - "Cannot add string array key '%s'.\n", - key); + beaker_log("WARN", + "context_set_string_array: TemplateContext is full. " + "Cannot add string array key '%s'.\n", + key); return; } } @@ -158,11 +157,11 @@ void context_set_string_array(TemplateContext *ctx, const char *key, var->type = CONTEXT_TYPE_STRING_ARRAY; var->value.string_array_data.values = (char **)malloc(sizeof(char *) * count); if (var->value.string_array_data.values == NULL) { - perror("Failed to allocate memory for string array pointers"); - fprintf(stderr, - "[ERROR] context_set_string_array: Allocation failed for values " - "for key '%s'.\n", - key); + beaker_log_errno_format( + "ERROR", + "context_set_string_array: Allocation failed for values " + "for key '%s'.\n", + key); return; } var->value.string_array_data.count = count; @@ -174,11 +173,11 @@ void context_set_string_array(TemplateContext *ctx, const char *key, } var->value.string_array_data.values[i] = strdup(values[i]); if (var->value.string_array_data.values[i] == NULL) { - perror("Failed to duplicate string for string array context"); - fprintf(stderr, - "[ERROR] context_set_string_array: Failed to duplicate value for " - "item %d of key '%s'.\n", - i, key); + beaker_log_errno_format( + "ERROR", + "context_set_string_array: Failed to duplicate value for " + "item %d of key '%s'.\n", + i, key); for (int j = 0; j < i; j++) { if (var->value.string_array_data.values[j] != NULL) { @@ -197,15 +196,15 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, char **values_2d[], int outer_count, int inner_counts[]) { if (ctx == NULL || key == NULL || values_2d == NULL || inner_counts == NULL) { - fprintf(stderr, "[ERROR] context_set_array_of_arrays: Invalid NULL input " - "(ctx, key, values_2d, or inner_counts).\n"); + beaker_log("ERROR", "context_set_array_of_arrays: Invalid NULL input " + "(ctx, key, values_2d, or inner_counts).\n"); return; } if (outer_count < 0 || outer_count > MAX_OUTER_ARRAY_ITEMS) { - fprintf(stderr, - "[ERROR] context_set_array_of_arrays: Invalid outer count %d for " - "array of arrays context '%s'. Max %d allowed.\n", - outer_count, key, MAX_OUTER_ARRAY_ITEMS); + beaker_log("ERROR", + "context_set_array_of_arrays: Invalid outer count %d for " + "array of arrays context '%s'. Max %d allowed.\n", + outer_count, key, MAX_OUTER_ARRAY_ITEMS); return; } @@ -220,10 +219,10 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, var->key[MAX_KEY_LEN - 1] = '\0'; ctx->count++; } else { - fprintf(stderr, - "[WARNING] context_set_array_of_arrays: TemplateContext is full. " - "Cannot add array of arrays key '%s'.\n", - key); + beaker_log("WARN", + "context_set_array_of_arrays: TemplateContext is full. " + "Cannot add array of arrays key '%s'.\n", + key); return; } } @@ -236,11 +235,11 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, if (var->value.string_2d_array_data.values == NULL || var->value.string_2d_array_data.inner_counts == NULL) { - perror("Failed to allocate memory for 2D string array pointers or counts"); - fprintf(stderr, - "[ERROR] context_set_array_of_arrays: Allocation failed for key " - "'%s'.\n", - key); + beaker_log_errno_format( + "ERROR", + "context_set_array_of_arrays: Allocation failed for key " + "'%s'.\n", + key); free(var->value.string_2d_array_data.values); free(var->value.string_2d_array_data.inner_counts); var->value.string_2d_array_data.values = NULL; @@ -253,10 +252,10 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, int current_inner_count = inner_counts[i]; if (current_inner_count < 0 || current_inner_count > MAX_INNER_ARRAY_ITEMS) { - fprintf(stderr, - "[ERROR] context_set_array_of_arrays: Invalid inner count %d for " - "item %d in 2D array '%s'. Max %d allowed.\n", - current_inner_count, i, key, MAX_INNER_ARRAY_ITEMS); + beaker_log("ERROR", + "context_set_array_of_arrays: Invalid inner count %d for " + "item %d in 2D array '%s'. Max %d allowed.\n", + current_inner_count, i, key, MAX_INNER_ARRAY_ITEMS); for (int k = 0; k < i; k++) { if (var->value.string_2d_array_data.values[k] != NULL) { @@ -279,11 +278,11 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, var->value.string_2d_array_data.values[i] = (char **)malloc(sizeof(char *) * current_inner_count); if (var->value.string_2d_array_data.values[i] == NULL) { - perror("Failed to allocate memory for inner string array pointers"); - fprintf(stderr, - "[ERROR] context_set_array_of_arrays: Allocation failed for " - "inner array %d of key '%s'.\n", - i, key); + beaker_log_errno_format( + "ERROR", + "context_set_array_of_arrays: Allocation failed for " + "inner array %d of key '%s'.\n", + i, key); for (int k = 0; k < i; k++) { if (var->value.string_2d_array_data.values[k] != NULL) { @@ -311,11 +310,11 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, } var->value.string_2d_array_data.values[i][j] = strdup(values_2d[i][j]); if (var->value.string_2d_array_data.values[i][j] == NULL) { - perror("Failed to duplicate string for inner array context"); - fprintf(stderr, - "[ERROR] context_set_array_of_arrays: Failed to duplicate " - "value for item [%d][%d] of key '%s'.\n", - i, j, key); + beaker_log_errno_format( + "ERROR", + "context_set_array_of_arrays: Failed to duplicate " + "value for item [%d][%d] of key '%s'.\n", + i, j, key); for (int k = 0; k <= i; k++) { if (var->value.string_2d_array_data.values[k] != NULL) { @@ -360,7 +359,7 @@ static char *html_escape(const char *input) { size_t estimated_len = input_len * 5 + 1; char *output = (char *)malloc(estimated_len); if (output == NULL) { - perror("Failed to allocate memory for HTML escape output"); + beaker_log_errno("Failed to allocate memory for HTML escape output"); return NULL; } output[0] = '\0'; @@ -396,7 +395,8 @@ static char *html_escape(const char *input) { estimated_len = (current_output_len + repl_len + 1) * 2; char *new_output = (char *)realloc(output, estimated_len); if (new_output == NULL) { - perror("Failed to reallocate memory for HTML escape output"); + beaker_log_errno( + "Failed to reallocate memory for HTML escape output"); free(output); return NULL; } @@ -410,7 +410,8 @@ static char *html_escape(const char *input) { estimated_len = (current_output_len + 1 + 1) * 2; char *new_output = (char *)realloc(output, estimated_len); if (new_output == NULL) { - perror("Failed to reallocate memory for HTML escape output"); + beaker_log_errno( + "Failed to reallocate memory for HTML escape output"); free(output); return NULL; } @@ -426,8 +427,8 @@ static char *html_escape(const char *input) { static void append_to_buffer(char **buffer, size_t *current_len, size_t *max_len, const char *str_to_add) { if (str_to_add == NULL) { - fprintf(stderr, "[WARNING] append_to_buffer: Attempted to append NULL " - "string. Skipping.\n"); + beaker_log("WARN", "append_to_buffer: Attempted to append NULL " + "string. Skipping.\n"); return; } @@ -442,11 +443,11 @@ static void append_to_buffer(char **buffer, size_t *current_len, char *new_buffer = (char *)realloc(*buffer, *max_len); if (new_buffer == NULL) { - perror("Failed to reallocate buffer for template rendering"); - fprintf(stderr, - "[ERROR] append_to_buffer: Reallocation failed (requested %zu " - "bytes).\n", - *max_len); + beaker_log_errno_format( + "ERROR", + "append_to_buffer: Reallocation failed (requested %zu " + "bytes).\n", + *max_len); free(*buffer); *buffer = NULL; @@ -467,23 +468,22 @@ static char *parse_indexed_tag(const char *tag_content, int *index_val) { char *key_name = strdup(tag_content); if (key_name == NULL) { - perror("Failed to duplicate tag content for simple key"); - fprintf(stderr, "[ERROR] parse_indexed_tag: strdup failed for '%s'.\n", - tag_content); + beaker_log_errno_format( + "ERROR", "parse_indexed_tag: strdup failed for '%s'.\n", tag_content); } return key_name; } const char *close_bracket = strchr(open_bracket, ']'); if (close_bracket == NULL) { - fprintf(stderr, - "[ERROR] parse_indexed_tag: Unclosed bracket in tag '%s'. " - "Returning raw tag content.\n", - tag_content); + beaker_log("ERROR", + "parse_indexed_tag: Unclosed bracket in tag '%s'. " + "Returning raw tag content.\n", + tag_content); char *key_name = strdup(tag_content); if (key_name == NULL) { - perror("Failed to duplicate malformed tag content"); + beaker_log_errno("Failed to duplicate malformed tag content"); } return key_name; } @@ -491,9 +491,8 @@ static char *parse_indexed_tag(const char *tag_content, int *index_val) { size_t key_len = open_bracket - tag_content; char *key_name = (char *)malloc(key_len + 1); if (key_name == NULL) { - perror("Failed to allocate memory for key_name in parse_indexed_tag"); - fprintf(stderr, - "[ERROR] parse_indexed_tag: Allocation failed for key_name.\n"); + beaker_log_errno_format( + "ERROR", "parse_indexed_tag: Allocation failed for key_name.\n"); return NULL; } strncpy(key_name, tag_content, key_len); @@ -502,9 +501,8 @@ static char *parse_indexed_tag(const char *tag_content, int *index_val) { size_t index_str_len = close_bracket - (open_bracket + 1); char *index_str = (char *)malloc(index_str_len + 1); if (index_str == NULL) { - perror("Failed to allocate memory for index_str in parse_indexed_tag"); - fprintf(stderr, - "[ERROR] parse_indexed_tag: Allocation failed for index_str.\n"); + beaker_log_errno_format( + "ERROR", "parse_indexed_tag: Allocation failed for index_str.\n"); free(key_name); return NULL; } @@ -540,8 +538,7 @@ static const char *resolve_compare_value(const Condition *cond, ContextVar *compare_var = find_context_var(ctx, cond->compare_value); if (cond->compare_index >= 0) { - if (compare_var != NULL && - compare_var->type == CONTEXT_TYPE_STRING_ARRAY && + if (compare_var != NULL && compare_var->type == CONTEXT_TYPE_STRING_ARRAY && cond->compare_index < compare_var->value.string_array_data.count) { compare_value = compare_var->value.string_array_data.values[cond->compare_index]; @@ -568,7 +565,8 @@ static bool evaluate_condition(const Condition *cond, TemplateContext *ctx) { return false; } var_value = ""; - } else if (var->type == CONTEXT_TYPE_STRING_ARRAY && cond->compare_index >= 0) { + } else if (var->type == CONTEXT_TYPE_STRING_ARRAY && + cond->compare_index >= 0) { if (cond->compare_index >= 0 && cond->compare_index < var->value.string_array_data.count) { var_value = var->value.string_array_data.values[cond->compare_index]; @@ -596,8 +594,7 @@ static bool evaluate_condition(const Condition *cond, TemplateContext *ctx) { case CONDITION_EQUAL: case CONDITION_NOT_EQUAL: { char compare_buf[MAX_VALUE_LEN]; - const char *compare_value = - resolve_compare_value(cond, ctx, compare_buf); + const char *compare_value = resolve_compare_value(cond, ctx, compare_buf); bool values_equal = strcmp(var_value, compare_value) == 0; return cond->type == CONDITION_EQUAL ? values_equal : !values_equal; } @@ -766,11 +763,11 @@ static char *render_template_segment(const char *template_segment, size_t initial_max_len = BUFFER_SIZE; char *rendered_buffer = (char *)malloc(initial_max_len); if (rendered_buffer == NULL) { - perror("Failed to allocate initial buffer for template segment rendering"); - fprintf(stderr, - "[ERROR] render_template_segment: Failed to allocate initial %zu " - "bytes for rendered_buffer.\n", - initial_max_len); + beaker_log_errno_format( + "ERROR", + "render_template_segment: Failed to allocate initial %zu " + "bytes for rendered_buffer.\n", + initial_max_len); return NULL; } rendered_buffer[0] = '\0'; @@ -786,9 +783,9 @@ static char *render_template_segment(const char *template_segment, if (text_len > 0) { char *text_before_tag = (char *)malloc(text_len + 1); if (text_before_tag == NULL) { - perror("Failed to allocate memory for text_before_tag"); - fprintf(stderr, "[ERROR] render_template_segment: Allocation failed " - "for text_before_tag.\n"); + beaker_log_errno_format("ERROR", + "render_template_segment: Allocation failed " + "for text_before_tag.\n"); free(rendered_buffer); return NULL; } @@ -801,8 +798,8 @@ static char *render_template_segment(const char *template_segment, const char *end_tag = strstr(start_tag, "}}"); if (end_tag == NULL) { - fprintf(stderr, "[ERROR] render_template_segment: Unclosed '{{' tag. " - "Appending remaining template content as-is.\n"); + beaker_log("ERROR", "render_template_segment: Unclosed '{{' tag. " + "Appending remaining template content as-is.\n"); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, start_tag); return rendered_buffer; } @@ -810,9 +807,9 @@ static char *render_template_segment(const char *template_segment, size_t tag_content_len = end_tag - (start_tag + 2); char *tag_content_raw = (char *)malloc(tag_content_len + 1); if (tag_content_raw == NULL) { - perror("Failed to allocate memory for tag_content_raw"); - fprintf(stderr, "[ERROR] render_template_segment: Allocation failed for " - "tag_content_raw.\n"); + beaker_log_errno_format("ERROR", + "render_template_segment: Allocation failed for " + "tag_content_raw.\n"); free(rendered_buffer); return NULL; } @@ -878,9 +875,9 @@ static char *render_template_segment(const char *template_segment, size_t loop_inner_len = loop_end_tag - loop_inner_start; char *loop_inner_template = (char *)malloc(loop_inner_len + 1); if (loop_inner_template == NULL) { - perror("Failed to allocate memory for loop_inner_template"); - fprintf(stderr, "[ERROR] render_template_segment: Allocation " - "failed for loop_inner_template.\n"); + beaker_log_errno_format("ERROR", + "render_template_segment: Allocation " + "failed for loop_inner_template.\n"); free(rendered_buffer); free(tag_content_raw); return NULL; @@ -940,9 +937,9 @@ static char *render_template_segment(const char *template_segment, free_context(&loop_ctx); } } else { - fprintf( - stderr, - "[IGNORE] [ERROR] render_template_segment: List variable '%s' " + beaker_log( + "DEBUG", + "render_template_segment: List variable '%s' " "(type %d) is not an iterable array type for 'for' loop.\n", list_var, list_ctx_var->type); } @@ -952,25 +949,26 @@ static char *render_template_segment(const char *template_segment, continue; } else { - fprintf(stderr, - "[IGNORE] [ERROR] render_template_segment: List variable " - "'%s' not found for 'for' loop. Skipping loop block.\n", - list_var); + beaker_log("DEBUG", + "render_template_segment: List variable " + "'%s' not found for 'for' loop. Skipping loop block.\n", + list_var); current_pos = loop_end_tag + strlen("{{endfor}}"); free(tag_content_raw); continue; } } else { - fprintf(stderr, - "[ERROR] render_template_segment: Malformed 'for' loop tag: " - "'%s'. Expected 'for var in list'. Appending loop tag as-is.\n", - trimmed_tag_content); + beaker_log( + "ERROR", + "render_template_segment: Malformed 'for' loop tag: " + "'%s'. Expected 'for var in list'. Appending loop tag as-is.\n", + trimmed_tag_content); } } else if (strcmp(trimmed_tag_content, "endfor") == 0) { - fprintf(stderr, "[WARNING] render_template_segment: '{{endfor}}' without " - "matching '{{for}}'. Appending endfor tag as-is.\n"); + beaker_log("WARN", "render_template_segment: '{{endfor}}' without " + "matching '{{for}}'. Appending endfor tag as-is.\n"); } else if (strncmp(trimmed_tag_content, "if ", 3) == 0) { @@ -992,7 +990,7 @@ static char *render_template_segment(const char *template_segment, const char *candidates[4] = {next_if, next_else, next_elif, next_endif}; const char *earliest = NULL; int earliest_idx = -1; - + for (int i = 0; i < 4; i++) { if (candidates[i] != NULL) { if (earliest == NULL || candidates[i] < earliest) { @@ -1035,18 +1033,16 @@ static char *render_template_segment(const char *template_segment, } if (endif_tag == NULL) { - fprintf(stderr, - "[ERROR] render_template_segment: Unclosed '{{if}}' tag. " - "Skipping if block.\n"); + beaker_log("ERROR", "render_template_segment: Unclosed '{{if}}' tag. " + "Skipping if block.\n"); current_pos = end_tag + 2; free(tag_content_raw); continue; } if (endif_tag == NULL) { - fprintf(stderr, - "[ERROR] render_template_segment: Unclosed '{{if}}' tag. " - "Skipping if block.\n"); + beaker_log("ERROR", "render_template_segment: Unclosed '{{if}}' tag. " + "Skipping if block.\n"); current_pos = end_tag + 2; free(tag_content_raw); continue; @@ -1139,18 +1135,18 @@ static char *render_template_segment(const char *template_segment, else if (strcmp(trimmed_tag_content, "elif ") == 0 || strcmp(trimmed_tag_content, "elif") == 0) { - fprintf(stderr, "[WARNING] render_template_segment: '{{elif}}' without " - "matching '{{if}}'. Appending elif tag as-is.\n"); + beaker_log("WARN", "render_template_segment: '{{elif}}' without " + "matching '{{if}}'. Appending elif tag as-is.\n"); } else if (strcmp(trimmed_tag_content, "else") == 0) { - fprintf(stderr, "[WARNING] render_template_segment: '{{else}}' without " - "matching '{{if}}'. Appending else tag as-is.\n"); + beaker_log("WARN", "render_template_segment: '{{else}}' without " + "matching '{{if}}'. Appending else tag as-is.\n"); } else if (strcmp(trimmed_tag_content, "endif") == 0) { - fprintf(stderr, "[WARNING] render_template_segment: '{{endif}}' without " - "matching '{{if}}'. Appending endif tag as-is.\n"); + beaker_log("WARN", "render_template_segment: '{{endif}}' without " + "matching '{{if}}'. Appending endif tag as-is.\n"); } else if (strncmp(trimmed_tag_content, "include ", 8) == 0) { @@ -1162,9 +1158,9 @@ static char *render_template_segment(const char *template_segment, size_t filename_len = filename_end - filename_start; char *included_filename = (char *)malloc(filename_len + 1); if (included_filename == NULL) { - perror("Failed to allocate memory for included filename"); - fprintf(stderr, "[ERROR] render_template_segment: Allocation " - "failed for included_filename.\n"); + beaker_log_errno_format("ERROR", + "render_template_segment: Allocation " + "failed for included_filename.\n"); free(rendered_buffer); free(tag_content_raw); return NULL; @@ -1172,13 +1168,16 @@ static char *render_template_segment(const char *template_segment, strncpy(included_filename, filename_start, filename_len); included_filename[filename_len] = '\0'; - if (strstr(included_filename, "..") != NULL || strchr(included_filename, '/') != NULL) { - fprintf(stderr, - "[SECURITY] render_template_segment: Path traversal attempt in include: %s\n", - included_filename); + if (strstr(included_filename, "..") != NULL || + strchr(included_filename, '/') != NULL) { + beaker_log("SECURITY", + "render_template_segment: Path traversal attempt in " + "include: %s\n", + included_filename); free(included_filename); - append_to_buffer(&rendered_buffer, ¤t_len, &max_len, - "<!-- Include blocked: path traversal not allowed -->"); + append_to_buffer( + &rendered_buffer, ¤t_len, &max_len, + "<!-- Include blocked: path traversal not allowed -->"); current_pos = end_tag + 2; free(tag_content_raw); continue; @@ -1190,10 +1189,10 @@ static char *render_template_segment(const char *template_segment, included_html); free(included_html); } else { - fprintf(stderr, - "[WARNING] render_template_segment: Failed to render " - "included template '%s'.\n", - included_filename); + beaker_log("WARN", + "render_template_segment: Failed to render " + "included template '%s'.\n", + included_filename); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, "<!-- Failed to include "); @@ -1206,27 +1205,26 @@ static char *render_template_segment(const char *template_segment, free(tag_content_raw); continue; } else { - fprintf(stderr, - "[ERROR] render_template_segment: Malformed include tag " - "'%s'. Missing closing quote.\n", - trimmed_tag_content); + beaker_log("ERROR", + "render_template_segment: Malformed include tag " + "'%s'. Missing closing quote.\n", + trimmed_tag_content); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, "{{"); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, trimmed_tag_content); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, "}}"); - free(tag_content_raw); - - current_pos = end_tag + 2; + free(tag_content_raw); - continue; + current_pos = end_tag + 2; + continue; } } else { - fprintf(stderr, - "[ERROR] render_template_segment: Malformed include tag '%s'. " - "Expected quoted filename.\n", - trimmed_tag_content); + beaker_log("ERROR", + "render_template_segment: Malformed include tag '%s'. " + "Expected quoted filename.\n", + trimmed_tag_content); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, "{{"); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, @@ -1239,10 +1237,10 @@ static char *render_template_segment(const char *template_segment, const char *key_start = trimmed_tag_content + 3; const char *key_end = strstr(key_start, "\")"); if (key_end == NULL) { - fprintf(stderr, - "[ERROR] render_template_segment: Malformed l() tag '%s'. " - "Expected l(\"key\"). Appending as-is.\n", - trimmed_tag_content); + beaker_log("ERROR", + "render_template_segment: Malformed l() tag '%s'. " + "Expected l(\"key\"). Appending as-is.\n", + trimmed_tag_content); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, "{{"); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, trimmed_tag_content); @@ -1254,7 +1252,8 @@ static char *render_template_segment(const char *template_segment, size_t key_len = key_end - key_start; char l10n_key[MAX_KEY_LEN]; - if (key_len >= MAX_KEY_LEN) key_len = MAX_KEY_LEN - 1; + if (key_len >= MAX_KEY_LEN) + key_len = MAX_KEY_LEN - 1; strncpy(l10n_key, key_start, key_len); l10n_key[key_len] = '\0'; @@ -1297,10 +1296,10 @@ static char *render_template_segment(const char *template_segment, } } } else { - fprintf(stderr, - "[WARNING] render_template_segment: l10n key '%s' not found " - "for current locale. Appending key as-is.\n", - l10n_key); + beaker_log("WARN", + "render_template_segment: l10n key '%s' not found " + "for current locale. Appending key as-is.\n", + l10n_key); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, l10n_key); } @@ -1313,7 +1312,7 @@ static char *render_template_segment(const char *template_segment, bool is_safe = false; char *processing_tag_content = strdup(trimmed_tag_content); if (processing_tag_content == NULL) { - perror("Failed to duplicate tag content for processing"); + beaker_log_errno("Failed to duplicate tag content for processing"); free(rendered_buffer); free(tag_content_raw); return NULL; @@ -1331,10 +1330,10 @@ static char *render_template_segment(const char *template_segment, free(processing_tag_content); if (key_name == NULL) { - fprintf(stderr, - "[ERROR] render_template_segment: parse_indexed_tag failed for " - "'%s'.\n", - trimmed_tag_content); + beaker_log("ERROR", + "render_template_segment: parse_indexed_tag failed for " + "'%s'.\n", + trimmed_tag_content); free(rendered_buffer); free(tag_content_raw); return NULL; @@ -1348,9 +1347,9 @@ static char *render_template_segment(const char *template_segment, if (var->type == CONTEXT_TYPE_STRING) { value_to_append = var->value.string_val; } else { - fprintf( - stderr, - "[WARNING] render_template_segment: Variable '%s' is not a " + beaker_log( + "WARN", + "render_template_segment: Variable '%s' is not a " "simple string (type %d). Not rendering as simple string.\n", key_name, var->type); @@ -1365,24 +1364,25 @@ static char *render_template_segment(const char *template_segment, index_val < var->value.string_array_data.count) { value_to_append = var->value.string_array_data.values[index_val]; } else { - fprintf(stderr, - "[ERROR] render_template_segment: Index %d out of bounds " - "for '%s' (count %d). Appending tag as-is.\n", - index_val, key_name, var->value.string_array_data.count); + beaker_log("ERROR", + "render_template_segment: Index %d out of bounds " + "for '%s' (count %d). Appending tag as-is.\n", + index_val, key_name, + var->value.string_array_data.count); } } else { - fprintf( - stderr, - "[ERROR] render_template_segment: Variable '%s' (type %d) is " + beaker_log( + "ERROR", + "render_template_segment: Variable '%s' (type %d) is " "not a string array for indexed access. Appending tag as-is.\n", key_name, var->type); } } } else { - fprintf(stderr, - "[WARNING] render_template_segment: Key '%s' not found in " - "context. Appending tag as-is.\n", - key_name); + beaker_log("WARN", + "render_template_segment: Key '%s' not found in " + "context. Appending tag as-is.\n", + key_name); } if (value_to_append != NULL) { @@ -1396,24 +1396,23 @@ static char *render_template_segment(const char *template_segment, escaped_value); free(escaped_value); } else { - fprintf(stderr, - "[ERROR] render_template_segment: Failed to HTML escape " - "value for key '%s'. Appending raw.\n", - key_name); + beaker_log("ERROR", + "render_template_segment: Failed to HTML escape " + "value for key '%s'. Appending raw.\n", + key_name); append_to_buffer(&rendered_buffer, ¤t_len, &max_len, value_to_append); } } } - free(key_name); - - free(tag_content_raw); + free(key_name); - current_pos = end_tag + 2; + free(tag_content_raw); - continue; + current_pos = end_tag + 2; + continue; } free(tag_content_raw); @@ -1428,9 +1427,8 @@ char *render_template(const char *template_file, TemplateContext *ctx) { char full_path[MAX_PATH_LEN]; if (strstr(template_file, "..") != NULL) { - fprintf(stderr, - "[SECURITY] render_template: Path traversal attempt: %s\n", - template_file); + beaker_log("SECURITY", "render_template: Path traversal attempt: %s\n", + template_file); return NULL; } @@ -1438,20 +1436,17 @@ char *render_template(const char *template_file, TemplateContext *ctx) { FILE *fp = fopen(full_path, "r"); if (fp == NULL) { - perror("Error opening template file"); - fprintf(stderr, - "[ERROR] render_template: Could not open template '%s'. %s\n", - full_path, strerror(errno)); + beaker_log_errno_format( + "ERROR", "render_template: Could not open template '%s'. %s\n", + full_path, strerror(errno)); return NULL; } fseek(fp, 0, SEEK_END); long file_size = ftell(fp); if (file_size < 0) { - perror("Error getting template file size"); - fprintf(stderr, - "[ERROR] render_template: Failed to get size of '%s'.\n", - full_path); + beaker_log_errno_format( + "ERROR", "render_template: Failed to get size of '%s'.\n", full_path); fclose(fp); return NULL; } @@ -1465,21 +1460,19 @@ char *render_template(const char *template_file, TemplateContext *ctx) { char *template_content = (char *)malloc((size_t)file_size + 1); if (template_content == NULL) { - perror("Error allocating memory for template content"); - fprintf(stderr, - "[ERROR] render_template: Failed to allocate %ld bytes for " - "template content.\n", - file_size + 1); + beaker_log_errno_format("ERROR", + "render_template: Failed to allocate %ld bytes for " + "template content.\n", + file_size + 1); fclose(fp); return NULL; } size_t bytes_read = fread(template_content, 1, (size_t)file_size, fp); if (bytes_read != (size_t)file_size) { - perror("Error reading template file"); - fprintf(stderr, - "[ERROR] render_template: Failed to read complete template '%s'.\n", - full_path); + beaker_log_errno_format( + "ERROR", "render_template: Failed to read complete template '%s'.\n", + full_path); free(template_content); fclose(fp); return NULL; |
