From 2a4640f6719b9688a68b796f697209b9b14cd179 Mon Sep 17 00:00:00 2001
From: Thomas Guyot-Sionnest <dermoth@users.sourceforge.net>
Date: Fri, 4 Apr 2008 10:11:22 +0000
Subject: make parse_ini die like in N::P if section isn't found

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1976 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 8c17fa4..b555417 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -35,11 +35,11 @@ int
 is_option2 (char *str)
 {
 	if (!str)
-		return 0;
+		return FALSE;
 	else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
-		return 1;
+		return TRUE;
 	else
-		return 0;
+		return FALSE;
 }
 
 /* this is the externally visible function used by plugins */
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index d5661c9..96551cf 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -34,7 +34,6 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-/* TODO: die like N::P if section is not found */
 /* TODO: die like N::P if config file is not found */
 
 /* np_ini_info contains the result of parsing a "locator" in the format
@@ -104,11 +103,8 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
 			inifile=fopen(i.file, "r");
 		}
 		if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
-		if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdin) {
-			/* We got nothing, try the default section */
-			rewind(inifile);
-			read_defaults(inifile, default_section, &defaults);
-		}
+		if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
+			die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
 
 		free(i.file);
 		if(inifile!=stdin) fclose(inifile);
diff --git a/lib/tests/test_ini.c b/lib/tests/test_ini.c
index 80396ca..1ab1e5d 100644
--- a/lib/tests/test_ini.c
+++ b/lib/tests/test_ini.c
@@ -55,7 +55,7 @@ main (int argc, char **argv)
 {
 	char *optstr=NULL;
 
-	plan_tests(12);
+	plan_tests(11);
 
 	optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
 	ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected");
@@ -65,9 +65,13 @@ main (int argc, char **argv)
 	ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific");
 	my_free(optstr);
 
-	optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
-	ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name over specified one");
-	my_free(optstr);
+	/*
+	 * This check is expected to die - It's commented so we can eventually put
+	 * it in a separate file for testing the return value
+	 */
+	/* optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
+	ok( 0, "die if section isn't found");
+	my_free(optstr); */
 
 	optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
 	ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c
index 61370a9..e878abd 100644
--- a/lib/tests/test_opts.c
+++ b/lib/tests/test_opts.c
@@ -59,7 +59,7 @@ main (int argc, char **argv)
 	char **argv_test=NULL, **argv_known=NULL;
 	int i, argc_test;
 
-	plan_tests(12);
+	plan_tests(9);
 
 	argv_test=(char **)malloc(2*sizeof(char **));
 	argv_test[0] = "prog_name";
@@ -145,10 +145,10 @@ main (int argc, char **argv)
 	ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
 	my_free(&argc_test,argv_test);
 
-	/* Next three checks should die according to N::P - for now they're useful
-	 * to test code is working properly (i.e. no srash or unexpected behavior)
+	/* Next three checks dre expected to die. They are commented out as they
+	 * could possibly go in a sepatare test checked for return value.
 	 */
-	argv_test=(char **)malloc(6*sizeof(char **));
+	/* argv_test=(char **)malloc(6*sizeof(char **));
 	argv_test[0] = "prog_name";
 	argv_test[1] = "arg1";
 	argv_test[2] = "--extra-opts=missing@./config-opts.ini";
@@ -164,9 +164,9 @@ main (int argc, char **argv)
 	argv_known[4] = NULL;
 	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
 	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
-	my_free(&argc_test,argv_test);
+	my_free(&argc_test,argv_test); */
 
-	argv_test=(char **)malloc(7*sizeof(char **));
+	/* argv_test=(char **)malloc(7*sizeof(char **));
 	argv_test[0] = "prog_name";
 	argv_test[1] = "arg1";
 	argv_test[2] = "--extra-opts";
@@ -183,9 +183,9 @@ main (int argc, char **argv)
 	argv_known[4] = NULL;
 	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
 	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
-	my_free(&argc_test,argv_test);
+	my_free(&argc_test,argv_test); */
 
-	argv_test=(char **)malloc(6*sizeof(char **));
+	/* argv_test=(char **)malloc(6*sizeof(char **));
 	argv_test[0] = "prog_name";
 	argv_test[1] = "arg1";
 	argv_test[2] = "--extra-opts";
@@ -201,7 +201,7 @@ main (int argc, char **argv)
 	argv_known[4] = NULL;
 	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
 	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
-	my_free(&argc_test,argv_test);
+	my_free(&argc_test,argv_test); */
 
 	setenv("NAGIOS_CONFIG_PATH", ".", 1);
 	argv_test=(char **)malloc(6*sizeof(char **));
@@ -257,8 +257,8 @@ main (int argc, char **argv)
 	argv_test=(char **)malloc(6*sizeof(char **));
 	argv_test[0] = "prog_name";
 	argv_test[1] = "arg1";
-	argv_test[2] = "--extra-opts=section2";
-	argv_test[3] = "--arg3";
+	argv_test[2] = "--extra-opts";
+	argv_test[3] = "-arg3";
 	argv_test[4] = "val2";
 	argv_test[5] = NULL;
 	argc_test=5;
@@ -266,11 +266,11 @@ main (int argc, char **argv)
 	argv_known[0] = "prog_name";
 	argv_known[1] = "--foo=bar";
 	argv_known[2] = "arg1";
-	argv_known[3] = "--arg3";
+	argv_known[3] = "-arg3";
 	argv_known[4] = "val2";
 	argv_known[5] = NULL;
 	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
-	ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 3");
+	ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 4");
 	my_free(&argc_test,argv_test);
 
 	return exit_status();
-- 
cgit v0.10-9-g596f

