C-API: clixon_xml2file and clixon_xml2cbuf added prefix argument

Added in-mem xml diff function: `xml_tree_diff_print`
This commit is contained in:
Olof hagsand 2023-04-12 09:54:37 +02:00
parent 819a0b5a9e
commit 6335f810d3
43 changed files with 309 additions and 149 deletions

View file

@ -50,6 +50,7 @@ Users may have to change how they access the system
* New `clixon-config@2023-03-01.yang` revision
* Added options: `CLICON_RESTCONF_NOALPN_DEFAULT`
* Extended datastore-format with CLI and text
* New `clixon-lib@2023-03-01.yang` revision
* Added creator meta-object
@ -57,6 +58,11 @@ Users may have to change how they access the system
Developers may need to change their code
* C-API
* `clixon_xml2file` and `clixon_xml2cbuf` added `prefix` argument
* Example application is to add "+"/"-" for diffs
* Example change:
* `clixon_xml2file(f,x,p,f,s,a)` -> `clixon_xml2file(f,x,p,NULL,f,s,a)`
* `clixon_xml2cbuf(c,x,l,p,d,s)` -> `clixon_xml2cbuf(c,x,l,p,NULL,d,s)`
* `xmldb_validate` is removed. Yang checks should be enough, remnant of time before YANG checks.
* `xml_diff`: removed 1st `yspec` parameter
* `xml2xpath()`: Added `int apostrophe` as 4th parameter, default 0

View file

@ -491,7 +491,7 @@ from_client_edit_config(clicon_handle h,
if ((ret = xml_bind_yang(h, xc, YB_MODULE, yspec, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -499,7 +499,7 @@ from_client_edit_config(clicon_handle h,
if ((ret = xml_non_config_data(xc, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -516,7 +516,7 @@ from_client_edit_config(clicon_handle h,
if (ret==1 && (ret = xml_yang_validate_list_key_only(xc, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -1529,7 +1529,7 @@ from_client_msg(clicon_handle h,
goto reply;
}
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto reply;
}
@ -1609,7 +1609,7 @@ from_client_msg(clicon_handle h,
goto done;
}
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
ce->ce_in_bad_rpcs++;
netconf_monitoring_counter_inc(h, "in-bad-rpcs");

View file

@ -193,7 +193,7 @@ startup_common(clicon_handle h,
* See similar clause below
*/
}
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -262,7 +262,7 @@ startup_common(clicon_handle h,
if ((ret = xml_bind_yang(h, xt, YB_MODULE, yspec, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -270,7 +270,7 @@ startup_common(clicon_handle h,
if ((ret = xml_non_config_data(xt, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -305,7 +305,7 @@ startup_common(clicon_handle h,
if ((ret = generic_validate(h, yspec, td, &xret)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail; /* STARTUP_INVALID */
}
@ -612,7 +612,7 @@ candidate_validate(clicon_handle h,
clicon_err(OE_CFG, EINVAL, "xret is NULL");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (!cbuf_len(cbret) &&
netconf_operation_failed(cbret, "application", clicon_err_reason)< 0)
@ -702,7 +702,7 @@ candidate_commit(clicon_handle h,
goto done;
}
if (ret == 0){
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -969,7 +969,7 @@ from_client_restart_one(clicon_handle h,
if ((ret = xml_yang_validate_all_top(h, td->td_target, &xerr)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -1018,7 +1018,7 @@ from_client_restart_one(clicon_handle h,
if ((ret = generic_validate(h, yspec, td, &xerr)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}

View file

@ -478,7 +478,7 @@ get_nacm_and_reply(clicon_handle h,
if (xml_name_set(xret, NETCONF_OUTPUT_DATA) < 0)
goto done;
/* Top level is data, so add 1 to depth if significant */
if (clixon_xml2cbuf(cbret, xret, 0, 0, depth>0?depth+1:depth, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, depth>0?depth+1:depth, 0) < 0)
goto done;
}
cprintf(cbret, "</rpc-reply>");
@ -755,7 +755,7 @@ get_list_pagination(clicon_handle h,
clicon_err_reason);
if (netconf_operation_failed_xml(&xerr, "application", cbuf_get(cberr)) < 0)
goto done;
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -769,7 +769,7 @@ get_list_pagination(clicon_handle h,
". Internal error, state callback returned invalid XML",
NULL) < 0)
goto done;
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -1001,7 +1001,7 @@ get_common(clicon_handle h,
if ((ret = get_statedata(h, xpath?xpath:"/", nsc, wdef, &xret)) < 0)
goto done;
if (ret == 0){ /* Error from callback (error in xret) */
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}
@ -1024,7 +1024,7 @@ get_common(clicon_handle h,
". Internal error, state callback returned invalid XML",
NULL) < 0)
goto done;
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
goto ok;
}

View file

@ -163,7 +163,7 @@ restconf_pseudo_set_inline(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xrestconf, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xrestconf, 0, 0, NULL, -1, 0) < 0)
goto done;
if ((str = strdup(cbuf_get(cb))) == NULL){
clicon_err(OE_XML, errno, "stdup");

View file

@ -263,7 +263,7 @@ transaction_dbg(clicon_handle h,
}
for (i=0; i<td->td_dlen; i++){
xn = td->td_dvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)
@ -272,7 +272,7 @@ transaction_dbg(clicon_handle h,
cbuf_reset(cb);
for (i=0; i<td->td_alen; i++){
xn = td->td_avec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)
@ -282,11 +282,11 @@ transaction_dbg(clicon_handle h,
for (i=0; i<td->td_clen; i++){
if (td->td_scvec){
xn = td->td_scvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
xn = td->td_tcvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)
@ -319,7 +319,7 @@ transaction_log(clicon_handle h,
}
for (i=0; i<td->td_dlen; i++){
xn = td->td_dvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)
@ -328,7 +328,7 @@ transaction_log(clicon_handle h,
cbuf_reset(cb);
for (i=0; i<td->td_alen; i++){
xn = td->td_avec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)
@ -337,11 +337,11 @@ transaction_log(clicon_handle h,
for (i=0; i<td->td_clen; i++){
if (td->td_scvec){
xn = td->td_scvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
xn = td->td_tcvec[i];
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
}
if (i)

View file

@ -472,7 +472,7 @@ cli_dbxml(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0)
goto done;
@ -865,7 +865,7 @@ compare_db_names(clicon_handle h,
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
if (clixon_compare_xmls(xc1, xc2, format, cligen_output) < 0) /* astext? */
if (clixon_compare_xmls(xc1, xc2, format) < 0) /* astext? */
goto done;
retval = 0;
done:
@ -1055,7 +1055,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 (clixon_xml2cbuf(cbxml, xt, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbxml, xt, 0, 0, NULL, -1, 1) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
@ -1158,7 +1158,7 @@ save_config_file(clicon_handle h,
}
switch (format){
case FORMAT_XML:
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0, 1) < 0)
if (clixon_xml2file(f, xt, 0, pretty, NULL, fprintf, 0, 1) < 0)
goto done;
break;
case FORMAT_JSON:
@ -1177,7 +1177,7 @@ save_config_file(clicon_handle h,
fprintf(f, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target>",
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
fprintf(f, "\n");
if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0, 1) < 0)
if (clixon_xml2file(f, xt, 0, pretty, NULL, fprintf, 0, 1) < 0)
goto done;
fprintf(f, "</edit-config></rpc>]]>]]>\n");
break;
@ -1288,7 +1288,7 @@ cli_notification_cb(int s,
goto done;
break;
case FORMAT_XML:
if (clixon_xml2file(stdout, xt, 0, 1, cligen_output, 1, 1) < 0)
if (clixon_xml2file(stdout, xt, 0, 1, NULL, cligen_output, 1, 1) < 0)
goto done;
break;
default:
@ -1535,7 +1535,7 @@ cli_copy_config(clicon_handle h,
/* resuse cb */
cbuf_reset(cb);
/* create xml copy tree and merge it with database configuration */
if (clixon_xml2cbuf(cb, x2, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, x2, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0)
goto done;

View file

@ -511,7 +511,7 @@ cli_show_common(clicon_handle h,
/* Print configuration according to format */
switch (format){
case FORMAT_XML:
if (clixon_xml2file(stdout, xp, 0, pretty, cligen_output, skiptop, 1) < 0)
if (clixon_xml2file(stdout, xp, 0, pretty, NULL, cligen_output, skiptop, 1) < 0)
goto done;
if (!pretty && i == veclen-1)
cligen_output(stdout, "\n");
@ -531,7 +531,7 @@ cli_show_common(clicon_handle h,
if (pretty)
cligen_output(stdout, "\n");
}
if (clixon_xml2file(stdout, xp, 2, pretty, cligen_output, skiptop, 1) < 0)
if (clixon_xml2file(stdout, xp, 2, pretty, NULL, cligen_output, skiptop, 1) < 0)
goto done;
if (i == veclen-1)
cligen_output(stdout, "</config></edit-config></rpc>]]>]]>\n");
@ -1219,7 +1219,7 @@ cli_pagination(clicon_handle h,
xc = xvec[j];
switch (format){
case FORMAT_XML:
if (clixon_xml2file(stdout, xc, 0, 1, cligen_output, 0, 1) < 0)
if (clixon_xml2file(stdout, xc, 0, 1, NULL, cligen_output, 0, 1) < 0)
goto done;
break;
case FORMAT_JSON:

View file

@ -220,7 +220,7 @@ netconf_rpc_message(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;
@ -241,7 +241,7 @@ netconf_rpc_message(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;
@ -262,7 +262,7 @@ netconf_rpc_message(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;
@ -278,7 +278,7 @@ netconf_rpc_message(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xml_child_i(xret,0), 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xml_child_i(xret,0), 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;
@ -335,7 +335,7 @@ netconf_input_packet(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;
@ -451,7 +451,7 @@ netconf_input_frame(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
if (netconf_output_encap(framing, cbret) < 0)
goto done;

View file

@ -479,7 +479,7 @@ netconf_notification_cb(int s,
clicon_err(OE_PLUGIN, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xn, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, 0, NULL, -1, 0) < 0)
goto done;
/* Send it to listening client on stdout */
if (netconf_output_encap(clicon_option_int(h, "netconf-framing"), cb) < 0){
@ -657,7 +657,7 @@ netconf_application_rpc(clicon_handle h,
if (ret > 0 && (ret = xml_yang_validate_add(h, xoutput, &xerr)) < 0)
goto done;
if (ret == 0){
if (clixon_xml2cbuf(cbret, xerr, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xerr, 0, 0, NULL, -1, 0) < 0)
goto done;
clicon_log(LOG_WARNING, "Errors in output netconf %s", cbuf_get(cbret));
goto ok;

View file

@ -278,13 +278,13 @@ api_return_err(clicon_handle h,
clicon_debug(1, "%s code:%d", __FUNCTION__, code);
if (pretty){
cprintf(cb, " <errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">\n");
if (clixon_xml2cbuf(cb, xerr, 2, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xerr, 2, pretty, NULL, -1, 0) < 0)
goto done;
cprintf(cb, " </errors>\r\n");
}
else {
cprintf(cb, "<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">");
if (clixon_xml2cbuf(cb, xerr, 2, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xerr, 2, pretty, NULL, -1, 0) < 0)
goto done;
cprintf(cb, "</errors>\r\n");
}

View file

@ -532,7 +532,7 @@ api_data_write(clicon_handle h,
CLIXON_LIB_PREFIX, CLIXON_LIB_PREFIX, CLIXON_LIB_NS);
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clixon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbx, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
clicon_debug(1, "%s xml: %s api_path:%s",__FUNCTION__, cbuf_get(cbx), api_path);
@ -782,7 +782,7 @@ api_data_delete(clicon_handle h,
CLIXON_LIB_PREFIX, CLIXON_LIB_PREFIX, CLIXON_LIB_NS);
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clixon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbx, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
if (clicon_rpc_netconf(h, cbuf_get(cbx), &xret, NULL) < 0)

View file

@ -231,7 +231,7 @@ api_data_get2(clicon_handle h,
if (xpath==NULL || strcmp(xpath,"/")==0){ /* Special case: data root */
switch (media_out){
case YANG_DATA_XML:
if (clixon_xml2cbuf(cbx, xret, 0, pretty, -1, 0) < 0) /* Dont print top object? */
if (clixon_xml2cbuf(cbx, xret, 0, pretty, NULL, -1, 0) < 0) /* Dont print top object? */
goto done;
break;
case YANG_DATA_JSON:
@ -275,7 +275,7 @@ api_data_get2(clicon_handle h,
cvec_free(nscd);
nscd = NULL;
}
if (clixon_xml2cbuf(cbx, x, 0, pretty, -1, 0) < 0) /* Dont print top object? */
if (clixon_xml2cbuf(cbx, x, 0, pretty, NULL, -1, 0) < 0) /* Dont print top object? */
goto done;
}
break;
@ -541,7 +541,7 @@ api_data_pagination(clicon_handle h,
if (xml_insert(xpr, xp, INS_LAST, NULL, NULL) < 0)
goto done;
}
if (clixon_xml2cbuf(cbx, xpr, 0, pretty, -1, 0) < 0) /* Dont print top object? */
if (clixon_xml2cbuf(cbx, xpr, 0, pretty, NULL, -1, 0) < 0) /* Dont print top object? */
goto done;
break;
case YANG_DATA_JSON:

View file

@ -362,7 +362,7 @@ api_data_post(clicon_handle h,
CLIXON_LIB_PREFIX, CLIXON_LIB_PREFIX, CLIXON_LIB_NS);
cprintf(cbx, "><target><candidate /></target>");
cprintf(cbx, "<default-operation>none</default-operation>");
if (clixon_xml2cbuf(cbx, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbx, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
cprintf(cbx, "</edit-config></rpc>");
clicon_debug(1, "%s xml: %s api_path:%s",__FUNCTION__, cbuf_get(cbx), api_path);
@ -871,7 +871,7 @@ api_operations_post(clicon_handle h,
cbuf_reset(cbret);
switch (media_out){
case YANG_DATA_XML:
if (clixon_xml2cbuf(cbret, xoutput, 0, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xoutput, 0, pretty, NULL, -1, 0) < 0)
goto done;
/* xoutput should now look: <output xmlns="uri"><x>0</x></output> */
break;

View file

@ -204,7 +204,7 @@ api_root_restconf_exact(clicon_handle h,
switch (media_out){
case YANG_DATA_XML:
case YANG_PATCH_XML:
if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xt, 0, pretty, NULL, -1, 0) < 0)
goto done;
break;
case YANG_DATA_JSON:
@ -288,7 +288,7 @@ api_yang_library_version(clicon_handle h,
switch (media_out){
case YANG_DATA_XML:
case YANG_PATCH_XML:
if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xt, 0, pretty, NULL, -1, 0) < 0)
goto done;
break;
case YANG_DATA_JSON:

View file

@ -254,7 +254,7 @@ restconf_stream_cb(int s,
FCGX_FPrintF(r->out, "M#id: %02d:0\r\n", tv.tv_sec);
}
#endif
if (clixon_xml2cbuf(cb, xn, 0, pretty, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xn, 0, pretty, NULL, -1, 0) < 0)
goto done;
FCGX_FPrintF(r->out, "data: %s\r\n", cbuf_get(cb));
FCGX_FPrintF(r->out, "\r\n");

View file

@ -430,7 +430,7 @@ snmp_scalar_set(clicon_handle h,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", op, cbuf_get(cb)) < 0)
goto done;
@ -500,7 +500,7 @@ snmp_cache_row_op(clicon_handle h,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xtop, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0)
goto done;

View file

@ -373,7 +373,7 @@ example_rpc(clicon_handle h, /* Clicon handle */
if (xmlns_set(x, NULL, namespace) < 0)
goto done;
}
if (clixon_xml2cbuf(cbret, xe, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbret, xe, 0, 0, NULL, -1, 1) < 0)
goto done;
}
cprintf(cbret, "</rpc-reply>");

View file

@ -74,7 +74,7 @@ mycallback(clicon_handle h, cvec *cvv, cvec *argv)
nsc, NULL,
&xret) < 0)
goto done;
if (clixon_xml2file(stdout, xret, 0, 1, cligen_output, 0, 1) < 0)
if (clixon_xml2file(stdout, xret, 0, 1, NULL, cligen_output, 0, 1) < 0)
goto done;
retval = 0;
done:
@ -119,7 +119,7 @@ example_client_rpc(clicon_handle h,
goto done;
}
/* Print result */
if (clixon_xml2file(stdout, xml_child_i(xret, 0), 0, 0, cligen_output, 0, 1) < 0)
if (clixon_xml2file(stdout, xml_child_i(xret, 0), 0, 0, NULL, cligen_output, 0, 1) < 0)
goto done;
fprintf(stdout,"\n");

View file

@ -90,7 +90,7 @@ netconf_client_rpc(clicon_handle h,
if (xmlns_set(x, NULL, namespace) < 0)
goto done;
}
if (clixon_xml2cbuf(cbret, xe, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbret, xe, 0, 0, NULL, -1, 1) < 0)
goto done;
}
cprintf(cbret, "</rpc-reply>");

View file

@ -330,7 +330,7 @@ restconf_client_rpc(clicon_handle h,
if (xmlns_set(x, NULL, namespace) < 0)
goto done;
}
if (clixon_xml2cbuf(cbret, xe, 0, 0, -1, 1) < 0)
if (clixon_xml2cbuf(cbret, xe, 0, 0, NULL, -1, 1) < 0)
goto done;
}
cprintf(cbret, "</rpc-reply>");

View file

@ -43,10 +43,10 @@
/*
* Prototypes
*/
int clixon_xml2file(FILE *f, cxobj *xn, int level, int pretty, clicon_output_cb *fn, int skiptop, int autocliext);
int clixon_xml2file(FILE *f, cxobj *xn, int level, int pretty, char *prefix, clicon_output_cb *fn, int skiptop, int autocliext);
int xml_print(FILE *f, cxobj *xn);
int xml_dump(FILE *f, cxobj *x);
int clixon_xml2cbuf(cbuf *cb, cxobj *x, int level, int prettyprint, int32_t depth, int skiptop);
int clixon_xml2cbuf(cbuf *cb, cxobj *x, int level, int prettyprint, char *prefix, int32_t depth, int skiptop);
int xmltree2cbuf(cbuf *cb, cxobj *x, int level);
int clixon_xml_parse_file(FILE *f, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xerr);
int clixon_xml_parse_string(const char *str, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xerr);

View file

@ -76,6 +76,7 @@ int yang_xml_mandatory(cxobj *xt, yang_stmt *ys);
int xml_rpc_isaction(cxobj *xn);
int xml_find_action(cxobj *xn, int top, cxobj **xap);
int purge_tagged_nodes(cxobj *xn, char *ns, char *name, char *value, int keepnode);
int clixon_compare_xmls(cxobj *xc1, cxobj *xc2, enum format_enum format, clicon_output_cb *fn);
int clixon_compare_xmls(cxobj *xc1, cxobj *xc2, enum format_enum format);
int xml_tree_diff_print(cbuf *cb, int level, cxobj *x0, cxobj *x1, enum format_enum format);
#endif /* _CLIXON_XML_MAP_H_ */

View file

@ -797,7 +797,7 @@ xmldb_get_nocache(clicon_handle h,
clicon_log(LOG_NOTICE, "%s: sort verify failed #2", __FUNCTION__);
#endif
if (clicon_debug_get()>1)
if (clixon_xml2file(stderr, xt, 0, 1, fprintf, 0, 0) < 0)
if (clixon_xml2file(stderr, xt, 0, 1, NULL, fprintf, 0, 0) < 0)
goto done;
*xtop = xt;
xt = NULL;
@ -1143,7 +1143,7 @@ xmldb_get_zerocopy(clicon_handle h,
goto done;
}
if (clicon_debug_get() > 1)
if (clixon_xml2file(stderr, x0t, 0, 1, fprintf, 0, 0) < 0)
if (clixon_xml2file(stderr, x0t, 0, 1, NULL, fprintf, 0, 0) < 0)
goto done;
*xtop = x0t;
retval = 1;

View file

@ -1328,7 +1328,7 @@ xmldb_put(clicon_handle h,
if (clixon_json2file(f, x0, pretty, fprintf, 0, 0) < 0)
goto done;
}
else if (clixon_xml2file(f, x0, 0, pretty, fprintf, 0, 0) < 0)
else if (clixon_xml2file(f, x0, 0, pretty, NULL, fprintf, 0, 0) < 0)
goto done;
/* Remove modules state after writing to file
*/
@ -1386,7 +1386,7 @@ xmldb_dump(clicon_handle h,
if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0)
goto done;
}
else if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0, 0) < 0)
else if (clixon_xml2file(f, xt, 0, pretty, NULL, fprintf, 0, 0) < 0)
goto done;
retval = 0;
done:

View file

@ -212,7 +212,7 @@ netconf_invalid_value(cbuf *cb,
if (netconf_invalid_value_xml(&xret, type, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -332,7 +332,7 @@ netconf_missing_attribute(cbuf *cb,
if (netconf_missing_attribute_xml(&xret, type, attr, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -361,7 +361,7 @@ netconf_bad_attribute(cbuf *cb,
if (netconf_bad_attribute_xml(&xret, type, info, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -538,7 +538,7 @@ netconf_missing_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "missing-element",
"bad-element", element, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -584,7 +584,7 @@ netconf_bad_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "bad-element",
"bad-element",element, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -622,7 +622,7 @@ netconf_unknown_element(cbuf *cb,
if (netconf_common_xml(&xret, type, "unknown-element",
"bad-element", element, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -669,7 +669,7 @@ netconf_unknown_namespace(cbuf *cb,
if (netconf_common_xml(&xret, type, "unknown-namespace",
"bad-namespace", ns, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -707,7 +707,7 @@ netconf_access_denied(cbuf *cb,
if (netconf_access_denied_xml(&xret, type, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -948,7 +948,7 @@ netconf_data_missing(cbuf *cb,
if (netconf_data_missing_xml(&xret, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -1153,7 +1153,7 @@ netconf_operation_not_supported(cbuf *cb,
if (netconf_operation_not_supported_xml(&xret, type, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -1181,7 +1181,7 @@ netconf_operation_failed(cbuf *cb,
if (netconf_operation_failed_xml(&xret, type, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -1266,7 +1266,7 @@ netconf_malformed_message(cbuf *cb,
if (netconf_malformed_message_xml(&xret, message) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -1351,7 +1351,7 @@ netconf_data_not_unique(cbuf *cb,
if (netconf_data_not_unique_xml(&xret, x, cvk) < 0)
goto done;
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
retval = 0;
done:
@ -1704,7 +1704,7 @@ netconf_err2cb(cxobj *xerr,
cprintf(cberr, "%s ", xml_body(x));
if ((x=xpath_first(xerr, NULL, "//error-info")) != NULL &&
xml_child_nr(x) > 0){
if (clixon_xml2cbuf(cberr, xml_child_i(x, 0), 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cberr, xml_child_i(x, 0), 0, 0, NULL, -1, 0) < 0)
goto done;
}
if ((x=xpath_first(xerr, NULL, "//error-app-tag"))!=NULL)
@ -2160,7 +2160,7 @@ netconf_output(int s,
{
cxobj *xt = NULL;
if (clixon_xml_parse_string(buf, YB_NONE, NULL, &xt, NULL) == 0){
if (clixon_xml2file(stderr, xml_child_i(xt, 0), 0, 0, fprintf, 0, 0) < 0)
if (clixon_xml2file(stderr, xml_child_i(xt, 0), 0, 0, NULL, fprintf, 0, 0) < 0)
goto done;
fprintf(stderr, "\n");
xml_free(xt);

View file

@ -805,7 +805,7 @@ send_msg_notify_xml(clicon_handle h,
clicon_err(OE_PLUGIN, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, xev, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xev, 0, 0, NULL, -1, 0) < 0)
goto done;
if (send_msg_notify(s, cbuf_get(cb)) < 0)
goto done;

View file

@ -450,7 +450,7 @@ clicon_rpc_netconf_xml(clicon_handle h,
goto done;
}
rpcname = xml_name(xname); /* Store rpc name and use in yang binding after reply */
if (clixon_xml2cbuf(cb, xml, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xml, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_netconf(h, cbuf_get(cb), xret, sp) < 0)
goto done;
@ -502,10 +502,6 @@ clicon_rpc_netconf_xml(clicon_handle h,
* err;
* if (clicon_rpc_get_config(h, NULL, "running", "/hello/world", nsc, "explicit", &xt) < 0)
* err;
* if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
* clixon_netconf_error(xerr, "msg", "/hello/world");
* err;
* }
* if (xt)
* xml_free(xt);
* if (nsc)

View file

@ -991,7 +991,7 @@ stream_publish_cb(clicon_handle h,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(d, event, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(d, event, 0, 0, NULL, -1, 0) < 0)
goto done;
if (url_post(cbuf_get(u), /* url+stream */
cbuf_get(d), /* postfields */

View file

@ -1439,7 +1439,7 @@ rpc_reply_check(clicon_handle h,
if (ret == 0){
clicon_debug(1, "%s failure when validating:%s", __FUNCTION__, cbuf_get(cbret));
cbuf_reset(cbret);
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}
@ -1448,7 +1448,7 @@ rpc_reply_check(clicon_handle h,
if (ret == 0){
clicon_debug(1, "%s failure when validating:%s", __FUNCTION__, cbuf_get(cbret));
cbuf_reset(cbret);
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cbret, xret, 0, 0, NULL, -1, 0) < 0)
goto done;
goto fail;
}

View file

@ -2466,7 +2466,7 @@ clicon_log_xml(int level,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, x, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, x, 0, 0, NULL, -1, 0) < 0)
goto done;
/* first round: compute length of debug message */
va_start(args, format);
@ -2532,7 +2532,7 @@ clicon_debug_xml(int dbglevel,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clixon_xml2cbuf(cb, x, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, x, 0, 0, NULL, -1, 0) < 0)
goto done;
/* first round: compute length of debug message */
va_start(args, format);

View file

@ -90,6 +90,7 @@
* @param[in] xn Clicon xml tree
* @param[in] level How many spaces to insert before each line
* @param[in] pretty Insert \n and spaces to make the xml more readable.
* @param[in] prefix Add string to beginning of each line (if pretty)
* @param[in] fn Callback to make print function
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @retval 0 OK
@ -104,22 +105,27 @@ xml2file_recurse(FILE *f,
cxobj *x,
int level,
int pretty,
char *prefix,
clicon_output_cb *fn,
int autocliext)
{
int retval = -1;
char *name;
char *namespace;
cxobj *xc;
int hasbody;
int haselement;
char *val;
char *encstr = NULL; /* xml encoded string */
int exist = 0;
int retval = -1;
char *name;
char *namespace;
cxobj *xc;
int hasbody;
int haselement;
char *val;
char *encstr = NULL; /* xml encoded string */
int exist = 0;
yang_stmt *y;
int level1;
if (x == NULL)
goto ok;
level1 = level*PRETTYPRINT_INDENT;
if (prefix)
level1 -= strlen(prefix);
if (autocliext &&
(y = xml_spec(x)) != NULL){
if (yang_extension_value(y, "hide-show", CLIXON_AUTOCLI_NS, &exist, NULL) < 0)
@ -144,7 +150,9 @@ xml2file_recurse(FILE *f,
(*fn)(f, "%s=\"%s\"", name, xml_value(x));
break;
case CX_ELMNT:
(*fn)(f, "%*s<", pretty?(level*PRETTYPRINT_INDENT):0, "");
if (pretty && prefix)
(*fn)(f, "%s", prefix);
(*fn)(f, "%*s<", pretty?level1:0, "");
if (namespace)
(*fn)(f, "%s:", namespace);
(*fn)(f, "%s", name);
@ -155,7 +163,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, pretty, fn, autocliext) <0)
if (xml2file_recurse(f, xc, level+1, pretty, prefix, fn, autocliext) <0)
goto done;
break;
case CX_BODY:
@ -175,23 +183,28 @@ xml2file_recurse(FILE *f,
(*fn)(f, "/>");
else{
(*fn)(f, ">");
if (pretty && hasbody == 0)
if (pretty && hasbody == 0){
(*fn)(f, "\n");
}
xc = NULL;
while ((xc = xml_child_each(x, xc, -1)) != NULL) {
if (xml_type(xc) != CX_ATTR)
if (xml2file_recurse(f, xc, level+1, pretty, fn, autocliext) <0)
if (xml2file_recurse(f, xc, level+1, pretty, prefix, fn, autocliext) <0)
goto done;
}
if (pretty && hasbody==0)
(*fn)(f, "%*s", level*PRETTYPRINT_INDENT, "");
if (pretty && hasbody==0){
if (pretty && prefix)
(*fn)(f, "%s", prefix);
(*fn)(f, "%*s", level1, "");
}
(*fn)(f, "</");
if (namespace)
(*fn)(f, "%s:", namespace);
(*fn)(f, "%s>", name);
}
if (pretty)
if (pretty){
(*fn)(f, "\n");
}
break;
default:
break;
@ -210,7 +223,8 @@ xml2file_recurse(FILE *f,
* @param[in] xn XML tree
* @param[in] level How many spaces to insert before each line
* @param[in] pretty Insert \n and spaces to make the xml more readable.
* @param[in] fn File print function (if NULL, use fprintf)
* @param[in] prefix Add string to beginning of each line (if pretty)
* @param[in] fn File print function (if NULL, use fprintf)
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @retval 0 OK
@ -224,6 +238,7 @@ clixon_xml2file(FILE *f,
cxobj *xn,
int level,
int pretty,
char *prefix,
clicon_output_cb *fn,
int skiptop,
int autocliext)
@ -236,11 +251,11 @@ clixon_xml2file(FILE *f,
if (skiptop){
xc = NULL;
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
if (xml2file_recurse(f, xc, level, pretty, fn, autocliext) < 0)
if (xml2file_recurse(f, xc, level, pretty, prefix, fn, autocliext) < 0)
goto done;
}
else {
if (xml2file_recurse(f, xn, level, pretty, fn, autocliext) < 0)
if (xml2file_recurse(f, xn, level, pretty, prefix, fn, autocliext) < 0)
goto done;
}
retval = 0;
@ -260,7 +275,7 @@ int
xml_print(FILE *f,
cxobj *x)
{
return xml2file_recurse(f, x, 0, 1, fprintf, 0);
return xml2file_recurse(f, x, 0, 1, NULL, fprintf, 0);
}
/*! Dump cxobj structure with pointers and flags for debugging, internal function
@ -314,6 +329,7 @@ xml_dump(FILE *f,
* @param[in] xn Clixon xml tree
* @param[in] level Indentation level for prettyprint
* @param[in] pretty Insert \n and spaces to make the xml more readable.
* @param[in] prefix Add string to beginning of each line (if pretty)
* @param[in] depth Limit levels of child resources: -1 is all, 0 is none, 1 is node itself
*/
static int
@ -321,6 +337,7 @@ clixon_xml2cbuf1(cbuf *cb,
cxobj *x,
int level,
int pretty,
char *prefix,
int32_t depth)
{
int retval = -1;
@ -330,9 +347,13 @@ clixon_xml2cbuf1(cbuf *cb,
int haselement;
char *namespace;
char *val;
int level1;
if (depth == 0)
goto ok;
level1 = level*PRETTYPRINT_INDENT;
if (prefix)
level1 -= strlen(prefix);
name = xml_name(x);
namespace = xml_prefix(x);
switch(xml_type(x)){
@ -351,8 +372,11 @@ clixon_xml2cbuf1(cbuf *cb,
cprintf(cb, "%s=\"%s\"", name, xml_value(x));
break;
case CX_ELMNT:
if (pretty)
cprintf(cb, "%*s<", level*PRETTYPRINT_INDENT, "");
if (pretty){
if (prefix)
cprintf(cb, "%s", prefix);
cprintf(cb, "%*s<", level1, "");
}
else
cbuf_append_str(cb, "<");
if (namespace){
@ -367,7 +391,7 @@ clixon_xml2cbuf1(cbuf *cb,
while ((xc = xml_child_each(x, xc, -1)) != NULL)
switch (xml_type(xc)){
case CX_ATTR:
if (clixon_xml2cbuf1(cb, xc, level+1, pretty, -1) < 0)
if (clixon_xml2cbuf1(cb, xc, level+1, pretty, prefix, -1) < 0)
goto done;
break;
case CX_BODY:
@ -389,10 +413,13 @@ clixon_xml2cbuf1(cbuf *cb,
xc = NULL;
while ((xc = xml_child_each(x, xc, -1)) != NULL)
if (xml_type(xc) != CX_ATTR)
if (clixon_xml2cbuf1(cb, xc, level+1, pretty, depth-1) < 0)
if (clixon_xml2cbuf1(cb, xc, level+1, pretty, prefix, depth-1) < 0)
goto done;
if (pretty && hasbody == 0)
cprintf(cb, "%*s", level*PRETTYPRINT_INDENT, "");
if (pretty && hasbody == 0){
if (prefix)
cprintf(cb, "%s", prefix);
cprintf(cb, "%*s", level1, "");
}
cbuf_append_str(cb, "</");
if (namespace){
cbuf_append_str(cb, namespace);
@ -419,6 +446,7 @@ clixon_xml2cbuf1(cbuf *cb,
* @param[in] xn Top-level xml object
* @param[in] level Indentation level for pretty
* @param[in] pretty Insert \n and spaces to make the xml more readable.
* @param[in] prefix Add string to beginning of each line (if pretty)
* @param[in] depth Limit levels of child resources: -1: all, 0: none, 1: node itself
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @retval 0 OK
@ -426,7 +454,7 @@ clixon_xml2cbuf1(cbuf *cb,
* Depth is used in NACM
* @code
* cbuf *cb = cbuf_new();
* if (clixon_xml2cbuf(cb, xn, 0, 1, -1, 0) < 0)
* if (clixon_xml2cbuf(cb, xn, 0, 1, NULL, -1, 0) < 0)
* goto err;
* fprintf(stderr, "%s", cbuf_get(cb));
* cbuf_free(cb);
@ -438,6 +466,7 @@ clixon_xml2cbuf(cbuf *cb,
cxobj *xn,
int level,
int pretty,
char *prefix,
int32_t depth,
int skiptop)
{
@ -447,11 +476,11 @@ clixon_xml2cbuf(cbuf *cb,
if (skiptop){
xc = NULL;
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
if (clixon_xml2cbuf1(cb, xc, level, pretty, depth) < 0)
if (clixon_xml2cbuf1(cb, xc, level, pretty, prefix, depth) < 0)
goto done;
}
else {
if (clixon_xml2cbuf1(cb, xn, level, pretty, depth) < 0)
if (clixon_xml2cbuf1(cb, xn, level, pretty, prefix, depth) < 0)
goto done;
}
retval = 0;

View file

@ -456,7 +456,7 @@ xml_diff(cxobj *x0,
* @param[in] x1 Second XML tree
* @retval 1 Not equal
* @retval 0 Equal
* @see xml_diff
* @see xml_diff which returns diff sets
*/
int
xml_tree_equal(cxobj *x0,
@ -1791,24 +1791,23 @@ purge_tagged_nodes(cxobj *xn,
return retval;
}
/*! Compare two dbs using XML. Write to file and run diff
/*! Compare two dbs using XML. Write to file and run diff. Independent of YANG
*
* @param[in] xc1 XML tree 1
* @param[in] xc2 XML tree 2
* @param[in] format "text"|"xml"|"json"|"cli"|"netconf" (see format_enum)
* @param[in] fn File print function (if NULL, use fprintf)
* @see xml_tree_diff_print with better XML in-mem comparison but is YANG dependent
*/
int
clixon_compare_xmls(cxobj *xc1,
cxobj *xc2,
enum format_enum format,
clicon_output_cb *fn)
enum format_enum format)
{
int retval = -1;
int fd;
FILE *f;
char filename1[MAXPATHLEN];
char filename2[MAXPATHLEN];
int retval = -1;
cbuf *cb = NULL;
snprintf(filename1, sizeof(filename1), "/tmp/cliconXXXXXX");
@ -1826,7 +1825,7 @@ clixon_compare_xmls(cxobj *xc1,
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc1, 0, 1, cligen_output, 1, 1) < 0)
if (clixon_xml2file(f, xc1, 0, 1, NULL, cligen_output, 1, 1) < 0)
goto done;
break;
}
@ -1847,11 +1846,10 @@ clixon_compare_xmls(cxobj *xc1,
break;
case FORMAT_XML:
default:
if (clixon_xml2file(f, xc2, 0, 1, cligen_output, 1, 1) < 0)
if (clixon_xml2file(f, xc2, 0, 1, NULL, cligen_output, 1, 1) < 0)
goto done;
break;
}
fclose(f);
close(fd);
@ -1872,3 +1870,126 @@ clixon_compare_xmls(cxobj *xc1,
unlink(filename2);
return retval;
}
/*! Print diff of two XML trees in memory. YANG dependent
*
* Uses underlying XML diff algorithm with better result than clixon_compare_xmls
* @param[in] cb CLIgen buffer
* @param[in] level How many spaces to insert before each line
* @param[in] x0 First XML tree
* @param[in] x1 Second XML tree
* @param[in] format "text"|"xml"|"json"|"cli"|"netconf" (NYI: only xml)
* @retval 0 Ok
* @retval -1 Error
* @see xml_diff which returns diff sets
* @see clixon_compare_xmls which uses files and is independent of YANG
*/
int
xml_tree_diff_print(cbuf *cb,
int level,
cxobj *x0,
cxobj *x1,
enum format_enum format)
{
int retval = -1;
cxobj *x0c = NULL; /* x0 child */
cxobj *x1c = NULL; /* x1 child */
yang_stmt *yc0;
yang_stmt *yc1;
char *b0;
char *b1;
int eq;
int nr=0;
/* Traverse x0 and x1 in lock-step */
x0c = x1c = NULL;
x0c = xml_child_each(x0, x0c, CX_ELMNT);
x1c = xml_child_each(x1, x1c, CX_ELMNT);
for (;;){
if (x0c == NULL && x1c == NULL)
goto ok;
else if (x0c == NULL){
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x1));
nr++;
if (clixon_xml2cbuf(cb, x1c, level+1, 1, "+", -1, 0) < 0)
goto done;
x1c = xml_child_each(x1, x1c, CX_ELMNT);
continue;
}
else if (x1c == NULL){
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x0));
nr++;
if (clixon_xml2cbuf(cb, x0c, level+1, 1, "-", -1, 0) < 0)
goto done;
x0c = xml_child_each(x0, x0c, CX_ELMNT);
continue;
}
/* Both x0c and x1c exists, check if they are yang-equal. */
eq = xml_cmp(x0c, x1c, 0, 0, NULL);
if (eq < 0){
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x0));
nr++;
if (clixon_xml2cbuf(cb, x0c, level+1, 1, "-", -1, 0) < 0)
goto done;
x0c = xml_child_each(x0, x0c, CX_ELMNT);
continue;
}
else if (eq > 0){
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x1));
nr++;
if (clixon_xml2cbuf(cb, x1c, level+1, 1, "+", -1, 0) < 0)
goto done;
x1c = xml_child_each(x1, x1c, CX_ELMNT);
continue;
}
else{ /* equal */
/* xml-spec NULL could happen with anydata children for example,
* if so, continute compare children but without yang
*/
yc0 = xml_spec(x0c);
yc1 = xml_spec(x1c);
if (yc0 && yc1 && yc0 != yc1){ /* choice */
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x0));
nr++;
if (clixon_xml2cbuf(cb, x0c, level+1, 1, "-", -1, 0) < 0)
goto done;
if (clixon_xml2cbuf(cb, x1c, level+1, 1, "+", -1, 0) < 0)
goto done;
}
else
if (yc0 && yang_keyword_get(yc0) == Y_LEAF){
/* if x0c and x1c are leafs w bodies, then they may be changed */
b0 = xml_body(x0c);
b1 = xml_body(x1c);
if (b0 == NULL && b1 == NULL)
;
else if (b0 == NULL || b1 == NULL
|| strcmp(b0, b1) != 0
){
if (nr==0)
cprintf(cb, "%*s%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x0));
nr++;
cprintf(cb, "-%*s%s>%s</%s>\n", (level*PRETTYPRINT_INDENT-1), "<",
xml_name(x0c), b0, xml_name(x0c));
cprintf(cb, "+%*s%s>%s</%s>\n", (level*PRETTYPRINT_INDENT-1), "<",
xml_name(x1c), b1, xml_name(x1c));
}
}
else if (xml_tree_diff_print(cb, level+1, x0c, x1c, format) < 0)
goto done;
}
x0c = xml_child_each(x0, x0c, CX_ELMNT);
x1c = xml_child_each(x1, x1c, CX_ELMNT);
}
ok:
if (nr)
cprintf(cb, "%*s/%s>\n", level*PRETTYPRINT_INDENT, "<", xml_name(x0));
retval = 0;
done:
return retval;
}

View file

@ -376,7 +376,7 @@ clixon_xvec_print(FILE *f,
int i;
for (i=0; i<xv->xv_len; i++)
if (clixon_xml2file(f, xv->xv_vec[i], 0, 1, fprintf, 0, 0) < 0)
if (clixon_xml2file(f, xv->xv_vec[i], 0, 1, NULL, fprintf, 0, 0) < 0)
return -1;
return 0;
}

View file

@ -73,8 +73,8 @@ DATASTORE_TOP="config"
# clixon yang revisions occuring in tests (see eg yang/clixon/Makefile.in)
CLIXON_AUTOCLI_REV="2022-02-11"
CLIXON_LIB_REV="2022-12-01"
CLIXON_CONFIG_REV="2022-12-01"
CLIXON_LIB_REV="2023-03-01"
CLIXON_CONFIG_REV="2023-03-01"
CLIXON_RESTCONF_REV="2022-08-01"
CLIXON_EXAMPLE_REV="2022-11-01"

View file

@ -216,7 +216,7 @@ main(int argc, char **argv)
xpath = "/";
if (xmldb_get(h, db, NULL, xpath, &xt) < 0)
goto done;
if (clixon_xml2file(stdout, xt, 0, 0, fprintf, 0, 0) < 0)
if (clixon_xml2file(stdout, xt, 0, 0, NULL, fprintf, 0, 0) < 0)
goto done;
fprintf(stdout, "\n");
if (xt){
@ -240,7 +240,7 @@ main(int argc, char **argv)
clicon_err(OE_DB, 0, "xt is NULL");
goto done;
}
if (clixon_xml2file(stdout, xt, 0, 0, fprintf, 0, 0) < 0)
if (clixon_xml2file(stdout, xt, 0, 0, NULL, fprintf, 0, 0) < 0)
goto done;
if (xt){
xml_free(xt);

View file

@ -147,7 +147,7 @@ main(int argc,
if (clixon_json2cbuf(cb, xt, pretty, 1, 0) < 0)
goto done;
}
else if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 1) < 0)
else if (clixon_xml2cbuf(cb, xt, 0, pretty, NULL, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);

View file

@ -281,7 +281,7 @@ main(int argc,
for (i = 0; i < xlen; i++){
xc = xvec[i];
fprintf(stdout, "%d: ", i);
clixon_xml2file(stdout, xc, 0, 0, fprintf, 0, 0);
clixon_xml2file(stdout, xc, 0, 0, NULL, fprintf, 0, 0);
fprintf(stdout, "\n");
fflush(stdout);
}

View file

@ -167,7 +167,7 @@ main(int argc,
fprintf(stderr, "No xml\n");
goto done;
}
if (clixon_xml2cbuf(cb, xc, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xc, 0, 0, NULL, -1, 0) < 0)
goto done;
if ((msg = clicon_msg_encode(getpid(), "%s", cbuf_get(cb))) < 0)
goto done;

View file

@ -341,7 +341,7 @@ main(int argc,
if (clixon_json2cbuf(cb, xt, pretty, 1, 0) < 0)
goto done;
}
else if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 1) < 0)
else if (clixon_xml2cbuf(cb, xt, 0, pretty, NULL, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);

View file

@ -282,11 +282,11 @@ main(int argc, char **argv)
if (sort)
xml_sort_recurse(xb);
if (strcmp(xml_name(xb),"top")==0){
if (clixon_xml2file(stdout, xb, 0, 0, fprintf, 1, 0) < 0)
if (clixon_xml2file(stdout, xb, 0, 0, NULL, fprintf, 1, 0) < 0)
goto done;
}
else{
if (clixon_xml2file(stdout, xb, 0, 0, fprintf, 0, 0) < 0)
if (clixon_xml2file(stdout, xb, 0, 0, NULL, fprintf, 0, 0) < 0)
goto done;
}
fprintf(stdout, "\n");

View file

@ -99,7 +99,7 @@ ctx_print2(cbuf *cb,
case XT_NODESET:
for (i=0; i<xc->xc_size; i++){
cprintf(cb, "%d:", i);
if (clixon_xml2cbuf(cb, xc->xc_nodeset[i], 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xc->xc_nodeset[i], 0, 0, NULL, -1, 0) < 0)
goto done;
}
break;

View file

@ -50,6 +50,7 @@ module clixon-config {
description
"Added options:
CLICON_RESTCONF_NOALPN_DEFAULT
Extended datastore-format with CLI and text
Released in Clixon 6.2";
}
revision 2022-12-01 {
@ -275,7 +276,7 @@ module clixon-config {
}
typedef datastore_format{
description
"Datastore format.";
"Datastore format (only xml and json implemented in actual data.";
type enumeration{
enum xml{
description
@ -286,6 +287,12 @@ module clixon-config {
enum json{
description "Save and load xmldb as JSON";
}
enum text{
description "'Curly' C-like text format";
}
enum cli{
description "CLI format";
}
}
}
typedef datastore_cache{