* 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:
Olof hagsand 2022-05-19 12:56:44 +02:00
parent 43a57dad79
commit 2ece0b8f51
29 changed files with 1140 additions and 238 deletions

View file

@ -107,110 +107,6 @@ isxmlns(cxobj *x)
return 0;
}
/*! x is element and has eactly 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);
}
/*! Translate XML to a "pseudo-code" textual format using a callback - internal function
* @param[in] f File to print to
* @param[in] x 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
*/
static int
xml2txt_recurse(FILE *f,
cxobj *x,
clicon_output_cb *fn,
int level)
{
cxobj *xc = NULL;
int children=0;
int retval = -1;
if (f == NULL || x == NULL || fn == NULL){
clicon_err(OE_XML, EINVAL, "f, x or fn is NULL");
goto done;
}
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)
children++;
if (!children){ /* If no children print line */
switch (xml_type(x)){
case CX_BODY:
(*fn)(f, "%s;\n", xml_value(x));
break;
case CX_ELMNT:
(*fn)(f, "%*s%s;\n", 4*level, "", xml_name(x));
break;
default:
break;
}
goto ok;
}
(*fn)(f, "%*s", 4*level, "");
(*fn)(f, "%s ", xml_name(x));
if (!tleaf(x))
(*fn)(f, "{\n");
xc = NULL;
while ((xc = xml_child_each(x, xc, -1)) != NULL){
if (xml_type(xc) == CX_ELMNT || xml_type(xc) == CX_BODY)
if (xml2txt_recurse(f, xc, fn, level+1) < 0)
break;
}
if (!tleaf(x))
(*fn)(f, "%*s}\n", 4*level, "");
ok:
retval = 0;
done:
return retval;
}
/*! Translate XML to a "pseudo-code" textual format using a callback
* @param[in] f File to print to
* @param[in] x XML object to print
* @param[in] fn Callback to make print function
*/
int
xml2txt_cb(FILE *f,
cxobj *x,
clicon_output_cb *fn)
{
return xml2txt_recurse(f, x, fn, 0);
}
/*! Translate XML to a "pseudo-code" textual format using stdio file
* @param[in] f File to print to
* @param[in] x XML object to print
* @param[in] level print 4 spaces per level in front of each line
* @see xml2txt_cb
*/
int
xml2txt(FILE *f,
cxobj *x,
int level)
{
return xml2txt_recurse(f, x, fprintf, level);
}
/*! Translate a single xml node to a cligen variable vector. Note not recursive
* @param[in] xt XML tree containing one top node
* @param[in] ys Yang spec containing type specification of top-node of xt