From c370ac8706d194a82f513e31a76d0e1eadec53e3 Mon Sep 17 00:00:00 2001 From: shmuels Date: Sun, 18 Apr 2021 16:58:18 +0300 Subject: [PATCH 1/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 8 ++++---- apps/cli/cli_common.c | 8 ++++---- apps/cli/cli_generate.c | 35 ++++++++++++++++++++++++++------ apps/cli/cli_show.c | 8 ++++---- apps/netconf/netconf_lib.c | 2 +- doc/FAQ.md | 2 +- example/main/example_cli.c | 4 ++-- lib/clixon/clixon_xml_io.h | 4 ++-- lib/src/clixon_datastore_read.c | 6 +++--- lib/src/clixon_datastore_write.c | 4 ++-- lib/src/clixon_xml_io.c | 33 ++++++++++++++++++++++-------- lib/src/clixon_xml_map.c | 16 +++++++++++++++ lib/src/clixon_xml_vec.c | 2 +- util/clixon_util_datastore.c | 4 ++-- util/clixon_util_path.c | 2 +- util/clixon_util_xml_mod.c | 4 ++-- 16 files changed, 98 insertions(+), 44 deletions(-) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index 0441937e..ebd56c4e 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -413,10 +413,10 @@ cli_auto_show(clicon_handle h, switch (format){ case FORMAT_XML: if (isroot) - clicon_xml2file(stdout, xp, 0, pretty); + clicon_xml2file(stdout, xp, 0, pretty, 1); else{ while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - clicon_xml2file(stdout, xc, 0, pretty); + clicon_xml2file(stdout, xc, 0, pretty, 1); } fprintf(stdout, "\n"); break; @@ -451,10 +451,10 @@ cli_auto_show(clicon_handle h, if (pretty) fprintf(stdout, "\n"); if (isroot) - clicon_xml2file(stdout, xp, 2, pretty); + clicon_xml2file(stdout, xp, 2, pretty, 1); else while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - clicon_xml2file(stdout, xc, 2, pretty); + clicon_xml2file(stdout, xc, 2, pretty, 1); fprintf(stdout, "]]>]]>\n"); break; } /* switch */ diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 2a3c74a9..8ab03396 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -676,7 +676,7 @@ compare_xmls(cxobj *xc1, xml2txt_cb(f, xc, cligen_output); else while ((xc = xml_child_each(xc1, xc, -1)) != NULL) - clicon_xml2file_cb(f, xc, 0, 1, cligen_output); + clicon_xml2file_cb(f, xc, 0, 1, cligen_output, 0); fclose(f); close(fd); @@ -693,7 +693,7 @@ compare_xmls(cxobj *xc1, xml2txt_cb(f, xc, cligen_output); else while ((xc = xml_child_each(xc2, xc, -1)) != NULL) - clicon_xml2file_cb(f, xc, 0, 1, cligen_output); + clicon_xml2file_cb(f, xc, 0, 1, cligen_output, 0); fclose(f); close(fd); @@ -923,7 +923,7 @@ save_config_file(clicon_handle h, clicon_err(OE_CFG, errno, "Creating file %s", filename); goto done; } - if (clicon_xml2file(f, xt, 0, 1) < 0) + if (clicon_xml2file(f, xt, 0, 1, 0) < 0) goto done; retval = 0; /* Fall through */ @@ -1029,7 +1029,7 @@ cli_notification_cb(int s, while ((x = xml_child_each(xe, x, -1)) != NULL) { switch (format){ case FORMAT_XML: - if (clicon_xml2file_cb(stdout, x, 0, 1, cligen_output) < 0) + if (clicon_xml2file_cb(stdout, x, 0, 1, cligen_output, 0) < 0) goto done; break; case FORMAT_TEXT: diff --git a/apps/cli/cli_generate.c b/apps/cli/cli_generate.c index bf76b26f..8235d9d8 100644 --- a/apps/cli/cli_generate.c +++ b/apps/cli/cli_generate.c @@ -119,7 +119,14 @@ cli_expand_var_generate(clicon_handle h, cbuf *cb) { int retval = -1; - char *api_path_fmt = NULL; + char *api_path_fmt = NULL, *opext = NULL; + + if (yang_extension_value(ys, "autocli-op", CLIXON_LIB_NS, &opext) < 0) + goto done; + if (opext && strcmp(opext, "hide-database") == 0) { + retval = 1; + goto done; + } if (yang2api_path_fmt(ys, 1, &api_path_fmt) < 0) goto done; @@ -574,7 +581,7 @@ yang2cli_var(clicon_handle h, uint8_t fraction_digits = 0; enum cv_type cvtype; int options = 0; - int completionp; + int completionp, result; char *type; if ((patterns = cvec_new(0)) == NULL){ @@ -599,9 +606,10 @@ yang2cli_var(clicon_handle h, if (yang2cli_var_union(h, ys, origtype, yrestype, helptext, cb) < 0) goto done; if (clicon_cli_genmodel_completion(h)){ - if (cli_expand_var_generate(h, ys, cvtype, + result = cli_expand_var_generate(h, ys, cvtype, options, fraction_digits, - cb) < 0) + cb); + if (result < 0) goto done; yang2cli_helptext(cb, helptext); } @@ -622,9 +630,10 @@ yang2cli_var(clicon_handle h, options, cvv, patterns, fraction_digits, cb)) < 0) goto done; if (completionp){ - if (cli_expand_var_generate(h, ys, cvtype, + result = cli_expand_var_generate(h, ys, cvtype, options, fraction_digits, - cb) < 0) + cb); + if (result < 0) goto done; yang2cli_helptext(cb, helptext); cprintf(cb, ")"); @@ -688,6 +697,10 @@ yang2cli_leaf(clicon_handle h, cprintf(cb, ",hide{"); extralevel = 1; } + if (opext && strcmp(opext, "hide-database-auto-completion") == 0){ + cprintf(cb, ",hide-database-auto-completion{"); + extralevel = 1; + } if (yang2cli_var(h, ys, helptext, cb) < 0) goto done; } @@ -695,6 +708,9 @@ yang2cli_leaf(clicon_handle h, if (opext && strcmp(opext, "hide") == 0){ cprintf(cb, ",hide"); } + if (opext && strcmp(opext, "hide-database-auto-completion") == 0){ + cprintf(cb, ",hide-database-auto-completion"); + } } } else{ @@ -767,6 +783,9 @@ yang2cli_container(clicon_handle h, if (opext != NULL && strcmp(opext, "hide") == 0){ cprintf(cb, ",hide"); } + if (opext != NULL && strcmp(opext, "hide-database-auto-completion") == 0){ + cprintf(cb, ",hide-database-auto-completion"); + } cprintf(cb, ";{\n"); } @@ -822,6 +841,10 @@ yang2cli_list(clicon_handle h, cprintf(cb, ",hide"); extralevel = 1; } + if (opext != NULL && strcmp(opext, "hide-database-auto-completion") == 0){ + cprintf(cb, ",hide-database-auto-completion"); + extralevel = 1; + } if ((yd = yang_find(ys, Y_DESCRIPTION, NULL)) != NULL){ if ((helptext = strdup(yang_argument_get(yd))) == NULL){ clicon_err(OE_UNIX, errno, "strdup"); diff --git a/apps/cli/cli_show.c b/apps/cli/cli_show.c index e323d8b3..47666e20 100644 --- a/apps/cli/cli_show.c +++ b/apps/cli/cli_show.c @@ -476,7 +476,7 @@ cli_show_config1(clicon_handle h, case FORMAT_XML: xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output); + clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output, 1); break; case FORMAT_JSON: xml2json_cb(stdout, xt, 1, cligen_output); @@ -499,7 +499,7 @@ cli_show_config1(clicon_handle h, NETCONF_BASE_NAMESPACE); xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - clicon_xml2file_cb(stdout, xc, 2, 1, cligen_output); + clicon_xml2file_cb(stdout, xc, 2, 1, cligen_output, 1); cligen_output(stdout, "]]>]]>\n"); break; } @@ -747,14 +747,14 @@ cli_show_auto1(clicon_handle h, break; case FORMAT_NETCONF: fprintf(stdout, "\n"); - clicon_xml2file(stdout, xp, 2, 1); + clicon_xml2file(stdout, xp, 2, 1, 1); fprintf(stdout, "]]>]]>\n"); break; default: for (; i < xml_child_nr(xml_parent(xp)) ; ++i, xp_helper = xml_child_i(xml_parent(xp), i)) { switch (format){ case FORMAT_XML: - clicon_xml2file(stdout, xp_helper, 0, 1); + clicon_xml2file(stdout, xp_helper, 0, 1, 1); break; case FORMAT_JSON: xml2json_cb(stdout, xp_helper, 1, cligen_output); diff --git a/apps/netconf/netconf_lib.c b/apps/netconf/netconf_lib.c index ebbe43cb..56580951 100644 --- a/apps/netconf/netconf_lib.c +++ b/apps/netconf/netconf_lib.c @@ -200,7 +200,7 @@ netconf_output(int s, if (clicon_debug_get() > 1){ /* XXX: below only works to stderr, clicon_debug may log to syslog */ cxobj *xt = NULL; if (clixon_xml_parse_string(buf, YB_NONE, NULL, &xt, NULL) == 0){ - clicon_xml2file(stderr, xml_child_i(xt, 0), 0, 0); + clicon_xml2file(stderr, xml_child_i(xt, 0), 0, 0, 0); fprintf(stderr, "\n"); xml_free(xt); } diff --git a/doc/FAQ.md b/doc/FAQ.md index d105af3f..d321d0a3 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -485,7 +485,7 @@ Suppose you want to print all added interfaces: cxobj *target = transaction_target(td); # wanted XML tree vec = xpath_vec_flag(target, NULL, "//interface", &len, XML_FLAG_ADD); /* Get added i/fs */ for (i=0; i1) - clicon_xml2file(stderr, xt, 0, 1); + clicon_xml2file(stderr, xt, 0, 1, 0); *xtop = xt; xt = NULL; retval = 1; @@ -872,7 +872,7 @@ xmldb_get_cache(clicon_handle h, * If cache was empty, also update to datastore cache */ if (clicon_debug_get()>1) - clicon_xml2file(stderr, x1t, 0, 1); + clicon_xml2file(stderr, x1t, 0, 1, 0); *xtop = x1t; retval = 1; done: @@ -971,7 +971,7 @@ xmldb_get_zerocopy(clicon_handle h, goto done; } if (clicon_debug_get()>1) - clicon_xml2file(stderr, x0t, 0, 1); + clicon_xml2file(stderr, x0t, 0, 1, 0); *xtop = x0t; retval = 1; done: diff --git a/lib/src/clixon_datastore_write.c b/lib/src/clixon_datastore_write.c index a4425258..5945d8fa 100644 --- a/lib/src/clixon_datastore_write.c +++ b/lib/src/clixon_datastore_write.c @@ -1043,7 +1043,7 @@ xmldb_put(clicon_handle h, if (xml2json(f, x0, pretty) < 0) goto done; } - else if (clicon_xml2file(f, x0, 0, pretty) < 0) + else if (clicon_xml2file(f, x0, 0, pretty, 0) < 0) goto done; /* Remove modules state after writing to file */ @@ -1101,7 +1101,7 @@ xmldb_dump(clicon_handle h, if (xml2json(f, xt, pretty) < 0) goto done; } - else if (clicon_xml2file(f, xt, 0, pretty) < 0) + else if (clicon_xml2file(f, xt, 0, pretty, 0) < 0) goto done; retval = 0; done: diff --git a/lib/src/clixon_xml_io.c b/lib/src/clixon_xml_io.c index 7b2e1a2f..4b424f40 100644 --- a/lib/src/clixon_xml_io.c +++ b/lib/src/clixon_xml_io.c @@ -94,6 +94,7 @@ * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. * @param[in] fn Callback to make print function + * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf * One can use clicon_xml2cbuf to get common code, but using fprintf is * much faster than using cbuf and then printing that,... @@ -103,7 +104,8 @@ xml2file_recurse(FILE *f, cxobj *x, int level, int prettyprint, - clicon_output_cb *fn) + clicon_output_cb *fn, + int used_for_show_command) { int retval = -1; char *name; @@ -113,9 +115,17 @@ xml2file_recurse(FILE *f, int haselement; char *val; char *encstr = NULL; /* xml encoded string */ - + char *opext = NULL; + if (x == NULL) goto ok; + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((used_for_show_command == 1) && (opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } name = xml_name(x); namespace = xml_prefix(x); switch(xml_type(x)){ @@ -144,7 +154,7 @@ xml2file_recurse(FILE *f, while ((xc = xml_child_each(x, xc, -1)) != NULL) { switch (xml_type(xc)){ case CX_ATTR: - if (xml2file_recurse(f, xc, level+1, prettyprint, fn) <0) + if (xml2file_recurse(f, xc, level+1, prettyprint, fn, used_for_show_command) <0) goto done; break; case CX_BODY: @@ -169,7 +179,7 @@ xml2file_recurse(FILE *f, xc = NULL; while ((xc = xml_child_each(x, xc, -1)) != NULL) { if (xml_type(xc) != CX_ATTR) - if (xml2file_recurse(f, xc, level+1, prettyprint, fn) <0) + if (xml2file_recurse(f, xc, level+1, prettyprint, fn, used_for_show_command) <0) goto done; } if (prettyprint && hasbody==0) @@ -199,6 +209,7 @@ xml2file_recurse(FILE *f, * @param[in] xn clicon xml tree * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. + * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf print to a cbuf string * @see clicon_xml2cbuf_cb print using a callback */ @@ -206,9 +217,10 @@ int clicon_xml2file(FILE *f, cxobj *x, int level, - int prettyprint) + int prettyprint, + int used_for_show_command) { - return xml2file_recurse(f, x, level, prettyprint, fprintf); + return xml2file_recurse(f, x, level, prettyprint, fprintf, used_for_show_command); } /*! Print an XML tree structure to an output stream and encode chars "<>&" @@ -217,6 +229,7 @@ clicon_xml2file(FILE *f, * @param[in] xn clicon xml tree * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. + * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf */ int @@ -224,9 +237,10 @@ clicon_xml2file_cb(FILE *f, cxobj *x, int level, int prettyprint, - clicon_output_cb *fn) + clicon_output_cb *fn, + int used_for_show_command) { - return xml2file_recurse(f, x, level, prettyprint, fn); + return xml2file_recurse(f, x, level, prettyprint, fn, used_for_show_command); } /*! Print an XML tree structure to an output stream @@ -235,6 +249,7 @@ clicon_xml2file_cb(FILE *f, * * @param[in] f UNIX output stream * @param[in] xn clicon xml tree + * used_for_show_command * @see clicon_xml2cbuf * @see clicon_xml2cbuf_cb print using a callback */ @@ -242,7 +257,7 @@ int xml_print(FILE *f, cxobj *x) { - return xml2file_recurse(f, x, 0, 1, fprintf); + return xml2file_recurse(f, x, 0, 1, fprintf, 0); } /*! Print an XML tree structure to a cligen buffer and encode chars "<>&" diff --git a/lib/src/clixon_xml_map.c b/lib/src/clixon_xml_map.c index 848f4ebf..5cd31f37 100644 --- a/lib/src/clixon_xml_map.c +++ b/lib/src/clixon_xml_map.c @@ -144,11 +144,19 @@ xml2txt_recurse(FILE *f, cxobj *xc = NULL; int children=0; int retval = -1; + char *opext = NULL; if (f == NULL || x == NULL || fn == NULL){ clicon_err(OE_XML, EINVAL, "f, x or fn is NULL"); goto done; } + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } xc = NULL; /* count children (elements and bodies, not attributes) */ while ((xc = xml_child_each(x, xc, -1)) != NULL) if (xml_type(xc) == CX_ELMNT || xml_type(xc) == CX_BODY) @@ -233,11 +241,19 @@ xml2cli_recurse(FILE *f, yang_stmt *ys; int match; char *body; + char *opext = NULL; if (xml_type(x)==CX_ATTR) goto ok; if ((ys = xml_spec(x)) == NULL) goto ok; + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } /* If leaf/leaf-list or presence container, then print line */ if (yang_keyword_get(ys) == Y_LEAF || yang_keyword_get(ys) == Y_LEAF_LIST){ diff --git a/lib/src/clixon_xml_vec.c b/lib/src/clixon_xml_vec.c index 21e222a9..e5ceb57c 100644 --- a/lib/src/clixon_xml_vec.c +++ b/lib/src/clixon_xml_vec.c @@ -341,7 +341,7 @@ clixon_xvec_print(FILE *f, int i; for (i=0; ixv_len; i++) - clicon_xml2file(f, xv->xv_vec[i], 0, 1); + clicon_xml2file(f, xv->xv_vec[i], 0, 1, 0); return 0; } diff --git a/util/clixon_util_datastore.c b/util/clixon_util_datastore.c index 65e611f2..8b8207a9 100644 --- a/util/clixon_util_datastore.c +++ b/util/clixon_util_datastore.c @@ -205,7 +205,7 @@ main(int argc, char **argv) xpath = "/"; if (xmldb_get(h, db, NULL, xpath, &xt) < 0) goto done; - clicon_xml2file(stdout, xt, 0, 0); + clicon_xml2file(stdout, xt, 0, 0, 0); fprintf(stdout, "\n"); if (xt){ xml_free(xt); @@ -228,7 +228,7 @@ main(int argc, char **argv) clicon_err(OE_DB, 0, "xt is NULL"); goto done; } - clicon_xml2file(stdout, xt, 0, 0); + clicon_xml2file(stdout, xt, 0, 0, 0); if (xt){ xml_free(xt); xt = NULL; diff --git a/util/clixon_util_path.c b/util/clixon_util_path.c index f75a36da..c2b73c98 100644 --- a/util/clixon_util_path.c +++ b/util/clixon_util_path.c @@ -279,7 +279,7 @@ main(int argc, for (i = 0; i < xlen; i++){ xc = xvec[i]; fprintf(stdout, "%d: ", i); - clicon_xml2file(stdout, xc, 0, 0); + clicon_xml2file(stdout, xc, 0, 0, 0); fprintf(stdout, "\n"); fflush(stdout); } diff --git a/util/clixon_util_xml_mod.c b/util/clixon_util_xml_mod.c index 7347842c..b2850afe 100644 --- a/util/clixon_util_xml_mod.c +++ b/util/clixon_util_xml_mod.c @@ -281,9 +281,9 @@ main(int argc, char **argv) if (sort) xml_sort_recurse(xb); if (strcmp(xml_name(xb),"top")==0) - clicon_xml2file(stdout, xml_child_i_type(xb, 0, CX_ELMNT), 0, 0); + clicon_xml2file(stdout, xml_child_i_type(xb, 0, CX_ELMNT), 0, 0, 0); else - clicon_xml2file(stdout, xb, 0, 0); + clicon_xml2file(stdout, xb, 0, 0, 0); fprintf(stdout, "\n"); retval = 0; done: From cede620a2a17f7233a79ca6494747a4891866777 Mon Sep 17 00:00:00 2001 From: shmuels Date: Mon, 19 Apr 2021 10:27:44 +0300 Subject: [PATCH 2/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_generate.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/cli/cli_generate.c b/apps/cli/cli_generate.c index 8235d9d8..4b665694 100644 --- a/apps/cli/cli_generate.c +++ b/apps/cli/cli_generate.c @@ -611,6 +611,7 @@ yang2cli_var(clicon_handle h, cb); if (result < 0) goto done; + if (result == 0) yang2cli_helptext(cb, helptext); } cprintf(cb, ")"); @@ -635,6 +636,7 @@ yang2cli_var(clicon_handle h, cb); if (result < 0) goto done; + if (result == 0) yang2cli_helptext(cb, helptext); cprintf(cb, ")"); } From d962399db0250f12eaf3644a2e59d6495649601f Mon Sep 17 00:00:00 2001 From: shmuels Date: Mon, 19 Apr 2021 13:29:00 +0300 Subject: [PATCH 3/9] Revert "change localtime to localtime_r which is a thread-safe function" This reverts commit 8ca59441119daf13fbc854dee5fe53a8130cf48d. --- lib/src/clixon_log.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/clixon_log.c b/lib/src/clixon_log.c index caac71e6..1d1f1b25 100644 --- a/lib/src/clixon_log.c +++ b/lib/src/clixon_log.c @@ -171,13 +171,13 @@ static int flogtime(FILE *f) { struct timeval tv; - struct tm tm; + struct tm *tm; gettimeofday(&tv, NULL); - localtime_r((time_t*)&tv.tv_sec, &tm); + tm = localtime((time_t*)&tv.tv_sec); fprintf(f, "%s %2d %02d:%02d:%02d: ", - mon2name(tm.tm_mon), tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); + mon2name(tm->tm_mon), tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); return 0; } From 3736927efa11585eee01da9616ed47abb65c834e Mon Sep 17 00:00:00 2001 From: shmuels Date: Tue, 20 Apr 2021 18:35:20 +0300 Subject: [PATCH 4/9] completing the function of hide for autocli-op in the yang files --- yang/clixon/clixon-lib@2021-03-08.yang | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/yang/clixon/clixon-lib@2021-03-08.yang b/yang/clixon/clixon-lib@2021-03-08.yang index 44c5c0c8..4923793a 100644 --- a/yang/clixon/clixon-lib@2021-03-08.yang +++ b/yang/clixon/clixon-lib@2021-03-08.yang @@ -103,7 +103,9 @@ module clixon-lib { this point in the YANG tree for the automated generated CLI. Note that this extension is only used in clixon_cli. Operations is expected to be extended, but the following operations are defined: - - hide This command is active but not shown by ? or TAB"; + - hide This command is active but not shown by ? or TAB (meaning, it hides the auto-completion of commands) + - hide-database This command hides the database + - hide-database-auto-completion This command hides the database and the auto completion (meaning, this command acts as both commands above)"; argument cliop; } rpc debug { From 33ad4288543be141f384964b3f64b66ecf93d287 Mon Sep 17 00:00:00 2001 From: shmuels Date: Wed, 21 Apr 2021 15:09:40 +0300 Subject: [PATCH 5/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 116 ++++++++++++++++++++++++++++++-- apps/cli/cli_common.c | 8 +-- apps/cli/cli_generate.c | 20 +++--- apps/cli/cli_show.c | 10 +-- apps/cli/clixon_cli_api.h | 1 + apps/netconf/netconf_lib.c | 2 +- doc/FAQ.md | 2 +- example/main/example_cli.c | 4 +- lib/clixon/clixon_xml_io.h | 4 +- lib/src/clixon_datastore_read.c | 6 +- lib/src/clixon_xml_io.c | 31 +++------ lib/src/clixon_xml_vec.c | 2 +- util/clixon_util_path.c | 2 +- util/clixon_util_xml_mod.c | 4 +- 14 files changed, 153 insertions(+), 59 deletions(-) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index ebd56c4e..a9b15a5d 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -120,6 +120,114 @@ cvec_append(cvec *cvv0, return cvv2; } +/*! Print an XML tree structure to an output stream and encode chars "<>&" + * + * @param[in] xn clicon xml tree + * @param[in] level how many spaces to insert before each line + * @param[in] prettyprint insert \n and spaces tomake the xml more readable. + * @param[in] fn Callback to make print function + * @see clicon_xml2cbuf + * One can use clicon_xml2cbuf to get common code, but using fprintf is + * much faster than using cbuf and then printing that,... + */ +int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) +{ + int retval = -1; + char *name; + char *namespace; + cxobj *xc; + int hasbody; + int haselement; + char *val; + char *encstr = NULL; /* xml encoded string */ + char *opext = NULL; + + if (x == NULL) + goto ok; + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } + name = xml_name(x); + namespace = xml_prefix(x); + switch(xml_type(x)){ + case CX_BODY: + if ((val = xml_value(x)) == NULL) /* incomplete tree */ + break; + if (xml_chardata_encode(&encstr, "%s", val) < 0) + goto done; + (*fn)(stdout, "%s", encstr); + break; + case CX_ATTR: + (*fn)(stdout, " "); + if (namespace) + (*fn)(stdout, "%s:", namespace); + (*fn)(stdout, "%s=\"%s\"", name, xml_value(x)); + break; + case CX_ELMNT: + (*fn)(stdout, "%*s<", prettyprint?(level*XML_INDENT):0, ""); + if (namespace) + (*fn)(stdout, "%s:", namespace); + (*fn)(stdout, "%s", name); + hasbody = 0; + haselement = 0; + xc = NULL; + /* print attributes only */ + while ((xc = xml_child_each(x, xc, -1)) != NULL) { + switch (xml_type(xc)){ + case CX_ATTR: + if (xml2file(xc, level+1, prettyprint, fprintf) <0) + goto done; + break; + case CX_BODY: + hasbody=1; + break; + case CX_ELMNT: + haselement=1; + break; + default: + break; + } + } + /* Check for special case instead of : + * Ie, no CX_BODY or CX_ELMNT child. + */ + if (hasbody==0 && haselement==0) + (*fn)(stdout, "/>"); + else{ + (*fn)(stdout, ">"); + if (prettyprint && hasbody == 0) + (*fn)(stdout, "\n"); + xc = NULL; + while ((xc = xml_child_each(x, xc, -1)) != NULL) { + if (xml_type(xc) != CX_ATTR) + if (xml2file(xc, level+1, prettyprint, fprintf) <0) + goto done; + } + if (prettyprint && hasbody==0) + (*fn)(stdout, "%*s", level*XML_INDENT, ""); + (*fn)(stdout, "", name); + } + if (prettyprint) + (*fn)(stdout, "\n"); + break; + default: + break; + }/* switch */ + ok: + retval = 0; + done: + if (encstr) + free(encstr); + return retval; +} + /*! Enter a CLI edit mode * @param[in] h CLICON handle * @param[in] cvv Vector of variables from CLIgen command-line @@ -413,10 +521,10 @@ cli_auto_show(clicon_handle h, switch (format){ case FORMAT_XML: if (isroot) - clicon_xml2file(stdout, xp, 0, pretty, 1); + stdout(xp, 0, pretty, 1); else{ while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - clicon_xml2file(stdout, xc, 0, pretty, 1); + stdout(xc, 0, pretty, 1); } fprintf(stdout, "\n"); break; @@ -451,10 +559,10 @@ cli_auto_show(clicon_handle h, if (pretty) fprintf(stdout, "\n"); if (isroot) - clicon_xml2file(stdout, xp, 2, pretty, 1); + xml2file(xp, 2, pretty, fprintf); else while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - clicon_xml2file(stdout, xc, 2, pretty, 1); + xml2file(xc, 2, pretty, fprintf); fprintf(stdout, "]]>]]>\n"); break; } /* switch */ diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 8ab03396..2a3c74a9 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -676,7 +676,7 @@ compare_xmls(cxobj *xc1, xml2txt_cb(f, xc, cligen_output); else while ((xc = xml_child_each(xc1, xc, -1)) != NULL) - clicon_xml2file_cb(f, xc, 0, 1, cligen_output, 0); + clicon_xml2file_cb(f, xc, 0, 1, cligen_output); fclose(f); close(fd); @@ -693,7 +693,7 @@ compare_xmls(cxobj *xc1, xml2txt_cb(f, xc, cligen_output); else while ((xc = xml_child_each(xc2, xc, -1)) != NULL) - clicon_xml2file_cb(f, xc, 0, 1, cligen_output, 0); + clicon_xml2file_cb(f, xc, 0, 1, cligen_output); fclose(f); close(fd); @@ -923,7 +923,7 @@ save_config_file(clicon_handle h, clicon_err(OE_CFG, errno, "Creating file %s", filename); goto done; } - if (clicon_xml2file(f, xt, 0, 1, 0) < 0) + if (clicon_xml2file(f, xt, 0, 1) < 0) goto done; retval = 0; /* Fall through */ @@ -1029,7 +1029,7 @@ cli_notification_cb(int s, while ((x = xml_child_each(xe, x, -1)) != NULL) { switch (format){ case FORMAT_XML: - if (clicon_xml2file_cb(stdout, x, 0, 1, cligen_output, 0) < 0) + if (clicon_xml2file_cb(stdout, x, 0, 1, cligen_output) < 0) goto done; break; case FORMAT_TEXT: diff --git a/apps/cli/cli_generate.c b/apps/cli/cli_generate.c index 4b665694..6e7b8f5e 100644 --- a/apps/cli/cli_generate.c +++ b/apps/cli/cli_generate.c @@ -836,7 +836,16 @@ yang2cli_list(clicon_handle h, char *opext = NULL; cprintf(cb, "%*s%s", level*3, "", yang_argument_get(ys)); - /* Look for autocli-op defined in clixon-lib.yang */ + if ((yd = yang_find(ys, Y_DESCRIPTION, NULL)) != NULL){ + if ((helptext = strdup(yang_argument_get(yd))) == NULL){ + clicon_err(OE_UNIX, errno, "strdup"); + goto done; + } + if ((s = strstr(helptext, "\n\n")) != NULL) + *s = '\0'; + yang2cli_helptext(cb, helptext); + } + /* Look for autocli-op defined in clixon-lib.yang */ if (yang_extension_value(ys, "autocli-op", CLIXON_LIB_NS, &opext) < 0) goto done; if (opext != NULL && strcmp(opext, "hide") == 0){ @@ -847,15 +856,6 @@ yang2cli_list(clicon_handle h, cprintf(cb, ",hide-database-auto-completion"); extralevel = 1; } - if ((yd = yang_find(ys, Y_DESCRIPTION, NULL)) != NULL){ - if ((helptext = strdup(yang_argument_get(yd))) == NULL){ - clicon_err(OE_UNIX, errno, "strdup"); - goto done; - } - if ((s = strstr(helptext, "\n\n")) != NULL) - *s = '\0'; - yang2cli_helptext(cb, helptext); - } /* Loop over all key variables */ cvk = yang_cvec_get(ys); /* Use Y_LIST cache, see ys_populate_list() */ cvi = NULL; diff --git a/apps/cli/cli_show.c b/apps/cli/cli_show.c index 47666e20..9aa39a0e 100644 --- a/apps/cli/cli_show.c +++ b/apps/cli/cli_show.c @@ -476,7 +476,7 @@ cli_show_config1(clicon_handle h, case FORMAT_XML: xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output, 1); + xml2file(xc, 0, 1, cligen_output); break; case FORMAT_JSON: xml2json_cb(stdout, xt, 1, cligen_output); @@ -499,7 +499,7 @@ cli_show_config1(clicon_handle h, NETCONF_BASE_NAMESPACE); xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - clicon_xml2file_cb(stdout, xc, 2, 1, cligen_output, 1); + xml2file(xc, 2, 1, cligen_output); cligen_output(stdout, "]]>]]>\n"); break; } @@ -623,7 +623,7 @@ show_conf_xpath(clicon_handle h, if (xpath_vec(xt, nsc, "%s", &xv, &xlen, xpath) < 0) goto done; for (i=0; i\n"); - clicon_xml2file(stdout, xp, 2, 1, 1); + xml2file(xp, 2, 1, fprintf); fprintf(stdout, "]]>]]>\n"); break; default: for (; i < xml_child_nr(xml_parent(xp)) ; ++i, xp_helper = xml_child_i(xml_parent(xp), i)) { switch (format){ case FORMAT_XML: - clicon_xml2file(stdout, xp_helper, 0, 1, 1); + xml2file(xp_helper, 0, 1, fprintf); break; case FORMAT_JSON: xml2json_cb(stdout, xp_helper, 1, cligen_output); diff --git a/apps/cli/clixon_cli_api.h b/apps/cli/clixon_cli_api.h index 2a5d4a60..7f2fa55d 100644 --- a/apps/cli/clixon_cli_api.h +++ b/apps/cli/clixon_cli_api.h @@ -116,6 +116,7 @@ int cli_help(clicon_handle h, cvec *vars, cvec *argv); /* In cli_show.c */ int expand_dbvar(void *h, char *name, cvec *cvv, cvec *argv, cvec *commands, cvec *helptexts); +int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn); /* cli_show.c: CLIgen new vector arg callbacks */ int show_yang(clicon_handle h, cvec *vars, cvec *argv); diff --git a/apps/netconf/netconf_lib.c b/apps/netconf/netconf_lib.c index 56580951..ebbe43cb 100644 --- a/apps/netconf/netconf_lib.c +++ b/apps/netconf/netconf_lib.c @@ -200,7 +200,7 @@ netconf_output(int s, if (clicon_debug_get() > 1){ /* XXX: below only works to stderr, clicon_debug may log to syslog */ cxobj *xt = NULL; if (clixon_xml_parse_string(buf, YB_NONE, NULL, &xt, NULL) == 0){ - clicon_xml2file(stderr, xml_child_i(xt, 0), 0, 0, 0); + clicon_xml2file(stderr, xml_child_i(xt, 0), 0, 0); fprintf(stderr, "\n"); xml_free(xt); } diff --git a/doc/FAQ.md b/doc/FAQ.md index d321d0a3..d105af3f 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -485,7 +485,7 @@ Suppose you want to print all added interfaces: cxobj *target = transaction_target(td); # wanted XML tree vec = xpath_vec_flag(target, NULL, "//interface", &len, XML_FLAG_ADD); /* Get added i/fs */ for (i=0; i1) - clicon_xml2file(stderr, xt, 0, 1, 0); + clicon_xml2file(stderr, xt, 0, 1); *xtop = xt; xt = NULL; retval = 1; @@ -872,7 +872,7 @@ xmldb_get_cache(clicon_handle h, * If cache was empty, also update to datastore cache */ if (clicon_debug_get()>1) - clicon_xml2file(stderr, x1t, 0, 1, 0); + clicon_xml2file(stderr, x1t, 0, 1); *xtop = x1t; retval = 1; done: @@ -971,7 +971,7 @@ xmldb_get_zerocopy(clicon_handle h, goto done; } if (clicon_debug_get()>1) - clicon_xml2file(stderr, x0t, 0, 1, 0); + clicon_xml2file(stderr, x0t, 0, 1); *xtop = x0t; retval = 1; done: diff --git a/lib/src/clixon_xml_io.c b/lib/src/clixon_xml_io.c index 4b424f40..a4a89577 100644 --- a/lib/src/clixon_xml_io.c +++ b/lib/src/clixon_xml_io.c @@ -104,8 +104,7 @@ xml2file_recurse(FILE *f, cxobj *x, int level, int prettyprint, - clicon_output_cb *fn, - int used_for_show_command) + clicon_output_cb *fn) { int retval = -1; char *name; @@ -115,17 +114,8 @@ xml2file_recurse(FILE *f, int haselement; char *val; char *encstr = NULL; /* xml encoded string */ - char *opext = NULL; - if (x == NULL) goto ok; - /* Look for autocli-op defined in clixon-lib.yang */ - if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { - goto ok; - } - if ((used_for_show_command == 1) && (opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ - goto ok; - } name = xml_name(x); namespace = xml_prefix(x); switch(xml_type(x)){ @@ -154,7 +144,7 @@ xml2file_recurse(FILE *f, while ((xc = xml_child_each(x, xc, -1)) != NULL) { switch (xml_type(xc)){ case CX_ATTR: - if (xml2file_recurse(f, xc, level+1, prettyprint, fn, used_for_show_command) <0) + if (xml2file_recurse(f, xc, level+1, prettyprint, fn) <0) goto done; break; case CX_BODY: @@ -179,7 +169,7 @@ xml2file_recurse(FILE *f, xc = NULL; while ((xc = xml_child_each(x, xc, -1)) != NULL) { if (xml_type(xc) != CX_ATTR) - if (xml2file_recurse(f, xc, level+1, prettyprint, fn, used_for_show_command) <0) + if (xml2file_recurse(f, xc, level+1, prettyprint, fn) <0) goto done; } if (prettyprint && hasbody==0) @@ -209,7 +199,6 @@ xml2file_recurse(FILE *f, * @param[in] xn clicon xml tree * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. - * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf print to a cbuf string * @see clicon_xml2cbuf_cb print using a callback */ @@ -217,10 +206,9 @@ int clicon_xml2file(FILE *f, cxobj *x, int level, - int prettyprint, - int used_for_show_command) + int prettyprint) { - return xml2file_recurse(f, x, level, prettyprint, fprintf, used_for_show_command); + return xml2file_recurse(f, x, level, prettyprint, fprintf); } /*! Print an XML tree structure to an output stream and encode chars "<>&" @@ -229,7 +217,6 @@ clicon_xml2file(FILE *f, * @param[in] xn clicon xml tree * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. - * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf */ int @@ -237,10 +224,9 @@ clicon_xml2file_cb(FILE *f, cxobj *x, int level, int prettyprint, - clicon_output_cb *fn, - int used_for_show_command) + clicon_output_cb *fn) { - return xml2file_recurse(f, x, level, prettyprint, fn, used_for_show_command); + return xml2file_recurse(f, x, level, prettyprint, fn); } /*! Print an XML tree structure to an output stream @@ -249,7 +235,6 @@ clicon_xml2file_cb(FILE *f, * * @param[in] f UNIX output stream * @param[in] xn clicon xml tree - * used_for_show_command * @see clicon_xml2cbuf * @see clicon_xml2cbuf_cb print using a callback */ @@ -257,7 +242,7 @@ int xml_print(FILE *f, cxobj *x) { - return xml2file_recurse(f, x, 0, 1, fprintf, 0); + return xml2file_recurse(f, x, 0, 1, fprintf); } /*! Print an XML tree structure to a cligen buffer and encode chars "<>&" diff --git a/lib/src/clixon_xml_vec.c b/lib/src/clixon_xml_vec.c index e5ceb57c..21e222a9 100644 --- a/lib/src/clixon_xml_vec.c +++ b/lib/src/clixon_xml_vec.c @@ -341,7 +341,7 @@ clixon_xvec_print(FILE *f, int i; for (i=0; ixv_len; i++) - clicon_xml2file(f, xv->xv_vec[i], 0, 1, 0); + clicon_xml2file(f, xv->xv_vec[i], 0, 1); return 0; } diff --git a/util/clixon_util_path.c b/util/clixon_util_path.c index c2b73c98..f75a36da 100644 --- a/util/clixon_util_path.c +++ b/util/clixon_util_path.c @@ -279,7 +279,7 @@ main(int argc, for (i = 0; i < xlen; i++){ xc = xvec[i]; fprintf(stdout, "%d: ", i); - clicon_xml2file(stdout, xc, 0, 0, 0); + clicon_xml2file(stdout, xc, 0, 0); fprintf(stdout, "\n"); fflush(stdout); } diff --git a/util/clixon_util_xml_mod.c b/util/clixon_util_xml_mod.c index b2850afe..7347842c 100644 --- a/util/clixon_util_xml_mod.c +++ b/util/clixon_util_xml_mod.c @@ -281,9 +281,9 @@ main(int argc, char **argv) if (sort) xml_sort_recurse(xb); if (strcmp(xml_name(xb),"top")==0) - clicon_xml2file(stdout, xml_child_i_type(xb, 0, CX_ELMNT), 0, 0, 0); + clicon_xml2file(stdout, xml_child_i_type(xb, 0, CX_ELMNT), 0, 0); else - clicon_xml2file(stdout, xb, 0, 0, 0); + clicon_xml2file(stdout, xb, 0, 0); fprintf(stdout, "\n"); retval = 0; done: From b0c5e43d50ef94dd7559eda6fc2c2140766be9b1 Mon Sep 17 00:00:00 2001 From: shmuels Date: Wed, 21 Apr 2021 15:15:35 +0300 Subject: [PATCH 6/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 4 ++-- lib/src/clixon_datastore_write.c | 4 ++-- lib/src/clixon_xml_io.c | 2 +- util/clixon_util_datastore.c | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index a9b15a5d..7e31db58 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -521,10 +521,10 @@ cli_auto_show(clicon_handle h, switch (format){ case FORMAT_XML: if (isroot) - stdout(xp, 0, pretty, 1); + xml2file(xp, 0, pretty, fprintf); else{ while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - stdout(xc, 0, pretty, 1); + xml2file(xc, 0, pretty, fprintf); } fprintf(stdout, "\n"); break; diff --git a/lib/src/clixon_datastore_write.c b/lib/src/clixon_datastore_write.c index 5945d8fa..a4425258 100644 --- a/lib/src/clixon_datastore_write.c +++ b/lib/src/clixon_datastore_write.c @@ -1043,7 +1043,7 @@ xmldb_put(clicon_handle h, if (xml2json(f, x0, pretty) < 0) goto done; } - else if (clicon_xml2file(f, x0, 0, pretty, 0) < 0) + else if (clicon_xml2file(f, x0, 0, pretty) < 0) goto done; /* Remove modules state after writing to file */ @@ -1101,7 +1101,7 @@ xmldb_dump(clicon_handle h, if (xml2json(f, xt, pretty) < 0) goto done; } - else if (clicon_xml2file(f, xt, 0, pretty, 0) < 0) + else if (clicon_xml2file(f, xt, 0, pretty) < 0) goto done; retval = 0; done: diff --git a/lib/src/clixon_xml_io.c b/lib/src/clixon_xml_io.c index a4a89577..d51bdc60 100644 --- a/lib/src/clixon_xml_io.c +++ b/lib/src/clixon_xml_io.c @@ -94,7 +94,6 @@ * @param[in] level how many spaces to insert before each line * @param[in] prettyprint insert \n and spaces tomake the xml more readable. * @param[in] fn Callback to make print function - * @param[in] used_for_show_command is the function used for a cli show command. * @see clicon_xml2cbuf * One can use clicon_xml2cbuf to get common code, but using fprintf is * much faster than using cbuf and then printing that,... @@ -114,6 +113,7 @@ xml2file_recurse(FILE *f, int haselement; char *val; char *encstr = NULL; /* xml encoded string */ + if (x == NULL) goto ok; name = xml_name(x); diff --git a/util/clixon_util_datastore.c b/util/clixon_util_datastore.c index 8b8207a9..65e611f2 100644 --- a/util/clixon_util_datastore.c +++ b/util/clixon_util_datastore.c @@ -205,7 +205,7 @@ main(int argc, char **argv) xpath = "/"; if (xmldb_get(h, db, NULL, xpath, &xt) < 0) goto done; - clicon_xml2file(stdout, xt, 0, 0, 0); + clicon_xml2file(stdout, xt, 0, 0); fprintf(stdout, "\n"); if (xt){ xml_free(xt); @@ -228,7 +228,7 @@ main(int argc, char **argv) clicon_err(OE_DB, 0, "xt is NULL"); goto done; } - clicon_xml2file(stdout, xt, 0, 0, 0); + clicon_xml2file(stdout, xt, 0, 0); if (xt){ xml_free(xt); xt = NULL; From 0ffff7844e567c48b19b1ed85a2d9feab2bba200 Mon Sep 17 00:00:00 2001 From: shmuels Date: Wed, 21 Apr 2021 15:25:11 +0300 Subject: [PATCH 7/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index 7e31db58..71caa510 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -142,20 +142,20 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) char *encstr = NULL; /* xml encoded string */ char *opext = NULL; - if (x == NULL) + if (xn == NULL) goto ok; /* Look for autocli-op defined in clixon-lib.yang */ - if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + if (yang_extension_value(xml_spec(xn), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { goto ok; } if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ goto ok; } - name = xml_name(x); - namespace = xml_prefix(x); - switch(xml_type(x)){ + name = xml_name(xn); + namespace = xml_prefix(xn); + switch(xml_type(xn)){ case CX_BODY: - if ((val = xml_value(x)) == NULL) /* incomplete tree */ + if ((val = xml_value(xn)) == NULL) /* incomplete tree */ break; if (xml_chardata_encode(&encstr, "%s", val) < 0) goto done; @@ -165,10 +165,10 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) (*fn)(stdout, " "); if (namespace) (*fn)(stdout, "%s:", namespace); - (*fn)(stdout, "%s=\"%s\"", name, xml_value(x)); + (*fn)(stdout, "%s=\"%s\"", name, xml_value(xn)); break; case CX_ELMNT: - (*fn)(stdout, "%*s<", prettyprint?(level*XML_INDENT):0, ""); + (*fn)(stdout, "%*s<", prettyprint?(level*3):0, ""); if (namespace) (*fn)(stdout, "%s:", namespace); (*fn)(stdout, "%s", name); @@ -176,7 +176,7 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) haselement = 0; xc = NULL; /* print attributes only */ - while ((xc = xml_child_each(x, xc, -1)) != NULL) { + while ((xc = xml_child_each(xn, xc, -1)) != NULL) { switch (xml_type(xc)){ case CX_ATTR: if (xml2file(xc, level+1, prettyprint, fprintf) <0) @@ -202,13 +202,13 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) if (prettyprint && hasbody == 0) (*fn)(stdout, "\n"); xc = NULL; - while ((xc = xml_child_each(x, xc, -1)) != NULL) { + while ((xc = xml_child_each(xn, xc, -1)) != NULL) { if (xml_type(xc) != CX_ATTR) if (xml2file(xc, level+1, prettyprint, fprintf) <0) goto done; } if (prettyprint && hasbody==0) - (*fn)(stdout, "%*s", level*XML_INDENT, ""); + (*fn)(stdout, "%*s", level*3, ""); (*fn)(stdout, " Date: Thu, 22 Apr 2021 19:17:32 +0300 Subject: [PATCH 8/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 188 +++++++++++++++++++++++++++++++++++--- apps/cli/cli_show.c | 18 ++-- apps/cli/clixon_cli_api.h | 4 +- lib/src/clixon_xml_map.c | 16 ---- 4 files changed, 189 insertions(+), 37 deletions(-) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index 71caa510..ad9cf538 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -130,7 +130,7 @@ cvec_append(cvec *cvv0, * One can use clicon_xml2cbuf to get common code, but using fprintf is * much faster than using cbuf and then printing that,... */ -int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) +int cli_xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) { int retval = -1; char *name; @@ -179,7 +179,7 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) while ((xc = xml_child_each(xn, xc, -1)) != NULL) { switch (xml_type(xc)){ case CX_ATTR: - if (xml2file(xc, level+1, prettyprint, fprintf) <0) + if (cli_xml2file(xc, level+1, prettyprint, fn) <0) goto done; break; case CX_BODY: @@ -204,7 +204,7 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) xc = NULL; while ((xc = xml_child_each(xn, xc, -1)) != NULL) { if (xml_type(xc) != CX_ATTR) - if (xml2file(xc, level+1, prettyprint, fprintf) <0) + if (cli_xml2file(xc, level+1, prettyprint, fn) <0) goto done; } if (prettyprint && hasbody==0) @@ -228,6 +228,172 @@ int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn) return retval; } +/*! Translate XML to a "pseudo-code" textual format using a callback - internal function + * @param[in] xn XML object to print + * @param[in] fn Callback to make print function + * @param[in] level print 4 spaces per level in front of each line + */ +int cli_xml2txt(cxobj *xn, clicon_output_cb *fn, int level) { + cxobj *xc = NULL; + int children=0; + int retval = -1; + char *opext = NULL; + + if (xn == NULL || fn == NULL){ + clicon_err(OE_XML, EINVAL, "xn or fn is NULL"); + goto done; + } + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(xn), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } + xc = NULL; /* count children (elements and bodies, not attributes) */ + while ((xc = xml_child_each(xn, xc, -1)) != NULL) + if (xml_type(xc) == CX_ELMNT || xml_type(xc) == CX_BODY) + children++; + if (!children){ /* If no children print line */ + switch (xml_type(xn)){ + case CX_BODY: + (*fn)(stdout, "%s;\n", xml_value(xn)); + break; + case CX_ELMNT: + (*fn)(stdout, "%*s%s;\n", 4*level, "", xml_name(xn)); + break; + default: + break; + } + goto ok; + } + (*fn)(stdout, "%*s", 4*level, ""); + (*fn)(stdout, "%s ", xml_name(xn)); + if (!tleaf(xn)) + (*fn)(stdout, "{\n"); + xc = NULL; + while ((xc = xml_child_each(xn, xc, -1)) != NULL){ + if (xml_type(xc) == CX_ELMNT || xml_type(xc) == CX_BODY) + if (cli_xml2txt(xc, fn, level+1) < 0) + break; + } + if (!tleaf(xn)) + (*fn)(stdout, "%*s}\n", 4*level, ""); + ok: + retval = 0; + done: + return retval; +} + +/*! Translate from XML to CLI commands + * Howto: join strings and pass them down. + * Identify unique/index keywords for correct set syntax. + * @param[in] xn XML Parse-tree (to translate) + * @param[in] prepend Print this text in front of all commands. + * @param[in] gt option to steer cli syntax + * @param[in] fn Callback to make print function + */ +int cli_xml2cli(cxobj *xn, char *prepend, enum genmodel_type gt, clicon_output_cb *fn) { + int retval = -1; + cxobj *xe = NULL; + cbuf *cbpre = NULL; + yang_stmt *ys; + int match; + char *body; + char *opext = NULL; + + if (xml_type(xn)==CX_ATTR) + goto ok; + if ((ys = xml_spec(xn)) == NULL) + goto ok; + /* Look for autocli-op defined in clixon-lib.yang */ + if (yang_extension_value(xml_spec(xn), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { + goto ok; + } + if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ + goto ok; + } + /* If leaf/leaf-list or presence container, then print line */ + if (yang_keyword_get(ys) == Y_LEAF || + yang_keyword_get(ys) == Y_LEAF_LIST){ + if (prepend) + (*fn)(stdout, "%s", prepend); + if (gt == GT_ALL || gt == GT_VARS || gt == GT_HIDE) + (*fn)(stdout, "%s ", xml_name(xn)); + if ((body = xml_body(xn)) != NULL){ + if (index(body, ' ')) + (*fn)(stdout, "\"%s\"", body); + else + (*fn)(stdout, "%s", body); + } + (*fn)(stdout, "\n"); + goto ok; + } + /* Create prepend variable string */ + if ((cbpre = cbuf_new()) == NULL){ + clicon_err(OE_PLUGIN, errno, "cbuf_new"); + goto done; + } + if (prepend) + cprintf(cbpre, "%s", prepend); + + /* If non-presence container && HIDE mode && only child is + * a list, then skip container keyword + * See also yang2cli_container */ + if (yang_container_cli_hide(ys, gt) == 0) + cprintf(cbpre, "%s ", xml_name(xn)); + + /* If list then first loop through keys */ + if (yang_keyword_get(ys) == Y_LIST){ + xe = NULL; + while ((xe = xml_child_each(xn, xe, -1)) != NULL){ + if ((match = yang_key_match(ys, xml_name(xe))) < 0) + goto done; + if (!match) + continue; + if (gt == GT_ALL) + cprintf(cbpre, "%s ", xml_name(xe)); + cprintf(cbpre, "%s ", xml_body(xe)); + } + } + else if ((yang_keyword_get(ys) == Y_CONTAINER) && + yang_find(ys, Y_PRESENCE, NULL) != NULL){ + /* If presence container, then print as leaf (but continue to children) */ + if (prepend) + (*fn)(stdout, "%s", prepend); + if (gt == GT_ALL || gt == GT_VARS || gt == GT_HIDE) + (*fn)(stdout, "%s ", xml_name(xn)); + if ((body = xml_body(xn)) != NULL){ + if (index(body, ' ')) + (*fn)(stdout, "\"%s\"", body); + else + (*fn)(stdout, "%s", body); + } + (*fn)(stdout, "\n"); + } + + /* Then loop through all other (non-keys) */ + xe = NULL; + while ((xe = xml_child_each(xn, xe, -1)) != NULL){ + if (yang_keyword_get(ys) == Y_LIST){ + if ((match = yang_key_match(ys, xml_name(xe))) < 0) + goto done; + if (match){ + (*fn)(stdout, "%s\n", cbuf_get(cbpre)); + continue; /* Not key itself */ + } + } + if (cli_xml2cli(xe, cbuf_get(cbpre), gt, fn) < 0) + goto done; + } + ok: + retval = 0; + done: + if (cbpre) + cbuf_free(cbpre); + return retval; +} + /*! Enter a CLI edit mode * @param[in] h CLICON handle * @param[in] cvv Vector of variables from CLIgen command-line @@ -521,10 +687,10 @@ cli_auto_show(clicon_handle h, switch (format){ case FORMAT_XML: if (isroot) - xml2file(xp, 0, pretty, fprintf); + cli_xml2file(xp, 0, pretty, fprintf); else{ while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - xml2file(xc, 0, pretty, fprintf); + cli_xml2file(xc, 0, pretty, fprintf); } fprintf(stdout, "\n"); break; @@ -539,19 +705,19 @@ cli_auto_show(clicon_handle h, break; case FORMAT_TEXT: if (isroot) - xml2txt_cb(stdout, xp, cligen_output); /* tree-formed text */ + cli_xml2txt(xp, cligen_output, 0); /* tree-formed text */ else while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */ + cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */ break; case FORMAT_CLI: if ((gt = clicon_cli_genmodel_type(h)) == GT_ERR) goto done; if (isroot) - xml2cli_cb(stdout, xp, prefix, gt, cligen_output); /* cli syntax */ + cli_xml2cli(xp, prefix, gt, cligen_output); /* cli syntax */ else while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - xml2cli_cb(stdout, xc, prefix, gt, cligen_output); /* cli syntax */ + cli_xml2cli(xc, prefix, gt, cligen_output); /* cli syntax */ break; case FORMAT_NETCONF: fprintf(stdout, "", @@ -559,10 +725,10 @@ cli_auto_show(clicon_handle h, if (pretty) fprintf(stdout, "\n"); if (isroot) - xml2file(xp, 2, pretty, fprintf); + cli_xml2file(xp, 2, pretty, fprintf); else while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL) - xml2file(xc, 2, pretty, fprintf); + cli_xml2file(xc, 2, pretty, fprintf); fprintf(stdout, "]]>]]>\n"); break; } /* switch */ diff --git a/apps/cli/cli_show.c b/apps/cli/cli_show.c index 9aa39a0e..28947ea4 100644 --- a/apps/cli/cli_show.c +++ b/apps/cli/cli_show.c @@ -476,7 +476,7 @@ cli_show_config1(clicon_handle h, case FORMAT_XML: xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - xml2file(xc, 0, 1, cligen_output); + cli_xml2file(xc, 0, 1, cligen_output); break; case FORMAT_JSON: xml2json_cb(stdout, xt, 1, cligen_output); @@ -484,7 +484,7 @@ cli_show_config1(clicon_handle h, case FORMAT_TEXT: xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */ + cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */ break; case FORMAT_CLI: /* get CLI generatade mode: VARS|ALL */ @@ -492,14 +492,14 @@ cli_show_config1(clicon_handle h, goto done; xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL) - xml2cli_cb(stdout, xc, prefix, gt, cligen_output); /* cli syntax */ + cli_xml2cli(xc, prefix, gt, cligen_output); /* cli syntax */ break; case FORMAT_NETCONF: cligen_output(stdout, "\n", NETCONF_BASE_NAMESPACE); xc = NULL; /* Dont print xt itself */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) - xml2file(xc, 2, 1, cligen_output); + cli_xml2file(xc, 2, 1, cligen_output); cligen_output(stdout, "]]>]]>\n"); break; } @@ -623,7 +623,7 @@ show_conf_xpath(clicon_handle h, if (xpath_vec(xt, nsc, "%s", &xv, &xlen, xpath) < 0) goto done; for (i=0; i\n"); - xml2file(xp, 2, 1, fprintf); + cli_xml2file(xp, 2, 1, fprintf); fprintf(stdout, "]]>]]>\n"); break; default: for (; i < xml_child_nr(xml_parent(xp)) ; ++i, xp_helper = xml_child_i(xml_parent(xp), i)) { switch (format){ case FORMAT_XML: - xml2file(xp_helper, 0, 1, fprintf); + cli_xml2file(xp_helper, 0, 1, fprintf); break; case FORMAT_JSON: xml2json_cb(stdout, xp_helper, 1, cligen_output); break; case FORMAT_TEXT: - xml2txt_cb(stdout, xp_helper, cligen_output); /* tree-formed text */ + cli_xml2txt(xp_helper, cligen_output, 0); /* tree-formed text */ break; default: /* see cli_show_config() */ break; diff --git a/apps/cli/clixon_cli_api.h b/apps/cli/clixon_cli_api.h index 7f2fa55d..457c4840 100644 --- a/apps/cli/clixon_cli_api.h +++ b/apps/cli/clixon_cli_api.h @@ -116,7 +116,9 @@ int cli_help(clicon_handle h, cvec *vars, cvec *argv); /* In cli_show.c */ int expand_dbvar(void *h, char *name, cvec *cvv, cvec *argv, cvec *commands, cvec *helptexts); -int xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn); +int cli_xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn); +int cli_xml2txt(cxobj *xn, clicon_output_cb *fn, int level); +int cli_xml2cli(cxobj *xn, char *prepend, enum genmodel_type gt, clicon_output_cb *fn); /* cli_show.c: CLIgen new vector arg callbacks */ int show_yang(clicon_handle h, cvec *vars, cvec *argv); diff --git a/lib/src/clixon_xml_map.c b/lib/src/clixon_xml_map.c index 5cd31f37..848f4ebf 100644 --- a/lib/src/clixon_xml_map.c +++ b/lib/src/clixon_xml_map.c @@ -144,19 +144,11 @@ xml2txt_recurse(FILE *f, cxobj *xc = NULL; int children=0; int retval = -1; - char *opext = NULL; if (f == NULL || x == NULL || fn == NULL){ clicon_err(OE_XML, EINVAL, "f, x or fn is NULL"); goto done; } - /* Look for autocli-op defined in clixon-lib.yang */ - if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { - goto ok; - } - if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ - goto ok; - } xc = NULL; /* count children (elements and bodies, not attributes) */ while ((xc = xml_child_each(x, xc, -1)) != NULL) if (xml_type(xc) == CX_ELMNT || xml_type(xc) == CX_BODY) @@ -241,19 +233,11 @@ xml2cli_recurse(FILE *f, yang_stmt *ys; int match; char *body; - char *opext = NULL; if (xml_type(x)==CX_ATTR) goto ok; if ((ys = xml_spec(x)) == NULL) goto ok; - /* Look for autocli-op defined in clixon-lib.yang */ - if (yang_extension_value(xml_spec(x), "autocli-op", CLIXON_LIB_NS, &opext) < 0) { - goto ok; - } - if ((opext != NULL) && ((strcmp(opext, "hide-database") == 0) || (strcmp(opext, "hide-database-auto-completion") == 0))){ - goto ok; - } /* If leaf/leaf-list or presence container, then print line */ if (yang_keyword_get(ys) == Y_LEAF || yang_keyword_get(ys) == Y_LEAF_LIST){ From 5d18c02dfeb9e543b3854fb700994686532e705f Mon Sep 17 00:00:00 2001 From: shmuels Date: Thu, 22 Apr 2021 19:22:31 +0300 Subject: [PATCH 9/9] completing the function of hide for autocli-op in the yang files --- apps/cli/cli_auto.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index ad9cf538..14664782 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -120,6 +120,28 @@ cvec_append(cvec *cvv0, return cvv2; } +/*! x is element and has exactly one child which in turn has none + * @see child_type in clixon_json.c + */ +static int +tleaf(cxobj *x) +{ + cxobj *xc; + + if (xml_type(x) != CX_ELMNT) + return 0; + if (xml_child_nr_notype(x, CX_ATTR) != 1) + return 0; + /* From here exactly one noattr child, get it */ + xc = NULL; + while ((xc = xml_child_each(x, xc, -1)) != NULL) + if (xml_type(xc) != CX_ATTR) + break; + if (xc == NULL) + return -1; /* n/a */ + return (xml_child_nr_notype(xc, CX_ATTR) == 0); +} + /*! Print an XML tree structure to an output stream and encode chars "<>&" * * @param[in] xn clicon xml tree