This commit is contained in:
Olof Hagsand 2019-02-28 13:12:06 +01:00
commit 3dd67474e0
23 changed files with 119 additions and 50 deletions

View file

@ -187,7 +187,7 @@ clicon_err_fn(const char *fn,
line,
clicon_strerror(category),
msg,
strerror(suberr));
suberr==XMLPARSE_ERRNO?"XML parse error":strerror(suberr));
}
else
clicon_log(LOG_ERR, "%s: %d: %s: %s",

View file

@ -39,7 +39,6 @@
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU /* strverscmp */
#include <string.h>
#include <errno.h>
#include <dirent.h>
@ -71,11 +70,7 @@ clicon_file_dirent_sort(const void* arg1,
struct dirent *d1 = (struct dirent *)arg1;
struct dirent *d2 = (struct dirent *)arg2;
#ifdef HAVE_STRVERSCMP
return strverscmp(d1->d_name, d2->d_name); /* strverscmp specific GNU function */
#else /* HAVE_STRVERSCMP */
return strcoll(d1->d_name, d2->d_name);
#endif /* HAVE_STRVERSCMP */
}
/*! Return alphabetically sorted files from a directory matching regexp

View file

@ -249,7 +249,7 @@ clicon_log_str(int level,
* @code
clicon_log(LOG_NOTICE, "%s: dump to dtd not supported", __PROGRAM__);
* @endcode
* @see cicon_log_init and clicon_log_str
* @see clicon_log_init and clicon_log_str
*/
int
clicon_log(int level,

View file

@ -122,12 +122,14 @@ struct xml{
int x_childvec_len;/* length of vector */
enum cxobj_type x_type; /* type of node: element, attribute, body */
char *x_value; /* attribute and body nodes have values */
int _x_vector_i; /* internal use: xml_child_each */
int x_flags; /* Flags according to XML_FLAG_* */
yang_stmt *x_spec; /* Pointer to specification, eg yang, by
reference, dont free */
cg_var *x_cv; /* Cached value as cligen variable
(eg xml_cmp) */
int _x_vector_i; /* internal use: xml_child_each */
int _x_i; /* internal use for sorting:
see xml_enumerate and xml_cmp */
};
/*
@ -1057,6 +1059,43 @@ xml_rootchild_node(cxobj *xp,
return retval;
}
/*! help function to sorting: enumerate all children according to present order
* This is so that the child itself know its present order in a list.
* When sorting by "ordered by user", the order should remain in its present
* state.
* A child can always compute its order functionally but it computes
* more cycles,..
* @param[in] xp Enumerate its children
* @retval 0 OK
* @see xml_sort
* @see xml_enumerate_get Call to the child to get the number
*/
int
xml_enumerate_children(cxobj *xp)
{
cxobj *x = NULL;
int i = 0;
while ((x = xml_child_each(xp, x, -1)) != NULL)
x->_x_i = i++;
return 0;
}
/*! Get the enumeration of a single child set by enumeration of parent
* @see xml_children_enumerate
* @note that it has to be called right after xml_children_enumerate. If not,
* there are many cases where this info is stale.
* @param[in] x A child whose parent has enumerated its children
* @retval n Enumeration
* @see xml_enumerate_children Call to the parent to compute the nr
*/
int
xml_enumerate_get(cxobj *x)
{
return x->_x_i;
}
/*! Get the first sub-node which is an XML body.
* @param[in] xn xml tree node
* @retval The returned body as a pointer to the name string
@ -1560,6 +1599,7 @@ _xml_parse(const char *str,
int retval = -1;
struct xml_parse_yacc_arg ya = {0,};
cxobj *x;
if (strlen(str) == 0)
return 0; /* OK */
if (xt == NULL){
@ -1595,7 +1635,7 @@ _xml_parse(const char *str,
retval = 0;
done:
clixon_xml_parsel_exit(&ya);
if(ya.ya_parse_string != NULL)
if (ya.ya_parse_string != NULL)
free(ya.ya_parse_string);
return retval;
}
@ -2046,6 +2086,28 @@ xml_apply_ancestor(cxobj *xn,
return retval;
}
/*! Is xpp ancestor of x?
* @param[in] x XML node
* @param[in] xpp Potential ancestor of x in XML tree
* @retval 0 No, xpp is not ancestor of x
* @retval 1 Yes, xpp is ancestor of x
*/
int
xml_isancestor(cxobj *x,
cxobj *xpp)
{
cxobj *xp = NULL;
cxobj *xn = NULL;
xn = x;
while ((xp = xml_parent(xn)) != NULL) {
if (xp == xpp)
return 1;
xn = xp;
}
return 0;
}
/*! Generic parse function for xml values
* @param[in] xb xml tree body node, ie containing a value to be parsed
* @param[in] type Type of value to be parsed in value
@ -2255,7 +2317,6 @@ clicon_log_xml(int level,
return retval;
}
/*
* Turn this on to get a xml parse and pretty print test program
* Usage: xpath

View file

@ -194,6 +194,7 @@ xml_child_spec(cxobj *x,
* @note args are pointer ot pointers, to fit into qsort cmp function
* @see xml_cmp1 Similar, but for one object
* @note empty value/NULL is smallest value
* @note xml_enumerate_children must have been called prior to this call
*/
static int
xml_cmp(const void* arg1,
@ -213,12 +214,19 @@ xml_cmp(const void* arg1,
char *keyname;
cg_var *cv1;
cg_var *cv2;
int nr1;
int nr2;
assert(x1&&x2);
if (x1==NULL || x2==NULL)
return 0; /* shouldnt happen */
y1 = xml_spec(x1);
y2 = xml_spec(x2);
if (y1==NULL || y2==NULL)
return 0; /* just ignore */
nr1 = xml_enumerate_get(x1);
nr2 = xml_enumerate_get(x2);
if (y1==NULL || y2==NULL){
equal = nr1-nr2;
return equal;
}
if (y1 != y2){
yi1 = yang_order(y1);
yi2 = yang_order(y2);
@ -230,8 +238,10 @@ xml_cmp(const void* arg1,
* otherwise sort according to key
*/
if (yang_config(y1)==0 ||
yang_find((yang_node*)y1, Y_ORDERED_BY, "user") != NULL)
return 0; /* Ordered by user or state data : maintain existing order */
yang_find((yang_node*)y1, Y_ORDERED_BY, "user") != NULL){
equal = nr1-nr2;
return equal; /* Ordered by user or state data : maintain existing order */
}
switch (y1->ys_keyword){
case Y_LEAF_LIST: /* Match with name and value */
if ((b1 = xml_body(x1)) == NULL)
@ -358,6 +368,7 @@ xml_sort(cxobj *x,
/* Abort sort if non-config (=state) data */
if ((ys = xml_spec(x)) != 0 && yang_config(ys)==0)
return 1;
xml_enumerate_children(x);
qsort(xml_childvec_get(x), xml_child_nr(x), sizeof(cxobj *), xml_cmp);
return 0;
}
@ -642,6 +653,7 @@ xml_sort_verify(cxobj *x0,
retval = 1;
goto done;
}
xml_enumerate_children(x0);
while ((x = xml_child_each(x0, x, -1)) != NULL) {
if (xprev != NULL){ /* Check xprev <= x */
if (xml_cmp(&xprev, &x) > 0)

View file

@ -56,8 +56,6 @@
#include <limits.h>
#include <ctype.h>
#include <unistd.h>
#define __USE_GNU /* strverscmp */
#define _GNU_SOURCE
#include <string.h>
#include <arpa/inet.h>
#include <regex.h>
@ -66,6 +64,7 @@
#include <fcntl.h>
#include <syslog.h>
#include <assert.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>
@ -2449,7 +2448,7 @@ yang_spec_parse_module(clicon_handle h,
*/
int
yang_spec_parse_file(clicon_handle h,
const char *filename,
char *filename,
yang_spec *yspec)
{
int retval = -1;

View file

@ -44,7 +44,6 @@
#include <limits.h>
#include <ctype.h>
#include <unistd.h>
#define __USE_GNU /* strverscmp */
#include <string.h>
#include <arpa/inet.h>
#include <regex.h>

View file

@ -44,7 +44,6 @@
#include <inttypes.h>
#include <limits.h>
#include <ctype.h>
#define __USE_GNU /* strverscmp */
#include <string.h>
#include <arpa/inet.h>
#include <regex.h>