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

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.19    ! misho       6: * $Id: parse.c,v 1.18.4.1 2021/11/25 23:44:52 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.19    ! misho      15: Copyright 2004 - 2021
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: 
                     48: 
1.7       misho      49: /*
                     50:  * cfgReadConfig() - Read file and add new item at config root
                     51:  *
                     52:  * @f = File resource
                     53:  * @cfg = Config root
                     54:  * return: -1 error or 0 ok
                     55:  */
1.9       misho      56: int
                     57: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.3       misho      58: {
1.17      misho      59:        char line[BUFSIZ], origin[BUFSIZ];
1.7       misho      60:        struct tagCfg *av = NULL;
                     61:        int flg = 0;
                     62:        char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.18      misho      63:        FILE *ff;
1.3       misho      64: 
1.11      misho      65:        if (!f || !cfg) {
                     66:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
                     67:                return -1;
                     68:        }
                     69: 
1.7       misho      70:        while (!feof(f)) {
                     71:                memset(line, 0, sizeof line);
                     72:                fgets(line, sizeof line - 1, f);
                     73: #ifdef SUPPORT_USER_EOF
                     74:                /* check for user end-of-file */
                     75:                if (line[0] == '.' && line[1] == '\n')
                     76:                        break;
                     77: #endif
                     78:                if (!(psAttr = strpbrk(line, "\r\n"))) {
                     79:                        /* skip line, too long */
                     80:                        continue;
                     81:                } else {
                     82:                        *psAttr = 0;
1.17      misho      83:                        strlcpy(origin, line, sizeof origin);
1.12      misho      84:                        str_Trim(line);
1.7       misho      85:                }
1.3       misho      86: 
1.7       misho      87:                if (flg) {
                     88:                        /* continues line */
                     89:                        if (!av)
                     90:                                continue;
                     91:                        else
                     92:                                psAttr = line + strlen(line) - 1;
                     93:                        if (*psAttr == '\\')
                     94:                                *psAttr = 0;
                     95:                        else
                     96:                                flg = 0;
                     97:                        /* concat line to value */
                     98:                        AIT_SET_STRCAT(&av->cfg_val, line);
1.10      misho      99:                        if (!flg && AIT_ADDR(&av->cfg_val))
1.12      misho     100:                                str_Unquot((char*) AIT_GET_STR(&av->cfg_val));
1.18      misho     101: 
                    102:                        /* read include file */
                    103:                        if (!flg && AIT_ADDR(&av->cfg_val) && 
                    104:                                        *AIT_GET_STR(&av->cfg_attr) == '%' && 
                    105:                                        !strcmp(AIT_GET_STR(&av->cfg_attr), "%include")) {
                    106:                                ff = fopen(AIT_GET_STR(&av->cfg_val), "r");
                    107:                                if (ff) {
                    108:                                        cfgReadConfig(ff, cfg);
                    109:                                        fclose(ff);
                    110:                                } else
                    111:                                        EDEBUG(7, "Error:: Can't open %s file", 
                    112:                                                        AIT_GET_STR(&av->cfg_val));
                    113:                        }
1.7       misho     114:                        continue;
                    115:                }
1.3       misho     116: 
1.7       misho     117:                /* *NEW PAIR* alloc new pair element */
1.12      misho     118:                av = e_malloc(sizeof(struct tagCfg));
1.7       misho     119:                if (!av) {
1.12      misho     120:                        cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.7       misho     121:                        return -1;
                    122:                } else {
                    123:                        memset(av, 0, sizeof(struct tagCfg));
                    124:                        CFG_RC_LOCK(cfg);
1.14      misho     125:                        TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7       misho     126:                        CFG_RC_UNLOCK(cfg);
1.3       misho     127:                }
1.7       misho     128: 
1.18      misho     129:                /* check for comment or empty line */
                    130:                if (!*line || *line == '#' || *line == ';') {
                    131:                        AIT_SET_STR(&av->cfg_val, line);
                    132:                        continue;
                    133:                }
                    134: 
1.7       misho     135:                /* check for continues line */
1.8       misho     136:                psAttr = line + (*line ? strlen(line) : 1) - 1;
1.7       misho     137:                if (*psAttr == '\\') {
                    138:                        *psAttr = 0;
                    139:                        flg = 1;
1.3       misho     140:                }
                    141: 
1.7       misho     142:                /* section */
                    143:                if (*line == '[') {
                    144:                        psAttr = line + strlen(line) - 1;
                    145:                        if (*psAttr == ']') {
                    146:                                *psAttr = 0; 
                    147:                                flg = 0;
                    148:                                strlcpy(szSection, line + 1, sizeof szSection);
1.13      misho     149:                                AIT_SET_STR(&av->cfg_sec, line);
1.7       misho     150:                        } else
1.12      misho     151:                                EDEBUG(7, "Ignore section '%s' ... not found ']'", line);
1.7       misho     152:                        continue;
                    153:                }
                    154:                /* parse pair */
                    155:                if (!(psAttr = strchr(line, '='))) {
1.17      misho     156:                        AIT_SET_STR(&av->cfg_val, origin);
1.12      misho     157:                        EDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
1.7       misho     158:                        continue;
1.3       misho     159:                } else {
1.7       misho     160:                        *psAttr = 0;
                    161:                        psVal = psAttr + 1;
                    162:                        psAttr = line;
                    163:                }
                    164: 
                    165:                /* if exists, added section name to element */
                    166:                if (*szSection) {
                    167:                        AIT_SET_STR(&av->cfg_sec, szSection);
                    168:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
1.12      misho     169:                                        E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.3       misho     170:                }
                    171: 
1.12      misho     172:                str_RTrim(psAttr);
                    173:                str_LTrim(psVal);
1.7       misho     174:                if (!flg)
1.12      misho     175:                        str_Unquot(psVal);
1.7       misho     176:                AIT_SET_STR(&av->cfg_val, psVal);
                    177:                AIT_SET_STR(&av->cfg_attr, psAttr);
                    178:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
1.12      misho     179:                                E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7       misho     180: 
                    181:                CFG_RC_LOCK(cfg);
                    182:                RB_INSERT(tagRC, cfg, av);
                    183:                CFG_RC_UNLOCK(cfg);
1.18      misho     184: 
                    185:                /* read include file */
                    186:                if (!flg && *AIT_GET_STR(&av->cfg_attr) == '%' && 
                    187:                                !strcmp(AIT_GET_STR(&av->cfg_attr), "%include")) {
                    188:                        ff = fopen(AIT_GET_STR(&av->cfg_val), "r");
                    189:                        if (ff) {
                    190:                                cfgReadConfig(ff, cfg);
                    191:                                fclose(ff);
                    192:                        } else
                    193:                                EDEBUG(7, "Error:: Can't open %s file", 
                    194:                                                AIT_GET_STR(&av->cfg_val));
                    195:                }
1.3       misho     196:        }
                    197: 
                    198:        return 0;
                    199: }
                    200: 
1.1       misho     201: /*
1.7       misho     202:  * cfgWriteConfig() - Write config from memory
                    203:  *
                    204:  * @f = File handle
                    205:  * @cfg = Config root
                    206:  * @whitespace = Additional whitespace characters to file
                    207:  * return: -1 error or 0 ok
                    208:  */
                    209: int
                    210: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
1.1       misho     211: {
1.7       misho     212:        struct tagCfg *av;
1.13      misho     213:        char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
1.1       misho     214: 
1.11      misho     215:        if (!f || !cfg) {
                    216:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
                    217:                return -1;
                    218:        }
                    219: 
1.7       misho     220:        CFG_RC_LOCK(cfg);
1.14      misho     221:        RB_FOREACH(av, tagRC, cfg) {
1.13      misho     222:                /* empty lines or comment */
                    223:                if (AIT_ISEMPTY(&av->cfg_attr)) {
                    224:                        if (AIT_ISEMPTY(&av->cfg_val))
                    225:                                continue;
                    226:                        strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line);
                    227:                        goto skip_sec;
                    228:                }
                    229: 
                    230:                /* section [] */
1.10      misho     231:                if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) && 
1.14      misho     232:                                strcmp(AIT_GET_STRZ(&av->cfg_sec), szSection)) {
1.7       misho     233:                        strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
1.14      misho     234:                        if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
1.7       misho     235:                                LOGERR;
                    236:                                CFG_RC_UNLOCK(cfg);
                    237:                                return -1;
1.1       misho     238:                        }
1.13      misho     239:                } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
1.7       misho     240:                        memset(szSection, 0, sizeof szSection);
1.14      misho     241:                        if (!cfg_Write(f, "\n[]\n")) {
1.1       misho     242:                                LOGERR;
1.7       misho     243:                                CFG_RC_UNLOCK(cfg);
1.1       misho     244:                                return -1;
                    245:                        }
1.7       misho     246:                }
                    247: 
                    248:                /* build line */
                    249:                memset(line, 0, sizeof line);
1.11      misho     250:                if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
                    251:                        strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
1.7       misho     252:                        if (whitespace)
                    253:                                strlcat(line, " = ", sizeof line);
                    254:                        else
                    255:                                strlcat(line, "=", sizeof line);
                    256:                }
1.11      misho     257:                if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
                    258:                        strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
1.13      misho     259: skip_sec:
1.7       misho     260:                /* write */
                    261:                if (!cfg_Write(f, "%s\n", line)) {
                    262:                        LOGERR;
                    263:                        CFG_RC_UNLOCK(cfg);
                    264:                        return -1;
1.1       misho     265:                }
                    266:        }
1.7       misho     267:        CFG_RC_UNLOCK(cfg);
                    268: 
1.1       misho     269:        return 0;
                    270: }
                    271: 
                    272: /*
1.19    ! misho     273:  * cfgWriteConfigRaw() - Write config from memory by list
        !           274:  *
        !           275:  * @f = File handle
        !           276:  * @cfg = Config root
        !           277:  * @whitespace = Additional whitespace characters to file
        !           278:  * return: -1 error or 0 ok
        !           279:  */
        !           280: int
        !           281: cfgWriteConfigRaw(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
        !           282: {
        !           283:        struct tagCfg *av, *nxt;
        !           284:        char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
        !           285: 
        !           286:        if (!f || !cfg) {
        !           287:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
        !           288:                return -1;
        !           289:        }
        !           290: 
        !           291:        CFG_RC_LOCK(cfg);
        !           292:        TAILQ_FOREACH_SAFE(av, cfg, cfg_next, nxt) {
        !           293:                /* empty lines or comment */
        !           294:                if (AIT_ISEMPTY(&av->cfg_attr)) {
        !           295:                        if (AIT_ISEMPTY(&av->cfg_val))
        !           296:                                continue;
        !           297:                        strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line);
        !           298:                        goto skip_sec;
        !           299:                }
        !           300: 
        !           301:                /* section [] */
        !           302:                if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) && 
        !           303:                                strcmp(AIT_GET_STRZ(&av->cfg_sec), szSection)) {
        !           304:                        strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
        !           305:                        if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
        !           306:                                LOGERR;
        !           307:                                CFG_RC_UNLOCK(cfg);
        !           308:                                return -1;
        !           309:                        }
        !           310:                } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
        !           311:                        memset(szSection, 0, sizeof szSection);
        !           312:                        if (!cfg_Write(f, "\n[]\n")) {
        !           313:                                LOGERR;
        !           314:                                CFG_RC_UNLOCK(cfg);
        !           315:                                return -1;
        !           316:                        }
        !           317:                }
        !           318: 
        !           319:                /* build line */
        !           320:                memset(line, 0, sizeof line);
        !           321:                if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
        !           322:                        strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
        !           323:                        if (whitespace)
        !           324:                                strlcat(line, " = ", sizeof line);
        !           325:                        else
        !           326:                                strlcat(line, "=", sizeof line);
        !           327:                }
        !           328:                if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
        !           329:                        strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
        !           330: skip_sec:
        !           331:                /* write */
        !           332:                if (!cfg_Write(f, "%s\n", line)) {
        !           333:                        LOGERR;
        !           334:                        CFG_RC_UNLOCK(cfg);
        !           335:                        return -1;
        !           336:                }
        !           337:        }
        !           338:        CFG_RC_UNLOCK(cfg);
        !           339: 
        !           340:        return 0;
        !           341: }
        !           342: /*
1.7       misho     343:  * cfgConcatConfig() - Concat two configs into one
                    344:  *
                    345:  * @cfg = Config root
                    346:  * @add_cfg = Concated config will be destroy after merge
                    347:  * return: -1 error or 0 ok
                    348:  */
                    349: int
                    350: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.1       misho     351: {
1.7       misho     352:        struct tagCfg *item;
1.3       misho     353: 
1.7       misho     354:        if (!cfg || !add_cfg)
                    355:                return -1;
1.3       misho     356: 
1.7       misho     357:        CFG_RC_LOCK(add_cfg);
                    358:        CFG_RC_LOCK(cfg);
1.3       misho     359: 
1.14      misho     360:        /* concat lists & red-black trees */
                    361:        TAILQ_FOREACH(item, add_cfg, cfg_next) {
                    362:                TAILQ_INSERT_TAIL(cfg, item, cfg_next);
1.7       misho     363:                RB_INSERT(tagRC, cfg, item);
1.14      misho     364:        }
1.3       misho     365: 
1.7       misho     366:        CFG_RC_UNLOCK(cfg);
1.3       misho     367: 
1.14      misho     368:        TAILQ_INIT(add_cfg);
                    369:        RB_INIT(add_cfg);
1.11      misho     370:        CFG_RC_UNLOCK(add_cfg);
1.7       misho     371:        pthread_mutex_destroy(&add_cfg->rc_mtx);
                    372:        return 0;
1.3       misho     373: }
1.1       misho     374: 
1.3       misho     375: /*
1.7       misho     376:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
                    377:  *
                    378:  * @cfg = Config root of main list
                    379:  * @add_cfg = Merged config will be destroy after merge
                    380:  * return: -1 error or 0 ok
                    381:  */
                    382: int
                    383: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3       misho     384: {
1.10      misho     385:        struct tagCfg *item, *merge, *add_next, *next;
1.3       misho     386:        int flg;
1.1       misho     387: 
1.3       misho     388:        if (!cfg || !add_cfg)
1.1       misho     389:                return -1;
                    390: 
1.7       misho     391:        CFG_RC_LOCK(add_cfg);
                    392:        CFG_RC_LOCK(cfg);
1.10      misho     393: 
                    394:        /* merge lists */
1.14      misho     395:        TAILQ_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
1.7       misho     396:                flg = 0;
1.14      misho     397:                TAILQ_FOREACH_SAFE(merge, cfg, cfg_next, next) {
1.7       misho     398:                        if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
1.3       misho     399:                                flg = 1;
                    400:                                break;
                    401:                        }
1.7       misho     402:                        if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
1.10      misho     403:                                        AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
1.7       misho     404:                                        !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
1.3       misho     405:                                flg = 1;
                    406:                                break;
1.1       misho     407:                        }
                    408:                }
1.3       misho     409: 
1.10      misho     410:                if (!flg)
1.14      misho     411:                        TAILQ_INSERT_TAIL(cfg, item, cfg_next);
1.10      misho     412:                else
1.14      misho     413:                        TAILQ_INSERT_AFTER(cfg, merge, item, cfg_next);
1.10      misho     414:                RB_INSERT(tagRC, cfg, item);
1.1       misho     415:        }
1.10      misho     416: 
1.7       misho     417:        CFG_RC_UNLOCK(cfg);
1.1       misho     418: 
1.14      misho     419:        TAILQ_INIT(add_cfg);
                    420:        RB_INIT(add_cfg);
1.11      misho     421:        CFG_RC_UNLOCK(add_cfg);
1.7       misho     422:        pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1       misho     423:        return 0;
                    424: }
1.9       misho     425: 
                    426: /*
                    427:  * cfgReadLines() - Read custom lines and add new item at config root
                    428:  *
                    429:  * @f = File resource
                    430:  * @delim = Custom delimiter, if =NULL default is '='
                    431:  * @end = Custom user end of file, if =NULL default is EOF
                    432:  * @cfg = Config root
                    433:  * return: -1 error or 0 ok
                    434:  */
                    435: int
                    436: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
                    437: {
                    438:        char line[BUFSIZ];
                    439:        struct tagCfg *d, *av = NULL;
1.11      misho     440:        char *p, *psSec, *psAttr, *psVal;
                    441: 
                    442:        if (!cfg)
                    443:                return -1;
                    444:        if (!delim)
                    445:                delim = ATR_LINES_DELIM;
1.9       misho     446: 
                    447:        while (!feof(f)) {
1.11      misho     448:                psSec = psAttr = psVal = NULL;
1.9       misho     449:                memset(line, 0, sizeof line);
                    450:                fgets(line, sizeof line - 1, f);
                    451:                /* check for user end-of-file */
                    452:                if (strspn(line, end))
                    453:                        break;
                    454: 
                    455:                if (!(psAttr = strpbrk(line, "\r\n"))) {
                    456:                        /* skip line, too long */
                    457:                        continue;
                    458:                } else {
                    459:                        *psAttr = 0;
1.12      misho     460:                        str_Trim(line);
1.9       misho     461:                        if (!*line)
                    462:                                continue;
                    463:                }
                    464: 
1.12      misho     465:                if (!av_MakeExt(line, delim, &p, &psVal))
1.9       misho     466:                        continue;
                    467:                else {
1.12      misho     468:                        str_RTrim(p);
                    469:                        str_LTrim(psVal);
1.9       misho     470:                }
1.12      misho     471:                if (!av_MakeExt(p, SEC_LINES_DELIM, &psSec, &psAttr))
1.11      misho     472:                        psAttr = p;
1.9       misho     473: 
                    474:                /* *NEW PAIR* alloc new pair element */
1.12      misho     475:                av = e_malloc(sizeof(struct tagCfg));
1.9       misho     476:                if (!av) {
                    477:                        LOGERR;
                    478:                        return -1;
                    479:                } else
                    480:                        memset(av, 0, sizeof(struct tagCfg));
                    481: 
1.11      misho     482:                if (psSec) {
                    483:                        AIT_SET_STR(&av->cfg_sec, psSec);
                    484:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
1.12      misho     485:                                        E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.11      misho     486:                }
1.9       misho     487:                if (psVal)
                    488:                        AIT_SET_STR(&av->cfg_val, psVal);
                    489:                AIT_SET_STR(&av->cfg_attr, psAttr);
                    490:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
1.12      misho     491:                                E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.9       misho     492: 
                    493:                CFG_RC_LOCK(cfg);
                    494:                /* find & delete duplicates */
                    495:                if ((d = RB_FIND(tagRC, cfg, av))) {
                    496:                        RB_REMOVE(tagRC, cfg, d);
1.14      misho     497:                        TAILQ_REMOVE(cfg, d, cfg_next);
1.9       misho     498: 
                    499:                        AIT_FREE_VAL(&d->cfg_val);
                    500:                        AIT_FREE_VAL(&d->cfg_attr);
                    501:                        AIT_FREE_VAL(&d->cfg_sec);
1.12      misho     502:                        e_free(d);
1.9       misho     503:                }
                    504: 
1.14      misho     505:                TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.9       misho     506:                RB_INSERT(tagRC, cfg, av);
                    507:                CFG_RC_UNLOCK(cfg);
                    508:        }
                    509: 
                    510:        return 0;
                    511: }
                    512: 
1.11      misho     513: /*
                    514:  * cfgWriteLines() - Write custom lines and export data to variable
                    515:  *
                    516:  * @f = File resource
                    517:  * @delim = Custom delimiter, if =NULL default is '='
                    518:  * @eol = End of line string, if =NULL default is "\n"
                    519:  * @section = Export only section, if =NULL default is all
                    520:  * @cfg = Config root
1.12      misho     521:  * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
1.11      misho     522:  */
                    523: ait_val_t *
                    524: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
                    525: {
                    526:        ait_val_t *v = NULL;
                    527:        struct tagCfg *av;
                    528: 
                    529:        if (!cfg)
                    530:                return NULL;
                    531:        if (!delim)
                    532:                delim = ATR_LINES_DELIM;
                    533:        if (!eol)
                    534:                eol = EOL_LINES_DELIM;
1.12      misho     535:        if (!(v = ait_allocVar())) {
                    536:                cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.11      misho     537:                return NULL;
                    538:        } else
                    539:                AIT_INIT_VAL2(v, string);
                    540: 
1.14      misho     541:        TAILQ_FOREACH(av, cfg, cfg_next) {
1.11      misho     542:                if (section) {
                    543:                        if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
                    544:                                continue;
1.14      misho     545:                        if (strcmp(section, AIT_GET_STRZ(&av->cfg_sec)))
1.11      misho     546:                                continue;
                    547:                }
                    548: 
                    549:                if (!AIT_ISEMPTY(&av->cfg_sec)) {
                    550:                        AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
                    551:                        AIT_SET_STRCAT(v, SEC_LINES_DELIM);
                    552:                }
1.16      misho     553:                if (!AIT_ISEMPTY(&av->cfg_attr)) {
                    554:                        AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
                    555:                        AIT_SET_STRCAT(v, delim);
                    556:                }
1.11      misho     557:                if (!AIT_ISEMPTY(&av->cfg_val))
                    558:                        AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
                    559:                AIT_SET_STRCAT(v, eol);
                    560:        }
                    561: 
                    562:        if (f)
                    563:                fputs(AIT_GET_STR(v), f);
                    564:        return v;
                    565: }

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