Changed C-API for xml translation/print the internal cxobj tree data structure to other formats

New API is as follows:
  * `clixon_xml2file()` - Print internal tree as XML to file
  * `clixon_xml2cbuf()` - Print internal tree as XML to buffer
  * `clixon_json2file()` - Print internal tree as JSON to file
  * `clixon_json2cbuf()` - Print internal tree as JSON to buffer
  * `clixon_cli2file()` - Print internal tree as CLI format to file
  * `clixon_txt2file()` - Print internal tree as text format to file
This commit is contained in:
Olof hagsand 2022-06-01 20:02:27 +02:00
parent 87719c623c
commit 0c79298e76
49 changed files with 421 additions and 503 deletions

View file

@ -125,115 +125,6 @@ cvec_append(cvec *cvv0,
return cvv2;
}
/*! Print an XML tree structure from an auto-cli env to stdout 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
cli_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 */
int exist = 0;
if (xn == NULL)
goto ok;
if (yang_extension_value(xml_spec(xn), "hide-show", CLIXON_AUTOCLI_NS, &exist, NULL) < 0)
goto done;
if (exist)
goto ok;
name = xml_name(xn);
namespace = xml_prefix(xn);
switch(xml_type(xn)){
case CX_BODY:
if ((val = xml_value(xn)) == 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(xn));
break;
case CX_ELMNT:
(*fn)(stdout, "%*s<", prettyprint?(level*3):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(xn, xc, -1)) != NULL) {
switch (xml_type(xc)){
case CX_ATTR:
if (cli_xml2file(xc, level+1, prettyprint, fn) <0)
goto done;
break;
case CX_BODY:
hasbody=1;
break;
case CX_ELMNT:
haselement=1;
break;
default:
break;
}
}
/* Check for special case <a/> instead of <a></a>:
* 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(xn, xc, -1)) != NULL) {
if (xml_type(xc) != CX_ATTR)
if (cli_xml2file(xc, level+1, prettyprint, fn) <0)
goto done;
}
if (prettyprint && hasbody==0)
(*fn)(stdout, "%*s", level*3, "");
(*fn)(stdout, "</");
if (namespace)
(*fn)(stdout, "%s:", namespace);
(*fn)(stdout, "%s>", 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
@ -473,7 +364,6 @@ cli_auto_show(clicon_handle h,
pt_head *ph;
char *xpath = NULL;
cxobj *xp;
cxobj *xc = NULL;
cvec *nsc = NULL;
yang_stmt *yspec;
cxobj *xerr;
@ -481,7 +371,7 @@ cli_auto_show(clicon_handle h,
cxobj **vec = NULL;
size_t veclen;
int i;
int isroot;
int skiproot;
int pretty;
char *prefix = NULL;
int state;
@ -537,7 +427,7 @@ cli_auto_show(clicon_handle h,
api_path = "/";
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done;
isroot = (xpath == NULL) || strcmp(xpath,"/")==0;
skiproot = (xpath != NULL) && (strcmp(xpath,"/") != 0);
if (state == 0){ /* Get configuration-only from database */
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, &xt) < 0)
goto done;
@ -562,28 +452,21 @@ cli_auto_show(clicon_handle h,
/* Print configuration according to format */
switch (format){
case FORMAT_XML:
if (isroot)
cli_xml2file(xp, 0, pretty, fprintf);
else{
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
cli_xml2file(xc, 0, pretty, fprintf);
}
if (clixon_xml2file(stdout, xp, 0, pretty, fprintf, skiproot) < 0)
goto done;
fprintf(stdout, "\n");
break;
case FORMAT_JSON:
if (xml2json_file(stdout, xp, pretty, cligen_output, !isroot) < 0)
if (clixon_json2file(stdout, xp, pretty, cligen_output, skiproot) < 0)
goto done;
fprintf(stdout, "\n");
break;
case FORMAT_TEXT:
if (isroot)
xml2txt(xp, cligen_output, stdout, 0); /* tree-formed text */
else
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
if (clixon_txt2file(stdout, xp, 0, cligen_output, skiproot) < 0)
goto done;
break;
case FORMAT_CLI:
if (xml2cli(h, stdout, xp, prefix, cligen_output, !isroot) < 0)
if (clixon_cli2file(h, stdout, xp, prefix, cligen_output, skiproot) < 0)
goto done;
break;
case FORMAT_NETCONF:
@ -591,11 +474,8 @@ cli_auto_show(clicon_handle h,
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
if (pretty)
fprintf(stdout, "\n");
if (isroot)
cli_xml2file(xp, 2, pretty, fprintf);
else
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
cli_xml2file(xc, 2, pretty, fprintf);
if (clixon_xml2file(stdout, xp, 2, pretty, fprintf, skiproot) < 0)
goto done;
fprintf(stdout, "</config></edit-config></rpc>]]>]]>\n");
break;
} /* switch */

View file

@ -325,7 +325,7 @@ cli_dbxml(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0)
goto done;
@ -675,16 +675,15 @@ cli_validate(clicon_handle h,
/*! Compare two dbs using XML. Write to file and run diff
*/
static int
compare_xmls(cxobj *xc1,
cxobj *xc2,
int astext)
compare_xmls(cxobj *xc1,
cxobj *xc2,
enum format_enum format)
{
int fd;
FILE *f;
char filename1[MAXPATHLEN];
char filename2[MAXPATHLEN];
int retval = -1;
cxobj *xc;
cbuf *cb = NULL;
snprintf(filename1, sizeof(filename1), "/tmp/cliconXXXXXX");
@ -695,14 +694,17 @@ compare_xmls(cxobj *xc1,
}
if ((f = fdopen(fd, "w")) == NULL)
goto done;
xc = NULL;
if (astext)
while ((xc = xml_child_each(xc1, xc, -1)) != NULL)
xml2txt(xc, cligen_output, f, 0);
else
while ((xc = xml_child_each(xc1, xc, -1)) != NULL)
clicon_xml2file_cb(f, xc, 0, 1, cligen_output);
switch(format){
case FORMAT_TEXT:
if (clixon_txt2file(f, xc1, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc1, 0, 1, cligen_output, 1) < 0)
goto done;
break;
}
fclose(f);
close(fd);
@ -712,13 +714,19 @@ compare_xmls(cxobj *xc1,
}
if ((f = fdopen(fd, "w")) == NULL)
goto done;
xc = NULL;
if (astext)
while ((xc = xml_child_each(xc2, xc, -1)) != NULL)
xml2txt(xc, cligen_output, f, 0);
else
while ((xc = xml_child_each(xc2, xc, -1)) != NULL)
clicon_xml2file_cb(f, xc, 0, 1, cligen_output);
switch(format){
case FORMAT_TEXT:
if (clixon_txt2file(f, xc2, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc2, 0, 1, cligen_output, 1) < 0)
goto done;
break;
}
fclose(f);
close(fd);
@ -754,16 +762,16 @@ compare_dbs(clicon_handle h,
cxobj *xc2 = NULL; /* candidate xml */
cxobj *xerr = NULL;
int retval = -1;
int astext;
enum format_enum format;
if (cvec_len(argv) > 1){
clicon_err(OE_PLUGIN, EINVAL, "Requires 0 or 1 element. If given: astext flag 0|1");
goto done;
}
if (cvec_len(argv))
astext = cv_int32_get(cvec_i(argv, 0));
if (cvec_len(argv) && cv_int32_get(cvec_i(argv, 0)) == 1)
format = FORMAT_TEXT;
else
astext = 0;
format = FORMAT_XML;
if (clicon_rpc_get_config(h, NULL, "running", "/", NULL, &xc1) < 0)
goto done;
if ((xerr = xpath_first(xc1, NULL, "/rpc-error")) != NULL){
@ -776,7 +784,7 @@ compare_dbs(clicon_handle h,
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
if (compare_xmls(xc1, xc2, astext) < 0) /* astext? */
if (compare_xmls(xc1, xc2, format) < 0) /* astext? */
goto done;
retval = 0;
done:
@ -934,7 +942,7 @@ load_config_file(clicon_handle h,
/* Read as datastore-top but transformed into an edit-config "config" */
xml_name_set(x, NETCONF_INPUT_CONFIG);
}
if (clicon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
@ -1034,26 +1042,26 @@ save_config_file(clicon_handle h,
}
switch (format){
case FORMAT_XML:
if (clicon_xml2file(f, xt, 0, pretty) < 0)
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0) < 0)
goto done;
break;
case FORMAT_JSON:
if (xml2json_file(f, xt, pretty, fprintf, 0) < 0)
if (clixon_json2file(f, xt, pretty, fprintf, 0) < 0)
goto done;
break;
case FORMAT_TEXT:
if (xml2txt(xt, fprintf, f, 0) < 0)
if (clixon_txt2file(f, xt, 0, fprintf, 0) < 0)
goto done;
break;
case FORMAT_CLI:
if (xml2cli(h, f, xt, prefix, fprintf, 1) < 0)
if (clixon_cli2file(h, f, xt, prefix, fprintf, 1) < 0)
goto done;
break;
case FORMAT_NETCONF:
fprintf(f, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target>",
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
fprintf(f, "\n");
if (clicon_xml2file(f, xt, 0, pretty) < 0)
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0) < 0)
goto done;
fprintf(f, "</edit-config></rpc>]]>]]>\n");
break;
@ -1135,8 +1143,6 @@ cli_notification_cb(int s,
int eof;
int retval = -1;
cxobj *xt = NULL;
cxobj *xe;
cxobj *x;
enum format_enum format = (enum format_enum)arg;
int ret;
@ -1159,30 +1165,19 @@ cli_notification_cb(int s,
}
switch (format){
case FORMAT_JSON:
if (xml2json_file(stdout, xt, 1, cligen_output, 1) < 0)
if (clixon_json2file(stdout, xt, 1, cligen_output, 1) < 0)
goto done;
case FORMAT_TEXT:
if (clixon_txt2file(stdout, xt, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_XML:
if (clixon_xml2file(stdout, xt, 0, 1, cligen_output, 1) < 0)
goto done;
break;
default:
break;
}
if ((xe = xpath_first(xt, NULL, "//event")) != NULL){
x = NULL;
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)
goto done;
break;
case FORMAT_TEXT:
if (xml2txt(x, cligen_output, stdout, 0) < 0)
goto done;
break;
default:
break;
}
if (cli_output_status() < 0)
break;
}
}
retval = 0;
done:
if (xt)
@ -1424,7 +1419,8 @@ cli_copy_config(clicon_handle h,
/* resuse cb */
cbuf_reset(cb);
/* create xml copy tree and merge it with database configuration */
clicon_xml2cbuf(cb, x2, 0, 0, -1, 0);
if (clixon_xml2cbuf(cb, x2, 0, 0, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0)
goto done;
retval = 0;

View file

@ -882,7 +882,7 @@ yang2cli_container(clicon_handle h,
goto done;
/* If non-presence container && HIDE mode && only child is
* a list, then skip container keyword
* See also xml2cli
* See also clixon_cli2file
*/
if (autocli_compress(h, ys, &compress) < 0)
goto done;

View file

@ -434,7 +434,6 @@ cli_show_config1(clicon_handle h,
cbuf *cbxpath = NULL;
char *val = NULL;
cxobj *xt = NULL;
cxobj *xc;
cxobj *xerr;
yang_stmt *yspec;
char *namespace = NULL;
@ -495,28 +494,26 @@ cli_show_config1(clicon_handle h,
/* Print configuration according to format */
switch (format){
case FORMAT_XML:
xc = NULL; /* Dont print xt itself */
while ((xc = xml_child_each(xt, xc, -1)) != NULL)
cli_xml2file(xc, 0, 1, cligen_output);
if (clixon_xml2file(stdout, xt, 0, 1, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_JSON:
xml2json_file(stdout, xt, 1, cligen_output, 0);
if (clixon_json2file(stdout, xt, 1, cligen_output, 0) < 0)
goto done;
break;
case FORMAT_TEXT:
xc = NULL; /* Dont print xt itself */
while ((xc = xml_child_each(xt, xc, -1)) != NULL)
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
if (clixon_txt2file(stdout, xt, 0, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_CLI:
if (xml2cli(h, stdout, xt, prefix, cligen_output, 1) < 0)
if (clixon_cli2file(h, stdout, xt, prefix, cligen_output, 1) < 0)
goto done;
break;
case FORMAT_NETCONF:
cligen_output(stdout, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target><config>\n",
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
xc = NULL; /* Dont print xt itself */
while ((xc = xml_child_each(xt, xc, -1)) != NULL)
cli_xml2file(xc, 2, 1, cligen_output);
if (clixon_xml2file(stdout, xt, 2, 1, cligen_output, 1) < 0)
goto done;
cligen_output(stdout, "</config></edit-config></rpc>]]>]]>\n");
break;
}
@ -650,7 +647,8 @@ show_conf_xpath(clicon_handle h,
if (xpath_vec(xt, nsc, "%s", &xv, &xlen, xpath) < 0)
goto done;
for (i=0; i<xlen; i++)
cli_xml2file(xv[i], 0, 1, fprintf);
if (clixon_xml2file(stdout, xv[i], 0, 1, fprintf, 0) < 0)
goto done;
retval = 0;
done:
@ -671,7 +669,7 @@ int cli_show_version(clicon_handle h,
return 0;
}
/*! Generic show configuration CLIgen callback using generated CLI syntax
/*! Generic show configuration CLIgen callback using auto CLI syntax
*
* This callback can be used only in context of an autocli generated syntax tree, such as:
* show @datamodel, cli_show_auto();
@ -690,10 +688,10 @@ int cli_show_version(clicon_handle h,
* @see cli_show_config1
*/
static int
cli_show_generated(clicon_handle h,
int state,
cvec *cvv,
cvec *argv)
cli_show_auto1(clicon_handle h,
int state,
cvec *cvv,
cvec *argv)
{
int retval = 1;
yang_stmt *yspec;
@ -705,12 +703,9 @@ cli_show_generated(clicon_handle h,
enum format_enum format = FORMAT_XML;
cxobj *xt = NULL;
cxobj *xp;
cxobj *xp_helper;
cxobj *xerr;
char *api_path = NULL;
char *prefix = NULL;
enum rfc_6020 ys_keyword;
int i = 0;
int cvvi = 0;
if (cvec_len(argv) < 3 || cvec_len(argv) > 4){
@ -763,40 +758,28 @@ cli_show_generated(clicon_handle h,
}
if ((xp = xpath_first(xt, nsc, "%s", xpath)) != NULL){
/* Print configuration according to format */
ys_keyword = yang_keyword_get(xml_spec(xp));
if (ys_keyword == Y_LIST)
xp_helper = xml_child_i(xml_parent(xp), i);
else
xp_helper = xp;
switch (format){
case FORMAT_CLI:
if (xml2cli(h, stdout, xp, prefix, cligen_output, 0) < 0) /* cli syntax */
if (clixon_cli2file(h, stdout, xp, prefix, cligen_output, 0) < 0) /* cli syntax */
goto done;
break;
case FORMAT_NETCONF:
fprintf(stdout, "<rpc><edit-config><target><candidate/></target><config>\n");
cli_xml2file(xp, 2, 1, fprintf);
if (clixon_xml2file(stdout, xp, 2, 1, fprintf, 0) < 0)
goto done;
fprintf(stdout, "</config></edit-config></rpc>]]>]]>\n");
break;
case FORMAT_JSON:
xml2json_file(stdout, xp_helper, 1, cligen_output, 1);
if (clixon_json2file(stdout, xp, 1, cligen_output, 1) < 0) // XXX helper?
goto done;
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:
cli_xml2file(xp_helper, 0, 1, fprintf);
break;
case FORMAT_TEXT:
xml2txt(xp_helper, cligen_output, stdout, 0); /* tree-formed text */
break;
default: /* see cli_show_config() */
break;
}
if (ys_keyword != Y_LIST)
break;
}
case FORMAT_TEXT:
if (clixon_txt2file(stdout, xp, 0, cligen_output, 1) < 0) // XXX helper?
goto done;
break;
case FORMAT_XML:
if (clixon_xml2file(stdout, xp, 0, 1, fprintf, 0) < 0) // XXX helper?
goto done;
break;
}
}
@ -828,7 +811,7 @@ cli_show_auto(clicon_handle h,
cvec *cvv,
cvec *argv)
{
return cli_show_generated(h, 0, cvv, argv);
return cli_show_auto1(h, 0, cvv, argv);
}
/*! Generic show config and state CLIgen callback using generated CLI syntax
@ -845,7 +828,7 @@ cli_show_auto_state(clicon_handle h,
cvec *cvv,
cvec *argv)
{
return cli_show_generated(h, 1, cvv, argv);
return cli_show_auto1(h, 1, cvv, argv);
}
/*! Show clixon configuration options as loaded
@ -980,17 +963,21 @@ cli_pagination(clicon_handle h,
xc = xvec[j];
switch (format){
case FORMAT_XML:
clicon_xml2file_cb(stdout, xc, 0, 1, cligen_output);
if (clixon_xml2file(stdout, xc, 0, 1, cligen_output, 0) < 0)
goto done;
break;
case FORMAT_JSON:
xml2json_file(stdout, xc, 1, cligen_output, 0);
if (clixon_json2file(stdout, xc, 1, cligen_output, 0) < 0)
goto done;
break;
case FORMAT_TEXT:
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
if (clixon_txt2file(stdout, xc, 0, cligen_output, 0) < 0)
goto done;
break;
case FORMAT_CLI:
/* hardcoded to compress and list-keyword = nokey */
xml2cli(h, stdout, xc, NULL, cligen_output, 0);
if (clixon_cli2file(h, stdout, xc, NULL, cligen_output, 0) < 0)
goto done;
break;
default:
break;
@ -1155,21 +1142,24 @@ xml2cli1(clicon_handle h,
* @param[in] f Output FILE (eg stdout)
* @param[in] xn XML Parse-tree (to translate)
* @param[in] prepend Print this text in front of all commands.
* @param[in] fn Callback to make print function
* @param[in] fn File print function (if NULL, use fprintf)
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @retval 0 OK
* @retval -1 Error
*/
int
xml2cli(clicon_handle h,
FILE *f,
cxobj *xn,
char *prepend,
clicon_output_cb *fn,
int skiptop)
clixon_cli2file(clicon_handle h,
FILE *f,
cxobj *xn,
char *prepend,
clicon_output_cb *fn,
int skiptop)
{
int retval = 1;
cxobj *xc;
if (fn == NULL)
fn = fprintf;
if (skiptop){
xc = NULL;
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)

View file

@ -135,8 +135,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 cli_xml2file (cxobj *xn, int level, int prettyprint, clicon_output_cb *fn);
int xml2cli(clicon_handle h, FILE *f, cxobj *xn, char *prepend, clicon_output_cb *fn, int skiptop);
int clixon_cli2file(clicon_handle h, FILE *f, cxobj *xn, char *prepend, clicon_output_cb *fn, int skiptop);
/* cli_show.c: CLIgen new vector arg callbacks */
int show_yang(clicon_handle h, cvec *vars, cvec *argv);