Annotation of libaitcfg/src/cfgprog.c, revision 1.1.2.4
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;
1.1.2.4 ! misho 6: char cfgname[MAXPATHLEN], outname[MAXPATHLEN], section[STRSIZ];
! 7: FILE *out;
1.1.2.2 misho 8:
9:
10: static void
11: Usage(const char *prog)
12: {
13: printf( " -= CFGPROG =- ELWIX config tool ver.%s\n"
14: "(C)`25 by Michael Pounov <misho@elwix.org>\n"
15: "==========================================\n"
16: " Syntax: %s [-options] <config> [attribute] [value]\n\n"
17: "\t-s <section>\tScope of attribute\n"
1.1.2.4 ! misho 18: "\t-o <file>\tWrite updated config to file\n"
1.1.2.2 misho 19: "\t-w\t\tSet and write attribute\n"
20: "\t-v\t\tVerbose, more -v more verbosity\n"
21: "\t-h\t\tHelp, this help screen!\n",
22: PACKAGE_VERSION, prog);
23: }
24:
1.1.2.1 misho 25: int
1.1.2.2 misho 26: Get(const char *prog, int argc, char **argv)
1.1.2.1 misho 27: {
1.1.2.3 misho 28: const char *str;
29: int i, ret = 0;
30: array_t *sec;
31: struct tagCfg *av;
32:
1.1.2.2 misho 33: EVERBS(1) printf("Get value for %s in scope %s from config %s\n",
34: *argv ? *argv : "*", section, prog);
35:
1.1.2.3 misho 36: if (*argv) {
37: str = cfg_getAttribute(&cfg, section, *argv);
38: if (str)
39: printf("%s=%s\n", *argv, str);
40: else
41: ret = 3;
42: } else {
43: sec = cfg_getSection(&cfg, section);
44: if (sec) {
45: for (i = 0; i < array_Size(sec) &&
46: (av = array(sec, i, struct tagCfg*)); i++) {
47: if (AIT_GET_STR(&av->cfg_attr))
48: printf("%s=%s\n",
49: AIT_GET_STR(&av->cfg_attr),
50: AIT_GET_STR(&av->cfg_val));
51: }
52: array_Destroy(&sec);
53: } else
54: ret = 3;
55: }
56:
57: return ret;
1.1.2.1 misho 58: }
1.1.2.2 misho 59:
60: int
61: Set(const char *prog, int argc, char **argv)
62: {
63: if (argc < 2) {
64: printf("Error:: missing attribute and value arguments!\n");
65: return 1;
66: }
67:
68: EVERBS(1) printf("Set value %s for %s in scope %s from config %s\n",
69: argv[1], argv[0], section, prog);
70:
1.1.2.4 ! misho 71: if (!*argv[1]) {
! 72: if (cfg_unsetAttribute(&cfg, section, argv[0]) < 1)
! 73: return 3;
! 74: } else if (cfg_setAttribute(&cfg, section, argv[0], argv[1]) < 1)
! 75: return 3;
! 76:
! 77: if (*outname) {
! 78: out = fopen(outname, "w");
! 79: if (!out) {
! 80: printf("Error:: can't create file %s #%d - %s\n",
! 81: outname, errno, strerror(errno));
! 82: return 2;
! 83: }
! 84: }
! 85:
! 86: if (cfgWriteConfigRaw(out, &cfg, 42)) {
! 87: printf("Error:: can't write config #%d - %s\n",
! 88: cfg_GetErrno(), cfg_GetError());
! 89: if (*outname)
! 90: fclose(out);
! 91: return 2;
! 92: }
! 93:
! 94: if (*outname)
! 95: fclose(out);
1.1.2.2 misho 96: return 0;
97: }
98:
99:
100: int
101: main(int argc, char **argv)
102: {
1.1.2.3 misho 103: int ch, ret = 0;
1.1.2.2 misho 104: int (*run)(const char*, int, char**) = Get;
105: const char *prog;
106: char *str;
107:
1.1.2.4 ! misho 108: out = stdout;
! 109:
1.1.2.2 misho 110: if (!argv || !*argv)
111: prog = "cfgprog";
112: else if (!(prog = strrchr(*argv, '/')))
113: prog = *argv;
114: else
115: prog++;
116:
1.1.2.4 ! misho 117: while ((ch = getopt(argc, argv, "hvws:o:")) != -1)
1.1.2.2 misho 118: switch (ch) {
1.1.2.4 ! misho 119: case 'o':
! 120: strlcpy(outname, optarg, sizeof outname);
! 121: break;
1.1.2.2 misho 122: case 'w':
123: run = Set;
124: break;
125: case 's':
126: strlcpy(section, optarg, sizeof section);
127: break;
128: case 'v':
129: e_incVerbose;
130: break;
131: default:
132: Usage(prog);
133: return 1;
134: }
135: argc -= optind;
136: argv += optind;
137:
138: if (!strcmp(prog, "cfg_set"))
139: run = Set;
140: else if (!strcmp(prog, "cfg_get"))
141: run = Get;
142:
143: if (!argc) {
144: printf("Error:: missing config name!\n");
145: Usage(prog);
146: return 1;
147: } else {
148: strlcpy(cfgname, *argv, sizeof cfgname);
149: argc--;
150: argv++;
151: }
152:
153: if (argc && *argv && (str = strchr(*argv, '/'))) {
154: *str++ = 0;
155: strlcpy(section, *argv, sizeof section);
156: *argv = str;
157: }
158:
1.1.2.3 misho 159: if (cfgLoadConfig(cfgname, &cfg)) {
160: printf("Error:: load config %s #%d - %s\n",
161: cfgname, cfg_GetErrno(), cfg_GetError());
162: return 2;
163: }
164:
165: ret = run(prog, argc, argv);
166:
167: cfgUnloadConfig(&cfg);
168: return ret;
1.1.2.2 misho 169: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>