char* blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local) { const char *date_format = blogc_get_variable("DATE_FORMAT", global, local); if (date == NULL) return NULL; if (date_format == NULL) return b_strdup(date); #ifdef HAVE_TIME_H struct tm tm; memset(&tm, 0, sizeof(struct tm)); if (NULL == strptime(date, "%Y-%m-%d %H:%M:%S", &tm)) { fprintf(stderr, "blogc: warning: Failed to parse DATE variable: %s\n", date); return b_strdup(date); } char tmp[1024]; if (0 == strftime(tmp, sizeof(tmp), date_format, &tm)) { fprintf(stderr, "blogc: warning: Failed to format DATE variable, " "FORMAT is too long: %s\n", date_format); return b_strdup(date); } return b_strdup(tmp); #else fprintf(stderr, "blogc: warning: Can't pre-process DATE variable.\n"); return NULL; #endif }
int fill_env(t_utils *utils, char **env) { int i; char **__tab; static t_base *last; i = -1; if (env == NULL) return (-1); while (env && env[++i]) { if ((__tab = totab(env[i], "=")) == NULL) return (-1); last = add_to_list((t_base **)&(utils->env), 0); ((t_env *)last)->token = 0; ((t_env *)last)->key = NULL; ((t_env *)last)->value = b_strdup(""); if (__tab[0]) ((t_env *)last)->key = b_strdup(__tab[0]); if (__tab[1]) { free(((t_env *)last)->value); ((t_env *)last)->value = b_strdup(__tab[1]); } free_tab(__tab); } return (0); }
inline status_t set_dev_data(e_dev_data_t *data, const char *name, const char *root_dir) { if (data == NULL) return B_BAD_VALUE; if (root_dir == NULL || *root_dir == 0) return B_BAD_VALUE; if (*root_dir != '/') return B_BAD_VALUE; if (strlen(root_dir) == 5 && strcmp(root_dir, "/proc") == 0) return B_BAD_VALUE; if (strlen(root_dir) == 4 && strcmp(root_dir, "/dev") == 0) return B_BAD_VALUE; char *fname = (name == NULL ? NULL : b_strdup(name)); char *fdir = b_strdup(root_dir); if ((fname == NULL && !(name == NULL || *name == 0)) || fdir == NULL) { if (fname) free(fname); if (fdir) free(fdir); return B_NO_MEMORY; } if (data->name) free(data->name); if (data->root_dir) free(data->root_dir); data->name = fname; data->root_dir = fdir; return B_OK; }
static void blogc_mkdir_recursive(const char *filename) { char *fname = b_strdup(filename); for (char *tmp = fname; *tmp != '\0'; tmp++) { if (*tmp != '/' && *tmp != '\\') continue; #if defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_TYPES_H) char bkp = *tmp; *tmp = '\0'; if ((strlen(fname) > 0) && (-1 == mkdir(fname, 0777)) && (errno != EEXIST)) { fprintf(stderr, "blogc: error: failed to create output " "directory (%s): %s\n", fname, strerror(errno)); free(fname); exit(2); } *tmp = bkp; #else // FIXME: show this warning only if actually trying to create a directory. fprintf(stderr, "blogc: warning: can't create output directories " "for your platform. please create the directories yourself.\n"); break; #endif } free(fname); }
int add_history_to_list(t_history **history) { int nb; int fd; char *buffer; static t_base *last; if ((fd = open(HISTORY_FILE, O_RDWR)) == -1) return (-1); nb = 1; while ((buffer = get_next_line(fd))) { last = add_to_list((t_base **)history, 1); ((t_history *)last)->token = 1; ((t_history *)last)->str = b_strdup(buffer); ((t_history *)last)->nb = nb++; free(buffer); } if (close(fd)) return (-1); return (0); }
char* blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing) { if (tmpl == NULL || sources == NULL) return NULL; b_slist_t *current_source = NULL; b_slist_t *listing_start = NULL; b_string_t *str = b_string_new(); b_trie_t *tmp_source = NULL; const char *config_value = NULL; const char *config_var = NULL; char *config_value2 = NULL; char *defined = NULL; unsigned int if_count = 0; unsigned int if_skip = 0; bool if_not = false; bool inside_block = false; bool evaluate = false; int cmp = 0; b_slist_t *tmp = tmpl; while (tmp != NULL) { blogc_template_stmt_t *stmt = tmp->data; switch (stmt->type) { case BLOGC_TEMPLATE_CONTENT_STMT: if (stmt->value != NULL) b_string_append(str, stmt->value); break; case BLOGC_TEMPLATE_BLOCK_STMT: inside_block = true; if_count = 0; if (0 == strcmp("entry", stmt->value)) { if (listing) { // we can just skip anything and walk until the next // 'endblock' while (stmt->type != BLOGC_TEMPLATE_ENDBLOCK_STMT) { tmp = tmp->next; stmt = tmp->data; } break; } current_source = sources; tmp_source = current_source->data; } else if ((0 == strcmp("listing", stmt->value)) || (0 == strcmp("listing_once", stmt->value))) { if (!listing) { // we can just skip anything and walk until the next // 'endblock' while (stmt->type != BLOGC_TEMPLATE_ENDBLOCK_STMT) { tmp = tmp->next; stmt = tmp->data; } break; } } if (0 == strcmp("listing", stmt->value)) { if (current_source == NULL) { listing_start = tmp; current_source = sources; } tmp_source = current_source->data; } break; case BLOGC_TEMPLATE_VARIABLE_STMT: if (stmt->value != NULL) { config_var = NULL; if (0 == strcmp(stmt->value, "DATE_FORMATTED")) config_var = "DATE"; else if (0 == strcmp(stmt->value, "DATE_FIRST_FORMATTED")) config_var = "DATE_FIRST"; else if (0 == strcmp(stmt->value, "DATE_LAST_FORMATTED")) config_var = "DATE_LAST"; if (config_var != NULL) { config_value2 = blogc_format_date( blogc_get_variable(config_var, config, inside_block ? tmp_source : NULL), config, inside_block ? tmp_source : NULL); if (config_value2 != NULL) { b_string_append(str, config_value2); free(config_value2); config_value2 = NULL; break; } } else { config_value = blogc_get_variable(stmt->value, config, inside_block ? tmp_source : NULL); if (config_value != NULL) b_string_append(str, config_value); } } break; case BLOGC_TEMPLATE_ENDBLOCK_STMT: inside_block = false; if (listing_start != NULL && current_source != NULL) { current_source = current_source->next; if (current_source != NULL) { tmp = listing_start; continue; } else listing_start = NULL; } break; case BLOGC_TEMPLATE_IFNDEF_STMT: if_not = true; case BLOGC_TEMPLATE_IF_STMT: case BLOGC_TEMPLATE_IFDEF_STMT: defined = NULL; if (stmt->value != NULL) { config_var = NULL; if (0 == strcmp(stmt->value, "DATE_FORMATTED")) config_var = "DATE"; else if (0 == strcmp(stmt->value, "DATE_FIRST_FORMATTED")) config_var = "DATE_FIRST"; else if (0 == strcmp(stmt->value, "DATE_LAST_FORMATTED")) config_var = "DATE_LAST"; if (config_var != NULL) { config_value2 = blogc_format_date( blogc_get_variable(config_var, config, inside_block ? tmp_source : NULL), config, inside_block ? tmp_source : NULL); if (config_value2 != NULL) { defined = config_value2; config_value2 = NULL; } } else defined = b_strdup(blogc_get_variable(stmt->value, config, inside_block ? tmp_source : NULL)); } evaluate = false; if (stmt->op != 0) { if (defined != NULL && stmt->value2 != NULL) { cmp = strcmp(defined, stmt->value2); if (cmp != 0 && stmt->op & BLOGC_TEMPLATE_OP_NEQ) evaluate = true; else if (cmp == 0 && stmt->op & BLOGC_TEMPLATE_OP_EQ) evaluate = true; else if (cmp < 0 && stmt->op & BLOGC_TEMPLATE_OP_LT) evaluate = true; else if (cmp > 0 && stmt->op & BLOGC_TEMPLATE_OP_GT) evaluate = true; } } else { if (if_not && defined == NULL) evaluate = true; if (!if_not && defined != NULL) evaluate = true; } if (!evaluate) { if_skip = if_count; // at this point we can just skip anything, counting the // number of 'if's, to know how many 'endif's we need to // skip as well. while (1) { tmp = tmp->next; stmt = tmp->data; if ((stmt->type == BLOGC_TEMPLATE_IF_STMT) || (stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) || (stmt->type == BLOGC_TEMPLATE_IFNDEF_STMT)) { if_count++; continue; } if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) { if (if_count > if_skip) { if_count--; continue; } if (if_count == if_skip) break; } } } free(defined); defined = NULL; if_not = false; break; case BLOGC_TEMPLATE_ENDIF_STMT: if_count--; break; } tmp = tmp->next; } return b_string_free(str, false); }
int main(int argc, char **argv) { int rv = 0; bool listing = false; char *template = NULL; char *output = NULL; char *print = NULL; char *tmp = NULL; char **pieces = NULL; b_slist_t *sources = NULL; b_trie_t *config = b_trie_new(free); b_trie_insert(config, "BLOGC_VERSION", b_strdup(PACKAGE_VERSION)); for (unsigned int i = 1; i < argc; i++) { tmp = NULL; if (argv[i][0] == '-') { switch (argv[i][1]) { case 'h': blogc_print_help(); goto cleanup; case 'v': printf("%s\n", PACKAGE_STRING); goto cleanup; case 'l': listing = true; break; case 't':