Merge branch 'autocli' of https://github.com/shmuelnatan/clixon into shmuelnatan-autocli
This commit is contained in:
commit
1c7e52f829
8 changed files with 379 additions and 37 deletions
|
|
@ -120,6 +120,315 @@ 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
|
||||
* @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 */
|
||||
char *opext = NULL;
|
||||
|
||||
if (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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/*! 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
|
||||
|
|
@ -413,10 +722,10 @@ cli_auto_show(clicon_handle h,
|
|||
switch (format){
|
||||
case FORMAT_XML:
|
||||
if (isroot)
|
||||
clicon_xml2file(stdout, xp, 0, pretty);
|
||||
cli_xml2file(xp, 0, pretty, fprintf);
|
||||
else{
|
||||
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
|
||||
clicon_xml2file(stdout, xc, 0, pretty);
|
||||
cli_xml2file(xc, 0, pretty, fprintf);
|
||||
}
|
||||
fprintf(stdout, "\n");
|
||||
break;
|
||||
|
|
@ -431,19 +740,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, "<rpc xmlns=\"%s\"><edit-config><target><candidate/></target><config>",
|
||||
|
|
@ -451,10 +760,10 @@ cli_auto_show(clicon_handle h,
|
|||
if (pretty)
|
||||
fprintf(stdout, "\n");
|
||||
if (isroot)
|
||||
clicon_xml2file(stdout, xp, 2, pretty);
|
||||
cli_xml2file(xp, 2, pretty, fprintf);
|
||||
else
|
||||
while ((xc = xml_child_each(xp, xc, CX_ELMNT)) != NULL)
|
||||
clicon_xml2file(stdout, xc, 2, pretty);
|
||||
cli_xml2file(xc, 2, pretty, fprintf);
|
||||
fprintf(stdout, "</config></edit-config></rpc>]]>]]>\n");
|
||||
break;
|
||||
} /* switch */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue