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

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

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