Example #1
0
static int parse_line(char* line, int line_count, void* ptr_data)
{
	struct linked_list* users = (struct linked_list*) ptr_data;
	struct cfg_tokens* tokens = cfg_tokenize(line);
	enum auth_credentials cred;
	char* credential;
	char* username;
	char* password;

	if (cfg_token_count(tokens) == 0)
	{
		cfg_tokens_free(tokens);
		return 0;
	}

	if (cfg_token_count(tokens) < 2)
	{
		cfg_tokens_free(tokens);
		return -1;
	}

	credential = cfg_token_get_first(tokens);
	username   = cfg_token_get_next(tokens);
	password   = cfg_token_get_next(tokens);

	if (!auth_string_to_cred(credential, &cred))
	{
		cfg_tokens_free(tokens);
		return -1;
	}

	insert_user(users, username, password, cred);
	cfg_tokens_free(tokens);
	return 0;
}
Example #2
0
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
{
	struct hub_info* hub = (struct hub_info*) ptr_data;
	struct uhub_plugins* handle = hub->plugins;
	struct cfg_tokens* tokens = cfg_tokenize(line);
	struct plugin_handle* plugin;
	char *directive, *soname, *params;

	if (cfg_token_count(tokens) == 0)
	{
		cfg_tokens_free(tokens);
		return 0;
	}

	if (cfg_token_count(tokens) < 2)
	{
		cfg_tokens_free(tokens);
		return -1;
	}

	directive = cfg_token_get_first(tokens);
	soname    = cfg_token_get_next(tokens);
	params    = cfg_token_get_next(tokens);

	if (strcmp(directive, "plugin") == 0 && soname && *soname)
	{
		if (!params)
			params = "";

		LOG_PLUGIN("Load plugin: \"%s\", params=\"%s\"", soname, params);
		plugin = plugin_load(soname, params, hub);
		if (plugin)
		{
			list_append(handle->loaded, plugin);
			cfg_tokens_free(tokens);
			return 0;
		}
	}

	cfg_tokens_free(tokens);
	return -1;
}
Example #3
0
struct cfg_settings* cfg_settings_split(const char* line)
{
	struct cfg_settings* s = NULL;
	struct cfg_tokens* tok = NULL;
	char* pos = NULL;

	if (   !line
		|| !*line
		|| ((pos = (char*) strchr(line, '=')) == NULL)
		|| ((s = hub_malloc_zero(sizeof(struct cfg_settings))) == NULL)
		|| ((tok = cfg_tokenize(line)) == NULL)
		|| (cfg_token_count(tok) < 1)
		|| (cfg_token_count(tok) > 3)
		|| (cfg_token_count(tok) == 3 && strcmp(cfg_token_get(tok, 1), "="))
		)
	{
		cfg_tokens_free(tok);
		cfg_settings_free(s);
		return NULL;
	}

	if (cfg_token_count(tok) == 1)
	{
		char* key = cfg_token_get_first(tok);
		pos = strchr(key, '=');
		if (!pos)
		{
			cfg_tokens_free(tok);
			cfg_settings_free(s);
			return NULL;
		}

		pos[0] = 0;
		key = strip_white_space(key);

		if (!*key)
		{
			cfg_tokens_free(tok);
			cfg_settings_free(s);
			return NULL;
		}

		s->key = strdup(key);
		s->value = strdup(strip_white_space(pos+1));
	}
	else if (cfg_token_count(tok) == 2)
	{
		char* key = cfg_token_get_first(tok);
		char* val = cfg_token_get_next(tok);

                if ((pos = strchr(key, '=')))
		{
			pos[0] = 0;
			key = strip_white_space(key);
		}
		else if ((pos = strchr(val, '=')))
		{
			val = strip_white_space(pos+1);
		}
		else
		{
			cfg_tokens_free(tok);
			cfg_settings_free(s);
			return NULL;
		}

		if (!*key)
		{
			cfg_tokens_free(tok);
			cfg_settings_free(s);
			return NULL;
		}

		s->key = strdup(key);
		s->value = strdup(val);
	}
	else
	{
		s->key = strdup(strip_white_space(cfg_token_get(tok, 0)));
		s->value = strdup(strip_white_space(cfg_token_get(tok, 2)));
	}
	cfg_tokens_free(tok);
	return s;
}