Files
repmgr/config.c
Dan Farina ec73a07e2f Make various buffers larger
MAXLEN definitely needed to be bigger to properly format fairly common
connection strings.  In addition, the reliance on xsnprintf helps
detect cases where even this buffer is not long enough.

the buffer in parse_config has been made bigger in a bit of sloppy
programming, but what really needs to happen is reporting when a line
cannot be properly parsed/is too big for the buffer.  That is just a
kludge.

Signed-off-by: Dan Farina <drfarina@acm.org>
Signed-off-by: Peter van Hardenberg <pvh@heroku.com>
2010-12-21 15:19:28 -08:00

107 lines
1.8 KiB
C

/*
* config.c
*
* Copyright (c) 2ndQuadrant, 2010
* Copyright (c) Heroku, 2010
*
* Functions to parse the config file
*/
#include "repmgr.h"
#include "strutil.h"
void
parse_config(const char *config_file, char *cluster_name, int *node,
char *conninfo)
{
char *s, buff[1024];
FILE *fp = fopen (config_file, "r");
if (fp == NULL)
return;
/* Read next line */
while ((s = fgets (buff, sizeof buff, fp)) != NULL)
{
char name[MAXLEN];
char value[MAXLEN];
/* Skip blank lines and comments */
if (buff[0] == '\n' || buff[0] == '#')
continue;
/* Parse name/value pair from line */
parse_line(buff, name, value);
/* Copy into correct entry in parameters struct */
if (strcmp(name, "cluster") == 0)
strncpy(cluster_name, value, MAXLEN);
else if (strcmp(name, "node") == 0)
*node = atoi(value);
else if (strcmp(name, "conninfo") == 0)
strncpy(conninfo, value, MAXLEN);
else
printf("WARNING: %s/%s: Unknown name/value pair!\n",
name, value);
}
/* Close file */
fclose (fp);
}
char *
trim (char *s)
{
/* Initialize start, end pointers */
char *s1 = s, *s2 = &s[strlen (s) - 1];
/* Trim and delimit right side */
while ( (isspace (*s2)) && (s2 >= s1) )
s2--;
*(s2+1) = '\0';
/* Trim left side */
while ( (isspace (*s1)) && (s1 < s2) )
s1++;
/* Copy finished string */
strcpy (s, s1);
return s;
}
void
parse_line(char *buff, char *name, char *value)
{
int i;
int j;
/*
* first we find the name of the parameter
*/
j = 0;
for (i = 0; i < MAXLEN; i++)
{
if (buff[i] != '=')
name[j++] = buff[i];
else
break;
}
name[j] = '\0';
i++;
/*
* Now the value
*/
j = 0;
for ( ; i < MAXLEN; i++)
if (buff[i] == '\'')
continue;
else if (buff[i] != '\n')
value[j++] = buff[i];
else
break;
value[j] = '\0';
trim(value);
}