cleaned up string functions
This commit is contained in:
parent
75e1ea7c1c
commit
236b661e43
18 changed files with 245 additions and 427 deletions
|
|
@ -694,7 +694,7 @@ from_client_create_subscription(clicon_handle h,
|
|||
}
|
||||
}
|
||||
}
|
||||
if (client_subscription_add(ce, stream, MSG_NOTIFY_XML, filter) == NULL)
|
||||
if (client_subscription_add(ce, stream, FORMAT_XML, filter) == NULL)
|
||||
goto done;
|
||||
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
|
||||
ok:
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@
|
|||
#include "backend_handle.h"
|
||||
|
||||
/* Command line options to be passed to getopt(3) */
|
||||
#define BACKEND_OPTS "hD:f:d:Fzu:P:1IRCc:rg:pt"
|
||||
#define BACKEND_OPTS "hD:f:d:Fzu:P:1IRCc:rg:pty:"
|
||||
|
||||
/*! Terminate. Cannot use h after this */
|
||||
static int
|
||||
|
|
@ -426,6 +426,9 @@ main(int argc, char **argv)
|
|||
case 't' : /* Print alternative dbspec format (eg if YANG, print KEY) */
|
||||
printalt++;
|
||||
break;
|
||||
case 'y' : /* yang module */
|
||||
clicon_option_str_set(h, "CLICON_YANG_MODULE_MAIN", optarg);
|
||||
break;
|
||||
default:
|
||||
usage(argv[0], h);
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ backend_notify(clicon_handle h,
|
|||
/* Then go thru all global (handle) subscriptions and find matches */
|
||||
hs = NULL;
|
||||
while ((hs = subscription_each(h, hs)) != NULL){
|
||||
if (hs->hs_format != MSG_NOTIFY_TXT)
|
||||
if (hs->hs_format != FORMAT_TEXT)
|
||||
continue;
|
||||
if (strcmp(hs->hs_stream, stream))
|
||||
continue;
|
||||
|
|
@ -240,7 +240,7 @@ backend_notify_xml(clicon_handle h,
|
|||
/* Then go thru all global (handle) subscriptions and find matches */
|
||||
hs = NULL;
|
||||
while ((hs = subscription_each(h, hs)) != NULL){
|
||||
if (hs->hs_format != MSG_NOTIFY_XML)
|
||||
if (hs->hs_format != FORMAT_XML)
|
||||
continue;
|
||||
if (strcmp(hs->hs_stream, stream))
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@
|
|||
|
||||
#include "cli_common.h"
|
||||
|
||||
|
||||
/*! Register log notification stream
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] stream Event stream. CLICON is predefined, others are application-defined
|
||||
|
|
@ -594,7 +595,7 @@ load_config_filev(clicon_handle h,
|
|||
{
|
||||
int ret = -1;
|
||||
struct stat st;
|
||||
char **vecp;
|
||||
char **vecp = NULL;
|
||||
char *filename;
|
||||
int replace;
|
||||
cg_var *cv;
|
||||
|
|
@ -668,6 +669,13 @@ load_config_filev(clicon_handle h,
|
|||
ret = 0;
|
||||
done:
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vecp){
|
||||
if (vecp[0])
|
||||
free(vecp[0]);
|
||||
if (vecp[1])
|
||||
free(vecp[1]);
|
||||
free(vecp);
|
||||
}
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (fd != -1)
|
||||
|
|
@ -803,14 +811,6 @@ db_copy(clicon_handle h,
|
|||
return clicon_rpc_copy_config(h, db1, db2);
|
||||
}
|
||||
|
||||
/* These are strings that can be used as 3rd argument to cli_setlog */
|
||||
#ifdef notused // broke in new version
|
||||
static const char *SHOWAS_TXT = "txt";
|
||||
static const char *SHOWAS_XML = "xml";
|
||||
static const char *SHOWAS_XML2TXT = "xml2txt";
|
||||
static const char *SHOWAS_XML2JSON = "xml2json";
|
||||
#endif
|
||||
|
||||
/*! This is the callback used by cli_setlog to print log message in CLI
|
||||
* param[in] s UNIX socket from backend where message should be read
|
||||
* param[in] arg format: txt, xml, xml2txt, xml2json
|
||||
|
|
@ -824,11 +824,7 @@ cli_notification_cb(int s,
|
|||
int retval = -1;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xe;
|
||||
char *format = (char*)arg;
|
||||
#if 0
|
||||
char *eventstr = NULL;
|
||||
cxobj *xn;
|
||||
#endif
|
||||
enum format_enum format = (enum format_enum)arg;
|
||||
|
||||
/* get msg (this is the reason this function is called) */
|
||||
if (clicon_msg_rcv(s, &reply, &eof) < 0)
|
||||
|
|
@ -840,40 +836,25 @@ cli_notification_cb(int s,
|
|||
event_unreg_fd(s, cli_notification_cb);
|
||||
goto done;
|
||||
}
|
||||
if (format == NULL)
|
||||
goto done;
|
||||
if (clicon_msg_decode(reply, &xt) < 0)
|
||||
goto done;
|
||||
if ((xe = xpath_first(xt, "//event")) != NULL){
|
||||
xe = xpath_first(xt, "//event");
|
||||
switch (format){
|
||||
case FORMAT_XML:
|
||||
if (xml_print(stdout, xe) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case FORMAT_TEXT:
|
||||
if (xml2txt(stdout, xe, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case FORMAT_JSON:
|
||||
if (xml2json(stdout, xe, 0) < 0)
|
||||
goto done;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#ifdef notyet /* Broke in new version */
|
||||
if (strcmp(format, SHOWAS_TXT) == 0){
|
||||
fprintf(stdout, "%s\n", eventstr);
|
||||
}
|
||||
else
|
||||
if (strcmp(format, SHOWAS_XML) == 0){
|
||||
if (clicon_xml_parse_string(&eventstr, &xt) < 0)
|
||||
goto done;
|
||||
if ((xn = xml_child_i(xt, 0)) != NULL)
|
||||
if (xml_print(stdout, xn) < 0)
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
if (strcmp(format, SHOWAS_XML2TXT) == 0){
|
||||
if ((xn = xml_child_i(xe, 0)) != NULL)
|
||||
if (xml2txt(stdout, xn, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
if (strcmp(format, SHOWAS_XML2JSON) == 0){
|
||||
if ((xn = xml_child_i(xe, 0)) != NULL){
|
||||
if (xml2json(stdout, xn, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
retval = 0;
|
||||
done:
|
||||
if (xt)
|
||||
|
|
@ -905,7 +886,7 @@ cli_notifyv(clicon_handle h,
|
|||
int retval = -1;
|
||||
int status;
|
||||
char *formatstr = NULL;
|
||||
enum format_enum format = MSG_NOTIFY_TXT;
|
||||
enum format_enum format = FORMAT_TEXT;
|
||||
|
||||
if (cvec_len(argv) != 2 && cvec_len(argv) != 3){
|
||||
clicon_err(OE_PLUGIN, 0, "%s Requires arguments: <logstream> <status> [<format>]", __FUNCTION__);
|
||||
|
|
@ -915,12 +896,7 @@ cli_notifyv(clicon_handle h,
|
|||
status = atoi(cv_string_get(cvec_i(argv, 1)));
|
||||
if (cvec_len(argv) > 2){
|
||||
formatstr = cv_string_get(cvec_i(argv, 2));
|
||||
if (strcmp(formatstr, "SHOWAS_TXT") != 0)
|
||||
format = MSG_NOTIFY_XML;
|
||||
if ((formatstr = strdup(formatstr)) == NULL){ /* XXX */
|
||||
clicon_err(OE_PLUGIN, 0, "%s Requires arguments: <logstream> <status> [<format>]", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
format = format_str2int(formatstr);
|
||||
}
|
||||
if (cli_notification_register(h,
|
||||
stream,
|
||||
|
|
@ -928,7 +904,7 @@ cli_notifyv(clicon_handle h,
|
|||
"",
|
||||
status,
|
||||
cli_notification_cb,
|
||||
formatstr) < 0)
|
||||
(void*)format) < 0)
|
||||
goto done;
|
||||
|
||||
retval = 0;
|
||||
|
|
@ -1046,7 +1022,7 @@ load_config_file(clicon_handle h,
|
|||
cvec *argv;
|
||||
cg_var *cv;
|
||||
char *str;
|
||||
char **vec;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
|
||||
/* Split string into two parts and build a cvec of it and supply that to
|
||||
|
|
@ -1055,10 +1031,8 @@ load_config_file(clicon_handle h,
|
|||
clicon_err(OE_PLUGIN, 0, "%s: requires string argument", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsplit(str, " ", &nvec, __FUNCTION__)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "clicon_strsplit");
|
||||
if ((vec = clicon_strsep(str, " ", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
if (nvec != 2){
|
||||
clicon_err(OE_PLUGIN, 0, "Arg syntax is <varname> <replace|merge>");
|
||||
goto done;
|
||||
|
|
@ -1076,7 +1050,8 @@ load_config_file(clicon_handle h,
|
|||
|
||||
retval = load_config_filev(h, cvv, argv);
|
||||
done:
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
}
|
||||
int
|
||||
|
|
@ -1088,7 +1063,7 @@ save_config_file(clicon_handle h,
|
|||
cvec *argv;
|
||||
cg_var *cv;
|
||||
char *str;
|
||||
char **vec;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
|
||||
/* Split string into two parts and build a cvec of it and supply that to
|
||||
|
|
@ -1097,10 +1072,8 @@ save_config_file(clicon_handle h,
|
|||
clicon_err(OE_PLUGIN, 0, "%s: requires string argument", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsplit(str, " ", &nvec, __FUNCTION__)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "clicon_strsplit");
|
||||
if ((vec = clicon_strsep(str, " ", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
if (nvec != 2){
|
||||
clicon_err(OE_PLUGIN, 0, "Arg syntax is <dbname> <varname>");
|
||||
goto done;
|
||||
|
|
@ -1118,7 +1091,8 @@ save_config_file(clicon_handle h,
|
|||
|
||||
retval = save_config_filev(h, cvv, argv);
|
||||
done:
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -1131,7 +1105,7 @@ cli_notify(clicon_handle h,
|
|||
cvec *argv;
|
||||
cg_var *cv;
|
||||
char *str;
|
||||
char **vec;
|
||||
char **vec = NULL;
|
||||
int nvec;
|
||||
|
||||
/* Split string into two parts and build a cvec of it and supply that to
|
||||
|
|
@ -1140,10 +1114,8 @@ cli_notify(clicon_handle h,
|
|||
clicon_err(OE_PLUGIN, 0, "%s: requires string argument", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsplit(str, " ", &nvec, __FUNCTION__)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "clicon_strsplit");
|
||||
if ((vec = clicon_strsep(str, " ", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
if (nvec != 2 && nvec != 3){
|
||||
clicon_err(OE_PLUGIN, 0, "Arg syntax is <logstream> <status> [<format>]");
|
||||
goto done;
|
||||
|
|
@ -1166,7 +1138,8 @@ cli_notify(clicon_handle h,
|
|||
}
|
||||
retval = cli_notifyv(h, cvv, argv);
|
||||
done:
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ main(int argc, char **argv)
|
|||
int help = 0;
|
||||
char *treename;
|
||||
int logdst = CLICON_LOG_STDERR;
|
||||
char *restarg; /* what remains after options */
|
||||
char *restarg = NULL; /* what remains after options */
|
||||
|
||||
/* Defaults */
|
||||
|
||||
|
|
@ -366,7 +366,7 @@ main(int argc, char **argv)
|
|||
clicon_option_dump(h, debug);
|
||||
|
||||
/* Join rest of argv to a single command */
|
||||
restarg = clicon_strjoin(argc, argv, " ", __FUNCTION__);
|
||||
restarg = clicon_strjoin(argc, argv, " ");
|
||||
|
||||
/* If several cligen object variables match same preference, select first */
|
||||
cligen_match_cgvar_same(1);
|
||||
|
|
@ -389,6 +389,8 @@ main(int argc, char **argv)
|
|||
if (!once)
|
||||
cli_interactive(h);
|
||||
done:
|
||||
if (restarg)
|
||||
free(restarg);
|
||||
unchunk_group(__FUNCTION__);
|
||||
// Gets in your face if we log on stderr
|
||||
clicon_log_init(__PROGRAM__, LOG_INFO, 0); /* Log on syslog no stderr */
|
||||
|
|
|
|||
|
|
@ -398,9 +398,8 @@ cli_load_syntax(clicon_handle h, const char *filename, const char *clispec_dir)
|
|||
clicon_err(OE_PLUGIN, 0, "No syntax mode specified in %s", filepath);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsplit(mode, ":", &nvec, __FUNCTION__)) == NULL) {
|
||||
if ((vec = clicon_strsep(mode, ":", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
for (i = 0; i < nvec; i++) {
|
||||
if (syntax_append(h, cli_syntax(h), vec[i], pt) < 0) {
|
||||
goto done;
|
||||
|
|
@ -415,6 +414,8 @@ cli_load_syntax(clicon_handle h, const char *filename, const char *clispec_dir)
|
|||
done:
|
||||
if (vr)
|
||||
cvec_free(vr);
|
||||
if (vec)
|
||||
free(vec);
|
||||
unchunk_group(__FUNCTION__);
|
||||
return retval;
|
||||
}
|
||||
|
|
@ -1020,7 +1021,7 @@ cli_ptpush(clicon_handle h, char *mode, char *string, char *op)
|
|||
return 0;
|
||||
pt = &co_cmd->co_pt;
|
||||
/* vec is the command, eg 'edit policy_option' */
|
||||
if ((vec = clicon_strsplit(string, " ", &nvec, __FUNCTION__)) == NULL)
|
||||
if ((vec = clicon_strsep(string, " ", &nvec)) == NULL)
|
||||
goto catch;
|
||||
co = NULL;
|
||||
found = 0;
|
||||
|
|
@ -1050,7 +1051,8 @@ cli_ptpush(clicon_handle h, char *mode, char *string, char *op)
|
|||
co_up_set(cc, co_cmd);
|
||||
}
|
||||
catch:
|
||||
unchunk_group(__FUNCTION__) ;
|
||||
if (vec)
|
||||
free(vec);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -787,10 +787,8 @@ expand_dbvar(void *h,
|
|||
goto done;
|
||||
}
|
||||
/* In the example, str = "candidate /x/m1/%s/b" */
|
||||
if ((vec = clicon_strsplit(str, " ", &nvec, __FUNCTION__)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "clicon_strsplit");
|
||||
if ((vec = clicon_strsep(str, " ", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
dbstr = vec[0];
|
||||
if (strcmp(dbstr, "running") != 0 &&
|
||||
strcmp(dbstr, "candidate") != 0 &&
|
||||
|
|
@ -856,7 +854,8 @@ expand_dbvar(void *h,
|
|||
}
|
||||
retval = 0;
|
||||
done:
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vec)
|
||||
free(vec);
|
||||
if (xvec)
|
||||
free(xvec);
|
||||
if (xt)
|
||||
|
|
@ -905,10 +904,8 @@ show_conf_as(clicon_handle h,
|
|||
clicon_err(OE_PLUGIN, 0, "%s: requires string argument", __FUNCTION__);
|
||||
goto done;
|
||||
}
|
||||
if ((vec = clicon_strsplit(str, " ", &nvec, __FUNCTION__)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "clicon_strsplit");
|
||||
if ((vec = clicon_strsep(str, " ", &nvec)) == NULL)
|
||||
goto done;
|
||||
}
|
||||
if (nvec != 2 && nvec != 3){
|
||||
clicon_err(OE_PLUGIN, 0, "format error \"%s\" - expected <dbname> <xpath> [<attr>] got %d arg", str, nvec);
|
||||
goto done;
|
||||
|
|
@ -956,7 +953,8 @@ done:
|
|||
free(val);
|
||||
if (cbx)
|
||||
cbuf_free(cbx);
|
||||
unchunk_group(__FUNCTION__);
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ request_process(clicon_handle h,
|
|||
char *path;
|
||||
char *query;
|
||||
char *method;
|
||||
char **pvec;
|
||||
char **pvec = NULL;
|
||||
int pn;
|
||||
cvec *qvec = NULL;
|
||||
cvec *dvec = NULL;
|
||||
|
|
@ -141,7 +141,7 @@ request_process(clicon_handle h,
|
|||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
path = FCGX_GetParam("DOCUMENT_URI", r->envp);
|
||||
query = FCGX_GetParam("QUERY_STRING", r->envp);
|
||||
if ((pvec = clicon_strsplit(path, "/", &pn, __FUNCTION__)) == NULL)
|
||||
if ((pvec = clicon_strsep(path, "/", &pn)) == NULL)
|
||||
goto done;
|
||||
|
||||
if (str2cvec(query, '&', '=', &qvec) < 0)
|
||||
|
|
@ -171,7 +171,6 @@ request_process(clicon_handle h,
|
|||
if (auth == 0)
|
||||
goto done;
|
||||
clicon_debug(1, "%s credentials ok 2", __FUNCTION__);
|
||||
clicon_debug(1, "%s credentials ok 3", __FUNCTION__);
|
||||
|
||||
if (strcmp(method, "data") == 0) /* restconf, skip /api/data */
|
||||
retval = api_data(h, r, path, pcvec, 2, qvec, data);
|
||||
|
|
@ -181,6 +180,8 @@ request_process(clicon_handle h,
|
|||
retval = notfound(r);
|
||||
done:
|
||||
clicon_debug(1, "%s retval:%d K", __FUNCTION__, retval);
|
||||
if (pvec)
|
||||
free(pvec);
|
||||
if (dvec)
|
||||
cvec_free(dvec);
|
||||
if (qvec)
|
||||
|
|
@ -189,7 +190,6 @@ request_process(clicon_handle h,
|
|||
cvec_free(pcvec);
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
unchunk_group(__FUNCTION__);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue