From 11bfb0def2e216eece4b680eeb91a671099a46e5 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 10:37:10 +0200 Subject: lib/parse_ini.[ch]: Change code formatting Change the indentation and formatting of the code in lib/parse_ini.c. This breaks patches against that file and makes it harder to track its history, but it (hopefully) improves readability a lot. diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 51ad2c1..a5b3d30 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -24,8 +24,8 @@ #include "common.h" #include "utils_base.h" #include "parse_ini.h" -#include +#include #include #include #include @@ -64,63 +64,71 @@ static char *default_ini_path_names[] = { /* internal function that returns the constructed defaults options */ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); + /* internal function that converts a single line into options format */ static int add_option(FILE *f, np_arg_list **optlst); + /* internal function to find default file */ -static char* default_file(void); +static char *default_file(void); /* parse_locator decomposes a string of the form * [stanza][@filename] * into its seperate parts */ -static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){ - size_t locator_len=0, stanza_len=0; +static void +parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) +{ + size_t locator_len = 0, stanza_len = 0; /* if locator is NULL we'll use default values */ - if(locator){ - locator_len=strlen(locator); - stanza_len=strcspn(locator, "@"); + if (locator != NULL) { + locator_len = strlen(locator); + stanza_len = strcspn(locator, "@"); } /* if a non-default stanza is provided */ - if(stanza_len>0){ - i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1)); + if (stanza_len > 0) { + i->stanza = malloc(sizeof(char) * (stanza_len + 1)); strncpy(i->stanza, locator, stanza_len); - i->stanza[stanza_len]='\0'; - } else { /* otherwise we use the default stanza */ - i->stanza=strdup(def_stanza); - } - if(i->stanza==NULL){ + i->stanza[stanza_len] = '\0'; + } else /* otherwise we use the default stanza */ + i->stanza = strdup(def_stanza); + + if (i->stanza == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); - } - /* if there is no @file part */ - if(stanza_len==locator_len){ - i->file=default_file(); - } else { - i->file=strdup(&(locator[stanza_len+1])); - } - if(i->file==NULL || i->file[0]=='\0'){ - die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); - } + + /* check whether there's an @file part */ + i->file = stanza_len == locator_len + ? default_file() + : strdup(&(locator[stanza_len + 1])); + if (i->file == NULL || i->file[0] == '\0') + die(STATE_UNKNOWN, + _("Cannot find config file in any standard location.\n")); } /* this is the externally visible function used by extra_opts */ -np_arg_list* np_get_defaults(const char *locator, const char *default_section){ - FILE *inifile=NULL; - np_arg_list *defaults=NULL; +np_arg_list * +np_get_defaults(const char *locator, const char *default_section) +{ + FILE *inifile = NULL; + np_arg_list *defaults = NULL; np_ini_info i; parse_locator(locator, default_section, &i); - if(strcmp(i.file, "-")==0){ - inifile=stdin; - } else { - inifile=fopen(i.file, "r"); - } - if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("Can't read config file")); - if(read_defaults(inifile, i.stanza, &defaults)==FALSE) - die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); + if (strcmp(i.file, "-") == 0) + inifile = stdin; + else + inifile = fopen(i.file, "r"); + + if (inifile == NULL) + die(STATE_UNKNOWN, "%s\n", _("Can't read config file")); + 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); + if (inifile != stdin) + fclose(inifile); free(i.stanza); return defaults; } @@ -131,67 +139,76 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){ * be extra careful about user-supplied input (i.e. avoiding possible * format string vulnerabilities, etc) */ -static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){ - int c, status=FALSE; +static int +read_defaults(FILE *f, const char *stanza, np_arg_list **opts) +{ + int c, status = FALSE; size_t i, stanza_len; - enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA; + enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; - stanza_len=strlen(stanza); + stanza_len = strlen(stanza); /* our little stanza-parsing state machine. */ - while((c=fgetc(f))!=EOF){ + while ((c = fgetc(f)) != EOF) { /* gobble up leading whitespace */ - if(isspace(c)) continue; - switch(c){ + if (isspace(c)) + continue; + switch (c) { /* globble up coment lines */ - case ';': - case '#': - GOBBLE_TO(f, c, '\n'); - break; + case ';': + case '#': + GOBBLE_TO(f, c, '\n'); + break; /* start of a stanza. check to see if it matches */ - case '[': - stanzastate=WRONGSTANZA; - for(i=0; i= linebuf_sz){ - linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz; - linebuf=realloc(linebuf, linebuf_sz); - if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); + if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) { + linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz; + linebuf = realloc(linebuf, linebuf_sz); + if (linebuf == NULL) + die(STATE_UNKNOWN, _("malloc() failed!\n")); } - if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1; + if (fgets(&linebuf[read_pos], read_sz, f) == NULL) + done_reading = 1; else { - read_pos=strlen(linebuf); - if(linebuf[read_pos-1]=='\n') { - linebuf[--read_pos]='\0'; - done_reading=1; + read_pos = strlen(linebuf); + if (linebuf[read_pos - 1] == '\n') { + linebuf[--read_pos] = '\0'; + done_reading = 1; } } } - lineend=&linebuf[read_pos]; + lineend = &linebuf[read_pos]; /* all that to read one line. isn't C fun? :) now comes the parsing :/ */ /* skip leading whitespace */ - for(optptr=linebuf; optptrnext=NULL; + optnew = malloc(sizeof(np_arg_list)); + optnew->next = NULL; - read_pos=0; - optnew->arg=malloc(cfg_len+1); + read_pos = 0; + optnew->arg = malloc(cfg_len + 1); /* 1-character params needs only one dash */ - if(opt_len==1) { + if (opt_len == 1) { strncpy(&optnew->arg[read_pos], "-", 1); - read_pos+=1; + read_pos += 1; } else { strncpy(&optnew->arg[read_pos], "--", 2); - read_pos+=2; + read_pos += 2; } - strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len; - if(value) { - optnew->arg[read_pos++]='='; - strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len; + strncpy(&optnew->arg[read_pos], optptr, opt_len); + read_pos += opt_len; + if (value) { + optnew->arg[read_pos++] = '='; + strncpy(&optnew->arg[read_pos], valptr, val_len); + read_pos += val_len; } - optnew->arg[read_pos]='\0'; + optnew->arg[read_pos] = '\0'; /* ...and put that to the end of the list */ - if(*optlst==NULL) { - *optlst=optnew; - } else { - while(opttmp->next!=NULL) { - opttmp=opttmp->next; - } + if (*optlst == NULL) + *optlst = optnew; + else { + while (opttmp->next != NULL) + opttmp = opttmp->next; opttmp->next = optnew; } @@ -308,20 +340,22 @@ static int add_option(FILE *f, np_arg_list **optlst){ return 0; } -static char *default_file_in_path(void){ +static char * +default_file_in_path(void) +{ char *config_path, **file; char *dir, *ini_file, *tokens; - if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL) + if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) return NULL; - if((tokens=strdup(config_path))==NULL) + if ((tokens = strdup(config_path)) == NULL) die(STATE_UNKNOWN, _("Insufficient Memory")); - for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){ - for(file=default_ini_file_names; *file!=NULL; file++){ - if((asprintf(&ini_file, "%s/%s", dir, *file))<0) + for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) { + for (file = default_ini_file_names; *file != NULL; file++) { + if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0) die(STATE_UNKNOWN, _("Insufficient Memory")); - if(access(ini_file, F_OK)==0){ + if (access(ini_file, F_OK) == 0) { free(tokens); return ini_file; } @@ -331,14 +365,16 @@ static char *default_file_in_path(void){ return NULL; } -static char *default_file(void){ +static char * +default_file(void) +{ char **p, *ini_file; - if((ini_file=getenv("MP_CONFIG_FILE"))!=NULL || - (ini_file=default_file_in_path())!=NULL) + if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || + (ini_file = default_file_in_path()) != NULL) return ini_file; - for(p=default_ini_path_names; *p!=NULL; p++) - if (access(*p, F_OK)==0) + for (p = default_ini_path_names; *p != NULL; p++) + if (access(*p, F_OK) == 0) return *p; return NULL; } diff --git a/lib/parse_ini.h b/lib/parse_ini.h index 8b67ea3..e37601b 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h @@ -16,7 +16,7 @@ typedef struct np_arg_el { /* np_load_defaults: load the default configuration (if present) for * a plugin from the ini file */ -np_arg_list* np_get_defaults(const char *locator, const char *default_section); +np_arg_list *np_get_defaults(const char *locator, const char *default_section); #endif /* _PARSE_INI_H_ */ -- cgit v0.10-9-g596f