Datastore cache introduced; Added yang to XML API

This commit is contained in:
Olof hagsand 2017-12-02 14:49:49 +01:00
parent c1cb3a2b6c
commit e169485985
34 changed files with 884 additions and 389 deletions

View file

@ -235,8 +235,8 @@ hash_add(clicon_hash_t *hash,
h = new;
}
/* Make copy of lvalue */
newval = malloc(align4(vlen+3)); /* XXX: qdbm needs aligned mallocs? */
/* Make copy of value. aligned */
newval = malloc(align4(vlen+3));
if (newval == NULL){
clicon_err(OE_UNIX, errno, "malloc: %s", strerror(errno));
goto catch;

View file

@ -54,6 +54,10 @@
/* clixon */
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_json.h"
#include "clixon_json_parse.h"
@ -465,7 +469,7 @@ xml2json_cbuf_vec(cbuf *cb,
cxobj *xp = NULL;
cxobj *xc;
if ((xp = xml_new("", NULL)) == NULL)
if ((xp = xml_new("", NULL, NULL)) == NULL)
goto done;
for (i=0; i<veclen; i++){
xc = xml_dup(vec[i]);
@ -619,7 +623,7 @@ int
json_parse_str(char *str,
cxobj **xt)
{
if ((*xt = xml_new("top", NULL)) == NULL)
if ((*xt = xml_new("top", NULL, NULL)) == NULL)
return -1;
return json_parse(str, "", *xt);
}

View file

@ -171,7 +171,7 @@ json_current_new(struct clicon_json_yacc_arg *jy,
cxobj *xn;
clicon_debug(2, "%s", __FUNCTION__);
if ((xn = xml_new(name, jy->jy_current)) == NULL)
if ((xn = xml_new(name, jy->jy_current, NULL)) == NULL)
goto done;
jy->jy_current = xn;
retval = 0;
@ -207,7 +207,7 @@ json_current_body(struct clicon_json_yacc_arg *jy,
cxobj *xn;
clicon_debug(2, "%s", __FUNCTION__);
if ((xn = xml_new("body", jy->jy_current)) == NULL)
if ((xn = xml_new("body", jy->jy_current, NULL)) == NULL)
goto done;
xml_type_set(xn, CX_BODY);
if (value && xml_value_append(xn, value)==NULL)

View file

@ -150,7 +150,7 @@ clicon_option_readfile_xml(clicon_hash_t *copt,
}
clicon_debug(2, "Reading config file %s", __FUNCTION__, filename);
fd = fileno(f);
if (clicon_xml_parse_file(fd, &xt, "</clicon>") < 0)
if (clicon_xml_parse_file(fd, "</clicon>", yspec, &xt) < 0)
goto done;
if (xml_child_nr(xt)==1 && xml_child_nr_type(xt, CX_BODY)==1){
clicon_err(OE_CFG, 0, "Config file %s: Expected XML but is probably old sh style", filename);
@ -160,8 +160,6 @@ clicon_option_readfile_xml(clicon_hash_t *copt,
clicon_err(OE_CFG, 0, "Config file %s: Lacks top-level \"config\" element", filename);
goto done;
}
if (xml_apply0(xc, CX_ELMNT, xml_spec_populate, yspec) < 0)
goto done;
if (xml_apply0(xc, CX_ELMNT, xml_default, yspec) < 0)
goto done;
if (xml_apply0(xc, CX_ELMNT, xml_yang_validate_add, NULL) < 0)

View file

@ -173,7 +173,7 @@ clicon_msg_decode(struct clicon_msg *msg,
/* body */
xmlstr = msg->op_body;
clicon_debug(1, "%s %s", __FUNCTION__, xmlstr);
if (clicon_xml_parse_str(xmlstr, xml) < 0)
if (clicon_xml_parse_str(xmlstr, NULL, xml) < 0)
goto done;
retval = 0;
done:

View file

@ -120,7 +120,7 @@ clicon_rpc_msg(clicon_handle h,
}
clicon_debug(1, "%s retdata:%s", __FUNCTION__, retdata);
if (retdata &&
clicon_xml_parse_str(retdata, &xret) < 0)
clicon_xml_parse_str(retdata, NULL, &xret) < 0)
goto done;
if (xret0){
*xret0 = xret;
@ -274,7 +274,7 @@ clicon_rpc_get_config(clicon_handle h,
if ((xd = xpath_first(xret, "/rpc-reply/rpc-error")) != NULL)
xd = xml_parent(xd); /* point to rpc-reply */
else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL)
if ((xd = xml_new("data", NULL)) == NULL)
if ((xd = xml_new("data", NULL, NULL)) == NULL)
goto done;
if (xt){
if (xml_rm(xd) < 0)
@ -522,7 +522,7 @@ clicon_rpc_get(clicon_handle h,
if ((xd = xpath_first(xret, "/rpc-reply/rpc-error")) != NULL)
xd = xml_parent(xd); /* point to rpc-reply */
else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL)
if ((xd = xml_new("data", NULL)) == NULL)
if ((xd = xml_new("data", NULL, NULL)) == NULL)
goto done;
if (xt){
if (xml_rm(xd) < 0)

View file

@ -32,6 +32,8 @@
***** END LICENSE BLOCK *****
* XML support functions.
* @see https://www.w3.org/TR/2008/REC-xml-20081126
* https://www.w3.org/TR/2009/REC-xml-names-20091208
*/
#ifdef HAVE_CONFIG_H
@ -86,12 +88,10 @@ struct xml{
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_* above */
void *x_spec; /* Pointer to specification, eg yang, by
yang_stmt *x_spec; /* Pointer to specification, eg yang, by
reference, dont free */
cg_var *x_cv; /* If body this contains the typed value */
#if (XML_CHILD_HASH==1)
clicon_hash_t *x_hash; /* Hash of children */
#endif
};
/* Mapping between xml type <--> string */
@ -103,6 +103,11 @@ static const map_str2int xsmap[] = {
{NULL, -1}
};
/* Hash for XML trees list entries
* Experimental
*/
int xml_child_hash = 0;
/*! Translate from xml type in enum form to string keyword
* @param[in] type Xml type
* @retval str String keyword
@ -490,57 +495,44 @@ xml_childvec_get(cxobj *x)
return x->x_childvec;
}
/*! Create new xml node given a name and parent. Free it with xml_free().
/*! Create new xml node given a name and parent. Free with xml_free().
*
* @param[in] name Name of new
* @param[in] xp The parent where the new xml node should be inserted
* @retval xml Created xml object if successful
* @param[in] spec Yang stmt or NULL.
* @retval xml Created xml object if successful. Free with xml_free()
* @retval NULL Error and clicon_err() called
* @code
* cxobj *x;
* if ((x = xml_new(name, xparent)) == NULL)
* if ((x = xml_new(name, xparent, NULL)) == NULL)
* err;
* ...
* xml_free(x);
* @endcode
* @see xml_new_spec Also sets yang spec.
* @note yspec may be NULL either because it is not known or it is irrelevant,
* eg for body or attribute
*/
cxobj *
xml_new(char *name,
cxobj *xp)
cxobj *xp,
void *spec)
{
cxobj *xn;
cxobj *x;
if ((xn=malloc(sizeof(cxobj))) == NULL){
if ((x = malloc(sizeof(cxobj))) == NULL){
clicon_err(OE_XML, errno, "%s: malloc", __FUNCTION__);
return NULL;
}
memset(xn, 0, sizeof(cxobj));
if ((xml_name_set(xn, name)) < 0)
memset(x, 0, sizeof(cxobj));
if ((xml_name_set(x, name)) < 0)
return NULL;
xml_parent_set(xn, xp);
if (xp)
if (xml_child_append(xp, xn) < 0)
return NULL;
return xn;
}
/*! Create new xml node given a name, parent and spec.
* @param[in] name Name of new xml node
* @param[in] xp XML parent
* @param[in] spec Yang spec
* @retval NULL Error
* @retval x XML tree. Free with xml_free().
*/
cxobj *
xml_new_spec(char *name,
cxobj *xp,
void *spec)
{
cxobj *x;
if ((x = xml_new(name, xp)) == NULL)
xml_parent_set(x, xp);
if (xp && xml_child_append(xp, x) < 0)
return NULL;
x->x_spec = spec; /* Can be NULL */
if (xml_child_hash && xml_hash_add(x) < 0)
return NULL;
x->x_spec = spec;
return x;
}
@ -630,7 +622,7 @@ xml_insert(cxobj *xp,
{
cxobj *xc; /* new child */
if ((xc = xml_new(tag, NULL)) == NULL)
if ((xc = xml_new(tag, NULL, NULL)) == NULL)
goto catch;
while (xp->x_childvec_len)
if (xml_addsub(xc, xml_child_i(xp, 0)) < 0)
@ -658,9 +650,8 @@ xml_purge(cxobj *xc)
int i;
cxobj *xp;
#if (XML_CHILD_HASH==1)
xml_hash_op(xc, 0);
#endif
if (xml_child_hash)
xml_hash_rm_entry(xc);
if ((xp = xml_parent(xc)) != NULL){
/* Find child order i in parent*/
for (i=0; i<xml_child_nr(xp); i++)
@ -923,10 +914,8 @@ xml_free(cxobj *x)
x->x_childvec[i] = NULL;
}
}
#if (XML_CHILD_HASH==1)
if (x->x_hash)
hash_free(x->x_hash);
#endif
if (x->x_childvec)
free(x->x_childvec);
free(x);
@ -1061,22 +1050,29 @@ clicon_xml2cbuf(cbuf *cb,
/*! Basic xml parsing function.
* @param[in] str Pointer to string containing XML definition.
* @param[in] yspec Yang specification or NULL
* @param[out] xtop Top of XML parse tree. Assume created.
* @see clicon_xml_parse_file clicon_xml_parse_string
*/
int
xml_parse(char *str,
cxobj *xt)
xml_parse(char *str,
yang_spec *yspec,
cxobj *xt)
{
int retval = -1;
struct xml_parse_yacc_arg ya = {0,};
if (xt == NULL){
clicon_err(OE_XML, errno, "Unexpected NULL XML");
return -1;
}
if ((ya.ya_parse_string = strdup(str)) == NULL){
clicon_err(OE_XML, errno, "%s: strdup", __FUNCTION__);
clicon_err(OE_XML, errno, "strdup");
return -1;
}
ya.ya_xparent = xt;
ya.ya_skipspace = 1; /* remove all non-terminal bodies (strip pretty-print) */
ya.ya_yspec = yspec;
if (clixon_xml_parsel_init(&ya) < 0)
goto done;
if (clixon_xml_parseparse(&ya) != 0) /* yacc returns 1 on error */
@ -1128,8 +1124,7 @@ xmltree2cbuf(cbuf *cb,
return 0;
}
/*
* FSM to detect a substring
/*! FSM to detect a substring
*/
static inline int
FSM(char *tag,
@ -1147,7 +1142,8 @@ FSM(char *tag,
* @param[in] fd A file descriptor containing the XML file (as ASCII characters)
* @param[out] xt Pointer to an (on entry empty) pointer to an XML parse tree
* _created_ by this function.
* @param endtag Read until you encounter "endtag" in the stream
* @param[in] endtag Read until you encounter "endtag" in the stream, or NULL
* @param[in] yspec Yang specification, or NULL
* @retval 0 OK
* @retval -1 Error with clicon_err called
*
@ -1165,9 +1161,10 @@ FSM(char *tag,
* May block
*/
int
clicon_xml_parse_file(int fd,
cxobj **cx,
char *endtag)
clicon_xml_parse_file(int fd,
char *endtag,
yang_spec *yspec,
cxobj **xt)
{
int retval = -1;
int ret;
@ -1176,15 +1173,13 @@ clicon_xml_parse_file(int fd,
char *xmlbuf = NULL;
char *ptr;
int maxbuf = BUFLEN;
int endtaglen = strlen(endtag);
int endtaglen = 0;
int state = 0;
int oldmaxbuf;
if (endtag == NULL){
clicon_err(OE_XML, 0, "%s: endtag required\n", __FUNCTION__);
goto done;
}
*cx = NULL;
if (endtag != NULL)
endtaglen = strlen(endtag);
*xt = NULL;
if ((xmlbuf = malloc(maxbuf)) == NULL){
clicon_err(OE_XML, errno, "%s: malloc", __FUNCTION__);
goto done;
@ -1199,16 +1194,17 @@ clicon_xml_parse_file(int fd,
break;
}
if (ret != 0){
state = FSM(endtag, ch, state);
if (endtag)
state = FSM(endtag, ch, state);
xmlbuf[len++] = ch;
}
if (ret == 0 || state == endtaglen){
if (ret == 0 ||
(endtag && (state == endtaglen))){
state = 0;
if ((*cx = xml_new("top", NULL)) == NULL)
break;
if (xml_parse(ptr, *cx) < 0){
if ((*xt = xml_new("top", NULL, NULL)) == NULL)
break;
if (xml_parse(ptr, yspec, *xt) < 0)
goto done;
}
break;
}
if (len>=maxbuf-1){ /* Space: one for the null character */
@ -1224,14 +1220,13 @@ clicon_xml_parse_file(int fd,
} /* while */
retval = 0;
done:
if (retval < 0 && *cx){
free(*cx);
*cx = NULL;
if (retval < 0 && *xt){
free(*xt);
*xt = NULL;
}
if (xmlbuf)
free(xmlbuf);
return retval;
// return (*cx)?0:-1;
}
/*! Read an XML definition from string and parse it into a parse-tree.
@ -1252,12 +1247,13 @@ clicon_xml_parse_file(int fd,
* @note you need to free the xml parse tree after use, using xml_free()
*/
int
clicon_xml_parse_str(char *str,
cxobj **cxtop)
clicon_xml_parse_str(char *str,
yang_spec *yspec,
cxobj **cxtop)
{
if ((*cxtop = xml_new("top", NULL)) == NULL)
return -1;
return xml_parse(str, *cxtop);
if ((*cxtop = xml_new("top", NULL, NULL)) == NULL)
return -1;
return xml_parse(str, yspec, *cxtop);
}
@ -1266,6 +1262,7 @@ clicon_xml_parse_str(char *str,
* Utility function using stdarg instead of static string.
* @param[out] xml_top Top of XML parse tree. Will add extra top element called 'top'.
* you must free it after use, using xml_free()
* @param[in] yspec Yang specification, or NULL
* @param[in] format Pointer to string containing XML definition.
* @retval 0 OK
@ -1281,8 +1278,9 @@ clicon_xml_parse_str(char *str,
* @note you need to free the xml parse tree after use, using xml_free()
*/
int
clicon_xml_parse(cxobj **cxtop,
char *format, ...)
clicon_xml_parse(cxobj **cxtop,
yang_spec *yspec,
char *format, ...)
{
int retval = -1;
va_list args;
@ -1300,9 +1298,9 @@ clicon_xml_parse(cxobj **cxtop,
va_start(args, format);
len = vsnprintf(str, len, format, args) + 1;
va_end(args);
if ((*cxtop = xml_new("top", NULL)) == NULL)
if ((*cxtop = xml_new("top", NULL, NULL)) == NULL)
return -1;
if (xml_parse(str, *cxtop) < 0)
if (xml_parse(str, yspec, *cxtop) < 0)
goto done;
retval = 0;
done:
@ -1313,28 +1311,28 @@ clicon_xml_parse(cxobj **cxtop,
/*! Copy single xml node without copying children
*/
static int
copy_one(cxobj *xn0,
cxobj *xn1)
int
xml_copy_one(cxobj *x0,
cxobj *x1)
{
cg_var *cv1;
xml_type_set(xn1, xml_type(xn0));
if (xml_value(xn0)){ /* malloced string */
if ((xn1->x_value = strdup(xn0->x_value)) == NULL){
xml_type_set(x1, xml_type(x0));
if (xml_value(x0)){ /* malloced string */
if ((x1->x_value = strdup(x0->x_value)) == NULL){
clicon_err(OE_XML, errno, "%s: strdup", __FUNCTION__);
return -1;
}
}
if (xml_name(xn0)) /* malloced string */
if ((xml_name_set(xn1, xml_name(xn0))) < 0)
if (xml_name(x0)) /* malloced string */
if ((xml_name_set(x1, xml_name(x0))) < 0)
return -1;
if (xml_cv_get(xn0)){
if ((cv1 = cv_dup(xml_cv_get(xn0))) == NULL){
if (xml_cv_get(x0)){
if ((cv1 = cv_dup(xml_cv_get(x0))) == NULL){
clicon_err(OE_XML, errno, "%s: cv_dup", __FUNCTION__);
return -1;
}
if ((xml_cv_set(xn1, cv1)) < 0)
if ((xml_cv_set(x1, cv1)) < 0)
return -1;
}
return 0;
@ -1345,8 +1343,9 @@ copy_one(cxobj *xn0,
* x1 should be a created placeholder. If x1 is non-empty,
* the copied tree is appended to the existing tree.
* @code
* x1 = xml_new("new", xparent);
* xml_copy(x0, x1);
* x1 = xml_new("new", xparent, NULL);
* if (xml_copy(x0, x1) < 0)
* err;
* @endcode
*/
int
@ -1357,11 +1356,11 @@ xml_copy(cxobj *x0,
cxobj *x;
cxobj *xcopy;
if (copy_one(x0, x1) <0)
if (xml_copy_one(x0, x1) <0)
goto done;
x = NULL;
while ((x = xml_child_each(x0, x, -1)) != NULL) {
if ((xcopy = xml_new(xml_name(x), x1)) == NULL)
if ((xcopy = xml_new(xml_name(x), x1, xml_spec(x))) == NULL)
goto done;
if (xml_copy(x, xcopy) < 0) /* recursion */
goto done;
@ -1384,7 +1383,7 @@ xml_dup(cxobj *x0)
{
cxobj *x1;
if ((x1 = xml_new("new", NULL)) == NULL)
if ((x1 = xml_new("new", NULL, xml_spec(x0))) == NULL)
return NULL;
if (xml_copy(x0, x1) < 0)
return NULL;
@ -1720,7 +1719,32 @@ xml_operation2str(enum operation_type op)
}
}
#if (XML_CHILD_HASH==1)
/*! Given an XML object and a child name, return yang stmt of child
* If no xml parent, find root yang stmt matching name
* @param[in] name Name of child
* @param[in] xp XML parent, can be NULL.
* @param[in] yspec Yang specification (top level)
* @param[out] yresult Pointer to yang stmt of result, or NULL, if not found
*/
int
xml_child_spec(char *name,
cxobj *xp,
yang_spec *yspec,
yang_stmt **yresult)
{
yang_stmt *y; /* result yang node */
yang_stmt *yparent; /* parent yang */
if (xp && (yparent = xml_spec(xp)) != NULL)
y = yang_find_datanode((yang_node*)yparent, name);
else if (yspec)
y = yang_find_topnode(yspec, name, 0); /* still NULL for config */
else
y = NULL;
*yresult = y;
return 0;
}
/*! Return yang hash
* Not necessarily set. Either has not been set yet (by xml_spec_set( or anyxml.
*/
@ -1730,7 +1754,7 @@ xml_hash(cxobj *x)
return x->x_hash;
}
int
static int
xml_hash_init(cxobj *x)
{
if ((x->x_hash = hash_init()) < 0)
@ -1738,8 +1762,12 @@ xml_hash_init(cxobj *x)
return 0;
}
/*! XML remove hash only. Not in parent.
* @param[in] x XML object
* eg same as xml_hash_op(x, -1)
*/
int
xml_hash_rm(cxobj *x)
xml_hash_rm_only(cxobj *x)
{
if (x && x->x_hash){
hash_free(x->x_hash);
@ -1819,7 +1847,7 @@ xml_hash_key(cxobj *x,
/*! XML hash add. Create hash and add key/value to parent
*
* @param[in] arg -1: rm only hash 0: rm entry, 1: add
* @param[in] arg 0: rm entry, 1: add
* Typically called for a whole tree.
*/
int
@ -1837,10 +1865,8 @@ xml_hash_op(cxobj *x,
if (op==1)
xml_hash_init(x);
}
else if (op==-1|| op==0)
xml_hash_rm(x);
if (op==-1)
goto ok;
else if (op==0)
xml_hash_rm_only(x);
if ((xp = xml_parent(x)) == NULL)
goto ok;
if ((ph = xml_hash(xp))==NULL)
@ -1871,8 +1897,85 @@ xml_hash_op(cxobj *x,
return retval;
}
#endif
/*! XML hash add. Create hash and add key/value to parent
*
* @param[in] x XML object
* eg same as xml_hash_op(x, 1)
*/
int
xml_hash_add(cxobj *x)
{
int retval = -1;
cxobj *xp;
clicon_hash_t *ph;
yang_stmt *y;
cbuf *key = NULL; /* cligen buffer hash key */
if ((ph = xml_hash(x))==NULL){
xml_hash_init(x);
ph = xml_hash(x);
}
if ((xp = xml_parent(x)) == NULL)
goto ok;
if ((y = xml_spec(x)) == NULL)
goto ok;
if ((key = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (xml_hash_key(x, y, key) < 0)
goto done;
if (cbuf_len(key)){
if (hash_add(ph, cbuf_get(key), &x, sizeof(x)) == NULL)
goto done;
}
ok:
retval = 0;
done:
if (key)
cbuf_free(key);
return retval;
}
/*! XML hash rm. Create hash and add key/value to parent
*
* @param[in] x XML object
* eg same as xml_hash_op(x, 0)
*/
int
xml_hash_rm_entry(cxobj *x)
{
int retval = -1;
cxobj *xp;
clicon_hash_t *ph;
yang_stmt *y;
cbuf *key = NULL; /* cligen buffer hash key */
if (xml_hash(x)!=NULL)
xml_hash_rm_only(x);
if ((xp = xml_parent(x)) == NULL)
goto ok;
if ((ph = xml_hash(xp))==NULL)
goto ok;
if ((y = xml_spec(x)) == NULL)
goto ok;
if ((key = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (xml_hash_key(x, y, key) < 0)
goto done;
if (cbuf_len(key)){
if (hash_del(ph, cbuf_get(key)) < 0)
goto done;
}
ok:
retval = 0;
done:
if (key)
cbuf_free(key);
return retval;
}
/*
* Turn this on to get a xml parse and pretty print test program

View file

@ -56,8 +56,8 @@
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_xml.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_plugin.h"
#include "clixon_options.h"
#include "clixon_xml_db.h"

View file

@ -561,7 +561,7 @@ cvec2xml_1(cvec *cvv,
cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL)
len++;
if ((xt = xml_new(toptag, xp)) == NULL)
if ((xt = xml_new(toptag, xp, NULL)) == NULL)
goto err;
if (xml_childvec_set(xt, len) < 0)
goto err;
@ -570,11 +570,11 @@ cvec2xml_1(cvec *cvv,
while ((cv = cvec_each(cvv, cv)) != NULL) {
if (cv_type_get(cv)==CGV_ERR || cv_name_get(cv) == NULL)
continue;
if ((xn = xml_new(cv_name_get(cv), NULL)) == NULL) /* this leaks */
if ((xn = xml_new(cv_name_get(cv), NULL, NULL)) == NULL) /* this leaks */
goto err;
xml_parent_set(xn, xt);
xml_child_i_set(xt, i++, xn);
if ((xb = xml_new("body", xn)) == NULL) /* this leaks */
if ((xb = xml_new("body", xn, NULL)) == NULL) /* this leaks */
goto err;
xml_type_set(xb, CX_BODY);
val = cv2str_dup(cv);
@ -620,35 +620,35 @@ match_base_child(cxobj *x0,
int equal;
char **b1vec = NULL;
int i;
#if (XML_CHILD_HASH==1)
cxobj **p;
cbuf *key = NULL; /* cligen buffer hash key */
size_t vlen;
*x0cp = NULL; /* return value */
if (xml_hash(x0) == NULL)
goto nohash;
if ((key = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done1;
}
if (xml_hash_key(x1c, yc, key) < 0)
goto done;
x0c = NULL;
if (cbuf_len(key))
if ((p = hash_value(xml_hash(x0), cbuf_get(key), &vlen)) != NULL){
assert(vlen == sizeof(x0c));
x0c = *p;
if (xml_child_hash){
*x0cp = NULL; /* return value */
if (xml_hash(x0) == NULL)
goto nohash;
if ((key = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done1;
}
// fprintf(stderr, "%s get %s = 0x%x\n", __FUNCTION__, cbuf_get(key), (unsigned int)x0c);
*x0cp = x0c;
retval = 0;
done1:
if (key)
cbuf_free(key);
return retval;
if (xml_hash_key(x1c, yc, key) < 0)
goto done;
x0c = NULL;
if (cbuf_len(key))
if ((p = hash_value(xml_hash(x0), cbuf_get(key), &vlen)) != NULL){
assert(vlen == sizeof(x0c));
x0c = *p;
}
// fprintf(stderr, "%s get %s = 0x%x\n", __FUNCTION__, cbuf_get(key), (unsigned int)x0c);
*x0cp = x0c;
retval = 0;
done1:
if (key)
cbuf_free(key);
return retval;
}
nohash:
#endif /* XML_CHILD_HASH */
*x0cp = NULL; /* return value */
x1cname = xml_name(x1c);
switch (yc->ys_keyword){
@ -1328,9 +1328,9 @@ xml_default(cxobj *xt,
assert(y->ys_cv);
if (!cv_flag(y->ys_cv, V_UNSET)){ /* Default value exists */
if (!xml_find(xt, y->ys_argument)){
if ((xc = xml_new_spec(y->ys_argument, xt, y)) == NULL)
if ((xc = xml_new(y->ys_argument, xt, y)) == NULL)
goto done;
if ((xb = xml_new("body", xc)) == NULL)
if ((xb = xml_new("body", xc, NULL)) == NULL)
goto done;
xml_type_set(xb, CX_BODY);
if ((str = cv2str_dup(y->ys_cv)) == NULL){
@ -1458,6 +1458,7 @@ xml_non_config_data(cxobj *xt,
return retval;
}
/*! Add yang specification backpoint to XML node
* @param[in] xt XML tree node
* @param[in] arg Yang spec
@ -1474,21 +1475,21 @@ xml_spec_populate(cxobj *x,
{
int retval = -1;
yang_spec *yspec = (yang_spec*)arg;
char *name;
yang_stmt *y=NULL; /* yang node */
cxobj *xp; /* xml parent */
yang_stmt *yp; /* parent yang */
name = xml_name(x);
if (xml_child_spec(xml_name(x), xml_parent(x), yspec, &y) < 0)
goto done;
#if 0
if ((xp = xml_parent(x)) != NULL &&
(yp = xml_spec(xp)) != NULL)
y = yang_find_datanode((yang_node*)yp, xml_name(x));
else
y = yang_find_topnode(yspec, name, 0); /* still NULL for config */
#endif
if (y)
xml_spec_set(x, y);
retval = 0;
// done:
done:
return retval;
}
@ -1700,10 +1701,10 @@ api_path2xml_vec(char **vec,
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
goto done;
}
if ((x = xml_new_spec(y->ys_argument, x0, y)) == NULL)
if ((x = xml_new(y->ys_argument, x0, y)) == NULL)
goto done;
xml_type_set(x, CX_ELMNT);
if ((xb = xml_new("body", x)) == NULL)
if ((xb = xml_new("body", x, NULL)) == NULL)
goto done;
xml_type_set(xb, CX_BODY);
if (restval && xml_value_set(xb, restval) < 0)
@ -1738,18 +1739,17 @@ api_path2xml_vec(char **vec,
}
cvi = NULL;
/* create list object */
if ((x = xml_new_spec(name, x0, y)) == NULL)
if ((x = xml_new(name, x0, y)) == NULL)
goto done;
xml_type_set(x, CX_ELMNT);
j = 0;
/* Create keys */
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
keyname = cv_string_get(cvi);
if ((xn = xml_new(keyname, x)) == NULL)
if ((xn = xml_new(keyname, x, NULL)) == NULL)
goto done;
xml_type_set(xn, CX_ELMNT);
if ((xb = xml_new("body", xn)) == NULL)
if ((xb = xml_new("body", xn, NULL)) == NULL)
goto done;
xml_type_set(xb, CX_BODY);
val2 = valvec?valvec[j++]:NULL;
@ -1762,7 +1762,7 @@ api_path2xml_vec(char **vec,
}
break;
default: /* eg Y_CONTAINER, Y_LEAF */
if ((x = xml_new_spec(name, x0, y)) == NULL)
if ((x = xml_new(name, x0, y)) == NULL)
goto done;
xml_type_set(x, CX_ELMNT);
break;
@ -1858,17 +1858,17 @@ xml_merge1(cxobj *x0,
if (y0->yn_keyword == Y_LEAF_LIST || y0->yn_keyword == Y_LEAF){
x1bstr = xml_body(x1);
if (x0==NULL){
if ((x0 = xml_new_spec(x1name, x0p, y0)) == NULL)
if ((x0 = xml_new(x1name, x0p, y0)) == NULL)
goto done;
if (x1bstr){ /* empty type does not have body */
if ((x0b = xml_new("body", x0)) == NULL)
if ((x0b = xml_new("body", x0, NULL)) == NULL)
goto done;
xml_type_set(x0b, CX_BODY);
}
}
if (x1bstr){
if ((x0b = xml_body_get(x0)) == NULL){
if ((x0b = xml_new("body", x0)) == NULL)
if ((x0b = xml_new("body", x0, NULL)) == NULL)
goto done;
xml_type_set(x0b, CX_BODY);
}
@ -1879,7 +1879,7 @@ xml_merge1(cxobj *x0,
} /* if LEAF|LEAF_LIST */
else { /* eg Y_CONTAINER, Y_LIST */
if (x0==NULL){
if ((x0 = xml_new_spec(x1name, x0p, y0)) == NULL)
if ((x0 = xml_new(x1name, x0p, y0)) == NULL)
goto done;
}
/* Loop through children of the modification tree */
@ -1992,6 +1992,7 @@ done:
return retval;
}
/*
* Turn this on for uni-test programs
* Usage: clixon_string join

View file

@ -32,6 +32,8 @@
***** END LICENSE BLOCK *****
* XML parser
* @see https://www.w3.org/TR/2008/REC-xml-20081126
* https://www.w3.org/TR/2009/REC-xml-names-20091208
*/
#ifndef _CLIXON_XML_PARSE_H_
#define _CLIXON_XML_PARSE_H_
@ -39,6 +41,7 @@
/*
* Types
*/
/*! XML parser yacc handler struct */
struct xml_parse_yacc_arg{
char *ya_parse_string; /* original (copy of) parse string */
int ya_linenum; /* Number of \n in parsed buffer */
@ -46,7 +49,8 @@ struct xml_parse_yacc_arg{
cxobj *ya_xelement; /* xml active element */
cxobj *ya_xparent; /* xml parent element*/
int ya_skipspace; /* If set, remove all non-terminal bodies (strip prettyr-print) */
int ya_skipspace; /* If set, remove all non-terminal bodies (strip pretty-print) */
yang_spec *ya_yspec; /* If set, top-level yang-spec */
};
extern char *clixon_xml_parsetext;

View file

@ -32,6 +32,8 @@
***** END LICENSE BLOCK *****
* XML parser
* @see https://www.w3.org/TR/2008/REC-xml-20081126
* https://www.w3.org/TR/2009/REC-xml-names-20091208
*/
%{

View file

@ -32,6 +32,8 @@
***** END LICENSE BLOCK *****
* XML parser
* @see https://www.w3.org/TR/2008/REC-xml-20081126
* https://www.w3.org/TR/2009/REC-xml-names-20091208
*/
%union {
char *string;
@ -46,7 +48,7 @@
%token BCOMMENT ECOMMENT
%type <string> val aid
%type <string> attvalue attqname
%lex-param {void *_ya} /* Add this argument to parse() and lex() function */
%parse-param {void *_ya}
@ -68,6 +70,10 @@
/* clicon */
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_xml_parse.h"
@ -79,8 +85,9 @@ clixon_xml_parseerror(void *_ya, char *s)
return;
}
/* note that we dont handle escaped characters correctly
there may also be some leakage here on NULL return
/*
* Note that we dont handle escaped characters correctly
* there may also be some leakage here on NULL return
*/
static int
xml_parse_content(struct xml_parse_yacc_arg *ya,
@ -92,7 +99,7 @@ xml_parse_content(struct xml_parse_yacc_arg *ya,
ya->ya_xelement = NULL; /* init */
if (xn == NULL){
if ((xn = xml_new("body", xp)) == NULL)
if ((xn = xml_new("body", xp, NULL)) == NULL)
goto done;
xml_type_set(xn, CX_BODY);
}
@ -105,7 +112,8 @@ xml_parse_content(struct xml_parse_yacc_arg *ya,
}
static int
xml_parse_version(struct xml_parse_yacc_arg *ya, char *ver)
xml_parse_version(struct xml_parse_yacc_arg *ya,
char *ver)
{
if(strcmp(ver, "1.0")){
clicon_err(OE_XML, errno, "Wrong XML version %s expected 1.0\n", ver);
@ -116,20 +124,35 @@ xml_parse_version(struct xml_parse_yacc_arg *ya, char *ver)
return 0;
}
/*! Parse Qualified name
* @param[in] ya XML parser yacc handler struct
* @param[in] prefix Prefix, namespace, or NULL
* @param[in] localpart Name
*/
static int
xml_parse_id(struct xml_parse_yacc_arg *ya, char *name, char *namespace)
xml_parse_qname(struct xml_parse_yacc_arg *ya,
char *prefix,
char *name)
{
if ((ya->ya_xelement=xml_new(name, ya->ya_xparent)) == NULL) {
if (namespace)
free(namespace);
free(name);
return -1;
}
xml_namespace_set(ya->ya_xelement, namespace);
if (namespace)
free(namespace);
int retval = -1;
cxobj *x;
yang_stmt *y=NULL; /* yang node */
cxobj *xp; /* xml parent */
xp = ya->ya_xparent;
if (xml_child_spec(name, xp, ya->ya_yspec, &y) < 0)
goto done;
if ((x = xml_new(name, xp, y)) == NULL)
goto done;
if (xml_namespace_set(x, prefix) < 0)
goto done;
ya->ya_xelement = x;
retval = 0;
done:
if (prefix)
free(prefix);
free(name);
return 0;
return retval;
}
static int
@ -250,39 +273,46 @@ xml_parse_bslash2(struct xml_parse_yacc_arg *ya,
return retval;
}
static int
xml_parse_attr(struct xml_parse_yacc_arg *ya,
char *qname,
char *attval)
{
int retval = -1;
cxobj *xa;
if ((xa = xml_new(qname, ya->ya_xelement, NULL)) == NULL)
goto done;
xml_type_set(xa, CX_ATTR);
if (xml_value_set(xa, attval) < 0)
goto done;
retval = 0;
done:
free(qname);
free(attval);
return retval;
}
/*! Parse Attribue Qualified name, Just transform prefix:name into a new string
*
*/
static char*
xml_parse_ida(struct xml_parse_yacc_arg *ya, char *namespace, char *name)
xml_merge_attqname(struct xml_parse_yacc_arg *ya,
char *prefix,
char *name)
{
char *str;
int len = strlen(namespace)+strlen(name)+2;
int len = strlen(prefix)+strlen(name)+2;
if ((str=malloc(len)) == NULL)
return NULL;
snprintf(str, len, "%s:%s", namespace, name);
free(namespace);
snprintf(str, len, "%s:%s", prefix, name);
free(prefix);
free(name);
return str;
}
static int
xml_parse_attr(struct xml_parse_yacc_arg *ya, char *id, char *val)
{
int retval = -1;
cxobj *xa;
if ((xa = xml_new(id, ya->ya_xelement)) == NULL)
goto done;
xml_type_set(xa, CX_ATTR);
if (xml_value_set(xa, val) < 0)
goto done;
retval = 0;
done:
free(id);
free(val);
return retval;
}
%}
%%
@ -309,22 +339,22 @@ encode : ENC '=' '\"' CHAR '\"' {free($4);}
| ENC '=' '\'' CHAR '\'' {free($4);}
;
emnt : '<' id attrs emnt1
{ clicon_debug(3, "emnt -> < id attrs emnt1"); }
element : '<' qname attrs element1
{ clicon_debug(3, "element -> < qname attrs element1"); }
;
id : NAME { if (xml_parse_id(_YA, $1, NULL) < 0) YYABORT;
clicon_debug(3, "id -> NAME %s", $1);}
| NAME ':' NAME { if (xml_parse_id(_YA, $3, $1) < 0) YYABORT;
clicon_debug(3, "id -> NAME : NAME");}
qname : NAME { if (xml_parse_qname(_YA, NULL, $1) < 0) YYABORT;
clicon_debug(3, "qname -> NAME %s", $1);}
| NAME ':' NAME { if (xml_parse_qname(_YA, $1, $3) < 0) YYABORT;
clicon_debug(3, "qname -> NAME : NAME");}
;
emnt1 : ESLASH {_YA->ya_xelement = NULL;
clicon_debug(3, "emnt1 -> />");}
| '>' { xml_parse_endslash_pre(_YA); }
list { xml_parse_endslash_mid(_YA); }
element1 : ESLASH {_YA->ya_xelement = NULL;
clicon_debug(3, "element1 -> />");}
| '>' { xml_parse_endslash_pre(_YA); }
list { xml_parse_endslash_mid(_YA); }
etg { xml_parse_endslash_post(_YA);
clicon_debug(3, "emnt1 -> > list etg");}
clicon_debug(3, "element1 -> > list etg");}
;
etg : BSLASH NAME '>'
@ -339,7 +369,7 @@ list : list content { clicon_debug(3, "list -> list content"); }
| content { clicon_debug(3, "list -> content"); }
;
content : emnt { clicon_debug(3, "content -> emnt"); }
content : element { clicon_debug(3, "content -> element"); }
| comment { clicon_debug(3, "content -> comment"); }
| CHAR { if (xml_parse_content(_YA, $1) < 0) YYABORT;
clicon_debug(3, "content -> CHAR %s", $1); }
@ -350,20 +380,20 @@ comment : BCOMMENT ECOMMENT
;
attrs : attrs att
attrs : attrs attr
|
;
attr : attqname '=' attvalue { if (xml_parse_attr(_YA, $1, $3) < 0) YYABORT; }
;
aid : NAME {$$ = $1;}
attqname : NAME {$$ = $1;}
| NAME ':' NAME
{ if (($$ = xml_parse_ida(_YA, $1, $3)) == NULL) YYABORT; }
{ if (($$ = xml_merge_attqname(_YA, $1, $3)) == NULL) YYABORT; }
;
att : aid '=' val { if (xml_parse_attr(_YA, $1, $3) < 0) YYABORT; }
;
val : '\"' CHAR '\"' { $$=$2; /* $2 must be consumed */}
attvalue : '\"' CHAR '\"' { $$=$2; /* $2 must be consumed */}
| '\"' '\"' { $$=strdup(""); /* $2 must be consumed */}
;

View file

@ -102,6 +102,10 @@ in
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_string.h"
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_xsl.h"

View file

@ -2077,7 +2077,7 @@ yang_spec_main(clicon_handle h,
* ...cv_string_get(cv);
* cvec_free(cvv);
* @endcode
* Note: must free return value after use w cvec_free
* @note must free return value after use w cvec_free
*/
cvec *
yang_arg2cvec(yang_stmt *ys,