Annotation of libaitcfg/src/cfgprog.c, revision 1.1.2.3

1.1.2.1   misho       1: #include "global.h"
                      2: #include "aitcfg.h"
                      3: 
                      4: 
1.1.2.2   misho       5: cfg_root_t cfg;
                      6: char cfgname[MAXPATHLEN], section[STRSIZ];
                      7: 
                      8: 
                      9: static void
                     10: Usage(const char *prog)
                     11: {
                     12:        printf( " -= CFGPROG =- ELWIX config tool ver.%s\n"
                     13:                "(C)`25 by Michael Pounov <misho@elwix.org>\n"
                     14:                "==========================================\n"
                     15:                " Syntax: %s [-options] <config> [attribute] [value]\n\n"
                     16:                "\t-s <section>\tScope of attribute\n"
                     17:                "\t-w\t\tSet and write attribute\n"
                     18:                "\t-v\t\tVerbose, more -v more verbosity\n"
                     19:                "\t-h\t\tHelp, this help screen!\n", 
                     20:                PACKAGE_VERSION, prog);
                     21: }
                     22: 
1.1.2.1   misho      23: int
1.1.2.2   misho      24: Get(const char *prog, int argc, char **argv)
1.1.2.1   misho      25: {
1.1.2.3 ! misho      26:        const char *str;
        !            27:        int i, ret = 0;
        !            28:        array_t *sec;
        !            29:        struct tagCfg *av;
        !            30: 
1.1.2.2   misho      31:        EVERBS(1) printf("Get value for %s in scope %s from config %s\n", 
                     32:                        *argv ? *argv : "*", section, prog);
                     33: 
1.1.2.3 ! misho      34:        if (*argv) {
        !            35:                str = cfg_getAttribute(&cfg, section, *argv);
        !            36:                if (str)
        !            37:                        printf("%s=%s\n", *argv, str);
        !            38:                else
        !            39:                        ret = 3;
        !            40:        } else {
        !            41:                sec = cfg_getSection(&cfg, section);
        !            42:                if (sec) {
        !            43:                        for (i = 0; i < array_Size(sec) && 
        !            44:                                        (av = array(sec, i, struct tagCfg*)); i++) {
        !            45:                                if (AIT_GET_STR(&av->cfg_attr))
        !            46:                                        printf("%s=%s\n", 
        !            47:                                                        AIT_GET_STR(&av->cfg_attr), 
        !            48:                                                        AIT_GET_STR(&av->cfg_val));
        !            49:                        }
        !            50:                        array_Destroy(&sec);
        !            51:                } else
        !            52:                        ret = 3;
        !            53:        }
        !            54: 
        !            55:        return ret;
1.1.2.1   misho      56: }
1.1.2.2   misho      57: 
                     58: int
                     59: Set(const char *prog, int argc, char **argv)
                     60: {
                     61:        if (argc < 2) {
                     62:                printf("Error:: missing attribute and value arguments!\n");
                     63:                return 1;
                     64:        }
                     65: 
                     66:        EVERBS(1) printf("Set value %s for %s in scope %s from config %s\n", 
                     67:                        argv[1], argv[0], section, prog);
                     68: 
                     69:        return 0;
                     70: }
                     71: 
                     72: 
                     73: int
                     74: main(int argc, char **argv)
                     75: {
1.1.2.3 ! misho      76:        int ch, ret = 0;
1.1.2.2   misho      77:        int (*run)(const char*, int, char**) = Get;
                     78:        const char *prog;
                     79:        char *str;
                     80: 
                     81:        if (!argv || !*argv)
                     82:                prog = "cfgprog";
                     83:        else if (!(prog = strrchr(*argv, '/')))
                     84:                prog = *argv;
                     85:        else
                     86:                prog++;
                     87: 
                     88:        while ((ch = getopt(argc, argv, "hvws:")) != -1)
                     89:                switch (ch) {
                     90:                        case 'w':
                     91:                                run = Set;
                     92:                                break;
                     93:                        case 's':
                     94:                                strlcpy(section, optarg, sizeof section);
                     95:                                break;
                     96:                        case 'v':
                     97:                                e_incVerbose;
                     98:                                break;
                     99:                        default:
                    100:                                Usage(prog);
                    101:                                return 1;
                    102:                }
                    103:        argc -= optind;
                    104:        argv += optind;
                    105: 
                    106:        if (!strcmp(prog, "cfg_set"))
                    107:                run = Set;
                    108:        else if (!strcmp(prog, "cfg_get"))
                    109:                run = Get;
                    110: 
                    111:        if (!argc) {
                    112:                printf("Error:: missing config name!\n");
                    113:                Usage(prog);
                    114:                return 1;
                    115:        } else {
                    116:                strlcpy(cfgname, *argv, sizeof cfgname);
                    117:                argc--;
                    118:                argv++;
                    119:        }
                    120: 
                    121:        if (argc && *argv && (str = strchr(*argv, '/'))) {
                    122:                *str++ = 0;
                    123:                strlcpy(section, *argv, sizeof section);
                    124:                *argv = str;
                    125:        }
                    126: 
1.1.2.3 ! misho     127:        if (cfgLoadConfig(cfgname, &cfg)) {
        !           128:                printf("Error:: load config %s #%d - %s\n", 
        !           129:                                cfgname, cfg_GetErrno(), cfg_GetError());
        !           130:                return 2;
        !           131:        }
        !           132: 
        !           133:        ret = run(prog, argc, argv);
        !           134: 
        !           135:        cfgUnloadConfig(&cfg);
        !           136:        return ret;
1.1.2.2   misho     137: }

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