summaryrefslogtreecommitdiffstats
path: root/lib/parse_ini.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r--lib/parse_ini.c41
1 files changed, 35 insertions, 6 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 70da7f14..e318ec32 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -30,6 +30,9 @@
30#include "utils_base.h" 30#include "utils_base.h"
31#include <ctype.h> 31#include <ctype.h>
32 32
33/* FIXME: N::P dies if section is not found */
34/* FIXME: N::P dies if config file is not found */
35
33/* np_ini_info contains the result of parsing a "locator" in the format 36/* np_ini_info contains the result of parsing a "locator" in the format
34 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example) 37 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
35 */ 38 */
@@ -45,16 +48,21 @@ typedef struct {
45static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); 48static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
46/* internal function that converts a single line into options format */ 49/* internal function that converts a single line into options format */
47static int add_option(FILE *f, np_arg_list **optlst); 50static int add_option(FILE *f, np_arg_list **optlst);
51/* internal function to find default file */
52static char* default_file(void);
48 53
49/* parse_locator decomposes a string of the form 54/* parse_locator decomposes a string of the form
50 * [stanza][@filename] 55 * [stanza][@filename]
51 * into its seperate parts 56 * into its seperate parts
52 */ 57 */
53static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){ 58static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
54 size_t locator_len, stanza_len; 59 size_t locator_len=0, stanza_len=0;
55 60
56 locator_len=strlen(locator); 61 /* if locator is NULL we'll use default values */
57 stanza_len=strcspn(locator, "@"); 62 if(locator){
63 locator_len=strlen(locator);
64 stanza_len=strcspn(locator, "@");
65 }
58 /* if a non-default stanza is provided */ 66 /* if a non-default stanza is provided */
59 if(stanza_len>0){ 67 if(stanza_len>0){
60 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1)); 68 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
@@ -65,7 +73,7 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
65 } 73 }
66 /* if there is no @file part */ 74 /* if there is no @file part */
67 if(stanza_len==locator_len){ 75 if(stanza_len==locator_len){
68 i->file=strdup(NP_DEFAULT_INI_PATH); 76 i->file=default_file();
69 } else { 77 } else {
70 i->file=strdup(&(locator[stanza_len+1])); 78 i->file=strdup(&(locator[stanza_len+1]));
71 } 79 }
@@ -75,7 +83,7 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
75 } 83 }
76} 84}
77 85
78/* this is the externally visible function used by plugins */ 86/* this is the externally visible function used by extra_opts */
79np_arg_list* np_get_defaults(const char *locator, const char *default_section){ 87np_arg_list* np_get_defaults(const char *locator, const char *default_section){
80 FILE *inifile=NULL; 88 FILE *inifile=NULL;
81 np_arg_list *defaults=NULL; 89 np_arg_list *defaults=NULL;
@@ -89,7 +97,7 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
89 } else { 97 } else {
90 inifile=fopen(i.file, "r"); 98 inifile=fopen(i.file, "r");
91 } 99 }
92 if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error")); 100 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
93 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */ 101 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
94 /* We got nothing, try the default section */ 102 /* We got nothing, try the default section */
95 rewind(inifile); 103 rewind(inifile);
@@ -287,3 +295,24 @@ static int add_option(FILE *f, np_arg_list **optlst){
287 return 0; 295 return 0;
288} 296}
289 297
298static char* default_file(void){
299 char *np_env=NULL;
300
301 /* FIXME: STUB */
302 return "";
303#if 0
304 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
305 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
306 * every PATHs defined (colon-separated).
307 */
308 }
309 if !file_found
310 search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1;
311 if !file_found
312 search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2;
313 if !file_found
314 return empty string (or null if we want to die);
315 return file name;
316#endif
317}
318