From 16e15e16e2d05d126dc38d640355411cf5851a68 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 28 Nov 2026 02:41:38 -0600 Subject: [PATCH] Start of config -c (interactive text question/answer config) --- scripts/kconfig.c | 226 +++++++++++++++++++++++++++++++++------------- 1 file changed, 161 insertions(+), 65 deletions(-) diff --git a/scripts/kconfig.c b/scripts/kconfig.c index 28e1c2be..5cf68641 100644 --- a/scripts/kconfig.c +++ b/scripts/kconfig.c @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -9,6 +11,7 @@ mainmenu, source, comment config bool "string" int "string" + range 123 456 default y/n/SYMBOL depends [on] SYMBOL help @@ -24,12 +27,36 @@ endmenu/endchoice // Data we're collecting about each entry struct kconfig { struct kconfig *next, *contain; - char *symbol, *value, *type, *prompt, *def, *depend, *help; + char *symbol, *value, *type, *prompt, *def, *depend, *help, *min, *max; }; // 0 = allnoconfig, 1 = defconfig, 2 = allyesconfig, 4 = randconfig int cfgtype; +__attribute__((format(printf, 1, 2))) _Noreturn void error_exit(char *msg, ...) +{ + va_list va; + + va_start(va, msg); + fprintf(stderr, "kconf: "); + vfprintf(stderr, msg, va); + va_end(va); + + exit(1); +} + +char *xstrndup(char *s, unsigned len) +{ + if (!(s = strndup(s, len))) error_exit("alloc %u\n", len); + + return s; +} + +char *xstrdup(char *s) +{ + return xstrndup(s, strlen(s)); +} + // Skip/remove leading space, quotes, and escapes within quotes char *trim(char *s) { @@ -89,13 +116,12 @@ void bump(char **to, char **from) struct kconfig *read_Config(char *name, struct kconfig *contain) { FILE *fp = fopen(name, "r"); - char *line, *help = 0, *s, *ss, *keywords[] = {"mainmenu", "menu", - "choice", "comment", "config", "endmenu", "endchoice", 0}; - struct kconfig *kc = 0, *klist = 0; + char *line, *help = 0, *s, *ss; + struct kconfig *kc = 0, *klist = 0, *wascontain = contain; int ii, jj, count = 1, hindent; size_t size; - if (!fp) dprintf(2, "Can't open '%s'\n", name), exit(1); + if (!fp) error_exit("Can't open '%s'\n", name); for (; (s = line = getrimline(fp)); free(line), count++) { // Append help text? if (help) { @@ -130,49 +156,63 @@ struct kconfig *read_Config(char *name, struct kconfig *contain) if (!*s || *s=='#') continue; ss = s; while (*s && !isspace(*s)) s++; - ss = strndup(ss, s-ss); + ss = xstrndup(ss, s-ss); - // Include other file - if (!strcmp(ss, "source")) { - struct kconfig *kt; - - if (!(kt = read_Config(trim(s), contain))) { fp = 0; break; } - if (klist) kc = (kc->next = kt); - else klist = kc = kt; - while (kc->next) kc = kc->next; - // start help block - } else if (!strcmp(ss, "help")) help = strdup(""); // start a new config entry? - else if ((ii = strany(ss, keywords))) { + if ((ii = strany(ss, (char *[]){"mainmenu", "menu", "choice", "comment", + "config", "endmenu", "endchoice", 0}+!!contain))) + { struct kconfig *kt = calloc(sizeof(struct kconfig), 1); - if (ii>5) contain = contain->contain; + + if (--ii && !contain) { dprintf(2, "need mainmenu\n"); fp = 0; break; } + if (ii>=4) { + if (strcmp(ss+3+!contain->contain, contain->type)) { fp = 0; break; }; + contain = contain->contain; + } kt->contain = contain; - if (ii<4) contain = kt; + if (ii<=1) contain = kt; if (klist) kc = (kc->next = kt); else klist = kc = kt; - if (ii==5) { + if (ii==3) { if (!*s) { fp = 0; break; } - else kt->symbol = strdup(trim(s)); - } else if (*s) kt->prompt = strdup(trim(s)); + kt->symbol = xstrdup(trim(s)); + } else if (*s) kt->prompt = xstrdup(trim(s)); bump(&kt->type, &ss); } else if (!kc) { fp = 0; break; } - else if (!strcmp(ss, "default")) kc->def = strdup(trim(s)); + // Include other file + else if (!strcmp(ss, "source")) { + struct kconfig *kt; + + if (!(kt = read_Config(trim(s), contain))) { fp = 0; break; } + if (klist) kc = (kc->next = kt); + else klist = kc = kt; + while (kc->next) kc = kc->next; + // start help block + } else if (!strcmp(ss, "help")) help = xstrdup(""); + else if (!strcmp(ss, "range")) { + if (!kc->type || strcmp(kc->type, "int") || 2!=sscanf(s, "%*s %*s")) { + fp = 0; + break; + } + } else if (!strcmp(ss, "default")) kc->def = xstrdup(trim(s)); else if (strany(ss, (char *[]){"bool", "int", "string", "prompt", 0})) { if (*ss!='p') bump(&kc->type, &ss); - ss = strdup(trim(s)); + ss = xstrdup(trim(s)); bump(&kc->prompt, &ss); } else if (!strcmp(ss, "depends")) { while (isspace(*s)) s++; if (s[0]=='o' && s[1]=='n' && isspace(s[2])) s += 3; - ss = strdup(trim(s)); + ss = xstrdup(trim(s)); bump(&kc->depend, &ss); - } else { dprintf(2, "%d %s\n", count, line); fp = 0; break; } + } else { fp = 0; break; } free(ss); } - if (!fp) dprintf(2, "%s[%d]: bad %s\n", name, count, line ? : ""), exit(1); + if (!fp) error_exit("%s[%d]: bad %s\n", name, count, line ? : ""); fclose(fp); + if (wascontain ? contain!=wascontain : !!contain->contain) + error_exit("%s unbalanced", name); return klist; } @@ -215,17 +255,16 @@ int depends(struct kconfig *klist, struct kconfig *kc) if (kc->depend) for (ss = kc->depend; *ss;) { for (tt = ss; *tt && !isspace(*tt);) tt++; if (tt==ss) break; - ss = sym = strndup(ss, tt-ss); + ss = sym = xstrndup(ss, tt-ss); sym += flip = *sym=='!'; while (isspace(*tt)) tt++; if (!strcmp("&&", sym)) { if (!rc) return 0; } else if (!strcmp("||", sym)) { if (rc) return 1; - } else if (!(kt = lookup(klist, sym))) { - dprintf(2, "unknown dependency %s in %s\n", sym, kc->symbol); - exit(1); - } else { + } else if (!(kt = lookup(klist, sym))) + error_exit("unknown dependency %s in %s\n", sym, kc->symbol); + else { rc = depends(klist, kt) ? value(kt) : 0; if (flip) rc = !rc; } @@ -246,9 +285,9 @@ void set_val(struct kconfig *kk, char *val) k2->value = 0; } free(kk->contain->def); - kk->contain->def = strdup(kk->symbol); + kk->contain->def = xstrdup(kk->symbol); } - val = strdup(val); + val = xstrdup(val); bump(&kk->value, &val); } @@ -272,24 +311,33 @@ void read_dotconfig(struct kconfig *klist, FILE *fp) val = s+1; } else { for (s = line; isspace(*s); s++); - if (*s && *s!='#') dprintf(2, "bad line %s\n", line); + if (*s && *s!='#') error_exit("bad line %s\n", line); continue; } - if (!(kk = lookup(klist, name))) dprintf(2, "bad symbol %s\n", name); + if (!(kk = lookup(klist, name))) error_exit("bad symbol %s\n", name); else set_val(kk, val); } fclose(fp); } -// Parse command line options, producing various types of output -void options(char *opt) +// Read Config.in (and maybe other files), set symbols, write .config +int main(int argc, char *argv[]) { - struct kconfig *kc = read_Config("Config.in", 0), *kk; - char *ss, *tt, *esc = "\n\\\""; + struct kconfig *kc = read_Config("Config.in", 0), *kk, *kt; + char *ss = argv[1], *rr = "ndyrhmc", *tt, *esc = "\n\\\"", buf[64]; + time_t t = time(0); + unsigned type; FILE *fp; + int ii, min, max; + + if (argc!=2 || *ss!='-' || (!ss[1] || !(tt = strchr(rr, ss[1]),ss[1]))||ss[2]) + error_exit("Need -[%s]\n", rr); + cfgtype = tt-rr; + if (cfgtype>3) cfgtype = 1; // interactive config falls back to defconfig - if (!strcmp(opt, "-h")) { + // Emit help.h instead of .config + if (*tt=='h') { for (kk = kc; kk; kk = kk->next) { if (!kk->symbol || !kk->help) continue; printf("#define HELP_"); @@ -301,28 +349,52 @@ void options(char *opt) printf("\"\n\n"); } - return; + return 0; } if ((ss = getenv("KCONFIG_ALLCONFIG")) && *ss) { - if (!(fp = fopen(ss, "r"))) - exit(dprintf(2, "bad KCONFIG_ALLCONFIG=%s\n", ss)|1); + if (!(fp = fopen(ss, "r"))) error_exit("bad KCONFIG_ALLCONFIG=%s\n", ss); read_dotconfig(kc, fp); } -// TODO if ((fp = fopen(getenv("KCONFIG_CONFIG") ? : ".config"))) -// read_dotconfig(kc, fp); - - if (-1 != (cfgtype = strany(opt, (char *[]){"-n", "-d", "-y", "-r", 0})-1)) { - time_t t = time(0); - struct tm *tt = localtime(&t); - char buf[64]; - strftime(buf, sizeof(buf), "%c", tt); - printf("# %s\n\n", buf); + // menuconfig + if (*tt=='m') { + if ((fp = fopen(getenv("KCONFIG_CONFIG") ? : ".config", "r"))) + read_dotconfig(kc, fp); + + // TODO oldconfig, indent, hidden or dependency-forced symbols + // old style interactive config + } else if (*tt=='c') for (kk = kc; kk; kk = kk->next) { + if (!kk->prompt || !*kk->prompt) continue; + if ((ii = strany(kk->type, (char *[]){"comment", "menu", 0}))) { + dprintf(2, (char *[]){"%s\n", "*\n%s\n*\n", 0}[ii-1], kk->prompt); + continue; + } + if (!strcmp(kk->type, "choice")) { + dprintf(2, "%s\n", kk->prompt ? : "ERROR"); + if (!kk->def && !(kk->def = kk->next->symbol)) error_exit("tilt"); + for (ii = 0, kt = kk; (kt = kt->next) && !strcmp(kt->type, "bool");) + dprintf(2, "%c %d. %s\n", " >"[!strcmp(kk->def, kt->symbol)], ++ii, + kt->prompt); + min = 1; + dprintf(2, "Choice [1-%d?]: ", max = ii); + } else { + if (!strcmp(kk->type, "bool")) + sprintf(tt = buf, "%s/?", (kk->def && *kk->def=='y') ? "Y/n" : "N/y"); + else if (!strcmp(kk->type, "int")) { + if (kk->min) + sprintf(buf, "%d-%d:", min = atoi(kk->min), max = atoi(kk->max)); + else min = 0, max = INT_MAX, *buf = 0; + ii = min; + if (kk->def) ii = atoi(kk->def); + sprintf((tt = buf)+strlen(buf), "%d", ii); + } else if (!strcmp(kk->type, "string")) tt = kk->def ? : ""; + else continue; + dprintf(2, "%s (%s) [%s] ", kk->prompt, kk->symbol, tt); + } + dprintf(2, "\n"); - for (kk = kc; kk; kk = kk->next) { - if (!strcmp(kk->type, "menu") || !strcmp(kk->type, "comment")) - printf("\n#\n# %s\n#\n", kk->prompt ? : ""); +/* if (!(ss = kk->symbol)) continue; if (*kk->type=='b') { printf((depends(kc, kk) && value(kk)) @@ -331,16 +403,40 @@ void options(char *opt) printf("CONFIG_%s=\"%s\"\n", ss, kk->value ? : kk->def ? : ""); else printf("CONFIG_%s=%d\n", ss, value(kk)); } - } -} - -// Read .config file and set each symbol -// void getconfig(struct kconfig *klist, char *) { } + if (askcfg && !kk->value) { + val = value(kk); + sdef = kk->value ? : kk->def ? : ""; + sprintf(buf, "%c/%c/?", "Yy"[!val], "nN"[!val]); + if (ty=='i') sprintf(buf, "%d", val); + printf("%s (%s) [%s] ", kk->prompt, kk->symbol, ty=='s' ? sdef : buf); + +indentation for dependent symbols +blah (BLAH) [500] +Security Blanket +> 1. None (TOYBOX_LSM_NONE) + 2. SELinux support (TOYBOX_SELINUX) + 3. SMACK support (TOYBOX_SMACK) +choice[1-3?]: x +string (STRING) [string] +*/ + } -int main(int argc, char *argv[]) -{ - if (argc==2) options(argv[1]); - return 0; + // Write .config contents to stdout + t = time(0); + strftime(buf, sizeof(buf), "%c", localtime(&t)); + printf("# %s\n\n", buf); + + for (kk = kc; kk; kk = kk->next) { + if (!strcmp(kk->type, "menu") || !strcmp(kk->type, "comment")) + printf("\n#\n# %s\n#\n", kk->prompt ? : ""); + if (!(ss = kk->symbol)) continue; + if (*kk->type=='b') { + printf((depends(kc, kk) && value(kk)) + ? "CONFIG_%s=y\n" : "# CONFIG_%s is not set\n", ss); + } else if (*kk->type=='s') + printf("CONFIG_%s=\"%s\"\n", ss, kk->value ? : kk->def ? : ""); + else printf("CONFIG_%s=%d\n", ss, value(kk)); + } } -- 2.39.5