426 lines
19 KiB
C
426 lines
19 KiB
C
/****************************************************************
|
|
* *
|
|
* Copyright (C) 1991-2008 Celoxica Ltd. All rights reserved. *
|
|
* *
|
|
*****************************************************************
|
|
* *
|
|
* Project : Configuration parser *
|
|
* Date : 06 JUN 2008 *
|
|
* File : config_file.h *
|
|
* Author : Will Benfold (WB) *
|
|
* Contributors: *
|
|
* *
|
|
* Description: *
|
|
* Configuration parser (public API) *
|
|
* *
|
|
* Date Version Author Reason for change *
|
|
* *
|
|
* 06 JUN 2008 1.00 WB Created *
|
|
* *
|
|
****************************************************************/
|
|
|
|
#ifndef CONFIG_FILE_H
|
|
#define CONFIG_FILE_H
|
|
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef struct _Config Config;
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Error handling
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* typedef enum ... ConfigStatus;
|
|
*
|
|
* Description:
|
|
* The type of error codes returned by RC API functions.
|
|
* The error codes are as follows:
|
|
*
|
|
* Status Code Meaning
|
|
*
|
|
* CONFIG_STATUS_OK The function completed successfully.
|
|
* CONFIG_STATUS_NULL_POINTER NULL was passed as a (Config *).
|
|
* CONFIG_STATUS_NOT_FOUND The specified item did not exist.
|
|
* CONFIG_STATUS_PARSE_FAILED An argument could not be converted to
|
|
* the requested type.
|
|
* CONFIG_STATUS_INVALID_INDEX The specified argument index did not
|
|
* exist.
|
|
* CONFIG_STATUS_INVALID_LINK An attempt was made to assign a
|
|
* reference to a non-leaf item.
|
|
* CONFIG_STATUS_INVALID_LINK_TARGET A reference target did not exist.
|
|
* CONFIG_STATUS_CIRCULAR_LINK A circular reference was requested.
|
|
* CONFIG_STATUS_INVALID_INDENT The input file has malformed
|
|
* indentation.
|
|
* CONFIG_STATUS_STACK_OVERFLOW The input file has too great an indent
|
|
* depth.
|
|
* CONFIG_STATUS_NO_MEMORY malloc() failed.
|
|
* CONFIG_STATUS_IO_ERROR Error occurred while reading a file.
|
|
* CONFIG_STATUS_NO_ENV The specified environment variable
|
|
* did not exist.
|
|
* CONFIG_STATUS_BAD_BRACKETS A closing bracket was not found.
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* typedef ... ConfigErrorHandler;
|
|
*
|
|
* void ConfigSetErrorHandler (ConfigErrorHandler Handler);
|
|
* ConfigErrorHandler ConfigGetErrorHandler (void);
|
|
* void ConfigSetDefaultErrorHandler (void);
|
|
* void ConfigSetNullErrorHandler (void);
|
|
*
|
|
* Description:
|
|
* ConfigSetErrorHandler() registers a new error handler, replacing
|
|
* the current one.
|
|
*
|
|
* ConfigGetErrorHandler() returns the current error handler.
|
|
*
|
|
* ConfigSetDefaultErrorHandler() registers a basic error handler.
|
|
* This will treat most errors as fatal (error will be described and
|
|
* exit() will be called), but will ignore some minor failures (the
|
|
* caller of the failing function will be able to test the return
|
|
* code to detect these). The errors treated as "minor" are:
|
|
*
|
|
* CONFIG_STATUS_NOT_FOUND
|
|
* CONFIG_STATUS_NULL_POINTER
|
|
* CONFIG_STATUS_NO_ENV
|
|
*
|
|
* The rationale is that the absence of an option (or environment
|
|
* variable) is often not an error, and sometimes does not even need
|
|
* to be explicitly detected.
|
|
*
|
|
****************************************************************************/
|
|
|
|
typedef enum {
|
|
CONFIG_STATUS_OK,
|
|
CONFIG_STATUS_NULL_POINTER,
|
|
CONFIG_STATUS_NOT_FOUND,
|
|
CONFIG_STATUS_PARSE_FAILED,
|
|
CONFIG_STATUS_PARSE_STR_FAILED,
|
|
CONFIG_STATUS_INVALID_INDEX,
|
|
CONFIG_STATUS_INVALID_LINK,
|
|
CONFIG_STATUS_INVALID_LINK_TARGET,
|
|
CONFIG_STATUS_CIRCULAR_LINK,
|
|
CONFIG_STATUS_INVALID_INDENT,
|
|
CONFIG_STATUS_STACK_OVERFLOW,
|
|
CONFIG_STATUS_NO_MEMORY,
|
|
CONFIG_STATUS_IO_ERROR,
|
|
CONFIG_STATUS_NO_ENV,
|
|
CONFIG_STATUS_BAD_BRACKETS,
|
|
CONFIG_STATUS_PARSE_SYMBOL_FAILED,
|
|
CONFIG_STATUS_NUMBER_REDEFINITION,
|
|
CONFIG_STATUS_NUMBER_EXPECTED
|
|
} ConfigStatus;
|
|
|
|
|
|
typedef ConfigStatus (*ConfigErrorHandler)(ConfigStatus Status, const Config *Object, int Offset, const char *ExtraInfo);
|
|
|
|
void ConfigSetDefaultErrorHandler (void);
|
|
ConfigErrorHandler ConfigGetErrorHandler (void);
|
|
void ConfigSetErrorHandler (ConfigErrorHandler Handler);
|
|
void ConfigSetNullErrorHandler (void);
|
|
|
|
ConfigStatus ConfigDefaultErrorHandler (ConfigStatus Status, const Config *Object, int Offset, const char *ExtraInfo);
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Open / Close
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* ConfigStatus ConfigOpen (Config **c);
|
|
* ConfigStatus ConfigClose (Config *c);
|
|
*
|
|
* Description:
|
|
* ConfigOpen creates a new Config tree. The structure returned is
|
|
* the root node of an otherwise empty tree.
|
|
*
|
|
* ConfigClose frees the resources associated with a Config tree.
|
|
* The argument must be the root node of a Config tree.
|
|
*
|
|
****************************************************************************/
|
|
|
|
ConfigStatus ConfigOpen (Config **c);
|
|
ConfigStatus ConfigClose (Config *c);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Parsing
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* ConfigStatus ConfigParseString (Config *c, const char *String);
|
|
* ConfigStatus ConfigParseBasicString (Config *c, const char *String);
|
|
* ConfigStatus ConfigParseArgs (Config *c, int argc, const char * const argv[]);
|
|
* ConfigStatus ConfigParseEnv (Config *c, const char *VarName, int Required);
|
|
* ConfigStatus ConfigParseFile (Config *c, const char *FileName);
|
|
* ConfigStatus ConfigParseBuffer (Config *c, const char *Buffer);
|
|
*
|
|
* Description:
|
|
* Parse option settings from a string, a list of strings, an
|
|
* environment variable, or a configuration file.
|
|
*
|
|
* Arguments:
|
|
* c A Config tree node, not necessarily the root.
|
|
* String A string containing a single option setting
|
|
* argc, argv A list of strings, as supplied to main(), each
|
|
* containing a single option settings. The first
|
|
* argument (argv[0]) will be ignored.
|
|
* VarName The name of an environment variable containing a
|
|
* space-separated list of option settings.
|
|
* FileName The name of a file containing a newline-separated
|
|
* list of option settings. (TODO: See file spec)
|
|
* Required Pass zero to suppress errors for unset
|
|
* environment variables, nonzero otherwise.
|
|
*
|
|
* ConfigParseString is using comma ',' as list delimiter, ConfigParseBasicString
|
|
* is using white space ' ' as list delimiter
|
|
****************************************************************************/
|
|
|
|
ConfigStatus ConfigParseString (Config *c, const char *String);
|
|
ConfigStatus ConfigParseBasicString (Config *c, const char *String);
|
|
ConfigStatus ConfigParseFormatedString (Config *c, const char *String, ...);
|
|
ConfigStatus ConfigParseArgs (Config *c, int argc, const char * const argv[]);
|
|
ConfigStatus ConfigParseEnv (Config *c, const char *VarName, int Required);
|
|
ConfigStatus ConfigParseFile (Config *c, const char *FileName);
|
|
ConfigStatus ConfigParseBuffer (Config *c, const char *Buffer);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Navigation
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* typedef ... ConfigIterator;
|
|
*
|
|
* int ConfigOptionCount (const Config *c);
|
|
* Config *ConfigOption (const Config *c, const char *Name);
|
|
* Config *ConfigOptionNext (const Config *c, const char **Name,
|
|
* ConfigIterator **Iterator);
|
|
* int ConfigIsLink (const ConfigIterator *Iterator);
|
|
*
|
|
* Description:
|
|
* ConfigOptionCount() returns the number of children a node has.
|
|
* If the argument is NULL, zero will be returned.
|
|
*
|
|
* ConfigOption() returns a specified child node, or NULL if that
|
|
* node does not exist. Arbitrary descendents may be retrieved if a
|
|
* period-separated list is supplied. For instance...
|
|
*
|
|
* c2 = ConfigOption (c1, "foo.bar.baz");
|
|
*
|
|
* ...will first retrieve the child (of c) named "foo", then the
|
|
* child of that named "bar", then the child of that named "baz".
|
|
*
|
|
* ConfigOptionNext() allows iteration over children of a node. The
|
|
* Iterator parameter should point to a (ConfigIterator *) pointer
|
|
* allocated by the caller and initialised to NULL. Each successive
|
|
* call will return a different child, the final call returning
|
|
* NULL.
|
|
*
|
|
* No cleanup is required after iteration. Adding or removing
|
|
* children invalidates any iterators operating on the parent. It
|
|
* is the caller's responsibility to avoid using an invalid
|
|
* iterator.
|
|
*
|
|
* ConfigIsLink() can be applied to an iterator to determine whether
|
|
* the most recent child returned is a "real" child (zero) or a
|
|
* reference (nonzero).
|
|
*
|
|
* Arguments:
|
|
* c A Config tree node, not necessarily the root.
|
|
* Name A string specifying the name of a child node.
|
|
* Iterator A ConfigIterator handle.
|
|
*
|
|
****************************************************************************/
|
|
|
|
typedef struct _ConfigIterator ConfigIterator;
|
|
|
|
int ConfigOptionCount (const Config *c);
|
|
const Config *ConfigOption (const Config *c, const char *Name);
|
|
const Config *ConfigOptionNext (const Config *c, const char **Name, ConfigIterator **Iterator);
|
|
int ConfigIsLink (const ConfigIterator *Iterator);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Argument values
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* int ConfigArgCount (const Config *c);
|
|
*
|
|
* ConfigStatus ConfigArg<FOO> (const Config *c, int Index, <FOO> *FooPtr);
|
|
*
|
|
* Description:
|
|
* ConfigArgCount() returns the number of arguments a node has.
|
|
* Leaf nodes usually have arguments, non-leaf nodes usually do not.
|
|
*
|
|
* ConfigArg<Foo>() reads an argument specified by node and index,
|
|
* converts it to type <Foo>, and stores the result in the specified
|
|
* location.
|
|
*
|
|
* Special cases:
|
|
* ConfigArgStringConst() provides a const string which the caller
|
|
* must not modify or free(). The string will be invalidated when
|
|
* ConfigClose is called.
|
|
*
|
|
* ConfigArgString() allocates a new string. It is the caller's
|
|
* responsibility to ensure that the string is eventually free()'d.
|
|
*
|
|
* Booleans should usually be specified as either "True" or "False".
|
|
* However, ConfigParseBoolean() is case-insensitive, and will also
|
|
* allow a few variations, such as On/Off, Yes/No, 1/0.
|
|
*
|
|
* All of the integer-parsing functions expect values to be
|
|
* expressed either as decimals or in hex. In the latter case, the
|
|
* "0x" prefix must be used.
|
|
*
|
|
* Arguments:
|
|
* c A Config tree node, not necessarily the root.
|
|
* Index The argument to access (0=leftmost)
|
|
* FooPtr A pointer to a variable of type <FOO>
|
|
*
|
|
****************************************************************************/
|
|
|
|
int ConfigArgCount (const Config *c);
|
|
|
|
ConfigStatus ConfigArgGet (const Config *c, int Index, const char **StrPtr);
|
|
|
|
/* Indirect arguments - through strings */
|
|
|
|
ConfigStatus ConfigArgStrUInt (const char *Str, unsigned *IntPtr);
|
|
ConfigStatus ConfigArgStrUInt8 (const char *Str, uint8_t *IntPtr);
|
|
ConfigStatus ConfigArgStrUInt16 (const char *Str, uint16_t *IntPtr);
|
|
ConfigStatus ConfigArgStrUInt32 (const char *Str, uint32_t *IntPtr);
|
|
ConfigStatus ConfigArgStrUInt64 (const char *Str, uint64_t *IntPtr);
|
|
|
|
ConfigStatus ConfigArgStrSInt (const char *Str, int *IntPtr);
|
|
ConfigStatus ConfigArgStrSInt8 (const char *Str, int8_t *IntPtr);
|
|
ConfigStatus ConfigArgStrSInt16 (const char *Str, int16_t *IntPtr);
|
|
ConfigStatus ConfigArgStrSInt32 (const char *Str, int32_t *IntPtr);
|
|
ConfigStatus ConfigArgStrSInt64 (const char *Str, int64_t *IntPtr);
|
|
|
|
ConfigStatus ConfigArgStrDouble (const char *Str, double *DblPtr);
|
|
ConfigStatus ConfigArgStrBoolean (const char *Str, int *BoolPtr);
|
|
ConfigStatus ConfigArgStrMAC (const char *Str, void *MACPtr);
|
|
ConfigStatus ConfigArgStrIP (const char *Str, uint32_t *IPPtr);
|
|
|
|
/* Direct arguments */
|
|
|
|
ConfigStatus ConfigArgUInt (const Config *c, int Index, unsigned *IntPtr);
|
|
ConfigStatus ConfigArgUInt8 (const Config *c, int Index, uint8_t *IntPtr);
|
|
ConfigStatus ConfigArgUInt16 (const Config *c, int Index, uint16_t *IntPtr);
|
|
ConfigStatus ConfigArgUInt32 (const Config *c, int Index, uint32_t *IntPtr);
|
|
ConfigStatus ConfigArgUInt64 (const Config *c, int Index, uint64_t *IntPtr);
|
|
|
|
ConfigStatus ConfigArgSInt (const Config *c, int Index, int *IntPtr);
|
|
ConfigStatus ConfigArgSInt8 (const Config *c, int Index, int8_t *IntPtr);
|
|
ConfigStatus ConfigArgSInt16 (const Config *c, int Index, int16_t *IntPtr);
|
|
ConfigStatus ConfigArgSInt32 (const Config *c, int Index, int32_t *IntPtr);
|
|
ConfigStatus ConfigArgSInt64 (const Config *c, int Index, int64_t *IntPtr);
|
|
|
|
ConfigStatus ConfigArgDouble (const Config *c, int Index, double *DblPtr);
|
|
|
|
/* Note: This call is not case sensitive */
|
|
ConfigStatus ConfigArgBoolean (const Config *c, int Index, int *BoolPtr);
|
|
|
|
|
|
ConfigStatus ConfigArgStringConst (const Config *c, int Index, const char **StrPtr);
|
|
|
|
static inline ConfigStatus ConfigArgString (const Config *c, int Index, char **StrPtr)
|
|
{
|
|
const char *Tmp = NULL;
|
|
ConfigStatus Status = ConfigArgStringConst (c, Index, &Tmp);
|
|
*StrPtr = Tmp ? strdup (Tmp) : NULL;
|
|
return Status;
|
|
}
|
|
|
|
|
|
ConfigStatus ConfigArgMAC (const Config *c, int Index, void *MACPtr);
|
|
ConfigStatus ConfigArgIP (const Config *c, int Index, uint32_t * IPPtr);
|
|
|
|
/* Ranges indirect arguments - through strings */
|
|
|
|
ConfigStatus ConfigArgStrRangeUInt (const char *Str, unsigned *From, unsigned *To);
|
|
ConfigStatus ConfigArgStrRangeUInt8 (const char *Str, uint8_t *From, uint8_t *To);
|
|
ConfigStatus ConfigArgStrRangeUInt16 (const char *Str, uint16_t *From, uint16_t *To);
|
|
ConfigStatus ConfigArgStrRangeUInt32 (const char *Str, uint32_t *From, uint32_t *To);
|
|
ConfigStatus ConfigArgStrRangeUInt64 (const char *Str, uint64_t *From, uint64_t *To);
|
|
|
|
ConfigStatus ConfigArgStrRangeSInt (const char *Str, int *From, int *To);
|
|
ConfigStatus ConfigArgStrRangeSInt8 (const char *Str, int8_t *From, int8_t *To);
|
|
ConfigStatus ConfigArgStrRangeSInt16 (const char *Str, int16_t *From, int16_t *To);
|
|
ConfigStatus ConfigArgStrRangeSInt32 (const char *Str, int32_t *From, int32_t *To);
|
|
ConfigStatus ConfigArgStrRangeSInt64 (const char *Str, int64_t *From, int64_t *To);
|
|
|
|
/* Ranges direct arguments */
|
|
|
|
ConfigStatus ConfigArgRangeUInt (const Config *c, int Index, unsigned *From, unsigned *To);
|
|
ConfigStatus ConfigArgRangeUInt8 (const Config *c, int Index, uint8_t *From, uint8_t *To);
|
|
ConfigStatus ConfigArgRangeUInt16 (const Config *c, int Index, uint16_t *From, uint16_t *To);
|
|
ConfigStatus ConfigArgRangeUInt32 (const Config *c, int Index, uint32_t *From, uint32_t *To);
|
|
ConfigStatus ConfigArgRangeUInt64 (const Config *c, int Index, uint64_t *From, uint64_t *To);
|
|
|
|
ConfigStatus ConfigArgRangeSInt (const Config *c, int Index, int *From, int *To);
|
|
ConfigStatus ConfigArgRangeSInt8 (const Config *c, int Index, int8_t *From, int8_t *To);
|
|
ConfigStatus ConfigArgRangeSInt16 (const Config *c, int Index, int16_t *From, int16_t *To);
|
|
ConfigStatus ConfigArgRangeSInt32 (const Config *c, int Index, int32_t *From, int32_t *To);
|
|
ConfigStatus ConfigArgRangeSInt64 (const Config *c, int Index, int64_t *From, int64_t *To);
|
|
|
|
/****************************************************************************
|
|
*
|
|
* Utility functions
|
|
*
|
|
****************************************************************************
|
|
*
|
|
* const char *ConfigStatusToString (ConfigStatus Status);
|
|
* const char *ConfigName (const Config *c);
|
|
* const char *ConfigNodeName (const Config *c);
|
|
* void ConfigPrint (const Config *c, FILE *f);
|
|
*
|
|
* Description:
|
|
* ConfigStatusToString() returns a string describing the specified
|
|
* status code.
|
|
*
|
|
* ConfigNodeName() returns the name of a given node.
|
|
*
|
|
* ConfigName() returns the canonical "full name" of a node. This
|
|
* will be a (period-separated) path by which this node may be
|
|
* retrieved from the root, but is not necessarily the path the
|
|
* caller used to find the node.
|
|
*
|
|
* ConfigPrint() will output an ASCII representation of a Config
|
|
* (sub-)tree to a stream. References will not be followed.
|
|
*
|
|
****************************************************************************/
|
|
|
|
const char *ConfigStatusToString (ConfigStatus Status);
|
|
const char *ConfigName (const Config *c);
|
|
const char *ConfigNodeName (const Config *c);
|
|
void ConfigPrint (const Config *c, FILE *f);
|
|
void ConfigPrintXML (const Config *c, FILE *f);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|