src/mech: add some basic 'show' commands

- Example of optional args (show log [FILE])
 - Example of dynamic expansion
 - Example of how to re-enable signals for external commands
 - Example of hidden commands
 - Add basic 'help' command for the current level

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-03-14 09:06:22 +01:00
parent 9041c54dbd
commit 2f7dbe110a
2 changed files with 98 additions and 3 deletions
+79 -2
View File
@@ -1,3 +1,4 @@
#include <glob.h>
#include <stdio.h>
#include <cligen/cligen.h>
@@ -5,9 +6,85 @@
#include <clixon/clixon_cli.h>
#include <clixon/cli_generate.h>
int cli_sysinfo(clicon_handle h, cvec *cvv, cvec *argv)
#define _PATH_LOG "/var/log/"
/* Not exported in the common Clixon API, needed to run external commands */
void cli_signal_block(clicon_handle h);
void cli_signal_unblock(clicon_handle h);
static int exec(clicon_handle h, char *cmd)
{
printf("System Information: Infix\n");
int rc;
cli_signal_unblock(h);
rc = system(cmd);
cli_signal_block(h);
return rc;
}
int cli_exec_shell(clicon_handle h, cvec *cvv, cvec *argv)
{
return exec(h, "$SHELL -l");
}
int cli_exec_show(clicon_handle h, cvec *cvv, cvec *argv)
{
char cmd[256];
int pos = 0;
int i;
for (i = 0; i < cvec_len(cvv); i++)
pos += snprintf(&cmd[pos], sizeof(cmd) - pos, "%s ",
cv_string_get(cvec_i(cvv, i)));
return exec(h, cmd);
}
int cli_exec_log(clicon_handle h, cvec *cvv, cvec *argv)
{
char cmd[128], arg[32];
cg_var *cv;
/* cvv[0] is the whole command, e.g. 'show log btmp' */
if (!strncmp("follow", cv_string_get(cvec_i(cvv, 0)), 6))
snprintf(cmd, sizeof(cmd), "tail -F ");
else
snprintf(cmd, sizeof(cmd), "cat ");
cv = cvec_find(cvv, "fn");
if (cv) {
snprintf(arg, sizeof(arg), _PATH_LOG "%s", cv_string_get(cv));
strcat(cmd, arg);
} else
strcat(cmd, _PATH_LOG "syslog");
return exec(h, cmd);
}
int logfiles(cligen_handle h, char *fn_str, cvec *cvv, cg_var *argv,
cvec *commands, cvec *helptexts)
{
glob_t gl;
size_t i;
int rc;
rc = glob(_PATH_LOG "*", 0, NULL, &gl);
if (rc && rc != GLOB_NOMATCH)
return 0;
for (i = 0; i < gl.gl_pathc; i++) {
char *fn;
fn = rindex(gl.gl_pathv[i], '/');
if (fn)
fn++;
else
fn = gl.gl_pathv[i];
cvec_add_string(commands, NULL, fn);
}
globfree(&gl);
return 0;
}
+19 -1
View File
@@ -32,8 +32,23 @@ copy("Copy and create a new object") {
}
}
follow("Continuously monitor a log file, default syslog"), cli_exec_log(); {
<fn:string logfiles()>(""), cli_exec_log();
}
help("Show help"), cli_help();
show("Show various system state and configuration") {
system-information("System information"), cli_sysinfo();
system-information("System information"), cli_exec_show();
fdb("Show forwarding database (unicast)"), cli_exec_show();
mdb("Show multicast forwarding database (unicast)"), cli_exec_show();
ports("Show ports available for bridging"), cli_exec_show();
vlans("Show port groups in bridge"), cli_exec_show();
ifaces("Show interfaces and their addresses"), cli_exec_show();
log("Show latest syslog entries"), cli_exec_log(); {
<fn:string logfiles()>(""), cli_exec_log();
}
version("Show operating system version"), cli_exec_show();
auto("Show expand x") {
xml @datamodelshow, cli_show_auto("candidate", "xml", true, false, "report-all");
@@ -72,3 +87,6 @@ show("Show various system state and configuration") {
}
}
}
# Hidden shell command, after 'show' command to avoid any possible completion problems
shell("Start domain shell"), hide, cli_exec_shell();