C-style update: Unified comment, retvals in order, remove trailing spaces

Changed function name for `clicon_debug` functions
This commit is contained in:
Olof hagsand 2023-10-22 18:04:47 +02:00
parent 6e314dd96f
commit 62348fc9c7
204 changed files with 6047 additions and 4904 deletions

View file

@ -135,7 +135,7 @@ ys_grouping_module_resolve(yang_stmt *ymod,
while ((yinc = yn_each(yrealmod, yinc)) != NULL){
if (yang_keyword_get(yinc) != Y_INCLUDE)
continue;
submname = yang_argument_get(yinc);
submname = yang_argument_get(yinc);
if ((ysubm = yang_find_module_by_name(yspec, submname)) == NULL)
continue;
if (ysubm == ymod)
@ -157,8 +157,8 @@ ys_grouping_module_resolve(yang_stmt *ymod,
* @retval -1 Error, with clicon_err called
*/
int
ys_grouping_resolve(yang_stmt *yuses,
char *prefix,
ys_grouping_resolve(yang_stmt *yuses,
char *prefix,
char *name,
yang_stmt **ygrouping0)
{
@ -176,7 +176,7 @@ ys_grouping_resolve(yang_stmt *yuses,
if ((ymod = yang_find_module_by_prefix(yuses, prefix)) != NULL)
ygrouping = ys_grouping_module_resolve(ymod, yspec, name);
}
else {
else {
ys = yuses; /* Check upwards in hierarchy for matching groupings */
while (1){
if (ys->ys_mymodule){
@ -206,6 +206,8 @@ ys_grouping_resolve(yang_stmt *yuses,
*
* @param[in] h Clicon handle
* @param[in] ys The augment statement
* @retval 0 OK
* @retval -1 Error
* @see RFC7950 Sec 7.17
* The target node MUST be either a container, list, choice, case, input,
* output, or notification node.
@ -215,9 +217,9 @@ ys_grouping_resolve(yang_stmt *yuses,
* elements in the XML namespace of the module where the "augment" is
* specified.
*
* @note If the augment has a when statement, which is commonplace, the when statement is not copied as
* datanodes are, since it should not apply to the target node. Instead it is added as a special "when"
* struct to the yang statements being inserted.
* @note If the augment has a when statement, which is commonplace, the when statement is not
* copied as datanodes are, since it should not apply to the target node. Instead it is added as
* a special "when" struct to the yang statements being inserted.
*/
static int
yang_augment_node(clicon_handle h,
@ -240,14 +242,14 @@ yang_augment_node(clicon_handle h,
goto done;
}
schema_nodeid = yang_argument_get(ys);
clicon_debug(CLIXON_DBG_DETAIL, "%s %s", __FUNCTION__, schema_nodeid);
clixon_debug(CLIXON_DBG_DETAIL, "%s %s", __FUNCTION__, schema_nodeid);
/* Find the target */
if (yang_abs_schema_nodeid(ys, schema_nodeid, &ytarget) < 0)
goto done;
if (ytarget == NULL){
if (clicon_option_bool(h, "CLICON_YANG_AUGMENT_ACCEPT_BROKEN")){
/* Log a warning and proceed if augment target not found
/* Log a warning and proceed if augment target not found
* This may be necessary with some broken models
*/
clicon_log(LOG_WARNING, "Warning: Augment failed in module %s: target node %s not found",
@ -262,7 +264,7 @@ yang_augment_node(clicon_handle h,
clicon_err(OE_YANG, 0, "Augment failed in module %s: target node %s not found",
yang_argument_get(ys_module(ys)),
schema_nodeid);
goto done;
goto done;
}
}
/* The target node MUST be either a container, list, choice, case, input, output, or notification node.
@ -316,7 +318,7 @@ yang_augment_node(clicon_handle h,
notification node, the "container", "leaf", "list", "leaf-list",
"uses", and "choice" statements can be used within the "augment"
statement. */
if (childkey != Y_CONTAINER && childkey != Y_LEAF && childkey != Y_LIST &&
if (childkey != Y_CONTAINER && childkey != Y_LEAF && childkey != Y_LIST &&
childkey != Y_LEAF_LIST && childkey != Y_USES && childkey != Y_CHOICE &&
childkey != Y_UNKNOWN){
clicon_log(LOG_WARNING, "Warning: Augment failed in module %s: node %s %d cannot be added to target node %s",
@ -365,9 +367,9 @@ yang_augment_node(clicon_handle h,
* see xml_yang_validate_all
*/
if (ywhen){
if (yang_when_xpath_set(yc, wxpath) < 0)
if (yang_when_xpath_set(yc, wxpath) < 0)
goto done;
if (yang_when_nsc_set(yc, wnsc) < 0)
if (yang_when_nsc_set(yc, wnsc) < 0)
goto done;
}
/* Note: ys_populate2 called as a special case here since the inserted child is
@ -425,6 +427,8 @@ yang_augment_module(clicon_handle h,
* Most nodes will be replaced, but some are added
* @param[in] yr Refine node
* @param[in] yt Refine target node (will be modified)
* @retval 0 OK
* @retval -1 Error
* @see RFC7950 Sec 7.13.2
* There may be some missed cornercases
*/
@ -438,7 +442,7 @@ ys_do_refine(yang_stmt *yr,
yang_stmt *ytc; /* target child */
enum rfc_6020 keyw;
int i;
/* Loop through refine node children. First if remove do that first
* In some cases remove a set of nodes.
*/
@ -467,7 +471,7 @@ ys_do_refine(yang_stmt *yr,
}
/* fall through and add if not found */
case Y_MUST: /* keep old, add new */
case Y_IF_FEATURE:
case Y_IF_FEATURE:
break;
default:
break;
@ -493,8 +497,8 @@ ys_do_refine(yang_stmt *yr,
* Could be made to a generic function used elsewhere as well
* @param[in] y Yang leaf
* @param[in] yp Yang list parent
* @retval 0 No, y is not a key leaf in list yp
* @retval 1 Yes, y is a key leaf in list yp
* @retval 0 No, y is not a key leaf in list yp
*/
static int
ys_iskey(yang_stmt *y,
@ -502,7 +506,7 @@ ys_iskey(yang_stmt *y,
{
cvec *cvv;
cg_var *cv;
char *name;
char *name;
if (yang_keyword_get(y) != Y_LEAF)
return 0;
@ -547,14 +551,14 @@ yang_expand_uses_node(yang_stmt *yn,
yang_stmt *ywhen;
char *wxpath = NULL; /* xpath of when statement */
cvec *wnsc = NULL; /* namespace context of when statement */
/* Split argument into prefix and name */
if (nodeid_split(yang_argument_get(ys), &prefix, &id) < 0)
goto done;
if (ys_grouping_resolve(ys, prefix, id, &ygrouping) < 0)
goto done;
if (ygrouping == NULL){
clicon_log(LOG_NOTICE, "%s: Yang error : grouping \"%s\" not found in module \"%s\"",
clicon_log(LOG_NOTICE, "%s: Yang error : grouping \"%s\" not found in module \"%s\"",
__FUNCTION__, yang_argument_get(ys), yang_argument_get(ys_module(ys)));
goto done;
}
@ -570,7 +574,7 @@ yang_expand_uses_node(yang_stmt *yn,
goto done;
}
} while((yp = yang_parent_get(yp)) != NULL);
if (yang_flag_get(ygrouping, YANG_FLAG_GROUPING) == 0){
if (yang_flag_get(ygrouping, YANG_FLAG_GROUPING) == 0){
/* Check mark flag to see if this grouping has been expanded before,
* here below in the traverse section
* A mark could be completely normal (several uses) or it could be a recursion.
@ -642,7 +646,7 @@ yang_expand_uses_node(yang_stmt *yn,
&yrt) < 0)
goto done;
/* Not found, try next */
if (yrt == NULL)
if (yrt == NULL)
continue;
/* Refine ANYDATA does not make sense */
if (yang_keyword_get(yrt) == Y_ANYDATA || yang_keyword_get(yrt) == Y_ANYXML)
@ -672,16 +676,15 @@ yang_expand_uses_node(yang_stmt *yn,
* If a key leaf is defined in a grouping that is used in a list, the
* "uses" statement MUST NOT have a "when" statement.
*/
clicon_err(OE_YANG, 0, "Key leaf '%s' defined in grouping '%s' is used in a 'uses' statement, This is not allowed according to RFC 7950 Sec 7.21.5",
yang_argument_get(yg),
yang_argument_get(ygrouping)
);
goto done;
}
if (yang_when_xpath_set(yg, wxpath) < 0)
if (yang_when_xpath_set(yg, wxpath) < 0)
goto done;
if (yang_when_nsc_set(yg, wnsc) < 0)
if (yang_when_nsc_set(yg, wnsc) < 0)
goto done;
}
/* This is for extensions that allow list keys to be optional, see restconf_main_extension_cb */
@ -700,9 +703,9 @@ yang_expand_uses_node(yang_stmt *yn,
if (wnsc)
cvec_free(wnsc);
if (prefix)
free(prefix);
free(prefix);
if (id)
free(id);
free(id);
return retval;
}
@ -730,7 +733,7 @@ yang_expand_grouping(yang_stmt *yn)
/* Cannot use yang_apply here since child-list is modified (is destructive) */
i = 0;
while (i < yang_len_get(yn)){
ys = yn->ys_stmt[i];
ys = yn->ys_stmt[i];
switch (yang_keyword_get(ys)){
case Y_USES:
if (yang_flag_get(ys, YANG_FLAG_GROUPING) == 0){
@ -738,7 +741,7 @@ yang_expand_grouping(yang_stmt *yn)
goto done;
yang_flag_set(ys, YANG_FLAG_GROUPING);
}
break;
break;
default:
break;
}
@ -816,9 +819,9 @@ yang_parse_str(char *str,
goto done;
}
if (yang_parse_exit(&yy) < 0)
goto done;
goto done;
if (yang_scan_exit(&yy) < 0)
goto done;
goto done;
}
if ((ymod = yy.yy_module) == NULL){
clicon_err(OE_YANG, 0, "No module in YANG %s", name);
@ -873,7 +876,7 @@ yang_parse_file(FILE *fp,
if ((buf = realloc(buf, 2*len)) == NULL){
clicon_err(OE_XML, errno, "realloc");
goto done;
}
}
memset(buf+len, 0, len);
len *= 2;
}
@ -892,6 +895,8 @@ yang_parse_file(FILE *fp,
* @param[in] filename Filename on the form: name [+ @rev ] + .yang
* @param[out] basep "Base" filename, stripped: [+ @rev ] + .yang (malloced)
* @param[out] revp Revision as YYYYMMDD (0 if not found)
* @retval 0 OK
* @retval -1 Error
*/
static int
filename2revision(const char *filename,
@ -901,13 +906,13 @@ filename2revision(const char *filename,
int retval = -1;
char *base = NULL;
char *p;
/* base = module name [+ @rev ] + .yang */
if ((base = strdup(filename)) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
clicon_debug(CLIXON_DBG_DETAIL, "%s %s", __FUNCTION__, base);
clixon_debug(CLIXON_DBG_DETAIL, "%s %s", __FUNCTION__, base);
if ((p = rindex(base, '.')) != NULL) /* strip postfix .yang */
*p = '\0';
if ((p = index(base, '@')) != NULL){ /* extract revision date */
@ -938,10 +943,10 @@ filename2revision(const char *filename,
* @note for bootstrapping, dir may have to be set.
*/
int
yang_file_find_match(clicon_handle h,
yang_file_find_match(clicon_handle h,
const char *module,
const char *revision,
cbuf *fbuf)
cbuf *fbuf)
{
int retval = -1;
cbuf *regex = NULL;
@ -977,8 +982,8 @@ yang_file_find_match(clicon_handle h,
dir = xml_body(xc);
/* get all matching files in this directory */
if ((ndp = clicon_file_dirent(dir,
&dp,
if ((ndp = clicon_file_dirent(dir,
&dp,
cbuf_get(regex),
S_IFREG)) < 0)
goto done;
@ -1051,20 +1056,20 @@ done:
*/
yang_stmt *
yang_parse_filename(clicon_handle h,
const char *filename,
const char *filename,
yang_stmt *yspec)
{
yang_stmt *ymod = NULL;
FILE *fp = NULL;
struct stat st;
clicon_debug(1, "%s %s", __FUNCTION__, filename);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s", __FUNCTION__, filename);
if (stat(filename, &st) < 0){
clicon_err(OE_YANG, errno, "%s not found", filename);
goto done;
}
if ((fp = fopen(filename, "r")) == NULL){
clicon_err(OE_YANG, errno, "fopen(%s)", filename);
clicon_err(OE_YANG, errno, "fopen(%s)", filename);
goto done;
}
if ((ymod = yang_parse_file(fp, filename, yspec)) < 0)
@ -1094,8 +1099,8 @@ yang_parse_filename(clicon_handle h,
*/
yang_stmt *
yang_parse_module(clicon_handle h,
const char *module,
const char *revision,
const char *module,
const char *revision,
yang_stmt *yspec,
char *origname)
{
@ -1134,7 +1139,7 @@ yang_parse_module(clicon_handle h,
goto done;
/* Sanity check that requested module name matches loaded module
* If this does not match, the filename and containing module do not match
* RFC 7950 Sec 5.2
* RFC 7950 Sec 5.2
*/
if (strcmp(yang_argument_get(ymod), module) != 0){
clicon_err(OE_YANG, EINVAL, "File %s contains yang module \"%s\" which does not match expected module %s",
@ -1153,8 +1158,8 @@ yang_parse_module(clicon_handle h,
if (filename2revision(filename, NULL, &revf) < 0)
goto done;
/* Sanity check that file revision does not match internal rev stmt */
if (revf && revm && revm != revf){
clicon_err(OE_YANG, EINVAL, "Yang module file revision and in yang does not match: %s vs %u", filename, revm);
if (revf && revm && revm != revf){
clicon_err(OE_YANG, EINVAL, "Yang module file revision and in yang does not match: %s vs %u", filename, revm);
ymod = NULL;
goto done;
}
@ -1236,7 +1241,7 @@ yang_parse_recurse(clicon_handle h,
}
}
retval = 0;
done:
done:
return retval; /* top-level (sub)module */
}
@ -1244,12 +1249,14 @@ yang_parse_recurse(clicon_handle h,
*
* @param[in] h Clicon handle
* @param[in] ys Yang statement
* @retval 0 OK
* @retval -1 Error
* Verify the following rule:
* RFC 7950 7.8.2: The "key" statement, which MUST be present if the list represents
* configuration and MAY be present otherwise
* Unless it is the "errors" rule of the ietf-restconf spec which seems to be a special case.
*/
static int
static int
ys_list_check(clicon_handle h,
yang_stmt *ys)
{
@ -1258,7 +1265,7 @@ ys_list_check(clicon_handle h,
yang_stmt *yc = NULL;
enum rfc_6020 keyw;
yang_stmt *yroot;
/* This node is state, not config */
if (yang_config_ancestor(ys) == 0)
goto ok;
@ -1266,7 +1273,6 @@ ys_list_check(clicon_handle h,
if ((yroot = yang_myroot(ys)) != NULL &&
yang_keyword_get(yroot) == Y_RPC)
goto ok;
keyw = yang_keyword_get(ys);
/* Check if list and if keys do not exist */
if (keyw == Y_LIST &&
@ -1275,7 +1281,7 @@ ys_list_check(clicon_handle h,
/* Except nokey exceptions such as rrc 8040 yang-data */
if (!yang_flag_get(yroot, YANG_FLAG_NOKEY)){
/* Note obsolete */
clicon_log(LOG_ERR, "Error: LIST \"%s\" in module \"%s\" lacks key statement which MUST be present (See RFC 7950 Sec 7.8.2)",
clicon_log(LOG_ERR, "Error: LIST \"%s\" in module \"%s\" lacks key statement which MUST be present (See RFC 7950 Sec 7.8.2)",
yang_argument_get(ys),
yang_argument_get(ymod)
);
@ -1316,7 +1322,7 @@ ys_visit(struct yang_stmt *yn,
struct yang_stmt *yi; /* import / include */
struct yang_stmt *yspec;
struct yang_stmt *ymod;
if (yn == NULL ||
(yang_keyword_get(yn) != Y_MODULE && yang_keyword_get(yn) != Y_SUBMODULE)){
clicon_err(OE_YANG, EINVAL, "Expected module or submodule");
@ -1374,6 +1380,8 @@ ys_visit(struct yang_stmt *yn,
* @param[in] modmax End of interval
* @param[out] ylist Result list of sorted nodes with "least significant" first
* @param[out] ylen Length of ylist
* @retval 0 OK
* @retval -1 Error
* see eg https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
*/
static int
@ -1435,7 +1443,7 @@ yang_parse_post(clicon_handle h,
int modmax;
struct yang_stmt **ylist = NULL; /* Topology sorted modules */
int ylen = 0; /* Length of ylist */
if (modmin < 0){
clicon_err(OE_YANG, EINVAL, "modmin negative");
goto done;
@ -1446,7 +1454,6 @@ yang_parse_post(clicon_handle h,
for (i=modmin; i<yang_len_get(yspec); i++)
if (yang_parse_recurse(h, yang_child_i(yspec, i), yspec) < 0)
goto done;
modmax = yang_len_get(yspec);
/* The set of modules [modmin..maxmax] is here complete wrt imports/includes and is a DAG
* Example: A imports B, C and D, and C and D imports B
@ -1457,12 +1464,11 @@ yang_parse_post(clicon_handle h,
goto done;
/* 2. Check cardinality a first time (done again last) */
for (i=modmin; i<modmax; i++)
for (i=modmin; i<modmax; i++)
if (yang_cardinality(h, yang_child_i(yspec, i), yang_argument_get(yspec->ys_stmt[i])) < 0)
goto done;
/* 3: Check features/if-features: check if enabled and remove disabled features */
for (i=modmin; i<modmax; i++)
for (i=modmin; i<modmax; i++)
if (yang_features(h, yang_child_i(yspec, i)) < 0)
goto done;
@ -1511,7 +1517,7 @@ yang_parse_post(clicon_handle h,
for (i=modmin; i<modmax; i++) /* Really only under (sub)modules no need to traverse whole tree */
if (yang_apply(yang_child_i(yspec, i), -1, yang_deviation, 1, (void*)h) < 0)
goto done;
/* 9: Go through parse tree and do 2nd step populate (eg default)
* Note that augments in step 7 are not covered here since they apply to
* modules already loaded. Therefore the call to ys_populate2 is made inline in
@ -1549,9 +1555,9 @@ yang_parse_post(clicon_handle h,
* @see yang_spec_parse_file
*/
int
yang_spec_parse_module(clicon_handle h,
yang_spec_parse_module(clicon_handle h,
const char *name,
const char *revision,
const char *revision,
yang_stmt *yspec)
{
int retval = -1;
@ -1596,8 +1602,8 @@ yang_spec_parse_module(clicon_handle h,
* @see yang_spec_load_dir For loading all files in a directory
*/
int
yang_spec_parse_file(clicon_handle h,
char *filename,
yang_spec_parse_file(clicon_handle h,
char *filename,
yang_stmt *yspec)
{
int retval = -1;
@ -1668,7 +1674,7 @@ yang_spec_load_dir(clicon_handle h,
uint32_t rev0; /* revision in existing module */
char *oldbase = NULL;
int taken = 0;
/* Get yang files names from yang module directory. Note that these
* are sorted alphatetically:
* a.yang,
@ -1731,7 +1737,7 @@ yang_spec_load_dir(clicon_handle h,
revm = cv_uint32_get(yang_cv_get(yrev));
/* Sanity check that file revision does not match internal rev stmt */
if (revf && revm && revm != revf){ /* XXX */
clicon_err(OE_YANG, EINVAL, "Yang module file revision and in yang does not match: %s(%u) vs %u", filename, revf, revm);
clicon_err(OE_YANG, EINVAL, "Yang module file revision and in yang does not match: %s(%u) vs %u", filename, revf, revm);
goto done;
}
/* If ym0 and ym exists, delete the yang with oldest revision
@ -1775,7 +1781,7 @@ ys_parse_date_arg(char *datearg,
int retval = -1;
int i;
uint32_t d = 0;
if (strlen(datearg) != 10 || datearg[4] != '-' || datearg[7] != '-'){
clicon_err(OE_YANG, EINVAL, "Revision date %s, but expected: YYYY-MM-DD", datearg);
goto done;
@ -1808,7 +1814,7 @@ ys_parse_date_arg(char *datearg,
* available in the first pass. Prefer to do stuff in ys_populate
*/
cg_var *
ys_parse(yang_stmt *ys,
ys_parse(yang_stmt *ys,
enum cv_type cvtype)
{
int cvret;
@ -1821,7 +1827,7 @@ ys_parse(yang_stmt *ys,
yang_cv_set(ys, NULL);
}
if ((cv = cv_new(cvtype)) == NULL){
clicon_err(OE_YANG, errno, "cv_new");
clicon_err(OE_YANG, errno, "cv_new");
goto done;
}
if ((cvret = cv_parse1(yang_argument_get(ys), cv, &reason)) < 0){ /* error */
@ -1873,20 +1879,20 @@ ys_parse_sub(yang_stmt *ys,
uint32_t minmax;
cg_var *cv = NULL;
yang_stmt *yp;
arg = yang_argument_get(ys);
keyword = yang_keyword_get(ys);
switch (keyword){
case Y_BASE:
case Y_TYPE:
case Y_USES:
/* Invoke next level parser
/* Invoke next level parser
*/
if (yang_schema_nodeid_subparse(yang_argument_get(ys), YA_ID_REF, filename, yang_linenum_get(ys)) < 0)
goto done;
break;
case Y_FRACTION_DIGITS:
if (ys_parse(ys, CGV_UINT8) == NULL)
if (ys_parse(ys, CGV_UINT8) == NULL)
goto done;
if ((cv = yang_cv_get(ys)) == NULL){
clicon_err(OE_YANG, ENOENT, "Unexpected NULL cv");
@ -1908,7 +1914,7 @@ ys_parse_sub(yang_stmt *ys,
if (ys_parse_date_arg(arg, &date) < 0)
goto done;
if ((cv = cv_new(CGV_UINT32)) == NULL){
clicon_err(OE_YANG, errno, "cv_new");
clicon_err(OE_YANG, errno, "cv_new");
goto done;
}
yang_cv_set(ys, cv);
@ -1918,7 +1924,7 @@ ys_parse_sub(yang_stmt *ys,
if (strcmp(arg, "current") &&
strcmp(arg, "deprecated") &&
strcmp(arg, "obsolete")){
clicon_err(OE_YANG, errno, "Invalid status: \"%s\", expected current, deprecated, or obsolete", arg);
clicon_err(OE_YANG, errno, "Invalid status: \"%s\", expected current, deprecated, or obsolete", arg);
goto done;
}
@ -1926,7 +1932,7 @@ ys_parse_sub(yang_stmt *ys,
case Y_MAX_ELEMENTS:
case Y_MIN_ELEMENTS:
if ((cv = cv_new(CGV_UINT32)) == NULL){
clicon_err(OE_YANG, errno, "cv_new");
clicon_err(OE_YANG, errno, "cv_new");
goto done;
}
yang_cv_set(ys, cv);
@ -1935,11 +1941,11 @@ ys_parse_sub(yang_stmt *ys,
cv_uint32_set(cv, 0); /* 0 means unbounded for max */
else{
if ((ret = parse_uint32(arg, &minmax, &reason)) < 0){
clicon_err(OE_YANG, errno, "parse_uint32");
clicon_err(OE_YANG, errno, "parse_uint32");
goto done;
}
if (ret == 0){
clicon_err(OE_YANG, EINVAL, "element-min/max parse error: %s", reason);
clicon_err(OE_YANG, EINVAL, "element-min/max parse error: %s", reason);
if (reason)
free(reason);
goto done;
@ -1962,7 +1968,7 @@ ys_parse_sub(yang_stmt *ys,
goto done;
break;
case Y_AUGMENT: /* If parent is module/submodule: absolute-schema-nodeid
* If parent is uses: descendant-schema-nodeid
* If parent is uses: descendant-schema-nodeid
*/
if ((yp = yang_parent_get(ys)) &&
yang_keyword_get(yp) != Y_USES){
@ -1981,7 +1987,7 @@ ys_parse_sub(yang_stmt *ys,
if (extra == NULL)
break;
if ((cv = cv_new(CGV_STRING)) == NULL){
clicon_err(OE_YANG, errno, "cv_new");
clicon_err(OE_YANG, errno, "cv_new");
goto done;
}
yang_cv_set(ys, cv);