src/server/config/serverconfig.h

Fri, 01 Nov 2024 12:25:52 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 01 Nov 2024 12:25:52 +0100
changeset 562
69fc4ecc5f60
parent 459
f21b4ff81c01
permissions
-rw-r--r--

fix pgext uses a wrong field number, if the column has the same name as a resource or property column

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2020 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef WS_CONFIG_SERVERCONFIG_H
#define WS_CONFIG_SERVERCONFIG_H

#include <cx/linked_list.h>
#include <cx/hash_map.h>
#include <cx/mempool.h>
#include <cx/string.h>

#include <stdbool.h>

#include "conf.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CFG_NODE_ADD(list_begin, list_end, elm) \
    cx_linked_list_add((void**)list_begin, (void**)list_end, offsetof(ConfigNode, prev), offsetof(ConfigNode, next), elm)
    
typedef struct ServerConfig    ServerConfig;
typedef struct ConfigNode      ConfigNode;
typedef struct CFGToken        CFGToken;
typedef struct ConfigParser2   ConfigParser2; // TODO: rename to ConfigParser after old ConfigParser is removed

typedef enum ConfigNodeType    ConfigNodeType;
typedef enum CFGTokenType      CFGTokenType;

struct ServerConfig {
    CxMempool  *mp;
    ConfigNode *root;
    cxstring   tab;
};

enum ConfigNodeType {
    CONFIG_NODE_SPACE = 0,
    CONFIG_NODE_COMMENT,
    CONFIG_NODE_OBJECT,
    CONFIG_NODE_DIRECTIVE,
    CONFIG_NODE_OPEN_OBJECT,
    CONFIG_NODE_CLOSE_OBJECT
};

struct ConfigNode {
    cxmutstr text_begin;
    cxmutstr text_end; 
    ConfigNodeType type;
    
    cxmutstr name;
    ConfigParam *args;
    ConfigNode *children_begin;
    ConfigNode *children_end;
    
    ConfigNode *prev;
    ConfigNode *next;
};

typedef struct ConfigNodeStack ConfigNodeStack;
struct ConfigNodeStack {
    ConfigNode      *node;
    ConfigNodeStack *next;
};

enum CFGTokenType {
    CFG_NO_TOKEN = 0,
    CFG_TOKEN_COMMENT,
    CFG_TOKEN_SPACE,
    CFG_TOKEN_NEWLINE,
    CFG_TOKEN
};

struct CFGToken {
    cxstring content;
    CFGTokenType type;
};

enum ConfigParserError {
    CONFIG_PARSER_OOM = 0,
    CONFIG_PARSER_IO_ERROR,
    CONFIG_PARSER_SYNTAX_ERROR
};

/*
 * CfgValidateNodeFunc functions are called by the parser to validate
 * objects and directives.
 * 
 * The function must return 0 if the validation was successful. If the
 * functions returns a value != 0, the parser aborts.
 */
typedef int(*CfgValidateNodeFunc)(ConfigParser2 *parser, ConfigNode *node);

struct ConfigParser2 {
    CxMempool *mp;
    
    const char *filename;
    
    int linenum;
    int linepos;
    
    enum ConfigParserError error;
    
    const char *delim;
    
    /*
     * errno in case of IO errors (parser.error == CONFIG_PARSER_IO_ERROR)
     */
    int io_errno;
    
    /*
     * called when an object is started
     */
    CfgValidateNodeFunc validateObjBegin;
    
    /*
     * called when an object is closed
     */
    CfgValidateNodeFunc validateObjEnd;
    
    /*
     * called before a directive is added
     */
    CfgValidateNodeFunc validateDirective;
    
    /*
     * true: store result in a tree
     * false: flat list of directives, don't allow '{' and  '}'
     */
    bool allow_hierarchy;
};

ServerConfig* serverconfig_load(const char *file);

ConfigNode* serverconfig_load_file(ConfigParser2 *parser, const char *file);

ConfigNode* serverconfig_parse(ConfigParser2 *parser, cxstring content);

void serverconfig_free(ServerConfig *cfg);

ConfigNode* serverconfig_get_node(ConfigNode *parent, ConfigNodeType type, cxstring name);

CxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, cxstring name);

/*
 * Searches in the specified object for the directive with the name
 * directive_name and returns the value
 * 
 * Directives only have a value, if they have one argument
 */
cxstring serverconfig_object_directive_value(ConfigNode *obj, cxstring directive_name);

/*
 * Returns the directive parameter value that has the specified name
 */
cxstring serverconfig_directive_get_arg(ConfigNode *directive, cxstring param_name);


/*
 * Checks if the directive name matches any string in the names array.
 * If it matches a name, the function returns 0 and stores the index of the
 * matched name in nameindex
 * If it doesn't match a name, the function returns 1
 */
int serverconfig_validate_directive_name(
        ConfigNode *directive,
        const char *names[],
        size_t numnames,
        size_t *nameindex);

/*
 * Checks if all directive parameters have a name
 * If every parameter has a name, the function returns 0
 * otherwise it returns 1
 * 
 * If a parameter doesn't contain a name, err is set to this parameter
 * 
 * config file syntax: paramname="value"
 */
int serverconfig_check_param_names(ConfigNode *directive, ConfigParam **err);

/*
 * Returns the previous node that is a directive or object
 */
ConfigNode* serverconfig_previous_dir_or_obj(ConfigNode *node);

/*
 * Count the direct children that have the specified type
 */
size_t serverconfig_children_count(ConfigNode *node, ConfigNodeType type);

#ifdef __cplusplus
}
#endif

#endif /* WS_CONFIG_SERVERCONFIG_H */

mercurial