Code

check_disk: rerpopulate the mount list after doing a stat() on paths specified with...
[nagiosplug.git] / lib / parse_ini.c
index 67d8367f5503c21a7fd5cd3217bcdd60f055ac6b..57321753c5b0bff692d4b621d82de2eee7336ae4 100644 (file)
 *****************************************************************************/
 
 #include "common.h"
-#include "parse_ini.h"
 #include "utils_base.h"
+#include "parse_ini.h"
 #include <ctype.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-/* FIXME: N::P dies if section is not found */
-/* FIXME: N::P dies if config file 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
  * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
@@ -99,19 +98,16 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
        /* if a file was specified or if we're using the default file */
        if(i.file != NULL && strlen(i.file) > 0){
                if(strcmp(i.file, "-")==0){
-                       inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
+                       inifile=stdin;
                } else {
                        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!=stdout) { /* FIXME: Shouldn't it be '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!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
+               if(inifile!=stdin) fclose(inifile);
        }
        free(i.stanza);
        return defaults;        
@@ -136,6 +132,7 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
                if(isspace(c)) continue;
                switch(c){
                        /* globble up coment lines */
+                       case ';':
                        case '#':
                                GOBBLE_TO(f, c, '\n');
                                break;
@@ -236,10 +233,8 @@ static int add_option(FILE *f, np_arg_list **optlst){
        if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
        /* continue from '=' to start of value or EOL */
        for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
-       /* continue to the end of value (FIXME: watching for trailing comments) */
-       for(valend=valptr; valend<lineend; valend++)
-               /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
-               if(*valend=='#') break;
+       /* continue to the end of value */
+       for(valend=valptr; valend<lineend; valend++);
        --valend;
        /* Finally trim off trailing spaces */
        for(valend; isspace(*valend); valend--);
@@ -308,7 +303,7 @@ static char* default_file(void){
        size_t len;
 
        if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
-               /* skip ant starting colon... */
+               /* skip any starting colon... */
                while(*np_env==':') np_env++;
                /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
                 * every PATHs defined (colon-separated).
@@ -344,8 +339,8 @@ static char* default_file(void){
                        default_file=strdup(temp_file);
        }
 
-       /* Return default_file or empty string (should return NULL if we want to
-        * die there...
+       /* Return default_file or empty string (should return NULL if we want plugins
+        * to die there)...
         */
        if(default_file)
                return default_file;
@@ -358,7 +353,7 @@ static char* default_file(void){
 static int test_file(const char* env, int len, const char* file, char* temp_file){
        struct stat sb;
 
-       /* test for len + filelen + '/' + '\0' */
+       /* test if len + filelen + '/' + '\0' fits in temp_file */
        if((len+strlen(file)+2)>MAX_INPUT_BUFFER)       return -1;
 
        strncpy(temp_file,env,len);