dnxCfgParser.c File Reference

Parses a standard Unix config file. More...

#include "dnxCfgParser.h"
#include "dnxLogging.h"
#include "dnxError.h"
#include "dnxDebug.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>

Go to the source code of this file.

Data Structures

struct  iDnxCfgParser
 The internal type of a DNX configuration parser object. More...

Defines

#define elemcount(x)   (sizeof(x)/sizeof(*(x)))
#define DNX_MAX_CFG_LINE   2048
 Longest allowed config file line.
#define INTSWAP(a, b)   do { int n = (a); (a) = (int)(intptr_t)(b); (b) = (void *)(intptr_t)n; } while(0)
#define PTRSWAP(a, b)   do { void * p = (a); (a) = (b); (b) = p; } while (0)

Typedefs

typedef int DnxVarParser_t (char *val, DnxCfgType type, void *prval)
 A typedef describing a single type variable parser.
typedef char * DnxVarFormatter_t (char *var, DnxCfgType type, void *prval)
 A typedef describing a single type variable formatter.

Functions

static char ** strToStrArray (char *str, char delim)
 Convert a delimited string into a null-terminated array of strings.
static DnxCfgDictcopyDictionary (DnxCfgDict *dict, unsigned *dictszp)
 Make a dynamic copy of a dnx configuration dictionary.
static int validateURL (char *url)
 Validate a URL for correctness.
static int validateFSPath (char *path)
 Validate a file system path for correctness.
static int parseString (char *val, DnxCfgType type, char **prval)
 Validate and return a copy of a string.
static char * strtrim (char *s)
 Trim leading and trailing white space from a string.
static int parseStringArray (char *val, DnxCfgType type, char ***prval)
 Validate and return a null-terminated array of strings.
static int parseIntOrUnsigned (char *val, DnxCfgType type, int *prval)
 Validate and an integer or unsigned integer value.
static int parseIntOrUnsignedArray (char *val, DnxCfgType type, int **prval)
 Validate and return a buffer of integers beginning with a count.
static int parseBool (char *val, DnxCfgType type, unsigned *prval)
 Validate and return a boolean.
static int dnxParseCfgVar (char *var, char *val, DnxCfgDict *dict, void **vptrs)
 Validate and convert a single variable/value pair against a dictionary.
static int dnxParseCfgLine (char *s, DnxCfgDict *dict, void **vptrs)
 Parse a single line of a configuration file.
static int applyCfgSetString (char **sap, DnxCfgDict *dict, void **vptrs)
 Apply an array of configuration parameters to a variable array.
static void freeArrayPtrs (DnxCfgDict *dict, void **vptrs)
 Free memory in all pointer types in a value array.
static char * formatString (char *var, DnxCfgType type, char *prval)
 Format a string configuration value.
static char * formatStringArray (char *var, DnxCfgType type, char **prval)
 Format a string array configuration value.
static char * formatIntOrUnsigned (char *var, DnxCfgType type, int prval)
 Format an int or unsigned configuration value.
static char * formatIntOrUnsignedArray (char *var, DnxCfgType type, int *prval)
 Format an int or unsigned array configuration value.
static char * formatBool (char *var, DnxCfgType type, unsigned prval)
 Format a boolean configuration value.
static char * buildCurrentCfgCache (DnxCfgDict *dict, size_t *ccsizep)
 Format an allocated buffer containing the current configuration.
int dnxCfgParserCreate (char *cfgdefs, char *cfgfile, char *cmdover, DnxCfgDict *dict, DnxCfgValidator_t *vfp, DnxCfgParser **cpp)
 Create a new configuration parser object.
int dnxCfgParserParse (DnxCfgParser *cp, void *passthru)
 Parse a configuration file into a value array.
int dnxCfgParserGetCfg (DnxCfgParser *cp, char *buf, size_t *bufszp)
 Return the current configuration as a formatted string.
void dnxCfgParserDestroy (DnxCfgParser *cp)
 Destroy a previously created configuration parser object.


Detailed Description

Parses a standard Unix config file.

The file format follows a rather trival format:

cfg-file = cfg-line [, cfg-line] cfg-line = cfg-var '=' cfg-val '
' cfg-var = (any alpha numeric text) cfg-val = (any alpha numeric text)

In addition, these rules must be followed:

1. White space may be found anywhere within the file. 2. cfg-line constructs may not contain line breaks. 3. Line comments of the form '#' (any text) may be found anywhere. 4. cfg-var constructs may not contain '=' characters.

Author:
John Calcote (jcalcote@users.sourceforge.net)
Attention:
Please submit patches to http://dnx.sourceforge.net

Definition in file dnxCfgParser.c.


Define Documentation

#define DNX_MAX_CFG_LINE   2048

Longest allowed config file line.

Definition at line 58 of file dnxCfgParser.c.

Referenced by dnxCfgParserParse().

#define elemcount (  )     (sizeof(x)/sizeof(*(x)))

Definition at line 56 of file dnxCfgParser.c.

#define INTSWAP ( a,
 )     do { int n = (a); (a) = (int)(intptr_t)(b); (b) = (void *)(intptr_t)n; } while(0)

Referenced by dnxCfgParserParse().

#define PTRSWAP ( a,
 )     do { void * p = (a); (a) = (b); (b) = p; } while (0)

Referenced by dnxCfgParserParse().


Typedef Documentation

typedef char* DnxVarFormatter_t(char *var, DnxCfgType type, void *prval)

A typedef describing a single type variable formatter.

Definition at line 81 of file dnxCfgParser.c.

typedef int DnxVarParser_t(char *val, DnxCfgType type, void *prval)

A typedef describing a single type variable parser.

Definition at line 78 of file dnxCfgParser.c.


Function Documentation

static int applyCfgSetString ( char **  sap,
DnxCfgDict dict,
void **  vptrs 
) [static]

Apply an array of configuration parameters to a variable array.

Each parameter has the same format as a line in a configuration file.

Parameters:
[in] sap - the null-terminated configuration string set to be applied to vptrs. This parameter may be NULL, in which case nothing is done and DNX_OK is returned.
[in] dict - the configuration dictionary to use.
[out] vptrs - an array of return storage addresses for parsed values. Each element returns the parsed value for the corresponding element of the dict array.
Returns:
Zero on success, or a non-zero error value. Possible return values include DNX_OK (on success) or DNX_ERR_MEMORY.

Definition at line 519 of file dnxCfgParser.c.

References DNX_OK, and dnxParseCfgLine().

Referenced by dnxCfgParserParse().

static char* buildCurrentCfgCache ( DnxCfgDict dict,
size_t *  ccsizep 
) [static]

Format an allocated buffer containing the current configuration.

Parameters:
[in] dict - the config dictionary to be formatted into a string.
[out] ccsizep - the address of storage for returning the size in bytes of the returned allocated string buffer.
Returns:
A pointer to an allocated string buffer, or NULL on memory allocation failure.

Definition at line 729 of file dnxCfgParser.c.

References cfg, formatBool(), formatIntOrUnsigned(), formatIntOrUnsignedArray(), formatString(), formatStringArray(), DnxCfgDict::varname, xfree, and xrealloc.

Referenced by dnxCfgParserGetCfg().

static DnxCfgDict* copyDictionary ( DnxCfgDict dict,
unsigned *  dictszp 
) [static]

Make a dynamic copy of a dnx configuration dictionary.

Also clears all values referred to by the valptr fields. This system really only supports two data types: int and pointer. If a value is not one of the int types, then it's a pointer type.

Parameters:
[in] dict - the dictionary describing the field value types.
[out] dictszp - the address of storage for the counted size of the dictionary.
Returns:
A pointer to a copy of dict, or NULL if out of memory.

Definition at line 141 of file dnxCfgParser.c.

References DNX_CFG_BOOL, DNX_CFG_INT, DNX_CFG_UNSIGNED, DnxCfgDict::type, DnxCfgDict::valptr, DnxCfgDict::varname, and xmalloc.

Referenced by dnxCfgParserCreate().

int dnxCfgParserCreate ( char *  cfgdefs,
char *  cfgfile,
char *  cmdover,
DnxCfgDict dict,
DnxCfgValidator_t vfp,
DnxCfgParser **  cpp 
)

Create a new configuration parser object.

A configuration parser is used to read and parse a specified configuration file using a user-provided configuration variable dictionary to validate and convert the string values into usable data values.

A null-terminated string containing default configuration file entries may optionally be passed in the cfgdefs parameter. The format of the string is the same as that of a configuration file. The cfgdefs string is parsed BEFORE the configuration file, thus configuration file entries override default configuration entries.

A null-terminated string containing command-line overrides for existing configuration file entries may optionally be passed in the cmdover parameter. The format of the string is the same as that of a configuration file. The cmdover string is parsed AFTER the configuration file, thus cmdover entries override configuration file entries.

The validator function vfp is called after all values are parsed. The values will only be written to the user's dictionary after vfp returns success (0).

Parameters:
[in] cfgdefs - an optional string containing default config file entries. Pass NULL if not required.
[in] cfgfile - an optional path name of the config file to be parsed. Pass NULL if not required.
[in] cmdover - an optional string containing command line override entries. Pass NULL if not required.
[in] dict - an array of DnxCfgDict objects, each of which defines a valid configuration variable name and type for this parser. The dict array should be terminated with NULL field values (a NULL pointer in the varname field, and DNX_CFG_NULL in the type field).
[in] vfp - a pointer to a validator function.
[out] cpp - the address of storage for the newly created configuration parser object.
Returns:
Zero on success, or a non-zero error value.

Definition at line 783 of file dnxCfgParser.c.

References iDnxCfgParser::cfgdefs, iDnxCfgParser::cfgfile, iDnxCfgParser::cmdover, copyDictionary(), iDnxCfgParser::dict, iDnxCfgParser::dictsz, DNX_ERR_MEMORY, DNX_OK, strToStrArray(), iDnxCfgParser::vfp, xfree, xmalloc, and xstrdup.

Referenced by initConfig().

void dnxCfgParserDestroy ( DnxCfgParser cp  ) 

Destroy a previously created configuration parser object.

Parameters:
[in] cp - the configuration parser object to be destroyed.

Definition at line 921 of file dnxCfgParser.c.

References iDnxCfgParser::cfgdefs, iDnxCfgParser::cfgfile, iDnxCfgParser::cmdover, iDnxCfgParser::curcfg, iDnxCfgParser::dict, freeArrayPtrs(), and xfree.

Referenced by initConfig(), main(), and releaseConfig().

int dnxCfgParserGetCfg ( DnxCfgParser cp,
char *  buf,
size_t *  bufszp 
)

Return the current configuration as a formatted string.

The configuration string is a set of '
' terminated lines, each containing the same text as might be specified in a configuration file. The set of lines contain the complete current configuration as defined by the specified configuration parser object.

The proper way to use this call is to call it twice, once with a zero length buffer (null for buf and 0 in bufszp). On return, bufszp will then contain the required size of buf, which may then be allocated before the second call. This operation is efficient because the current configuration string is cached by the configuration parser object.

Parameters:
[in] cp - the configuration parser whose current configuration data should be returned.
[out] buf - the address of storage for the formatted configuration string data.
[in,out] bufszp - on entry contains the size of buf; on exit, returns the number of bytes used or required to store the entire formatted configuration string.
Returns:
Zero on success, or a non-zero error value.

Definition at line 896 of file dnxCfgParser.c.

References buildCurrentCfgCache(), iDnxCfgParser::ccsize, iDnxCfgParser::curcfg, iDnxCfgParser::dict, and DNX_ERR_MEMORY.

Referenced by buildMgmtCfgReply().

int dnxCfgParserParse ( DnxCfgParser cp,
void *  passthru 
)

Parse a configuration file into a value array.

Parses an optional default set of configuration values, followed by the configuration file, followed by an optional set of command line override configuration values. After all parsing is complete, calls the validator function (if specified). If the validator passes (returns success - 0), then the parsed values are copied into the user's dictionary and success is returned. Otherwise no dictionary value changes take effect.

Parameters:
[in] cp - the configuration parser object on which to run the parser.
[in] passthru - an opaque pointer to user data that's passed through to the validator function.
Returns:
Zero on success, or a non-zero error value. Possible error values include DNX_OK (on success), DNX_ERR_ACCESS, DNX_ERR_NOTFOUND, DNX_ERR_SYNTAX or DNX_ERR_MEMORY.

Definition at line 814 of file dnxCfgParser.c.

References applyCfgSetString(), iDnxCfgParser::ccsize, iDnxCfgParser::cfgdefs, iDnxCfgParser::cfgfile, iDnxCfgParser::cmdover, iDnxCfgParser::curcfg, iDnxCfgParser::dict, iDnxCfgParser::dictsz, DNX_CFG_BOOL, DNX_CFG_INT, DNX_CFG_UNSIGNED, DNX_ERR_ACCESS, DNX_ERR_INVALID, DNX_ERR_MEMORY, DNX_ERR_NOTFOUND, DNX_MAX_CFG_LINE, dnxErrorString(), dnxLog(), dnxParseCfgLine(), freeArrayPtrs(), INTSWAP, PTRSWAP, DnxCfgDict::type, DnxCfgDict::valptr, iDnxCfgParser::vfp, xfree, and xmalloc.

Referenced by dnxReconfigure(), initConfig(), and processCommands().

static int dnxParseCfgLine ( char *  s,
DnxCfgDict dict,
void **  vptrs 
) [static]

Parse a single line of a configuration file.

Parameters:
[in] s - a buffer containing the line text to be parsed. This buffer is constant and will not be written to.
[in] dict - the configuration dictionary.
[out] vptrs - an array of return storage addresses for parsed values. Each element returns the parsed value for the corresponding element of the dict array.
Returns:
Zero on success, or a non-zero error value. Possible error values include DNX_OK (on success), DNX_ERR_SYNTAX or DNX_ERR_MEMORY.

Definition at line 467 of file dnxCfgParser.c.

References DNX_ERR_MEMORY, DNX_ERR_SYNTAX, dnxParseCfgVar(), strtrim(), xfree, and xstrdup.

Referenced by applyCfgSetString(), and dnxCfgParserParse().

static int dnxParseCfgVar ( char *  var,
char *  val,
DnxCfgDict dict,
void **  vptrs 
) [static]

Validate and convert a single variable/value pair against a dictionary.

The value is parsed, converted according to the dictionary-specified type, and stored in the corresponding vptrs data field. If the type is a pointer type, the existing value is freed, and new memory is allocated.

Parameters:
[in] var - the variable name to be validated.
[in] val - the string form of the value to be parsed, validated,
[in] dict - an array of legal variable names and types.
[out] vptrs - an array of return value storage locations, where each element is the storage location for the variable indicated by the corresponding location in the dict array.
Returns:
Zero on success, or a non-zero error value.

Definition at line 426 of file dnxCfgParser.c.

References DNX_ERR_INVALID, elemcount, parseBool(), parseIntOrUnsigned(), parseIntOrUnsignedArray(), parseString(), parseStringArray(), DnxCfgDict::type, and DnxCfgDict::varname.

Referenced by dnxParseCfgLine().

static char* formatBool ( char *  var,
DnxCfgType  type,
unsigned  prval 
) [static]

Format a boolean configuration value.

Parameters:
[in] var - the variable name to be used during formatting.
[in] type - the type of the variable to be formatted.
[in] prval - the string value to be formatted.
Returns:
A pointer to an allocated buffer containing the formatted configuration line, or NULL on memory allocation failure. The caller is responsible for freeing the memory returned.

Definition at line 704 of file dnxCfgParser.c.

References cfg, DNX_CFG_BOOL, and xmalloc.

Referenced by buildCurrentCfgCache().

static char* formatIntOrUnsigned ( char *  var,
DnxCfgType  type,
int  prval 
) [static]

Format an int or unsigned configuration value.

Parameters:
[in] var - the variable name to be used during formatting.
[in] type - the type of the variable to be formatted.
[in] prval - the integer to be formatted.
Returns:
A pointer to an allocated buffer containing the formatted configuration line, or NULL on memory allocation failure. The caller is responsible for freeing the memory returned.

Definition at line 638 of file dnxCfgParser.c.

References cfg, DNX_CFG_INT, DNX_CFG_UNSIGNED, and xmalloc.

Referenced by buildCurrentCfgCache().

static char* formatIntOrUnsignedArray ( char *  var,
DnxCfgType  type,
int *  prval 
) [static]

Format an int or unsigned array configuration value.

Parameters:
[in] var - the variable name to be used during formatting.
[in] type - the type of the variable to be formatted.
[in] prval - the array of integers to be formatted. The first value indicates the number of integers following.
Returns:
A pointer to an allocated buffer containing the formatted configuration line, or NULL on memory allocation failure. The caller is responsible for freeing the memory returned.

Definition at line 668 of file dnxCfgParser.c.

References cfg, DNX_CFG_INT_ARRAY, DNX_CFG_UNSIGNED_ARRAY, and xmalloc.

Referenced by buildCurrentCfgCache().

static char* formatString ( char *  var,
DnxCfgType  type,
char *  prval 
) [static]

Format a string configuration value.

Parameters:
[in] var - the variable name to be used during formatting.
[in] type - the type of the variable to be formatted.
[in] prval - the string value to be formatted.
Returns:
A pointer to an allocated buffer containing the formatted configuration line, or NULL on memory allocation failure. The caller is responsible for freeing the memory returned.

Definition at line 572 of file dnxCfgParser.c.

References cfg, DNX_CFG_FSPATH, DNX_CFG_STRING, DNX_CFG_URL, and xmalloc.

Referenced by buildCurrentCfgCache().

static char* formatStringArray ( char *  var,
DnxCfgType  type,
char **  prval 
) [static]

Format a string array configuration value.

Parameters:
[in] var - the variable name to be used during formatting.
[in] type - the type of the variable to be formatted.
[in] prval - the null-terminated array of strings to be formatted.
Returns:
A pointer to an allocated buffer containing the formatted configuration line, or NULL on memory allocation failure. The caller is responsible for freeing the memory returned.

Definition at line 599 of file dnxCfgParser.c.

References cfg, DNX_CFG_STRING_ARRAY, and xmalloc.

Referenced by buildCurrentCfgCache().

static void freeArrayPtrs ( DnxCfgDict dict,
void **  vptrs 
) [static]

Free memory in all pointer types in a value array.

If vptrs is NULL, then the values referred to by dict will be freed, otherwise the values referred to by vptrs will be freed.

Parameters:
[in] dict - the configuration dictionary that defines the types and values to be freed in case vptrs is null.
[in] vptrs - the value pointer array whose pointer values are to be freed. Optional; if this parameter is NULL, then the value pointers in dict are freed instead.

Definition at line 548 of file dnxCfgParser.c.

References DNX_CFG_BOOL, DNX_CFG_INT, DNX_CFG_UNSIGNED, DnxCfgDict::varname, and xfree.

Referenced by dnxCfgParserDestroy(), and dnxCfgParserParse().

static int parseBool ( char *  val,
DnxCfgType  type,
unsigned *  prval 
) [static]

Validate and return a boolean.

Values may be specified with ON, OFF, TRUE, FALSE, YES, NO, 0 or non-zero numeric values. Anything else is treated as a syntax error.

Parameters:
[in] val - the value to be validated.
[in] type - the high-level type (BOOL) of val.
[in,out] prval - the address of storage for the returned boolean.
Returns:
Zero on success, or a non-zero error value.

Definition at line 395 of file dnxCfgParser.c.

References DNX_CFG_BOOL, DNX_ERR_SYNTAX, and DNX_OK.

Referenced by dnxParseCfgVar().

static int parseIntOrUnsigned ( char *  val,
DnxCfgType  type,
int *  prval 
) [static]

Validate and an integer or unsigned integer value.

Parameters:
[in] val - the value to be validated.
[in] type - the high-level type (INT or UNSIGNED) of val.
[in,out] prval - the address of storage for the returned int.
Returns:
Zero on success, or a non-zero error value.

Definition at line 309 of file dnxCfgParser.c.

References DNX_CFG_INT, DNX_CFG_UNSIGNED, DNX_ERR_SYNTAX, and DNX_OK.

Referenced by dnxParseCfgVar(), and parseIntOrUnsignedArray().

static int parseIntOrUnsignedArray ( char *  val,
DnxCfgType  type,
int **  prval 
) [static]

Validate and return a buffer of integers beginning with a count.

Any non-zero value passed in prval is assumed to be a pointer to previously allocated array memory. It will be freed before reassigning to the new buffer value.

The buffer is returned with one extra element; the first element is a count of the remaining elements of the array.

Parameters:
[in] val - the value to be validated.
[in] type - the high-level type (INT_ or UNSIGNED_ARRAY) of val.
[in,out] prval - the address of storage for the returned allocated array buffer address.
Returns:
Zero on success, or a non-zero error value.

Definition at line 339 of file dnxCfgParser.c.

References DNX_CFG_INT, DNX_CFG_INT_ARRAY, DNX_CFG_UNSIGNED, DNX_CFG_UNSIGNED_ARRAY, DNX_ERR_MEMORY, DNX_OK, parseIntOrUnsigned(), strToStrArray(), strtrim(), xfree, and xmalloc.

Referenced by dnxParseCfgVar().

static int parseString ( char *  val,
DnxCfgType  type,
char **  prval 
) [static]

Validate and return a copy of a string.

Any non-zero value passed in prval is assumed to be a pointer to previously allocated memory. It will be freed before reassigning to the new value.

Parameters:
[in] val - the value to be validated.
[in] type - the high-level type (URL, FSPATH, or STRING) of val.
[in,out] prval - the address of storage for the returned allocated string buffer address.
Returns:
Zero on success, or a non-zero error value.

Definition at line 223 of file dnxCfgParser.c.

References DNX_CFG_FSPATH, DNX_CFG_STRING, DNX_CFG_URL, DNX_ERR_MEMORY, DNX_OK, validateFSPath(), validateURL(), xfree, and xstrdup.

Referenced by dnxParseCfgVar().

static int parseStringArray ( char *  val,
DnxCfgType  type,
char ***  prval 
) [static]

Validate and return a null-terminated array of strings.

Any non-zero value passed in prval is assumed to be a pointer to previously allocated array memory. It will be freed before reassigning to the new buffer value.

Parameters:
[in] val - the value to be validated.
[in] type - the high-level type (ADDR_ARRAY) of val.
[in,out] prval - the address of storage for the returned allocated array buffer address.
Returns:
Zero on success, or a non-zero error value.

Definition at line 279 of file dnxCfgParser.c.

References DNX_CFG_STRING_ARRAY, DNX_ERR_MEMORY, DNX_OK, strToStrArray(), strtrim(), and xfree.

Referenced by dnxParseCfgVar().

static char** strToStrArray ( char *  str,
char  delim 
) [static]

Convert a delimited string into a null-terminated array of strings.

Parameters:
[in] str - the delimited string to be converted.
[in] delim - the delimiter character for which to search.
Returns:
A null-terminated array of pointers to strings, or null on memory allocation failure.

Definition at line 95 of file dnxCfgParser.c.

References xmalloc.

Referenced by dnxCfgParserCreate(), parseIntOrUnsignedArray(), and parseStringArray().

static char* strtrim ( char *  s  )  [static]

Trim leading and trailing white space from a string.

Leading white space is trimmed by returning a pointer to the first non- white-space character in s. Trailing white space is trimmed by null- terminating s after the last non-white-space character.

Parameters:
[in] s - the string to be trimmed.
Returns:
A pointer to the first non-white space character in s.

Definition at line 251 of file dnxCfgParser.c.

Referenced by dnxParseCfgLine(), parseIntOrUnsignedArray(), and parseStringArray().

static int validateFSPath ( char *  path  )  [static]

Validate a file system path for correctness.

Parameters:
[in] path - the path to be validated.
Returns:
Zero on success, or a non-zero error value.
Todo:
Implement validateFSPath.

Definition at line 206 of file dnxCfgParser.c.

Referenced by parseString().

static int validateURL ( char *  url  )  [static]

Validate a URL for correctness.

Parameters:
[in] url - the URL to be validated.
Returns:
Zero on success, or a non-zero error value.
Todo:
Implement validateURL.

Definition at line 194 of file dnxCfgParser.c.

Referenced by parseString().


Generated on Tue Apr 13 15:48:07 2010 for DNX by  doxygen 1.5.6