From 20750cb61686e297e7597f4c30fc48176158865a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D1=80=D0=BE=D0=BC=D0=B5=D0=BD=D0=BE=D0=BA=20=D0=A0?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 23 May 2024 21:01:19 +0200 Subject: [PATCH] Added examples to the cli The function has been renamed and has become more universal Added error checking (incorrect input) --- apps/cli/cli_common.c | 31 ++++++++++++++++++++----------- apps/cli/clixon_cli_api.h | 2 +- example/main/example_cli.cli | 10 ++++++---- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 0f4201e7..bb23dfe6 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -789,34 +789,42 @@ cli_set_mode(clixon_handle h, * The function returns the exit code of the Python script, or a value of (-1) on error. */ int -cli_start_python3(clixon_handle h, +cli_start_program(clixon_handle h, cvec *cvv, cvec *argv) { int pid = 0; int retval = -1; char *script_path = NULL; - char *runner = "python3"; + char *runner = NULL; struct passwd *pw = NULL; - if (cvec_len(argv) > 1){ + /* Check parameters */ + if (cvec_len(argv) == 0){ + clixon_err(OE_PLUGIN, EINVAL, "Can not found argument in a function"); + goto done; + } + if ((cvec_len(argv) >= 2) && (cvec_len(cvv) >= 2)){ clixon_err(OE_PLUGIN, EINVAL, "A lot of arguments"); goto done; } - - if (cvec_len(argv) == 1){ - script_path = cv_string_get(cvec_i(argv, 0)); + if ((cvec_len(argv) == 2) && (cvec_len(cvv) == 2)){ + clixon_err(OE_PLUGIN, EINVAL, "You cannot use 2 arguments in a function and 1 argument in a vector"); + goto done; } + /* get data */ + if (cvec_len(argv) == 1){ + runner = cv_string_get(cvec_i(argv, 0)); + } + if (cvec_len(argv) == 2){ + runner = cv_string_get(cvec_i(argv, 0)); + script_path = cv_string_get(cvec_i(argv, 1)); + } if (cvec_len(cvv) == 2){ script_path = cv_string_get(cvec_i(cvv, 1)); } - if ((cvec_len(cvv) == 2) && (cvec_len(argv) == 1)){ - clixon_err(OE_PLUGIN, EINVAL, "Both the arguments of the function and the vector of values are specified\n"); - goto done; - } - if ((pw = getpwuid(getuid())) == NULL){ clixon_err(OE_PLUGIN, errno, "getpwuid"); goto done; @@ -826,6 +834,7 @@ cli_start_python3(clixon_handle h, goto done; } + /* main run */ if ((pid = fork()) == 0) { execlp(runner, runner, script_path, NULL); clixon_err(OE_PLUGIN, errno, "Error run script"); diff --git a/apps/cli/clixon_cli_api.h b/apps/cli/clixon_cli_api.h index 27ff7a8d..4bfa9f62 100644 --- a/apps/cli/clixon_cli_api.h +++ b/apps/cli/clixon_cli_api.h @@ -96,7 +96,7 @@ int cli_debug_cli(clixon_handle h, cvec *vars, cvec *argv); int cli_debug_backend(clixon_handle h, cvec *vars, cvec *argv); int cli_debug_restconf(clixon_handle h, cvec *vars, cvec *argv); int cli_set_mode(clixon_handle h, cvec *vars, cvec *argv); -int cli_start_python3(clixon_handle h, cvec *vars, cvec *argv); +int cli_start_program(clixon_handle h, cvec *vars, cvec *argv); int cli_start_shell(clixon_handle h, cvec *vars, cvec *argv); int cli_quit(clixon_handle h, cvec *vars, cvec *argv); int cli_commit(clixon_handle h, cvec *vars, cvec *argv); diff --git a/example/main/example_cli.cli b/example/main/example_cli.cli index 8e069b04..44324e0f 100644 --- a/example/main/example_cli.cli +++ b/example/main/example_cli.cli @@ -72,10 +72,12 @@ shell("System command"), cli_start_shell("bash");{ ("Single shell command"), cli_start_shell("bash"); } -run_python3("Run python"), cli_start_python3(); -run_python3_script("Run python script"), cli_start_python3("/tmp/test.py"); -run_python3_script_manual("Run python script manual") ("Path to script"), cli_start_python3(); -run_python3_script_manual_err("Run python script") ("Path to script"), cli_start_python3("/tmp/test2.py"); +run_program_err("Run program"), cli_start_program(); +run_program_python3("Run program"), cli_start_program("python3"); +run_program_python3_source_arg("Run program"), cli_start_program("python3", "/tmp/test.py"); +run_program_python3_source_arg_vector("Run program") ("Path program"), cli_start_program("python3"); +run_program_python3_source_arg_vector_err("Run program") ("Path program"), cli_start_program("python3", "/tmp/test2.py"); +run_program_bash("Run program"), cli_start_program("bash"); copy("Copy and create a new object") { running("Copy from running db") startup("Copy to startup config"), db_copy("running", "startup");