]> git.droids-corp.org - dpdk.git/commitdiff
cmdline: add function to verify valid commands
authorBruce Richardson <bruce.richardson@intel.com>
Fri, 10 Jun 2022 14:24:05 +0000 (15:24 +0100)
committerDavid Marchand <david.marchand@redhat.com>
Mon, 13 Jun 2022 08:26:39 +0000 (10:26 +0200)
The cmdline library cmdline_parse() function parses a command and
executes the action automatically too. The cmdline_valid_buffer function
also uses this function to validate commands, meaning that there is no
function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new
static inline function with an extra parameter to indicate whether the
action should be performed or not. Then we create two wrappers around
that - a replacement for the existing cmdline_parse function where the
extra parameter is "true" to execute the command, and a new function
"cmdline_parse_check" which passes the parameter as "false" to perform
cmdline validation only.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Tested-by: Weiyuan Li <weiyuanx.li@intel.com>
Acked-by: Olivier Matz <olivier.matz@6wind.com>
lib/cmdline/cmdline_parse.c
lib/cmdline/cmdline_parse.h
lib/cmdline/version.map

index 349ec87bd74b35ddb403976d69a95ac529941885..b6d6dacdef080a6c28f2a71ddb6c9fbd8bbdf022 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
        unsigned int inst_num=0;
        cmdline_parse_inst_t *inst;
@@ -282,20 +283,31 @@ cmdline_parse(struct cmdline *cl, const char * buf)
                inst = ctx[inst_num];
        }
 
-       /* call func */
-       if (f) {
-               f(result.buf, cl, data);
-       }
-
        /* no match */
-       else {
+       if (f == NULL) {
                debug_printf("No match err=%d\n", err);
                return err;
        }
 
+       /* call func if requested */
+       if (call_fn)
+               f(result.buf, cl, data);
+
        return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+       return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+       return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                 char *dst, unsigned int size)
index e4d802fff74f3fe7e24084bbfc02be987092dfa3..afc2fcd3dcf606654313970998f6c68262206ac6 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,25 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0).
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0)
+ * on successful parse.
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
index b9bbb87510921112443a5de222bfbab2a8beb950..fc7fdd6ea4fbac12bcdab5066ef53f16b6fe3979 100644 (file)
@@ -81,5 +81,8 @@ EXPERIMENTAL {
        rdline_get_history_buffer_size;
        rdline_get_opaque;
 
+       # added in 22.07
+       cmdline_parse_check;
+
        local: *;
 };