Exemplo n.º 1
0
static errcode_t parse_init_state(struct parse_state *state)
{
    state->state = STATE_INIT_COMMENT;
    state->group_level = 0;

    return profile_create_node("(root)", 0, &state->root_section);
}
Exemplo n.º 2
0
long profile_update_file(prf_file_t prf)
{
	long retval;
	FILE *f;
	char buf[2048];
	struct parse_state state;

	if (prf->flags & PROFILE_FILE_NO_RELOAD)
		return 0;

	if (prf->root) {
	    return 0;
	}

	memset(&state, 0, sizeof(struct parse_state));
	retval = profile_create_node("(root)", 0, &state.root_section);
	if (retval)
		return retval;
	errno = 0;
	f = fopen(prf->filespec, "r");
	if (f == NULL) {
		retval = errno;
		if (retval == 0)
			retval = ENOENT;
		return retval;
	}
	prf->upd_serial++;
	while (!feof(f)) {
		if (fgets(buf, sizeof(buf), f) == NULL)
			break;
		retval = parse_line(buf, &state);
		if (retval) {
			if (syntax_err_cb)
				(syntax_err_cb)(prf->filespec, retval,
						state.line_num);
			fclose(f);
			return retval;
		}
	}
	prf->root = state.root_section;

	fclose(f);

	return 0;
}
Exemplo n.º 3
0
errcode_t profile_process_directory(const char *dirname,
                                    struct profile_node **root)
{
    errcode_t retval;
    struct profile_node *node;

    *root = NULL;
    retval = profile_create_node("(root)", 0, &node);
    if (retval)
        return retval;
    retval = parse_include_dir(dirname, node);
    if (retval) {
        profile_free_node(node);
        return retval;
    }
    *root = node;
    return 0;
}
Exemplo n.º 4
0
errcode_t profile_parse_file(FILE *f, struct profile_node **root,
                             char **ret_modspec)
{
    struct parse_state state;
    errcode_t retval;

    *root = NULL;

    /* Initialize parsing state with a new root node. */
    state.state = STATE_INIT_COMMENT;
    state.group_level = 0;
    state.current_section = NULL;
    retval = profile_create_node("(root)", 0, &state.root_section);
    if (retval)
        return retval;

    retval = parse_file(f, &state, ret_modspec);
    if (retval) {
        profile_free_node(state.root_section);
        return retval;
    }
    *root = state.root_section;
    return 0;
}
Exemplo n.º 5
0
/*
 * This function sets the value of the pseudo file "<default>".  If
 * the file "<default>" had previously been passed to profile_init(),
 * then def_string parameter will be parsed and used as the profile
 * information for the "<default>" file.
 */
long profile_set_default(profile_t profile, const char *def_string)
{
	struct parse_state	state;
	prf_file_t		prf;
	long		retval;
	const char		*in;
	char			*line, *p, *end;
	int			line_size, len;

	if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
		return PROF_MAGIC_PROFILE;

	for (prf = profile->first_file; prf; prf = prf->next) {
		if (strcmp(prf->filespec, default_filename) == 0)
			break;
	}
	if (!prf)
		return 0;

	if (prf->root) {
		profile_free_node(prf->root);
		prf->root = 0;
	}

	memset(&state, 0, sizeof(struct parse_state));
	retval = profile_create_node("(root)", 0, &state.root_section);
	if (retval)
		return retval;

	line = 0;
	line_size = 0;
	in = def_string;
	while (*in) {
		end = strchr(in, '\n');
		len = end ? (end - in) : (int) strlen(in);
		if (len >= line_size) {
			line_size = len+1;
			p = realloc(line, line_size);
			if (!p) {
				retval = ENOMEM;
				goto errout;
			}
			line = p;
		}
		memcpy(line, in, len);
		line[len] = 0;
		retval = parse_line(line, &state);
		if (retval) {
		errout:
			if (syntax_err_cb)
				(syntax_err_cb)(prf->filespec, retval,
						state.line_num);
			free(line);
			if (prf->root)
				profile_free_node(prf->root);
			return retval;
		}
		if (!end)
			break;
		in = end+1;
	}
	prf->root = state.root_section;
	free(line);

	return 0;
}