cleaned up string functions

This commit is contained in:
Olof hagsand 2017-04-04 17:11:21 +02:00
parent 75e1ea7c1c
commit 236b661e43
18 changed files with 245 additions and 427 deletions

View file

@ -62,19 +62,22 @@
#include "clixon_string.h"
#include "clixon_file.h"
/*
* Resolve the real path of a given 'path', following symbolic links and '../'.
/*! 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)
clicon_realpath(const char *cwd,
char *path,
const char *label)
{
char **ret = NULL;
char *rest;
char **vec, **vec2;
char **vec = NULL;
char **vec2;
int nvec, nvec2;
char *p;
char *rp = NULL;
@ -122,8 +125,10 @@ clicon_realpath(const char *cwd, char *path, const char *label)
/* 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 '.' */
vec = clicon_strsplit (p, "/", &nvec, __FUNCTION__);
vec2 = chunk(nvec * sizeof(char *), __FUNCTION__);
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)
@ -135,14 +140,14 @@ clicon_realpath(const char *cwd, char *path, const char *label)
}
/* Create resulting vector */
if ((ret = chunk(sizeof(char *) * 2, label)) != NULL) {
if((ret[0] = clicon_strjoin(nvec-nvec2, &vec2[nvec2], "/", label)) == NULL) {
unchunk(ret);
if ((ret = malloc(sizeof(char *) * 2)) != NULL) {
if((ret[0] = clicon_strjoin(nvec-nvec2, &vec2[nvec2], "/")) == NULL) {
free(ret);
ret = NULL;
}
if ((ret[1] = chunkdup(rp, strlen(rp)+1, label)) == NULL) {
unchunk(ret[0]);
unchunk(ret);
if ((ret[1] = strdup(rp)) == NULL) {
free(ret[0]);
free(ret);
ret = NULL;
}
}
@ -150,6 +155,10 @@ clicon_realpath(const char *cwd, char *path, const char *label)
catch:
if(rp)
free(rp);
if(vec)
free(vec);
if(vec2)
free(vec2);
unchunk_group(__FUNCTION__);
return ret;
}

View file

@ -121,13 +121,12 @@ clicon_proc_run (char *cmd,
sigfn_t oldhandler = NULL;
sigset_t oset;
argv = clicon_sepsplit (cmd, " \t", &argc, __FUNCTION__);
argv = clicon_strsep(cmd, " \t", &argc);
if (!argv)
return -1;
if (pipe (outfd) == -1)
goto done;
signal_get_mask(&oset);
set_signal(SIGINT, clicon_proc_sigint, &oldhandler);
@ -194,7 +193,8 @@ clicon_proc_run (char *cmd,
signal_set_mask (&oset);
set_signal(SIGINT, oldhandler, NULL);
unchunk_group (__FUNCTION__);
if(argv)
free(argv);
return retval;
}
@ -216,7 +216,7 @@ clicon_proc_daemon (char *cmd)
struct rlimit
rlim;
argv = clicon_sepsplit (cmd, " \t", &argc, NULL);
argv = clicon_strsep(cmd, " \t", &argc);
if (!argv)
return -1;
@ -260,7 +260,8 @@ clicon_proc_daemon (char *cmd)
retval = 0;
done:
unchunk_group(__FUNCTION__);
if (argv)
free(argv);
return (retval);
}

View file

@ -74,6 +74,46 @@
static int _atomicio_sig = 0;
/*! Formats (showas) derived from XML
*/
struct formatvec{
char *fv_str;
int fv_int;
};
static struct formatvec _FORMATS[] = {
{"xml", FORMAT_XML},
{"text", FORMAT_TEXT},
{"json", FORMAT_JSON},
{"cli", FORMAT_CLI},
{"netconf", FORMAT_NETCONF},
{NULL, -1}
};
/*! Translate from numeric error to string representation
*/
char *
format_int2str(enum format_enum showas)
{
struct formatvec *fv;
for (fv=_FORMATS; fv->fv_int != -1; fv++)
if (fv->fv_int == showas)
break;
return fv?(fv->fv_str?fv->fv_str:"unknown"):"unknown";
}
enum format_enum
format_str2int(char *str)
{
struct formatvec *fv;
for (fv=_FORMATS; fv->fv_int != -1; fv++)
if (strcmp(fv->fv_str, str) == 0)
break;
return fv?fv->fv_int:-1;
}
/*! Encode a clicon netconf message
* @param[in] param Variable agrument list format an XML netconf string
* @retval msg Clicon message to send to eg clicon_msg_send()

View file

@ -53,117 +53,6 @@
#include "clixon_string.h"
#include "clixon_err.h"
/*! Split string into a vector based on character delimiters
*
* The given string is split into a vector where the delimiter can be
* any of the characters in the specified delimiter string.
*
* The vector returned is one single memory chunk that must be unchunked
* by the caller
*
* @param[in] string String to be split
* @param[in] delim String of delimiter characters
* @param[out] nvec Number of entries in returned vector
* @param[in] label Chunk label for returned vector
* @retval vec Vector of strings. Free with unchunk
* @retval NULL Error
* @see clicon_strsplit Operates on full string delimiters rather than
* individual character delimiters.
* @see clicon_strsep Use malloc instead of chunk
*/
char **
clicon_sepsplit (char *string,
char *delim,
int *nvec,
const char *label)
{
int idx;
size_t siz;
char *s, *s0;
char **vec, *vecp;
*nvec = 0;
s0 = s = chunkdup (string, strlen(string)+1, __FUNCTION__);
while (strsep(&s, delim))
(*nvec)++;
unchunk (s0);
siz = ((*nvec +1) * sizeof (char *)) + strlen(string) + 1;
vec = (char **) chunk (siz, label);
if (!vec) {
return NULL;
}
bzero (vec, siz);
vecp = (char *)&vec[*nvec +1];
bcopy (string, vecp, strlen (string));
for (idx = 0; idx < *nvec; idx++) {
vec[idx] = vecp;
strsep (&vecp, delim);
}
return vec;
}
/*! Split string into a vector based on a string delimiter
*
* The given string is split into a vector where the delimited by the
* the full delimiter string. The matched delimiters are not part of the
* resulting vector.
*
* The vector returned is one single memory chunk that must be unchunked
* by the caller
*
* @param[in] string String to be split
* @param[in] delim String of delimiter characters
* @param[out] nvec Number of entries in returned vector
* @param[in] label Chunk label for returned vector
* @retval vec Vector of strings. Free with unchunk
* @retval NULL Error
* @see clicon_sepsplit Operates on individual character delimiters rather
* than full string delimiter.
* @see clicon_strsep Use malloc instead of chunk
*/
char **
clicon_strsplit (char *string,
char *delim,
int *nvec,
const char *label)
{
int idx;
size_t siz;
char *s;
char **vec, *vecp;
*nvec = 1;
s = string;
while ((s = strstr(s, delim))) {
s += strlen(delim);
(*nvec)++;
}
siz = ((*nvec +1) * sizeof (char *)) + strlen(string) + 1;
vec = (char **) chunk (siz, label);
if (!vec) {
return NULL;
}
bzero (vec, siz);
vecp = (char *)&vec[*nvec +1];
bcopy (string, vecp, strlen (string));
s = vecp;
for (idx = 0; idx < *nvec; idx++) {
vec[idx] = s;
if ((s = strstr(s, delim)) != NULL) {
*s = '\0';
s += strlen(delim);
}
}
return vec;
}
/*! Split string into a vector based on character delimiters. Using malloc
*
@ -189,14 +78,20 @@ clicon_strsep(char *string,
char *p;
int nvec = 1;
int i;
for (i=0; i<strlen(string); i++)
if (index(delim, string[i]))
char *s;
char *d;
if ((s = string)==NULL)
goto done;
while (*s){
if ((d = index(delim, *s)) != NULL)
nvec++;
s++;
}
/* alloc vector and append copy of string */
if ((vec = (char**)malloc(nvec* sizeof(char*) + strlen(string)+1)) == NULL){
clicon_err(OE_YANG, errno, "malloc");
goto err;
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
ptr = (char*)vec + nvec* sizeof(char*); /* this is where ptr starts */
strncpy(ptr, string, strlen(string)+1);
@ -204,121 +99,40 @@ clicon_strsep(char *string,
while ((p = strsep(&ptr, delim)) != NULL)
vec[i++] = p;
*nvec0 = nvec;
err:
done:
return vec;
}
/*! Concatenate elements of a string array into a string.
* An optional delimiter string can be specified which will be inserted betwen
* each element.
* @param[in] label Chunk label for returned vector
* @retval str Joined string. Free with unchunk()
* @retval str Joined string. Free after use.
* @retval NULL Failure
*/
char *
clicon_strjoin (int argc,
char **argv,
char *delim,
const char *label)
clicon_strjoin(int argc,
char **argv,
char *delim)
{
int i;
int len;
char *str;
len = 0;
for (i = 0; i < argc; i++)
len += strlen(argv[i]);
if (delim)
len += (strlen(delim) * argc);
len += 1; /* '\0' */
if ((str = chunk (len, label)) == NULL)
return NULL;
memset (str, '\0', len);
for (i = 0; i < argc; i++) {
if (i != 0)
strncat (str, delim, len - strlen(str));
strncat (str, argv[i], len - strlen(str));
}
return str;
}
/*! Trim whitespace in beginning and end of string.
*
* @param[in] label Chunk label for returned vector
* @retval str Trimmed string. Free with unchunk()
* @retval NULL Failure
*/
char *
clicon_strtrim(char *str,
const char *label)
{
char *start, *end, *new;
start = str;
while (*start != '\0' && isspace(*start))
start++;
if (!strlen(start))
return (char *)chunkdup("\0", 1, label);
end = str + strlen(str) ;
while (end > str && isspace(*(end-1)))
end--;
if((new = chunkdup (start, end-start+1, label)))
new[end-start] = '\0';
return new;
}
/*! Given a string s, on format: a[b], separate it into two parts: a and b
* [] are separators.
* alterative use:
* a/b -> a and b (where sep = "/")
* @param[in] label Chunk label for returned vector
*/
int
clicon_sep(char *s,
const char sep[2],
const char *label,
char **a0,
char **b0)
{
char *a = NULL;
char *b = NULL;
char *ptr;
int i;
int len;
int retval = -1;
char *str;
ptr = s;
/* move forward to last char of element name */
while (*ptr && *ptr != sep[0] && *ptr != sep[1] )
ptr++;
/* Copy first element name */
len = ptr-s;
if ((a = chunkdup(s, len+1, label)) == NULL)
goto catch;
a[len] = '\0';
/* Do we have an extended format? */
if (*ptr == sep[0]) {
b = ++ptr;
/* move forward to end extension */
while (*ptr && *ptr != sep[1])
ptr++;
/* Copy extension */
len = ptr-b;
if ((b = chunkdup(b, len+1, label)) == NULL)
goto catch;
b[len] = '\0';
len = 0;
for (i = 0; i < argc; i++)
len += strlen(argv[i]);
if (delim)
len += (strlen(delim) * argc);
len += 1; /* '\0' */
if ((str = malloc(len)) == NULL)
return NULL;
memset (str, '\0', len);
for (i = 0; i < argc; i++) {
if (i != 0)
strncat (str, delim, len - strlen(str));
strncat (str, argv[i], len - strlen(str));
}
*a0 = a;
*b0 = b;
retval = 0;
catch:
return retval;
return str;
}
@ -346,82 +160,48 @@ clicon_strndup (const char *str,
}
#endif /* ! HAVE_STRNDUP */
/*! Match string against regexp.
*
* If a match pointer is given, the matching substring
* will be allocated 'match' will be pointing to it. The match string must
* be free:ed by the application.
* @retval -1 Failure
* @retval 0 No match
* @retval >0 Match: Length of matching substring
*/
/*
* Turn this on for uni-test programs
* Usage: clixon_string join
* Example compile:
gcc -g -o clixon_string -I. -I../clixon ./clixon_string.c -lclixon -lcligen
* Example run:
*/
#if 0 /* Test program */
static int
usage(char *argv0)
{
fprintf(stderr, "usage:%s <string>\n", argv0);
exit(0);
}
int
clicon_strmatch(const char *str,
const char *regexp,
char **match)
main(int argc, char **argv)
{
size_t len;
int status;
regex_t re;
char rxerr[128];
size_t nmatch = 1;
regmatch_t pmatch[1];
if (match)
*match = NULL;
if ((status = regcomp(&re, regexp, REG_EXTENDED)) != 0) {
regerror(status, &re, rxerr, sizeof(rxerr));
clicon_err(OE_REGEX, errno, "%s", rxerr);
return -1;
}
status = regexec(&re, str, nmatch, pmatch, 0);
regfree(&re);
if (status != 0)
return 0; /* No match */
len = pmatch[0].rm_eo - pmatch[0].rm_so;
/* If we've specified a match pointer, allocate and populate it. */
if (match) {
if ((*match = malloc(len + 1)) == NULL) {
clicon_err(OE_UNIX, errno, "Failed to allocate string");
return -1;
}
memset(*match, '\0', len + 1);
strncpy(*match, str + pmatch[0].rm_so, len);
}
return len;
}
/*! Substitute pattern in string.
* @retval str Malloc:ed string on success, use free to deallocate
* @retval NULL Failure.
*/
char *
clicon_strsub(char *str,
char *from,
char *to)
{
char **vec;
int nvec;
char *new;
char *retval = NULL;
char **vec;
char *str0;
char *str1;
int i;
if ((vec = clicon_strsplit(str, from, &nvec, __FUNCTION__)) == NULL) {
clicon_err(OE_UNIX, errno, "Failed to split string");
goto done;
if (argc != 2){
usage(argv[0]);
return 0;
}
if ((new = clicon_strjoin (nvec, vec, to, __FUNCTION__)) == NULL) {
clicon_err(OE_UNIX, errno, "Failed to split string");
goto done;
}
retval = strdup(new);
done:
unchunk_group(__FUNCTION__);
return retval;
str0 = argv[1];
if ((vec = clicon_strsep("a b\tc", " \t", &nvec)) == NULL)
return -1;
fprintf(stderr, "nvec: %d\n", nvec);
for (i=0; i<nvec; i++)
fprintf(stderr, "vec[%d]: %s\n", i, vec[i]);
if ((str1 = clicon_strjoin(nvec, vec, " ")) == NULL)
return -1;
fprintf(stderr, "join: %s\n", str1);
free(vec);
free(str1);
return 0;
}
#endif /* Test program */

View file

@ -665,9 +665,9 @@ get(char *dbname,
cxobj *xt)
{
int retval = -1;
char **vec;
char **vec = NULL;
int nvec;
char **valvec;
char **valvec = NULL;
int nvalvec;
int i;
int j;
@ -691,7 +691,7 @@ get(char *dbname,
clicon_err(OE_DB, 0, "Invalid key: %s", xk);
goto done;
}
if ((vec = clicon_strsplit(xk, "/", &nvec, __FUNCTION__)) == NULL)
if ((vec = clicon_strsep(xk, "/", &nvec)) == NULL)
goto done;
/* Element 0 is NULL '/',
Element 1 is top symbol and needs to find subs in all modules:
@ -759,7 +759,9 @@ get(char *dbname,
cvi = NULL;
/* Iterate over individual yang keys */
cprintf(cb, "%s", name);
if ((valvec = clicon_strsplit(restval, ",", &nvalvec, __FUNCTION__)) == NULL)
if (valvec)
free(valvec);
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
goto done;
if (cvec_len(cvk)!=nvalvec){
retval = 0;
@ -834,7 +836,10 @@ get(char *dbname,
}
retval = 0;
done:
unchunk_group(__FUNCTION__);
if (vec)
free(vec);
if (valvec)
free(valvec);
if (cvk)
cvec_free(cvk);
return retval;
@ -1210,9 +1215,9 @@ xmldb_put_xkey(clicon_handle h,
cxobj *x = NULL;
yang_stmt *y = NULL;
yang_stmt *ykey;
char **vec;
char **vec = NULL;
int nvec;
char **valvec;
char **valvec = NULL;
int nvalvec;
int i;
int j;
@ -1246,7 +1251,7 @@ xmldb_put_xkey(clicon_handle h,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if ((vec = clicon_strsplit(xk, "/", &nvec, __FUNCTION__)) == NULL)
if ((vec = clicon_strsep(xk, "/", &nvec)) == NULL)
goto done;
if (nvec < 2){
clicon_err(OE_XML, 0, "Malformed key: %s", xk);
@ -1305,7 +1310,9 @@ xmldb_put_xkey(clicon_handle h,
clicon_err(OE_XML, 0, "malformed key, expected '=<restval>'");
goto done;
}
if ((valvec = clicon_strsplit(restval, ",", &nvalvec, __FUNCTION__)) == NULL)
if (valvec)
free(valvec);
if ((valvec = clicon_strsep(restval, ",", &nvalvec)) == NULL)
goto done;
if (cvec_len(cvk) != nvalvec){
@ -1398,6 +1405,11 @@ xmldb_put_xkey(clicon_handle h,
cbuf_free(crx);
if (cvk)
cvec_free(cvk);
if (vec)
free(vec);
if (valvec)
free(valvec);
unchunk_group(__FUNCTION__);
return retval;
}
@ -1431,7 +1443,7 @@ xmldb_put_restconf_api_path(clicon_handle h,
int retval = -1;
yang_stmt *y = NULL;
yang_stmt *ykey;
char **vec;
char **vec = NULL;
int nvec;
int i;
char *name;
@ -1466,7 +1478,7 @@ xmldb_put_restconf_api_path(clicon_handle h,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if ((vec = clicon_strsplit(api_path, "/", &nvec, __FUNCTION__)) == NULL)
if ((vec = clicon_strsep(api_path, "/", &nvec)) == NULL)
goto done;
if (nvec < 2){
clicon_err(OE_XML, 0, "Malformed key: %s", api_path);
@ -1611,6 +1623,8 @@ xmldb_put_restconf_api_path(clicon_handle h,
cbuf_free(crx);
if (cvk)
cvec_free(cvk);
if (vec)
free(vec);
unchunk_group(__FUNCTION__);
return retval;
}

View file

@ -1695,11 +1695,9 @@ yang_xpath_abs(yang_node *yn,
char *prefix = NULL;
if ((vec = clicon_strsep(xpath, "/", &nvec)) == NULL){
clicon_err(OE_YANG, errno, "%s: strsplit", __FUNCTION__);
clicon_err(OE_YANG, errno, "%s: strsep", __FUNCTION__);
return NULL;
}
/* Assume path looks like: "/prefix:id[/prefix:id]*" */
if (nvec < 2){
clicon_err(OE_YANG, 0, "%s: NULL or truncated path: %s",
@ -1955,16 +1953,14 @@ cvec *
yang_arg2cvec(yang_stmt *ys,
char *delim)
{
char **vec;
char **vec = NULL;
int i;
int nvec;
cvec *cvv = NULL;
cg_var *cv;
if ((vec = clicon_strsplit(ys->ys_argument, " ", &nvec, __FUNCTION__)) == NULL){
clicon_err(OE_YANG, errno, "clicon_strsplit");
if ((vec = clicon_strsep(ys->ys_argument, " ", &nvec)) == NULL)
goto done;
}
if ((cvv = cvec_new(nvec)) == NULL){
clicon_err(OE_YANG, errno, "cvec_new");
goto done;
@ -1979,7 +1975,8 @@ yang_arg2cvec(yang_stmt *ys,
}
}
done:
unchunk_group(__FUNCTION__);
if (vec)
free(vec);
return cvv;
}