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

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.2 ! misho       6: * $Id: parse.c,v 1.6.4.1 2012/04/02 14:39:02 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.6.4.1   misho     258: #if 0
1.1       misho     259: /*
                    260:  * WriteConfig() Write to file from items in config list
                    261:  * @f = file resource
                    262:  * @cfg = Head list element
                    263:  * return: 0 ok; -1 error:: can`t write to file
                    264: */
                    265: int WriteConfig(FILE *f, sl_config * __restrict cfg)
                    266: {
1.3       misho     267:        return cfgWrite(f, cfg, 1);
                    268: }
                    269: 
                    270: /*
                    271:  * cfg_WriteConfig() Write to file from items in config list without whitespaces!
                    272:  * @f = file resource
                    273:  * @cfg = Head list element
                    274:  * return: 0 ok; -1 error:: can`t write to file
                    275: */
                    276: int cfg_WriteConfig(FILE *f, sl_config * __restrict cfg)
                    277: {
                    278:        return cfgWrite(f, cfg, 0);
                    279: }
                    280: 
                    281: /*
                    282:  * ConcatConfig() Concat two list in one
                    283:  * @cfg = Head list element of main list
                    284:  * @add_cfg = Head list element of added list
                    285:  * return: 0 ok; -1 error:: can`t concat lists
                    286: */
                    287: int ConcatConfig(sl_config * __restrict cfg, sl_config * __restrict add_cfg)
                    288: {
                    289:        struct tagPair *item;
                    290:        int ret = 0;
                    291: 
                    292:        if (!cfg || !add_cfg)
                    293:                return -1;
                    294: 
                    295:        for (item = cfg->slh_first; item->sle_next; item = item->sle_next);
                    296:        item->sle_next = add_cfg->slh_first;
                    297: 
                    298:        add_cfg->slh_first = NULL;
                    299: 
                    300:        return ret;
                    301: }
1.1       misho     302: 
1.3       misho     303: /*
                    304:  * MergeConfig() Marge two list in one cfg and destroy add_cfg
                    305:  * @cfg = Head list element of main list
                    306:  * @add_cfg = Head list element of merged list (destroy after all!)
                    307:  * return: 0 ok; -1 error:: can`t merge lists
                    308: */
                    309: int MergeConfig(sl_config * __restrict cfg, sl_config * __restrict add_cfg)
                    310: {
                    311:        struct tagPair *item, *merge, *add_next, *next = NULL;
                    312:        int flg;
1.1       misho     313: 
1.3       misho     314:        if (!cfg || !add_cfg)
1.1       misho     315:                return -1;
                    316: 
1.3       misho     317:        item = add_cfg->slh_first;
                    318:        while (item) {
                    319:                add_next = item->sle_next;
                    320: 
                    321:                for (flg = 0, merge = cfg->slh_first, next = merge->sle_next; next; 
                    322:                                merge = merge->sle_next, next = merge->sle_next) {
                    323:                        if (!merge->psSection && !item->psSection) {
                    324:                                flg = 1;
                    325:                                merge->sle_next = item;
                    326:                                item->sle_next = next;
                    327:                                break;
                    328:                        }
                    329:                        if (merge->psSection && item->psSection && 
                    330:                                        !strcmp((char*) merge->psSection, (char*) item->psSection)) {
                    331:                                flg = 1;
                    332:                                merge->sle_next = item;
                    333:                                item->sle_next = next;
                    334:                                break;
1.1       misho     335:                        }
                    336:                }
1.3       misho     337: 
                    338:                if (!flg) {
                    339:                        if (!merge->sle_next) {
                    340:                                merge->sle_next = item;
                    341:                                item->sle_next = NULL;
                    342:                        } else
1.1       misho     343:                                return -1;
                    344:                }
                    345: 
1.3       misho     346:                item = add_next;
1.1       misho     347:        }
                    348: 
1.3       misho     349:        add_cfg->slh_first = NULL;
1.1       misho     350: 
                    351:        return 0;
                    352: }
1.6.4.1   misho     353: #endif

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