Beispiel #1
0
static int parse_entry(Parser* self, const char* line, size_t len) {
  unsigned i = 0;
  unsigned bufi = 0;

  // Read key
  while (i < len && line[i] != '=') {
    if (bufi+1 == sizeof(self->buf)) {
      self->error = "key name is too long";
      return -1;
    }
    self->buf[bufi++] = line[i++];
  }
  if (i == len) {
    self->error = "'=' not found";
    return -1;
  }
  i++; // consume '='

  char* key = trim(self->buf, bufi);
  bufi = 0;

  // Read value
  bufi = 0;
  while (i < len && isspace(line[i])) i++;
  while (i < len) {
    if (bufi+1 == sizeof(self->buf)) {
      self->error = "value is too long";
      free(key);
      return -1;
    }
    self->buf[bufi++] = line[i++];
  }

  char* value = trim(self->buf, bufi);

  // Add entry
  ini_add(self->ini, self->section, key, value);
  free(key);
  free(value);
  return 0;
}
Beispiel #2
0
extern int ini_load(ini_t * self)
{
	char *line;
	char *section;
	char *var, *value;
	int nread;
	size_t n;
	FILE *file;

	assert(self);

	file = fopen(self->filepath, "r");

	if (!file)
		return -1;

	section = NULL;

	line = NULL;

	while ((nread = getline(&line, &n, file)) >= 0) {
		*(line + strlen(line) - 1) = 0;
		strtrim(line);

		if (!strlen(line) || *line == '#') {
			continue;
		}

		eatcomments(line);

		if (*line == '[') {
			free(section);
			section = xmalloc(strlen(line) - 1);
			memset(section, 0, strlen(line) - 1);
			strncpy(section, line + 1, strlen(line) - 2);
			ini_add(self, strtrim(section));
			continue;
		}

		if (!section)
			continue;
		value = NULL;
		value = strchr(line, '=');

		if (!value)
			continue;

		var = xmalloc(strlen(line) - strlen(value) + 1);
		memset(var, 0, strlen(line) - strlen(value) + 1);
		strncpy(var, line, strlen(line) - strlen(value) - 1);
		++value;
		strtrim(value);
		if (*value == '"' && *(value + strlen(value) - 1) == '"') {
			*(value + strlen(value) - 1) = 0;
			value++;
		}
		ini_set(self, section, strtrim(var), strtrim(value));
		free(var);
	}
	free(line);
	free(section);
	fclose(file);

	return 0;
}
Beispiel #3
0
conf_t *conf_init(void)
{
	conf_t *self;
	ini_t *ini;
	char *tmp, **splitted;
	int nsplits;
	unsigned i;
	dict_t *vars;

	self = xmalloc(sizeof(conf_t));
	self->post_pkgadd = NULL;
	self->ask_for_update = 1;
	self->not_found_policy = ASK;

	ini = ini_new(CONF_FILE);
	ini_add(ini, "ilenia");
	ini_set_default(ini, "ilenia", "post_pkgadd", xstrdup(""));
	ini_set_default(ini, "ilenia", "ask_for_update", xstrdup("1"));
	ini_set_default(ini, "ilenia", "not_found_policy", xstrdup("ASK"));
	ini_set_default(ini, "ilenia", "repositories_hierarchy", xstrdup(""));
	ini_set_default(ini, "ilenia", "enable_colors", xstrdup("Yes"));
	ini_set_default(ini, "ilenia", "verbose", xstrdup("No"));
	ini_add(ini, "favourite_repositories");
	ini_add(ini, "locked_versions");
	ini_add(ini, "aliases");
	ini_add(ini, "pkgmk_confs");
	ini_load(ini);

	if ((tmp = getenv("POST_PKGADD")) == NULL)
		tmp = ini_get(ini, "ilenia", "post_pkgadd");
	self->post_pkgadd = xstrdup(tmp);

	if ((tmp = getenv("ASK_FOR_UPDATE")) == NULL)
		tmp = ini_get(ini, "ilenia", "ask_for_update");
	if (!strcasecmp(tmp, "NO"))
		self->ask_for_update = 0;
	else
		self->ask_for_update = 1;

	if ((tmp = getenv("NOT_FOUND_POLICY")) == NULL)
		tmp = ini_get(ini, "ilenia", "not_found_policy");
	if (!strcasecmp(tmp, "NEVERMIND"))
		self->not_found_policy = NEVERMIND;
	else if (!strcasecmp(tmp, "STOP"))
		self->not_found_policy = STOP;
	else
		self->not_found_policy = ASK;

	tmp = ini_get(ini, "ilenia", "repositories_hierarchy");
	if (tmp != NULL) {
		strreplace(&tmp, "\t", " ", -1);
		while (strstr(tmp, "  "))
			strreplace(&tmp, "  ", " ", -1);

		splitted = NULL;
		nsplits = strsplit(tmp, ' ', &splitted);
		self->repositories_hierarchy =
		    list_new_from_array((void **)splitted, nsplits);
		free(splitted);
	} else
		self->repositories_hierarchy = list_new();

	if ((tmp = getenv("ENABLE_COLORS")) == NULL)
		tmp = ini_get(ini, "ilenia", "enable_colors");
	if (!strcasecmp(tmp, "NO"))
		self->enable_colors = 0;
	else
		self->enable_colors = 1;

	if ((tmp = getenv("VERBOSE")) == NULL)
		tmp = ini_get(ini, "ilenia", "verbose");
	if (strcasecmp(tmp, "Yes") == 0)
		self->verbose = 1;
	else
		self->verbose = 0;

	self->favourite_repositories = dict_new();
	vars = ini_get_vars(ini, "favourite_repositories");

	for (i = 0; vars && i < vars->length; i++)
		dict_add(self->favourite_repositories, vars->elements[i]->key,
			 xstrdup(vars->elements[i]->value));

	self->locked_versions = dict_new();
	vars = ini_get_vars(ini, "locked_versions");
	for (i = 0; vars && i < vars->length; i++)
		dict_add(self->locked_versions, vars->elements[i]->key,
			 xstrdup(vars->elements[i]->value));

	self->aliases = dict_new();
	vars = ini_get_vars(ini, "aliases");
	for (i = 0; vars != NULL && i < vars->length; i++) {
		tmp = vars->elements[i]->value;

		strreplace(&tmp, "\t", " ", -1);
		while (strstr(tmp, "  "))
			strreplace(&tmp, "  ", " ", -1);

		splitted = NULL;
		nsplits = strsplit(tmp, ' ', &splitted);
		if (nsplits == 0)
			continue;
		dict_add(self->aliases, vars->elements[i]->key,
			 list_new_from_array((void **)splitted, nsplits));
		free(splitted);
	}

	self->pkgmk_confs = dict_new();
	vars = ini_get_vars(ini, "pkgmk_confs");

	for (i = 0; vars && i < vars->length; i++)
		dict_add(self->pkgmk_confs, vars->elements[i]->key,
			 xstrdup(vars->elements[i]->value));

	ini_free(ini);
	return self;
}