/** * Parse a file that has a key=value type and return the specified key * FILE *config defined in SRC define * char *separator The separator that is used in the string of the file * char *key The key that you are looking for * @return string */ char *parse_file(FILE *config,char *separator,char *key) { char line[maximum]; char buffer[maximum]; char *copy; while(fgets(line, maximum, config) != NULL) { /* get a line, up to 1000 chars from line. done if NULL */ sscanf (line, "%s", &buffer); if( strstr(line,key) ) { copy = str_explode(buffer,separator); break; } } rewind(config); return copy; }
geo_t *geo_load(geo_t *g, char *filename, int geo_mode) { int n; FILE *fp; char buf[MAX_LINE]; char *p; unsigned char *field[2]; if (!filename) { return NULL; } if (geo_mode != GEO_RANGE && geo_mode != GEO_CIDR) { return NULL; } if (!g) { g = (geo_t *)malloc(sizeof(geo_t)); if (!g) { return NULL; } g->geo_mode = geo_mode; if (geo_mode == GEO_RANGE) { g->u.range = range_create(); if (!g->u.range) { free(g); return NULL; } } else if (geo_mode == GEO_CIDR) { g->u.tree = radix_tree_create(); if (!g->u.tree) { free(g); return NULL; } } else { return NULL; } } fp = fopen(filename, "r"); if (!fp) { return NULL; } while (fgets(buf, MAX_LINE, fp)) { n = strlen(buf); if (buf[n - 1] == '\n') { buf[n - 1] = '\0'; } p = buf; while (*p == ' ' || *p == '\t') { ++p; } if (*p == '\0' || *p == '#' || (*p == '/' && *(p + 1) == '/')) { continue; } if (str_explode(NULL, (unsigned char *)p, field, 2) != 2) { goto error; } if (strcmp((char *)field[0], "include") == 0) { if (geo_load(g, (char *)field[1], geo_mode) == NULL) { goto error; } continue; } if (geo_mode == GEO_RANGE) { if (geo_parse_range(g, (char *)field[0], (char *)field[1]) != 0) { goto error; } } else if (geo_mode == GEO_CIDR) { if (geo_parse_cidr(g, (char *)field[0], (char *)field[1]) != 0) { goto error; } } else { /* should NOT get here */ assert(0); } } fclose(fp); return g; error: fclose(fp); geo_unload(g); return NULL; }
static int conf_parse(conf_t *conf, char *resolved_path, FILE *fp, int block) { int n; int ret = 0; char buf[MAX_LINE] = {}; unsigned char *field[2]; conf_value_t *cv; char *var = NULL; if (!conf->ht) { conf->ht = hash_create(HASH_INIT_SLOTS); if (!conf->ht) { perror("hash_create failed"); return -1; } HASH_SET_KEYCPY(conf->ht, key_dup); HASH_SET_FREE_KEY(conf->ht, free_key); HASH_SET_FREE_VAL(conf->ht, free_val); HASH_SET_KEYCMP(conf->ht, key_cmp); } while (fgets(buf, MAX_LINE, fp)) { n = strlen(buf); if (n == 0) { continue; } if (buf[n - 1] == '\n') { buf[n - 1] = '\0'; } if (*buf == '#') { continue; } else if (str_explode(NULL, (unsigned char*)buf, field, 2) == 2) { int is_block = 0; if ((var = conf_evaluate(conf, (char *)field[1])) == NULL) { ret = -1; goto error; } /* Process `include' directive. */ if (!strcmp((char *)field[0], "include")) { if (conf_parse_include(conf, resolved_path, var)) { if (var != (char *)field[1]) { free(var); } ret = -1; goto error; } if (var != (char *)field[1]) { free(var); } continue; } if (!strcmp((char *)field[1], "{")) { is_block = 1; /* meet a block */ } /* process a key/value config */ cv = (conf_value_t*)hash_get_val(conf->ht, (void *)field[0]); if (!cv) { /* Add a conf value */ cv = calloc(1, sizeof(conf_value_t)); if (!cv) { ret = -1; goto error; } if (hash_insert(conf->ht, (void *)field[0], cv) != 0) { free(cv); ret = -1; goto error; } if (is_block) { conf_block_t *cb = NULL; cb = calloc(1, sizeof(conf_block_t)); if (!cb) { ret = -1; goto error; } if (conf_parse(&cb->block, resolved_path, fp, 1) != 0) { free(cb); ret = -1; goto error; } cb->next = NULL; cv->type = CONF_TYPE_BLOCK; cv->value = (void *)cb; } else { conf_entry_t *ce = NULL; ce = calloc(1, sizeof(conf_entry_t)); if (!ce) { ret = -1; goto error; } ce->value = (var == (char *)field[1]) ? strdup((char *)field[1]) : var; ce->next = NULL; cv->type = CONF_TYPE_ENTRY; cv->value = ce; } } else { if (cv->type == CONF_TYPE_ENTRY) { conf_entry_t *ce; if (is_block) { ret = -1; goto error; } ce = calloc(1, sizeof(conf_entry_t)); if (!ce) { ret = -1; goto error; } ce->value = (var == (char *)field[1]) ? strdup((char *)field[1]) : var; ce->next = (conf_entry_t*)(cv->value); cv->value = ce; } else { conf_block_t *cb = NULL; if (!is_block) { ret = -1; goto error; } cb = calloc(1, sizeof(conf_block_t)); if (!cb) { ret = -1; goto error; } if (conf_parse(&cb->block, resolved_path, fp, 1) != 0) { free(cb); ret = -1; goto error; } cb->next = (conf_block_t *)(cv->value); cv->value = cb; } } } else { if (field[0] && field[0][0] == '}') { if (block) { return 0; /* parse block over */ } else { ret = -1; goto error; } } } } error: if (ret == -1) { conf_free(conf); } return ret; }