Annotation of embedtools/src/cfger.c, revision 1.4

1.2       misho       1: /*************************************************************************
                      2:  * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.4     ! misho       6:  * $Id: cfger.c,v 1.3.2.1 2017/06/29 08:29:02 misho Exp $
1.2       misho       7:  *
                      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.3       misho      15: Copyright 2004 - 2017
1.2       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: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: int Verbose;
                     50: extern char compiled[], compiledby[], compilehost[];
                     51: 
                     52: cfg_root_t cfg;
                     53: 
                     54: 
                     55: static void
                     56: Usage()
                     57: {
                     58: 
                     59:        printf( "CFGer is tool for managment R/W operation with CFGs\n"
                     60:                "=== %s === %s@%s ===\n\n"
                     61:                "  Syntax: cfger [options] <file_config> [data]\n\n"
                     62:                "\t-v\t\tVerbose ...\n"
                     63:                "\t-q\t\tQuiet mode\n"
1.3       misho      64:                "\t-S\t\tShell script mode\n"
1.4     ! misho      65:                "\t-C\t\tWithout comment header\n"
1.2       misho      66:                "\t-o <output>\tOutput result to file\n"
                     67:                "\t-l\t\tList A/V pairs\n"
1.4     ! misho      68:                "\t-c <comment>\tAdds comment on header of file\n"
1.2       misho      69:                "\t-s <attr>\tSet A/V pair\n"
                     70:                "\t-g <attr>\tGet value from A/V pair\n"
                     71:                "*\"attr\" format: [section/]attribute\n"
                     72:                "\n", compiled, compiledby, compilehost);
                     73: }
                     74: 
                     75: int
                     76: main(int argc, char **argv)
                     77: {
1.3       misho      78:        char ch, *str, m = 0, q = 0, sh = 42, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
1.2       misho      79:        int ret = 0;
                     80:        ait_val_t data = AIT_VAL_INITIALIZER(data);
                     81:        FILE *out = stdout;
1.4     ! misho      82:        time_t tim;
        !            83:        struct tm tm;
        !            84:        char cm = 42, szComment[STRSIZ] = { [0 ... STRSIZ - 1] = 0 }, szTim[STRSIZ] = { 0 };
1.2       misho      85: 
1.4     ! misho      86:        while ((ch = getopt(argc, argv, "hvqlSCs:g:o:c:")) != -1)
1.2       misho      87:                switch (ch) {
                     88:                        case 'v':
                     89:                                Verbose++;
                     90:                                break;
                     91:                        case 'q':
                     92:                                q = 42;
                     93:                                break;
1.3       misho      94:                        case 'S':
                     95:                                sh ^= sh;
                     96:                                break;
1.4     ! misho      97:                        case 'C':
        !            98:                                cm ^= cm;
        !            99:                                break;
1.2       misho     100:                        case 'o':
                    101:                                strlcpy(szName, optarg, sizeof szName);
                    102:                                break;
1.4     ! misho     103:                        case 'c':
        !           104:                                strlcpy(szComment, optarg, sizeof szComment);
        !           105:                                break;
1.2       misho     106:                        case 'l':
                    107:                                if (m) {
                    108:                                        Usage();
                    109:                                        return 1;
                    110:                                } else
                    111:                                        m = 'l';
                    112:                                break;
                    113:                        case 's':
                    114:                                if (m) {
                    115:                                        Usage();
                    116:                                        return 1;
                    117:                                } else
                    118:                                        m = 's';
                    119:                                strlcpy(szAttr, optarg, sizeof szAttr);
                    120:                                break;
                    121:                        case 'g':
                    122:                                if (m) {
                    123:                                        Usage();
                    124:                                        return 1;
                    125:                                } else
                    126:                                        m = 'g';
                    127:                                strlcpy(szAttr, optarg, sizeof szAttr);
                    128:                                break;
                    129:                        case 'h':
                    130:                        default:
                    131:                                Usage();
                    132:                                return 1;
                    133:                }
                    134:        argc -= optind;
                    135:        argv += optind;
                    136:        if (!argc) {
                    137:                Usage();
                    138:                return 1;
                    139:        }
                    140:        if (argc > 1 && argv[1])
                    141:                AIT_SET_STR(&data, argv[1]);
                    142: 
                    143:        if (cfgLoadConfig(*argv, &cfg)) {
                    144:                ELIBERR(cfg);
                    145:                ret = 2;
                    146:                goto end;
                    147:        }
                    148: 
                    149:        if (*szName) {
                    150:                out = fopen(szName, "w");
                    151:                if (!out) {
                    152:                        printf("Error:: Can't create file %s\n", szName);
                    153:                        ret = 2;
                    154:                        out = stdout;
                    155:                        goto end;
                    156:                }
                    157:        }
                    158: 
                    159:        switch (m) {
                    160:                case 'g':
                    161:                        if (!q)
                    162:                                fprintf(out, "%s = ", szAttr);
                    163:                        str = strchr(szAttr, '/');
                    164:                        if (str) {
                    165:                                *str++ = 0;
                    166:                                str = (char*) cfg_getAttribute(&cfg, szAttr, str);
                    167:                        } else
                    168:                                str = (char*) cfg_getAttribute(&cfg, str, szAttr);
                    169:                        fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!");
                    170:                        break;
                    171:                case 's':
                    172:                        str = strchr(szAttr, '/');
                    173:                        if (str) {
                    174:                                *str++ = 0;
                    175:                                if (AIT_ISEMPTY(&data))
                    176:                                        cfg_unsetAttribute(&cfg, szAttr, str);
                    177:                                else
                    178:                                        cfg_setAttribute(&cfg, szAttr, str, 
                    179:                                                        AIT_GET_STRZ(&data));
                    180:                        } else {
                    181:                                if (AIT_ISEMPTY(&data))
                    182:                                        cfg_unsetAttribute(&cfg, str, szAttr);
                    183:                                else
                    184:                                        cfg_setAttribute(&cfg, str, szAttr, 
                    185:                                                        AIT_GET_STRZ(&data));
                    186:                        }
                    187:                case 'l':
                    188:                default:
1.4     ! misho     189:                        if (cm) {
        !           190:                                time(&tim);
        !           191:                                localtime_r(&tim, &tm);
        !           192:                                strftime(szTim, sizeof szTim, "%Y-%m-%d %H:%M:%S", &tm);
        !           193: 
        !           194:                                fprintf(out, "# Auto-generated file %s from cfger at %s\n#\n# %s\n\n", *argv, szTim, szComment);
        !           195:                        }
        !           196: 
1.3       misho     197:                        cfgWriteConfig(out, &cfg, sh);
1.2       misho     198:                        break;
                    199:        }
                    200: end:
                    201:        if (out != stdout)
                    202:                fclose(out);
                    203:        AIT_FREE_VAL(&data);
                    204:        cfgUnloadConfig(&cfg);
                    205:        return ret;
                    206: }

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