From aba5c68fe2d29a94ea7eda6b000f611893eeb820 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Tue, 16 Apr 2024 12:05:36 +0200 Subject: [PATCH] Move format code to options and cleanup --- lib/clixon/clixon_options.h | 2 ++ lib/clixon/clixon_proto.h | 2 -- lib/src/clixon_datastore.c | 40 +++++++++++++++++++++---------------- lib/src/clixon_file.c | 13 ++++++------ lib/src/clixon_options.c | 36 +++++++++++++++++++++++++++++++++ lib/src/clixon_proto.c | 36 --------------------------------- 6 files changed, 67 insertions(+), 62 deletions(-) diff --git a/lib/clixon/clixon_options.h b/lib/clixon/clixon_options.h index 404c0986..33615de3 100644 --- a/lib/clixon/clixon_options.h +++ b/lib/clixon/clixon_options.h @@ -97,6 +97,8 @@ enum regexp_mode{ /* * Prototypes */ +char *format_int2str(enum format_enum showas); +enum format_enum format_str2int(char *str); /* Debug dump config options */ int clicon_option_dump(clixon_handle h, int dblevel); diff --git a/lib/clixon/clixon_proto.h b/lib/clixon/clixon_proto.h index 77802bd9..523f2452 100644 --- a/lib/clixon/clixon_proto.h +++ b/lib/clixon/clixon_proto.h @@ -56,8 +56,6 @@ struct clicon_msg { /* * Prototypes */ -char *format_int2str(enum format_enum showas); -enum format_enum format_str2int(char *str); int clixon_inet2sin(const char *addrtype, const char *addrstr, uint16_t port, struct sockaddr *sa, size_t *sa_len); struct clicon_msg *clicon_msg_encode(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 2, 3))); diff --git a/lib/src/clixon_datastore.c b/lib/src/clixon_datastore.c index 80d2efd1..d23c406c 100644 --- a/lib/src/clixon_datastore.c +++ b/lib/src/clixon_datastore.c @@ -846,11 +846,12 @@ xmldb_dump(clixon_handle h, cxobj *xt, withdefaults_type wdef) { - int retval = -1; - cxobj *xm; - cxobj *xmodst = NULL; - char *format; - int pretty; + int retval = -1; + cxobj *xm; + cxobj *xmodst = NULL; + char *formatstr; + enum format_enum format = FORMAT_XML; + int pretty; /* Add modstate first */ if ((xm = clicon_modst_cache_get(h, 1)) != NULL){ @@ -860,17 +861,27 @@ xmldb_dump(clixon_handle h, goto done; xml_parent_set(xmodst, xt); } - if ((format = clicon_option_str(h, "CLICON_XMLDB_FORMAT")) == NULL){ - clixon_err(OE_CFG, ENOENT, "No CLICON_XMLDB_FORMAT"); - goto done; - } pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY"); - if (strcmp(format,"json")==0){ + if ((formatstr = clicon_option_str(h, "CLICON_XMLDB_FORMAT")) != NULL){ + if ((format = format_str2int(formatstr)) < 0){ + clixon_err(OE_XML, 0, "Format %s invalid", formatstr); + goto done; + } + } + switch (format){ + case FORMAT_JSON: if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0) goto done; - } - else if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0) + break; + case FORMAT_XML: + if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0) + goto done; + break; + default: + clixon_err(OE_XML, 0, "Format %s not supported", format_int2str(format)); goto done; + break; + } /* Remove modules state after writing to file */ if (xmodst && xml_purge(xmodst) < 0) goto done; @@ -912,10 +923,6 @@ xmldb_write_cache2file(clixon_handle h, } if (xmldb_dump(h, f, xt, WITHDEFAULTS_EXPLICIT) < 0) goto done; - if (f) { - fclose(f); - f = NULL; - } retval = 0; done: if (dbfile) @@ -924,4 +931,3 @@ xmldb_write_cache2file(clixon_handle h, fclose(f); return retval; } - diff --git a/lib/src/clixon_file.c b/lib/src/clixon_file.c index 8c9257d7..6ac54307 100644 --- a/lib/src/clixon_file.c +++ b/lib/src/clixon_file.c @@ -88,12 +88,11 @@ clicon_files_recursive1(const char *dir, regex_t *re, cvec *cvv) { - int retval = -1; - struct dirent *dent = NULL; - char path[MAXPATHLEN]; - DIR *dirp = NULL; - int res = 0; - struct stat st; + int retval = -1; + struct dirent *dent = NULL; + char path[MAXPATHLEN]; + DIR *dirp = NULL; + struct stat st = {0,}; if (dir == NULL){ clixon_err(OE_UNIX, EINVAL, "Requires dir != NULL"); @@ -119,7 +118,7 @@ clicon_files_recursive1(const char *dir, regexec(re, dent->d_name, (size_t)0, NULL, 0) != 0) continue; snprintf(path, MAXPATHLEN-1, "%s/%s", dir, dent->d_name); - if ((res = lstat(path, &st)) != 0){ + if (lstat(path, &st) < 0){ clixon_err(OE_UNIX, errno, "lstat"); goto done; } diff --git a/lib/src/clixon_options.c b/lib/src/clixon_options.c index fcdd33da..839f245c 100644 --- a/lib/src/clixon_options.c +++ b/lib/src/clixon_options.c @@ -125,6 +125,42 @@ static const map_str2int yang_regexp_map[] = { {NULL, -1} }; +/*! Translate between int and string of tree formats + * + * @see enum format_enum + */ +static const map_str2int _FORMATS[] = { + {"xml", FORMAT_XML}, + {"text", FORMAT_TEXT}, + {"json", FORMAT_JSON}, + {"cli", FORMAT_CLI}, + {"netconf", FORMAT_NETCONF}, + {"default", FORMAT_DEFAULT}, + {NULL, -1} +}; + +/*! Translate from numeric format to string representation + * + * @param[in] showas Format value (see enum format_enum) + * @retval str String value + */ +char * +format_int2str(enum format_enum showas) +{ + return (char*)clicon_int2str(_FORMATS, showas); +} + +/*! Translate from string to numeric format representation + * + * @param[in] str String value + * @retval enum Format value (see enum format_enum) + */ +enum format_enum +format_str2int(char *str) +{ + return clicon_str2int(_FORMATS, str); +} + /*! Debug dump config options * * @param[in] h Clixon handle diff --git a/lib/src/clixon_proto.c b/lib/src/clixon_proto.c index 892f0434..0dfb7080 100644 --- a/lib/src/clixon_proto.c +++ b/lib/src/clixon_proto.c @@ -84,42 +84,6 @@ static int _atomicio_sig = 0; -/*! Translate between int and string of tree formats - * - * @see enum format_enum - */ -static const map_str2int _FORMATS[] = { - {"xml", FORMAT_XML}, - {"text", FORMAT_TEXT}, - {"json", FORMAT_JSON}, - {"cli", FORMAT_CLI}, - {"netconf", FORMAT_NETCONF}, - {"default", FORMAT_DEFAULT}, - {NULL, -1} -}; - -/*! Translate from numeric format to string representation - * - * @param[in] showas Format value (see enum format_enum) - * @retval str String value - */ -char * -format_int2str(enum format_enum showas) -{ - return (char*)clicon_int2str(_FORMATS, showas); -} - -/*! Translate from string to numeric format representation - * - * @param[in] str String value - * @retval enum Format value (see enum format_enum) - */ -enum format_enum -format_str2int(char *str) -{ - return clicon_str2int(_FORMATS, str); -} - /*! Given family, addr str, port, return sockaddr and length * * @param[in] addrtype Address family: inet:ipv4-address or inet:ipv6-address