src/server/config/initconf.c

Sun, 27 Nov 2022 10:20:10 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Nov 2022 10:20:10 +0100
changeset 442
05c2b62448b1
parent 418
b7dcc9c4f270
child 459
f21b4ff81c01
permissions
-rw-r--r--

fix stdout log message duplicates

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "initconf.h"

InitConfig *initconfig_load(const char *file) {
    CxMempool *mp = cxBasicMempoolCreate(512);
    if(!mp) {
        return NULL;
    }
    
    // setup parser
    ConfigParser2 parser;
    memset(&parser, 0, sizeof(ConfigParser2));
    parser.mp = mp;
    parser.validateDirective = initconfig_validate_directive;
    parser.allow_hierarchy = 0;
    
    ConfigNode *init_config = serverconfig_load_file(&parser, file);
    if(!init_config) {
        cxMempoolDestroy(mp);
        return NULL;
    }
    
    InitConfig *conf = cxMalloc(mp->allocator, sizeof(InitConfig));
    if(!conf) {
        cxMempoolDestroy(mp);
        return NULL;
    }
    
    conf->mp = mp;
    conf->root = init_config;

    return conf;
}

int initconfig_validate_directive(ConfigParser2 *parser, ConfigNode *node) {
    // check directive type
    // currently only Init is supported
    const char *types[] = { "Init" };
    size_t typeindex;
    if(serverconfig_validate_directive_name(node, types, 1, &typeindex)) {
        return 1;
    }
    
    // init directives must have parameter names
    ConfigParam *param_err;
    if(serverconfig_check_param_names(node, &param_err)) {
        return 1;
    }
    
    // check if the fn parameter exists
    cxstring fn = serverconfig_directive_get_arg(node, cx_str("fn"));
    if(fn.length == 0) {
        return 1;
    }
    
    return 0;
}

void initconfig_free(InitConfig *conf) {
    cxMempoolDestroy(conf->mp);
}

mercurial