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

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.7     ! misho       6: * $Id: aitcfg.c,v 1.6.2.1 2012/07/25 12:37:06 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.5       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.5       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.5       misho      78: // cfg_GetErrno() Get error code of last operation
                     79: inline int
                     80: cfg_GetErrno()
                     81: {
                     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: }
                     91: 
                     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);
                    103: }
                    104: 
                    105: 
1.1       misho     106: /*
1.5       misho     107:  * cfgInitConfig() - Init config root
                    108:  *
                    109:  * @cfg = Config root
                    110:  * return: -1 error or 0 ok
                    111:  */
                    112: int
                    113: cfgInitConfig(cfg_root_t * __restrict cfg)
1.1       misho     114: {
                    115:        if (!cfg)
                    116:                return -1;
                    117: 
1.5       misho     118:        pthread_mutex_init(&cfg->rc_mtx, NULL);
                    119: 
                    120:        SLIST_INIT(cfg);
                    121:        RB_INIT(cfg);
1.2       misho     122:        return 0;
1.1       misho     123: }
                    124: 
                    125: /*
1.5       misho     126:  * cfgLoadConfig() - Load config from file
                    127:  *
                    128:  * @cfgName = Config filename
                    129:  * @cfg = Config root
                    130:  * return: -1 error or 0 ok
                    131:  */
                    132: int
                    133: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
1.1       misho     134: {
                    135:        FILE *f;
                    136:        int ret;
                    137: 
1.5       misho     138:        if (!cfgName || !cfg) {
                    139:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
1.1       misho     140:                return -1;
1.5       misho     141:        } else
                    142:                cfgInitConfig(cfg);
1.1       misho     143: 
1.5       misho     144:        f = fopen(cfgName, "r");
                    145:        if (!f) {
1.1       misho     146:                LOGERR;
                    147:                return -1;
                    148:        }
                    149: 
1.5       misho     150:        ret = cfgReadConfig(f, cfg);
1.1       misho     151: 
                    152:        fclose(f);
                    153:        return ret;
                    154: }
                    155: 
                    156: /*
1.7     ! misho     157:  * cfgClearConfig() - Clear config and free resources
1.5       misho     158:  *
                    159:  * @cfg = Config root
                    160:  * return: none
                    161:  */
                    162: void
1.7     ! misho     163: cfgClearConfig(cfg_root_t * __restrict cfg)
1.1       misho     164: {
1.5       misho     165:        struct tagCfg *av;
1.1       misho     166: 
1.5       misho     167:        if (!cfg)
1.1       misho     168:                return;
                    169: 
1.5       misho     170:        CFG_RC_LOCK(cfg);
                    171:        while ((av = SLIST_FIRST(cfg))) {
                    172:                SLIST_REMOVE_HEAD(cfg, cfg_next);
                    173: 
                    174:                AIT_FREE_VAL(&av->cfg_val);
                    175:                AIT_FREE_VAL(&av->cfg_attr);
                    176:                AIT_FREE_VAL(&av->cfg_sec);
1.6       misho     177:                io_free(av);
1.1       misho     178:        }
1.5       misho     179:        cfg->rbh_root = NULL;
                    180:        CFG_RC_UNLOCK(cfg);
1.7     ! misho     181: }
        !           182: 
        !           183: /*
        !           184:  * cfgUnloadConfig() - Unload config from memory and destroy resources
        !           185:  *
        !           186:  * @cfg = Config root
        !           187:  * return: none
        !           188:  */
        !           189: void
        !           190: cfgUnloadConfig(cfg_root_t * __restrict cfg)
        !           191: {
        !           192:        if (!cfg)
        !           193:                return;
1.1       misho     194: 
1.7     ! misho     195:        cfgClearConfig(cfg);
1.5       misho     196:        pthread_mutex_destroy(&cfg->rc_mtx);
1.1       misho     197: }
                    198: 
1.3       misho     199: /*
1.5       misho     200:  * cfgCreateConfig() - Create config file from memory
                    201:  *
1.3       misho     202:  * @csConfigName = New config filename
1.5       misho     203:  * @cfg = Config root
                    204:  * @whitespace = Additional whitespace characters to file
                    205:  * return: -1 error or 0 ok
                    206:  */
                    207: int
                    208: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
1.3       misho     209: {
                    210:        FILE *f;
                    211:        int ret;
                    212: 
                    213:        if (!csConfigName || !cfg)
                    214:                return -1;
                    215: 
1.5       misho     216:        f = fopen(csConfigName, "w");
1.3       misho     217:        if (!f) {
                    218:                LOGERR;
                    219:                return -1;
                    220:        }
                    221:        
1.5       misho     222:        ret = cfgWriteConfig(f, cfg, whitespace);
1.3       misho     223: 
                    224:        fclose(f);
                    225:        return ret;
                    226: }

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