#include "global.h"
#include "aitcfg.h"
cfg_root_t cfg;
char cfgname[MAXPATHLEN], outname[MAXPATHLEN], section[STRSIZ];
FILE *out;
static void
Usage(const char *prog)
{
printf( " -= CFGPROG =- ELWIX config tool ver.%s\n"
"(C)`25 by Michael Pounov <misho@elwix.org>\n"
"==========================================\n"
" Syntax: %s [-options] <config> [attribute] [value]\n\n"
"\t-s <section>\tScope of attribute\n"
"\t-o <file>\tWrite updated config to file\n"
"\t-w\t\tSet and write attribute\n"
"\t-v\t\tVerbose, more -v more verbosity\n"
"\t-h\t\tHelp, this help screen!\n",
PACKAGE_VERSION, prog);
}
int
Get(const char *prog, int argc, char **argv)
{
const char *str;
int i, ret = 0;
array_t *sec;
struct tagCfg *av;
EVERBS(1) printf("Get value for %s in scope %s from config %s\n",
*argv ? *argv : "*", section, prog);
if (*argv) {
str = cfg_getAttribute(&cfg, section, *argv);
if (str)
printf("%s=%s\n", *argv, str);
else
ret = 3;
} else {
sec = cfg_getSection(&cfg, section);
if (sec) {
for (i = 0; i < array_Size(sec) &&
(av = array(sec, i, struct tagCfg*)); i++) {
if (AIT_GET_STR(&av->cfg_attr))
printf("%s=%s\n",
AIT_GET_STR(&av->cfg_attr),
AIT_GET_STR(&av->cfg_val));
}
array_Destroy(&sec);
} else
ret = 3;
}
return ret;
}
int
Set(const char *prog, int argc, char **argv)
{
if (argc < 2) {
printf("Error:: missing attribute and value arguments!\n");
return 1;
}
EVERBS(1) printf("Set value %s for %s in scope %s from config %s\n",
argv[1], argv[0], section, prog);
if (!*argv[1]) {
if (cfg_unsetAttribute(&cfg, section, argv[0]) < 1)
return 3;
} else if (cfg_setAttribute(&cfg, section, argv[0], argv[1]) < 1)
return 3;
if (*outname) {
out = fopen(outname, "w");
if (!out) {
printf("Error:: can't create file %s #%d - %s\n",
outname, errno, strerror(errno));
return 2;
}
}
if (cfgWriteConfigRaw(out, &cfg, 42)) {
printf("Error:: can't write config #%d - %s\n",
cfg_GetErrno(), cfg_GetError());
if (*outname)
fclose(out);
return 2;
}
if (*outname)
fclose(out);
return 0;
}
int
main(int argc, char **argv)
{
int ch, ret = 0;
int (*run)(const char*, int, char**) = Get;
const char *prog;
char *str;
out = stdout;
if (!argv || !*argv)
prog = "cfgprog";
else if (!(prog = strrchr(*argv, '/')))
prog = *argv;
else
prog++;
while ((ch = getopt(argc, argv, "hvws:o:")) != -1)
switch (ch) {
case 'o':
strlcpy(outname, optarg, sizeof outname);
break;
case 'w':
run = Set;
break;
case 's':
strlcpy(section, optarg, sizeof section);
break;
case 'v':
e_incVerbose;
break;
default:
Usage(prog);
return 1;
}
argc -= optind;
argv += optind;
if (!strcmp(prog, "cfg_set"))
run = Set;
else if (!strcmp(prog, "cfg_get"))
run = Get;
if (!argc) {
printf("Error:: missing config name!\n");
Usage(prog);
return 1;
} else {
strlcpy(cfgname, *argv, sizeof cfgname);
argc--;
argv++;
}
if (argc && *argv && (str = strchr(*argv, '/'))) {
*str++ = 0;
strlcpy(section, *argv, sizeof section);
*argv = str;
}
if (cfgLoadConfig(cfgname, &cfg)) {
printf("Error:: load config %s #%d - %s\n",
cfgname, cfg_GetErrno(), cfg_GetError());
return 2;
}
ret = run(prog, argc, argv);
cfgUnloadConfig(&cfg);
return ret;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>