int config_copy(config *dest, config *source) { /* copy a config to another */ memcpy (dest, source, sizeof(config)); /* copy all pointer values */ config_save_value ("server-name", source->server_name, dest, NULL); config_save_value ("doc-root", source->doc_root, dest, NULL); config_save_value ("index-file", source->index_file, dest, NULL); config_save_value ("chroot", source->chroot, dest, NULL); config_save_value ("hostname", source->hostname, dest, NULL); config_save_value ("access-log", source->access_log_path, dest, NULL); config_save_value ("error-log", source->error_log_path, dest, NULL); config_save_value ("cache-dir", source->cache_dir, dest, NULL); return 0; }
static int parse_request(prelude_client_t *client, int rtype, char *request, prelude_string_t *out) { config_t *cfg; void *context = client; char pname[256], iname[256]; prelude_option_t *last = NULL; int ret = 0, last_cmd = 0, ent; char *str, *value, *prev = NULL, *ptr = NULL; unsigned int line = 0; ret = _config_open(&cfg, prelude_client_get_config_filename(client)); if ( ret < 0 ) return ret; value = request; strsep(&value, "="); while ( (str = (option_strsep(&request))) ) { if ( ! request ) { last_cmd = 1; ptr = value; } *pname = 0; *iname = 0; ent = ret = sscanf(str, "%255[^[][%255[^]]", pname, iname); if ( ret < 1 ) { prelude_string_sprintf(out, "Error parsing option path"); break; } ret = parse_single(&context, &last, last_cmd, rtype, pname, (ent == 2) ? iname : ptr, out); if ( ret < 0 ) break; config_save_value(cfg, rtype, last, last_cmd, &prev, pname, (ent == 2) ? iname : ptr, &line); } _config_close(cfg); free(prev); return ret; }
int config_copy(config *dest, config *source) { /* copy a config to another */ memcpy (dest, source, sizeof(config)); /* copy all pointer values */ config_save_value ("server-name", source->server_name, dest); config_save_value ("hostname", source->hostname, dest); config_save_value ("hostname", source->hostname, dest); config_save_value ("chroot", source->chroot, dest); config_save_value ("queue-file", source->queue_file, dest); config_save_value ("web-service-url", source->web_service_url, dest); return 0; }
int config_load_servers(char *filename, master_server *master_srv) { /* load servers from config file */ config *conf = NULL; FILE *file = NULL; char line[4048]; int len, i; if ((file = fopen(filename, "r")) == NULL) { perror ("ERROR opening config file"); return -1; } int is_key, is_value, next; char key[128]; char value[2048]; int key_len = 0; int value_len = 0; int out_of_scope = 1; int count = 0; char *scope = "none"; while (fgets(line, sizeof(line), file) != NULL) { if ((len = strlen(line)) > 1) { is_key = 1; is_value = 0; next = 0; key_len = 0; value_len = 0; for (i = 0; i < len; ++i) { switch (line[i]) { case '#': /* comment */ next = 1; break; case '=': if (is_key) { key[key_len] = '\0'; is_key = 0; is_value = 1; } break; case '\t': case '\r': case '\n': break; case '{': if (key_len == 6) { if (strcmp(key, "server") == 0) { out_of_scope = 0; next = 1; /* create a new server */ master_srv->servers[count] = malloc(sizeof(server)); server *srv = master_srv->servers[count]; /* copy global config */ srv->config = config_init(); config_copy (srv->config, master_srv->config); conf = srv->config; ++count; scope = "server"; } } else if (key_len == 5) { if (strcmp(key, "proxy") == 0) { out_of_scope = 0; next = 1; /* create a new proxy */ conf->proxies[conf->proxy_count] = proxy_init(); conf->proxy_count++; scope = "proxy"; } } break; case '}': if (!out_of_scope) { out_of_scope = 1; scope = "none"; } break; default: if (is_key) { if (line[i] != ' ') { key[key_len] = line[i]; ++key_len; } } else if (is_value) { if (line[i] != ' ' || value_len != 0) { value[value_len] = line[i]; ++value_len; } } break; } if (next) { break; } } if (!out_of_scope) { value[value_len] = '\0'; if (key_len > 0 && value_len > 0) { if (conf != NULL) { /* save the value */ config_save_value (key, value, conf, scope); if (strcmp(key, "default") == 0) { /* found default server */ if (atoi(value) == 1) { master_srv->server_default = count-1; } } } } } } } fclose (file); /* validate */ if (conf->cache_turn_off_limit == 0 || conf->cache_turn_off_limit > conf->cache_memory_size) { /* invalid cache turn-off limit */ conf->cache_turn_off_limit = conf->cache_memory_size * 0.90; } master_srv->server_count = count; return 0; }
int config_load(char *filename, config *conf) { /* load configuration from file */ FILE *file = NULL; char line[4048]; int len, i; if ((file = fopen(filename, "r")) == NULL) { perror ("ERROR opening config file"); return -1; } int is_key, is_value, next; char key[512]; char value[4096]; int key_len = 0; int value_len = 0; int out_of_scope = 0; while (fgets(line, sizeof(line), file) != NULL) { if ((len = strlen(line)) > 1) { is_key = 1; is_value = 0; next = 0; key_len = 0; value_len = 0; for (i = 0; i < len; ++i) { switch (line[i]) { case '#': /* comment */ next = 1; break; case '=': if (is_key) { key[key_len] = '\0'; is_key = 0; is_value = 1; } break; case '\t': case '\r': case '\n': break; case '{': out_of_scope = 1; break; case '}': if (out_of_scope) { out_of_scope = 0; } break; default: if (is_key) { if (line[i] != ' ') { key[key_len] = line[i]; ++key_len; } } else if (is_value) { if (line[i] != ' ' || value_len != 0) { value[value_len] = line[i]; ++value_len; } } break; } if (next) { break; } } if (!out_of_scope) { value[value_len] = '\0'; if (key_len > 0 && value_len > 0) { /* save the value */ config_save_value (key, value, conf, NULL); } } } } fclose (file); /* validate */ if (conf->cache_turn_off_limit == 0 || conf->cache_turn_off_limit > conf->cache_memory_size) { /* invalid cache turn-off limit */ conf->cache_turn_off_limit = conf->cache_memory_size * 0.90; } return 0; }
int config_load(char *filename, config *conf) { /* load configuration from file */ FILE *file = NULL; char line[4048]; int len, i; if ((file = fopen(filename, "r")) == NULL) { perror ("ERROR opening config file"); return -1; } int is_key, is_value, next; char key[512]; char value[4096]; int key_len = 0; int value_len = 0; while (fgets(line, sizeof(line), file) != NULL) { if ((len = strlen(line)) > 1) { is_key = 1; is_value = 0; next = 0; key_len = 0; value_len = 0; for (i = 0; i < len; ++i) { switch (line[i]) { case '#': /* comment */ next = 1; break; case '=': if (is_key) { key[key_len] = '\0'; is_key = 0; is_value = 1; } break; case '\t': case '\r': case '\n': break; default: if (is_key) { if (line[i] != ' ') { key[key_len] = line[i]; ++key_len; } } else if (is_value) { if (line[i] != ' ' || value_len != 0) { value[value_len] = line[i]; ++value_len; } } break; } if (next) { break; } } value[value_len] = '\0'; if (key_len > 0 && value_len > 0) { /* save the value */ config_save_value (&key, &value, conf); } } } fclose (file); return 0; }