From a8be34bae896870bea3c24d8e4523e7903519152 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 5 Jan 2013 16:09:29 +0100 Subject: [PATCH] lldpcli: accept a command from the command line arguments As stated in the manual page, lldpcli should accept a command from command line arguments. --- src/client/lldpcli.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index bd28f05a..6b0a1de0 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -245,6 +245,11 @@ main(int argc, char *argv[]) } must_exit = 1; goto skipeditline; + } else { + if (optind < argc) { + /* More arguments! */ + must_exit = 1; + } } /* Init editline */ @@ -292,17 +297,17 @@ skipeditline: do { const char *line; - const char **argv; - int count, n, argc; + const char **cargv; + int count, n, cargc; - if (!is_lldpctl(NULL)) { + if (!is_lldpctl(NULL) && (optind >= argc)) { /* Read a new line. */ line = el_gets(el, &count); if (line == NULL) break; /* Tokenize it */ log_debug("lldpctl", "tokenize command line"); - n = tok_str(eltok, line, &argc, &argv); + n = tok_str(eltok, line, &cargc, &cargv); switch (n) { case -1: log_warnx("lldpctl", "internal error while tokenizing"); @@ -315,7 +320,7 @@ skipeditline: tok_reset(eltok); continue; } - if (argc == 0) { + if (cargc == 0) { tok_reset(eltok); continue; } @@ -335,17 +340,21 @@ skipeditline: if (is_lldpctl(NULL)) { if (!interfaces) { - argv = (const char*[]){ "show", "neighbors", "details" }; - argc = 3; + cargv = (const char*[]){ "show", "neighbors", "details" }; + cargc = 3; } else { - argv = (const char*[]){ "show", "neighbors", "ports", interfaces, "details" }; - argc = 5; + cargv = (const char*[]){ "show", "neighbors", "ports", interfaces, "details" }; + cargc = 5; } + } else if (optind < argc) { + cargv = (const char **)argv; + cargv = &cargv[optind]; + cargc = argc - optind; } /* Execute command */ if (commands_execute(conn, w, - root, argc, argv) != 0) + root, cargc, cargv) != 0) log_info("lldpctl", "an error occurred while executing last command"); w->finish(w); -- 2.39.5