--- libaitcfg/src/cfgprog.c 2025/01/30 14:34:25 1.1.2.3 +++ libaitcfg/src/cfgprog.c 2025/01/31 00:03:15 1.2 @@ -3,7 +3,8 @@ cfg_root_t cfg; -char cfgname[MAXPATHLEN], section[STRSIZ]; +char cfgname[MAXPATHLEN], outname[MAXPATHLEN], section[STRSIZ]; +FILE *out; static void @@ -14,6 +15,7 @@ Usage(const char *prog) "==========================================\n" " Syntax: %s [-options] [attribute] [value]\n\n" "\t-s
\tScope of attribute\n" + "\t-o \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", @@ -66,6 +68,31 @@ Set(const char *prog, int argc, char **argv) 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; } @@ -78,6 +105,8 @@ main(int argc, char **argv) const char *prog; char *str; + out = stdout; + if (!argv || !*argv) prog = "cfgprog"; else if (!(prog = strrchr(*argv, '/'))) @@ -85,8 +114,11 @@ main(int argc, char **argv) else prog++; - while ((ch = getopt(argc, argv, "hvws:")) != -1) + while ((ch = getopt(argc, argv, "hvws:o:")) != -1) switch (ch) { + case 'o': + strlcpy(outname, optarg, sizeof outname); + break; case 'w': run = Set; break;