Annotation of libaitcfg/src/aitcfg.c, revision 1.4.4.1

1.2       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.4.4.1 ! misho       6: * $Id: aitcfg.c,v 1.4 2011/05/01 17:24:28 misho Exp $
1.2       misho       7: *
1.4       misho       8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.4.4.1 ! misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.4       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: #include "aitcfg.h"
                     48: 
                     49: 
                     50: #pragma GCC visibility push(hidden)
                     51: 
1.4.4.1 ! misho      52: int cfg_Errno;
        !            53: char cfg_Error[STRSIZ];
        !            54: 
        !            55: inline int
        !            56: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
        !            57: {
        !            58:        int ret;
        !            59: 
        !            60:        assert(a && b);
        !            61: 
        !            62:        ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - 
        !            63:                ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr));
        !            64: 
        !            65:        if (ret < 0)
        !            66:                return -1;
        !            67:        else if (ret > 0)
        !            68:                return 1;
        !            69: 
        !            70:        return ret;
        !            71: }
        !            72: 
        !            73: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
1.1       misho      74: 
                     75: #pragma GCC visibility pop
                     76: 
                     77: 
1.4.4.1 ! misho      78: // cfg_GetErrno() Get error code of last operation
        !            79: inline int
        !            80: cfg_GetErrno()
1.1       misho      81: {
1.4.4.1 ! misho      82:        return cfg_Errno;
        !            83: }
        !            84: 
        !            85: // cfg_GetError() Get error text of last operation
        !            86: inline const char *
        !            87: cfg_GetError()
        !            88: {
        !            89:        return cfg_Error;
        !            90: }
1.1       misho      91: 
1.4.4.1 ! misho      92: // cfg_SetErr() Set error to variables for internal use!!!
        !            93: inline void
        !            94: cfg_SetErr(int eno, char *estr, ...)
        !            95: {
        !            96:        va_list lst;
        !            97: 
        !            98:        cfg_Errno = eno;
        !            99:        memset(cfg_Error, 0, sizeof cfg_Error);
        !           100:        va_start(lst, estr);
        !           101:        vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
        !           102:        va_end(lst);
1.1       misho     103: }
                    104: 
1.4.4.1 ! misho     105: 
1.1       misho     106: /*
1.4.4.1 ! misho     107:  * cfgLoadConfig() - Load config from file
        !           108:  *
        !           109:  * @cfgName = Config filename
        !           110:  * @cfg = Config root
        !           111:  * return: -1 error or 0 ok
        !           112:  */
        !           113: int
        !           114: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
1.1       misho     115: {
                    116:        FILE *f;
                    117:        int ret;
                    118: 
1.4.4.1 ! misho     119:        if (!cfgName || !cfg) {
        !           120:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
1.1       misho     121:                return -1;
1.4.4.1 ! misho     122:        } else {
        !           123: #ifdef HAVE_LIBPTHREAD
        !           124:                pthread_mutex_init(&cfg->rc_mtx, NULL);
        !           125: #endif
        !           126:                SLIST_INIT(cfg);
        !           127:                RB_INIT(cfg);
1.1       misho     128:        }
                    129: 
1.4.4.1 ! misho     130:        f = fopen(cfgName, "r");
1.1       misho     131:        if (!f) {
                    132:                LOGERR;
                    133:                return -1;
                    134:        }
1.4.4.1 ! misho     135: 
        !           136:        ret = cfgReadConfig(f, cfg);
1.1       misho     137: 
                    138:        fclose(f);
                    139:        return ret;
                    140: }
                    141: 
                    142: /*
1.4.4.1 ! misho     143:  * cfgUnloadConfig() - Unload config from memory and free resources
        !           144:  *
        !           145:  * @cfg = Config root
        !           146:  * return: none
        !           147:  */
        !           148: void
        !           149: cfgUnloadConfig(cfg_root_t * __restrict cfg)
1.1       misho     150: {
1.4.4.1 ! misho     151:        struct tagCfg *av;
1.1       misho     152: 
1.4.4.1 ! misho     153:        if (!cfg)
1.1       misho     154:                return;
                    155: 
1.4.4.1 ! misho     156:        CFG_RC_LOCK(cfg);
        !           157:        while ((av = SLIST_FIRST(cfg))) {
        !           158:                SLIST_REMOVE_HEAD(cfg, cfg_next);
        !           159: 
        !           160:                AIT_FREE_VAL(&av->cfg_val);
        !           161:                AIT_FREE_VAL(&av->cfg_attr);
        !           162:                AIT_FREE_VAL(&av->cfg_sec);
1.1       misho     163:                free(av);
                    164:        }
1.4.4.1 ! misho     165:        cfg->rbh_root = NULL;
        !           166:        CFG_RC_UNLOCK(cfg);
        !           167: 
        !           168: #ifdef HAVE_LIBPTHREAD
        !           169:        pthread_mutex_destroy(&cfg->rc_mtx);
        !           170: #endif
1.1       misho     171: }
                    172: 
1.4.4.1 ! misho     173: #if 0
1.1       misho     174: /*
                    175:  * CreateConfig() Create config file from memory
                    176:  * @csConfigName = New config filename
                    177:  * @cfg = Head list element
                    178:  * return: 0 ok; -1 error:: can`t save new config
                    179: */
                    180: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
                    181: {
                    182:        FILE *f;
                    183:        int ret;
                    184: 
                    185:        if (!csConfigName || !cfg)
                    186:                return -1;
                    187: 
                    188:        f = fopen(csConfigName, "wt");
                    189:        if (!f) {
                    190:                LOGERR;
                    191:                return -1;
                    192:        }
                    193:        
                    194:        ret ^= ret;
                    195:        ret = WriteConfig(f, cfg);
                    196: 
                    197:        fclose(f);
                    198:        return ret;
                    199: }
                    200: 
1.3       misho     201: /*
                    202:  * cfg_CreateConfig() Create config file from memory without whitespaces!
                    203:  * @csConfigName = New config filename
                    204:  * @cfg = Head list element
                    205:  * return: 0 ok; -1 error:: can`t save new config
                    206: */
                    207: int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
                    208: {
                    209:        FILE *f;
                    210:        int ret;
                    211: 
                    212:        if (!csConfigName || !cfg)
                    213:                return -1;
                    214: 
                    215:        f = fopen(csConfigName, "wt");
                    216:        if (!f) {
                    217:                LOGERR;
                    218:                return -1;
                    219:        }
                    220:        
                    221:        ret ^= ret;
                    222:        ret = cfg_WriteConfig(f, cfg);
                    223: 
                    224:        fclose(f);
                    225:        return ret;
                    226: }
1.4.4.1 ! misho     227: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>