Annotation of libaitcfg/src/parse.c, revision 1.6.4.4

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.6.4.4 ! misho       6: * $Id: parse.c,v 1.6.4.3 2012/04/03 12:54:49 misho Exp $
1.2       misho       7: *
1.6       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.6.4.1   misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.6       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: 
1.6.4.2   misho      50: static inline int
                     51: cfg_Write(FILE *f, char *fmt, ...)
1.1       misho      52: {
                     53:        int ret = 0;
                     54:        va_list lst;
                     55: 
                     56:        va_start(lst, fmt);
                     57:        ret = vfprintf(f, fmt, lst);
                     58:        va_end(lst);
                     59: 
                     60:        return ret;
                     61: }
                     62: 
1.6.4.2   misho      63: static inline void
                     64: _invertQueue(cfg_root_t * __restrict cfg)
1.3       misho      65: {
1.6.4.2   misho      66:        struct tagCfg *item, *next, *prev = NULL;
1.3       misho      67: 
1.6.4.2   misho      68:        SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
                     69:                item->cfg_next.sle_next = prev;
1.3       misho      70:                prev = item;
                     71:        }
                     72:        cfg->slh_first = prev;
                     73: }
                     74: 
1.1       misho      75: 
                     76: /*
1.6.4.1   misho      77:  * cfgReadConfig() - Read file and add new item at config root
                     78:  *
                     79:  * @f = File resource
                     80:  * @cfg = Config root
                     81:  * return: -1 error or 0 ok
                     82:  */
                     83: int cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.1       misho      84: {
1.6.4.1   misho      85:        char line[BUFSIZ];
                     86:        struct tagCfg *av = NULL;
                     87:        int flg = 0;
                     88:        char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.1       misho      89: 
                     90:        while (!feof(f)) {
1.6.4.1   misho      91:                memset(line, 0, sizeof line);
                     92:                fgets(line, sizeof line - 1, f);
                     93: #ifdef SUPPORT_USER_EOF
                     94:                /* check for user end-of-file */
                     95:                if (line[0] == '.' && line[1] == '\n')
                     96:                        break;
1.1       misho      97: #endif
1.6.4.1   misho      98:                if (!(psAttr = strpbrk(line, "\r\n"))) {
                     99:                        /* skip line, too long */
                    100:                        continue;
                    101:                } else {
                    102:                        *psAttr = 0;
                    103:                        io_TrimStr(line);
                    104:                }
1.1       misho     105: 
1.6.4.1   misho     106:                if (flg) {
                    107:                        /* continues line */
                    108:                        if (!av)
                    109:                                continue;
                    110:                        else
                    111:                                psAttr = line + strlen(line) - 1;
                    112:                        if (*psAttr == '\\')
                    113:                                *psAttr = 0;
                    114:                        else
                    115:                                flg = 0;
                    116:                        /* concat line to value */
                    117:                        AIT_SET_STRCAT(&av->cfg_val, line);
                    118:                        if (!flg)
                    119:                                io_UnquotStr((char*) AIT_GET_STR(&av->cfg_val));
1.1       misho     120:                        continue;
1.6.4.1   misho     121:                }
1.1       misho     122: 
1.6.4.1   misho     123:                /* *NEW PAIR* alloc new pair element */
                    124:                av = malloc(sizeof(struct tagCfg));
                    125:                if (!av) {
                    126:                        LOGERR;
                    127:                        return -1;
                    128:                } else {
                    129:                        memset(av, 0, sizeof(struct tagCfg));
                    130:                        CFG_RC_LOCK(cfg);
                    131:                        SLIST_INSERT_HEAD(cfg, av, cfg_next);
                    132:                        CFG_RC_UNLOCK(cfg);
                    133:                }
1.1       misho     134: 
1.6.4.1   misho     135:                /* check for continues line */
                    136:                psAttr = line + strlen(line) - 1;
                    137:                if (*psAttr == '\\') {
                    138:                        *psAttr = 0;
                    139:                        flg = 1;
1.1       misho     140:                }
                    141: 
1.6.4.1   misho     142:                /* check for comment or empty line */
                    143:                if (!*line || *line == '#' || *line == ';') {
                    144:                        AIT_SET_STR(&av->cfg_val, line);
1.1       misho     145:                        continue;
1.6.4.1   misho     146:                }
                    147:                /* section */
                    148:                if (*line == '[') {
                    149:                        AIT_SET_STR(&av->cfg_val, line);
                    150:                        psAttr = line + strlen(line) - 1;
                    151:                        if (*psAttr == ']') {
                    152:                                *psAttr = 0; 
                    153:                                flg = 0;
                    154:                                strlcpy(szSection, line + 1, sizeof szSection);
1.1       misho     155:                        } else
1.6.4.1   misho     156:                                ioDEBUG(7, "Ignore section '%s' ... not found ']'", line);
                    157:                        continue;
                    158:                }
                    159:                /* parse pair */
                    160:                if (!(psAttr = strchr(line, '='))) {
                    161:                        AIT_SET_STR(&av->cfg_val, line);
                    162:                        ioDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
                    163:                        continue;
                    164:                } else {
                    165:                        *psAttr = 0;
                    166:                        psVal = psAttr + 1;
                    167:                        psAttr = line;
                    168:                }
1.1       misho     169: 
1.6.4.1   misho     170:                /* if exists, added section name to element */
                    171:                if (*szSection) {
                    172:                        AIT_SET_STR(&av->cfg_sec, szSection);
                    173:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
                    174:                                        io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
1.1       misho     175:                }
1.6.4.1   misho     176: 
                    177:                io_RTrimStr(psAttr);
                    178:                io_LTrimStr(psVal);
                    179:                if (!flg)
                    180:                        io_UnquotStr(psVal);
                    181:                AIT_SET_STR(&av->cfg_val, psVal);
                    182:                AIT_SET_STR(&av->cfg_attr, psAttr);
                    183:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
                    184:                                io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
                    185: 
                    186:                CFG_RC_LOCK(cfg);
                    187:                RB_INSERT(tagRC, cfg, av);
                    188:                CFG_RC_UNLOCK(cfg);
1.1       misho     189:        }
                    190: 
                    191:        return 0;
                    192: }
                    193: 
1.6.4.2   misho     194: /*
                    195:  * cfgWriteConfig() - Write config from memory
                    196:  *
                    197:  * @f = File handle
                    198:  * @cfg = Config root
                    199:  * @whitespace = Additional whitespace characters to file
                    200:  * return: -1 error or 0 ok
                    201:  */
                    202: int
                    203: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
                    204: {
                    205:        struct tagCfg *av;
                    206:        time_t tim;
                    207:        char szTime[STRSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
                    208: 
                    209:        time(&tim);
                    210:        strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
                    211:        if (!cfg_Write(f, "## Write Config :: %s\n#\n", szTime)) {
                    212:                LOGERR;
                    213:                return -1;
                    214:        }
                    215: 
                    216:        CFG_RC_LOCK(cfg);
                    217:        _invertQueue(cfg);
                    218:        SLIST_FOREACH(av, cfg, cfg_next) {
                    219:                if (!AIT_ISEMPTY(&av->cfg_sec) && 
                    220:                                strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
                    221:                        strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
                    222:                        if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
                    223:                                LOGERR;
                    224:                                CFG_RC_UNLOCK(cfg);
                    225:                                return -1;
                    226:                        }
                    227:                }
                    228:                if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
                    229:                        memset(szSection, 0, sizeof szSection);
                    230:                        if (!cfg_Write(f, "\n[]\n")) {
                    231:                                LOGERR;
                    232:                                CFG_RC_UNLOCK(cfg);
                    233:                                return -1;
                    234:                        }
                    235:                }
                    236: 
                    237:                if (!cfg_Write(f, ((whitespace) ? "%s = %s\n" : "%s=%s\n"), 
                    238:                                        AIT_GET_STR(&av->cfg_attr), AIT_GET_STR(&av->cfg_val))) {
                    239:                        LOGERR;
                    240:                        CFG_RC_UNLOCK(cfg);
                    241:                        return -1;
                    242:                }
                    243:        }
                    244:        _invertQueue(cfg);
                    245:        CFG_RC_UNLOCK(cfg);
                    246: 
                    247:        memset(szTime, 0, sizeof szTime);
                    248:        time(&tim);
                    249:        strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
                    250:        if (!cfg_Write(f, "\n#\n## Done. :: %s\n", szTime)) {
                    251:                LOGERR;
                    252:                return -1;
                    253:        }
                    254: 
                    255:        return 0;
                    256: }
                    257: 
1.1       misho     258: /*
1.6.4.4 ! misho     259:  * cfgConcatConfig() - Concat two configs into one
        !           260:  *
        !           261:  * @cfg = Config root
        !           262:  * @add_cfg = Concated config will be destroy after merge
        !           263:  * return: -1 error or 0 ok
        !           264:  */
        !           265: int
        !           266: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3       misho     267: {
1.6.4.4 ! misho     268:        struct tagCfg *item;
1.3       misho     269: 
                    270:        if (!cfg || !add_cfg)
                    271:                return -1;
                    272: 
1.6.4.4 ! misho     273:        CFG_RC_LOCK(add_cfg);
        !           274:        CFG_RC_LOCK(cfg);
1.3       misho     275: 
1.6.4.4 ! misho     276:        /* concat lists */
        !           277:        for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
        !           278:        SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
        !           279: 
        !           280:        /* concat red-black trees */
        !           281:        SLIST_FOREACH(item, add_cfg, cfg_next)
        !           282:                RB_INSERT(tagRC, cfg, item);
1.3       misho     283: 
1.6.4.4 ! misho     284:        CFG_RC_UNLOCK(cfg);
        !           285:        CFG_RC_UNLOCK(add_cfg);
        !           286: 
        !           287:        add_cfg->slh_first = NULL;
        !           288:        add_cfg->rbh_root = NULL;
        !           289:        pthread_mutex_destroy(&add_cfg->rc_mtx);
        !           290:        return 0;
1.3       misho     291: }
1.1       misho     292: 
1.3       misho     293: /*
1.6.4.3   misho     294:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
                    295:  *
                    296:  * @cfg = Config root of main list
                    297:  * @add_cfg = Merged config will be destroy after merge
                    298:  * return: -1 error or 0 ok
                    299:  */
                    300: int
                    301: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3       misho     302: {
1.6.4.3   misho     303:        struct tagCfg *item, *merge, *add_next, *next = NULL;
1.3       misho     304:        int flg;
1.1       misho     305: 
1.3       misho     306:        if (!cfg || !add_cfg)
1.1       misho     307:                return -1;
                    308: 
1.6.4.3   misho     309:        CFG_RC_LOCK(add_cfg);
                    310:        CFG_RC_LOCK(cfg);
                    311:        SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
                    312:                flg = 0;
                    313:                SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
                    314:                        if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
                    315:                                SLIST_INSERT_AFTER(merge, item, cfg_next);
                    316:                                RB_INSERT(tagRC, cfg, item);
1.3       misho     317:                                flg = 1;
                    318:                                break;
                    319:                        }
1.6.4.3   misho     320:                        if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
                    321:                                        !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
                    322:                                SLIST_INSERT_AFTER(merge, item, cfg_next);
                    323:                                RB_INSERT(tagRC, cfg, item);
1.3       misho     324:                                flg = 1;
                    325:                                break;
1.1       misho     326:                        }
                    327:                }
1.3       misho     328: 
                    329:                if (!flg) {
1.6.4.3   misho     330:                        SLIST_INSERT_AFTER(merge, item, cfg_next);
                    331:                        RB_INSERT(tagRC, cfg, item);
1.1       misho     332:                }
                    333:        }
1.6.4.3   misho     334:        CFG_RC_UNLOCK(cfg);
                    335:        CFG_RC_UNLOCK(add_cfg);
1.1       misho     336: 
1.3       misho     337:        add_cfg->slh_first = NULL;
1.6.4.3   misho     338:        add_cfg->rbh_root = NULL;
                    339:        pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1       misho     340:        return 0;
                    341: }

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