예제 #1
0
static void test_utils_tokenize_ignore_comments(void)
{
	int nb_token;
	char line[BUFSIZ];
	char *tokens[DEFAULT_MAX_CONF_TOKEN];

	diag("Utils tokenize line test");

	helper_reset_tokens(tokens);
	strcpy(line, "foo bar");
	nb_token = utils_tokenize_ignore_comments(line, sizeof(tokens), tokens);
	ok(nb_token == 2 &&
		(0 == strcmp(tokens[0], "foo")) &&
		(0 == strcmp(tokens[1], "bar")),
		"Returns 2 tokens");

	helper_reset_tokens(tokens);
	strcpy(line, "a b c");
	nb_token = utils_tokenize_ignore_comments(line, sizeof(tokens), tokens);
	ok(nb_token == 3 &&
		(0 == strcmp(tokens[0], "a")) &&
		(0 == strcmp(tokens[1], "b")) &&
		(0 == strcmp(tokens[2], "c")),
		"Returns 3 tokens");

	helper_reset_tokens(tokens);
	strcpy(line, "# this is a comment");
	nb_token = utils_tokenize_ignore_comments(line, sizeof(tokens), tokens);
	ok(nb_token == 0, "Returns 0 tokens for comment");
}
예제 #2
0
/*
 * Parse a single line of from a configuration file and set the value found in
 * the configuration object.
 *
 * Return 0 on success or else a negative value.
 */
static int parse_config_line(const char *line, struct configuration *config)
{
	int ret, nb_token;
	char *tokens[DEFAULT_MAX_CONF_TOKEN];

	assert(line);
	assert(config);

	/*
	 * The line is tokenized and each token is NULL terminated.
	 */
	nb_token = utils_tokenize_ignore_comments(line, sizeof(tokens), tokens);
	if (nb_token <= 0) {
		/* Nothing on this line that is useful to parse. */
		ret = 0;
		goto end;
	}

	if (!strcmp(tokens[0], conf_toraddr_str)) {
		ret = set_tor_address(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_torport_str)) {
		ret = set_tor_port(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_onion_str)) {
		ret = set_onion_info(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_socks5_user_str)) {
		ret = conf_file_set_socks5_user(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_socks5_pass_str)) {
		ret = conf_file_set_socks5_pass(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_allow_inbound_str)) {
		ret = conf_file_set_allow_inbound(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_allow_outbound_localhost_str)) {
		ret = conf_file_set_allow_outbound_localhost(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else if (!strcmp(tokens[0], conf_isolate_pid_str)) {
		ret = conf_file_set_isolate_pid(tokens[1], config);
		if (ret < 0) {
			goto error;
		}
	} else {
		WARN("Config file contains unknown value: %s", line);
	}

	/* Everything went well. */
	ret = 0;

end:
error:
	return ret;
}