From 132885b4bb1baa18e0faed4d2c22ba1510c2be8a Mon Sep 17 00:00:00 2001 From: Ralf Schandl Date: Sun, 17 Sep 2023 20:44:26 +0200 Subject: [PATCH] Change generated function check code Just use a simple if-then. --- doc/tutorial.adoc | 10 +++++++--- src/shell_code.rs | 10 +++++----- tests/code_gen.rs | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/doc/tutorial.adoc b/doc/tutorial.adoc index 0233500..65d43b6 100644 --- a/doc/tutorial.adoc +++ b/doc/tutorial.adoc @@ -433,15 +433,19 @@ To better understand how this works, see the sections <> and </dev/null 2>&1 || { echo >&2 "ERROR: Function set_out_file does not exist.";exit 127; }; +# default +if ! LC_ALL=C command -V set_out_file 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 "ERROR: Function 'set_out_file' does not exist."; exit 127; fi; + +# with --shell bash, ksh or zsh +if ! typeset -f set_out_file >/dev/null 2>&1; then echo >&2 "ERROR: Function 'set_out_file' does not exist."; exit 127; fi; ---- This code will exit the calling script if the function does not exist. -This check is always done, whether the function is needed in the actually generated code or not. +The check is always done, whether the function is needed in the actually generated code or not. When calling the callback the exit status of the function must be zero else the calling script is terminated with function exit code. The code for this looks like this: diff --git a/src/shell_code.rs b/src/shell_code.rs index 94adcbe..099c28c 100644 --- a/src/shell_code.rs +++ b/src/shell_code.rs @@ -221,7 +221,7 @@ const SH_TEMPLATE : CodeTemplates = CodeTemplates { assign_empty_array : "", add_to_array : "", - check_function_exists : "LC_ALL=C type {NAME} 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function '{NAME}' does not exist.\";exit 1) || exit 127" , + check_function_exists : "if ! LC_ALL=C command -V {NAME} 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function '{NAME}' does not exist.\"; exit 127; fi" , call_function : "{NAME} {VALUE} || exit $?", set_args : "set -- {ARGS}", @@ -241,7 +241,7 @@ const BASH_TEMPLATE : CodeTemplates = CodeTemplates { assign_empty_array : "{NAME}=()", add_to_array : "{NAME}+=({VALUE})", - check_function_exists: "typeset -f {NAME} >/dev/null 2>&1 || { echo >&2 \"ERROR: Function '{NAME}' does not exist.\";exit 127; }", + check_function_exists : "if ! typeset -f {NAME} >/dev/null 2>&1; then echo >&2 \"ERROR: Function '{NAME}' does not exist.\"; exit 127; fi" , // others from sh template ..SH_TEMPLATE @@ -401,7 +401,7 @@ mod shell_template_test { assert_eq!("func 'value' || exit $?", shell.format(&chunk)); let chunk = CodeChunk::CheckForFunction(var_name.clone()); - assert_eq!("LC_ALL=C type func 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function 'func' does not exist.\";exit 1) || exit 127", shell.format(&chunk)); + assert_eq!("if ! LC_ALL=C command -V func 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk)); let chunk = CodeChunk::SetArgs(vec![ "one".to_string(), @@ -464,7 +464,7 @@ mod shell_template_test { assert_eq!("func 'value' || exit $?", shell.format(&chunk)); let chunk = CodeChunk::CheckForFunction(var_name); - assert_eq!("typeset -f func >/dev/null 2>&1 || { echo >&2 \"ERROR: Function 'func' does not exist.\";exit 127; }", shell.format(&chunk)); + assert_eq!("if ! typeset -f func >/dev/null 2>&1; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk)); let chunk = CodeChunk::SetArgs(vec![ "one".to_string(), @@ -515,7 +515,7 @@ mod shell_template_test { assert_eq!("func 'value' || exit $?", shell.format(&chunk)); let chunk = CodeChunk::CheckForFunction(var_name); - assert_eq!("typeset -f func >/dev/null 2>&1 || { echo >&2 \"ERROR: Function 'func' does not exist.\";exit 127; }", shell.format(&chunk)); + assert_eq!("if ! typeset -f func >/dev/null 2>&1; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk)); let chunk = CodeChunk::SetArgs(vec![ "one".to_string(), diff --git a/tests/code_gen.rs b/tests/code_gen.rs index dd3f346..98fe5a6 100644 --- a/tests/code_gen.rs +++ b/tests/code_gen.rs @@ -2,7 +2,7 @@ mod exec; /// Create function check code for given function name for standard shell fn sh_func_check(func_name: &str) -> String { - format!("LC_ALL=C type {func_name} 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function '{func_name}' does not exist.\";exit 1) || exit 127;") + format!("if ! LC_ALL=C command -V {func_name} 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function '{func_name}' does not exist.\"; exit 127; fi;") } #[test]