Alternative yang spec option -y added to all applications

This commit is contained in:
Olof hagsand 2017-04-04 21:37:42 +02:00
parent 236b661e43
commit c59869a44e
10 changed files with 86 additions and 167 deletions

View file

@ -62,108 +62,6 @@
#include "clixon_string.h"
#include "clixon_file.h"
/*! Resolve the real path of a given 'path', following symbolic links and '../'.
* If 'path' relative, it will be resolved based on the currnt working
* directory 'cwd'. The response is a 2 entry vector of strings. The first
* entry is the resolved path and the second is the part of the path which
* actually exist.
* @retval vec
*/
char **
clicon_realpath(const char *cwd,
char *path,
const char *label)
{
char **ret = NULL;
char *rest;
char **vec = NULL;
char **vec2;
int nvec, nvec2;
char *p;
char *rp = NULL;
char *ptr;
int i;
struct passwd *pwd;
char cwdbuf[PATH_MAX];
/* Prepend 'cwd' if not absolute */
if (path[0] == '/')
p = path;
else {
if (cwd == NULL || strlen(cwd) == 0)
cwd = getcwd(cwdbuf, sizeof(cwdbuf));
else if (cwd[0] == '~') {
if((pwd = getpwuid(getuid())) == NULL)
goto catch;
cwd = pwd->pw_dir;
}
p = chunk_sprintf(__FUNCTION__, "%s%s/%s",
(cwd[0]=='/' ? "" : "/"), cwd, path);
}
if (p == NULL)
goto catch;
/* Make a local copy of 'path' */
if ((path = chunkdup(p, strlen(p)+1, __FUNCTION__)) == NULL)
goto catch;
/* Find the smallest portion of the path that exist and run realpath() */
while(strlen(p) && ((rp = realpath(p, NULL)) == NULL)) {
if((ptr = strrchr(p, '/')) == NULL)
break;
*ptr = '\0';
}
if(rp == NULL)
goto catch;
/* Use the result of realpath() and the rest of 'path' untouched, to
form a new path */
rest = path + strlen(p);
ptr = chunk_sprintf(__FUNCTION__, "%s%s", rp, rest);
p = ptr;
/* Split path based on '/'. Loop through vector from the end and copy
each entry into a new vector, skipping '..' and it's previous directory
as well as all '.' */
if ((vec = clicon_strsep(p, "/", &nvec)) == NULL)
goto catch;
if ((vec2 = malloc(nvec * sizeof(char *))) == NULL)
goto catch;
nvec2 = i = nvec;
while(--i >= 0) {
if(strcmp(vec[i], "..") == 0)
i--; /* Skip previous */
else if(strcmp(vec[i], ".") == 0)
/* do nothing */ ;
else
vec2[--nvec2] = vec[i];
}
/* Create resulting vector */
if ((ret = malloc(sizeof(char *) * 2)) != NULL) {
if((ret[0] = clicon_strjoin(nvec-nvec2, &vec2[nvec2], "/")) == NULL) {
free(ret);
ret = NULL;
}
if ((ret[1] = strdup(rp)) == NULL) {
free(ret[0]);
free(ret);
ret = NULL;
}
}
catch:
if(rp)
free(rp);
if(vec)
free(vec);
if(vec2)
free(vec2);
unchunk_group(__FUNCTION__);
return ret;
}
/*
* qsort function
*/
@ -354,24 +252,3 @@ clicon_file_copy(char *src,
}
#ifdef NOTUSED
/*
* (un)lock a whole file.
* Arguments:
* fd - File descriptor
* cmd - F_GETLK, F_SETLK, F_SETLKW
* type - F_RDLCK, F_WRLCK, F_UNLCK
*/
int
file_lock(int fd, int cmd, int type)
{
struct flock lock;
lock.l_type = type;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
return fcntl(fd, cmd, &lock);
}
#endif

View file

@ -65,7 +65,7 @@
* @param[in] string String to be split
* @param[in] delim String of delimiter characters
* @param[out] nvec Number of entries in returned vector
* @retval vec Vector of strings. Free after use
* @retval vec Vector of strings. NULL terminated. Free after use
* @retval NULL Error *
*/
char **
@ -78,6 +78,7 @@ clicon_strsep(char *string,
char *p;
int nvec = 1;
int i;
size_t siz;
char *s;
char *d;
@ -89,11 +90,13 @@ clicon_strsep(char *string,
s++;
}
/* alloc vector and append copy of string */
if ((vec = (char**)malloc(nvec* sizeof(char*) + strlen(string)+1)) == NULL){
siz = (nvec+1)* sizeof(char*) + strlen(string)+1;
if ((vec = (char**)malloc(siz)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
ptr = (char*)vec + nvec* sizeof(char*); /* this is where ptr starts */
memset(vec, 0, siz);
ptr = (char*)vec + (nvec+1)* sizeof(char*); /* this is where ptr starts */
strncpy(ptr, string, strlen(string)+1);
i = 0;
while ((p = strsep(&ptr, delim)) != NULL)
@ -190,10 +193,10 @@ main(int argc, char **argv)
return 0;
}
str0 = argv[1];
if ((vec = clicon_strsep("a b\tc", " \t", &nvec)) == NULL)
if ((vec = clicon_strsep(str0, " \t", &nvec)) == NULL)
return -1;
fprintf(stderr, "nvec: %d\n", nvec);
for (i=0; i<nvec; i++)
for (i=0; i<nvec+1; i++)
fprintf(stderr, "vec[%d]: %s\n", i, vec[i]);
if ((str1 = clicon_strjoin(nvec, vec, " ")) == NULL)
return -1;

View file

@ -292,6 +292,7 @@ xml_yang_validate(cxobj *xt,
yang_stmt *yc;
int i;
yang_stmt *ys;
char *body;
/* if not given by argument (overide) use default link */
ys = ys0?ys0:xml_spec(xt);
@ -321,17 +322,19 @@ xml_yang_validate(cxobj *xt,
/* In the union case, value is parsed as generic REST type,
* needs to be reparsed when concrete type is selected
*/
if (cv_parse(xml_body(xt), cv) <0){
clicon_err(OE_UNIX, errno, "cv_parse");
goto done;
}
if ((ys_cv_validate(cv, ys, &reason)) != 1){
clicon_err(OE_DB, 0,
"validation of %s failed %s",
ys->ys_argument, reason?reason:"");
if (reason)
free(reason);
goto done;
if ((body = xml_body(xt)) != NULL){
if (cv_parse(body, cv) <0){
clicon_err(OE_UNIX, errno, "cv_parse");
goto done;
}
if ((ys_cv_validate(cv, ys, &reason)) != 1){
clicon_err(OE_DB, 0,
"validation of %s failed %s",
ys->ys_argument, reason?reason:"");
if (reason)
free(reason);
goto done;
}
}
break;
default: