* Experimental text syntax parser/loader
* Added new text syntax parsing and loading from CLI
* Unified text output functions to `xml2txt` and moved to clixon_text_syntax.[ch]
* The following are removed: `cli_xml2txt` and `xml2txt_cb`
* Text output format changed:
* Namespace/modulename added to top-level
* See [Support performant load_config_file(...) for TEXT format](https://github.com/clicon/clixon/issues/324)
This commit is contained in:
parent
43a57dad79
commit
2ece0b8f51
29 changed files with 1140 additions and 238 deletions
|
|
@ -125,28 +125,6 @@ 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 from an auto-cli env to stdout and encode chars "<>&"
|
||||
*
|
||||
* @param[in] xn clicon xml tree
|
||||
|
|
@ -256,64 +234,6 @@ cli_xml2file(cxobj *xn,
|
|||
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;
|
||||
int exist = 0;
|
||||
|
||||
if (xn == NULL || fn == NULL){
|
||||
clicon_err(OE_XML, EINVAL, "xn or fn is NULL");
|
||||
goto done;
|
||||
}
|
||||
if (yang_extension_value(xml_spec(xn), "hide-show", CLIXON_AUTOCLI_NS, &exist, NULL) < 0)
|
||||
goto done;
|
||||
if (exist)
|
||||
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;
|
||||
}
|
||||
|
||||
/*! Enter a CLI edit mode
|
||||
* @param[in] h CLICON handle
|
||||
* @param[in] cvv Vector of variables from CLIgen command-line
|
||||
|
|
@ -657,10 +577,10 @@ cli_auto_show(clicon_handle h,
|
|||
break;
|
||||
case FORMAT_TEXT:
|
||||
if (isroot)
|
||||
cli_xml2txt(xp, cligen_output, 0); /* tree-formed text */
|
||||
xml2txt(xp, cligen_output, stdout, 0); /* tree-formed text */
|
||||
else
|
||||
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
|
||||
cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */
|
||||
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
|
||||
break;
|
||||
case FORMAT_CLI:
|
||||
if (xml2cli(h, stdout, xp, prefix, cligen_output, !isroot) < 0)
|
||||
|
|
|
|||
|
|
@ -698,7 +698,7 @@ compare_xmls(cxobj *xc1,
|
|||
xc = NULL;
|
||||
if (astext)
|
||||
while ((xc = xml_child_each(xc1, xc, -1)) != NULL)
|
||||
xml2txt_cb(f, xc, cligen_output);
|
||||
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);
|
||||
|
|
@ -715,7 +715,7 @@ compare_xmls(cxobj *xc1,
|
|||
xc = NULL;
|
||||
if (astext)
|
||||
while ((xc = xml_child_each(xc2, xc, -1)) != NULL)
|
||||
xml2txt_cb(f, xc, cligen_output);
|
||||
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);
|
||||
|
|
@ -882,6 +882,14 @@ load_config_file(clicon_handle h,
|
|||
goto done;
|
||||
}
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
if ((ret = clixon_text_syntax_parse_file(fp, YB_NONE, yspec, &xt, &xerr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
clixon_netconf_error(xerr, "Loading", filename);
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case FORMAT_CLI:
|
||||
{
|
||||
char *mode = cli_syntax_mode(h);
|
||||
|
|
@ -1034,7 +1042,7 @@ save_config_file(clicon_handle h,
|
|||
goto done;
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
if (xml2txt(f, xt, 0) < 0)
|
||||
if (xml2txt(xt, fprintf, f, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case FORMAT_CLI:
|
||||
|
|
@ -1165,7 +1173,7 @@ cli_notification_cb(int s,
|
|||
goto done;
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
if (xml2txt_cb(stdout, x, cligen_output) < 0)
|
||||
if (xml2txt(x, cligen_output, stdout, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -505,7 +505,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)
|
||||
cli_xml2txt(xc, cligen_output, 0); /* tree-formed text */
|
||||
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
|
||||
break;
|
||||
case FORMAT_CLI:
|
||||
if (xml2cli(h, stdout, xt, prefix, cligen_output, 1) < 0)
|
||||
|
|
@ -789,7 +789,7 @@ cli_show_generated(clicon_handle h,
|
|||
cli_xml2file(xp_helper, 0, 1, fprintf);
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
cli_xml2txt(xp_helper, cligen_output, 0); /* tree-formed text */
|
||||
xml2txt(xp_helper, cligen_output, stdout, 0); /* tree-formed text */
|
||||
break;
|
||||
default: /* see cli_show_config() */
|
||||
break;
|
||||
|
|
@ -986,7 +986,7 @@ cli_pagination(clicon_handle h,
|
|||
xml2json_file(stdout, xc, 1, cligen_output, 0);
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
xml2txt_cb(stdout, xc, cligen_output); /* tree-formed text */
|
||||
xml2txt(xc, cligen_output, stdout, 0); /* tree-formed text */
|
||||
break;
|
||||
case FORMAT_CLI:
|
||||
/* hardcoded to compress and list-keyword = nokey */
|
||||
|
|
|
|||
|
|
@ -136,7 +136,6 @@ int cli_help(clicon_handle h, cvec *vars, cvec *argv);
|
|||
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 cli_xml2txt(cxobj *xn, clicon_output_cb *fn, int level);
|
||||
int xml2cli(clicon_handle h, FILE *f, cxobj *xn, char *prepend, clicon_output_cb *fn, int skiptop);
|
||||
|
||||
/* cli_show.c: CLIgen new vector arg callbacks */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue