feat(gen): generate --help and argument parsing

Replace the v1 minimal preamble in the generated ./configure with the
full argument-parsing section (src/gen/args.{h,c}): -h/--help usage,
--version, --prefix/--exec-prefix/--host/--build in = and space forms,
per-DSL-option --enable-<name>/--disable-<name> (default from the
option's default property), generic positional VAR=VALUE overrides
(identifier-validated, single-quote-escaped eval assignment), unknown
--* -> usage on stderr + exit 1, cross_compiling=yes when --host differs
from --build. The substitution set gains exec_prefix/host/build/
cross_compiling plus one enable_<name> per option. Option names are
validated as shell identifiers at generation time.

Tests: 12 new /gen/args/* cases (help lists --prefix/--enable-debug/
--disable-debug; CC=clang positional + env; enable/debug default;
cross_compiling observable; --bogus/--bogus=1/--enable- errors;
--prefix missing/empty; space form; exec_prefix defaulting; VAR=VALUE
injection inert; hostile option name errors at generation) - 28/28
green, three-shell -n + banned-construct sweep clean.
This commit is contained in:
2026-08-29 00:35:07 -04:00
parent 08e1ea93dd
commit 9f41034197
5 changed files with 1333 additions and 61 deletions
+461 -1
View File
@@ -1,4 +1,4 @@
/* LINK: ../../src/gen/config.c ../../src/gen/configure.c ../../src/gen/sh_emit.c ../../src/detect/probe.c ../../src/detect/checks.c ../../src/detect/check_registry.c ../../src/detect/resolve.c ../../src/kdl/schema.c ../../src/kdl/parser.c ../../src/kdl/lexer.c ../../src/kdl/value.c ../../src/ext/abi.c ../../src/ext/lang_c.c ../../src/ext/lang_cpp.c ../../src/error.c ../../src/span.c */
/* LINK: ../../src/gen/args.c ../../src/gen/config.c ../../src/gen/configure.c ../../src/gen/sh_emit.c ../../src/detect/probe.c ../../src/detect/checks.c ../../src/detect/check_registry.c ../../src/detect/resolve.c ../../src/kdl/schema.c ../../src/kdl/parser.c ../../src/kdl/lexer.c ../../src/kdl/value.c ../../src/ext/abi.c ../../src/ext/lang_c.c ../../src/ext/lang_cpp.c ../../src/error.c ../../src/span.c */
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L /* mkdtemp/unsetenv/setenv (POSIX.1-2008) */
#endif
@@ -181,6 +181,31 @@ run_configure(const char *args)
return WEXITSTATUS(spawned);
}
/* Run "cd temp_dir && <env>sh ./configure <args>" with stdout captured to
* run.out and stderr to run.err. `env` is a prefix like "CC=clang " (the
* exported VAR=VALUE form) or NULL/"" for none. Returns the exit status,
* or -1 if configure could not be spawned. */
static int
run_configure_full(const char *env, const char *args)
{
char cmd[2400];
int n;
int spawned;
n = snprintf(cmd, sizeof cmd,
"cd '%s' && %ssh ./configure %s >'%s/run.out' 2>'%s/run.err'",
temp_dir, env != NULL ? env : "", args != NULL ? args : "",
temp_dir, temp_dir);
if (n < 0 || (size_t)n >= sizeof cmd) {
return -1;
}
spawned = system(cmd);
if (spawned == -1) {
return -1;
}
return WEXITSTATUS(spawned);
}
/* "<shell> -n <path>": the shell's exit status or -1. */
static int
syntax_check(const char *shell, const char *path)
@@ -235,6 +260,14 @@ slurp_named(const char *name)
return slurp(path);
}
/* Slurp temp_dir/run.out (stdout of the last run_configure_full), or
* NULL. Caller frees. */
static char *
stdout_text(void)
{
return slurp_named("run.out");
}
/* Remove temp_dir/<name> (a missing file is not an error: the stale_state
* checks remove outputs that may or may not exist). Returns 0 on success. */
static int
@@ -968,6 +1001,411 @@ test_config_status_help(const MunitParameter params[], void *data)
return MUNIT_OK;
}
/* ---- todo 18 (a): the full preamble stays shell-safe + carries the
* parser machinery ------------------------------------------------------ */
static MunitResult
test_args_syntax(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char conf[600];
char *bytes;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(mkpath(conf, sizeof conf, "configure"), >, 0);
munit_assert_int(syntax_check("sh", conf), ==, 0);
munit_assert_int(syntax_check("bash", conf), ==, 0);
munit_assert_int(syntax_check("zsh", conf), ==, 0);
bytes = slurp(conf);
munit_assert_not_null(bytes);
munit_assert_not_null(strstr(bytes, "# --- PREAMBLE ---"));
munit_assert_not_null(strstr(bytes, "st_usage()"));
/* the fixture's option "debug" -> enable/disable arms + default */
munit_assert_not_null(strstr(bytes, "--enable-debug) enable_debug=yes"));
munit_assert_not_null(strstr(bytes, "--disable-debug) enable_debug=no"));
munit_assert_not_null(strstr(bytes, "enable_debug=no"));
/* VAR=VALUE parser machinery (identifier check + quoted eval) */
munit_assert_not_null(strstr(bytes, "st_val_q="));
munit_assert_not_null(strstr(bytes, "st_var=${st_arg%%=*}"));
free(bytes);
return MUNIT_OK;
}
/* ---- todo 18 (b): --help lists the standard + per-option flags -------- */
static MunitResult
test_args_help(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *out;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
/* help exits 0 BEFORE probing/substitution (no Makefile.in needed) */
munit_assert_int(run_configure_full(NULL, "--help"), ==, 0);
out = stdout_text();
munit_assert_not_null(out);
munit_assert_not_null(strstr(out, "Usage:"));
munit_assert_not_null(strstr(out, "--prefix"));
munit_assert_not_null(strstr(out, "--exec-prefix"));
munit_assert_not_null(strstr(out, "--host"));
munit_assert_not_null(strstr(out, "--build"));
munit_assert_not_null(strstr(out, "VAR=VALUE"));
munit_assert_not_null(strstr(out, "--enable-debug"));
munit_assert_not_null(strstr(out, "--disable-debug"));
munit_assert_not_null(strstr(out, "--version"));
free(out);
return MUNIT_OK;
}
/* ---- todo 18 (g): --version prints name + version, exits 0 ------------ */
static MunitResult
test_args_version(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *out;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(run_configure_full(NULL, "--version"), ==, 0);
out = stdout_text();
munit_assert_not_null(out);
munit_assert_not_null(strstr(out, "stupidtools"));
munit_assert_not_null(strstr(out, "1.0.0"));
free(out);
return MUNIT_OK;
}
/* ---- todo 18 (c): CC=clang as positional AND as env override ---------- */
static MunitResult
test_args_cc_override(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *mk;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
/* positional form: ./configure CC=clang */
munit_assert_int(write_makefile_in("CC = @CC@\n"), ==, 0);
munit_assert_int(run_configure_full(NULL, "CC=clang"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "CC = clang"));
free(mk);
/* exported form: CC=clang ./configure */
munit_assert_int(run_configure_full("CC=clang ", NULL), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "CC = clang"));
free(mk);
return MUNIT_OK;
}
/* ---- todo 18 (d): --enable-debug / --disable-debug / default ---------- */
static MunitResult
test_args_enable_debug(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *mk;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(write_makefile_in("DEBUG = @enable_debug@\n"), ==, 0);
munit_assert_int(run_configure_full(NULL, "--enable-debug"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "DEBUG = yes"));
free(mk);
/* the fixture option has default=#false, so no flag -> no */
munit_assert_int(run_configure_full(NULL, NULL), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "DEBUG = no"));
free(mk);
munit_assert_int(run_configure_full(NULL, "--disable-debug"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "DEBUG = no"));
free(mk);
return MUNIT_OK;
}
/* ---- todo 18 (e): --host != --build -> cross_compiling=yes ------------ */
static MunitResult
test_args_cross_compiling(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *mk;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(write_makefile_in("CROSS = @cross_compiling@\n"
"HOST = @host@\n"
"BUILD = @build@\n"), ==, 0);
munit_assert_int(run_configure_full(NULL,
"--host=aarch64-linux-gnu "
"--build=x86_64-linux-gnu"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "CROSS = yes"));
munit_assert_not_null(strstr(mk, "HOST = aarch64-linux-gnu"));
munit_assert_not_null(strstr(mk, "BUILD = x86_64-linux-gnu"));
free(mk);
munit_assert_int(run_configure_full(NULL, NULL), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "CROSS = no"));
free(mk);
return MUNIT_OK;
}
/* ---- todo 18 (f): unknown --* -> exit 1 + "unrecognized option" ------- */
static MunitResult
test_args_bogus(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *err;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(run_configure_full(NULL, "--bogus"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "unrecognized option"));
munit_assert_not_null(strstr(err, "Usage:")); /* usage goes to stderr */
free(err);
munit_assert_int(run_configure_full(NULL, "--bogus=1"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "unrecognized option"));
free(err);
/* --enable- with no name falls through to the unknown-option error */
munit_assert_int(run_configure_full(NULL, "--enable-"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "unrecognized option"));
free(err);
return MUNIT_OK;
}
/* ---- malformed values: missing/empty/option-looking --prefix ---------- */
static MunitResult
test_args_malformed(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *err;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
/* missing value (space form, end of argv) */
munit_assert_int(run_configure_full(NULL, "--prefix"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "requires an argument"));
free(err);
/* empty value */
munit_assert_int(run_configure_full(NULL, "--prefix="), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "non-empty argument"));
free(err);
/* an option-looking value is rejected */
munit_assert_int(run_configure_full(NULL, "--prefix --bogus"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "requires an argument"));
free(err);
return MUNIT_OK;
}
/* ---- --prefix <dir> (space form) and --exec-prefix -------------------- */
static MunitResult
test_args_prefix_space(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char *mk;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(write_makefile_in("prefix = @prefix@\n"
"EP = @exec_prefix@\n"), ==, 0);
munit_assert_int(run_configure_full(NULL, "--prefix /opt/space"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "prefix = /opt/space"));
/* exec_prefix defaults to prefix when not given */
munit_assert_not_null(strstr(mk, "EP = /opt/space"));
free(mk);
munit_assert_int(run_configure_full(NULL, "--prefix '/opt/x y'"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "prefix = /opt/x y"));
free(mk);
munit_assert_int(run_configure_full(NULL, "--exec-prefix=/opt/ep"), ==, 0);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "EP = /opt/ep"));
munit_assert_not_null(strstr(mk, "prefix = /usr/local"));
free(mk);
return MUNIT_OK;
}
/* ---- positional VAR=VALUE injection stays inert ----------------------- */
static MunitResult
test_args_injection(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
struct st_ext_ctx *ctx;
char pwned3[600];
char pwned4[600];
char *mk;
char *err;
ctx = build_ctx();
munit_assert_not_null(ctx);
munit_assert_int(emit_fixture_configure(ctx), ==, 0);
st_ext_ctx_free(ctx);
munit_assert_int(mkpath(pwned3, sizeof pwned3, "pwned3"), >, 0);
munit_assert_int(mkpath(pwned4, sizeof pwned4, "pwned4"), >, 0);
munit_assert_int(write_makefile_in("FLAGS = @CFLAGS@\n"), ==, 0);
munit_assert_int(run_configure_full(NULL, "'CFLAGS=$(touch pwned3)'"),
==, 0);
munit_assert_int(access(pwned3, F_OK), ==, -1);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "FLAGS = $(touch pwned3)"));
free(mk);
munit_assert_int(run_configure_full(NULL, "'CFLAGS=`touch pwned4`'"),
==, 0);
munit_assert_int(access(pwned4, F_OK), ==, -1);
mk = makefile_text();
munit_assert_not_null(mk);
munit_assert_not_null(strstr(mk, "FLAGS = `touch pwned4`"));
free(mk);
/* a non-identifier VAR name is a clean error */
munit_assert_int(run_configure_full(NULL, "B-AD=1"), !=, 0);
err = stderr_text();
munit_assert_not_null(err);
munit_assert_not_null(strstr(err, "invalid variable assignment"));
free(err);
return MUNIT_OK;
}
/* ---- a hostile option NAME errors at GENERATION time ------------------ */
static MunitResult
test_args_option_name(const MunitParameter params[], void *data)
{
(void)params;
(void)data;
static const char *const docsrc =
"project \"x\" version \"1.0\"\n"
"option \"x; rm -rf /\" default=#false\n"
"feature \"f\" { header \"unistd.h\" }\n";
struct st_ext_ctx *ctx;
struct st_kdl_document *doc = NULL;
struct st_error *err = NULL;
char conf[600];
ctx = build_ctx();
munit_assert_not_null(ctx);
doc = st_kdl_parse(docsrc, "inline.kdl", &err);
munit_assert_null(err);
munit_assert_not_null(doc);
munit_assert_null(st_kdl_validate(doc)); /* schema-valid, hostile name */
munit_assert_int(mkpath(conf, sizeof conf, "configure"), >, 0);
err = st_gen_configure_emit_path(conf, doc, ctx);
munit_assert_not_null(err);
munit_assert_not_null(strstr(st_error_message(err),
"not a valid shell identifier"));
st_error_free(err);
err = NULL;
/* nothing was emitted: the truncated output is an empty file */
{
char *bytes = slurp(conf);
munit_assert_not_null(bytes);
munit_assert_string_equal(bytes, "");
free(bytes);
}
st_kdl_document_free(doc);
st_ext_ctx_free(ctx);
return MUNIT_OK;
}
static MunitTest tests[] = {
{ "/gen/syntax", test_syntax, setup, teardown, MUNIT_TEST_OPTION_NONE,
NULL },
@@ -1003,6 +1441,28 @@ static MunitTest tests[] = {
teardown, MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/config-status-help", test_config_status_help, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/syntax", test_args_syntax, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/help", test_args_help, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/version", test_args_version, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/cc-override", test_args_cc_override, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/enable-debug", test_args_enable_debug, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/cross-compiling", test_args_cross_compiling, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/bogus", test_args_bogus, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/malformed", test_args_malformed, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/prefix-space", test_args_prefix_space, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/injection", test_args_injection, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ "/gen/args/option-name", test_args_option_name, setup, teardown,
MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
};