From e5fbdad5bebb30ef487916f55941e202a416b0a2 Mon Sep 17 00:00:00 2001 From: liquidaty Date: Tue, 21 Mar 2023 18:26:46 -0700 Subject: [PATCH] fix crash when using `zsv help` syntax for commands using CLI_BUILTIN_COMMAND() (#119) * fix crash when using `zsv help` syntax for commands using CLI_BUILTIN_COMMAND() * add a simple test --- app/Makefile | 9 +++++++-- app/cli.c | 7 ++++++- app/test/Makefile | 6 ++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/Makefile b/app/Makefile index c511a8d7..70971232 100644 --- a/app/Makefile +++ b/app/Makefile @@ -415,8 +415,13 @@ ${YAJL_HELPER_OBJ}: external/yajl_helper/yajl_helper.c @mkdir -p `dirname "$@"` ${CC} ${CFLAGS} -I${BASEDIR}/yajl_helper ${YAJL_INCLUDE} ${YAJL_HELPER_INCLUDE} -c $< -o $@ -test: - @${MAKE} -C test $@ QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG} +test: test-standalone test-cli + +test-standalone: + @${MAKE} -C test test QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG} + +test-cli: ${CLI} + @${MAKE} -C test $@ QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG} CLI=${CLI} clean-all: clean clean-external clean-obj clean-lib diff --git a/app/cli.c b/app/cli.c index 762e71b3..3d9fa5c7 100644 --- a/app/cli.c +++ b/app/cli.c @@ -476,7 +476,12 @@ int ZSV_CLI_MAIN(int argc, const char *argv[]) { argv[2], "--help" }; - return help_builtin->main(2, argv_tmp); + if(help_builtin->main) + return help_builtin->main(2, argv_tmp); + else if(help_builtin->cmd) + return help_builtin->cmd(2, argv_tmp, NULL, NULL); + else + return fprintf(stderr, "Unexpected syntax!\n"); } else { const char *ext_cmd = extension_cmd_from_arg(argv[2]); if(ext_cmd) { diff --git a/app/test/Makefile b/app/test/Makefile index 2f82bdbd..434afc22 100644 --- a/app/test/Makefile +++ b/app/test/Makefile @@ -126,6 +126,12 @@ worldcitiespop_mil.csv: test-count test-count-pull: test-% : test-1-% test-2-% +test-cli: ${CLI} + @${TEST_INIT} + @[ "${CLI}" = "" ] && echo 1>&2 'test-cli: missing CLI env var' && exit 1 || exit 0 + @$< help select 2>&1 > ${TMP_DIR}/$@.out + @[ "`head -1 ${TMP_DIR}/$@.out`" = "select: streaming CSV parser" ] && [ $$(( `cat ${TMP_DIR}/$@.out | wc -l` )) = "35" ] && ${TEST_PASS} || ${TEST_FAIL} + test-1-count test-1-count-pull: test-1-% : ${BUILD_DIR}/bin/zsv_%${EXE} worldcitiespop_mil.csv @${TEST_INIT} @cat worldcitiespop_mil.csv | ${PREFIX} $< ${REDIRECT} ${TMP_DIR}/$@.out